1 /*
2 * relaxng.c : implementation of the Relax-NG handling and validity checking
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <veillard@redhat.com>
7 */
8
9 /**
10 * TODO:
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.
14 */
15
16 #define IN_LIBXML
17 #include "libxml.h"
18
19 #ifdef LIBXML_SCHEMAS_ENABLED
20
21 #include <string.h>
22 #include <stdio.h>
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>
28
29 #include <libxml/relaxng.h>
30
31 #include <libxml/xmlschemastypes.h>
32 #include <libxml/xmlautomata.h>
33 #include <libxml/xmlregexp.h>
34 #include <libxml/xmlschemastypes.h>
35
36 /*
37 * The Relax-NG namespace
38 */
39 static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
40 "http://relaxng.org/ns/structure/1.0";
41
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)))
47
48
49 #if 0
50 #define DEBUG 1
51
52 #define DEBUG_GRAMMAR 1
53
54 #define DEBUG_CONTENT 1
55
56 #define DEBUG_TYPE 1
57
58 #define DEBUG_VALID 1
59
60 #define DEBUG_INTERLEAVE 1
61
62 #define DEBUG_LIST 1
63
64 #define DEBUG_INCLUDE 1
65
66 #define DEBUG_ERROR 1
67
68 #define DEBUG_COMPILE 1
69
70 #define DEBUG_PROGRESSIVE 1
71 #endif
72
73 #define MAX_ERROR 5
74
75 #define TODO \
76 xmlGenericError(xmlGenericErrorContext, \
77 "Unimplemented block at %s:%d\n", \
78 __FILE__, __LINE__);
79
80 typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
81 typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
82
83 typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
84 typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
85
86 typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
87 typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
88
89 typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
90 typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
91
92 typedef enum {
93 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
94 XML_RELAXNG_COMBINE_CHOICE, /* choice */
95 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
96 } xmlRelaxNGCombine;
97
98 typedef enum {
99 XML_RELAXNG_CONTENT_ERROR = -1,
100 XML_RELAXNG_CONTENT_EMPTY = 0,
101 XML_RELAXNG_CONTENT_SIMPLE,
102 XML_RELAXNG_CONTENT_COMPLEX
103 } xmlRelaxNGContentType;
104
105 typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
106 typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
107
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 */
117 };
118
119
120 typedef enum {
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 */
143 } xmlRelaxNGType;
144
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)
154
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 */
171 };
172
173 /**
174 * _xmlRelaxNG:
175 *
176 * A RelaxNGs definition
177 */
178 struct _xmlRelaxNG {
179 void *_private; /* unused by the library for users or bindings */
180 xmlRelaxNGGrammarPtr topgrammar;
181 xmlDocPtr doc;
182
183 int idref; /* requires idref checking */
184
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 */
191
192 };
193
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)
204
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;
211
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 */
220
221 int nbInterleaves;
222 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
223
224 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
225 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
226 xmlChar *URL;
227 xmlDocPtr document;
228
229 int defNr; /* number of defines used */
230 int defMax; /* number of defines aloocated */
231 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
232
233 const char *buffer;
234 int size;
235
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 */
241
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 */
247
248 int idref; /* requires idref checking */
249
250 /* used to compile content models */
251 xmlAutomataPtr am; /* the automata */
252 xmlAutomataStatePtr state; /* used to build the automata */
253
254 int crng; /* compact syntax and other flags */
255 int freedoc; /* need to free the document */
256 };
257
258 #define FLAGS_IGNORABLE 1
259 #define FLAGS_NEGATIVE 2
260 #define FLAGS_MIXED_CONTENT 4
261 #define FLAGS_NOERROR 8
262
263 /**
264 * xmlRelaxNGInterleaveGroup:
265 *
266 * A RelaxNGs partition set associated to lists of definitions
267 */
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 */
274 };
275
276 #define IS_DETERMINIST 1
277 #define IS_NEEDCHECK 2
278
279 /**
280 * xmlRelaxNGPartitions:
281 *
282 * A RelaxNGs partition associated to an interleave group
283 */
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;
292 };
293
294 /**
295 * xmlRelaxNGValidState:
296 *
297 * A RelaxNGs validation state
298 */
299 #define MAX_ATTR 20
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 */
311 };
312
313 /**
314 * xmlRelaxNGStates:
315 *
316 * A RelaxNGs container for validation state
317 */
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;
324 };
325
326 #define ERROR_IS_DUP 1
327
328 /**
329 * xmlRelaxNGValidError:
330 *
331 * A RelaxNGs validation error
332 */
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 */
342 };
343
344 /**
345 * xmlRelaxNGValidCtxt:
346 *
347 * A RelaxNGs validation context
348 */
349
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 */
356
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 */
363
364 /*
365 * Errors accumulated in branches may have to be stacked to be
366 * provided back when it's sure they affect validation.
367 */
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 */
372
373 xmlRelaxNGValidStatePtr state; /* the current validation state */
374 xmlRelaxNGStatesPtr states; /* the accumulated state list */
375
376 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
377 int freeStatesNr;
378 int freeStatesMax;
379 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
380
381 /*
382 * This is used for "progressive" validation
383 */
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 */
393 };
394
395 /**
396 * xmlRelaxNGInclude:
397 *
398 * Structure associated to a RelaxNGs document element
399 */
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 */
406 };
407
408 /**
409 * xmlRelaxNGDocument:
410 *
411 * Structure associated to a RelaxNGs document element
412 */
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 */
420 };
421
422
423 /************************************************************************
424 * *
425 * Some factorized error routines *
426 * *
427 ************************************************************************/
428
429 /**
430 * xmlRngPErrMemory:
431 * @ctxt: an Relax-NG parser context
432 * @extra: extra informations
433 *
434 * Handle a redefinition of attribute error
435 */
436 static void
xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt,const char * extra)437 xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
438 {
439 xmlStructuredErrorFunc schannel = NULL;
440 xmlGenericErrorFunc channel = NULL;
441 void *data = NULL;
442
443 if (ctxt != NULL) {
444 if (ctxt->serror != NULL)
445 schannel = ctxt->serror;
446 else
447 channel = ctxt->error;
448 data = ctxt->userData;
449 ctxt->nbErrors++;
450 }
451 if (extra)
452 __xmlRaiseError(schannel, channel, data,
453 NULL, NULL, XML_FROM_RELAXNGP,
454 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
455 NULL, NULL, 0, 0,
456 "Memory allocation failed : %s\n", extra);
457 else
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");
462 }
463
464 /**
465 * xmlRngVErrMemory:
466 * @ctxt: a Relax-NG validation context
467 * @extra: extra informations
468 *
469 * Handle a redefinition of attribute error
470 */
471 static void
xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt,const char * extra)472 xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
473 {
474 xmlStructuredErrorFunc schannel = NULL;
475 xmlGenericErrorFunc channel = NULL;
476 void *data = NULL;
477
478 if (ctxt != NULL) {
479 if (ctxt->serror != NULL)
480 schannel = ctxt->serror;
481 else
482 channel = ctxt->error;
483 data = ctxt->userData;
484 ctxt->nbErrors++;
485 }
486 if (extra)
487 __xmlRaiseError(schannel, channel, data,
488 NULL, NULL, XML_FROM_RELAXNGV,
489 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
490 NULL, NULL, 0, 0,
491 "Memory allocation failed : %s\n", extra);
492 else
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");
497 }
498
499 /**
500 * xmlRngPErr:
501 * @ctxt: a Relax-NG parser context
502 * @node: the node raising the error
503 * @error: the error code
504 * @msg: message
505 * @str1: extra info
506 * @str2: extra info
507 *
508 * Handle a Relax NG Parsing error
509 */
510 static void
xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node,int error,const char * msg,const xmlChar * str1,const xmlChar * str2)511 xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
512 const char *msg, const xmlChar * str1, const xmlChar * str2)
513 {
514 xmlStructuredErrorFunc schannel = NULL;
515 xmlGenericErrorFunc channel = NULL;
516 void *data = NULL;
517
518 if (ctxt != NULL) {
519 if (ctxt->serror != NULL)
520 schannel = ctxt->serror;
521 else
522 channel = ctxt->error;
523 data = ctxt->userData;
524 ctxt->nbErrors++;
525 }
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,
530 msg, str1, str2);
531 }
532
533 /**
534 * xmlRngVErr:
535 * @ctxt: a Relax-NG validation context
536 * @node: the node raising the error
537 * @error: the error code
538 * @msg: message
539 * @str1: extra info
540 * @str2: extra info
541 *
542 * Handle a Relax NG Validation error
543 */
544 static void
xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt,xmlNodePtr node,int error,const char * msg,const xmlChar * str1,const xmlChar * str2)545 xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
546 const char *msg, const xmlChar * str1, const xmlChar * str2)
547 {
548 xmlStructuredErrorFunc schannel = NULL;
549 xmlGenericErrorFunc channel = NULL;
550 void *data = NULL;
551
552 if (ctxt != NULL) {
553 if (ctxt->serror != NULL)
554 schannel = ctxt->serror;
555 else
556 channel = ctxt->error;
557 data = ctxt->userData;
558 ctxt->nbErrors++;
559 }
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,
564 msg, str1, str2);
565 }
566
567 /************************************************************************
568 * *
569 * Preliminary type checking interfaces *
570 * *
571 ************************************************************************/
572
573 /**
574 * xmlRelaxNGTypeHave:
575 * @data: data needed for the library
576 * @type: the type name
577 * @value: the value to check
578 *
579 * Function provided by a type library to check if a type is exported
580 *
581 * Returns 1 if yes, 0 if no and -1 in case of error.
582 */
583 typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
584
585 /**
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
591 *
592 * Function provided by a type library to check if a value match a type
593 *
594 * Returns 1 if yes, 0 if no and -1 in case of error.
595 */
596 typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
597 const xmlChar * value, void **result,
598 xmlNodePtr node);
599
600 /**
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
608 *
609 * Function provided by a type library to check a value facet
610 *
611 * Returns 1 if yes, 0 if no and -1 in case of error.
612 */
613 typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
614 const xmlChar * facet,
615 const xmlChar * val,
616 const xmlChar * strval, void *value);
617
618 /**
619 * xmlRelaxNGTypeFree:
620 * @data: data needed for the library
621 * @result: the value to free
622 *
623 * Function provided by a type library to free a returned result
624 */
625 typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
626
627 /**
628 * xmlRelaxNGTypeCompare:
629 * @data: data needed for the library
630 * @type: the type name
631 * @value1: the first value
632 * @value2: the second value
633 *
634 * Function provided by a type library to compare two values accordingly
635 * to a type.
636 *
637 * Returns 1 if yes, 0 if no and -1 in case of error.
638 */
639 typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
640 const xmlChar * value1,
641 xmlNodePtr ctxt1,
642 void *comp1,
643 const xmlChar * value2,
644 xmlNodePtr ctxt2);
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 */
655 };
656
657 /************************************************************************
658 * *
659 * Allocation functions *
660 * *
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
667 ATTRIBUTE_UNUSED,
668 xmlRelaxNGValidStatePtr state1,
669 xmlRelaxNGValidStatePtr state2);
670 static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
671 xmlRelaxNGValidStatePtr state);
672
673 /**
674 * xmlRelaxNGFreeDocument:
675 * @docu: a document structure
676 *
677 * Deallocate a RelaxNG document structure.
678 */
679 static void
xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)680 xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
681 {
682 if (docu == NULL)
683 return;
684
685 if (docu->href != NULL)
686 xmlFree(docu->href);
687 if (docu->doc != NULL)
688 xmlFreeDoc(docu->doc);
689 if (docu->schema != NULL)
690 xmlRelaxNGFreeInnerSchema(docu->schema);
691 xmlFree(docu);
692 }
693
694 /**
695 * xmlRelaxNGFreeDocumentList:
696 * @docu: a list of document structure
697 *
698 * Deallocate a RelaxNG document structures.
699 */
700 static void
xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)701 xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
702 {
703 xmlRelaxNGDocumentPtr next;
704
705 while (docu != NULL) {
706 next = docu->next;
707 xmlRelaxNGFreeDocument(docu);
708 docu = next;
709 }
710 }
711
712 /**
713 * xmlRelaxNGFreeInclude:
714 * @incl: a include structure
715 *
716 * Deallocate a RelaxNG include structure.
717 */
718 static void
xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)719 xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
720 {
721 if (incl == NULL)
722 return;
723
724 if (incl->href != NULL)
725 xmlFree(incl->href);
726 if (incl->doc != NULL)
727 xmlFreeDoc(incl->doc);
728 if (incl->schema != NULL)
729 xmlRelaxNGFree(incl->schema);
730 xmlFree(incl);
731 }
732
733 /**
734 * xmlRelaxNGFreeIncludeList:
735 * @incl: a include structure list
736 *
737 * Deallocate a RelaxNG include structure.
738 */
739 static void
xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)740 xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
741 {
742 xmlRelaxNGIncludePtr next;
743
744 while (incl != NULL) {
745 next = incl->next;
746 xmlRelaxNGFreeInclude(incl);
747 incl = next;
748 }
749 }
750
751 /**
752 * xmlRelaxNGNewRelaxNG:
753 * @ctxt: a Relax-NG validation context (optional)
754 *
755 * Allocate a new RelaxNG structure.
756 *
757 * Returns the newly allocated structure or NULL in case or error
758 */
759 static xmlRelaxNGPtr
xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)760 xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
761 {
762 xmlRelaxNGPtr ret;
763
764 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
765 if (ret == NULL) {
766 xmlRngPErrMemory(ctxt, NULL);
767 return (NULL);
768 }
769 memset(ret, 0, sizeof(xmlRelaxNG));
770
771 return (ret);
772 }
773
774 /**
775 * xmlRelaxNGFreeInnerSchema:
776 * @schema: a schema structure
777 *
778 * Deallocate a RelaxNG schema structure.
779 */
780 static void
xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)781 xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
782 {
783 if (schema == NULL)
784 return;
785
786 if (schema->doc != NULL)
787 xmlFreeDoc(schema->doc);
788 if (schema->defTab != NULL) {
789 int i;
790
791 for (i = 0; i < schema->defNr; i++)
792 xmlRelaxNGFreeDefine(schema->defTab[i]);
793 xmlFree(schema->defTab);
794 }
795
796 xmlFree(schema);
797 }
798
799 /**
800 * xmlRelaxNGFree:
801 * @schema: a schema structure
802 *
803 * Deallocate a RelaxNG structure.
804 */
805 void
xmlRelaxNGFree(xmlRelaxNGPtr schema)806 xmlRelaxNGFree(xmlRelaxNGPtr schema)
807 {
808 if (schema == NULL)
809 return;
810
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) {
820 int i;
821
822 for (i = 0; i < schema->defNr; i++)
823 xmlRelaxNGFreeDefine(schema->defTab[i]);
824 xmlFree(schema->defTab);
825 }
826
827 xmlFree(schema);
828 }
829
830 /**
831 * xmlRelaxNGNewGrammar:
832 * @ctxt: a Relax-NG validation context (optional)
833 *
834 * Allocate a new RelaxNG grammar.
835 *
836 * Returns the newly allocated structure or NULL in case or error
837 */
838 static xmlRelaxNGGrammarPtr
xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)839 xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
840 {
841 xmlRelaxNGGrammarPtr ret;
842
843 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
844 if (ret == NULL) {
845 xmlRngPErrMemory(ctxt, NULL);
846 return (NULL);
847 }
848 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
849
850 return (ret);
851 }
852
853 /**
854 * xmlRelaxNGFreeGrammar:
855 * @grammar: a grammar structure
856 *
857 * Deallocate a RelaxNG grammar structure.
858 */
859 static void
xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)860 xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
861 {
862 if (grammar == NULL)
863 return;
864
865 if (grammar->children != NULL) {
866 xmlRelaxNGFreeGrammar(grammar->children);
867 }
868 if (grammar->next != NULL) {
869 xmlRelaxNGFreeGrammar(grammar->next);
870 }
871 if (grammar->refs != NULL) {
872 xmlHashFree(grammar->refs, NULL);
873 }
874 if (grammar->defs != NULL) {
875 xmlHashFree(grammar->defs, NULL);
876 }
877
878 xmlFree(grammar);
879 }
880
881 /**
882 * xmlRelaxNGNewDefine:
883 * @ctxt: a Relax-NG validation context
884 * @node: the node in the input document.
885 *
886 * Allocate a new RelaxNG define.
887 *
888 * Returns the newly allocated structure or NULL in case or error
889 */
890 static xmlRelaxNGDefinePtr
xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)891 xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
892 {
893 xmlRelaxNGDefinePtr ret;
894
895 if (ctxt->defMax == 0) {
896 ctxt->defMax = 16;
897 ctxt->defNr = 0;
898 ctxt->defTab = (xmlRelaxNGDefinePtr *)
899 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
900 if (ctxt->defTab == NULL) {
901 xmlRngPErrMemory(ctxt, "allocating define\n");
902 return (NULL);
903 }
904 } else if (ctxt->defMax <= ctxt->defNr) {
905 xmlRelaxNGDefinePtr *tmp;
906
907 ctxt->defMax *= 2;
908 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
909 ctxt->defMax *
910 sizeof
911 (xmlRelaxNGDefinePtr));
912 if (tmp == NULL) {
913 xmlRngPErrMemory(ctxt, "allocating define\n");
914 return (NULL);
915 }
916 ctxt->defTab = tmp;
917 }
918 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
919 if (ret == NULL) {
920 xmlRngPErrMemory(ctxt, "allocating define\n");
921 return (NULL);
922 }
923 memset(ret, 0, sizeof(xmlRelaxNGDefine));
924 ctxt->defTab[ctxt->defNr++] = ret;
925 ret->node = node;
926 ret->depth = -1;
927 return (ret);
928 }
929
930 /**
931 * xmlRelaxNGFreePartition:
932 * @partitions: a partition set structure
933 *
934 * Deallocate RelaxNG partition set structures.
935 */
936 static void
xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)937 xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
938 {
939 xmlRelaxNGInterleaveGroupPtr group;
940 int j;
941
942 if (partitions != NULL) {
943 if (partitions->groups != NULL) {
944 for (j = 0; j < partitions->nbgroups; j++) {
945 group = partitions->groups[j];
946 if (group != NULL) {
947 if (group->defs != NULL)
948 xmlFree(group->defs);
949 if (group->attrs != NULL)
950 xmlFree(group->attrs);
951 xmlFree(group);
952 }
953 }
954 xmlFree(partitions->groups);
955 }
956 if (partitions->triage != NULL) {
957 xmlHashFree(partitions->triage, NULL);
958 }
959 xmlFree(partitions);
960 }
961 }
962
963 /**
964 * xmlRelaxNGFreeDefine:
965 * @define: a define structure
966 *
967 * Deallocate a RelaxNG define structure.
968 */
969 static void
xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)970 xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
971 {
972 if (define == NULL)
973 return;
974
975 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
976 xmlRelaxNGTypeLibraryPtr lib;
977
978 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
979 if ((lib != NULL) && (lib->freef != NULL))
980 lib->freef(lib->data, (void *) define->attrs);
981 }
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)
989 xmlFree(define->ns);
990 if (define->value != NULL)
991 xmlFree(define->value);
992 if (define->contModel != NULL)
993 xmlRegFreeRegexp(define->contModel);
994 xmlFree(define);
995 }
996
997 /**
998 * xmlRelaxNGNewStates:
999 * @ctxt: a Relax-NG validation context
1000 * @size: the default size for the container
1001 *
1002 * Allocate a new RelaxNG validation state container
1003 *
1004 * Returns the newly allocated structure or NULL in case or error
1005 */
1006 static xmlRelaxNGStatesPtr
xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt,int size)1007 xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1008 {
1009 xmlRelaxNGStatesPtr ret;
1010
1011 if ((ctxt != NULL) &&
1012 (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
1013 ctxt->freeStatesNr--;
1014 ret = ctxt->freeStates[ctxt->freeStatesNr];
1015 ret->nbState = 0;
1016 return (ret);
1017 }
1018 if (size < 16)
1019 size = 16;
1020
1021 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
1022 (size -
1023 1) *
1024 sizeof(xmlRelaxNGValidStatePtr));
1025 if (ret == NULL) {
1026 xmlRngVErrMemory(ctxt, "allocating states\n");
1027 return (NULL);
1028 }
1029 ret->nbState = 0;
1030 ret->maxState = size;
1031 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1032 sizeof
1033 (xmlRelaxNGValidStatePtr));
1034 if (ret->tabState == NULL) {
1035 xmlRngVErrMemory(ctxt, "allocating states\n");
1036 xmlFree(ret);
1037 return (NULL);
1038 }
1039 return (ret);
1040 }
1041
1042 /**
1043 * xmlRelaxNGAddStateUniq:
1044 * @ctxt: a Relax-NG validation context
1045 * @states: the states container
1046 * @state: the validation state
1047 *
1048 * Add a RelaxNG validation state to the container without checking
1049 * for unicity.
1050 *
1051 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1052 */
1053 static int
xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGStatesPtr states,xmlRelaxNGValidStatePtr state)1054 xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
1055 xmlRelaxNGStatesPtr states,
1056 xmlRelaxNGValidStatePtr state)
1057 {
1058 if (state == NULL) {
1059 return (-1);
1060 }
1061 if (states->nbState >= states->maxState) {
1062 xmlRelaxNGValidStatePtr *tmp;
1063 int size;
1064
1065 size = states->maxState * 2;
1066 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1067 (size) *
1068 sizeof
1069 (xmlRelaxNGValidStatePtr));
1070 if (tmp == NULL) {
1071 xmlRngVErrMemory(ctxt, "adding states\n");
1072 return (-1);
1073 }
1074 states->tabState = tmp;
1075 states->maxState = size;
1076 }
1077 states->tabState[states->nbState++] = state;
1078 return (1);
1079 }
1080
1081 /**
1082 * xmlRelaxNGAddState:
1083 * @ctxt: a Relax-NG validation context
1084 * @states: the states container
1085 * @state: the validation state
1086 *
1087 * Add a RelaxNG validation state to the container
1088 *
1089 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1090 */
1091 static int
xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGStatesPtr states,xmlRelaxNGValidStatePtr state)1092 xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1093 xmlRelaxNGStatesPtr states,
1094 xmlRelaxNGValidStatePtr state)
1095 {
1096 int i;
1097
1098 if (state == NULL || states == NULL) {
1099 return (-1);
1100 }
1101 if (states->nbState >= states->maxState) {
1102 xmlRelaxNGValidStatePtr *tmp;
1103 int size;
1104
1105 size = states->maxState * 2;
1106 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1107 (size) *
1108 sizeof
1109 (xmlRelaxNGValidStatePtr));
1110 if (tmp == NULL) {
1111 xmlRngVErrMemory(ctxt, "adding states\n");
1112 return (-1);
1113 }
1114 states->tabState = tmp;
1115 states->maxState = size;
1116 }
1117 for (i = 0; i < states->nbState; i++) {
1118 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1119 xmlRelaxNGFreeValidState(ctxt, state);
1120 return (0);
1121 }
1122 }
1123 states->tabState[states->nbState++] = state;
1124 return (1);
1125 }
1126
1127 /**
1128 * xmlRelaxNGFreeStates:
1129 * @ctxt: a Relax-NG validation context
1130 * @states: teh container
1131 *
1132 * Free a RelaxNG validation state container
1133 */
1134 static void
xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGStatesPtr states)1135 xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
1136 xmlRelaxNGStatesPtr states)
1137 {
1138 if (states == NULL)
1139 return;
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");
1147 }
1148 } else if ((ctxt != NULL)
1149 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1150 xmlRelaxNGStatesPtr *tmp;
1151
1152 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1153 2 * ctxt->freeStatesMax *
1154 sizeof
1155 (xmlRelaxNGStatesPtr));
1156 if (tmp == NULL) {
1157 xmlRngVErrMemory(ctxt, "storing states\n");
1158 xmlFree(states->tabState);
1159 xmlFree(states);
1160 return;
1161 }
1162 ctxt->freeStates = tmp;
1163 ctxt->freeStatesMax *= 2;
1164 }
1165 if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
1166 xmlFree(states->tabState);
1167 xmlFree(states);
1168 } else {
1169 ctxt->freeStates[ctxt->freeStatesNr++] = states;
1170 }
1171 }
1172
1173 /**
1174 * xmlRelaxNGNewValidState:
1175 * @ctxt: a Relax-NG validation context
1176 * @node: the current node or NULL for the document
1177 *
1178 * Allocate a new RelaxNG validation state
1179 *
1180 * Returns the newly allocated structure or NULL in case or error
1181 */
1182 static xmlRelaxNGValidStatePtr
xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt,xmlNodePtr node)1183 xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1184 {
1185 xmlRelaxNGValidStatePtr ret;
1186 xmlAttrPtr attr;
1187 xmlAttrPtr attrs[MAX_ATTR];
1188 int nbAttrs = 0;
1189 xmlNodePtr root = NULL;
1190
1191 if (node == NULL) {
1192 root = xmlDocGetRootElement(ctxt->doc);
1193 if (root == NULL)
1194 return (NULL);
1195 } else {
1196 attr = node->properties;
1197 while (attr != NULL) {
1198 if (nbAttrs < MAX_ATTR)
1199 attrs[nbAttrs++] = attr;
1200 else
1201 nbAttrs++;
1202 attr = attr->next;
1203 }
1204 }
1205 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1206 ctxt->freeState->nbState--;
1207 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1208 } else {
1209 ret =
1210 (xmlRelaxNGValidStatePtr)
1211 xmlMalloc(sizeof(xmlRelaxNGValidState));
1212 if (ret == NULL) {
1213 xmlRngVErrMemory(ctxt, "allocating states\n");
1214 return (NULL);
1215 }
1216 memset(ret, 0, sizeof(xmlRelaxNGValidState));
1217 }
1218 ret->value = NULL;
1219 ret->endvalue = NULL;
1220 if (node == NULL) {
1221 ret->node = (xmlNodePtr) ctxt->doc;
1222 ret->seq = root;
1223 } else {
1224 ret->node = node;
1225 ret->seq = node->children;
1226 }
1227 ret->nbAttrs = 0;
1228 if (nbAttrs > 0) {
1229 if (ret->attrs == NULL) {
1230 if (nbAttrs < 4)
1231 ret->maxAttrs = 4;
1232 else
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");
1238 return (ret);
1239 }
1240 } else if (ret->maxAttrs < nbAttrs) {
1241 xmlAttrPtr *tmp;
1242
1243 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1244 sizeof(xmlAttrPtr));
1245 if (tmp == NULL) {
1246 xmlRngVErrMemory(ctxt, "allocating states\n");
1247 return (ret);
1248 }
1249 ret->attrs = tmp;
1250 ret->maxAttrs = nbAttrs;
1251 }
1252 ret->nbAttrs = nbAttrs;
1253 if (nbAttrs < MAX_ATTR) {
1254 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1255 } else {
1256 attr = node->properties;
1257 nbAttrs = 0;
1258 while (attr != NULL) {
1259 ret->attrs[nbAttrs++] = attr;
1260 attr = attr->next;
1261 }
1262 }
1263 }
1264 ret->nbAttrLeft = ret->nbAttrs;
1265 return (ret);
1266 }
1267
1268 /**
1269 * xmlRelaxNGCopyValidState:
1270 * @ctxt: a Relax-NG validation context
1271 * @state: a validation state
1272 *
1273 * Copy the validation state
1274 *
1275 * Returns the newly allocated structure or NULL in case or error
1276 */
1277 static xmlRelaxNGValidStatePtr
xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGValidStatePtr state)1278 xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1279 xmlRelaxNGValidStatePtr state)
1280 {
1281 xmlRelaxNGValidStatePtr ret;
1282 unsigned int maxAttrs;
1283 xmlAttrPtr *attrs;
1284
1285 if (state == NULL)
1286 return (NULL);
1287 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1288 ctxt->freeState->nbState--;
1289 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1290 } else {
1291 ret =
1292 (xmlRelaxNGValidStatePtr)
1293 xmlMalloc(sizeof(xmlRelaxNGValidState));
1294 if (ret == NULL) {
1295 xmlRngVErrMemory(ctxt, "allocating states\n");
1296 return (NULL);
1297 }
1298 memset(ret, 0, sizeof(xmlRelaxNGValidState));
1299 }
1300 attrs = ret->attrs;
1301 maxAttrs = ret->maxAttrs;
1302 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1303 ret->attrs = attrs;
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");
1312 ret->nbAttrs = 0;
1313 return (ret);
1314 }
1315 } else if (ret->maxAttrs < state->nbAttrs) {
1316 xmlAttrPtr *tmp;
1317
1318 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1319 sizeof(xmlAttrPtr));
1320 if (tmp == NULL) {
1321 xmlRngVErrMemory(ctxt, "allocating states\n");
1322 ret->nbAttrs = 0;
1323 return (ret);
1324 }
1325 ret->maxAttrs = state->maxAttrs;
1326 ret->attrs = tmp;
1327 }
1328 memcpy(ret->attrs, state->attrs,
1329 state->nbAttrs * sizeof(xmlAttrPtr));
1330 }
1331 return (ret);
1332 }
1333
1334 /**
1335 * xmlRelaxNGEqualValidState:
1336 * @ctxt: a Relax-NG validation context
1337 * @state1: a validation state
1338 * @state2: a validation state
1339 *
1340 * Compare the validation states for equality
1341 *
1342 * Returns 1 if equald, 0 otherwise
1343 */
1344 static int
xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,xmlRelaxNGValidStatePtr state1,xmlRelaxNGValidStatePtr state2)1345 xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1346 xmlRelaxNGValidStatePtr state1,
1347 xmlRelaxNGValidStatePtr state2)
1348 {
1349 int i;
1350
1351 if ((state1 == NULL) || (state2 == NULL))
1352 return (0);
1353 if (state1 == state2)
1354 return (1);
1355 if (state1->node != state2->node)
1356 return (0);
1357 if (state1->seq != state2->seq)
1358 return (0);
1359 if (state1->nbAttrLeft != state2->nbAttrLeft)
1360 return (0);
1361 if (state1->nbAttrs != state2->nbAttrs)
1362 return (0);
1363 if (state1->endvalue != state2->endvalue)
1364 return (0);
1365 if ((state1->value != state2->value) &&
1366 (!xmlStrEqual(state1->value, state2->value)))
1367 return (0);
1368 for (i = 0; i < state1->nbAttrs; i++) {
1369 if (state1->attrs[i] != state2->attrs[i])
1370 return (0);
1371 }
1372 return (1);
1373 }
1374
1375 /**
1376 * xmlRelaxNGFreeValidState:
1377 * @state: a validation state structure
1378 *
1379 * Deallocate a RelaxNG validation state structure.
1380 */
1381 static void
xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGValidStatePtr state)1382 xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1383 xmlRelaxNGValidStatePtr state)
1384 {
1385 if (state == NULL)
1386 return;
1387
1388 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1389 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1390 }
1391 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1392 if (state->attrs != NULL)
1393 xmlFree(state->attrs);
1394 xmlFree(state);
1395 } else {
1396 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1397 }
1398 }
1399
1400 /************************************************************************
1401 * *
1402 * Semi internal functions *
1403 * *
1404 ************************************************************************/
1405
1406 /**
1407 * xmlRelaxParserSetFlag:
1408 * @ctxt: a RelaxNG parser context
1409 * @flags: a set of flags values
1410 *
1411 * Semi private function used to pass informations to a parser context
1412 * which are a combination of xmlRelaxNGParserFlag .
1413 *
1414 * Returns 0 if success and -1 in case of error
1415 */
1416 int
xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt,int flags)1417 xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1418 {
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;
1423 }
1424 if (flags & XML_RELAXNGP_CRNG) {
1425 ctxt->crng |= XML_RELAXNGP_CRNG;
1426 flags -= XML_RELAXNGP_CRNG;
1427 }
1428 if (flags != 0) return(-1);
1429 return(0);
1430 }
1431
1432 /************************************************************************
1433 * *
1434 * Document functions *
1435 * *
1436 ************************************************************************/
1437 static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1438 xmlDocPtr doc);
1439
1440 /**
1441 * xmlRelaxNGIncludePush:
1442 * @ctxt: the parser context
1443 * @value: the element doc
1444 *
1445 * Pushes a new include on top of the include stack
1446 *
1447 * Returns 0 in case of error, the index in the stack otherwise
1448 */
1449 static int
xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGIncludePtr value)1450 xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1451 xmlRelaxNGIncludePtr value)
1452 {
1453 if (ctxt->incTab == NULL) {
1454 ctxt->incMax = 4;
1455 ctxt->incNr = 0;
1456 ctxt->incTab =
1457 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1458 sizeof(ctxt->incTab[0]));
1459 if (ctxt->incTab == NULL) {
1460 xmlRngPErrMemory(ctxt, "allocating include\n");
1461 return (0);
1462 }
1463 }
1464 if (ctxt->incNr >= ctxt->incMax) {
1465 ctxt->incMax *= 2;
1466 ctxt->incTab =
1467 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1468 ctxt->incMax *
1469 sizeof(ctxt->incTab[0]));
1470 if (ctxt->incTab == NULL) {
1471 xmlRngPErrMemory(ctxt, "allocating include\n");
1472 return (0);
1473 }
1474 }
1475 ctxt->incTab[ctxt->incNr] = value;
1476 ctxt->inc = value;
1477 return (ctxt->incNr++);
1478 }
1479
1480 /**
1481 * xmlRelaxNGIncludePop:
1482 * @ctxt: the parser context
1483 *
1484 * Pops the top include from the include stack
1485 *
1486 * Returns the include just removed
1487 */
1488 static xmlRelaxNGIncludePtr
xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)1489 xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1490 {
1491 xmlRelaxNGIncludePtr ret;
1492
1493 if (ctxt->incNr <= 0)
1494 return (NULL);
1495 ctxt->incNr--;
1496 if (ctxt->incNr > 0)
1497 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1498 else
1499 ctxt->inc = NULL;
1500 ret = ctxt->incTab[ctxt->incNr];
1501 ctxt->incTab[ctxt->incNr] = NULL;
1502 return (ret);
1503 }
1504
1505 /**
1506 * xmlRelaxNGRemoveRedefine:
1507 * @ctxt: the parser context
1508 * @URL: the normalized URL
1509 * @target: the included target
1510 * @name: the define name to eliminate
1511 *
1512 * Applies the elimination algorithm of 4.7
1513 *
1514 * Returns 0 in case of error, 1 in case of success.
1515 */
1516 static int
xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,const xmlChar * URL ATTRIBUTE_UNUSED,xmlNodePtr target,const xmlChar * name)1517 xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1518 const xmlChar * URL ATTRIBUTE_UNUSED,
1519 xmlNodePtr target, const xmlChar * name)
1520 {
1521 int found = 0;
1522 xmlNodePtr tmp, tmp2;
1523 xmlChar *name2;
1524
1525 #ifdef DEBUG_INCLUDE
1526 if (name == NULL)
1527 xmlGenericError(xmlGenericErrorContext,
1528 "Elimination of <include> start from %s\n", URL);
1529 else
1530 xmlGenericError(xmlGenericErrorContext,
1531 "Elimination of <include> define %s from %s\n",
1532 name, URL);
1533 #endif
1534 tmp = target;
1535 while (tmp != NULL) {
1536 tmp2 = tmp->next;
1537 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1538 found = 1;
1539 xmlUnlinkNode(tmp);
1540 xmlFreeNode(tmp);
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)) {
1546 found = 1;
1547 xmlUnlinkNode(tmp);
1548 xmlFreeNode(tmp);
1549 }
1550 xmlFree(name2);
1551 }
1552 } else if (IS_RELAXNG(tmp, "include")) {
1553 xmlChar *href = NULL;
1554 xmlRelaxNGDocumentPtr inc = tmp->psvi;
1555
1556 if ((inc != NULL) && (inc->doc != NULL) &&
1557 (inc->doc->children != NULL)) {
1558
1559 if (xmlStrEqual
1560 (inc->doc->children->name, BAD_CAST "grammar")) {
1561 #ifdef DEBUG_INCLUDE
1562 href = xmlGetProp(tmp, BAD_CAST "href");
1563 #endif
1564 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1565 xmlDocGetRootElement(inc->doc)->children,
1566 name) == 1) {
1567 found = 1;
1568 }
1569 #ifdef DEBUG_INCLUDE
1570 if (href != NULL)
1571 xmlFree(href);
1572 #endif
1573 }
1574 }
1575 }
1576 tmp = tmp2;
1577 }
1578 return (found);
1579 }
1580
1581 /**
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.
1587 *
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.
1591 *
1592 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1593 */
1594 static xmlRelaxNGIncludePtr
xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt,const xmlChar * URL,xmlNodePtr node,const xmlChar * ns)1595 xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1596 xmlNodePtr node, const xmlChar * ns)
1597 {
1598 xmlRelaxNGIncludePtr ret = NULL;
1599 xmlDocPtr doc;
1600 int i;
1601 xmlNodePtr root, cur;
1602
1603 #ifdef DEBUG_INCLUDE
1604 xmlGenericError(xmlGenericErrorContext,
1605 "xmlRelaxNGLoadInclude(%s)\n", URL);
1606 #endif
1607
1608 /*
1609 * check against recursion in the stack
1610 */
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,
1615 NULL);
1616 return (NULL);
1617 }
1618 }
1619
1620 /*
1621 * load the document
1622 */
1623 doc = xmlReadFile((const char *) URL,NULL,0);
1624 if (doc == NULL) {
1625 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1626 "xmlRelaxNG: could not load %s\n", URL, NULL);
1627 return (NULL);
1628 }
1629 #ifdef DEBUG_INCLUDE
1630 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
1631 #endif
1632
1633 /*
1634 * Allocate the document structures and register it first.
1635 */
1636 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1637 if (ret == NULL) {
1638 xmlRngPErrMemory(ctxt, "allocating include\n");
1639 xmlFreeDoc(doc);
1640 return (NULL);
1641 }
1642 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1643 ret->doc = doc;
1644 ret->href = xmlStrdup(URL);
1645 ret->next = ctxt->includes;
1646 ctxt->includes = ret;
1647
1648 /*
1649 * transmit the ns if needed
1650 */
1651 if (ns != NULL) {
1652 root = xmlDocGetRootElement(doc);
1653 if (root != NULL) {
1654 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1655 xmlSetProp(root, BAD_CAST "ns", ns);
1656 }
1657 }
1658 }
1659
1660 /*
1661 * push it on the stack
1662 */
1663 xmlRelaxNGIncludePush(ctxt, ret);
1664
1665 /*
1666 * Some preprocessing of the document content, this include recursing
1667 * in the include stack.
1668 */
1669 #ifdef DEBUG_INCLUDE
1670 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
1671 #endif
1672
1673 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1674 if (doc == NULL) {
1675 ctxt->inc = NULL;
1676 return (NULL);
1677 }
1678
1679 /*
1680 * Pop up the include from the stack
1681 */
1682 xmlRelaxNGIncludePop(ctxt);
1683
1684 #ifdef DEBUG_INCLUDE
1685 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
1686 #endif
1687 /*
1688 * Check that the top element is a grammar
1689 */
1690 root = xmlDocGetRootElement(doc);
1691 if (root == NULL) {
1692 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1693 "xmlRelaxNG: included document is empty %s\n", URL,
1694 NULL);
1695 return (NULL);
1696 }
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",
1700 URL, NULL);
1701 return (NULL);
1702 }
1703
1704 /*
1705 * Elimination of redefined rules in the include.
1706 */
1707 cur = node->children;
1708 while (cur != NULL) {
1709 if (IS_RELAXNG(cur, "start")) {
1710 int found = 0;
1711
1712 found =
1713 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1714 if (!found) {
1715 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1716 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1717 URL, NULL);
1718 }
1719 } else if (IS_RELAXNG(cur, "define")) {
1720 xmlChar *name;
1721
1722 name = xmlGetProp(cur, BAD_CAST "name");
1723 if (name == NULL) {
1724 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1725 "xmlRelaxNG: include %s has define without name\n",
1726 URL, NULL);
1727 } else {
1728 int found;
1729
1730 xmlRelaxNGNormExtSpace(name);
1731 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1732 root->children, name);
1733 if (!found) {
1734 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1735 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1736 URL, name);
1737 }
1738 xmlFree(name);
1739 }
1740 }
1741 cur = cur->next;
1742 }
1743
1744
1745 return (ret);
1746 }
1747
1748 /**
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
1755 *
1756 * Pushes a new error on top of the error stack
1757 *
1758 * Returns 0 in case of error, the index in the stack otherwise
1759 */
1760 static int
xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGValidErr err,const xmlChar * arg1,const xmlChar * arg2,int dup)1761 xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1762 xmlRelaxNGValidErr err, const xmlChar * arg1,
1763 const xmlChar * arg2, int dup)
1764 {
1765 xmlRelaxNGValidErrorPtr cur;
1766
1767 #ifdef DEBUG_ERROR
1768 xmlGenericError(xmlGenericErrorContext,
1769 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
1770 #endif
1771 if (ctxt->errTab == NULL) {
1772 ctxt->errMax = 8;
1773 ctxt->errNr = 0;
1774 ctxt->errTab =
1775 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1776 sizeof
1777 (xmlRelaxNGValidError));
1778 if (ctxt->errTab == NULL) {
1779 xmlRngVErrMemory(ctxt, "pushing error\n");
1780 return (0);
1781 }
1782 ctxt->err = NULL;
1783 }
1784 if (ctxt->errNr >= ctxt->errMax) {
1785 ctxt->errMax *= 2;
1786 ctxt->errTab =
1787 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
1788 ctxt->errMax *
1789 sizeof
1790 (xmlRelaxNGValidError));
1791 if (ctxt->errTab == NULL) {
1792 xmlRngVErrMemory(ctxt, "pushing error\n");
1793 return (0);
1794 }
1795 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1796 }
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];
1801 cur->err = err;
1802 if (dup) {
1803 cur->arg1 = xmlStrdup(arg1);
1804 cur->arg2 = xmlStrdup(arg2);
1805 cur->flags = ERROR_IS_DUP;
1806 } else {
1807 cur->arg1 = arg1;
1808 cur->arg2 = arg2;
1809 cur->flags = 0;
1810 }
1811 if (ctxt->state != NULL) {
1812 cur->node = ctxt->state->node;
1813 cur->seq = ctxt->state->seq;
1814 } else {
1815 cur->node = NULL;
1816 cur->seq = NULL;
1817 }
1818 ctxt->err = cur;
1819 return (ctxt->errNr++);
1820 }
1821
1822 /**
1823 * xmlRelaxNGValidErrorPop:
1824 * @ctxt: the validation context
1825 *
1826 * Pops the top error from the error stack
1827 */
1828 static void
xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)1829 xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1830 {
1831 xmlRelaxNGValidErrorPtr cur;
1832
1833 if (ctxt->errNr <= 0) {
1834 ctxt->err = NULL;
1835 return;
1836 }
1837 ctxt->errNr--;
1838 if (ctxt->errNr > 0)
1839 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1840 else
1841 ctxt->err = NULL;
1842 cur = &ctxt->errTab[ctxt->errNr];
1843 if (cur->flags & ERROR_IS_DUP) {
1844 if (cur->arg1 != NULL)
1845 xmlFree((xmlChar *) cur->arg1);
1846 cur->arg1 = NULL;
1847 if (cur->arg2 != NULL)
1848 xmlFree((xmlChar *) cur->arg2);
1849 cur->arg2 = NULL;
1850 cur->flags = 0;
1851 }
1852 }
1853
1854 /**
1855 * xmlRelaxNGDocumentPush:
1856 * @ctxt: the parser context
1857 * @value: the element doc
1858 *
1859 * Pushes a new doc on top of the doc stack
1860 *
1861 * Returns 0 in case of error, the index in the stack otherwise
1862 */
1863 static int
xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDocumentPtr value)1864 xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1865 xmlRelaxNGDocumentPtr value)
1866 {
1867 if (ctxt->docTab == NULL) {
1868 ctxt->docMax = 4;
1869 ctxt->docNr = 0;
1870 ctxt->docTab =
1871 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1872 sizeof(ctxt->docTab[0]));
1873 if (ctxt->docTab == NULL) {
1874 xmlRngPErrMemory(ctxt, "adding document\n");
1875 return (0);
1876 }
1877 }
1878 if (ctxt->docNr >= ctxt->docMax) {
1879 ctxt->docMax *= 2;
1880 ctxt->docTab =
1881 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1882 ctxt->docMax *
1883 sizeof(ctxt->docTab[0]));
1884 if (ctxt->docTab == NULL) {
1885 xmlRngPErrMemory(ctxt, "adding document\n");
1886 return (0);
1887 }
1888 }
1889 ctxt->docTab[ctxt->docNr] = value;
1890 ctxt->doc = value;
1891 return (ctxt->docNr++);
1892 }
1893
1894 /**
1895 * xmlRelaxNGDocumentPop:
1896 * @ctxt: the parser context
1897 *
1898 * Pops the top doc from the doc stack
1899 *
1900 * Returns the doc just removed
1901 */
1902 static xmlRelaxNGDocumentPtr
xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)1903 xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1904 {
1905 xmlRelaxNGDocumentPtr ret;
1906
1907 if (ctxt->docNr <= 0)
1908 return (NULL);
1909 ctxt->docNr--;
1910 if (ctxt->docNr > 0)
1911 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1912 else
1913 ctxt->doc = NULL;
1914 ret = ctxt->docTab[ctxt->docNr];
1915 ctxt->docTab[ctxt->docNr] = NULL;
1916 return (ret);
1917 }
1918
1919 /**
1920 * xmlRelaxNGLoadExternalRef:
1921 * @ctxt: the parser context
1922 * @URL: the normalized URL
1923 * @ns: the inherited ns if any
1924 *
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.
1928 *
1929 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1930 */
1931 static xmlRelaxNGDocumentPtr
xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,const xmlChar * URL,const xmlChar * ns)1932 xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1933 const xmlChar * URL, const xmlChar * ns)
1934 {
1935 xmlRelaxNGDocumentPtr ret = NULL;
1936 xmlDocPtr doc;
1937 xmlNodePtr root;
1938 int i;
1939
1940 /*
1941 * check against recursion in the stack
1942 */
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,
1947 NULL);
1948 return (NULL);
1949 }
1950 }
1951
1952 /*
1953 * load the document
1954 */
1955 doc = xmlReadFile((const char *) URL,NULL,0);
1956 if (doc == NULL) {
1957 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1958 "xmlRelaxNG: could not load %s\n", URL, NULL);
1959 return (NULL);
1960 }
1961
1962 /*
1963 * Allocate the document structures and register it first.
1964 */
1965 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1966 if (ret == NULL) {
1967 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1968 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1969 xmlFreeDoc(doc);
1970 return (NULL);
1971 }
1972 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1973 ret->doc = doc;
1974 ret->href = xmlStrdup(URL);
1975 ret->next = ctxt->documents;
1976 ret->externalRef = 1;
1977 ctxt->documents = ret;
1978
1979 /*
1980 * transmit the ns if needed
1981 */
1982 if (ns != NULL) {
1983 root = xmlDocGetRootElement(doc);
1984 if (root != NULL) {
1985 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1986 xmlSetProp(root, BAD_CAST "ns", ns);
1987 }
1988 }
1989 }
1990
1991 /*
1992 * push it on the stack and register it in the hash table
1993 */
1994 xmlRelaxNGDocumentPush(ctxt, ret);
1995
1996 /*
1997 * Some preprocessing of the document content
1998 */
1999 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
2000 if (doc == NULL) {
2001 ctxt->doc = NULL;
2002 return (NULL);
2003 }
2004
2005 xmlRelaxNGDocumentPop(ctxt);
2006
2007 return (ret);
2008 }
2009
2010 /************************************************************************
2011 * *
2012 * Error functions *
2013 * *
2014 ************************************************************************/
2015
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);
2021
2022 static const char *
xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)2023 xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2024 {
2025 if (def == NULL)
2026 return ("none");
2027 switch (def->type) {
2028 case XML_RELAXNG_EMPTY:
2029 return ("empty");
2030 case XML_RELAXNG_NOT_ALLOWED:
2031 return ("notAllowed");
2032 case XML_RELAXNG_EXCEPT:
2033 return ("except");
2034 case XML_RELAXNG_TEXT:
2035 return ("text");
2036 case XML_RELAXNG_ELEMENT:
2037 return ("element");
2038 case XML_RELAXNG_DATATYPE:
2039 return ("datatype");
2040 case XML_RELAXNG_VALUE:
2041 return ("value");
2042 case XML_RELAXNG_LIST:
2043 return ("list");
2044 case XML_RELAXNG_ATTRIBUTE:
2045 return ("attribute");
2046 case XML_RELAXNG_DEF:
2047 return ("def");
2048 case XML_RELAXNG_REF:
2049 return ("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:
2061 return ("choice");
2062 case XML_RELAXNG_GROUP:
2063 return ("group");
2064 case XML_RELAXNG_INTERLEAVE:
2065 return ("interleave");
2066 case XML_RELAXNG_START:
2067 return ("start");
2068 case XML_RELAXNG_NOOP:
2069 return ("noop");
2070 case XML_RELAXNG_PARAM:
2071 return ("param");
2072 }
2073 return ("unknown");
2074 }
2075
2076 /**
2077 * xmlRelaxNGGetErrorString:
2078 * @err: the error code
2079 * @arg1: the first string argument
2080 * @arg2: the second string argument
2081 *
2082 * computes a formatted error string for the given error code and args
2083 *
2084 * Returns the error string, it must be deallocated by the caller
2085 */
2086 static xmlChar *
xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err,const xmlChar * arg1,const xmlChar * arg2)2087 xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2088 const xmlChar * arg2)
2089 {
2090 char msg[1000];
2091
2092 if (arg1 == NULL)
2093 arg1 = BAD_CAST "";
2094 if (arg2 == NULL)
2095 arg2 = BAD_CAST "";
2096
2097 msg[0] = 0;
2098 switch (err) {
2099 case XML_RELAXNG_OK:
2100 return (NULL);
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);
2105 break;
2106 case XML_RELAXNG_ERR_TYPEVAL:
2107 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2108 arg2);
2109 break;
2110 case XML_RELAXNG_ERR_DUPID:
2111 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2112 break;
2113 case XML_RELAXNG_ERR_TYPECMP:
2114 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2115 break;
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);
2122 break;
2123 case XML_RELAXNG_ERR_LISTEXTRA:
2124 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2125 break;
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);
2133 break;
2134 case XML_RELAXNG_ERR_ELEMNAME:
2135 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2136 arg2);
2137 break;
2138 case XML_RELAXNG_ERR_ELEMNONS:
2139 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2140 arg1);
2141 break;
2142 case XML_RELAXNG_ERR_ELEMWRONGNS:
2143 snprintf(msg, 1000,
2144 "Element %s has wrong namespace: expecting %s\n", arg1,
2145 arg2);
2146 break;
2147 case XML_RELAXNG_ERR_ELEMWRONG:
2148 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2149 break;
2150 case XML_RELAXNG_ERR_TEXTWRONG:
2151 snprintf(msg, 1000,
2152 "Did not expect text in element %s content\n", arg1);
2153 break;
2154 case XML_RELAXNG_ERR_ELEMEXTRANS:
2155 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2156 arg1);
2157 break;
2158 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2159 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2160 break;
2161 case XML_RELAXNG_ERR_NOELEM:
2162 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2163 arg1);
2164 break;
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",
2169 arg1);
2170 break;
2171 case XML_RELAXNG_ERR_CONTENTVALID:
2172 snprintf(msg, 1000, "Element %s failed to validate content\n",
2173 arg1);
2174 break;
2175 case XML_RELAXNG_ERR_EXTRACONTENT:
2176 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2177 arg1, arg2);
2178 break;
2179 case XML_RELAXNG_ERR_INVALIDATTR:
2180 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2181 arg1, arg2);
2182 break;
2183 case XML_RELAXNG_ERR_LACKDATA:
2184 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2185 arg1);
2186 break;
2187 case XML_RELAXNG_ERR_DATAELEM:
2188 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2189 arg1);
2190 break;
2191 case XML_RELAXNG_ERR_VALELEM:
2192 snprintf(msg, 1000, "Value element %s has child elements\n",
2193 arg1);
2194 break;
2195 case XML_RELAXNG_ERR_LISTELEM:
2196 snprintf(msg, 1000, "List element %s has child elements\n",
2197 arg1);
2198 break;
2199 case XML_RELAXNG_ERR_DATATYPE:
2200 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2201 break;
2202 case XML_RELAXNG_ERR_VALUE:
2203 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2204 break;
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"));
2211 default:
2212 return (xmlCharStrdup("Unknown error !\n"));
2213 }
2214 if (msg[0] == 0) {
2215 snprintf(msg, 1000, "Unknown error code %d\n", err);
2216 }
2217 msg[1000 - 1] = 0;
2218 return (xmlStrdup((xmlChar *) msg));
2219 }
2220
2221 /**
2222 * xmlRelaxNGShowValidError:
2223 * @ctxt: the validation context
2224 * @err: the error number
2225 * @node: the node
2226 * @child: the node child generating the problem.
2227 * @arg1: the first argument
2228 * @arg2: the second argument
2229 *
2230 * Show a validation error.
2231 */
2232 static void
xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGValidErr err,xmlNodePtr node,xmlNodePtr child,const xmlChar * arg1,const xmlChar * arg2)2233 xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2234 xmlRelaxNGValidErr err, xmlNodePtr node,
2235 xmlNodePtr child, const xmlChar * arg1,
2236 const xmlChar * arg2)
2237 {
2238 xmlChar *msg;
2239
2240 if (ctxt->flags & FLAGS_NOERROR)
2241 return;
2242
2243 #ifdef DEBUG_ERROR
2244 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
2245 #endif
2246 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2247 if (msg == NULL)
2248 return;
2249
2250 if (ctxt->errNo == XML_RELAXNG_OK)
2251 ctxt->errNo = err;
2252 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2253 (const char *) msg, arg1, arg2);
2254 xmlFree(msg);
2255 }
2256
2257 /**
2258 * xmlRelaxNGPopErrors:
2259 * @ctxt: the validation context
2260 * @level: the error level in the stack
2261 *
2262 * pop and discard all errors until the given level is reached
2263 */
2264 static void
xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt,int level)2265 xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2266 {
2267 int i;
2268 xmlRelaxNGValidErrorPtr err;
2269
2270 #ifdef DEBUG_ERROR
2271 xmlGenericError(xmlGenericErrorContext,
2272 "Pop errors till level %d\n", level);
2273 #endif
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);
2279 err->arg1 = NULL;
2280 if (err->arg2 != NULL)
2281 xmlFree((xmlChar *) err->arg2);
2282 err->arg2 = NULL;
2283 err->flags = 0;
2284 }
2285 }
2286 ctxt->errNr = level;
2287 if (ctxt->errNr <= 0)
2288 ctxt->err = NULL;
2289 }
2290
2291 /**
2292 * xmlRelaxNGDumpValidError:
2293 * @ctxt: the validation context
2294 *
2295 * Show all validation error over a given index.
2296 */
2297 static void
xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)2298 xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2299 {
2300 int i, j, k;
2301 xmlRelaxNGValidErrorPtr err, dup;
2302
2303 #ifdef DEBUG_ERROR
2304 xmlGenericError(xmlGenericErrorContext,
2305 "Dumping error stack %d errors\n", ctxt->errNr);
2306 #endif
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))) {
2315 goto skip;
2316 }
2317 }
2318 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2319 err->arg1, err->arg2);
2320 k++;
2321 }
2322 skip:
2323 if (err->flags & ERROR_IS_DUP) {
2324 if (err->arg1 != NULL)
2325 xmlFree((xmlChar *) err->arg1);
2326 err->arg1 = NULL;
2327 if (err->arg2 != NULL)
2328 xmlFree((xmlChar *) err->arg2);
2329 err->arg2 = NULL;
2330 err->flags = 0;
2331 }
2332 }
2333 ctxt->errNr = 0;
2334 }
2335
2336 /**
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
2343 *
2344 * Register a validation error, either generating it if it's sure
2345 * or stacking it for later handling if unsure.
2346 */
2347 static void
xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGValidErr err,const xmlChar * arg1,const xmlChar * arg2,int dup)2348 xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2349 xmlRelaxNGValidErr err, const xmlChar * arg1,
2350 const xmlChar * arg2, int dup)
2351 {
2352 if (ctxt == NULL)
2353 return;
2354 if (ctxt->flags & FLAGS_NOERROR)
2355 return;
2356
2357 #ifdef DEBUG_ERROR
2358 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
2359 #endif
2360 /*
2361 * generate the error directly
2362 */
2363 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2364 (ctxt->flags & FLAGS_NEGATIVE)) {
2365 xmlNodePtr node, seq;
2366
2367 /*
2368 * Flush first any stacked error which might be the
2369 * real cause of the problem.
2370 */
2371 if (ctxt->errNr != 0)
2372 xmlRelaxNGDumpValidError(ctxt);
2373 if (ctxt->state != NULL) {
2374 node = ctxt->state->node;
2375 seq = ctxt->state->seq;
2376 } else {
2377 node = seq = NULL;
2378 }
2379 if ((node == NULL) && (seq == NULL)) {
2380 node = ctxt->pnode;
2381 }
2382 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2383 }
2384 /*
2385 * Stack the error for later processing if needed
2386 */
2387 else {
2388 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
2389 }
2390 }
2391
2392
2393 /************************************************************************
2394 * *
2395 * Type library hooks *
2396 * *
2397 ************************************************************************/
2398 static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2399 const xmlChar * str);
2400
2401 /**
2402 * xmlRelaxNGSchemaTypeHave:
2403 * @data: data needed for the library
2404 * @type: the type name
2405 *
2406 * Check if the given type is provided by
2407 * the W3C XMLSchema Datatype library.
2408 *
2409 * Returns 1 if yes, 0 if no and -1 in case of error.
2410 */
2411 static int
xmlRelaxNGSchemaTypeHave(void * data ATTRIBUTE_UNUSED,const xmlChar * type)2412 xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2413 {
2414 xmlSchemaTypePtr typ;
2415
2416 if (type == NULL)
2417 return (-1);
2418 typ = xmlSchemaGetPredefinedType(type,
2419 BAD_CAST
2420 "http://www.w3.org/2001/XMLSchema");
2421 if (typ == NULL)
2422 return (0);
2423 return (1);
2424 }
2425
2426 /**
2427 * xmlRelaxNGSchemaTypeCheck:
2428 * @data: data needed for the library
2429 * @type: the type name
2430 * @value: the value to check
2431 * @node: the node
2432 *
2433 * Check if the given type and value are validated by
2434 * the W3C XMLSchema Datatype library.
2435 *
2436 * Returns 1 if yes, 0 if no and -1 in case of error.
2437 */
2438 static int
xmlRelaxNGSchemaTypeCheck(void * data ATTRIBUTE_UNUSED,const xmlChar * type,const xmlChar * value,void ** result,xmlNodePtr node)2439 xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
2440 const xmlChar * type,
2441 const xmlChar * value,
2442 void **result, xmlNodePtr node)
2443 {
2444 xmlSchemaTypePtr typ;
2445 int ret;
2446
2447 if ((type == NULL) || (value == NULL))
2448 return (-1);
2449 typ = xmlSchemaGetPredefinedType(type,
2450 BAD_CAST
2451 "http://www.w3.org/2001/XMLSchema");
2452 if (typ == NULL)
2453 return (-1);
2454 ret = xmlSchemaValPredefTypeNode(typ, value,
2455 (xmlSchemaValPtr *) result, node);
2456 if (ret == 2) /* special ID error code */
2457 return (2);
2458 if (ret == 0)
2459 return (1);
2460 if (ret > 0)
2461 return (0);
2462 return (-1);
2463 }
2464
2465 /**
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
2473 *
2474 * Function provided by a type library to check a value facet
2475 *
2476 * Returns 1 if yes, 0 if no and -1 in case of error.
2477 */
2478 static int
xmlRelaxNGSchemaFacetCheck(void * data ATTRIBUTE_UNUSED,const xmlChar * type,const xmlChar * facetname,const xmlChar * val,const xmlChar * strval,void * value)2479 xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2480 const xmlChar * type, const xmlChar * facetname,
2481 const xmlChar * val, const xmlChar * strval,
2482 void *value)
2483 {
2484 xmlSchemaFacetPtr facet;
2485 xmlSchemaTypePtr typ;
2486 int ret;
2487
2488 if ((type == NULL) || (strval == NULL))
2489 return (-1);
2490 typ = xmlSchemaGetPredefinedType(type,
2491 BAD_CAST
2492 "http://www.w3.org/2001/XMLSchema");
2493 if (typ == NULL)
2494 return (-1);
2495
2496 facet = xmlSchemaNewFacet();
2497 if (facet == NULL)
2498 return (-1);
2499
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;
2524 } else {
2525 xmlSchemaFreeFacet(facet);
2526 return (-1);
2527 }
2528 facet->value = val;
2529 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2530 if (ret != 0) {
2531 xmlSchemaFreeFacet(facet);
2532 return (-1);
2533 }
2534 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2535 xmlSchemaFreeFacet(facet);
2536 if (ret != 0)
2537 return (-1);
2538 return (0);
2539 }
2540
2541 /**
2542 * xmlRelaxNGSchemaFreeValue:
2543 * @data: data needed for the library
2544 * @value: the value to free
2545 *
2546 * Function provided by a type library to free a Schemas value
2547 *
2548 * Returns 1 if yes, 0 if no and -1 in case of error.
2549 */
2550 static void
xmlRelaxNGSchemaFreeValue(void * data ATTRIBUTE_UNUSED,void * value)2551 xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2552 {
2553 xmlSchemaFreeValue(value);
2554 }
2555
2556 /**
2557 * xmlRelaxNGSchemaTypeCompare:
2558 * @data: data needed for the library
2559 * @type: the type name
2560 * @value1: the first value
2561 * @value2: the second value
2562 *
2563 * Compare two values for equality accordingly a type from the W3C XMLSchema
2564 * Datatype library.
2565 *
2566 * Returns 1 if equal, 0 if no and -1 in case of error.
2567 */
2568 static int
xmlRelaxNGSchemaTypeCompare(void * data ATTRIBUTE_UNUSED,const xmlChar * type,const xmlChar * value1,xmlNodePtr ctxt1,void * comp1,const xmlChar * value2,xmlNodePtr ctxt2)2569 xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
2570 const xmlChar * type,
2571 const xmlChar * value1,
2572 xmlNodePtr ctxt1,
2573 void *comp1,
2574 const xmlChar * value2, xmlNodePtr ctxt2)
2575 {
2576 int ret;
2577 xmlSchemaTypePtr typ;
2578 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2579
2580 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2581 return (-1);
2582 typ = xmlSchemaGetPredefinedType(type,
2583 BAD_CAST
2584 "http://www.w3.org/2001/XMLSchema");
2585 if (typ == NULL)
2586 return (-1);
2587 if (comp1 == NULL) {
2588 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2589 if (ret != 0)
2590 return (-1);
2591 if (res1 == NULL)
2592 return (-1);
2593 } else {
2594 res1 = (xmlSchemaValPtr) comp1;
2595 }
2596 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
2597 if (ret != 0) {
2598 if (res1 != (xmlSchemaValPtr) comp1)
2599 xmlSchemaFreeValue(res1);
2600 return (-1);
2601 }
2602 ret = xmlSchemaCompareValues(res1, res2);
2603 if (res1 != (xmlSchemaValPtr) comp1)
2604 xmlSchemaFreeValue(res1);
2605 xmlSchemaFreeValue(res2);
2606 if (ret == -2)
2607 return (-1);
2608 if (ret == 0)
2609 return (1);
2610 return (0);
2611 }
2612
2613 /**
2614 * xmlRelaxNGDefaultTypeHave:
2615 * @data: data needed for the library
2616 * @type: the type name
2617 *
2618 * Check if the given type is provided by
2619 * the default datatype library.
2620 *
2621 * Returns 1 if yes, 0 if no and -1 in case of error.
2622 */
2623 static int
xmlRelaxNGDefaultTypeHave(void * data ATTRIBUTE_UNUSED,const xmlChar * type)2624 xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2625 const xmlChar * type)
2626 {
2627 if (type == NULL)
2628 return (-1);
2629 if (xmlStrEqual(type, BAD_CAST "string"))
2630 return (1);
2631 if (xmlStrEqual(type, BAD_CAST "token"))
2632 return (1);
2633 return (0);
2634 }
2635
2636 /**
2637 * xmlRelaxNGDefaultTypeCheck:
2638 * @data: data needed for the library
2639 * @type: the type name
2640 * @value: the value to check
2641 * @node: the node
2642 *
2643 * Check if the given type and value are validated by
2644 * the default datatype library.
2645 *
2646 * Returns 1 if yes, 0 if no and -1 in case of error.
2647 */
2648 static int
xmlRelaxNGDefaultTypeCheck(void * data ATTRIBUTE_UNUSED,const xmlChar * type ATTRIBUTE_UNUSED,const xmlChar * value ATTRIBUTE_UNUSED,void ** result ATTRIBUTE_UNUSED,xmlNodePtr node ATTRIBUTE_UNUSED)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)
2654 {
2655 if (value == NULL)
2656 return (-1);
2657 if (xmlStrEqual(type, BAD_CAST "string"))
2658 return (1);
2659 if (xmlStrEqual(type, BAD_CAST "token")) {
2660 return (1);
2661 }
2662
2663 return (0);
2664 }
2665
2666 /**
2667 * xmlRelaxNGDefaultTypeCompare:
2668 * @data: data needed for the library
2669 * @type: the type name
2670 * @value1: the first value
2671 * @value2: the second value
2672 *
2673 * Compare two values accordingly a type from the default
2674 * datatype library.
2675 *
2676 * Returns 1 if yes, 0 if no and -1 in case of error.
2677 */
2678 static int
xmlRelaxNGDefaultTypeCompare(void * data ATTRIBUTE_UNUSED,const xmlChar * type,const xmlChar * value1,xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,void * comp1 ATTRIBUTE_UNUSED,const xmlChar * value2,xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)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)
2686 {
2687 int ret = -1;
2688
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;
2694
2695 /*
2696 * TODO: trivial optimizations are possible by
2697 * computing at compile-time
2698 */
2699 nval = xmlRelaxNGNormalize(NULL, value1);
2700 nvalue = xmlRelaxNGNormalize(NULL, value2);
2701
2702 if ((nval == NULL) || (nvalue == NULL))
2703 ret = -1;
2704 else if (xmlStrEqual(nval, nvalue))
2705 ret = 1;
2706 else
2707 ret = 0;
2708 if (nval != NULL)
2709 xmlFree(nval);
2710 if (nvalue != NULL)
2711 xmlFree(nvalue);
2712 } else
2713 ret = 1;
2714 }
2715 return (ret);
2716 }
2717
2718 static int xmlRelaxNGTypeInitialized = 0;
2719 static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2720
2721 /**
2722 * xmlRelaxNGFreeTypeLibrary:
2723 * @lib: the type library structure
2724 * @namespace: the URI bound to the library
2725 *
2726 * Free the structure associated to the type library
2727 */
2728 static void
xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,const xmlChar * namespace ATTRIBUTE_UNUSED)2729 xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2730 const xmlChar * namespace ATTRIBUTE_UNUSED)
2731 {
2732 if (lib == NULL)
2733 return;
2734 if (lib->namespace != NULL)
2735 xmlFree((xmlChar *) lib->namespace);
2736 xmlFree(lib);
2737 }
2738
2739 /**
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
2746 *
2747 * Register a new type library
2748 *
2749 * Returns 0 in case of success and -1 in case of error.
2750 */
2751 static int
xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace,void * data,xmlRelaxNGTypeHave have,xmlRelaxNGTypeCheck check,xmlRelaxNGTypeCompare comp,xmlRelaxNGFacetCheck facet,xmlRelaxNGTypeFree freef)2752 xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2753 xmlRelaxNGTypeHave have,
2754 xmlRelaxNGTypeCheck check,
2755 xmlRelaxNGTypeCompare comp,
2756 xmlRelaxNGFacetCheck facet,
2757 xmlRelaxNGTypeFree freef)
2758 {
2759 xmlRelaxNGTypeLibraryPtr lib;
2760 int ret;
2761
2762 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2763 (check == NULL) || (comp == NULL))
2764 return (-1);
2765 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2766 xmlGenericError(xmlGenericErrorContext,
2767 "Relax-NG types library '%s' already registered\n",
2768 namespace);
2769 return (-1);
2770 }
2771 lib =
2772 (xmlRelaxNGTypeLibraryPtr)
2773 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2774 if (lib == NULL) {
2775 xmlRngVErrMemory(NULL, "adding types library\n");
2776 return (-1);
2777 }
2778 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2779 lib->namespace = xmlStrdup(namespace);
2780 lib->data = data;
2781 lib->have = have;
2782 lib->comp = comp;
2783 lib->check = check;
2784 lib->facet = facet;
2785 lib->freef = freef;
2786 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2787 if (ret < 0) {
2788 xmlGenericError(xmlGenericErrorContext,
2789 "Relax-NG types library failed to register '%s'\n",
2790 namespace);
2791 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2792 return (-1);
2793 }
2794 return (0);
2795 }
2796
2797 /**
2798 * xmlRelaxNGInitTypes:
2799 *
2800 * Initilize the default type libraries.
2801 *
2802 * Returns 0 in case of success and -1 in case of error.
2803 */
2804 int
xmlRelaxNGInitTypes(void)2805 xmlRelaxNGInitTypes(void)
2806 {
2807 if (xmlRelaxNGTypeInitialized != 0)
2808 return (0);
2809 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2810 if (xmlRelaxNGRegisteredTypes == NULL) {
2811 xmlGenericError(xmlGenericErrorContext,
2812 "Failed to allocate sh table for Relax-NG types\n");
2813 return (-1);
2814 }
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,
2826 NULL);
2827 xmlRelaxNGTypeInitialized = 1;
2828 return (0);
2829 }
2830
2831 /**
2832 * xmlRelaxNGCleanupTypes:
2833 *
2834 * Cleanup the default Schemas type library associated to RelaxNG
2835 */
2836 void
xmlRelaxNGCleanupTypes(void)2837 xmlRelaxNGCleanupTypes(void)
2838 {
2839 xmlSchemaCleanupTypes();
2840 if (xmlRelaxNGTypeInitialized == 0)
2841 return;
2842 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2843 xmlRelaxNGFreeTypeLibrary);
2844 xmlRelaxNGTypeInitialized = 0;
2845 }
2846
2847 /************************************************************************
2848 * *
2849 * Compiling element content into regexp *
2850 * *
2851 * Sometime the element content can be compiled into a pure regexp, *
2852 * This allows a faster execution and streamability at that level *
2853 * *
2854 ************************************************************************/
2855
2856 /* from automata.c but not exported */
2857 void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2858
2859
2860 static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2861 xmlRelaxNGDefinePtr def);
2862
2863 /**
2864 * xmlRelaxNGIsCompileable:
2865 * @define: the definition to check
2866 *
2867 * Check if a definition is nullable.
2868 *
2869 * Returns 1 if yes, 0 if no and -1 in case of error
2870 */
2871 static int
xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)2872 xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2873 {
2874 int ret = -1;
2875
2876 if (def == NULL) {
2877 return (-1);
2878 }
2879 if ((def->type != XML_RELAXNG_ELEMENT) &&
2880 (def->dflags & IS_COMPILABLE))
2881 return (1);
2882 if ((def->type != XML_RELAXNG_ELEMENT) &&
2883 (def->dflags & IS_NOT_COMPILABLE))
2884 return (0);
2885 switch (def->type) {
2886 case XML_RELAXNG_NOOP:
2887 ret = xmlRelaxNGIsCompileable(def->content);
2888 break;
2889 case XML_RELAXNG_TEXT:
2890 case XML_RELAXNG_EMPTY:
2891 ret = 1;
2892 break;
2893 case XML_RELAXNG_ELEMENT:
2894 /*
2895 * Check if the element content is compileable
2896 */
2897 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2898 ((def->dflags & IS_COMPILABLE) == 0)) {
2899 xmlRelaxNGDefinePtr list;
2900
2901 list = def->content;
2902 while (list != NULL) {
2903 ret = xmlRelaxNGIsCompileable(list);
2904 if (ret != 1)
2905 break;
2906 list = list->next;
2907 }
2908 /*
2909 * Because the routine is recursive, we must guard against
2910 * discovering both COMPILABLE and NOT_COMPILABLE
2911 */
2912 if (ret == 0) {
2913 def->dflags &= ~IS_COMPILABLE;
2914 def->dflags |= IS_NOT_COMPILABLE;
2915 }
2916 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
2917 def->dflags |= IS_COMPILABLE;
2918 #ifdef DEBUG_COMPILE
2919 if (ret == 1) {
2920 xmlGenericError(xmlGenericErrorContext,
2921 "element content for %s is compilable\n",
2922 def->name);
2923 } else if (ret == 0) {
2924 xmlGenericError(xmlGenericErrorContext,
2925 "element content for %s is not compilable\n",
2926 def->name);
2927 } else {
2928 xmlGenericError(xmlGenericErrorContext,
2929 "Problem in RelaxNGIsCompileable for element %s\n",
2930 def->name);
2931 }
2932 #endif
2933 }
2934 /*
2935 * All elements return a compileable status unless they
2936 * are generic like anyName
2937 */
2938 if ((def->nameClass != NULL) || (def->name == NULL))
2939 ret = 0;
2940 else
2941 ret = 1;
2942 return (ret);
2943 case XML_RELAXNG_REF:
2944 case XML_RELAXNG_EXTERNALREF:
2945 case XML_RELAXNG_PARENTREF:
2946 if (def->depth == -20) {
2947 return (1);
2948 } else {
2949 xmlRelaxNGDefinePtr list;
2950
2951 def->depth = -20;
2952 list = def->content;
2953 while (list != NULL) {
2954 ret = xmlRelaxNGIsCompileable(list);
2955 if (ret != 1)
2956 break;
2957 list = list->next;
2958 }
2959 }
2960 break;
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;
2969
2970 list = def->content;
2971 while (list != NULL) {
2972 ret = xmlRelaxNGIsCompileable(list);
2973 if (ret != 1)
2974 break;
2975 list = list->next;
2976 }
2977 break;
2978 }
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:
2987 ret = 0;
2988 break;
2989 }
2990 if (ret == 0)
2991 def->dflags |= IS_NOT_COMPILABLE;
2992 if (ret == 1)
2993 def->dflags |= IS_COMPILABLE;
2994 #ifdef DEBUG_COMPILE
2995 if (ret == 1) {
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));
3003 } else {
3004 xmlGenericError(xmlGenericErrorContext,
3005 "Problem in RelaxNGIsCompileable %s\n",
3006 xmlRelaxNGDefName(def));
3007 }
3008 #endif
3009 return (ret);
3010 }
3011
3012 /**
3013 * xmlRelaxNGCompile:
3014 * ctxt: the RelaxNG parser context
3015 * @define: the definition tree to compile
3016 *
3017 * Compile the set of definitions, it works recursively, till the
3018 * element boundaries, where it tries to compile the content if possible
3019 *
3020 * Returns 0 if success and -1 in case of error
3021 */
3022 static int
xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDefinePtr def)3023 xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3024 {
3025 int ret = 0;
3026 xmlRelaxNGDefinePtr list;
3027
3028 if ((ctxt == NULL) || (def == NULL))
3029 return (-1);
3030
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;
3036
3037 def->depth = -25;
3038
3039 list = def->content;
3040 ctxt->am = xmlNewAutomata();
3041 if (ctxt->am == NULL)
3042 return (-1);
3043
3044 /*
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.
3051 */
3052 xmlAutomataSetFlags(ctxt->am, 1);
3053
3054 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3055 while (list != NULL) {
3056 xmlRelaxNGCompile(ctxt, list);
3057 list = list->next;
3058 }
3059 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3060 if (xmlAutomataIsDeterminist(ctxt->am))
3061 def->contModel = xmlAutomataCompile(ctxt->am);
3062
3063 xmlFreeAutomata(ctxt->am);
3064 ctxt->state = oldstate;
3065 ctxt->am = oldam;
3066 }
3067 break;
3068 case XML_RELAXNG_ELEMENT:
3069 if ((ctxt->am != NULL) && (def->name != NULL)) {
3070 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3071 ctxt->state, NULL,
3072 def->name, def->ns,
3073 def);
3074 }
3075 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3076 xmlAutomataPtr oldam = ctxt->am;
3077 xmlAutomataStatePtr oldstate = ctxt->state;
3078
3079 def->depth = -25;
3080
3081 list = def->content;
3082 ctxt->am = xmlNewAutomata();
3083 if (ctxt->am == NULL)
3084 return (-1);
3085 xmlAutomataSetFlags(ctxt->am, 1);
3086 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3087 while (list != NULL) {
3088 xmlRelaxNGCompile(ctxt, list);
3089 list = list->next;
3090 }
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",
3097 def->name);
3098 #endif
3099 /*
3100 * we can only use the automata if it is determinist
3101 */
3102 xmlRegFreeRegexp(def->contModel);
3103 def->contModel = NULL;
3104 }
3105 xmlFreeAutomata(ctxt->am);
3106 ctxt->state = oldstate;
3107 ctxt->am = oldam;
3108 } else {
3109 xmlAutomataPtr oldam = ctxt->am;
3110
3111 /*
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.
3115 */
3116 ret = xmlRelaxNGTryCompile(ctxt, def);
3117 ctxt->am = oldam;
3118 }
3119 break;
3120 case XML_RELAXNG_NOOP:
3121 ret = xmlRelaxNGCompile(ctxt, def->content);
3122 break;
3123 case XML_RELAXNG_OPTIONAL:{
3124 xmlAutomataStatePtr oldstate = ctxt->state;
3125
3126 list = def->content;
3127 while (list != NULL) {
3128 xmlRelaxNGCompile(ctxt, list);
3129 list = list->next;
3130 }
3131 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3132 break;
3133 }
3134 case XML_RELAXNG_ZEROORMORE:{
3135 xmlAutomataStatePtr oldstate;
3136
3137 ctxt->state =
3138 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3139 oldstate = ctxt->state;
3140 list = def->content;
3141 while (list != NULL) {
3142 xmlRelaxNGCompile(ctxt, list);
3143 list = list->next;
3144 }
3145 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3146 ctxt->state =
3147 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3148 break;
3149 }
3150 case XML_RELAXNG_ONEORMORE:{
3151 xmlAutomataStatePtr oldstate;
3152
3153 list = def->content;
3154 while (list != NULL) {
3155 xmlRelaxNGCompile(ctxt, list);
3156 list = list->next;
3157 }
3158 oldstate = ctxt->state;
3159 list = def->content;
3160 while (list != NULL) {
3161 xmlRelaxNGCompile(ctxt, list);
3162 list = list->next;
3163 }
3164 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3165 ctxt->state =
3166 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3167 break;
3168 }
3169 case XML_RELAXNG_CHOICE:{
3170 xmlAutomataStatePtr target = NULL;
3171 xmlAutomataStatePtr oldstate = ctxt->state;
3172
3173 list = def->content;
3174 while (list != NULL) {
3175 ctxt->state = oldstate;
3176 ret = xmlRelaxNGCompile(ctxt, list);
3177 if (ret != 0)
3178 break;
3179 if (target == NULL)
3180 target = ctxt->state;
3181 else {
3182 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3183 target);
3184 }
3185 list = list->next;
3186 }
3187 ctxt->state = target;
3188
3189 break;
3190 }
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);
3199 if (ret != 0)
3200 break;
3201 list = list->next;
3202 }
3203 break;
3204 case XML_RELAXNG_TEXT:{
3205 xmlAutomataStatePtr oldstate;
3206
3207 ctxt->state =
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",
3213 NULL);
3214 ctxt->state =
3215 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3216 break;
3217 }
3218 case XML_RELAXNG_EMPTY:
3219 ctxt->state =
3220 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3221 break;
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));
3233 break;
3234 }
3235 return (ret);
3236 }
3237
3238 /**
3239 * xmlRelaxNGTryCompile:
3240 * ctxt: the RelaxNG parser context
3241 * @define: the definition tree to compile
3242 *
3243 * Try to compile the set of definitions, it works recursively,
3244 * possibly ignoring parts which cannot be compiled.
3245 *
3246 * Returns 0 if success and -1 in case of error
3247 */
3248 static int
xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDefinePtr def)3249 xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3250 {
3251 int ret = 0;
3252 xmlRelaxNGDefinePtr list;
3253
3254 if ((ctxt == NULL) || (def == NULL))
3255 return (-1);
3256
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)) {
3261 ctxt->am = NULL;
3262 ret = xmlRelaxNGCompile(ctxt, def);
3263 #ifdef DEBUG_PROGRESSIVE
3264 if (ret == 0) {
3265 if (def->type == XML_RELAXNG_START)
3266 xmlGenericError(xmlGenericErrorContext,
3267 "compiled the start\n");
3268 else
3269 xmlGenericError(xmlGenericErrorContext,
3270 "compiled element %s\n", def->name);
3271 } else {
3272 if (def->type == XML_RELAXNG_START)
3273 xmlGenericError(xmlGenericErrorContext,
3274 "failed to compile the start\n");
3275 else
3276 xmlGenericError(xmlGenericErrorContext,
3277 "failed to compile element %s\n",
3278 def->name);
3279 }
3280 #endif
3281 return (ret);
3282 }
3283 }
3284 switch (def->type) {
3285 case XML_RELAXNG_NOOP:
3286 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3287 break;
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:
3295 ret = 0;
3296 break;
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);
3310 if (ret != 0)
3311 break;
3312 list = list->next;
3313 }
3314 break;
3315 case XML_RELAXNG_EXCEPT:
3316 case XML_RELAXNG_ATTRIBUTE:
3317 case XML_RELAXNG_INTERLEAVE:
3318 case XML_RELAXNG_NOT_ALLOWED:
3319 ret = 0;
3320 break;
3321 }
3322 return (ret);
3323 }
3324
3325 /************************************************************************
3326 * *
3327 * Parsing functions *
3328 * *
3329 ************************************************************************/
3330
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,
3337 int group);
3338 static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3339 ctxt, xmlNodePtr node);
3340 static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3341 xmlNodePtr node);
3342 static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3343 xmlNodePtr nodes);
3344 static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3345 ctxt, xmlNodePtr node,
3346 xmlRelaxNGDefinePtr
3347 def);
3348 static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3349 ctxt, xmlNodePtr nodes);
3350 static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3351 xmlRelaxNGDefinePtr define,
3352 xmlNodePtr elem);
3353
3354
3355 #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
3356
3357 /**
3358 * xmlRelaxNGIsNullable:
3359 * @define: the definition to verify
3360 *
3361 * Check if a definition is nullable.
3362 *
3363 * Returns 1 if yes, 0 if no and -1 in case of error
3364 */
3365 static int
xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)3366 xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3367 {
3368 int ret;
3369
3370 if (define == NULL)
3371 return (-1);
3372
3373 if (define->dflags & IS_NULLABLE)
3374 return (1);
3375 if (define->dflags & IS_NOT_NULLABLE)
3376 return (0);
3377 switch (define->type) {
3378 case XML_RELAXNG_EMPTY:
3379 case XML_RELAXNG_TEXT:
3380 ret = 1;
3381 break;
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);
3389 break;
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:
3398 ret = 0;
3399 break;
3400 case XML_RELAXNG_CHOICE:{
3401 xmlRelaxNGDefinePtr list = define->content;
3402
3403 while (list != NULL) {
3404 ret = xmlRelaxNGIsNullable(list);
3405 if (ret != 0)
3406 goto done;
3407 list = list->next;
3408 }
3409 ret = 0;
3410 break;
3411 }
3412 case XML_RELAXNG_START:
3413 case XML_RELAXNG_INTERLEAVE:
3414 case XML_RELAXNG_GROUP:{
3415 xmlRelaxNGDefinePtr list = define->content;
3416
3417 while (list != NULL) {
3418 ret = xmlRelaxNGIsNullable(list);
3419 if (ret != 1)
3420 goto done;
3421 list = list->next;
3422 }
3423 return (1);
3424 }
3425 default:
3426 return (-1);
3427 }
3428 done:
3429 if (ret == 0)
3430 define->dflags |= IS_NOT_NULLABLE;
3431 if (ret == 1)
3432 define->dflags |= IS_NULLABLE;
3433 return (ret);
3434 }
3435
3436 /**
3437 * xmlRelaxNGIsBlank:
3438 * @str: a string
3439 *
3440 * Check if a string is ignorable c.f. 4.2. Whitespace
3441 *
3442 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3443 */
3444 static int
xmlRelaxNGIsBlank(xmlChar * str)3445 xmlRelaxNGIsBlank(xmlChar * str)
3446 {
3447 if (str == NULL)
3448 return (1);
3449 while (*str != 0) {
3450 if (!(IS_BLANK_CH(*str)))
3451 return (0);
3452 str++;
3453 }
3454 return (1);
3455 }
3456
3457 /**
3458 * xmlRelaxNGGetDataTypeLibrary:
3459 * @ctxt: a Relax-NG parser context
3460 * @node: the current data or value element
3461 *
3462 * Applies algorithm from 4.3. datatypeLibrary attribute
3463 *
3464 * Returns the datatypeLibary value or NULL if not found
3465 */
3466 static xmlChar *
xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,xmlNodePtr node)3467 xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3468 xmlNodePtr node)
3469 {
3470 xmlChar *ret, *escape;
3471
3472 if (node == NULL)
3473 return(NULL);
3474
3475 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
3476 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3477 if (ret != NULL) {
3478 if (ret[0] == 0) {
3479 xmlFree(ret);
3480 return (NULL);
3481 }
3482 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3483 if (escape == NULL) {
3484 return (ret);
3485 }
3486 xmlFree(ret);
3487 return (escape);
3488 }
3489 }
3490 node = node->parent;
3491 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
3492 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3493 if (ret != NULL) {
3494 if (ret[0] == 0) {
3495 xmlFree(ret);
3496 return (NULL);
3497 }
3498 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3499 if (escape == NULL) {
3500 return (ret);
3501 }
3502 xmlFree(ret);
3503 return (escape);
3504 }
3505 node = node->parent;
3506 }
3507 return (NULL);
3508 }
3509
3510 /**
3511 * xmlRelaxNGParseValue:
3512 * @ctxt: a Relax-NG parser context
3513 * @node: the data node.
3514 *
3515 * parse the content of a RelaxNG value node.
3516 *
3517 * Returns the definition pointer or NULL in case of error
3518 */
3519 static xmlRelaxNGDefinePtr
xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)3520 xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3521 {
3522 xmlRelaxNGDefinePtr def = NULL;
3523 xmlRelaxNGTypeLibraryPtr lib = NULL;
3524 xmlChar *type;
3525 xmlChar *library;
3526 int success = 0;
3527
3528 def = xmlRelaxNGNewDefine(ctxt, node);
3529 if (def == NULL)
3530 return (NULL);
3531 def->type = XML_RELAXNG_VALUE;
3532
3533 type = xmlGetProp(node, BAD_CAST "type");
3534 if (type != NULL) {
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);
3539 }
3540 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3541 if (library == NULL)
3542 library =
3543 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3544
3545 def->name = type;
3546 def->ns = library;
3547
3548 lib = (xmlRelaxNGTypeLibraryPtr)
3549 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3550 if (lib == NULL) {
3551 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3552 "Use of unregistered type library '%s'\n", library,
3553 NULL);
3554 def->data = NULL;
3555 } else {
3556 def->data = lib;
3557 if (lib->have == NULL) {
3558 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3559 "Internal error with type library '%s': no 'have'\n",
3560 library, NULL);
3561 } else {
3562 success = lib->have(lib->data, def->name);
3563 if (success != 1) {
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);
3567 }
3568 }
3569 }
3570 }
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",
3578 NULL, NULL);
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)) {
3585 void *val = NULL;
3586
3587 success =
3588 lib->check(lib->data, def->name, def->value, &val, node);
3589 if (success != 1) {
3590 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3591 "Value '%s' is not acceptable for type '%s'\n",
3592 def->value, def->name);
3593 } else {
3594 if (val != NULL)
3595 def->attrs = val;
3596 }
3597 }
3598 }
3599 return (def);
3600 }
3601
3602 /**
3603 * xmlRelaxNGParseData:
3604 * @ctxt: a Relax-NG parser context
3605 * @node: the data node.
3606 *
3607 * parse the content of a RelaxNG data node.
3608 *
3609 * Returns the definition pointer or NULL in case of error
3610 */
3611 static xmlRelaxNGDefinePtr
xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)3612 xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3613 {
3614 xmlRelaxNGDefinePtr def = NULL, except;
3615 xmlRelaxNGDefinePtr param, lastparam = NULL;
3616 xmlRelaxNGTypeLibraryPtr lib;
3617 xmlChar *type;
3618 xmlChar *library;
3619 xmlNodePtr content;
3620 int tmp;
3621
3622 type = xmlGetProp(node, BAD_CAST "type");
3623 if (type == NULL) {
3624 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3625 NULL);
3626 return (NULL);
3627 }
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);
3632 }
3633 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3634 if (library == NULL)
3635 library =
3636 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3637
3638 def = xmlRelaxNGNewDefine(ctxt, node);
3639 if (def == NULL) {
3640 xmlFree(type);
3641 return (NULL);
3642 }
3643 def->type = XML_RELAXNG_DATATYPE;
3644 def->name = type;
3645 def->ns = library;
3646
3647 lib = (xmlRelaxNGTypeLibraryPtr)
3648 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3649 if (lib == NULL) {
3650 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3651 "Use of unregistered type library '%s'\n", library,
3652 NULL);
3653 def->data = NULL;
3654 } else {
3655 def->data = lib;
3656 if (lib->have == NULL) {
3657 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3658 "Internal error with type library '%s': no 'have'\n",
3659 library, NULL);
3660 } else {
3661 tmp = lib->have(lib->data, def->name);
3662 if (tmp != 1) {
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);
3666 } else
3667 if ((xmlStrEqual
3668 (library,
3669 BAD_CAST
3670 "http://www.w3.org/2001/XMLSchema-datatypes"))
3671 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3672 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3673 ctxt->idref = 1;
3674 }
3675 }
3676 }
3677 content = node->children;
3678
3679 /*
3680 * Handle optional params
3681 */
3682 while (content != NULL) {
3683 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3684 break;
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",
3689 library, NULL);
3690 content = content->next;
3691 while ((content != NULL) &&
3692 (xmlStrEqual(content->name, BAD_CAST "param")))
3693 content = content->next;
3694 } else {
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);
3702 }
3703 param->value = xmlNodeGetContent(content);
3704 if (lastparam == NULL) {
3705 def->attrs = lastparam = param;
3706 } else {
3707 lastparam->next = param;
3708 lastparam = param;
3709 }
3710 if (lib != NULL) {
3711 }
3712 }
3713 content = content->next;
3714 }
3715 }
3716 /*
3717 * Handle optional except
3718 */
3719 if ((content != NULL)
3720 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3721 xmlNodePtr child;
3722 xmlRelaxNGDefinePtr tmp2, last = NULL;
3723
3724 except = xmlRelaxNGNewDefine(ctxt, node);
3725 if (except == NULL) {
3726 return (def);
3727 }
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);
3734 }
3735 while (child != NULL) {
3736 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3737 if (tmp2 != NULL) {
3738 if (last == NULL) {
3739 except->content = last = tmp2;
3740 } else {
3741 last->next = tmp2;
3742 last = tmp2;
3743 }
3744 }
3745 child = child->next;
3746 }
3747 content = content->next;
3748 }
3749 /*
3750 * Check there is no unhandled data
3751 */
3752 if (content != NULL) {
3753 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3754 "Element data has unexpected content %s\n",
3755 content->name, NULL);
3756 }
3757
3758 return (def);
3759 }
3760
3761 static const xmlChar *invalidName = BAD_CAST "\1";
3762
3763 /**
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
3769 *
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
3773 *
3774 * Returns 1 disttinct, 0 if equal
3775 */
3776 static int
xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,xmlRelaxNGDefinePtr def2)3777 xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
3778 xmlRelaxNGDefinePtr def2)
3779 {
3780 int ret = 1;
3781 xmlNode node;
3782 xmlNs ns;
3783 xmlRelaxNGValidCtxt ctxt;
3784
3785 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3786
3787 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3788
3789 if ((def1->type == XML_RELAXNG_ELEMENT) ||
3790 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3791 if (def2->type == XML_RELAXNG_TEXT)
3792 return (1);
3793 if (def1->name != NULL) {
3794 node.name = def1->name;
3795 } else {
3796 node.name = invalidName;
3797 }
3798 if (def1->ns != NULL) {
3799 if (def1->ns[0] == 0) {
3800 node.ns = NULL;
3801 } else {
3802 node.ns = &ns;
3803 ns.href = def1->ns;
3804 }
3805 } else {
3806 node.ns = NULL;
3807 }
3808 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
3809 if (def1->nameClass != NULL) {
3810 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3811 } else {
3812 ret = 0;
3813 }
3814 } else {
3815 ret = 1;
3816 }
3817 } else if (def1->type == XML_RELAXNG_TEXT) {
3818 if (def2->type == XML_RELAXNG_TEXT)
3819 return (0);
3820 return (1);
3821 } else if (def1->type == XML_RELAXNG_EXCEPT) {
3822 ret = xmlRelaxNGCompareNameClasses(def1->content, def2);
3823 if (ret == 0)
3824 ret = 1;
3825 else if (ret == 1)
3826 ret = 0;
3827 } else {
3828 TODO ret = 0;
3829 }
3830 if (ret == 0)
3831 return (ret);
3832 if ((def2->type == XML_RELAXNG_ELEMENT) ||
3833 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3834 if (def2->name != NULL) {
3835 node.name = def2->name;
3836 } else {
3837 node.name = invalidName;
3838 }
3839 node.ns = &ns;
3840 if (def2->ns != NULL) {
3841 if (def2->ns[0] == 0) {
3842 node.ns = NULL;
3843 } else {
3844 ns.href = def2->ns;
3845 }
3846 } else {
3847 ns.href = invalidName;
3848 }
3849 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
3850 if (def2->nameClass != NULL) {
3851 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3852 } else {
3853 ret = 0;
3854 }
3855 } else {
3856 ret = 1;
3857 }
3858 } else {
3859 TODO ret = 0;
3860 }
3861
3862 return (ret);
3863 }
3864
3865 /**
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
3870 *
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
3874 *
3875 * Returns 1 disttinct, 0 if equal
3876 */
3877 static int
xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,xmlRelaxNGDefinePtr * def1,xmlRelaxNGDefinePtr * def2)3878 xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3879 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3880 xmlRelaxNGDefinePtr * def2)
3881 {
3882 xmlRelaxNGDefinePtr *basedef2 = def2;
3883
3884 if ((def1 == NULL) || (def2 == NULL))
3885 return (1);
3886 if ((*def1 == NULL) || (*def2 == NULL))
3887 return (1);
3888 while (*def1 != NULL) {
3889 while ((*def2) != NULL) {
3890 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3891 return (0);
3892 def2++;
3893 }
3894 def2 = basedef2;
3895 def1++;
3896 }
3897 return (1);
3898 }
3899
3900 /**
3901 * xmlRelaxNGGenerateAttributes:
3902 * @ctxt: a Relax-NG parser context
3903 * @def: the definition definition
3904 *
3905 * Check if the definition can only generate attributes
3906 *
3907 * Returns 1 if yes, 0 if no and -1 in case of error.
3908 */
3909 static int
xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDefinePtr def)3910 xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
3911 xmlRelaxNGDefinePtr def)
3912 {
3913 xmlRelaxNGDefinePtr parent, cur, tmp;
3914
3915 /*
3916 * Don't run that check in case of error. Infinite recursion
3917 * becomes possible.
3918 */
3919 if (ctxt->nbErrors != 0)
3920 return (-1);
3921
3922 parent = NULL;
3923 cur = def;
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))
3932 return (0);
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) {
3944 parent = cur;
3945 cur = cur->content;
3946 tmp = cur;
3947 while (tmp != NULL) {
3948 tmp->parent = parent;
3949 tmp = tmp->next;
3950 }
3951 continue;
3952 }
3953 }
3954 if (cur == def)
3955 break;
3956 if (cur->next != NULL) {
3957 cur = cur->next;
3958 continue;
3959 }
3960 do {
3961 cur = cur->parent;
3962 if (cur == NULL)
3963 break;
3964 if (cur == def)
3965 return (1);
3966 if (cur->next != NULL) {
3967 cur = cur->next;
3968 break;
3969 }
3970 } while (cur != NULL);
3971 }
3972 return (1);
3973 }
3974
3975 /**
3976 * xmlRelaxNGGetElements:
3977 * @ctxt: a Relax-NG parser context
3978 * @def: the definition definition
3979 * @eora: gather elements (0) or attributes (1)
3980 *
3981 * Compute the list of top elements a definition can generate
3982 *
3983 * Returns a list of elements or NULL if none was found.
3984 */
3985 static xmlRelaxNGDefinePtr *
xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDefinePtr def,int eora)3986 xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
3987 xmlRelaxNGDefinePtr def, int eora)
3988 {
3989 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
3990 int len = 0;
3991 int max = 0;
3992
3993 /*
3994 * Don't run that check in case of error. Infinite recursion
3995 * becomes possible.
3996 */
3997 if (ctxt->nbErrors != 0)
3998 return (NULL);
3999
4000 parent = NULL;
4001 cur = def;
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))) {
4006 if (ret == NULL) {
4007 max = 10;
4008 ret = (xmlRelaxNGDefinePtr *)
4009 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
4010 if (ret == NULL) {
4011 xmlRngPErrMemory(ctxt, "getting element list\n");
4012 return (NULL);
4013 }
4014 } else if (max <= len) {
4015 xmlRelaxNGDefinePtr *temp;
4016
4017 max *= 2;
4018 temp = xmlRealloc(ret,
4019 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
4020 if (temp == NULL) {
4021 xmlRngPErrMemory(ctxt, "getting element list\n");
4022 xmlFree(ret);
4023 return (NULL);
4024 }
4025 ret = temp;
4026 }
4027 ret[len++] = cur;
4028 ret[len] = NULL;
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)) {
4039 /*
4040 * Don't go within elements or attributes or string values.
4041 * Just gather the element top list
4042 */
4043 if (cur->content != NULL) {
4044 parent = cur;
4045 cur = cur->content;
4046 tmp = cur;
4047 while (tmp != NULL) {
4048 tmp->parent = parent;
4049 tmp = tmp->next;
4050 }
4051 continue;
4052 }
4053 }
4054 if (cur == def)
4055 break;
4056 if (cur->next != NULL) {
4057 cur = cur->next;
4058 continue;
4059 }
4060 do {
4061 cur = cur->parent;
4062 if (cur == NULL)
4063 break;
4064 if (cur == def)
4065 return (ret);
4066 if (cur->next != NULL) {
4067 cur = cur->next;
4068 break;
4069 }
4070 } while (cur != NULL);
4071 }
4072 return (ret);
4073 }
4074
4075 /**
4076 * xmlRelaxNGCheckChoiceDeterminism:
4077 * @ctxt: a Relax-NG parser context
4078 * @def: the choice definition
4079 *
4080 * Also used to find indeterministic pattern in choice
4081 */
4082 static void
xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDefinePtr def)4083 xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
4084 xmlRelaxNGDefinePtr def)
4085 {
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;
4092 int is_triable = 1;
4093
4094 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4095 return;
4096
4097 if (def->dflags & IS_PROCESSED)
4098 return;
4099
4100 /*
4101 * Don't run that check in case of error. Infinite recursion
4102 * becomes possible.
4103 */
4104 if (ctxt->nbErrors != 0)
4105 return;
4106
4107 is_nullable = xmlRelaxNGIsNullable(def);
4108
4109 cur = def->content;
4110 while (cur != NULL) {
4111 nbchild++;
4112 cur = cur->next;
4113 }
4114
4115 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4116 sizeof(xmlRelaxNGDefinePtr
4117 *));
4118 if (list == NULL) {
4119 xmlRngPErrMemory(ctxt, "building choice\n");
4120 return;
4121 }
4122 i = 0;
4123 /*
4124 * a bit strong but safe
4125 */
4126 if (is_nullable == 0) {
4127 triage = xmlHashCreate(10);
4128 } else {
4129 is_triable = 0;
4130 }
4131 cur = def->content;
4132 while (cur != NULL) {
4133 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4134 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4135 is_triable = 0;
4136 } else if (is_triable == 1) {
4137 xmlRelaxNGDefinePtr *tmp;
4138 int res;
4139
4140 tmp = list[i];
4141 while ((*tmp != NULL) && (is_triable == 1)) {
4142 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4143 res = xmlHashAddEntry2(triage,
4144 BAD_CAST "#text", NULL,
4145 (void *) cur);
4146 if (res != 0)
4147 is_triable = -1;
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,
4152 (*tmp)->name, NULL,
4153 (void *) cur);
4154 else
4155 res = xmlHashAddEntry2(triage,
4156 (*tmp)->name, (*tmp)->ns,
4157 (void *) cur);
4158 if (res != 0)
4159 is_triable = -1;
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,
4164 (void *) cur);
4165 else
4166 res = xmlHashAddEntry2(triage,
4167 BAD_CAST "#any", (*tmp)->ns,
4168 (void *) cur);
4169 if (res != 0)
4170 is_triable = -1;
4171 } else {
4172 is_triable = -1;
4173 }
4174 tmp++;
4175 }
4176 }
4177 i++;
4178 cur = cur->next;
4179 }
4180
4181 for (i = 0; i < nbchild; i++) {
4182 if (list[i] == NULL)
4183 continue;
4184 for (j = 0; j < i; j++) {
4185 if (list[j] == NULL)
4186 continue;
4187 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4188 if (ret == 0) {
4189 is_indeterminist = 1;
4190 }
4191 }
4192 }
4193 for (i = 0; i < nbchild; i++) {
4194 if (list[i] != NULL)
4195 xmlFree(list[i]);
4196 }
4197
4198 xmlFree(list);
4199 if (is_indeterminist) {
4200 def->dflags |= IS_INDETERMINIST;
4201 }
4202 if (is_triable == 1) {
4203 def->dflags |= IS_TRIABLE;
4204 def->data = triage;
4205 } else if (triage != NULL) {
4206 xmlHashFree(triage, NULL);
4207 }
4208 def->dflags |= IS_PROCESSED;
4209 }
4210
4211 /**
4212 * xmlRelaxNGCheckGroupAttrs:
4213 * @ctxt: a Relax-NG parser context
4214 * @def: the group definition
4215 *
4216 * Detects violations of rule 7.3
4217 */
4218 static void
xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDefinePtr def)4219 xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
4220 xmlRelaxNGDefinePtr def)
4221 {
4222 xmlRelaxNGDefinePtr **list;
4223 xmlRelaxNGDefinePtr cur;
4224 int nbchild = 0, i, j, ret;
4225
4226 if ((def == NULL) ||
4227 ((def->type != XML_RELAXNG_GROUP) &&
4228 (def->type != XML_RELAXNG_ELEMENT)))
4229 return;
4230
4231 if (def->dflags & IS_PROCESSED)
4232 return;
4233
4234 /*
4235 * Don't run that check in case of error. Infinite recursion
4236 * becomes possible.
4237 */
4238 if (ctxt->nbErrors != 0)
4239 return;
4240
4241 cur = def->attrs;
4242 while (cur != NULL) {
4243 nbchild++;
4244 cur = cur->next;
4245 }
4246 cur = def->content;
4247 while (cur != NULL) {
4248 nbchild++;
4249 cur = cur->next;
4250 }
4251
4252 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4253 sizeof(xmlRelaxNGDefinePtr
4254 *));
4255 if (list == NULL) {
4256 xmlRngPErrMemory(ctxt, "building group\n");
4257 return;
4258 }
4259 i = 0;
4260 cur = def->attrs;
4261 while (cur != NULL) {
4262 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4263 i++;
4264 cur = cur->next;
4265 }
4266 cur = def->content;
4267 while (cur != NULL) {
4268 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4269 i++;
4270 cur = cur->next;
4271 }
4272
4273 for (i = 0; i < nbchild; i++) {
4274 if (list[i] == NULL)
4275 continue;
4276 for (j = 0; j < i; j++) {
4277 if (list[j] == NULL)
4278 continue;
4279 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4280 if (ret == 0) {
4281 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4282 "Attributes conflicts in group\n", NULL, NULL);
4283 }
4284 }
4285 }
4286 for (i = 0; i < nbchild; i++) {
4287 if (list[i] != NULL)
4288 xmlFree(list[i]);
4289 }
4290
4291 xmlFree(list);
4292 def->dflags |= IS_PROCESSED;
4293 }
4294
4295 /**
4296 * xmlRelaxNGComputeInterleaves:
4297 * @def: the interleave definition
4298 * @ctxt: a Relax-NG parser context
4299 * @name: the definition name
4300 *
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
4308 * algorithm
4309 */
4310 static void
xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,xmlRelaxNGParserCtxtPtr ctxt,xmlChar * name ATTRIBUTE_UNUSED)4311 xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
4312 xmlRelaxNGParserCtxtPtr ctxt,
4313 xmlChar * name ATTRIBUTE_UNUSED)
4314 {
4315 xmlRelaxNGDefinePtr cur, *tmp;
4316
4317 xmlRelaxNGPartitionPtr partitions = NULL;
4318 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4319 xmlRelaxNGInterleaveGroupPtr group;
4320 int i, j, ret, res;
4321 int nbgroups = 0;
4322 int nbchild = 0;
4323 int is_mixed = 0;
4324 int is_determinist = 1;
4325
4326 /*
4327 * Don't run that check in case of error. Infinite recursion
4328 * becomes possible.
4329 */
4330 if (ctxt->nbErrors != 0)
4331 return;
4332
4333 #ifdef DEBUG_INTERLEAVE
4334 xmlGenericError(xmlGenericErrorContext,
4335 "xmlRelaxNGComputeInterleaves(%s)\n", name);
4336 #endif
4337 cur = def->content;
4338 while (cur != NULL) {
4339 nbchild++;
4340 cur = cur->next;
4341 }
4342
4343 #ifdef DEBUG_INTERLEAVE
4344 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4345 #endif
4346 groups = (xmlRelaxNGInterleaveGroupPtr *)
4347 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
4348 if (groups == NULL)
4349 goto error;
4350 cur = def->content;
4351 while (cur != NULL) {
4352 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4353 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4354 if (groups[nbgroups] == NULL)
4355 goto error;
4356 if (cur->type == XML_RELAXNG_TEXT)
4357 is_mixed++;
4358 groups[nbgroups]->rule = cur;
4359 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4360 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4361 nbgroups++;
4362 cur = cur->next;
4363 }
4364 #ifdef DEBUG_INTERLEAVE
4365 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4366 #endif
4367
4368 /*
4369 * Let's check that all rules makes a partitions according to 7.4
4370 */
4371 partitions = (xmlRelaxNGPartitionPtr)
4372 xmlMalloc(sizeof(xmlRelaxNGPartition));
4373 if (partitions == NULL)
4374 goto error;
4375 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
4376 partitions->nbgroups = nbgroups;
4377 partitions->triage = xmlHashCreate(nbgroups);
4378 for (i = 0; i < nbgroups; i++) {
4379 group = groups[i];
4380 for (j = i + 1; j < nbgroups; j++) {
4381 if (groups[j] == NULL)
4382 continue;
4383
4384 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4385 groups[j]->defs);
4386 if (ret == 0) {
4387 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4388 "Element or text conflicts in interleave\n",
4389 NULL, NULL);
4390 }
4391 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4392 groups[j]->attrs);
4393 if (ret == 0) {
4394 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4395 "Attributes conflicts in interleave\n", NULL,
4396 NULL);
4397 }
4398 }
4399 tmp = group->defs;
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));
4406 if (res != 0)
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,
4412 (*tmp)->name, NULL,
4413 (void *) (long) (i + 1));
4414 else
4415 res = xmlHashAddEntry2(partitions->triage,
4416 (*tmp)->name, (*tmp)->ns,
4417 (void *) (long) (i + 1));
4418 if (res != 0)
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));
4425 else
4426 res = xmlHashAddEntry2(partitions->triage,
4427 BAD_CAST "#any", (*tmp)->ns,
4428 (void *) (long) (i + 1));
4429 if ((*tmp)->nameClass != NULL)
4430 is_determinist = 2;
4431 if (res != 0)
4432 is_determinist = -1;
4433 } else {
4434 is_determinist = -1;
4435 }
4436 tmp++;
4437 }
4438 } else {
4439 is_determinist = 0;
4440 }
4441 }
4442 partitions->groups = groups;
4443
4444 /*
4445 * and save the partition list back in the def
4446 */
4447 def->data = partitions;
4448 if (is_mixed != 0)
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;
4454 return;
4455
4456 error:
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);
4463 xmlFree(groups[i]);
4464 }
4465 xmlFree(groups);
4466 }
4467 xmlRelaxNGFreePartition(partitions);
4468 }
4469
4470 /**
4471 * xmlRelaxNGParseInterleave:
4472 * @ctxt: a Relax-NG parser context
4473 * @node: the data node.
4474 *
4475 * parse the content of a RelaxNG interleave node.
4476 *
4477 * Returns the definition pointer or NULL in case of error
4478 */
4479 static xmlRelaxNGDefinePtr
xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)4480 xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4481 {
4482 xmlRelaxNGDefinePtr def = NULL;
4483 xmlRelaxNGDefinePtr last = NULL, cur;
4484 xmlNodePtr child;
4485
4486 def = xmlRelaxNGNewDefine(ctxt, node);
4487 if (def == NULL) {
4488 return (NULL);
4489 }
4490 def->type = XML_RELAXNG_INTERLEAVE;
4491
4492 if (ctxt->interleaves == NULL)
4493 ctxt->interleaves = xmlHashCreate(10);
4494 if (ctxt->interleaves == NULL) {
4495 xmlRngPErrMemory(ctxt, "create interleaves\n");
4496 } else {
4497 char name[32];
4498
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);
4504 }
4505 }
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);
4510 }
4511 while (child != NULL) {
4512 if (IS_RELAXNG(child, "element")) {
4513 cur = xmlRelaxNGParseElement(ctxt, child);
4514 } else {
4515 cur = xmlRelaxNGParsePattern(ctxt, child);
4516 }
4517 if (cur != NULL) {
4518 cur->parent = def;
4519 if (last == NULL) {
4520 def->content = last = cur;
4521 } else {
4522 last->next = cur;
4523 last = cur;
4524 }
4525 }
4526 child = child->next;
4527 }
4528
4529 return (def);
4530 }
4531
4532 /**
4533 * xmlRelaxNGParseInclude:
4534 * @ctxt: a Relax-NG parser context
4535 * @node: the include node
4536 *
4537 * Integrate the content of an include node in the current grammar
4538 *
4539 * Returns 0 in case of success or -1 in case of error
4540 */
4541 static int
xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)4542 xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4543 {
4544 xmlRelaxNGIncludePtr incl;
4545 xmlNodePtr root;
4546 int ret = 0, tmp;
4547
4548 incl = node->psvi;
4549 if (incl == NULL) {
4550 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4551 "Include node has no data\n", NULL, NULL);
4552 return (-1);
4553 }
4554 root = xmlDocGetRootElement(incl->doc);
4555 if (root == NULL) {
4556 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4557 NULL, NULL);
4558 return (-1);
4559 }
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);
4563 return (-1);
4564 }
4565
4566 /*
4567 * Merge the definition from both the include and the internal list
4568 */
4569 if (root->children != NULL) {
4570 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4571 if (tmp != 0)
4572 ret = -1;
4573 }
4574 if (node->children != NULL) {
4575 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4576 if (tmp != 0)
4577 ret = -1;
4578 }
4579 return (ret);
4580 }
4581
4582 /**
4583 * xmlRelaxNGParseDefine:
4584 * @ctxt: a Relax-NG parser context
4585 * @node: the define node
4586 *
4587 * parse the content of a RelaxNG define element node.
4588 *
4589 * Returns 0 in case of success or -1 in case of error
4590 */
4591 static int
xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)4592 xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4593 {
4594 xmlChar *name;
4595 int ret = 0, tmp;
4596 xmlRelaxNGDefinePtr def;
4597 const xmlChar *olddefine;
4598
4599 name = xmlGetProp(node, BAD_CAST "name");
4600 if (name == NULL) {
4601 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4602 "define has no name\n", NULL, NULL);
4603 } else {
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);
4608 }
4609 def = xmlRelaxNGNewDefine(ctxt, node);
4610 if (def == NULL) {
4611 xmlFree(name);
4612 return (-1);
4613 }
4614 def->type = XML_RELAXNG_DEF;
4615 def->name = name;
4616 if (node->children == NULL) {
4617 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4618 "define has no children\n", NULL, NULL);
4619 } else {
4620 olddefine = ctxt->define;
4621 ctxt->define = name;
4622 def->content =
4623 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4624 ctxt->define = olddefine;
4625 }
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);
4631 ret = -1;
4632 } else {
4633 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4634 if (tmp < 0) {
4635 xmlRelaxNGDefinePtr prev;
4636
4637 prev = xmlHashLookup(ctxt->grammar->defs, name);
4638 if (prev == NULL) {
4639 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4640 "Internal error on define aggregation of %s\n",
4641 name, NULL);
4642 ret = -1;
4643 } else {
4644 while (prev->nextHash != NULL)
4645 prev = prev->nextHash;
4646 prev->nextHash = def;
4647 }
4648 }
4649 }
4650 }
4651 return (ret);
4652 }
4653
4654 /**
4655 * xmlRelaxNGParseImportRef:
4656 * @payload: the parser context
4657 * @data: the current grammar
4658 * @name: the reference name
4659 *
4660 * Import import one references into the current grammar
4661 */
4662 static void
xmlRelaxNGParseImportRef(void * payload,void * data,xmlChar * name)4663 xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4664 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4665 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4666 int tmp;
4667
4668 def->dflags |= IS_EXTERNAL_REF;
4669
4670 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4671 if (tmp < 0) {
4672 xmlRelaxNGDefinePtr prev;
4673
4674 prev = (xmlRelaxNGDefinePtr)
4675 xmlHashLookup(ctxt->grammar->refs, def->name);
4676 if (prev == NULL) {
4677 if (def->name != NULL) {
4678 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4679 "Error refs definitions '%s'\n",
4680 def->name, NULL);
4681 } else {
4682 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4683 "Error refs definitions\n",
4684 NULL, NULL);
4685 }
4686 } else {
4687 def->nextHash = prev->nextHash;
4688 prev->nextHash = def;
4689 }
4690 }
4691 }
4692
4693 /**
4694 * xmlRelaxNGParseImportRefs:
4695 * @ctxt: the parser context
4696 * @grammar: the sub grammar
4697 *
4698 * Import references from the subgrammar into the current grammar
4699 *
4700 * Returns 0 in case of success, -1 in case of failure
4701 */
4702 static int
xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGGrammarPtr grammar)4703 xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4704 xmlRelaxNGGrammarPtr grammar) {
4705 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4706 return(-1);
4707 if (grammar->refs == NULL)
4708 return(0);
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);
4714 return(-1);
4715 }
4716 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
4717 return(0);
4718 }
4719
4720 /**
4721 * xmlRelaxNGProcessExternalRef:
4722 * @ctxt: the parser context
4723 * @node: the externlRef node
4724 *
4725 * Process and compile an externlRef node
4726 *
4727 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4728 */
4729 static xmlRelaxNGDefinePtr
xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)4730 xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4731 {
4732 xmlRelaxNGDocumentPtr docu;
4733 xmlNodePtr root, tmp;
4734 xmlChar *ns;
4735 int newNs = 0, oldflags;
4736 xmlRelaxNGDefinePtr def;
4737
4738 docu = node->psvi;
4739 if (docu != NULL) {
4740 def = xmlRelaxNGNewDefine(ctxt, node);
4741 if (def == NULL)
4742 return (NULL);
4743 def->type = XML_RELAXNG_EXTERNALREF;
4744
4745 if (docu->content == NULL) {
4746 /*
4747 * Then do the parsing for good
4748 */
4749 root = xmlDocGetRootElement(docu->doc);
4750 if (root == NULL) {
4751 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4752 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4753 NULL);
4754 return (NULL);
4755 }
4756 /*
4757 * ns transmission rules
4758 */
4759 ns = xmlGetProp(root, BAD_CAST "ns");
4760 if (ns == NULL) {
4761 tmp = node;
4762 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4763 ns = xmlGetProp(tmp, BAD_CAST "ns");
4764 if (ns != NULL) {
4765 break;
4766 }
4767 tmp = tmp->parent;
4768 }
4769 if (ns != NULL) {
4770 xmlSetProp(root, BAD_CAST "ns", ns);
4771 newNs = 1;
4772 xmlFree(ns);
4773 }
4774 } else {
4775 xmlFree(ns);
4776 }
4777
4778 /*
4779 * Parsing to get a precompiled schemas.
4780 */
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);
4790 }
4791
4792 /*
4793 * the externalRef may be reused in a different ns context
4794 */
4795 if (newNs == 1) {
4796 xmlUnsetProp(root, BAD_CAST "ns");
4797 }
4798 }
4799 def->content = docu->content;
4800 } else {
4801 def = NULL;
4802 }
4803 return (def);
4804 }
4805
4806 /**
4807 * xmlRelaxNGParsePattern:
4808 * @ctxt: a Relax-NG parser context
4809 * @node: the pattern node.
4810 *
4811 * parse the content of a RelaxNG pattern node.
4812 *
4813 * Returns the definition pointer or NULL in case of error or if no
4814 * pattern is generated.
4815 */
4816 static xmlRelaxNGDefinePtr
xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)4817 xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4818 {
4819 xmlRelaxNGDefinePtr def = NULL;
4820
4821 if (node == NULL) {
4822 return (NULL);
4823 }
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);
4830 if (def == NULL)
4831 return (NULL);
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);
4836 }
4837 } else if (IS_RELAXNG(node, "text")) {
4838 def = xmlRelaxNGNewDefine(ctxt, node);
4839 if (def == NULL)
4840 return (NULL);
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);
4845 }
4846 } else if (IS_RELAXNG(node, "zeroOrMore")) {
4847 def = xmlRelaxNGNewDefine(ctxt, node);
4848 if (def == NULL)
4849 return (NULL);
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);
4854 } else {
4855 def->content =
4856 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4857 }
4858 } else if (IS_RELAXNG(node, "oneOrMore")) {
4859 def = xmlRelaxNGNewDefine(ctxt, node);
4860 if (def == NULL)
4861 return (NULL);
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);
4866 } else {
4867 def->content =
4868 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4869 }
4870 } else if (IS_RELAXNG(node, "optional")) {
4871 def = xmlRelaxNGNewDefine(ctxt, node);
4872 if (def == NULL)
4873 return (NULL);
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);
4878 } else {
4879 def->content =
4880 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4881 }
4882 } else if (IS_RELAXNG(node, "choice")) {
4883 def = xmlRelaxNGNewDefine(ctxt, node);
4884 if (def == NULL)
4885 return (NULL);
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);
4890 } else {
4891 def->content =
4892 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4893 }
4894 } else if (IS_RELAXNG(node, "group")) {
4895 def = xmlRelaxNGNewDefine(ctxt, node);
4896 if (def == NULL)
4897 return (NULL);
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);
4902 } else {
4903 def->content =
4904 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4905 }
4906 } else if (IS_RELAXNG(node, "ref")) {
4907 def = xmlRelaxNGNewDefine(ctxt, node);
4908 if (def == NULL)
4909 return (NULL);
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",
4914 NULL, NULL);
4915 } else {
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,
4920 NULL);
4921 }
4922 }
4923 if (node->children != NULL) {
4924 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4925 NULL, NULL);
4926 }
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);
4932 def = NULL;
4933 } else {
4934 int tmp;
4935
4936 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4937 if (tmp < 0) {
4938 xmlRelaxNGDefinePtr prev;
4939
4940 prev = (xmlRelaxNGDefinePtr)
4941 xmlHashLookup(ctxt->grammar->refs, def->name);
4942 if (prev == NULL) {
4943 if (def->name != NULL) {
4944 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4945 "Error refs definitions '%s'\n",
4946 def->name, NULL);
4947 } else {
4948 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4949 "Error refs definitions\n",
4950 NULL, NULL);
4951 }
4952 def = NULL;
4953 } else {
4954 def->nextHash = prev->nextHash;
4955 prev->nextHash = def;
4956 }
4957 }
4958 }
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);
4965 if (def == NULL)
4966 return (NULL);
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);
4971 } else {
4972 def->content =
4973 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4974 }
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);
4981 if (def == NULL)
4982 return (NULL);
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",
4987 NULL, NULL);
4988 }
4989 } else if (IS_RELAXNG(node, "grammar")) {
4990 xmlRelaxNGGrammarPtr grammar, old;
4991 xmlRelaxNGGrammarPtr oldparent;
4992
4993 #ifdef DEBUG_GRAMMAR
4994 xmlGenericError(xmlGenericErrorContext,
4995 "Found <grammar> pattern\n");
4996 #endif
4997
4998 oldparent = ctxt->parentgrammar;
4999 old = ctxt->grammar;
5000 ctxt->parentgrammar = old;
5001 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
5002 if (old != NULL) {
5003 ctxt->grammar = old;
5004 ctxt->parentgrammar = oldparent;
5005 #if 0
5006 if (grammar != NULL) {
5007 grammar->next = old->next;
5008 old->next = grammar;
5009 }
5010 #endif
5011 }
5012 if (grammar != NULL)
5013 def = grammar->start;
5014 else
5015 def = NULL;
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,
5020 NULL);
5021 return (NULL);
5022 }
5023 def = xmlRelaxNGNewDefine(ctxt, node);
5024 if (def == NULL)
5025 return (NULL);
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);
5031 } else {
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",
5036 def->name, NULL);
5037 }
5038 }
5039 if (node->children != NULL) {
5040 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5041 "parentRef is not empty\n", NULL, NULL);
5042 }
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);
5048 def = NULL;
5049 } else if (def->name != NULL) {
5050 int tmp;
5051
5052 tmp =
5053 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5054 if (tmp < 0) {
5055 xmlRelaxNGDefinePtr prev;
5056
5057 prev = (xmlRelaxNGDefinePtr)
5058 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5059 if (prev == NULL) {
5060 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5061 "Internal error parentRef definitions '%s'\n",
5062 def->name, NULL);
5063 def = NULL;
5064 } else {
5065 def->nextHash = prev->nextHash;
5066 prev->nextHash = def;
5067 }
5068 }
5069 }
5070 } else if (IS_RELAXNG(node, "mixed")) {
5071 if (node->children == NULL) {
5072 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5073 NULL, NULL);
5074 def = NULL;
5075 } else {
5076 def = xmlRelaxNGParseInterleave(ctxt, node);
5077 if (def != NULL) {
5078 xmlRelaxNGDefinePtr tmp;
5079
5080 if ((def->content != NULL) && (def->content->next != NULL)) {
5081 tmp = xmlRelaxNGNewDefine(ctxt, node);
5082 if (tmp != NULL) {
5083 tmp->type = XML_RELAXNG_GROUP;
5084 tmp->content = def->content;
5085 def->content = tmp;
5086 }
5087 }
5088
5089 tmp = xmlRelaxNGNewDefine(ctxt, node);
5090 if (tmp == NULL)
5091 return (def);
5092 tmp->type = XML_RELAXNG_TEXT;
5093 tmp->next = def->content;
5094 def->content = tmp;
5095 }
5096 }
5097 } else {
5098 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5099 "Unexpected node %s is not a pattern\n", node->name,
5100 NULL);
5101 def = NULL;
5102 }
5103 return (def);
5104 }
5105
5106 /**
5107 * xmlRelaxNGParseAttribute:
5108 * @ctxt: a Relax-NG parser context
5109 * @node: the element node
5110 *
5111 * parse the content of a RelaxNG attribute node.
5112 *
5113 * Returns the definition pointer or NULL in case of error.
5114 */
5115 static xmlRelaxNGDefinePtr
xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)5116 xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5117 {
5118 xmlRelaxNGDefinePtr ret, cur;
5119 xmlNodePtr child;
5120 int old_flags;
5121
5122 ret = xmlRelaxNGNewDefine(ctxt, node);
5123 if (ret == NULL)
5124 return (NULL);
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",
5131 NULL, NULL);
5132 return (ret);
5133 }
5134 old_flags = ctxt->flags;
5135 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
5136 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5137 if (cur != NULL)
5138 child = child->next;
5139
5140 if (child != NULL) {
5141 cur = xmlRelaxNGParsePattern(ctxt, child);
5142 if (cur != NULL) {
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:
5162 ret->content = cur;
5163 cur->parent = ret;
5164 break;
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,
5170 NULL);
5171 break;
5172 case XML_RELAXNG_NOOP:
5173 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5174 "RNG Internal error, noop found in attribute\n",
5175 NULL, NULL);
5176 break;
5177 }
5178 }
5179 child = child->next;
5180 }
5181 if (child != NULL) {
5182 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5183 "attribute has multiple children\n", NULL, NULL);
5184 }
5185 ctxt->flags = old_flags;
5186 return (ret);
5187 }
5188
5189 /**
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
5194 *
5195 * parse the content of a RelaxNG nameClass node.
5196 *
5197 * Returns the definition pointer or NULL in case of error.
5198 */
5199 static xmlRelaxNGDefinePtr
xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node,int attr)5200 xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
5201 xmlNodePtr node, int attr)
5202 {
5203 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5204 xmlNodePtr child;
5205
5206 if (!IS_RELAXNG(node, "except")) {
5207 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5208 "Expecting an except node\n", NULL, NULL);
5209 return (NULL);
5210 }
5211 if (node->next != NULL) {
5212 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5213 "exceptNameClass allows only a single except node\n",
5214 NULL, NULL);
5215 }
5216 if (node->children == NULL) {
5217 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5218 NULL, NULL);
5219 return (NULL);
5220 }
5221
5222 ret = xmlRelaxNGNewDefine(ctxt, node);
5223 if (ret == NULL)
5224 return (NULL);
5225 ret->type = XML_RELAXNG_EXCEPT;
5226 child = node->children;
5227 while (child != NULL) {
5228 cur = xmlRelaxNGNewDefine(ctxt, child);
5229 if (cur == NULL)
5230 break;
5231 if (attr)
5232 cur->type = XML_RELAXNG_ATTRIBUTE;
5233 else
5234 cur->type = XML_RELAXNG_ELEMENT;
5235
5236 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
5237 if (last == NULL) {
5238 ret->content = cur;
5239 } else {
5240 last->next = cur;
5241 }
5242 last = cur;
5243 }
5244 child = child->next;
5245 }
5246
5247 return (ret);
5248 }
5249
5250 /**
5251 * xmlRelaxNGParseNameClass:
5252 * @ctxt: a Relax-NG parser context
5253 * @node: the nameClass node
5254 * @def: the current definition
5255 *
5256 * parse the content of a RelaxNG nameClass node.
5257 *
5258 * Returns the definition pointer or NULL in case of error.
5259 */
5260 static xmlRelaxNGDefinePtr
xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node,xmlRelaxNGDefinePtr def)5261 xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
5262 xmlRelaxNGDefinePtr def)
5263 {
5264 xmlRelaxNGDefinePtr ret, tmp;
5265 xmlChar *val;
5266
5267 ret = def;
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);
5273 if (ret == NULL)
5274 return (NULL);
5275 ret->parent = def;
5276 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5277 ret->type = XML_RELAXNG_ATTRIBUTE;
5278 else
5279 ret->type = XML_RELAXNG_ELEMENT;
5280 }
5281 }
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);
5290 else
5291 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5292 "name '%s' is not an NCName\n",
5293 val, NULL);
5294 }
5295 ret->name = val;
5296 val = xmlGetProp(node, BAD_CAST "ns");
5297 ret->ns = val;
5298 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5299 (val != NULL) &&
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",
5303 val, NULL);
5304 }
5305 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5306 (val != NULL) &&
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",
5310 val, NULL);
5311 }
5312 } else if (IS_RELAXNG(node, "anyName")) {
5313 ret->name = NULL;
5314 ret->ns = NULL;
5315 if (node->children != NULL) {
5316 ret->nameClass =
5317 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5318 (def->type ==
5319 XML_RELAXNG_ATTRIBUTE));
5320 }
5321 } else if (IS_RELAXNG(node, "nsName")) {
5322 ret->name = NULL;
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);
5327 }
5328 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5329 (ret->ns != NULL) &&
5330 (xmlStrEqual
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",
5334 ret->ns, NULL);
5335 }
5336 if (node->children != NULL) {
5337 ret->nameClass =
5338 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5339 (def->type ==
5340 XML_RELAXNG_ATTRIBUTE));
5341 }
5342 } else if (IS_RELAXNG(node, "choice")) {
5343 xmlNodePtr child;
5344 xmlRelaxNGDefinePtr last = NULL;
5345
5346 ret = xmlRelaxNGNewDefine(ctxt, node);
5347 if (ret == NULL)
5348 return (NULL);
5349 ret->parent = def;
5350 ret->type = XML_RELAXNG_CHOICE;
5351
5352 if (node->children == NULL) {
5353 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5354 "Element choice is empty\n", NULL, NULL);
5355 } else {
5356
5357 child = node->children;
5358 while (child != NULL) {
5359 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5360 if (tmp != NULL) {
5361 if (last == NULL) {
5362 last = ret->nameClass = tmp;
5363 } else {
5364 last->next = tmp;
5365 last = tmp;
5366 }
5367 }
5368 child = child->next;
5369 }
5370 }
5371 } else {
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),
5375 NULL);
5376 return (NULL);
5377 }
5378 if (ret != def) {
5379 if (def->nameClass == NULL) {
5380 def->nameClass = ret;
5381 } else {
5382 tmp = def->nameClass;
5383 while (tmp->next != NULL) {
5384 tmp = tmp->next;
5385 }
5386 tmp->next = ret;
5387 }
5388 }
5389 return (ret);
5390 }
5391
5392 /**
5393 * xmlRelaxNGParseElement:
5394 * @ctxt: a Relax-NG parser context
5395 * @node: the element node
5396 *
5397 * parse the content of a RelaxNG element node.
5398 *
5399 * Returns the definition pointer or NULL in case of error.
5400 */
5401 static xmlRelaxNGDefinePtr
xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)5402 xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5403 {
5404 xmlRelaxNGDefinePtr ret, cur, last;
5405 xmlNodePtr child;
5406 const xmlChar *olddefine;
5407
5408 ret = xmlRelaxNGNewDefine(ctxt, node);
5409 if (ret == NULL)
5410 return (NULL);
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",
5417 NULL, NULL);
5418 return (ret);
5419 }
5420 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5421 if (cur != NULL)
5422 child = child->next;
5423
5424 if (child == NULL) {
5425 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5426 "xmlRelaxNGParseElement: element has no content\n",
5427 NULL, NULL);
5428 return (ret);
5429 }
5430 olddefine = ctxt->define;
5431 ctxt->define = NULL;
5432 last = NULL;
5433 while (child != NULL) {
5434 cur = xmlRelaxNGParsePattern(ctxt, child);
5435 if (cur != NULL) {
5436 cur->parent = ret;
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:
5455 if (last == NULL) {
5456 ret->content = last = cur;
5457 } else {
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;
5464 } else {
5465 ret->content = last;
5466 }
5467 }
5468 last->next = cur;
5469 last = cur;
5470 }
5471 break;
5472 case XML_RELAXNG_ATTRIBUTE:
5473 cur->next = ret->attrs;
5474 ret->attrs = cur;
5475 break;
5476 case XML_RELAXNG_START:
5477 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5478 "RNG Internal error, start found in element\n",
5479 NULL, NULL);
5480 break;
5481 case XML_RELAXNG_PARAM:
5482 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5483 "RNG Internal error, param found in element\n",
5484 NULL, NULL);
5485 break;
5486 case XML_RELAXNG_EXCEPT:
5487 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5488 "RNG Internal error, except found in element\n",
5489 NULL, NULL);
5490 break;
5491 case XML_RELAXNG_NOOP:
5492 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5493 "RNG Internal error, noop found in element\n",
5494 NULL, NULL);
5495 break;
5496 }
5497 }
5498 child = child->next;
5499 }
5500 ctxt->define = olddefine;
5501 return (ret);
5502 }
5503
5504 /**
5505 * xmlRelaxNGParsePatterns:
5506 * @ctxt: a Relax-NG parser context
5507 * @nodes: list of nodes
5508 * @group: use an implicit <group> for elements
5509 *
5510 * parse the content of a RelaxNG start node.
5511 *
5512 * Returns the definition pointer or NULL in case of error.
5513 */
5514 static xmlRelaxNGDefinePtr
xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr nodes,int group)5515 xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
5516 int group)
5517 {
5518 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
5519
5520 parent = ctxt->def;
5521 while (nodes != NULL) {
5522 if (IS_RELAXNG(nodes, "element")) {
5523 cur = xmlRelaxNGParseElement(ctxt, nodes);
5524 if (def == NULL) {
5525 def = last = cur;
5526 } else {
5527 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5528 (def == last)) {
5529 def = xmlRelaxNGNewDefine(ctxt, nodes);
5530 def->type = XML_RELAXNG_GROUP;
5531 def->content = last;
5532 }
5533 last->next = cur;
5534 last = cur;
5535 }
5536 cur->parent = parent;
5537 } else {
5538 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5539 if (cur != NULL) {
5540 if (def == NULL) {
5541 def = last = cur;
5542 } else {
5543 last->next = cur;
5544 last = cur;
5545 }
5546 }
5547 }
5548 nodes = nodes->next;
5549 }
5550 return (def);
5551 }
5552
5553 /**
5554 * xmlRelaxNGParseStart:
5555 * @ctxt: a Relax-NG parser context
5556 * @nodes: start children nodes
5557 *
5558 * parse the content of a RelaxNG start node.
5559 *
5560 * Returns 0 in case of success, -1 in case of error
5561 */
5562 static int
xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr nodes)5563 xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5564 {
5565 int ret = 0;
5566 xmlRelaxNGDefinePtr def = NULL, last;
5567
5568 if (nodes == NULL) {
5569 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5570 NULL, NULL);
5571 return (-1);
5572 }
5573 if (IS_RELAXNG(nodes, "empty")) {
5574 def = xmlRelaxNGNewDefine(ctxt, nodes);
5575 if (def == NULL)
5576 return (-1);
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);
5581 }
5582 } else if (IS_RELAXNG(nodes, "notAllowed")) {
5583 def = xmlRelaxNGNewDefine(ctxt, nodes);
5584 if (def == NULL)
5585 return (-1);
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);
5590 }
5591 } else {
5592 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
5593 }
5594 if (ctxt->grammar->start != NULL) {
5595 last = ctxt->grammar->start;
5596 while (last->next != NULL)
5597 last = last->next;
5598 last->next = def;
5599 } else {
5600 ctxt->grammar->start = def;
5601 }
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);
5606 return (-1);
5607 }
5608 return (ret);
5609 }
5610
5611 /**
5612 * xmlRelaxNGParseGrammarContent:
5613 * @ctxt: a Relax-NG parser context
5614 * @nodes: grammar children nodes
5615 *
5616 * parse the content of a RelaxNG grammar node.
5617 *
5618 * Returns 0 in case of success, -1 in case of error
5619 */
5620 static int
xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr nodes)5621 xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5622 xmlNodePtr nodes)
5623 {
5624 int ret = 0, tmp;
5625
5626 if (nodes == NULL) {
5627 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5628 "grammar has no children\n", NULL, NULL);
5629 return (-1);
5630 }
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);
5636 } else {
5637 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5638 if (tmp != 0)
5639 ret = -1;
5640 }
5641 } else if (IS_RELAXNG(nodes, "define")) {
5642 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5643 if (tmp != 0)
5644 ret = -1;
5645 } else if (IS_RELAXNG(nodes, "include")) {
5646 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5647 if (tmp != 0)
5648 ret = -1;
5649 } else {
5650 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5651 "grammar has unexpected child %s\n", nodes->name,
5652 NULL);
5653 ret = -1;
5654 }
5655 nodes = nodes->next;
5656 }
5657 return (ret);
5658 }
5659
5660 /**
5661 * xmlRelaxNGCheckReference:
5662 * @ref: the ref
5663 * @ctxt: a Relax-NG parser context
5664 * @name: the name associated to the defines
5665 *
5666 * Applies the 4.17. combine attribute rule for all the define
5667 * element of a given grammar using the same name.
5668 */
5669 static void
xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,xmlRelaxNGParserCtxtPtr ctxt,const xmlChar * name)5670 xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
5671 xmlRelaxNGParserCtxtPtr ctxt,
5672 const xmlChar * name)
5673 {
5674 xmlRelaxNGGrammarPtr grammar;
5675 xmlRelaxNGDefinePtr def, cur;
5676
5677 /*
5678 * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
5679 */
5680 if (ref->dflags & IS_EXTERNAL_REF)
5681 return;
5682
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",
5687 name, NULL);
5688 return;
5689 }
5690 if (ref->content != NULL) {
5691 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5692 "Internal error: reference has content in CheckReference %s\n",
5693 name, NULL);
5694 return;
5695 }
5696 if (grammar->defs != NULL) {
5697 def = xmlHashLookup(grammar->defs, name);
5698 if (def != NULL) {
5699 cur = ref;
5700 while (cur != NULL) {
5701 cur->content = def;
5702 cur = cur->nextHash;
5703 }
5704 } else {
5705 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5706 "Reference %s has no matching definition\n", name,
5707 NULL);
5708 }
5709 } else {
5710 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5711 "Reference %s has no matching definition\n", name,
5712 NULL);
5713 }
5714 }
5715
5716 /**
5717 * xmlRelaxNGCheckCombine:
5718 * @define: the define(s) list
5719 * @ctxt: a Relax-NG parser context
5720 * @name: the name associated to the defines
5721 *
5722 * Applies the 4.17. combine attribute rule for all the define
5723 * element of a given grammar using the same name.
5724 */
5725 static void
xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,xmlRelaxNGParserCtxtPtr ctxt,const xmlChar * name)5726 xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
5727 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5728 {
5729 xmlChar *combine;
5730 int choiceOrInterleave = -1;
5731 int missing = 0;
5732 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5733
5734 if (define->nextHash == NULL)
5735 return;
5736 cur = define;
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",
5746 name, NULL);
5747 }
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",
5754 name, NULL);
5755 }
5756 } else {
5757 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5758 "Defines for %s use unknown combine value '%s''\n",
5759 name, combine);
5760 }
5761 xmlFree(combine);
5762 } else {
5763 if (missing == 0)
5764 missing = 1;
5765 else {
5766 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5767 "Some defines for %s needs the combine attribute\n",
5768 name, NULL);
5769 }
5770 }
5771
5772 cur = cur->nextHash;
5773 }
5774 #ifdef DEBUG
5775 xmlGenericError(xmlGenericErrorContext,
5776 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5777 name, choiceOrInterleave);
5778 #endif
5779 if (choiceOrInterleave == -1)
5780 choiceOrInterleave = 0;
5781 cur = xmlRelaxNGNewDefine(ctxt, define->node);
5782 if (cur == NULL)
5783 return;
5784 if (choiceOrInterleave == 0)
5785 cur->type = XML_RELAXNG_INTERLEAVE;
5786 else
5787 cur->type = XML_RELAXNG_CHOICE;
5788 tmp = define;
5789 last = NULL;
5790 while (tmp != NULL) {
5791 if (tmp->content != NULL) {
5792 if (tmp->content->next != NULL) {
5793 /*
5794 * we need first to create a wrapper.
5795 */
5796 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5797 if (tmp2 == NULL)
5798 break;
5799 tmp2->type = XML_RELAXNG_GROUP;
5800 tmp2->content = tmp->content;
5801 } else {
5802 tmp2 = tmp->content;
5803 }
5804 if (last == NULL) {
5805 cur->content = tmp2;
5806 } else {
5807 last->next = tmp2;
5808 }
5809 last = tmp2;
5810 }
5811 tmp->content = cur;
5812 tmp = tmp->nextHash;
5813 }
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,
5821 NULL);
5822 } else {
5823 char tmpname[32];
5824
5825 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5826 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5827 0) {
5828 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5829 "Failed to add %s to hash table\n",
5830 (const xmlChar *) tmpname, NULL);
5831 }
5832 }
5833 }
5834 }
5835
5836 /**
5837 * xmlRelaxNGCombineStart:
5838 * @ctxt: a Relax-NG parser context
5839 * @grammar: the grammar
5840 *
5841 * Applies the 4.17. combine rule for all the start
5842 * element of a given grammar.
5843 */
5844 static void
xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGGrammarPtr grammar)5845 xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
5846 xmlRelaxNGGrammarPtr grammar)
5847 {
5848 xmlRelaxNGDefinePtr starts;
5849 xmlChar *combine;
5850 int choiceOrInterleave = -1;
5851 int missing = 0;
5852 xmlRelaxNGDefinePtr cur;
5853
5854 starts = grammar->start;
5855 if ((starts == NULL) || (starts->next == NULL))
5856 return;
5857 cur = starts;
5858 while (cur != NULL) {
5859 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5860 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5861 combine = NULL;
5862 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5863 "Internal error: start element not found\n", NULL,
5864 NULL);
5865 } else {
5866 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5867 }
5868
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",
5876 NULL, NULL);
5877 }
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",
5884 NULL, NULL);
5885 }
5886 } else {
5887 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5888 "<start> uses unknown combine value '%s''\n",
5889 combine, NULL);
5890 }
5891 xmlFree(combine);
5892 } else {
5893 if (missing == 0)
5894 missing = 1;
5895 else {
5896 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5897 "Some <start> element miss the combine attribute\n",
5898 NULL, NULL);
5899 }
5900 }
5901
5902 cur = cur->next;
5903 }
5904 #ifdef DEBUG
5905 xmlGenericError(xmlGenericErrorContext,
5906 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5907 choiceOrInterleave);
5908 #endif
5909 if (choiceOrInterleave == -1)
5910 choiceOrInterleave = 0;
5911 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
5912 if (cur == NULL)
5913 return;
5914 if (choiceOrInterleave == 0)
5915 cur->type = XML_RELAXNG_INTERLEAVE;
5916 else
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,
5926 NULL);
5927 } else {
5928 char tmpname[32];
5929
5930 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5931 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5932 0) {
5933 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5934 "Failed to add %s to hash table\n",
5935 (const xmlChar *) tmpname, NULL);
5936 }
5937 }
5938 }
5939 }
5940
5941 /**
5942 * xmlRelaxNGCheckCycles:
5943 * @ctxt: a Relax-NG parser context
5944 * @nodes: grammar children nodes
5945 * @depth: the counter
5946 *
5947 * Check for cycles.
5948 *
5949 * Returns 0 if check passed, and -1 in case of error
5950 */
5951 static int
xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDefinePtr cur,int depth)5952 xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5953 xmlRelaxNGDefinePtr cur, int depth)
5954 {
5955 int ret = 0;
5956
5957 while ((ret == 0) && (cur != NULL)) {
5958 if ((cur->type == XML_RELAXNG_REF) ||
5959 (cur->type == XML_RELAXNG_PARENTREF)) {
5960 if (cur->depth == -1) {
5961 cur->depth = depth;
5962 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5963 cur->depth = -2;
5964 } else if (depth == cur->depth) {
5965 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5966 "Detected a cycle in %s references\n",
5967 cur->name, NULL);
5968 return (-1);
5969 }
5970 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5971 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5972 } else {
5973 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5974 }
5975 cur = cur->next;
5976 }
5977 return (ret);
5978 }
5979
5980 /**
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
5986 *
5987 * Try to unlink a definition. If not possble make it a NOOP
5988 *
5989 * Returns the new prev definition
5990 */
5991 static xmlRelaxNGDefinePtr
xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,xmlRelaxNGDefinePtr cur,xmlRelaxNGDefinePtr parent,xmlRelaxNGDefinePtr prev)5992 xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5993 xmlRelaxNGDefinePtr cur,
5994 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5995 {
5996 if (prev != NULL) {
5997 prev->next = cur->next;
5998 } else {
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;
6006 } else {
6007 cur->type = XML_RELAXNG_NOOP;
6008 prev = cur;
6009 }
6010 }
6011 return (prev);
6012 }
6013
6014 /**
6015 * xmlRelaxNGSimplify:
6016 * @ctxt: a Relax-NG parser context
6017 * @nodes: grammar children nodes
6018 *
6019 * Check for simplification of empty and notAllowed
6020 */
6021 static void
xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDefinePtr cur,xmlRelaxNGDefinePtr parent)6022 xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
6023 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
6024 {
6025 xmlRelaxNGDefinePtr prev = NULL;
6026
6027 while (cur != NULL) {
6028 if ((cur->type == XML_RELAXNG_REF) ||
6029 (cur->type == XML_RELAXNG_PARENTREF)) {
6030 if (cur->depth != -3) {
6031 cur->depth = -3;
6032 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6033 }
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;
6044 break;
6045 }
6046 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6047 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6048 } else
6049 prev = cur;
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;
6056 break;
6057 }
6058 if ((parent != NULL) &&
6059 ((parent->type == XML_RELAXNG_GROUP) ||
6060 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6061 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6062 } else
6063 prev = cur;
6064 } else {
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);
6072 /*
6073 * On Elements, try to move attribute only generating rules on
6074 * the attrs rules.
6075 */
6076 if (cur->type == XML_RELAXNG_ELEMENT) {
6077 int attronly;
6078 xmlRelaxNGDefinePtr tmp, pre;
6079
6080 while (cur->content != NULL) {
6081 attronly =
6082 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6083 if (attronly == 1) {
6084 /*
6085 * migrate cur->content to attrs
6086 */
6087 tmp = cur->content;
6088 cur->content = tmp->next;
6089 tmp->next = cur->attrs;
6090 cur->attrs = tmp;
6091 } else {
6092 /*
6093 * cur->content can generate elements or text
6094 */
6095 break;
6096 }
6097 }
6098 pre = cur->content;
6099 while ((pre != NULL) && (pre->next != NULL)) {
6100 tmp = pre->next;
6101 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6102 if (attronly == 1) {
6103 /*
6104 * migrate tmp to attrs
6105 */
6106 pre->next = tmp->next;
6107 tmp->next = cur->attrs;
6108 cur->attrs = tmp;
6109 } else {
6110 pre = tmp;
6111 }
6112 }
6113 }
6114 /*
6115 * This may result in a simplification
6116 */
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;
6127 cur = cur->content;
6128 } else {
6129 cur->content->next = cur->next;
6130 prev->next = cur->content;
6131 cur = cur->content;
6132 }
6133 }
6134 }
6135 /*
6136 * the current node may have been transformed back
6137 */
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;
6151 break;
6152 }
6153 if ((parent != NULL) &&
6154 (parent->type == XML_RELAXNG_CHOICE)) {
6155 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6156 } else
6157 prev = cur;
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;
6163 break;
6164 }
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);
6170 } else
6171 prev = cur;
6172 } else {
6173 prev = cur;
6174 }
6175 }
6176 cur = cur->next;
6177 }
6178 }
6179
6180 /**
6181 * xmlRelaxNGGroupContentType:
6182 * @ct1: the first content type
6183 * @ct2: the second content type
6184 *
6185 * Try to group 2 content types
6186 *
6187 * Returns the content type
6188 */
6189 static xmlRelaxNGContentType
xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,xmlRelaxNGContentType ct2)6190 xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
6191 xmlRelaxNGContentType ct2)
6192 {
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)
6197 return (ct2);
6198 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
6199 return (ct1);
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);
6204 }
6205
6206 /**
6207 * xmlRelaxNGMaxContentType:
6208 * @ct1: the first content type
6209 * @ct2: the second content type
6210 *
6211 * Compute the max content-type
6212 *
6213 * Returns the content type
6214 */
6215 static xmlRelaxNGContentType
xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,xmlRelaxNGContentType ct2)6216 xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
6217 xmlRelaxNGContentType ct2)
6218 {
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);
6229 }
6230
6231 /**
6232 * xmlRelaxNGCheckRules:
6233 * @ctxt: a Relax-NG parser context
6234 * @cur: the current definition
6235 * @flags: some accumulated flags
6236 * @ptype: the parent type
6237 *
6238 * Check for rules in section 7.1 and 7.2
6239 *
6240 * Returns the content type of @cur
6241 */
6242 static xmlRelaxNGContentType
xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGDefinePtr cur,int flags,xmlRelaxNGType ptype)6243 xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6244 xmlRelaxNGDefinePtr cur, int flags,
6245 xmlRelaxNGType ptype)
6246 {
6247 int nflags;
6248 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
6249
6250 while (cur != NULL) {
6251 ret = XML_RELAXNG_CONTENT_EMPTY;
6252 if ((cur->type == XML_RELAXNG_REF) ||
6253 (cur->type == XML_RELAXNG_PARENTREF)) {
6254 /*
6255 * This should actually be caught by list//element(ref) at the
6256 * element boundaries, c.f. Bug #159968 local refs are dropped
6257 * in step 4.19.
6258 */
6259 #if 0
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,
6263 NULL);
6264 }
6265 #endif
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",
6269 NULL, NULL);
6270 }
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",
6275 NULL, NULL);
6276 else
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);
6280 }
6281 if (cur->depth > -4) {
6282 cur->depth = -4;
6283 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6284 flags, cur->type);
6285 cur->depth = ret - 15;
6286 } else if (cur->depth == -4) {
6287 ret = XML_RELAXNG_CONTENT_COMPLEX;
6288 } else {
6289 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6290 }
6291 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6292 /*
6293 * The 7.3 Attribute derivation rule for groups is plugged there
6294 */
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",
6299 NULL, NULL);
6300 }
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",
6304 NULL, NULL);
6305 }
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",
6309 NULL, NULL);
6310 }
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",
6314 NULL, NULL);
6315 }
6316 /*
6317 * reset since in the simple form elements are only child
6318 * of grammar/define
6319 */
6320 nflags = 0;
6321 ret =
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",
6326 cur->name, NULL);
6327 }
6328 ret =
6329 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6330 cur->type);
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",
6334 cur->name, NULL);
6335 } else {
6336 ret = XML_RELAXNG_CONTENT_COMPLEX;
6337 }
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",
6342 NULL, NULL);
6343 }
6344 if (flags & XML_RELAXNG_IN_LIST) {
6345 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6346 "Found forbidden pattern list//attribute\n",
6347 NULL, NULL);
6348 }
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",
6352 NULL, NULL);
6353 }
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",
6357 NULL, NULL);
6358 }
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",
6362 NULL, NULL);
6363 }
6364 if (flags & XML_RELAXNG_IN_START) {
6365 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6366 "Found forbidden pattern start//attribute\n",
6367 NULL, NULL);
6368 }
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",
6374 NULL, NULL);
6375 } else {
6376 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6377 "Found nsName attribute without oneOrMore ancestor\n",
6378 NULL, NULL);
6379 }
6380 }
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",
6389 NULL, NULL);
6390 }
6391 if (flags & XML_RELAXNG_IN_START) {
6392 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6393 "Found forbidden pattern start//oneOrMore\n",
6394 NULL, NULL);
6395 }
6396 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6397 ret =
6398 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6399 cur->type);
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,
6405 NULL);
6406 }
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",
6410 NULL, NULL);
6411 }
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,
6415 NULL);
6416 }
6417 nflags = flags | XML_RELAXNG_IN_LIST;
6418 ret =
6419 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6420 cur->type);
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",
6425 NULL, NULL);
6426 }
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,
6430 NULL);
6431 }
6432 if (flags & XML_RELAXNG_IN_ONEORMORE)
6433 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6434 else
6435 nflags = flags;
6436 ret =
6437 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6438 cur->type);
6439 /*
6440 * The 7.3 Attribute derivation rule for groups is plugged there
6441 */
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",
6447 NULL, NULL);
6448 }
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",
6452 NULL, NULL);
6453 }
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",
6457 NULL, NULL);
6458 }
6459 if (flags & XML_RELAXNG_IN_ONEORMORE)
6460 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6461 else
6462 nflags = flags;
6463 ret =
6464 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6465 cur->type);
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;
6470 else
6471 nflags = flags;
6472 ret =
6473 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6474 cur->type);
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,
6479 NULL);
6480 }
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,
6487 NULL);
6488 }
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,
6495 NULL);
6496 }
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",
6500 NULL, NULL);
6501 }
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,
6505 NULL);
6506 }
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",
6512 NULL, NULL);
6513 }
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,
6517 NULL);
6518 }
6519 ret = XML_RELAXNG_CONTENT_EMPTY;
6520 } else if (cur->type == XML_RELAXNG_CHOICE) {
6521 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6522 ret =
6523 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6524 } else {
6525 ret =
6526 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6527 }
6528 cur = cur->next;
6529 if (ptype == XML_RELAXNG_GROUP) {
6530 val = xmlRelaxNGGroupContentType(val, ret);
6531 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6532 /*
6533 * TODO: scan complain that tmp is never used, seems on purpose
6534 * need double-checking
6535 */
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;
6546 else
6547 val = XML_RELAXNG_CONTENT_SIMPLE;
6548 } else {
6549 val = xmlRelaxNGGroupContentType(val, ret);
6550 }
6551
6552 }
6553 return (val);
6554 }
6555
6556 /**
6557 * xmlRelaxNGParseGrammar:
6558 * @ctxt: a Relax-NG parser context
6559 * @nodes: grammar children nodes
6560 *
6561 * parse a Relax-NG <grammar> node
6562 *
6563 * Returns the internal xmlRelaxNGGrammarPtr built or
6564 * NULL in case of error
6565 */
6566 static xmlRelaxNGGrammarPtr
xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr nodes)6567 xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6568 {
6569 xmlRelaxNGGrammarPtr ret, tmp, old;
6570
6571 #ifdef DEBUG_GRAMMAR
6572 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6573 #endif
6574
6575 ret = xmlRelaxNGNewGrammar(ctxt);
6576 if (ret == NULL)
6577 return (NULL);
6578
6579 /*
6580 * Link the new grammar in the tree
6581 */
6582 ret->parent = ctxt->grammar;
6583 if (ctxt->grammar != NULL) {
6584 tmp = ctxt->grammar->children;
6585 if (tmp == NULL) {
6586 ctxt->grammar->children = ret;
6587 } else {
6588 while (tmp->next != NULL)
6589 tmp = tmp->next;
6590 tmp->next = ret;
6591 }
6592 }
6593
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);
6604 }
6605
6606 /*
6607 * Apply 4.17 merging rules to defines and starts
6608 */
6609 xmlRelaxNGCombineStart(ctxt, ret);
6610 if (ret->defs != NULL) {
6611 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6612 ctxt);
6613 }
6614
6615 /*
6616 * link together defines and refs in this grammar
6617 */
6618 if (ret->refs != NULL) {
6619 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6620 ctxt);
6621 }
6622
6623
6624 /* @@@@ */
6625
6626 ctxt->grammar = old;
6627 return (ret);
6628 }
6629
6630 /**
6631 * xmlRelaxNGParseDocument:
6632 * @ctxt: a Relax-NG parser context
6633 * @node: the root node of the RelaxNG schema
6634 *
6635 * parse a Relax-NG definition resource and build an internal
6636 * xmlRelaxNG struture which can be used to validate instances.
6637 *
6638 * Returns the internal XML RelaxNG structure built or
6639 * NULL in case of error
6640 */
6641 static xmlRelaxNGPtr
xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)6642 xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6643 {
6644 xmlRelaxNGPtr schema = NULL;
6645 const xmlChar *olddefine;
6646 xmlRelaxNGGrammarPtr old;
6647
6648 if ((ctxt == NULL) || (node == NULL))
6649 return (NULL);
6650
6651 schema = xmlRelaxNGNewRelaxNG(ctxt);
6652 if (schema == NULL)
6653 return (NULL);
6654
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);
6661 return (NULL);
6662 }
6663 } else {
6664 xmlRelaxNGGrammarPtr tmp, ret;
6665
6666 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6667 if (schema->topgrammar == NULL) {
6668 xmlRelaxNGFree(schema);
6669 return (NULL);
6670 }
6671 /*
6672 * Link the new grammar in the tree
6673 */
6674 ret->parent = ctxt->grammar;
6675 if (ctxt->grammar != NULL) {
6676 tmp = ctxt->grammar->children;
6677 if (tmp == NULL) {
6678 ctxt->grammar->children = ret;
6679 } else {
6680 while (tmp->next != NULL)
6681 tmp = tmp->next;
6682 tmp->next = ret;
6683 }
6684 }
6685 old = ctxt->grammar;
6686 ctxt->grammar = ret;
6687 xmlRelaxNGParseStart(ctxt, node);
6688 if (old != NULL)
6689 ctxt->grammar = old;
6690 }
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);
6703 }
6704 }
6705 #ifdef DEBUG
6706 if (schema == NULL)
6707 xmlGenericError(xmlGenericErrorContext,
6708 "xmlRelaxNGParseDocument() failed\n");
6709 #endif
6710
6711 return (schema);
6712 }
6713
6714 /************************************************************************
6715 * *
6716 * Reading RelaxNGs *
6717 * *
6718 ************************************************************************/
6719
6720 /**
6721 * xmlRelaxNGNewParserCtxt:
6722 * @URL: the location of the schema
6723 *
6724 * Create an XML RelaxNGs parse context for that file/resource expected
6725 * to contain an XML RelaxNGs file.
6726 *
6727 * Returns the parser context or NULL in case of error
6728 */
6729 xmlRelaxNGParserCtxtPtr
xmlRelaxNGNewParserCtxt(const char * URL)6730 xmlRelaxNGNewParserCtxt(const char *URL)
6731 {
6732 xmlRelaxNGParserCtxtPtr ret;
6733
6734 if (URL == NULL)
6735 return (NULL);
6736
6737 ret =
6738 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6739 if (ret == NULL) {
6740 xmlRngPErrMemory(NULL, "building parser\n");
6741 return (NULL);
6742 }
6743 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6744 ret->URL = xmlStrdup((const xmlChar *) URL);
6745 ret->error = xmlGenericError;
6746 ret->userData = xmlGenericErrorContext;
6747 return (ret);
6748 }
6749
6750 /**
6751 * xmlRelaxNGNewMemParserCtxt:
6752 * @buffer: a pointer to a char array containing the schemas
6753 * @size: the size of the array
6754 *
6755 * Create an XML RelaxNGs parse context for that memory buffer expected
6756 * to contain an XML RelaxNGs file.
6757 *
6758 * Returns the parser context or NULL in case of error
6759 */
6760 xmlRelaxNGParserCtxtPtr
xmlRelaxNGNewMemParserCtxt(const char * buffer,int size)6761 xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6762 {
6763 xmlRelaxNGParserCtxtPtr ret;
6764
6765 if ((buffer == NULL) || (size <= 0))
6766 return (NULL);
6767
6768 ret =
6769 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6770 if (ret == NULL) {
6771 xmlRngPErrMemory(NULL, "building parser\n");
6772 return (NULL);
6773 }
6774 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6775 ret->buffer = buffer;
6776 ret->size = size;
6777 ret->error = xmlGenericError;
6778 ret->userData = xmlGenericErrorContext;
6779 return (ret);
6780 }
6781
6782 /**
6783 * xmlRelaxNGNewDocParserCtxt:
6784 * @doc: a preparsed document tree
6785 *
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.
6789 *
6790 * Returns the parser context or NULL in case of error
6791 */
6792 xmlRelaxNGParserCtxtPtr
xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)6793 xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6794 {
6795 xmlRelaxNGParserCtxtPtr ret;
6796 xmlDocPtr copy;
6797
6798 if (doc == NULL)
6799 return (NULL);
6800 copy = xmlCopyDoc(doc, 1);
6801 if (copy == NULL)
6802 return (NULL);
6803
6804 ret =
6805 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6806 if (ret == NULL) {
6807 xmlRngPErrMemory(NULL, "building parser\n");
6808 return (NULL);
6809 }
6810 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6811 ret->document = copy;
6812 ret->freedoc = 1;
6813 ret->userData = xmlGenericErrorContext;
6814 return (ret);
6815 }
6816
6817 /**
6818 * xmlRelaxNGFreeParserCtxt:
6819 * @ctxt: the schema parser context
6820 *
6821 * Free the resources associated to the schema parser context
6822 */
6823 void
xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)6824 xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6825 {
6826 if (ctxt == NULL)
6827 return;
6828 if (ctxt->URL != NULL)
6829 xmlFree(ctxt->URL);
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) {
6843 int i;
6844
6845 for (i = 0; i < ctxt->defNr; i++)
6846 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6847 xmlFree(ctxt->defTab);
6848 }
6849 if ((ctxt->document != NULL) && (ctxt->freedoc))
6850 xmlFreeDoc(ctxt->document);
6851 xmlFree(ctxt);
6852 }
6853
6854 /**
6855 * xmlRelaxNGNormExtSpace:
6856 * @value: a value
6857 *
6858 * Removes the leading and ending spaces of the value
6859 * The string is modified "in situ"
6860 */
6861 static void
xmlRelaxNGNormExtSpace(xmlChar * value)6862 xmlRelaxNGNormExtSpace(xmlChar * value)
6863 {
6864 xmlChar *start = value;
6865 xmlChar *cur = value;
6866
6867 if (value == NULL)
6868 return;
6869
6870 while (IS_BLANK_CH(*cur))
6871 cur++;
6872 if (cur == start) {
6873 do {
6874 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
6875 cur++;
6876 if (*cur == 0)
6877 return;
6878 start = cur;
6879 while (IS_BLANK_CH(*cur))
6880 cur++;
6881 if (*cur == 0) {
6882 *start = 0;
6883 return;
6884 }
6885 } while (1);
6886 } else {
6887 do {
6888 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
6889 *start++ = *cur++;
6890 if (*cur == 0) {
6891 *start = 0;
6892 return;
6893 }
6894 /* don't try to normalize the inner spaces */
6895 while (IS_BLANK_CH(*cur))
6896 cur++;
6897 if (*cur == 0) {
6898 *start = 0;
6899 return;
6900 }
6901 *start++ = *cur++;
6902 } while (1);
6903 }
6904 }
6905
6906 /**
6907 * xmlRelaxNGCleanupAttributes:
6908 * @ctxt: a Relax-NG parser context
6909 * @node: a Relax-NG node
6910 *
6911 * Check all the attributes on the given node
6912 */
6913 static void
xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr node)6914 xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6915 {
6916 xmlAttrPtr cur, next;
6917
6918 cur = node->properties;
6919 while (cur != NULL) {
6920 next = cur->next;
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);
6933 }
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);
6940 }
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);
6947 }
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);
6954 }
6955 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6956 xmlChar *val;
6957 xmlURIPtr uri;
6958
6959 val = xmlNodeListGetString(node->doc, cur->children, 1);
6960 if (val != NULL) {
6961 if (val[0] != 0) {
6962 uri = xmlParseURI((const char *) val);
6963 if (uri == NULL) {
6964 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6965 "Attribute %s contains invalid URI %s\n",
6966 cur->name, val);
6967 } else {
6968 if (uri->scheme == NULL) {
6969 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6970 "Attribute %s URI %s is not absolute\n",
6971 cur->name, val);
6972 }
6973 if (uri->fragment != NULL) {
6974 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6975 "Attribute %s URI %s has a fragment ID\n",
6976 cur->name, val);
6977 }
6978 xmlFreeURI(uri);
6979 }
6980 }
6981 xmlFree(val);
6982 }
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,
6986 node->name);
6987 }
6988 }
6989 cur = next;
6990 }
6991 }
6992
6993 /**
6994 * xmlRelaxNGCleanupTree:
6995 * @ctxt: a Relax-NG parser context
6996 * @root: an xmlNodePtr subtree
6997 *
6998 * Cleanup the subtree from unwanted nodes for parsing, resolve
6999 * Include and externalRef lookups.
7000 */
7001 static void
xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt,xmlNodePtr root)7002 xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
7003 {
7004 xmlNodePtr cur, delete;
7005
7006 delete = NULL;
7007 cur = root;
7008 while (cur != NULL) {
7009 if (delete != NULL) {
7010 xmlUnlinkNode(delete);
7011 xmlFreeNode(delete);
7012 delete = NULL;
7013 }
7014 if (cur->type == XML_ELEMENT_NODE) {
7015 /*
7016 * Simplification 4.1. Annotations
7017 */
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);
7028 }
7029 delete = cur;
7030 goto skip_children;
7031 } else {
7032 xmlRelaxNGCleanupAttributes(ctxt, cur);
7033 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
7034 xmlChar *href, *ns, *base, *URL;
7035 xmlRelaxNGDocumentPtr docu;
7036 xmlNodePtr tmp;
7037 xmlURIPtr uri;
7038
7039 ns = xmlGetProp(cur, BAD_CAST "ns");
7040 if (ns == NULL) {
7041 tmp = cur->parent;
7042 while ((tmp != NULL) &&
7043 (tmp->type == XML_ELEMENT_NODE)) {
7044 ns = xmlGetProp(tmp, BAD_CAST "ns");
7045 if (ns != NULL)
7046 break;
7047 tmp = tmp->parent;
7048 }
7049 }
7050 href = xmlGetProp(cur, BAD_CAST "href");
7051 if (href == NULL) {
7052 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7053 "xmlRelaxNGParse: externalRef has no href attribute\n",
7054 NULL, NULL);
7055 if (ns != NULL)
7056 xmlFree(ns);
7057 delete = cur;
7058 goto skip_children;
7059 }
7060 uri = xmlParseURI((const char *) href);
7061 if (uri == NULL) {
7062 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7063 "Incorrect URI for externalRef %s\n",
7064 href, NULL);
7065 if (ns != NULL)
7066 xmlFree(ns);
7067 if (href != NULL)
7068 xmlFree(href);
7069 delete = cur;
7070 goto skip_children;
7071 }
7072 if (uri->fragment != NULL) {
7073 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7074 "Fragment forbidden in URI for externalRef %s\n",
7075 href, NULL);
7076 if (ns != NULL)
7077 xmlFree(ns);
7078 xmlFreeURI(uri);
7079 if (href != NULL)
7080 xmlFree(href);
7081 delete = cur;
7082 goto skip_children;
7083 }
7084 xmlFreeURI(uri);
7085 base = xmlNodeGetBase(cur->doc, cur);
7086 URL = xmlBuildURI(href, base);
7087 if (URL == NULL) {
7088 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7089 "Failed to compute URL for externalRef %s\n",
7090 href, NULL);
7091 if (ns != NULL)
7092 xmlFree(ns);
7093 if (href != NULL)
7094 xmlFree(href);
7095 if (base != NULL)
7096 xmlFree(base);
7097 delete = cur;
7098 goto skip_children;
7099 }
7100 if (href != NULL)
7101 xmlFree(href);
7102 if (base != NULL)
7103 xmlFree(base);
7104 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7105 if (docu == NULL) {
7106 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7107 "Failed to load externalRef %s\n", URL,
7108 NULL);
7109 if (ns != NULL)
7110 xmlFree(ns);
7111 xmlFree(URL);
7112 delete = cur;
7113 goto skip_children;
7114 }
7115 if (ns != NULL)
7116 xmlFree(ns);
7117 xmlFree(URL);
7118 cur->psvi = docu;
7119 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7120 xmlChar *href, *ns, *base, *URL;
7121 xmlRelaxNGIncludePtr incl;
7122 xmlNodePtr tmp;
7123
7124 href = xmlGetProp(cur, BAD_CAST "href");
7125 if (href == NULL) {
7126 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7127 "xmlRelaxNGParse: include has no href attribute\n",
7128 NULL, NULL);
7129 delete = cur;
7130 goto skip_children;
7131 }
7132 base = xmlNodeGetBase(cur->doc, cur);
7133 URL = xmlBuildURI(href, base);
7134 if (URL == NULL) {
7135 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7136 "Failed to compute URL for include %s\n",
7137 href, NULL);
7138 if (href != NULL)
7139 xmlFree(href);
7140 if (base != NULL)
7141 xmlFree(base);
7142 delete = cur;
7143 goto skip_children;
7144 }
7145 if (href != NULL)
7146 xmlFree(href);
7147 if (base != NULL)
7148 xmlFree(base);
7149 ns = xmlGetProp(cur, BAD_CAST "ns");
7150 if (ns == NULL) {
7151 tmp = cur->parent;
7152 while ((tmp != NULL) &&
7153 (tmp->type == XML_ELEMENT_NODE)) {
7154 ns = xmlGetProp(tmp, BAD_CAST "ns");
7155 if (ns != NULL)
7156 break;
7157 tmp = tmp->parent;
7158 }
7159 }
7160 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7161 if (ns != NULL)
7162 xmlFree(ns);
7163 if (incl == NULL) {
7164 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7165 "Failed to load include %s\n", URL,
7166 NULL);
7167 xmlFree(URL);
7168 delete = cur;
7169 goto skip_children;
7170 }
7171 xmlFree(URL);
7172 cur->psvi = incl;
7173 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7174 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7175 {
7176 xmlChar *name, *ns;
7177 xmlNodePtr text = NULL;
7178
7179 /*
7180 * Simplification 4.8. name attribute of element
7181 * and attribute elements
7182 */
7183 name = xmlGetProp(cur, BAD_CAST "name");
7184 if (name != NULL) {
7185 if (cur->children == NULL) {
7186 text =
7187 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7188 name);
7189 } else {
7190 xmlNodePtr node;
7191
7192 node = xmlNewDocNode(cur->doc, cur->ns,
7193 BAD_CAST "name", NULL);
7194 if (node != NULL) {
7195 xmlAddPrevSibling(cur->children, node);
7196 text = xmlNewText(name);
7197 xmlAddChild(node, text);
7198 text = node;
7199 }
7200 }
7201 if (text == NULL) {
7202 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7203 "Failed to create a name %s element\n",
7204 name, NULL);
7205 }
7206 xmlUnsetProp(cur, BAD_CAST "name");
7207 xmlFree(name);
7208 ns = xmlGetProp(cur, BAD_CAST "ns");
7209 if (ns != NULL) {
7210 if (text != NULL) {
7211 xmlSetProp(text, BAD_CAST "ns", ns);
7212 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7213 }
7214 xmlFree(ns);
7215 } else if (xmlStrEqual(cur->name,
7216 BAD_CAST "attribute")) {
7217 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7218 }
7219 }
7220 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7221 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7222 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7223 /*
7224 * Simplification 4.8. name attribute of element
7225 * and attribute elements
7226 */
7227 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7228 xmlNodePtr node;
7229 xmlChar *ns = NULL;
7230
7231 node = cur->parent;
7232 while ((node != NULL) &&
7233 (node->type == XML_ELEMENT_NODE)) {
7234 ns = xmlGetProp(node, BAD_CAST "ns");
7235 if (ns != NULL) {
7236 break;
7237 }
7238 node = node->parent;
7239 }
7240 if (ns == NULL) {
7241 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7242 } else {
7243 xmlSetProp(cur, BAD_CAST "ns", ns);
7244 xmlFree(ns);
7245 }
7246 }
7247 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7248 xmlChar *name, *local, *prefix;
7249
7250 /*
7251 * Simplification: 4.10. QNames
7252 */
7253 name = xmlNodeGetContent(cur);
7254 if (name != NULL) {
7255 local = xmlSplitQName2(name, &prefix);
7256 if (local != NULL) {
7257 xmlNsPtr ns;
7258
7259 ns = xmlSearchNs(cur->doc, cur, prefix);
7260 if (ns == NULL) {
7261 xmlRngPErr(ctxt, cur,
7262 XML_RNGP_PREFIX_UNDEFINED,
7263 "xmlRelaxNGParse: no namespace for prefix %s\n",
7264 prefix, NULL);
7265 } else {
7266 xmlSetProp(cur, BAD_CAST "ns",
7267 ns->href);
7268 xmlNodeSetContent(cur, local);
7269 }
7270 xmlFree(local);
7271 xmlFree(prefix);
7272 }
7273 xmlFree(name);
7274 }
7275 }
7276 /*
7277 * 4.16
7278 */
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",
7284 NULL, NULL);
7285 }
7286 }
7287 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7288 (cur != root)) {
7289 int oldflags = ctxt->flags;
7290
7291 /*
7292 * 4.16
7293 */
7294 if ((cur->parent != NULL) &&
7295 (xmlStrEqual
7296 (cur->parent->name, BAD_CAST "anyName"))) {
7297 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7298 xmlRelaxNGCleanupTree(ctxt, cur);
7299 ctxt->flags = oldflags;
7300 goto skip_children;
7301 } else if ((cur->parent != NULL) &&
7302 (xmlStrEqual
7303 (cur->parent->name, BAD_CAST "nsName"))) {
7304 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7305 xmlRelaxNGCleanupTree(ctxt, cur);
7306 ctxt->flags = oldflags;
7307 goto skip_children;
7308 }
7309 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7310 /*
7311 * 4.16
7312 */
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",
7317 NULL, NULL);
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",
7322 NULL, NULL);
7323 }
7324 }
7325 /*
7326 * This is not an else since "include" is transformed
7327 * into a div
7328 */
7329 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7330 xmlChar *ns;
7331 xmlNodePtr child, ins, tmp;
7332
7333 /*
7334 * implements rule 4.11
7335 */
7336
7337 ns = xmlGetProp(cur, BAD_CAST "ns");
7338
7339 child = cur->children;
7340 ins = cur;
7341 while (child != NULL) {
7342 if (ns != NULL) {
7343 if (!xmlHasProp(child, BAD_CAST "ns")) {
7344 xmlSetProp(child, BAD_CAST "ns", ns);
7345 }
7346 }
7347 tmp = child->next;
7348 xmlUnlinkNode(child);
7349 ins = xmlAddNextSibling(ins, child);
7350 child = tmp;
7351 }
7352 if (ns != NULL)
7353 xmlFree(ns);
7354 /*
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
7359 * (bug 143738).
7360 */
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;
7366 cur->nsDef = NULL;
7367 }
7368 delete = cur;
7369 goto skip_children;
7370 }
7371 }
7372 }
7373 /*
7374 * Simplification 4.2 whitespaces
7375 */
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"))
7382 &&
7383 (!xmlStrEqual
7384 (cur->parent->name, BAD_CAST "param")))
7385 delete = cur;
7386 } else {
7387 delete = cur;
7388 goto skip_children;
7389 }
7390 }
7391 } else {
7392 delete = cur;
7393 goto skip_children;
7394 }
7395
7396 /*
7397 * Skip to next node
7398 */
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;
7404 continue;
7405 }
7406 }
7407 skip_children:
7408 if (cur->next != NULL) {
7409 cur = cur->next;
7410 continue;
7411 }
7412
7413 do {
7414 cur = cur->parent;
7415 if (cur == NULL)
7416 break;
7417 if (cur == root) {
7418 cur = NULL;
7419 break;
7420 }
7421 if (cur->next != NULL) {
7422 cur = cur->next;
7423 break;
7424 }
7425 } while (cur != NULL);
7426 }
7427 if (delete != NULL) {
7428 xmlUnlinkNode(delete);
7429 xmlFreeNode(delete);
7430 delete = NULL;
7431 }
7432 }
7433
7434 /**
7435 * xmlRelaxNGCleanupDoc:
7436 * @ctxt: a Relax-NG parser context
7437 * @doc: an xmldocPtr document pointer
7438 *
7439 * Cleanup the document from unwanted nodes for parsing, resolve
7440 * Include and externalRef lookups.
7441 *
7442 * Returns the cleaned up document or NULL in case of error
7443 */
7444 static xmlDocPtr
xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,xmlDocPtr doc)7445 xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7446 {
7447 xmlNodePtr root;
7448
7449 /*
7450 * Extract the root
7451 */
7452 root = xmlDocGetRootElement(doc);
7453 if (root == NULL) {
7454 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7455 ctxt->URL, NULL);
7456 return (NULL);
7457 }
7458 xmlRelaxNGCleanupTree(ctxt, root);
7459 return (doc);
7460 }
7461
7462 /**
7463 * xmlRelaxNGParse:
7464 * @ctxt: a Relax-NG parser context
7465 *
7466 * parse a schema definition resource and build an internal
7467 * XML Shema struture which can be used to validate instances.
7468 *
7469 * Returns the internal XML RelaxNG structure built from the resource or
7470 * NULL in case of error
7471 */
7472 xmlRelaxNGPtr
xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)7473 xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7474 {
7475 xmlRelaxNGPtr ret = NULL;
7476 xmlDocPtr doc;
7477 xmlNodePtr root;
7478
7479 xmlRelaxNGInitTypes();
7480
7481 if (ctxt == NULL)
7482 return (NULL);
7483
7484 /*
7485 * First step is to parse the input document into an DOM/Infoset
7486 */
7487 if (ctxt->URL != NULL) {
7488 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
7489 if (doc == NULL) {
7490 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7491 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7492 NULL);
7493 return (NULL);
7494 }
7495 } else if (ctxt->buffer != NULL) {
7496 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
7497 if (doc == NULL) {
7498 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7499 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7500 NULL);
7501 return (NULL);
7502 }
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;
7507 } else {
7508 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7509 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7510 return (NULL);
7511 }
7512 ctxt->document = doc;
7513
7514 /*
7515 * Some preprocessing of the document content
7516 */
7517 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7518 if (doc == NULL) {
7519 xmlFreeDoc(ctxt->document);
7520 ctxt->document = NULL;
7521 return (NULL);
7522 }
7523
7524 /*
7525 * Then do the parsing for good
7526 */
7527 root = xmlDocGetRootElement(doc);
7528 if (root == NULL) {
7529 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7530 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7531 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
7532
7533 xmlFreeDoc(ctxt->document);
7534 ctxt->document = NULL;
7535 return (NULL);
7536 }
7537 ret = xmlRelaxNGParseDocument(ctxt, root);
7538 if (ret == NULL) {
7539 xmlFreeDoc(ctxt->document);
7540 ctxt->document = NULL;
7541 return (NULL);
7542 }
7543
7544 /*
7545 * Check the ref/defines links
7546 */
7547 /*
7548 * try to preprocess interleaves
7549 */
7550 if (ctxt->interleaves != NULL) {
7551 xmlHashScan(ctxt->interleaves,
7552 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
7553 }
7554
7555 /*
7556 * if there was a parsing error return NULL
7557 */
7558 if (ctxt->nbErrors > 0) {
7559 xmlRelaxNGFree(ret);
7560 ctxt->document = NULL;
7561 xmlFreeDoc(doc);
7562 return (NULL);
7563 }
7564
7565 /*
7566 * try to compile (parts of) the schemas
7567 */
7568 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7569 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
7570 xmlRelaxNGDefinePtr def;
7571
7572 def = xmlRelaxNGNewDefine(ctxt, NULL);
7573 if (def != NULL) {
7574 def->type = XML_RELAXNG_START;
7575 def->content = ret->topgrammar->start;
7576 ret->topgrammar->start = def;
7577 }
7578 }
7579 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
7580 }
7581
7582 /*
7583 * Transfer the pointer for cleanup at the schema level.
7584 */
7585 ret->doc = doc;
7586 ctxt->document = NULL;
7587 ret->documents = ctxt->documents;
7588 ctxt->documents = NULL;
7589
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)
7596 ret->idref = 1;
7597
7598 return (ret);
7599 }
7600
7601 /**
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
7607 *
7608 * Set the callback functions used to handle errors for a validation context
7609 */
7610 void
xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGValidityErrorFunc err,xmlRelaxNGValidityWarningFunc warn,void * ctx)7611 xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7612 xmlRelaxNGValidityErrorFunc err,
7613 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7614 {
7615 if (ctxt == NULL)
7616 return;
7617 ctxt->error = err;
7618 ctxt->warning = warn;
7619 ctxt->serror = NULL;
7620 ctxt->userData = ctx;
7621 }
7622
7623 /**
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
7629 *
7630 * Get the callback information used to handle errors for a validation context
7631 *
7632 * Returns -1 in case of failure, 0 otherwise.
7633 */
7634 int
xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,xmlRelaxNGValidityErrorFunc * err,xmlRelaxNGValidityWarningFunc * warn,void ** ctx)7635 xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7636 xmlRelaxNGValidityErrorFunc * err,
7637 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7638 {
7639 if (ctxt == NULL)
7640 return (-1);
7641 if (err != NULL)
7642 *err = ctxt->error;
7643 if (warn != NULL)
7644 *warn = ctxt->warning;
7645 if (ctx != NULL)
7646 *ctx = ctxt->userData;
7647 return (0);
7648 }
7649
7650 /**
7651 * xmlRelaxNGSetParserStructuredErrors:
7652 * @ctxt: a Relax-NG parser context
7653 * @serror: the error callback
7654 * @ctx: contextual data for the callbacks
7655 *
7656 * Set the callback functions used to handle errors for a parsing context
7657 */
7658 void
xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,xmlStructuredErrorFunc serror,void * ctx)7659 xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
7660 xmlStructuredErrorFunc serror,
7661 void *ctx)
7662 {
7663 if (ctxt == NULL)
7664 return;
7665 ctxt->serror = serror;
7666 ctxt->error = NULL;
7667 ctxt->warning = NULL;
7668 ctxt->userData = ctx;
7669 }
7670
7671 #ifdef LIBXML_OUTPUT_ENABLED
7672
7673 /************************************************************************
7674 * *
7675 * Dump back a compiled form *
7676 * *
7677 ************************************************************************/
7678 static void xmlRelaxNGDumpDefine(FILE * output,
7679 xmlRelaxNGDefinePtr define);
7680
7681 /**
7682 * xmlRelaxNGDumpDefines:
7683 * @output: the file output
7684 * @defines: a list of define structures
7685 *
7686 * Dump a RelaxNG structure back
7687 */
7688 static void
xmlRelaxNGDumpDefines(FILE * output,xmlRelaxNGDefinePtr defines)7689 xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7690 {
7691 while (defines != NULL) {
7692 xmlRelaxNGDumpDefine(output, defines);
7693 defines = defines->next;
7694 }
7695 }
7696
7697 /**
7698 * xmlRelaxNGDumpDefine:
7699 * @output: the file output
7700 * @define: a define structure
7701 *
7702 * Dump a RelaxNG structure back
7703 */
7704 static void
xmlRelaxNGDumpDefine(FILE * output,xmlRelaxNGDefinePtr define)7705 xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7706 {
7707 if (define == NULL)
7708 return;
7709 switch (define->type) {
7710 case XML_RELAXNG_EMPTY:
7711 fprintf(output, "<empty/>\n");
7712 break;
7713 case XML_RELAXNG_NOT_ALLOWED:
7714 fprintf(output, "<notAllowed/>\n");
7715 break;
7716 case XML_RELAXNG_TEXT:
7717 fprintf(output, "<text/>\n");
7718 break;
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);
7726 }
7727 xmlRelaxNGDumpDefines(output, define->attrs);
7728 xmlRelaxNGDumpDefines(output, define->content);
7729 fprintf(output, "</element>\n");
7730 break;
7731 case XML_RELAXNG_LIST:
7732 fprintf(output, "<list>\n");
7733 xmlRelaxNGDumpDefines(output, define->content);
7734 fprintf(output, "</list>\n");
7735 break;
7736 case XML_RELAXNG_ONEORMORE:
7737 fprintf(output, "<oneOrMore>\n");
7738 xmlRelaxNGDumpDefines(output, define->content);
7739 fprintf(output, "</oneOrMore>\n");
7740 break;
7741 case XML_RELAXNG_ZEROORMORE:
7742 fprintf(output, "<zeroOrMore>\n");
7743 xmlRelaxNGDumpDefines(output, define->content);
7744 fprintf(output, "</zeroOrMore>\n");
7745 break;
7746 case XML_RELAXNG_CHOICE:
7747 fprintf(output, "<choice>\n");
7748 xmlRelaxNGDumpDefines(output, define->content);
7749 fprintf(output, "</choice>\n");
7750 break;
7751 case XML_RELAXNG_GROUP:
7752 fprintf(output, "<group>\n");
7753 xmlRelaxNGDumpDefines(output, define->content);
7754 fprintf(output, "</group>\n");
7755 break;
7756 case XML_RELAXNG_INTERLEAVE:
7757 fprintf(output, "<interleave>\n");
7758 xmlRelaxNGDumpDefines(output, define->content);
7759 fprintf(output, "</interleave>\n");
7760 break;
7761 case XML_RELAXNG_OPTIONAL:
7762 fprintf(output, "<optional>\n");
7763 xmlRelaxNGDumpDefines(output, define->content);
7764 fprintf(output, "</optional>\n");
7765 break;
7766 case XML_RELAXNG_ATTRIBUTE:
7767 fprintf(output, "<attribute>\n");
7768 xmlRelaxNGDumpDefines(output, define->content);
7769 fprintf(output, "</attribute>\n");
7770 break;
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");
7778 break;
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");
7786 break;
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");
7794 break;
7795 case XML_RELAXNG_EXTERNALREF:
7796 fprintf(output, "<externalRef>");
7797 xmlRelaxNGDumpDefines(output, define->content);
7798 fprintf(output, "</externalRef>\n");
7799 break;
7800 case XML_RELAXNG_DATATYPE:
7801 case XML_RELAXNG_VALUE:
7802 TODO break;
7803 case XML_RELAXNG_START:
7804 case XML_RELAXNG_EXCEPT:
7805 case XML_RELAXNG_PARAM:
7806 TODO break;
7807 case XML_RELAXNG_NOOP:
7808 xmlRelaxNGDumpDefines(output, define->content);
7809 break;
7810 }
7811 }
7812
7813 /**
7814 * xmlRelaxNGDumpGrammar:
7815 * @output: the file output
7816 * @grammar: a grammar structure
7817 * @top: is this a top grammar
7818 *
7819 * Dump a RelaxNG structure back
7820 */
7821 static void
xmlRelaxNGDumpGrammar(FILE * output,xmlRelaxNGGrammarPtr grammar,int top)7822 xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7823 {
7824 if (grammar == NULL)
7825 return;
7826
7827 fprintf(output, "<grammar");
7828 if (top)
7829 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7830 switch (grammar->combine) {
7831 case XML_RELAXNG_COMBINE_UNDEFINED:
7832 break;
7833 case XML_RELAXNG_COMBINE_CHOICE:
7834 fprintf(output, " combine=\"choice\"");
7835 break;
7836 case XML_RELAXNG_COMBINE_INTERLEAVE:
7837 fprintf(output, " combine=\"interleave\"");
7838 break;
7839 default:
7840 fprintf(output, " <!-- invalid combine value -->");
7841 }
7842 fprintf(output, ">\n");
7843 if (grammar->start == NULL) {
7844 fprintf(output, " <!-- grammar had no start -->");
7845 } else {
7846 fprintf(output, "<start>\n");
7847 xmlRelaxNGDumpDefine(output, grammar->start);
7848 fprintf(output, "</start>\n");
7849 }
7850 /* TODO ? Dump the defines ? */
7851 fprintf(output, "</grammar>\n");
7852 }
7853
7854 /**
7855 * xmlRelaxNGDump:
7856 * @output: the file output
7857 * @schema: a schema structure
7858 *
7859 * Dump a RelaxNG structure back
7860 */
7861 void
xmlRelaxNGDump(FILE * output,xmlRelaxNGPtr schema)7862 xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7863 {
7864 if (output == NULL)
7865 return;
7866 if (schema == NULL) {
7867 fprintf(output, "RelaxNG empty or failed to compile\n");
7868 return;
7869 }
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);
7875 } else {
7876 fprintf(output, "\n");
7877 }
7878 if (schema->topgrammar == NULL) {
7879 fprintf(output, "RelaxNG has no top grammar\n");
7880 return;
7881 }
7882 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7883 }
7884
7885 /**
7886 * xmlRelaxNGDumpTree:
7887 * @output: the file output
7888 * @schema: a schema structure
7889 *
7890 * Dump the transformed RelaxNG tree.
7891 */
7892 void
xmlRelaxNGDumpTree(FILE * output,xmlRelaxNGPtr schema)7893 xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7894 {
7895 if (output == NULL)
7896 return;
7897 if (schema == NULL) {
7898 fprintf(output, "RelaxNG empty or failed to compile\n");
7899 return;
7900 }
7901 if (schema->doc == NULL) {
7902 fprintf(output, "no document\n");
7903 } else {
7904 xmlDocDump(output, schema->doc);
7905 }
7906 }
7907 #endif /* LIBXML_OUTPUT_ENABLED */
7908
7909 /************************************************************************
7910 * *
7911 * Validation of compiled content *
7912 * *
7913 ************************************************************************/
7914 static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7915 xmlRelaxNGDefinePtr define);
7916
7917 /**
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
7923 *
7924 * Handle the callback and if needed validate the element children.
7925 */
7926 static void
xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,const xmlChar * token,void * transdata,void * inputdata)7927 xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7928 const xmlChar * token,
7929 void *transdata, void *inputdata)
7930 {
7931 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7932 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7933 int ret;
7934
7935 #ifdef DEBUG_COMPILE
7936 xmlGenericError(xmlGenericErrorContext,
7937 "Compiled callback for: '%s'\n", token);
7938 #endif
7939 if (ctxt == NULL) {
7940 fprintf(stderr, "callback on %s missing context\n", token);
7941 return;
7942 }
7943 if (define == NULL) {
7944 if (token[0] == '#')
7945 return;
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;
7949 return;
7950 }
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;
7955 return;
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;
7960 return;
7961 }
7962 ret = xmlRelaxNGValidateDefinition(ctxt, define);
7963 if (ret != 0)
7964 ctxt->perr = ret;
7965 }
7966
7967 /**
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
7972 *
7973 * Validate the content model of an element or start using the regexp
7974 *
7975 * Returns 0 in case of success, -1 in case of error.
7976 */
7977 static int
xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,xmlRegexpPtr regexp,xmlNodePtr content)7978 xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
7979 xmlRegexpPtr regexp, xmlNodePtr content)
7980 {
7981 xmlRegExecCtxtPtr exec;
7982 xmlNodePtr cur;
7983 int ret = 0;
7984 int oldperr;
7985
7986 if ((ctxt == NULL) || (regexp == NULL))
7987 return (-1);
7988 oldperr = ctxt->perr;
7989 exec = xmlRegNewExecCtxt(regexp,
7990 xmlRelaxNGValidateCompiledCallback, ctxt);
7991 ctxt->perr = 0;
7992 cur = content;
7993 while (cur != NULL) {
7994 ctxt->state->seq = cur;
7995 switch (cur->type) {
7996 case XML_TEXT_NODE:
7997 case XML_CDATA_SECTION_NODE:
7998 if (xmlIsBlankNode(cur))
7999 break;
8000 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
8001 if (ret < 0) {
8002 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
8003 cur->parent->name);
8004 }
8005 break;
8006 case XML_ELEMENT_NODE:
8007 if (cur->ns != NULL) {
8008 ret = xmlRegExecPushString2(exec, cur->name,
8009 cur->ns->href, ctxt);
8010 } else {
8011 ret = xmlRegExecPushString(exec, cur->name, ctxt);
8012 }
8013 if (ret < 0) {
8014 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
8015 }
8016 break;
8017 default:
8018 break;
8019 }
8020 if (ret < 0)
8021 break;
8022 /*
8023 * Switch to next element
8024 */
8025 cur = cur->next;
8026 }
8027 ret = xmlRegExecPushString(exec, NULL, NULL);
8028 if (ret == 1) {
8029 ret = 0;
8030 ctxt->state->seq = NULL;
8031 } else if (ret == 0) {
8032 /*
8033 * TODO: get some of the names needed to exit the current state of exec
8034 */
8035 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8036 ret = -1;
8037 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8038 xmlRelaxNGDumpValidError(ctxt);
8039 } else {
8040 ret = -1;
8041 }
8042 xmlRegFreeExecCtxt(exec);
8043 /*
8044 * There might be content model errors outside of the pure
8045 * regexp validation, e.g. for attribute values.
8046 */
8047 if ((ret == 0) && (ctxt->perr != 0)) {
8048 ret = ctxt->perr;
8049 }
8050 ctxt->perr = oldperr;
8051 return (ret);
8052 }
8053
8054 /************************************************************************
8055 * *
8056 * Progressive validation of when possible *
8057 * *
8058 ************************************************************************/
8059 static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8060 xmlRelaxNGDefinePtr defines);
8061 static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
8062 int dolog);
8063 static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
8064
8065 /**
8066 * xmlRelaxNGElemPush:
8067 * @ctxt: the validation context
8068 * @exec: the regexp runtime for the new content model
8069 *
8070 * Push a new regexp for the current node content model on the stack
8071 *
8072 * Returns 0 in case of success and -1 in case of error.
8073 */
8074 static int
xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt,xmlRegExecCtxtPtr exec)8075 xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8076 {
8077 if (ctxt->elemTab == NULL) {
8078 ctxt->elemMax = 10;
8079 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8080 sizeof
8081 (xmlRegExecCtxtPtr));
8082 if (ctxt->elemTab == NULL) {
8083 xmlRngVErrMemory(ctxt, "validating\n");
8084 return (-1);
8085 }
8086 }
8087 if (ctxt->elemNr >= ctxt->elemMax) {
8088 ctxt->elemMax *= 2;
8089 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
8090 ctxt->elemMax *
8091 sizeof
8092 (xmlRegExecCtxtPtr));
8093 if (ctxt->elemTab == NULL) {
8094 xmlRngVErrMemory(ctxt, "validating\n");
8095 return (-1);
8096 }
8097 }
8098 ctxt->elemTab[ctxt->elemNr++] = exec;
8099 ctxt->elem = exec;
8100 return (0);
8101 }
8102
8103 /**
8104 * xmlRelaxNGElemPop:
8105 * @ctxt: the validation context
8106 *
8107 * Pop the regexp of the current node content model from the stack
8108 *
8109 * Returns the exec or NULL if empty
8110 */
8111 static xmlRegExecCtxtPtr
xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)8112 xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8113 {
8114 xmlRegExecCtxtPtr ret;
8115
8116 if (ctxt->elemNr <= 0)
8117 return (NULL);
8118 ctxt->elemNr--;
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];
8123 else
8124 ctxt->elem = NULL;
8125 return (ret);
8126 }
8127
8128 /**
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
8134 *
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.
8137 */
8138 static void
xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,const xmlChar * token,void * transdata,void * inputdata)8139 xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8140 ATTRIBUTE_UNUSED,
8141 const xmlChar * token,
8142 void *transdata, void *inputdata)
8143 {
8144 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8145 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
8146 xmlRelaxNGValidStatePtr state, oldstate;
8147 xmlNodePtr node;
8148 int ret = 0, oldflags;
8149
8150 #ifdef DEBUG_PROGRESSIVE
8151 xmlGenericError(xmlGenericErrorContext,
8152 "Progressive callback for: '%s'\n", token);
8153 #endif
8154 if (ctxt == NULL) {
8155 fprintf(stderr, "callback on %s missing context\n", token);
8156 return;
8157 }
8158 node = ctxt->pnode;
8159 ctxt->pstate = 1;
8160 if (define == NULL) {
8161 if (token[0] == '#')
8162 return;
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;
8166 ctxt->pstate = -1;
8167 return;
8168 }
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;
8173 ctxt->pstate = -1;
8174 return;
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;
8179 ctxt->pstate = -1;
8180 return;
8181 }
8182 if (node->type != XML_ELEMENT_NODE) {
8183 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8184 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8185 xmlRelaxNGDumpValidError(ctxt);
8186 ctxt->pstate = -1;
8187 return;
8188 }
8189 if (define->contModel == NULL) {
8190 /*
8191 * this node cannot be validated in a streamable fashion
8192 */
8193 #ifdef DEBUG_PROGRESSIVE
8194 xmlGenericError(xmlGenericErrorContext,
8195 "Element '%s' validation is not streamable\n",
8196 token);
8197 #endif
8198 ctxt->pstate = 0;
8199 ctxt->pdef = define;
8200 return;
8201 }
8202 exec = xmlRegNewExecCtxt(define->contModel,
8203 xmlRelaxNGValidateProgressiveCallback, ctxt);
8204 if (exec == NULL) {
8205 ctxt->pstate = -1;
8206 return;
8207 }
8208 xmlRelaxNGElemPush(ctxt, exec);
8209
8210 /*
8211 * Validate the attributes part of the content.
8212 */
8213 state = xmlRelaxNGNewValidState(ctxt, node);
8214 if (state == NULL) {
8215 ctxt->pstate = -1;
8216 return;
8217 }
8218 oldstate = ctxt->state;
8219 ctxt->state = state;
8220 if (define->attrs != NULL) {
8221 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8222 if (ret != 0) {
8223 ctxt->pstate = -1;
8224 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8225 }
8226 }
8227 if (ctxt->state != NULL) {
8228 ctxt->state->seq = NULL;
8229 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8230 if (ret != 0) {
8231 ctxt->pstate = -1;
8232 }
8233 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
8234 } else if (ctxt->states != NULL) {
8235 int tmp = -1, i;
8236
8237 oldflags = ctxt->flags;
8238
8239 for (i = 0; i < ctxt->states->nbState; i++) {
8240 state = ctxt->states->tabState[i];
8241 ctxt->state = state;
8242 ctxt->state->seq = NULL;
8243
8244 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8245 tmp = 0;
8246 break;
8247 }
8248 }
8249 if (tmp != 0) {
8250 /*
8251 * validation error, log the message for the "best" one
8252 */
8253 ctxt->flags |= FLAGS_IGNORABLE;
8254 xmlRelaxNGLogBestError(ctxt);
8255 }
8256 for (i = 0; i < ctxt->states->nbState; i++) {
8257 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8258 }
8259 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8260 ctxt->states = NULL;
8261 if ((ret == 0) && (tmp == -1))
8262 ctxt->pstate = -1;
8263 ctxt->flags = oldflags;
8264 }
8265 if (ctxt->pstate == -1) {
8266 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8267 xmlRelaxNGDumpValidError(ctxt);
8268 }
8269 }
8270 ctxt->state = oldstate;
8271 }
8272
8273 /**
8274 * xmlRelaxNGValidatePushElement:
8275 * @ctxt: the validation context
8276 * @doc: a document instance
8277 * @elem: an element instance
8278 *
8279 * Push a new element start on the RelaxNG validation stack.
8280 *
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.
8283 */
8284 int
xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,xmlDocPtr doc ATTRIBUTE_UNUSED,xmlNodePtr elem)8285 xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8286 xmlDocPtr doc ATTRIBUTE_UNUSED,
8287 xmlNodePtr elem)
8288 {
8289 int ret = 1;
8290
8291 if ((ctxt == NULL) || (elem == NULL))
8292 return (-1);
8293
8294 #ifdef DEBUG_PROGRESSIVE
8295 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8296 #endif
8297 if (ctxt->elem == 0) {
8298 xmlRelaxNGPtr schema;
8299 xmlRelaxNGGrammarPtr grammar;
8300 xmlRegExecCtxtPtr exec;
8301 xmlRelaxNGDefinePtr define;
8302
8303 schema = ctxt->schema;
8304 if (schema == NULL) {
8305 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8306 return (-1);
8307 }
8308 grammar = schema->topgrammar;
8309 if ((grammar == NULL) || (grammar->start == NULL)) {
8310 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8311 return (-1);
8312 }
8313 define = grammar->start;
8314 if (define->contModel == NULL) {
8315 ctxt->pdef = define;
8316 return (0);
8317 }
8318 exec = xmlRegNewExecCtxt(define->contModel,
8319 xmlRelaxNGValidateProgressiveCallback,
8320 ctxt);
8321 if (exec == NULL) {
8322 return (-1);
8323 }
8324 xmlRelaxNGElemPush(ctxt, exec);
8325 }
8326 ctxt->pnode = elem;
8327 ctxt->pstate = 0;
8328 if (elem->ns != NULL) {
8329 ret =
8330 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8331 ctxt);
8332 } else {
8333 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8334 }
8335 if (ret < 0) {
8336 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8337 } else {
8338 if (ctxt->pstate == 0)
8339 ret = 0;
8340 else if (ctxt->pstate < 0)
8341 ret = -1;
8342 else
8343 ret = 1;
8344 }
8345 #ifdef DEBUG_PROGRESSIVE
8346 if (ret < 0)
8347 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8348 elem->name);
8349 #endif
8350 return (ret);
8351 }
8352
8353 /**
8354 * xmlRelaxNGValidatePushCData:
8355 * @ctxt: the RelaxNG validation context
8356 * @data: some character data read
8357 * @len: the length of the data
8358 *
8359 * check the CData parsed for validation in the current stack
8360 *
8361 * returns 1 if no validation problem was found or -1 otherwise
8362 */
8363 int
xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,const xmlChar * data,int len ATTRIBUTE_UNUSED)8364 xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
8365 const xmlChar * data, int len ATTRIBUTE_UNUSED)
8366 {
8367 int ret = 1;
8368
8369 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8370 return (-1);
8371
8372 #ifdef DEBUG_PROGRESSIVE
8373 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8374 #endif
8375
8376 while (*data != 0) {
8377 if (!IS_BLANK_CH(*data))
8378 break;
8379 data++;
8380 }
8381 if (*data == 0)
8382 return (1);
8383
8384 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8385 if (ret < 0) {
8386 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
8387 #ifdef DEBUG_PROGRESSIVE
8388 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
8389 #endif
8390
8391 return (-1);
8392 }
8393 return (1);
8394 }
8395
8396 /**
8397 * xmlRelaxNGValidatePopElement:
8398 * @ctxt: the RelaxNG validation context
8399 * @doc: a document instance
8400 * @elem: an element instance
8401 *
8402 * Pop the element end from the RelaxNG validation stack.
8403 *
8404 * returns 1 if no validation problem was found or 0 otherwise
8405 */
8406 int
xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,xmlDocPtr doc ATTRIBUTE_UNUSED,xmlNodePtr elem)8407 xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8408 xmlDocPtr doc ATTRIBUTE_UNUSED,
8409 xmlNodePtr elem)
8410 {
8411 int ret;
8412 xmlRegExecCtxtPtr exec;
8413
8414 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8415 return (-1);
8416 #ifdef DEBUG_PROGRESSIVE
8417 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8418 #endif
8419 /*
8420 * verify that we reached a terminal state of the content model.
8421 */
8422 exec = xmlRelaxNGElemPop(ctxt);
8423 ret = xmlRegExecPushString(exec, NULL, NULL);
8424 if (ret == 0) {
8425 /*
8426 * TODO: get some of the names needed to exit the current state of exec
8427 */
8428 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8429 ret = -1;
8430 } else if (ret < 0) {
8431 ret = -1;
8432 } else {
8433 ret = 1;
8434 }
8435 xmlRegFreeExecCtxt(exec);
8436 #ifdef DEBUG_PROGRESSIVE
8437 if (ret < 0)
8438 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8439 elem->name);
8440 #endif
8441 return (ret);
8442 }
8443
8444 /**
8445 * xmlRelaxNGValidateFullElement:
8446 * @ctxt: the validation context
8447 * @doc: a document instance
8448 * @elem: an element instance
8449 *
8450 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8451 * 0 and the content of the node has been expanded.
8452 *
8453 * returns 1 if no validation problem was found or -1 in case of error.
8454 */
8455 int
xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,xmlDocPtr doc ATTRIBUTE_UNUSED,xmlNodePtr elem)8456 xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8457 xmlDocPtr doc ATTRIBUTE_UNUSED,
8458 xmlNodePtr elem)
8459 {
8460 int ret;
8461 xmlRelaxNGValidStatePtr state;
8462
8463 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8464 return (-1);
8465 #ifdef DEBUG_PROGRESSIVE
8466 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8467 #endif
8468 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8469 if (state == NULL) {
8470 return (-1);
8471 }
8472 state->seq = elem;
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))
8477 ret = -1;
8478 else
8479 ret = 1;
8480 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
8481 ctxt->state = NULL;
8482 #ifdef DEBUG_PROGRESSIVE
8483 if (ret < 0)
8484 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8485 elem->name);
8486 #endif
8487 return (ret);
8488 }
8489
8490 /************************************************************************
8491 * *
8492 * Generic interpreted validation implementation *
8493 * *
8494 ************************************************************************/
8495 static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8496 xmlRelaxNGDefinePtr define);
8497
8498 /**
8499 * xmlRelaxNGSkipIgnored:
8500 * @ctxt: a schema validation context
8501 * @node: the top node.
8502 *
8503 * Skip ignorable nodes in that context
8504 *
8505 * Returns the new sibling or NULL in case of error.
8506 */
8507 static xmlNodePtr
xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,xmlNodePtr node)8508 xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
8509 xmlNodePtr node)
8510 {
8511 /*
8512 * TODO complete and handle entities
8513 */
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)))))) {
8523 node = node->next;
8524 }
8525 return (node);
8526 }
8527
8528 /**
8529 * xmlRelaxNGNormalize:
8530 * @ctxt: a schema validation context
8531 * @str: the string to normalize
8532 *
8533 * Implements the normalizeWhiteSpace( s ) function from
8534 * section 6.2.9 of the spec
8535 *
8536 * Returns the new string or NULL in case of error.
8537 */
8538 static xmlChar *
xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,const xmlChar * str)8539 xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8540 {
8541 xmlChar *ret, *p;
8542 const xmlChar *tmp;
8543 int len;
8544
8545 if (str == NULL)
8546 return (NULL);
8547 tmp = str;
8548 while (*tmp != 0)
8549 tmp++;
8550 len = tmp - str;
8551
8552 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
8553 if (ret == NULL) {
8554 xmlRngVErrMemory(ctxt, "validating\n");
8555 return (NULL);
8556 }
8557 p = ret;
8558 while (IS_BLANK_CH(*str))
8559 str++;
8560 while (*str != 0) {
8561 if (IS_BLANK_CH(*str)) {
8562 while (IS_BLANK_CH(*str))
8563 str++;
8564 if (*str == 0)
8565 break;
8566 *p++ = ' ';
8567 } else
8568 *p++ = *str++;
8569 }
8570 *p = 0;
8571 return (ret);
8572 }
8573
8574 /**
8575 * xmlRelaxNGValidateDatatype:
8576 * @ctxt: a Relax-NG validation context
8577 * @value: the string value
8578 * @type: the datatype definition
8579 * @node: the node
8580 *
8581 * Validate the given value against the dataype
8582 *
8583 * Returns 0 if the validation succeeded or an error code.
8584 */
8585 static int
xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,const xmlChar * value,xmlRelaxNGDefinePtr define,xmlNodePtr node)8586 xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8587 const xmlChar * value,
8588 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8589 {
8590 int ret, tmp;
8591 xmlRelaxNGTypeLibraryPtr lib;
8592 void *result = NULL;
8593 xmlRelaxNGDefinePtr cur;
8594
8595 if ((define == NULL) || (define->data == NULL)) {
8596 return (-1);
8597 }
8598 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8599 if (lib->check != NULL) {
8600 if ((define->attrs != NULL) &&
8601 (define->attrs->type == XML_RELAXNG_PARAM)) {
8602 ret =
8603 lib->check(lib->data, define->name, value, &result, node);
8604 } else {
8605 ret = lib->check(lib->data, define->name, value, NULL, node);
8606 }
8607 } else
8608 ret = -1;
8609 if (ret < 0) {
8610 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8611 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8612 lib->freef(lib->data, result);
8613 return (-1);
8614 } else if (ret == 1) {
8615 ret = 0;
8616 } else if (ret == 2) {
8617 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
8618 } else {
8619 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8620 ret = -1;
8621 }
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);
8627 if (tmp != 0)
8628 ret = -1;
8629 }
8630 cur = cur->next;
8631 }
8632 if ((ret == 0) && (define->content != NULL)) {
8633 const xmlChar *oldvalue, *oldendvalue;
8634
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;
8642 }
8643 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8644 lib->freef(lib->data, result);
8645 return (ret);
8646 }
8647
8648 /**
8649 * xmlRelaxNGNextValue:
8650 * @ctxt: a Relax-NG validation context
8651 *
8652 * Skip to the next value when validating within a list
8653 *
8654 * Returns 0 if the operation succeeded or an error code.
8655 */
8656 static int
xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)8657 xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8658 {
8659 xmlChar *cur;
8660
8661 cur = ctxt->state->value;
8662 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
8663 ctxt->state->value = NULL;
8664 ctxt->state->endvalue = NULL;
8665 return (0);
8666 }
8667 while (*cur != 0)
8668 cur++;
8669 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8670 cur++;
8671 if (cur == ctxt->state->endvalue)
8672 ctxt->state->value = NULL;
8673 else
8674 ctxt->state->value = cur;
8675 return (0);
8676 }
8677
8678 /**
8679 * xmlRelaxNGValidateValueList:
8680 * @ctxt: a Relax-NG validation context
8681 * @defines: the list of definitions to verify
8682 *
8683 * Validate the given set of definitions for the current value
8684 *
8685 * Returns 0 if the validation succeeded or an error code.
8686 */
8687 static int
xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr defines)8688 xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8689 xmlRelaxNGDefinePtr defines)
8690 {
8691 int ret = 0;
8692
8693 while (defines != NULL) {
8694 ret = xmlRelaxNGValidateValue(ctxt, defines);
8695 if (ret != 0)
8696 break;
8697 defines = defines->next;
8698 }
8699 return (ret);
8700 }
8701
8702 /**
8703 * xmlRelaxNGValidateValue:
8704 * @ctxt: a Relax-NG validation context
8705 * @define: the definition to verify
8706 *
8707 * Validate the given definition for the current value
8708 *
8709 * Returns 0 if the validation succeeded or an error code.
8710 */
8711 static int
xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr define)8712 xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8713 xmlRelaxNGDefinePtr define)
8714 {
8715 int ret = 0, oldflags;
8716 xmlChar *value;
8717
8718 value = ctxt->state->value;
8719 switch (define->type) {
8720 case XML_RELAXNG_EMPTY:{
8721 if ((value != NULL) && (value[0] != 0)) {
8722 int idx = 0;
8723
8724 while (IS_BLANK_CH(value[idx]))
8725 idx++;
8726 if (value[idx] != 0)
8727 ret = -1;
8728 }
8729 break;
8730 }
8731 case XML_RELAXNG_TEXT:
8732 break;
8733 case XML_RELAXNG_VALUE:{
8734 if (!xmlStrEqual(value, define->value)) {
8735 if (define->name != NULL) {
8736 xmlRelaxNGTypeLibraryPtr lib;
8737
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);
8744 } else
8745 ret = -1;
8746 if (ret < 0) {
8747 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8748 define->name);
8749 return (-1);
8750 } else if (ret == 1) {
8751 ret = 0;
8752 } else {
8753 ret = -1;
8754 }
8755 } else {
8756 xmlChar *nval, *nvalue;
8757
8758 /*
8759 * TODO: trivial optimizations are possible by
8760 * computing at compile-time
8761 */
8762 nval = xmlRelaxNGNormalize(ctxt, define->value);
8763 nvalue = xmlRelaxNGNormalize(ctxt, value);
8764
8765 if ((nval == NULL) || (nvalue == NULL) ||
8766 (!xmlStrEqual(nval, nvalue)))
8767 ret = -1;
8768 if (nval != NULL)
8769 xmlFree(nval);
8770 if (nvalue != NULL)
8771 xmlFree(nvalue);
8772 }
8773 }
8774 if (ret == 0)
8775 xmlRelaxNGNextValue(ctxt);
8776 break;
8777 }
8778 case XML_RELAXNG_DATATYPE:{
8779 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8780 ctxt->state->seq);
8781 if (ret == 0)
8782 xmlRelaxNGNextValue(ctxt);
8783
8784 break;
8785 }
8786 case XML_RELAXNG_CHOICE:{
8787 xmlRelaxNGDefinePtr list = define->content;
8788 xmlChar *oldvalue;
8789
8790 oldflags = ctxt->flags;
8791 ctxt->flags |= FLAGS_IGNORABLE;
8792
8793 oldvalue = ctxt->state->value;
8794 while (list != NULL) {
8795 ret = xmlRelaxNGValidateValue(ctxt, list);
8796 if (ret == 0) {
8797 break;
8798 }
8799 ctxt->state->value = oldvalue;
8800 list = list->next;
8801 }
8802 ctxt->flags = oldflags;
8803 if (ret != 0) {
8804 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8805 xmlRelaxNGDumpValidError(ctxt);
8806 } else {
8807 if (ctxt->errNr > 0)
8808 xmlRelaxNGPopErrors(ctxt, 0);
8809 }
8810 break;
8811 }
8812 case XML_RELAXNG_LIST:{
8813 xmlRelaxNGDefinePtr list = define->content;
8814 xmlChar *oldvalue, *oldend, *val, *cur;
8815
8816 #ifdef DEBUG_LIST
8817 int nb_values = 0;
8818 #endif
8819
8820 oldvalue = ctxt->state->value;
8821 oldend = ctxt->state->endvalue;
8822
8823 val = xmlStrdup(oldvalue);
8824 if (val == NULL) {
8825 val = xmlStrdup(BAD_CAST "");
8826 }
8827 if (val == NULL) {
8828 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8829 return (-1);
8830 }
8831 cur = val;
8832 while (*cur != 0) {
8833 if (IS_BLANK_CH(*cur)) {
8834 *cur = 0;
8835 cur++;
8836 #ifdef DEBUG_LIST
8837 nb_values++;
8838 #endif
8839 while (IS_BLANK_CH(*cur))
8840 *cur++ = 0;
8841 } else
8842 cur++;
8843 }
8844 #ifdef DEBUG_LIST
8845 xmlGenericError(xmlGenericErrorContext,
8846 "list value: '%s' found %d items\n",
8847 oldvalue, nb_values);
8848 nb_values = 0;
8849 #endif
8850 ctxt->state->endvalue = cur;
8851 cur = val;
8852 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8853 cur++;
8854
8855 ctxt->state->value = cur;
8856
8857 while (list != NULL) {
8858 if (ctxt->state->value == ctxt->state->endvalue)
8859 ctxt->state->value = NULL;
8860 ret = xmlRelaxNGValidateValue(ctxt, list);
8861 if (ret != 0) {
8862 #ifdef DEBUG_LIST
8863 xmlGenericError(xmlGenericErrorContext,
8864 "Failed to validate value: '%s' with %d rule\n",
8865 ctxt->state->value, nb_values);
8866 #endif
8867 break;
8868 }
8869 #ifdef DEBUG_LIST
8870 nb_values++;
8871 #endif
8872 list = list->next;
8873 }
8874
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);
8879 ret = -1;
8880 }
8881 xmlFree(val);
8882 ctxt->state->value = oldvalue;
8883 ctxt->state->endvalue = oldend;
8884 break;
8885 }
8886 case XML_RELAXNG_ONEORMORE:
8887 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8888 if (ret != 0) {
8889 break;
8890 }
8891 /* no break on purpose */
8892 case XML_RELAXNG_ZEROORMORE:{
8893 xmlChar *cur, *temp;
8894
8895 if ((ctxt->state->value == NULL) ||
8896 (*ctxt->state->value == 0)) {
8897 ret = 0;
8898 break;
8899 }
8900 oldflags = ctxt->flags;
8901 ctxt->flags |= FLAGS_IGNORABLE;
8902 cur = ctxt->state->value;
8903 temp = NULL;
8904 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8905 (temp != cur)) {
8906 temp = cur;
8907 ret =
8908 xmlRelaxNGValidateValueList(ctxt, define->content);
8909 if (ret != 0) {
8910 ctxt->state->value = temp;
8911 ret = 0;
8912 break;
8913 }
8914 cur = ctxt->state->value;
8915 }
8916 ctxt->flags = oldflags;
8917 if (ctxt->errNr > 0)
8918 xmlRelaxNGPopErrors(ctxt, 0);
8919 break;
8920 }
8921 case XML_RELAXNG_OPTIONAL:{
8922 xmlChar *temp;
8923
8924 if ((ctxt->state->value == NULL) ||
8925 (*ctxt->state->value == 0)) {
8926 ret = 0;
8927 break;
8928 }
8929 oldflags = ctxt->flags;
8930 ctxt->flags |= FLAGS_IGNORABLE;
8931 temp = ctxt->state->value;
8932 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8933 ctxt->flags = oldflags;
8934 if (ret != 0) {
8935 ctxt->state->value = temp;
8936 if (ctxt->errNr > 0)
8937 xmlRelaxNGPopErrors(ctxt, 0);
8938 ret = 0;
8939 break;
8940 }
8941 if (ctxt->errNr > 0)
8942 xmlRelaxNGPopErrors(ctxt, 0);
8943 break;
8944 }
8945 case XML_RELAXNG_EXCEPT:{
8946 xmlRelaxNGDefinePtr list;
8947
8948 list = define->content;
8949 while (list != NULL) {
8950 ret = xmlRelaxNGValidateValue(ctxt, list);
8951 if (ret == 0) {
8952 ret = -1;
8953 break;
8954 } else
8955 ret = 0;
8956 list = list->next;
8957 }
8958 break;
8959 }
8960 case XML_RELAXNG_DEF:
8961 case XML_RELAXNG_GROUP:{
8962 xmlRelaxNGDefinePtr list;
8963
8964 list = define->content;
8965 while (list != NULL) {
8966 ret = xmlRelaxNGValidateValue(ctxt, list);
8967 if (ret != 0) {
8968 ret = -1;
8969 break;
8970 } else
8971 ret = 0;
8972 list = list->next;
8973 }
8974 break;
8975 }
8976 case XML_RELAXNG_REF:
8977 case XML_RELAXNG_PARENTREF:
8978 if (define->content == NULL) {
8979 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8980 ret = -1;
8981 } else {
8982 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8983 }
8984 break;
8985 default:
8986 TODO ret = -1;
8987 }
8988 return (ret);
8989 }
8990
8991 /**
8992 * xmlRelaxNGValidateValueContent:
8993 * @ctxt: a Relax-NG validation context
8994 * @defines: the list of definitions to verify
8995 *
8996 * Validate the given definitions for the current value
8997 *
8998 * Returns 0 if the validation succeeded or an error code.
8999 */
9000 static int
xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr defines)9001 xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
9002 xmlRelaxNGDefinePtr defines)
9003 {
9004 int ret = 0;
9005
9006 while (defines != NULL) {
9007 ret = xmlRelaxNGValidateValue(ctxt, defines);
9008 if (ret != 0)
9009 break;
9010 defines = defines->next;
9011 }
9012 return (ret);
9013 }
9014
9015 /**
9016 * xmlRelaxNGAttributeMatch:
9017 * @ctxt: a Relax-NG validation context
9018 * @define: the definition to check
9019 * @prop: the attribute
9020 *
9021 * Check if the attribute matches the definition nameClass
9022 *
9023 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
9024 */
9025 static int
xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr define,xmlAttrPtr prop)9026 xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
9027 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
9028 {
9029 int ret;
9030
9031 if (define->name != NULL) {
9032 if (!xmlStrEqual(define->name, prop->name))
9033 return (0);
9034 }
9035 if (define->ns != NULL) {
9036 if (define->ns[0] == 0) {
9037 if (prop->ns != NULL)
9038 return (0);
9039 } else {
9040 if ((prop->ns == NULL) ||
9041 (!xmlStrEqual(define->ns, prop->ns->href)))
9042 return (0);
9043 }
9044 }
9045 if (define->nameClass == NULL)
9046 return (1);
9047 define = define->nameClass;
9048 if (define->type == XML_RELAXNG_EXCEPT) {
9049 xmlRelaxNGDefinePtr list;
9050
9051 list = define->content;
9052 while (list != NULL) {
9053 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9054 if (ret == 1)
9055 return (0);
9056 if (ret < 0)
9057 return (ret);
9058 list = list->next;
9059 }
9060 } else if (define->type == XML_RELAXNG_CHOICE) {
9061 xmlRelaxNGDefinePtr list;
9062
9063 list = define->nameClass;
9064 while (list != NULL) {
9065 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9066 if (ret == 1)
9067 return (1);
9068 if (ret < 0)
9069 return (ret);
9070 list = list->next;
9071 }
9072 return (0);
9073 } else {
9074 TODO}
9075 return (1);
9076 }
9077
9078 /**
9079 * xmlRelaxNGValidateAttribute:
9080 * @ctxt: a Relax-NG validation context
9081 * @define: the definition to verify
9082 *
9083 * Validate the given attribute definition for that node
9084 *
9085 * Returns 0 if the validation succeeded or an error code.
9086 */
9087 static int
xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr define)9088 xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9089 xmlRelaxNGDefinePtr define)
9090 {
9091 int ret = 0, i;
9092 xmlChar *value, *oldvalue;
9093 xmlAttrPtr prop = NULL, tmp;
9094 xmlNodePtr oldseq;
9095
9096 if (ctxt->state->nbAttrLeft <= 0)
9097 return (-1);
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)))) {
9106 prop = tmp;
9107 break;
9108 }
9109 }
9110 }
9111 if (prop != NULL) {
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;
9121 if (value != NULL)
9122 xmlFree(value);
9123 ctxt->state->value = oldvalue;
9124 ctxt->state->seq = oldseq;
9125 if (ret == 0) {
9126 /*
9127 * flag the attribute as processed
9128 */
9129 ctxt->state->attrs[i] = NULL;
9130 ctxt->state->nbAttrLeft--;
9131 }
9132 } else {
9133 ret = -1;
9134 }
9135 #ifdef DEBUG
9136 xmlGenericError(xmlGenericErrorContext,
9137 "xmlRelaxNGValidateAttribute(%s): %d\n",
9138 define->name, ret);
9139 #endif
9140 } else {
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)) {
9145 prop = tmp;
9146 break;
9147 }
9148 }
9149 if (prop != NULL) {
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;
9158 if (value != NULL)
9159 xmlFree(value);
9160 ctxt->state->value = oldvalue;
9161 ctxt->state->seq = oldseq;
9162 if (ret == 0) {
9163 /*
9164 * flag the attribute as processed
9165 */
9166 ctxt->state->attrs[i] = NULL;
9167 ctxt->state->nbAttrLeft--;
9168 }
9169 } else {
9170 ret = -1;
9171 }
9172 #ifdef DEBUG
9173 if (define->ns != NULL) {
9174 xmlGenericError(xmlGenericErrorContext,
9175 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9176 define->ns, ret);
9177 } else {
9178 xmlGenericError(xmlGenericErrorContext,
9179 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9180 ret);
9181 }
9182 #endif
9183 }
9184
9185 return (ret);
9186 }
9187
9188 /**
9189 * xmlRelaxNGValidateAttributeList:
9190 * @ctxt: a Relax-NG validation context
9191 * @define: the list of definition to verify
9192 *
9193 * Validate the given node against the list of attribute definitions
9194 *
9195 * Returns 0 if the validation succeeded or an error code.
9196 */
9197 static int
xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr defines)9198 xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9199 xmlRelaxNGDefinePtr defines)
9200 {
9201 int ret = 0, res;
9202 int needmore = 0;
9203 xmlRelaxNGDefinePtr cur;
9204
9205 cur = defines;
9206 while (cur != NULL) {
9207 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
9208 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9209 ret = -1;
9210 } else
9211 needmore = 1;
9212 cur = cur->next;
9213 }
9214 if (!needmore)
9215 return (ret);
9216 cur = defines;
9217 while (cur != NULL) {
9218 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
9219 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9220 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9221 if (res < 0)
9222 ret = -1;
9223 } else {
9224 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9225 return (-1);
9226 }
9227 if (res == -1) /* continues on -2 */
9228 break;
9229 }
9230 cur = cur->next;
9231 }
9232
9233 return (ret);
9234 }
9235
9236 /**
9237 * xmlRelaxNGNodeMatchesList:
9238 * @node: the node
9239 * @list: a NULL terminated array of definitions
9240 *
9241 * Check if a node can be matched by one of the definitions
9242 *
9243 * Returns 1 if matches 0 otherwise
9244 */
9245 static int
xmlRelaxNGNodeMatchesList(xmlNodePtr node,xmlRelaxNGDefinePtr * list)9246 xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9247 {
9248 xmlRelaxNGDefinePtr cur;
9249 int i = 0, tmp;
9250
9251 if ((node == NULL) || (list == NULL))
9252 return (0);
9253
9254 cur = list[i++];
9255 while (cur != NULL) {
9256 if ((node->type == XML_ELEMENT_NODE) &&
9257 (cur->type == XML_RELAXNG_ELEMENT)) {
9258 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9259 if (tmp == 1)
9260 return (1);
9261 } else if (((node->type == XML_TEXT_NODE) ||
9262 (node->type == XML_CDATA_SECTION_NODE)) &&
9263 (cur->type == XML_RELAXNG_TEXT)) {
9264 return (1);
9265 }
9266 cur = list[i++];
9267 }
9268 return (0);
9269 }
9270
9271 /**
9272 * xmlRelaxNGValidateInterleave:
9273 * @ctxt: a Relax-NG validation context
9274 * @define: the definition to verify
9275 *
9276 * Validate an interleave definition for a node.
9277 *
9278 * Returns 0 if the validation succeeded or an error code.
9279 */
9280 static int
xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr define)9281 xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9282 xmlRelaxNGDefinePtr define)
9283 {
9284 int ret = 0, i, nbgroups;
9285 int errNr = ctxt->errNr;
9286 int oldflags;
9287
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;
9293
9294 if (define->data != NULL) {
9295 partitions = (xmlRelaxNGPartitionPtr) define->data;
9296 nbgroups = partitions->nbgroups;
9297 } else {
9298 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9299 return (-1);
9300 }
9301 /*
9302 * Optimizations for MIXED
9303 */
9304 oldflags = ctxt->flags;
9305 if (define->dflags & IS_MIXED) {
9306 ctxt->flags |= FLAGS_MIXED_CONTENT;
9307 if (nbgroups == 2) {
9308 /*
9309 * this is a pure <mixed> case
9310 */
9311 if (ctxt->state != NULL)
9312 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9313 ctxt->state->seq);
9314 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9315 ret = xmlRelaxNGValidateDefinition(ctxt,
9316 partitions->groups[1]->
9317 rule);
9318 else
9319 ret = xmlRelaxNGValidateDefinition(ctxt,
9320 partitions->groups[0]->
9321 rule);
9322 if (ret == 0) {
9323 if (ctxt->state != NULL)
9324 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9325 ctxt->state->
9326 seq);
9327 }
9328 ctxt->flags = oldflags;
9329 return (ret);
9330 }
9331 }
9332
9333 /*
9334 * Build arrays to store the first and last node of the chain
9335 * pertaining to each group
9336 */
9337 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9338 if (list == NULL) {
9339 xmlRngVErrMemory(ctxt, "validating\n");
9340 return (-1);
9341 }
9342 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9343 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9344 if (lasts == NULL) {
9345 xmlRngVErrMemory(ctxt, "validating\n");
9346 return (-1);
9347 }
9348 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9349
9350 /*
9351 * Walk the sequence of children finding the right group and
9352 * sorting them in sequences.
9353 */
9354 cur = ctxt->state->seq;
9355 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9356 start = cur;
9357 while (cur != NULL) {
9358 ctxt->state->seq = cur;
9359 if ((partitions->triage != NULL) &&
9360 (partitions->flags & IS_DETERMINIST)) {
9361 void *tmp = NULL;
9362
9363 if ((cur->type == XML_TEXT_NODE) ||
9364 (cur->type == XML_CDATA_SECTION_NODE)) {
9365 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9366 NULL);
9367 } else if (cur->type == XML_ELEMENT_NODE) {
9368 if (cur->ns != NULL) {
9369 tmp = xmlHashLookup2(partitions->triage, cur->name,
9370 cur->ns->href);
9371 if (tmp == NULL)
9372 tmp = xmlHashLookup2(partitions->triage,
9373 BAD_CAST "#any",
9374 cur->ns->href);
9375 } else
9376 tmp =
9377 xmlHashLookup2(partitions->triage, cur->name,
9378 NULL);
9379 if (tmp == NULL)
9380 tmp =
9381 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9382 NULL);
9383 }
9384
9385 if (tmp == NULL) {
9386 i = nbgroups;
9387 } else {
9388 i = ((long) tmp) - 1;
9389 if (partitions->flags & IS_NEEDCHECK) {
9390 group = partitions->groups[i];
9391 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9392 i = nbgroups;
9393 }
9394 }
9395 } else {
9396 for (i = 0; i < nbgroups; i++) {
9397 group = partitions->groups[i];
9398 if (group == NULL)
9399 continue;
9400 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9401 break;
9402 }
9403 }
9404 /*
9405 * We break as soon as an element not matched is found
9406 */
9407 if (i >= nbgroups) {
9408 break;
9409 }
9410 if (lasts[i] != NULL) {
9411 lasts[i]->next = cur;
9412 lasts[i] = cur;
9413 } else {
9414 list[i] = cur;
9415 lasts[i] = cur;
9416 }
9417 if (cur->next != NULL)
9418 lastchg = cur->next;
9419 else
9420 lastchg = cur;
9421 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
9422 }
9423 if (ret != 0) {
9424 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9425 ret = -1;
9426 goto done;
9427 }
9428 lastelem = cur;
9429 oldstate = ctxt->state;
9430 for (i = 0; i < nbgroups; i++) {
9431 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9432 if (ctxt->state == NULL) {
9433 ret = -1;
9434 break;
9435 }
9436 group = partitions->groups[i];
9437 if (lasts[i] != NULL) {
9438 last = lasts[i]->next;
9439 lasts[i]->next = NULL;
9440 }
9441 ctxt->state->seq = list[i];
9442 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9443 if (ret != 0)
9444 break;
9445 if (ctxt->state != NULL) {
9446 cur = ctxt->state->seq;
9447 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9448 xmlRelaxNGFreeValidState(ctxt, oldstate);
9449 oldstate = ctxt->state;
9450 ctxt->state = NULL;
9451 if (cur != NULL) {
9452 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9453 ret = -1;
9454 ctxt->state = oldstate;
9455 goto done;
9456 }
9457 } else if (ctxt->states != NULL) {
9458 int j;
9459 int found = 0;
9460 int best = -1;
9461 int lowattr = -1;
9462
9463 /*
9464 * PBM: what happen if there is attributes checks in the interleaves
9465 */
9466
9467 for (j = 0; j < ctxt->states->nbState; j++) {
9468 cur = ctxt->states->tabState[j]->seq;
9469 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9470 if (cur == NULL) {
9471 if (found == 0) {
9472 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9473 best = j;
9474 }
9475 found = 1;
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;
9479 best = j;
9480 }
9481 if (lowattr == 0)
9482 break;
9483 } else if (found == 0) {
9484 if (lowattr == -1) {
9485 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9486 best = j;
9487 } else
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;
9491 best = j;
9492 }
9493 }
9494 }
9495 /*
9496 * BIG PBM: here we pick only one restarting point :-(
9497 */
9498 if (ctxt->states->nbState > 0) {
9499 xmlRelaxNGFreeValidState(ctxt, oldstate);
9500 if (best != -1) {
9501 oldstate = ctxt->states->tabState[best];
9502 ctxt->states->tabState[best] = NULL;
9503 } else {
9504 oldstate =
9505 ctxt->states->tabState[ctxt->states->nbState - 1];
9506 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
9507 ctxt->states->nbState--;
9508 }
9509 }
9510 for (j = 0; j < ctxt->states->nbState ; j++) {
9511 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9512 }
9513 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9514 ctxt->states = NULL;
9515 if (found == 0) {
9516 if (cur == NULL) {
9517 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA,
9518 (const xmlChar *) "noname");
9519 } else {
9520 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9521 }
9522 ret = -1;
9523 ctxt->state = oldstate;
9524 goto done;
9525 }
9526 } else {
9527 ret = -1;
9528 break;
9529 }
9530 if (lasts[i] != NULL) {
9531 lasts[i]->next = last;
9532 }
9533 }
9534 if (ctxt->state != NULL)
9535 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9536 ctxt->state = oldstate;
9537 ctxt->state->seq = lastelem;
9538 if (ret != 0) {
9539 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9540 ret = -1;
9541 goto done;
9542 }
9543
9544 done:
9545 ctxt->flags = oldflags;
9546 /*
9547 * builds the next links chain from the prev one
9548 */
9549 cur = lastchg;
9550 while (cur != NULL) {
9551 if ((cur == start) || (cur->prev == NULL))
9552 break;
9553 cur->prev->next = cur;
9554 cur = cur->prev;
9555 }
9556 if (ret == 0) {
9557 if (ctxt->errNr > errNr)
9558 xmlRelaxNGPopErrors(ctxt, errNr);
9559 }
9560
9561 xmlFree(list);
9562 xmlFree(lasts);
9563 return (ret);
9564 }
9565
9566 /**
9567 * xmlRelaxNGValidateDefinitionList:
9568 * @ctxt: a Relax-NG validation context
9569 * @define: the list of definition to verify
9570 *
9571 * Validate the given node content against the (list) of definitions
9572 *
9573 * Returns 0 if the validation succeeded or an error code.
9574 */
9575 static int
xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr defines)9576 xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9577 xmlRelaxNGDefinePtr defines)
9578 {
9579 int ret = 0, res;
9580
9581
9582 if (defines == NULL) {
9583 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9584 BAD_CAST "NULL definition list");
9585 return (-1);
9586 }
9587 while (defines != NULL) {
9588 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9589 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9590 if (res < 0)
9591 ret = -1;
9592 } else {
9593 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9594 return (-1);
9595 }
9596 if (res == -1) /* continues on -2 */
9597 break;
9598 defines = defines->next;
9599 }
9600
9601 return (ret);
9602 }
9603
9604 /**
9605 * xmlRelaxNGElementMatch:
9606 * @ctxt: a Relax-NG validation context
9607 * @define: the definition to check
9608 * @elem: the element
9609 *
9610 * Check if the element matches the definition nameClass
9611 *
9612 * Returns 1 if the element matches, 0 if no, or -1 in case of error
9613 */
9614 static int
xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr define,xmlNodePtr elem)9615 xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9616 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9617 {
9618 int ret = 0, oldflags = 0;
9619
9620 if (define->name != NULL) {
9621 if (!xmlStrEqual(elem->name, define->name)) {
9622 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9623 return (0);
9624 }
9625 }
9626 if ((define->ns != NULL) && (define->ns[0] != 0)) {
9627 if (elem->ns == NULL) {
9628 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9629 return (0);
9630 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9631 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9632 elem->name, define->ns);
9633 return (0);
9634 }
9635 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
9636 (define->name == NULL)) {
9637 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9638 return (0);
9639 } else if ((elem->ns != NULL) && (define->name != NULL)) {
9640 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9641 return (0);
9642 }
9643
9644 if (define->nameClass == NULL)
9645 return (1);
9646
9647 define = define->nameClass;
9648 if (define->type == XML_RELAXNG_EXCEPT) {
9649 xmlRelaxNGDefinePtr list;
9650
9651 if (ctxt != NULL) {
9652 oldflags = ctxt->flags;
9653 ctxt->flags |= FLAGS_IGNORABLE;
9654 }
9655
9656 list = define->content;
9657 while (list != NULL) {
9658 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9659 if (ret == 1) {
9660 if (ctxt != NULL)
9661 ctxt->flags = oldflags;
9662 return (0);
9663 }
9664 if (ret < 0) {
9665 if (ctxt != NULL)
9666 ctxt->flags = oldflags;
9667 return (ret);
9668 }
9669 list = list->next;
9670 }
9671 ret = 1;
9672 if (ctxt != NULL) {
9673 ctxt->flags = oldflags;
9674 }
9675 } else if (define->type == XML_RELAXNG_CHOICE) {
9676 xmlRelaxNGDefinePtr list;
9677
9678 if (ctxt != NULL) {
9679 oldflags = ctxt->flags;
9680 ctxt->flags |= FLAGS_IGNORABLE;
9681 }
9682
9683 list = define->nameClass;
9684 while (list != NULL) {
9685 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9686 if (ret == 1) {
9687 if (ctxt != NULL)
9688 ctxt->flags = oldflags;
9689 return (1);
9690 }
9691 if (ret < 0) {
9692 if (ctxt != NULL)
9693 ctxt->flags = oldflags;
9694 return (ret);
9695 }
9696 list = list->next;
9697 }
9698 if (ctxt != NULL) {
9699 if (ret != 0) {
9700 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9701 xmlRelaxNGDumpValidError(ctxt);
9702 } else {
9703 if (ctxt->errNr > 0)
9704 xmlRelaxNGPopErrors(ctxt, 0);
9705 }
9706 }
9707 ret = 0;
9708 if (ctxt != NULL) {
9709 ctxt->flags = oldflags;
9710 }
9711 } else {
9712 TODO ret = -1;
9713 }
9714 return (ret);
9715 }
9716
9717 /**
9718 * xmlRelaxNGBestState:
9719 * @ctxt: a Relax-NG validation context
9720 *
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
9725 *
9726 * Returns the index of the "best" state or -1 in case of error
9727 */
9728 static int
xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)9729 xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9730 {
9731 xmlRelaxNGValidStatePtr state;
9732 int i, tmp;
9733 int best = -1;
9734 int value = 1000000;
9735
9736 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9737 (ctxt->states->nbState <= 0))
9738 return (-1);
9739
9740 for (i = 0; i < ctxt->states->nbState; i++) {
9741 state = ctxt->states->tabState[i];
9742 if (state == NULL)
9743 continue;
9744 if (state->seq != NULL) {
9745 if ((best == -1) || (value > 100000)) {
9746 value = 100000;
9747 best = i;
9748 }
9749 } else {
9750 tmp = state->nbAttrLeft;
9751 if ((best == -1) || (value > tmp)) {
9752 value = tmp;
9753 best = i;
9754 }
9755 }
9756 }
9757 return (best);
9758 }
9759
9760 /**
9761 * xmlRelaxNGLogBestError:
9762 * @ctxt: a Relax-NG validation context
9763 *
9764 * Find the "best" state in the ctxt->states list of states to report
9765 * errors about and log it.
9766 */
9767 static void
xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)9768 xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9769 {
9770 int best;
9771
9772 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9773 (ctxt->states->nbState <= 0))
9774 return;
9775
9776 best = xmlRelaxNGBestState(ctxt);
9777 if ((best >= 0) && (best < ctxt->states->nbState)) {
9778 ctxt->state = ctxt->states->tabState[best];
9779
9780 xmlRelaxNGValidateElementEnd(ctxt, 1);
9781 }
9782 }
9783
9784 /**
9785 * xmlRelaxNGValidateElementEnd:
9786 * @ctxt: a Relax-NG validation context
9787 * @dolog: indicate that error logging should be done
9788 *
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.
9792 *
9793 * Returns 0 if the validation succeeded or an error code.
9794 */
9795 static int
xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,int dolog)9796 xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
9797 {
9798 int i;
9799 xmlRelaxNGValidStatePtr state;
9800
9801 state = ctxt->state;
9802 if (state->seq != NULL) {
9803 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9804 if (state->seq != NULL) {
9805 if (dolog) {
9806 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9807 state->node->name, state->seq->name);
9808 }
9809 return (-1);
9810 }
9811 }
9812 for (i = 0; i < state->nbAttrs; i++) {
9813 if (state->attrs[i] != NULL) {
9814 if (dolog) {
9815 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9816 state->attrs[i]->name, state->node->name);
9817 }
9818 return (-1 - i);
9819 }
9820 }
9821 return (0);
9822 }
9823
9824 /**
9825 * xmlRelaxNGValidateState:
9826 * @ctxt: a Relax-NG validation context
9827 * @define: the definition to verify
9828 *
9829 * Validate the current state against the definition
9830 *
9831 * Returns 0 if the validation succeeded or an error code.
9832 */
9833 static int
xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr define)9834 xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9835 xmlRelaxNGDefinePtr define)
9836 {
9837 xmlNodePtr node;
9838 int ret = 0, i, tmp, oldflags, errNr;
9839 xmlRelaxNGValidStatePtr oldstate = NULL, state;
9840
9841 if (define == NULL) {
9842 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9843 return (-1);
9844 }
9845
9846 if (ctxt->state != NULL) {
9847 node = ctxt->state->seq;
9848 } else {
9849 node = NULL;
9850 }
9851 #ifdef DEBUG
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);
9860 else
9861 xmlGenericError(xmlGenericErrorContext, "\n");
9862 #endif
9863 ctxt->depth++;
9864 switch (define->type) {
9865 case XML_RELAXNG_EMPTY:
9866 xmlRelaxNGSkipIgnored(ctxt, node);
9867 ret = 0;
9868 break;
9869 case XML_RELAXNG_NOT_ALLOWED:
9870 ret = -1;
9871 break;
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)))
9878 node = node->next;
9879 ctxt->state->seq = node;
9880 break;
9881 case XML_RELAXNG_ELEMENT:
9882 errNr = ctxt->errNr;
9883 node = xmlRelaxNGSkipIgnored(ctxt, node);
9884 if (node == NULL) {
9885 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9886 ret = -1;
9887 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9888 xmlRelaxNGDumpValidError(ctxt);
9889 break;
9890 }
9891 if (node->type != XML_ELEMENT_NODE) {
9892 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9893 ret = -1;
9894 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9895 xmlRelaxNGDumpValidError(ctxt);
9896 break;
9897 }
9898 /*
9899 * This node was already validated successfully against
9900 * this definition.
9901 */
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)))
9910 ||
9911 ((ctxt->err->err ==
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);
9918 }
9919 break;
9920 }
9921
9922 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9923 if (ret <= 0) {
9924 ret = -1;
9925 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9926 xmlRelaxNGDumpValidError(ctxt);
9927 break;
9928 }
9929 ret = 0;
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);
9941 }
9942 errNr = ctxt->errNr;
9943
9944 oldflags = ctxt->flags;
9945 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9946 ctxt->flags -= FLAGS_MIXED_CONTENT;
9947 }
9948 state = xmlRelaxNGNewValidState(ctxt, node);
9949 if (state == NULL) {
9950 ret = -1;
9951 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9952 xmlRelaxNGDumpValidError(ctxt);
9953 break;
9954 }
9955
9956 oldstate = ctxt->state;
9957 ctxt->state = state;
9958 if (define->attrs != NULL) {
9959 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9960 if (tmp != 0) {
9961 ret = -1;
9962 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9963 }
9964 }
9965 if (define->contModel != NULL) {
9966 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9967 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9968 xmlNodePtr nseq;
9969
9970 nstate = xmlRelaxNGNewValidState(ctxt, node);
9971 ctxt->state = nstate;
9972 ctxt->states = NULL;
9973
9974 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9975 define->contModel,
9976 ctxt->state->seq);
9977 nseq = ctxt->state->seq;
9978 ctxt->state = tmpstate;
9979 ctxt->states = tmpstates;
9980 xmlRelaxNGFreeValidState(ctxt, nstate);
9981
9982 #ifdef DEBUG_COMPILE
9983 xmlGenericError(xmlGenericErrorContext,
9984 "Validating content of '%s' : %d\n",
9985 define->name, tmp);
9986 #endif
9987 if (tmp != 0)
9988 ret = -1;
9989
9990 if (ctxt->states != NULL) {
9991 tmp = -1;
9992
9993 for (i = 0; i < ctxt->states->nbState; i++) {
9994 state = ctxt->states->tabState[i];
9995 ctxt->state = state;
9996 ctxt->state->seq = nseq;
9997
9998 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
9999 tmp = 0;
10000 break;
10001 }
10002 }
10003 if (tmp != 0) {
10004 /*
10005 * validation error, log the message for the "best" one
10006 */
10007 ctxt->flags |= FLAGS_IGNORABLE;
10008 xmlRelaxNGLogBestError(ctxt);
10009 }
10010 for (i = 0; i < ctxt->states->nbState; i++) {
10011 xmlRelaxNGFreeValidState(ctxt,
10012 ctxt->states->
10013 tabState[i]);
10014 }
10015 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10016 ctxt->flags = oldflags;
10017 ctxt->states = NULL;
10018 if ((ret == 0) && (tmp == -1))
10019 ret = -1;
10020 } else {
10021 state = ctxt->state;
10022 if (ctxt->state != NULL)
10023 ctxt->state->seq = nseq;
10024 if (ret == 0)
10025 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
10026 xmlRelaxNGFreeValidState(ctxt, state);
10027 }
10028 } else {
10029 if (define->content != NULL) {
10030 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
10031 define->
10032 content);
10033 if (tmp != 0) {
10034 ret = -1;
10035 if (ctxt->state == NULL) {
10036 ctxt->state = oldstate;
10037 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10038 node->name);
10039 ctxt->state = NULL;
10040 } else {
10041 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10042 node->name);
10043 }
10044
10045 }
10046 }
10047 if (ctxt->states != NULL) {
10048 tmp = -1;
10049
10050 for (i = 0; i < ctxt->states->nbState; i++) {
10051 state = ctxt->states->tabState[i];
10052 ctxt->state = state;
10053
10054 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
10055 tmp = 0;
10056 break;
10057 }
10058 }
10059 if (tmp != 0) {
10060 /*
10061 * validation error, log the message for the "best" one
10062 */
10063 ctxt->flags |= FLAGS_IGNORABLE;
10064 xmlRelaxNGLogBestError(ctxt);
10065 }
10066 for (i = 0; i < ctxt->states->nbState; i++) {
10067 xmlRelaxNGFreeValidState(ctxt,
10068 ctxt->states->tabState[i]);
10069 ctxt->states->tabState[i] = NULL;
10070 }
10071 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10072 ctxt->flags = oldflags;
10073 ctxt->states = NULL;
10074 if ((ret == 0) && (tmp == -1))
10075 ret = -1;
10076 } else {
10077 state = ctxt->state;
10078 if (ret == 0)
10079 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
10080 xmlRelaxNGFreeValidState(ctxt, state);
10081 }
10082 }
10083 if (ret == 0) {
10084 node->psvi = define;
10085 }
10086 ctxt->flags = oldflags;
10087 ctxt->state = oldstate;
10088 if (oldstate != NULL)
10089 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
10090 if (ret != 0) {
10091 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10092 xmlRelaxNGDumpValidError(ctxt);
10093 ret = 0;
10094 #if 0
10095 } else {
10096 ret = -2;
10097 #endif
10098 }
10099 } else {
10100 if (ctxt->errNr > errNr)
10101 xmlRelaxNGPopErrors(ctxt, errNr);
10102 }
10103
10104 #ifdef DEBUG
10105 xmlGenericError(xmlGenericErrorContext,
10106 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10107 node->name, ret);
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);
10115 else
10116 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10117 oldstate->seq->name, oldstate->seq->type);
10118 #endif
10119 break;
10120 case XML_RELAXNG_OPTIONAL:{
10121 errNr = ctxt->errNr;
10122 oldflags = ctxt->flags;
10123 ctxt->flags |= FLAGS_IGNORABLE;
10124 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10125 ret =
10126 xmlRelaxNGValidateDefinitionList(ctxt,
10127 define->content);
10128 if (ret != 0) {
10129 if (ctxt->state != NULL)
10130 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10131 ctxt->state = oldstate;
10132 ctxt->flags = oldflags;
10133 ret = 0;
10134 if (ctxt->errNr > errNr)
10135 xmlRelaxNGPopErrors(ctxt, errNr);
10136 break;
10137 }
10138 if (ctxt->states != NULL) {
10139 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10140 } else {
10141 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10142 if (ctxt->states == NULL) {
10143 xmlRelaxNGFreeValidState(ctxt, oldstate);
10144 ctxt->flags = oldflags;
10145 ret = -1;
10146 if (ctxt->errNr > errNr)
10147 xmlRelaxNGPopErrors(ctxt, errNr);
10148 break;
10149 }
10150 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10151 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10152 ctxt->state = NULL;
10153 }
10154 ctxt->flags = oldflags;
10155 ret = 0;
10156 if (ctxt->errNr > errNr)
10157 xmlRelaxNGPopErrors(ctxt, errNr);
10158 break;
10159 }
10160 case XML_RELAXNG_ONEORMORE:
10161 errNr = ctxt->errNr;
10162 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10163 if (ret != 0) {
10164 break;
10165 }
10166 if (ctxt->errNr > errNr)
10167 xmlRelaxNGPopErrors(ctxt, errNr);
10168 /* no break on purpose */
10169 case XML_RELAXNG_ZEROORMORE:{
10170 int progress;
10171 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10172 int base, j;
10173
10174 errNr = ctxt->errNr;
10175 res = xmlRelaxNGNewStates(ctxt, 1);
10176 if (res == NULL) {
10177 ret = -1;
10178 break;
10179 }
10180 /*
10181 * All the input states are also exit states
10182 */
10183 if (ctxt->state != NULL) {
10184 xmlRelaxNGAddStates(ctxt, res,
10185 xmlRelaxNGCopyValidState(ctxt,
10186 ctxt->
10187 state));
10188 } else {
10189 for (j = 0; j < ctxt->states->nbState; j++) {
10190 xmlRelaxNGAddStates(ctxt, res,
10191 xmlRelaxNGCopyValidState(ctxt,
10192 ctxt->states->tabState[j]));
10193 }
10194 }
10195 oldflags = ctxt->flags;
10196 ctxt->flags |= FLAGS_IGNORABLE;
10197 do {
10198 progress = 0;
10199 base = res->nbState;
10200
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,
10207 define->
10208 content);
10209 if (ret == 0) {
10210 if (ctxt->state != NULL) {
10211 tmp = xmlRelaxNGAddStates(ctxt, res,
10212 ctxt->state);
10213 ctxt->state = NULL;
10214 if (tmp == 1)
10215 progress = 1;
10216 } else if (ctxt->states != NULL) {
10217 for (j = 0; j < ctxt->states->nbState;
10218 j++) {
10219 tmp =
10220 xmlRelaxNGAddStates(ctxt, res,
10221 ctxt->states->tabState[j]);
10222 if (tmp == 1)
10223 progress = 1;
10224 }
10225 xmlRelaxNGFreeStates(ctxt,
10226 ctxt->states);
10227 ctxt->states = NULL;
10228 }
10229 } else {
10230 if (ctxt->state != NULL) {
10231 xmlRelaxNGFreeValidState(ctxt,
10232 ctxt->state);
10233 ctxt->state = NULL;
10234 }
10235 }
10236 }
10237 } else {
10238 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10239 define->
10240 content);
10241 if (ret != 0) {
10242 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10243 ctxt->state = NULL;
10244 } else {
10245 base = res->nbState;
10246 if (ctxt->state != NULL) {
10247 tmp = xmlRelaxNGAddStates(ctxt, res,
10248 ctxt->state);
10249 ctxt->state = NULL;
10250 if (tmp == 1)
10251 progress = 1;
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]);
10256 if (tmp == 1)
10257 progress = 1;
10258 }
10259 if (states == NULL) {
10260 states = ctxt->states;
10261 } else {
10262 xmlRelaxNGFreeStates(ctxt,
10263 ctxt->states);
10264 }
10265 ctxt->states = NULL;
10266 }
10267 }
10268 }
10269 if (progress) {
10270 /*
10271 * Collect all the new nodes added at that step
10272 * and make them the new node set
10273 */
10274 if (res->nbState - base == 1) {
10275 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10276 res->
10277 tabState
10278 [base]);
10279 } else {
10280 if (states == NULL) {
10281 xmlRelaxNGNewStates(ctxt,
10282 res->nbState - base);
10283 states = ctxt->states;
10284 if (states == NULL) {
10285 progress = 0;
10286 break;
10287 }
10288 }
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;
10295 }
10296 }
10297 } while (progress == 1);
10298 if (states != NULL) {
10299 xmlRelaxNGFreeStates(ctxt, states);
10300 }
10301 ctxt->states = res;
10302 ctxt->flags = oldflags;
10303 #if 0
10304 /*
10305 * errors may have to be propagated back...
10306 */
10307 if (ctxt->errNr > errNr)
10308 xmlRelaxNGPopErrors(ctxt, errNr);
10309 #endif
10310 ret = 0;
10311 break;
10312 }
10313 case XML_RELAXNG_CHOICE:{
10314 xmlRelaxNGDefinePtr list = NULL;
10315 xmlRelaxNGStatesPtr states = NULL;
10316
10317 node = xmlRelaxNGSkipIgnored(ctxt, node);
10318
10319 errNr = ctxt->errNr;
10320 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10321 (node != NULL)) {
10322 /*
10323 * node == NULL can't be optimized since IS_TRIABLE
10324 * doesn't account for choice which may lead to
10325 * only attributes.
10326 */
10327 xmlHashTablePtr triage =
10328 (xmlHashTablePtr) define->data;
10329
10330 /*
10331 * Something we can optimize cleanly there is only one
10332 * possble branch out !
10333 */
10334 if ((node->type == XML_TEXT_NODE) ||
10335 (node->type == XML_CDATA_SECTION_NODE)) {
10336 list =
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,
10341 node->ns->href);
10342 if (list == NULL)
10343 list =
10344 xmlHashLookup2(triage, BAD_CAST "#any",
10345 node->ns->href);
10346 } else
10347 list =
10348 xmlHashLookup2(triage, node->name, NULL);
10349 if (list == NULL)
10350 list =
10351 xmlHashLookup2(triage, BAD_CAST "#any",
10352 NULL);
10353 }
10354 if (list == NULL) {
10355 ret = -1;
10356 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
10357 break;
10358 }
10359 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10360 if (ret == 0) {
10361 }
10362 break;
10363 }
10364
10365 list = define->content;
10366 oldflags = ctxt->flags;
10367 ctxt->flags |= FLAGS_IGNORABLE;
10368
10369 while (list != NULL) {
10370 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10371 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10372 if (ret == 0) {
10373 if (states == NULL) {
10374 states = xmlRelaxNGNewStates(ctxt, 1);
10375 }
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,
10381 ctxt->states->
10382 tabState[i]);
10383 }
10384 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10385 ctxt->states = NULL;
10386 }
10387 } else {
10388 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10389 }
10390 ctxt->state = oldstate;
10391 list = list->next;
10392 }
10393 if (states != NULL) {
10394 xmlRelaxNGFreeValidState(ctxt, oldstate);
10395 ctxt->states = states;
10396 ctxt->state = NULL;
10397 ret = 0;
10398 } else {
10399 ctxt->states = NULL;
10400 }
10401 ctxt->flags = oldflags;
10402 if (ret != 0) {
10403 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10404 xmlRelaxNGDumpValidError(ctxt);
10405 }
10406 } else {
10407 if (ctxt->errNr > errNr)
10408 xmlRelaxNGPopErrors(ctxt, errNr);
10409 }
10410 break;
10411 }
10412 case XML_RELAXNG_DEF:
10413 case XML_RELAXNG_GROUP:
10414 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10415 break;
10416 case XML_RELAXNG_INTERLEAVE:
10417 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10418 break;
10419 case XML_RELAXNG_ATTRIBUTE:
10420 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10421 break;
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);
10428 break;
10429 case XML_RELAXNG_DATATYPE:{
10430 xmlNodePtr child;
10431 xmlChar *content = NULL;
10432
10433 child = node;
10434 while (child != NULL) {
10435 if (child->type == XML_ELEMENT_NODE) {
10436 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10437 node->parent->name);
10438 ret = -1;
10439 break;
10440 } else if ((child->type == XML_TEXT_NODE) ||
10441 (child->type == XML_CDATA_SECTION_NODE)) {
10442 content = xmlStrcat(content, child->content);
10443 }
10444 /* TODO: handle entities ... */
10445 child = child->next;
10446 }
10447 if (ret == -1) {
10448 if (content != NULL)
10449 xmlFree(content);
10450 break;
10451 }
10452 if (content == NULL) {
10453 content = xmlStrdup(BAD_CAST "");
10454 if (content == NULL) {
10455 xmlRngVErrMemory(ctxt, "validating\n");
10456 ret = -1;
10457 break;
10458 }
10459 }
10460 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10461 ctxt->state->seq);
10462 if (ret == -1) {
10463 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10464 } else if (ret == 0) {
10465 ctxt->state->seq = NULL;
10466 }
10467 if (content != NULL)
10468 xmlFree(content);
10469 break;
10470 }
10471 case XML_RELAXNG_VALUE:{
10472 xmlChar *content = NULL;
10473 xmlChar *oldvalue;
10474 xmlNodePtr child;
10475
10476 child = node;
10477 while (child != NULL) {
10478 if (child->type == XML_ELEMENT_NODE) {
10479 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10480 node->parent->name);
10481 ret = -1;
10482 break;
10483 } else if ((child->type == XML_TEXT_NODE) ||
10484 (child->type == XML_CDATA_SECTION_NODE)) {
10485 content = xmlStrcat(content, child->content);
10486 }
10487 /* TODO: handle entities ... */
10488 child = child->next;
10489 }
10490 if (ret == -1) {
10491 if (content != NULL)
10492 xmlFree(content);
10493 break;
10494 }
10495 if (content == NULL) {
10496 content = xmlStrdup(BAD_CAST "");
10497 if (content == NULL) {
10498 xmlRngVErrMemory(ctxt, "validating\n");
10499 ret = -1;
10500 break;
10501 }
10502 }
10503 oldvalue = ctxt->state->value;
10504 ctxt->state->value = content;
10505 ret = xmlRelaxNGValidateValue(ctxt, define);
10506 ctxt->state->value = oldvalue;
10507 if (ret == -1) {
10508 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10509 } else if (ret == 0) {
10510 ctxt->state->seq = NULL;
10511 }
10512 if (content != NULL)
10513 xmlFree(content);
10514 break;
10515 }
10516 case XML_RELAXNG_LIST:{
10517 xmlChar *content;
10518 xmlNodePtr child;
10519 xmlChar *oldvalue, *oldendvalue;
10520 int len;
10521
10522 /*
10523 * Make sure it's only text nodes
10524 */
10525
10526 content = NULL;
10527 child = node;
10528 while (child != NULL) {
10529 if (child->type == XML_ELEMENT_NODE) {
10530 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10531 node->parent->name);
10532 ret = -1;
10533 break;
10534 } else if ((child->type == XML_TEXT_NODE) ||
10535 (child->type == XML_CDATA_SECTION_NODE)) {
10536 content = xmlStrcat(content, child->content);
10537 }
10538 /* TODO: handle entities ... */
10539 child = child->next;
10540 }
10541 if (ret == -1) {
10542 if (content != NULL)
10543 xmlFree(content);
10544 break;
10545 }
10546 if (content == NULL) {
10547 content = xmlStrdup(BAD_CAST "");
10548 if (content == NULL) {
10549 xmlRngVErrMemory(ctxt, "validating\n");
10550 ret = -1;
10551 break;
10552 }
10553 }
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;
10562 if (ret == -1) {
10563 VALID_ERR(XML_RELAXNG_ERR_LIST);
10564 } else if ((ret == 0) && (node != NULL)) {
10565 ctxt->state->seq = node->next;
10566 }
10567 if (content != NULL)
10568 xmlFree(content);
10569 break;
10570 }
10571 case XML_RELAXNG_EXCEPT:
10572 case XML_RELAXNG_PARAM:
10573 TODO ret = -1;
10574 break;
10575 }
10576 ctxt->depth--;
10577 #ifdef DEBUG
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);
10584 if (ret == 0)
10585 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
10586 else
10587 xmlGenericError(xmlGenericErrorContext, "failed\n");
10588 #endif
10589 return (ret);
10590 }
10591
10592 /**
10593 * xmlRelaxNGValidateDefinition:
10594 * @ctxt: a Relax-NG validation context
10595 * @define: the definition to verify
10596 *
10597 * Validate the current node lists against the definition
10598 *
10599 * Returns 0 if the validation succeeded or an error code.
10600 */
10601 static int
xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGDefinePtr define)10602 xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10603 xmlRelaxNGDefinePtr define)
10604 {
10605 xmlRelaxNGStatesPtr states, res;
10606 int i, j, k, ret, oldflags;
10607
10608 /*
10609 * We should NOT have both ctxt->state and ctxt->states
10610 */
10611 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10612 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10613 ctxt->state = NULL;
10614 }
10615
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;
10621 }
10622 ret = xmlRelaxNGValidateState(ctxt, define);
10623 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10624 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10625 ctxt->state = NULL;
10626 }
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;
10631 }
10632 return (ret);
10633 }
10634
10635 states = ctxt->states;
10636 ctxt->states = NULL;
10637 res = NULL;
10638 j = 0;
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);
10645 /*
10646 * We should NOT have both ctxt->state and ctxt->states
10647 */
10648 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10649 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10650 ctxt->state = NULL;
10651 }
10652 if (ret == 0) {
10653 if (ctxt->states == NULL) {
10654 if (res != NULL) {
10655 /* add the state to the container */
10656 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10657 ctxt->state = NULL;
10658 } else {
10659 /* add the state directly in states */
10660 states->tabState[j++] = ctxt->state;
10661 ctxt->state = NULL;
10662 }
10663 } else {
10664 if (res == 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]);
10671 } else {
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;
10678 }
10679 }
10680 } else {
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;
10690 }
10691 }
10692 }
10693 ctxt->flags = oldflags;
10694 if (res != NULL) {
10695 xmlRelaxNGFreeStates(ctxt, states);
10696 ctxt->states = res;
10697 ret = 0;
10698 } else if (j > 1) {
10699 states->nbState = j;
10700 ctxt->states = states;
10701 ret = 0;
10702 } else if (j == 1) {
10703 ctxt->state = states->tabState[0];
10704 xmlRelaxNGFreeStates(ctxt, states);
10705 ret = 0;
10706 } else {
10707 ret = -1;
10708 xmlRelaxNGFreeStates(ctxt, states);
10709 if (ctxt->states != NULL) {
10710 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10711 ctxt->states = NULL;
10712 }
10713 }
10714 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10715 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10716 ctxt->state = NULL;
10717 }
10718 return (ret);
10719 }
10720
10721 /**
10722 * xmlRelaxNGValidateDocument:
10723 * @ctxt: a Relax-NG validation context
10724 * @doc: the document
10725 *
10726 * Validate the given document
10727 *
10728 * Returns 0 if the validation succeeded or an error code.
10729 */
10730 static int
xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt,xmlDocPtr doc)10731 xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10732 {
10733 int ret;
10734 xmlRelaxNGPtr schema;
10735 xmlRelaxNGGrammarPtr grammar;
10736 xmlRelaxNGValidStatePtr state;
10737 xmlNodePtr node;
10738
10739 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
10740 return (-1);
10741
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);
10747 return (-1);
10748 }
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;
10754 node = state->seq;
10755 node = xmlRelaxNGSkipIgnored(ctxt, node);
10756 if (node != NULL) {
10757 if (ret != -1) {
10758 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10759 ret = -1;
10760 }
10761 }
10762 } else if (ctxt->states != NULL) {
10763 int i;
10764 int tmp = -1;
10765
10766 for (i = 0; i < ctxt->states->nbState; i++) {
10767 state = ctxt->states->tabState[i];
10768 node = state->seq;
10769 node = xmlRelaxNGSkipIgnored(ctxt, node);
10770 if (node == NULL)
10771 tmp = 0;
10772 xmlRelaxNGFreeValidState(ctxt, state);
10773 }
10774 if (tmp == -1) {
10775 if (ret != -1) {
10776 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10777 ret = -1;
10778 }
10779 }
10780 }
10781 if (ctxt->state != NULL) {
10782 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10783 ctxt->state = NULL;
10784 }
10785 if (ret != 0)
10786 xmlRelaxNGDumpValidError(ctxt);
10787 #ifdef DEBUG
10788 else if (ctxt->errNr != 0) {
10789 ctxt->error(ctxt->userData,
10790 "%d Extra error messages left on stack !\n",
10791 ctxt->errNr);
10792 xmlRelaxNGDumpValidError(ctxt);
10793 }
10794 #endif
10795 #ifdef LIBXML_VALID_ENABLED
10796 if (ctxt->idref == 1) {
10797 xmlValidCtxt vctxt;
10798
10799 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10800 vctxt.valid = 1;
10801 vctxt.error = ctxt->error;
10802 vctxt.warning = ctxt->warning;
10803 vctxt.userData = ctxt->userData;
10804
10805 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10806 ret = -1;
10807 }
10808 #endif /* LIBXML_VALID_ENABLED */
10809 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
10810 ret = -1;
10811
10812 return (ret);
10813 }
10814
10815 /**
10816 * xmlRelaxNGCleanPSVI:
10817 * @node: an input element or document
10818 *
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.
10824 *
10825 * Returns the number of elements found in the document or -1 in case
10826 * of error.
10827 */
10828 static void
xmlRelaxNGCleanPSVI(xmlNodePtr node)10829 xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10830 xmlNodePtr cur;
10831
10832 if ((node == NULL) ||
10833 ((node->type != XML_ELEMENT_NODE) &&
10834 (node->type != XML_DOCUMENT_NODE) &&
10835 (node->type != XML_HTML_DOCUMENT_NODE)))
10836 return;
10837 if (node->type == XML_ELEMENT_NODE)
10838 node->psvi = NULL;
10839
10840 cur = node->children;
10841 while (cur != NULL) {
10842 if (cur->type == XML_ELEMENT_NODE) {
10843 cur->psvi = NULL;
10844 if (cur->children != NULL) {
10845 cur = cur->children;
10846 continue;
10847 }
10848 }
10849 if (cur->next != NULL) {
10850 cur = cur->next;
10851 continue;
10852 }
10853 do {
10854 cur = cur->parent;
10855 if (cur == NULL)
10856 break;
10857 if (cur == node) {
10858 cur = NULL;
10859 break;
10860 }
10861 if (cur->next != NULL) {
10862 cur = cur->next;
10863 break;
10864 }
10865 } while (cur != NULL);
10866 }
10867 return;
10868 }
10869 /************************************************************************
10870 * *
10871 * Validation interfaces *
10872 * *
10873 ************************************************************************/
10874
10875 /**
10876 * xmlRelaxNGNewValidCtxt:
10877 * @schema: a precompiled XML RelaxNGs
10878 *
10879 * Create an XML RelaxNGs validation context based on the given schema
10880 *
10881 * Returns the validation context or NULL in case of error
10882 */
10883 xmlRelaxNGValidCtxtPtr
xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)10884 xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10885 {
10886 xmlRelaxNGValidCtxtPtr ret;
10887
10888 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10889 if (ret == NULL) {
10890 xmlRngVErrMemory(NULL, "building context\n");
10891 return (NULL);
10892 }
10893 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10894 ret->schema = schema;
10895 ret->error = xmlGenericError;
10896 ret->userData = xmlGenericErrorContext;
10897 ret->errNr = 0;
10898 ret->errMax = 0;
10899 ret->err = NULL;
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;
10907 return (ret);
10908 }
10909
10910 /**
10911 * xmlRelaxNGFreeValidCtxt:
10912 * @ctxt: the schema validation context
10913 *
10914 * Free the resources associated to the schema validation context
10915 */
10916 void
xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)10917 xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10918 {
10919 int k;
10920
10921 if (ctxt == NULL)
10922 return;
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]);
10928 }
10929 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
10930 }
10931 if (ctxt->freeStates != NULL) {
10932 for (k = 0; k < ctxt->freeStatesNr; k++) {
10933 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10934 }
10935 xmlFree(ctxt->freeStates);
10936 }
10937 if (ctxt->errTab != NULL)
10938 xmlFree(ctxt->errTab);
10939 if (ctxt->elemTab != NULL) {
10940 xmlRegExecCtxtPtr exec;
10941
10942 exec = xmlRelaxNGElemPop(ctxt);
10943 while (exec != NULL) {
10944 xmlRegFreeExecCtxt(exec);
10945 exec = xmlRelaxNGElemPop(ctxt);
10946 }
10947 xmlFree(ctxt->elemTab);
10948 }
10949 xmlFree(ctxt);
10950 }
10951
10952 /**
10953 * xmlRelaxNGSetValidErrors:
10954 * @ctxt: a Relax-NG validation context
10955 * @err: the error function
10956 * @warn: the warning function
10957 * @ctx: the functions context
10958 *
10959 * Set the error and warning callback informations
10960 */
10961 void
xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGValidityErrorFunc err,xmlRelaxNGValidityWarningFunc warn,void * ctx)10962 xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10963 xmlRelaxNGValidityErrorFunc err,
10964 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10965 {
10966 if (ctxt == NULL)
10967 return;
10968 ctxt->error = err;
10969 ctxt->warning = warn;
10970 ctxt->userData = ctx;
10971 ctxt->serror = NULL;
10972 }
10973
10974 /**
10975 * xmlRelaxNGSetValidStructuredErrors:
10976 * @ctxt: a Relax-NG validation context
10977 * @serror: the structured error function
10978 * @ctx: the functions context
10979 *
10980 * Set the structured error callback
10981 */
10982 void
xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,xmlStructuredErrorFunc serror,void * ctx)10983 xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
10984 xmlStructuredErrorFunc serror, void *ctx)
10985 {
10986 if (ctxt == NULL)
10987 return;
10988 ctxt->serror = serror;
10989 ctxt->error = NULL;
10990 ctxt->warning = NULL;
10991 ctxt->userData = ctx;
10992 }
10993
10994 /**
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
11000 *
11001 * Get the error and warning callback informations
11002 *
11003 * Returns -1 in case of error and 0 otherwise
11004 */
11005 int
xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,xmlRelaxNGValidityErrorFunc * err,xmlRelaxNGValidityWarningFunc * warn,void ** ctx)11006 xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
11007 xmlRelaxNGValidityErrorFunc * err,
11008 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
11009 {
11010 if (ctxt == NULL)
11011 return (-1);
11012 if (err != NULL)
11013 *err = ctxt->error;
11014 if (warn != NULL)
11015 *warn = ctxt->warning;
11016 if (ctx != NULL)
11017 *ctx = ctxt->userData;
11018 return (0);
11019 }
11020
11021 /**
11022 * xmlRelaxNGValidateDoc:
11023 * @ctxt: a Relax-NG validation context
11024 * @doc: a parsed document tree
11025 *
11026 * Validate a document tree in memory.
11027 *
11028 * Returns 0 if the document is valid, a positive error code
11029 * number otherwise and -1 in case of internal or API error.
11030 */
11031 int
xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt,xmlDocPtr doc)11032 xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
11033 {
11034 int ret;
11035
11036 if ((ctxt == NULL) || (doc == NULL))
11037 return (-1);
11038
11039 ctxt->doc = doc;
11040
11041 ret = xmlRelaxNGValidateDocument(ctxt, doc);
11042 /*
11043 * Remove all left PSVI
11044 */
11045 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
11046
11047 /*
11048 * TODO: build error codes
11049 */
11050 if (ret == -1)
11051 return (1);
11052 return (ret);
11053 }
11054
11055 #define bottom_relaxng
11056 #include "elfgcchack.h"
11057 #endif /* LIBXML_SCHEMAS_ENABLED */
11058