Coverage Report

Created: 2025-08-04 07:15

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