blob: 5dcd0461511f6282d904f781aae552882c829bb7 [file] [log] [blame]
Daniel Veillard6eadf632003-01-23 18:29:16 +00001/*
2 * relaxng.c : implementation of the Relax-NG handling and validity checking
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <veillard@redhat.com>
7 */
8
Daniel Veillardd41f4f42003-01-29 21:07:52 +00009/**
10 * TODO:
11 * - <interleave></interleave><element>...
12 * - error reporting
13 * - module
14 * - fixing ref/def cross grammar contexts
15 */
16
Daniel Veillard6eadf632003-01-23 18:29:16 +000017#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_SCHEMAS_ENABLED
21
22#include <string.h>
23#include <stdio.h>
24#include <libxml/xmlmemory.h>
25#include <libxml/parser.h>
26#include <libxml/parserInternals.h>
27#include <libxml/hash.h>
28#include <libxml/uri.h>
29
30#include <libxml/relaxng.h>
31
32#include <libxml/xmlschemastypes.h>
33#include <libxml/xmlautomata.h>
34#include <libxml/xmlregexp.h>
Daniel Veillardc6e997c2003-01-27 12:35:42 +000035#include <libxml/xmlschemastypes.h>
Daniel Veillard6eadf632003-01-23 18:29:16 +000036
37/*
38 * The Relax-NG namespace
39 */
40static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
41 "http://relaxng.org/ns/structure/1.0";
42
43#define IS_RELAXNG(node, type) \
44 ((node != NULL) && (node->ns != NULL) && \
45 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
46 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
47
48
49#define DEBUG 1 /* very verbose output */
50#define DEBUG_CONTENT 1
51#define DEBUG_TYPE 1
Daniel Veillard276be4a2003-01-24 01:03:34 +000052#define DEBUG_VALID 1
Daniel Veillardb08c9812003-01-28 23:09:49 +000053#define DEBUG_INTERLEAVE 1 */
Daniel Veillard6eadf632003-01-23 18:29:16 +000054
55#define UNBOUNDED (1 << 30)
56#define TODO \
57 xmlGenericError(xmlGenericErrorContext, \
58 "Unimplemented block at %s:%d\n", \
59 __FILE__, __LINE__);
60
61typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
62typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
63
64typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
65typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
66
Daniel Veillardd41f4f42003-01-29 21:07:52 +000067typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
68typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
69
Daniel Veillarda9d912d2003-02-01 17:43:10 +000070typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
71typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
72
Daniel Veillard6eadf632003-01-23 18:29:16 +000073typedef enum {
74 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
75 XML_RELAXNG_COMBINE_CHOICE, /* choice */
76 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
77} xmlRelaxNGCombine;
78
79typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
80typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
81
82struct _xmlRelaxNGGrammar {
83 xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */
84 xmlRelaxNGGrammarPtr children;/* the children grammar if any */
85 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
86 xmlRelaxNGDefinePtr start; /* <start> content */
87 xmlRelaxNGCombine combine; /* the default combine value */
Daniel Veillardd41f4f42003-01-29 21:07:52 +000088 xmlRelaxNGDefinePtr startList;/* list of <start> definitions */
Daniel Veillard6eadf632003-01-23 18:29:16 +000089 xmlHashTablePtr defs; /* define* */
90 xmlHashTablePtr refs; /* references */
91};
92
93
Daniel Veillard6eadf632003-01-23 18:29:16 +000094typedef enum {
95 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
96 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
97 XML_RELAXNG_TEXT, /* textual content */
98 XML_RELAXNG_ELEMENT, /* an element */
99 XML_RELAXNG_DATATYPE, /* extenal data type definition */
100 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
101 XML_RELAXNG_LIST, /* a list of patterns */
102 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
103 XML_RELAXNG_DEF, /* a definition */
104 XML_RELAXNG_REF, /* reference to a definition */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000105 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000106 XML_RELAXNG_OPTIONAL, /* optional patterns */
107 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
108 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
109 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
110 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000111 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
112 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000113} xmlRelaxNGType;
114
115struct _xmlRelaxNGDefine {
116 xmlRelaxNGType type; /* the type of definition */
117 xmlNodePtr node; /* the node in the source */
118 xmlChar *name; /* the element local name if present */
119 xmlChar *ns; /* the namespace local name if present */
Daniel Veillardedc91922003-01-26 00:52:04 +0000120 xmlChar *value; /* value when available */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000121 void *data; /* data lib or specific pointer */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000122 xmlRelaxNGDefinePtr content;/* the expected content */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000123 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000124 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
125 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
126 xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */
127};
128
129/**
130 * _xmlRelaxNG:
131 *
132 * A RelaxNGs definition
133 */
134struct _xmlRelaxNG {
135 xmlRelaxNGGrammarPtr topgrammar;
136 xmlDocPtr doc;
137
138 xmlHashTablePtr defs; /* define */
139 xmlHashTablePtr refs; /* references */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000140 xmlHashTablePtr documents; /* all the documents loaded */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000141 xmlHashTablePtr includes; /* all the includes loaded */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000142 void *_private; /* unused by the library for users or bindings */
143};
144
145typedef enum {
146 XML_RELAXNG_ERR_OK = 0,
147 XML_RELAXNG_ERR_NOROOT = 1,
148 XML_RELAXNG_ERR_
149} xmlRelaxNGValidError;
150
151#define XML_RELAXNG_IN_ATTRIBUTE 1
152
153struct _xmlRelaxNGParserCtxt {
154 void *userData; /* user specific data block */
155 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
156 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
157 xmlRelaxNGValidError err;
158
159 xmlRelaxNGPtr schema; /* The schema in use */
160 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
161 int flags; /* parser flags */
162 int nbErrors; /* number of errors at parse time */
163 int nbWarnings; /* number of warnings at parse time */
Daniel Veillard276be4a2003-01-24 01:03:34 +0000164 const xmlChar *define; /* the current define scope */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000165 xmlRelaxNGDefinePtr def; /* the current define */
166
167 int nbInterleaves;
168 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000169
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000170 xmlHashTablePtr documents; /* all the documents loaded */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000171 xmlHashTablePtr includes; /* all the includes loaded */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000172 xmlChar *URL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000173 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000174
175 const char *buffer;
176 int size;
177
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000178 /* the document stack */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000179 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000180 int docNr; /* Depth of the parsing stack */
181 int docMax; /* Max depth of the parsing stack */
182 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000183
184 /* the include stack */
185 xmlRelaxNGIncludePtr inc; /* Current parsed include */
186 int incNr; /* Depth of the include parsing stack */
187 int incMax; /* Max depth of the parsing stack */
188 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000189};
190
191#define FLAGS_IGNORABLE 1
192#define FLAGS_NEGATIVE 2
193
194/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000195 * xmlRelaxNGInterleaveGroup:
196 *
197 * A RelaxNGs partition set associated to lists of definitions
198 */
199typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
200typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
201struct _xmlRelaxNGInterleaveGroup {
202 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
203 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
204};
205
206/**
207 * xmlRelaxNGPartitions:
208 *
209 * A RelaxNGs partition associated to an interleave group
210 */
211typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
212typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
213struct _xmlRelaxNGPartition {
214 int nbgroups; /* number of groups in the partitions */
215 xmlRelaxNGInterleaveGroupPtr *groups;
216};
217
218/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000219 * xmlRelaxNGValidState:
220 *
221 * A RelaxNGs validation state
222 */
223#define MAX_ATTR 20
224typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
225typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
226struct _xmlRelaxNGValidState {
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000227 xmlNodePtr node; /* the current node */
228 xmlNodePtr seq; /* the sequence of children left to validate */
229 int nbAttrs; /* the number of attributes */
230 xmlChar *value; /* the value when operating on string */
231 xmlChar *endvalue; /* the end value when operating on string */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000232 xmlAttrPtr attrs[1]; /* the array of attributes */
233};
234
235/**
236 * xmlRelaxNGValidCtxt:
237 *
238 * A RelaxNGs validation context
239 */
240
241struct _xmlRelaxNGValidCtxt {
242 void *userData; /* user specific data block */
243 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
244 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
245
246 xmlRelaxNGPtr schema; /* The schema in use */
247 xmlDocPtr doc; /* the document being validated */
248 xmlRelaxNGValidStatePtr state; /* the current validation state */
249 int flags; /* validation flags */
250};
251
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000252/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000253 * xmlRelaxNGInclude:
254 *
255 * Structure associated to a RelaxNGs document element
256 */
257struct _xmlRelaxNGInclude {
258 xmlChar *href; /* the normalized href value */
259 xmlDocPtr doc; /* the associated XML document */
260 xmlRelaxNGDefinePtr content;/* the definitions */
261 xmlRelaxNGPtr schema; /* the schema */
262};
263
264/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000265 * xmlRelaxNGDocument:
266 *
267 * Structure associated to a RelaxNGs document element
268 */
269struct _xmlRelaxNGDocument {
270 xmlChar *href; /* the normalized href value */
271 xmlDocPtr doc; /* the associated XML document */
272 xmlRelaxNGDefinePtr content;/* the definitions */
273 xmlRelaxNGPtr schema; /* the schema */
274};
275
Daniel Veillard6eadf632003-01-23 18:29:16 +0000276/************************************************************************
277 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000278 * Preliminary type checking interfaces *
279 * *
280 ************************************************************************/
281/**
282 * xmlRelaxNGTypeHave:
283 * @data: data needed for the library
284 * @type: the type name
285 * @value: the value to check
286 *
287 * Function provided by a type library to check if a type is exported
288 *
289 * Returns 1 if yes, 0 if no and -1 in case of error.
290 */
291typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type);
292
293/**
294 * xmlRelaxNGTypeCheck:
295 * @data: data needed for the library
296 * @type: the type name
297 * @value: the value to check
298 *
299 * Function provided by a type library to check if a value match a type
300 *
301 * Returns 1 if yes, 0 if no and -1 in case of error.
302 */
303typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type,
304 const xmlChar *value);
305
306/**
307 * xmlRelaxNGTypeCompare:
308 * @data: data needed for the library
309 * @type: the type name
310 * @value1: the first value
311 * @value2: the second value
312 *
313 * Function provided by a type library to compare two values accordingly
314 * to a type.
315 *
316 * Returns 1 if yes, 0 if no and -1 in case of error.
317 */
318typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type,
319 const xmlChar *value1,
320 const xmlChar *value2);
321typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
322typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
323struct _xmlRelaxNGTypeLibrary {
324 const xmlChar *namespace; /* the datatypeLibrary value */
325 void *data; /* data needed for the library */
326 xmlRelaxNGTypeHave have; /* the export function */
327 xmlRelaxNGTypeCheck check; /* the checking function */
328 xmlRelaxNGTypeCompare comp; /* the compare function */
329};
330
331/************************************************************************
332 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000333 * Allocation functions *
334 * *
335 ************************************************************************/
336static void xmlRelaxNGFreeDefineList(xmlRelaxNGDefinePtr defines);
337static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
338static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
339
340/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000341 * xmlRelaxNGFreeDocument:
342 * @docu: a document structure
343 *
344 * Deallocate a RelaxNG document structure.
345 */
346static void
347xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
348{
349 if (docu == NULL)
350 return;
351
352 if (docu->href != NULL)
353 xmlFree(docu->href);
354 if (docu->doc != NULL)
355 xmlFreeDoc(docu->doc);
356 if (docu->schema != NULL)
357 xmlRelaxNGFree(docu->schema);
358 xmlFree(docu);
359}
360
361/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000362 * xmlRelaxNGFreeInclude:
363 * @incl: a include structure
364 *
365 * Deallocate a RelaxNG include structure.
366 */
367static void
368xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
369{
370 if (incl == NULL)
371 return;
372
373 if (incl->href != NULL)
374 xmlFree(incl->href);
375 if (incl->doc != NULL)
376 xmlFreeDoc(incl->doc);
377 if (incl->schema != NULL)
378 xmlRelaxNGFree(incl->schema);
379 xmlFree(incl);
380}
381
382/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000383 * xmlRelaxNGNewRelaxNG:
384 * @ctxt: a Relax-NG validation context (optional)
385 *
386 * Allocate a new RelaxNG structure.
387 *
388 * Returns the newly allocated structure or NULL in case or error
389 */
390static xmlRelaxNGPtr
391xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
392{
393 xmlRelaxNGPtr ret;
394
395 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
396 if (ret == NULL) {
397 if ((ctxt != NULL) && (ctxt->error != NULL))
398 ctxt->error(ctxt->userData, "Out of memory\n");
399 ctxt->nbErrors++;
400 return (NULL);
401 }
402 memset(ret, 0, sizeof(xmlRelaxNG));
403
404 return (ret);
405}
406
407/**
408 * xmlRelaxNGFree:
409 * @schema: a schema structure
410 *
411 * Deallocate a RelaxNG structure.
412 */
413void
414xmlRelaxNGFree(xmlRelaxNGPtr schema)
415{
416 if (schema == NULL)
417 return;
418
Daniel Veillard6eadf632003-01-23 18:29:16 +0000419 if (schema->topgrammar != NULL)
420 xmlRelaxNGFreeGrammar(schema->topgrammar);
421 if (schema->doc != NULL)
422 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000423 if (schema->documents != NULL)
424 xmlHashFree(schema->documents, (xmlHashDeallocator)
425 xmlRelaxNGFreeDocument);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000426 if (schema->includes != NULL)
427 xmlHashFree(schema->includes, (xmlHashDeallocator)
428 xmlRelaxNGFreeInclude);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000429
430 xmlFree(schema);
431}
432
433/**
434 * xmlRelaxNGNewGrammar:
435 * @ctxt: a Relax-NG validation context (optional)
436 *
437 * Allocate a new RelaxNG grammar.
438 *
439 * Returns the newly allocated structure or NULL in case or error
440 */
441static xmlRelaxNGGrammarPtr
442xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
443{
444 xmlRelaxNGGrammarPtr ret;
445
446 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
447 if (ret == NULL) {
448 if ((ctxt != NULL) && (ctxt->error != NULL))
449 ctxt->error(ctxt->userData, "Out of memory\n");
450 ctxt->nbErrors++;
451 return (NULL);
452 }
453 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
454
455 return (ret);
456}
457
458/**
Daniel Veillard276be4a2003-01-24 01:03:34 +0000459 * xmlRelaxNGFreeDefineHash:
460 * @defines: a list of define structures
461 *
462 * Deallocate a RelaxNG definition in the hash table
463 */
464static void
465xmlRelaxNGFreeDefineHash(xmlRelaxNGDefinePtr defines)
466{
467 xmlRelaxNGDefinePtr next;
468
469 while (defines != NULL) {
470 next = defines->nextHash;
471 xmlRelaxNGFreeDefine(defines);
472 defines = next;
473 }
474}
475
476/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000477 * xmlRelaxNGFreeGrammar:
478 * @grammar: a grammar structure
479 *
480 * Deallocate a RelaxNG grammar structure.
481 */
482static void
483xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
484{
485 if (grammar == NULL)
486 return;
487
488 if (grammar->start != NULL)
489 xmlRelaxNGFreeDefine(grammar->start);
490 if (grammar->refs != NULL) {
491 xmlHashFree(grammar->refs, NULL);
492 }
493 if (grammar->defs != NULL) {
Daniel Veillard276be4a2003-01-24 01:03:34 +0000494 xmlHashFree(grammar->defs, (xmlHashDeallocator)
495 xmlRelaxNGFreeDefineHash);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000496 }
497
498 xmlFree(grammar);
499}
500
501/**
502 * xmlRelaxNGNewDefine:
503 * @ctxt: a Relax-NG validation context
504 * @node: the node in the input document.
505 *
506 * Allocate a new RelaxNG define.
507 *
508 * Returns the newly allocated structure or NULL in case or error
509 */
510static xmlRelaxNGDefinePtr
511xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
512{
513 xmlRelaxNGDefinePtr ret;
514
515 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
516 if (ret == NULL) {
517 if ((ctxt != NULL) && (ctxt->error != NULL))
518 ctxt->error(ctxt->userData, "Out of memory\n");
519 ctxt->nbErrors++;
520 return (NULL);
521 }
522 memset(ret, 0, sizeof(xmlRelaxNGDefine));
523 ret->node = node;
524
525 return (ret);
526}
527
528/**
529 * xmlRelaxNGFreeDefineList:
530 * @defines: a list of define structures
531 *
532 * Deallocate a RelaxNG define structures.
533 */
534static void
535xmlRelaxNGFreeDefineList(xmlRelaxNGDefinePtr defines)
536{
537 xmlRelaxNGDefinePtr next;
538
539 while (defines != NULL) {
540 next = defines->next;
541 xmlRelaxNGFreeDefine(defines);
542 defines = next;
543 }
544}
545
546/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000547 * xmlRelaxNGFreePartition:
548 * @partitions: a partition set structure
549 *
550 * Deallocate RelaxNG partition set structures.
551 */
552static void
553xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) {
554 xmlRelaxNGInterleaveGroupPtr group;
555 int j;
556
557 if (partitions != NULL) {
558 if (partitions->groups != NULL) {
559 for (j = 0;j < partitions->nbgroups;j++) {
560 group = partitions->groups[j];
561 if (group != NULL) {
562 if (group->defs != NULL)
563 xmlFree(group->defs);
564 xmlFree(group);
565 }
566 }
567 xmlFree(partitions->groups);
568 }
569 xmlFree(partitions);
570 }
571}
572/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000573 * xmlRelaxNGFreeDefine:
574 * @define: a define structure
575 *
576 * Deallocate a RelaxNG define structure.
577 */
578static void
579xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
580{
581 if (define == NULL)
582 return;
583
584 if (define->name != NULL)
585 xmlFree(define->name);
586 if (define->ns != NULL)
587 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000588 if (define->value != NULL)
589 xmlFree(define->value);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000590 if (define->attrs != NULL)
591 xmlRelaxNGFreeDefineList(define->attrs);
Daniel Veillard276be4a2003-01-24 01:03:34 +0000592 if ((define->content != NULL) &&
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000593 (define->type != XML_RELAXNG_REF) &&
594 (define->type != XML_RELAXNG_EXTERNALREF))
Daniel Veillard6eadf632003-01-23 18:29:16 +0000595 xmlRelaxNGFreeDefineList(define->content);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000596 if ((define->data != NULL) &&
597 (define->type == XML_RELAXNG_INTERLEAVE))
598 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000599 xmlFree(define);
600}
601
602/**
603 * xmlRelaxNGNewValidState:
604 * @ctxt: a Relax-NG validation context
605 * @node: the current node or NULL for the document
606 *
607 * Allocate a new RelaxNG validation state
608 *
609 * Returns the newly allocated structure or NULL in case or error
610 */
611static xmlRelaxNGValidStatePtr
612xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
613{
614 xmlRelaxNGValidStatePtr ret;
615 xmlAttrPtr attr;
616 xmlAttrPtr attrs[MAX_ATTR];
617 int nbAttrs = 0;
618 xmlNodePtr root = NULL;
619
620 if (node == NULL) {
621 root = xmlDocGetRootElement(ctxt->doc);
622 if (root == NULL)
623 return(NULL);
624 } else {
625 attr = node->properties;
626 while (attr != NULL) {
627 if (nbAttrs < MAX_ATTR)
628 attrs[nbAttrs++] = attr;
629 else
630 nbAttrs++;
631 attr = attr->next;
632 }
633 }
634
635 if (nbAttrs < MAX_ATTR)
636 attrs[nbAttrs] = NULL;
637 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState) +
638 nbAttrs * sizeof(xmlAttrPtr));
639 if (ret == NULL) {
640 if ((ctxt != NULL) && (ctxt->error != NULL))
641 ctxt->error(ctxt->userData, "Out of memory\n");
642 return (NULL);
643 }
644 if (node == NULL) {
645 ret->node = (xmlNodePtr) ctxt->doc;
646 ret->seq = root;
647 ret->nbAttrs = 0;
648 } else {
649 ret->node = node;
650 ret->seq = node->children;
651 ret->nbAttrs = nbAttrs;
652 if (nbAttrs > 0) {
653 if (nbAttrs < MAX_ATTR) {
654 memcpy(&(ret->attrs[0]), attrs,
655 sizeof(xmlAttrPtr) * (nbAttrs + 1));
656 } else {
657 attr = node->properties;
658 nbAttrs = 0;
659 while (attr != NULL) {
660 ret->attrs[nbAttrs++] = attr;
661 attr = attr->next;
662 }
663 ret->attrs[nbAttrs] = NULL;
664 }
665 }
666 }
667 return (ret);
668}
669
670/**
671 * xmlRelaxNGCopyValidState:
672 * @ctxt: a Relax-NG validation context
673 * @state: a validation state
674 *
675 * Copy the validation state
676 *
677 * Returns the newly allocated structure or NULL in case or error
678 */
679static xmlRelaxNGValidStatePtr
680xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
681 xmlRelaxNGValidStatePtr state)
682{
683 xmlRelaxNGValidStatePtr ret;
684 unsigned int size;
685
686 if (state == NULL)
687 return(NULL);
688
689 size = sizeof(xmlRelaxNGValidState) +
690 state->nbAttrs * sizeof(xmlAttrPtr);
691 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(size);
692 if (ret == NULL) {
693 if ((ctxt != NULL) && (ctxt->error != NULL))
694 ctxt->error(ctxt->userData, "Out of memory\n");
695 return (NULL);
696 }
697 memcpy(ret, state, size);
698 return(ret);
699}
700
701/**
702 * xmlRelaxNGFreeValidState:
703 * @state: a validation state structure
704 *
705 * Deallocate a RelaxNG validation state structure.
706 */
707static void
708xmlRelaxNGFreeValidState(xmlRelaxNGValidStatePtr state)
709{
710 if (state == NULL)
711 return;
712
713 xmlFree(state);
714}
715
716/************************************************************************
717 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000718 * Document functions *
719 * *
720 ************************************************************************/
721static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
722 xmlDocPtr doc);
723
724/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000725 * xmlRelaxNGIncludePush:
726 * @ctxt: the parser context
727 * @value: the element doc
728 *
729 * Pushes a new include on top of the include stack
730 *
731 * Returns 0 in case of error, the index in the stack otherwise
732 */
733static int
734xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
735 xmlRelaxNGIncludePtr value)
736{
737 if (ctxt->incTab == NULL) {
738 ctxt->incMax = 4;
739 ctxt->incNr = 0;
740 ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc(
741 ctxt->incMax * sizeof(ctxt->incTab[0]));
742 if (ctxt->incTab == NULL) {
743 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
744 return (0);
745 }
746 }
747 if (ctxt->incNr >= ctxt->incMax) {
748 ctxt->incMax *= 2;
749 ctxt->incTab =
750 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
751 ctxt->incMax *
752 sizeof(ctxt->incTab[0]));
753 if (ctxt->incTab == NULL) {
754 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
755 return (0);
756 }
757 }
758 ctxt->incTab[ctxt->incNr] = value;
759 ctxt->inc = value;
760 return (ctxt->incNr++);
761}
762
763/**
764 * xmlRelaxNGIncludePop:
765 * @ctxt: the parser context
766 *
767 * Pops the top include from the include stack
768 *
769 * Returns the include just removed
770 */
771static xmlRelaxNGIncludePtr
772xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
773{
774 xmlRelaxNGIncludePtr ret;
775
776 if (ctxt->incNr <= 0)
777 return (0);
778 ctxt->incNr--;
779 if (ctxt->incNr > 0)
780 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
781 else
782 ctxt->inc = NULL;
783 ret = ctxt->incTab[ctxt->incNr];
784 ctxt->incTab[ctxt->incNr] = 0;
785 return (ret);
786}
787
788/**
789 * xmlRelaxNGLoadInclude:
790 * @ctxt: the parser context
791 * @URL: the normalized URL
792 * @node: the include node.
793 *
794 * First lookup if the document is already loaded into the parser context,
795 * check against recursion. If not found the resource is loaded and
796 * the content is preprocessed before being returned back to the caller.
797 *
798 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
799 */
800static xmlRelaxNGIncludePtr
801xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
802 xmlNodePtr node) {
803 xmlRelaxNGIncludePtr ret = NULL;
804 xmlDocPtr doc;
805 int i;
806 xmlNodePtr root, tmp, tmp2, cur;
807
808 /*
809 * check against recursion in the stack
810 */
811 for (i = 0;i < ctxt->incNr;i++) {
812 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
813 if (ctxt->error != NULL)
814 ctxt->error(ctxt->userData,
815 "Detected an externalRef recursion for %s\n",
816 URL);
817 ctxt->nbErrors++;
818 return(NULL);
819 }
820 }
821
822 /*
823 * Lookup in the hash table
824 */
825 if (ctxt->includes == NULL) {
826 ctxt->includes = xmlHashCreate(10);
827 if (ctxt->includes == NULL) {
828 if (ctxt->error != NULL)
829 ctxt->error(ctxt->userData,
830 "Failed to allocate hash table for document\n");
831 ctxt->nbErrors++;
832 return(NULL);
833 }
834 } else {
835 ret = xmlHashLookup(ctxt->includes, URL);
836 if (ret != NULL)
837 return(ret);
838 }
839
840
841 /*
842 * load the document
843 */
844 doc = xmlParseFile((const char *) URL);
845 if (doc == NULL) {
846 if (ctxt->error != NULL)
847 ctxt->error(ctxt->userData,
848 "xmlRelaxNG: could not load %s\n", URL);
849 ctxt->nbErrors++;
850 return (NULL);
851 }
852
853 /*
854 * Allocate the document structures and register it first.
855 */
856 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
857 if (ret == NULL) {
858 if (ctxt->error != NULL)
859 ctxt->error(ctxt->userData,
860 "xmlRelaxNG: allocate memory for doc %s\n", URL);
861 ctxt->nbErrors++;
862 xmlFreeDoc(doc);
863 return (NULL);
864 }
865 memset(ret, 0, sizeof(xmlRelaxNGInclude));
866 ret->doc = doc;
867 ret->href = xmlStrdup(URL);
868
869 /*
870 * push it on the stack and register it in the hash table
871 */
872 xmlHashAddEntry(ctxt->includes, URL, ret);
873 xmlRelaxNGIncludePush(ctxt, ret);
874
875 /*
876 * Some preprocessing of the document content, this include recursing
877 * in the include stack.
878 */
879 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
880 if (doc == NULL) {
881 /* xmlFreeDoc(ctxt->include); */
882 ctxt->inc = NULL;
883 return(NULL);
884 }
885
886 /*
887 * Pop up the include from the stack
888 */
889 xmlRelaxNGIncludePop(ctxt);
890
891 /*
892 * Check that the top element is a grammar
893 */
894 root = xmlDocGetRootElement(doc);
895 if (root == NULL) {
896 if (ctxt->error != NULL)
897 ctxt->error(ctxt->userData,
898 "xmlRelaxNG: included document is empty %s\n", URL);
899 ctxt->nbErrors++;
900 xmlFreeDoc(doc);
901 return (NULL);
902 }
903 if (!IS_RELAXNG(root, "grammar")) {
904 if (ctxt->error != NULL)
905 ctxt->error(ctxt->userData,
906 "xmlRelaxNG: included document %s root is not a grammar\n",
907 URL);
908 ctxt->nbErrors++;
909 xmlFreeDoc(doc);
910 return (NULL);
911 }
912
913 /*
914 * Elimination of redefined rules in the include.
915 */
916 cur = node->children;
917 while (cur != NULL) {
918 if (IS_RELAXNG(cur, "start")) {
919 int found = 0;
920
921 tmp = root->children;
922 while (tmp != NULL) {
923 tmp2 = tmp->next;
924 if (IS_RELAXNG(tmp, "start")) {
925 found = 1;
926 xmlUnlinkNode(tmp);
927 xmlFreeNode(tmp);
928 }
929 tmp = tmp2;
930 }
931 if (!found) {
932 if (ctxt->error != NULL)
933 ctxt->error(ctxt->userData,
934 "xmlRelaxNG: include %s has a start but not the included grammar\n",
935 URL);
936 ctxt->nbErrors++;
937 }
938 } else if (IS_RELAXNG(cur, "define")) {
939 xmlChar *name, *name2;
940
941 name = xmlGetProp(cur, BAD_CAST "name");
942 if (name == NULL) {
943 if (ctxt->error != NULL)
944 ctxt->error(ctxt->userData,
945 "xmlRelaxNG: include %s has define without name\n",
946 URL);
947 ctxt->nbErrors++;
948 } else {
949 int found = 0;
950
951 tmp = root->children;
952 while (tmp != NULL) {
953 tmp2 = tmp->next;
954 if (IS_RELAXNG(tmp, "define")) {
955 name2 = xmlGetProp(tmp, BAD_CAST "name");
956 if (name2 != NULL) {
957 if (xmlStrEqual(name, name2)) {
958 found = 1;
959 xmlUnlinkNode(tmp);
960 xmlFreeNode(tmp);
961 }
962 xmlFree(name2);
963 }
964 }
965 tmp = tmp2;
966 }
967 if (!found) {
968 if (ctxt->error != NULL)
969 ctxt->error(ctxt->userData,
970 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
971 URL, name);
972 ctxt->nbErrors++;
973 }
974 xmlFree(name);
975 }
976 }
977 cur = cur->next;
978 }
979
980
981 return(ret);
982}
983
984/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000985 * xmlRelaxNGDocumentPush:
986 * @ctxt: the parser context
987 * @value: the element doc
988 *
989 * Pushes a new doc on top of the doc stack
990 *
991 * Returns 0 in case of error, the index in the stack otherwise
992 */
993static int
994xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
995 xmlRelaxNGDocumentPtr value)
996{
997 if (ctxt->docTab == NULL) {
998 ctxt->docMax = 4;
999 ctxt->docNr = 0;
1000 ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc(
1001 ctxt->docMax * sizeof(ctxt->docTab[0]));
1002 if (ctxt->docTab == NULL) {
1003 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1004 return (0);
1005 }
1006 }
1007 if (ctxt->docNr >= ctxt->docMax) {
1008 ctxt->docMax *= 2;
1009 ctxt->docTab =
1010 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1011 ctxt->docMax *
1012 sizeof(ctxt->docTab[0]));
1013 if (ctxt->docTab == NULL) {
1014 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1015 return (0);
1016 }
1017 }
1018 ctxt->docTab[ctxt->docNr] = value;
1019 ctxt->doc = value;
1020 return (ctxt->docNr++);
1021}
1022
1023/**
1024 * xmlRelaxNGDocumentPop:
1025 * @ctxt: the parser context
1026 *
1027 * Pops the top doc from the doc stack
1028 *
1029 * Returns the doc just removed
1030 */
1031static xmlRelaxNGDocumentPtr
1032xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1033{
1034 xmlRelaxNGDocumentPtr ret;
1035
1036 if (ctxt->docNr <= 0)
1037 return (0);
1038 ctxt->docNr--;
1039 if (ctxt->docNr > 0)
1040 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1041 else
1042 ctxt->doc = NULL;
1043 ret = ctxt->docTab[ctxt->docNr];
1044 ctxt->docTab[ctxt->docNr] = 0;
1045 return (ret);
1046}
1047
1048/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001049 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001050 * @ctxt: the parser context
1051 * @URL: the normalized URL
1052 * @ns: the inherited ns if any
1053 *
1054 * First lookup if the document is already loaded into the parser context,
1055 * check against recursion. If not found the resource is loaded and
1056 * the content is preprocessed before being returned back to the caller.
1057 *
1058 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1059 */
1060static xmlRelaxNGDocumentPtr
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001061xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001062 const xmlChar *ns) {
1063 xmlRelaxNGDocumentPtr ret = NULL;
1064 xmlDocPtr doc;
1065 xmlNodePtr root;
1066 int i;
1067
1068 /*
1069 * check against recursion in the stack
1070 */
1071 for (i = 0;i < ctxt->docNr;i++) {
1072 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1073 if (ctxt->error != NULL)
1074 ctxt->error(ctxt->userData,
1075 "Detected an externalRef recursion for %s\n",
1076 URL);
1077 ctxt->nbErrors++;
1078 return(NULL);
1079 }
1080 }
1081
1082 /*
1083 * Lookup in the hash table
1084 */
1085 if (ctxt->documents == NULL) {
1086 ctxt->documents = xmlHashCreate(10);
1087 if (ctxt->documents == NULL) {
1088 if (ctxt->error != NULL)
1089 ctxt->error(ctxt->userData,
1090 "Failed to allocate hash table for document\n");
1091 ctxt->nbErrors++;
1092 return(NULL);
1093 }
1094 } else {
1095 if (ns == NULL)
1096 ret = xmlHashLookup2(ctxt->documents, BAD_CAST "", URL);
1097 else
1098 ret = xmlHashLookup2(ctxt->documents, ns, URL);
1099 if (ret != NULL)
1100 return(ret);
1101 }
1102
1103
1104 /*
1105 * load the document
1106 */
1107 doc = xmlParseFile((const char *) URL);
1108 if (doc == NULL) {
1109 if (ctxt->error != NULL)
1110 ctxt->error(ctxt->userData,
1111 "xmlRelaxNG: could not load %s\n", URL);
1112 ctxt->nbErrors++;
1113 return (NULL);
1114 }
1115
1116 /*
1117 * Allocate the document structures and register it first.
1118 */
1119 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1120 if (ret == NULL) {
1121 if (ctxt->error != NULL)
1122 ctxt->error(ctxt->userData,
1123 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1124 ctxt->nbErrors++;
1125 xmlFreeDoc(doc);
1126 return (NULL);
1127 }
1128 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1129 ret->doc = doc;
1130 ret->href = xmlStrdup(URL);
1131
1132 /*
1133 * transmit the ns if needed
1134 */
1135 if (ns != NULL) {
1136 root = xmlDocGetRootElement(doc);
1137 if (root != NULL) {
1138 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1139 xmlSetProp(root, BAD_CAST"ns", ns);
1140 }
1141 }
1142 }
1143
1144 /*
1145 * push it on the stack and register it in the hash table
1146 */
1147 if (ns == NULL)
1148 xmlHashAddEntry2(ctxt->documents, BAD_CAST "", URL, ret);
1149 else
1150 xmlHashAddEntry2(ctxt->documents, ns, URL, ret);
1151 xmlRelaxNGDocumentPush(ctxt, ret);
1152
1153 /*
1154 * Some preprocessing of the document content
1155 */
1156 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1157 if (doc == NULL) {
1158 xmlFreeDoc(ctxt->document);
1159 ctxt->doc = NULL;
1160 return(NULL);
1161 }
1162
1163 xmlRelaxNGDocumentPop(ctxt);
1164
1165 return(ret);
1166}
1167
1168/************************************************************************
1169 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00001170 * Error functions *
1171 * *
1172 ************************************************************************/
1173
1174#define VALID_CTXT() \
1175 if (ctxt->flags == 0) xmlGenericError(xmlGenericErrorContext, \
1176 "error detected at %s:%d\n", \
1177 __FILE__, __LINE__);
1178#define VALID_ERROR if (ctxt->flags == 0) printf
1179
1180#if 0
1181/**
1182 * xmlRelaxNGErrorContext:
1183 * @ctxt: the parsing context
1184 * @schema: the schema being built
1185 * @node: the node being processed
1186 * @child: the child being processed
1187 *
1188 * Dump a RelaxNGType structure
1189 */
1190static void
1191xmlRelaxNGErrorContext(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGPtr schema,
1192 xmlNodePtr node, xmlNodePtr child)
1193{
1194 int line = 0;
1195 const xmlChar *file = NULL;
1196 const xmlChar *name = NULL;
1197 const char *type = "error";
1198
1199 if ((ctxt == NULL) || (ctxt->error == NULL))
1200 return;
1201
1202 if (child != NULL)
1203 node = child;
1204
1205 if (node != NULL) {
1206 if ((node->type == XML_DOCUMENT_NODE) ||
1207 (node->type == XML_HTML_DOCUMENT_NODE)) {
1208 xmlDocPtr doc = (xmlDocPtr) node;
1209
1210 file = doc->URL;
1211 } else {
1212 /*
1213 * Try to find contextual informations to report
1214 */
1215 if (node->type == XML_ELEMENT_NODE) {
1216 line = (int) node->content;
1217 } else if ((node->prev != NULL) &&
1218 (node->prev->type == XML_ELEMENT_NODE)) {
1219 line = (int) node->prev->content;
1220 } else if ((node->parent != NULL) &&
1221 (node->parent->type == XML_ELEMENT_NODE)) {
1222 line = (int) node->parent->content;
1223 }
1224 if ((node->doc != NULL) && (node->doc->URL != NULL))
1225 file = node->doc->URL;
1226 if (node->name != NULL)
1227 name = node->name;
1228 }
1229 }
1230
1231 if (ctxt != NULL)
1232 type = "compilation error";
1233 else if (schema != NULL)
1234 type = "runtime error";
1235
1236 if ((file != NULL) && (line != 0) && (name != NULL))
1237 ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n",
1238 type, file, line, name);
1239 else if ((file != NULL) && (name != NULL))
1240 ctxt->error(ctxt->userData, "%s: file %s element %s\n",
1241 type, file, name);
1242 else if ((file != NULL) && (line != 0))
1243 ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line);
1244 else if (file != NULL)
1245 ctxt->error(ctxt->userData, "%s: file %s\n", type, file);
1246 else if (name != NULL)
1247 ctxt->error(ctxt->userData, "%s: element %s\n", type, name);
1248 else
1249 ctxt->error(ctxt->userData, "%s\n", type);
1250}
1251#endif
1252
1253/************************************************************************
1254 * *
1255 * Type library hooks *
1256 * *
1257 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00001258static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
1259 const xmlChar *str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001260
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001261/**
1262 * xmlRelaxNGSchemaTypeHave:
1263 * @data: data needed for the library
1264 * @type: the type name
1265 *
1266 * Check if the given type is provided by
1267 * the W3C XMLSchema Datatype library.
1268 *
1269 * Returns 1 if yes, 0 if no and -1 in case of error.
1270 */
1271static int
1272xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00001273 const xmlChar *type) {
1274 xmlSchemaTypePtr typ;
1275
1276 if (type == NULL)
1277 return(-1);
1278 typ = xmlSchemaGetPredefinedType(type,
1279 BAD_CAST "http://www.w3.org/2001/XMLSchema");
1280 if (typ == NULL)
1281 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001282 return(1);
1283}
1284
1285/**
1286 * xmlRelaxNGSchemaTypeCheck:
1287 * @data: data needed for the library
1288 * @type: the type name
1289 * @value: the value to check
1290 *
1291 * Check if the given type and value are validated by
1292 * the W3C XMLSchema Datatype library.
1293 *
1294 * Returns 1 if yes, 0 if no and -1 in case of error.
1295 */
1296static int
1297xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00001298 const xmlChar *type,
1299 const xmlChar *value) {
1300 xmlSchemaTypePtr typ;
1301 int ret;
1302
1303 /*
1304 * TODO: the type should be cached ab provided back, interface subject
1305 * to changes.
1306 * TODO: handle facets, may require an additional interface and keep
1307 * the value returned from the validation.
1308 */
1309 if ((type == NULL) || (value == NULL))
1310 return(-1);
1311 typ = xmlSchemaGetPredefinedType(type,
1312 BAD_CAST "http://www.w3.org/2001/XMLSchema");
1313 if (typ == NULL)
1314 return(-1);
1315 ret = xmlSchemaValidatePredefinedType(typ, value, NULL);
1316 if (ret == 0)
1317 return(1);
1318 if (ret > 0)
1319 return(0);
1320 return(-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001321}
1322
1323/**
1324 * xmlRelaxNGSchemaTypeCompare:
1325 * @data: data needed for the library
1326 * @type: the type name
1327 * @value1: the first value
1328 * @value2: the second value
1329 *
1330 * Compare two values accordingly a type from the W3C XMLSchema
1331 * Datatype library.
1332 *
1333 * Returns 1 if yes, 0 if no and -1 in case of error.
1334 */
1335static int
1336xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
1337 const xmlChar *type ATTRIBUTE_UNUSED,
1338 const xmlChar *value1 ATTRIBUTE_UNUSED,
1339 const xmlChar *value2 ATTRIBUTE_UNUSED) {
1340 TODO
1341 return(1);
1342}
1343
1344/**
1345 * xmlRelaxNGDefaultTypeHave:
1346 * @data: data needed for the library
1347 * @type: the type name
1348 *
1349 * Check if the given type is provided by
1350 * the default datatype library.
1351 *
1352 * Returns 1 if yes, 0 if no and -1 in case of error.
1353 */
1354static int
1355xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) {
1356 if (type == NULL)
1357 return(-1);
1358 if (xmlStrEqual(type, BAD_CAST "string"))
1359 return(1);
1360 if (xmlStrEqual(type, BAD_CAST "token"))
1361 return(1);
1362 return(0);
1363}
1364
1365/**
1366 * xmlRelaxNGDefaultTypeCheck:
1367 * @data: data needed for the library
1368 * @type: the type name
1369 * @value: the value to check
1370 *
1371 * Check if the given type and value are validated by
1372 * the default datatype library.
1373 *
1374 * Returns 1 if yes, 0 if no and -1 in case of error.
1375 */
1376static int
1377xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
1378 const xmlChar *type ATTRIBUTE_UNUSED,
1379 const xmlChar *value ATTRIBUTE_UNUSED) {
1380 return(1);
1381}
1382
1383/**
1384 * xmlRelaxNGDefaultTypeCompare:
1385 * @data: data needed for the library
1386 * @type: the type name
1387 * @value1: the first value
1388 * @value2: the second value
1389 *
1390 * Compare two values accordingly a type from the default
1391 * datatype library.
1392 *
1393 * Returns 1 if yes, 0 if no and -1 in case of error.
1394 */
1395static int
1396xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
1397 const xmlChar *type ATTRIBUTE_UNUSED,
1398 const xmlChar *value1 ATTRIBUTE_UNUSED,
1399 const xmlChar *value2 ATTRIBUTE_UNUSED) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00001400 int ret = -1;
1401
1402 if (xmlStrEqual(type, BAD_CAST "string")) {
1403 ret = xmlStrEqual(value1, value2);
1404 } else if (xmlStrEqual(type, BAD_CAST "token")) {
1405 if (!xmlStrEqual(value1, value2)) {
1406 xmlChar *nval, *nvalue;
1407
1408 /*
1409 * TODO: trivial optimizations are possible by
1410 * computing at compile-time
1411 */
1412 nval = xmlRelaxNGNormalize(NULL, value1);
1413 nvalue = xmlRelaxNGNormalize(NULL, value2);
1414
1415 if ((nval == NULL) || (nvalue == NULL) ||
1416 (!xmlStrEqual(nval, nvalue)))
1417 ret = -1;
1418 if (nval != NULL)
1419 xmlFree(nval);
1420 if (nvalue != NULL)
1421 xmlFree(nvalue);
1422 }
1423 }
1424 return(ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001425}
1426
1427static int xmlRelaxNGTypeInitialized = 0;
1428static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
1429
1430/**
1431 * xmlRelaxNGFreeTypeLibrary:
1432 * @lib: the type library structure
1433 * @namespace: the URI bound to the library
1434 *
1435 * Free the structure associated to the type library
1436 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00001437static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001438xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
1439 const xmlChar *namespace ATTRIBUTE_UNUSED) {
1440 if (lib == NULL)
1441 return;
1442 if (lib->namespace != NULL)
1443 xmlFree((xmlChar *)lib->namespace);
1444 xmlFree(lib);
1445}
1446
1447/**
1448 * xmlRelaxNGRegisterTypeLibrary:
1449 * @namespace: the URI bound to the library
1450 * @data: data associated to the library
1451 * @have: the provide function
1452 * @check: the checking function
1453 * @comp: the comparison function
1454 *
1455 * Register a new type library
1456 *
1457 * Returns 0 in case of success and -1 in case of error.
1458 */
1459static int
1460xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data,
1461 xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check,
1462 xmlRelaxNGTypeCompare comp) {
1463 xmlRelaxNGTypeLibraryPtr lib;
1464 int ret;
1465
1466 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
1467 (check == NULL) || (comp == NULL))
1468 return(-1);
1469 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
1470 xmlGenericError(xmlGenericErrorContext,
1471 "Relax-NG types library '%s' already registered\n",
1472 namespace);
1473 return(-1);
1474 }
1475 lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
1476 if (lib == NULL) {
1477 xmlGenericError(xmlGenericErrorContext,
1478 "Relax-NG types library '%s' malloc() failed\n",
1479 namespace);
1480 return (-1);
1481 }
1482 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
1483 lib->namespace = xmlStrdup(namespace);
1484 lib->data = data;
1485 lib->have = have;
1486 lib->comp = comp;
1487 lib->check = check;
1488 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
1489 if (ret < 0) {
1490 xmlGenericError(xmlGenericErrorContext,
1491 "Relax-NG types library failed to register '%s'\n",
1492 namespace);
1493 xmlRelaxNGFreeTypeLibrary(lib, namespace);
1494 return(-1);
1495 }
1496 return(0);
1497}
1498
1499/**
1500 * xmlRelaxNGInitTypes:
1501 *
1502 * Initilize the default type libraries.
1503 *
1504 * Returns 0 in case of success and -1 in case of error.
1505 */
1506static int
Daniel Veillard6eadf632003-01-23 18:29:16 +00001507xmlRelaxNGInitTypes(void) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001508 if (xmlRelaxNGTypeInitialized != 0)
1509 return(0);
1510 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
1511 if (xmlRelaxNGRegisteredTypes == NULL) {
1512 xmlGenericError(xmlGenericErrorContext,
1513 "Failed to allocate sh table for Relax-NG types\n");
1514 return(-1);
1515 }
1516 xmlRelaxNGRegisterTypeLibrary(
1517 BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes",
1518 NULL,
1519 xmlRelaxNGSchemaTypeHave,
1520 xmlRelaxNGSchemaTypeCheck,
1521 xmlRelaxNGSchemaTypeCompare);
1522 xmlRelaxNGRegisterTypeLibrary(
1523 xmlRelaxNGNs,
1524 NULL,
1525 xmlRelaxNGDefaultTypeHave,
1526 xmlRelaxNGDefaultTypeCheck,
1527 xmlRelaxNGDefaultTypeCompare);
1528 xmlRelaxNGTypeInitialized = 1;
1529 return(0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001530}
1531
1532/**
1533 * xmlRelaxNGCleanupTypes:
1534 *
1535 * Cleanup the default Schemas type library associated to RelaxNG
1536 */
1537void
1538xmlRelaxNGCleanupTypes(void) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001539 if (xmlRelaxNGTypeInitialized == 0)
1540 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001541 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001542 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
1543 xmlRelaxNGFreeTypeLibrary);
1544 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001545}
1546
1547/************************************************************************
1548 * *
1549 * Parsing functions *
1550 * *
1551 ************************************************************************/
1552
1553static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(
1554 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
1555static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(
1556 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
1557static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(
Daniel Veillard154877e2003-01-30 12:17:05 +00001558 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00001559static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(
1560 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001561static xmlRelaxNGPtr xmlRelaxNGParseDocument(
1562 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001563
1564
1565#define IS_BLANK_NODE(n) \
1566 (((n)->type == XML_TEXT_NODE) && (xmlRelaxNGIsBlank((n)->content)))
1567
1568/**
1569 * xmlRelaxNGIsBlank:
1570 * @str: a string
1571 *
1572 * Check if a string is ignorable c.f. 4.2. Whitespace
1573 *
1574 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
1575 */
1576static int
1577xmlRelaxNGIsBlank(xmlChar *str) {
1578 if (str == NULL)
1579 return(1);
1580 while (*str != 0) {
1581 if (!(IS_BLANK(*str))) return(0);
1582 str++;
1583 }
1584 return(1);
1585}
1586
Daniel Veillard6eadf632003-01-23 18:29:16 +00001587/**
1588 * xmlRelaxNGGetDataTypeLibrary:
1589 * @ctxt: a Relax-NG parser context
1590 * @node: the current data or value element
1591 *
1592 * Applies algorithm from 4.3. datatypeLibrary attribute
1593 *
1594 * Returns the datatypeLibary value or NULL if not found
1595 */
1596static xmlChar *
1597xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
1598 xmlNodePtr node) {
1599 xmlChar *ret, *escape;
1600
Daniel Veillard6eadf632003-01-23 18:29:16 +00001601 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
1602 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
1603 if (ret != NULL) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001604 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
Daniel Veillard6eadf632003-01-23 18:29:16 +00001605 if (escape == NULL) {
1606 return(ret);
1607 }
1608 xmlFree(ret);
1609 return(escape);
1610 }
1611 }
1612 node = node->parent;
1613 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
1614 if (IS_RELAXNG(node, "element")) {
1615 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
1616 if (ret != NULL) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001617 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
Daniel Veillard6eadf632003-01-23 18:29:16 +00001618 if (escape == NULL) {
1619 return(ret);
1620 }
1621 xmlFree(ret);
1622 return(escape);
1623 }
1624 }
1625 node = node->parent;
1626 }
1627 return(NULL);
1628}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001629
1630/**
Daniel Veillardedc91922003-01-26 00:52:04 +00001631 * xmlRelaxNGParseValue:
1632 * @ctxt: a Relax-NG parser context
1633 * @node: the data node.
1634 *
1635 * parse the content of a RelaxNG value node.
1636 *
1637 * Returns the definition pointer or NULL in case of error
1638 */
1639static xmlRelaxNGDefinePtr
1640xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
1641 xmlRelaxNGDefinePtr def = NULL;
1642 xmlRelaxNGTypeLibraryPtr lib;
1643 xmlChar *type;
1644 xmlChar *library;
1645 int tmp;
1646
1647 def = xmlRelaxNGNewDefine(ctxt, node);
1648 if (def == NULL)
1649 return(NULL);
1650 def->type = XML_RELAXNG_VALUE;
1651
1652 type = xmlGetProp(node, BAD_CAST "type");
1653 if (type != NULL) {
1654 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
1655 if (library == NULL)
1656 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
1657
1658 def->name = type;
1659 def->ns = library;
1660
1661 lib = (xmlRelaxNGTypeLibraryPtr)
1662 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
1663 if (lib == NULL) {
1664 if (ctxt->error != NULL)
1665 ctxt->error(ctxt->userData,
1666 "Use of unregistered type library '%s'\n",
1667 library);
1668 ctxt->nbErrors++;
1669 def->data = NULL;
1670 } else {
1671 def->data = lib;
1672 if (lib->have == NULL) {
1673 ctxt->error(ctxt->userData,
1674 "Internal error with type library '%s': no 'have'\n",
1675 library);
1676 ctxt->nbErrors++;
1677 } else {
1678 tmp = lib->have(lib->data, def->name);
1679 if (tmp != 1) {
1680 ctxt->error(ctxt->userData,
1681 "Error type '%s' is not exported by type library '%s'\n",
1682 def->name, library);
1683 ctxt->nbErrors++;
1684 }
1685 }
1686 }
1687 }
1688 if (node->children == NULL) {
1689 if (ctxt->error != NULL)
1690 ctxt->error(ctxt->userData,
1691 "Element <value> has no content\n");
1692 ctxt->nbErrors++;
1693 } else if ((node->children->type != XML_TEXT_NODE) ||
1694 (node->children->next != NULL)) {
1695 if (ctxt->error != NULL)
1696 ctxt->error(ctxt->userData,
1697 "Expecting a single text value for <value>content\n");
1698 ctxt->nbErrors++;
1699 } else {
1700 def->value = xmlNodeGetContent(node);
1701 if (def->value == NULL) {
1702 if (ctxt->error != NULL)
1703 ctxt->error(ctxt->userData,
1704 "Element <value> has no content\n");
1705 ctxt->nbErrors++;
1706 }
1707 }
1708 /* TODO check ahead of time that the value is okay per the type */
1709 return(def);
1710}
1711
1712/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00001713 * xmlRelaxNGParseData:
1714 * @ctxt: a Relax-NG parser context
1715 * @node: the data node.
1716 *
1717 * parse the content of a RelaxNG data node.
1718 *
1719 * Returns the definition pointer or NULL in case of error
1720 */
1721static xmlRelaxNGDefinePtr
1722xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
1723 xmlRelaxNGDefinePtr def = NULL;
1724 xmlRelaxNGTypeLibraryPtr lib;
1725 xmlChar *type;
1726 xmlChar *library;
1727 xmlNodePtr content;
1728 int tmp;
1729
1730 type = xmlGetProp(node, BAD_CAST "type");
1731 if (type == NULL) {
1732 if (ctxt->error != NULL)
1733 ctxt->error(ctxt->userData,
1734 "data has no type\n");
1735 ctxt->nbErrors++;
1736 return(NULL);
1737 }
1738 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
1739 if (library == NULL)
1740 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
1741
1742 def = xmlRelaxNGNewDefine(ctxt, node);
1743 if (def == NULL) {
1744 xmlFree(type);
1745 return(NULL);
1746 }
1747 def->type = XML_RELAXNG_DATATYPE;
1748 def->name = type;
1749 def->ns = library;
1750
1751 lib = (xmlRelaxNGTypeLibraryPtr)
1752 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
1753 if (lib == NULL) {
1754 if (ctxt->error != NULL)
1755 ctxt->error(ctxt->userData,
1756 "Use of unregistered type library '%s'\n",
1757 library);
1758 ctxt->nbErrors++;
1759 def->data = NULL;
1760 } else {
1761 def->data = lib;
1762 if (lib->have == NULL) {
1763 ctxt->error(ctxt->userData,
1764 "Internal error with type library '%s': no 'have'\n",
1765 library);
1766 ctxt->nbErrors++;
1767 } else {
1768 tmp = lib->have(lib->data, def->name);
1769 if (tmp != 1) {
1770 ctxt->error(ctxt->userData,
1771 "Error type '%s' is not exported by type library '%s'\n",
1772 def->name, library);
1773 ctxt->nbErrors++;
1774 }
1775 }
1776 }
1777 content = node->children;
1778 while (content != NULL) {
1779 TODO
1780 content = content->next;
1781 }
1782
1783 return(def);
1784}
1785
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00001786/**
1787 * xmlRelaxNGCompareElemDefLists:
1788 * @ctxt: a Relax-NG parser context
1789 * @defs1: the first list of element defs
1790 * @defs2: the second list of element defs
1791 *
1792 * Compare the 2 lists of element definitions. The comparison is
1793 * that if both lists do not accept the same QNames, it returns 1
1794 * If the 2 lists can accept the same QName the comparison returns 0
1795 *
1796 * Returns 1 disttinct, 0 if equal
1797 */
1798static int
1799xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
1800 xmlRelaxNGDefinePtr *def1,
1801 xmlRelaxNGDefinePtr *def2) {
1802 xmlRelaxNGDefinePtr *basedef2 = def2;
1803
Daniel Veillard154877e2003-01-30 12:17:05 +00001804 if ((def1 == NULL) || (def2 == NULL))
1805 return(1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00001806 if ((*def1 == NULL) || (*def2 == NULL))
1807 return(1);
1808 while (*def1 != NULL) {
1809 while ((*def2) != NULL) {
1810 if ((*def1)->name == NULL) {
1811 if (xmlStrEqual((*def2)->ns, (*def1)->ns))
1812 return(0);
1813 } else if ((*def2)->name == NULL) {
1814 if (xmlStrEqual((*def2)->ns, (*def1)->ns))
1815 return(0);
1816 } else if (xmlStrEqual((*def1)->name, (*def2)->name)) {
1817 if (xmlStrEqual((*def2)->ns, (*def1)->ns))
1818 return(0);
1819 }
1820 def2++;
1821 }
1822 def2 = basedef2;
1823 def1++;
1824 }
1825 return(1);
1826}
1827
1828/**
1829 * xmlRelaxNGGetElements:
1830 * @ctxt: a Relax-NG parser context
1831 * @def: the interleave definition
1832 *
1833 * Compute the list of top elements a definition can generate
1834 *
1835 * Returns a list of elements or NULL if none was found.
1836 */
1837static xmlRelaxNGDefinePtr *
1838xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
1839 xmlRelaxNGDefinePtr def) {
1840 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
1841 int len = 0;
1842 int max = 0;
1843
1844 parent = NULL;
1845 cur = def;
1846 while (cur != NULL) {
Daniel Veillardb08c9812003-01-28 23:09:49 +00001847 if ((cur->type == XML_RELAXNG_ELEMENT) ||
1848 (cur->type == XML_RELAXNG_TEXT)) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00001849 if (ret == NULL) {
1850 max = 10;
1851 ret = (xmlRelaxNGDefinePtr *)
1852 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
1853 if (ret == NULL) {
1854 if (ctxt->error != NULL)
1855 ctxt->error(ctxt->userData,
1856 "Out of memory in element search\n");
1857 ctxt->nbErrors++;
1858 return(NULL);
1859 }
1860 } else if (max <= len) {
1861 max *= 2;
1862 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr));
1863 if (ret == NULL) {
1864 if (ctxt->error != NULL)
1865 ctxt->error(ctxt->userData,
1866 "Out of memory in element search\n");
1867 ctxt->nbErrors++;
1868 return(NULL);
1869 }
1870 }
Daniel Veillardb08c9812003-01-28 23:09:49 +00001871 ret[len++] = cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00001872 ret[len] = NULL;
1873 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
1874 (cur->type == XML_RELAXNG_INTERLEAVE) ||
1875 (cur->type == XML_RELAXNG_GROUP) ||
1876 (cur->type == XML_RELAXNG_ONEORMORE) ||
Daniel Veillardb08c9812003-01-28 23:09:49 +00001877 (cur->type == XML_RELAXNG_ZEROORMORE) ||
1878 (cur->type == XML_RELAXNG_OPTIONAL) ||
1879 (cur->type == XML_RELAXNG_REF) ||
1880 (cur->type == XML_RELAXNG_DEF)) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00001881 /*
1882 * Don't go within elements or attributes or string values.
1883 * Just gather the element top list
1884 */
1885 if (cur->content != NULL) {
1886 parent = cur;
1887 cur = cur->content;
1888 tmp = cur;
1889 while (tmp != NULL) {
1890 tmp->parent = parent;
1891 tmp = tmp->next;
1892 }
1893 continue;
1894 }
1895 }
Daniel Veillard154877e2003-01-30 12:17:05 +00001896 if (cur == def)
1897 return(ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00001898 if (cur->next != NULL) {
1899 cur = cur->next;
1900 continue;
1901 }
1902 do {
1903 cur = cur->parent;
1904 if (cur == NULL) break;
1905 if (cur == def) return(ret);
1906 if (cur->next != NULL) {
1907 cur = cur->next;
1908 break;
1909 }
1910 } while (cur != NULL);
1911 }
1912 return(ret);
1913}
1914
1915/**
1916 * xmlRelaxNGComputeInterleaves:
1917 * @def: the interleave definition
1918 * @ctxt: a Relax-NG parser context
1919 * @node: the data node.
1920 *
1921 * A lot of work for preprocessing interleave definitions
1922 * is potentially needed to get a decent execution speed at runtime
1923 * - trying to get a total order on the element nodes generated
1924 * by the interleaves, order the list of interleave definitions
1925 * following that order.
1926 * - if <text/> is used to handle mixed content, it is better to
1927 * flag this in the define and simplify the runtime checking
1928 * algorithm
1929 */
1930static void
1931xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
1932 xmlRelaxNGParserCtxtPtr ctxt,
1933 xmlChar *name ATTRIBUTE_UNUSED) {
1934 xmlRelaxNGDefinePtr cur;
1935
1936 xmlRelaxNGDefinePtr *list = NULL;
1937 xmlRelaxNGPartitionPtr partitions = NULL;
1938 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
1939 xmlRelaxNGInterleaveGroupPtr group;
1940 int i,j,ret;
1941 int nbgroups = 0;
1942 int nbchild = 0;
1943
1944#ifdef DEBUG_INTERLEAVE
1945 xmlGenericError(xmlGenericErrorContext,
1946 "xmlRelaxNGComputeInterleaves(%s)\n",
1947 name);
1948#endif
1949 cur = def->content;
1950 while (cur != NULL) {
1951 nbchild++;
1952 cur = cur->next;
1953 }
1954
1955#ifdef DEBUG_INTERLEAVE
1956 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
1957#endif
1958 groups = (xmlRelaxNGInterleaveGroupPtr *)
1959 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
1960 if (groups == NULL)
1961 goto error;
1962 cur = def->content;
1963 while (cur != NULL) {
Daniel Veillard154877e2003-01-30 12:17:05 +00001964 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
1965 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
1966 if (groups[nbgroups] == NULL)
1967 goto error;
1968 groups[nbgroups]->rule = cur;
1969 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur);
1970 nbgroups++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00001971 cur = cur->next;
1972 }
1973 list = NULL;
1974#ifdef DEBUG_INTERLEAVE
1975 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
1976#endif
1977
1978 /*
1979 * Let's check that all rules makes a partitions according to 7.4
1980 */
1981 partitions = (xmlRelaxNGPartitionPtr)
1982 xmlMalloc(sizeof(xmlRelaxNGPartition));
1983 if (partitions == NULL)
1984 goto error;
1985 partitions->nbgroups = nbgroups;
1986 for (i = 0;i < nbgroups;i++) {
1987 group = groups[i];
1988 for (j = i+1;j < nbgroups;j++) {
1989 if (groups[j] == NULL)
1990 continue;
1991 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
1992 groups[j]->defs);
1993 if (ret == 0) {
1994 if (ctxt->error != NULL)
1995 ctxt->error(ctxt->userData,
1996 "Element or text conflicts in interleave\n");
1997 ctxt->nbErrors++;
1998 }
1999 }
2000 }
2001 partitions->groups = groups;
2002
2003 /*
2004 * Free Up the child list, and save the partition list back in the def
2005 */
2006 def->data = partitions;
2007 return;
2008
2009error:
2010 if (ctxt->error != NULL)
2011 ctxt->error(ctxt->userData,
2012 "Out of memory in interleave computation\n");
2013 ctxt->nbErrors++;
2014 if (list == NULL)
2015 xmlFree(list);
2016 if (groups != NULL) {
2017 for (i = 0;i < nbgroups;i++)
2018 if (groups[i] != NULL) {
2019 if (groups[i]->defs != NULL)
2020 xmlFree(groups[i]->defs);
2021 xmlFree(groups[i]);
2022 }
2023 xmlFree(groups);
2024 }
2025 xmlRelaxNGFreePartition(partitions);
2026}
2027
2028/**
2029 * xmlRelaxNGParseInterleave:
2030 * @ctxt: a Relax-NG parser context
2031 * @node: the data node.
2032 *
2033 * parse the content of a RelaxNG interleave node.
2034 *
2035 * Returns the definition pointer or NULL in case of error
2036 */
2037static xmlRelaxNGDefinePtr
2038xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
2039 xmlRelaxNGDefinePtr def = NULL;
2040 xmlRelaxNGDefinePtr last = NULL, cur;
2041 xmlNodePtr child;
2042
2043 def = xmlRelaxNGNewDefine(ctxt, node);
2044 if (def == NULL) {
2045 return(NULL);
2046 }
2047 def->type = XML_RELAXNG_INTERLEAVE;
2048
2049 if (ctxt->interleaves == NULL)
2050 ctxt->interleaves = xmlHashCreate(10);
2051 if (ctxt->interleaves == NULL) {
2052 if (ctxt->error != NULL)
2053 ctxt->error(ctxt->userData,
2054 "Failed to create interleaves hash table\n");
2055 ctxt->nbErrors++;
2056 } else {
2057 char name[32];
2058
2059 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
2060 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
2061 if (ctxt->error != NULL)
2062 ctxt->error(ctxt->userData,
2063 "Failed to add %s to hash table\n", name);
2064 ctxt->nbErrors++;
2065 }
2066 }
2067 child = node->children;
2068 while (child != NULL) {
2069 if (IS_RELAXNG(child, "element")) {
2070 cur = xmlRelaxNGParseElement(ctxt, child);
2071 } else {
2072 cur = xmlRelaxNGParsePattern(ctxt, child);
2073 }
2074 if (cur != NULL) {
2075 cur->parent = def;
2076 if (last == NULL) {
2077 def->content = last = cur;
2078 } else {
2079 last->next = cur;
2080 last = cur;
2081 }
2082 }
2083 child = child->next;
2084 }
2085
2086 return(def);
2087}
Daniel Veillard6eadf632003-01-23 18:29:16 +00002088
2089/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00002090 * xmlRelaxNGParseDefine:
2091 * @ctxt: a Relax-NG parser context
2092 * @node: the define node
2093 *
2094 * parse the content of a RelaxNG define element node.
2095 *
2096 * Returns the definition pointer or NULL in case of error.
2097 */
2098static int
2099xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
2100 xmlChar *name;
2101 int ret = 0, tmp;
2102 xmlRelaxNGDefinePtr def;
2103 const xmlChar *olddefine;
2104
2105 name = xmlGetProp(node, BAD_CAST "name");
2106 if (name == NULL) {
2107 if (ctxt->error != NULL)
2108 ctxt->error(ctxt->userData,
2109 "define has no name\n");
2110 ctxt->nbErrors++;
2111 } else {
2112 def = xmlRelaxNGNewDefine(ctxt, node);
2113 if (def == NULL) {
2114 xmlFree(name);
2115 return(-1);
2116 }
2117 def->type = XML_RELAXNG_DEF;
2118 def->name = name;
2119 if (node->children == NULL) {
2120 if (ctxt->error != NULL)
2121 ctxt->error(ctxt->userData,
2122 "define has no children\n");
2123 ctxt->nbErrors++;
2124 } else {
2125 olddefine = ctxt->define;
2126 ctxt->define = name;
Daniel Veillard154877e2003-01-30 12:17:05 +00002127 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
Daniel Veillard276be4a2003-01-24 01:03:34 +00002128 ctxt->define = olddefine;
2129 }
2130 if (ctxt->grammar->defs == NULL)
2131 ctxt->grammar->defs = xmlHashCreate(10);
2132 if (ctxt->grammar->defs == NULL) {
2133 if (ctxt->error != NULL)
2134 ctxt->error(ctxt->userData,
2135 "Could not create definition hash\n");
2136 ctxt->nbErrors++;
2137 ret = -1;
2138 xmlRelaxNGFreeDefine(def);
2139 } else {
2140 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
2141 if (tmp < 0) {
Daniel Veillard154877e2003-01-30 12:17:05 +00002142 xmlRelaxNGDefinePtr prev;
2143
2144 prev = xmlHashLookup(ctxt->grammar->defs, name);
2145 if (prev == NULL) {
2146 if (ctxt->error != NULL)
2147 ctxt->error(ctxt->userData,
2148 "Internal error on define aggregation of %s\n",
2149 name);
2150 ctxt->nbErrors++;
2151 ret = -1;
2152 xmlRelaxNGFreeDefine(def);
2153 } else {
2154 while (prev->nextHash != NULL)
2155 prev = prev->nextHash;
2156 prev->nextHash = def;
2157 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00002158 }
2159 }
2160 }
2161 return(ret);
2162}
2163
2164/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00002165 * xmlRelaxNGParsePattern:
2166 * @ctxt: a Relax-NG parser context
2167 * @node: the pattern node.
2168 *
2169 * parse the content of a RelaxNG pattern node.
2170 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00002171 * Returns the definition pointer or NULL in case of error or if no
2172 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00002173 */
2174static xmlRelaxNGDefinePtr
2175xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
2176 xmlRelaxNGDefinePtr def = NULL;
2177
2178 if (IS_RELAXNG(node, "element")) {
2179 def = xmlRelaxNGParseElement(ctxt, node);
2180 } else if (IS_RELAXNG(node, "attribute")) {
2181 def = xmlRelaxNGParseAttribute(ctxt, node);
2182 } else if (IS_RELAXNG(node, "empty")) {
2183 def = xmlRelaxNGNewDefine(ctxt, node);
2184 if (def == NULL)
2185 return(NULL);
2186 def->type = XML_RELAXNG_EMPTY;
2187 } else if (IS_RELAXNG(node, "text")) {
2188 def = xmlRelaxNGNewDefine(ctxt, node);
2189 if (def == NULL)
2190 return(NULL);
2191 def->type = XML_RELAXNG_TEXT;
2192 if (node->children != NULL) {
2193 if (ctxt->error != NULL)
2194 ctxt->error(ctxt->userData, "text: had a child node\n");
2195 ctxt->nbErrors++;
2196 }
2197 } else if (IS_RELAXNG(node, "zeroOrMore")) {
2198 def = xmlRelaxNGNewDefine(ctxt, node);
2199 if (def == NULL)
2200 return(NULL);
2201 def->type = XML_RELAXNG_ZEROORMORE;
Daniel Veillard154877e2003-01-30 12:17:05 +00002202 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002203 } else if (IS_RELAXNG(node, "oneOrMore")) {
2204 def = xmlRelaxNGNewDefine(ctxt, node);
2205 if (def == NULL)
2206 return(NULL);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002207 def->type = XML_RELAXNG_ONEORMORE;
Daniel Veillard154877e2003-01-30 12:17:05 +00002208 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002209 } else if (IS_RELAXNG(node, "optional")) {
2210 def = xmlRelaxNGNewDefine(ctxt, node);
2211 if (def == NULL)
2212 return(NULL);
2213 def->type = XML_RELAXNG_OPTIONAL;
Daniel Veillard154877e2003-01-30 12:17:05 +00002214 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002215 } else if (IS_RELAXNG(node, "choice")) {
2216 def = xmlRelaxNGNewDefine(ctxt, node);
2217 if (def == NULL)
2218 return(NULL);
2219 def->type = XML_RELAXNG_CHOICE;
Daniel Veillard154877e2003-01-30 12:17:05 +00002220 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002221 } else if (IS_RELAXNG(node, "group")) {
2222 def = xmlRelaxNGNewDefine(ctxt, node);
2223 if (def == NULL)
2224 return(NULL);
2225 def->type = XML_RELAXNG_GROUP;
Daniel Veillard154877e2003-01-30 12:17:05 +00002226 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002227 } else if (IS_RELAXNG(node, "ref")) {
2228 def = xmlRelaxNGNewDefine(ctxt, node);
2229 if (def == NULL)
2230 return(NULL);
2231 def->type = XML_RELAXNG_REF;
2232 def->name = xmlGetProp(node, BAD_CAST "name");
2233 if (def->name == NULL) {
2234 if (ctxt->error != NULL)
2235 ctxt->error(ctxt->userData,
2236 "ref has no name\n");
2237 ctxt->nbErrors++;
Daniel Veillard276be4a2003-01-24 01:03:34 +00002238 } else {
2239 if ((ctxt->define != NULL) &&
2240 (xmlStrEqual(ctxt->define, def->name))) {
2241 if (ctxt->error != NULL)
2242 ctxt->error(ctxt->userData,
2243 "Recursive reference to %s not in an element\n",
2244 def->name);
2245 ctxt->nbErrors++;
2246 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00002247 }
2248 if (node->children != NULL) {
2249 if (ctxt->error != NULL)
2250 ctxt->error(ctxt->userData,
2251 "ref is not empty\n");
2252 ctxt->nbErrors++;
2253 }
2254 if (ctxt->grammar->refs == NULL)
2255 ctxt->grammar->refs = xmlHashCreate(10);
2256 if (ctxt->grammar->refs == NULL) {
2257 if (ctxt->error != NULL)
2258 ctxt->error(ctxt->userData,
2259 "Could not create references hash\n");
2260 ctxt->nbErrors++;
2261 xmlRelaxNGFreeDefine(def);
2262 def = NULL;
2263 } else {
2264 int tmp;
2265
2266 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
2267 if (tmp < 0) {
2268 xmlRelaxNGDefinePtr prev;
2269
2270 prev = (xmlRelaxNGDefinePtr)
2271 xmlHashLookup(ctxt->grammar->refs, def->name);
2272 if (prev == NULL) {
2273 if (ctxt->error != NULL)
2274 ctxt->error(ctxt->userData,
2275 "Internal error refs definitions '%s'\n",
2276 def->name);
2277 ctxt->nbErrors++;
2278 xmlRelaxNGFreeDefine(def);
2279 def = NULL;
2280 } else {
2281 def->nextHash = prev->nextHash;
2282 prev->nextHash = def;
2283 }
2284 }
2285 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002286 } else if (IS_RELAXNG(node, "data")) {
2287 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillard276be4a2003-01-24 01:03:34 +00002288 } else if (IS_RELAXNG(node, "define")) {
2289 xmlRelaxNGParseDefine(ctxt, node);
2290 def = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00002291 } else if (IS_RELAXNG(node, "value")) {
2292 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002293 } else if (IS_RELAXNG(node, "list")) {
2294 def = xmlRelaxNGNewDefine(ctxt, node);
2295 if (def == NULL)
2296 return(NULL);
2297 def->type = XML_RELAXNG_LIST;
Daniel Veillard154877e2003-01-30 12:17:05 +00002298 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002299 } else if (IS_RELAXNG(node, "interleave")) {
2300 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002301 } else if (IS_RELAXNG(node, "externalRef")) {
2302 xmlRelaxNGDocumentPtr docu;
2303 xmlNodePtr root;
2304
2305 docu = node->_private;
2306 if (docu != NULL) {
2307 def = xmlRelaxNGNewDefine(ctxt, node);
2308 if (def == NULL)
2309 return(NULL);
2310 def->type = XML_RELAXNG_EXTERNALREF;
2311
2312 if (docu->content == NULL) {
2313 /*
2314 * Then do the parsing for good
2315 */
2316 root = xmlDocGetRootElement(docu->doc);
2317 if (root == NULL) {
2318 if (ctxt->error != NULL)
2319 ctxt->error(ctxt->userData,
2320 "xmlRelaxNGParse: %s is empty\n",
2321 ctxt->URL);
2322 ctxt->nbErrors++;
2323 return (NULL);
2324 }
2325 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
2326 if ((docu->schema != NULL) &&
2327 (docu->schema->topgrammar != NULL)) {
2328 docu->content = docu->schema->topgrammar->start;
2329 }
2330 }
2331 def->content = docu->content;
2332 } else {
2333 def = NULL;
2334 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00002335 } else {
2336 TODO
2337 }
2338 return(def);
2339}
2340
2341/**
2342 * xmlRelaxNGParseAttribute:
2343 * @ctxt: a Relax-NG parser context
2344 * @node: the element node
2345 *
2346 * parse the content of a RelaxNG attribute node.
2347 *
2348 * Returns the definition pointer or NULL in case of error.
2349 */
2350static xmlRelaxNGDefinePtr
2351xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
2352 xmlRelaxNGDefinePtr ret, cur, last;
2353 xmlNodePtr child;
2354 xmlChar *val;
2355 int old_flags;
2356
2357 ret = xmlRelaxNGNewDefine(ctxt, node);
2358 if (ret == NULL)
2359 return(NULL);
2360 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002361 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002362 child = node->children;
2363 if (child == NULL) {
2364 if (ctxt->error != NULL)
2365 ctxt->error(ctxt->userData,
2366 "xmlRelaxNGParseattribute: attribute has no children\n");
2367 ctxt->nbErrors++;
2368 return(ret);
2369 }
2370 old_flags = ctxt->flags;
2371 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
2372 if (IS_RELAXNG(child, "name")) {
2373 val = xmlNodeGetContent(child);
2374 ret->name = val;
2375 val = xmlGetProp(child, BAD_CAST "ns");
2376 ret->ns = val;
2377 } else if (IS_RELAXNG(child, "anyName")) {
2378 TODO
2379 } else if (IS_RELAXNG(child, "nsName")) {
2380 TODO
2381 } else if (IS_RELAXNG(child, "choice")) {
2382 TODO
2383 } else {
2384 if (ctxt->error != NULL)
2385 ctxt->error(ctxt->userData,
2386 "element: expecting name, anyName, nsName or choice : got %s\n",
2387 child->name);
2388 ctxt->nbErrors++;
2389 ctxt->flags = old_flags;
2390 return(ret);
2391 }
2392 child = child->next;
2393 last = NULL;
2394 while (child != NULL) {
2395 cur = xmlRelaxNGParsePattern(ctxt, child);
2396 if (cur != NULL) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002397 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002398 switch (cur->type) {
2399 case XML_RELAXNG_EMPTY:
2400 case XML_RELAXNG_NOT_ALLOWED:
2401 case XML_RELAXNG_TEXT:
2402 case XML_RELAXNG_ELEMENT:
2403 case XML_RELAXNG_DATATYPE:
2404 case XML_RELAXNG_VALUE:
2405 case XML_RELAXNG_LIST:
2406 case XML_RELAXNG_REF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002407 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00002408 case XML_RELAXNG_DEF:
2409 case XML_RELAXNG_ONEORMORE:
2410 case XML_RELAXNG_ZEROORMORE:
2411 case XML_RELAXNG_OPTIONAL:
2412 case XML_RELAXNG_CHOICE:
2413 case XML_RELAXNG_GROUP:
2414 case XML_RELAXNG_INTERLEAVE:
2415 if (last == NULL) {
2416 ret->content = last = cur;
2417 } else {
2418 if ((last->type == XML_RELAXNG_ELEMENT) &&
2419 (ret->content == last)) {
2420 ret->content = xmlRelaxNGNewDefine(ctxt, node);
2421 if (ret->content != NULL) {
2422 ret->content->type = XML_RELAXNG_GROUP;
2423 ret->content->content = last;
2424 } else {
2425 ret->content = last;
2426 }
2427 }
2428 last->next = cur;
2429 last = cur;
2430 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002431 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002432 break;
2433 case XML_RELAXNG_ATTRIBUTE:
2434 cur->next = ret->attrs;
2435 ret->attrs = cur;
2436 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002437 case XML_RELAXNG_START:
2438 TODO
2439 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002440 }
2441 }
2442 child = child->next;
2443 }
2444 ctxt->flags = old_flags;
2445 return(ret);
2446}
2447
2448/**
2449 * xmlRelaxNGParseElement:
2450 * @ctxt: a Relax-NG parser context
2451 * @node: the element node
2452 *
2453 * parse the content of a RelaxNG element node.
2454 *
2455 * Returns the definition pointer or NULL in case of error.
2456 */
2457static xmlRelaxNGDefinePtr
2458xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
2459 xmlRelaxNGDefinePtr ret, cur, last;
2460 xmlNodePtr child;
2461 xmlChar *val;
Daniel Veillard276be4a2003-01-24 01:03:34 +00002462 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002463
2464 ret = xmlRelaxNGNewDefine(ctxt, node);
2465 if (ret == NULL)
2466 return(NULL);
2467 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002468 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002469 child = node->children;
2470 if (child == NULL) {
2471 if (ctxt->error != NULL)
2472 ctxt->error(ctxt->userData,
2473 "xmlRelaxNGParseElement: element has no children\n");
2474 ctxt->nbErrors++;
2475 return(ret);
2476 }
2477 if (IS_RELAXNG(child, "name")) {
2478 val = xmlNodeGetContent(child);
2479 ret->name = val;
2480 val = xmlGetProp(child, BAD_CAST "ns");
2481 ret->ns = val;
2482 } else if (IS_RELAXNG(child, "anyName")) {
2483 TODO
2484 } else if (IS_RELAXNG(child, "nsName")) {
2485 TODO
2486 } else if (IS_RELAXNG(child, "choice")) {
2487 TODO
2488 } else {
2489 if (ctxt->error != NULL)
2490 ctxt->error(ctxt->userData,
2491 "element: expecting name, anyName, nsName or choice : got %s\n",
2492 child->name);
2493 ctxt->nbErrors++;
2494 return(ret);
2495 }
2496 child = child->next;
2497 if (child == NULL) {
2498 if (ctxt->error != NULL)
2499 ctxt->error(ctxt->userData,
2500 "xmlRelaxNGParseElement: element has no content\n");
2501 ctxt->nbErrors++;
2502 return(ret);
2503 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00002504 olddefine = ctxt->define;
2505 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002506 last = NULL;
2507 while (child != NULL) {
2508 cur = xmlRelaxNGParsePattern(ctxt, child);
2509 if (cur != NULL) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002510 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002511 switch (cur->type) {
2512 case XML_RELAXNG_EMPTY:
2513 case XML_RELAXNG_NOT_ALLOWED:
2514 case XML_RELAXNG_TEXT:
2515 case XML_RELAXNG_ELEMENT:
2516 case XML_RELAXNG_DATATYPE:
2517 case XML_RELAXNG_VALUE:
2518 case XML_RELAXNG_LIST:
2519 case XML_RELAXNG_REF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002520 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00002521 case XML_RELAXNG_DEF:
2522 case XML_RELAXNG_ZEROORMORE:
2523 case XML_RELAXNG_ONEORMORE:
2524 case XML_RELAXNG_OPTIONAL:
2525 case XML_RELAXNG_CHOICE:
2526 case XML_RELAXNG_GROUP:
2527 case XML_RELAXNG_INTERLEAVE:
2528 if (last == NULL) {
2529 ret->content = last = cur;
2530 } else {
2531 if ((last->type == XML_RELAXNG_ELEMENT) &&
2532 (ret->content == last)) {
2533 ret->content = xmlRelaxNGNewDefine(ctxt, node);
2534 if (ret->content != NULL) {
2535 ret->content->type = XML_RELAXNG_GROUP;
2536 ret->content->content = last;
2537 } else {
2538 ret->content = last;
2539 }
2540 }
2541 last->next = cur;
2542 last = cur;
2543 }
2544 break;
2545 case XML_RELAXNG_ATTRIBUTE:
2546 cur->next = ret->attrs;
2547 ret->attrs = cur;
2548 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002549 case XML_RELAXNG_START:
2550 TODO
2551 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002552 }
2553 }
2554 child = child->next;
2555 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00002556 ctxt->define = olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002557 return(ret);
2558}
2559
2560/**
2561 * xmlRelaxNGParsePatterns:
2562 * @ctxt: a Relax-NG parser context
2563 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00002564 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00002565 *
2566 * parse the content of a RelaxNG start node.
2567 *
2568 * Returns the definition pointer or NULL in case of error.
2569 */
2570static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00002571xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
2572 int group) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002573 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002574
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002575 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002576 while (nodes != NULL) {
2577 if (IS_RELAXNG(nodes, "element")) {
2578 cur = xmlRelaxNGParseElement(ctxt, nodes);
2579 if (def == NULL) {
2580 def = last = cur;
2581 } else {
Daniel Veillard154877e2003-01-30 12:17:05 +00002582 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
2583 (def == last)) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00002584 def = xmlRelaxNGNewDefine(ctxt, nodes);
2585 def->type = XML_RELAXNG_GROUP;
2586 def->content = last;
2587 }
2588 last->next = cur;
2589 last = cur;
2590 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002591 cur->parent = parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002592 } else {
2593 cur = xmlRelaxNGParsePattern(ctxt, nodes);
2594 if (def == NULL) {
2595 def = last = cur;
2596 } else {
2597 last->next = cur;
2598 last = cur;
2599 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002600 cur->parent = parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002601 }
2602 nodes = nodes->next;
2603 }
2604 return(def);
2605}
2606
2607/**
2608 * xmlRelaxNGParseStart:
2609 * @ctxt: a Relax-NG parser context
2610 * @nodes: start children nodes
2611 *
2612 * parse the content of a RelaxNG start node.
2613 *
2614 * Returns 0 in case of success, -1 in case of error
2615 */
2616static int
2617xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
2618 int ret = 0;
2619 xmlRelaxNGDefinePtr def = NULL;
2620
2621 while (nodes != NULL) {
2622 if (IS_RELAXNG(nodes, "empty")) {
2623 TODO
2624 xmlElemDump(stdout, nodes->doc, nodes);
2625 } else if (IS_RELAXNG(nodes, "notAllowed")) {
2626 TODO
2627 xmlElemDump(stdout, nodes->doc, nodes);
2628 } else {
Daniel Veillard154877e2003-01-30 12:17:05 +00002629 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002630 ctxt->grammar->start = def;
2631 }
2632 nodes = nodes->next;
2633 }
2634 return(ret);
2635}
2636
2637/**
2638 * xmlRelaxNGParseGrammarContent:
2639 * @ctxt: a Relax-NG parser context
2640 * @nodes: grammar children nodes
2641 *
2642 * parse the content of a RelaxNG grammar node.
2643 *
2644 * Returns 0 in case of success, -1 in case of error
2645 */
2646static int
2647xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt
2648 ATTRIBUTE_UNUSED, xmlNodePtr nodes)
2649{
Daniel Veillard276be4a2003-01-24 01:03:34 +00002650 int ret = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002651
2652 if (nodes == NULL) {
2653 if (ctxt->error != NULL)
2654 ctxt->error(ctxt->userData,
2655 "grammar has no children\n");
2656 ctxt->nbErrors++;
2657 return(-1);
2658 }
2659 if (IS_RELAXNG(nodes, "start")) {
2660 if (nodes->children == NULL) {
2661 if (ctxt->error != NULL)
2662 ctxt->error(ctxt->userData,
2663 "grammar has no children\n");
2664 ctxt->nbErrors++;
2665 } else {
2666 xmlRelaxNGParseStart(ctxt, nodes->children);
2667 }
2668 nodes = nodes->next;
2669 } else {
2670 if (ctxt->error != NULL)
2671 ctxt->error(ctxt->userData,
2672 "grammar first child must be a <start>\n");
2673 ctxt->nbErrors++;
2674 return(-1);
2675 }
2676 while (nodes != NULL) {
2677 if (IS_RELAXNG(nodes, "define")) {
Daniel Veillard276be4a2003-01-24 01:03:34 +00002678 ret = xmlRelaxNGParseDefine(ctxt, nodes);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002679 } else {
2680 if (ctxt->error != NULL)
2681 ctxt->error(ctxt->userData,
2682 "grammar allows onlys <define> child after <start>\n");
2683 ctxt->nbErrors++;
2684 ret = -1;
2685 }
2686 nodes = nodes->next;
2687 }
2688 return (ret);
2689}
2690
2691/**
2692 * xmlRelaxNGCheckReference:
2693 * @ref: the ref
2694 * @ctxt: a Relax-NG parser context
2695 * @name: the name associated to the defines
2696 *
2697 * Applies the 4.17. combine attribute rule for all the define
2698 * element of a given grammar using the same name.
2699 */
2700static void
2701xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
2702 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
2703 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00002704 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002705
2706 grammar = ctxt->grammar;
2707 if (grammar == NULL) {
2708 if (ctxt->error != NULL)
2709 ctxt->error(ctxt->userData,
2710 "Internal error: no grammar in CheckReference %s\n",
2711 name);
2712 ctxt->nbErrors++;
2713 return;
2714 }
2715 if (ref->content != NULL) {
2716 if (ctxt->error != NULL)
2717 ctxt->error(ctxt->userData,
2718 "Internal error: reference has content in CheckReference %s\n",
2719 name);
2720 ctxt->nbErrors++;
2721 return;
2722 }
2723 if (grammar->defs != NULL) {
2724 def = xmlHashLookup(grammar->defs, name);
2725 if (def != NULL) {
Daniel Veillard276be4a2003-01-24 01:03:34 +00002726 cur = ref;
2727 while (cur != NULL) {
2728 cur->content = def;
2729 cur = cur->nextHash;
2730 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00002731 } else {
2732 TODO
2733 }
2734 }
2735 /*
2736 * TODO: make a closure and verify there is no loop !
2737 */
2738}
2739
2740/**
2741 * xmlRelaxNGCheckCombine:
2742 * @define: the define(s) list
2743 * @ctxt: a Relax-NG parser context
2744 * @name: the name associated to the defines
2745 *
2746 * Applies the 4.17. combine attribute rule for all the define
2747 * element of a given grammar using the same name.
2748 */
2749static void
2750xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
2751 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
2752 xmlChar *combine;
2753 int choiceOrInterleave = -1;
2754 int missing = 0;
2755 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
2756
2757 if (define->nextHash == NULL)
2758 return;
2759 cur = define;
2760 while (cur != NULL) {
2761 combine = xmlGetProp(cur->node, BAD_CAST "combine");
2762 if (combine != NULL) {
2763 if (xmlStrEqual(combine, BAD_CAST "choice")) {
2764 if (choiceOrInterleave == -1)
2765 choiceOrInterleave = 1;
2766 else if (choiceOrInterleave == 0) {
2767 if (ctxt->error != NULL)
2768 ctxt->error(ctxt->userData,
2769 "Defines for %s use both 'choice' and 'interleave'\n",
2770 name);
2771 ctxt->nbErrors++;
2772 }
Daniel Veillard154877e2003-01-30 12:17:05 +00002773 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00002774 if (choiceOrInterleave == -1)
2775 choiceOrInterleave = 0;
2776 else if (choiceOrInterleave == 1) {
2777 if (ctxt->error != NULL)
2778 ctxt->error(ctxt->userData,
2779 "Defines for %s use both 'choice' and 'interleave'\n",
2780 name);
2781 ctxt->nbErrors++;
2782 }
2783 } else {
2784 if (ctxt->error != NULL)
2785 ctxt->error(ctxt->userData,
2786 "Defines for %s use unknown combine value '%s''\n",
2787 name, combine);
2788 ctxt->nbErrors++;
2789 }
2790 xmlFree(combine);
2791 } else {
2792 if (missing == 0)
2793 missing = 1;
2794 else {
2795 if (ctxt->error != NULL)
2796 ctxt->error(ctxt->userData,
2797 "Some defines for %s lacks the combine attribute\n",
2798 name);
2799 ctxt->nbErrors++;
2800 }
2801 }
2802
2803 cur = cur->nextHash;
2804 }
2805#ifdef DEBUG
2806 xmlGenericError(xmlGenericErrorContext,
2807 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
2808 name, choiceOrInterleave);
2809#endif
2810 if (choiceOrInterleave == -1)
2811 choiceOrInterleave = 0;
2812 cur = xmlRelaxNGNewDefine(ctxt, define->node);
2813 if (cur == NULL)
2814 return;
2815 if (choiceOrInterleave == 0)
Daniel Veillard6eadf632003-01-23 18:29:16 +00002816 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillard154877e2003-01-30 12:17:05 +00002817 else
2818 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002819 tmp = define;
2820 last = NULL;
2821 while (tmp != NULL) {
2822 if (tmp->content != NULL) {
2823 if (tmp->content->next != NULL) {
2824 /*
2825 * we need first to create a wrapper.
2826 */
2827 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
2828 if (tmp2 == NULL)
2829 break;
2830 tmp2->type = XML_RELAXNG_GROUP;
2831 tmp2->content = tmp->content;
2832 } else {
2833 tmp2 = tmp->content;
2834 }
2835 if (last == NULL) {
2836 cur->content = tmp2;
2837 } else {
2838 last->next = tmp2;
2839 }
2840 last = tmp2;
2841 tmp->content = NULL;
2842 }
2843 tmp = tmp->nextHash;
2844 }
2845 define->content = cur;
Daniel Veillard154877e2003-01-30 12:17:05 +00002846 if (choiceOrInterleave == 0) {
2847 if (ctxt->interleaves == NULL)
2848 ctxt->interleaves = xmlHashCreate(10);
2849 if (ctxt->interleaves == NULL) {
2850 if (ctxt->error != NULL)
2851 ctxt->error(ctxt->userData,
2852 "Failed to create interleaves hash table\n");
2853 ctxt->nbErrors++;
2854 } else {
2855 char tmpname[32];
2856
2857 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
2858 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
2859 if (ctxt->error != NULL)
2860 ctxt->error(ctxt->userData,
2861 "Failed to add %s to hash table\n", tmpname);
2862 ctxt->nbErrors++;
2863 }
2864 }
2865 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00002866}
2867
2868/**
2869 * xmlRelaxNGCombineStart:
2870 * @ctxt: a Relax-NG parser context
2871 * @grammar: the grammar
2872 *
2873 * Applies the 4.17. combine rule for all the start
2874 * element of a given grammar.
2875 */
2876static void
2877xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
2878 xmlRelaxNGGrammarPtr grammar) {
2879 xmlRelaxNGDefinePtr starts;
2880 xmlChar *combine;
2881 int choiceOrInterleave = -1;
2882 int missing = 0;
2883 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
2884
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002885 starts = grammar->startList;
2886 if ((starts == NULL) || (starts->nextHash == NULL))
Daniel Veillard6eadf632003-01-23 18:29:16 +00002887 return;
2888 cur = starts;
2889 while (cur != NULL) {
2890 combine = xmlGetProp(cur->node, BAD_CAST "combine");
2891 if (combine != NULL) {
2892 if (xmlStrEqual(combine, BAD_CAST "choice")) {
2893 if (choiceOrInterleave == -1)
2894 choiceOrInterleave = 1;
2895 else if (choiceOrInterleave == 0) {
2896 if (ctxt->error != NULL)
2897 ctxt->error(ctxt->userData,
2898 "<start> use both 'choice' and 'interleave'\n");
2899 ctxt->nbErrors++;
2900 }
2901 } else if (xmlStrEqual(combine, BAD_CAST "choice")) {
2902 if (choiceOrInterleave == -1)
2903 choiceOrInterleave = 0;
2904 else if (choiceOrInterleave == 1) {
2905 if (ctxt->error != NULL)
2906 ctxt->error(ctxt->userData,
2907 "<start> use both 'choice' and 'interleave'\n");
2908 ctxt->nbErrors++;
2909 }
2910 } else {
2911 if (ctxt->error != NULL)
2912 ctxt->error(ctxt->userData,
2913 "<start> uses unknown combine value '%s''\n", combine);
2914 ctxt->nbErrors++;
2915 }
2916 xmlFree(combine);
2917 } else {
2918 if (missing == 0)
2919 missing = 1;
2920 else {
2921 if (ctxt->error != NULL)
2922 ctxt->error(ctxt->userData,
2923 "Some <start> elements lacks the combine attribute\n");
2924 ctxt->nbErrors++;
2925 }
2926 }
2927
2928 cur = cur->nextHash;
2929 }
2930#ifdef DEBUG
2931 xmlGenericError(xmlGenericErrorContext,
2932 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
2933 choiceOrInterleave);
2934#endif
2935 if (choiceOrInterleave == -1)
2936 choiceOrInterleave = 0;
2937 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
2938 if (cur == NULL)
2939 return;
2940 if (choiceOrInterleave == 0)
2941 cur->type = XML_RELAXNG_CHOICE;
2942 else
2943 cur->type = XML_RELAXNG_INTERLEAVE;
2944 tmp = starts;
2945 last = NULL;
2946 while (tmp != NULL) {
2947 if (tmp->content != NULL) {
2948 if (tmp->content->next != NULL) {
2949 /*
2950 * we need first to create a wrapper.
2951 */
2952 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
2953 if (tmp2 == NULL)
2954 break;
2955 tmp2->type = XML_RELAXNG_GROUP;
2956 tmp2->content = tmp->content;
2957 } else {
2958 tmp2 = tmp->content;
2959 }
2960 if (last == NULL) {
2961 cur->content = tmp2;
2962 } else {
2963 last->next = tmp2;
2964 }
2965 last = tmp2;
2966 tmp->content = NULL;
2967 }
2968 tmp = tmp->nextHash;
2969 }
2970 starts->content = cur;
2971}
2972
2973/**
2974 * xmlRelaxNGParseGrammar:
2975 * @ctxt: a Relax-NG parser context
2976 * @nodes: grammar children nodes
2977 *
2978 * parse a Relax-NG <grammar> node
2979 *
2980 * Returns the internal xmlRelaxNGGrammarPtr built or
2981 * NULL in case of error
2982 */
2983static xmlRelaxNGGrammarPtr
2984xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
2985 xmlRelaxNGGrammarPtr ret, tmp, old;
2986
Daniel Veillard6eadf632003-01-23 18:29:16 +00002987 ret = xmlRelaxNGNewGrammar(ctxt);
2988 if (ret == NULL)
2989 return(NULL);
2990
2991 /*
2992 * Link the new grammar in the tree
2993 */
2994 ret->parent = ctxt->grammar;
2995 if (ctxt->grammar != NULL) {
2996 tmp = ctxt->grammar->children;
2997 if (tmp == NULL) {
2998 ctxt->grammar->children = ret;
2999 } else {
3000 while (tmp->next != NULL)
3001 tmp = tmp->next;
3002 tmp->next = ret;
3003 }
3004 }
3005
3006 old = ctxt->grammar;
3007 ctxt->grammar = ret;
3008 xmlRelaxNGParseGrammarContent(ctxt, nodes);
3009 ctxt->grammar = ret;
3010
3011 /*
3012 * Apply 4.17 mergingd rules to defines and starts
3013 */
3014 xmlRelaxNGCombineStart(ctxt, ret);
3015 if (ret->defs != NULL) {
3016 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
3017 ctxt);
3018 }
3019
3020 /*
3021 * link together defines and refs in this grammar
3022 */
3023 if (ret->refs != NULL) {
3024 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
3025 ctxt);
3026 }
3027 ctxt->grammar = old;
3028 return(ret);
3029}
3030
3031/**
3032 * xmlRelaxNGParseDocument:
3033 * @ctxt: a Relax-NG parser context
3034 * @node: the root node of the RelaxNG schema
3035 *
3036 * parse a Relax-NG definition resource and build an internal
3037 * xmlRelaxNG struture which can be used to validate instances.
3038 *
3039 * Returns the internal XML RelaxNG structure built or
3040 * NULL in case of error
3041 */
3042static xmlRelaxNGPtr
3043xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3044 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00003045 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00003046 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003047
3048 if ((ctxt == NULL) || (node == NULL))
3049 return (NULL);
3050
3051 schema = xmlRelaxNGNewRelaxNG(ctxt);
3052 if (schema == NULL)
3053 return(NULL);
3054
Daniel Veillard276be4a2003-01-24 01:03:34 +00003055 olddefine = ctxt->define;
3056 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003057 if (IS_RELAXNG(node, "grammar")) {
3058 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
3059 } else {
3060 schema->topgrammar = xmlRelaxNGNewGrammar(ctxt);
3061 if (schema->topgrammar == NULL) {
3062 return(schema);
3063 }
3064 schema->topgrammar->parent = NULL;
Daniel Veillarde431a272003-01-29 23:02:33 +00003065 old = ctxt->grammar;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003066 ctxt->grammar = schema->topgrammar;
3067 xmlRelaxNGParseStart(ctxt, node);
Daniel Veillarde431a272003-01-29 23:02:33 +00003068 if (old != NULL)
3069 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003070 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00003071 ctxt->define = olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003072
3073#ifdef DEBUG
3074 if (schema == NULL)
3075 xmlGenericError(xmlGenericErrorContext,
3076 "xmlRelaxNGParseDocument() failed\n");
3077#endif
3078
3079 return (schema);
3080}
3081
3082/************************************************************************
3083 * *
3084 * Reading RelaxNGs *
3085 * *
3086 ************************************************************************/
3087
3088/**
3089 * xmlRelaxNGNewParserCtxt:
3090 * @URL: the location of the schema
3091 *
3092 * Create an XML RelaxNGs parse context for that file/resource expected
3093 * to contain an XML RelaxNGs file.
3094 *
3095 * Returns the parser context or NULL in case of error
3096 */
3097xmlRelaxNGParserCtxtPtr
3098xmlRelaxNGNewParserCtxt(const char *URL) {
3099 xmlRelaxNGParserCtxtPtr ret;
3100
3101 if (URL == NULL)
3102 return(NULL);
3103
3104 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
3105 if (ret == NULL) {
3106 xmlGenericError(xmlGenericErrorContext,
3107 "Failed to allocate new schama parser context for %s\n", URL);
3108 return (NULL);
3109 }
3110 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
3111 ret->URL = xmlStrdup((const xmlChar *)URL);
3112 return (ret);
3113}
3114
3115/**
3116 * xmlRelaxNGNewMemParserCtxt:
3117 * @buffer: a pointer to a char array containing the schemas
3118 * @size: the size of the array
3119 *
3120 * Create an XML RelaxNGs parse context for that memory buffer expected
3121 * to contain an XML RelaxNGs file.
3122 *
3123 * Returns the parser context or NULL in case of error
3124 */
3125xmlRelaxNGParserCtxtPtr
3126xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) {
3127 xmlRelaxNGParserCtxtPtr ret;
3128
3129 if ((buffer == NULL) || (size <= 0))
3130 return(NULL);
3131
3132 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
3133 if (ret == NULL) {
3134 xmlGenericError(xmlGenericErrorContext,
3135 "Failed to allocate new schama parser context\n");
3136 return (NULL);
3137 }
3138 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
3139 ret->buffer = buffer;
3140 ret->size = size;
3141 return (ret);
3142}
3143
3144/**
3145 * xmlRelaxNGFreeParserCtxt:
3146 * @ctxt: the schema parser context
3147 *
3148 * Free the resources associated to the schema parser context
3149 */
3150void
3151xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) {
3152 if (ctxt == NULL)
3153 return;
3154 if (ctxt->URL != NULL)
3155 xmlFree(ctxt->URL);
3156 if (ctxt->doc != NULL)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003157 xmlFreeDoc(ctxt->document);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003158 if (ctxt->interleaves != NULL)
3159 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003160 if (ctxt->documents != NULL)
3161 xmlHashFree(ctxt->documents, (xmlHashDeallocator)
3162 xmlRelaxNGFreeDocument);
3163 if (ctxt->docTab != NULL)
3164 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00003165 if (ctxt->incTab != NULL)
3166 xmlFree(ctxt->incTab);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003167 xmlFree(ctxt);
3168}
3169
Daniel Veillard6eadf632003-01-23 18:29:16 +00003170/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003171 * xmlRelaxNGCleanupDoc:
3172 * @ctxt: a Relax-NG parser context
3173 * @doc: an xmldocPtr document pointer
Daniel Veillard6eadf632003-01-23 18:29:16 +00003174 *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003175 * Cleanup the document from unwanted nodes for parsing, resolve
3176 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00003177 *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003178 * Returns the cleaned up document or NULL in case of error
Daniel Veillard6eadf632003-01-23 18:29:16 +00003179 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003180static xmlDocPtr
3181xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00003182 xmlNodePtr root, cur, delete;
3183
Daniel Veillard6eadf632003-01-23 18:29:16 +00003184 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003185 * Extract the root
Daniel Veillard6eadf632003-01-23 18:29:16 +00003186 */
3187 root = xmlDocGetRootElement(doc);
3188 if (root == NULL) {
3189 if (ctxt->error != NULL)
3190 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
3191 ctxt->URL);
3192 ctxt->nbErrors++;
3193 return (NULL);
3194 }
3195
3196 /*
3197 * Remove all the blank text nodes
3198 */
3199 delete = NULL;
3200 cur = root;
3201 while (cur != NULL) {
3202 if (delete != NULL) {
3203 xmlUnlinkNode(delete);
3204 xmlFreeNode(delete);
3205 delete = NULL;
3206 }
3207 if (cur->type == XML_ELEMENT_NODE) {
3208 /*
3209 * Simplification 4.1. Annotations
3210 */
3211 if ((cur->ns == NULL) ||
3212 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
3213 delete = cur;
3214 goto skip_children;
3215 } else {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00003216 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
3217 /*
3218 * implements rule 4.11
3219 */
3220 xmlNodePtr child, ins, tmp;
3221
3222 child = cur->children;
3223 ins = child;
3224 while (child != NULL) {
3225 tmp = child->next;
3226 xmlUnlinkNode(child);
3227 ins = xmlAddNextSibling(ins, child);
3228 child = tmp;
3229 }
3230 delete = cur;
3231 goto skip_children;
3232 } else if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003233 xmlChar *href, *ns, *base, *URL;
3234 xmlRelaxNGDocumentPtr docu;
3235
3236 ns = xmlGetProp(cur, BAD_CAST "ns");
3237 href = xmlGetProp(cur, BAD_CAST "href");
3238 if (href == NULL) {
3239 if (ctxt->error != NULL)
3240 ctxt->error(ctxt->userData,
3241 "xmlRelaxNGParse: externalRef has no href attribute\n");
3242 ctxt->nbErrors++;
3243 delete = cur;
3244 goto skip_children;
3245 }
3246 base = xmlNodeGetBase(cur->doc, cur);
3247 URL = xmlBuildURI(href, base);
3248 if (URL == NULL) {
3249 if (ctxt->error != NULL)
3250 ctxt->error(ctxt->userData,
3251 "Failed to compute URL for externalRef %s\n", href);
3252 ctxt->nbErrors++;
3253 if (href != NULL)
3254 xmlFree(href);
3255 if (base != NULL)
3256 xmlFree(base);
3257 delete = cur;
3258 goto skip_children;
3259 }
3260 if (href != NULL)
3261 xmlFree(href);
3262 if (base != NULL)
3263 xmlFree(base);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00003264 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003265 if (docu == NULL) {
3266 if (ctxt->error != NULL)
3267 ctxt->error(ctxt->userData,
3268 "Failed to load externalRef %s\n", URL);
3269 ctxt->nbErrors++;
3270 xmlFree(URL);
3271 delete = cur;
3272 goto skip_children;
3273 }
3274 xmlFree(URL);
3275 cur->_private = docu;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003276 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00003277 xmlChar *href, *base, *URL;
3278 xmlRelaxNGIncludePtr incl;
3279
3280 href = xmlGetProp(cur, BAD_CAST "href");
3281 if (href == NULL) {
3282 if (ctxt->error != NULL)
3283 ctxt->error(ctxt->userData,
3284 "xmlRelaxNGParse: externalRef has no href attribute\n");
3285 ctxt->nbErrors++;
3286 delete = cur;
3287 goto skip_children;
3288 }
3289 base = xmlNodeGetBase(cur->doc, cur);
3290 URL = xmlBuildURI(href, base);
3291 if (URL == NULL) {
3292 if (ctxt->error != NULL)
3293 ctxt->error(ctxt->userData,
3294 "Failed to compute URL for externalRef %s\n", href);
3295 ctxt->nbErrors++;
3296 if (href != NULL)
3297 xmlFree(href);
3298 if (base != NULL)
3299 xmlFree(base);
3300 delete = cur;
3301 goto skip_children;
3302 }
3303 if (href != NULL)
3304 xmlFree(href);
3305 if (base != NULL)
3306 xmlFree(base);
3307 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur);
3308 if (incl == NULL) {
3309 if (ctxt->error != NULL)
3310 ctxt->error(ctxt->userData,
3311 "Failed to load externalRef %s\n", URL);
3312 ctxt->nbErrors++;
3313 xmlFree(URL);
3314 delete = cur;
3315 goto skip_children;
3316 }
3317 xmlFree(URL);
3318 cur->_private = incl;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003319 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
3320 (xmlStrEqual(cur->name, BAD_CAST "attribute"))) {
3321 xmlChar *name;
3322 xmlNodePtr text = NULL;
3323
3324 /*
3325 * Simplification 4.8. name attribute of element
3326 * and attribute elements
3327 */
3328 name = xmlGetProp(cur, BAD_CAST "name");
3329 if (name != NULL) {
3330 if (cur->children == NULL) {
3331 text = xmlNewChild(cur, cur->ns, BAD_CAST "name",
3332 name);
3333 } else {
3334 xmlNodePtr node;
3335 node = xmlNewNode(cur->ns, BAD_CAST "name");
3336 if (node != NULL) {
3337 xmlAddPrevSibling(cur->children, node);
3338 text = xmlNewText(name);
3339 xmlAddChild(node, text);
3340 text = node;
3341 }
3342 }
3343 xmlUnsetProp(cur, BAD_CAST "name");
3344 xmlFree(name);
3345 }
3346 if (xmlStrEqual(cur->name, BAD_CAST "attribute")) {
3347 if (text == NULL) {
3348 text = cur->children;
3349 while (text != NULL) {
3350 if ((text->type == XML_ELEMENT_NODE) &&
3351 (xmlStrEqual(text->name, BAD_CAST "name")))
3352 break;
3353 text = text->next;
3354 }
3355 }
3356 if (text == NULL) {
3357 if (ctxt->error != NULL)
3358 ctxt->error(ctxt->userData,
3359 "xmlRelaxNGParse: attribute without name\n");
3360 ctxt->nbErrors++;
3361 } else {
3362 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
3363 }
3364 }
3365 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
3366 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
3367 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
3368 /*
3369 * Simplification 4.8. name attribute of element
3370 * and attribute elements
3371 */
3372 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
3373 xmlNodePtr node;
3374 xmlChar *ns = NULL;
3375
3376 node = cur->parent;
3377 while ((node != NULL) &&
3378 (node->type == XML_ELEMENT_NODE)) {
3379 ns = xmlGetProp(node, BAD_CAST "ns");
3380 if (ns != NULL) {
3381 break;
3382 }
3383 node = node->parent;
3384 }
3385 if (ns == NULL) {
3386 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
3387 } else {
3388 xmlSetProp(cur, BAD_CAST "ns", ns);
3389 xmlFree(ns);
3390 }
3391 }
3392 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
3393 xmlChar *name, *local, *prefix;
3394
3395 /*
3396 * Simplification: 4.10. QNames
3397 */
3398 name = xmlNodeGetContent(cur);
3399 if (name != NULL) {
3400 local = xmlSplitQName2(name, &prefix);
3401 if (local != NULL) {
3402 xmlNsPtr ns;
3403
3404 ns = xmlSearchNs(cur->doc, cur, prefix);
3405 if (ns == NULL) {
3406 if (ctxt->error != NULL)
3407 ctxt->error(ctxt->userData,
3408 "xmlRelaxNGParse: no namespace for prefix %s\n", prefix);
3409 ctxt->nbErrors++;
3410 } else {
3411 xmlSetProp(cur, BAD_CAST "ns", ns->href);
3412 xmlNodeSetContent(cur, local);
3413 }
3414 xmlFree(local);
3415 xmlFree(prefix);
3416 }
3417 xmlFree(name);
3418 }
3419 }
3420 }
3421 }
3422 }
3423 /*
3424 * Simplification 4.2 whitespaces
3425 */
3426 else if (cur->type == XML_TEXT_NODE) {
3427 if (IS_BLANK_NODE(cur)) {
3428 if (cur->parent->type == XML_ELEMENT_NODE) {
3429 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) &&
3430 (!xmlStrEqual(cur->parent->name, BAD_CAST "param")))
3431 delete = cur;
3432 } else {
3433 delete = cur;
3434 goto skip_children;
3435 }
3436 }
3437 } else if (cur->type != XML_CDATA_SECTION_NODE) {
3438 delete = cur;
3439 goto skip_children;
3440 }
3441
3442 /*
3443 * Skip to next node
3444 */
3445 if (cur->children != NULL) {
3446 if ((cur->children->type != XML_ENTITY_DECL) &&
3447 (cur->children->type != XML_ENTITY_REF_NODE) &&
3448 (cur->children->type != XML_ENTITY_NODE)) {
3449 cur = cur->children;
3450 continue;
3451 }
3452 }
3453skip_children:
3454 if (cur->next != NULL) {
3455 cur = cur->next;
3456 continue;
3457 }
3458
3459 do {
3460 cur = cur->parent;
3461 if (cur == NULL)
3462 break;
3463 if (cur == root) {
3464 cur = NULL;
3465 break;
3466 }
3467 if (cur->next != NULL) {
3468 cur = cur->next;
3469 break;
3470 }
3471 } while (cur != NULL);
3472 }
3473 if (delete != NULL) {
3474 xmlUnlinkNode(delete);
3475 xmlFreeNode(delete);
3476 delete = NULL;
3477 }
3478
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003479 return(doc);
3480}
3481
3482/**
3483 * xmlRelaxNGParse:
3484 * @ctxt: a Relax-NG parser context
3485 *
3486 * parse a schema definition resource and build an internal
3487 * XML Shema struture which can be used to validate instances.
3488 * *WARNING* this interface is highly subject to change
3489 *
3490 * Returns the internal XML RelaxNG structure built from the resource or
3491 * NULL in case of error
3492 */
3493xmlRelaxNGPtr
3494xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
3495{
3496 xmlRelaxNGPtr ret = NULL;
3497 xmlDocPtr doc;
3498 xmlNodePtr root;
3499
3500 xmlRelaxNGInitTypes();
3501
3502 if (ctxt == NULL)
3503 return (NULL);
3504
3505 /*
3506 * First step is to parse the input document into an DOM/Infoset
3507 */
3508 if (ctxt->URL != NULL) {
3509 doc = xmlParseFile((const char *) ctxt->URL);
3510 if (doc == NULL) {
3511 if (ctxt->error != NULL)
3512 ctxt->error(ctxt->userData,
3513 "xmlRelaxNGParse: could not load %s\n", ctxt->URL);
3514 ctxt->nbErrors++;
3515 return (NULL);
3516 }
3517 } else if (ctxt->buffer != NULL) {
3518 doc = xmlParseMemory(ctxt->buffer, ctxt->size);
3519 if (doc == NULL) {
3520 if (ctxt->error != NULL)
3521 ctxt->error(ctxt->userData,
3522 "xmlRelaxNGParse: could not parse schemas\n");
3523 ctxt->nbErrors++;
3524 return (NULL);
3525 }
3526 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
3527 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
3528 } else {
3529 if (ctxt->error != NULL)
3530 ctxt->error(ctxt->userData,
3531 "xmlRelaxNGParse: nothing to parse\n");
3532 ctxt->nbErrors++;
3533 return (NULL);
3534 }
3535 ctxt->document = doc;
3536
3537 /*
3538 * Some preprocessing of the document content
3539 */
3540 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
3541 if (doc == NULL) {
3542 xmlFreeDoc(ctxt->document);
3543 ctxt->document = NULL;
3544 return(NULL);
3545 }
3546
Daniel Veillard6eadf632003-01-23 18:29:16 +00003547 /*
3548 * Then do the parsing for good
3549 */
3550 root = xmlDocGetRootElement(doc);
3551 if (root == NULL) {
3552 if (ctxt->error != NULL)
3553 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
3554 ctxt->URL);
3555 ctxt->nbErrors++;
3556 return (NULL);
3557 }
3558 ret = xmlRelaxNGParseDocument(ctxt, root);
3559 if (ret == NULL)
3560 return(NULL);
3561
3562 /*
3563 * Check the ref/defines links
3564 */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003565 /*
3566 * try to preprocess interleaves
3567 */
3568 if (ctxt->interleaves != NULL) {
3569 xmlHashScan(ctxt->interleaves,
3570 (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt);
3571 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003572
3573 /*
3574 * if there was a parsing error return NULL
3575 */
3576 if (ctxt->nbErrors > 0) {
3577 xmlRelaxNGFree(ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003578 ctxt->document = NULL;
3579 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003580 return(NULL);
3581 }
3582
3583 /*
3584 * Transfer the pointer for cleanup at the schema level.
3585 */
3586 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003587 ctxt->document = NULL;
3588 ret->documents = ctxt->documents;
3589 ctxt->documents = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003590
3591 return (ret);
3592}
3593
3594/**
3595 * xmlRelaxNGSetParserErrors:
3596 * @ctxt: a Relax-NG validation context
3597 * @err: the error callback
3598 * @warn: the warning callback
3599 * @ctx: contextual data for the callbacks
3600 *
3601 * Set the callback functions used to handle errors for a validation context
3602 */
3603void
3604xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
3605 xmlRelaxNGValidityErrorFunc err,
3606 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
3607 if (ctxt == NULL)
3608 return;
3609 ctxt->error = err;
3610 ctxt->warning = warn;
3611 ctxt->userData = ctx;
3612}
3613/************************************************************************
3614 * *
3615 * Dump back a compiled form *
3616 * *
3617 ************************************************************************/
3618static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define);
3619
3620/**
3621 * xmlRelaxNGDumpDefines:
3622 * @output: the file output
3623 * @defines: a list of define structures
3624 *
3625 * Dump a RelaxNG structure back
3626 */
3627static void
3628xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) {
3629 while (defines != NULL) {
3630 xmlRelaxNGDumpDefine(output, defines);
3631 defines = defines->next;
3632 }
3633}
3634
3635/**
3636 * xmlRelaxNGDumpDefine:
3637 * @output: the file output
3638 * @define: a define structure
3639 *
3640 * Dump a RelaxNG structure back
3641 */
3642static void
3643xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) {
3644 if (define == NULL)
3645 return;
3646 switch(define->type) {
3647 case XML_RELAXNG_EMPTY:
3648 fprintf(output, "<empty/>\n");
3649 break;
3650 case XML_RELAXNG_NOT_ALLOWED:
3651 fprintf(output, "<notAllowed/>\n");
3652 break;
3653 case XML_RELAXNG_TEXT:
3654 fprintf(output, "<text/>\n");
3655 break;
3656 case XML_RELAXNG_ELEMENT:
3657 fprintf(output, "<element>\n");
3658 if (define->name != NULL) {
3659 fprintf(output, "<name");
3660 if (define->ns != NULL)
3661 fprintf(output, " ns=\"%s\"", define->ns);
3662 fprintf(output, ">%s</name>\n", define->name);
3663 }
3664 xmlRelaxNGDumpDefines(output, define->attrs);
3665 xmlRelaxNGDumpDefines(output, define->content);
3666 fprintf(output, "</element>\n");
3667 break;
3668 case XML_RELAXNG_LIST:
3669 fprintf(output, "<list>\n");
3670 xmlRelaxNGDumpDefines(output, define->content);
3671 fprintf(output, "</list>\n");
3672 break;
3673 case XML_RELAXNG_ONEORMORE:
3674 fprintf(output, "<oneOrMore>\n");
3675 xmlRelaxNGDumpDefines(output, define->content);
3676 fprintf(output, "</oneOrMore>\n");
3677 break;
3678 case XML_RELAXNG_ZEROORMORE:
3679 fprintf(output, "<zeroOrMore>\n");
3680 xmlRelaxNGDumpDefines(output, define->content);
3681 fprintf(output, "</zeroOrMore>\n");
3682 break;
3683 case XML_RELAXNG_CHOICE:
3684 fprintf(output, "<choice>\n");
3685 xmlRelaxNGDumpDefines(output, define->content);
3686 fprintf(output, "</choice>\n");
3687 break;
3688 case XML_RELAXNG_GROUP:
3689 fprintf(output, "<group>\n");
3690 xmlRelaxNGDumpDefines(output, define->content);
3691 fprintf(output, "</group>\n");
3692 break;
3693 case XML_RELAXNG_INTERLEAVE:
3694 fprintf(output, "<interleave>\n");
3695 xmlRelaxNGDumpDefines(output, define->content);
3696 fprintf(output, "</interleave>\n");
3697 break;
3698 case XML_RELAXNG_OPTIONAL:
3699 fprintf(output, "<optional>\n");
3700 xmlRelaxNGDumpDefines(output, define->content);
3701 fprintf(output, "</optional>\n");
3702 break;
3703 case XML_RELAXNG_ATTRIBUTE:
3704 fprintf(output, "<attribute>\n");
3705 xmlRelaxNGDumpDefines(output, define->content);
3706 fprintf(output, "</attribute>\n");
3707 break;
3708 case XML_RELAXNG_DEF:
3709 fprintf(output, "<define");
3710 if (define->name != NULL)
3711 fprintf(output, " name=\"%s\"", define->name);
3712 fprintf(output, ">\n");
3713 xmlRelaxNGDumpDefines(output, define->content);
3714 fprintf(output, "</define>\n");
3715 break;
3716 case XML_RELAXNG_REF:
3717 fprintf(output, "<ref");
3718 if (define->name != NULL)
3719 fprintf(output, " name=\"%s\"", define->name);
3720 fprintf(output, ">\n");
3721 xmlRelaxNGDumpDefines(output, define->content);
3722 fprintf(output, "</ref>\n");
3723 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003724 case XML_RELAXNG_EXTERNALREF:
Daniel Veillarde431a272003-01-29 23:02:33 +00003725 fprintf(output, "<externalRef");
3726 xmlRelaxNGDumpDefines(output, define->content);
3727 fprintf(output, "</externalRef>\n");
3728 break;
3729 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00003730 case XML_RELAXNG_VALUE:
3731 TODO
3732 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003733 case XML_RELAXNG_START:
3734 TODO
3735 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003736 }
3737}
3738
3739/**
3740 * xmlRelaxNGDumpGrammar:
3741 * @output: the file output
3742 * @grammar: a grammar structure
3743 * @top: is this a top grammar
3744 *
3745 * Dump a RelaxNG structure back
3746 */
3747static void
3748xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
3749{
3750 if (grammar == NULL)
3751 return;
3752
3753 fprintf(output, "<grammar");
3754 if (top)
3755 fprintf(output,
3756 " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
3757 switch(grammar->combine) {
3758 case XML_RELAXNG_COMBINE_UNDEFINED:
3759 break;
3760 case XML_RELAXNG_COMBINE_CHOICE:
3761 fprintf(output, " combine=\"choice\"");
3762 break;
3763 case XML_RELAXNG_COMBINE_INTERLEAVE:
3764 fprintf(output, " combine=\"interleave\"");
3765 break;
3766 default:
3767 fprintf(output, " <!-- invalid combine value -->");
3768 }
3769 fprintf(output, ">\n");
3770 if (grammar->start == NULL) {
3771 fprintf(output, " <!-- grammar had no start -->");
3772 } else {
3773 fprintf(output, "<start>\n");
3774 xmlRelaxNGDumpDefine(output, grammar->start);
3775 fprintf(output, "</start>\n");
3776 }
3777 /* TODO ? Dump the defines ? */
3778 fprintf(output, "</grammar>\n");
3779}
3780
3781/**
3782 * xmlRelaxNGDump:
3783 * @output: the file output
3784 * @schema: a schema structure
3785 *
3786 * Dump a RelaxNG structure back
3787 */
3788void
3789xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
3790{
3791 if (schema == NULL) {
3792 fprintf(output, "RelaxNG empty or failed to compile\n");
3793 return;
3794 }
3795 fprintf(output, "RelaxNG: ");
3796 if (schema->doc == NULL) {
3797 fprintf(output, "no document\n");
3798 } else if (schema->doc->URL != NULL) {
3799 fprintf(output, "%s\n", schema->doc->URL);
3800 } else {
3801 fprintf(output, "\n");
3802 }
3803 if (schema->topgrammar == NULL) {
3804 fprintf(output, "RelaxNG has no top grammar\n");
3805 return;
3806 }
3807 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
3808}
3809
3810/************************************************************************
3811 * *
3812 * Validation implementation *
3813 * *
3814 ************************************************************************/
3815static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
3816 xmlRelaxNGDefinePtr define);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00003817static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
3818 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003819
3820/**
3821 * xmlRelaxNGSkipIgnored:
3822 * @ctxt: a schema validation context
3823 * @node: the top node.
3824 *
3825 * Skip ignorable nodes in that context
3826 *
3827 * Returns the new sibling or NULL in case of error.
3828 */
3829static xmlNodePtr
3830xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
3831 xmlNodePtr node) {
3832 /*
3833 * TODO complete and handle entities
3834 */
3835 while ((node != NULL) &&
3836 ((node->type == XML_COMMENT_NODE) ||
3837 ((node->type == XML_TEXT_NODE) &&
3838 (IS_BLANK_NODE(node))))) {
3839 node = node->next;
3840 }
3841 return(node);
3842}
3843
3844/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003845 * xmlRelaxNGNormalize:
3846 * @ctxt: a schema validation context
3847 * @str: the string to normalize
3848 *
3849 * Implements the normalizeWhiteSpace( s ) function from
3850 * section 6.2.9 of the spec
3851 *
3852 * Returns the new string or NULL in case of error.
3853 */
3854static xmlChar *
3855xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) {
3856 xmlChar *ret, *p;
3857 const xmlChar *tmp;
3858 int len;
3859
3860 if (str == NULL)
3861 return(NULL);
3862 tmp = str;
3863 while (*tmp != 0) tmp++;
3864 len = tmp - str;
3865
3866 ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar));
3867 if (ret == NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00003868 if (ctxt != NULL) {
3869 VALID_CTXT();
3870 VALID_ERROR("xmlRelaxNGNormalize: out of memory\n");
3871 } else {
3872 xmlGenericError(xmlGenericErrorContext,
3873 "xmlRelaxNGNormalize: out of memory\n");
3874 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003875 return(NULL);
3876 }
3877 p = ret;
3878 while (IS_BLANK(*str)) str++;
3879 while (*str != 0) {
3880 if (IS_BLANK(*str)) {
3881 while (IS_BLANK(*str)) str++;
3882 if (*str == 0)
3883 break;
3884 *p++ = ' ';
3885 } else
3886 *p++ = *str++;
3887 }
3888 *p = 0;
3889 return(ret);
3890}
3891
3892/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003893 * xmlRelaxNGValidateDatatype:
3894 * @ctxt: a Relax-NG validation context
3895 * @value: the string value
3896 * @type: the datatype definition
3897 *
3898 * Validate the given value against the dataype
3899 *
3900 * Returns 0 if the validation succeeded or an error code.
3901 */
3902static int
3903xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value,
3904 xmlRelaxNGDefinePtr define) {
3905 int ret;
3906 xmlRelaxNGTypeLibraryPtr lib;
3907
3908 if ((define == NULL) || (define->data == NULL)) {
3909 return(-1);
3910 }
3911 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
3912 if (lib->check != NULL)
3913 ret = lib->check(lib->data, define->name, value);
3914 else
3915 ret = -1;
3916 if (ret < 0) {
3917 VALID_CTXT();
3918 VALID_ERROR("Internal: failed to validate type %s\n", define->name);
3919 return(-1);
3920 } else if (ret == 1) {
3921 ret = 0;
3922 } else {
3923 VALID_CTXT();
3924 VALID_ERROR("Type %s doesn't allow value %s\n", define->name, value);
3925 return(-1);
3926 ret = -1;
3927 }
3928 return(ret);
3929}
3930
3931/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00003932 * xmlRelaxNGNextValue:
3933 * @ctxt: a Relax-NG validation context
3934 *
3935 * Skip to the next value when validating within a list
3936 *
3937 * Returns 0 if the operation succeeded or an error code.
3938 */
3939static int
3940xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) {
3941 xmlChar *cur;
3942
3943 cur = ctxt->state->value;
3944 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
3945 ctxt->state->value = NULL;
3946 return(0);
3947 }
3948 while (*cur != 0) cur++;
3949 while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++;
3950 if (cur == ctxt->state->endvalue)
3951 ctxt->state->value = NULL;
3952 else
3953 ctxt->state->value = cur;
3954 return(0);
3955}
3956
3957/**
3958 * xmlRelaxNGValidateValueList:
3959 * @ctxt: a Relax-NG validation context
3960 * @defines: the list of definitions to verify
3961 *
3962 * Validate the given set of definitions for the current value
3963 *
3964 * Returns 0 if the validation succeeded or an error code.
3965 */
3966static int
3967xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
3968 xmlRelaxNGDefinePtr defines) {
3969 int ret = 0;
3970
3971 while (defines != NULL) {
3972 ret = xmlRelaxNGValidateValue(ctxt, defines);
3973 if (ret != 0)
3974 break;
3975 defines = defines->next;
3976 }
3977 return(ret);
3978}
3979
3980/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003981 * xmlRelaxNGValidateValue:
3982 * @ctxt: a Relax-NG validation context
3983 * @define: the definition to verify
3984 *
3985 * Validate the given definition for the current value
3986 *
3987 * Returns 0 if the validation succeeded or an error code.
3988 */
3989static int
3990xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
3991 xmlRelaxNGDefinePtr define) {
Daniel Veillardedc91922003-01-26 00:52:04 +00003992 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003993 xmlChar *value;
3994
3995 value = ctxt->state->value;
3996 switch (define->type) {
3997 case XML_RELAXNG_EMPTY:
3998 if ((value != NULL) && (value[0] != '0'))
3999 ret = -1;
4000 break;
4001 case XML_RELAXNG_TEXT:
4002 break;
Daniel Veillardedc91922003-01-26 00:52:04 +00004003 case XML_RELAXNG_VALUE: {
4004 if (!xmlStrEqual(value, define->value)) {
4005 if (define->name != NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00004006 xmlRelaxNGTypeLibraryPtr lib;
4007
4008 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
4009 if ((lib != NULL) && (lib->comp != NULL))
4010 ret = lib->comp(lib->data, define->name, value,
4011 define->value);
4012 else
4013 ret = -1;
4014 if (ret < 0) {
4015 VALID_CTXT();
4016 VALID_ERROR("Internal: failed to compare type %s\n",
4017 define->name);
4018 return(-1);
4019 } else if (ret == 1) {
4020 ret = 0;
4021 } else {
4022 ret = -1;
4023 }
Daniel Veillardedc91922003-01-26 00:52:04 +00004024 } else {
4025 xmlChar *nval, *nvalue;
4026
4027 /*
4028 * TODO: trivial optimizations are possible by
4029 * computing at compile-time
4030 */
4031 nval = xmlRelaxNGNormalize(ctxt, define->value);
4032 nvalue = xmlRelaxNGNormalize(ctxt, value);
4033
Daniel Veillardea3f3982003-01-26 19:45:18 +00004034 if ((nval == NULL) || (nvalue == NULL) ||
4035 (!xmlStrEqual(nval, nvalue)))
Daniel Veillardedc91922003-01-26 00:52:04 +00004036 ret = -1;
4037 if (nval != NULL)
4038 xmlFree(nval);
4039 if (nvalue != NULL)
4040 xmlFree(nvalue);
4041 }
4042 }
4043 break;
4044 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004045 case XML_RELAXNG_DATATYPE: {
4046 ret = xmlRelaxNGValidateDatatype(ctxt, value, define);
4047 if (ret == 0)
4048 xmlRelaxNGNextValue(ctxt);
4049
4050 break;
4051 }
4052 case XML_RELAXNG_CHOICE: {
4053 xmlRelaxNGDefinePtr list = define->content;
4054 xmlChar *oldvalue;
4055
4056 oldflags = ctxt->flags;
4057 ctxt->flags |= FLAGS_IGNORABLE;
4058
4059 oldvalue = ctxt->state->value;
4060 while (list != NULL) {
4061 ret = xmlRelaxNGValidateValue(ctxt, list);
4062 if (ret == 0) {
4063 break;
4064 }
4065 ctxt->state->value = oldvalue;
4066 list = list->next;
4067 }
4068 ctxt->flags = oldflags;
4069 break;
4070 }
4071 case XML_RELAXNG_LIST: {
4072 xmlRelaxNGDefinePtr list = define->content;
4073 xmlChar *oldvalue, *oldend, *val, *cur;
4074
4075 oldvalue = ctxt->state->value;
4076 oldend = ctxt->state->endvalue;
4077
4078 val = xmlStrdup(oldvalue);
4079 if (val == NULL) {
4080 VALID_CTXT();
4081 VALID_ERROR("Internal: no state\n");
4082 return(-1);
4083 }
4084 cur = val;
4085 while (*cur != 0) {
4086 if (IS_BLANK(*cur))
4087 *cur = 0;
4088 cur++;
4089 }
4090 ctxt->state->endvalue = cur;
4091 cur = val;
4092 while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++;
4093
4094 ctxt->state->value = cur;
4095
4096 while (list != NULL) {
4097 ret = xmlRelaxNGValidateValue(ctxt, list);
4098 if (ret != 0) {
4099 break;
4100 }
4101 list = list->next;
4102 }
4103 if ((ret == 0) && (ctxt->state->value != NULL) &&
4104 (ctxt->state->value != ctxt->state->endvalue)) {
4105 VALID_CTXT();
4106 VALID_ERROR("Extra data in list: %s\n", ctxt->state->value);
4107 ret = -1;
4108 }
4109 xmlFree(val);
4110 ctxt->state->value = oldvalue;
4111 ctxt->state->endvalue = oldend;
4112 break;
4113 }
4114 case XML_RELAXNG_ONEORMORE:
4115 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
4116 if (ret != 0) {
4117 break;
4118 }
4119 /* no break on purpose */
4120 case XML_RELAXNG_ZEROORMORE: {
4121 xmlChar *cur, *temp;
4122
4123 oldflags = ctxt->flags;
4124 ctxt->flags |= FLAGS_IGNORABLE;
4125 cur = ctxt->state->value;
4126 temp = NULL;
4127 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
4128 (temp != cur)) {
4129 temp = cur;
4130 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
4131 if (ret != 0) {
4132 ctxt->state->value = temp;
4133 ret = 0;
4134 break;
4135 }
4136 cur = ctxt->state->value;
4137 }
4138 ctxt->flags = oldflags;
4139 break;
4140 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004141 default:
4142 TODO
4143 ret = -1;
4144 }
4145 return(ret);
4146}
4147
4148/**
4149 * xmlRelaxNGValidateValueContent:
4150 * @ctxt: a Relax-NG validation context
4151 * @defines: the list of definitions to verify
4152 *
4153 * Validate the given definitions for the current value
4154 *
4155 * Returns 0 if the validation succeeded or an error code.
4156 */
4157static int
4158xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
4159 xmlRelaxNGDefinePtr defines) {
4160 int ret = 0;
4161
4162 while (defines != NULL) {
4163 ret = xmlRelaxNGValidateValue(ctxt, defines);
4164 if (ret != 0)
4165 break;
4166 defines = defines->next;
4167 }
4168 return(ret);
4169}
4170
4171/**
4172 * xmlRelaxNGValidateAttribute:
4173 * @ctxt: a Relax-NG validation context
4174 * @define: the definition to verify
4175 *
4176 * Validate the given attribute definition for that node
4177 *
4178 * Returns 0 if the validation succeeded or an error code.
4179 */
4180static int
4181xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
4182 xmlRelaxNGDefinePtr define) {
4183 int ret = 0, i;
4184 xmlChar *value, *oldvalue;
4185 xmlAttrPtr prop = NULL, tmp;
4186
4187 if (define->name != NULL) {
4188 for (i = 0;i < ctxt->state->nbAttrs;i++) {
4189 tmp = ctxt->state->attrs[i];
4190 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
4191 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
4192 (tmp->ns == NULL)) ||
4193 ((tmp->ns != NULL) &&
4194 (xmlStrEqual(define->ns, tmp->ns->href)))) {
4195 prop = tmp;
4196 break;
4197 }
4198 }
4199 }
4200 if (prop != NULL) {
4201 value = xmlNodeListGetString(prop->doc, prop->children, 1);
4202 oldvalue = ctxt->state->value;
4203 ctxt->state->value = value;
4204 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
4205 value = ctxt->state->value;
4206 ctxt->state->value = oldvalue;
4207 if (value != NULL)
4208 xmlFree(value);
4209 if (ret == 0) {
4210 /*
4211 * flag the attribute as processed
4212 */
4213 ctxt->state->attrs[i] = NULL;
4214 }
4215 } else {
4216 ret = -1;
4217 }
4218#ifdef DEBUG
4219 xmlGenericError(xmlGenericErrorContext,
4220 "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret);
4221#endif
4222 } else {
4223 TODO
4224 }
4225
4226 return(ret);
4227}
4228
4229/**
4230 * xmlRelaxNGValidateAttributeList:
4231 * @ctxt: a Relax-NG validation context
4232 * @define: the list of definition to verify
4233 *
4234 * Validate the given node against the list of attribute definitions
4235 *
4236 * Returns 0 if the validation succeeded or an error code.
4237 */
4238static int
4239xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
4240 xmlRelaxNGDefinePtr defines) {
4241 int ret = 0;
4242 while (defines != NULL) {
4243 if (xmlRelaxNGValidateAttribute(ctxt, defines) != 0)
4244 ret = -1;
4245 defines = defines->next;
4246 }
4247 return(ret);
4248}
4249
4250/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004251 * xmlRelaxNGValidateTryPermutation:
4252 * @ctxt: a Relax-NG validation context
4253 * @groups: the array of groups
4254 * @nbgroups: the number of groups in the array
4255 * @array: the permutation to try
4256 * @len: the size of the set
4257 *
4258 * Try to validate a permutation for the group of definitions.
4259 *
4260 * Returns 0 if the validation succeeded or an error code.
4261 */
4262static int
4263xmlRelaxNGValidateTryPermutation(xmlRelaxNGValidCtxtPtr ctxt,
4264 xmlRelaxNGDefinePtr rule,
4265 xmlNodePtr *array, int len) {
4266 int i, ret;
4267
4268 if (len > 0) {
4269 /*
4270 * One only need the next pointer set-up to do the validation
4271 */
4272 for (i = 0;i < (len - 1);i++)
4273 array[i]->next = array[i + 1];
4274 array[i]->next = NULL;
4275
4276 /*
4277 * Now try to validate the sequence
4278 */
4279 ctxt->state->seq = array[0];
4280 ret = xmlRelaxNGValidateDefinition(ctxt, rule);
4281 } else {
4282 ctxt->state->seq = NULL;
4283 ret = xmlRelaxNGValidateDefinition(ctxt, rule);
4284 }
4285
4286 /*
4287 * the sequence must be fully consumed
4288 */
4289 if (ctxt->state->seq != NULL)
4290 return(-1);
4291
4292 return(ret);
4293}
4294
4295/**
4296 * xmlRelaxNGValidateWalkPermutations:
4297 * @ctxt: a Relax-NG validation context
4298 * @groups: the array of groups
4299 * @nbgroups: the number of groups in the array
4300 * @nodes: the set of nodes
4301 * @array: the current state of the parmutation
4302 * @len: the size of the set
4303 * @level: a pointer to the level variable
4304 * @k: the index in the array to fill
4305 *
4306 * Validate a set of nodes for a groups of definitions, will try the
4307 * full set of permutations
4308 *
4309 * Returns 0 if the validation succeeded or an error code.
4310 */
4311static int
4312xmlRelaxNGValidateWalkPermutations(xmlRelaxNGValidCtxtPtr ctxt,
4313 xmlRelaxNGDefinePtr rule, xmlNodePtr *nodes,
4314 xmlNodePtr *array, int len,
4315 int *level, int k) {
4316 int i, ret;
4317
4318 if ((k >= 0) && (k < len))
4319 array[k] = nodes[*level];
4320 *level = *level + 1;
4321 if (*level == len) {
4322 ret = xmlRelaxNGValidateTryPermutation(ctxt, rule, array, len);
4323 if (ret == 0)
4324 return(0);
4325 } else {
4326 for (i = 0;i < len;i++) {
4327 if (array[i] == NULL) {
4328 ret = xmlRelaxNGValidateWalkPermutations(ctxt, rule,
4329 nodes, array, len, level, i);
4330 if (ret == 0)
4331 return(0);
4332 }
4333 }
4334 }
4335 *level = *level - 1;
4336 array[k] = NULL;
4337 return(-1);
4338}
4339
4340/**
4341 * xmlRelaxNGNodeMatchesList:
4342 * @node: the node
4343 * @list: a NULL terminated array of definitions
4344 *
4345 * Check if a node can be matched by one of the definitions
4346 *
4347 * Returns 1 if matches 0 otherwise
4348 */
4349static int
4350xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) {
4351 xmlRelaxNGDefinePtr cur;
4352 int i = 0;
4353
4354 if ((node == NULL) || (list == NULL))
4355 return(0);
4356
4357 cur = list[i++];
4358 while (cur != NULL) {
4359 if ((node->type == XML_ELEMENT_NODE) &&
4360 (cur->type == XML_RELAXNG_ELEMENT)) {
4361 if (cur->name == NULL) {
4362 if ((node->ns != NULL) &&
4363 (xmlStrEqual(node->ns->href, cur->ns)))
4364 return(1);
4365 } else if (xmlStrEqual(cur->name, node->name)) {
4366 if ((cur->ns == NULL) || (cur->ns[0] == 0)) {
4367 if (node->ns == NULL)
4368 return(1);
4369 } else {
4370 if ((node->ns != NULL) &&
4371 (xmlStrEqual(node->ns->href, cur->ns)))
4372 return(1);
4373 }
4374 }
4375 } else if ((node->type == XML_TEXT_NODE) &&
4376 (cur->type == XML_RELAXNG_TEXT)) {
4377 return(1);
4378 }
4379 cur = list[i++];
4380 }
4381 return(0);
4382}
4383
4384/**
4385 * xmlRelaxNGValidatePartGroup:
4386 * @ctxt: a Relax-NG validation context
4387 * @groups: the array of groups
4388 * @nbgroups: the number of groups in the array
4389 * @nodes: the set of nodes
4390 * @len: the size of the set of nodes
4391 *
4392 * Validate a set of nodes for a groups of definitions
4393 *
4394 * Returns 0 if the validation succeeded or an error code.
4395 */
4396static int
4397xmlRelaxNGValidatePartGroup(xmlRelaxNGValidCtxtPtr ctxt,
4398 xmlRelaxNGInterleaveGroupPtr *groups,
4399 int nbgroups, xmlNodePtr *nodes, int len) {
Daniel Veillardb08c9812003-01-28 23:09:49 +00004400 int level, ret = -1, i, j, k;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004401 xmlNodePtr *array = NULL, *list, oldseq;
4402 xmlRelaxNGInterleaveGroupPtr group;
4403
4404 list = (xmlNodePtr *) xmlMalloc(len * sizeof(xmlNodePtr));
4405 if (list == NULL) {
4406 return(-1);
4407 }
4408 array = (xmlNodePtr *) xmlMalloc(len * sizeof(xmlNodePtr));
4409 if (array == NULL) {
4410 xmlFree(list);
4411 return(-1);
4412 }
4413 memset(array, 0, len * sizeof(xmlNodePtr));
4414
4415 /*
4416 * Partition the elements and validate the subsets.
4417 */
4418 oldseq = ctxt->state->seq;
4419 for (i = 0;i < nbgroups;i++) {
4420 group = groups[i];
4421 if (group == NULL)
4422 continue;
4423 k = 0;
4424 for (j = 0;j < len;j++) {
4425 if (nodes[j] == NULL)
4426 continue;
4427 if (xmlRelaxNGNodeMatchesList(nodes[j], group->defs)) {
4428 list[k++] = nodes[j];
4429 nodes[j] = NULL;
4430 }
4431 }
4432 ctxt->state->seq = oldseq;
4433 if (k > 1) {
4434 memset(array, 0, k * sizeof(xmlNodePtr));
Daniel Veillardb08c9812003-01-28 23:09:49 +00004435 level = -1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004436 ret = xmlRelaxNGValidateWalkPermutations(ctxt, group->rule,
4437 list, array, k, &level, -1);
4438 } else {
4439 ret = xmlRelaxNGValidateTryPermutation(ctxt, group->rule, list, k);
4440 }
4441 if (ret != 0) {
4442 ctxt->state->seq = oldseq;
4443 break;
4444 }
4445 }
4446
4447 xmlFree(list);
4448 xmlFree(array);
4449 return(ret);
4450}
4451
4452/**
4453 * xmlRelaxNGValidateInterleave:
4454 * @ctxt: a Relax-NG validation context
4455 * @define: the definition to verify
4456 *
4457 * Validate an interleave definition for a node.
4458 *
4459 * Returns 0 if the validation succeeded or an error code.
4460 */
4461static int
4462xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
4463 xmlRelaxNGDefinePtr define) {
4464 int ret = 0, nbchildren, nbtot, i, j;
4465 xmlRelaxNGPartitionPtr partitions;
4466 xmlNodePtr *children = NULL;
4467 xmlNodePtr *order = NULL;
4468 xmlNodePtr cur;
4469
4470 if (define->data != NULL) {
4471 partitions = (xmlRelaxNGPartitionPtr) define->data;
4472 } else {
4473 VALID_CTXT();
4474 VALID_ERROR("Internal: interleave block has no data\n");
4475 return(-1);
4476 }
4477
4478 /*
4479 * Build the sequence of child and an array preserving the children
4480 * initial order.
4481 */
4482 cur = ctxt->state->seq;
4483 nbchildren = 0;
4484 nbtot = 0;
4485 while (cur != NULL) {
4486 if ((cur->type == XML_COMMENT_NODE) ||
4487 (cur->type == XML_PI_NODE) ||
4488 ((cur->type == XML_TEXT_NODE) &&
4489 (IS_BLANK_NODE(cur)))) {
4490 nbtot++;
4491 } else {
4492 nbchildren++;
4493 nbtot++;
4494 }
4495 cur = cur->next;
4496 }
4497 children = (xmlNodePtr *) xmlMalloc(nbchildren * sizeof(xmlNodePtr));
4498 if (children == NULL)
4499 goto error;
4500 order = (xmlNodePtr *) xmlMalloc(nbtot * sizeof(xmlNodePtr));
4501 if (order == NULL)
4502 goto error;
4503 cur = ctxt->state->seq;
4504 i = 0;
4505 j = 0;
4506 while (cur != NULL) {
4507 if ((cur->type == XML_COMMENT_NODE) ||
4508 (cur->type == XML_PI_NODE) ||
4509 ((cur->type == XML_TEXT_NODE) &&
4510 (IS_BLANK_NODE(cur)))) {
4511 order[j++] = cur;
4512 } else {
4513 order[j++] = cur;
4514 children[i++] = cur;
4515 }
4516 cur = cur->next;
4517 }
4518
4519 /* TODO: retry with a maller set of child if there is a next... */
4520 ret = xmlRelaxNGValidatePartGroup(ctxt, partitions->groups,
4521 partitions->nbgroups, children, nbchildren);
4522 if (ret == 0) {
4523 ctxt->state->seq = NULL;
4524 }
4525
4526 /*
4527 * Cleanup: rebuid the child sequence and free the structure
4528 */
4529 if (order != NULL) {
4530 for (i = 0;i < nbtot;i++) {
4531 if (i == 0)
4532 order[i]->prev = NULL;
4533 else
4534 order[i]->prev = order[i - 1];
4535 if (i == nbtot - 1)
4536 order[i]->next = NULL;
4537 else
4538 order[i]->next = order[i + 1];
4539 }
4540 xmlFree(order);
4541 }
4542 if (children != NULL)
4543 xmlFree(children);
4544
4545 return(ret);
4546
4547error:
4548 if (order != NULL) {
4549 for (i = 0;i < nbtot;i++) {
4550 if (i == 0)
4551 order[i]->prev = NULL;
4552 else
4553 order[i]->prev = order[i - 1];
4554 if (i == nbtot - 1)
4555 order[i]->next = NULL;
4556 else
4557 order[i]->next = order[i + 1];
4558 }
4559 xmlFree(order);
4560 }
4561 if (children != NULL)
4562 xmlFree(children);
4563 return(-1);
4564}
4565
4566/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004567 * xmlRelaxNGValidateElementContent:
4568 * @ctxt: a Relax-NG validation context
4569 * @define: the list of definition to verify
4570 *
4571 * Validate the given node content against the (list) of definitions
4572 *
4573 * Returns 0 if the validation succeeded or an error code.
4574 */
4575static int
4576xmlRelaxNGValidateElementContent(xmlRelaxNGValidCtxtPtr ctxt,
4577 xmlRelaxNGDefinePtr defines) {
4578 int ret = 0, res;
4579
4580 if (ctxt->state == NULL) {
4581 VALID_CTXT();
4582 VALID_ERROR("Internal: no state\n");
4583 return(-1);
4584 }
4585 while (defines != NULL) {
4586 res = xmlRelaxNGValidateDefinition(ctxt, defines);
4587 if (res < 0)
4588 ret = -1;
4589 defines = defines->next;
4590 }
4591
4592 return(ret);
4593}
4594
4595/**
4596 * xmlRelaxNGValidateDefinition:
4597 * @ctxt: a Relax-NG validation context
4598 * @define: the definition to verify
4599 *
4600 * Validate the current node against the definition
4601 *
4602 * Returns 0 if the validation succeeded or an error code.
4603 */
4604static int
4605xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
4606 xmlRelaxNGDefinePtr define) {
4607 xmlNodePtr node;
4608 int ret = 0, i, tmp, oldflags;
4609 xmlRelaxNGValidStatePtr oldstate, state;
4610
4611 if (define == NULL) {
4612 VALID_CTXT();
4613 VALID_ERROR("internal error: define == NULL\n");
4614 return(-1);
4615 }
4616 if (ctxt->state != NULL) {
4617 node = ctxt->state->seq;
4618 } else {
4619 node = NULL;
4620 }
4621 switch (define->type) {
4622 case XML_RELAXNG_EMPTY:
4623 if (node != NULL) {
4624 VALID_CTXT();
4625 VALID_ERROR("Expecting an empty element\n");
4626 return(-1);
4627 }
4628#ifdef DEBUG
4629 xmlGenericError(xmlGenericErrorContext,
4630 "xmlRelaxNGValidateDefinition(): validated empty\n");
4631#endif
4632 return(0);
4633 case XML_RELAXNG_NOT_ALLOWED:
4634 TODO
4635 break;
4636 case XML_RELAXNG_TEXT:
4637 if (node == NULL)
4638 return(0);
4639 while ((node != NULL) &&
4640 ((node->type == XML_TEXT_NODE) ||
4641 (node->type == XML_CDATA_SECTION_NODE)))
4642 node = node->next;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004643 if (node == ctxt->state->seq) {
4644 VALID_CTXT();
4645 VALID_ERROR("Expecting text content\n");
4646 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004647 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004648 ctxt->state->seq = node;
4649 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004650 case XML_RELAXNG_ELEMENT:
4651 node = xmlRelaxNGSkipIgnored(ctxt, node);
4652 if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
4653 VALID_CTXT();
4654 VALID_ERROR("Expecting an element\n");
4655 return(-1);
4656 }
4657 if (define->name != NULL) {
4658 if (!xmlStrEqual(node->name, define->name)) {
4659 VALID_CTXT();
4660 VALID_ERROR("Expecting element %s, got %s\n",
4661 define->name, node->name);
4662 return(-1);
4663 }
4664 }
4665 if ((define->ns != NULL) && (define->ns[0] != 0)) {
4666 if (node->ns == NULL) {
4667 VALID_CTXT();
4668 VALID_ERROR("Expecting a namespace for element %s\n",
4669 node->name);
4670 return(-1);
4671 } else if (!xmlStrEqual(node->ns->href, define->ns)) {
4672 VALID_CTXT();
4673 VALID_ERROR("Expecting element %s has wrong namespace: expecting %s\n",
4674 node->name, define->ns);
4675 return(-1);
4676 }
4677 } else {
4678 if (node->ns != NULL) {
4679 VALID_CTXT();
4680 VALID_ERROR("Expecting no namespace for element %s\n",
4681 node->name);
4682 return(-1);
4683 }
4684 }
4685
4686 state = xmlRelaxNGNewValidState(ctxt, node);
4687 if (state == NULL) {
4688 return(-1);
4689 }
4690
4691 oldstate = ctxt->state;
4692 ctxt->state = state;
4693 if (define->attrs != NULL) {
4694 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
4695 if (tmp != 0)
4696 ret = -1;
4697 }
4698 if (define->content != NULL) {
4699 tmp = xmlRelaxNGValidateElementContent(ctxt, define->content);
4700 if (tmp != 0)
4701 ret = -1;
4702 }
4703 state = ctxt->state;
4704 if (state->seq != NULL) {
4705 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
4706 if (state->seq != NULL) {
4707 VALID_CTXT();
4708 VALID_ERROR("Extra content for element %s\n",
4709 node->name);
4710 ret = -1;
4711 }
4712 }
4713 for (i = 0;i < state->nbAttrs;i++) {
4714 if (state->attrs[i] != NULL) {
4715 VALID_CTXT();
Daniel Veillardea3f3982003-01-26 19:45:18 +00004716 VALID_ERROR("Invalid attribute %s for element %s\n",
Daniel Veillard6eadf632003-01-23 18:29:16 +00004717 state->attrs[i]->name, node->name);
4718 ret = -1;
4719 }
4720 }
4721 ctxt->state = oldstate;
4722 xmlRelaxNGFreeValidState(state);
4723 if (oldstate != NULL)
4724 oldstate->seq = node->next;
4725
4726
4727#ifdef DEBUG
4728 xmlGenericError(xmlGenericErrorContext,
4729 "xmlRelaxNGValidateDefinition(): validated %s : %d\n",
4730 node->name, ret);
4731#endif
4732 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004733 case XML_RELAXNG_OPTIONAL:
4734 oldflags = ctxt->flags;
4735 ctxt->flags |= FLAGS_IGNORABLE;
4736 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
4737 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
4738 if (ret != 0) {
4739 xmlRelaxNGFreeValidState(ctxt->state);
4740 ctxt->state = oldstate;
4741 ret = 0;
4742 break;
4743 }
4744 xmlRelaxNGFreeValidState(oldstate);
4745 ctxt->flags = oldflags;
4746 ret = 0;
4747 break;
4748 case XML_RELAXNG_ONEORMORE:
4749 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
4750 if (ret != 0) {
4751 break;
4752 }
4753 /* no break on purpose */
Daniel Veillard276be4a2003-01-24 01:03:34 +00004754 case XML_RELAXNG_ZEROORMORE: {
4755 xmlNodePtr cur, temp;
4756
Daniel Veillard6eadf632003-01-23 18:29:16 +00004757 oldflags = ctxt->flags;
4758 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004759 cur = ctxt->state->seq;
4760 temp = NULL;
4761 while ((cur != NULL) && (temp != cur)) {
4762 temp = cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004763 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
4764 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
4765 if (ret != 0) {
4766 xmlRelaxNGFreeValidState(ctxt->state);
4767 ctxt->state = oldstate;
4768 ret = 0;
4769 break;
4770 }
4771 xmlRelaxNGFreeValidState(oldstate);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004772 cur = ctxt->state->seq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004773 }
4774 ctxt->flags = oldflags;
4775 break;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004776 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004777 case XML_RELAXNG_CHOICE: {
4778 xmlRelaxNGDefinePtr list = define->content;
4779
4780 oldflags = ctxt->flags;
4781 ctxt->flags |= FLAGS_IGNORABLE;
4782
4783 while (list != NULL) {
4784 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
4785 ret = xmlRelaxNGValidateDefinition(ctxt, list);
4786 if (ret == 0) {
4787 xmlRelaxNGFreeValidState(oldstate);
4788 break;
4789 }
4790 xmlRelaxNGFreeValidState(ctxt->state);
4791 ctxt->state = oldstate;
4792 list = list->next;
4793 }
4794 ctxt->flags = oldflags;
4795 break;
4796 }
4797 case XML_RELAXNG_GROUP: {
4798 xmlRelaxNGDefinePtr list = define->content;
4799
4800 while (list != NULL) {
4801 ret = xmlRelaxNGValidateDefinition(ctxt, list);
4802 if (ret != 0)
4803 break;
4804 list = list->next;
4805 }
4806 break;
4807 }
4808 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004809 ret = xmlRelaxNGValidateInterleave(ctxt, define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004810 break;
4811 case XML_RELAXNG_ATTRIBUTE:
4812 ret = xmlRelaxNGValidateAttribute(ctxt, define);
4813 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004814 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004815 case XML_RELAXNG_REF:
4816 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
4817 break;
4818 case XML_RELAXNG_DEF:
4819 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
4820 break;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004821 case XML_RELAXNG_DATATYPE: {
4822 xmlChar *content;
4823
4824 content = xmlNodeGetContent(node);
4825 ret = xmlRelaxNGValidateDatatype(ctxt, content, define);
4826 if (ret == -1) {
4827 VALID_CTXT();
4828 VALID_ERROR("internal error validating %s\n", define->name);
4829 } else if (ret == 0) {
4830 ctxt->state->seq = node->next;
4831 }
4832 /*
4833 * TODO cover the problems with
4834 * <p>12<!-- comment -->34</p>
4835 * TODO detect full element coverage at compilation time.
4836 */
4837 if ((node != NULL) && (node->next != NULL)) {
4838 VALID_CTXT();
4839 VALID_ERROR("The data does not cover the full element %s\n",
4840 node->parent->name);
4841 ret = -1;
4842 }
4843 if (content != NULL)
4844 xmlFree(content);
4845 break;
4846 }
Daniel Veillardea3f3982003-01-26 19:45:18 +00004847 case XML_RELAXNG_VALUE: {
4848 xmlChar *content;
4849 xmlChar *oldvalue;
4850
4851 content = xmlNodeGetContent(node);
4852 oldvalue = ctxt->state->value;
4853 ctxt->state->value = content;
4854 ret = xmlRelaxNGValidateValue(ctxt, define);
4855 ctxt->state->value = oldvalue;
4856 if (ret == -1) {
4857 VALID_CTXT();
4858 VALID_ERROR("internal error validating %s\n", define->name);
4859 } else if (ret == 0) {
4860 ctxt->state->seq = node->next;
4861 }
4862 /*
4863 * TODO cover the problems with
4864 * <p>12<!-- comment -->34</p>
4865 * TODO detect full element coverage at compilation time.
4866 */
4867 if ((node != NULL) && (node->next != NULL)) {
4868 VALID_CTXT();
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004869 VALID_ERROR("The value does not cover the full element %s\n",
Daniel Veillardea3f3982003-01-26 19:45:18 +00004870 node->parent->name);
4871 ret = -1;
4872 }
4873 if (content != NULL)
4874 xmlFree(content);
4875 break;
4876 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004877 case XML_RELAXNG_LIST: {
4878 xmlChar *content;
4879 xmlChar *oldvalue, *oldendvalue;
4880 int len;
Daniel Veillardea3f3982003-01-26 19:45:18 +00004881
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004882 content = xmlNodeGetContent(node);
4883 len = xmlStrlen(content);
4884 oldvalue = ctxt->state->value;
4885 oldendvalue = ctxt->state->endvalue;
4886 ctxt->state->value = content;
4887 ctxt->state->endvalue = content + len;
4888 ret = xmlRelaxNGValidateValue(ctxt, define);
4889 ctxt->state->value = oldvalue;
4890 ctxt->state->endvalue = oldendvalue;
4891 if (ret == -1) {
4892 VALID_CTXT();
4893 VALID_ERROR("internal error validating list\n");
4894 } else if (ret == 0) {
4895 ctxt->state->seq = node->next;
4896 }
4897 /*
4898 * TODO cover the problems with
4899 * <p>12<!-- comment -->34</p>
4900 * TODO detect full element coverage at compilation time.
4901 */
4902 if ((node != NULL) && (node->next != NULL)) {
4903 VALID_CTXT();
4904 VALID_ERROR("The list does not cover the full element %s\n",
4905 node->parent->name);
4906 ret = -1;
4907 }
4908 if (content != NULL)
4909 xmlFree(content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004910 break;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004911 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004912 case XML_RELAXNG_START:
4913 TODO
4914 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004915 }
4916 return(ret);
4917}
4918
4919/**
4920 * xmlRelaxNGValidateDocument:
4921 * @ctxt: a Relax-NG validation context
4922 * @doc: the document
4923 *
4924 * Validate the given document
4925 *
4926 * Returns 0 if the validation succeeded or an error code.
4927 */
4928static int
4929xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
4930 int ret;
4931 xmlRelaxNGPtr schema;
4932 xmlRelaxNGGrammarPtr grammar;
4933 xmlRelaxNGValidStatePtr state;
4934
4935 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
4936 return(-1);
4937
4938 schema = ctxt->schema;
4939 grammar = schema->topgrammar;
4940 if (grammar == NULL) {
4941 VALID_CTXT();
4942 VALID_ERROR("No top grammar defined\n");
4943 return(-1);
4944 }
4945 state = xmlRelaxNGNewValidState(ctxt, NULL);
4946 ctxt->state = state;
4947 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
4948 state = ctxt->state;
4949 if ((state != NULL) && (state->seq != NULL)) {
4950 xmlNodePtr node;
4951
4952 node = state->seq;
4953 node = xmlRelaxNGSkipIgnored(ctxt, node);
4954 if (node != NULL) {
4955 VALID_CTXT();
4956 VALID_ERROR("extra data on the document\n");
4957 ret = -1;
4958 }
4959 }
4960 xmlRelaxNGFreeValidState(state);
4961
4962 return(ret);
4963}
4964
4965/************************************************************************
4966 * *
4967 * Validation interfaces *
4968 * *
4969 ************************************************************************/
4970/**
4971 * xmlRelaxNGNewValidCtxt:
4972 * @schema: a precompiled XML RelaxNGs
4973 *
4974 * Create an XML RelaxNGs validation context based on the given schema
4975 *
4976 * Returns the validation context or NULL in case of error
4977 */
4978xmlRelaxNGValidCtxtPtr
4979xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) {
4980 xmlRelaxNGValidCtxtPtr ret;
4981
4982 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
4983 if (ret == NULL) {
4984 xmlGenericError(xmlGenericErrorContext,
4985 "Failed to allocate new schama validation context\n");
4986 return (NULL);
4987 }
4988 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
4989 ret->schema = schema;
4990 return (ret);
4991}
4992
4993/**
4994 * xmlRelaxNGFreeValidCtxt:
4995 * @ctxt: the schema validation context
4996 *
4997 * Free the resources associated to the schema validation context
4998 */
4999void
5000xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) {
5001 if (ctxt == NULL)
5002 return;
5003 xmlFree(ctxt);
5004}
5005
5006/**
5007 * xmlRelaxNGSetValidErrors:
5008 * @ctxt: a Relax-NG validation context
5009 * @err: the error function
5010 * @warn: the warning function
5011 * @ctx: the functions context
5012 *
5013 * Set the error and warning callback informations
5014 */
5015void
5016xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
5017 xmlRelaxNGValidityErrorFunc err,
5018 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
5019 if (ctxt == NULL)
5020 return;
5021 ctxt->error = err;
5022 ctxt->warning = warn;
5023 ctxt->userData = ctx;
5024}
5025
5026/**
5027 * xmlRelaxNGValidateDoc:
5028 * @ctxt: a Relax-NG validation context
5029 * @doc: a parsed document tree
5030 *
5031 * Validate a document tree in memory.
5032 *
5033 * Returns 0 if the document is valid, a positive error code
5034 * number otherwise and -1 in case of internal or API error.
5035 */
5036int
5037xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
5038 int ret;
5039
5040 if ((ctxt == NULL) || (doc == NULL))
5041 return(-1);
5042
5043 ctxt->doc = doc;
5044
5045 ret = xmlRelaxNGValidateDocument(ctxt, doc);
5046 return(ret);
5047}
5048
5049#endif /* LIBXML_SCHEMAS_ENABLED */
5050