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, typ) \
43 ((node != NULL) && (node->ns != NULL) && \
44 (node->type == XML_ELEMENT_NODE) && \
45 (xmlStrEqual(node->name, (const xmlChar *) typ)) && \
46 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
52 #define DEBUG_GRAMMAR 1
54 #define DEBUG_CONTENT 1
60 #define DEBUG_INTERLEAVE 1
64 #define DEBUG_INCLUDE 1
68 #define DEBUG_COMPILE 1
70 #define DEBUG_PROGRESSIVE 1
76 xmlGenericError(xmlGenericErrorContext, \
77 "Unimplemented block at %s:%d\n", \
80 typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
81 typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
83 typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
84 typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
86 typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
87 typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
89 typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
90 typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
93 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
94 XML_RELAXNG_COMBINE_CHOICE, /* choice */
95 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
99 XML_RELAXNG_CONTENT_ERROR = -1,
100 XML_RELAXNG_CONTENT_EMPTY = 0,
101 XML_RELAXNG_CONTENT_SIMPLE,
102 XML_RELAXNG_CONTENT_COMPLEX
103 } xmlRelaxNGContentType;
105 typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
106 typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
108 struct _xmlRelaxNGGrammar {
109 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
110 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
111 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
112 xmlRelaxNGDefinePtr start; /* <start> content */
113 xmlRelaxNGCombine combine; /* the default combine value */
114 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
115 xmlHashTablePtr defs; /* define* */
116 xmlHashTablePtr refs; /* references */
121 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
122 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
123 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
124 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
125 XML_RELAXNG_TEXT, /* textual content */
126 XML_RELAXNG_ELEMENT, /* an element */
127 XML_RELAXNG_DATATYPE, /* extenal data type definition */
128 XML_RELAXNG_PARAM, /* extenal data type parameter */
129 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
130 XML_RELAXNG_LIST, /* a list of patterns */
131 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
132 XML_RELAXNG_DEF, /* a definition */
133 XML_RELAXNG_REF, /* reference to a definition */
134 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
135 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
136 XML_RELAXNG_OPTIONAL, /* optional patterns */
137 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
138 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
139 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
140 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
141 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
142 XML_RELAXNG_START /* Used to keep track of starts on grammars */
145 #define IS_NULLABLE (1 << 0)
146 #define IS_NOT_NULLABLE (1 << 1)
147 #define IS_INDETERMINIST (1 << 2)
148 #define IS_MIXED (1 << 3)
149 #define IS_TRIABLE (1 << 4)
150 #define IS_PROCESSED (1 << 5)
151 #define IS_COMPILABLE (1 << 6)
152 #define IS_NOT_COMPILABLE (1 << 7)
153 #define IS_EXTERNAL_REF (1 << 8)
155 struct _xmlRelaxNGDefine {
156 xmlRelaxNGType type; /* the type of definition */
157 xmlNodePtr node; /* the node in the source */
158 xmlChar *name; /* the element local name if present */
159 xmlChar *ns; /* the namespace local name if present */
160 xmlChar *value; /* value when available */
161 void *data; /* data lib or specific pointer */
162 xmlRelaxNGDefinePtr content; /* the expected content */
163 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
164 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
165 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
166 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
167 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
168 short depth; /* used for the cycle detection */
169 short dflags; /* define related flags */
170 xmlRegexpPtr contModel; /* a compiled content model if available */
176 * A RelaxNGs definition
179 void *_private; /* unused by the library for users or bindings */
180 xmlRelaxNGGrammarPtr topgrammar;
183 int idref; /* requires idref checking */
185 xmlHashTablePtr defs; /* define */
186 xmlHashTablePtr refs; /* references */
187 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
188 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
189 int defNr; /* number of defines used */
190 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
194 #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
195 #define XML_RELAXNG_IN_ONEORMORE (1 << 1)
196 #define XML_RELAXNG_IN_LIST (1 << 2)
197 #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
198 #define XML_RELAXNG_IN_START (1 << 4)
199 #define XML_RELAXNG_IN_OOMGROUP (1 << 5)
200 #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
201 #define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
202 #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
203 #define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
205 struct _xmlRelaxNGParserCtxt {
206 void *userData; /* user specific data block */
207 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
208 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
209 xmlStructuredErrorFunc serror;
210 xmlRelaxNGValidErr err;
212 xmlRelaxNGPtr schema; /* The schema in use */
213 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
214 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
215 int flags; /* parser flags */
216 int nbErrors; /* number of errors at parse time */
217 int nbWarnings; /* number of warnings at parse time */
218 const xmlChar *define; /* the current define scope */
219 xmlRelaxNGDefinePtr def; /* the current define */
222 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
224 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
225 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
229 int defNr; /* number of defines used */
230 int defMax; /* number of defines aloocated */
231 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
236 /* the document stack */
237 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
238 int docNr; /* Depth of the parsing stack */
239 int docMax; /* Max depth of the parsing stack */
240 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
242 /* the include stack */
243 xmlRelaxNGIncludePtr inc; /* Current parsed include */
244 int incNr; /* Depth of the include parsing stack */
245 int incMax; /* Max depth of the parsing stack */
246 xmlRelaxNGIncludePtr *incTab; /* array of incs */
248 int idref; /* requires idref checking */
250 /* used to compile content models */
251 xmlAutomataPtr am; /* the automata */
252 xmlAutomataStatePtr state; /* used to build the automata */
254 int crng; /* compact syntax and other flags */
255 int freedoc; /* need to free the document */
258 #define FLAGS_IGNORABLE 1
259 #define FLAGS_NEGATIVE 2
260 #define FLAGS_MIXED_CONTENT 4
261 #define FLAGS_NOERROR 8
264 * xmlRelaxNGInterleaveGroup:
266 * A RelaxNGs partition set associated to lists of definitions
268 typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
269 typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
270 struct _xmlRelaxNGInterleaveGroup {
271 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
272 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
273 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
276 #define IS_DETERMINIST 1
277 #define IS_NEEDCHECK 2
280 * xmlRelaxNGPartitions:
282 * A RelaxNGs partition associated to an interleave group
284 typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
285 typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
286 struct _xmlRelaxNGPartition {
287 int nbgroups; /* number of groups in the partitions */
288 xmlHashTablePtr triage; /* hash table used to direct nodes to the
289 * right group when possible */
290 int flags; /* determinist ? */
291 xmlRelaxNGInterleaveGroupPtr *groups;
295 * xmlRelaxNGValidState:
297 * A RelaxNGs validation state
300 typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
301 typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
302 struct _xmlRelaxNGValidState {
303 xmlNodePtr node; /* the current node */
304 xmlNodePtr seq; /* the sequence of children left to validate */
305 int nbAttrs; /* the number of attributes */
306 int maxAttrs; /* the size of attrs */
307 int nbAttrLeft; /* the number of attributes left to validate */
308 xmlChar *value; /* the value when operating on string */
309 xmlChar *endvalue; /* the end value when operating on string */
310 xmlAttrPtr *attrs; /* the array of attributes */
316 * A RelaxNGs container for validation state
318 typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
319 typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
320 struct _xmlRelaxNGStates {
321 int nbState; /* the number of states */
322 int maxState; /* the size of the array */
323 xmlRelaxNGValidStatePtr *tabState;
326 #define ERROR_IS_DUP 1
329 * xmlRelaxNGValidError:
331 * A RelaxNGs validation error
333 typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
334 typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
335 struct _xmlRelaxNGValidError {
336 xmlRelaxNGValidErr err; /* the error number */
337 int flags; /* flags */
338 xmlNodePtr node; /* the current node */
339 xmlNodePtr seq; /* the current child */
340 const xmlChar *arg1; /* first arg */
341 const xmlChar *arg2; /* second arg */
345 * xmlRelaxNGValidCtxt:
347 * A RelaxNGs validation context
350 struct _xmlRelaxNGValidCtxt {
351 void *userData; /* user specific data block */
352 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
353 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
354 xmlStructuredErrorFunc serror;
355 int nbErrors; /* number of errors in validation */
357 xmlRelaxNGPtr schema; /* The schema in use */
358 xmlDocPtr doc; /* the document being validated */
359 int flags; /* validation flags */
360 int depth; /* validation depth */
361 int idref; /* requires idref checking */
362 int errNo; /* the first error found */
365 * Errors accumulated in branches may have to be stacked to be
366 * provided back when it's sure they affect validation.
368 xmlRelaxNGValidErrorPtr err; /* Last error */
369 int errNr; /* Depth of the error stack */
370 int errMax; /* Max depth of the error stack */
371 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
373 xmlRelaxNGValidStatePtr state; /* the current validation state */
374 xmlRelaxNGStatesPtr states; /* the accumulated state list */
376 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
379 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
382 * This is used for "progressive" validation
384 xmlRegExecCtxtPtr elem; /* the current element regexp */
385 int elemNr; /* the number of element validated */
386 int elemMax; /* the max depth of elements */
387 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
388 int pstate; /* progressive state */
389 xmlNodePtr pnode; /* the current node */
390 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
391 int perr; /* signal error in content model
392 * outside the regexp */
398 * Structure associated to a RelaxNGs document element
400 struct _xmlRelaxNGInclude {
401 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
402 xmlChar *href; /* the normalized href value */
403 xmlDocPtr doc; /* the associated XML document */
404 xmlRelaxNGDefinePtr content; /* the definitions */
405 xmlRelaxNGPtr schema; /* the schema */
409 * xmlRelaxNGDocument:
411 * Structure associated to a RelaxNGs document element
413 struct _xmlRelaxNGDocument {
414 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
415 xmlChar *href; /* the normalized href value */
416 xmlDocPtr doc; /* the associated XML document */
417 xmlRelaxNGDefinePtr content; /* the definitions */
418 xmlRelaxNGPtr schema; /* the schema */
419 int externalRef; /* 1 if an external ref */
423 /************************************************************************
425 * Some factorized error routines *
427 ************************************************************************/
431 * @ctxt: an Relax-NG parser context
432 * @extra: extra informations
434 * Handle a redefinition of attribute error
437 xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
439 xmlStructuredErrorFunc schannel = NULL;
440 xmlGenericErrorFunc channel = NULL;
444 if (ctxt->serror != NULL)
445 schannel = ctxt->serror;
447 channel = ctxt->error;
448 data = ctxt->userData;
452 __xmlRaiseError(schannel, channel, data,
453 NULL, NULL, XML_FROM_RELAXNGP,
454 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
456 "Memory allocation failed : %s\n", extra);
458 __xmlRaiseError(schannel, channel, data,
459 NULL, NULL, XML_FROM_RELAXNGP,
460 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
461 NULL, NULL, 0, 0, "Memory allocation failed\n");
466 * @ctxt: a Relax-NG validation context
467 * @extra: extra informations
469 * Handle a redefinition of attribute error
472 xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
474 xmlStructuredErrorFunc schannel = NULL;
475 xmlGenericErrorFunc channel = NULL;
479 if (ctxt->serror != NULL)
480 schannel = ctxt->serror;
482 channel = ctxt->error;
483 data = ctxt->userData;
487 __xmlRaiseError(schannel, channel, data,
488 NULL, NULL, XML_FROM_RELAXNGV,
489 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
491 "Memory allocation failed : %s\n", extra);
493 __xmlRaiseError(schannel, channel, data,
494 NULL, NULL, XML_FROM_RELAXNGV,
495 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
496 NULL, NULL, 0, 0, "Memory allocation failed\n");
501 * @ctxt: a Relax-NG parser context
502 * @node: the node raising the error
503 * @error: the error code
508 * Handle a Relax NG Parsing error
511 xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
512 const char *msg, const xmlChar * str1, const xmlChar * str2)
514 xmlStructuredErrorFunc schannel = NULL;
515 xmlGenericErrorFunc channel = NULL;
519 if (ctxt->serror != NULL)
520 schannel = ctxt->serror;
522 channel = ctxt->error;
523 data = ctxt->userData;
526 __xmlRaiseError(schannel, channel, data,
527 NULL, node, XML_FROM_RELAXNGP,
528 error, XML_ERR_ERROR, NULL, 0,
529 (const char *) str1, (const char *) str2, NULL, 0, 0,
535 * @ctxt: a Relax-NG validation context
536 * @node: the node raising the error
537 * @error: the error code
542 * Handle a Relax NG Validation error
545 xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
546 const char *msg, const xmlChar * str1, const xmlChar * str2)
548 xmlStructuredErrorFunc schannel = NULL;
549 xmlGenericErrorFunc channel = NULL;
553 if (ctxt->serror != NULL)
554 schannel = ctxt->serror;
556 channel = ctxt->error;
557 data = ctxt->userData;
560 __xmlRaiseError(schannel, channel, data,
561 NULL, node, XML_FROM_RELAXNGV,
562 error, XML_ERR_ERROR, NULL, 0,
563 (const char *) str1, (const char *) str2, NULL, 0, 0,
567 /************************************************************************
569 * Preliminary type checking interfaces *
571 ************************************************************************/
574 * xmlRelaxNGTypeHave:
575 * @data: data needed for the library
576 * @type: the type name
577 * @value: the value to check
579 * Function provided by a type library to check if a type is exported
581 * Returns 1 if yes, 0 if no and -1 in case of error.
583 typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
586 * xmlRelaxNGTypeCheck:
587 * @data: data needed for the library
588 * @type: the type name
589 * @value: the value to check
590 * @result: place to store the result if needed
592 * Function provided by a type library to check if a value match a type
594 * Returns 1 if yes, 0 if no and -1 in case of error.
596 typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
597 const xmlChar * value, void **result,
601 * xmlRelaxNGFacetCheck:
602 * @data: data needed for the library
603 * @type: the type name
604 * @facet: the facet name
605 * @val: the facet value
606 * @strval: the string value
607 * @value: the value to check
609 * Function provided by a type library to check a value facet
611 * Returns 1 if yes, 0 if no and -1 in case of error.
613 typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
614 const xmlChar * facet,
616 const xmlChar * strval, void *value);
619 * xmlRelaxNGTypeFree:
620 * @data: data needed for the library
621 * @result: the value to free
623 * Function provided by a type library to free a returned result
625 typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
628 * xmlRelaxNGTypeCompare:
629 * @data: data needed for the library
630 * @type: the type name
631 * @value1: the first value
632 * @value2: the second value
634 * Function provided by a type library to compare two values accordingly
637 * Returns 1 if yes, 0 if no and -1 in case of error.
639 typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
640 const xmlChar * value1,
643 const xmlChar * value2,
645 typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
646 typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
647 struct _xmlRelaxNGTypeLibrary {
648 const xmlChar *namespace; /* the datatypeLibrary value */
649 void *data; /* data needed for the library */
650 xmlRelaxNGTypeHave have; /* the export function */
651 xmlRelaxNGTypeCheck check; /* the checking function */
652 xmlRelaxNGTypeCompare comp; /* the compare function */
653 xmlRelaxNGFacetCheck facet; /* the facet check function */
654 xmlRelaxNGTypeFree freef; /* the freeing function */
657 /************************************************************************
659 * Allocation functions *
661 ************************************************************************/
662 static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
663 static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
664 static void xmlRelaxNGNormExtSpace(xmlChar * value);
665 static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
666 static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
668 xmlRelaxNGValidStatePtr state1,
669 xmlRelaxNGValidStatePtr state2);
670 static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
671 xmlRelaxNGValidStatePtr state);
674 * xmlRelaxNGFreeDocument:
675 * @docu: a document structure
677 * Deallocate a RelaxNG document structure.
680 xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
685 if (docu->href != NULL)
687 if (docu->doc != NULL)
688 xmlFreeDoc(docu->doc);
689 if (docu->schema != NULL)
690 xmlRelaxNGFreeInnerSchema(docu->schema);
695 * xmlRelaxNGFreeDocumentList:
696 * @docu: a list of document structure
698 * Deallocate a RelaxNG document structures.
701 xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
703 xmlRelaxNGDocumentPtr next;
705 while (docu != NULL) {
707 xmlRelaxNGFreeDocument(docu);
713 * xmlRelaxNGFreeInclude:
714 * @incl: a include structure
716 * Deallocate a RelaxNG include structure.
719 xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
724 if (incl->href != NULL)
726 if (incl->doc != NULL)
727 xmlFreeDoc(incl->doc);
728 if (incl->schema != NULL)
729 xmlRelaxNGFree(incl->schema);
734 * xmlRelaxNGFreeIncludeList:
735 * @incl: a include structure list
737 * Deallocate a RelaxNG include structure.
740 xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
742 xmlRelaxNGIncludePtr next;
744 while (incl != NULL) {
746 xmlRelaxNGFreeInclude(incl);
752 * xmlRelaxNGNewRelaxNG:
753 * @ctxt: a Relax-NG validation context (optional)
755 * Allocate a new RelaxNG structure.
757 * Returns the newly allocated structure or NULL in case or error
760 xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
764 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
766 xmlRngPErrMemory(ctxt, NULL);
769 memset(ret, 0, sizeof(xmlRelaxNG));
775 * xmlRelaxNGFreeInnerSchema:
776 * @schema: a schema structure
778 * Deallocate a RelaxNG schema structure.
781 xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
786 if (schema->doc != NULL)
787 xmlFreeDoc(schema->doc);
788 if (schema->defTab != NULL) {
791 for (i = 0; i < schema->defNr; i++)
792 xmlRelaxNGFreeDefine(schema->defTab[i]);
793 xmlFree(schema->defTab);
801 * @schema: a schema structure
803 * Deallocate a RelaxNG structure.
806 xmlRelaxNGFree(xmlRelaxNGPtr schema)
811 if (schema->topgrammar != NULL)
812 xmlRelaxNGFreeGrammar(schema->topgrammar);
813 if (schema->doc != NULL)
814 xmlFreeDoc(schema->doc);
815 if (schema->documents != NULL)
816 xmlRelaxNGFreeDocumentList(schema->documents);
817 if (schema->includes != NULL)
818 xmlRelaxNGFreeIncludeList(schema->includes);
819 if (schema->defTab != NULL) {
822 for (i = 0; i < schema->defNr; i++)
823 xmlRelaxNGFreeDefine(schema->defTab[i]);
824 xmlFree(schema->defTab);
831 * xmlRelaxNGNewGrammar:
832 * @ctxt: a Relax-NG validation context (optional)
834 * Allocate a new RelaxNG grammar.
836 * Returns the newly allocated structure or NULL in case or error
838 static xmlRelaxNGGrammarPtr
839 xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
841 xmlRelaxNGGrammarPtr ret;
843 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
845 xmlRngPErrMemory(ctxt, NULL);
848 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
854 * xmlRelaxNGFreeGrammar:
855 * @grammar: a grammar structure
857 * Deallocate a RelaxNG grammar structure.
860 xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
865 if (grammar->children != NULL) {
866 xmlRelaxNGFreeGrammar(grammar->children);
868 if (grammar->next != NULL) {
869 xmlRelaxNGFreeGrammar(grammar->next);
871 if (grammar->refs != NULL) {
872 xmlHashFree(grammar->refs, NULL);
874 if (grammar->defs != NULL) {
875 xmlHashFree(grammar->defs, NULL);
882 * xmlRelaxNGNewDefine:
883 * @ctxt: a Relax-NG validation context
884 * @node: the node in the input document.
886 * Allocate a new RelaxNG define.
888 * Returns the newly allocated structure or NULL in case or error
890 static xmlRelaxNGDefinePtr
891 xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
893 xmlRelaxNGDefinePtr ret;
895 if (ctxt->defMax == 0) {
898 ctxt->defTab = (xmlRelaxNGDefinePtr *)
899 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
900 if (ctxt->defTab == NULL) {
901 xmlRngPErrMemory(ctxt, "allocating define\n");
904 } else if (ctxt->defMax <= ctxt->defNr) {
905 xmlRelaxNGDefinePtr *tmp;
908 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
911 (xmlRelaxNGDefinePtr));
913 xmlRngPErrMemory(ctxt, "allocating define\n");
918 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
920 xmlRngPErrMemory(ctxt, "allocating define\n");
923 memset(ret, 0, sizeof(xmlRelaxNGDefine));
924 ctxt->defTab[ctxt->defNr++] = ret;
931 * xmlRelaxNGFreePartition:
932 * @partitions: a partition set structure
934 * Deallocate RelaxNG partition set structures.
937 xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
939 xmlRelaxNGInterleaveGroupPtr group;
942 if (partitions != NULL) {
943 if (partitions->groups != NULL) {
944 for (j = 0; j < partitions->nbgroups; j++) {
945 group = partitions->groups[j];
947 if (group->defs != NULL)
948 xmlFree(group->defs);
949 if (group->attrs != NULL)
950 xmlFree(group->attrs);
954 xmlFree(partitions->groups);
956 if (partitions->triage != NULL) {
957 xmlHashFree(partitions->triage, NULL);
964 * xmlRelaxNGFreeDefine:
965 * @define: a define structure
967 * Deallocate a RelaxNG define structure.
970 xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
975 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
976 xmlRelaxNGTypeLibraryPtr lib;
978 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
979 if ((lib != NULL) && (lib->freef != NULL))
980 lib->freef(lib->data, (void *) define->attrs);
982 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
983 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
984 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
985 xmlHashFree((xmlHashTablePtr) define->data, NULL);
986 if (define->name != NULL)
987 xmlFree(define->name);
988 if (define->ns != NULL)
990 if (define->value != NULL)
991 xmlFree(define->value);
992 if (define->contModel != NULL)
993 xmlRegFreeRegexp(define->contModel);
998 * xmlRelaxNGNewStates:
999 * @ctxt: a Relax-NG validation context
1000 * @size: the default size for the container
1002 * Allocate a new RelaxNG validation state container
1004 * Returns the newly allocated structure or NULL in case or error
1006 static xmlRelaxNGStatesPtr
1007 xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1009 xmlRelaxNGStatesPtr ret;
1011 if ((ctxt != NULL) &&
1012 (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
1013 ctxt->freeStatesNr--;
1014 ret = ctxt->freeStates[ctxt->freeStatesNr];
1021 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
1024 sizeof(xmlRelaxNGValidStatePtr));
1026 xmlRngVErrMemory(ctxt, "allocating states\n");
1030 ret->maxState = size;
1031 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1033 (xmlRelaxNGValidStatePtr));
1034 if (ret->tabState == NULL) {
1035 xmlRngVErrMemory(ctxt, "allocating states\n");
1043 * xmlRelaxNGAddStateUniq:
1044 * @ctxt: a Relax-NG validation context
1045 * @states: the states container
1046 * @state: the validation state
1048 * Add a RelaxNG validation state to the container without checking
1051 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1054 xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
1055 xmlRelaxNGStatesPtr states,
1056 xmlRelaxNGValidStatePtr state)
1058 if (state == NULL) {
1061 if (states->nbState >= states->maxState) {
1062 xmlRelaxNGValidStatePtr *tmp;
1065 size = states->maxState * 2;
1066 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1069 (xmlRelaxNGValidStatePtr));
1071 xmlRngVErrMemory(ctxt, "adding states\n");
1074 states->tabState = tmp;
1075 states->maxState = size;
1077 states->tabState[states->nbState++] = state;
1082 * xmlRelaxNGAddState:
1083 * @ctxt: a Relax-NG validation context
1084 * @states: the states container
1085 * @state: the validation state
1087 * Add a RelaxNG validation state to the container
1089 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1092 xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1093 xmlRelaxNGStatesPtr states,
1094 xmlRelaxNGValidStatePtr state)
1098 if (state == NULL || states == NULL) {
1101 if (states->nbState >= states->maxState) {
1102 xmlRelaxNGValidStatePtr *tmp;
1105 size = states->maxState * 2;
1106 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1109 (xmlRelaxNGValidStatePtr));
1111 xmlRngVErrMemory(ctxt, "adding states\n");
1114 states->tabState = tmp;
1115 states->maxState = size;
1117 for (i = 0; i < states->nbState; i++) {
1118 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1119 xmlRelaxNGFreeValidState(ctxt, state);
1123 states->tabState[states->nbState++] = state;
1128 * xmlRelaxNGFreeStates:
1129 * @ctxt: a Relax-NG validation context
1130 * @states: teh container
1132 * Free a RelaxNG validation state container
1135 xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
1136 xmlRelaxNGStatesPtr states)
1140 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
1141 ctxt->freeStatesMax = 40;
1142 ctxt->freeStatesNr = 0;
1143 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1144 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1145 if (ctxt->freeStates == NULL) {
1146 xmlRngVErrMemory(ctxt, "storing states\n");
1148 } else if ((ctxt != NULL)
1149 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1150 xmlRelaxNGStatesPtr *tmp;
1152 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1153 2 * ctxt->freeStatesMax *
1155 (xmlRelaxNGStatesPtr));
1157 xmlRngVErrMemory(ctxt, "storing states\n");
1158 xmlFree(states->tabState);
1162 ctxt->freeStates = tmp;
1163 ctxt->freeStatesMax *= 2;
1165 if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
1166 xmlFree(states->tabState);
1169 ctxt->freeStates[ctxt->freeStatesNr++] = states;
1174 * xmlRelaxNGNewValidState:
1175 * @ctxt: a Relax-NG validation context
1176 * @node: the current node or NULL for the document
1178 * Allocate a new RelaxNG validation state
1180 * Returns the newly allocated structure or NULL in case or error
1182 static xmlRelaxNGValidStatePtr
1183 xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1185 xmlRelaxNGValidStatePtr ret;
1187 xmlAttrPtr attrs[MAX_ATTR];
1189 xmlNodePtr root = NULL;
1192 root = xmlDocGetRootElement(ctxt->doc);
1196 attr = node->properties;
1197 while (attr != NULL) {
1198 if (nbAttrs < MAX_ATTR)
1199 attrs[nbAttrs++] = attr;
1205 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1206 ctxt->freeState->nbState--;
1207 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1210 (xmlRelaxNGValidStatePtr)
1211 xmlMalloc(sizeof(xmlRelaxNGValidState));
1213 xmlRngVErrMemory(ctxt, "allocating states\n");
1216 memset(ret, 0, sizeof(xmlRelaxNGValidState));
1219 ret->endvalue = NULL;
1221 ret->node = (xmlNodePtr) ctxt->doc;
1225 ret->seq = node->children;
1229 if (ret->attrs == NULL) {
1233 ret->maxAttrs = nbAttrs;
1234 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1235 sizeof(xmlAttrPtr));
1236 if (ret->attrs == NULL) {
1237 xmlRngVErrMemory(ctxt, "allocating states\n");
1240 } else if (ret->maxAttrs < nbAttrs) {
1243 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1244 sizeof(xmlAttrPtr));
1246 xmlRngVErrMemory(ctxt, "allocating states\n");
1250 ret->maxAttrs = nbAttrs;
1252 ret->nbAttrs = nbAttrs;
1253 if (nbAttrs < MAX_ATTR) {
1254 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1256 attr = node->properties;
1258 while (attr != NULL) {
1259 ret->attrs[nbAttrs++] = attr;
1264 ret->nbAttrLeft = ret->nbAttrs;
1269 * xmlRelaxNGCopyValidState:
1270 * @ctxt: a Relax-NG validation context
1271 * @state: a validation state
1273 * Copy the validation state
1275 * Returns the newly allocated structure or NULL in case or error
1277 static xmlRelaxNGValidStatePtr
1278 xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1279 xmlRelaxNGValidStatePtr state)
1281 xmlRelaxNGValidStatePtr ret;
1282 unsigned int maxAttrs;
1287 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1288 ctxt->freeState->nbState--;
1289 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1292 (xmlRelaxNGValidStatePtr)
1293 xmlMalloc(sizeof(xmlRelaxNGValidState));
1295 xmlRngVErrMemory(ctxt, "allocating states\n");
1298 memset(ret, 0, sizeof(xmlRelaxNGValidState));
1301 maxAttrs = ret->maxAttrs;
1302 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1304 ret->maxAttrs = maxAttrs;
1305 if (state->nbAttrs > 0) {
1306 if (ret->attrs == NULL) {
1307 ret->maxAttrs = state->maxAttrs;
1308 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1309 sizeof(xmlAttrPtr));
1310 if (ret->attrs == NULL) {
1311 xmlRngVErrMemory(ctxt, "allocating states\n");
1315 } else if (ret->maxAttrs < state->nbAttrs) {
1318 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1319 sizeof(xmlAttrPtr));
1321 xmlRngVErrMemory(ctxt, "allocating states\n");
1325 ret->maxAttrs = state->maxAttrs;
1328 memcpy(ret->attrs, state->attrs,
1329 state->nbAttrs * sizeof(xmlAttrPtr));
1335 * xmlRelaxNGEqualValidState:
1336 * @ctxt: a Relax-NG validation context
1337 * @state1: a validation state
1338 * @state2: a validation state
1340 * Compare the validation states for equality
1342 * Returns 1 if equald, 0 otherwise
1345 xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1346 xmlRelaxNGValidStatePtr state1,
1347 xmlRelaxNGValidStatePtr state2)
1351 if ((state1 == NULL) || (state2 == NULL))
1353 if (state1 == state2)
1355 if (state1->node != state2->node)
1357 if (state1->seq != state2->seq)
1359 if (state1->nbAttrLeft != state2->nbAttrLeft)
1361 if (state1->nbAttrs != state2->nbAttrs)
1363 if (state1->endvalue != state2->endvalue)
1365 if ((state1->value != state2->value) &&
1366 (!xmlStrEqual(state1->value, state2->value)))
1368 for (i = 0; i < state1->nbAttrs; i++) {
1369 if (state1->attrs[i] != state2->attrs[i])
1376 * xmlRelaxNGFreeValidState:
1377 * @state: a validation state structure
1379 * Deallocate a RelaxNG validation state structure.
1382 xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1383 xmlRelaxNGValidStatePtr state)
1388 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1389 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1391 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1392 if (state->attrs != NULL)
1393 xmlFree(state->attrs);
1396 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1400 /************************************************************************
1402 * Semi internal functions *
1404 ************************************************************************/
1407 * xmlRelaxParserSetFlag:
1408 * @ctxt: a RelaxNG parser context
1409 * @flags: a set of flags values
1411 * Semi private function used to pass informations to a parser context
1412 * which are a combination of xmlRelaxNGParserFlag .
1414 * Returns 0 if success and -1 in case of error
1417 xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1419 if (ctxt == NULL) return(-1);
1420 if (flags & XML_RELAXNGP_FREE_DOC) {
1421 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1422 flags -= XML_RELAXNGP_FREE_DOC;
1424 if (flags & XML_RELAXNGP_CRNG) {
1425 ctxt->crng |= XML_RELAXNGP_CRNG;
1426 flags -= XML_RELAXNGP_CRNG;
1428 if (flags != 0) return(-1);
1432 /************************************************************************
1434 * Document functions *
1436 ************************************************************************/
1437 static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1441 * xmlRelaxNGIncludePush:
1442 * @ctxt: the parser context
1443 * @value: the element doc
1445 * Pushes a new include on top of the include stack
1447 * Returns 0 in case of error, the index in the stack otherwise
1450 xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1451 xmlRelaxNGIncludePtr value)
1453 if (ctxt->incTab == NULL) {
1457 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1458 sizeof(ctxt->incTab[0]));
1459 if (ctxt->incTab == NULL) {
1460 xmlRngPErrMemory(ctxt, "allocating include\n");
1464 if (ctxt->incNr >= ctxt->incMax) {
1467 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1469 sizeof(ctxt->incTab[0]));
1470 if (ctxt->incTab == NULL) {
1471 xmlRngPErrMemory(ctxt, "allocating include\n");
1475 ctxt->incTab[ctxt->incNr] = value;
1477 return (ctxt->incNr++);
1481 * xmlRelaxNGIncludePop:
1482 * @ctxt: the parser context
1484 * Pops the top include from the include stack
1486 * Returns the include just removed
1488 static xmlRelaxNGIncludePtr
1489 xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1491 xmlRelaxNGIncludePtr ret;
1493 if (ctxt->incNr <= 0)
1496 if (ctxt->incNr > 0)
1497 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1500 ret = ctxt->incTab[ctxt->incNr];
1501 ctxt->incTab[ctxt->incNr] = NULL;
1506 * xmlRelaxNGRemoveRedefine:
1507 * @ctxt: the parser context
1508 * @URL: the normalized URL
1509 * @target: the included target
1510 * @name: the define name to eliminate
1512 * Applies the elimination algorithm of 4.7
1514 * Returns 0 in case of error, 1 in case of success.
1517 xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1518 const xmlChar * URL ATTRIBUTE_UNUSED,
1519 xmlNodePtr target, const xmlChar * name)
1522 xmlNodePtr tmp, tmp2;
1525 #ifdef DEBUG_INCLUDE
1527 xmlGenericError(xmlGenericErrorContext,
1528 "Elimination of <include> start from %s\n", URL);
1530 xmlGenericError(xmlGenericErrorContext,
1531 "Elimination of <include> define %s from %s\n",
1535 while (tmp != NULL) {
1537 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1541 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1542 name2 = xmlGetProp(tmp, BAD_CAST "name");
1543 xmlRelaxNGNormExtSpace(name2);
1544 if (name2 != NULL) {
1545 if (xmlStrEqual(name, name2)) {
1552 } else if (IS_RELAXNG(tmp, "include")) {
1553 xmlChar *href = NULL;
1554 xmlRelaxNGDocumentPtr inc = tmp->psvi;
1556 if ((inc != NULL) && (inc->doc != NULL) &&
1557 (inc->doc->children != NULL)) {
1560 (inc->doc->children->name, BAD_CAST "grammar")) {
1561 #ifdef DEBUG_INCLUDE
1562 href = xmlGetProp(tmp, BAD_CAST "href");
1564 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1565 xmlDocGetRootElement(inc->doc)->children,
1569 #ifdef DEBUG_INCLUDE
1582 * xmlRelaxNGLoadInclude:
1583 * @ctxt: the parser context
1584 * @URL: the normalized URL
1585 * @node: the include node.
1586 * @ns: the namespace passed from the context.
1588 * First lookup if the document is already loaded into the parser context,
1589 * check against recursion. If not found the resource is loaded and
1590 * the content is preprocessed before being returned back to the caller.
1592 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1594 static xmlRelaxNGIncludePtr
1595 xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1596 xmlNodePtr node, const xmlChar * ns)
1598 xmlRelaxNGIncludePtr ret = NULL;
1601 xmlNodePtr root, cur;
1603 #ifdef DEBUG_INCLUDE
1604 xmlGenericError(xmlGenericErrorContext,
1605 "xmlRelaxNGLoadInclude(%s)\n", URL);
1609 * check against recursion in the stack
1611 for (i = 0; i < ctxt->incNr; i++) {
1612 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1613 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1614 "Detected an Include recursion for %s\n", URL,
1623 doc = xmlReadFile((const char *) URL,NULL,0);
1625 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1626 "xmlRelaxNG: could not load %s\n", URL, NULL);
1629 #ifdef DEBUG_INCLUDE
1630 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
1634 * Allocate the document structures and register it first.
1636 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1638 xmlRngPErrMemory(ctxt, "allocating include\n");
1642 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1644 ret->href = xmlStrdup(URL);
1645 ret->next = ctxt->includes;
1646 ctxt->includes = ret;
1649 * transmit the ns if needed
1652 root = xmlDocGetRootElement(doc);
1654 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1655 xmlSetProp(root, BAD_CAST "ns", ns);
1661 * push it on the stack
1663 xmlRelaxNGIncludePush(ctxt, ret);
1666 * Some preprocessing of the document content, this include recursing
1667 * in the include stack.
1669 #ifdef DEBUG_INCLUDE
1670 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
1673 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1680 * Pop up the include from the stack
1682 xmlRelaxNGIncludePop(ctxt);
1684 #ifdef DEBUG_INCLUDE
1685 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
1688 * Check that the top element is a grammar
1690 root = xmlDocGetRootElement(doc);
1692 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1693 "xmlRelaxNG: included document is empty %s\n", URL,
1697 if (!IS_RELAXNG(root, "grammar")) {
1698 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1699 "xmlRelaxNG: included document %s root is not a grammar\n",
1705 * Elimination of redefined rules in the include.
1707 cur = node->children;
1708 while (cur != NULL) {
1709 if (IS_RELAXNG(cur, "start")) {
1713 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1715 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1716 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1719 } else if (IS_RELAXNG(cur, "define")) {
1722 name = xmlGetProp(cur, BAD_CAST "name");
1724 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1725 "xmlRelaxNG: include %s has define without name\n",
1730 xmlRelaxNGNormExtSpace(name);
1731 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1732 root->children, name);
1734 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1735 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1749 * xmlRelaxNGValidErrorPush:
1750 * @ctxt: the validation context
1751 * @err: the error code
1752 * @arg1: the first string argument
1753 * @arg2: the second string argument
1754 * @dup: arg need to be duplicated
1756 * Pushes a new error on top of the error stack
1758 * Returns 0 in case of error, the index in the stack otherwise
1761 xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1762 xmlRelaxNGValidErr err, const xmlChar * arg1,
1763 const xmlChar * arg2, int dup)
1765 xmlRelaxNGValidErrorPtr cur;
1768 xmlGenericError(xmlGenericErrorContext,
1769 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
1771 if (ctxt->errTab == NULL) {
1775 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1777 (xmlRelaxNGValidError));
1778 if (ctxt->errTab == NULL) {
1779 xmlRngVErrMemory(ctxt, "pushing error\n");
1784 if (ctxt->errNr >= ctxt->errMax) {
1787 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
1790 (xmlRelaxNGValidError));
1791 if (ctxt->errTab == NULL) {
1792 xmlRngVErrMemory(ctxt, "pushing error\n");
1795 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1797 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
1798 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1799 return (ctxt->errNr);
1800 cur = &ctxt->errTab[ctxt->errNr];
1803 cur->arg1 = xmlStrdup(arg1);
1804 cur->arg2 = xmlStrdup(arg2);
1805 cur->flags = ERROR_IS_DUP;
1811 if (ctxt->state != NULL) {
1812 cur->node = ctxt->state->node;
1813 cur->seq = ctxt->state->seq;
1819 return (ctxt->errNr++);
1823 * xmlRelaxNGValidErrorPop:
1824 * @ctxt: the validation context
1826 * Pops the top error from the error stack
1829 xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1831 xmlRelaxNGValidErrorPtr cur;
1833 if (ctxt->errNr <= 0) {
1838 if (ctxt->errNr > 0)
1839 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1842 cur = &ctxt->errTab[ctxt->errNr];
1843 if (cur->flags & ERROR_IS_DUP) {
1844 if (cur->arg1 != NULL)
1845 xmlFree((xmlChar *) cur->arg1);
1847 if (cur->arg2 != NULL)
1848 xmlFree((xmlChar *) cur->arg2);
1855 * xmlRelaxNGDocumentPush:
1856 * @ctxt: the parser context
1857 * @value: the element doc
1859 * Pushes a new doc on top of the doc stack
1861 * Returns 0 in case of error, the index in the stack otherwise
1864 xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1865 xmlRelaxNGDocumentPtr value)
1867 if (ctxt->docTab == NULL) {
1871 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1872 sizeof(ctxt->docTab[0]));
1873 if (ctxt->docTab == NULL) {
1874 xmlRngPErrMemory(ctxt, "adding document\n");
1878 if (ctxt->docNr >= ctxt->docMax) {
1881 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1883 sizeof(ctxt->docTab[0]));
1884 if (ctxt->docTab == NULL) {
1885 xmlRngPErrMemory(ctxt, "adding document\n");
1889 ctxt->docTab[ctxt->docNr] = value;
1891 return (ctxt->docNr++);
1895 * xmlRelaxNGDocumentPop:
1896 * @ctxt: the parser context
1898 * Pops the top doc from the doc stack
1900 * Returns the doc just removed
1902 static xmlRelaxNGDocumentPtr
1903 xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1905 xmlRelaxNGDocumentPtr ret;
1907 if (ctxt->docNr <= 0)
1910 if (ctxt->docNr > 0)
1911 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1914 ret = ctxt->docTab[ctxt->docNr];
1915 ctxt->docTab[ctxt->docNr] = NULL;
1920 * xmlRelaxNGLoadExternalRef:
1921 * @ctxt: the parser context
1922 * @URL: the normalized URL
1923 * @ns: the inherited ns if any
1925 * First lookup if the document is already loaded into the parser context,
1926 * check against recursion. If not found the resource is loaded and
1927 * the content is preprocessed before being returned back to the caller.
1929 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1931 static xmlRelaxNGDocumentPtr
1932 xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1933 const xmlChar * URL, const xmlChar * ns)
1935 xmlRelaxNGDocumentPtr ret = NULL;
1941 * check against recursion in the stack
1943 for (i = 0; i < ctxt->docNr; i++) {
1944 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1945 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1946 "Detected an externalRef recursion for %s\n", URL,
1955 doc = xmlReadFile((const char *) URL,NULL,0);
1957 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1958 "xmlRelaxNG: could not load %s\n", URL, NULL);
1963 * Allocate the document structures and register it first.
1965 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1967 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1968 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1972 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1974 ret->href = xmlStrdup(URL);
1975 ret->next = ctxt->documents;
1976 ret->externalRef = 1;
1977 ctxt->documents = ret;
1980 * transmit the ns if needed
1983 root = xmlDocGetRootElement(doc);
1985 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1986 xmlSetProp(root, BAD_CAST "ns", ns);
1992 * push it on the stack and register it in the hash table
1994 xmlRelaxNGDocumentPush(ctxt, ret);
1997 * Some preprocessing of the document content
1999 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
2005 xmlRelaxNGDocumentPop(ctxt);
2010 /************************************************************************
2014 ************************************************************************/
2016 #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2017 #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2018 #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2019 #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2020 #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
2023 xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2027 switch (def->type) {
2028 case XML_RELAXNG_EMPTY:
2030 case XML_RELAXNG_NOT_ALLOWED:
2031 return ("notAllowed");
2032 case XML_RELAXNG_EXCEPT:
2034 case XML_RELAXNG_TEXT:
2036 case XML_RELAXNG_ELEMENT:
2038 case XML_RELAXNG_DATATYPE:
2039 return ("datatype");
2040 case XML_RELAXNG_VALUE:
2042 case XML_RELAXNG_LIST:
2044 case XML_RELAXNG_ATTRIBUTE:
2045 return ("attribute");
2046 case XML_RELAXNG_DEF:
2048 case XML_RELAXNG_REF:
2050 case XML_RELAXNG_EXTERNALREF:
2051 return ("externalRef");
2052 case XML_RELAXNG_PARENTREF:
2053 return ("parentRef");
2054 case XML_RELAXNG_OPTIONAL:
2055 return ("optional");
2056 case XML_RELAXNG_ZEROORMORE:
2057 return ("zeroOrMore");
2058 case XML_RELAXNG_ONEORMORE:
2059 return ("oneOrMore");
2060 case XML_RELAXNG_CHOICE:
2062 case XML_RELAXNG_GROUP:
2064 case XML_RELAXNG_INTERLEAVE:
2065 return ("interleave");
2066 case XML_RELAXNG_START:
2068 case XML_RELAXNG_NOOP:
2070 case XML_RELAXNG_PARAM:
2077 * xmlRelaxNGGetErrorString:
2078 * @err: the error code
2079 * @arg1: the first string argument
2080 * @arg2: the second string argument
2082 * computes a formatted error string for the given error code and args
2084 * Returns the error string, it must be deallocated by the caller
2087 xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2088 const xmlChar * arg2)
2099 case XML_RELAXNG_OK:
2101 case XML_RELAXNG_ERR_MEMORY:
2102 return (xmlCharStrdup("out of memory\n"));
2103 case XML_RELAXNG_ERR_TYPE:
2104 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2106 case XML_RELAXNG_ERR_TYPEVAL:
2107 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2110 case XML_RELAXNG_ERR_DUPID:
2111 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2113 case XML_RELAXNG_ERR_TYPECMP:
2114 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2116 case XML_RELAXNG_ERR_NOSTATE:
2117 return (xmlCharStrdup("Internal error: no state\n"));
2118 case XML_RELAXNG_ERR_NODEFINE:
2119 return (xmlCharStrdup("Internal error: no define\n"));
2120 case XML_RELAXNG_ERR_INTERNAL:
2121 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2123 case XML_RELAXNG_ERR_LISTEXTRA:
2124 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2126 case XML_RELAXNG_ERR_INTERNODATA:
2127 return (xmlCharStrdup
2128 ("Internal: interleave block has no data\n"));
2129 case XML_RELAXNG_ERR_INTERSEQ:
2130 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2131 case XML_RELAXNG_ERR_INTEREXTRA:
2132 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2134 case XML_RELAXNG_ERR_ELEMNAME:
2135 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2138 case XML_RELAXNG_ERR_ELEMNONS:
2139 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2142 case XML_RELAXNG_ERR_ELEMWRONGNS:
2144 "Element %s has wrong namespace: expecting %s\n", arg1,
2147 case XML_RELAXNG_ERR_ELEMWRONG:
2148 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2150 case XML_RELAXNG_ERR_TEXTWRONG:
2152 "Did not expect text in element %s content\n", arg1);
2154 case XML_RELAXNG_ERR_ELEMEXTRANS:
2155 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2158 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2159 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2161 case XML_RELAXNG_ERR_NOELEM:
2162 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2165 case XML_RELAXNG_ERR_NOTELEM:
2166 return (xmlCharStrdup("Expecting an element got text\n"));
2167 case XML_RELAXNG_ERR_ATTRVALID:
2168 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2171 case XML_RELAXNG_ERR_CONTENTVALID:
2172 snprintf(msg, 1000, "Element %s failed to validate content\n",
2175 case XML_RELAXNG_ERR_EXTRACONTENT:
2176 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2179 case XML_RELAXNG_ERR_INVALIDATTR:
2180 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2183 case XML_RELAXNG_ERR_LACKDATA:
2184 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2187 case XML_RELAXNG_ERR_DATAELEM:
2188 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2191 case XML_RELAXNG_ERR_VALELEM:
2192 snprintf(msg, 1000, "Value element %s has child elements\n",
2195 case XML_RELAXNG_ERR_LISTELEM:
2196 snprintf(msg, 1000, "List element %s has child elements\n",
2199 case XML_RELAXNG_ERR_DATATYPE:
2200 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2202 case XML_RELAXNG_ERR_VALUE:
2203 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2205 case XML_RELAXNG_ERR_LIST:
2206 return (xmlCharStrdup("Error validating list\n"));
2207 case XML_RELAXNG_ERR_NOGRAMMAR:
2208 return (xmlCharStrdup("No top grammar defined\n"));
2209 case XML_RELAXNG_ERR_EXTRADATA:
2210 return (xmlCharStrdup("Extra data in the document\n"));
2212 return (xmlCharStrdup("Unknown error !\n"));
2215 snprintf(msg, 1000, "Unknown error code %d\n", err);
2218 return (xmlStrdup((xmlChar *) msg));
2222 * xmlRelaxNGShowValidError:
2223 * @ctxt: the validation context
2224 * @err: the error number
2226 * @child: the node child generating the problem.
2227 * @arg1: the first argument
2228 * @arg2: the second argument
2230 * Show a validation error.
2233 xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2234 xmlRelaxNGValidErr err, xmlNodePtr node,
2235 xmlNodePtr child, const xmlChar * arg1,
2236 const xmlChar * arg2)
2240 if (ctxt->flags & FLAGS_NOERROR)
2244 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
2246 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2250 if (ctxt->errNo == XML_RELAXNG_OK)
2252 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2253 (const char *) msg, arg1, arg2);
2258 * xmlRelaxNGPopErrors:
2259 * @ctxt: the validation context
2260 * @level: the error level in the stack
2262 * pop and discard all errors until the given level is reached
2265 xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2268 xmlRelaxNGValidErrorPtr err;
2271 xmlGenericError(xmlGenericErrorContext,
2272 "Pop errors till level %d\n", level);
2274 for (i = level; i < ctxt->errNr; i++) {
2275 err = &ctxt->errTab[i];
2276 if (err->flags & ERROR_IS_DUP) {
2277 if (err->arg1 != NULL)
2278 xmlFree((xmlChar *) err->arg1);
2280 if (err->arg2 != NULL)
2281 xmlFree((xmlChar *) err->arg2);
2286 ctxt->errNr = level;
2287 if (ctxt->errNr <= 0)
2292 * xmlRelaxNGDumpValidError:
2293 * @ctxt: the validation context
2295 * Show all validation error over a given index.
2298 xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2301 xmlRelaxNGValidErrorPtr err, dup;
2304 xmlGenericError(xmlGenericErrorContext,
2305 "Dumping error stack %d errors\n", ctxt->errNr);
2307 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2308 err = &ctxt->errTab[i];
2309 if (k < MAX_ERROR) {
2310 for (j = 0; j < i; j++) {
2311 dup = &ctxt->errTab[j];
2312 if ((err->err == dup->err) && (err->node == dup->node) &&
2313 (xmlStrEqual(err->arg1, dup->arg1)) &&
2314 (xmlStrEqual(err->arg2, dup->arg2))) {
2318 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2319 err->arg1, err->arg2);
2323 if (err->flags & ERROR_IS_DUP) {
2324 if (err->arg1 != NULL)
2325 xmlFree((xmlChar *) err->arg1);
2327 if (err->arg2 != NULL)
2328 xmlFree((xmlChar *) err->arg2);
2337 * xmlRelaxNGAddValidError:
2338 * @ctxt: the validation context
2339 * @err: the error number
2340 * @arg1: the first argument
2341 * @arg2: the second argument
2342 * @dup: need to dup the args
2344 * Register a validation error, either generating it if it's sure
2345 * or stacking it for later handling if unsure.
2348 xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2349 xmlRelaxNGValidErr err, const xmlChar * arg1,
2350 const xmlChar * arg2, int dup)
2354 if (ctxt->flags & FLAGS_NOERROR)
2358 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
2361 * generate the error directly
2363 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2364 (ctxt->flags & FLAGS_NEGATIVE)) {
2365 xmlNodePtr node, seq;
2368 * Flush first any stacked error which might be the
2369 * real cause of the problem.
2371 if (ctxt->errNr != 0)
2372 xmlRelaxNGDumpValidError(ctxt);
2373 if (ctxt->state != NULL) {
2374 node = ctxt->state->node;
2375 seq = ctxt->state->seq;
2379 if ((node == NULL) && (seq == NULL)) {
2382 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2385 * Stack the error for later processing if needed
2388 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
2393 /************************************************************************
2395 * Type library hooks *
2397 ************************************************************************/
2398 static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2399 const xmlChar * str);
2402 * xmlRelaxNGSchemaTypeHave:
2403 * @data: data needed for the library
2404 * @type: the type name
2406 * Check if the given type is provided by
2407 * the W3C XMLSchema Datatype library.
2409 * Returns 1 if yes, 0 if no and -1 in case of error.
2412 xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2414 xmlSchemaTypePtr typ;
2418 typ = xmlSchemaGetPredefinedType(type,
2420 "http://www.w3.org/2001/XMLSchema");
2427 * xmlRelaxNGSchemaTypeCheck:
2428 * @data: data needed for the library
2429 * @type: the type name
2430 * @value: the value to check
2433 * Check if the given type and value are validated by
2434 * the W3C XMLSchema Datatype library.
2436 * Returns 1 if yes, 0 if no and -1 in case of error.
2439 xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
2440 const xmlChar * type,
2441 const xmlChar * value,
2442 void **result, xmlNodePtr node)
2444 xmlSchemaTypePtr typ;
2447 if ((type == NULL) || (value == NULL))
2449 typ = xmlSchemaGetPredefinedType(type,
2451 "http://www.w3.org/2001/XMLSchema");
2454 ret = xmlSchemaValPredefTypeNode(typ, value,
2455 (xmlSchemaValPtr *) result, node);
2456 if (ret == 2) /* special ID error code */
2466 * xmlRelaxNGSchemaFacetCheck:
2467 * @data: data needed for the library
2468 * @type: the type name
2469 * @facet: the facet name
2470 * @val: the facet value
2471 * @strval: the string value
2472 * @value: the value to check
2474 * Function provided by a type library to check a value facet
2476 * Returns 1 if yes, 0 if no and -1 in case of error.
2479 xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2480 const xmlChar * type, const xmlChar * facetname,
2481 const xmlChar * val, const xmlChar * strval,
2484 xmlSchemaFacetPtr facet;
2485 xmlSchemaTypePtr typ;
2488 if ((type == NULL) || (strval == NULL))
2490 typ = xmlSchemaGetPredefinedType(type,
2492 "http://www.w3.org/2001/XMLSchema");
2496 facet = xmlSchemaNewFacet();
2500 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2501 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2502 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2503 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2504 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2505 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2506 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2507 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2508 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2509 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2510 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2511 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2512 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2513 facet->type = XML_SCHEMA_FACET_PATTERN;
2514 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2515 facet->type = XML_SCHEMA_FACET_ENUMERATION;
2516 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2517 facet->type = XML_SCHEMA_FACET_WHITESPACE;
2518 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2519 facet->type = XML_SCHEMA_FACET_LENGTH;
2520 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2521 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2522 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2523 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2525 xmlSchemaFreeFacet(facet);
2529 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2531 xmlSchemaFreeFacet(facet);
2534 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2535 xmlSchemaFreeFacet(facet);
2542 * xmlRelaxNGSchemaFreeValue:
2543 * @data: data needed for the library
2544 * @value: the value to free
2546 * Function provided by a type library to free a Schemas value
2548 * Returns 1 if yes, 0 if no and -1 in case of error.
2551 xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2553 xmlSchemaFreeValue(value);
2557 * xmlRelaxNGSchemaTypeCompare:
2558 * @data: data needed for the library
2559 * @type: the type name
2560 * @value1: the first value
2561 * @value2: the second value
2563 * Compare two values for equality accordingly a type from the W3C XMLSchema
2566 * Returns 1 if equal, 0 if no and -1 in case of error.
2569 xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
2570 const xmlChar * type,
2571 const xmlChar * value1,
2574 const xmlChar * value2, xmlNodePtr ctxt2)
2577 xmlSchemaTypePtr typ;
2578 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2580 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2582 typ = xmlSchemaGetPredefinedType(type,
2584 "http://www.w3.org/2001/XMLSchema");
2587 if (comp1 == NULL) {
2588 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2594 res1 = (xmlSchemaValPtr) comp1;
2596 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
2598 if (res1 != (xmlSchemaValPtr) comp1)
2599 xmlSchemaFreeValue(res1);
2602 ret = xmlSchemaCompareValues(res1, res2);
2603 if (res1 != (xmlSchemaValPtr) comp1)
2604 xmlSchemaFreeValue(res1);
2605 xmlSchemaFreeValue(res2);
2614 * xmlRelaxNGDefaultTypeHave:
2615 * @data: data needed for the library
2616 * @type: the type name
2618 * Check if the given type is provided by
2619 * the default datatype library.
2621 * Returns 1 if yes, 0 if no and -1 in case of error.
2624 xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2625 const xmlChar * type)
2629 if (xmlStrEqual(type, BAD_CAST "string"))
2631 if (xmlStrEqual(type, BAD_CAST "token"))
2637 * xmlRelaxNGDefaultTypeCheck:
2638 * @data: data needed for the library
2639 * @type: the type name
2640 * @value: the value to check
2643 * Check if the given type and value are validated by
2644 * the default datatype library.
2646 * Returns 1 if yes, 0 if no and -1 in case of error.
2649 xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2650 const xmlChar * type ATTRIBUTE_UNUSED,
2651 const xmlChar * value ATTRIBUTE_UNUSED,
2652 void **result ATTRIBUTE_UNUSED,
2653 xmlNodePtr node ATTRIBUTE_UNUSED)
2657 if (xmlStrEqual(type, BAD_CAST "string"))
2659 if (xmlStrEqual(type, BAD_CAST "token")) {
2667 * xmlRelaxNGDefaultTypeCompare:
2668 * @data: data needed for the library
2669 * @type: the type name
2670 * @value1: the first value
2671 * @value2: the second value
2673 * Compare two values accordingly a type from the default
2676 * Returns 1 if yes, 0 if no and -1 in case of error.
2679 xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
2680 const xmlChar * type,
2681 const xmlChar * value1,
2682 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2683 void *comp1 ATTRIBUTE_UNUSED,
2684 const xmlChar * value2,
2685 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2689 if (xmlStrEqual(type, BAD_CAST "string")) {
2690 ret = xmlStrEqual(value1, value2);
2691 } else if (xmlStrEqual(type, BAD_CAST "token")) {
2692 if (!xmlStrEqual(value1, value2)) {
2693 xmlChar *nval, *nvalue;
2696 * TODO: trivial optimizations are possible by
2697 * computing at compile-time
2699 nval = xmlRelaxNGNormalize(NULL, value1);
2700 nvalue = xmlRelaxNGNormalize(NULL, value2);
2702 if ((nval == NULL) || (nvalue == NULL))
2704 else if (xmlStrEqual(nval, nvalue))
2718 static int xmlRelaxNGTypeInitialized = 0;
2719 static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2722 * xmlRelaxNGFreeTypeLibrary:
2723 * @lib: the type library structure
2724 * @namespace: the URI bound to the library
2726 * Free the structure associated to the type library
2729 xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2730 const xmlChar * namespace ATTRIBUTE_UNUSED)
2734 if (lib->namespace != NULL)
2735 xmlFree((xmlChar *) lib->namespace);
2740 * xmlRelaxNGRegisterTypeLibrary:
2741 * @namespace: the URI bound to the library
2742 * @data: data associated to the library
2743 * @have: the provide function
2744 * @check: the checking function
2745 * @comp: the comparison function
2747 * Register a new type library
2749 * Returns 0 in case of success and -1 in case of error.
2752 xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2753 xmlRelaxNGTypeHave have,
2754 xmlRelaxNGTypeCheck check,
2755 xmlRelaxNGTypeCompare comp,
2756 xmlRelaxNGFacetCheck facet,
2757 xmlRelaxNGTypeFree freef)
2759 xmlRelaxNGTypeLibraryPtr lib;
2762 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2763 (check == NULL) || (comp == NULL))
2765 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2766 xmlGenericError(xmlGenericErrorContext,
2767 "Relax-NG types library '%s' already registered\n",
2772 (xmlRelaxNGTypeLibraryPtr)
2773 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2775 xmlRngVErrMemory(NULL, "adding types library\n");
2778 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2779 lib->namespace = xmlStrdup(namespace);
2786 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2788 xmlGenericError(xmlGenericErrorContext,
2789 "Relax-NG types library failed to register '%s'\n",
2791 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2798 * xmlRelaxNGInitTypes:
2800 * Initilize the default type libraries.
2802 * Returns 0 in case of success and -1 in case of error.
2805 xmlRelaxNGInitTypes(void)
2807 if (xmlRelaxNGTypeInitialized != 0)
2809 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2810 if (xmlRelaxNGRegisteredTypes == NULL) {
2811 xmlGenericError(xmlGenericErrorContext,
2812 "Failed to allocate sh table for Relax-NG types\n");
2815 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2816 "http://www.w3.org/2001/XMLSchema-datatypes",
2817 NULL, xmlRelaxNGSchemaTypeHave,
2818 xmlRelaxNGSchemaTypeCheck,
2819 xmlRelaxNGSchemaTypeCompare,
2820 xmlRelaxNGSchemaFacetCheck,
2821 xmlRelaxNGSchemaFreeValue);
2822 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2823 xmlRelaxNGDefaultTypeHave,
2824 xmlRelaxNGDefaultTypeCheck,
2825 xmlRelaxNGDefaultTypeCompare, NULL,
2827 xmlRelaxNGTypeInitialized = 1;
2832 * xmlRelaxNGCleanupTypes:
2834 * Cleanup the default Schemas type library associated to RelaxNG
2837 xmlRelaxNGCleanupTypes(void)
2839 xmlSchemaCleanupTypes();
2840 if (xmlRelaxNGTypeInitialized == 0)
2842 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2843 xmlRelaxNGFreeTypeLibrary);
2844 xmlRelaxNGTypeInitialized = 0;
2847 /************************************************************************
2849 * Compiling element content into regexp *
2851 * Sometime the element content can be compiled into a pure regexp, *
2852 * This allows a faster execution and streamability at that level *
2854 ************************************************************************/
2856 /* from automata.c but not exported */
2857 void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2860 static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2861 xmlRelaxNGDefinePtr def);
2864 * xmlRelaxNGIsCompileable:
2865 * @define: the definition to check
2867 * Check if a definition is nullable.
2869 * Returns 1 if yes, 0 if no and -1 in case of error
2872 xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2879 if ((def->type != XML_RELAXNG_ELEMENT) &&
2880 (def->dflags & IS_COMPILABLE))
2882 if ((def->type != XML_RELAXNG_ELEMENT) &&
2883 (def->dflags & IS_NOT_COMPILABLE))
2885 switch (def->type) {
2886 case XML_RELAXNG_NOOP:
2887 ret = xmlRelaxNGIsCompileable(def->content);
2889 case XML_RELAXNG_TEXT:
2890 case XML_RELAXNG_EMPTY:
2893 case XML_RELAXNG_ELEMENT:
2895 * Check if the element content is compileable
2897 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2898 ((def->dflags & IS_COMPILABLE) == 0)) {
2899 xmlRelaxNGDefinePtr list;
2901 list = def->content;
2902 while (list != NULL) {
2903 ret = xmlRelaxNGIsCompileable(list);
2909 * Because the routine is recursive, we must guard against
2910 * discovering both COMPILABLE and NOT_COMPILABLE
2913 def->dflags &= ~IS_COMPILABLE;
2914 def->dflags |= IS_NOT_COMPILABLE;
2916 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
2917 def->dflags |= IS_COMPILABLE;
2918 #ifdef DEBUG_COMPILE
2920 xmlGenericError(xmlGenericErrorContext,
2921 "element content for %s is compilable\n",
2923 } else if (ret == 0) {
2924 xmlGenericError(xmlGenericErrorContext,
2925 "element content for %s is not compilable\n",
2928 xmlGenericError(xmlGenericErrorContext,
2929 "Problem in RelaxNGIsCompileable for element %s\n",
2935 * All elements return a compileable status unless they
2936 * are generic like anyName
2938 if ((def->nameClass != NULL) || (def->name == NULL))
2943 case XML_RELAXNG_REF:
2944 case XML_RELAXNG_EXTERNALREF:
2945 case XML_RELAXNG_PARENTREF:
2946 if (def->depth == -20) {
2949 xmlRelaxNGDefinePtr list;
2952 list = def->content;
2953 while (list != NULL) {
2954 ret = xmlRelaxNGIsCompileable(list);
2961 case XML_RELAXNG_START:
2962 case XML_RELAXNG_OPTIONAL:
2963 case XML_RELAXNG_ZEROORMORE:
2964 case XML_RELAXNG_ONEORMORE:
2965 case XML_RELAXNG_CHOICE:
2966 case XML_RELAXNG_GROUP:
2967 case XML_RELAXNG_DEF:{
2968 xmlRelaxNGDefinePtr list;
2970 list = def->content;
2971 while (list != NULL) {
2972 ret = xmlRelaxNGIsCompileable(list);
2979 case XML_RELAXNG_EXCEPT:
2980 case XML_RELAXNG_ATTRIBUTE:
2981 case XML_RELAXNG_INTERLEAVE:
2982 case XML_RELAXNG_DATATYPE:
2983 case XML_RELAXNG_LIST:
2984 case XML_RELAXNG_PARAM:
2985 case XML_RELAXNG_VALUE:
2986 case XML_RELAXNG_NOT_ALLOWED:
2991 def->dflags |= IS_NOT_COMPILABLE;
2993 def->dflags |= IS_COMPILABLE;
2994 #ifdef DEBUG_COMPILE
2996 xmlGenericError(xmlGenericErrorContext,
2997 "RelaxNGIsCompileable %s : true\n",
2998 xmlRelaxNGDefName(def));
2999 } else if (ret == 0) {
3000 xmlGenericError(xmlGenericErrorContext,
3001 "RelaxNGIsCompileable %s : false\n",
3002 xmlRelaxNGDefName(def));
3004 xmlGenericError(xmlGenericErrorContext,
3005 "Problem in RelaxNGIsCompileable %s\n",
3006 xmlRelaxNGDefName(def));
3013 * xmlRelaxNGCompile:
3014 * ctxt: the RelaxNG parser context
3015 * @define: the definition tree to compile
3017 * Compile the set of definitions, it works recursively, till the
3018 * element boundaries, where it tries to compile the content if possible
3020 * Returns 0 if success and -1 in case of error
3023 xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3026 xmlRelaxNGDefinePtr list;
3028 if ((ctxt == NULL) || (def == NULL))
3031 switch (def->type) {
3032 case XML_RELAXNG_START:
3033 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
3034 xmlAutomataPtr oldam = ctxt->am;
3035 xmlAutomataStatePtr oldstate = ctxt->state;
3039 list = def->content;
3040 ctxt->am = xmlNewAutomata();
3041 if (ctxt->am == NULL)
3045 * assume identical strings but not same pointer are different
3046 * atoms, needed for non-determinism detection
3047 * That way if 2 elements with the same name are in a choice
3048 * branch the automata is found non-deterministic and
3049 * we fallback to the normal validation which does the right
3050 * thing of exploring both choices.
3052 xmlAutomataSetFlags(ctxt->am, 1);
3054 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3055 while (list != NULL) {
3056 xmlRelaxNGCompile(ctxt, list);
3059 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3060 if (xmlAutomataIsDeterminist(ctxt->am))
3061 def->contModel = xmlAutomataCompile(ctxt->am);
3063 xmlFreeAutomata(ctxt->am);
3064 ctxt->state = oldstate;
3068 case XML_RELAXNG_ELEMENT:
3069 if ((ctxt->am != NULL) && (def->name != NULL)) {
3070 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3075 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3076 xmlAutomataPtr oldam = ctxt->am;
3077 xmlAutomataStatePtr oldstate = ctxt->state;
3081 list = def->content;
3082 ctxt->am = xmlNewAutomata();
3083 if (ctxt->am == NULL)
3085 xmlAutomataSetFlags(ctxt->am, 1);
3086 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3087 while (list != NULL) {
3088 xmlRelaxNGCompile(ctxt, list);
3091 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3092 def->contModel = xmlAutomataCompile(ctxt->am);
3093 if (!xmlRegexpIsDeterminist(def->contModel)) {
3094 #ifdef DEBUG_COMPILE
3095 xmlGenericError(xmlGenericErrorContext,
3096 "Content model not determinist %s\n",
3100 * we can only use the automata if it is determinist
3102 xmlRegFreeRegexp(def->contModel);
3103 def->contModel = NULL;
3105 xmlFreeAutomata(ctxt->am);
3106 ctxt->state = oldstate;
3109 xmlAutomataPtr oldam = ctxt->am;
3112 * we can't build the content model for this element content
3113 * but it still might be possible to build it for some of its
3114 * children, recurse.
3116 ret = xmlRelaxNGTryCompile(ctxt, def);
3120 case XML_RELAXNG_NOOP:
3121 ret = xmlRelaxNGCompile(ctxt, def->content);
3123 case XML_RELAXNG_OPTIONAL:{
3124 xmlAutomataStatePtr oldstate = ctxt->state;
3126 list = def->content;
3127 while (list != NULL) {
3128 xmlRelaxNGCompile(ctxt, list);
3131 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3134 case XML_RELAXNG_ZEROORMORE:{
3135 xmlAutomataStatePtr oldstate;
3138 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3139 oldstate = ctxt->state;
3140 list = def->content;
3141 while (list != NULL) {
3142 xmlRelaxNGCompile(ctxt, list);
3145 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3147 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3150 case XML_RELAXNG_ONEORMORE:{
3151 xmlAutomataStatePtr oldstate;
3153 list = def->content;
3154 while (list != NULL) {
3155 xmlRelaxNGCompile(ctxt, list);
3158 oldstate = ctxt->state;
3159 list = def->content;
3160 while (list != NULL) {
3161 xmlRelaxNGCompile(ctxt, list);
3164 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3166 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3169 case XML_RELAXNG_CHOICE:{
3170 xmlAutomataStatePtr target = NULL;
3171 xmlAutomataStatePtr oldstate = ctxt->state;
3173 list = def->content;
3174 while (list != NULL) {
3175 ctxt->state = oldstate;
3176 ret = xmlRelaxNGCompile(ctxt, list);
3180 target = ctxt->state;
3182 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3187 ctxt->state = target;
3191 case XML_RELAXNG_REF:
3192 case XML_RELAXNG_EXTERNALREF:
3193 case XML_RELAXNG_PARENTREF:
3194 case XML_RELAXNG_GROUP:
3195 case XML_RELAXNG_DEF:
3196 list = def->content;
3197 while (list != NULL) {
3198 ret = xmlRelaxNGCompile(ctxt, list);
3204 case XML_RELAXNG_TEXT:{
3205 xmlAutomataStatePtr oldstate;
3208 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3209 oldstate = ctxt->state;
3210 xmlRelaxNGCompile(ctxt, def->content);
3211 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3212 ctxt->state, BAD_CAST "#text",
3215 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3218 case XML_RELAXNG_EMPTY:
3220 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3222 case XML_RELAXNG_EXCEPT:
3223 case XML_RELAXNG_ATTRIBUTE:
3224 case XML_RELAXNG_INTERLEAVE:
3225 case XML_RELAXNG_NOT_ALLOWED:
3226 case XML_RELAXNG_DATATYPE:
3227 case XML_RELAXNG_LIST:
3228 case XML_RELAXNG_PARAM:
3229 case XML_RELAXNG_VALUE:
3230 /* This should not happen and generate an internal error */
3231 fprintf(stderr, "RNG internal error trying to compile %s\n",
3232 xmlRelaxNGDefName(def));
3239 * xmlRelaxNGTryCompile:
3240 * ctxt: the RelaxNG parser context
3241 * @define: the definition tree to compile
3243 * Try to compile the set of definitions, it works recursively,
3244 * possibly ignoring parts which cannot be compiled.
3246 * Returns 0 if success and -1 in case of error
3249 xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3252 xmlRelaxNGDefinePtr list;
3254 if ((ctxt == NULL) || (def == NULL))
3257 if ((def->type == XML_RELAXNG_START) ||
3258 (def->type == XML_RELAXNG_ELEMENT)) {
3259 ret = xmlRelaxNGIsCompileable(def);
3260 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3262 ret = xmlRelaxNGCompile(ctxt, def);
3263 #ifdef DEBUG_PROGRESSIVE
3265 if (def->type == XML_RELAXNG_START)
3266 xmlGenericError(xmlGenericErrorContext,
3267 "compiled the start\n");
3269 xmlGenericError(xmlGenericErrorContext,
3270 "compiled element %s\n", def->name);
3272 if (def->type == XML_RELAXNG_START)
3273 xmlGenericError(xmlGenericErrorContext,
3274 "failed to compile the start\n");
3276 xmlGenericError(xmlGenericErrorContext,
3277 "failed to compile element %s\n",
3284 switch (def->type) {
3285 case XML_RELAXNG_NOOP:
3286 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3288 case XML_RELAXNG_TEXT:
3289 case XML_RELAXNG_DATATYPE:
3290 case XML_RELAXNG_LIST:
3291 case XML_RELAXNG_PARAM:
3292 case XML_RELAXNG_VALUE:
3293 case XML_RELAXNG_EMPTY:
3294 case XML_RELAXNG_ELEMENT:
3297 case XML_RELAXNG_OPTIONAL:
3298 case XML_RELAXNG_ZEROORMORE:
3299 case XML_RELAXNG_ONEORMORE:
3300 case XML_RELAXNG_CHOICE:
3301 case XML_RELAXNG_GROUP:
3302 case XML_RELAXNG_DEF:
3303 case XML_RELAXNG_START:
3304 case XML_RELAXNG_REF:
3305 case XML_RELAXNG_EXTERNALREF:
3306 case XML_RELAXNG_PARENTREF:
3307 list = def->content;
3308 while (list != NULL) {
3309 ret = xmlRelaxNGTryCompile(ctxt, list);
3315 case XML_RELAXNG_EXCEPT:
3316 case XML_RELAXNG_ATTRIBUTE:
3317 case XML_RELAXNG_INTERLEAVE:
3318 case XML_RELAXNG_NOT_ALLOWED:
3325 /************************************************************************
3327 * Parsing functions *
3329 ************************************************************************/
3331 static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3332 ctxt, xmlNodePtr node);
3333 static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3334 ctxt, xmlNodePtr node);
3335 static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3336 ctxt, xmlNodePtr nodes,
3338 static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3339 ctxt, xmlNodePtr node);
3340 static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3342 static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3344 static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3345 ctxt, xmlNodePtr node,
3348 static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3349 ctxt, xmlNodePtr nodes);
3350 static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3351 xmlRelaxNGDefinePtr define,
3355 #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
3358 * xmlRelaxNGIsNullable:
3359 * @define: the definition to verify
3361 * Check if a definition is nullable.
3363 * Returns 1 if yes, 0 if no and -1 in case of error
3366 xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3373 if (define->dflags & IS_NULLABLE)
3375 if (define->dflags & IS_NOT_NULLABLE)
3377 switch (define->type) {
3378 case XML_RELAXNG_EMPTY:
3379 case XML_RELAXNG_TEXT:
3382 case XML_RELAXNG_NOOP:
3383 case XML_RELAXNG_DEF:
3384 case XML_RELAXNG_REF:
3385 case XML_RELAXNG_EXTERNALREF:
3386 case XML_RELAXNG_PARENTREF:
3387 case XML_RELAXNG_ONEORMORE:
3388 ret = xmlRelaxNGIsNullable(define->content);
3390 case XML_RELAXNG_EXCEPT:
3391 case XML_RELAXNG_NOT_ALLOWED:
3392 case XML_RELAXNG_ELEMENT:
3393 case XML_RELAXNG_DATATYPE:
3394 case XML_RELAXNG_PARAM:
3395 case XML_RELAXNG_VALUE:
3396 case XML_RELAXNG_LIST:
3397 case XML_RELAXNG_ATTRIBUTE:
3400 case XML_RELAXNG_CHOICE:{
3401 xmlRelaxNGDefinePtr list = define->content;
3403 while (list != NULL) {
3404 ret = xmlRelaxNGIsNullable(list);
3412 case XML_RELAXNG_START:
3413 case XML_RELAXNG_INTERLEAVE:
3414 case XML_RELAXNG_GROUP:{
3415 xmlRelaxNGDefinePtr list = define->content;
3417 while (list != NULL) {
3418 ret = xmlRelaxNGIsNullable(list);
3430 define->dflags |= IS_NOT_NULLABLE;
3432 define->dflags |= IS_NULLABLE;
3437 * xmlRelaxNGIsBlank:
3440 * Check if a string is ignorable c.f. 4.2. Whitespace
3442 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3445 xmlRelaxNGIsBlank(xmlChar * str)
3450 if (!(IS_BLANK_CH(*str)))
3458 * xmlRelaxNGGetDataTypeLibrary:
3459 * @ctxt: a Relax-NG parser context
3460 * @node: the current data or value element
3462 * Applies algorithm from 4.3. datatypeLibrary attribute
3464 * Returns the datatypeLibary value or NULL if not found
3467 xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3470 xmlChar *ret, *escape;
3475 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
3476 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3482 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3483 if (escape == NULL) {
3490 node = node->parent;
3491 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
3492 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3498 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3499 if (escape == NULL) {
3505 node = node->parent;
3511 * xmlRelaxNGParseValue:
3512 * @ctxt: a Relax-NG parser context
3513 * @node: the data node.
3515 * parse the content of a RelaxNG value node.
3517 * Returns the definition pointer or NULL in case of error
3519 static xmlRelaxNGDefinePtr
3520 xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3522 xmlRelaxNGDefinePtr def = NULL;
3523 xmlRelaxNGTypeLibraryPtr lib = NULL;
3528 def = xmlRelaxNGNewDefine(ctxt, node);
3531 def->type = XML_RELAXNG_VALUE;
3533 type = xmlGetProp(node, BAD_CAST "type");
3535 xmlRelaxNGNormExtSpace(type);
3536 if (xmlValidateNCName(type, 0)) {
3537 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3538 "value type '%s' is not an NCName\n", type, NULL);
3540 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3541 if (library == NULL)
3543 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3548 lib = (xmlRelaxNGTypeLibraryPtr)
3549 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3551 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3552 "Use of unregistered type library '%s'\n", library,
3557 if (lib->have == NULL) {
3558 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3559 "Internal error with type library '%s': no 'have'\n",
3562 success = lib->have(lib->data, def->name);
3564 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3565 "Error type '%s' is not exported by type library '%s'\n",
3566 def->name, library);
3571 if (node->children == NULL) {
3572 def->value = xmlStrdup(BAD_CAST "");
3573 } else if (((node->children->type != XML_TEXT_NODE) &&
3574 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3575 (node->children->next != NULL)) {
3576 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3577 "Expecting a single text value for <value>content\n",
3579 } else if (def != NULL) {
3580 def->value = xmlNodeGetContent(node);
3581 if (def->value == NULL) {
3582 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3583 "Element <value> has no content\n", NULL, NULL);
3584 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3588 lib->check(lib->data, def->name, def->value, &val, node);
3590 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3591 "Value '%s' is not acceptable for type '%s'\n",
3592 def->value, def->name);
3603 * xmlRelaxNGParseData:
3604 * @ctxt: a Relax-NG parser context
3605 * @node: the data node.
3607 * parse the content of a RelaxNG data node.
3609 * Returns the definition pointer or NULL in case of error
3611 static xmlRelaxNGDefinePtr
3612 xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3614 xmlRelaxNGDefinePtr def = NULL, except;
3615 xmlRelaxNGDefinePtr param, lastparam = NULL;
3616 xmlRelaxNGTypeLibraryPtr lib;
3622 type = xmlGetProp(node, BAD_CAST "type");
3624 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3628 xmlRelaxNGNormExtSpace(type);
3629 if (xmlValidateNCName(type, 0)) {
3630 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3631 "data type '%s' is not an NCName\n", type, NULL);
3633 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3634 if (library == NULL)
3636 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3638 def = xmlRelaxNGNewDefine(ctxt, node);
3643 def->type = XML_RELAXNG_DATATYPE;
3647 lib = (xmlRelaxNGTypeLibraryPtr)
3648 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3650 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3651 "Use of unregistered type library '%s'\n", library,
3656 if (lib->have == NULL) {
3657 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3658 "Internal error with type library '%s': no 'have'\n",
3661 tmp = lib->have(lib->data, def->name);
3663 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3664 "Error type '%s' is not exported by type library '%s'\n",
3665 def->name, library);
3670 "http://www.w3.org/2001/XMLSchema-datatypes"))
3671 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3672 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3677 content = node->children;
3680 * Handle optional params
3682 while (content != NULL) {
3683 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3685 if (xmlStrEqual(library,
3686 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3687 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3688 "Type library '%s' does not allow type parameters\n",
3690 content = content->next;
3691 while ((content != NULL) &&
3692 (xmlStrEqual(content->name, BAD_CAST "param")))
3693 content = content->next;
3695 param = xmlRelaxNGNewDefine(ctxt, node);
3696 if (param != NULL) {
3697 param->type = XML_RELAXNG_PARAM;
3698 param->name = xmlGetProp(content, BAD_CAST "name");
3699 if (param->name == NULL) {
3700 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3701 "param has no name\n", NULL, NULL);
3703 param->value = xmlNodeGetContent(content);
3704 if (lastparam == NULL) {
3705 def->attrs = lastparam = param;
3707 lastparam->next = param;
3713 content = content->next;
3717 * Handle optional except
3719 if ((content != NULL)
3720 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3722 xmlRelaxNGDefinePtr tmp2, last = NULL;
3724 except = xmlRelaxNGNewDefine(ctxt, node);
3725 if (except == NULL) {
3728 except->type = XML_RELAXNG_EXCEPT;
3729 child = content->children;
3730 def->content = except;
3731 if (child == NULL) {
3732 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3733 "except has no content\n", NULL, NULL);
3735 while (child != NULL) {
3736 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3739 except->content = last = tmp2;
3745 child = child->next;
3747 content = content->next;
3750 * Check there is no unhandled data
3752 if (content != NULL) {
3753 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3754 "Element data has unexpected content %s\n",
3755 content->name, NULL);
3761 static const xmlChar *invalidName = BAD_CAST "\1";
3764 * xmlRelaxNGCompareNameClasses:
3765 * @defs1: the first element/attribute defs
3766 * @defs2: the second element/attribute defs
3767 * @name: the restriction on the name
3768 * @ns: the restriction on the namespace
3770 * Compare the 2 lists of element definitions. The comparison is
3771 * that if both lists do not accept the same QNames, it returns 1
3772 * If the 2 lists can accept the same QName the comparison returns 0
3774 * Returns 1 disttinct, 0 if equal
3777 xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
3778 xmlRelaxNGDefinePtr def2)
3783 xmlRelaxNGValidCtxt ctxt;
3785 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3787 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3789 if ((def1->type == XML_RELAXNG_ELEMENT) ||
3790 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3791 if (def2->type == XML_RELAXNG_TEXT)
3793 if (def1->name != NULL) {
3794 node.name = def1->name;
3796 node.name = invalidName;
3798 if (def1->ns != NULL) {
3799 if (def1->ns[0] == 0) {
3808 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
3809 if (def1->nameClass != NULL) {
3810 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3817 } else if (def1->type == XML_RELAXNG_TEXT) {
3818 if (def2->type == XML_RELAXNG_TEXT)
3821 } else if (def1->type == XML_RELAXNG_EXCEPT) {
3822 ret = xmlRelaxNGCompareNameClasses(def1->content, def2);
3832 if ((def2->type == XML_RELAXNG_ELEMENT) ||
3833 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3834 if (def2->name != NULL) {
3835 node.name = def2->name;
3837 node.name = invalidName;
3840 if (def2->ns != NULL) {
3841 if (def2->ns[0] == 0) {
3847 ns.href = invalidName;
3849 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
3850 if (def2->nameClass != NULL) {
3851 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3866 * xmlRelaxNGCompareElemDefLists:
3867 * @ctxt: a Relax-NG parser context
3868 * @defs1: the first list of element/attribute defs
3869 * @defs2: the second list of element/attribute defs
3871 * Compare the 2 lists of element or attribute definitions. The comparison
3872 * is that if both lists do not accept the same QNames, it returns 1
3873 * If the 2 lists can accept the same QName the comparison returns 0
3875 * Returns 1 disttinct, 0 if equal
3878 xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3879 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3880 xmlRelaxNGDefinePtr * def2)
3882 xmlRelaxNGDefinePtr *basedef2 = def2;
3884 if ((def1 == NULL) || (def2 == NULL))
3886 if ((*def1 == NULL) || (*def2 == NULL))
3888 while (*def1 != NULL) {
3889 while ((*def2) != NULL) {
3890 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3901 * xmlRelaxNGGenerateAttributes:
3902 * @ctxt: a Relax-NG parser context
3903 * @def: the definition definition
3905 * Check if the definition can only generate attributes
3907 * Returns 1 if yes, 0 if no and -1 in case of error.
3910 xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
3911 xmlRelaxNGDefinePtr def)
3913 xmlRelaxNGDefinePtr parent, cur, tmp;
3916 * Don't run that check in case of error. Infinite recursion
3919 if (ctxt->nbErrors != 0)
3924 while (cur != NULL) {
3925 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3926 (cur->type == XML_RELAXNG_TEXT) ||
3927 (cur->type == XML_RELAXNG_DATATYPE) ||
3928 (cur->type == XML_RELAXNG_PARAM) ||
3929 (cur->type == XML_RELAXNG_LIST) ||
3930 (cur->type == XML_RELAXNG_VALUE) ||
3931 (cur->type == XML_RELAXNG_EMPTY))
3933 if ((cur->type == XML_RELAXNG_CHOICE) ||
3934 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3935 (cur->type == XML_RELAXNG_GROUP) ||
3936 (cur->type == XML_RELAXNG_ONEORMORE) ||
3937 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3938 (cur->type == XML_RELAXNG_OPTIONAL) ||
3939 (cur->type == XML_RELAXNG_PARENTREF) ||
3940 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3941 (cur->type == XML_RELAXNG_REF) ||
3942 (cur->type == XML_RELAXNG_DEF)) {
3943 if (cur->content != NULL) {
3947 while (tmp != NULL) {
3948 tmp->parent = parent;
3956 if (cur->next != NULL) {
3966 if (cur->next != NULL) {
3970 } while (cur != NULL);
3976 * xmlRelaxNGGetElements:
3977 * @ctxt: a Relax-NG parser context
3978 * @def: the definition definition
3979 * @eora: gather elements (0) or attributes (1)
3981 * Compute the list of top elements a definition can generate
3983 * Returns a list of elements or NULL if none was found.
3985 static xmlRelaxNGDefinePtr *
3986 xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
3987 xmlRelaxNGDefinePtr def, int eora)
3989 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
3994 * Don't run that check in case of error. Infinite recursion
3997 if (ctxt->nbErrors != 0)
4002 while (cur != NULL) {
4003 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
4004 (cur->type == XML_RELAXNG_TEXT))) ||
4005 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
4008 ret = (xmlRelaxNGDefinePtr *)
4009 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
4011 xmlRngPErrMemory(ctxt, "getting element list\n");
4014 } else if (max <= len) {
4015 xmlRelaxNGDefinePtr *temp;
4018 temp = xmlRealloc(ret,
4019 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
4021 xmlRngPErrMemory(ctxt, "getting element list\n");
4029 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4030 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4031 (cur->type == XML_RELAXNG_GROUP) ||
4032 (cur->type == XML_RELAXNG_ONEORMORE) ||
4033 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4034 (cur->type == XML_RELAXNG_OPTIONAL) ||
4035 (cur->type == XML_RELAXNG_PARENTREF) ||
4036 (cur->type == XML_RELAXNG_REF) ||
4037 (cur->type == XML_RELAXNG_DEF) ||
4038 (cur->type == XML_RELAXNG_EXTERNALREF)) {
4040 * Don't go within elements or attributes or string values.
4041 * Just gather the element top list
4043 if (cur->content != NULL) {
4047 while (tmp != NULL) {
4048 tmp->parent = parent;
4056 if (cur->next != NULL) {
4066 if (cur->next != NULL) {
4070 } while (cur != NULL);
4076 * xmlRelaxNGCheckChoiceDeterminism:
4077 * @ctxt: a Relax-NG parser context
4078 * @def: the choice definition
4080 * Also used to find indeterministic pattern in choice
4083 xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
4084 xmlRelaxNGDefinePtr def)
4086 xmlRelaxNGDefinePtr **list;
4087 xmlRelaxNGDefinePtr cur;
4088 int nbchild = 0, i, j, ret;
4089 int is_nullable = 0;
4090 int is_indeterminist = 0;
4091 xmlHashTablePtr triage = NULL;
4094 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4097 if (def->dflags & IS_PROCESSED)
4101 * Don't run that check in case of error. Infinite recursion
4104 if (ctxt->nbErrors != 0)
4107 is_nullable = xmlRelaxNGIsNullable(def);
4110 while (cur != NULL) {
4115 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4116 sizeof(xmlRelaxNGDefinePtr
4119 xmlRngPErrMemory(ctxt, "building choice\n");
4124 * a bit strong but safe
4126 if (is_nullable == 0) {
4127 triage = xmlHashCreate(10);
4132 while (cur != NULL) {
4133 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4134 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4136 } else if (is_triable == 1) {
4137 xmlRelaxNGDefinePtr *tmp;
4141 while ((*tmp != NULL) && (is_triable == 1)) {
4142 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4143 res = xmlHashAddEntry2(triage,
4144 BAD_CAST "#text", NULL,
4148 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4149 ((*tmp)->name != NULL)) {
4150 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4151 res = xmlHashAddEntry2(triage,
4155 res = xmlHashAddEntry2(triage,
4156 (*tmp)->name, (*tmp)->ns,
4160 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4161 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4162 res = xmlHashAddEntry2(triage,
4163 BAD_CAST "#any", NULL,
4166 res = xmlHashAddEntry2(triage,
4167 BAD_CAST "#any", (*tmp)->ns,
4181 for (i = 0; i < nbchild; i++) {
4182 if (list[i] == NULL)
4184 for (j = 0; j < i; j++) {
4185 if (list[j] == NULL)
4187 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4189 is_indeterminist = 1;
4193 for (i = 0; i < nbchild; i++) {
4194 if (list[i] != NULL)
4199 if (is_indeterminist) {
4200 def->dflags |= IS_INDETERMINIST;
4202 if (is_triable == 1) {
4203 def->dflags |= IS_TRIABLE;
4205 } else if (triage != NULL) {
4206 xmlHashFree(triage, NULL);
4208 def->dflags |= IS_PROCESSED;
4212 * xmlRelaxNGCheckGroupAttrs:
4213 * @ctxt: a Relax-NG parser context
4214 * @def: the group definition
4216 * Detects violations of rule 7.3
4219 xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
4220 xmlRelaxNGDefinePtr def)
4222 xmlRelaxNGDefinePtr **list;
4223 xmlRelaxNGDefinePtr cur;
4224 int nbchild = 0, i, j, ret;
4226 if ((def == NULL) ||
4227 ((def->type != XML_RELAXNG_GROUP) &&
4228 (def->type != XML_RELAXNG_ELEMENT)))
4231 if (def->dflags & IS_PROCESSED)
4235 * Don't run that check in case of error. Infinite recursion
4238 if (ctxt->nbErrors != 0)
4242 while (cur != NULL) {
4247 while (cur != NULL) {
4252 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4253 sizeof(xmlRelaxNGDefinePtr
4256 xmlRngPErrMemory(ctxt, "building group\n");
4261 while (cur != NULL) {
4262 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4267 while (cur != NULL) {
4268 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4273 for (i = 0; i < nbchild; i++) {
4274 if (list[i] == NULL)
4276 for (j = 0; j < i; j++) {
4277 if (list[j] == NULL)
4279 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4281 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4282 "Attributes conflicts in group\n", NULL, NULL);
4286 for (i = 0; i < nbchild; i++) {
4287 if (list[i] != NULL)
4292 def->dflags |= IS_PROCESSED;
4296 * xmlRelaxNGComputeInterleaves:
4297 * @def: the interleave definition
4298 * @ctxt: a Relax-NG parser context
4299 * @name: the definition name
4301 * A lot of work for preprocessing interleave definitions
4302 * is potentially needed to get a decent execution speed at runtime
4303 * - trying to get a total order on the element nodes generated
4304 * by the interleaves, order the list of interleave definitions
4305 * following that order.
4306 * - if <text/> is used to handle mixed content, it is better to
4307 * flag this in the define and simplify the runtime checking
4311 xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
4312 xmlRelaxNGParserCtxtPtr ctxt,
4313 xmlChar * name ATTRIBUTE_UNUSED)
4315 xmlRelaxNGDefinePtr cur, *tmp;
4317 xmlRelaxNGPartitionPtr partitions = NULL;
4318 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4319 xmlRelaxNGInterleaveGroupPtr group;
4324 int is_determinist = 1;
4327 * Don't run that check in case of error. Infinite recursion
4330 if (ctxt->nbErrors != 0)
4333 #ifdef DEBUG_INTERLEAVE
4334 xmlGenericError(xmlGenericErrorContext,
4335 "xmlRelaxNGComputeInterleaves(%s)\n", name);
4338 while (cur != NULL) {
4343 #ifdef DEBUG_INTERLEAVE
4344 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4346 groups = (xmlRelaxNGInterleaveGroupPtr *)
4347 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
4351 while (cur != NULL) {
4352 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4353 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4354 if (groups[nbgroups] == NULL)
4356 if (cur->type == XML_RELAXNG_TEXT)
4358 groups[nbgroups]->rule = cur;
4359 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4360 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4364 #ifdef DEBUG_INTERLEAVE
4365 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4369 * Let's check that all rules makes a partitions according to 7.4
4371 partitions = (xmlRelaxNGPartitionPtr)
4372 xmlMalloc(sizeof(xmlRelaxNGPartition));
4373 if (partitions == NULL)
4375 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
4376 partitions->nbgroups = nbgroups;
4377 partitions->triage = xmlHashCreate(nbgroups);
4378 for (i = 0; i < nbgroups; i++) {
4380 for (j = i + 1; j < nbgroups; j++) {
4381 if (groups[j] == NULL)
4384 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4387 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4388 "Element or text conflicts in interleave\n",
4391 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4394 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4395 "Attributes conflicts in interleave\n", NULL,
4400 if ((tmp != NULL) && (*tmp != NULL)) {
4401 while (*tmp != NULL) {
4402 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4403 res = xmlHashAddEntry2(partitions->triage,
4404 BAD_CAST "#text", NULL,
4405 (void *) (long) (i + 1));
4407 is_determinist = -1;
4408 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4409 ((*tmp)->name != NULL)) {
4410 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4411 res = xmlHashAddEntry2(partitions->triage,
4413 (void *) (long) (i + 1));
4415 res = xmlHashAddEntry2(partitions->triage,
4416 (*tmp)->name, (*tmp)->ns,
4417 (void *) (long) (i + 1));
4419 is_determinist = -1;
4420 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4421 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4422 res = xmlHashAddEntry2(partitions->triage,
4423 BAD_CAST "#any", NULL,
4424 (void *) (long) (i + 1));
4426 res = xmlHashAddEntry2(partitions->triage,
4427 BAD_CAST "#any", (*tmp)->ns,
4428 (void *) (long) (i + 1));
4429 if ((*tmp)->nameClass != NULL)
4432 is_determinist = -1;
4434 is_determinist = -1;
4442 partitions->groups = groups;
4445 * and save the partition list back in the def
4447 def->data = partitions;
4449 def->dflags |= IS_MIXED;
4450 if (is_determinist == 1)
4451 partitions->flags = IS_DETERMINIST;
4452 if (is_determinist == 2)
4453 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
4457 xmlRngPErrMemory(ctxt, "in interleave computation\n");
4458 if (groups != NULL) {
4459 for (i = 0; i < nbgroups; i++)
4460 if (groups[i] != NULL) {
4461 if (groups[i]->defs != NULL)
4462 xmlFree(groups[i]->defs);
4467 xmlRelaxNGFreePartition(partitions);
4471 * xmlRelaxNGParseInterleave:
4472 * @ctxt: a Relax-NG parser context
4473 * @node: the data node.
4475 * parse the content of a RelaxNG interleave node.
4477 * Returns the definition pointer or NULL in case of error
4479 static xmlRelaxNGDefinePtr
4480 xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4482 xmlRelaxNGDefinePtr def = NULL;
4483 xmlRelaxNGDefinePtr last = NULL, cur;
4486 def = xmlRelaxNGNewDefine(ctxt, node);
4490 def->type = XML_RELAXNG_INTERLEAVE;
4492 if (ctxt->interleaves == NULL)
4493 ctxt->interleaves = xmlHashCreate(10);
4494 if (ctxt->interleaves == NULL) {
4495 xmlRngPErrMemory(ctxt, "create interleaves\n");
4499 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4500 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4501 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4502 "Failed to add %s to hash table\n",
4503 (const xmlChar *) name, NULL);
4506 child = node->children;
4507 if (child == NULL) {
4508 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4509 "Element interleave is empty\n", NULL, NULL);
4511 while (child != NULL) {
4512 if (IS_RELAXNG(child, "element")) {
4513 cur = xmlRelaxNGParseElement(ctxt, child);
4515 cur = xmlRelaxNGParsePattern(ctxt, child);
4520 def->content = last = cur;
4526 child = child->next;
4533 * xmlRelaxNGParseInclude:
4534 * @ctxt: a Relax-NG parser context
4535 * @node: the include node
4537 * Integrate the content of an include node in the current grammar
4539 * Returns 0 in case of success or -1 in case of error
4542 xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4544 xmlRelaxNGIncludePtr incl;
4550 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4551 "Include node has no data\n", NULL, NULL);
4554 root = xmlDocGetRootElement(incl->doc);
4556 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4560 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
4561 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4562 "Include document root is not a grammar\n", NULL, NULL);
4567 * Merge the definition from both the include and the internal list
4569 if (root->children != NULL) {
4570 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4574 if (node->children != NULL) {
4575 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4583 * xmlRelaxNGParseDefine:
4584 * @ctxt: a Relax-NG parser context
4585 * @node: the define node
4587 * parse the content of a RelaxNG define element node.
4589 * Returns 0 in case of success or -1 in case of error
4592 xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4596 xmlRelaxNGDefinePtr def;
4597 const xmlChar *olddefine;
4599 name = xmlGetProp(node, BAD_CAST "name");
4601 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4602 "define has no name\n", NULL, NULL);
4604 xmlRelaxNGNormExtSpace(name);
4605 if (xmlValidateNCName(name, 0)) {
4606 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4607 "define name '%s' is not an NCName\n", name, NULL);
4609 def = xmlRelaxNGNewDefine(ctxt, node);
4614 def->type = XML_RELAXNG_DEF;
4616 if (node->children == NULL) {
4617 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4618 "define has no children\n", NULL, NULL);
4620 olddefine = ctxt->define;
4621 ctxt->define = name;
4623 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4624 ctxt->define = olddefine;
4626 if (ctxt->grammar->defs == NULL)
4627 ctxt->grammar->defs = xmlHashCreate(10);
4628 if (ctxt->grammar->defs == NULL) {
4629 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4630 "Could not create definition hash\n", NULL, NULL);
4633 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4635 xmlRelaxNGDefinePtr prev;
4637 prev = xmlHashLookup(ctxt->grammar->defs, name);
4639 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4640 "Internal error on define aggregation of %s\n",
4644 while (prev->nextHash != NULL)
4645 prev = prev->nextHash;
4646 prev->nextHash = def;
4655 * xmlRelaxNGParseImportRef:
4656 * @payload: the parser context
4657 * @data: the current grammar
4658 * @name: the reference name
4660 * Import import one references into the current grammar
4663 xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4664 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4665 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4668 def->dflags |= IS_EXTERNAL_REF;
4670 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4672 xmlRelaxNGDefinePtr prev;
4674 prev = (xmlRelaxNGDefinePtr)
4675 xmlHashLookup(ctxt->grammar->refs, def->name);
4677 if (def->name != NULL) {
4678 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4679 "Error refs definitions '%s'\n",
4682 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4683 "Error refs definitions\n",
4687 def->nextHash = prev->nextHash;
4688 prev->nextHash = def;
4694 * xmlRelaxNGParseImportRefs:
4695 * @ctxt: the parser context
4696 * @grammar: the sub grammar
4698 * Import references from the subgrammar into the current grammar
4700 * Returns 0 in case of success, -1 in case of failure
4703 xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4704 xmlRelaxNGGrammarPtr grammar) {
4705 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4707 if (grammar->refs == NULL)
4709 if (ctxt->grammar->refs == NULL)
4710 ctxt->grammar->refs = xmlHashCreate(10);
4711 if (ctxt->grammar->refs == NULL) {
4712 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4713 "Could not create references hash\n", NULL, NULL);
4716 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
4721 * xmlRelaxNGProcessExternalRef:
4722 * @ctxt: the parser context
4723 * @node: the externlRef node
4725 * Process and compile an externlRef node
4727 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4729 static xmlRelaxNGDefinePtr
4730 xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4732 xmlRelaxNGDocumentPtr docu;
4733 xmlNodePtr root, tmp;
4735 int newNs = 0, oldflags;
4736 xmlRelaxNGDefinePtr def;
4740 def = xmlRelaxNGNewDefine(ctxt, node);
4743 def->type = XML_RELAXNG_EXTERNALREF;
4745 if (docu->content == NULL) {
4747 * Then do the parsing for good
4749 root = xmlDocGetRootElement(docu->doc);
4751 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4752 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4757 * ns transmission rules
4759 ns = xmlGetProp(root, BAD_CAST "ns");
4762 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4763 ns = xmlGetProp(tmp, BAD_CAST "ns");
4770 xmlSetProp(root, BAD_CAST "ns", ns);
4779 * Parsing to get a precompiled schemas.
4781 oldflags = ctxt->flags;
4782 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4783 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4784 ctxt->flags = oldflags;
4785 if ((docu->schema != NULL) &&
4786 (docu->schema->topgrammar != NULL)) {
4787 docu->content = docu->schema->topgrammar->start;
4788 if (docu->schema->topgrammar->refs)
4789 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
4793 * the externalRef may be reused in a different ns context
4796 xmlUnsetProp(root, BAD_CAST "ns");
4799 def->content = docu->content;
4807 * xmlRelaxNGParsePattern:
4808 * @ctxt: a Relax-NG parser context
4809 * @node: the pattern node.
4811 * parse the content of a RelaxNG pattern node.
4813 * Returns the definition pointer or NULL in case of error or if no
4814 * pattern is generated.
4816 static xmlRelaxNGDefinePtr
4817 xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4819 xmlRelaxNGDefinePtr def = NULL;
4824 if (IS_RELAXNG(node, "element")) {
4825 def = xmlRelaxNGParseElement(ctxt, node);
4826 } else if (IS_RELAXNG(node, "attribute")) {
4827 def = xmlRelaxNGParseAttribute(ctxt, node);
4828 } else if (IS_RELAXNG(node, "empty")) {
4829 def = xmlRelaxNGNewDefine(ctxt, node);
4832 def->type = XML_RELAXNG_EMPTY;
4833 if (node->children != NULL) {
4834 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4835 "empty: had a child node\n", NULL, NULL);
4837 } else if (IS_RELAXNG(node, "text")) {
4838 def = xmlRelaxNGNewDefine(ctxt, node);
4841 def->type = XML_RELAXNG_TEXT;
4842 if (node->children != NULL) {
4843 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4844 "text: had a child node\n", NULL, NULL);
4846 } else if (IS_RELAXNG(node, "zeroOrMore")) {
4847 def = xmlRelaxNGNewDefine(ctxt, node);
4850 def->type = XML_RELAXNG_ZEROORMORE;
4851 if (node->children == NULL) {
4852 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4853 "Element %s is empty\n", node->name, NULL);
4856 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4858 } else if (IS_RELAXNG(node, "oneOrMore")) {
4859 def = xmlRelaxNGNewDefine(ctxt, node);
4862 def->type = XML_RELAXNG_ONEORMORE;
4863 if (node->children == NULL) {
4864 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4865 "Element %s is empty\n", node->name, NULL);
4868 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4870 } else if (IS_RELAXNG(node, "optional")) {
4871 def = xmlRelaxNGNewDefine(ctxt, node);
4874 def->type = XML_RELAXNG_OPTIONAL;
4875 if (node->children == NULL) {
4876 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4877 "Element %s is empty\n", node->name, NULL);
4880 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4882 } else if (IS_RELAXNG(node, "choice")) {
4883 def = xmlRelaxNGNewDefine(ctxt, node);
4886 def->type = XML_RELAXNG_CHOICE;
4887 if (node->children == NULL) {
4888 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4889 "Element %s is empty\n", node->name, NULL);
4892 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4894 } else if (IS_RELAXNG(node, "group")) {
4895 def = xmlRelaxNGNewDefine(ctxt, node);
4898 def->type = XML_RELAXNG_GROUP;
4899 if (node->children == NULL) {
4900 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4901 "Element %s is empty\n", node->name, NULL);
4904 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4906 } else if (IS_RELAXNG(node, "ref")) {
4907 def = xmlRelaxNGNewDefine(ctxt, node);
4910 def->type = XML_RELAXNG_REF;
4911 def->name = xmlGetProp(node, BAD_CAST "name");
4912 if (def->name == NULL) {
4913 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4916 xmlRelaxNGNormExtSpace(def->name);
4917 if (xmlValidateNCName(def->name, 0)) {
4918 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4919 "ref name '%s' is not an NCName\n", def->name,
4923 if (node->children != NULL) {
4924 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4927 if (ctxt->grammar->refs == NULL)
4928 ctxt->grammar->refs = xmlHashCreate(10);
4929 if (ctxt->grammar->refs == NULL) {
4930 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4931 "Could not create references hash\n", NULL, NULL);
4936 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4938 xmlRelaxNGDefinePtr prev;
4940 prev = (xmlRelaxNGDefinePtr)
4941 xmlHashLookup(ctxt->grammar->refs, def->name);
4943 if (def->name != NULL) {
4944 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4945 "Error refs definitions '%s'\n",
4948 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4949 "Error refs definitions\n",
4954 def->nextHash = prev->nextHash;
4955 prev->nextHash = def;
4959 } else if (IS_RELAXNG(node, "data")) {
4960 def = xmlRelaxNGParseData(ctxt, node);
4961 } else if (IS_RELAXNG(node, "value")) {
4962 def = xmlRelaxNGParseValue(ctxt, node);
4963 } else if (IS_RELAXNG(node, "list")) {
4964 def = xmlRelaxNGNewDefine(ctxt, node);
4967 def->type = XML_RELAXNG_LIST;
4968 if (node->children == NULL) {
4969 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4970 "Element %s is empty\n", node->name, NULL);
4973 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4975 } else if (IS_RELAXNG(node, "interleave")) {
4976 def = xmlRelaxNGParseInterleave(ctxt, node);
4977 } else if (IS_RELAXNG(node, "externalRef")) {
4978 def = xmlRelaxNGProcessExternalRef(ctxt, node);
4979 } else if (IS_RELAXNG(node, "notAllowed")) {
4980 def = xmlRelaxNGNewDefine(ctxt, node);
4983 def->type = XML_RELAXNG_NOT_ALLOWED;
4984 if (node->children != NULL) {
4985 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4986 "xmlRelaxNGParse: notAllowed element is not empty\n",
4989 } else if (IS_RELAXNG(node, "grammar")) {
4990 xmlRelaxNGGrammarPtr grammar, old;
4991 xmlRelaxNGGrammarPtr oldparent;
4993 #ifdef DEBUG_GRAMMAR
4994 xmlGenericError(xmlGenericErrorContext,
4995 "Found <grammar> pattern\n");
4998 oldparent = ctxt->parentgrammar;
4999 old = ctxt->grammar;
5000 ctxt->parentgrammar = old;
5001 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
5003 ctxt->grammar = old;
5004 ctxt->parentgrammar = oldparent;
5006 if (grammar != NULL) {
5007 grammar->next = old->next;
5008 old->next = grammar;
5012 if (grammar != NULL)
5013 def = grammar->start;
5016 } else if (IS_RELAXNG(node, "parentRef")) {
5017 if (ctxt->parentgrammar == NULL) {
5018 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
5019 "Use of parentRef without a parent grammar\n", NULL,
5023 def = xmlRelaxNGNewDefine(ctxt, node);
5026 def->type = XML_RELAXNG_PARENTREF;
5027 def->name = xmlGetProp(node, BAD_CAST "name");
5028 if (def->name == NULL) {
5029 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
5030 "parentRef has no name\n", NULL, NULL);
5032 xmlRelaxNGNormExtSpace(def->name);
5033 if (xmlValidateNCName(def->name, 0)) {
5034 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5035 "parentRef name '%s' is not an NCName\n",
5039 if (node->children != NULL) {
5040 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5041 "parentRef is not empty\n", NULL, NULL);
5043 if (ctxt->parentgrammar->refs == NULL)
5044 ctxt->parentgrammar->refs = xmlHashCreate(10);
5045 if (ctxt->parentgrammar->refs == NULL) {
5046 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5047 "Could not create references hash\n", NULL, NULL);
5049 } else if (def->name != NULL) {
5053 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5055 xmlRelaxNGDefinePtr prev;
5057 prev = (xmlRelaxNGDefinePtr)
5058 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5060 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5061 "Internal error parentRef definitions '%s'\n",
5065 def->nextHash = prev->nextHash;
5066 prev->nextHash = def;
5070 } else if (IS_RELAXNG(node, "mixed")) {
5071 if (node->children == NULL) {
5072 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5076 def = xmlRelaxNGParseInterleave(ctxt, node);
5078 xmlRelaxNGDefinePtr tmp;
5080 if ((def->content != NULL) && (def->content->next != NULL)) {
5081 tmp = xmlRelaxNGNewDefine(ctxt, node);
5083 tmp->type = XML_RELAXNG_GROUP;
5084 tmp->content = def->content;
5089 tmp = xmlRelaxNGNewDefine(ctxt, node);
5092 tmp->type = XML_RELAXNG_TEXT;
5093 tmp->next = def->content;
5098 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5099 "Unexpected node %s is not a pattern\n", node->name,
5107 * xmlRelaxNGParseAttribute:
5108 * @ctxt: a Relax-NG parser context
5109 * @node: the element node
5111 * parse the content of a RelaxNG attribute node.
5113 * Returns the definition pointer or NULL in case of error.
5115 static xmlRelaxNGDefinePtr
5116 xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5118 xmlRelaxNGDefinePtr ret, cur;
5122 ret = xmlRelaxNGNewDefine(ctxt, node);
5125 ret->type = XML_RELAXNG_ATTRIBUTE;
5126 ret->parent = ctxt->def;
5127 child = node->children;
5128 if (child == NULL) {
5129 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5130 "xmlRelaxNGParseattribute: attribute has no children\n",
5134 old_flags = ctxt->flags;
5135 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
5136 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5138 child = child->next;
5140 if (child != NULL) {
5141 cur = xmlRelaxNGParsePattern(ctxt, child);
5143 switch (cur->type) {
5144 case XML_RELAXNG_EMPTY:
5145 case XML_RELAXNG_NOT_ALLOWED:
5146 case XML_RELAXNG_TEXT:
5147 case XML_RELAXNG_ELEMENT:
5148 case XML_RELAXNG_DATATYPE:
5149 case XML_RELAXNG_VALUE:
5150 case XML_RELAXNG_LIST:
5151 case XML_RELAXNG_REF:
5152 case XML_RELAXNG_PARENTREF:
5153 case XML_RELAXNG_EXTERNALREF:
5154 case XML_RELAXNG_DEF:
5155 case XML_RELAXNG_ONEORMORE:
5156 case XML_RELAXNG_ZEROORMORE:
5157 case XML_RELAXNG_OPTIONAL:
5158 case XML_RELAXNG_CHOICE:
5159 case XML_RELAXNG_GROUP:
5160 case XML_RELAXNG_INTERLEAVE:
5161 case XML_RELAXNG_ATTRIBUTE:
5165 case XML_RELAXNG_START:
5166 case XML_RELAXNG_PARAM:
5167 case XML_RELAXNG_EXCEPT:
5168 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5169 "attribute has invalid content\n", NULL,
5172 case XML_RELAXNG_NOOP:
5173 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5174 "RNG Internal error, noop found in attribute\n",
5179 child = child->next;
5181 if (child != NULL) {
5182 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5183 "attribute has multiple children\n", NULL, NULL);
5185 ctxt->flags = old_flags;
5190 * xmlRelaxNGParseExceptNameClass:
5191 * @ctxt: a Relax-NG parser context
5192 * @node: the except node
5193 * @attr: 1 if within an attribute, 0 if within an element
5195 * parse the content of a RelaxNG nameClass node.
5197 * Returns the definition pointer or NULL in case of error.
5199 static xmlRelaxNGDefinePtr
5200 xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
5201 xmlNodePtr node, int attr)
5203 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5206 if (!IS_RELAXNG(node, "except")) {
5207 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5208 "Expecting an except node\n", NULL, NULL);
5211 if (node->next != NULL) {
5212 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5213 "exceptNameClass allows only a single except node\n",
5216 if (node->children == NULL) {
5217 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5222 ret = xmlRelaxNGNewDefine(ctxt, node);
5225 ret->type = XML_RELAXNG_EXCEPT;
5226 child = node->children;
5227 while (child != NULL) {
5228 cur = xmlRelaxNGNewDefine(ctxt, child);
5232 cur->type = XML_RELAXNG_ATTRIBUTE;
5234 cur->type = XML_RELAXNG_ELEMENT;
5236 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
5244 child = child->next;
5251 * xmlRelaxNGParseNameClass:
5252 * @ctxt: a Relax-NG parser context
5253 * @node: the nameClass node
5254 * @def: the current definition
5256 * parse the content of a RelaxNG nameClass node.
5258 * Returns the definition pointer or NULL in case of error.
5260 static xmlRelaxNGDefinePtr
5261 xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
5262 xmlRelaxNGDefinePtr def)
5264 xmlRelaxNGDefinePtr ret, tmp;
5268 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
5269 (IS_RELAXNG(node, "nsName"))) {
5270 if ((def->type != XML_RELAXNG_ELEMENT) &&
5271 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5272 ret = xmlRelaxNGNewDefine(ctxt, node);
5276 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5277 ret->type = XML_RELAXNG_ATTRIBUTE;
5279 ret->type = XML_RELAXNG_ELEMENT;
5282 if (IS_RELAXNG(node, "name")) {
5283 val = xmlNodeGetContent(node);
5284 xmlRelaxNGNormExtSpace(val);
5285 if (xmlValidateNCName(val, 0)) {
5286 if (node->parent != NULL)
5287 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5288 "Element %s name '%s' is not an NCName\n",
5289 node->parent->name, val);
5291 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5292 "name '%s' is not an NCName\n",
5296 val = xmlGetProp(node, BAD_CAST "ns");
5298 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5300 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5301 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5302 "Attribute with namespace '%s' is not allowed\n",
5305 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5307 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
5308 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5309 "Attribute with QName 'xmlns' is not allowed\n",
5312 } else if (IS_RELAXNG(node, "anyName")) {
5315 if (node->children != NULL) {
5317 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5319 XML_RELAXNG_ATTRIBUTE));
5321 } else if (IS_RELAXNG(node, "nsName")) {
5323 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5324 if (ret->ns == NULL) {
5325 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5326 "nsName has no ns attribute\n", NULL, NULL);
5328 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5329 (ret->ns != NULL) &&
5331 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5332 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5333 "Attribute with namespace '%s' is not allowed\n",
5336 if (node->children != NULL) {
5338 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5340 XML_RELAXNG_ATTRIBUTE));
5342 } else if (IS_RELAXNG(node, "choice")) {
5344 xmlRelaxNGDefinePtr last = NULL;
5346 ret = xmlRelaxNGNewDefine(ctxt, node);
5350 ret->type = XML_RELAXNG_CHOICE;
5352 if (node->children == NULL) {
5353 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5354 "Element choice is empty\n", NULL, NULL);
5357 child = node->children;
5358 while (child != NULL) {
5359 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5362 last = ret->nameClass = tmp;
5368 child = child->next;
5372 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5373 "expecting name, anyName, nsName or choice : got %s\n",
5374 (node == NULL ? (const xmlChar *) "nothing" : node->name),
5379 if (def->nameClass == NULL) {
5380 def->nameClass = ret;
5382 tmp = def->nameClass;
5383 while (tmp->next != NULL) {
5393 * xmlRelaxNGParseElement:
5394 * @ctxt: a Relax-NG parser context
5395 * @node: the element node
5397 * parse the content of a RelaxNG element node.
5399 * Returns the definition pointer or NULL in case of error.
5401 static xmlRelaxNGDefinePtr
5402 xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5404 xmlRelaxNGDefinePtr ret, cur, last;
5406 const xmlChar *olddefine;
5408 ret = xmlRelaxNGNewDefine(ctxt, node);
5411 ret->type = XML_RELAXNG_ELEMENT;
5412 ret->parent = ctxt->def;
5413 child = node->children;
5414 if (child == NULL) {
5415 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5416 "xmlRelaxNGParseElement: element has no children\n",
5420 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5422 child = child->next;
5424 if (child == NULL) {
5425 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5426 "xmlRelaxNGParseElement: element has no content\n",
5430 olddefine = ctxt->define;
5431 ctxt->define = NULL;
5433 while (child != NULL) {
5434 cur = xmlRelaxNGParsePattern(ctxt, child);
5437 switch (cur->type) {
5438 case XML_RELAXNG_EMPTY:
5439 case XML_RELAXNG_NOT_ALLOWED:
5440 case XML_RELAXNG_TEXT:
5441 case XML_RELAXNG_ELEMENT:
5442 case XML_RELAXNG_DATATYPE:
5443 case XML_RELAXNG_VALUE:
5444 case XML_RELAXNG_LIST:
5445 case XML_RELAXNG_REF:
5446 case XML_RELAXNG_PARENTREF:
5447 case XML_RELAXNG_EXTERNALREF:
5448 case XML_RELAXNG_DEF:
5449 case XML_RELAXNG_ZEROORMORE:
5450 case XML_RELAXNG_ONEORMORE:
5451 case XML_RELAXNG_OPTIONAL:
5452 case XML_RELAXNG_CHOICE:
5453 case XML_RELAXNG_GROUP:
5454 case XML_RELAXNG_INTERLEAVE:
5456 ret->content = last = cur;
5458 if ((last->type == XML_RELAXNG_ELEMENT) &&
5459 (ret->content == last)) {
5460 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5461 if (ret->content != NULL) {
5462 ret->content->type = XML_RELAXNG_GROUP;
5463 ret->content->content = last;
5465 ret->content = last;
5472 case XML_RELAXNG_ATTRIBUTE:
5473 cur->next = ret->attrs;
5476 case XML_RELAXNG_START:
5477 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5478 "RNG Internal error, start found in element\n",
5481 case XML_RELAXNG_PARAM:
5482 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5483 "RNG Internal error, param found in element\n",
5486 case XML_RELAXNG_EXCEPT:
5487 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5488 "RNG Internal error, except found in element\n",
5491 case XML_RELAXNG_NOOP:
5492 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5493 "RNG Internal error, noop found in element\n",
5498 child = child->next;
5500 ctxt->define = olddefine;
5505 * xmlRelaxNGParsePatterns:
5506 * @ctxt: a Relax-NG parser context
5507 * @nodes: list of nodes
5508 * @group: use an implicit <group> for elements
5510 * parse the content of a RelaxNG start node.
5512 * Returns the definition pointer or NULL in case of error.
5514 static xmlRelaxNGDefinePtr
5515 xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
5518 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
5521 while (nodes != NULL) {
5522 if (IS_RELAXNG(nodes, "element")) {
5523 cur = xmlRelaxNGParseElement(ctxt, nodes);
5527 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5529 def = xmlRelaxNGNewDefine(ctxt, nodes);
5530 def->type = XML_RELAXNG_GROUP;
5531 def->content = last;
5536 cur->parent = parent;
5538 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5548 nodes = nodes->next;
5554 * xmlRelaxNGParseStart:
5555 * @ctxt: a Relax-NG parser context
5556 * @nodes: start children nodes
5558 * parse the content of a RelaxNG start node.
5560 * Returns 0 in case of success, -1 in case of error
5563 xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5566 xmlRelaxNGDefinePtr def = NULL, last;
5568 if (nodes == NULL) {
5569 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5573 if (IS_RELAXNG(nodes, "empty")) {
5574 def = xmlRelaxNGNewDefine(ctxt, nodes);
5577 def->type = XML_RELAXNG_EMPTY;
5578 if (nodes->children != NULL) {
5579 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5580 "element empty is not empty\n", NULL, NULL);
5582 } else if (IS_RELAXNG(nodes, "notAllowed")) {
5583 def = xmlRelaxNGNewDefine(ctxt, nodes);
5586 def->type = XML_RELAXNG_NOT_ALLOWED;
5587 if (nodes->children != NULL) {
5588 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5589 "element notAllowed is not empty\n", NULL, NULL);
5592 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
5594 if (ctxt->grammar->start != NULL) {
5595 last = ctxt->grammar->start;
5596 while (last->next != NULL)
5600 ctxt->grammar->start = def;
5602 nodes = nodes->next;
5603 if (nodes != NULL) {
5604 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5605 "start more than one children\n", NULL, NULL);
5612 * xmlRelaxNGParseGrammarContent:
5613 * @ctxt: a Relax-NG parser context
5614 * @nodes: grammar children nodes
5616 * parse the content of a RelaxNG grammar node.
5618 * Returns 0 in case of success, -1 in case of error
5621 xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5626 if (nodes == NULL) {
5627 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5628 "grammar has no children\n", NULL, NULL);
5631 while (nodes != NULL) {
5632 if (IS_RELAXNG(nodes, "start")) {
5633 if (nodes->children == NULL) {
5634 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5635 "start has no children\n", NULL, NULL);
5637 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5641 } else if (IS_RELAXNG(nodes, "define")) {
5642 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5645 } else if (IS_RELAXNG(nodes, "include")) {
5646 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5650 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5651 "grammar has unexpected child %s\n", nodes->name,
5655 nodes = nodes->next;
5661 * xmlRelaxNGCheckReference:
5663 * @ctxt: a Relax-NG parser context
5664 * @name: the name associated to the defines
5666 * Applies the 4.17. combine attribute rule for all the define
5667 * element of a given grammar using the same name.
5670 xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
5671 xmlRelaxNGParserCtxtPtr ctxt,
5672 const xmlChar * name)
5674 xmlRelaxNGGrammarPtr grammar;
5675 xmlRelaxNGDefinePtr def, cur;
5678 * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
5680 if (ref->dflags & IS_EXTERNAL_REF)
5683 grammar = ctxt->grammar;
5684 if (grammar == NULL) {
5685 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5686 "Internal error: no grammar in CheckReference %s\n",
5690 if (ref->content != NULL) {
5691 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5692 "Internal error: reference has content in CheckReference %s\n",
5696 if (grammar->defs != NULL) {
5697 def = xmlHashLookup(grammar->defs, name);
5700 while (cur != NULL) {
5702 cur = cur->nextHash;
5705 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5706 "Reference %s has no matching definition\n", name,
5710 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5711 "Reference %s has no matching definition\n", name,
5717 * xmlRelaxNGCheckCombine:
5718 * @define: the define(s) list
5719 * @ctxt: a Relax-NG parser context
5720 * @name: the name associated to the defines
5722 * Applies the 4.17. combine attribute rule for all the define
5723 * element of a given grammar using the same name.
5726 xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
5727 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5730 int choiceOrInterleave = -1;
5732 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5734 if (define->nextHash == NULL)
5737 while (cur != NULL) {
5738 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5739 if (combine != NULL) {
5740 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5741 if (choiceOrInterleave == -1)
5742 choiceOrInterleave = 1;
5743 else if (choiceOrInterleave == 0) {
5744 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5745 "Defines for %s use both 'choice' and 'interleave'\n",
5748 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5749 if (choiceOrInterleave == -1)
5750 choiceOrInterleave = 0;
5751 else if (choiceOrInterleave == 1) {
5752 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5753 "Defines for %s use both 'choice' and 'interleave'\n",
5757 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5758 "Defines for %s use unknown combine value '%s''\n",
5766 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5767 "Some defines for %s needs the combine attribute\n",
5772 cur = cur->nextHash;
5775 xmlGenericError(xmlGenericErrorContext,
5776 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5777 name, choiceOrInterleave);
5779 if (choiceOrInterleave == -1)
5780 choiceOrInterleave = 0;
5781 cur = xmlRelaxNGNewDefine(ctxt, define->node);
5784 if (choiceOrInterleave == 0)
5785 cur->type = XML_RELAXNG_INTERLEAVE;
5787 cur->type = XML_RELAXNG_CHOICE;
5790 while (tmp != NULL) {
5791 if (tmp->content != NULL) {
5792 if (tmp->content->next != NULL) {
5794 * we need first to create a wrapper.
5796 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5799 tmp2->type = XML_RELAXNG_GROUP;
5800 tmp2->content = tmp->content;
5802 tmp2 = tmp->content;
5805 cur->content = tmp2;
5812 tmp = tmp->nextHash;
5814 define->content = cur;
5815 if (choiceOrInterleave == 0) {
5816 if (ctxt->interleaves == NULL)
5817 ctxt->interleaves = xmlHashCreate(10);
5818 if (ctxt->interleaves == NULL) {
5819 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5820 "Failed to create interleaves hash table\n", NULL,
5825 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5826 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5828 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5829 "Failed to add %s to hash table\n",
5830 (const xmlChar *) tmpname, NULL);
5837 * xmlRelaxNGCombineStart:
5838 * @ctxt: a Relax-NG parser context
5839 * @grammar: the grammar
5841 * Applies the 4.17. combine rule for all the start
5842 * element of a given grammar.
5845 xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
5846 xmlRelaxNGGrammarPtr grammar)
5848 xmlRelaxNGDefinePtr starts;
5850 int choiceOrInterleave = -1;
5852 xmlRelaxNGDefinePtr cur;
5854 starts = grammar->start;
5855 if ((starts == NULL) || (starts->next == NULL))
5858 while (cur != NULL) {
5859 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5860 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5862 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5863 "Internal error: start element not found\n", NULL,
5866 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5869 if (combine != NULL) {
5870 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5871 if (choiceOrInterleave == -1)
5872 choiceOrInterleave = 1;
5873 else if (choiceOrInterleave == 0) {
5874 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5875 "<start> use both 'choice' and 'interleave'\n",
5878 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5879 if (choiceOrInterleave == -1)
5880 choiceOrInterleave = 0;
5881 else if (choiceOrInterleave == 1) {
5882 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5883 "<start> use both 'choice' and 'interleave'\n",
5887 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5888 "<start> uses unknown combine value '%s''\n",
5896 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5897 "Some <start> element miss the combine attribute\n",
5905 xmlGenericError(xmlGenericErrorContext,
5906 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5907 choiceOrInterleave);
5909 if (choiceOrInterleave == -1)
5910 choiceOrInterleave = 0;
5911 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
5914 if (choiceOrInterleave == 0)
5915 cur->type = XML_RELAXNG_INTERLEAVE;
5917 cur->type = XML_RELAXNG_CHOICE;
5918 cur->content = grammar->start;
5919 grammar->start = cur;
5920 if (choiceOrInterleave == 0) {
5921 if (ctxt->interleaves == NULL)
5922 ctxt->interleaves = xmlHashCreate(10);
5923 if (ctxt->interleaves == NULL) {
5924 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5925 "Failed to create interleaves hash table\n", NULL,
5930 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5931 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5933 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5934 "Failed to add %s to hash table\n",
5935 (const xmlChar *) tmpname, NULL);
5942 * xmlRelaxNGCheckCycles:
5943 * @ctxt: a Relax-NG parser context
5944 * @nodes: grammar children nodes
5945 * @depth: the counter
5949 * Returns 0 if check passed, and -1 in case of error
5952 xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5953 xmlRelaxNGDefinePtr cur, int depth)
5957 while ((ret == 0) && (cur != NULL)) {
5958 if ((cur->type == XML_RELAXNG_REF) ||
5959 (cur->type == XML_RELAXNG_PARENTREF)) {
5960 if (cur->depth == -1) {
5962 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5964 } else if (depth == cur->depth) {
5965 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5966 "Detected a cycle in %s references\n",
5970 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5971 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5973 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5981 * xmlRelaxNGTryUnlink:
5982 * @ctxt: a Relax-NG parser context
5983 * @cur: the definition to unlink
5984 * @parent: the parent definition
5985 * @prev: the previous sibling definition
5987 * Try to unlink a definition. If not possble make it a NOOP
5989 * Returns the new prev definition
5991 static xmlRelaxNGDefinePtr
5992 xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5993 xmlRelaxNGDefinePtr cur,
5994 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5997 prev->next = cur->next;
5999 if (parent != NULL) {
6000 if (parent->content == cur)
6001 parent->content = cur->next;
6002 else if (parent->attrs == cur)
6003 parent->attrs = cur->next;
6004 else if (parent->nameClass == cur)
6005 parent->nameClass = cur->next;
6007 cur->type = XML_RELAXNG_NOOP;
6015 * xmlRelaxNGSimplify:
6016 * @ctxt: a Relax-NG parser context
6017 * @nodes: grammar children nodes
6019 * Check for simplification of empty and notAllowed
6022 xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
6023 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
6025 xmlRelaxNGDefinePtr prev = NULL;
6027 while (cur != NULL) {
6028 if ((cur->type == XML_RELAXNG_REF) ||
6029 (cur->type == XML_RELAXNG_PARENTREF)) {
6030 if (cur->depth != -3) {
6032 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6034 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6035 cur->parent = parent;
6036 if ((parent != NULL) &&
6037 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6038 (parent->type == XML_RELAXNG_LIST) ||
6039 (parent->type == XML_RELAXNG_GROUP) ||
6040 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6041 (parent->type == XML_RELAXNG_ONEORMORE) ||
6042 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6043 parent->type = XML_RELAXNG_NOT_ALLOWED;
6046 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6047 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6050 } else if (cur->type == XML_RELAXNG_EMPTY) {
6051 cur->parent = parent;
6052 if ((parent != NULL) &&
6053 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6054 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6055 parent->type = XML_RELAXNG_EMPTY;
6058 if ((parent != NULL) &&
6059 ((parent->type == XML_RELAXNG_GROUP) ||
6060 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6061 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6065 cur->parent = parent;
6066 if (cur->content != NULL)
6067 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6068 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6069 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6070 if (cur->nameClass != NULL)
6071 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6073 * On Elements, try to move attribute only generating rules on
6076 if (cur->type == XML_RELAXNG_ELEMENT) {
6078 xmlRelaxNGDefinePtr tmp, pre;
6080 while (cur->content != NULL) {
6082 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6083 if (attronly == 1) {
6085 * migrate cur->content to attrs
6088 cur->content = tmp->next;
6089 tmp->next = cur->attrs;
6093 * cur->content can generate elements or text
6099 while ((pre != NULL) && (pre->next != NULL)) {
6101 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6102 if (attronly == 1) {
6104 * migrate tmp to attrs
6106 pre->next = tmp->next;
6107 tmp->next = cur->attrs;
6115 * This may result in a simplification
6117 if ((cur->type == XML_RELAXNG_GROUP) ||
6118 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6119 if (cur->content == NULL)
6120 cur->type = XML_RELAXNG_EMPTY;
6121 else if (cur->content->next == NULL) {
6122 if ((parent == NULL) && (prev == NULL)) {
6123 cur->type = XML_RELAXNG_NOOP;
6124 } else if (prev == NULL) {
6125 parent->content = cur->content;
6126 cur->content->next = cur->next;
6129 cur->content->next = cur->next;
6130 prev->next = cur->content;
6136 * the current node may have been transformed back
6138 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6139 (cur->content != NULL) &&
6140 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6141 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6142 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6143 if ((parent != NULL) &&
6144 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6145 (parent->type == XML_RELAXNG_LIST) ||
6146 (parent->type == XML_RELAXNG_GROUP) ||
6147 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6148 (parent->type == XML_RELAXNG_ONEORMORE) ||
6149 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6150 parent->type = XML_RELAXNG_NOT_ALLOWED;
6153 if ((parent != NULL) &&
6154 (parent->type == XML_RELAXNG_CHOICE)) {
6155 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6158 } else if (cur->type == XML_RELAXNG_EMPTY) {
6159 if ((parent != NULL) &&
6160 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6161 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6162 parent->type = XML_RELAXNG_EMPTY;
6165 if ((parent != NULL) &&
6166 ((parent->type == XML_RELAXNG_GROUP) ||
6167 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6168 (parent->type == XML_RELAXNG_CHOICE))) {
6169 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6181 * xmlRelaxNGGroupContentType:
6182 * @ct1: the first content type
6183 * @ct2: the second content type
6185 * Try to group 2 content types
6187 * Returns the content type
6189 static xmlRelaxNGContentType
6190 xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
6191 xmlRelaxNGContentType ct2)
6193 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6194 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6195 return (XML_RELAXNG_CONTENT_ERROR);
6196 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
6198 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
6200 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
6201 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6202 return (XML_RELAXNG_CONTENT_COMPLEX);
6203 return (XML_RELAXNG_CONTENT_ERROR);
6207 * xmlRelaxNGMaxContentType:
6208 * @ct1: the first content type
6209 * @ct2: the second content type
6211 * Compute the max content-type
6213 * Returns the content type
6215 static xmlRelaxNGContentType
6216 xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
6217 xmlRelaxNGContentType ct2)
6219 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6220 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6221 return (XML_RELAXNG_CONTENT_ERROR);
6222 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
6223 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6224 return (XML_RELAXNG_CONTENT_SIMPLE);
6225 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
6226 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6227 return (XML_RELAXNG_CONTENT_COMPLEX);
6228 return (XML_RELAXNG_CONTENT_EMPTY);
6232 * xmlRelaxNGCheckRules:
6233 * @ctxt: a Relax-NG parser context
6234 * @cur: the current definition
6235 * @flags: some accumulated flags
6236 * @ptype: the parent type
6238 * Check for rules in section 7.1 and 7.2
6240 * Returns the content type of @cur
6242 static xmlRelaxNGContentType
6243 xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6244 xmlRelaxNGDefinePtr cur, int flags,
6245 xmlRelaxNGType ptype)
6248 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
6250 while (cur != NULL) {
6251 ret = XML_RELAXNG_CONTENT_EMPTY;
6252 if ((cur->type == XML_RELAXNG_REF) ||
6253 (cur->type == XML_RELAXNG_PARENTREF)) {
6255 * This should actually be caught by list//element(ref) at the
6256 * element boundaries, c.f. Bug #159968 local refs are dropped
6260 if (flags & XML_RELAXNG_IN_LIST) {
6261 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6262 "Found forbidden pattern list//ref\n", NULL,
6266 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6267 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6268 "Found forbidden pattern data/except//ref\n",
6271 if (cur->content == NULL) {
6272 if (cur->type == XML_RELAXNG_PARENTREF)
6273 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6274 "Internal found no define for parent refs\n",
6277 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6278 "Internal found no define for ref %s\n",
6279 (cur->name ? cur->name: BAD_CAST "null"), NULL);
6281 if (cur->depth > -4) {
6283 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6285 cur->depth = ret - 15;
6286 } else if (cur->depth == -4) {
6287 ret = XML_RELAXNG_CONTENT_COMPLEX;
6289 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6291 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6293 * The 7.3 Attribute derivation rule for groups is plugged there
6295 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6296 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6297 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6298 "Found forbidden pattern data/except//element(ref)\n",
6301 if (flags & XML_RELAXNG_IN_LIST) {
6302 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6303 "Found forbidden pattern list//element(ref)\n",
6306 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6307 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6308 "Found forbidden pattern attribute//element(ref)\n",
6311 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6312 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6313 "Found forbidden pattern attribute//element(ref)\n",
6317 * reset since in the simple form elements are only child
6322 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6323 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6324 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6325 "Element %s attributes have a content type error\n",
6329 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6331 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6332 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6333 "Element %s has a content type error\n",
6336 ret = XML_RELAXNG_CONTENT_COMPLEX;
6338 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6339 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6340 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6341 "Found forbidden pattern attribute//attribute\n",
6344 if (flags & XML_RELAXNG_IN_LIST) {
6345 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6346 "Found forbidden pattern list//attribute\n",
6349 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6350 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6351 "Found forbidden pattern oneOrMore//group//attribute\n",
6354 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6355 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6356 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6359 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6360 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6361 "Found forbidden pattern data/except//attribute\n",
6364 if (flags & XML_RELAXNG_IN_START) {
6365 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6366 "Found forbidden pattern start//attribute\n",
6369 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6370 && (cur->name == NULL)) {
6371 if (cur->ns == NULL) {
6372 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6373 "Found anyName attribute without oneOrMore ancestor\n",
6376 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6377 "Found nsName attribute without oneOrMore ancestor\n",
6381 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6382 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6383 ret = XML_RELAXNG_CONTENT_EMPTY;
6384 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6385 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6386 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6387 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6388 "Found forbidden pattern data/except//oneOrMore\n",
6391 if (flags & XML_RELAXNG_IN_START) {
6392 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6393 "Found forbidden pattern start//oneOrMore\n",
6396 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6398 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6400 ret = xmlRelaxNGGroupContentType(ret, ret);
6401 } else if (cur->type == XML_RELAXNG_LIST) {
6402 if (flags & XML_RELAXNG_IN_LIST) {
6403 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6404 "Found forbidden pattern list//list\n", NULL,
6407 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6408 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6409 "Found forbidden pattern data/except//list\n",
6412 if (flags & XML_RELAXNG_IN_START) {
6413 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6414 "Found forbidden pattern start//list\n", NULL,
6417 nflags = flags | XML_RELAXNG_IN_LIST;
6419 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6421 } else if (cur->type == XML_RELAXNG_GROUP) {
6422 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6423 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6424 "Found forbidden pattern data/except//group\n",
6427 if (flags & XML_RELAXNG_IN_START) {
6428 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6429 "Found forbidden pattern start//group\n", NULL,
6432 if (flags & XML_RELAXNG_IN_ONEORMORE)
6433 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6437 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6440 * The 7.3 Attribute derivation rule for groups is plugged there
6442 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6443 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6444 if (flags & XML_RELAXNG_IN_LIST) {
6445 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6446 "Found forbidden pattern list//interleave\n",
6449 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6450 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6451 "Found forbidden pattern data/except//interleave\n",
6454 if (flags & XML_RELAXNG_IN_START) {
6455 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6456 "Found forbidden pattern start//interleave\n",
6459 if (flags & XML_RELAXNG_IN_ONEORMORE)
6460 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6464 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6466 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6467 if ((cur->parent != NULL) &&
6468 (cur->parent->type == XML_RELAXNG_DATATYPE))
6469 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6473 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6475 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6476 if (flags & XML_RELAXNG_IN_START) {
6477 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6478 "Found forbidden pattern start//data\n", NULL,
6481 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6482 ret = XML_RELAXNG_CONTENT_SIMPLE;
6483 } else if (cur->type == XML_RELAXNG_VALUE) {
6484 if (flags & XML_RELAXNG_IN_START) {
6485 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6486 "Found forbidden pattern start//value\n", NULL,
6489 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6490 ret = XML_RELAXNG_CONTENT_SIMPLE;
6491 } else if (cur->type == XML_RELAXNG_TEXT) {
6492 if (flags & XML_RELAXNG_IN_LIST) {
6493 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6494 "Found forbidden pattern list//text\n", NULL,
6497 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6498 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6499 "Found forbidden pattern data/except//text\n",
6502 if (flags & XML_RELAXNG_IN_START) {
6503 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6504 "Found forbidden pattern start//text\n", NULL,
6507 ret = XML_RELAXNG_CONTENT_COMPLEX;
6508 } else if (cur->type == XML_RELAXNG_EMPTY) {
6509 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6510 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6511 "Found forbidden pattern data/except//empty\n",
6514 if (flags & XML_RELAXNG_IN_START) {
6515 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6516 "Found forbidden pattern start//empty\n", NULL,
6519 ret = XML_RELAXNG_CONTENT_EMPTY;
6520 } else if (cur->type == XML_RELAXNG_CHOICE) {
6521 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6523 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6526 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6529 if (ptype == XML_RELAXNG_GROUP) {
6530 val = xmlRelaxNGGroupContentType(val, ret);
6531 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6533 * TODO: scan complain that tmp is never used, seems on purpose
6534 * need double-checking
6536 tmp = xmlRelaxNGGroupContentType(val, ret);
6537 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6538 tmp = xmlRelaxNGMaxContentType(val, ret);
6539 } else if (ptype == XML_RELAXNG_CHOICE) {
6540 val = xmlRelaxNGMaxContentType(val, ret);
6541 } else if (ptype == XML_RELAXNG_LIST) {
6542 val = XML_RELAXNG_CONTENT_SIMPLE;
6543 } else if (ptype == XML_RELAXNG_EXCEPT) {
6544 if (ret == XML_RELAXNG_CONTENT_ERROR)
6545 val = XML_RELAXNG_CONTENT_ERROR;
6547 val = XML_RELAXNG_CONTENT_SIMPLE;
6549 val = xmlRelaxNGGroupContentType(val, ret);
6557 * xmlRelaxNGParseGrammar:
6558 * @ctxt: a Relax-NG parser context
6559 * @nodes: grammar children nodes
6561 * parse a Relax-NG <grammar> node
6563 * Returns the internal xmlRelaxNGGrammarPtr built or
6564 * NULL in case of error
6566 static xmlRelaxNGGrammarPtr
6567 xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6569 xmlRelaxNGGrammarPtr ret, tmp, old;
6571 #ifdef DEBUG_GRAMMAR
6572 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6575 ret = xmlRelaxNGNewGrammar(ctxt);
6580 * Link the new grammar in the tree
6582 ret->parent = ctxt->grammar;
6583 if (ctxt->grammar != NULL) {
6584 tmp = ctxt->grammar->children;
6586 ctxt->grammar->children = ret;
6588 while (tmp->next != NULL)
6594 old = ctxt->grammar;
6595 ctxt->grammar = ret;
6596 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6597 ctxt->grammar = ret;
6598 if (ctxt->grammar == NULL) {
6599 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6600 "Failed to parse <grammar> content\n", NULL, NULL);
6601 } else if (ctxt->grammar->start == NULL) {
6602 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6603 "Element <grammar> has no <start>\n", NULL, NULL);
6607 * Apply 4.17 merging rules to defines and starts
6609 xmlRelaxNGCombineStart(ctxt, ret);
6610 if (ret->defs != NULL) {
6611 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6616 * link together defines and refs in this grammar
6618 if (ret->refs != NULL) {
6619 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6626 ctxt->grammar = old;
6631 * xmlRelaxNGParseDocument:
6632 * @ctxt: a Relax-NG parser context
6633 * @node: the root node of the RelaxNG schema
6635 * parse a Relax-NG definition resource and build an internal
6636 * xmlRelaxNG struture which can be used to validate instances.
6638 * Returns the internal XML RelaxNG structure built or
6639 * NULL in case of error
6641 static xmlRelaxNGPtr
6642 xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6644 xmlRelaxNGPtr schema = NULL;
6645 const xmlChar *olddefine;
6646 xmlRelaxNGGrammarPtr old;
6648 if ((ctxt == NULL) || (node == NULL))
6651 schema = xmlRelaxNGNewRelaxNG(ctxt);
6655 olddefine = ctxt->define;
6656 ctxt->define = NULL;
6657 if (IS_RELAXNG(node, "grammar")) {
6658 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
6659 if (schema->topgrammar == NULL) {
6660 xmlRelaxNGFree(schema);
6664 xmlRelaxNGGrammarPtr tmp, ret;
6666 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6667 if (schema->topgrammar == NULL) {
6668 xmlRelaxNGFree(schema);
6672 * Link the new grammar in the tree
6674 ret->parent = ctxt->grammar;
6675 if (ctxt->grammar != NULL) {
6676 tmp = ctxt->grammar->children;
6678 ctxt->grammar->children = ret;
6680 while (tmp->next != NULL)
6685 old = ctxt->grammar;
6686 ctxt->grammar = ret;
6687 xmlRelaxNGParseStart(ctxt, node);
6689 ctxt->grammar = old;
6691 ctxt->define = olddefine;
6692 if (schema->topgrammar->start != NULL) {
6693 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6694 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6695 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6696 while ((schema->topgrammar->start != NULL) &&
6697 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6698 (schema->topgrammar->start->next != NULL))
6699 schema->topgrammar->start =
6700 schema->topgrammar->start->content;
6701 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6702 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6707 xmlGenericError(xmlGenericErrorContext,
6708 "xmlRelaxNGParseDocument() failed\n");
6714 /************************************************************************
6716 * Reading RelaxNGs *
6718 ************************************************************************/
6721 * xmlRelaxNGNewParserCtxt:
6722 * @URL: the location of the schema
6724 * Create an XML RelaxNGs parse context for that file/resource expected
6725 * to contain an XML RelaxNGs file.
6727 * Returns the parser context or NULL in case of error
6729 xmlRelaxNGParserCtxtPtr
6730 xmlRelaxNGNewParserCtxt(const char *URL)
6732 xmlRelaxNGParserCtxtPtr ret;
6738 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6740 xmlRngPErrMemory(NULL, "building parser\n");
6743 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6744 ret->URL = xmlStrdup((const xmlChar *) URL);
6745 ret->error = xmlGenericError;
6746 ret->userData = xmlGenericErrorContext;
6751 * xmlRelaxNGNewMemParserCtxt:
6752 * @buffer: a pointer to a char array containing the schemas
6753 * @size: the size of the array
6755 * Create an XML RelaxNGs parse context for that memory buffer expected
6756 * to contain an XML RelaxNGs file.
6758 * Returns the parser context or NULL in case of error
6760 xmlRelaxNGParserCtxtPtr
6761 xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6763 xmlRelaxNGParserCtxtPtr ret;
6765 if ((buffer == NULL) || (size <= 0))
6769 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6771 xmlRngPErrMemory(NULL, "building parser\n");
6774 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6775 ret->buffer = buffer;
6777 ret->error = xmlGenericError;
6778 ret->userData = xmlGenericErrorContext;
6783 * xmlRelaxNGNewDocParserCtxt:
6784 * @doc: a preparsed document tree
6786 * Create an XML RelaxNGs parser context for that document.
6787 * Note: since the process of compiling a RelaxNG schemas modifies the
6788 * document, the @doc parameter is duplicated internally.
6790 * Returns the parser context or NULL in case of error
6792 xmlRelaxNGParserCtxtPtr
6793 xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6795 xmlRelaxNGParserCtxtPtr ret;
6800 copy = xmlCopyDoc(doc, 1);
6805 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6807 xmlRngPErrMemory(NULL, "building parser\n");
6810 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6811 ret->document = copy;
6813 ret->userData = xmlGenericErrorContext;
6818 * xmlRelaxNGFreeParserCtxt:
6819 * @ctxt: the schema parser context
6821 * Free the resources associated to the schema parser context
6824 xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6828 if (ctxt->URL != NULL)
6830 if (ctxt->doc != NULL)
6831 xmlRelaxNGFreeDocument(ctxt->doc);
6832 if (ctxt->interleaves != NULL)
6833 xmlHashFree(ctxt->interleaves, NULL);
6834 if (ctxt->documents != NULL)
6835 xmlRelaxNGFreeDocumentList(ctxt->documents);
6836 if (ctxt->includes != NULL)
6837 xmlRelaxNGFreeIncludeList(ctxt->includes);
6838 if (ctxt->docTab != NULL)
6839 xmlFree(ctxt->docTab);
6840 if (ctxt->incTab != NULL)
6841 xmlFree(ctxt->incTab);
6842 if (ctxt->defTab != NULL) {
6845 for (i = 0; i < ctxt->defNr; i++)
6846 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6847 xmlFree(ctxt->defTab);
6849 if ((ctxt->document != NULL) && (ctxt->freedoc))
6850 xmlFreeDoc(ctxt->document);
6855 * xmlRelaxNGNormExtSpace:
6858 * Removes the leading and ending spaces of the value
6859 * The string is modified "in situ"
6862 xmlRelaxNGNormExtSpace(xmlChar * value)
6864 xmlChar *start = value;
6865 xmlChar *cur = value;
6870 while (IS_BLANK_CH(*cur))
6874 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
6879 while (IS_BLANK_CH(*cur))
6888 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
6894 /* don't try to normalize the inner spaces */
6895 while (IS_BLANK_CH(*cur))
6907 * xmlRelaxNGCleanupAttributes:
6908 * @ctxt: a Relax-NG parser context
6909 * @node: a Relax-NG node
6911 * Check all the attributes on the given node
6914 xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6916 xmlAttrPtr cur, next;
6918 cur = node->properties;
6919 while (cur != NULL) {
6921 if ((cur->ns == NULL) ||
6922 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6923 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6924 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6925 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6926 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6927 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6928 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6929 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6930 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6931 "Attribute %s is not allowed on %s\n",
6932 cur->name, node->name);
6934 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6935 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6936 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6937 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6938 "Attribute %s is not allowed on %s\n",
6939 cur->name, node->name);
6941 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6942 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6943 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6944 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6945 "Attribute %s is not allowed on %s\n",
6946 cur->name, node->name);
6948 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6949 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6950 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6951 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6952 "Attribute %s is not allowed on %s\n",
6953 cur->name, node->name);
6955 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6959 val = xmlNodeListGetString(node->doc, cur->children, 1);
6962 uri = xmlParseURI((const char *) val);
6964 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6965 "Attribute %s contains invalid URI %s\n",
6968 if (uri->scheme == NULL) {
6969 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6970 "Attribute %s URI %s is not absolute\n",
6973 if (uri->fragment != NULL) {
6974 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6975 "Attribute %s URI %s has a fragment ID\n",
6983 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6984 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6985 "Unknown attribute %s on %s\n", cur->name,
6994 * xmlRelaxNGCleanupTree:
6995 * @ctxt: a Relax-NG parser context
6996 * @root: an xmlNodePtr subtree
6998 * Cleanup the subtree from unwanted nodes for parsing, resolve
6999 * Include and externalRef lookups.
7002 xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
7004 xmlNodePtr cur, delete;
7008 while (cur != NULL) {
7009 if (delete != NULL) {
7010 xmlUnlinkNode(delete);
7011 xmlFreeNode(delete);
7014 if (cur->type == XML_ELEMENT_NODE) {
7016 * Simplification 4.1. Annotations
7018 if ((cur->ns == NULL) ||
7019 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
7020 if ((cur->parent != NULL) &&
7021 (cur->parent->type == XML_ELEMENT_NODE) &&
7022 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
7023 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
7024 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
7025 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
7026 "element %s doesn't allow foreign elements\n",
7027 cur->parent->name, NULL);
7032 xmlRelaxNGCleanupAttributes(ctxt, cur);
7033 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
7034 xmlChar *href, *ns, *base, *URL;
7035 xmlRelaxNGDocumentPtr docu;
7039 ns = xmlGetProp(cur, BAD_CAST "ns");
7042 while ((tmp != NULL) &&
7043 (tmp->type == XML_ELEMENT_NODE)) {
7044 ns = xmlGetProp(tmp, BAD_CAST "ns");
7050 href = xmlGetProp(cur, BAD_CAST "href");
7052 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7053 "xmlRelaxNGParse: externalRef has no href attribute\n",
7060 uri = xmlParseURI((const char *) href);
7062 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7063 "Incorrect URI for externalRef %s\n",
7072 if (uri->fragment != NULL) {
7073 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7074 "Fragment forbidden in URI for externalRef %s\n",
7085 base = xmlNodeGetBase(cur->doc, cur);
7086 URL = xmlBuildURI(href, base);
7088 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7089 "Failed to compute URL for externalRef %s\n",
7104 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7106 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7107 "Failed to load externalRef %s\n", URL,
7119 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7120 xmlChar *href, *ns, *base, *URL;
7121 xmlRelaxNGIncludePtr incl;
7124 href = xmlGetProp(cur, BAD_CAST "href");
7126 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7127 "xmlRelaxNGParse: include has no href attribute\n",
7132 base = xmlNodeGetBase(cur->doc, cur);
7133 URL = xmlBuildURI(href, base);
7135 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7136 "Failed to compute URL for include %s\n",
7149 ns = xmlGetProp(cur, BAD_CAST "ns");
7152 while ((tmp != NULL) &&
7153 (tmp->type == XML_ELEMENT_NODE)) {
7154 ns = xmlGetProp(tmp, BAD_CAST "ns");
7160 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7164 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7165 "Failed to load include %s\n", URL,
7173 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7174 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7177 xmlNodePtr text = NULL;
7180 * Simplification 4.8. name attribute of element
7181 * and attribute elements
7183 name = xmlGetProp(cur, BAD_CAST "name");
7185 if (cur->children == NULL) {
7187 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7192 node = xmlNewDocNode(cur->doc, cur->ns,
7193 BAD_CAST "name", NULL);
7195 xmlAddPrevSibling(cur->children, node);
7196 text = xmlNewText(name);
7197 xmlAddChild(node, text);
7202 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7203 "Failed to create a name %s element\n",
7206 xmlUnsetProp(cur, BAD_CAST "name");
7208 ns = xmlGetProp(cur, BAD_CAST "ns");
7211 xmlSetProp(text, BAD_CAST "ns", ns);
7212 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7215 } else if (xmlStrEqual(cur->name,
7216 BAD_CAST "attribute")) {
7217 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7220 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7221 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7222 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7224 * Simplification 4.8. name attribute of element
7225 * and attribute elements
7227 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7232 while ((node != NULL) &&
7233 (node->type == XML_ELEMENT_NODE)) {
7234 ns = xmlGetProp(node, BAD_CAST "ns");
7238 node = node->parent;
7241 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7243 xmlSetProp(cur, BAD_CAST "ns", ns);
7247 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7248 xmlChar *name, *local, *prefix;
7251 * Simplification: 4.10. QNames
7253 name = xmlNodeGetContent(cur);
7255 local = xmlSplitQName2(name, &prefix);
7256 if (local != NULL) {
7259 ns = xmlSearchNs(cur->doc, cur, prefix);
7261 xmlRngPErr(ctxt, cur,
7262 XML_RNGP_PREFIX_UNDEFINED,
7263 "xmlRelaxNGParse: no namespace for prefix %s\n",
7266 xmlSetProp(cur, BAD_CAST "ns",
7268 xmlNodeSetContent(cur, local);
7279 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7280 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7281 xmlRngPErr(ctxt, cur,
7282 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7283 "Found nsName/except//nsName forbidden construct\n",
7287 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7289 int oldflags = ctxt->flags;
7294 if ((cur->parent != NULL) &&
7296 (cur->parent->name, BAD_CAST "anyName"))) {
7297 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7298 xmlRelaxNGCleanupTree(ctxt, cur);
7299 ctxt->flags = oldflags;
7301 } else if ((cur->parent != NULL) &&
7303 (cur->parent->name, BAD_CAST "nsName"))) {
7304 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7305 xmlRelaxNGCleanupTree(ctxt, cur);
7306 ctxt->flags = oldflags;
7309 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7313 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7314 xmlRngPErr(ctxt, cur,
7315 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7316 "Found anyName/except//anyName forbidden construct\n",
7318 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7319 xmlRngPErr(ctxt, cur,
7320 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7321 "Found nsName/except//anyName forbidden construct\n",
7326 * This is not an else since "include" is transformed
7329 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7331 xmlNodePtr child, ins, tmp;
7334 * implements rule 4.11
7337 ns = xmlGetProp(cur, BAD_CAST "ns");
7339 child = cur->children;
7341 while (child != NULL) {
7343 if (!xmlHasProp(child, BAD_CAST "ns")) {
7344 xmlSetProp(child, BAD_CAST "ns", ns);
7348 xmlUnlinkNode(child);
7349 ins = xmlAddNextSibling(ins, child);
7355 * Since we are about to delete cur, if its nsDef is non-NULL we
7356 * need to preserve it (it contains the ns definitions for the
7357 * children we just moved). We'll just stick it on to the end
7358 * of cur->parent's list, since it's never going to be re-serialized
7361 if ((cur->nsDef != NULL) && (cur->parent != NULL)) {
7362 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7363 while (parDef->next != NULL)
7364 parDef = parDef->next;
7365 parDef->next = cur->nsDef;
7374 * Simplification 4.2 whitespaces
7376 else if ((cur->type == XML_TEXT_NODE) ||
7377 (cur->type == XML_CDATA_SECTION_NODE)) {
7378 if (IS_BLANK_NODE(cur)) {
7379 if ((cur->parent != NULL) &&
7380 (cur->parent->type == XML_ELEMENT_NODE)) {
7381 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7384 (cur->parent->name, BAD_CAST "param")))
7399 if (cur->children != NULL) {
7400 if ((cur->children->type != XML_ENTITY_DECL) &&
7401 (cur->children->type != XML_ENTITY_REF_NODE) &&
7402 (cur->children->type != XML_ENTITY_NODE)) {
7403 cur = cur->children;
7408 if (cur->next != NULL) {
7421 if (cur->next != NULL) {
7425 } while (cur != NULL);
7427 if (delete != NULL) {
7428 xmlUnlinkNode(delete);
7429 xmlFreeNode(delete);
7435 * xmlRelaxNGCleanupDoc:
7436 * @ctxt: a Relax-NG parser context
7437 * @doc: an xmldocPtr document pointer
7439 * Cleanup the document from unwanted nodes for parsing, resolve
7440 * Include and externalRef lookups.
7442 * Returns the cleaned up document or NULL in case of error
7445 xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7452 root = xmlDocGetRootElement(doc);
7454 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7458 xmlRelaxNGCleanupTree(ctxt, root);
7464 * @ctxt: a Relax-NG parser context
7466 * parse a schema definition resource and build an internal
7467 * XML Shema struture which can be used to validate instances.
7469 * Returns the internal XML RelaxNG structure built from the resource or
7470 * NULL in case of error
7473 xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7475 xmlRelaxNGPtr ret = NULL;
7479 xmlRelaxNGInitTypes();
7485 * First step is to parse the input document into an DOM/Infoset
7487 if (ctxt->URL != NULL) {
7488 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
7490 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7491 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7495 } else if (ctxt->buffer != NULL) {
7496 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
7498 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7499 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7503 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7504 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7505 } else if (ctxt->document != NULL) {
7506 doc = ctxt->document;
7508 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7509 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7512 ctxt->document = doc;
7515 * Some preprocessing of the document content
7517 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7519 xmlFreeDoc(ctxt->document);
7520 ctxt->document = NULL;
7525 * Then do the parsing for good
7527 root = xmlDocGetRootElement(doc);
7529 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7530 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7531 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
7533 xmlFreeDoc(ctxt->document);
7534 ctxt->document = NULL;
7537 ret = xmlRelaxNGParseDocument(ctxt, root);
7539 xmlFreeDoc(ctxt->document);
7540 ctxt->document = NULL;
7545 * Check the ref/defines links
7548 * try to preprocess interleaves
7550 if (ctxt->interleaves != NULL) {
7551 xmlHashScan(ctxt->interleaves,
7552 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
7556 * if there was a parsing error return NULL
7558 if (ctxt->nbErrors > 0) {
7559 xmlRelaxNGFree(ret);
7560 ctxt->document = NULL;
7566 * try to compile (parts of) the schemas
7568 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7569 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
7570 xmlRelaxNGDefinePtr def;
7572 def = xmlRelaxNGNewDefine(ctxt, NULL);
7574 def->type = XML_RELAXNG_START;
7575 def->content = ret->topgrammar->start;
7576 ret->topgrammar->start = def;
7579 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
7583 * Transfer the pointer for cleanup at the schema level.
7586 ctxt->document = NULL;
7587 ret->documents = ctxt->documents;
7588 ctxt->documents = NULL;
7590 ret->includes = ctxt->includes;
7591 ctxt->includes = NULL;
7592 ret->defNr = ctxt->defNr;
7593 ret->defTab = ctxt->defTab;
7594 ctxt->defTab = NULL;
7595 if (ctxt->idref == 1)
7602 * xmlRelaxNGSetParserErrors:
7603 * @ctxt: a Relax-NG validation context
7604 * @err: the error callback
7605 * @warn: the warning callback
7606 * @ctx: contextual data for the callbacks
7608 * Set the callback functions used to handle errors for a validation context
7611 xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7612 xmlRelaxNGValidityErrorFunc err,
7613 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7618 ctxt->warning = warn;
7619 ctxt->serror = NULL;
7620 ctxt->userData = ctx;
7624 * xmlRelaxNGGetParserErrors:
7625 * @ctxt: a Relax-NG validation context
7626 * @err: the error callback result
7627 * @warn: the warning callback result
7628 * @ctx: contextual data for the callbacks result
7630 * Get the callback information used to handle errors for a validation context
7632 * Returns -1 in case of failure, 0 otherwise.
7635 xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7636 xmlRelaxNGValidityErrorFunc * err,
7637 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7644 *warn = ctxt->warning;
7646 *ctx = ctxt->userData;
7651 * xmlRelaxNGSetParserStructuredErrors:
7652 * @ctxt: a Relax-NG parser context
7653 * @serror: the error callback
7654 * @ctx: contextual data for the callbacks
7656 * Set the callback functions used to handle errors for a parsing context
7659 xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
7660 xmlStructuredErrorFunc serror,
7665 ctxt->serror = serror;
7667 ctxt->warning = NULL;
7668 ctxt->userData = ctx;
7671 #ifdef LIBXML_OUTPUT_ENABLED
7673 /************************************************************************
7675 * Dump back a compiled form *
7677 ************************************************************************/
7678 static void xmlRelaxNGDumpDefine(FILE * output,
7679 xmlRelaxNGDefinePtr define);
7682 * xmlRelaxNGDumpDefines:
7683 * @output: the file output
7684 * @defines: a list of define structures
7686 * Dump a RelaxNG structure back
7689 xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7691 while (defines != NULL) {
7692 xmlRelaxNGDumpDefine(output, defines);
7693 defines = defines->next;
7698 * xmlRelaxNGDumpDefine:
7699 * @output: the file output
7700 * @define: a define structure
7702 * Dump a RelaxNG structure back
7705 xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7709 switch (define->type) {
7710 case XML_RELAXNG_EMPTY:
7711 fprintf(output, "<empty/>\n");
7713 case XML_RELAXNG_NOT_ALLOWED:
7714 fprintf(output, "<notAllowed/>\n");
7716 case XML_RELAXNG_TEXT:
7717 fprintf(output, "<text/>\n");
7719 case XML_RELAXNG_ELEMENT:
7720 fprintf(output, "<element>\n");
7721 if (define->name != NULL) {
7722 fprintf(output, "<name");
7723 if (define->ns != NULL)
7724 fprintf(output, " ns=\"%s\"", define->ns);
7725 fprintf(output, ">%s</name>\n", define->name);
7727 xmlRelaxNGDumpDefines(output, define->attrs);
7728 xmlRelaxNGDumpDefines(output, define->content);
7729 fprintf(output, "</element>\n");
7731 case XML_RELAXNG_LIST:
7732 fprintf(output, "<list>\n");
7733 xmlRelaxNGDumpDefines(output, define->content);
7734 fprintf(output, "</list>\n");
7736 case XML_RELAXNG_ONEORMORE:
7737 fprintf(output, "<oneOrMore>\n");
7738 xmlRelaxNGDumpDefines(output, define->content);
7739 fprintf(output, "</oneOrMore>\n");
7741 case XML_RELAXNG_ZEROORMORE:
7742 fprintf(output, "<zeroOrMore>\n");
7743 xmlRelaxNGDumpDefines(output, define->content);
7744 fprintf(output, "</zeroOrMore>\n");
7746 case XML_RELAXNG_CHOICE:
7747 fprintf(output, "<choice>\n");
7748 xmlRelaxNGDumpDefines(output, define->content);
7749 fprintf(output, "</choice>\n");
7751 case XML_RELAXNG_GROUP:
7752 fprintf(output, "<group>\n");
7753 xmlRelaxNGDumpDefines(output, define->content);
7754 fprintf(output, "</group>\n");
7756 case XML_RELAXNG_INTERLEAVE:
7757 fprintf(output, "<interleave>\n");
7758 xmlRelaxNGDumpDefines(output, define->content);
7759 fprintf(output, "</interleave>\n");
7761 case XML_RELAXNG_OPTIONAL:
7762 fprintf(output, "<optional>\n");
7763 xmlRelaxNGDumpDefines(output, define->content);
7764 fprintf(output, "</optional>\n");
7766 case XML_RELAXNG_ATTRIBUTE:
7767 fprintf(output, "<attribute>\n");
7768 xmlRelaxNGDumpDefines(output, define->content);
7769 fprintf(output, "</attribute>\n");
7771 case XML_RELAXNG_DEF:
7772 fprintf(output, "<define");
7773 if (define->name != NULL)
7774 fprintf(output, " name=\"%s\"", define->name);
7775 fprintf(output, ">\n");
7776 xmlRelaxNGDumpDefines(output, define->content);
7777 fprintf(output, "</define>\n");
7779 case XML_RELAXNG_REF:
7780 fprintf(output, "<ref");
7781 if (define->name != NULL)
7782 fprintf(output, " name=\"%s\"", define->name);
7783 fprintf(output, ">\n");
7784 xmlRelaxNGDumpDefines(output, define->content);
7785 fprintf(output, "</ref>\n");
7787 case XML_RELAXNG_PARENTREF:
7788 fprintf(output, "<parentRef");
7789 if (define->name != NULL)
7790 fprintf(output, " name=\"%s\"", define->name);
7791 fprintf(output, ">\n");
7792 xmlRelaxNGDumpDefines(output, define->content);
7793 fprintf(output, "</parentRef>\n");
7795 case XML_RELAXNG_EXTERNALREF:
7796 fprintf(output, "<externalRef>");
7797 xmlRelaxNGDumpDefines(output, define->content);
7798 fprintf(output, "</externalRef>\n");
7800 case XML_RELAXNG_DATATYPE:
7801 case XML_RELAXNG_VALUE:
7803 case XML_RELAXNG_START:
7804 case XML_RELAXNG_EXCEPT:
7805 case XML_RELAXNG_PARAM:
7807 case XML_RELAXNG_NOOP:
7808 xmlRelaxNGDumpDefines(output, define->content);
7814 * xmlRelaxNGDumpGrammar:
7815 * @output: the file output
7816 * @grammar: a grammar structure
7817 * @top: is this a top grammar
7819 * Dump a RelaxNG structure back
7822 xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7824 if (grammar == NULL)
7827 fprintf(output, "<grammar");
7829 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7830 switch (grammar->combine) {
7831 case XML_RELAXNG_COMBINE_UNDEFINED:
7833 case XML_RELAXNG_COMBINE_CHOICE:
7834 fprintf(output, " combine=\"choice\"");
7836 case XML_RELAXNG_COMBINE_INTERLEAVE:
7837 fprintf(output, " combine=\"interleave\"");
7840 fprintf(output, " <!-- invalid combine value -->");
7842 fprintf(output, ">\n");
7843 if (grammar->start == NULL) {
7844 fprintf(output, " <!-- grammar had no start -->");
7846 fprintf(output, "<start>\n");
7847 xmlRelaxNGDumpDefine(output, grammar->start);
7848 fprintf(output, "</start>\n");
7850 /* TODO ? Dump the defines ? */
7851 fprintf(output, "</grammar>\n");
7856 * @output: the file output
7857 * @schema: a schema structure
7859 * Dump a RelaxNG structure back
7862 xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7866 if (schema == NULL) {
7867 fprintf(output, "RelaxNG empty or failed to compile\n");
7870 fprintf(output, "RelaxNG: ");
7871 if (schema->doc == NULL) {
7872 fprintf(output, "no document\n");
7873 } else if (schema->doc->URL != NULL) {
7874 fprintf(output, "%s\n", schema->doc->URL);
7876 fprintf(output, "\n");
7878 if (schema->topgrammar == NULL) {
7879 fprintf(output, "RelaxNG has no top grammar\n");
7882 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7886 * xmlRelaxNGDumpTree:
7887 * @output: the file output
7888 * @schema: a schema structure
7890 * Dump the transformed RelaxNG tree.
7893 xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7897 if (schema == NULL) {
7898 fprintf(output, "RelaxNG empty or failed to compile\n");
7901 if (schema->doc == NULL) {
7902 fprintf(output, "no document\n");
7904 xmlDocDump(output, schema->doc);
7907 #endif /* LIBXML_OUTPUT_ENABLED */
7909 /************************************************************************
7911 * Validation of compiled content *
7913 ************************************************************************/
7914 static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7915 xmlRelaxNGDefinePtr define);
7918 * xmlRelaxNGValidateCompiledCallback:
7919 * @exec: the regular expression instance
7920 * @token: the token which matched
7921 * @transdata: callback data, the define for the subelement if available
7922 @ @inputdata: callback data, the Relax NG validation context
7924 * Handle the callback and if needed validate the element children.
7927 xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7928 const xmlChar * token,
7929 void *transdata, void *inputdata)
7931 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7932 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7935 #ifdef DEBUG_COMPILE
7936 xmlGenericError(xmlGenericErrorContext,
7937 "Compiled callback for: '%s'\n", token);
7940 fprintf(stderr, "callback on %s missing context\n", token);
7943 if (define == NULL) {
7944 if (token[0] == '#')
7946 fprintf(stderr, "callback on %s missing define\n", token);
7947 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7948 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7951 if ((ctxt == NULL) || (define == NULL)) {
7952 fprintf(stderr, "callback on %s missing info\n", token);
7953 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7954 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7956 } else if (define->type != XML_RELAXNG_ELEMENT) {
7957 fprintf(stderr, "callback on %s define is not element\n", token);
7958 if (ctxt->errNo == XML_RELAXNG_OK)
7959 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7962 ret = xmlRelaxNGValidateDefinition(ctxt, define);
7968 * xmlRelaxNGValidateCompiledContent:
7969 * @ctxt: the RelaxNG validation context
7970 * @regexp: the regular expression as compiled
7971 * @content: list of children to test against the regexp
7973 * Validate the content model of an element or start using the regexp
7975 * Returns 0 in case of success, -1 in case of error.
7978 xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
7979 xmlRegexpPtr regexp, xmlNodePtr content)
7981 xmlRegExecCtxtPtr exec;
7986 if ((ctxt == NULL) || (regexp == NULL))
7988 oldperr = ctxt->perr;
7989 exec = xmlRegNewExecCtxt(regexp,
7990 xmlRelaxNGValidateCompiledCallback, ctxt);
7993 while (cur != NULL) {
7994 ctxt->state->seq = cur;
7995 switch (cur->type) {
7997 case XML_CDATA_SECTION_NODE:
7998 if (xmlIsBlankNode(cur))
8000 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
8002 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
8006 case XML_ELEMENT_NODE:
8007 if (cur->ns != NULL) {
8008 ret = xmlRegExecPushString2(exec, cur->name,
8009 cur->ns->href, ctxt);
8011 ret = xmlRegExecPushString(exec, cur->name, ctxt);
8014 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
8023 * Switch to next element
8027 ret = xmlRegExecPushString(exec, NULL, NULL);
8030 ctxt->state->seq = NULL;
8031 } else if (ret == 0) {
8033 * TODO: get some of the names needed to exit the current state of exec
8035 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8037 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8038 xmlRelaxNGDumpValidError(ctxt);
8042 xmlRegFreeExecCtxt(exec);
8044 * There might be content model errors outside of the pure
8045 * regexp validation, e.g. for attribute values.
8047 if ((ret == 0) && (ctxt->perr != 0)) {
8050 ctxt->perr = oldperr;
8054 /************************************************************************
8056 * Progressive validation of when possible *
8058 ************************************************************************/
8059 static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8060 xmlRelaxNGDefinePtr defines);
8061 static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
8063 static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
8066 * xmlRelaxNGElemPush:
8067 * @ctxt: the validation context
8068 * @exec: the regexp runtime for the new content model
8070 * Push a new regexp for the current node content model on the stack
8072 * Returns 0 in case of success and -1 in case of error.
8075 xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8077 if (ctxt->elemTab == NULL) {
8079 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8081 (xmlRegExecCtxtPtr));
8082 if (ctxt->elemTab == NULL) {
8083 xmlRngVErrMemory(ctxt, "validating\n");
8087 if (ctxt->elemNr >= ctxt->elemMax) {
8089 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
8092 (xmlRegExecCtxtPtr));
8093 if (ctxt->elemTab == NULL) {
8094 xmlRngVErrMemory(ctxt, "validating\n");
8098 ctxt->elemTab[ctxt->elemNr++] = exec;
8104 * xmlRelaxNGElemPop:
8105 * @ctxt: the validation context
8107 * Pop the regexp of the current node content model from the stack
8109 * Returns the exec or NULL if empty
8111 static xmlRegExecCtxtPtr
8112 xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8114 xmlRegExecCtxtPtr ret;
8116 if (ctxt->elemNr <= 0)
8119 ret = ctxt->elemTab[ctxt->elemNr];
8120 ctxt->elemTab[ctxt->elemNr] = NULL;
8121 if (ctxt->elemNr > 0)
8122 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8129 * xmlRelaxNGValidateProgressiveCallback:
8130 * @exec: the regular expression instance
8131 * @token: the token which matched
8132 * @transdata: callback data, the define for the subelement if available
8133 @ @inputdata: callback data, the Relax NG validation context
8135 * Handle the callback and if needed validate the element children.
8136 * some of the in/out informations are passed via the context in @inputdata.
8139 xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8141 const xmlChar * token,
8142 void *transdata, void *inputdata)
8144 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8145 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
8146 xmlRelaxNGValidStatePtr state, oldstate;
8148 int ret = 0, oldflags;
8150 #ifdef DEBUG_PROGRESSIVE
8151 xmlGenericError(xmlGenericErrorContext,
8152 "Progressive callback for: '%s'\n", token);
8155 fprintf(stderr, "callback on %s missing context\n", token);
8160 if (define == NULL) {
8161 if (token[0] == '#')
8163 fprintf(stderr, "callback on %s missing define\n", token);
8164 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8165 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8169 if ((ctxt == NULL) || (define == NULL)) {
8170 fprintf(stderr, "callback on %s missing info\n", token);
8171 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8172 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8175 } else if (define->type != XML_RELAXNG_ELEMENT) {
8176 fprintf(stderr, "callback on %s define is not element\n", token);
8177 if (ctxt->errNo == XML_RELAXNG_OK)
8178 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8182 if (node->type != XML_ELEMENT_NODE) {
8183 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8184 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8185 xmlRelaxNGDumpValidError(ctxt);
8189 if (define->contModel == NULL) {
8191 * this node cannot be validated in a streamable fashion
8193 #ifdef DEBUG_PROGRESSIVE
8194 xmlGenericError(xmlGenericErrorContext,
8195 "Element '%s' validation is not streamable\n",
8199 ctxt->pdef = define;
8202 exec = xmlRegNewExecCtxt(define->contModel,
8203 xmlRelaxNGValidateProgressiveCallback, ctxt);
8208 xmlRelaxNGElemPush(ctxt, exec);
8211 * Validate the attributes part of the content.
8213 state = xmlRelaxNGNewValidState(ctxt, node);
8214 if (state == NULL) {
8218 oldstate = ctxt->state;
8219 ctxt->state = state;
8220 if (define->attrs != NULL) {
8221 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8224 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8227 if (ctxt->state != NULL) {
8228 ctxt->state->seq = NULL;
8229 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8233 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
8234 } else if (ctxt->states != NULL) {
8237 oldflags = ctxt->flags;
8239 for (i = 0; i < ctxt->states->nbState; i++) {
8240 state = ctxt->states->tabState[i];
8241 ctxt->state = state;
8242 ctxt->state->seq = NULL;
8244 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8251 * validation error, log the message for the "best" one
8253 ctxt->flags |= FLAGS_IGNORABLE;
8254 xmlRelaxNGLogBestError(ctxt);
8256 for (i = 0; i < ctxt->states->nbState; i++) {
8257 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8259 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8260 ctxt->states = NULL;
8261 if ((ret == 0) && (tmp == -1))
8263 ctxt->flags = oldflags;
8265 if (ctxt->pstate == -1) {
8266 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8267 xmlRelaxNGDumpValidError(ctxt);
8270 ctxt->state = oldstate;
8274 * xmlRelaxNGValidatePushElement:
8275 * @ctxt: the validation context
8276 * @doc: a document instance
8277 * @elem: an element instance
8279 * Push a new element start on the RelaxNG validation stack.
8281 * returns 1 if no validation problem was found or 0 if validating the
8282 * element requires a full node, and -1 in case of error.
8285 xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8286 xmlDocPtr doc ATTRIBUTE_UNUSED,
8291 if ((ctxt == NULL) || (elem == NULL))
8294 #ifdef DEBUG_PROGRESSIVE
8295 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8297 if (ctxt->elem == 0) {
8298 xmlRelaxNGPtr schema;
8299 xmlRelaxNGGrammarPtr grammar;
8300 xmlRegExecCtxtPtr exec;
8301 xmlRelaxNGDefinePtr define;
8303 schema = ctxt->schema;
8304 if (schema == NULL) {
8305 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8308 grammar = schema->topgrammar;
8309 if ((grammar == NULL) || (grammar->start == NULL)) {
8310 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8313 define = grammar->start;
8314 if (define->contModel == NULL) {
8315 ctxt->pdef = define;
8318 exec = xmlRegNewExecCtxt(define->contModel,
8319 xmlRelaxNGValidateProgressiveCallback,
8324 xmlRelaxNGElemPush(ctxt, exec);
8328 if (elem->ns != NULL) {
8330 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8333 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8336 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8338 if (ctxt->pstate == 0)
8340 else if (ctxt->pstate < 0)
8345 #ifdef DEBUG_PROGRESSIVE
8347 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8354 * xmlRelaxNGValidatePushCData:
8355 * @ctxt: the RelaxNG validation context
8356 * @data: some character data read
8357 * @len: the length of the data
8359 * check the CData parsed for validation in the current stack
8361 * returns 1 if no validation problem was found or -1 otherwise
8364 xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
8365 const xmlChar * data, int len ATTRIBUTE_UNUSED)
8369 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8372 #ifdef DEBUG_PROGRESSIVE
8373 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8376 while (*data != 0) {
8377 if (!IS_BLANK_CH(*data))
8384 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8386 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
8387 #ifdef DEBUG_PROGRESSIVE
8388 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
8397 * xmlRelaxNGValidatePopElement:
8398 * @ctxt: the RelaxNG validation context
8399 * @doc: a document instance
8400 * @elem: an element instance
8402 * Pop the element end from the RelaxNG validation stack.
8404 * returns 1 if no validation problem was found or 0 otherwise
8407 xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8408 xmlDocPtr doc ATTRIBUTE_UNUSED,
8412 xmlRegExecCtxtPtr exec;
8414 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8416 #ifdef DEBUG_PROGRESSIVE
8417 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8420 * verify that we reached a terminal state of the content model.
8422 exec = xmlRelaxNGElemPop(ctxt);
8423 ret = xmlRegExecPushString(exec, NULL, NULL);
8426 * TODO: get some of the names needed to exit the current state of exec
8428 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8430 } else if (ret < 0) {
8435 xmlRegFreeExecCtxt(exec);
8436 #ifdef DEBUG_PROGRESSIVE
8438 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8445 * xmlRelaxNGValidateFullElement:
8446 * @ctxt: the validation context
8447 * @doc: a document instance
8448 * @elem: an element instance
8450 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8451 * 0 and the content of the node has been expanded.
8453 * returns 1 if no validation problem was found or -1 in case of error.
8456 xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8457 xmlDocPtr doc ATTRIBUTE_UNUSED,
8461 xmlRelaxNGValidStatePtr state;
8463 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8465 #ifdef DEBUG_PROGRESSIVE
8466 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8468 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8469 if (state == NULL) {
8473 ctxt->state = state;
8474 ctxt->errNo = XML_RELAXNG_OK;
8475 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
8476 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8480 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
8482 #ifdef DEBUG_PROGRESSIVE
8484 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8490 /************************************************************************
8492 * Generic interpreted validation implementation *
8494 ************************************************************************/
8495 static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8496 xmlRelaxNGDefinePtr define);
8499 * xmlRelaxNGSkipIgnored:
8500 * @ctxt: a schema validation context
8501 * @node: the top node.
8503 * Skip ignorable nodes in that context
8505 * Returns the new sibling or NULL in case of error.
8508 xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
8512 * TODO complete and handle entities
8514 while ((node != NULL) &&
8515 ((node->type == XML_COMMENT_NODE) ||
8516 (node->type == XML_PI_NODE) ||
8517 (node->type == XML_XINCLUDE_START) ||
8518 (node->type == XML_XINCLUDE_END) ||
8519 (((node->type == XML_TEXT_NODE) ||
8520 (node->type == XML_CDATA_SECTION_NODE)) &&
8521 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8522 (IS_BLANK_NODE(node)))))) {
8529 * xmlRelaxNGNormalize:
8530 * @ctxt: a schema validation context
8531 * @str: the string to normalize
8533 * Implements the normalizeWhiteSpace( s ) function from
8534 * section 6.2.9 of the spec
8536 * Returns the new string or NULL in case of error.
8539 xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8552 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
8554 xmlRngVErrMemory(ctxt, "validating\n");
8558 while (IS_BLANK_CH(*str))
8561 if (IS_BLANK_CH(*str)) {
8562 while (IS_BLANK_CH(*str))
8575 * xmlRelaxNGValidateDatatype:
8576 * @ctxt: a Relax-NG validation context
8577 * @value: the string value
8578 * @type: the datatype definition
8581 * Validate the given value against the dataype
8583 * Returns 0 if the validation succeeded or an error code.
8586 xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8587 const xmlChar * value,
8588 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8591 xmlRelaxNGTypeLibraryPtr lib;
8592 void *result = NULL;
8593 xmlRelaxNGDefinePtr cur;
8595 if ((define == NULL) || (define->data == NULL)) {
8598 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8599 if (lib->check != NULL) {
8600 if ((define->attrs != NULL) &&
8601 (define->attrs->type == XML_RELAXNG_PARAM)) {
8603 lib->check(lib->data, define->name, value, &result, node);
8605 ret = lib->check(lib->data, define->name, value, NULL, node);
8610 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8611 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8612 lib->freef(lib->data, result);
8614 } else if (ret == 1) {
8616 } else if (ret == 2) {
8617 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
8619 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8622 cur = define->attrs;
8623 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
8624 if (lib->facet != NULL) {
8625 tmp = lib->facet(lib->data, define->name, cur->name,
8626 cur->value, value, result);
8632 if ((ret == 0) && (define->content != NULL)) {
8633 const xmlChar *oldvalue, *oldendvalue;
8635 oldvalue = ctxt->state->value;
8636 oldendvalue = ctxt->state->endvalue;
8637 ctxt->state->value = (xmlChar *) value;
8638 ctxt->state->endvalue = NULL;
8639 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8640 ctxt->state->value = (xmlChar *) oldvalue;
8641 ctxt->state->endvalue = (xmlChar *) oldendvalue;
8643 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8644 lib->freef(lib->data, result);
8649 * xmlRelaxNGNextValue:
8650 * @ctxt: a Relax-NG validation context
8652 * Skip to the next value when validating within a list
8654 * Returns 0 if the operation succeeded or an error code.
8657 xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8661 cur = ctxt->state->value;
8662 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
8663 ctxt->state->value = NULL;
8664 ctxt->state->endvalue = NULL;
8669 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8671 if (cur == ctxt->state->endvalue)
8672 ctxt->state->value = NULL;
8674 ctxt->state->value = cur;
8679 * xmlRelaxNGValidateValueList:
8680 * @ctxt: a Relax-NG validation context
8681 * @defines: the list of definitions to verify
8683 * Validate the given set of definitions for the current value
8685 * Returns 0 if the validation succeeded or an error code.
8688 xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8689 xmlRelaxNGDefinePtr defines)
8693 while (defines != NULL) {
8694 ret = xmlRelaxNGValidateValue(ctxt, defines);
8697 defines = defines->next;
8703 * xmlRelaxNGValidateValue:
8704 * @ctxt: a Relax-NG validation context
8705 * @define: the definition to verify
8707 * Validate the given definition for the current value
8709 * Returns 0 if the validation succeeded or an error code.
8712 xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8713 xmlRelaxNGDefinePtr define)
8715 int ret = 0, oldflags;
8718 value = ctxt->state->value;
8719 switch (define->type) {
8720 case XML_RELAXNG_EMPTY:{
8721 if ((value != NULL) && (value[0] != 0)) {
8724 while (IS_BLANK_CH(value[idx]))
8726 if (value[idx] != 0)
8731 case XML_RELAXNG_TEXT:
8733 case XML_RELAXNG_VALUE:{
8734 if (!xmlStrEqual(value, define->value)) {
8735 if (define->name != NULL) {
8736 xmlRelaxNGTypeLibraryPtr lib;
8738 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8739 if ((lib != NULL) && (lib->comp != NULL)) {
8740 ret = lib->comp(lib->data, define->name,
8741 define->value, define->node,
8742 (void *) define->attrs,
8743 value, ctxt->state->node);
8747 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8750 } else if (ret == 1) {
8756 xmlChar *nval, *nvalue;
8759 * TODO: trivial optimizations are possible by
8760 * computing at compile-time
8762 nval = xmlRelaxNGNormalize(ctxt, define->value);
8763 nvalue = xmlRelaxNGNormalize(ctxt, value);
8765 if ((nval == NULL) || (nvalue == NULL) ||
8766 (!xmlStrEqual(nval, nvalue)))
8775 xmlRelaxNGNextValue(ctxt);
8778 case XML_RELAXNG_DATATYPE:{
8779 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8782 xmlRelaxNGNextValue(ctxt);
8786 case XML_RELAXNG_CHOICE:{
8787 xmlRelaxNGDefinePtr list = define->content;
8790 oldflags = ctxt->flags;
8791 ctxt->flags |= FLAGS_IGNORABLE;
8793 oldvalue = ctxt->state->value;
8794 while (list != NULL) {
8795 ret = xmlRelaxNGValidateValue(ctxt, list);
8799 ctxt->state->value = oldvalue;
8802 ctxt->flags = oldflags;
8804 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8805 xmlRelaxNGDumpValidError(ctxt);
8807 if (ctxt->errNr > 0)
8808 xmlRelaxNGPopErrors(ctxt, 0);
8812 case XML_RELAXNG_LIST:{
8813 xmlRelaxNGDefinePtr list = define->content;
8814 xmlChar *oldvalue, *oldend, *val, *cur;
8820 oldvalue = ctxt->state->value;
8821 oldend = ctxt->state->endvalue;
8823 val = xmlStrdup(oldvalue);
8825 val = xmlStrdup(BAD_CAST "");
8828 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8833 if (IS_BLANK_CH(*cur)) {
8839 while (IS_BLANK_CH(*cur))
8845 xmlGenericError(xmlGenericErrorContext,
8846 "list value: '%s' found %d items\n",
8847 oldvalue, nb_values);
8850 ctxt->state->endvalue = cur;
8852 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8855 ctxt->state->value = cur;
8857 while (list != NULL) {
8858 if (ctxt->state->value == ctxt->state->endvalue)
8859 ctxt->state->value = NULL;
8860 ret = xmlRelaxNGValidateValue(ctxt, list);
8863 xmlGenericError(xmlGenericErrorContext,
8864 "Failed to validate value: '%s' with %d rule\n",
8865 ctxt->state->value, nb_values);
8875 if ((ret == 0) && (ctxt->state->value != NULL) &&
8876 (ctxt->state->value != ctxt->state->endvalue)) {
8877 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8878 ctxt->state->value);
8882 ctxt->state->value = oldvalue;
8883 ctxt->state->endvalue = oldend;
8886 case XML_RELAXNG_ONEORMORE:
8887 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8891 /* no break on purpose */
8892 case XML_RELAXNG_ZEROORMORE:{
8893 xmlChar *cur, *temp;
8895 if ((ctxt->state->value == NULL) ||
8896 (*ctxt->state->value == 0)) {
8900 oldflags = ctxt->flags;
8901 ctxt->flags |= FLAGS_IGNORABLE;
8902 cur = ctxt->state->value;
8904 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8908 xmlRelaxNGValidateValueList(ctxt, define->content);
8910 ctxt->state->value = temp;
8914 cur = ctxt->state->value;
8916 ctxt->flags = oldflags;
8917 if (ctxt->errNr > 0)
8918 xmlRelaxNGPopErrors(ctxt, 0);
8921 case XML_RELAXNG_OPTIONAL:{
8924 if ((ctxt->state->value == NULL) ||
8925 (*ctxt->state->value == 0)) {
8929 oldflags = ctxt->flags;
8930 ctxt->flags |= FLAGS_IGNORABLE;
8931 temp = ctxt->state->value;
8932 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8933 ctxt->flags = oldflags;
8935 ctxt->state->value = temp;
8936 if (ctxt->errNr > 0)
8937 xmlRelaxNGPopErrors(ctxt, 0);
8941 if (ctxt->errNr > 0)
8942 xmlRelaxNGPopErrors(ctxt, 0);
8945 case XML_RELAXNG_EXCEPT:{
8946 xmlRelaxNGDefinePtr list;
8948 list = define->content;
8949 while (list != NULL) {
8950 ret = xmlRelaxNGValidateValue(ctxt, list);
8960 case XML_RELAXNG_DEF:
8961 case XML_RELAXNG_GROUP:{
8962 xmlRelaxNGDefinePtr list;
8964 list = define->content;
8965 while (list != NULL) {
8966 ret = xmlRelaxNGValidateValue(ctxt, list);
8976 case XML_RELAXNG_REF:
8977 case XML_RELAXNG_PARENTREF:
8978 if (define->content == NULL) {
8979 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8982 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8992 * xmlRelaxNGValidateValueContent:
8993 * @ctxt: a Relax-NG validation context
8994 * @defines: the list of definitions to verify
8996 * Validate the given definitions for the current value
8998 * Returns 0 if the validation succeeded or an error code.
9001 xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
9002 xmlRelaxNGDefinePtr defines)
9006 while (defines != NULL) {
9007 ret = xmlRelaxNGValidateValue(ctxt, defines);
9010 defines = defines->next;
9016 * xmlRelaxNGAttributeMatch:
9017 * @ctxt: a Relax-NG validation context
9018 * @define: the definition to check
9019 * @prop: the attribute
9021 * Check if the attribute matches the definition nameClass
9023 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
9026 xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
9027 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
9031 if (define->name != NULL) {
9032 if (!xmlStrEqual(define->name, prop->name))
9035 if (define->ns != NULL) {
9036 if (define->ns[0] == 0) {
9037 if (prop->ns != NULL)
9040 if ((prop->ns == NULL) ||
9041 (!xmlStrEqual(define->ns, prop->ns->href)))
9045 if (define->nameClass == NULL)
9047 define = define->nameClass;
9048 if (define->type == XML_RELAXNG_EXCEPT) {
9049 xmlRelaxNGDefinePtr list;
9051 list = define->content;
9052 while (list != NULL) {
9053 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9060 } else if (define->type == XML_RELAXNG_CHOICE) {
9061 xmlRelaxNGDefinePtr list;
9063 list = define->nameClass;
9064 while (list != NULL) {
9065 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9079 * xmlRelaxNGValidateAttribute:
9080 * @ctxt: a Relax-NG validation context
9081 * @define: the definition to verify
9083 * Validate the given attribute definition for that node
9085 * Returns 0 if the validation succeeded or an error code.
9088 xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9089 xmlRelaxNGDefinePtr define)
9092 xmlChar *value, *oldvalue;
9093 xmlAttrPtr prop = NULL, tmp;
9096 if (ctxt->state->nbAttrLeft <= 0)
9098 if (define->name != NULL) {
9099 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9100 tmp = ctxt->state->attrs[i];
9101 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9102 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9103 (tmp->ns == NULL)) ||
9104 ((tmp->ns != NULL) &&
9105 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9112 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9113 oldvalue = ctxt->state->value;
9114 oldseq = ctxt->state->seq;
9115 ctxt->state->seq = (xmlNodePtr) prop;
9116 ctxt->state->value = value;
9117 ctxt->state->endvalue = NULL;
9118 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9119 if (ctxt->state->value != NULL)
9120 value = ctxt->state->value;
9123 ctxt->state->value = oldvalue;
9124 ctxt->state->seq = oldseq;
9127 * flag the attribute as processed
9129 ctxt->state->attrs[i] = NULL;
9130 ctxt->state->nbAttrLeft--;
9136 xmlGenericError(xmlGenericErrorContext,
9137 "xmlRelaxNGValidateAttribute(%s): %d\n",
9141 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9142 tmp = ctxt->state->attrs[i];
9143 if ((tmp != NULL) &&
9144 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9150 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9151 oldvalue = ctxt->state->value;
9152 oldseq = ctxt->state->seq;
9153 ctxt->state->seq = (xmlNodePtr) prop;
9154 ctxt->state->value = value;
9155 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9156 if (ctxt->state->value != NULL)
9157 value = ctxt->state->value;
9160 ctxt->state->value = oldvalue;
9161 ctxt->state->seq = oldseq;
9164 * flag the attribute as processed
9166 ctxt->state->attrs[i] = NULL;
9167 ctxt->state->nbAttrLeft--;
9173 if (define->ns != NULL) {
9174 xmlGenericError(xmlGenericErrorContext,
9175 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9178 xmlGenericError(xmlGenericErrorContext,
9179 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9189 * xmlRelaxNGValidateAttributeList:
9190 * @ctxt: a Relax-NG validation context
9191 * @define: the list of definition to verify
9193 * Validate the given node against the list of attribute definitions
9195 * Returns 0 if the validation succeeded or an error code.
9198 xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9199 xmlRelaxNGDefinePtr defines)
9203 xmlRelaxNGDefinePtr cur;
9206 while (cur != NULL) {
9207 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
9208 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9217 while (cur != NULL) {
9218 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
9219 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9220 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9224 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9227 if (res == -1) /* continues on -2 */
9237 * xmlRelaxNGNodeMatchesList:
9239 * @list: a NULL terminated array of definitions
9241 * Check if a node can be matched by one of the definitions
9243 * Returns 1 if matches 0 otherwise
9246 xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9248 xmlRelaxNGDefinePtr cur;
9251 if ((node == NULL) || (list == NULL))
9255 while (cur != NULL) {
9256 if ((node->type == XML_ELEMENT_NODE) &&
9257 (cur->type == XML_RELAXNG_ELEMENT)) {
9258 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9261 } else if (((node->type == XML_TEXT_NODE) ||
9262 (node->type == XML_CDATA_SECTION_NODE)) &&
9263 (cur->type == XML_RELAXNG_TEXT)) {
9272 * xmlRelaxNGValidateInterleave:
9273 * @ctxt: a Relax-NG validation context
9274 * @define: the definition to verify
9276 * Validate an interleave definition for a node.
9278 * Returns 0 if the validation succeeded or an error code.
9281 xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9282 xmlRelaxNGDefinePtr define)
9284 int ret = 0, i, nbgroups;
9285 int errNr = ctxt->errNr;
9288 xmlRelaxNGValidStatePtr oldstate;
9289 xmlRelaxNGPartitionPtr partitions;
9290 xmlRelaxNGInterleaveGroupPtr group = NULL;
9291 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9292 xmlNodePtr *list = NULL, *lasts = NULL;
9294 if (define->data != NULL) {
9295 partitions = (xmlRelaxNGPartitionPtr) define->data;
9296 nbgroups = partitions->nbgroups;
9298 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9302 * Optimizations for MIXED
9304 oldflags = ctxt->flags;
9305 if (define->dflags & IS_MIXED) {
9306 ctxt->flags |= FLAGS_MIXED_CONTENT;
9307 if (nbgroups == 2) {
9309 * this is a pure <mixed> case
9311 if (ctxt->state != NULL)
9312 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9314 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9315 ret = xmlRelaxNGValidateDefinition(ctxt,
9316 partitions->groups[1]->
9319 ret = xmlRelaxNGValidateDefinition(ctxt,
9320 partitions->groups[0]->
9323 if (ctxt->state != NULL)
9324 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9328 ctxt->flags = oldflags;
9334 * Build arrays to store the first and last node of the chain
9335 * pertaining to each group
9337 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9339 xmlRngVErrMemory(ctxt, "validating\n");
9342 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9343 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9344 if (lasts == NULL) {
9345 xmlRngVErrMemory(ctxt, "validating\n");
9348 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9351 * Walk the sequence of children finding the right group and
9352 * sorting them in sequences.
9354 cur = ctxt->state->seq;
9355 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9357 while (cur != NULL) {
9358 ctxt->state->seq = cur;
9359 if ((partitions->triage != NULL) &&
9360 (partitions->flags & IS_DETERMINIST)) {
9363 if ((cur->type == XML_TEXT_NODE) ||
9364 (cur->type == XML_CDATA_SECTION_NODE)) {
9365 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9367 } else if (cur->type == XML_ELEMENT_NODE) {
9368 if (cur->ns != NULL) {
9369 tmp = xmlHashLookup2(partitions->triage, cur->name,
9372 tmp = xmlHashLookup2(partitions->triage,
9377 xmlHashLookup2(partitions->triage, cur->name,
9381 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9388 i = ((long) tmp) - 1;
9389 if (partitions->flags & IS_NEEDCHECK) {
9390 group = partitions->groups[i];
9391 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9396 for (i = 0; i < nbgroups; i++) {
9397 group = partitions->groups[i];
9400 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9405 * We break as soon as an element not matched is found
9407 if (i >= nbgroups) {
9410 if (lasts[i] != NULL) {
9411 lasts[i]->next = cur;
9417 if (cur->next != NULL)
9418 lastchg = cur->next;
9421 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
9424 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9429 oldstate = ctxt->state;
9430 for (i = 0; i < nbgroups; i++) {
9431 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9432 if (ctxt->state == NULL) {
9436 group = partitions->groups[i];
9437 if (lasts[i] != NULL) {
9438 last = lasts[i]->next;
9439 lasts[i]->next = NULL;
9441 ctxt->state->seq = list[i];
9442 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9445 if (ctxt->state != NULL) {
9446 cur = ctxt->state->seq;
9447 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9448 xmlRelaxNGFreeValidState(ctxt, oldstate);
9449 oldstate = ctxt->state;
9452 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9454 ctxt->state = oldstate;
9457 } else if (ctxt->states != NULL) {
9464 * PBM: what happen if there is attributes checks in the interleaves
9467 for (j = 0; j < ctxt->states->nbState; j++) {
9468 cur = ctxt->states->tabState[j]->seq;
9469 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9472 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9476 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9477 /* try to keep the latest one to mach old heuristic */
9478 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9483 } else if (found == 0) {
9484 if (lowattr == -1) {
9485 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9488 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9489 /* try to keep the latest one to mach old heuristic */
9490 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9496 * BIG PBM: here we pick only one restarting point :-(
9498 if (ctxt->states->nbState > 0) {
9499 xmlRelaxNGFreeValidState(ctxt, oldstate);
9501 oldstate = ctxt->states->tabState[best];
9502 ctxt->states->tabState[best] = NULL;
9505 ctxt->states->tabState[ctxt->states->nbState - 1];
9506 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
9507 ctxt->states->nbState--;
9510 for (j = 0; j < ctxt->states->nbState ; j++) {
9511 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9513 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9514 ctxt->states = NULL;
9517 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA,
9518 (const xmlChar *) "noname");
9520 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9523 ctxt->state = oldstate;
9530 if (lasts[i] != NULL) {
9531 lasts[i]->next = last;
9534 if (ctxt->state != NULL)
9535 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9536 ctxt->state = oldstate;
9537 ctxt->state->seq = lastelem;
9539 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9545 ctxt->flags = oldflags;
9547 * builds the next links chain from the prev one
9550 while (cur != NULL) {
9551 if ((cur == start) || (cur->prev == NULL))
9553 cur->prev->next = cur;
9557 if (ctxt->errNr > errNr)
9558 xmlRelaxNGPopErrors(ctxt, errNr);
9567 * xmlRelaxNGValidateDefinitionList:
9568 * @ctxt: a Relax-NG validation context
9569 * @define: the list of definition to verify
9571 * Validate the given node content against the (list) of definitions
9573 * Returns 0 if the validation succeeded or an error code.
9576 xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9577 xmlRelaxNGDefinePtr defines)
9582 if (defines == NULL) {
9583 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9584 BAD_CAST "NULL definition list");
9587 while (defines != NULL) {
9588 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9589 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9593 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9596 if (res == -1) /* continues on -2 */
9598 defines = defines->next;
9605 * xmlRelaxNGElementMatch:
9606 * @ctxt: a Relax-NG validation context
9607 * @define: the definition to check
9608 * @elem: the element
9610 * Check if the element matches the definition nameClass
9612 * Returns 1 if the element matches, 0 if no, or -1 in case of error
9615 xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9616 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9618 int ret = 0, oldflags = 0;
9620 if (define->name != NULL) {
9621 if (!xmlStrEqual(elem->name, define->name)) {
9622 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9626 if ((define->ns != NULL) && (define->ns[0] != 0)) {
9627 if (elem->ns == NULL) {
9628 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9630 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9631 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9632 elem->name, define->ns);
9635 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
9636 (define->name == NULL)) {
9637 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9639 } else if ((elem->ns != NULL) && (define->name != NULL)) {
9640 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9644 if (define->nameClass == NULL)
9647 define = define->nameClass;
9648 if (define->type == XML_RELAXNG_EXCEPT) {
9649 xmlRelaxNGDefinePtr list;
9652 oldflags = ctxt->flags;
9653 ctxt->flags |= FLAGS_IGNORABLE;
9656 list = define->content;
9657 while (list != NULL) {
9658 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9661 ctxt->flags = oldflags;
9666 ctxt->flags = oldflags;
9673 ctxt->flags = oldflags;
9675 } else if (define->type == XML_RELAXNG_CHOICE) {
9676 xmlRelaxNGDefinePtr list;
9679 oldflags = ctxt->flags;
9680 ctxt->flags |= FLAGS_IGNORABLE;
9683 list = define->nameClass;
9684 while (list != NULL) {
9685 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9688 ctxt->flags = oldflags;
9693 ctxt->flags = oldflags;
9700 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9701 xmlRelaxNGDumpValidError(ctxt);
9703 if (ctxt->errNr > 0)
9704 xmlRelaxNGPopErrors(ctxt, 0);
9709 ctxt->flags = oldflags;
9718 * xmlRelaxNGBestState:
9719 * @ctxt: a Relax-NG validation context
9721 * Find the "best" state in the ctxt->states list of states to report
9722 * errors about. I.e. a state with no element left in the child list
9723 * or the one with the less attributes left.
9724 * This is called only if a falidation error was detected
9726 * Returns the index of the "best" state or -1 in case of error
9729 xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9731 xmlRelaxNGValidStatePtr state;
9734 int value = 1000000;
9736 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9737 (ctxt->states->nbState <= 0))
9740 for (i = 0; i < ctxt->states->nbState; i++) {
9741 state = ctxt->states->tabState[i];
9744 if (state->seq != NULL) {
9745 if ((best == -1) || (value > 100000)) {
9750 tmp = state->nbAttrLeft;
9751 if ((best == -1) || (value > tmp)) {
9761 * xmlRelaxNGLogBestError:
9762 * @ctxt: a Relax-NG validation context
9764 * Find the "best" state in the ctxt->states list of states to report
9765 * errors about and log it.
9768 xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9772 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9773 (ctxt->states->nbState <= 0))
9776 best = xmlRelaxNGBestState(ctxt);
9777 if ((best >= 0) && (best < ctxt->states->nbState)) {
9778 ctxt->state = ctxt->states->tabState[best];
9780 xmlRelaxNGValidateElementEnd(ctxt, 1);
9785 * xmlRelaxNGValidateElementEnd:
9786 * @ctxt: a Relax-NG validation context
9787 * @dolog: indicate that error logging should be done
9789 * Validate the end of the element, implements check that
9790 * there is nothing left not consumed in the element content
9791 * or in the attribute list.
9793 * Returns 0 if the validation succeeded or an error code.
9796 xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
9799 xmlRelaxNGValidStatePtr state;
9801 state = ctxt->state;
9802 if (state->seq != NULL) {
9803 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9804 if (state->seq != NULL) {
9806 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9807 state->node->name, state->seq->name);
9812 for (i = 0; i < state->nbAttrs; i++) {
9813 if (state->attrs[i] != NULL) {
9815 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9816 state->attrs[i]->name, state->node->name);
9825 * xmlRelaxNGValidateState:
9826 * @ctxt: a Relax-NG validation context
9827 * @define: the definition to verify
9829 * Validate the current state against the definition
9831 * Returns 0 if the validation succeeded or an error code.
9834 xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9835 xmlRelaxNGDefinePtr define)
9838 int ret = 0, i, tmp, oldflags, errNr;
9839 xmlRelaxNGValidStatePtr oldstate = NULL, state;
9841 if (define == NULL) {
9842 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9846 if (ctxt->state != NULL) {
9847 node = ctxt->state->seq;
9852 for (i = 0; i < ctxt->depth; i++)
9853 xmlGenericError(xmlGenericErrorContext, " ");
9854 xmlGenericError(xmlGenericErrorContext,
9855 "Start validating %s ", xmlRelaxNGDefName(define));
9856 if (define->name != NULL)
9857 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
9858 if ((node != NULL) && (node->name != NULL))
9859 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
9861 xmlGenericError(xmlGenericErrorContext, "\n");
9864 switch (define->type) {
9865 case XML_RELAXNG_EMPTY:
9866 xmlRelaxNGSkipIgnored(ctxt, node);
9869 case XML_RELAXNG_NOT_ALLOWED:
9872 case XML_RELAXNG_TEXT:
9873 while ((node != NULL) &&
9874 ((node->type == XML_TEXT_NODE) ||
9875 (node->type == XML_COMMENT_NODE) ||
9876 (node->type == XML_PI_NODE) ||
9877 (node->type == XML_CDATA_SECTION_NODE)))
9879 ctxt->state->seq = node;
9881 case XML_RELAXNG_ELEMENT:
9882 errNr = ctxt->errNr;
9883 node = xmlRelaxNGSkipIgnored(ctxt, node);
9885 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9887 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9888 xmlRelaxNGDumpValidError(ctxt);
9891 if (node->type != XML_ELEMENT_NODE) {
9892 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9894 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9895 xmlRelaxNGDumpValidError(ctxt);
9899 * This node was already validated successfully against
9902 if (node->psvi == define) {
9903 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9904 if (ctxt->errNr > errNr)
9905 xmlRelaxNGPopErrors(ctxt, errNr);
9906 if (ctxt->errNr != 0) {
9907 while ((ctxt->err != NULL) &&
9908 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9909 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9912 XML_RELAXNG_ERR_ELEMEXTRANS)
9913 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9914 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9915 || (ctxt->err->err ==
9916 XML_RELAXNG_ERR_NOTELEM)))
9917 xmlRelaxNGValidErrorPop(ctxt);
9922 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9925 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9926 xmlRelaxNGDumpValidError(ctxt);
9930 if (ctxt->errNr != 0) {
9931 if (ctxt->errNr > errNr)
9932 xmlRelaxNGPopErrors(ctxt, errNr);
9933 while ((ctxt->err != NULL) &&
9934 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9935 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9936 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9937 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9938 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9939 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9940 xmlRelaxNGValidErrorPop(ctxt);
9942 errNr = ctxt->errNr;
9944 oldflags = ctxt->flags;
9945 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9946 ctxt->flags -= FLAGS_MIXED_CONTENT;
9948 state = xmlRelaxNGNewValidState(ctxt, node);
9949 if (state == NULL) {
9951 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9952 xmlRelaxNGDumpValidError(ctxt);
9956 oldstate = ctxt->state;
9957 ctxt->state = state;
9958 if (define->attrs != NULL) {
9959 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9962 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9965 if (define->contModel != NULL) {
9966 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9967 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9970 nstate = xmlRelaxNGNewValidState(ctxt, node);
9971 ctxt->state = nstate;
9972 ctxt->states = NULL;
9974 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9977 nseq = ctxt->state->seq;
9978 ctxt->state = tmpstate;
9979 ctxt->states = tmpstates;
9980 xmlRelaxNGFreeValidState(ctxt, nstate);
9982 #ifdef DEBUG_COMPILE
9983 xmlGenericError(xmlGenericErrorContext,
9984 "Validating content of '%s' : %d\n",
9990 if (ctxt->states != NULL) {
9993 for (i = 0; i < ctxt->states->nbState; i++) {
9994 state = ctxt->states->tabState[i];
9995 ctxt->state = state;
9996 ctxt->state->seq = nseq;
9998 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
10005 * validation error, log the message for the "best" one
10007 ctxt->flags |= FLAGS_IGNORABLE;
10008 xmlRelaxNGLogBestError(ctxt);
10010 for (i = 0; i < ctxt->states->nbState; i++) {
10011 xmlRelaxNGFreeValidState(ctxt,
10015 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10016 ctxt->flags = oldflags;
10017 ctxt->states = NULL;
10018 if ((ret == 0) && (tmp == -1))
10021 state = ctxt->state;
10022 if (ctxt->state != NULL)
10023 ctxt->state->seq = nseq;
10025 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
10026 xmlRelaxNGFreeValidState(ctxt, state);
10029 if (define->content != NULL) {
10030 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
10035 if (ctxt->state == NULL) {
10036 ctxt->state = oldstate;
10037 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10039 ctxt->state = NULL;
10041 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10047 if (ctxt->states != NULL) {
10050 for (i = 0; i < ctxt->states->nbState; i++) {
10051 state = ctxt->states->tabState[i];
10052 ctxt->state = state;
10054 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
10061 * validation error, log the message for the "best" one
10063 ctxt->flags |= FLAGS_IGNORABLE;
10064 xmlRelaxNGLogBestError(ctxt);
10066 for (i = 0; i < ctxt->states->nbState; i++) {
10067 xmlRelaxNGFreeValidState(ctxt,
10068 ctxt->states->tabState[i]);
10069 ctxt->states->tabState[i] = NULL;
10071 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10072 ctxt->flags = oldflags;
10073 ctxt->states = NULL;
10074 if ((ret == 0) && (tmp == -1))
10077 state = ctxt->state;
10079 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
10080 xmlRelaxNGFreeValidState(ctxt, state);
10084 node->psvi = define;
10086 ctxt->flags = oldflags;
10087 ctxt->state = oldstate;
10088 if (oldstate != NULL)
10089 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
10091 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10092 xmlRelaxNGDumpValidError(ctxt);
10100 if (ctxt->errNr > errNr)
10101 xmlRelaxNGPopErrors(ctxt, errNr);
10105 xmlGenericError(xmlGenericErrorContext,
10106 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10108 if (oldstate == NULL)
10109 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10110 else if (oldstate->seq == NULL)
10111 xmlGenericError(xmlGenericErrorContext, ": done\n");
10112 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10113 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10114 oldstate->seq->name);
10116 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10117 oldstate->seq->name, oldstate->seq->type);
10120 case XML_RELAXNG_OPTIONAL:{
10121 errNr = ctxt->errNr;
10122 oldflags = ctxt->flags;
10123 ctxt->flags |= FLAGS_IGNORABLE;
10124 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10126 xmlRelaxNGValidateDefinitionList(ctxt,
10129 if (ctxt->state != NULL)
10130 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10131 ctxt->state = oldstate;
10132 ctxt->flags = oldflags;
10134 if (ctxt->errNr > errNr)
10135 xmlRelaxNGPopErrors(ctxt, errNr);
10138 if (ctxt->states != NULL) {
10139 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10141 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10142 if (ctxt->states == NULL) {
10143 xmlRelaxNGFreeValidState(ctxt, oldstate);
10144 ctxt->flags = oldflags;
10146 if (ctxt->errNr > errNr)
10147 xmlRelaxNGPopErrors(ctxt, errNr);
10150 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10151 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10152 ctxt->state = NULL;
10154 ctxt->flags = oldflags;
10156 if (ctxt->errNr > errNr)
10157 xmlRelaxNGPopErrors(ctxt, errNr);
10160 case XML_RELAXNG_ONEORMORE:
10161 errNr = ctxt->errNr;
10162 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10166 if (ctxt->errNr > errNr)
10167 xmlRelaxNGPopErrors(ctxt, errNr);
10168 /* no break on purpose */
10169 case XML_RELAXNG_ZEROORMORE:{
10171 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10174 errNr = ctxt->errNr;
10175 res = xmlRelaxNGNewStates(ctxt, 1);
10181 * All the input states are also exit states
10183 if (ctxt->state != NULL) {
10184 xmlRelaxNGAddStates(ctxt, res,
10185 xmlRelaxNGCopyValidState(ctxt,
10189 for (j = 0; j < ctxt->states->nbState; j++) {
10190 xmlRelaxNGAddStates(ctxt, res,
10191 xmlRelaxNGCopyValidState(ctxt,
10192 ctxt->states->tabState[j]));
10195 oldflags = ctxt->flags;
10196 ctxt->flags |= FLAGS_IGNORABLE;
10199 base = res->nbState;
10201 if (ctxt->states != NULL) {
10202 states = ctxt->states;
10203 for (i = 0; i < states->nbState; i++) {
10204 ctxt->state = states->tabState[i];
10205 ctxt->states = NULL;
10206 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10210 if (ctxt->state != NULL) {
10211 tmp = xmlRelaxNGAddStates(ctxt, res,
10213 ctxt->state = NULL;
10216 } else if (ctxt->states != NULL) {
10217 for (j = 0; j < ctxt->states->nbState;
10220 xmlRelaxNGAddStates(ctxt, res,
10221 ctxt->states->tabState[j]);
10225 xmlRelaxNGFreeStates(ctxt,
10227 ctxt->states = NULL;
10230 if (ctxt->state != NULL) {
10231 xmlRelaxNGFreeValidState(ctxt,
10233 ctxt->state = NULL;
10238 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10242 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10243 ctxt->state = NULL;
10245 base = res->nbState;
10246 if (ctxt->state != NULL) {
10247 tmp = xmlRelaxNGAddStates(ctxt, res,
10249 ctxt->state = NULL;
10252 } else if (ctxt->states != NULL) {
10253 for (j = 0; j < ctxt->states->nbState; j++) {
10254 tmp = xmlRelaxNGAddStates(ctxt, res,
10255 ctxt->states->tabState[j]);
10259 if (states == NULL) {
10260 states = ctxt->states;
10262 xmlRelaxNGFreeStates(ctxt,
10265 ctxt->states = NULL;
10271 * Collect all the new nodes added at that step
10272 * and make them the new node set
10274 if (res->nbState - base == 1) {
10275 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10280 if (states == NULL) {
10281 xmlRelaxNGNewStates(ctxt,
10282 res->nbState - base);
10283 states = ctxt->states;
10284 if (states == NULL) {
10289 states->nbState = 0;
10290 for (i = base; i < res->nbState; i++)
10291 xmlRelaxNGAddStates(ctxt, states,
10292 xmlRelaxNGCopyValidState
10293 (ctxt, res->tabState[i]));
10294 ctxt->states = states;
10297 } while (progress == 1);
10298 if (states != NULL) {
10299 xmlRelaxNGFreeStates(ctxt, states);
10301 ctxt->states = res;
10302 ctxt->flags = oldflags;
10305 * errors may have to be propagated back...
10307 if (ctxt->errNr > errNr)
10308 xmlRelaxNGPopErrors(ctxt, errNr);
10313 case XML_RELAXNG_CHOICE:{
10314 xmlRelaxNGDefinePtr list = NULL;
10315 xmlRelaxNGStatesPtr states = NULL;
10317 node = xmlRelaxNGSkipIgnored(ctxt, node);
10319 errNr = ctxt->errNr;
10320 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10323 * node == NULL can't be optimized since IS_TRIABLE
10324 * doesn't account for choice which may lead to
10327 xmlHashTablePtr triage =
10328 (xmlHashTablePtr) define->data;
10331 * Something we can optimize cleanly there is only one
10332 * possble branch out !
10334 if ((node->type == XML_TEXT_NODE) ||
10335 (node->type == XML_CDATA_SECTION_NODE)) {
10337 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10338 } else if (node->type == XML_ELEMENT_NODE) {
10339 if (node->ns != NULL) {
10340 list = xmlHashLookup2(triage, node->name,
10344 xmlHashLookup2(triage, BAD_CAST "#any",
10348 xmlHashLookup2(triage, node->name, NULL);
10351 xmlHashLookup2(triage, BAD_CAST "#any",
10354 if (list == NULL) {
10356 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
10359 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10365 list = define->content;
10366 oldflags = ctxt->flags;
10367 ctxt->flags |= FLAGS_IGNORABLE;
10369 while (list != NULL) {
10370 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10371 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10373 if (states == NULL) {
10374 states = xmlRelaxNGNewStates(ctxt, 1);
10376 if (ctxt->state != NULL) {
10377 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10378 } else if (ctxt->states != NULL) {
10379 for (i = 0; i < ctxt->states->nbState; i++) {
10380 xmlRelaxNGAddStates(ctxt, states,
10384 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10385 ctxt->states = NULL;
10388 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10390 ctxt->state = oldstate;
10393 if (states != NULL) {
10394 xmlRelaxNGFreeValidState(ctxt, oldstate);
10395 ctxt->states = states;
10396 ctxt->state = NULL;
10399 ctxt->states = NULL;
10401 ctxt->flags = oldflags;
10403 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10404 xmlRelaxNGDumpValidError(ctxt);
10407 if (ctxt->errNr > errNr)
10408 xmlRelaxNGPopErrors(ctxt, errNr);
10412 case XML_RELAXNG_DEF:
10413 case XML_RELAXNG_GROUP:
10414 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10416 case XML_RELAXNG_INTERLEAVE:
10417 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10419 case XML_RELAXNG_ATTRIBUTE:
10420 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10422 case XML_RELAXNG_START:
10423 case XML_RELAXNG_NOOP:
10424 case XML_RELAXNG_REF:
10425 case XML_RELAXNG_EXTERNALREF:
10426 case XML_RELAXNG_PARENTREF:
10427 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10429 case XML_RELAXNG_DATATYPE:{
10431 xmlChar *content = NULL;
10434 while (child != NULL) {
10435 if (child->type == XML_ELEMENT_NODE) {
10436 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10437 node->parent->name);
10440 } else if ((child->type == XML_TEXT_NODE) ||
10441 (child->type == XML_CDATA_SECTION_NODE)) {
10442 content = xmlStrcat(content, child->content);
10444 /* TODO: handle entities ... */
10445 child = child->next;
10448 if (content != NULL)
10452 if (content == NULL) {
10453 content = xmlStrdup(BAD_CAST "");
10454 if (content == NULL) {
10455 xmlRngVErrMemory(ctxt, "validating\n");
10460 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10463 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10464 } else if (ret == 0) {
10465 ctxt->state->seq = NULL;
10467 if (content != NULL)
10471 case XML_RELAXNG_VALUE:{
10472 xmlChar *content = NULL;
10477 while (child != NULL) {
10478 if (child->type == XML_ELEMENT_NODE) {
10479 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10480 node->parent->name);
10483 } else if ((child->type == XML_TEXT_NODE) ||
10484 (child->type == XML_CDATA_SECTION_NODE)) {
10485 content = xmlStrcat(content, child->content);
10487 /* TODO: handle entities ... */
10488 child = child->next;
10491 if (content != NULL)
10495 if (content == NULL) {
10496 content = xmlStrdup(BAD_CAST "");
10497 if (content == NULL) {
10498 xmlRngVErrMemory(ctxt, "validating\n");
10503 oldvalue = ctxt->state->value;
10504 ctxt->state->value = content;
10505 ret = xmlRelaxNGValidateValue(ctxt, define);
10506 ctxt->state->value = oldvalue;
10508 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10509 } else if (ret == 0) {
10510 ctxt->state->seq = NULL;
10512 if (content != NULL)
10516 case XML_RELAXNG_LIST:{
10519 xmlChar *oldvalue, *oldendvalue;
10523 * Make sure it's only text nodes
10528 while (child != NULL) {
10529 if (child->type == XML_ELEMENT_NODE) {
10530 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10531 node->parent->name);
10534 } else if ((child->type == XML_TEXT_NODE) ||
10535 (child->type == XML_CDATA_SECTION_NODE)) {
10536 content = xmlStrcat(content, child->content);
10538 /* TODO: handle entities ... */
10539 child = child->next;
10542 if (content != NULL)
10546 if (content == NULL) {
10547 content = xmlStrdup(BAD_CAST "");
10548 if (content == NULL) {
10549 xmlRngVErrMemory(ctxt, "validating\n");
10554 len = xmlStrlen(content);
10555 oldvalue = ctxt->state->value;
10556 oldendvalue = ctxt->state->endvalue;
10557 ctxt->state->value = content;
10558 ctxt->state->endvalue = content + len;
10559 ret = xmlRelaxNGValidateValue(ctxt, define);
10560 ctxt->state->value = oldvalue;
10561 ctxt->state->endvalue = oldendvalue;
10563 VALID_ERR(XML_RELAXNG_ERR_LIST);
10564 } else if ((ret == 0) && (node != NULL)) {
10565 ctxt->state->seq = node->next;
10567 if (content != NULL)
10571 case XML_RELAXNG_EXCEPT:
10572 case XML_RELAXNG_PARAM:
10578 for (i = 0; i < ctxt->depth; i++)
10579 xmlGenericError(xmlGenericErrorContext, " ");
10580 xmlGenericError(xmlGenericErrorContext,
10581 "Validating %s ", xmlRelaxNGDefName(define));
10582 if (define->name != NULL)
10583 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
10585 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
10587 xmlGenericError(xmlGenericErrorContext, "failed\n");
10593 * xmlRelaxNGValidateDefinition:
10594 * @ctxt: a Relax-NG validation context
10595 * @define: the definition to verify
10597 * Validate the current node lists against the definition
10599 * Returns 0 if the validation succeeded or an error code.
10602 xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10603 xmlRelaxNGDefinePtr define)
10605 xmlRelaxNGStatesPtr states, res;
10606 int i, j, k, ret, oldflags;
10609 * We should NOT have both ctxt->state and ctxt->states
10611 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10612 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10613 ctxt->state = NULL;
10616 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
10617 if (ctxt->states != NULL) {
10618 ctxt->state = ctxt->states->tabState[0];
10619 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10620 ctxt->states = NULL;
10622 ret = xmlRelaxNGValidateState(ctxt, define);
10623 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10624 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10625 ctxt->state = NULL;
10627 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10628 ctxt->state = ctxt->states->tabState[0];
10629 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10630 ctxt->states = NULL;
10635 states = ctxt->states;
10636 ctxt->states = NULL;
10639 oldflags = ctxt->flags;
10640 ctxt->flags |= FLAGS_IGNORABLE;
10641 for (i = 0; i < states->nbState; i++) {
10642 ctxt->state = states->tabState[i];
10643 ctxt->states = NULL;
10644 ret = xmlRelaxNGValidateState(ctxt, define);
10646 * We should NOT have both ctxt->state and ctxt->states
10648 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10649 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10650 ctxt->state = NULL;
10653 if (ctxt->states == NULL) {
10655 /* add the state to the container */
10656 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10657 ctxt->state = NULL;
10659 /* add the state directly in states */
10660 states->tabState[j++] = ctxt->state;
10661 ctxt->state = NULL;
10665 /* make it the new container and copy other results */
10666 res = ctxt->states;
10667 ctxt->states = NULL;
10668 for (k = 0; k < j; k++)
10669 xmlRelaxNGAddStates(ctxt, res,
10670 states->tabState[k]);
10672 /* add all the new results to res and reff the container */
10673 for (k = 0; k < ctxt->states->nbState; k++)
10674 xmlRelaxNGAddStates(ctxt, res,
10675 ctxt->states->tabState[k]);
10676 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10677 ctxt->states = NULL;
10681 if (ctxt->state != NULL) {
10682 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10683 ctxt->state = NULL;
10684 } else if (ctxt->states != NULL) {
10685 for (k = 0; k < ctxt->states->nbState; k++)
10686 xmlRelaxNGFreeValidState(ctxt,
10687 ctxt->states->tabState[k]);
10688 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10689 ctxt->states = NULL;
10693 ctxt->flags = oldflags;
10695 xmlRelaxNGFreeStates(ctxt, states);
10696 ctxt->states = res;
10698 } else if (j > 1) {
10699 states->nbState = j;
10700 ctxt->states = states;
10702 } else if (j == 1) {
10703 ctxt->state = states->tabState[0];
10704 xmlRelaxNGFreeStates(ctxt, states);
10708 xmlRelaxNGFreeStates(ctxt, states);
10709 if (ctxt->states != NULL) {
10710 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10711 ctxt->states = NULL;
10714 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10715 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10716 ctxt->state = NULL;
10722 * xmlRelaxNGValidateDocument:
10723 * @ctxt: a Relax-NG validation context
10724 * @doc: the document
10726 * Validate the given document
10728 * Returns 0 if the validation succeeded or an error code.
10731 xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10734 xmlRelaxNGPtr schema;
10735 xmlRelaxNGGrammarPtr grammar;
10736 xmlRelaxNGValidStatePtr state;
10739 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
10742 ctxt->errNo = XML_RELAXNG_OK;
10743 schema = ctxt->schema;
10744 grammar = schema->topgrammar;
10745 if (grammar == NULL) {
10746 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10749 state = xmlRelaxNGNewValidState(ctxt, NULL);
10750 ctxt->state = state;
10751 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
10752 if ((ctxt->state != NULL) && (state->seq != NULL)) {
10753 state = ctxt->state;
10755 node = xmlRelaxNGSkipIgnored(ctxt, node);
10756 if (node != NULL) {
10758 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10762 } else if (ctxt->states != NULL) {
10766 for (i = 0; i < ctxt->states->nbState; i++) {
10767 state = ctxt->states->tabState[i];
10769 node = xmlRelaxNGSkipIgnored(ctxt, node);
10772 xmlRelaxNGFreeValidState(ctxt, state);
10776 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10781 if (ctxt->state != NULL) {
10782 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10783 ctxt->state = NULL;
10786 xmlRelaxNGDumpValidError(ctxt);
10788 else if (ctxt->errNr != 0) {
10789 ctxt->error(ctxt->userData,
10790 "%d Extra error messages left on stack !\n",
10792 xmlRelaxNGDumpValidError(ctxt);
10795 #ifdef LIBXML_VALID_ENABLED
10796 if (ctxt->idref == 1) {
10797 xmlValidCtxt vctxt;
10799 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10801 vctxt.error = ctxt->error;
10802 vctxt.warning = ctxt->warning;
10803 vctxt.userData = ctxt->userData;
10805 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10808 #endif /* LIBXML_VALID_ENABLED */
10809 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
10816 * xmlRelaxNGCleanPSVI:
10817 * @node: an input element or document
10819 * Call this routine to speed up XPath computation on static documents.
10820 * This stamps all the element nodes with the document order
10821 * Like for line information, the order is kept in the element->content
10822 * field, the value stored is actually - the node number (starting at -1)
10823 * to be able to differentiate from line numbers.
10825 * Returns the number of elements found in the document or -1 in case
10829 xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10832 if ((node == NULL) ||
10833 ((node->type != XML_ELEMENT_NODE) &&
10834 (node->type != XML_DOCUMENT_NODE) &&
10835 (node->type != XML_HTML_DOCUMENT_NODE)))
10837 if (node->type == XML_ELEMENT_NODE)
10840 cur = node->children;
10841 while (cur != NULL) {
10842 if (cur->type == XML_ELEMENT_NODE) {
10844 if (cur->children != NULL) {
10845 cur = cur->children;
10849 if (cur->next != NULL) {
10861 if (cur->next != NULL) {
10865 } while (cur != NULL);
10869 /************************************************************************
10871 * Validation interfaces *
10873 ************************************************************************/
10876 * xmlRelaxNGNewValidCtxt:
10877 * @schema: a precompiled XML RelaxNGs
10879 * Create an XML RelaxNGs validation context based on the given schema
10881 * Returns the validation context or NULL in case of error
10883 xmlRelaxNGValidCtxtPtr
10884 xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10886 xmlRelaxNGValidCtxtPtr ret;
10888 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10890 xmlRngVErrMemory(NULL, "building context\n");
10893 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10894 ret->schema = schema;
10895 ret->error = xmlGenericError;
10896 ret->userData = xmlGenericErrorContext;
10900 ret->errTab = NULL;
10901 if (schema != NULL)
10902 ret->idref = schema->idref;
10903 ret->states = NULL;
10904 ret->freeState = NULL;
10905 ret->freeStates = NULL;
10906 ret->errNo = XML_RELAXNG_OK;
10911 * xmlRelaxNGFreeValidCtxt:
10912 * @ctxt: the schema validation context
10914 * Free the resources associated to the schema validation context
10917 xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10923 if (ctxt->states != NULL)
10924 xmlRelaxNGFreeStates(NULL, ctxt->states);
10925 if (ctxt->freeState != NULL) {
10926 for (k = 0; k < ctxt->freeState->nbState; k++) {
10927 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10929 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
10931 if (ctxt->freeStates != NULL) {
10932 for (k = 0; k < ctxt->freeStatesNr; k++) {
10933 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10935 xmlFree(ctxt->freeStates);
10937 if (ctxt->errTab != NULL)
10938 xmlFree(ctxt->errTab);
10939 if (ctxt->elemTab != NULL) {
10940 xmlRegExecCtxtPtr exec;
10942 exec = xmlRelaxNGElemPop(ctxt);
10943 while (exec != NULL) {
10944 xmlRegFreeExecCtxt(exec);
10945 exec = xmlRelaxNGElemPop(ctxt);
10947 xmlFree(ctxt->elemTab);
10953 * xmlRelaxNGSetValidErrors:
10954 * @ctxt: a Relax-NG validation context
10955 * @err: the error function
10956 * @warn: the warning function
10957 * @ctx: the functions context
10959 * Set the error and warning callback informations
10962 xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10963 xmlRelaxNGValidityErrorFunc err,
10964 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10969 ctxt->warning = warn;
10970 ctxt->userData = ctx;
10971 ctxt->serror = NULL;
10975 * xmlRelaxNGSetValidStructuredErrors:
10976 * @ctxt: a Relax-NG validation context
10977 * @serror: the structured error function
10978 * @ctx: the functions context
10980 * Set the structured error callback
10983 xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
10984 xmlStructuredErrorFunc serror, void *ctx)
10988 ctxt->serror = serror;
10989 ctxt->error = NULL;
10990 ctxt->warning = NULL;
10991 ctxt->userData = ctx;
10995 * xmlRelaxNGGetValidErrors:
10996 * @ctxt: a Relax-NG validation context
10997 * @err: the error function result
10998 * @warn: the warning function result
10999 * @ctx: the functions context result
11001 * Get the error and warning callback informations
11003 * Returns -1 in case of error and 0 otherwise
11006 xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
11007 xmlRelaxNGValidityErrorFunc * err,
11008 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
11013 *err = ctxt->error;
11015 *warn = ctxt->warning;
11017 *ctx = ctxt->userData;
11022 * xmlRelaxNGValidateDoc:
11023 * @ctxt: a Relax-NG validation context
11024 * @doc: a parsed document tree
11026 * Validate a document tree in memory.
11028 * Returns 0 if the document is valid, a positive error code
11029 * number otherwise and -1 in case of internal or API error.
11032 xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
11036 if ((ctxt == NULL) || (doc == NULL))
11041 ret = xmlRelaxNGValidateDocument(ctxt, doc);
11043 * Remove all left PSVI
11045 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
11048 * TODO: build error codes
11055 #define bottom_relaxng
11056 #include "elfgcchack.h"
11057 #endif /* LIBXML_SCHEMAS_ENABLED */