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)))
49 /* #define DEBUG_GRAMMAR 1 */
50 /* #define DEBUG_CONTENT 1 */
51 /* #define DEBUG_TYPE 1 */
52 /* #define DEBUG_VALID 1 */
53 /* #define DEBUG_INTERLEAVE 1 */
54 /* #define DEBUG_LIST 1 */
55 /* #define DEBUG_INCLUDE */
56 /* #define DEBUG_ERROR 1 */
57 /* #define DEBUG_COMPILE 1 */
58 /* #define DEBUG_PROGRESSIVE 1 */
63 xmlGenericError(xmlGenericErrorContext, \
64 "Unimplemented block at %s:%d\n", \
67 typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
68 typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
70 typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
71 typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
73 typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
74 typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
76 typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
77 typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
80 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
81 XML_RELAXNG_COMBINE_CHOICE, /* choice */
82 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
86 XML_RELAXNG_CONTENT_ERROR = -1,
87 XML_RELAXNG_CONTENT_EMPTY = 0,
88 XML_RELAXNG_CONTENT_SIMPLE,
89 XML_RELAXNG_CONTENT_COMPLEX
90 } xmlRelaxNGContentType;
92 typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
93 typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
95 struct _xmlRelaxNGGrammar {
96 xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */
97 xmlRelaxNGGrammarPtr children;/* the children grammar if any */
98 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
99 xmlRelaxNGDefinePtr start; /* <start> content */
100 xmlRelaxNGCombine combine; /* the default combine value */
101 xmlRelaxNGDefinePtr startList;/* list of <start> definitions */
102 xmlHashTablePtr defs; /* define* */
103 xmlHashTablePtr refs; /* references */
108 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
109 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
110 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
111 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
112 XML_RELAXNG_TEXT, /* textual content */
113 XML_RELAXNG_ELEMENT, /* an element */
114 XML_RELAXNG_DATATYPE, /* extenal data type definition */
115 XML_RELAXNG_PARAM, /* extenal data type parameter */
116 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
117 XML_RELAXNG_LIST, /* a list of patterns */
118 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
119 XML_RELAXNG_DEF, /* a definition */
120 XML_RELAXNG_REF, /* reference to a definition */
121 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
122 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
123 XML_RELAXNG_OPTIONAL, /* optional patterns */
124 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
125 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
126 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
127 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
128 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
129 XML_RELAXNG_START /* Used to keep track of starts on grammars */
132 #define IS_NULLABLE (1 << 0)
133 #define IS_NOT_NULLABLE (1 << 1)
134 #define IS_INDETERMINIST (1 << 2)
135 #define IS_MIXED (1 << 3)
136 #define IS_TRIABLE (1 << 4)
137 #define IS_PROCESSED (1 << 5)
138 #define IS_COMPILABLE (1 << 6)
139 #define IS_NOT_COMPILABLE (1 << 7)
141 struct _xmlRelaxNGDefine {
142 xmlRelaxNGType type; /* the type of definition */
143 xmlNodePtr node; /* the node in the source */
144 xmlChar *name; /* the element local name if present */
145 xmlChar *ns; /* the namespace local name if present */
146 xmlChar *value; /* value when available */
147 void *data; /* data lib or specific pointer */
148 xmlRelaxNGDefinePtr content;/* the expected content */
149 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
150 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
151 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
152 xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */
153 xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */
154 short depth; /* used for the cycle detection */
155 short dflags; /* define related flags */
156 xmlRegexpPtr contModel; /* a compiled content model if available */
162 * A RelaxNGs definition
165 void *_private; /* unused by the library for users or bindings */
166 xmlRelaxNGGrammarPtr topgrammar;
169 int idref; /* requires idref checking */
171 xmlHashTablePtr defs; /* define */
172 xmlHashTablePtr refs; /* references */
173 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
174 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
175 int defNr; /* number of defines used */
176 xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */
180 #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
181 #define XML_RELAXNG_IN_ONEORMORE (1 << 1)
182 #define XML_RELAXNG_IN_LIST (1 << 2)
183 #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
184 #define XML_RELAXNG_IN_START (1 << 4)
185 #define XML_RELAXNG_IN_OOMGROUP (1 << 5)
186 #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
187 #define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
188 #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
189 #define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
191 struct _xmlRelaxNGParserCtxt {
192 void *userData; /* user specific data block */
193 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
194 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
195 xmlRelaxNGValidErr err;
197 xmlRelaxNGPtr schema; /* The schema in use */
198 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
199 xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */
200 int flags; /* parser flags */
201 int nbErrors; /* number of errors at parse time */
202 int nbWarnings; /* number of warnings at parse time */
203 const xmlChar *define; /* the current define scope */
204 xmlRelaxNGDefinePtr def; /* the current define */
207 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
209 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
210 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
214 int defNr; /* number of defines used */
215 int defMax; /* number of defines aloocated */
216 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
221 /* the document stack */
222 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
223 int docNr; /* Depth of the parsing stack */
224 int docMax; /* Max depth of the parsing stack */
225 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
227 /* the include stack */
228 xmlRelaxNGIncludePtr inc; /* Current parsed include */
229 int incNr; /* Depth of the include parsing stack */
230 int incMax; /* Max depth of the parsing stack */
231 xmlRelaxNGIncludePtr *incTab; /* array of incs */
233 int idref; /* requires idref checking */
235 /* used to compile content models */
236 xmlAutomataPtr am; /* the automata */
237 xmlAutomataStatePtr state; /* used to build the automata */
240 #define FLAGS_IGNORABLE 1
241 #define FLAGS_NEGATIVE 2
242 #define FLAGS_MIXED_CONTENT 4
245 * xmlRelaxNGInterleaveGroup:
247 * A RelaxNGs partition set associated to lists of definitions
249 typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
250 typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
251 struct _xmlRelaxNGInterleaveGroup {
252 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
253 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
254 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
257 #define IS_DETERMINIST 1
258 #define IS_NEEDCHECK 2
260 * xmlRelaxNGPartitions:
262 * A RelaxNGs partition associated to an interleave group
264 typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
265 typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
266 struct _xmlRelaxNGPartition {
267 int nbgroups; /* number of groups in the partitions */
268 xmlHashTablePtr triage; /* hash table used to direct nodes to the
269 right group when possible */
270 int flags; /* determinist ? */
271 xmlRelaxNGInterleaveGroupPtr *groups;
275 * xmlRelaxNGValidState:
277 * A RelaxNGs validation state
280 typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
281 typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
282 struct _xmlRelaxNGValidState {
283 xmlNodePtr node; /* the current node */
284 xmlNodePtr seq; /* the sequence of children left to validate */
285 int nbAttrs; /* the number of attributes */
286 int maxAttrs; /* the size of attrs */
287 int nbAttrLeft; /* the number of attributes left to validate */
288 xmlChar *value; /* the value when operating on string */
289 xmlChar *endvalue; /* the end value when operating on string */
290 xmlAttrPtr *attrs; /* the array of attributes */
296 * A RelaxNGs container for validation state
298 typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
299 typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
300 struct _xmlRelaxNGStates {
301 int nbState; /* the number of states */
302 int maxState; /* the size of the array */
303 xmlRelaxNGValidStatePtr *tabState;
306 #define ERROR_IS_DUP 1
308 * xmlRelaxNGValidError:
310 * A RelaxNGs validation error
312 typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
313 typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
314 struct _xmlRelaxNGValidError {
315 xmlRelaxNGValidErr err; /* the error number */
316 int flags; /* flags */
317 xmlNodePtr node; /* the current node */
318 xmlNodePtr seq; /* the current child */
319 const xmlChar * arg1; /* first arg */
320 const xmlChar * arg2; /* second arg */
324 * xmlRelaxNGValidCtxt:
326 * A RelaxNGs validation context
329 struct _xmlRelaxNGValidCtxt {
330 void *userData; /* user specific data block */
331 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
332 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
334 xmlRelaxNGPtr schema; /* The schema in use */
335 xmlDocPtr doc; /* the document being validated */
336 int flags; /* validation flags */
337 int depth; /* validation depth */
338 int idref; /* requires idref checking */
339 int errNo; /* the first error found */
342 * Errors accumulated in branches may have to be stacked to be
343 * provided back when it's sure they affect validation.
345 xmlRelaxNGValidErrorPtr err; /* Last error */
346 int errNr; /* Depth of the error stack */
347 int errMax; /* Max depth of the error stack */
348 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
350 xmlRelaxNGValidStatePtr state; /* the current validation state */
351 xmlRelaxNGStatesPtr states; /* the accumulated state list */
353 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
356 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
359 * This is used for "progressive" validation
361 xmlRegExecCtxtPtr elem; /* the current element regexp */
362 int elemNr; /* the number of element validated */
363 int elemMax; /* the max depth of elements */
364 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
365 int pstate; /* progressive state */
366 xmlNodePtr pnode; /* the current node */
367 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
373 * Structure associated to a RelaxNGs document element
375 struct _xmlRelaxNGInclude {
376 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
377 xmlChar *href; /* the normalized href value */
378 xmlDocPtr doc; /* the associated XML document */
379 xmlRelaxNGDefinePtr content;/* the definitions */
380 xmlRelaxNGPtr schema; /* the schema */
384 * xmlRelaxNGDocument:
386 * Structure associated to a RelaxNGs document element
388 struct _xmlRelaxNGDocument {
389 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
390 xmlChar *href; /* the normalized href value */
391 xmlDocPtr doc; /* the associated XML document */
392 xmlRelaxNGDefinePtr content;/* the definitions */
393 xmlRelaxNGPtr schema; /* the schema */
397 /************************************************************************
399 * Preliminary type checking interfaces *
401 ************************************************************************/
403 * xmlRelaxNGTypeHave:
404 * @data: data needed for the library
405 * @type: the type name
406 * @value: the value to check
408 * Function provided by a type library to check if a type is exported
410 * Returns 1 if yes, 0 if no and -1 in case of error.
412 typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type);
415 * xmlRelaxNGTypeCheck:
416 * @data: data needed for the library
417 * @type: the type name
418 * @value: the value to check
419 * @result: place to store the result if needed
421 * Function provided by a type library to check if a value match a type
423 * Returns 1 if yes, 0 if no and -1 in case of error.
425 typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type,
426 const xmlChar *value, void **result,
430 * xmlRelaxNGFacetCheck:
431 * @data: data needed for the library
432 * @type: the type name
433 * @facet: the facet name
434 * @val: the facet value
435 * @strval: the string value
436 * @value: the value to check
438 * Function provided by a type library to check a value facet
440 * Returns 1 if yes, 0 if no and -1 in case of error.
442 typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type,
443 const xmlChar *facet, const xmlChar *val,
444 const xmlChar *strval, void *value);
447 * xmlRelaxNGTypeFree:
448 * @data: data needed for the library
449 * @result: the value to free
451 * Function provided by a type library to free a returned result
453 typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
456 * xmlRelaxNGTypeCompare:
457 * @data: data needed for the library
458 * @type: the type name
459 * @value1: the first value
460 * @value2: the second value
462 * Function provided by a type library to compare two values accordingly
465 * Returns 1 if yes, 0 if no and -1 in case of error.
467 typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type,
468 const xmlChar *value1,
471 const xmlChar *value2,
473 typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
474 typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
475 struct _xmlRelaxNGTypeLibrary {
476 const xmlChar *namespace; /* the datatypeLibrary value */
477 void *data; /* data needed for the library */
478 xmlRelaxNGTypeHave have; /* the export function */
479 xmlRelaxNGTypeCheck check; /* the checking function */
480 xmlRelaxNGTypeCompare comp; /* the compare function */
481 xmlRelaxNGFacetCheck facet; /* the facet check function */
482 xmlRelaxNGTypeFree freef; /* the freeing function */
485 /************************************************************************
487 * Allocation functions *
489 ************************************************************************/
490 static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
491 static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
492 static void xmlRelaxNGNormExtSpace(xmlChar *value);
493 static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
494 static int xmlRelaxNGEqualValidState(
495 xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
496 xmlRelaxNGValidStatePtr state1,
497 xmlRelaxNGValidStatePtr state2);
498 static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
499 xmlRelaxNGValidStatePtr state);
502 * xmlRelaxNGFreeDocument:
503 * @docu: a document structure
505 * Deallocate a RelaxNG document structure.
508 xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
513 if (docu->href != NULL)
515 if (docu->doc != NULL)
516 xmlFreeDoc(docu->doc);
517 if (docu->schema != NULL)
518 xmlRelaxNGFreeInnerSchema(docu->schema);
523 * xmlRelaxNGFreeDocumentList:
524 * @docu: a list of document structure
526 * Deallocate a RelaxNG document structures.
529 xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
531 xmlRelaxNGDocumentPtr next;
532 while (docu != NULL) {
534 xmlRelaxNGFreeDocument(docu);
540 * xmlRelaxNGFreeInclude:
541 * @incl: a include structure
543 * Deallocate a RelaxNG include structure.
546 xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
551 if (incl->href != NULL)
553 if (incl->doc != NULL)
554 xmlFreeDoc(incl->doc);
555 if (incl->schema != NULL)
556 xmlRelaxNGFree(incl->schema);
561 * xmlRelaxNGFreeIncludeList:
562 * @incl: a include structure list
564 * Deallocate a RelaxNG include structure.
567 xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
569 xmlRelaxNGIncludePtr next;
570 while (incl != NULL) {
572 xmlRelaxNGFreeInclude(incl);
578 * xmlRelaxNGNewRelaxNG:
579 * @ctxt: a Relax-NG validation context (optional)
581 * Allocate a new RelaxNG structure.
583 * Returns the newly allocated structure or NULL in case or error
586 xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
590 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
592 if ((ctxt != NULL) && (ctxt->error != NULL))
593 ctxt->error(ctxt->userData, "Out of memory\n");
597 memset(ret, 0, sizeof(xmlRelaxNG));
603 * xmlRelaxNGFreeInnerSchema:
604 * @schema: a schema structure
606 * Deallocate a RelaxNG schema structure.
609 xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
614 if (schema->doc != NULL)
615 xmlFreeDoc(schema->doc);
616 if (schema->defTab != NULL) {
619 for (i = 0;i < schema->defNr;i++)
620 xmlRelaxNGFreeDefine(schema->defTab[i]);
621 xmlFree(schema->defTab);
629 * @schema: a schema structure
631 * Deallocate a RelaxNG structure.
634 xmlRelaxNGFree(xmlRelaxNGPtr schema)
639 if (schema->topgrammar != NULL)
640 xmlRelaxNGFreeGrammar(schema->topgrammar);
641 if (schema->doc != NULL)
642 xmlFreeDoc(schema->doc);
643 if (schema->documents != NULL)
644 xmlRelaxNGFreeDocumentList(schema->documents);
645 if (schema->includes != NULL)
646 xmlRelaxNGFreeIncludeList(schema->includes);
647 if (schema->defTab != NULL) {
650 for (i = 0;i < schema->defNr;i++)
651 xmlRelaxNGFreeDefine(schema->defTab[i]);
652 xmlFree(schema->defTab);
659 * xmlRelaxNGNewGrammar:
660 * @ctxt: a Relax-NG validation context (optional)
662 * Allocate a new RelaxNG grammar.
664 * Returns the newly allocated structure or NULL in case or error
666 static xmlRelaxNGGrammarPtr
667 xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
669 xmlRelaxNGGrammarPtr ret;
671 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
673 if ((ctxt != NULL) && (ctxt->error != NULL))
674 ctxt->error(ctxt->userData, "Out of memory\n");
678 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
684 * xmlRelaxNGFreeGrammar:
685 * @grammar: a grammar structure
687 * Deallocate a RelaxNG grammar structure.
690 xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
695 if (grammar->children != NULL) {
696 xmlRelaxNGFreeGrammar(grammar->children);
698 if (grammar->next != NULL) {
699 xmlRelaxNGFreeGrammar(grammar->next);
701 if (grammar->refs != NULL) {
702 xmlHashFree(grammar->refs, NULL);
704 if (grammar->defs != NULL) {
705 xmlHashFree(grammar->defs, NULL);
712 * xmlRelaxNGNewDefine:
713 * @ctxt: a Relax-NG validation context
714 * @node: the node in the input document.
716 * Allocate a new RelaxNG define.
718 * Returns the newly allocated structure or NULL in case or error
720 static xmlRelaxNGDefinePtr
721 xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
723 xmlRelaxNGDefinePtr ret;
725 if (ctxt->defMax == 0) {
728 ctxt->defTab = (xmlRelaxNGDefinePtr *)
729 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
730 if (ctxt->defTab == NULL) {
731 if ((ctxt != NULL) && (ctxt->error != NULL))
732 ctxt->error(ctxt->userData, "Out of memory\n");
736 } else if (ctxt->defMax <= ctxt->defNr) {
737 xmlRelaxNGDefinePtr *tmp;
739 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
740 ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
742 if ((ctxt != NULL) && (ctxt->error != NULL))
743 ctxt->error(ctxt->userData, "Out of memory\n");
749 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
751 if ((ctxt != NULL) && (ctxt->error != NULL))
752 ctxt->error(ctxt->userData, "Out of memory\n");
756 memset(ret, 0, sizeof(xmlRelaxNGDefine));
757 ctxt->defTab[ctxt->defNr++] = ret;
764 * xmlRelaxNGFreePartition:
765 * @partitions: a partition set structure
767 * Deallocate RelaxNG partition set structures.
770 xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) {
771 xmlRelaxNGInterleaveGroupPtr group;
774 if (partitions != NULL) {
775 if (partitions->groups != NULL) {
776 for (j = 0;j < partitions->nbgroups;j++) {
777 group = partitions->groups[j];
779 if (group->defs != NULL)
780 xmlFree(group->defs);
781 if (group->attrs != NULL)
782 xmlFree(group->attrs);
786 xmlFree(partitions->groups);
788 if (partitions->triage != NULL) {
789 xmlHashFree(partitions->triage, NULL);
795 * xmlRelaxNGFreeDefine:
796 * @define: a define structure
798 * Deallocate a RelaxNG define structure.
801 xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
806 if ((define->type == XML_RELAXNG_VALUE) &&
807 (define->attrs != NULL)) {
808 xmlRelaxNGTypeLibraryPtr lib;
810 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
811 if ((lib != NULL) && (lib->freef != NULL))
812 lib->freef(lib->data, (void *) define->attrs);
814 if ((define->data != NULL) &&
815 (define->type == XML_RELAXNG_INTERLEAVE))
816 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
817 if ((define->data != NULL) &&
818 (define->type == XML_RELAXNG_CHOICE))
819 xmlHashFree((xmlHashTablePtr) define->data, NULL);
820 if (define->name != NULL)
821 xmlFree(define->name);
822 if (define->ns != NULL)
824 if (define->value != NULL)
825 xmlFree(define->value);
826 if (define->contModel != NULL)
827 xmlRegFreeRegexp(define->contModel);
832 * xmlRelaxNGNewStates:
833 * @ctxt: a Relax-NG validation context
834 * @size: the default size for the container
836 * Allocate a new RelaxNG validation state container
838 * Returns the newly allocated structure or NULL in case or error
840 static xmlRelaxNGStatesPtr
841 xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
843 xmlRelaxNGStatesPtr ret;
845 if ((ctxt != NULL) &&
846 (ctxt->freeState != NULL) &&
847 (ctxt->freeStatesNr > 0)) {
848 ctxt->freeStatesNr--;
849 ret = ctxt->freeStates[ctxt->freeStatesNr];
853 if (size < 16) size = 16;
855 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
856 (size - 1) * sizeof(xmlRelaxNGValidStatePtr));
858 if ((ctxt != NULL) && (ctxt->error != NULL))
859 ctxt->error(ctxt->userData, "Out of memory\n");
863 ret->maxState = size;
864 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc(
865 (size) * sizeof(xmlRelaxNGValidStatePtr));
866 if (ret->tabState == NULL) {
867 if ((ctxt != NULL) && (ctxt->error != NULL))
868 ctxt->error(ctxt->userData, "Out of memory\n");
869 xmlFree(ret->tabState);
876 * xmlRelaxNGAddStateUniq:
877 * @ctxt: a Relax-NG validation context
878 * @states: the states container
879 * @state: the validation state
881 * Add a RelaxNG validation state to the container without checking
884 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
887 xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
888 xmlRelaxNGStatesPtr states,
889 xmlRelaxNGValidStatePtr state)
894 if (states->nbState >= states->maxState) {
895 xmlRelaxNGValidStatePtr *tmp;
898 size = states->maxState * 2;
899 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
900 (size) * sizeof(xmlRelaxNGValidStatePtr));
902 if ((ctxt != NULL) && (ctxt->error != NULL))
903 ctxt->error(ctxt->userData, "Out of memory\n");
906 states->tabState = tmp;
907 states->maxState = size;
909 states->tabState[states->nbState++] = state;
914 * xmlRelaxNGAddState:
915 * @ctxt: a Relax-NG validation context
916 * @states: the states container
917 * @state: the validation state
919 * Add a RelaxNG validation state to the container
921 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
924 xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states,
925 xmlRelaxNGValidStatePtr state)
932 if (states->nbState >= states->maxState) {
933 xmlRelaxNGValidStatePtr *tmp;
936 size = states->maxState * 2;
937 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
938 (size) * sizeof(xmlRelaxNGValidStatePtr));
940 if ((ctxt != NULL) && (ctxt->error != NULL))
941 ctxt->error(ctxt->userData, "Out of memory\n");
944 states->tabState = tmp;
945 states->maxState = size;
947 for (i = 0;i < states->nbState;i++) {
948 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
949 xmlRelaxNGFreeValidState(ctxt, state);
953 states->tabState[states->nbState++] = state;
958 * xmlRelaxNGFreeStates:
959 * @ctxt: a Relax-NG validation context
960 * @states: teh container
962 * Free a RelaxNG validation state container
965 xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
966 xmlRelaxNGStatesPtr states)
970 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
971 ctxt->freeStatesMax = 40;
972 ctxt->freeStatesNr = 0;
973 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
974 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
975 if (ctxt->freeStates == NULL) {
976 if ((ctxt != NULL) && (ctxt->error != NULL))
977 ctxt->error(ctxt->userData, "Out of memory\n");
979 } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
980 xmlRelaxNGStatesPtr *tmp;
982 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
983 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
985 if ((ctxt != NULL) && (ctxt->error != NULL))
986 ctxt->error(ctxt->userData, "Out of memory\n");
987 xmlFree(states->tabState);
991 ctxt->freeStates = tmp;
992 ctxt->freeStatesMax *= 2;
994 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
995 xmlFree(states->tabState);
998 ctxt->freeStates[ctxt->freeStatesNr++] = states;
1003 * xmlRelaxNGNewValidState:
1004 * @ctxt: a Relax-NG validation context
1005 * @node: the current node or NULL for the document
1007 * Allocate a new RelaxNG validation state
1009 * Returns the newly allocated structure or NULL in case or error
1011 static xmlRelaxNGValidStatePtr
1012 xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1014 xmlRelaxNGValidStatePtr ret;
1016 xmlAttrPtr attrs[MAX_ATTR];
1018 xmlNodePtr root = NULL;
1021 root = xmlDocGetRootElement(ctxt->doc);
1025 attr = node->properties;
1026 while (attr != NULL) {
1027 if (nbAttrs < MAX_ATTR)
1028 attrs[nbAttrs++] = attr;
1034 if ((ctxt->freeState != NULL) &&
1035 (ctxt->freeState->nbState > 0)) {
1036 ctxt->freeState->nbState--;
1037 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1039 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1041 if ((ctxt != NULL) && (ctxt->error != NULL))
1042 ctxt->error(ctxt->userData, "Out of memory\n");
1045 memset(ret, 0, sizeof(xmlRelaxNGValidState));
1048 ret->endvalue = NULL;
1050 ret->node = (xmlNodePtr) ctxt->doc;
1054 ret->seq = node->children;
1058 if (ret->attrs == NULL) {
1059 if (nbAttrs < 4) ret->maxAttrs = 4;
1060 else ret->maxAttrs = nbAttrs;
1061 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1062 sizeof(xmlAttrPtr));
1063 if (ret->attrs == NULL) {
1064 if ((ctxt != NULL) && (ctxt->error != NULL))
1065 ctxt->error(ctxt->userData, "Out of memory\n");
1068 } else if (ret->maxAttrs < nbAttrs) {
1071 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1072 sizeof(xmlAttrPtr));
1074 if ((ctxt != NULL) && (ctxt->error != NULL))
1075 ctxt->error(ctxt->userData, "Out of memory\n");
1080 ret->nbAttrs = nbAttrs;
1081 if (nbAttrs < MAX_ATTR) {
1082 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1084 attr = node->properties;
1086 while (attr != NULL) {
1087 ret->attrs[nbAttrs++] = attr;
1092 ret->nbAttrLeft = ret->nbAttrs;
1097 * xmlRelaxNGCopyValidState:
1098 * @ctxt: a Relax-NG validation context
1099 * @state: a validation state
1101 * Copy the validation state
1103 * Returns the newly allocated structure or NULL in case or error
1105 static xmlRelaxNGValidStatePtr
1106 xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1107 xmlRelaxNGValidStatePtr state)
1109 xmlRelaxNGValidStatePtr ret;
1110 unsigned int maxAttrs;
1115 if ((ctxt->freeState != NULL) &&
1116 (ctxt->freeState->nbState > 0)) {
1117 ctxt->freeState->nbState--;
1118 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1120 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1122 if ((ctxt != NULL) && (ctxt->error != NULL))
1123 ctxt->error(ctxt->userData, "Out of memory\n");
1126 memset(ret, 0, sizeof(xmlRelaxNGValidState));
1129 maxAttrs = ret->maxAttrs;
1130 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1132 ret->maxAttrs = maxAttrs;
1133 if (state->nbAttrs > 0) {
1134 if (ret->attrs == NULL) {
1135 ret->maxAttrs = state->maxAttrs;
1136 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1137 sizeof(xmlAttrPtr));
1138 if (ret->attrs == NULL) {
1139 if ((ctxt != NULL) && (ctxt->error != NULL))
1140 ctxt->error(ctxt->userData, "Out of memory\n");
1144 } else if (ret->maxAttrs < state->nbAttrs) {
1147 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1148 sizeof(xmlAttrPtr));
1150 if ((ctxt != NULL) && (ctxt->error != NULL))
1151 ctxt->error(ctxt->userData, "Out of memory\n");
1155 ret->maxAttrs = state->maxAttrs;
1157 memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr));
1163 * xmlRelaxNGEqualValidState:
1164 * @ctxt: a Relax-NG validation context
1165 * @state1: a validation state
1166 * @state2: a validation state
1168 * Compare the validation states for equality
1170 * Returns 1 if equald, 0 otherwise
1173 xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1174 xmlRelaxNGValidStatePtr state1,
1175 xmlRelaxNGValidStatePtr state2)
1179 if ((state1 == NULL) || (state2 == NULL))
1181 if (state1 == state2)
1183 if (state1->node != state2->node)
1185 if (state1->seq != state2->seq)
1187 if (state1->nbAttrLeft != state2->nbAttrLeft)
1189 if (state1->nbAttrs != state2->nbAttrs)
1191 if (state1->endvalue != state2->endvalue)
1193 if ((state1->value != state2->value) &&
1194 (!xmlStrEqual(state1->value, state2->value)))
1196 for (i = 0;i < state1->nbAttrs;i++) {
1197 if (state1->attrs[i] != state2->attrs[i])
1204 * xmlRelaxNGFreeValidState:
1205 * @state: a validation state structure
1207 * Deallocate a RelaxNG validation state structure.
1210 xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1211 xmlRelaxNGValidStatePtr state)
1216 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1217 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1219 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1220 if (state->attrs != NULL)
1221 xmlFree(state->attrs);
1224 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1228 /************************************************************************
1230 * Document functions *
1232 ************************************************************************/
1233 static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1237 * xmlRelaxNGIncludePush:
1238 * @ctxt: the parser context
1239 * @value: the element doc
1241 * Pushes a new include on top of the include stack
1243 * Returns 0 in case of error, the index in the stack otherwise
1246 xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1247 xmlRelaxNGIncludePtr value)
1249 if (ctxt->incTab == NULL) {
1252 ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc(
1253 ctxt->incMax * sizeof(ctxt->incTab[0]));
1254 if (ctxt->incTab == NULL) {
1255 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1259 if (ctxt->incNr >= ctxt->incMax) {
1262 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1264 sizeof(ctxt->incTab[0]));
1265 if (ctxt->incTab == NULL) {
1266 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1270 ctxt->incTab[ctxt->incNr] = value;
1272 return (ctxt->incNr++);
1276 * xmlRelaxNGIncludePop:
1277 * @ctxt: the parser context
1279 * Pops the top include from the include stack
1281 * Returns the include just removed
1283 static xmlRelaxNGIncludePtr
1284 xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1286 xmlRelaxNGIncludePtr ret;
1288 if (ctxt->incNr <= 0)
1291 if (ctxt->incNr > 0)
1292 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1295 ret = ctxt->incTab[ctxt->incNr];
1296 ctxt->incTab[ctxt->incNr] = 0;
1301 * xmlRelaxNGRemoveRedefine:
1302 * @ctxt: the parser context
1303 * @URL: the normalized URL
1304 * @target: the included target
1305 * @name: the define name to eliminate
1307 * Applies the elimination algorithm of 4.7
1309 * Returns 0 in case of error, 1 in case of success.
1312 xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1313 const xmlChar *URL ATTRIBUTE_UNUSED,
1314 xmlNodePtr target, const xmlChar *name) {
1316 xmlNodePtr tmp, tmp2;
1319 #ifdef DEBUG_INCLUDE
1321 xmlGenericError(xmlGenericErrorContext,
1322 "Elimination of <include> start from %s\n", URL);
1324 xmlGenericError(xmlGenericErrorContext,
1325 "Elimination of <include> define %s from %s\n", name, URL);
1328 while (tmp != NULL) {
1330 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1334 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1335 name2 = xmlGetProp(tmp, BAD_CAST "name");
1336 xmlRelaxNGNormExtSpace(name2);
1337 if (name2 != NULL) {
1338 if (xmlStrEqual(name, name2)) {
1345 } else if (IS_RELAXNG(tmp, "include")) {
1346 xmlChar *href = NULL;
1347 xmlRelaxNGDocumentPtr inc = tmp->_private;
1349 if ((inc != NULL) && (inc->doc != NULL) &&
1350 (inc->doc->children != NULL)) {
1352 if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) {
1353 #ifdef DEBUG_INCLUDE
1354 href = xmlGetProp(tmp, BAD_CAST "href");
1356 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1357 inc->doc->children->children, name) == 1) {
1371 * xmlRelaxNGLoadInclude:
1372 * @ctxt: the parser context
1373 * @URL: the normalized URL
1374 * @node: the include node.
1375 * @ns: the namespace passed from the context.
1377 * First lookup if the document is already loaded into the parser context,
1378 * check against recursion. If not found the resource is loaded and
1379 * the content is preprocessed before being returned back to the caller.
1381 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1383 static xmlRelaxNGIncludePtr
1384 xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
1385 xmlNodePtr node, const xmlChar *ns) {
1386 xmlRelaxNGIncludePtr ret = NULL;
1389 xmlNodePtr root, cur;
1391 #ifdef DEBUG_INCLUDE
1392 xmlGenericError(xmlGenericErrorContext,
1393 "xmlRelaxNGLoadInclude(%s)\n", URL);
1397 * check against recursion in the stack
1399 for (i = 0;i < ctxt->incNr;i++) {
1400 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1401 if (ctxt->error != NULL)
1402 ctxt->error(ctxt->userData,
1403 "Detected an Include recursion for %s\n",
1413 doc = xmlParseFile((const char *) URL);
1415 if (ctxt->error != NULL)
1416 ctxt->error(ctxt->userData,
1417 "xmlRelaxNG: could not load %s\n", URL);
1422 #ifdef DEBUG_INCLUDE
1423 xmlGenericError(xmlGenericErrorContext,
1424 "Parsed %s Okay\n", URL);
1428 * Allocate the document structures and register it first.
1430 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1432 if (ctxt->error != NULL)
1433 ctxt->error(ctxt->userData,
1434 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1439 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1441 ret->href = xmlStrdup(URL);
1442 ret->next = ctxt->includes;
1443 ctxt->includes = ret;
1446 * transmit the ns if needed
1449 root = xmlDocGetRootElement(doc);
1451 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1452 xmlSetProp(root, BAD_CAST"ns", ns);
1458 * push it on the stack
1460 xmlRelaxNGIncludePush(ctxt, ret);
1463 * Some preprocessing of the document content, this include recursing
1464 * in the include stack.
1466 #ifdef DEBUG_INCLUDE
1467 xmlGenericError(xmlGenericErrorContext,
1468 "cleanup of %s\n", URL);
1471 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1478 * Pop up the include from the stack
1480 xmlRelaxNGIncludePop(ctxt);
1482 #ifdef DEBUG_INCLUDE
1483 xmlGenericError(xmlGenericErrorContext,
1484 "Checking of %s\n", URL);
1487 * Check that the top element is a grammar
1489 root = xmlDocGetRootElement(doc);
1491 if (ctxt->error != NULL)
1492 ctxt->error(ctxt->userData,
1493 "xmlRelaxNG: included document is empty %s\n", URL);
1497 if (!IS_RELAXNG(root, "grammar")) {
1498 if (ctxt->error != NULL)
1499 ctxt->error(ctxt->userData,
1500 "xmlRelaxNG: included document %s root is not a grammar\n",
1507 * Elimination of redefined rules in the include.
1509 cur = node->children;
1510 while (cur != NULL) {
1511 if (IS_RELAXNG(cur, "start")) {
1514 found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1516 if (ctxt->error != NULL)
1517 ctxt->error(ctxt->userData,
1518 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1522 } else if (IS_RELAXNG(cur, "define")) {
1525 name = xmlGetProp(cur, BAD_CAST "name");
1527 if (ctxt->error != NULL)
1528 ctxt->error(ctxt->userData,
1529 "xmlRelaxNG: include %s has define without name\n",
1535 xmlRelaxNGNormExtSpace(name);
1536 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1537 root->children, name);
1539 if (ctxt->error != NULL)
1540 ctxt->error(ctxt->userData,
1541 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1556 * xmlRelaxNGValidErrorPush:
1557 * @ctxt: the validation context
1558 * @err: the error code
1559 * @arg1: the first string argument
1560 * @arg2: the second string argument
1561 * @dup: arg need to be duplicated
1563 * Pushes a new error on top of the error stack
1565 * Returns 0 in case of error, the index in the stack otherwise
1568 xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
1569 const xmlChar *arg1, const xmlChar *arg2, int dup)
1571 xmlRelaxNGValidErrorPtr cur;
1573 xmlGenericError(xmlGenericErrorContext,
1574 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
1576 if (ctxt->errTab == NULL) {
1579 ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc(
1580 ctxt->errMax * sizeof(xmlRelaxNGValidError));
1581 if (ctxt->errTab == NULL) {
1582 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1587 if (ctxt->errNr >= ctxt->errMax) {
1590 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
1591 ctxt->errMax * sizeof(xmlRelaxNGValidError));
1592 if (ctxt->errTab == NULL) {
1593 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1596 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1598 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
1599 (ctxt->err->node == ctxt->state->node) &&
1600 (ctxt->err->err == err))
1601 return(ctxt->errNr);
1602 cur = &ctxt->errTab[ctxt->errNr];
1605 cur->arg1 = xmlStrdup(arg1);
1606 cur->arg2 = xmlStrdup(arg2);
1607 cur->flags = ERROR_IS_DUP;
1613 if (ctxt->state != NULL) {
1614 cur->node = ctxt->state->node;
1615 cur->seq = ctxt->state->seq;
1621 return (ctxt->errNr++);
1625 * xmlRelaxNGValidErrorPop:
1626 * @ctxt: the validation context
1628 * Pops the top error from the error stack
1631 xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1633 xmlRelaxNGValidErrorPtr cur;
1635 if (ctxt->errNr <= 0) {
1640 if (ctxt->errNr > 0)
1641 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1644 cur = &ctxt->errTab[ctxt->errNr];
1645 if (cur->flags & ERROR_IS_DUP) {
1646 if (cur->arg1 != NULL)
1647 xmlFree((xmlChar *)cur->arg1);
1649 if (cur->arg2 != NULL)
1650 xmlFree((xmlChar *)cur->arg2);
1657 * xmlRelaxNGDocumentPush:
1658 * @ctxt: the parser context
1659 * @value: the element doc
1661 * Pushes a new doc on top of the doc stack
1663 * Returns 0 in case of error, the index in the stack otherwise
1666 xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1667 xmlRelaxNGDocumentPtr value)
1669 if (ctxt->docTab == NULL) {
1672 ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc(
1673 ctxt->docMax * sizeof(ctxt->docTab[0]));
1674 if (ctxt->docTab == NULL) {
1675 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1679 if (ctxt->docNr >= ctxt->docMax) {
1682 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1684 sizeof(ctxt->docTab[0]));
1685 if (ctxt->docTab == NULL) {
1686 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1690 ctxt->docTab[ctxt->docNr] = value;
1692 return (ctxt->docNr++);
1696 * xmlRelaxNGDocumentPop:
1697 * @ctxt: the parser context
1699 * Pops the top doc from the doc stack
1701 * Returns the doc just removed
1703 static xmlRelaxNGDocumentPtr
1704 xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1706 xmlRelaxNGDocumentPtr ret;
1708 if (ctxt->docNr <= 0)
1711 if (ctxt->docNr > 0)
1712 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1715 ret = ctxt->docTab[ctxt->docNr];
1716 ctxt->docTab[ctxt->docNr] = 0;
1721 * xmlRelaxNGLoadExternalRef:
1722 * @ctxt: the parser context
1723 * @URL: the normalized URL
1724 * @ns: the inherited ns if any
1726 * First lookup if the document is already loaded into the parser context,
1727 * check against recursion. If not found the resource is loaded and
1728 * the content is preprocessed before being returned back to the caller.
1730 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1732 static xmlRelaxNGDocumentPtr
1733 xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
1734 const xmlChar *ns) {
1735 xmlRelaxNGDocumentPtr ret = NULL;
1741 * check against recursion in the stack
1743 for (i = 0;i < ctxt->docNr;i++) {
1744 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1745 if (ctxt->error != NULL)
1746 ctxt->error(ctxt->userData,
1747 "Detected an externalRef recursion for %s\n",
1757 doc = xmlParseFile((const char *) URL);
1759 if (ctxt->error != NULL)
1760 ctxt->error(ctxt->userData,
1761 "xmlRelaxNG: could not load %s\n", URL);
1767 * Allocate the document structures and register it first.
1769 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1771 if (ctxt->error != NULL)
1772 ctxt->error(ctxt->userData,
1773 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1778 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1780 ret->href = xmlStrdup(URL);
1781 ret->next = ctxt->documents;
1782 ctxt->documents = ret;
1785 * transmit the ns if needed
1788 root = xmlDocGetRootElement(doc);
1790 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1791 xmlSetProp(root, BAD_CAST"ns", ns);
1797 * push it on the stack and register it in the hash table
1799 xmlRelaxNGDocumentPush(ctxt, ret);
1802 * Some preprocessing of the document content
1804 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1810 xmlRelaxNGDocumentPop(ctxt);
1815 /************************************************************************
1819 ************************************************************************/
1821 #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
1822 #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
1823 #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
1824 #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
1825 #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
1828 xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) {
1832 case XML_RELAXNG_EMPTY: return("empty");
1833 case XML_RELAXNG_NOT_ALLOWED: return("notAllowed");
1834 case XML_RELAXNG_EXCEPT: return("except");
1835 case XML_RELAXNG_TEXT: return("text");
1836 case XML_RELAXNG_ELEMENT: return("element");
1837 case XML_RELAXNG_DATATYPE: return("datatype");
1838 case XML_RELAXNG_VALUE: return("value");
1839 case XML_RELAXNG_LIST: return("list");
1840 case XML_RELAXNG_ATTRIBUTE: return("attribute");
1841 case XML_RELAXNG_DEF: return("def");
1842 case XML_RELAXNG_REF: return("ref");
1843 case XML_RELAXNG_EXTERNALREF: return("externalRef");
1844 case XML_RELAXNG_PARENTREF: return("parentRef");
1845 case XML_RELAXNG_OPTIONAL: return("optional");
1846 case XML_RELAXNG_ZEROORMORE: return("zeroOrMore");
1847 case XML_RELAXNG_ONEORMORE: return("oneOrMore");
1848 case XML_RELAXNG_CHOICE: return("choice");
1849 case XML_RELAXNG_GROUP: return("group");
1850 case XML_RELAXNG_INTERLEAVE: return("interleave");
1851 case XML_RELAXNG_START: return("start");
1852 case XML_RELAXNG_NOOP: return("noop");
1853 case XML_RELAXNG_PARAM: return("param");
1859 * xmlRelaxNGGetErrorString:
1860 * @err: the error code
1861 * @arg1: the first string argument
1862 * @arg2: the second string argument
1864 * computes a formatted error string for the given error code and args
1866 * Returns the error string, it must be deallocated by the caller
1869 xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1,
1870 const xmlChar *arg2) {
1880 case XML_RELAXNG_OK:
1882 case XML_RELAXNG_ERR_MEMORY:
1883 return(xmlCharStrdup("out of memory"));
1884 case XML_RELAXNG_ERR_TYPE:
1885 snprintf(msg, 1000, "failed to validate type %s", arg1);
1887 case XML_RELAXNG_ERR_TYPEVAL:
1888 snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2);
1890 case XML_RELAXNG_ERR_DUPID:
1891 snprintf(msg, 1000, "ID %s redefined", arg1);
1893 case XML_RELAXNG_ERR_TYPECMP:
1894 snprintf(msg, 1000, "failed to compare type %s", arg1);
1896 case XML_RELAXNG_ERR_NOSTATE:
1897 return(xmlCharStrdup("Internal error: no state"));
1898 case XML_RELAXNG_ERR_NODEFINE:
1899 return(xmlCharStrdup("Internal error: no define"));
1900 case XML_RELAXNG_ERR_INTERNAL:
1901 snprintf(msg, 1000, "Internal error: %s", arg1);
1903 case XML_RELAXNG_ERR_LISTEXTRA:
1904 snprintf(msg, 1000, "Extra data in list: %s", arg1);
1906 case XML_RELAXNG_ERR_INTERNODATA:
1907 return(xmlCharStrdup("Internal: interleave block has no data"));
1908 case XML_RELAXNG_ERR_INTERSEQ:
1909 return(xmlCharStrdup("Invalid sequence in interleave"));
1910 case XML_RELAXNG_ERR_INTEREXTRA:
1911 snprintf(msg, 1000, "Extra element %s in interleave", arg1);
1913 case XML_RELAXNG_ERR_ELEMNAME:
1914 snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2);
1916 case XML_RELAXNG_ERR_ELEMNONS:
1917 snprintf(msg, 1000, "Expecting a namespace for element %s", arg1);
1919 case XML_RELAXNG_ERR_ELEMWRONGNS:
1920 snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s",
1923 case XML_RELAXNG_ERR_ELEMWRONG:
1924 snprintf(msg, 1000, "Did not expect element %s there",
1927 case XML_RELAXNG_ERR_TEXTWRONG:
1928 snprintf(msg, 1000, "Did not expect text in element %s content",
1931 case XML_RELAXNG_ERR_ELEMEXTRANS:
1932 snprintf(msg, 1000, "Expecting no namespace for element %s", arg1);
1934 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
1935 snprintf(msg, 1000, "Expecting element %s to be empty", arg1);
1937 case XML_RELAXNG_ERR_NOELEM:
1938 snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1);
1940 case XML_RELAXNG_ERR_NOTELEM:
1941 return(xmlCharStrdup("Expecting an element got text"));
1942 case XML_RELAXNG_ERR_ATTRVALID:
1943 snprintf(msg, 1000, "Element %s failed to validate attributes",
1946 case XML_RELAXNG_ERR_CONTENTVALID:
1947 snprintf(msg, 1000, "Element %s failed to validate content",
1950 case XML_RELAXNG_ERR_EXTRACONTENT:
1951 snprintf(msg, 1000, "Element %s has extra content: %s",
1954 case XML_RELAXNG_ERR_INVALIDATTR:
1955 snprintf(msg, 1000, "Invalid attribute %s for element %s",
1958 case XML_RELAXNG_ERR_LACKDATA:
1959 snprintf(msg, 1000, "Datatype element %s contains no data",
1962 case XML_RELAXNG_ERR_DATAELEM:
1963 snprintf(msg, 1000, "Datatype element %s has child elements",
1966 case XML_RELAXNG_ERR_VALELEM:
1967 snprintf(msg, 1000, "Value element %s has child elements",
1970 case XML_RELAXNG_ERR_LISTELEM:
1971 snprintf(msg, 1000, "List element %s has child elements",
1974 case XML_RELAXNG_ERR_DATATYPE:
1975 snprintf(msg, 1000, "Error validating datatype %s",
1978 case XML_RELAXNG_ERR_VALUE:
1979 snprintf(msg, 1000, "Error validating value %s",
1982 case XML_RELAXNG_ERR_LIST:
1983 return(xmlCharStrdup("Error validating list"));
1984 case XML_RELAXNG_ERR_NOGRAMMAR:
1985 return(xmlCharStrdup("No top grammar defined"));
1986 case XML_RELAXNG_ERR_EXTRADATA:
1987 return(xmlCharStrdup("Extra data in the document"));
1989 return(xmlCharStrdup("Unknown error !"));
1992 snprintf(msg, 1000, "Unknown error code %d", err);
1995 return(xmlStrdup((xmlChar *) msg));
1999 * xmlRelaxNGValidErrorContext:
2000 * @ctxt: the validation context
2002 * @child: the node child generating the problem.
2004 * Dump informations about the kocation of the error in the instance
2007 xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node,
2011 const xmlChar *file = NULL;
2012 const xmlChar *name = NULL;
2013 const char *type = "error";
2015 if ((ctxt == NULL) || (ctxt->error == NULL))
2022 if ((node->type == XML_DOCUMENT_NODE) ||
2023 (node->type == XML_HTML_DOCUMENT_NODE)) {
2024 xmlDocPtr doc = (xmlDocPtr) node;
2029 * Try to find contextual informations to report
2031 if (node->type == XML_ELEMENT_NODE) {
2032 line = (long) node->content;
2033 } else if ((node->prev != NULL) &&
2034 (node->prev->type == XML_ELEMENT_NODE)) {
2035 line = (long) node->prev->content;
2036 } else if ((node->parent != NULL) &&
2037 (node->parent->type == XML_ELEMENT_NODE)) {
2038 line = (long) node->parent->content;
2040 if ((node->doc != NULL) && (node->doc->URL != NULL))
2041 file = node->doc->URL;
2042 if (node->name != NULL)
2047 type = "RNG validity error";
2049 if ((file != NULL) && (line != 0) && (name != NULL))
2050 ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n",
2051 type, file, line, name);
2052 else if ((file != NULL) && (name != NULL))
2053 ctxt->error(ctxt->userData, "%s: file %s element %s\n",
2055 else if ((file != NULL) && (line != 0))
2056 ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line);
2057 else if (file != NULL)
2058 ctxt->error(ctxt->userData, "%s: file %s\n", type, file);
2059 else if (name != NULL)
2060 ctxt->error(ctxt->userData, "%s: element %s\n", type, name);
2062 ctxt->error(ctxt->userData, "%s\n", type);
2066 * xmlRelaxNGShowValidError:
2067 * @ctxt: the validation context
2068 * @err: the error number
2070 * @child: the node child generating the problem.
2071 * @arg1: the first argument
2072 * @arg2: the second argument
2074 * Show a validation error.
2077 xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
2078 xmlNodePtr node, xmlNodePtr child,
2079 const xmlChar *arg1, const xmlChar *arg2)
2083 if (ctxt->error == NULL)
2087 xmlGenericError(xmlGenericErrorContext,
2088 "Show error %d\n", err);
2090 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2094 if (ctxt->errNo == XML_RELAXNG_OK)
2096 xmlRelaxNGValidErrorContext(ctxt, node, child);
2097 ctxt->error(ctxt->userData, "%s\n", msg);
2102 * xmlRelaxNGPopErrors:
2103 * @ctxt: the validation context
2104 * @level: the error level in the stack
2106 * pop and discard all errors until the given level is reached
2109 xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) {
2111 xmlRelaxNGValidErrorPtr err;
2114 xmlGenericError(xmlGenericErrorContext,
2115 "Pop errors till level %d\n", level);
2117 for (i = level;i < ctxt->errNr;i++) {
2118 err = &ctxt->errTab[i];
2119 if (err->flags & ERROR_IS_DUP) {
2120 if (err->arg1 != NULL)
2121 xmlFree((xmlChar *)err->arg1);
2123 if (err->arg2 != NULL)
2124 xmlFree((xmlChar *)err->arg2);
2129 ctxt->errNr = level;
2130 if (ctxt->errNr <= 0)
2134 * xmlRelaxNGDumpValidError:
2135 * @ctxt: the validation context
2137 * Show all validation error over a given index.
2140 xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) {
2142 xmlRelaxNGValidErrorPtr err, dup;
2145 xmlGenericError(xmlGenericErrorContext,
2146 "Dumping error stack %d errors\n", ctxt->errNr);
2148 for (i = 0, k = 0;i < ctxt->errNr;i++) {
2149 err = &ctxt->errTab[i];
2150 if (k < MAX_ERROR) {
2151 for (j = 0;j < i;j++) {
2152 dup = &ctxt->errTab[j];
2153 if ((err->err == dup->err) && (err->node == dup->node) &&
2154 (xmlStrEqual(err->arg1, dup->arg1)) &&
2155 (xmlStrEqual(err->arg2, dup->arg2))) {
2159 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2160 err->arg1, err->arg2);
2164 if (err->flags & ERROR_IS_DUP) {
2165 if (err->arg1 != NULL)
2166 xmlFree((xmlChar *)err->arg1);
2168 if (err->arg2 != NULL)
2169 xmlFree((xmlChar *)err->arg2);
2177 * xmlRelaxNGAddValidError:
2178 * @ctxt: the validation context
2179 * @err: the error number
2180 * @arg1: the first argument
2181 * @arg2: the second argument
2182 * @dup: need to dup the args
2184 * Register a validation error, either generating it if it's sure
2185 * or stacking it for later handling if unsure.
2188 xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
2189 const xmlChar *arg1, const xmlChar *arg2, int dup)
2191 if ((ctxt == NULL) || (ctxt->error == NULL))
2195 xmlGenericError(xmlGenericErrorContext,
2196 "Adding error %d\n", err);
2199 * generate the error directly
2201 if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) {
2202 xmlNodePtr node, seq;
2204 * Flush first any stacked error which might be the
2205 * real cause of the problem.
2207 if (ctxt->errNr != 0)
2208 xmlRelaxNGDumpValidError(ctxt);
2209 if (ctxt->state != NULL) {
2210 node = ctxt->state->node;
2211 seq = ctxt->state->seq;
2215 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2218 * Stack the error for later processing if needed
2221 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
2226 /************************************************************************
2228 * Type library hooks *
2230 ************************************************************************/
2231 static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2232 const xmlChar *str);
2235 * xmlRelaxNGSchemaTypeHave:
2236 * @data: data needed for the library
2237 * @type: the type name
2239 * Check if the given type is provided by
2240 * the W3C XMLSchema Datatype library.
2242 * Returns 1 if yes, 0 if no and -1 in case of error.
2245 xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED,
2246 const xmlChar *type) {
2247 xmlSchemaTypePtr typ;
2251 typ = xmlSchemaGetPredefinedType(type,
2252 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2259 * xmlRelaxNGSchemaTypeCheck:
2260 * @data: data needed for the library
2261 * @type: the type name
2262 * @value: the value to check
2265 * Check if the given type and value are validated by
2266 * the W3C XMLSchema Datatype library.
2268 * Returns 1 if yes, 0 if no and -1 in case of error.
2271 xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
2272 const xmlChar *type,
2273 const xmlChar *value,
2276 xmlSchemaTypePtr typ;
2279 if ((type == NULL) || (value == NULL))
2281 typ = xmlSchemaGetPredefinedType(type,
2282 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2285 ret = xmlSchemaValPredefTypeNode(typ, value,
2286 (xmlSchemaValPtr *) result, node);
2287 if (ret == 2) /* special ID error code */
2297 * xmlRelaxNGSchemaFacetCheck:
2298 * @data: data needed for the library
2299 * @type: the type name
2300 * @facet: the facet name
2301 * @val: the facet value
2302 * @strval: the string value
2303 * @value: the value to check
2305 * Function provided by a type library to check a value facet
2307 * Returns 1 if yes, 0 if no and -1 in case of error.
2310 xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type,
2311 const xmlChar *facetname, const xmlChar *val,
2312 const xmlChar *strval, void *value) {
2313 xmlSchemaFacetPtr facet;
2314 xmlSchemaTypePtr typ;
2317 if ((type == NULL) || (strval == NULL))
2319 typ = xmlSchemaGetPredefinedType(type,
2320 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2324 facet = xmlSchemaNewFacet();
2328 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2329 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2330 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2331 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2332 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2333 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2334 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2335 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2336 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2337 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2338 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2339 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2340 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2341 facet->type = XML_SCHEMA_FACET_PATTERN;
2342 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2343 facet->type = XML_SCHEMA_FACET_ENUMERATION;
2344 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2345 facet->type = XML_SCHEMA_FACET_WHITESPACE;
2346 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2347 facet->type = XML_SCHEMA_FACET_LENGTH;
2348 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2349 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2350 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2351 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2353 xmlSchemaFreeFacet(facet);
2356 facet->value = xmlStrdup(val);
2357 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2359 xmlSchemaFreeFacet(facet);
2362 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2363 xmlSchemaFreeFacet(facet);
2370 * xmlRelaxNGSchemaFreeValue:
2371 * @data: data needed for the library
2372 * @value: the value to free
2374 * Function provided by a type library to free a Schemas value
2376 * Returns 1 if yes, 0 if no and -1 in case of error.
2379 xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) {
2380 xmlSchemaFreeValue(value);
2384 * xmlRelaxNGSchemaTypeCompare:
2385 * @data: data needed for the library
2386 * @type: the type name
2387 * @value1: the first value
2388 * @value2: the second value
2390 * Compare two values for equality accordingly a type from the W3C XMLSchema
2393 * Returns 1 if equal, 0 if no and -1 in case of error.
2396 xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
2397 const xmlChar *type,
2398 const xmlChar *value1,
2401 const xmlChar *value2,
2404 xmlSchemaTypePtr typ;
2405 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2407 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2409 typ = xmlSchemaGetPredefinedType(type,
2410 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2413 if (comp1 == NULL) {
2414 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2420 res1 = (xmlSchemaValPtr) comp1;
2422 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
2424 xmlSchemaFreeValue(res1);
2428 xmlSchemaFreeValue(res1);
2431 ret = xmlSchemaCompareValues(res1, res2);
2432 if (res1 != (xmlSchemaValPtr) comp1)
2433 xmlSchemaFreeValue(res1);
2434 xmlSchemaFreeValue(res2);
2443 * xmlRelaxNGDefaultTypeHave:
2444 * @data: data needed for the library
2445 * @type: the type name
2447 * Check if the given type is provided by
2448 * the default datatype library.
2450 * Returns 1 if yes, 0 if no and -1 in case of error.
2453 xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) {
2456 if (xmlStrEqual(type, BAD_CAST "string"))
2458 if (xmlStrEqual(type, BAD_CAST "token"))
2464 * xmlRelaxNGDefaultTypeCheck:
2465 * @data: data needed for the library
2466 * @type: the type name
2467 * @value: the value to check
2470 * Check if the given type and value are validated by
2471 * the default datatype library.
2473 * Returns 1 if yes, 0 if no and -1 in case of error.
2476 xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2477 const xmlChar *type ATTRIBUTE_UNUSED,
2478 const xmlChar *value ATTRIBUTE_UNUSED,
2479 void **result ATTRIBUTE_UNUSED,
2480 xmlNodePtr node ATTRIBUTE_UNUSED) {
2483 if (xmlStrEqual(type, BAD_CAST "string"))
2485 if (xmlStrEqual(type, BAD_CAST "token")) {
2493 * xmlRelaxNGDefaultTypeCompare:
2494 * @data: data needed for the library
2495 * @type: the type name
2496 * @value1: the first value
2497 * @value2: the second value
2499 * Compare two values accordingly a type from the default
2502 * Returns 1 if yes, 0 if no and -1 in case of error.
2505 xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
2506 const xmlChar *type,
2507 const xmlChar *value1,
2508 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2509 void *comp1 ATTRIBUTE_UNUSED,
2510 const xmlChar *value2,
2511 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) {
2514 if (xmlStrEqual(type, BAD_CAST "string")) {
2515 ret = xmlStrEqual(value1, value2);
2516 } else if (xmlStrEqual(type, BAD_CAST "token")) {
2517 if (!xmlStrEqual(value1, value2)) {
2518 xmlChar *nval, *nvalue;
2521 * TODO: trivial optimizations are possible by
2522 * computing at compile-time
2524 nval = xmlRelaxNGNormalize(NULL, value1);
2525 nvalue = xmlRelaxNGNormalize(NULL, value2);
2527 if ((nval == NULL) || (nvalue == NULL))
2529 else if (xmlStrEqual(nval, nvalue))
2543 static int xmlRelaxNGTypeInitialized = 0;
2544 static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2547 * xmlRelaxNGFreeTypeLibrary:
2548 * @lib: the type library structure
2549 * @namespace: the URI bound to the library
2551 * Free the structure associated to the type library
2554 xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2555 const xmlChar *namespace ATTRIBUTE_UNUSED) {
2558 if (lib->namespace != NULL)
2559 xmlFree((xmlChar *)lib->namespace);
2564 * xmlRelaxNGRegisterTypeLibrary:
2565 * @namespace: the URI bound to the library
2566 * @data: data associated to the library
2567 * @have: the provide function
2568 * @check: the checking function
2569 * @comp: the comparison function
2571 * Register a new type library
2573 * Returns 0 in case of success and -1 in case of error.
2576 xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data,
2577 xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check,
2578 xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet,
2579 xmlRelaxNGTypeFree freef) {
2580 xmlRelaxNGTypeLibraryPtr lib;
2583 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2584 (check == NULL) || (comp == NULL))
2586 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2587 xmlGenericError(xmlGenericErrorContext,
2588 "Relax-NG types library '%s' already registered\n",
2592 lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2594 xmlGenericError(xmlGenericErrorContext,
2595 "Relax-NG types library '%s' malloc() failed\n",
2599 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2600 lib->namespace = xmlStrdup(namespace);
2607 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2609 xmlGenericError(xmlGenericErrorContext,
2610 "Relax-NG types library failed to register '%s'\n",
2612 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2619 * xmlRelaxNGInitTypes:
2621 * Initilize the default type libraries.
2623 * Returns 0 in case of success and -1 in case of error.
2626 xmlRelaxNGInitTypes(void) {
2627 if (xmlRelaxNGTypeInitialized != 0)
2629 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2630 if (xmlRelaxNGRegisteredTypes == NULL) {
2631 xmlGenericError(xmlGenericErrorContext,
2632 "Failed to allocate sh table for Relax-NG types\n");
2635 xmlRelaxNGRegisterTypeLibrary(
2636 BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes",
2638 xmlRelaxNGSchemaTypeHave,
2639 xmlRelaxNGSchemaTypeCheck,
2640 xmlRelaxNGSchemaTypeCompare,
2641 xmlRelaxNGSchemaFacetCheck,
2642 xmlRelaxNGSchemaFreeValue);
2643 xmlRelaxNGRegisterTypeLibrary(
2646 xmlRelaxNGDefaultTypeHave,
2647 xmlRelaxNGDefaultTypeCheck,
2648 xmlRelaxNGDefaultTypeCompare,
2651 xmlRelaxNGTypeInitialized = 1;
2656 * xmlRelaxNGCleanupTypes:
2658 * Cleanup the default Schemas type library associated to RelaxNG
2661 xmlRelaxNGCleanupTypes(void) {
2662 xmlSchemaCleanupTypes();
2663 if (xmlRelaxNGTypeInitialized == 0)
2665 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2666 xmlRelaxNGFreeTypeLibrary);
2667 xmlRelaxNGTypeInitialized = 0;
2670 /************************************************************************
2672 * Compiling element content into regexp *
2674 * Sometime the element content can be compiled into a pure regexp, *
2675 * This allows a faster execution and streamability at that level *
2677 ************************************************************************/
2679 static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2680 xmlRelaxNGDefinePtr def);
2683 * xmlRelaxNGIsCompileable:
2684 * @define: the definition to check
2686 * Check if a definition is nullable.
2688 * Returns 1 if yes, 0 if no and -1 in case of error
2691 xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) {
2697 if ((def->type != XML_RELAXNG_ELEMENT) &&
2698 (def->dflags & IS_COMPILABLE))
2700 if ((def->type != XML_RELAXNG_ELEMENT) &&
2701 (def->dflags & IS_NOT_COMPILABLE))
2704 case XML_RELAXNG_NOOP:
2705 ret = xmlRelaxNGIsCompileable(def->content);
2707 case XML_RELAXNG_TEXT:
2708 case XML_RELAXNG_EMPTY:
2711 case XML_RELAXNG_ELEMENT:
2713 * Check if the element content is compileable
2715 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2716 ((def->dflags & IS_COMPILABLE) == 0)) {
2717 xmlRelaxNGDefinePtr list;
2718 list = def->content;
2719 while (list != NULL) {
2720 ret = xmlRelaxNGIsCompileable(list);
2725 if (ret == 0) def->dflags |= IS_NOT_COMPILABLE;
2726 if (ret == 1) def->dflags |= IS_COMPILABLE;
2727 #ifdef DEBUG_COMPILE
2729 xmlGenericError(xmlGenericErrorContext,
2730 "element content for %s is compilable\n",
2732 } else if (ret == 0) {
2733 xmlGenericError(xmlGenericErrorContext,
2734 "element content for %s is not compilable\n",
2737 xmlGenericError(xmlGenericErrorContext,
2738 "Problem in RelaxNGIsCompileable for element %s\n",
2744 * All elements return a compileable status unless they
2745 * are generic like anyName
2747 if ((def->nameClass != NULL) || (def->name == NULL))
2752 case XML_RELAXNG_REF:
2753 case XML_RELAXNG_EXTERNALREF:
2754 case XML_RELAXNG_PARENTREF:
2755 if (def->depth == -20) {
2758 xmlRelaxNGDefinePtr list;
2761 list = def->content;
2762 while (list != NULL) {
2763 ret = xmlRelaxNGIsCompileable(list);
2770 case XML_RELAXNG_START:
2771 case XML_RELAXNG_OPTIONAL:
2772 case XML_RELAXNG_ZEROORMORE:
2773 case XML_RELAXNG_ONEORMORE:
2774 case XML_RELAXNG_CHOICE:
2775 case XML_RELAXNG_GROUP:
2776 case XML_RELAXNG_DEF: {
2777 xmlRelaxNGDefinePtr list;
2779 list = def->content;
2780 while (list != NULL) {
2781 ret = xmlRelaxNGIsCompileable(list);
2788 case XML_RELAXNG_EXCEPT:
2789 case XML_RELAXNG_ATTRIBUTE:
2790 case XML_RELAXNG_INTERLEAVE:
2791 case XML_RELAXNG_DATATYPE:
2792 case XML_RELAXNG_LIST:
2793 case XML_RELAXNG_PARAM:
2794 case XML_RELAXNG_VALUE:
2797 case XML_RELAXNG_NOT_ALLOWED:
2801 if (ret == 0) def->dflags |= IS_NOT_COMPILABLE;
2802 if (ret == 1) def->dflags |= IS_COMPILABLE;
2803 #ifdef DEBUG_COMPILE
2805 xmlGenericError(xmlGenericErrorContext,
2806 "RelaxNGIsCompileable %s : true\n",
2807 xmlRelaxNGDefName(def));
2808 } else if (ret == 0) {
2809 xmlGenericError(xmlGenericErrorContext,
2810 "RelaxNGIsCompileable %s : false\n",
2811 xmlRelaxNGDefName(def));
2813 xmlGenericError(xmlGenericErrorContext,
2814 "Problem in RelaxNGIsCompileable %s\n",
2815 xmlRelaxNGDefName(def));
2822 * xmlRelaxNGCompile:
2823 * ctxt: the RelaxNG parser context
2824 * @define: the definition tree to compile
2826 * Compile the set of definitions, it works recursively, till the
2827 * element boundaries, where it tries to compile the content if possible
2829 * Returns 0 if success and -1 in case of error
2832 xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) {
2834 xmlRelaxNGDefinePtr list;
2836 if ((ctxt == NULL) || (def == NULL)) return(-1);
2839 case XML_RELAXNG_START:
2840 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
2841 xmlAutomataPtr oldam = ctxt->am;
2842 xmlAutomataStatePtr oldstate = ctxt->state;
2846 list = def->content;
2847 ctxt->am = xmlNewAutomata();
2848 if (ctxt->am == NULL)
2850 ctxt->state = xmlAutomataGetInitState(ctxt->am);
2851 while (list != NULL) {
2852 xmlRelaxNGCompile(ctxt, list);
2855 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
2856 def->contModel = xmlAutomataCompile(ctxt->am);
2857 xmlRegexpIsDeterminist(def->contModel);
2859 xmlFreeAutomata(ctxt->am);
2860 ctxt->state = oldstate;
2864 case XML_RELAXNG_ELEMENT:
2865 if ((ctxt->am != NULL) && (def->name != NULL)) {
2866 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
2867 ctxt->state, NULL, def->name, def->ns, def);
2869 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
2870 xmlAutomataPtr oldam = ctxt->am;
2871 xmlAutomataStatePtr oldstate = ctxt->state;
2875 list = def->content;
2876 ctxt->am = xmlNewAutomata();
2877 if (ctxt->am == NULL)
2879 ctxt->state = xmlAutomataGetInitState(ctxt->am);
2880 while (list != NULL) {
2881 xmlRelaxNGCompile(ctxt, list);
2884 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
2885 def->contModel = xmlAutomataCompile(ctxt->am);
2886 if (!xmlRegexpIsDeterminist(def->contModel)) {
2888 * we can only use the automata if it is determinist
2890 xmlRegFreeRegexp(def->contModel);
2891 def->contModel = NULL;
2893 xmlFreeAutomata(ctxt->am);
2894 ctxt->state = oldstate;
2897 xmlAutomataPtr oldam = ctxt->am;
2900 * we can't build the content model for this element content
2901 * but it still might be possible to build it for some of its
2902 * children, recurse.
2904 ret = xmlRelaxNGTryCompile(ctxt, def);
2908 case XML_RELAXNG_NOOP:
2909 ret = xmlRelaxNGCompile(ctxt, def->content);
2911 case XML_RELAXNG_OPTIONAL: {
2912 xmlAutomataStatePtr oldstate = ctxt->state;
2914 xmlRelaxNGCompile(ctxt, def->content);
2915 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
2918 case XML_RELAXNG_ZEROORMORE: {
2919 xmlAutomataStatePtr oldstate;
2921 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
2922 oldstate = ctxt->state;
2923 list = def->content;
2924 while (list != NULL) {
2925 xmlRelaxNGCompile(ctxt, list);
2928 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
2929 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
2932 case XML_RELAXNG_ONEORMORE: {
2933 xmlAutomataStatePtr oldstate;
2935 list = def->content;
2936 while (list != NULL) {
2937 xmlRelaxNGCompile(ctxt, list);
2940 oldstate = ctxt->state;
2941 list = def->content;
2942 while (list != NULL) {
2943 xmlRelaxNGCompile(ctxt, list);
2946 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
2947 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
2950 case XML_RELAXNG_CHOICE: {
2951 xmlAutomataStatePtr target = NULL;
2952 xmlAutomataStatePtr oldstate = ctxt->state;
2954 list = def->content;
2955 while (list != NULL) {
2956 ctxt->state = oldstate;
2957 ret = xmlRelaxNGCompile(ctxt, list);
2961 target = ctxt->state;
2963 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target);
2967 ctxt->state = target;
2971 case XML_RELAXNG_REF:
2972 case XML_RELAXNG_EXTERNALREF:
2973 case XML_RELAXNG_PARENTREF:
2974 case XML_RELAXNG_GROUP:
2975 case XML_RELAXNG_DEF:
2976 list = def->content;
2977 while (list != NULL) {
2978 ret = xmlRelaxNGCompile(ctxt, list);
2984 case XML_RELAXNG_TEXT: {
2985 xmlAutomataStatePtr oldstate;
2987 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
2988 oldstate = ctxt->state;
2989 xmlRelaxNGCompile(ctxt, def->content);
2990 xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state,
2991 BAD_CAST "#text", NULL);
2992 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
2995 case XML_RELAXNG_EMPTY:
2996 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
2998 case XML_RELAXNG_EXCEPT:
2999 case XML_RELAXNG_ATTRIBUTE:
3000 case XML_RELAXNG_INTERLEAVE:
3001 case XML_RELAXNG_NOT_ALLOWED:
3002 case XML_RELAXNG_DATATYPE:
3003 case XML_RELAXNG_LIST:
3004 case XML_RELAXNG_PARAM:
3005 case XML_RELAXNG_VALUE:
3006 /* This should not happen and generate an internal error */
3007 fprintf(stderr, "RNG internal error trying to compile %s\n",
3008 xmlRelaxNGDefName(def));
3015 * xmlRelaxNGTryCompile:
3016 * ctxt: the RelaxNG parser context
3017 * @define: the definition tree to compile
3019 * Try to compile the set of definitions, it works recursively,
3020 * possibly ignoring parts which cannot be compiled.
3022 * Returns 0 if success and -1 in case of error
3025 xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) {
3027 xmlRelaxNGDefinePtr list;
3029 if ((ctxt == NULL) || (def == NULL)) return(-1);
3031 if ((def->type == XML_RELAXNG_START) ||
3032 (def->type == XML_RELAXNG_ELEMENT)) {
3033 ret = xmlRelaxNGIsCompileable(def);
3034 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3036 ret = xmlRelaxNGCompile(ctxt, def);
3037 #ifdef DEBUG_PROGRESSIVE
3039 if (def->type == XML_RELAXNG_START)
3040 xmlGenericError(xmlGenericErrorContext,
3041 "compiled the start\n");
3043 xmlGenericError(xmlGenericErrorContext,
3044 "compiled element %s\n", def->name);
3046 if (def->type == XML_RELAXNG_START)
3047 xmlGenericError(xmlGenericErrorContext,
3048 "failed to compile the start\n");
3050 xmlGenericError(xmlGenericErrorContext,
3051 "failed to compile element %s\n", def->name);
3058 case XML_RELAXNG_NOOP:
3059 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3061 case XML_RELAXNG_TEXT:
3062 case XML_RELAXNG_DATATYPE:
3063 case XML_RELAXNG_LIST:
3064 case XML_RELAXNG_PARAM:
3065 case XML_RELAXNG_VALUE:
3066 case XML_RELAXNG_EMPTY:
3067 case XML_RELAXNG_ELEMENT:
3070 case XML_RELAXNG_OPTIONAL:
3071 case XML_RELAXNG_ZEROORMORE:
3072 case XML_RELAXNG_ONEORMORE:
3073 case XML_RELAXNG_CHOICE:
3074 case XML_RELAXNG_GROUP:
3075 case XML_RELAXNG_DEF:
3076 case XML_RELAXNG_START:
3077 case XML_RELAXNG_REF:
3078 case XML_RELAXNG_EXTERNALREF:
3079 case XML_RELAXNG_PARENTREF:
3080 list = def->content;
3081 while (list != NULL) {
3082 ret = xmlRelaxNGTryCompile(ctxt, list);
3088 case XML_RELAXNG_EXCEPT:
3089 case XML_RELAXNG_ATTRIBUTE:
3090 case XML_RELAXNG_INTERLEAVE:
3091 case XML_RELAXNG_NOT_ALLOWED:
3098 /************************************************************************
3100 * Parsing functions *
3102 ************************************************************************/
3104 static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(
3105 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
3106 static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(
3107 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
3108 static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(
3109 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group);
3110 static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(
3111 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
3112 static xmlRelaxNGPtr xmlRelaxNGParseDocument(
3113 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
3114 static int xmlRelaxNGParseGrammarContent(
3115 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
3116 static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(
3117 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
3118 xmlRelaxNGDefinePtr def);
3119 static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(
3120 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
3121 static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3122 xmlRelaxNGDefinePtr define, xmlNodePtr elem);
3125 #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
3128 * xmlRelaxNGIsNullable:
3129 * @define: the definition to verify
3131 * Check if a definition is nullable.
3133 * Returns 1 if yes, 0 if no and -1 in case of error
3136 xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) {
3141 if (define->dflags & IS_NULLABLE)
3143 if (define->dflags & IS_NOT_NULLABLE)
3145 switch (define->type) {
3146 case XML_RELAXNG_EMPTY:
3147 case XML_RELAXNG_TEXT:
3149 case XML_RELAXNG_NOOP:
3150 case XML_RELAXNG_DEF:
3151 case XML_RELAXNG_REF:
3152 case XML_RELAXNG_EXTERNALREF:
3153 case XML_RELAXNG_PARENTREF:
3154 case XML_RELAXNG_ONEORMORE:
3155 ret = xmlRelaxNGIsNullable(define->content);
3157 case XML_RELAXNG_EXCEPT:
3158 case XML_RELAXNG_NOT_ALLOWED:
3159 case XML_RELAXNG_ELEMENT:
3160 case XML_RELAXNG_DATATYPE:
3161 case XML_RELAXNG_PARAM:
3162 case XML_RELAXNG_VALUE:
3163 case XML_RELAXNG_LIST:
3164 case XML_RELAXNG_ATTRIBUTE:
3166 case XML_RELAXNG_CHOICE: {
3167 xmlRelaxNGDefinePtr list = define->content;
3169 while (list != NULL) {
3170 ret = xmlRelaxNGIsNullable(list);
3177 case XML_RELAXNG_START:
3178 case XML_RELAXNG_INTERLEAVE:
3179 case XML_RELAXNG_GROUP: {
3180 xmlRelaxNGDefinePtr list = define->content;
3182 while (list != NULL) {
3183 ret = xmlRelaxNGIsNullable(list);
3195 define->dflags |= IS_NOT_NULLABLE;
3197 define->dflags |= IS_NULLABLE;
3202 * xmlRelaxNGIsBlank:
3205 * Check if a string is ignorable c.f. 4.2. Whitespace
3207 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3210 xmlRelaxNGIsBlank(xmlChar *str) {
3214 if (!(IS_BLANK(*str))) return(0);
3221 * xmlRelaxNGGetDataTypeLibrary:
3222 * @ctxt: a Relax-NG parser context
3223 * @node: the current data or value element
3225 * Applies algorithm from 4.3. datatypeLibrary attribute
3227 * Returns the datatypeLibary value or NULL if not found
3230 xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3232 xmlChar *ret, *escape;
3234 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
3235 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3241 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3242 if (escape == NULL) {
3249 node = node->parent;
3250 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
3251 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3257 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3258 if (escape == NULL) {
3264 node = node->parent;
3270 * xmlRelaxNGParseValue:
3271 * @ctxt: a Relax-NG parser context
3272 * @node: the data node.
3274 * parse the content of a RelaxNG value node.
3276 * Returns the definition pointer or NULL in case of error
3278 static xmlRelaxNGDefinePtr
3279 xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3280 xmlRelaxNGDefinePtr def = NULL;
3281 xmlRelaxNGTypeLibraryPtr lib = NULL;
3286 def = xmlRelaxNGNewDefine(ctxt, node);
3289 def->type = XML_RELAXNG_VALUE;
3291 type = xmlGetProp(node, BAD_CAST "type");
3293 xmlRelaxNGNormExtSpace(type);
3294 if (xmlValidateNCName(type, 0)) {
3295 if (ctxt->error != NULL)
3296 ctxt->error(ctxt->userData,
3297 "value type '%s' is not an NCName\n",
3301 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3302 if (library == NULL)
3303 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3308 lib = (xmlRelaxNGTypeLibraryPtr)
3309 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3311 if (ctxt->error != NULL)
3312 ctxt->error(ctxt->userData,
3313 "Use of unregistered type library '%s'\n",
3319 if (lib->have == NULL) {
3320 if (ctxt->error != NULL)
3321 ctxt->error(ctxt->userData,
3322 "Internal error with type library '%s': no 'have'\n",
3326 success = lib->have(lib->data, def->name);
3328 if (ctxt->error != NULL)
3329 ctxt->error(ctxt->userData,
3330 "Error type '%s' is not exported by type library '%s'\n",
3331 def->name, library);
3337 if (node->children == NULL) {
3338 def->value = xmlStrdup(BAD_CAST "");
3339 } else if (((node->children->type != XML_TEXT_NODE) &&
3340 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3341 (node->children->next != NULL)) {
3342 if (ctxt->error != NULL)
3343 ctxt->error(ctxt->userData,
3344 "Expecting a single text value for <value>content\n");
3346 } else if (def != NULL) {
3347 def->value = xmlNodeGetContent(node);
3348 if (def->value == NULL) {
3349 if (ctxt->error != NULL)
3350 ctxt->error(ctxt->userData,
3351 "Element <value> has no content\n");
3353 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3356 success = lib->check(lib->data, def->name, def->value, &val, node);
3358 if (ctxt->error != NULL)
3359 ctxt->error(ctxt->userData,
3360 "Value '%s' is not acceptable for type '%s'\n",
3361 def->value, def->name);
3373 * xmlRelaxNGParseData:
3374 * @ctxt: a Relax-NG parser context
3375 * @node: the data node.
3377 * parse the content of a RelaxNG data node.
3379 * Returns the definition pointer or NULL in case of error
3381 static xmlRelaxNGDefinePtr
3382 xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3383 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
3384 xmlRelaxNGDefinePtr param, lastparam = NULL;
3385 xmlRelaxNGTypeLibraryPtr lib;
3391 type = xmlGetProp(node, BAD_CAST "type");
3393 if (ctxt->error != NULL)
3394 ctxt->error(ctxt->userData,
3395 "data has no type\n");
3399 xmlRelaxNGNormExtSpace(type);
3400 if (xmlValidateNCName(type, 0)) {
3401 if (ctxt->error != NULL)
3402 ctxt->error(ctxt->userData,
3403 "data type '%s' is not an NCName\n",
3407 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3408 if (library == NULL)
3409 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3411 def = xmlRelaxNGNewDefine(ctxt, node);
3416 def->type = XML_RELAXNG_DATATYPE;
3420 lib = (xmlRelaxNGTypeLibraryPtr)
3421 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3423 if (ctxt->error != NULL)
3424 ctxt->error(ctxt->userData,
3425 "Use of unregistered type library '%s'\n",
3431 if (lib->have == NULL) {
3432 if (ctxt->error != NULL)
3433 ctxt->error(ctxt->userData,
3434 "Internal error with type library '%s': no 'have'\n",
3438 tmp = lib->have(lib->data, def->name);
3440 if (ctxt->error != NULL)
3441 ctxt->error(ctxt->userData,
3442 "Error type '%s' is not exported by type library '%s'\n",
3443 def->name, library);
3445 } else if ((xmlStrEqual(library, BAD_CAST
3446 "http://www.w3.org/2001/XMLSchema-datatypes")) &&
3447 ((xmlStrEqual(def->name, BAD_CAST "IDREF")) ||
3448 (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3453 content = node->children;
3456 * Handle optional params
3458 while (content != NULL) {
3459 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3461 if (xmlStrEqual(library,
3462 BAD_CAST"http://relaxng.org/ns/structure/1.0")) {
3463 if (ctxt->error != NULL)
3464 ctxt->error(ctxt->userData,
3465 "Type library '%s' does not allow type parameters\n",
3468 content = content->next;
3469 while ((content != NULL) &&
3470 (xmlStrEqual(content->name, BAD_CAST "param")))
3471 content = content->next;
3473 param = xmlRelaxNGNewDefine(ctxt, node);
3474 if (param != NULL) {
3475 param->type = XML_RELAXNG_PARAM;
3476 param->name = xmlGetProp(content, BAD_CAST "name");
3477 if (param->name == NULL) {
3478 if (ctxt->error != NULL)
3479 ctxt->error(ctxt->userData,
3480 "param has no name\n");
3483 param->value = xmlNodeGetContent(content);
3484 if (lastparam == NULL) {
3485 def->attrs = lastparam = param;
3487 lastparam->next = param;
3493 content = content->next;
3497 * Handle optional except
3499 if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3501 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
3503 except = xmlRelaxNGNewDefine(ctxt, node);
3504 if (except == NULL) {
3507 except->type = XML_RELAXNG_EXCEPT;
3508 child = content->children;
3510 def->content = except;
3512 last->next = except;
3514 if (child == NULL) {
3515 if (ctxt->error != NULL)
3516 ctxt->error(ctxt->userData,
3517 "except has no content\n");
3520 while (child != NULL) {
3521 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3523 if (last2 == NULL) {
3524 except->content = last2 = tmp2;
3530 child = child->next;
3532 content = content->next;
3535 * Check there is no unhandled data
3537 if (content != NULL) {
3538 if (ctxt->error != NULL)
3539 ctxt->error(ctxt->userData,
3540 "Element data has unexpected content %s\n", content->name);
3547 static const xmlChar *invalidName = BAD_CAST "\1";
3550 * xmlRelaxNGCompareNameClasses:
3551 * @defs1: the first element/attribute defs
3552 * @defs2: the second element/attribute defs
3553 * @name: the restriction on the name
3554 * @ns: the restriction on the namespace
3556 * Compare the 2 lists of element definitions. The comparison is
3557 * that if both lists do not accept the same QNames, it returns 1
3558 * If the 2 lists can accept the same QName the comparison returns 0
3560 * Returns 1 disttinct, 0 if equal
3563 xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
3564 xmlRelaxNGDefinePtr def2) {
3568 xmlRelaxNGValidCtxt ctxt;
3569 ctxt.flags = FLAGS_IGNORABLE;
3571 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3573 if ((def1->type == XML_RELAXNG_ELEMENT) ||
3574 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3575 if (def2->type == XML_RELAXNG_TEXT)
3577 if (def1->name != NULL) {
3578 node.name = def1->name;
3580 node.name = invalidName;
3583 if (def1->ns != NULL) {
3584 if (def1->ns[0] == 0) {
3590 ns.href = invalidName;
3592 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
3593 if (def1->nameClass != NULL) {
3594 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3601 } else if (def1->type == XML_RELAXNG_TEXT) {
3602 if (def2->type == XML_RELAXNG_TEXT)
3605 } else if (def1->type == XML_RELAXNG_EXCEPT) {
3614 if ((def2->type == XML_RELAXNG_ELEMENT) ||
3615 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3616 if (def2->name != NULL) {
3617 node.name = def2->name;
3619 node.name = invalidName;
3622 if (def2->ns != NULL) {
3623 if (def2->ns[0] == 0) {
3629 ns.href = invalidName;
3631 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
3632 if (def2->nameClass != NULL) {
3633 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3649 * xmlRelaxNGCompareElemDefLists:
3650 * @ctxt: a Relax-NG parser context
3651 * @defs1: the first list of element/attribute defs
3652 * @defs2: the second list of element/attribute defs
3654 * Compare the 2 lists of element or attribute definitions. The comparison
3655 * is that if both lists do not accept the same QNames, it returns 1
3656 * If the 2 lists can accept the same QName the comparison returns 0
3658 * Returns 1 disttinct, 0 if equal
3661 xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3662 xmlRelaxNGDefinePtr *def1,
3663 xmlRelaxNGDefinePtr *def2) {
3664 xmlRelaxNGDefinePtr *basedef2 = def2;
3666 if ((def1 == NULL) || (def2 == NULL))
3668 if ((*def1 == NULL) || (*def2 == NULL))
3670 while (*def1 != NULL) {
3671 while ((*def2) != NULL) {
3672 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3683 * xmlRelaxNGGenerateAttributes:
3684 * @ctxt: a Relax-NG parser context
3685 * @def: the definition definition
3687 * Check if the definition can only generate attributes
3689 * Returns 1 if yes, 0 if no and -1 in case of error.
3692 xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
3693 xmlRelaxNGDefinePtr def) {
3694 xmlRelaxNGDefinePtr parent, cur, tmp;
3697 * Don't run that check in case of error. Infinite recursion
3700 if (ctxt->nbErrors != 0)
3705 while (cur != NULL) {
3706 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3707 (cur->type == XML_RELAXNG_TEXT) ||
3708 (cur->type == XML_RELAXNG_DATATYPE) ||
3709 (cur->type == XML_RELAXNG_PARAM) ||
3710 (cur->type == XML_RELAXNG_LIST) ||
3711 (cur->type == XML_RELAXNG_VALUE) ||
3712 (cur->type == XML_RELAXNG_EMPTY))
3714 if ((cur->type == XML_RELAXNG_CHOICE) ||
3715 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3716 (cur->type == XML_RELAXNG_GROUP) ||
3717 (cur->type == XML_RELAXNG_ONEORMORE) ||
3718 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3719 (cur->type == XML_RELAXNG_OPTIONAL) ||
3720 (cur->type == XML_RELAXNG_PARENTREF) ||
3721 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3722 (cur->type == XML_RELAXNG_REF) ||
3723 (cur->type == XML_RELAXNG_DEF)) {
3724 if (cur->content != NULL) {
3728 while (tmp != NULL) {
3729 tmp->parent = parent;
3737 if (cur->next != NULL) {
3743 if (cur == NULL) break;
3744 if (cur == def) return(1);
3745 if (cur->next != NULL) {
3749 } while (cur != NULL);
3755 * xmlRelaxNGGetElements:
3756 * @ctxt: a Relax-NG parser context
3757 * @def: the definition definition
3758 * @eora: gather elements (0) or attributes (1)
3760 * Compute the list of top elements a definition can generate
3762 * Returns a list of elements or NULL if none was found.
3764 static xmlRelaxNGDefinePtr *
3765 xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
3766 xmlRelaxNGDefinePtr def,
3768 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
3773 * Don't run that check in case of error. Infinite recursion
3776 if (ctxt->nbErrors != 0)
3781 while (cur != NULL) {
3782 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3783 (cur->type == XML_RELAXNG_TEXT))) ||
3784 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3787 ret = (xmlRelaxNGDefinePtr *)
3788 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3790 if (ctxt->error != NULL)
3791 ctxt->error(ctxt->userData,
3792 "Out of memory in element search\n");
3796 } else if (max <= len) {
3798 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3800 if (ctxt->error != NULL)
3801 ctxt->error(ctxt->userData,
3802 "Out of memory in element search\n");
3809 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3810 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3811 (cur->type == XML_RELAXNG_GROUP) ||
3812 (cur->type == XML_RELAXNG_ONEORMORE) ||
3813 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3814 (cur->type == XML_RELAXNG_OPTIONAL) ||
3815 (cur->type == XML_RELAXNG_PARENTREF) ||
3816 (cur->type == XML_RELAXNG_REF) ||
3817 (cur->type == XML_RELAXNG_DEF)) {
3819 * Don't go within elements or attributes or string values.
3820 * Just gather the element top list
3822 if (cur->content != NULL) {
3826 while (tmp != NULL) {
3827 tmp->parent = parent;
3835 if (cur->next != NULL) {
3841 if (cur == NULL) break;
3842 if (cur == def) return(ret);
3843 if (cur->next != NULL) {
3847 } while (cur != NULL);
3853 * xmlRelaxNGCheckChoiceDeterminism:
3854 * @ctxt: a Relax-NG parser context
3855 * @def: the choice definition
3857 * Also used to find indeterministic pattern in choice
3860 xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
3861 xmlRelaxNGDefinePtr def) {
3862 xmlRelaxNGDefinePtr **list;
3863 xmlRelaxNGDefinePtr cur;
3864 int nbchild = 0, i, j, ret;
3865 int is_nullable = 0;
3866 int is_indeterminist = 0;
3867 xmlHashTablePtr triage = NULL;
3870 if ((def == NULL) ||
3871 (def->type != XML_RELAXNG_CHOICE))
3874 if (def->dflags & IS_PROCESSED)
3878 * Don't run that check in case of error. Infinite recursion
3881 if (ctxt->nbErrors != 0)
3884 is_nullable = xmlRelaxNGIsNullable(def);
3887 while (cur != NULL) {
3892 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
3893 sizeof(xmlRelaxNGDefinePtr *));
3895 if (ctxt->error != NULL)
3896 ctxt->error(ctxt->userData,
3897 "Out of memory in choice computation\n");
3903 * a bit strong but safe
3905 if (is_nullable == 0) {
3906 triage = xmlHashCreate(10);
3911 while (cur != NULL) {
3912 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
3913 if ((list[i] == NULL) || (list[i][0] == NULL)) {
3915 } else if (is_triable == 1) {
3916 xmlRelaxNGDefinePtr *tmp;
3920 while ((*tmp != NULL) && (is_triable == 1)) {
3921 if ((*tmp)->type == XML_RELAXNG_TEXT) {
3922 res = xmlHashAddEntry2(triage,
3923 BAD_CAST "#text", NULL,
3927 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
3928 ((*tmp)->name != NULL)) {
3929 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3930 res = xmlHashAddEntry2(triage,
3934 res = xmlHashAddEntry2(triage,
3935 (*tmp)->name, (*tmp)->ns,
3939 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
3940 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3941 res = xmlHashAddEntry2(triage,
3942 BAD_CAST "#any", NULL,
3945 res = xmlHashAddEntry2(triage,
3946 BAD_CAST "#any", (*tmp)->ns,
3960 for (i = 0;i < nbchild;i++) {
3961 if (list[i] == NULL)
3963 for (j = 0;j < i;j++) {
3964 if (list[j] == NULL)
3966 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
3968 is_indeterminist = 1;
3972 for (i = 0;i < nbchild;i++) {
3973 if (list[i] != NULL)
3978 if (is_indeterminist) {
3979 def->dflags |= IS_INDETERMINIST;
3981 if (is_triable == 1) {
3982 def->dflags |= IS_TRIABLE;
3984 } else if (triage != NULL) {
3985 xmlHashFree(triage, NULL);
3987 def->dflags |= IS_PROCESSED;
3991 * xmlRelaxNGCheckGroupAttrs:
3992 * @ctxt: a Relax-NG parser context
3993 * @def: the group definition
3995 * Detects violations of rule 7.3
3998 xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
3999 xmlRelaxNGDefinePtr def) {
4000 xmlRelaxNGDefinePtr **list;
4001 xmlRelaxNGDefinePtr cur;
4002 int nbchild = 0, i, j, ret;
4004 if ((def == NULL) ||
4005 ((def->type != XML_RELAXNG_GROUP) &&
4006 (def->type != XML_RELAXNG_ELEMENT)))
4009 if (def->dflags & IS_PROCESSED)
4013 * Don't run that check in case of error. Infinite recursion
4016 if (ctxt->nbErrors != 0)
4020 while (cur != NULL) {
4025 while (cur != NULL) {
4030 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4031 sizeof(xmlRelaxNGDefinePtr *));
4033 if (ctxt->error != NULL)
4034 ctxt->error(ctxt->userData,
4035 "Out of memory in group computation\n");
4041 while (cur != NULL) {
4042 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4047 while (cur != NULL) {
4048 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4053 for (i = 0;i < nbchild;i++) {
4054 if (list[i] == NULL)
4056 for (j = 0;j < i;j++) {
4057 if (list[j] == NULL)
4059 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4061 if (ctxt->error != NULL)
4062 ctxt->error(ctxt->userData,
4063 "Attributes conflicts in group\n");
4068 for (i = 0;i < nbchild;i++) {
4069 if (list[i] != NULL)
4074 def->dflags |= IS_PROCESSED;
4078 * xmlRelaxNGComputeInterleaves:
4079 * @def: the interleave definition
4080 * @ctxt: a Relax-NG parser context
4081 * @name: the definition name
4083 * A lot of work for preprocessing interleave definitions
4084 * is potentially needed to get a decent execution speed at runtime
4085 * - trying to get a total order on the element nodes generated
4086 * by the interleaves, order the list of interleave definitions
4087 * following that order.
4088 * - if <text/> is used to handle mixed content, it is better to
4089 * flag this in the define and simplify the runtime checking
4093 xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
4094 xmlRelaxNGParserCtxtPtr ctxt,
4095 xmlChar *name ATTRIBUTE_UNUSED) {
4096 xmlRelaxNGDefinePtr cur, *tmp;
4098 xmlRelaxNGPartitionPtr partitions = NULL;
4099 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4100 xmlRelaxNGInterleaveGroupPtr group;
4105 int is_determinist = 1;
4108 * Don't run that check in case of error. Infinite recursion
4111 if (ctxt->nbErrors != 0)
4114 #ifdef DEBUG_INTERLEAVE
4115 xmlGenericError(xmlGenericErrorContext,
4116 "xmlRelaxNGComputeInterleaves(%s)\n",
4120 while (cur != NULL) {
4125 #ifdef DEBUG_INTERLEAVE
4126 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4128 groups = (xmlRelaxNGInterleaveGroupPtr *)
4129 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
4133 while (cur != NULL) {
4134 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4135 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4136 if (groups[nbgroups] == NULL)
4138 if (cur->type == XML_RELAXNG_TEXT)
4140 groups[nbgroups]->rule = cur;
4141 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4142 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4146 #ifdef DEBUG_INTERLEAVE
4147 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4151 * Let's check that all rules makes a partitions according to 7.4
4153 partitions = (xmlRelaxNGPartitionPtr)
4154 xmlMalloc(sizeof(xmlRelaxNGPartition));
4155 if (partitions == NULL)
4157 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
4158 partitions->nbgroups = nbgroups;
4159 partitions->triage = xmlHashCreate(nbgroups);
4160 for (i = 0;i < nbgroups;i++) {
4162 for (j = i+1;j < nbgroups;j++) {
4163 if (groups[j] == NULL)
4166 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4169 if (ctxt->error != NULL)
4170 ctxt->error(ctxt->userData,
4171 "Element or text conflicts in interleave\n");
4174 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4177 if (ctxt->error != NULL)
4178 ctxt->error(ctxt->userData,
4179 "Attributes conflicts in interleave\n");
4184 if ((tmp != NULL) && (*tmp != NULL)) {
4185 while (*tmp != NULL) {
4186 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4187 res = xmlHashAddEntry2(partitions->triage,
4188 BAD_CAST "#text", NULL,
4189 (void *)(long)(i + 1));
4191 is_determinist = -1;
4192 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4193 ((*tmp)->name != NULL)) {
4194 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4195 res = xmlHashAddEntry2(partitions->triage,
4197 (void *)(long)(i + 1));
4199 res = xmlHashAddEntry2(partitions->triage,
4200 (*tmp)->name, (*tmp)->ns,
4201 (void *)(long)(i + 1));
4203 is_determinist = -1;
4204 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4205 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4206 res = xmlHashAddEntry2(partitions->triage,
4207 BAD_CAST "#any", NULL,
4208 (void *)(long)(i + 1));
4210 res = xmlHashAddEntry2(partitions->triage,
4211 BAD_CAST "#any", (*tmp)->ns,
4212 (void *)(long)(i + 1));
4213 if ((*tmp)->nameClass != NULL)
4216 is_determinist = -1;
4218 is_determinist = -1;
4226 partitions->groups = groups;
4229 * and save the partition list back in the def
4231 def->data = partitions;
4233 def->dflags |= IS_MIXED;
4234 if (is_determinist == 1)
4235 partitions->flags = IS_DETERMINIST;
4236 if (is_determinist == 2)
4237 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
4241 if (ctxt->error != NULL)
4242 ctxt->error(ctxt->userData,
4243 "Out of memory in interleave computation\n");
4245 if (groups != NULL) {
4246 for (i = 0;i < nbgroups;i++)
4247 if (groups[i] != NULL) {
4248 if (groups[i]->defs != NULL)
4249 xmlFree(groups[i]->defs);
4254 xmlRelaxNGFreePartition(partitions);
4258 * xmlRelaxNGParseInterleave:
4259 * @ctxt: a Relax-NG parser context
4260 * @node: the data node.
4262 * parse the content of a RelaxNG interleave node.
4264 * Returns the definition pointer or NULL in case of error
4266 static xmlRelaxNGDefinePtr
4267 xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4268 xmlRelaxNGDefinePtr def = NULL;
4269 xmlRelaxNGDefinePtr last = NULL, cur;
4272 def = xmlRelaxNGNewDefine(ctxt, node);
4276 def->type = XML_RELAXNG_INTERLEAVE;
4278 if (ctxt->interleaves == NULL)
4279 ctxt->interleaves = xmlHashCreate(10);
4280 if (ctxt->interleaves == NULL) {
4281 if (ctxt->error != NULL)
4282 ctxt->error(ctxt->userData,
4283 "Failed to create interleaves hash table\n");
4288 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4289 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4290 if (ctxt->error != NULL)
4291 ctxt->error(ctxt->userData,
4292 "Failed to add %s to hash table\n", name);
4296 child = node->children;
4297 if (child == NULL) {
4298 if (ctxt->error != NULL)
4299 ctxt->error(ctxt->userData, "Element interleave is empty\n");
4302 while (child != NULL) {
4303 if (IS_RELAXNG(child, "element")) {
4304 cur = xmlRelaxNGParseElement(ctxt, child);
4306 cur = xmlRelaxNGParsePattern(ctxt, child);
4311 def->content = last = cur;
4317 child = child->next;
4324 * xmlRelaxNGParseInclude:
4325 * @ctxt: a Relax-NG parser context
4326 * @node: the include node
4328 * Integrate the content of an include node in the current grammar
4330 * Returns 0 in case of success or -1 in case of error
4333 xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4334 xmlRelaxNGIncludePtr incl;
4338 incl = node->_private;
4340 if (ctxt->error != NULL)
4341 ctxt->error(ctxt->userData,
4342 "Include node has no data\n");
4346 root = xmlDocGetRootElement(incl->doc);
4348 if (ctxt->error != NULL)
4349 ctxt->error(ctxt->userData,
4350 "Include document is empty\n");
4354 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
4355 if (ctxt->error != NULL)
4356 ctxt->error(ctxt->userData,
4357 "Include document root is not a grammar\n");
4363 * Merge the definition from both the include and the internal list
4365 if (root->children != NULL) {
4366 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4370 if (node->children != NULL) {
4371 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4379 * xmlRelaxNGParseDefine:
4380 * @ctxt: a Relax-NG parser context
4381 * @node: the define node
4383 * parse the content of a RelaxNG define element node.
4385 * Returns 0 in case of success or -1 in case of error
4388 xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4391 xmlRelaxNGDefinePtr def;
4392 const xmlChar *olddefine;
4394 name = xmlGetProp(node, BAD_CAST "name");
4396 if (ctxt->error != NULL)
4397 ctxt->error(ctxt->userData,
4398 "define has no name\n");
4401 xmlRelaxNGNormExtSpace(name);
4402 if (xmlValidateNCName(name, 0)) {
4403 if (ctxt->error != NULL)
4404 ctxt->error(ctxt->userData,
4405 "define name '%s' is not an NCName\n",
4409 def = xmlRelaxNGNewDefine(ctxt, node);
4414 def->type = XML_RELAXNG_DEF;
4416 if (node->children == NULL) {
4417 if (ctxt->error != NULL)
4418 ctxt->error(ctxt->userData,
4419 "define has no children\n");
4422 olddefine = ctxt->define;
4423 ctxt->define = name;
4424 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4425 ctxt->define = olddefine;
4427 if (ctxt->grammar->defs == NULL)
4428 ctxt->grammar->defs = xmlHashCreate(10);
4429 if (ctxt->grammar->defs == NULL) {
4430 if (ctxt->error != NULL)
4431 ctxt->error(ctxt->userData,
4432 "Could not create definition hash\n");
4436 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4438 xmlRelaxNGDefinePtr prev;
4440 prev = xmlHashLookup(ctxt->grammar->defs, name);
4442 if (ctxt->error != NULL)
4443 ctxt->error(ctxt->userData,
4444 "Internal error on define aggregation of %s\n",
4449 while (prev->nextHash != NULL)
4450 prev = prev->nextHash;
4451 prev->nextHash = def;
4460 * xmlRelaxNGProcessExternalRef:
4461 * @ctxt: the parser context
4462 * @node: the externlRef node
4464 * Process and compile an externlRef node
4466 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4468 static xmlRelaxNGDefinePtr
4469 xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4470 xmlRelaxNGDocumentPtr docu;
4471 xmlNodePtr root, tmp;
4473 int newNs = 0, oldflags;
4474 xmlRelaxNGDefinePtr def;
4476 docu = node->_private;
4478 def = xmlRelaxNGNewDefine(ctxt, node);
4481 def->type = XML_RELAXNG_EXTERNALREF;
4483 if (docu->content == NULL) {
4485 * Then do the parsing for good
4487 root = xmlDocGetRootElement(docu->doc);
4489 if (ctxt->error != NULL)
4490 ctxt->error(ctxt->userData,
4491 "xmlRelaxNGParse: %s is empty\n",
4497 * ns transmission rules
4499 ns = xmlGetProp(root, BAD_CAST "ns");
4502 while ((tmp != NULL) &&
4503 (tmp->type == XML_ELEMENT_NODE)) {
4504 ns = xmlGetProp(tmp, BAD_CAST "ns");
4511 xmlSetProp(root, BAD_CAST "ns", ns);
4520 * Parsing to get a precompiled schemas.
4522 oldflags = ctxt->flags;
4523 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4524 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4525 ctxt->flags = oldflags;
4526 if ((docu->schema != NULL) &&
4527 (docu->schema->topgrammar != NULL)) {
4528 docu->content = docu->schema->topgrammar->start;
4532 * the externalRef may be reused in a different ns context
4535 xmlUnsetProp(root, BAD_CAST "ns");
4538 def->content = docu->content;
4546 * xmlRelaxNGParsePattern:
4547 * @ctxt: a Relax-NG parser context
4548 * @node: the pattern node.
4550 * parse the content of a RelaxNG pattern node.
4552 * Returns the definition pointer or NULL in case of error or if no
4553 * pattern is generated.
4555 static xmlRelaxNGDefinePtr
4556 xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4557 xmlRelaxNGDefinePtr def = NULL;
4562 if (IS_RELAXNG(node, "element")) {
4563 def = xmlRelaxNGParseElement(ctxt, node);
4564 } else if (IS_RELAXNG(node, "attribute")) {
4565 def = xmlRelaxNGParseAttribute(ctxt, node);
4566 } else if (IS_RELAXNG(node, "empty")) {
4567 def = xmlRelaxNGNewDefine(ctxt, node);
4570 def->type = XML_RELAXNG_EMPTY;
4571 if (node->children != NULL) {
4572 if (ctxt->error != NULL)
4573 ctxt->error(ctxt->userData, "empty: had a child node\n");
4576 } else if (IS_RELAXNG(node, "text")) {
4577 def = xmlRelaxNGNewDefine(ctxt, node);
4580 def->type = XML_RELAXNG_TEXT;
4581 if (node->children != NULL) {
4582 if (ctxt->error != NULL)
4583 ctxt->error(ctxt->userData, "text: had a child node\n");
4586 } else if (IS_RELAXNG(node, "zeroOrMore")) {
4587 def = xmlRelaxNGNewDefine(ctxt, node);
4590 def->type = XML_RELAXNG_ZEROORMORE;
4591 if (node->children == NULL) {
4592 if (ctxt->error != NULL)
4593 ctxt->error(ctxt->userData,
4594 "Element %s is empty\n", node->name);
4597 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4599 } else if (IS_RELAXNG(node, "oneOrMore")) {
4600 def = xmlRelaxNGNewDefine(ctxt, node);
4603 def->type = XML_RELAXNG_ONEORMORE;
4604 if (node->children == NULL) {
4605 if (ctxt->error != NULL)
4606 ctxt->error(ctxt->userData,
4607 "Element %s is empty\n", node->name);
4610 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4612 } else if (IS_RELAXNG(node, "optional")) {
4613 def = xmlRelaxNGNewDefine(ctxt, node);
4616 def->type = XML_RELAXNG_OPTIONAL;
4617 if (node->children == NULL) {
4618 if (ctxt->error != NULL)
4619 ctxt->error(ctxt->userData,
4620 "Element %s is empty\n", node->name);
4623 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4625 } else if (IS_RELAXNG(node, "choice")) {
4626 def = xmlRelaxNGNewDefine(ctxt, node);
4629 def->type = XML_RELAXNG_CHOICE;
4630 if (node->children == NULL) {
4631 if (ctxt->error != NULL)
4632 ctxt->error(ctxt->userData,
4633 "Element %s is empty\n", node->name);
4636 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4638 } else if (IS_RELAXNG(node, "group")) {
4639 def = xmlRelaxNGNewDefine(ctxt, node);
4642 def->type = XML_RELAXNG_GROUP;
4643 if (node->children == NULL) {
4644 if (ctxt->error != NULL)
4645 ctxt->error(ctxt->userData,
4646 "Element %s is empty\n", node->name);
4649 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4651 } else if (IS_RELAXNG(node, "ref")) {
4652 def = xmlRelaxNGNewDefine(ctxt, node);
4655 def->type = XML_RELAXNG_REF;
4656 def->name = xmlGetProp(node, BAD_CAST "name");
4657 if (def->name == NULL) {
4658 if (ctxt->error != NULL)
4659 ctxt->error(ctxt->userData,
4660 "ref has no name\n");
4663 xmlRelaxNGNormExtSpace(def->name);
4664 if (xmlValidateNCName(def->name, 0)) {
4665 if (ctxt->error != NULL)
4666 ctxt->error(ctxt->userData,
4667 "ref name '%s' is not an NCName\n",
4672 if (node->children != NULL) {
4673 if (ctxt->error != NULL)
4674 ctxt->error(ctxt->userData,
4675 "ref is not empty\n");
4678 if (ctxt->grammar->refs == NULL)
4679 ctxt->grammar->refs = xmlHashCreate(10);
4680 if (ctxt->grammar->refs == NULL) {
4681 if (ctxt->error != NULL)
4682 ctxt->error(ctxt->userData,
4683 "Could not create references hash\n");
4689 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4691 xmlRelaxNGDefinePtr prev;
4693 prev = (xmlRelaxNGDefinePtr)
4694 xmlHashLookup(ctxt->grammar->refs, def->name);
4696 if (def->name != NULL) {
4697 if (ctxt->error != NULL)
4698 ctxt->error(ctxt->userData,
4699 "Error refs definitions '%s'\n",
4702 if (ctxt->error != NULL)
4703 ctxt->error(ctxt->userData,
4704 "Error refs definitions\n");
4709 def->nextHash = prev->nextHash;
4710 prev->nextHash = def;
4714 } else if (IS_RELAXNG(node, "data")) {
4715 def = xmlRelaxNGParseData(ctxt, node);
4716 } else if (IS_RELAXNG(node, "value")) {
4717 def = xmlRelaxNGParseValue(ctxt, node);
4718 } else if (IS_RELAXNG(node, "list")) {
4719 def = xmlRelaxNGNewDefine(ctxt, node);
4722 def->type = XML_RELAXNG_LIST;
4723 if (node->children == NULL) {
4724 if (ctxt->error != NULL)
4725 ctxt->error(ctxt->userData,
4726 "Element %s is empty\n", node->name);
4729 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4731 } else if (IS_RELAXNG(node, "interleave")) {
4732 def = xmlRelaxNGParseInterleave(ctxt, node);
4733 } else if (IS_RELAXNG(node, "externalRef")) {
4734 def = xmlRelaxNGProcessExternalRef(ctxt, node);
4735 } else if (IS_RELAXNG(node, "notAllowed")) {
4736 def = xmlRelaxNGNewDefine(ctxt, node);
4739 def->type = XML_RELAXNG_NOT_ALLOWED;
4740 if (node->children != NULL) {
4741 if (ctxt->error != NULL)
4742 ctxt->error(ctxt->userData,
4743 "xmlRelaxNGParse: notAllowed element is not empty\n");
4746 } else if (IS_RELAXNG(node, "grammar")) {
4747 xmlRelaxNGGrammarPtr grammar, old;
4748 xmlRelaxNGGrammarPtr oldparent;
4750 #ifdef DEBUG_GRAMMAR
4751 xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n");
4754 oldparent = ctxt->parentgrammar;
4755 old = ctxt->grammar;
4756 ctxt->parentgrammar = old;
4757 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4759 ctxt->grammar = old;
4760 ctxt->parentgrammar = oldparent;
4762 if (grammar != NULL) {
4763 grammar->next = old->next;
4764 old->next = grammar;
4768 if (grammar != NULL)
4769 def = grammar->start;
4772 } else if (IS_RELAXNG(node, "parentRef")) {
4773 if (ctxt->parentgrammar == NULL) {
4774 if (ctxt->error != NULL)
4775 ctxt->error(ctxt->userData,
4776 "Use of parentRef without a parent grammar\n");
4780 def = xmlRelaxNGNewDefine(ctxt, node);
4783 def->type = XML_RELAXNG_PARENTREF;
4784 def->name = xmlGetProp(node, BAD_CAST "name");
4785 if (def->name == NULL) {
4786 if (ctxt->error != NULL)
4787 ctxt->error(ctxt->userData,
4788 "parentRef has no name\n");
4791 xmlRelaxNGNormExtSpace(def->name);
4792 if (xmlValidateNCName(def->name, 0)) {
4793 if (ctxt->error != NULL)
4794 ctxt->error(ctxt->userData,
4795 "parentRef name '%s' is not an NCName\n",
4800 if (node->children != NULL) {
4801 if (ctxt->error != NULL)
4802 ctxt->error(ctxt->userData,
4803 "parentRef is not empty\n");
4806 if (ctxt->parentgrammar->refs == NULL)
4807 ctxt->parentgrammar->refs = xmlHashCreate(10);
4808 if (ctxt->parentgrammar->refs == NULL) {
4809 if (ctxt->error != NULL)
4810 ctxt->error(ctxt->userData,
4811 "Could not create references hash\n");
4814 } else if (def->name != NULL) {
4817 tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4819 xmlRelaxNGDefinePtr prev;
4821 prev = (xmlRelaxNGDefinePtr)
4822 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4824 if (ctxt->error != NULL)
4825 ctxt->error(ctxt->userData,
4826 "Internal error parentRef definitions '%s'\n",
4831 def->nextHash = prev->nextHash;
4832 prev->nextHash = def;
4836 } else if (IS_RELAXNG(node, "mixed")) {
4837 if (node->children == NULL) {
4838 if (ctxt->error != NULL)
4839 ctxt->error(ctxt->userData,
4840 "Mixed is empty\n");
4844 def = xmlRelaxNGParseInterleave(ctxt, node);
4846 xmlRelaxNGDefinePtr tmp;
4848 if ((def->content != NULL) && (def->content->next != NULL)) {
4849 tmp = xmlRelaxNGNewDefine(ctxt, node);
4851 tmp->type = XML_RELAXNG_GROUP;
4852 tmp->content = def->content;
4857 tmp = xmlRelaxNGNewDefine(ctxt, node);
4860 tmp->type = XML_RELAXNG_TEXT;
4861 tmp->next = def->content;
4866 if (ctxt->error != NULL)
4867 ctxt->error(ctxt->userData,
4868 "Unexpected node %s is not a pattern\n",
4877 * xmlRelaxNGParseAttribute:
4878 * @ctxt: a Relax-NG parser context
4879 * @node: the element node
4881 * parse the content of a RelaxNG attribute node.
4883 * Returns the definition pointer or NULL in case of error.
4885 static xmlRelaxNGDefinePtr
4886 xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4887 xmlRelaxNGDefinePtr ret, cur;
4891 ret = xmlRelaxNGNewDefine(ctxt, node);
4894 ret->type = XML_RELAXNG_ATTRIBUTE;
4895 ret->parent = ctxt->def;
4896 child = node->children;
4897 if (child == NULL) {
4898 if (ctxt->error != NULL)
4899 ctxt->error(ctxt->userData,
4900 "xmlRelaxNGParseattribute: attribute has no children\n");
4904 old_flags = ctxt->flags;
4905 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
4906 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
4908 child = child->next;
4910 if (child != NULL) {
4911 cur = xmlRelaxNGParsePattern(ctxt, child);
4913 switch (cur->type) {
4914 case XML_RELAXNG_EMPTY:
4915 case XML_RELAXNG_NOT_ALLOWED:
4916 case XML_RELAXNG_TEXT:
4917 case XML_RELAXNG_ELEMENT:
4918 case XML_RELAXNG_DATATYPE:
4919 case XML_RELAXNG_VALUE:
4920 case XML_RELAXNG_LIST:
4921 case XML_RELAXNG_REF:
4922 case XML_RELAXNG_PARENTREF:
4923 case XML_RELAXNG_EXTERNALREF:
4924 case XML_RELAXNG_DEF:
4925 case XML_RELAXNG_ONEORMORE:
4926 case XML_RELAXNG_ZEROORMORE:
4927 case XML_RELAXNG_OPTIONAL:
4928 case XML_RELAXNG_CHOICE:
4929 case XML_RELAXNG_GROUP:
4930 case XML_RELAXNG_INTERLEAVE:
4931 case XML_RELAXNG_ATTRIBUTE:
4935 case XML_RELAXNG_START:
4936 case XML_RELAXNG_PARAM:
4937 case XML_RELAXNG_EXCEPT:
4938 if (ctxt->error != NULL)
4939 ctxt->error(ctxt->userData,
4940 "attribute has invalid content\n");
4943 case XML_RELAXNG_NOOP:
4944 if (ctxt->error != NULL)
4945 ctxt->error(ctxt->userData,
4946 "RNG Internal error, noop found in attribute\n");
4951 child = child->next;
4953 if (child != NULL) {
4954 if (ctxt->error != NULL)
4955 ctxt->error(ctxt->userData, "attribute has multiple children\n");
4958 ctxt->flags = old_flags;
4963 * xmlRelaxNGParseExceptNameClass:
4964 * @ctxt: a Relax-NG parser context
4965 * @node: the except node
4966 * @attr: 1 if within an attribute, 0 if within an element
4968 * parse the content of a RelaxNG nameClass node.
4970 * Returns the definition pointer or NULL in case of error.
4972 static xmlRelaxNGDefinePtr
4973 xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
4974 xmlNodePtr node, int attr) {
4975 xmlRelaxNGDefinePtr ret, cur, last = NULL;
4978 if (!IS_RELAXNG(node, "except")) {
4979 if (ctxt->error != NULL)
4980 ctxt->error(ctxt->userData,
4981 "Expecting an except node\n");
4985 if (node->next != NULL) {
4986 if (ctxt->error != NULL)
4987 ctxt->error(ctxt->userData,
4988 "exceptNameClass allows only a single except node\n");
4991 if (node->children == NULL) {
4992 if (ctxt->error != NULL)
4993 ctxt->error(ctxt->userData,
4994 "except has no content\n");
4999 ret = xmlRelaxNGNewDefine(ctxt, node);
5002 ret->type = XML_RELAXNG_EXCEPT;
5003 child = node->children;
5004 while (child != NULL) {
5005 cur = xmlRelaxNGNewDefine(ctxt, child);
5009 cur->type = XML_RELAXNG_ATTRIBUTE;
5011 cur->type = XML_RELAXNG_ELEMENT;
5013 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
5021 child = child->next;
5028 * xmlRelaxNGParseNameClass:
5029 * @ctxt: a Relax-NG parser context
5030 * @node: the nameClass node
5031 * @def: the current definition
5033 * parse the content of a RelaxNG nameClass node.
5035 * Returns the definition pointer or NULL in case of error.
5037 static xmlRelaxNGDefinePtr
5038 xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
5039 xmlRelaxNGDefinePtr def) {
5040 xmlRelaxNGDefinePtr ret, tmp;
5044 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
5045 (IS_RELAXNG(node, "nsName"))) {
5046 if ((def->type != XML_RELAXNG_ELEMENT) &&
5047 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5048 ret = xmlRelaxNGNewDefine(ctxt, node);
5052 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5053 ret->type = XML_RELAXNG_ATTRIBUTE;
5055 ret->type = XML_RELAXNG_ELEMENT;
5058 if (IS_RELAXNG(node, "name")) {
5059 val = xmlNodeGetContent(node);
5060 xmlRelaxNGNormExtSpace(val);
5061 if (xmlValidateNCName(val, 0)) {
5062 if (ctxt->error != NULL) {
5063 if (node->parent != NULL)
5064 ctxt->error(ctxt->userData,
5065 "Element %s name '%s' is not an NCName\n",
5066 node->parent->name, val);
5068 ctxt->error(ctxt->userData,
5069 "name '%s' is not an NCName\n",
5075 val = xmlGetProp(node, BAD_CAST "ns");
5077 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5079 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5080 ctxt->error(ctxt->userData,
5081 "Attribute with namespace '%s' is not allowed\n",
5085 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5088 (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
5089 ctxt->error(ctxt->userData,
5090 "Attribute with QName 'xmlns' is not allowed\n",
5094 } else if (IS_RELAXNG(node, "anyName")) {
5097 if (node->children != NULL) {
5099 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5100 (def->type == XML_RELAXNG_ATTRIBUTE));
5102 } else if (IS_RELAXNG(node, "nsName")) {
5104 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5105 if (ret->ns == NULL) {
5106 if (ctxt->error != NULL)
5107 ctxt->error(ctxt->userData,
5108 "nsName has no ns attribute\n");
5111 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5112 (ret->ns != NULL) &&
5113 (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5114 ctxt->error(ctxt->userData,
5115 "Attribute with namespace '%s' is not allowed\n",
5119 if (node->children != NULL) {
5121 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5122 (def->type == XML_RELAXNG_ATTRIBUTE));
5124 } else if (IS_RELAXNG(node, "choice")) {
5126 xmlRelaxNGDefinePtr last = NULL;
5128 ret = xmlRelaxNGNewDefine(ctxt, node);
5132 ret->type = XML_RELAXNG_CHOICE;
5134 if (node->children == NULL) {
5135 if (ctxt->error != NULL)
5136 ctxt->error(ctxt->userData,
5137 "Element choice is empty\n");
5141 child = node->children;
5142 while (child != NULL) {
5143 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5146 last = ret->nameClass = tmp;
5152 child = child->next;
5156 if (ctxt->error != NULL)
5157 ctxt->error(ctxt->userData,
5158 "expecting name, anyName, nsName or choice : got %s\n",
5164 if (def->nameClass == NULL) {
5165 def->nameClass = ret;
5167 tmp = def->nameClass;
5168 while (tmp->next != NULL) {
5178 * xmlRelaxNGParseElement:
5179 * @ctxt: a Relax-NG parser context
5180 * @node: the element node
5182 * parse the content of a RelaxNG element node.
5184 * Returns the definition pointer or NULL in case of error.
5186 static xmlRelaxNGDefinePtr
5187 xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
5188 xmlRelaxNGDefinePtr ret, cur, last;
5190 const xmlChar *olddefine;
5192 ret = xmlRelaxNGNewDefine(ctxt, node);
5195 ret->type = XML_RELAXNG_ELEMENT;
5196 ret->parent = ctxt->def;
5197 child = node->children;
5198 if (child == NULL) {
5199 if (ctxt->error != NULL)
5200 ctxt->error(ctxt->userData,
5201 "xmlRelaxNGParseElement: element has no children\n");
5205 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5207 child = child->next;
5209 if (child == NULL) {
5210 if (ctxt->error != NULL)
5211 ctxt->error(ctxt->userData,
5212 "xmlRelaxNGParseElement: element has no content\n");
5216 olddefine = ctxt->define;
5217 ctxt->define = NULL;
5219 while (child != NULL) {
5220 cur = xmlRelaxNGParsePattern(ctxt, child);
5223 switch (cur->type) {
5224 case XML_RELAXNG_EMPTY:
5225 case XML_RELAXNG_NOT_ALLOWED:
5226 case XML_RELAXNG_TEXT:
5227 case XML_RELAXNG_ELEMENT:
5228 case XML_RELAXNG_DATATYPE:
5229 case XML_RELAXNG_VALUE:
5230 case XML_RELAXNG_LIST:
5231 case XML_RELAXNG_REF:
5232 case XML_RELAXNG_PARENTREF:
5233 case XML_RELAXNG_EXTERNALREF:
5234 case XML_RELAXNG_DEF:
5235 case XML_RELAXNG_ZEROORMORE:
5236 case XML_RELAXNG_ONEORMORE:
5237 case XML_RELAXNG_OPTIONAL:
5238 case XML_RELAXNG_CHOICE:
5239 case XML_RELAXNG_GROUP:
5240 case XML_RELAXNG_INTERLEAVE:
5242 ret->content = last = cur;
5244 if ((last->type == XML_RELAXNG_ELEMENT) &&
5245 (ret->content == last)) {
5246 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5247 if (ret->content != NULL) {
5248 ret->content->type = XML_RELAXNG_GROUP;
5249 ret->content->content = last;
5251 ret->content = last;
5258 case XML_RELAXNG_ATTRIBUTE:
5260 cur->next = ret->attrs;
5263 case XML_RELAXNG_START:
5264 if (ctxt->error != NULL)
5265 ctxt->error(ctxt->userData,
5266 "RNG Internal error, start found in element\n");
5269 case XML_RELAXNG_PARAM:
5270 if (ctxt->error != NULL)
5271 ctxt->error(ctxt->userData,
5272 "RNG Internal error, param found in element\n");
5275 case XML_RELAXNG_EXCEPT:
5276 if (ctxt->error != NULL)
5277 ctxt->error(ctxt->userData,
5278 "RNG Internal error, except found in element\n");
5281 case XML_RELAXNG_NOOP:
5282 if (ctxt->error != NULL)
5283 ctxt->error(ctxt->userData,
5284 "RNG Internal error, noop found in element\n");
5289 child = child->next;
5291 ctxt->define = olddefine;
5296 * xmlRelaxNGParsePatterns:
5297 * @ctxt: a Relax-NG parser context
5298 * @nodes: list of nodes
5299 * @group: use an implicit <group> for elements
5301 * parse the content of a RelaxNG start node.
5303 * Returns the definition pointer or NULL in case of error.
5305 static xmlRelaxNGDefinePtr
5306 xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
5308 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
5311 while (nodes != NULL) {
5312 if (IS_RELAXNG(nodes, "element")) {
5313 cur = xmlRelaxNGParseElement(ctxt, nodes);
5317 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5319 def = xmlRelaxNGNewDefine(ctxt, nodes);
5320 def->type = XML_RELAXNG_GROUP;
5321 def->content = last;
5326 cur->parent = parent;
5328 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5338 nodes = nodes->next;
5344 * xmlRelaxNGParseStart:
5345 * @ctxt: a Relax-NG parser context
5346 * @nodes: start children nodes
5348 * parse the content of a RelaxNG start node.
5350 * Returns 0 in case of success, -1 in case of error
5353 xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
5355 xmlRelaxNGDefinePtr def = NULL, last;
5357 if (nodes == NULL) {
5358 if (ctxt->error != NULL)
5359 ctxt->error(ctxt->userData,
5360 "start has no children\n");
5364 if (IS_RELAXNG(nodes, "empty")) {
5365 def = xmlRelaxNGNewDefine(ctxt, nodes);
5368 def->type = XML_RELAXNG_EMPTY;
5369 if (nodes->children != NULL) {
5370 if (ctxt->error != NULL)
5371 ctxt->error(ctxt->userData, "element empty is not empty\n");
5374 } else if (IS_RELAXNG(nodes, "notAllowed")) {
5375 def = xmlRelaxNGNewDefine(ctxt, nodes);
5378 def->type = XML_RELAXNG_NOT_ALLOWED;
5379 if (nodes->children != NULL) {
5380 if (ctxt->error != NULL)
5381 ctxt->error(ctxt->userData,
5382 "element notAllowed is not empty\n");
5386 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
5388 if (ctxt->grammar->start != NULL) {
5389 last = ctxt->grammar->start;
5390 while (last->next != NULL)
5394 ctxt->grammar->start = def;
5396 nodes = nodes->next;
5397 if (nodes != NULL) {
5398 if (ctxt->error != NULL)
5399 ctxt->error(ctxt->userData,
5400 "start more than one children\n");
5408 * xmlRelaxNGParseGrammarContent:
5409 * @ctxt: a Relax-NG parser context
5410 * @nodes: grammar children nodes
5412 * parse the content of a RelaxNG grammar node.
5414 * Returns 0 in case of success, -1 in case of error
5417 xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5421 if (nodes == NULL) {
5422 if (ctxt->error != NULL)
5423 ctxt->error(ctxt->userData,
5424 "grammar has no children\n");
5428 while (nodes != NULL) {
5429 if (IS_RELAXNG(nodes, "start")) {
5430 if (nodes->children == NULL) {
5431 if (ctxt->error != NULL)
5432 ctxt->error(ctxt->userData,
5433 "start has no children\n");
5436 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5440 } else if (IS_RELAXNG(nodes, "define")) {
5441 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5444 } else if (IS_RELAXNG(nodes, "include")) {
5445 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5449 if (ctxt->error != NULL)
5450 ctxt->error(ctxt->userData,
5451 "grammar has unexpected child %s\n", nodes->name);
5455 nodes = nodes->next;
5461 * xmlRelaxNGCheckReference:
5463 * @ctxt: a Relax-NG parser context
5464 * @name: the name associated to the defines
5466 * Applies the 4.17. combine attribute rule for all the define
5467 * element of a given grammar using the same name.
5470 xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
5471 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
5472 xmlRelaxNGGrammarPtr grammar;
5473 xmlRelaxNGDefinePtr def, cur;
5475 grammar = ctxt->grammar;
5476 if (grammar == NULL) {
5477 if (ctxt->error != NULL)
5478 ctxt->error(ctxt->userData,
5479 "Internal error: no grammar in CheckReference %s\n",
5484 if (ref->content != NULL) {
5485 if (ctxt->error != NULL)
5486 ctxt->error(ctxt->userData,
5487 "Internal error: reference has content in CheckReference %s\n",
5492 if (grammar->defs != NULL) {
5493 def = xmlHashLookup(grammar->defs, name);
5496 while (cur != NULL) {
5498 cur = cur->nextHash;
5501 if (ctxt->error != NULL)
5502 ctxt->error(ctxt->userData,
5503 "Reference %s has no matching definition\n",
5508 if (ctxt->error != NULL)
5509 ctxt->error(ctxt->userData,
5510 "Reference %s has no matching definition\n",
5517 * xmlRelaxNGCheckCombine:
5518 * @define: the define(s) list
5519 * @ctxt: a Relax-NG parser context
5520 * @name: the name associated to the defines
5522 * Applies the 4.17. combine attribute rule for all the define
5523 * element of a given grammar using the same name.
5526 xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
5527 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
5529 int choiceOrInterleave = -1;
5531 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5533 if (define->nextHash == NULL)
5536 while (cur != NULL) {
5537 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5538 if (combine != NULL) {
5539 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5540 if (choiceOrInterleave == -1)
5541 choiceOrInterleave = 1;
5542 else if (choiceOrInterleave == 0) {
5543 if (ctxt->error != NULL)
5544 ctxt->error(ctxt->userData,
5545 "Defines for %s use both 'choice' and 'interleave'\n",
5549 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5550 if (choiceOrInterleave == -1)
5551 choiceOrInterleave = 0;
5552 else if (choiceOrInterleave == 1) {
5553 if (ctxt->error != NULL)
5554 ctxt->error(ctxt->userData,
5555 "Defines for %s use both 'choice' and 'interleave'\n",
5560 if (ctxt->error != NULL)
5561 ctxt->error(ctxt->userData,
5562 "Defines for %s use unknown combine value '%s''\n",
5571 if (ctxt->error != NULL)
5572 ctxt->error(ctxt->userData,
5573 "Some defines for %s needs the combine attribute\n",
5579 cur = cur->nextHash;
5582 xmlGenericError(xmlGenericErrorContext,
5583 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5584 name, choiceOrInterleave);
5586 if (choiceOrInterleave == -1)
5587 choiceOrInterleave = 0;
5588 cur = xmlRelaxNGNewDefine(ctxt, define->node);
5591 if (choiceOrInterleave == 0)
5592 cur->type = XML_RELAXNG_INTERLEAVE;
5594 cur->type = XML_RELAXNG_CHOICE;
5597 while (tmp != NULL) {
5598 if (tmp->content != NULL) {
5599 if (tmp->content->next != NULL) {
5601 * we need first to create a wrapper.
5603 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5606 tmp2->type = XML_RELAXNG_GROUP;
5607 tmp2->content = tmp->content;
5609 tmp2 = tmp->content;
5612 cur->content = tmp2;
5619 tmp = tmp->nextHash;
5621 define->content = cur;
5622 if (choiceOrInterleave == 0) {
5623 if (ctxt->interleaves == NULL)
5624 ctxt->interleaves = xmlHashCreate(10);
5625 if (ctxt->interleaves == NULL) {
5626 if (ctxt->error != NULL)
5627 ctxt->error(ctxt->userData,
5628 "Failed to create interleaves hash table\n");
5633 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5634 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5635 if (ctxt->error != NULL)
5636 ctxt->error(ctxt->userData,
5637 "Failed to add %s to hash table\n", tmpname);
5645 * xmlRelaxNGCombineStart:
5646 * @ctxt: a Relax-NG parser context
5647 * @grammar: the grammar
5649 * Applies the 4.17. combine rule for all the start
5650 * element of a given grammar.
5653 xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
5654 xmlRelaxNGGrammarPtr grammar) {
5655 xmlRelaxNGDefinePtr starts;
5657 int choiceOrInterleave = -1;
5659 xmlRelaxNGDefinePtr cur;
5661 starts = grammar->start;
5662 if ((starts == NULL) || (starts->next == NULL))
5665 while (cur != NULL) {
5666 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5667 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5669 if (ctxt->error != NULL)
5670 ctxt->error(ctxt->userData,
5671 "Internal error: start element not found\n");
5674 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5677 if (combine != NULL) {
5678 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5679 if (choiceOrInterleave == -1)
5680 choiceOrInterleave = 1;
5681 else if (choiceOrInterleave == 0) {
5682 if (ctxt->error != NULL)
5683 ctxt->error(ctxt->userData,
5684 "<start> use both 'choice' and 'interleave'\n");
5687 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5688 if (choiceOrInterleave == -1)
5689 choiceOrInterleave = 0;
5690 else if (choiceOrInterleave == 1) {
5691 if (ctxt->error != NULL)
5692 ctxt->error(ctxt->userData,
5693 "<start> use both 'choice' and 'interleave'\n");
5697 if (ctxt->error != NULL)
5698 ctxt->error(ctxt->userData,
5699 "<start> uses unknown combine value '%s''\n", combine);
5707 if (ctxt->error != NULL)
5708 ctxt->error(ctxt->userData,
5709 "Some <start> element miss the combine attribute\n");
5717 xmlGenericError(xmlGenericErrorContext,
5718 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5719 choiceOrInterleave);
5721 if (choiceOrInterleave == -1)
5722 choiceOrInterleave = 0;
5723 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
5726 if (choiceOrInterleave == 0)
5727 cur->type = XML_RELAXNG_INTERLEAVE;
5729 cur->type = XML_RELAXNG_CHOICE;
5730 cur->content = grammar->start;
5731 grammar->start = cur;
5732 if (choiceOrInterleave == 0) {
5733 if (ctxt->interleaves == NULL)
5734 ctxt->interleaves = xmlHashCreate(10);
5735 if (ctxt->interleaves == NULL) {
5736 if (ctxt->error != NULL)
5737 ctxt->error(ctxt->userData,
5738 "Failed to create interleaves hash table\n");
5743 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5744 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5745 if (ctxt->error != NULL)
5746 ctxt->error(ctxt->userData,
5747 "Failed to add %s to hash table\n", tmpname);
5755 * xmlRelaxNGCheckCycles:
5756 * @ctxt: a Relax-NG parser context
5757 * @nodes: grammar children nodes
5758 * @depth: the counter
5762 * Returns 0 if check passed, and -1 in case of error
5765 xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5766 xmlRelaxNGDefinePtr cur, int depth) {
5769 while ((ret == 0) && (cur != NULL)) {
5770 if ((cur->type == XML_RELAXNG_REF) ||
5771 (cur->type == XML_RELAXNG_PARENTREF)) {
5772 if (cur->depth == -1) {
5774 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5776 } else if (depth == cur->depth) {
5777 if (ctxt->error != NULL)
5778 ctxt->error(ctxt->userData,
5779 "Detected a cycle in %s references\n", cur->name);
5783 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5784 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5786 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5794 * xmlRelaxNGTryUnlink:
5795 * @ctxt: a Relax-NG parser context
5796 * @cur: the definition to unlink
5797 * @parent: the parent definition
5798 * @prev: the previous sibling definition
5800 * Try to unlink a definition. If not possble make it a NOOP
5802 * Returns the new prev definition
5804 static xmlRelaxNGDefinePtr
5805 xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5806 xmlRelaxNGDefinePtr cur,
5807 xmlRelaxNGDefinePtr parent,
5808 xmlRelaxNGDefinePtr prev) {
5810 prev->next = cur->next;
5812 if (parent != NULL) {
5813 if (parent->content == cur)
5814 parent->content = cur->next;
5815 else if (parent->attrs == cur)
5816 parent->attrs = cur->next;
5817 else if (parent->nameClass == cur)
5818 parent->nameClass = cur->next;
5820 cur->type = XML_RELAXNG_NOOP;
5828 * xmlRelaxNGSimplify:
5829 * @ctxt: a Relax-NG parser context
5830 * @nodes: grammar children nodes
5832 * Check for simplification of empty and notAllowed
5835 xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5836 xmlRelaxNGDefinePtr cur,
5837 xmlRelaxNGDefinePtr parent) {
5838 xmlRelaxNGDefinePtr prev = NULL;
5840 while (cur != NULL) {
5841 if ((cur->type == XML_RELAXNG_REF) ||
5842 (cur->type == XML_RELAXNG_PARENTREF)) {
5843 if (cur->depth != -3) {
5845 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5847 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5848 cur->parent = parent;
5849 if ((parent != NULL) &&
5850 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5851 (parent->type == XML_RELAXNG_LIST) ||
5852 (parent->type == XML_RELAXNG_GROUP) ||
5853 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5854 (parent->type == XML_RELAXNG_ONEORMORE) ||
5855 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5856 parent->type = XML_RELAXNG_NOT_ALLOWED;
5859 if ((parent != NULL) &&
5860 (parent->type == XML_RELAXNG_CHOICE)) {
5861 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5864 } else if (cur->type == XML_RELAXNG_EMPTY){
5865 cur->parent = parent;
5866 if ((parent != NULL) &&
5867 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5868 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5869 parent->type = XML_RELAXNG_EMPTY;
5872 if ((parent != NULL) &&
5873 ((parent->type == XML_RELAXNG_GROUP) ||
5874 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5875 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5879 cur->parent = parent;
5880 if (cur->content != NULL)
5881 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5882 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5883 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5884 if (cur->nameClass != NULL)
5885 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5887 * On Elements, try to move attribute only generating rules on
5890 if (cur->type == XML_RELAXNG_ELEMENT) {
5892 xmlRelaxNGDefinePtr tmp, pre;
5894 while (cur->content != NULL) {
5895 attronly = xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5896 if (attronly == 1) {
5898 * migrate cur->content to attrs
5901 cur->content = tmp->next;
5902 tmp->next = cur->attrs;
5906 * cur->content can generate elements or text
5912 while ((pre != NULL) && (pre->next != NULL)) {
5914 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5915 if (attronly == 1) {
5917 * migrate tmp to attrs
5919 pre->next = tmp->next;
5920 tmp->next = cur->attrs;
5928 * This may result in a simplification
5930 if ((cur->type == XML_RELAXNG_GROUP) ||
5931 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5932 if (cur->content == NULL)
5933 cur->type = XML_RELAXNG_EMPTY;
5934 else if (cur->content->next == NULL) {
5935 if ((parent == NULL) && (prev == NULL)) {
5936 cur->type = XML_RELAXNG_NOOP;
5937 } else if (prev == NULL) {
5938 parent->content = cur->content;
5939 cur->content->next = cur->next;
5942 cur->content->next = cur->next;
5943 prev->next = cur->content;
5949 * the current node may have been transformed back
5951 if ((cur->type == XML_RELAXNG_EXCEPT) &&
5952 (cur->content != NULL) &&
5953 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
5954 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5955 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5956 if ((parent != NULL) &&
5957 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5958 (parent->type == XML_RELAXNG_LIST) ||
5959 (parent->type == XML_RELAXNG_GROUP) ||
5960 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5961 (parent->type == XML_RELAXNG_ONEORMORE) ||
5962 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5963 parent->type = XML_RELAXNG_NOT_ALLOWED;
5966 if ((parent != NULL) &&
5967 (parent->type == XML_RELAXNG_CHOICE)) {
5968 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5971 } else if (cur->type == XML_RELAXNG_EMPTY){
5972 if ((parent != NULL) &&
5973 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5974 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5975 parent->type = XML_RELAXNG_EMPTY;
5978 if ((parent != NULL) &&
5979 ((parent->type == XML_RELAXNG_GROUP) ||
5980 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5981 (parent->type == XML_RELAXNG_CHOICE))) {
5982 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5994 * xmlRelaxNGGroupContentType:
5995 * @ct1: the first content type
5996 * @ct2: the second content type
5998 * Try to group 2 content types
6000 * Returns the content type
6002 static xmlRelaxNGContentType
6003 xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
6004 xmlRelaxNGContentType ct2) {
6005 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6006 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6007 return(XML_RELAXNG_CONTENT_ERROR);
6008 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
6010 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
6012 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
6013 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6014 return(XML_RELAXNG_CONTENT_COMPLEX);
6015 return(XML_RELAXNG_CONTENT_ERROR);
6019 * xmlRelaxNGMaxContentType:
6020 * @ct1: the first content type
6021 * @ct2: the second content type
6023 * Compute the max content-type
6025 * Returns the content type
6027 static xmlRelaxNGContentType
6028 xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
6029 xmlRelaxNGContentType ct2) {
6030 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6031 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6032 return(XML_RELAXNG_CONTENT_ERROR);
6033 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
6034 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6035 return(XML_RELAXNG_CONTENT_SIMPLE);
6036 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
6037 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6038 return(XML_RELAXNG_CONTENT_COMPLEX);
6039 return(XML_RELAXNG_CONTENT_EMPTY);
6043 * xmlRelaxNGCheckRules:
6044 * @ctxt: a Relax-NG parser context
6045 * @cur: the current definition
6046 * @flags: some accumulated flags
6047 * @ptype: the parent type
6049 * Check for rules in section 7.1 and 7.2
6051 * Returns the content type of @cur
6053 static xmlRelaxNGContentType
6054 xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6055 xmlRelaxNGDefinePtr cur, int flags,
6056 xmlRelaxNGType ptype) {
6058 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
6060 while (cur != NULL) {
6061 ret = XML_RELAXNG_CONTENT_EMPTY;
6062 if ((cur->type == XML_RELAXNG_REF) ||
6063 (cur->type == XML_RELAXNG_PARENTREF)) {
6064 if (flags & XML_RELAXNG_IN_LIST) {
6065 if (ctxt->error != NULL)
6066 ctxt->error(ctxt->userData,
6067 "Found forbidden pattern list//ref\n");
6070 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6071 if (ctxt->error != NULL)
6072 ctxt->error(ctxt->userData,
6073 "Found forbidden pattern data/except//ref\n");
6076 if (cur->depth > -4) {
6078 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6080 cur->depth = ret - 15 ;
6081 } else if (cur->depth == -4) {
6082 ret = XML_RELAXNG_CONTENT_COMPLEX;
6084 ret = (xmlRelaxNGContentType) cur->depth + 15;
6086 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6088 * The 7.3 Attribute derivation rule for groups is plugged there
6090 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6091 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6092 if (ctxt->error != NULL)
6093 ctxt->error(ctxt->userData,
6094 "Found forbidden pattern data/except//element(ref)\n");
6097 if (flags & XML_RELAXNG_IN_LIST) {
6098 if (ctxt->error != NULL)
6099 ctxt->error(ctxt->userData,
6100 "Found forbidden pattern list//element(ref)\n");
6103 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6104 if (ctxt->error != NULL)
6105 ctxt->error(ctxt->userData,
6106 "Found forbidden pattern attribute//element(ref)\n");
6109 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6110 if (ctxt->error != NULL)
6111 ctxt->error(ctxt->userData,
6112 "Found forbidden pattern attribute//element(ref)\n");
6116 * reset since in the simple form elements are only child
6120 ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6121 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6122 if (ctxt->error != NULL)
6123 ctxt->error(ctxt->userData,
6124 "Element %s attributes have a content type error\n",
6128 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6129 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6130 if (ctxt->error != NULL)
6131 ctxt->error(ctxt->userData,
6132 "Element %s has a content type error\n",
6136 ret = XML_RELAXNG_CONTENT_COMPLEX;
6138 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6139 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6140 if (ctxt->error != NULL)
6141 ctxt->error(ctxt->userData,
6142 "Found forbidden pattern attribute//attribute\n");
6145 if (flags & XML_RELAXNG_IN_LIST) {
6146 if (ctxt->error != NULL)
6147 ctxt->error(ctxt->userData,
6148 "Found forbidden pattern list//attribute\n");
6151 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6152 if (ctxt->error != NULL)
6153 ctxt->error(ctxt->userData,
6154 "Found forbidden pattern oneOrMore//group//attribute\n");
6157 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6158 if (ctxt->error != NULL)
6159 ctxt->error(ctxt->userData,
6160 "Found forbidden pattern oneOrMore//interleave//attribute\n");
6163 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6164 if (ctxt->error != NULL)
6165 ctxt->error(ctxt->userData,
6166 "Found forbidden pattern data/except//attribute\n");
6169 if (flags & XML_RELAXNG_IN_START) {
6170 if (ctxt->error != NULL)
6171 ctxt->error(ctxt->userData,
6172 "Found forbidden pattern start//attribute\n");
6175 if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) {
6176 if (cur->ns == NULL) {
6177 if (ctxt->error != NULL)
6178 ctxt->error(ctxt->userData,
6179 "Found anyName attribute without oneOrMore ancestor\n");
6182 if (ctxt->error != NULL)
6183 ctxt->error(ctxt->userData,
6184 "Found nsName attribute without oneOrMore ancestor\n");
6188 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6189 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6190 ret = XML_RELAXNG_CONTENT_EMPTY;
6191 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6192 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6193 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6194 if (ctxt->error != NULL)
6195 ctxt->error(ctxt->userData,
6196 "Found forbidden pattern data/except//oneOrMore\n");
6199 if (flags & XML_RELAXNG_IN_START) {
6200 if (ctxt->error != NULL)
6201 ctxt->error(ctxt->userData,
6202 "Found forbidden pattern start//oneOrMore\n");
6205 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6206 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6207 ret = xmlRelaxNGGroupContentType(ret, ret);
6208 } else if (cur->type == XML_RELAXNG_LIST) {
6209 if (flags & XML_RELAXNG_IN_LIST) {
6210 if (ctxt->error != NULL)
6211 ctxt->error(ctxt->userData,
6212 "Found forbidden pattern list//list\n");
6215 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6216 if (ctxt->error != NULL)
6217 ctxt->error(ctxt->userData,
6218 "Found forbidden pattern data/except//list\n");
6221 if (flags & XML_RELAXNG_IN_START) {
6222 if (ctxt->error != NULL)
6223 ctxt->error(ctxt->userData,
6224 "Found forbidden pattern start//list\n");
6227 nflags = flags | XML_RELAXNG_IN_LIST;
6228 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6229 } else if (cur->type == XML_RELAXNG_GROUP) {
6230 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6231 if (ctxt->error != NULL)
6232 ctxt->error(ctxt->userData,
6233 "Found forbidden pattern data/except//group\n");
6236 if (flags & XML_RELAXNG_IN_START) {
6237 if (ctxt->error != NULL)
6238 ctxt->error(ctxt->userData,
6239 "Found forbidden pattern start//group\n");
6242 if (flags & XML_RELAXNG_IN_ONEORMORE)
6243 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6246 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6248 * The 7.3 Attribute derivation rule for groups is plugged there
6250 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6251 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6252 if (flags & XML_RELAXNG_IN_LIST) {
6253 if (ctxt->error != NULL)
6254 ctxt->error(ctxt->userData,
6255 "Found forbidden pattern list//interleave\n");
6258 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6259 if (ctxt->error != NULL)
6260 ctxt->error(ctxt->userData,
6261 "Found forbidden pattern data/except//interleave\n");
6264 if (flags & XML_RELAXNG_IN_START) {
6265 if (ctxt->error != NULL)
6266 ctxt->error(ctxt->userData,
6267 "Found forbidden pattern start//interleave\n");
6270 if (flags & XML_RELAXNG_IN_ONEORMORE)
6271 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6274 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6275 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6276 if ((cur->parent != NULL) &&
6277 (cur->parent->type == XML_RELAXNG_DATATYPE))
6278 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6281 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6282 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6283 if (flags & XML_RELAXNG_IN_START) {
6284 if (ctxt->error != NULL)
6285 ctxt->error(ctxt->userData,
6286 "Found forbidden pattern start//data\n");
6289 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6290 ret = XML_RELAXNG_CONTENT_SIMPLE;
6291 } else if (cur->type == XML_RELAXNG_VALUE) {
6292 if (flags & XML_RELAXNG_IN_START) {
6293 if (ctxt->error != NULL)
6294 ctxt->error(ctxt->userData,
6295 "Found forbidden pattern start//value\n");
6298 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6299 ret = XML_RELAXNG_CONTENT_SIMPLE;
6300 } else if (cur->type == XML_RELAXNG_TEXT) {
6301 if (flags & XML_RELAXNG_IN_LIST) {
6302 if (ctxt->error != NULL)
6303 ctxt->error(ctxt->userData,
6304 "Found forbidden pattern list//text\n");
6307 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6308 if (ctxt->error != NULL)
6309 ctxt->error(ctxt->userData,
6310 "Found forbidden pattern data/except//text\n");
6313 if (flags & XML_RELAXNG_IN_START) {
6314 if (ctxt->error != NULL)
6315 ctxt->error(ctxt->userData,
6316 "Found forbidden pattern start//text\n");
6319 ret = XML_RELAXNG_CONTENT_COMPLEX;
6320 } else if (cur->type == XML_RELAXNG_EMPTY) {
6321 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6322 if (ctxt->error != NULL)
6323 ctxt->error(ctxt->userData,
6324 "Found forbidden pattern data/except//empty\n");
6327 if (flags & XML_RELAXNG_IN_START) {
6328 if (ctxt->error != NULL)
6329 ctxt->error(ctxt->userData,
6330 "Found forbidden pattern start//empty\n");
6333 ret = XML_RELAXNG_CONTENT_EMPTY;
6334 } else if (cur->type == XML_RELAXNG_CHOICE) {
6335 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6336 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6338 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6341 if (ptype == XML_RELAXNG_GROUP) {
6342 val = xmlRelaxNGGroupContentType(val, ret);
6343 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6344 tmp = xmlRelaxNGGroupContentType(val, ret);
6345 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6346 tmp = xmlRelaxNGMaxContentType(val, ret);
6347 } else if (ptype == XML_RELAXNG_CHOICE) {
6348 val = xmlRelaxNGMaxContentType(val, ret);
6349 } else if (ptype == XML_RELAXNG_LIST) {
6350 val = XML_RELAXNG_CONTENT_SIMPLE;
6351 } else if (ptype == XML_RELAXNG_EXCEPT) {
6352 if (ret == XML_RELAXNG_CONTENT_ERROR)
6353 val = XML_RELAXNG_CONTENT_ERROR;
6355 val = XML_RELAXNG_CONTENT_SIMPLE;
6357 val = xmlRelaxNGGroupContentType(val, ret);
6365 * xmlRelaxNGParseGrammar:
6366 * @ctxt: a Relax-NG parser context
6367 * @nodes: grammar children nodes
6369 * parse a Relax-NG <grammar> node
6371 * Returns the internal xmlRelaxNGGrammarPtr built or
6372 * NULL in case of error
6374 static xmlRelaxNGGrammarPtr
6375 xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
6376 xmlRelaxNGGrammarPtr ret, tmp, old;
6378 #ifdef DEBUG_GRAMMAR
6379 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6382 ret = xmlRelaxNGNewGrammar(ctxt);
6387 * Link the new grammar in the tree
6389 ret->parent = ctxt->grammar;
6390 if (ctxt->grammar != NULL) {
6391 tmp = ctxt->grammar->children;
6393 ctxt->grammar->children = ret;
6395 while (tmp->next != NULL)
6401 old = ctxt->grammar;
6402 ctxt->grammar = ret;
6403 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6404 ctxt->grammar = ret;
6405 if (ctxt->grammar == NULL) {
6406 if (ctxt->error != NULL)
6407 ctxt->error(ctxt->userData,
6408 "Failed to parse <grammar> content\n");
6410 } else if (ctxt->grammar->start == NULL) {
6411 if (ctxt->error != NULL)
6412 ctxt->error(ctxt->userData,
6413 "Element <grammar> has no <start>\n");
6418 * Apply 4.17 mergingd rules to defines and starts
6420 xmlRelaxNGCombineStart(ctxt, ret);
6421 if (ret->defs != NULL) {
6422 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6427 * link together defines and refs in this grammar
6429 if (ret->refs != NULL) {
6430 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6434 ctxt->grammar = old;
6439 * xmlRelaxNGParseDocument:
6440 * @ctxt: a Relax-NG parser context
6441 * @node: the root node of the RelaxNG schema
6443 * parse a Relax-NG definition resource and build an internal
6444 * xmlRelaxNG struture which can be used to validate instances.
6446 * Returns the internal XML RelaxNG structure built or
6447 * NULL in case of error
6449 static xmlRelaxNGPtr
6450 xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
6451 xmlRelaxNGPtr schema = NULL;
6452 const xmlChar *olddefine;
6453 xmlRelaxNGGrammarPtr old;
6455 if ((ctxt == NULL) || (node == NULL))
6458 schema = xmlRelaxNGNewRelaxNG(ctxt);
6462 olddefine = ctxt->define;
6463 ctxt->define = NULL;
6464 if (IS_RELAXNG(node, "grammar")) {
6465 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
6467 xmlRelaxNGGrammarPtr tmp, ret;
6469 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6470 if (schema->topgrammar == NULL) {
6474 * Link the new grammar in the tree
6476 ret->parent = ctxt->grammar;
6477 if (ctxt->grammar != NULL) {
6478 tmp = ctxt->grammar->children;
6480 ctxt->grammar->children = ret;
6482 while (tmp->next != NULL)
6487 old = ctxt->grammar;
6488 ctxt->grammar = ret;
6489 xmlRelaxNGParseStart(ctxt, node);
6491 ctxt->grammar = old;
6493 ctxt->define = olddefine;
6494 if (schema->topgrammar->start != NULL) {
6495 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6496 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6497 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6498 while ((schema->topgrammar->start != NULL) &&
6499 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6500 (schema->topgrammar->start->next != NULL))
6501 schema->topgrammar->start = schema->topgrammar->start->content;
6502 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6503 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6509 xmlGenericError(xmlGenericErrorContext,
6510 "xmlRelaxNGParseDocument() failed\n");
6516 /************************************************************************
6518 * Reading RelaxNGs *
6520 ************************************************************************/
6523 * xmlRelaxNGNewParserCtxt:
6524 * @URL: the location of the schema
6526 * Create an XML RelaxNGs parse context for that file/resource expected
6527 * to contain an XML RelaxNGs file.
6529 * Returns the parser context or NULL in case of error
6531 xmlRelaxNGParserCtxtPtr
6532 xmlRelaxNGNewParserCtxt(const char *URL) {
6533 xmlRelaxNGParserCtxtPtr ret;
6538 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6540 xmlGenericError(xmlGenericErrorContext,
6541 "Failed to allocate new schama parser context for %s\n", URL);
6544 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6545 ret->URL = xmlStrdup((const xmlChar *)URL);
6546 ret->error = xmlGenericError;
6547 ret->userData = xmlGenericErrorContext;
6552 * xmlRelaxNGNewMemParserCtxt:
6553 * @buffer: a pointer to a char array containing the schemas
6554 * @size: the size of the array
6556 * Create an XML RelaxNGs parse context for that memory buffer expected
6557 * to contain an XML RelaxNGs file.
6559 * Returns the parser context or NULL in case of error
6561 xmlRelaxNGParserCtxtPtr
6562 xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) {
6563 xmlRelaxNGParserCtxtPtr ret;
6565 if ((buffer == NULL) || (size <= 0))
6568 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6570 xmlGenericError(xmlGenericErrorContext,
6571 "Failed to allocate new schama parser context\n");
6574 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6575 ret->buffer = buffer;
6577 ret->error = xmlGenericError;
6578 ret->userData = xmlGenericErrorContext;
6583 * xmlRelaxNGNewDocParserCtxt:
6584 * @doc: a preparsed document tree
6586 * Create an XML RelaxNGs parser context for that document.
6587 * Note: since the process of compiling a RelaxNG schemas modifies the
6588 * document, the @doc parameter is duplicated internally.
6590 * Returns the parser context or NULL in case of error
6592 xmlRelaxNGParserCtxtPtr
6593 xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) {
6594 xmlRelaxNGParserCtxtPtr ret;
6599 copy = xmlCopyDoc(doc, 1);
6603 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6605 xmlGenericError(xmlGenericErrorContext,
6606 "Failed to allocate new schama parser context\n");
6609 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6610 ret->document = copy;
6611 ret->userData = xmlGenericErrorContext;
6616 * xmlRelaxNGFreeParserCtxt:
6617 * @ctxt: the schema parser context
6619 * Free the resources associated to the schema parser context
6622 xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) {
6625 if (ctxt->URL != NULL)
6627 if (ctxt->doc != NULL)
6628 xmlRelaxNGFreeDocument(ctxt->doc);
6629 if (ctxt->interleaves != NULL)
6630 xmlHashFree(ctxt->interleaves, NULL);
6631 if (ctxt->documents != NULL)
6632 xmlRelaxNGFreeDocumentList(ctxt->documents);
6633 if (ctxt->includes != NULL)
6634 xmlRelaxNGFreeIncludeList(ctxt->includes);
6635 if (ctxt->docTab != NULL)
6636 xmlFree(ctxt->docTab);
6637 if (ctxt->incTab != NULL)
6638 xmlFree(ctxt->incTab);
6639 if (ctxt->defTab != NULL) {
6642 for (i = 0;i < ctxt->defNr;i++)
6643 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6644 xmlFree(ctxt->defTab);
6650 * xmlRelaxNGNormExtSpace:
6653 * Removes the leading and ending spaces of the value
6654 * The string is modified "in situ"
6657 xmlRelaxNGNormExtSpace(xmlChar *value) {
6658 xmlChar *start = value;
6659 xmlChar *cur = value;
6663 while (IS_BLANK(*cur)) cur++;
6666 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
6670 while (IS_BLANK(*cur)) cur++;
6678 while ((*cur != 0) && (!IS_BLANK(*cur)))
6684 /* don't try to normalize the inner spaces */
6685 while (IS_BLANK(*cur)) cur++;
6696 * xmlRelaxNGCheckAttributes:
6697 * @ctxt: a Relax-NG parser context
6698 * @node: a Relax-NG node
6700 * Check all the attributes on the given node
6703 xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
6704 xmlAttrPtr cur, next;
6706 cur = node->properties;
6707 while (cur != NULL) {
6709 if ((cur->ns == NULL) ||
6710 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6711 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6712 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6713 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6714 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6715 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6716 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6717 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6718 if (ctxt->error != NULL)
6719 ctxt->error(ctxt->userData,
6720 "Attribute %s is not allowed on %s\n",
6721 cur->name, node->name);
6724 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6725 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6726 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6727 if (ctxt->error != NULL)
6728 ctxt->error(ctxt->userData,
6729 "Attribute %s is not allowed on %s\n",
6730 cur->name, node->name);
6733 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6734 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6735 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6736 if (ctxt->error != NULL)
6737 ctxt->error(ctxt->userData,
6738 "Attribute %s is not allowed on %s\n",
6739 cur->name, node->name);
6742 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6743 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6744 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6745 if (ctxt->error != NULL)
6746 ctxt->error(ctxt->userData,
6747 "Attribute %s is not allowed on %s\n",
6748 cur->name, node->name);
6751 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6755 val = xmlNodeListGetString(node->doc, cur->children, 1);
6758 uri = xmlParseURI((const char *) val);
6760 if (ctxt->error != NULL)
6761 ctxt->error(ctxt->userData,
6762 "Attribute %s contains invalid URI %s\n",
6766 if (uri->scheme == NULL) {
6767 if (ctxt->error != NULL)
6768 ctxt->error(ctxt->userData,
6769 "Attribute %s URI %s is not absolute\n",
6773 if (uri->fragment != NULL) {
6774 if (ctxt->error != NULL)
6775 ctxt->error(ctxt->userData,
6776 "Attribute %s URI %s has a fragment ID\n",
6785 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6786 if (ctxt->error != NULL)
6787 ctxt->error(ctxt->userData,
6788 "Unknown attribute %s on %s\n",
6789 cur->name, node->name);
6798 * xmlRelaxNGCleanupTree:
6799 * @ctxt: a Relax-NG parser context
6800 * @root: an xmlNodePtr subtree
6802 * Cleanup the subtree from unwanted nodes for parsing, resolve
6803 * Include and externalRef lookups.
6806 xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) {
6807 xmlNodePtr cur, delete;
6811 while (cur != NULL) {
6812 if (delete != NULL) {
6813 xmlUnlinkNode(delete);
6814 xmlFreeNode(delete);
6817 if (cur->type == XML_ELEMENT_NODE) {
6819 * Simplification 4.1. Annotations
6821 if ((cur->ns == NULL) ||
6822 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6823 if ((cur->parent != NULL) &&
6824 (cur->parent->type == XML_ELEMENT_NODE) &&
6825 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6826 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6827 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6828 if (ctxt->error != NULL)
6829 ctxt->error(ctxt->userData,
6830 "element %s doesn't allow foreign elements\n",
6837 xmlRelaxNGCleanupAttributes(ctxt, cur);
6838 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6839 xmlChar *href, *ns, *base, *URL;
6840 xmlRelaxNGDocumentPtr docu;
6843 ns = xmlGetProp(cur, BAD_CAST "ns");
6846 while ((tmp != NULL) &&
6847 (tmp->type == XML_ELEMENT_NODE)) {
6848 ns = xmlGetProp(tmp, BAD_CAST "ns");
6854 href = xmlGetProp(cur, BAD_CAST "href");
6856 if (ctxt->error != NULL)
6857 ctxt->error(ctxt->userData,
6858 "xmlRelaxNGParse: externalRef has no href attribute\n");
6863 base = xmlNodeGetBase(cur->doc, cur);
6864 URL = xmlBuildURI(href, base);
6866 if (ctxt->error != NULL)
6867 ctxt->error(ctxt->userData,
6868 "Failed to compute URL for externalRef %s\n", href);
6881 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6883 if (ctxt->error != NULL)
6884 ctxt->error(ctxt->userData,
6885 "Failed to load externalRef %s\n", URL);
6894 cur->_private = docu;
6895 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6896 xmlChar *href, *ns, *base, *URL;
6897 xmlRelaxNGIncludePtr incl;
6900 href = xmlGetProp(cur, BAD_CAST "href");
6902 if (ctxt->error != NULL)
6903 ctxt->error(ctxt->userData,
6904 "xmlRelaxNGParse: include has no href attribute\n");
6909 base = xmlNodeGetBase(cur->doc, cur);
6910 URL = xmlBuildURI(href, base);
6912 if (ctxt->error != NULL)
6913 ctxt->error(ctxt->userData,
6914 "Failed to compute URL for include %s\n", href);
6927 ns = xmlGetProp(cur, BAD_CAST "ns");
6930 while ((tmp != NULL) &&
6931 (tmp->type == XML_ELEMENT_NODE)) {
6932 ns = xmlGetProp(tmp, BAD_CAST "ns");
6938 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6942 if (ctxt->error != NULL)
6943 ctxt->error(ctxt->userData,
6944 "Failed to load include %s\n", URL);
6951 cur->_private = incl;
6952 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
6953 (xmlStrEqual(cur->name, BAD_CAST "attribute"))) {
6955 xmlNodePtr text = NULL;
6958 * Simplification 4.8. name attribute of element
6959 * and attribute elements
6961 name = xmlGetProp(cur, BAD_CAST "name");
6963 if (cur->children == NULL) {
6964 text = xmlNewChild(cur, cur->ns, BAD_CAST "name",
6968 node = xmlNewNode(cur->ns, BAD_CAST "name");
6970 xmlAddPrevSibling(cur->children, node);
6971 text = xmlNewText(name);
6972 xmlAddChild(node, text);
6977 if (ctxt->error != NULL)
6978 ctxt->error(ctxt->userData,
6979 "Failed to create a name %s element\n", name);
6982 xmlUnsetProp(cur, BAD_CAST "name");
6984 ns = xmlGetProp(cur, BAD_CAST "ns");
6987 xmlSetProp(text, BAD_CAST "ns", ns);
6988 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
6991 } else if (xmlStrEqual(cur->name,
6992 BAD_CAST "attribute")) {
6993 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
6996 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
6997 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
6998 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7000 * Simplification 4.8. name attribute of element
7001 * and attribute elements
7003 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7008 while ((node != NULL) &&
7009 (node->type == XML_ELEMENT_NODE)) {
7010 ns = xmlGetProp(node, BAD_CAST "ns");
7014 node = node->parent;
7017 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7019 xmlSetProp(cur, BAD_CAST "ns", ns);
7023 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7024 xmlChar *name, *local, *prefix;
7027 * Simplification: 4.10. QNames
7029 name = xmlNodeGetContent(cur);
7031 local = xmlSplitQName2(name, &prefix);
7032 if (local != NULL) {
7035 ns = xmlSearchNs(cur->doc, cur, prefix);
7037 if (ctxt->error != NULL)
7038 ctxt->error(ctxt->userData,
7039 "xmlRelaxNGParse: no namespace for prefix %s\n", prefix);
7042 xmlSetProp(cur, BAD_CAST "ns", ns->href);
7043 xmlNodeSetContent(cur, local);
7054 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7055 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7056 if (ctxt->error != NULL)
7057 ctxt->error(ctxt->userData,
7058 "Found nsName/except//nsName forbidden construct\n");
7062 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7064 int oldflags = ctxt->flags;
7069 if ((cur->parent != NULL) &&
7070 (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) {
7071 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7072 xmlRelaxNGCleanupTree(ctxt, cur);
7073 ctxt->flags = oldflags;
7075 } else if ((cur->parent != NULL) &&
7076 (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) {
7077 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7078 xmlRelaxNGCleanupTree(ctxt, cur);
7079 ctxt->flags = oldflags;
7082 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7086 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7087 if (ctxt->error != NULL)
7088 ctxt->error(ctxt->userData,
7089 "Found anyName/except//anyName forbidden construct\n");
7091 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7092 if (ctxt->error != NULL)
7093 ctxt->error(ctxt->userData,
7094 "Found nsName/except//anyName forbidden construct\n");
7099 * Thisd is not an else since "include" is transformed
7102 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7104 xmlNodePtr child, ins, tmp;
7107 * implements rule 4.11
7110 ns = xmlGetProp(cur, BAD_CAST "ns");
7112 child = cur->children;
7114 while (child != NULL) {
7116 if (!xmlHasProp(child, BAD_CAST "ns")) {
7117 xmlSetProp(child, BAD_CAST "ns", ns);
7121 xmlUnlinkNode(child);
7122 ins = xmlAddNextSibling(ins, child);
7133 * Simplification 4.2 whitespaces
7135 else if ((cur->type == XML_TEXT_NODE) ||
7136 (cur->type == XML_CDATA_SECTION_NODE)) {
7137 if (IS_BLANK_NODE(cur)) {
7138 if (cur->parent->type == XML_ELEMENT_NODE) {
7139 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) &&
7140 (!xmlStrEqual(cur->parent->name, BAD_CAST "param")))
7155 if (cur->children != NULL) {
7156 if ((cur->children->type != XML_ENTITY_DECL) &&
7157 (cur->children->type != XML_ENTITY_REF_NODE) &&
7158 (cur->children->type != XML_ENTITY_NODE)) {
7159 cur = cur->children;
7164 if (cur->next != NULL) {
7177 if (cur->next != NULL) {
7181 } while (cur != NULL);
7183 if (delete != NULL) {
7184 xmlUnlinkNode(delete);
7185 xmlFreeNode(delete);
7191 * xmlRelaxNGCleanupDoc:
7192 * @ctxt: a Relax-NG parser context
7193 * @doc: an xmldocPtr document pointer
7195 * Cleanup the document from unwanted nodes for parsing, resolve
7196 * Include and externalRef lookups.
7198 * Returns the cleaned up document or NULL in case of error
7201 xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) {
7207 root = xmlDocGetRootElement(doc);
7209 if (ctxt->error != NULL)
7210 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
7215 xmlRelaxNGCleanupTree(ctxt, root);
7221 * @ctxt: a Relax-NG parser context
7223 * parse a schema definition resource and build an internal
7224 * XML Shema struture which can be used to validate instances.
7225 * *WARNING* this interface is highly subject to change
7227 * Returns the internal XML RelaxNG structure built from the resource or
7228 * NULL in case of error
7231 xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7233 xmlRelaxNGPtr ret = NULL;
7237 xmlRelaxNGInitTypes();
7243 * First step is to parse the input document into an DOM/Infoset
7245 if (ctxt->URL != NULL) {
7246 doc = xmlParseFile((const char *) ctxt->URL);
7248 if (ctxt->error != NULL)
7249 ctxt->error(ctxt->userData,
7250 "xmlRelaxNGParse: could not load %s\n", ctxt->URL);
7254 } else if (ctxt->buffer != NULL) {
7255 doc = xmlParseMemory(ctxt->buffer, ctxt->size);
7257 if (ctxt->error != NULL)
7258 ctxt->error(ctxt->userData,
7259 "xmlRelaxNGParse: could not parse schemas\n");
7263 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7264 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7265 } else if (ctxt->document != NULL) {
7266 doc = ctxt->document;
7268 if (ctxt->error != NULL)
7269 ctxt->error(ctxt->userData,
7270 "xmlRelaxNGParse: nothing to parse\n");
7274 ctxt->document = doc;
7277 * Some preprocessing of the document content
7279 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7281 xmlFreeDoc(ctxt->document);
7282 ctxt->document = NULL;
7287 * Then do the parsing for good
7289 root = xmlDocGetRootElement(doc);
7291 if (ctxt->error != NULL)
7292 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
7298 ret = xmlRelaxNGParseDocument(ctxt, root);
7305 * Check the ref/defines links
7308 * try to preprocess interleaves
7310 if (ctxt->interleaves != NULL) {
7311 xmlHashScan(ctxt->interleaves,
7312 (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt);
7316 * if there was a parsing error return NULL
7318 if (ctxt->nbErrors > 0) {
7319 xmlRelaxNGFree(ret);
7320 ctxt->document = NULL;
7326 * try to compile (parts of) the schemas
7328 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7329 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
7330 xmlRelaxNGDefinePtr def;
7332 def = xmlRelaxNGNewDefine(ctxt, NULL);
7334 def->type = XML_RELAXNG_START;
7335 def->content = ret->topgrammar->start;
7336 ret->topgrammar->start = def;
7339 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
7343 * Transfer the pointer for cleanup at the schema level.
7346 ctxt->document = NULL;
7347 ret->documents = ctxt->documents;
7348 ctxt->documents = NULL;
7350 ret->includes = ctxt->includes;
7351 ctxt->includes = NULL;
7352 ret->defNr = ctxt->defNr;
7353 ret->defTab = ctxt->defTab;
7354 ctxt->defTab = NULL;
7355 if (ctxt->idref == 1)
7362 * xmlRelaxNGSetParserErrors:
7363 * @ctxt: a Relax-NG validation context
7364 * @err: the error callback
7365 * @warn: the warning callback
7366 * @ctx: contextual data for the callbacks
7368 * Set the callback functions used to handle errors for a validation context
7371 xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7372 xmlRelaxNGValidityErrorFunc err,
7373 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
7377 ctxt->warning = warn;
7378 ctxt->userData = ctx;
7382 * xmlRelaxNGGetParserErrors:
7383 * @ctxt: a Relax-NG validation context
7384 * @err: the error callback result
7385 * @warn: the warning callback result
7386 * @ctx: contextual data for the callbacks result
7388 * Get the callback information used to handle errors for a validation context
7390 * Returns -1 in case of failure, 0 otherwise.
7393 xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7394 xmlRelaxNGValidityErrorFunc *err,
7395 xmlRelaxNGValidityWarningFunc *warn, void **ctx) {
7398 if (err != NULL) *err = ctxt->error;
7399 if (warn != NULL) *warn = ctxt->warning;
7400 if (ctx != NULL) *ctx = ctxt->userData;
7404 /************************************************************************
7406 * Dump back a compiled form *
7408 ************************************************************************/
7409 static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define);
7412 * xmlRelaxNGDumpDefines:
7413 * @output: the file output
7414 * @defines: a list of define structures
7416 * Dump a RelaxNG structure back
7419 xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) {
7420 while (defines != NULL) {
7421 xmlRelaxNGDumpDefine(output, defines);
7422 defines = defines->next;
7427 * xmlRelaxNGDumpDefine:
7428 * @output: the file output
7429 * @define: a define structure
7431 * Dump a RelaxNG structure back
7434 xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) {
7437 switch(define->type) {
7438 case XML_RELAXNG_EMPTY:
7439 fprintf(output, "<empty/>\n");
7441 case XML_RELAXNG_NOT_ALLOWED:
7442 fprintf(output, "<notAllowed/>\n");
7444 case XML_RELAXNG_TEXT:
7445 fprintf(output, "<text/>\n");
7447 case XML_RELAXNG_ELEMENT:
7448 fprintf(output, "<element>\n");
7449 if (define->name != NULL) {
7450 fprintf(output, "<name");
7451 if (define->ns != NULL)
7452 fprintf(output, " ns=\"%s\"", define->ns);
7453 fprintf(output, ">%s</name>\n", define->name);
7455 xmlRelaxNGDumpDefines(output, define->attrs);
7456 xmlRelaxNGDumpDefines(output, define->content);
7457 fprintf(output, "</element>\n");
7459 case XML_RELAXNG_LIST:
7460 fprintf(output, "<list>\n");
7461 xmlRelaxNGDumpDefines(output, define->content);
7462 fprintf(output, "</list>\n");
7464 case XML_RELAXNG_ONEORMORE:
7465 fprintf(output, "<oneOrMore>\n");
7466 xmlRelaxNGDumpDefines(output, define->content);
7467 fprintf(output, "</oneOrMore>\n");
7469 case XML_RELAXNG_ZEROORMORE:
7470 fprintf(output, "<zeroOrMore>\n");
7471 xmlRelaxNGDumpDefines(output, define->content);
7472 fprintf(output, "</zeroOrMore>\n");
7474 case XML_RELAXNG_CHOICE:
7475 fprintf(output, "<choice>\n");
7476 xmlRelaxNGDumpDefines(output, define->content);
7477 fprintf(output, "</choice>\n");
7479 case XML_RELAXNG_GROUP:
7480 fprintf(output, "<group>\n");
7481 xmlRelaxNGDumpDefines(output, define->content);
7482 fprintf(output, "</group>\n");
7484 case XML_RELAXNG_INTERLEAVE:
7485 fprintf(output, "<interleave>\n");
7486 xmlRelaxNGDumpDefines(output, define->content);
7487 fprintf(output, "</interleave>\n");
7489 case XML_RELAXNG_OPTIONAL:
7490 fprintf(output, "<optional>\n");
7491 xmlRelaxNGDumpDefines(output, define->content);
7492 fprintf(output, "</optional>\n");
7494 case XML_RELAXNG_ATTRIBUTE:
7495 fprintf(output, "<attribute>\n");
7496 xmlRelaxNGDumpDefines(output, define->content);
7497 fprintf(output, "</attribute>\n");
7499 case XML_RELAXNG_DEF:
7500 fprintf(output, "<define");
7501 if (define->name != NULL)
7502 fprintf(output, " name=\"%s\"", define->name);
7503 fprintf(output, ">\n");
7504 xmlRelaxNGDumpDefines(output, define->content);
7505 fprintf(output, "</define>\n");
7507 case XML_RELAXNG_REF:
7508 fprintf(output, "<ref");
7509 if (define->name != NULL)
7510 fprintf(output, " name=\"%s\"", define->name);
7511 fprintf(output, ">\n");
7512 xmlRelaxNGDumpDefines(output, define->content);
7513 fprintf(output, "</ref>\n");
7515 case XML_RELAXNG_PARENTREF:
7516 fprintf(output, "<parentRef");
7517 if (define->name != NULL)
7518 fprintf(output, " name=\"%s\"", define->name);
7519 fprintf(output, ">\n");
7520 xmlRelaxNGDumpDefines(output, define->content);
7521 fprintf(output, "</parentRef>\n");
7523 case XML_RELAXNG_EXTERNALREF:
7524 fprintf(output, "<externalRef>");
7525 xmlRelaxNGDumpDefines(output, define->content);
7526 fprintf(output, "</externalRef>\n");
7528 case XML_RELAXNG_DATATYPE:
7529 case XML_RELAXNG_VALUE:
7532 case XML_RELAXNG_START:
7533 case XML_RELAXNG_EXCEPT:
7534 case XML_RELAXNG_PARAM:
7537 case XML_RELAXNG_NOOP:
7538 xmlRelaxNGDumpDefines(output, define->content);
7544 * xmlRelaxNGDumpGrammar:
7545 * @output: the file output
7546 * @grammar: a grammar structure
7547 * @top: is this a top grammar
7549 * Dump a RelaxNG structure back
7552 xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7554 if (grammar == NULL)
7557 fprintf(output, "<grammar");
7560 " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7561 switch(grammar->combine) {
7562 case XML_RELAXNG_COMBINE_UNDEFINED:
7564 case XML_RELAXNG_COMBINE_CHOICE:
7565 fprintf(output, " combine=\"choice\"");
7567 case XML_RELAXNG_COMBINE_INTERLEAVE:
7568 fprintf(output, " combine=\"interleave\"");
7571 fprintf(output, " <!-- invalid combine value -->");
7573 fprintf(output, ">\n");
7574 if (grammar->start == NULL) {
7575 fprintf(output, " <!-- grammar had no start -->");
7577 fprintf(output, "<start>\n");
7578 xmlRelaxNGDumpDefine(output, grammar->start);
7579 fprintf(output, "</start>\n");
7581 /* TODO ? Dump the defines ? */
7582 fprintf(output, "</grammar>\n");
7587 * @output: the file output
7588 * @schema: a schema structure
7590 * Dump a RelaxNG structure back
7593 xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7595 if (schema == NULL) {
7596 fprintf(output, "RelaxNG empty or failed to compile\n");
7599 fprintf(output, "RelaxNG: ");
7600 if (schema->doc == NULL) {
7601 fprintf(output, "no document\n");
7602 } else if (schema->doc->URL != NULL) {
7603 fprintf(output, "%s\n", schema->doc->URL);
7605 fprintf(output, "\n");
7607 if (schema->topgrammar == NULL) {
7608 fprintf(output, "RelaxNG has no top grammar\n");
7611 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7615 * xmlRelaxNGDumpTree:
7616 * @output: the file output
7617 * @schema: a schema structure
7619 * Dump the transformed RelaxNG tree.
7622 xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7624 if (schema == NULL) {
7625 fprintf(output, "RelaxNG empty or failed to compile\n");
7628 if (schema->doc == NULL) {
7629 fprintf(output, "no document\n");
7631 xmlDocDump(output, schema->doc);
7635 /************************************************************************
7637 * Validation of compiled content *
7639 ************************************************************************/
7640 static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7641 xmlRelaxNGDefinePtr define);
7644 * xmlRelaxNGValidateCompiledCallback:
7645 * @exec: the regular expression instance
7646 * @token: the token which matched
7647 * @transdata: callback data, the define for the subelement if available
7648 @ @inputdata: callback data, the Relax NG validation context
7650 * Handle the callback and if needed validate the element children.
7653 xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7654 const xmlChar *token,
7657 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7658 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7661 #ifdef DEBUG_COMPILE
7662 xmlGenericError(xmlGenericErrorContext,
7663 "Compiled callback for: '%s'\n", token);
7666 fprintf(stderr, "callback on %s missing context\n", token);
7667 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7668 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7671 if (define == NULL) {
7672 if (token[0] == '#')
7674 fprintf(stderr, "callback on %s missing define\n", token);
7675 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7676 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7679 if ((ctxt == NULL) || (define == NULL)) {
7680 fprintf(stderr, "callback on %s missing info\n", token);
7681 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7682 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7684 } else if (define->type != XML_RELAXNG_ELEMENT) {
7685 fprintf(stderr, "callback on %s define is not element\n", token);
7686 if (ctxt->errNo == XML_RELAXNG_OK)
7687 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7690 ret = xmlRelaxNGValidateDefinition(ctxt, define);
7694 * xmlRelaxNGValidateCompiledContent:
7695 * @ctxt: the RelaxNG validation context
7696 * @regexp: the regular expression as compiled
7697 * @content: list of children to test against the regexp
7699 * Validate the content model of an element or start using the regexp
7701 * Returns 0 in case of success, -1 in case of error.
7704 xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
7705 xmlRegexpPtr regexp, xmlNodePtr content) {
7706 xmlRegExecCtxtPtr exec;
7710 if ((ctxt == NULL) || (regexp == NULL))
7712 exec = xmlRegNewExecCtxt(regexp,
7713 xmlRelaxNGValidateCompiledCallback, ctxt);
7715 while (cur != NULL) {
7716 ctxt->state->seq = cur;
7717 switch (cur->type) {
7719 case XML_CDATA_SECTION_NODE:
7720 if (xmlIsBlankNode(cur))
7722 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7724 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, cur->parent->name);
7727 case XML_ELEMENT_NODE:
7728 if (cur->ns != NULL) {
7729 ret = xmlRegExecPushString2(exec, cur->name,
7730 cur->ns->href, ctxt);
7732 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7735 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7743 * Switch to next element
7747 ret = xmlRegExecPushString(exec, NULL, NULL);
7750 ctxt->state->seq = NULL;
7751 } else if (ret == 0) {
7753 * TODO: get some of the names needed to exit the current state of exec
7755 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7757 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7758 xmlRelaxNGDumpValidError(ctxt);
7762 xmlRegFreeExecCtxt(exec);
7766 /************************************************************************
7768 * Progressive validation of when possible *
7770 ************************************************************************/
7771 static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7772 xmlRelaxNGDefinePtr defines);
7773 static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt);
7776 * xmlRelaxNGElemPush:
7777 * @ctxt: the validation context
7778 * @exec: the regexp runtime for the new content model
7780 * Push a new regexp for the current node content model on the stack
7782 * Returns 0 in case of success and -1 in case of error.
7785 xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) {
7786 if (ctxt->elemTab == NULL) {
7788 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7789 sizeof(xmlRegExecCtxtPtr));
7790 if (ctxt->elemTab == NULL) {
7791 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7795 if (ctxt->elemNr >= ctxt->elemMax) {
7797 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
7798 ctxt->elemMax * sizeof(xmlRegExecCtxtPtr));
7799 if (ctxt->elemTab == NULL) {
7800 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7804 ctxt->elemTab[ctxt->elemNr++] = exec;
7810 * xmlRelaxNGElemPop:
7811 * @ctxt: the validation context
7813 * Pop the regexp of the current node content model from the stack
7815 * Returns the exec or NULL if empty
7817 static xmlRegExecCtxtPtr
7818 xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) {
7819 xmlRegExecCtxtPtr ret;
7821 if (ctxt->elemNr <= 0) return(NULL);
7823 ret = ctxt->elemTab[ctxt->elemNr];
7824 ctxt->elemTab[ctxt->elemNr] = NULL;
7825 if (ctxt->elemNr > 0)
7826 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7833 * xmlRelaxNGValidateProgressiveCallback:
7834 * @exec: the regular expression instance
7835 * @token: the token which matched
7836 * @transdata: callback data, the define for the subelement if available
7837 @ @inputdata: callback data, the Relax NG validation context
7839 * Handle the callback and if needed validate the element children.
7840 * some of the in/out informations are passed via the context in @inputdata.
7843 xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7844 const xmlChar *token,
7847 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7848 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7849 xmlRelaxNGValidStatePtr state, oldstate;
7850 xmlNodePtr node = ctxt->pnode;
7851 int ret = 0, oldflags;
7853 #ifdef DEBUG_PROGRESSIVE
7854 xmlGenericError(xmlGenericErrorContext,
7855 "Progressive callback for: '%s'\n", token);
7858 fprintf(stderr, "callback on %s missing context\n", token);
7862 if (define == NULL) {
7863 if (token[0] == '#')
7865 fprintf(stderr, "callback on %s missing define\n", token);
7866 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7867 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7871 if ((ctxt == NULL) || (define == NULL)) {
7872 fprintf(stderr, "callback on %s missing info\n", token);
7873 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7874 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7877 } else if (define->type != XML_RELAXNG_ELEMENT) {
7878 fprintf(stderr, "callback on %s define is not element\n", token);
7879 if (ctxt->errNo == XML_RELAXNG_OK)
7880 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7884 if (node->type != XML_ELEMENT_NODE) {
7885 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7886 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7887 xmlRelaxNGDumpValidError(ctxt);
7891 if (define->contModel == NULL) {
7893 * this node cannot be validated in a streamable fashion
7895 #ifdef DEBUG_PROGRESSIVE
7896 xmlGenericError(xmlGenericErrorContext,
7897 "Element '%s' validation is not streamable\n", token);
7900 ctxt->pdef = define;
7903 exec = xmlRegNewExecCtxt(define->contModel,
7904 xmlRelaxNGValidateProgressiveCallback,
7910 xmlRelaxNGElemPush(ctxt, exec);
7913 * Validate the attributes part of the content.
7915 state = xmlRelaxNGNewValidState(ctxt, node);
7916 if (state == NULL) {
7920 oldstate = ctxt->state;
7921 ctxt->state = state;
7922 if (define->attrs != NULL) {
7923 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
7926 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
7929 if (ctxt->state != NULL) {
7930 ctxt->state->seq = NULL;
7931 ret = xmlRelaxNGValidateElementEnd(ctxt);
7935 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
7936 } else if (ctxt->states != NULL) {
7939 oldflags = ctxt->flags;
7940 ctxt->flags |= FLAGS_IGNORABLE;
7942 for (i = 0; i < ctxt->states->nbState; i++) {
7943 state = ctxt->states->tabState[i];
7944 ctxt->state = state;
7945 ctxt->state->seq = NULL;
7947 if (xmlRelaxNGValidateElementEnd(ctxt) == 0)
7949 xmlRelaxNGFreeValidState(ctxt, state);
7951 xmlRelaxNGFreeStates(ctxt, ctxt->states);
7952 ctxt->states = NULL;
7953 if ((ret == 0) && (tmp == -1))
7955 ctxt->flags = oldflags;
7957 if (ctxt->pstate == -1) {
7958 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
7959 xmlRelaxNGDumpValidError(ctxt);
7962 ctxt->state = oldstate;
7966 * xmlRelaxNGValidatePushElement:
7967 * @ctxt: the validation context
7968 * @doc: a document instance
7969 * @elem: an element instance
7971 * Push a new element start on the RelaxNG validation stack.
7973 * returns 1 if no validation problem was found or 0 if validating the
7974 * element requires a full node, and -1 in case of error.
7977 xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
7978 xmlDocPtr doc ATTRIBUTE_UNUSED,
7983 if ((ctxt == NULL) || (elem == NULL))
7986 #ifdef DEBUG_PROGRESSIVE
7987 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
7989 if (ctxt->elem == 0) {
7990 xmlRelaxNGPtr schema;
7991 xmlRelaxNGGrammarPtr grammar;
7992 xmlRegExecCtxtPtr exec;
7993 xmlRelaxNGDefinePtr define;
7995 schema = ctxt->schema;
7996 if (schema == NULL) {
7997 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8000 grammar = schema->topgrammar;
8001 if ((grammar == NULL) || (grammar->start == NULL)) {
8002 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8005 define = grammar->start;
8006 if (define->contModel == NULL) {
8007 ctxt->pdef = define;
8010 exec = xmlRegNewExecCtxt(define->contModel,
8011 xmlRelaxNGValidateProgressiveCallback,
8016 xmlRelaxNGElemPush(ctxt, exec);
8020 if (elem->ns != NULL) {
8022 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8025 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8028 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8030 if (ctxt->pstate == 0)
8032 else if (ctxt->pstate < 0)
8037 #ifdef DEBUG_PROGRESSIVE
8039 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8046 * xmlRelaxNGValidatePushCData:
8047 * @ctxt: the RelaxNG validation context
8048 * @data: some character data read
8049 * @len: the lenght of the data
8051 * check the CData parsed for validation in the current stack
8053 * returns 1 if no validation problem was found or -1 otherwise
8056 xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
8057 const xmlChar * data,
8058 int len ATTRIBUTE_UNUSED)
8062 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8065 #ifdef DEBUG_PROGRESSIVE
8066 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8069 while (*data != 0) {
8070 if (!IS_BLANK(*data))
8077 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8079 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
8080 #ifdef DEBUG_PROGRESSIVE
8081 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
8090 * xmlRelaxNGValidatePopElement:
8091 * @ctxt: the RelaxNG validation context
8092 * @doc: a document instance
8093 * @elem: an element instance
8095 * Pop the element end from the RelaxNG validation stack.
8097 * returns 1 if no validation problem was found or 0 otherwise
8100 xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8101 xmlDocPtr doc ATTRIBUTE_UNUSED,
8104 xmlRegExecCtxtPtr exec;
8106 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) return(-1);
8107 #ifdef DEBUG_PROGRESSIVE
8108 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8111 * verify that we reached a terminal state of the content model.
8113 exec = xmlRelaxNGElemPop(ctxt);
8114 ret = xmlRegExecPushString(exec, NULL, NULL);
8117 * TODO: get some of the names needed to exit the current state of exec
8119 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8121 } else if (ret < 0) {
8126 xmlRegFreeExecCtxt(exec);
8127 #ifdef DEBUG_PROGRESSIVE
8129 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8136 * xmlRelaxNGValidateFullElement:
8137 * @ctxt: the validation context
8138 * @doc: a document instance
8139 * @elem: an element instance
8141 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8142 * 0 and the content of the node has been expanded.
8144 * returns 1 if no validation problem was found or -1 in case of error.
8147 xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8148 xmlDocPtr doc ATTRIBUTE_UNUSED,
8151 xmlRelaxNGValidStatePtr state;
8153 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) return(-1);
8154 #ifdef DEBUG_PROGRESSIVE
8155 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8157 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8158 if (state == NULL) {
8162 ctxt->state = state;
8163 ctxt->errNo = XML_RELAXNG_OK;
8164 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
8165 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) ret = -1;
8167 xmlRelaxNGFreeValidState(ctxt, state);
8169 #ifdef DEBUG_PROGRESSIVE
8171 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8177 /************************************************************************
8179 * Generic interpreted validation implementation *
8181 ************************************************************************/
8182 static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8183 xmlRelaxNGDefinePtr define);
8186 * xmlRelaxNGSkipIgnored:
8187 * @ctxt: a schema validation context
8188 * @node: the top node.
8190 * Skip ignorable nodes in that context
8192 * Returns the new sibling or NULL in case of error.
8195 xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
8198 * TODO complete and handle entities
8200 while ((node != NULL) &&
8201 ((node->type == XML_COMMENT_NODE) ||
8202 (node->type == XML_PI_NODE) ||
8203 (((node->type == XML_TEXT_NODE) ||
8204 (node->type == XML_CDATA_SECTION_NODE)) &&
8205 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8206 (IS_BLANK_NODE(node)))))) {
8213 * xmlRelaxNGNormalize:
8214 * @ctxt: a schema validation context
8215 * @str: the string to normalize
8217 * Implements the normalizeWhiteSpace( s ) function from
8218 * section 6.2.9 of the spec
8220 * Returns the new string or NULL in case of error.
8223 xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) {
8231 while (*tmp != 0) tmp++;
8234 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
8237 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8239 xmlGenericError(xmlGenericErrorContext,
8240 "xmlRelaxNGNormalize: out of memory\n");
8245 while (IS_BLANK(*str)) str++;
8247 if (IS_BLANK(*str)) {
8248 while (IS_BLANK(*str)) str++;
8260 * xmlRelaxNGValidateDatatype:
8261 * @ctxt: a Relax-NG validation context
8262 * @value: the string value
8263 * @type: the datatype definition
8266 * Validate the given value against the dataype
8268 * Returns 0 if the validation succeeded or an error code.
8271 xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value,
8272 xmlRelaxNGDefinePtr define, xmlNodePtr node) {
8274 xmlRelaxNGTypeLibraryPtr lib;
8275 void *result = NULL;
8276 xmlRelaxNGDefinePtr cur;
8278 if ((define == NULL) || (define->data == NULL)) {
8281 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8282 if (lib->check != NULL) {
8283 if ((define->attrs != NULL) &&
8284 (define->attrs->type == XML_RELAXNG_PARAM)) {
8285 ret = lib->check(lib->data, define->name, value, &result, node);
8287 ret = lib->check(lib->data, define->name, value, NULL, node);
8292 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8293 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8294 lib->freef(lib->data, result);
8296 } else if (ret == 1) {
8298 } else if (ret == 2) {
8299 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
8301 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8304 cur = define->attrs;
8305 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
8306 if (lib->facet != NULL) {
8307 tmp = lib->facet(lib->data, define->name, cur->name,
8308 cur->value, value, result);
8314 if ((ret == 0) && (define->content != NULL)) {
8315 const xmlChar *oldvalue, *oldendvalue;
8317 oldvalue = ctxt->state->value;
8318 oldendvalue = ctxt->state->endvalue;
8319 ctxt->state->value = (xmlChar *) value;
8320 ctxt->state->endvalue = NULL;
8321 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8322 ctxt->state->value = (xmlChar *) oldvalue;
8323 ctxt->state->endvalue = (xmlChar *) oldendvalue;
8325 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8326 lib->freef(lib->data, result);
8331 * xmlRelaxNGNextValue:
8332 * @ctxt: a Relax-NG validation context
8334 * Skip to the next value when validating within a list
8336 * Returns 0 if the operation succeeded or an error code.
8339 xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) {
8342 cur = ctxt->state->value;
8343 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
8344 ctxt->state->value = NULL;
8345 ctxt->state->endvalue = NULL;
8348 while (*cur != 0) cur++;
8349 while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++;
8350 if (cur == ctxt->state->endvalue)
8351 ctxt->state->value = NULL;
8353 ctxt->state->value = cur;
8358 * xmlRelaxNGValidateValueList:
8359 * @ctxt: a Relax-NG validation context
8360 * @defines: the list of definitions to verify
8362 * Validate the given set of definitions for the current value
8364 * Returns 0 if the validation succeeded or an error code.
8367 xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8368 xmlRelaxNGDefinePtr defines) {
8371 while (defines != NULL) {
8372 ret = xmlRelaxNGValidateValue(ctxt, defines);
8375 defines = defines->next;
8381 * xmlRelaxNGValidateValue:
8382 * @ctxt: a Relax-NG validation context
8383 * @define: the definition to verify
8385 * Validate the given definition for the current value
8387 * Returns 0 if the validation succeeded or an error code.
8390 xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8391 xmlRelaxNGDefinePtr define) {
8392 int ret = 0, oldflags;
8395 value = ctxt->state->value;
8396 switch (define->type) {
8397 case XML_RELAXNG_EMPTY: {
8398 if ((value != NULL) && (value[0] != 0)) {
8401 while (IS_BLANK(value[idx]))
8403 if (value[idx] != 0)
8408 case XML_RELAXNG_TEXT:
8410 case XML_RELAXNG_VALUE: {
8411 if (!xmlStrEqual(value, define->value)) {
8412 if (define->name != NULL) {
8413 xmlRelaxNGTypeLibraryPtr lib;
8415 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8416 if ((lib != NULL) && (lib->comp != NULL)) {
8417 ret = lib->comp(lib->data, define->name,
8418 define->value, define->node,
8419 (void *) define->attrs,
8420 value, ctxt->state->node);
8424 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name);
8426 } else if (ret == 1) {
8432 xmlChar *nval, *nvalue;
8435 * TODO: trivial optimizations are possible by
8436 * computing at compile-time
8438 nval = xmlRelaxNGNormalize(ctxt, define->value);
8439 nvalue = xmlRelaxNGNormalize(ctxt, value);
8441 if ((nval == NULL) || (nvalue == NULL) ||
8442 (!xmlStrEqual(nval, nvalue)))
8451 xmlRelaxNGNextValue(ctxt);
8454 case XML_RELAXNG_DATATYPE: {
8455 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8458 xmlRelaxNGNextValue(ctxt);
8462 case XML_RELAXNG_CHOICE: {
8463 xmlRelaxNGDefinePtr list = define->content;
8466 oldflags = ctxt->flags;
8467 ctxt->flags |= FLAGS_IGNORABLE;
8469 oldvalue = ctxt->state->value;
8470 while (list != NULL) {
8471 ret = xmlRelaxNGValidateValue(ctxt, list);
8475 ctxt->state->value = oldvalue;
8478 ctxt->flags = oldflags;
8480 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8481 xmlRelaxNGDumpValidError(ctxt);
8483 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
8486 xmlRelaxNGNextValue(ctxt);
8489 case XML_RELAXNG_LIST: {
8490 xmlRelaxNGDefinePtr list = define->content;
8491 xmlChar *oldvalue, *oldend, *val, *cur;
8496 oldvalue = ctxt->state->value;
8497 oldend = ctxt->state->endvalue;
8499 val = xmlStrdup(oldvalue);
8501 val = xmlStrdup(BAD_CAST "");
8504 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8509 if (IS_BLANK(*cur)) {
8515 while (IS_BLANK(*cur))
8521 xmlGenericError(xmlGenericErrorContext,
8522 "list value: '%s' found %d items\n", oldvalue, nb_values);
8525 ctxt->state->endvalue = cur;
8527 while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++;
8529 ctxt->state->value = cur;
8531 while (list != NULL) {
8532 if (ctxt->state->value == ctxt->state->endvalue)
8533 ctxt->state->value = NULL;
8534 ret = xmlRelaxNGValidateValue(ctxt, list);
8537 xmlGenericError(xmlGenericErrorContext,
8538 "Failed to validate value: '%s' with %d rule\n",
8539 ctxt->state->value, nb_values);
8549 if ((ret == 0) && (ctxt->state->value != NULL) &&
8550 (ctxt->state->value != ctxt->state->endvalue)) {
8551 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value);
8555 ctxt->state->value = oldvalue;
8556 ctxt->state->endvalue = oldend;
8559 case XML_RELAXNG_ONEORMORE:
8560 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8564 /* no break on purpose */
8565 case XML_RELAXNG_ZEROORMORE: {
8566 xmlChar *cur, *temp;
8568 oldflags = ctxt->flags;
8569 ctxt->flags |= FLAGS_IGNORABLE;
8570 cur = ctxt->state->value;
8572 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8575 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8577 ctxt->state->value = temp;
8581 cur = ctxt->state->value;
8583 ctxt->flags = oldflags;
8585 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8586 xmlRelaxNGDumpValidError(ctxt);
8588 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
8592 case XML_RELAXNG_EXCEPT: {
8593 xmlRelaxNGDefinePtr list;
8595 list = define->content;
8596 while (list != NULL) {
8597 ret = xmlRelaxNGValidateValue(ctxt, list);
8607 case XML_RELAXNG_DEF:
8608 case XML_RELAXNG_GROUP: {
8609 xmlRelaxNGDefinePtr list;
8611 list = define->content;
8612 while (list != NULL) {
8613 ret = xmlRelaxNGValidateValue(ctxt, list);
8623 case XML_RELAXNG_REF:
8624 case XML_RELAXNG_PARENTREF:
8625 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8635 * xmlRelaxNGValidateValueContent:
8636 * @ctxt: a Relax-NG validation context
8637 * @defines: the list of definitions to verify
8639 * Validate the given definitions for the current value
8641 * Returns 0 if the validation succeeded or an error code.
8644 xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8645 xmlRelaxNGDefinePtr defines) {
8648 while (defines != NULL) {
8649 ret = xmlRelaxNGValidateValue(ctxt, defines);
8652 defines = defines->next;
8658 * xmlRelaxNGAttributeMatch:
8659 * @ctxt: a Relax-NG validation context
8660 * @define: the definition to check
8661 * @prop: the attribute
8663 * Check if the attribute matches the definition nameClass
8665 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8668 xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8669 xmlRelaxNGDefinePtr define,
8673 if (define->name != NULL) {
8674 if (!xmlStrEqual(define->name, prop->name))
8677 if (define->ns != NULL) {
8678 if (define->ns[0] == 0) {
8679 if (prop->ns != NULL)
8682 if ((prop->ns == NULL) ||
8683 (!xmlStrEqual(define->ns, prop->ns->href)))
8687 if (define->nameClass == NULL)
8689 define = define->nameClass;
8690 if (define->type == XML_RELAXNG_EXCEPT) {
8691 xmlRelaxNGDefinePtr list;
8693 list = define->content;
8694 while (list != NULL) {
8695 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8709 * xmlRelaxNGValidateAttribute:
8710 * @ctxt: a Relax-NG validation context
8711 * @define: the definition to verify
8713 * Validate the given attribute definition for that node
8715 * Returns 0 if the validation succeeded or an error code.
8718 xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8719 xmlRelaxNGDefinePtr define) {
8721 xmlChar *value, *oldvalue;
8722 xmlAttrPtr prop = NULL, tmp;
8725 if (ctxt->state->nbAttrLeft <= 0)
8727 if (define->name != NULL) {
8728 for (i = 0;i < ctxt->state->nbAttrs;i++) {
8729 tmp = ctxt->state->attrs[i];
8730 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8731 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8732 (tmp->ns == NULL)) ||
8733 ((tmp->ns != NULL) &&
8734 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8741 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8742 oldvalue = ctxt->state->value;
8743 oldseq = ctxt->state->seq;
8744 ctxt->state->seq = (xmlNodePtr) prop;
8745 ctxt->state->value = value;
8746 ctxt->state->endvalue = NULL;
8747 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8748 if (ctxt->state->value != NULL)
8749 value = ctxt->state->value;
8752 ctxt->state->value = oldvalue;
8753 ctxt->state->seq = oldseq;
8756 * flag the attribute as processed
8758 ctxt->state->attrs[i] = NULL;
8759 ctxt->state->nbAttrLeft--;
8765 xmlGenericError(xmlGenericErrorContext,
8766 "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret);
8769 for (i = 0;i < ctxt->state->nbAttrs;i++) {
8770 tmp = ctxt->state->attrs[i];
8771 if ((tmp != NULL) &&
8772 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8778 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8779 oldvalue = ctxt->state->value;
8780 oldseq = ctxt->state->seq;
8781 ctxt->state->seq = (xmlNodePtr) prop;
8782 ctxt->state->value = value;
8783 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8784 if (ctxt->state->value != NULL)
8785 value = ctxt->state->value;
8788 ctxt->state->value = oldvalue;
8789 ctxt->state->seq = oldseq;
8792 * flag the attribute as processed
8794 ctxt->state->attrs[i] = NULL;
8795 ctxt->state->nbAttrLeft--;
8801 if (define->ns != NULL) {
8802 xmlGenericError(xmlGenericErrorContext,
8803 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8806 xmlGenericError(xmlGenericErrorContext,
8807 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8817 * xmlRelaxNGValidateAttributeList:
8818 * @ctxt: a Relax-NG validation context
8819 * @define: the list of definition to verify
8821 * Validate the given node against the list of attribute definitions
8823 * Returns 0 if the validation succeeded or an error code.
8826 xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8827 xmlRelaxNGDefinePtr defines) {
8830 xmlRelaxNGDefinePtr cur;
8833 while (cur != NULL) {
8834 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
8835 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8844 while (cur != NULL) {
8845 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
8846 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8847 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8851 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8854 if (res == -1) /* continues on -2 */
8864 * xmlRelaxNGNodeMatchesList:
8866 * @list: a NULL terminated array of definitions
8868 * Check if a node can be matched by one of the definitions
8870 * Returns 1 if matches 0 otherwise
8873 xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) {
8874 xmlRelaxNGDefinePtr cur;
8877 if ((node == NULL) || (list == NULL))
8881 while (cur != NULL) {
8882 if ((node->type == XML_ELEMENT_NODE) &&
8883 (cur->type == XML_RELAXNG_ELEMENT)) {
8884 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
8887 } else if (((node->type == XML_TEXT_NODE) ||
8888 (node->type == XML_CDATA_SECTION_NODE)) &&
8889 (cur->type == XML_RELAXNG_TEXT)) {
8898 * xmlRelaxNGValidateInterleave:
8899 * @ctxt: a Relax-NG validation context
8900 * @define: the definition to verify
8902 * Validate an interleave definition for a node.
8904 * Returns 0 if the validation succeeded or an error code.
8907 xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
8908 xmlRelaxNGDefinePtr define) {
8909 int ret = 0, i, nbgroups;
8910 int errNr = ctxt->errNr;
8913 xmlRelaxNGValidStatePtr oldstate;
8914 xmlRelaxNGPartitionPtr partitions;
8915 xmlRelaxNGInterleaveGroupPtr group = NULL;
8916 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
8917 xmlNodePtr *list = NULL, *lasts = NULL;
8919 if (define->data != NULL) {
8920 partitions = (xmlRelaxNGPartitionPtr) define->data;
8921 nbgroups = partitions->nbgroups;
8923 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
8927 * Optimizations for MIXED
8929 oldflags = ctxt->flags;
8930 if (define->dflags & IS_MIXED) {
8931 ctxt->flags |= FLAGS_MIXED_CONTENT;
8932 if (nbgroups == 2) {
8934 * this is a pure <mixed> case
8936 if (ctxt->state != NULL)
8937 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8939 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
8940 ret = xmlRelaxNGValidateDefinition(ctxt,
8941 partitions->groups[1]->rule);
8943 ret = xmlRelaxNGValidateDefinition(ctxt,
8944 partitions->groups[0]->rule);
8946 if (ctxt->state != NULL)
8947 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8950 ctxt->flags = oldflags;
8956 * Build arrays to store the first and last node of the chain
8957 * pertaining to each group
8959 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
8961 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8964 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
8965 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
8966 if (lasts == NULL) {
8967 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8970 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
8973 * Walk the sequence of children finding the right group and
8974 * sorting them in sequences.
8976 cur = ctxt->state->seq;
8977 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
8979 while (cur != NULL) {
8980 ctxt->state->seq = cur;
8981 if ((partitions->triage != NULL) &&
8982 (partitions->flags & IS_DETERMINIST)) {
8985 if ((cur->type == XML_TEXT_NODE) ||
8986 (cur->type == XML_CDATA_SECTION_NODE)) {
8987 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
8989 } else if (cur->type == XML_ELEMENT_NODE) {
8990 if (cur->ns != NULL) {
8991 tmp = xmlHashLookup2(partitions->triage, cur->name,
8994 tmp = xmlHashLookup2(partitions->triage,
8995 BAD_CAST "#any", cur->ns->href);
8997 tmp = xmlHashLookup2(partitions->triage, cur->name, NULL);
8999 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9006 i = ((long) tmp) - 1;
9007 if (partitions->flags & IS_NEEDCHECK) {
9008 group = partitions->groups[i];
9009 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9014 for (i = 0;i < nbgroups;i++) {
9015 group = partitions->groups[i];
9018 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9023 * We break as soon as an element not matched is found
9025 if (i >= nbgroups) {
9028 if (lasts[i] != NULL) {
9029 lasts[i]->next = cur;
9035 if (cur->next != NULL)
9036 lastchg = cur->next;
9039 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
9042 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9047 oldstate = ctxt->state;
9048 for (i = 0;i < nbgroups;i++) {
9049 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9050 group = partitions->groups[i];
9051 if (lasts[i] != NULL) {
9052 last = lasts[i]->next;
9053 lasts[i]->next = NULL;
9055 ctxt->state->seq = list[i];
9056 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9059 if (ctxt->state != NULL) {
9060 cur = ctxt->state->seq;
9061 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9062 xmlRelaxNGFreeValidState(ctxt,oldstate);
9063 oldstate = ctxt->state;
9066 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9068 ctxt->state = oldstate;
9071 } else if (ctxt->states != NULL) {
9075 for (j = 0;j < ctxt->states->nbState;j++) {
9076 cur = ctxt->states->tabState[j]->seq;
9077 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9083 if (ctxt->states->nbState > 0) {
9084 xmlRelaxNGFreeValidState(ctxt,oldstate);
9085 oldstate = ctxt->states->tabState[ctxt->states->nbState - 1];
9087 for (j = 0;j < ctxt->states->nbState - 1;j++) {
9088 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]);
9090 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9091 ctxt->states = NULL;
9093 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9095 ctxt->state = oldstate;
9102 if (lasts[i] != NULL) {
9103 lasts[i]->next = last;
9106 if (ctxt->state != NULL)
9107 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
9108 ctxt->state = oldstate;
9109 ctxt->state->seq = lastelem;
9111 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9117 ctxt->flags = oldflags;
9119 * builds the next links chain from the prev one
9122 while (cur != NULL) {
9123 if ((cur == start) || (cur->prev == NULL))
9125 cur->prev->next = cur;
9129 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
9138 * xmlRelaxNGValidateDefinitionList:
9139 * @ctxt: a Relax-NG validation context
9140 * @define: the list of definition to verify
9142 * Validate the given node content against the (list) of definitions
9144 * Returns 0 if the validation succeeded or an error code.
9147 xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9148 xmlRelaxNGDefinePtr defines) {
9152 if (defines == NULL) {
9153 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list");
9156 while (defines != NULL) {
9157 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9158 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9162 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9165 if (res == -1) /* continues on -2 */
9167 defines = defines->next;
9174 * xmlRelaxNGElementMatch:
9175 * @ctxt: a Relax-NG validation context
9176 * @define: the definition to check
9177 * @elem: the element
9179 * Check if the element matches the definition nameClass
9181 * Returns 1 if the element matches, 0 if no, or -1 in case of error
9184 xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9185 xmlRelaxNGDefinePtr define,
9187 int ret = 0, oldflags = 0;
9189 if (define->name != NULL) {
9190 if (!xmlStrEqual(elem->name, define->name)) {
9191 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9195 if ((define->ns != NULL) && (define->ns[0] != 0)) {
9196 if (elem->ns == NULL) {
9197 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS,
9200 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9201 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9202 elem->name, define->ns);
9205 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
9206 (define->name == NULL)) {
9207 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
9210 } else if ((elem->ns != NULL) && (define->name != NULL)) {
9211 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
9216 if (define->nameClass == NULL)
9219 define = define->nameClass;
9220 if (define->type == XML_RELAXNG_EXCEPT) {
9221 xmlRelaxNGDefinePtr list;
9223 oldflags = ctxt->flags;
9224 ctxt->flags |= FLAGS_IGNORABLE;
9227 list = define->content;
9228 while (list != NULL) {
9229 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9232 ctxt->flags = oldflags;
9237 ctxt->flags = oldflags;
9244 ctxt->flags = oldflags;
9246 } else if (define->type == XML_RELAXNG_CHOICE) {
9247 xmlRelaxNGDefinePtr list;
9250 oldflags = ctxt->flags;
9251 ctxt->flags |= FLAGS_IGNORABLE;
9254 list = define->nameClass;
9255 while (list != NULL) {
9256 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9259 ctxt->flags = oldflags;
9264 ctxt->flags = oldflags;
9271 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9272 xmlRelaxNGDumpValidError(ctxt);
9274 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
9279 ctxt->flags = oldflags;
9289 * xmlRelaxNGValidateElementEnd:
9290 * @ctxt: a Relax-NG validation context
9292 * Validate the end of the element, implements check that
9293 * there is nothing left not consumed in the element content
9294 * or in the attribute list.
9296 * Returns 0 if the validation succeeded or an error code.
9299 xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) {
9301 xmlRelaxNGValidStatePtr state;
9303 state = ctxt->state;
9304 if (state->seq != NULL) {
9305 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9306 if (state->seq != NULL) {
9307 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9308 state->node->name, state->seq->name);
9312 for (i = 0;i < state->nbAttrs;i++) {
9313 if (state->attrs[i] != NULL) {
9314 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9315 state->attrs[i]->name, state->node->name);
9323 * xmlRelaxNGValidateState:
9324 * @ctxt: a Relax-NG validation context
9325 * @define: the definition to verify
9327 * Validate the current state against the definition
9329 * Returns 0 if the validation succeeded or an error code.
9332 xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9333 xmlRelaxNGDefinePtr define)
9336 int ret = 0, i, tmp, oldflags, errNr;
9337 xmlRelaxNGValidStatePtr oldstate = NULL, state;
9339 if (define == NULL) {
9340 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9344 if (ctxt->state != NULL) {
9345 node = ctxt->state->seq;
9350 for (i = 0; i < ctxt->depth; i++)
9351 xmlGenericError(xmlGenericErrorContext, " ");
9352 xmlGenericError(xmlGenericErrorContext,
9353 "Start validating %s ", xmlRelaxNGDefName(define));
9354 if (define->name != NULL)
9355 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
9356 if ((node != NULL) && (node->name != NULL))
9357 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
9359 xmlGenericError(xmlGenericErrorContext, "\n");
9362 switch (define->type) {
9363 case XML_RELAXNG_EMPTY:
9364 node = xmlRelaxNGSkipIgnored(ctxt, node);
9367 case XML_RELAXNG_NOT_ALLOWED:
9370 case XML_RELAXNG_TEXT:
9371 while ((node != NULL) &&
9372 ((node->type == XML_TEXT_NODE) ||
9373 (node->type == XML_COMMENT_NODE) ||
9374 (node->type == XML_PI_NODE) ||
9375 (node->type == XML_CDATA_SECTION_NODE)))
9377 ctxt->state->seq = node;
9379 case XML_RELAXNG_ELEMENT:
9380 errNr = ctxt->errNr;
9381 node = xmlRelaxNGSkipIgnored(ctxt, node);
9383 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9385 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9386 xmlRelaxNGDumpValidError(ctxt);
9389 if (node->type != XML_ELEMENT_NODE) {
9390 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9392 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9393 xmlRelaxNGDumpValidError(ctxt);
9397 * This node was already validated successfully against
9400 if (node->_private == define) {
9401 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9402 if (ctxt->errNr > errNr)
9403 xmlRelaxNGPopErrors(ctxt, errNr);
9404 if (ctxt->errNr != 0) {
9405 while ((ctxt->err != NULL) &&
9406 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9407 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9410 XML_RELAXNG_ERR_ELEMEXTRANS)
9411 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9412 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9413 || (ctxt->err->err ==
9414 XML_RELAXNG_ERR_NOTELEM)))
9415 xmlRelaxNGValidErrorPop(ctxt);
9420 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9423 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9424 xmlRelaxNGDumpValidError(ctxt);
9428 if (ctxt->errNr != 0) {
9429 if (ctxt->errNr > errNr)
9430 xmlRelaxNGPopErrors(ctxt, errNr);
9431 while ((ctxt->err != NULL) &&
9432 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9433 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9434 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9435 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9436 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9437 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9438 xmlRelaxNGValidErrorPop(ctxt);
9440 errNr = ctxt->errNr;
9442 oldflags = ctxt->flags;
9443 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9444 ctxt->flags -= FLAGS_MIXED_CONTENT;
9446 state = xmlRelaxNGNewValidState(ctxt, node);
9447 if (state == NULL) {
9449 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9450 xmlRelaxNGDumpValidError(ctxt);
9454 oldstate = ctxt->state;
9455 ctxt->state = state;
9456 if (define->attrs != NULL) {
9457 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9460 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9463 if (define->contModel != NULL) {
9464 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9465 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9468 nstate = xmlRelaxNGNewValidState(ctxt, node);
9469 ctxt->state = nstate;
9470 ctxt->states = NULL;
9472 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9475 nseq = ctxt->state->seq;
9476 ctxt->state = tmpstate;
9477 ctxt->states = tmpstates;
9478 xmlRelaxNGFreeValidState(ctxt, nstate);
9480 #ifdef DEBUG_COMPILE
9481 xmlGenericError(xmlGenericErrorContext,
9482 "Validating content of '%s' : %d\n", define->name, tmp);
9487 if (ctxt->states != NULL) {
9490 ctxt->flags |= FLAGS_IGNORABLE;
9492 for (i = 0; i < ctxt->states->nbState; i++) {
9493 state = ctxt->states->tabState[i];
9494 ctxt->state = state;
9495 ctxt->state->seq = nseq;
9497 if (xmlRelaxNGValidateElementEnd(ctxt) == 0)
9499 xmlRelaxNGFreeValidState(ctxt, state);
9501 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9502 ctxt->flags = oldflags;
9503 ctxt->states = NULL;
9504 if ((ret == 0) && (tmp == -1))
9507 state = ctxt->state;
9508 ctxt->state->seq = nseq;
9510 ret = xmlRelaxNGValidateElementEnd(ctxt);
9511 xmlRelaxNGFreeValidState(ctxt, state);
9514 if (define->content != NULL) {
9515 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
9519 if (ctxt->state == NULL) {
9520 ctxt->state = oldstate;
9521 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9525 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9531 if (ctxt->states != NULL) {
9534 ctxt->flags |= FLAGS_IGNORABLE;
9536 for (i = 0; i < ctxt->states->nbState; i++) {
9537 state = ctxt->states->tabState[i];
9538 ctxt->state = state;
9540 if (xmlRelaxNGValidateElementEnd(ctxt) == 0)
9542 xmlRelaxNGFreeValidState(ctxt, state);
9544 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9545 ctxt->flags = oldflags;
9546 ctxt->states = NULL;
9547 if ((ret == 0) && (tmp == -1))
9550 state = ctxt->state;
9552 ret = xmlRelaxNGValidateElementEnd(ctxt);
9553 xmlRelaxNGFreeValidState(ctxt, state);
9557 node->_private = define;
9559 ctxt->flags = oldflags;
9560 ctxt->state = oldstate;
9561 if (oldstate != NULL)
9562 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9564 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9565 xmlRelaxNGDumpValidError(ctxt);
9571 if (ctxt->errNr > errNr)
9572 xmlRelaxNGPopErrors(ctxt, errNr);
9576 xmlGenericError(xmlGenericErrorContext,
9577 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9579 if (oldstate == NULL)
9580 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9581 else if (oldstate->seq == NULL)
9582 xmlGenericError(xmlGenericErrorContext, ": done\n");
9583 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9584 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9585 oldstate->seq->name);
9587 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9588 oldstate->seq->name, oldstate->seq->type);
9591 case XML_RELAXNG_OPTIONAL:{
9592 errNr = ctxt->errNr;
9593 oldflags = ctxt->flags;
9594 ctxt->flags |= FLAGS_IGNORABLE;
9595 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9597 xmlRelaxNGValidateDefinitionList(ctxt,
9600 if (ctxt->state != NULL)
9601 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9602 ctxt->state = oldstate;
9603 ctxt->flags = oldflags;
9605 if (ctxt->errNr > errNr)
9606 xmlRelaxNGPopErrors(ctxt, errNr);
9609 if (ctxt->states != NULL) {
9610 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9612 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9613 if (ctxt->states == NULL) {
9614 xmlRelaxNGFreeValidState(ctxt, oldstate);
9615 ctxt->flags = oldflags;
9617 if (ctxt->errNr > errNr)
9618 xmlRelaxNGPopErrors(ctxt, errNr);
9621 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9622 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9625 ctxt->flags = oldflags;
9627 if (ctxt->errNr > errNr)
9628 xmlRelaxNGPopErrors(ctxt, errNr);
9631 case XML_RELAXNG_ONEORMORE:
9632 errNr = ctxt->errNr;
9633 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9637 if (ctxt->errNr > errNr)
9638 xmlRelaxNGPopErrors(ctxt, errNr);
9639 /* no break on purpose */
9640 case XML_RELAXNG_ZEROORMORE:{
9642 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9645 errNr = ctxt->errNr;
9646 res = xmlRelaxNGNewStates(ctxt, 1);
9652 * All the input states are also exit states
9654 if (ctxt->state != NULL) {
9655 xmlRelaxNGAddStates(ctxt, res,
9656 xmlRelaxNGCopyValidState(ctxt,
9660 for (j = 0; j < ctxt->states->nbState; j++) {
9661 xmlRelaxNGAddStates(ctxt, res,
9662 xmlRelaxNGCopyValidState(ctxt,
9669 oldflags = ctxt->flags;
9670 ctxt->flags |= FLAGS_IGNORABLE;
9673 base = res->nbState;
9675 if (ctxt->states != NULL) {
9676 states = ctxt->states;
9677 for (i = 0; i < states->nbState; i++) {
9678 ctxt->state = states->tabState[i];
9679 ctxt->states = NULL;
9680 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9684 if (ctxt->state != NULL) {
9685 tmp = xmlRelaxNGAddStates(ctxt, res,
9690 } else if (ctxt->states != NULL) {
9691 for (j = 0; j < ctxt->states->nbState;
9694 xmlRelaxNGAddStates(ctxt, res,
9702 xmlRelaxNGFreeStates(ctxt,
9704 ctxt->states = NULL;
9707 if (ctxt->state != NULL) {
9708 xmlRelaxNGFreeValidState(ctxt,
9715 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9719 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9722 base = res->nbState;
9723 if (ctxt->state != NULL) {
9724 tmp = xmlRelaxNGAddStates(ctxt, res,
9729 } else if (ctxt->states != NULL) {
9730 for (j = 0; j < ctxt->states->nbState; j++) {
9731 tmp = xmlRelaxNGAddStates(ctxt, res,
9738 if (states == NULL) {
9739 states = ctxt->states;
9741 xmlRelaxNGFreeStates(ctxt,
9744 ctxt->states = NULL;
9750 * Collect all the new nodes added at that step
9751 * and make them the new node set
9753 if (res->nbState - base == 1) {
9754 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9759 if (states == NULL) {
9760 xmlRelaxNGNewStates(ctxt,
9761 res->nbState - base);
9763 states->nbState = 0;
9764 for (i = base; i < res->nbState; i++)
9765 xmlRelaxNGAddStates(ctxt, states,
9766 xmlRelaxNGCopyValidState
9769 ctxt->states = states;
9772 } while (progress == 1);
9773 if (states != NULL) {
9774 xmlRelaxNGFreeStates(ctxt, states);
9777 ctxt->flags = oldflags;
9778 if (ctxt->errNr > errNr)
9779 xmlRelaxNGPopErrors(ctxt, errNr);
9783 case XML_RELAXNG_CHOICE:{
9784 xmlRelaxNGDefinePtr list = NULL;
9785 xmlRelaxNGStatesPtr states = NULL;
9787 node = xmlRelaxNGSkipIgnored(ctxt, node);
9789 errNr = ctxt->errNr;
9790 if ((define->dflags & IS_TRIABLE)
9791 && (define->data != NULL)) {
9792 xmlHashTablePtr triage =
9793 (xmlHashTablePtr) define->data;
9796 * Something we can optimize cleanly there is only one
9797 * possble branch out !
9803 if ((node->type == XML_TEXT_NODE) ||
9804 (node->type == XML_CDATA_SECTION_NODE)) {
9806 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
9807 } else if (node->type == XML_ELEMENT_NODE) {
9808 if (node->ns != NULL) {
9809 list = xmlHashLookup2(triage, node->name,
9813 xmlHashLookup2(triage, BAD_CAST "#any",
9817 xmlHashLookup2(triage, node->name, NULL);
9820 xmlHashLookup2(triage, BAD_CAST "#any",
9827 ret = xmlRelaxNGValidateDefinition(ctxt, list);
9833 list = define->content;
9834 oldflags = ctxt->flags;
9835 ctxt->flags |= FLAGS_IGNORABLE;
9837 while (list != NULL) {
9838 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9839 ret = xmlRelaxNGValidateDefinition(ctxt, list);
9841 if (states == NULL) {
9842 states = xmlRelaxNGNewStates(ctxt, 1);
9844 if (ctxt->state != NULL) {
9845 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
9846 } else if (ctxt->states != NULL) {
9847 for (i = 0; i < ctxt->states->nbState; i++) {
9848 xmlRelaxNGAddStates(ctxt, states,
9852 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9853 ctxt->states = NULL;
9856 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9858 ctxt->state = oldstate;
9861 if (states != NULL) {
9862 xmlRelaxNGFreeValidState(ctxt, oldstate);
9863 ctxt->states = states;
9867 ctxt->states = NULL;
9869 ctxt->flags = oldflags;
9871 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9872 xmlRelaxNGDumpValidError(ctxt);
9875 if (ctxt->errNr > errNr)
9876 xmlRelaxNGPopErrors(ctxt, errNr);
9880 case XML_RELAXNG_DEF:
9881 case XML_RELAXNG_GROUP:
9882 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9884 case XML_RELAXNG_INTERLEAVE:
9885 ret = xmlRelaxNGValidateInterleave(ctxt, define);
9887 case XML_RELAXNG_ATTRIBUTE:
9888 ret = xmlRelaxNGValidateAttribute(ctxt, define);
9890 case XML_RELAXNG_START:
9891 case XML_RELAXNG_NOOP:
9892 case XML_RELAXNG_REF:
9893 case XML_RELAXNG_EXTERNALREF:
9894 case XML_RELAXNG_PARENTREF:
9895 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
9897 case XML_RELAXNG_DATATYPE:{
9899 xmlChar *content = NULL;
9902 while (child != NULL) {
9903 if (child->type == XML_ELEMENT_NODE) {
9904 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
9905 node->parent->name);
9908 } else if ((child->type == XML_TEXT_NODE) ||
9909 (child->type == XML_CDATA_SECTION_NODE)) {
9910 content = xmlStrcat(content, child->content);
9912 /* TODO: handle entities ... */
9913 child = child->next;
9916 if (content != NULL)
9920 if (content == NULL) {
9921 content = xmlStrdup(BAD_CAST "");
9922 if (content == NULL) {
9923 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
9928 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
9931 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
9932 } else if (ret == 0) {
9933 ctxt->state->seq = NULL;
9935 if (content != NULL)
9939 case XML_RELAXNG_VALUE:{
9940 xmlChar *content = NULL;
9945 while (child != NULL) {
9946 if (child->type == XML_ELEMENT_NODE) {
9947 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
9948 node->parent->name);
9951 } else if ((child->type == XML_TEXT_NODE) ||
9952 (child->type == XML_CDATA_SECTION_NODE)) {
9953 content = xmlStrcat(content, child->content);
9955 /* TODO: handle entities ... */
9956 child = child->next;
9959 if (content != NULL)
9963 if (content == NULL) {
9964 content = xmlStrdup(BAD_CAST "");
9965 if (content == NULL) {
9966 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
9971 oldvalue = ctxt->state->value;
9972 ctxt->state->value = content;
9973 ret = xmlRelaxNGValidateValue(ctxt, define);
9974 ctxt->state->value = oldvalue;
9976 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
9977 } else if (ret == 0) {
9978 ctxt->state->seq = NULL;
9980 if (content != NULL)
9984 case XML_RELAXNG_LIST:{
9987 xmlChar *oldvalue, *oldendvalue;
9991 * Make sure it's only text nodes
9996 while (child != NULL) {
9997 if (child->type == XML_ELEMENT_NODE) {
9998 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
9999 node->parent->name);
10002 } else if ((child->type == XML_TEXT_NODE) ||
10003 (child->type == XML_CDATA_SECTION_NODE)) {
10004 content = xmlStrcat(content, child->content);
10006 /* TODO: handle entities ... */
10007 child = child->next;
10010 if (content != NULL)
10014 if (content == NULL) {
10015 content = xmlStrdup(BAD_CAST "");
10016 if (content == NULL) {
10017 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
10022 len = xmlStrlen(content);
10023 oldvalue = ctxt->state->value;
10024 oldendvalue = ctxt->state->endvalue;
10025 ctxt->state->value = content;
10026 ctxt->state->endvalue = content + len;
10027 ret = xmlRelaxNGValidateValue(ctxt, define);
10028 ctxt->state->value = oldvalue;
10029 ctxt->state->endvalue = oldendvalue;
10031 VALID_ERR(XML_RELAXNG_ERR_LIST);
10032 } else if ((ret == 0) && (node != NULL)) {
10033 ctxt->state->seq = node->next;
10035 if (content != NULL)
10039 case XML_RELAXNG_EXCEPT:
10040 case XML_RELAXNG_PARAM:
10046 for (i = 0; i < ctxt->depth; i++)
10047 xmlGenericError(xmlGenericErrorContext, " ");
10048 xmlGenericError(xmlGenericErrorContext,
10049 "Validating %s ", xmlRelaxNGDefName(define));
10050 if (define->name != NULL)
10051 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
10053 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
10055 xmlGenericError(xmlGenericErrorContext, "failed\n");
10061 * xmlRelaxNGValidateDefinition:
10062 * @ctxt: a Relax-NG validation context
10063 * @define: the definition to verify
10065 * Validate the current node lists against the definition
10067 * Returns 0 if the validation succeeded or an error code.
10070 xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10071 xmlRelaxNGDefinePtr define) {
10072 xmlRelaxNGStatesPtr states, res;
10073 int i, j, k, ret, oldflags;
10076 * We should NOT have both ctxt->state and ctxt->states
10078 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10080 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
10081 ctxt->state = NULL;
10084 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
10085 if (ctxt->states != NULL) {
10086 ctxt->state = ctxt->states->tabState[0];
10087 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10088 ctxt->states = NULL;
10090 ret = xmlRelaxNGValidateState(ctxt, define);
10091 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10093 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
10094 ctxt->state = NULL;
10096 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10097 ctxt->state = ctxt->states->tabState[0];
10098 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10099 ctxt->states = NULL;
10104 states = ctxt->states;
10105 ctxt->states = NULL;
10108 oldflags = ctxt->flags;
10109 ctxt->flags |= FLAGS_IGNORABLE;
10110 for (i = 0;i < states->nbState;i++) {
10111 ctxt->state = states->tabState[i];
10112 ctxt->states = NULL;
10113 ret = xmlRelaxNGValidateState(ctxt, define);
10115 * We should NOT have both ctxt->state and ctxt->states
10117 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10119 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
10120 ctxt->state = NULL;
10123 if (ctxt->states == NULL) {
10125 /* add the state to the container */
10126 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10127 ctxt->state = NULL;
10129 /* add the state directly in states */
10130 states->tabState[j++] = ctxt->state;
10131 ctxt->state = NULL;
10135 /* make it the new container and copy other results */
10136 res = ctxt->states;
10137 ctxt->states = NULL;
10138 for (k = 0;k < j;k++)
10139 xmlRelaxNGAddStates(ctxt, res, states->tabState[k]);
10141 /* add all the new results to res and reff the container */
10142 for (k = 0;k < ctxt->states->nbState;k++)
10143 xmlRelaxNGAddStates(ctxt, res,
10144 ctxt->states->tabState[k]);
10145 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10146 ctxt->states = NULL;
10150 if (ctxt->state != NULL) {
10151 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
10152 ctxt->state = NULL;
10153 } else if (ctxt->states != NULL) {
10154 for (k = 0;k < ctxt->states->nbState;k++)
10155 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]);
10156 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10157 ctxt->states = NULL;
10161 ctxt->flags = oldflags;
10163 xmlRelaxNGFreeStates(ctxt, states);
10164 ctxt->states = res;
10166 } else if (j > 1) {
10167 states->nbState = j;
10168 ctxt->states = states;
10170 } else if (j == 1) {
10171 ctxt->state = states->tabState[0];
10172 xmlRelaxNGFreeStates(ctxt, states);
10176 xmlRelaxNGFreeStates(ctxt, states);
10177 if (ctxt->states != NULL) {
10178 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10179 ctxt->states = NULL;
10182 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10184 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
10185 ctxt->state = NULL;
10191 * xmlRelaxNGValidateDocument:
10192 * @ctxt: a Relax-NG validation context
10193 * @doc: the document
10195 * Validate the given document
10197 * Returns 0 if the validation succeeded or an error code.
10200 xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
10202 xmlRelaxNGPtr schema;
10203 xmlRelaxNGGrammarPtr grammar;
10204 xmlRelaxNGValidStatePtr state;
10207 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
10210 ctxt->errNo = XML_RELAXNG_OK;
10211 schema = ctxt->schema;
10212 grammar = schema->topgrammar;
10213 if (grammar == NULL) {
10214 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10217 state = xmlRelaxNGNewValidState(ctxt, NULL);
10218 ctxt->state = state;
10219 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
10220 if ((ctxt->state != NULL) && (state->seq != NULL)) {
10221 state = ctxt->state;
10223 node = xmlRelaxNGSkipIgnored(ctxt, node);
10224 if (node != NULL) {
10226 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10230 } else if (ctxt->states != NULL) {
10234 for (i = 0;i < ctxt->states->nbState;i++) {
10235 state = ctxt->states->tabState[i];
10237 node = xmlRelaxNGSkipIgnored(ctxt, node);
10240 xmlRelaxNGFreeValidState(ctxt,state);
10244 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10249 if (ctxt->state != NULL) {
10250 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10251 ctxt->state = NULL;
10254 xmlRelaxNGDumpValidError(ctxt);
10256 else if (ctxt->errNr != 0) {
10257 ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n",
10259 xmlRelaxNGDumpValidError(ctxt);
10262 if (ctxt->idref == 1) {
10263 xmlValidCtxt vctxt;
10265 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10267 vctxt.error = ctxt->error;
10268 vctxt.warning = ctxt->warning;
10269 vctxt.userData = ctxt->userData;
10271 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10274 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
10280 /************************************************************************
10282 * Validation interfaces *
10284 ************************************************************************/
10286 * xmlRelaxNGNewValidCtxt:
10287 * @schema: a precompiled XML RelaxNGs
10289 * Create an XML RelaxNGs validation context based on the given schema
10291 * Returns the validation context or NULL in case of error
10293 xmlRelaxNGValidCtxtPtr
10294 xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) {
10295 xmlRelaxNGValidCtxtPtr ret;
10297 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10299 xmlGenericError(xmlGenericErrorContext,
10300 "Failed to allocate new schema validation context\n");
10303 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10304 ret->schema = schema;
10305 ret->error = xmlGenericError;
10306 ret->userData = xmlGenericErrorContext;
10310 ret->errTab = NULL;
10311 ret->idref = schema->idref;
10312 ret->states = NULL;
10313 ret->freeState = NULL;
10314 ret->freeStates = NULL;
10315 ret->errNo = XML_RELAXNG_OK;
10320 * xmlRelaxNGFreeValidCtxt:
10321 * @ctxt: the schema validation context
10323 * Free the resources associated to the schema validation context
10326 xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) {
10331 if (ctxt->states != NULL)
10332 xmlRelaxNGFreeStates(NULL, ctxt->states);
10333 if (ctxt->freeState != NULL) {
10334 for (k = 0;k < ctxt->freeState->nbState;k++) {
10335 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10337 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
10339 if (ctxt->freeStates != NULL) {
10340 for (k = 0;k < ctxt->freeStatesNr;k++) {
10341 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10343 xmlFree(ctxt->freeStates);
10345 if (ctxt->errTab != NULL)
10346 xmlFree(ctxt->errTab);
10347 if (ctxt->elemTab != NULL) {
10348 xmlRegExecCtxtPtr exec;
10350 exec = xmlRelaxNGElemPop(ctxt);
10351 while (exec != NULL) {
10352 xmlRegFreeExecCtxt(exec);
10353 exec = xmlRelaxNGElemPop(ctxt);
10355 xmlFree(ctxt->elemTab);
10361 * xmlRelaxNGSetValidErrors:
10362 * @ctxt: a Relax-NG validation context
10363 * @err: the error function
10364 * @warn: the warning function
10365 * @ctx: the functions context
10367 * Set the error and warning callback informations
10370 xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10371 xmlRelaxNGValidityErrorFunc err,
10372 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
10376 ctxt->warning = warn;
10377 ctxt->userData = ctx;
10381 * xmlRelaxNGGetValidErrors:
10382 * @ctxt: a Relax-NG validation context
10383 * @err: the error function result
10384 * @warn: the warning function result
10385 * @ctx: the functions context result
10387 * Get the error and warning callback informations
10389 * Returns -1 in case of error and 0 otherwise
10392 xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10393 xmlRelaxNGValidityErrorFunc *err,
10394 xmlRelaxNGValidityWarningFunc *warn, void **ctx) {
10397 if (err != NULL) *err = ctxt->error;
10398 if (warn != NULL) *warn = ctxt->warning;
10399 if (ctx != NULL) *ctx = ctxt->userData;
10404 * xmlRelaxNGValidateDoc:
10405 * @ctxt: a Relax-NG validation context
10406 * @doc: a parsed document tree
10408 * Validate a document tree in memory.
10410 * Returns 0 if the document is valid, a positive error code
10411 * number otherwise and -1 in case of internal or API error.
10414 xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
10417 if ((ctxt == NULL) || (doc == NULL))
10422 ret = xmlRelaxNGValidateDocument(ctxt, doc);
10424 * TODO: build error codes
10431 #endif /* LIBXML_SCHEMAS_ENABLED */