Line | Count | Source |
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 | | * Author: Daniel Veillard |
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_RELAXNG_ENABLED |
20 | | |
21 | | #include <errno.h> |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | #include <stdio.h> |
25 | | #include <stddef.h> |
26 | | #include <libxml/xmlmemory.h> |
27 | | #include <libxml/parser.h> |
28 | | #include <libxml/parserInternals.h> |
29 | | #include <libxml/hash.h> |
30 | | #include <libxml/uri.h> |
31 | | |
32 | | #include <libxml/relaxng.h> |
33 | | |
34 | | #include <libxml/xmlautomata.h> |
35 | | #include <libxml/xmlregexp.h> |
36 | | #include <libxml/xmlschemastypes.h> |
37 | | |
38 | | #include "private/error.h" |
39 | | #include "private/regexp.h" |
40 | | #include "private/string.h" |
41 | | #include "private/threads.h" |
42 | | |
43 | | /* |
44 | | * The Relax-NG namespace |
45 | | */ |
46 | | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
47 | | "http://relaxng.org/ns/structure/1.0"; |
48 | | |
49 | | /* |
50 | | * Default include limit, this can be override with RNG_INCLUDE_LIMIT |
51 | | * env variable |
52 | | */ |
53 | | static const int _xmlRelaxNGIncludeLimit = 1000; |
54 | | |
55 | | #define IS_RELAXNG(node, typ) \ |
56 | 0 | ((node != NULL) && (node->ns != NULL) && \ |
57 | 0 | (node->type == XML_ELEMENT_NODE) && \ |
58 | 0 | (xmlStrEqual(node->name, (const xmlChar *) typ)) && \ |
59 | 0 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
60 | | |
61 | | |
62 | 0 | #define MAX_ERROR 5 |
63 | | |
64 | | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
65 | | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
66 | | |
67 | | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
68 | | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
69 | | |
70 | | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
71 | | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
72 | | |
73 | | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
74 | | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
75 | | |
76 | | typedef enum { |
77 | | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
78 | | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
79 | | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
80 | | } xmlRelaxNGCombine; |
81 | | |
82 | | typedef enum { |
83 | | XML_RELAXNG_CONTENT_ERROR = -1, |
84 | | XML_RELAXNG_CONTENT_EMPTY = 0, |
85 | | XML_RELAXNG_CONTENT_SIMPLE, |
86 | | XML_RELAXNG_CONTENT_COMPLEX |
87 | | } xmlRelaxNGContentType; |
88 | | |
89 | | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
90 | | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
91 | | |
92 | | struct _xmlRelaxNGGrammar { |
93 | | xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */ |
94 | | xmlRelaxNGGrammarPtr children; /* the children grammar if any */ |
95 | | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
96 | | xmlRelaxNGDefinePtr start; /* <start> content */ |
97 | | xmlRelaxNGCombine combine; /* the default combine value */ |
98 | | xmlRelaxNGDefinePtr startList; /* list of <start> definitions */ |
99 | | xmlHashTablePtr defs; /* define* */ |
100 | | xmlHashTablePtr refs; /* references */ |
101 | | }; |
102 | | |
103 | | |
104 | | typedef enum { |
105 | | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
106 | | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
107 | | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
108 | | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
109 | | XML_RELAXNG_TEXT, /* textual content */ |
110 | | XML_RELAXNG_ELEMENT, /* an element */ |
111 | | XML_RELAXNG_DATATYPE, /* external data type definition */ |
112 | | XML_RELAXNG_PARAM, /* external data type parameter */ |
113 | | XML_RELAXNG_VALUE, /* value from an external data type definition */ |
114 | | XML_RELAXNG_LIST, /* a list of patterns */ |
115 | | XML_RELAXNG_ATTRIBUTE, /* an attribute following a pattern */ |
116 | | XML_RELAXNG_DEF, /* a definition */ |
117 | | XML_RELAXNG_REF, /* reference to a definition */ |
118 | | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
119 | | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
120 | | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
121 | | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
122 | | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
123 | | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
124 | | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
125 | | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
126 | | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
127 | | } xmlRelaxNGType; |
128 | | |
129 | 0 | #define IS_NULLABLE (1 << 0) |
130 | 0 | #define IS_NOT_NULLABLE (1 << 1) |
131 | 0 | #define IS_INDETERMINIST (1 << 2) |
132 | 0 | #define IS_MIXED (1 << 3) |
133 | 0 | #define IS_TRIABLE (1 << 4) |
134 | 0 | #define IS_PROCESSED (1 << 5) |
135 | 0 | #define IS_COMPILABLE (1 << 6) |
136 | 0 | #define IS_NOT_COMPILABLE (1 << 7) |
137 | 0 | #define IS_EXTERNAL_REF (1 << 8) |
138 | | |
139 | | struct _xmlRelaxNGDefine { |
140 | | xmlRelaxNGType type; /* the type of definition */ |
141 | | xmlNodePtr node; /* the node in the source */ |
142 | | xmlChar *name; /* the element local name if present */ |
143 | | xmlChar *ns; /* the namespace local name if present */ |
144 | | xmlChar *value; /* value when available */ |
145 | | void *data; /* data lib or specific pointer */ |
146 | | xmlRelaxNGDefinePtr content; /* the expected content */ |
147 | | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
148 | | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
149 | | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
150 | | xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */ |
151 | | xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */ |
152 | | short depth; /* used for the cycle detection */ |
153 | | short dflags; /* define related flags */ |
154 | | xmlRegexpPtr contModel; /* a compiled content model if available */ |
155 | | }; |
156 | | |
157 | | /** |
158 | | * A RelaxNGs definition |
159 | | */ |
160 | | struct _xmlRelaxNG { |
161 | | void *_private; /* unused by the library for users or bindings */ |
162 | | xmlRelaxNGGrammarPtr topgrammar; |
163 | | xmlDocPtr doc; |
164 | | |
165 | | int idref; /* requires idref checking */ |
166 | | |
167 | | xmlHashTablePtr defs; /* define */ |
168 | | xmlHashTablePtr refs; /* references */ |
169 | | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
170 | | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
171 | | int defNr; /* number of defines used */ |
172 | | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
173 | | |
174 | | }; |
175 | | |
176 | 0 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
177 | 0 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
178 | 0 | #define XML_RELAXNG_IN_LIST (1 << 2) |
179 | 0 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
180 | 0 | #define XML_RELAXNG_IN_START (1 << 4) |
181 | 0 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
182 | 0 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
183 | 0 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
184 | 0 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
185 | 0 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
186 | | |
187 | | struct _xmlRelaxNGParserCtxt { |
188 | | void *userData; /* user specific data block */ |
189 | | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
190 | | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
191 | | xmlStructuredErrorFunc serror; |
192 | | xmlRelaxNGValidErr err; |
193 | | |
194 | | xmlRelaxNGPtr schema; /* The schema in use */ |
195 | | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
196 | | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
197 | | int flags; /* parser flags */ |
198 | | int nbErrors; /* number of errors at parse time */ |
199 | | int nbWarnings; /* number of warnings at parse time */ |
200 | | const xmlChar *define; /* the current define scope */ |
201 | | xmlRelaxNGDefinePtr def; /* the current define */ |
202 | | |
203 | | int nbInterleaves; |
204 | | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
205 | | |
206 | | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
207 | | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
208 | | xmlChar *URL; |
209 | | xmlDocPtr document; |
210 | | |
211 | | int defNr; /* number of defines used */ |
212 | | int defMax; /* number of defines allocated */ |
213 | | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
214 | | |
215 | | const char *buffer; |
216 | | int size; |
217 | | |
218 | | /* the document stack */ |
219 | | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
220 | | int docNr; /* Depth of the parsing stack */ |
221 | | int docMax; /* Max depth of the parsing stack */ |
222 | | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
223 | | |
224 | | /* the include stack */ |
225 | | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
226 | | int incNr; /* Depth of the include parsing stack */ |
227 | | int incMax; /* Max depth of the parsing stack */ |
228 | | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
229 | | int incLimit; /* Include limit, to avoid stack-overflow on parse */ |
230 | | |
231 | | int idref; /* requires idref checking */ |
232 | | |
233 | | /* used to compile content models */ |
234 | | xmlAutomataPtr am; /* the automata */ |
235 | | xmlAutomataStatePtr state; /* used to build the automata */ |
236 | | |
237 | | int crng; /* compact syntax and other flags */ |
238 | | int freedoc; /* need to free the document */ |
239 | | |
240 | | xmlResourceLoader resourceLoader; |
241 | | void *resourceCtxt; |
242 | | }; |
243 | | |
244 | 0 | #define FLAGS_IGNORABLE 1 |
245 | 0 | #define FLAGS_NEGATIVE 2 |
246 | 0 | #define FLAGS_MIXED_CONTENT 4 |
247 | 0 | #define FLAGS_NOERROR 8 |
248 | | |
249 | | /** |
250 | | * A RelaxNGs partition set associated to lists of definitions |
251 | | */ |
252 | | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
253 | | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
254 | | struct _xmlRelaxNGInterleaveGroup { |
255 | | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
256 | | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
257 | | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
258 | | }; |
259 | | |
260 | 0 | #define IS_DETERMINIST 1 |
261 | 0 | #define IS_NEEDCHECK 2 |
262 | | |
263 | | /** |
264 | | * A RelaxNGs partition associated to an interleave group |
265 | | */ |
266 | | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
267 | | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
268 | | struct _xmlRelaxNGPartition { |
269 | | int nbgroups; /* number of groups in the partitions */ |
270 | | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
271 | | * right group when possible */ |
272 | | int flags; /* determinist ? */ |
273 | | xmlRelaxNGInterleaveGroupPtr *groups; |
274 | | }; |
275 | | |
276 | | /** |
277 | | * A RelaxNGs validation state |
278 | | */ |
279 | 0 | #define MAX_ATTR 20 |
280 | | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
281 | | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
282 | | struct _xmlRelaxNGValidState { |
283 | | xmlNodePtr node; /* the current node */ |
284 | | xmlNodePtr seq; /* the sequence of children left to validate */ |
285 | | int nbAttrs; /* the number of attributes */ |
286 | | int maxAttrs; /* the size of attrs */ |
287 | | int nbAttrLeft; /* the number of attributes left to validate */ |
288 | | xmlChar *value; /* the value when operating on string */ |
289 | | xmlChar *endvalue; /* the end value when operating on string */ |
290 | | xmlAttrPtr *attrs; /* the array of attributes */ |
291 | | }; |
292 | | |
293 | | /** |
294 | | * A RelaxNGs container for validation state |
295 | | */ |
296 | | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
297 | | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
298 | | struct _xmlRelaxNGStates { |
299 | | int nbState; /* the number of states */ |
300 | | int maxState; /* the size of the array */ |
301 | | xmlRelaxNGValidStatePtr *tabState; |
302 | | }; |
303 | | |
304 | 0 | #define ERROR_IS_DUP 1 |
305 | | |
306 | | /** |
307 | | * A RelaxNGs validation error |
308 | | */ |
309 | | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
310 | | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
311 | | struct _xmlRelaxNGValidError { |
312 | | xmlRelaxNGValidErr err; /* the error number */ |
313 | | int flags; /* flags */ |
314 | | xmlNodePtr node; /* the current node */ |
315 | | xmlNodePtr seq; /* the current child */ |
316 | | const xmlChar *arg1; /* first arg */ |
317 | | const xmlChar *arg2; /* second arg */ |
318 | | }; |
319 | | |
320 | | /** |
321 | | * A RelaxNGs validation context |
322 | | */ |
323 | | |
324 | | struct _xmlRelaxNGValidCtxt { |
325 | | void *userData; /* user specific data block */ |
326 | | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
327 | | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
328 | | xmlStructuredErrorFunc serror; |
329 | | int nbErrors; /* number of errors in validation */ |
330 | | |
331 | | xmlRelaxNGPtr schema; /* The schema in use */ |
332 | | xmlDocPtr doc; /* the document being validated */ |
333 | | int flags; /* validation flags */ |
334 | | int depth; /* validation depth */ |
335 | | int idref; /* requires idref checking */ |
336 | | int errNo; /* the first error found */ |
337 | | |
338 | | /* |
339 | | * Errors accumulated in branches may have to be stacked to be |
340 | | * provided back when it's sure they affect validation. |
341 | | */ |
342 | | xmlRelaxNGValidErrorPtr err; /* Last error */ |
343 | | int errNr; /* Depth of the error stack */ |
344 | | int errMax; /* Max depth of the error stack */ |
345 | | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
346 | | |
347 | | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
348 | | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
349 | | |
350 | | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
351 | | int freeStatesNr; |
352 | | int freeStatesMax; |
353 | | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
354 | | |
355 | | /* |
356 | | * This is used for "progressive" validation |
357 | | */ |
358 | | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
359 | | int elemNr; /* the number of element validated */ |
360 | | int elemMax; /* the max depth of elements */ |
361 | | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
362 | | int pstate; /* progressive state */ |
363 | | xmlNodePtr pnode; /* the current node */ |
364 | | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
365 | | int perr; /* signal error in content model |
366 | | * outside the regexp */ |
367 | | }; |
368 | | |
369 | | /** |
370 | | * Structure associated to a RelaxNGs document element |
371 | | */ |
372 | | struct _xmlRelaxNGInclude { |
373 | | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
374 | | xmlChar *href; /* the normalized href value */ |
375 | | xmlDocPtr doc; /* the associated XML document */ |
376 | | xmlRelaxNGDefinePtr content; /* the definitions */ |
377 | | xmlRelaxNGPtr schema; /* the schema */ |
378 | | }; |
379 | | |
380 | | /** |
381 | | * Structure associated to a RelaxNGs document element |
382 | | */ |
383 | | struct _xmlRelaxNGDocument { |
384 | | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
385 | | xmlChar *href; /* the normalized href value */ |
386 | | xmlDocPtr doc; /* the associated XML document */ |
387 | | xmlRelaxNGDefinePtr content; /* the definitions */ |
388 | | xmlRelaxNGPtr schema; /* the schema */ |
389 | | int externalRef; /* 1 if an external ref */ |
390 | | }; |
391 | | |
392 | | |
393 | | /************************************************************************ |
394 | | * * |
395 | | * Some factorized error routines * |
396 | | * * |
397 | | ************************************************************************/ |
398 | | |
399 | | /** |
400 | | * Handle a redefinition of attribute error |
401 | | * |
402 | | * @param ctxt an Relax-NG parser context |
403 | | */ |
404 | | static void |
405 | | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt) |
406 | 0 | { |
407 | 0 | xmlStructuredErrorFunc schannel = NULL; |
408 | 0 | xmlGenericErrorFunc channel = NULL; |
409 | 0 | void *data = NULL; |
410 | |
|
411 | 0 | if (ctxt != NULL) { |
412 | 0 | if (ctxt->serror != NULL) |
413 | 0 | schannel = ctxt->serror; |
414 | 0 | else |
415 | 0 | channel = ctxt->error; |
416 | 0 | data = ctxt->userData; |
417 | 0 | ctxt->nbErrors++; |
418 | 0 | } |
419 | |
|
420 | 0 | xmlRaiseMemoryError(schannel, channel, data, XML_FROM_RELAXNGP, NULL); |
421 | 0 | } |
422 | | |
423 | | /** |
424 | | * Handle a redefinition of attribute error |
425 | | * |
426 | | * @param ctxt a Relax-NG validation context |
427 | | */ |
428 | | static void |
429 | | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt) |
430 | 0 | { |
431 | 0 | xmlStructuredErrorFunc schannel = NULL; |
432 | 0 | xmlGenericErrorFunc channel = NULL; |
433 | 0 | void *data = NULL; |
434 | |
|
435 | 0 | if (ctxt != NULL) { |
436 | 0 | if (ctxt->serror != NULL) |
437 | 0 | schannel = ctxt->serror; |
438 | 0 | else |
439 | 0 | channel = ctxt->error; |
440 | 0 | data = ctxt->userData; |
441 | 0 | ctxt->nbErrors++; |
442 | 0 | } |
443 | |
|
444 | 0 | xmlRaiseMemoryError(schannel, channel, data, XML_FROM_RELAXNGV, NULL); |
445 | 0 | } |
446 | | |
447 | | /** |
448 | | * Handle a Relax NG Parsing error |
449 | | * |
450 | | * @param ctxt a Relax-NG parser context |
451 | | * @param node the node raising the error |
452 | | * @param error the error code |
453 | | * @param msg message |
454 | | * @param str1 extra info |
455 | | * @param str2 extra info |
456 | | */ |
457 | | static void LIBXML_ATTR_FORMAT(4,0) |
458 | | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
459 | | const char *msg, const xmlChar * str1, const xmlChar * str2) |
460 | 0 | { |
461 | 0 | xmlStructuredErrorFunc schannel = NULL; |
462 | 0 | xmlGenericErrorFunc channel = NULL; |
463 | 0 | void *data = NULL; |
464 | 0 | int res; |
465 | |
|
466 | 0 | if (ctxt != NULL) { |
467 | 0 | if (ctxt->serror != NULL) |
468 | 0 | schannel = ctxt->serror; |
469 | 0 | else |
470 | 0 | channel = ctxt->error; |
471 | 0 | data = ctxt->userData; |
472 | 0 | ctxt->nbErrors++; |
473 | 0 | } |
474 | |
|
475 | 0 | if ((channel == NULL) && (schannel == NULL)) { |
476 | 0 | channel = xmlGenericError; |
477 | 0 | data = xmlGenericErrorContext; |
478 | 0 | } |
479 | |
|
480 | 0 | res = xmlRaiseError(schannel, channel, data, NULL, node, |
481 | 0 | XML_FROM_RELAXNGP, error, XML_ERR_ERROR, NULL, 0, |
482 | 0 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
483 | 0 | msg, str1, str2); |
484 | 0 | if (res < 0) |
485 | 0 | xmlRngPErrMemory(ctxt); |
486 | 0 | } |
487 | | |
488 | | /** |
489 | | * Handle a Relax NG Validation error |
490 | | * |
491 | | * @param ctxt a Relax-NG validation context |
492 | | * @param node the node raising the error |
493 | | * @param error the error code |
494 | | * @param msg message |
495 | | * @param str1 extra info |
496 | | * @param str2 extra info |
497 | | */ |
498 | | static void LIBXML_ATTR_FORMAT(4,0) |
499 | | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
500 | | const char *msg, const xmlChar * str1, const xmlChar * str2) |
501 | 0 | { |
502 | 0 | xmlStructuredErrorFunc schannel = NULL; |
503 | 0 | xmlGenericErrorFunc channel = NULL; |
504 | 0 | void *data = NULL; |
505 | 0 | int res; |
506 | |
|
507 | 0 | if (ctxt != NULL) { |
508 | 0 | if (ctxt->serror != NULL) |
509 | 0 | schannel = ctxt->serror; |
510 | 0 | else |
511 | 0 | channel = ctxt->error; |
512 | 0 | data = ctxt->userData; |
513 | 0 | ctxt->nbErrors++; |
514 | 0 | } |
515 | |
|
516 | 0 | if ((channel == NULL) && (schannel == NULL)) { |
517 | 0 | channel = xmlGenericError; |
518 | 0 | data = xmlGenericErrorContext; |
519 | 0 | } |
520 | |
|
521 | 0 | res = xmlRaiseError(schannel, channel, data, NULL, node, |
522 | 0 | XML_FROM_RELAXNGV, error, XML_ERR_ERROR, NULL, 0, |
523 | 0 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
524 | 0 | msg, str1, str2); |
525 | 0 | if (res < 0) |
526 | 0 | xmlRngVErrMemory(ctxt); |
527 | 0 | } |
528 | | |
529 | | /************************************************************************ |
530 | | * * |
531 | | * Preliminary type checking interfaces * |
532 | | * * |
533 | | ************************************************************************/ |
534 | | |
535 | | /** |
536 | | * Function provided by a type library to check if a type is exported |
537 | | * |
538 | | * @param data data needed for the library |
539 | | * @param type the type name |
540 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
541 | | */ |
542 | | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
543 | | |
544 | | /** |
545 | | * Function provided by a type library to check if a value match a type |
546 | | * |
547 | | * @param data data needed for the library |
548 | | * @param type the type name |
549 | | * @param value the value to check |
550 | | * @param result place to store the result if needed |
551 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
552 | | */ |
553 | | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
554 | | const xmlChar * value, void **result, |
555 | | xmlNode *node); |
556 | | |
557 | | /** |
558 | | * Function provided by a type library to check a value facet |
559 | | * |
560 | | * @param data data needed for the library |
561 | | * @param type the type name |
562 | | * @param facet the facet name |
563 | | * @param val the facet value |
564 | | * @param strval the string value |
565 | | * @param value the value to check |
566 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
567 | | */ |
568 | | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
569 | | const xmlChar * facet, |
570 | | const xmlChar * val, |
571 | | const xmlChar * strval, void *value); |
572 | | |
573 | | /** |
574 | | * Function provided by a type library to free a returned result |
575 | | * |
576 | | * @param data data needed for the library |
577 | | * @param result the value to free |
578 | | */ |
579 | | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
580 | | |
581 | | /** |
582 | | * Function provided by a type library to compare two values accordingly |
583 | | * to a type. |
584 | | * |
585 | | * @param data data needed for the library |
586 | | * @param type the type name |
587 | | * @param value1 the first value |
588 | | * @param value2 the second value |
589 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
590 | | */ |
591 | | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
592 | | const xmlChar * value1, |
593 | | xmlNode *ctxt1, |
594 | | void *comp1, |
595 | | const xmlChar * value2, |
596 | | xmlNode *ctxt2); |
597 | | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
598 | | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
599 | | struct _xmlRelaxNGTypeLibrary { |
600 | | const xmlChar *namespace; /* the datatypeLibrary value */ |
601 | | void *data; /* data needed for the library */ |
602 | | xmlRelaxNGTypeHave have; /* the export function */ |
603 | | xmlRelaxNGTypeCheck check; /* the checking function */ |
604 | | xmlRelaxNGTypeCompare comp; /* the compare function */ |
605 | | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
606 | | xmlRelaxNGTypeFree freef; /* the freeing function */ |
607 | | }; |
608 | | |
609 | | /************************************************************************ |
610 | | * * |
611 | | * Allocation functions * |
612 | | * * |
613 | | ************************************************************************/ |
614 | | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
615 | | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
616 | | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
617 | | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
618 | | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
619 | | ATTRIBUTE_UNUSED, |
620 | | xmlRelaxNGValidStatePtr state1, |
621 | | xmlRelaxNGValidStatePtr state2); |
622 | | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
623 | | xmlRelaxNGValidStatePtr state); |
624 | | |
625 | | /** |
626 | | * Deallocate a RelaxNG document structure. |
627 | | * |
628 | | * @param docu a document structure |
629 | | */ |
630 | | static void |
631 | | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
632 | 0 | { |
633 | 0 | if (docu == NULL) |
634 | 0 | return; |
635 | | |
636 | 0 | if (docu->href != NULL) |
637 | 0 | xmlFree(docu->href); |
638 | 0 | if (docu->doc != NULL) |
639 | 0 | xmlFreeDoc(docu->doc); |
640 | 0 | if (docu->schema != NULL) |
641 | 0 | xmlRelaxNGFreeInnerSchema(docu->schema); |
642 | 0 | xmlFree(docu); |
643 | 0 | } |
644 | | |
645 | | /** |
646 | | * Deallocate a RelaxNG document structures. |
647 | | * |
648 | | * @param docu a list of document structure |
649 | | */ |
650 | | static void |
651 | | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
652 | 0 | { |
653 | 0 | xmlRelaxNGDocumentPtr next; |
654 | |
|
655 | 0 | while (docu != NULL) { |
656 | 0 | next = docu->next; |
657 | 0 | xmlRelaxNGFreeDocument(docu); |
658 | 0 | docu = next; |
659 | 0 | } |
660 | 0 | } |
661 | | |
662 | | /** |
663 | | * Deallocate a RelaxNG include structure. |
664 | | * |
665 | | * @param incl a include structure |
666 | | */ |
667 | | static void |
668 | | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
669 | 0 | { |
670 | 0 | if (incl == NULL) |
671 | 0 | return; |
672 | | |
673 | 0 | if (incl->href != NULL) |
674 | 0 | xmlFree(incl->href); |
675 | 0 | if (incl->doc != NULL) |
676 | 0 | xmlFreeDoc(incl->doc); |
677 | 0 | if (incl->schema != NULL) |
678 | 0 | xmlRelaxNGFree(incl->schema); |
679 | 0 | xmlFree(incl); |
680 | 0 | } |
681 | | |
682 | | /** |
683 | | * Deallocate a RelaxNG include structure. |
684 | | * |
685 | | * @param incl a include structure list |
686 | | */ |
687 | | static void |
688 | | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
689 | 0 | { |
690 | 0 | xmlRelaxNGIncludePtr next; |
691 | |
|
692 | 0 | while (incl != NULL) { |
693 | 0 | next = incl->next; |
694 | 0 | xmlRelaxNGFreeInclude(incl); |
695 | 0 | incl = next; |
696 | 0 | } |
697 | 0 | } |
698 | | |
699 | | /** |
700 | | * Allocate a new RelaxNG structure. |
701 | | * |
702 | | * @param ctxt a Relax-NG validation context (optional) |
703 | | * @returns the newly allocated structure or NULL in case or error |
704 | | */ |
705 | | static xmlRelaxNGPtr |
706 | | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
707 | 0 | { |
708 | 0 | xmlRelaxNGPtr ret; |
709 | |
|
710 | 0 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
711 | 0 | if (ret == NULL) { |
712 | 0 | xmlRngPErrMemory(ctxt); |
713 | 0 | return (NULL); |
714 | 0 | } |
715 | 0 | memset(ret, 0, sizeof(xmlRelaxNG)); |
716 | |
|
717 | 0 | return (ret); |
718 | 0 | } |
719 | | |
720 | | /** |
721 | | * Deallocate a RelaxNG schema structure. |
722 | | * |
723 | | * @param schema a schema structure |
724 | | */ |
725 | | static void |
726 | | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
727 | 0 | { |
728 | 0 | if (schema == NULL) |
729 | 0 | return; |
730 | | |
731 | 0 | if (schema->doc != NULL) |
732 | 0 | xmlFreeDoc(schema->doc); |
733 | 0 | if (schema->defTab != NULL) { |
734 | 0 | int i; |
735 | |
|
736 | 0 | for (i = 0; i < schema->defNr; i++) |
737 | 0 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
738 | 0 | xmlFree(schema->defTab); |
739 | 0 | } |
740 | |
|
741 | 0 | xmlFree(schema); |
742 | 0 | } |
743 | | |
744 | | /** |
745 | | * Deallocate a RelaxNG structure. |
746 | | * |
747 | | * @param schema a schema structure |
748 | | */ |
749 | | void |
750 | | xmlRelaxNGFree(xmlRelaxNG *schema) |
751 | 0 | { |
752 | 0 | if (schema == NULL) |
753 | 0 | return; |
754 | | |
755 | 0 | if (schema->topgrammar != NULL) |
756 | 0 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
757 | 0 | if (schema->doc != NULL) |
758 | 0 | xmlFreeDoc(schema->doc); |
759 | 0 | if (schema->documents != NULL) |
760 | 0 | xmlRelaxNGFreeDocumentList(schema->documents); |
761 | 0 | if (schema->includes != NULL) |
762 | 0 | xmlRelaxNGFreeIncludeList(schema->includes); |
763 | 0 | if (schema->defTab != NULL) { |
764 | 0 | int i; |
765 | |
|
766 | 0 | for (i = 0; i < schema->defNr; i++) |
767 | 0 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
768 | 0 | xmlFree(schema->defTab); |
769 | 0 | } |
770 | |
|
771 | 0 | xmlFree(schema); |
772 | 0 | } |
773 | | |
774 | | /** |
775 | | * Allocate a new RelaxNG grammar. |
776 | | * |
777 | | * @param ctxt a Relax-NG validation context (optional) |
778 | | * @returns the newly allocated structure or NULL in case or error |
779 | | */ |
780 | | static xmlRelaxNGGrammarPtr |
781 | | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
782 | 0 | { |
783 | 0 | xmlRelaxNGGrammarPtr ret; |
784 | |
|
785 | 0 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
786 | 0 | if (ret == NULL) { |
787 | 0 | xmlRngPErrMemory(ctxt); |
788 | 0 | return (NULL); |
789 | 0 | } |
790 | 0 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
791 | |
|
792 | 0 | return (ret); |
793 | 0 | } |
794 | | |
795 | | /** |
796 | | * Deallocate a RelaxNG grammar structure. |
797 | | * |
798 | | * @param grammar a grammar structure |
799 | | */ |
800 | | static void |
801 | | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
802 | 0 | { |
803 | 0 | if (grammar == NULL) |
804 | 0 | return; |
805 | | |
806 | 0 | if (grammar->children != NULL) { |
807 | 0 | xmlRelaxNGFreeGrammar(grammar->children); |
808 | 0 | } |
809 | 0 | if (grammar->next != NULL) { |
810 | 0 | xmlRelaxNGFreeGrammar(grammar->next); |
811 | 0 | } |
812 | 0 | if (grammar->refs != NULL) { |
813 | 0 | xmlHashFree(grammar->refs, NULL); |
814 | 0 | } |
815 | 0 | if (grammar->defs != NULL) { |
816 | 0 | xmlHashFree(grammar->defs, NULL); |
817 | 0 | } |
818 | |
|
819 | 0 | xmlFree(grammar); |
820 | 0 | } |
821 | | |
822 | | /** |
823 | | * Allocate a new RelaxNG define. |
824 | | * |
825 | | * @param ctxt a Relax-NG validation context |
826 | | * @param node the node in the input document. |
827 | | * @returns the newly allocated structure or NULL in case or error |
828 | | */ |
829 | | static xmlRelaxNGDefinePtr |
830 | | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
831 | 0 | { |
832 | 0 | xmlRelaxNGDefinePtr ret; |
833 | |
|
834 | 0 | if (ctxt->defMax == 0) { |
835 | 0 | ctxt->defMax = 16; |
836 | 0 | ctxt->defNr = 0; |
837 | 0 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
838 | 0 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
839 | 0 | if (ctxt->defTab == NULL) { |
840 | 0 | xmlRngPErrMemory(ctxt); |
841 | 0 | return (NULL); |
842 | 0 | } |
843 | 0 | } else if (ctxt->defMax <= ctxt->defNr) { |
844 | 0 | xmlRelaxNGDefinePtr *tmp; |
845 | |
|
846 | 0 | ctxt->defMax *= 2; |
847 | 0 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
848 | 0 | ctxt->defMax * |
849 | 0 | sizeof |
850 | 0 | (xmlRelaxNGDefinePtr)); |
851 | 0 | if (tmp == NULL) { |
852 | 0 | xmlRngPErrMemory(ctxt); |
853 | 0 | return (NULL); |
854 | 0 | } |
855 | 0 | ctxt->defTab = tmp; |
856 | 0 | } |
857 | 0 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
858 | 0 | if (ret == NULL) { |
859 | 0 | xmlRngPErrMemory(ctxt); |
860 | 0 | return (NULL); |
861 | 0 | } |
862 | 0 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
863 | 0 | ctxt->defTab[ctxt->defNr++] = ret; |
864 | 0 | ret->node = node; |
865 | 0 | ret->depth = -1; |
866 | 0 | return (ret); |
867 | 0 | } |
868 | | |
869 | | /** |
870 | | * Deallocate RelaxNG partition set structures. |
871 | | * |
872 | | * @param partitions a partition set structure |
873 | | */ |
874 | | static void |
875 | | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
876 | 0 | { |
877 | 0 | xmlRelaxNGInterleaveGroupPtr group; |
878 | 0 | int j; |
879 | |
|
880 | 0 | if (partitions != NULL) { |
881 | 0 | if (partitions->groups != NULL) { |
882 | 0 | for (j = 0; j < partitions->nbgroups; j++) { |
883 | 0 | group = partitions->groups[j]; |
884 | 0 | if (group != NULL) { |
885 | 0 | if (group->defs != NULL) |
886 | 0 | xmlFree(group->defs); |
887 | 0 | if (group->attrs != NULL) |
888 | 0 | xmlFree(group->attrs); |
889 | 0 | xmlFree(group); |
890 | 0 | } |
891 | 0 | } |
892 | 0 | xmlFree(partitions->groups); |
893 | 0 | } |
894 | 0 | if (partitions->triage != NULL) { |
895 | 0 | xmlHashFree(partitions->triage, NULL); |
896 | 0 | } |
897 | 0 | xmlFree(partitions); |
898 | 0 | } |
899 | 0 | } |
900 | | |
901 | | /** |
902 | | * Deallocate a RelaxNG define structure. |
903 | | * |
904 | | * @param define a define structure |
905 | | */ |
906 | | static void |
907 | | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
908 | 0 | { |
909 | 0 | if (define == NULL) |
910 | 0 | return; |
911 | | |
912 | 0 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
913 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
914 | |
|
915 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
916 | 0 | if ((lib != NULL) && (lib->freef != NULL)) |
917 | 0 | lib->freef(lib->data, (void *) define->attrs); |
918 | 0 | } |
919 | 0 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
920 | 0 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
921 | 0 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
922 | 0 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
923 | 0 | if (define->name != NULL) |
924 | 0 | xmlFree(define->name); |
925 | 0 | if (define->ns != NULL) |
926 | 0 | xmlFree(define->ns); |
927 | 0 | if (define->value != NULL) |
928 | 0 | xmlFree(define->value); |
929 | 0 | if (define->contModel != NULL) |
930 | 0 | xmlRegFreeRegexp(define->contModel); |
931 | 0 | xmlFree(define); |
932 | 0 | } |
933 | | |
934 | | /** |
935 | | * Allocate a new RelaxNG validation state container |
936 | | * |
937 | | * @param ctxt a Relax-NG validation context |
938 | | * @param size the default size for the container |
939 | | * @returns the newly allocated structure or NULL in case or error |
940 | | */ |
941 | | static xmlRelaxNGStatesPtr |
942 | | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
943 | 0 | { |
944 | 0 | xmlRelaxNGStatesPtr ret; |
945 | |
|
946 | 0 | if ((ctxt != NULL) && |
947 | 0 | (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) { |
948 | 0 | ctxt->freeStatesNr--; |
949 | 0 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
950 | 0 | ret->nbState = 0; |
951 | 0 | return (ret); |
952 | 0 | } |
953 | 0 | if (size < 16) |
954 | 0 | size = 16; |
955 | |
|
956 | 0 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
957 | 0 | (size - |
958 | 0 | 1) * |
959 | 0 | sizeof(xmlRelaxNGValidStatePtr)); |
960 | 0 | if (ret == NULL) { |
961 | 0 | xmlRngVErrMemory(ctxt); |
962 | 0 | return (NULL); |
963 | 0 | } |
964 | 0 | ret->nbState = 0; |
965 | 0 | ret->maxState = size; |
966 | 0 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
967 | 0 | sizeof |
968 | 0 | (xmlRelaxNGValidStatePtr)); |
969 | 0 | if (ret->tabState == NULL) { |
970 | 0 | xmlRngVErrMemory(ctxt); |
971 | 0 | xmlFree(ret); |
972 | 0 | return (NULL); |
973 | 0 | } |
974 | 0 | return (ret); |
975 | 0 | } |
976 | | |
977 | | /** |
978 | | * Add a RelaxNG validation state to the container without checking |
979 | | * for unicity. |
980 | | * |
981 | | * @param ctxt a Relax-NG validation context |
982 | | * @param states the states container |
983 | | * @param state the validation state |
984 | | * @returns 1 in case of success and 0 if this is a duplicate and -1 on error |
985 | | */ |
986 | | static int |
987 | | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
988 | | xmlRelaxNGStatesPtr states, |
989 | | xmlRelaxNGValidStatePtr state) |
990 | 0 | { |
991 | 0 | if (state == NULL) { |
992 | 0 | return (-1); |
993 | 0 | } |
994 | 0 | if (states->nbState >= states->maxState) { |
995 | 0 | xmlRelaxNGValidStatePtr *tmp; |
996 | 0 | int size; |
997 | |
|
998 | 0 | size = states->maxState * 2; |
999 | 0 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
1000 | 0 | (size) * |
1001 | 0 | sizeof |
1002 | 0 | (xmlRelaxNGValidStatePtr)); |
1003 | 0 | if (tmp == NULL) { |
1004 | 0 | xmlRngVErrMemory(ctxt); |
1005 | 0 | return (-1); |
1006 | 0 | } |
1007 | 0 | states->tabState = tmp; |
1008 | 0 | states->maxState = size; |
1009 | 0 | } |
1010 | 0 | states->tabState[states->nbState++] = state; |
1011 | 0 | return (1); |
1012 | 0 | } |
1013 | | |
1014 | | /** |
1015 | | * Add a RelaxNG validation state to the container |
1016 | | * |
1017 | | * @param ctxt a Relax-NG validation context |
1018 | | * @param states the states container |
1019 | | * @param state the validation state |
1020 | | * @returns 1 in case of success and 0 if this is a duplicate and -1 on error |
1021 | | */ |
1022 | | static int |
1023 | | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
1024 | | xmlRelaxNGStatesPtr states, |
1025 | | xmlRelaxNGValidStatePtr state) |
1026 | 0 | { |
1027 | 0 | int i; |
1028 | |
|
1029 | 0 | if (state == NULL || states == NULL) { |
1030 | 0 | return (-1); |
1031 | 0 | } |
1032 | 0 | if (states->nbState >= states->maxState) { |
1033 | 0 | xmlRelaxNGValidStatePtr *tmp; |
1034 | 0 | int size; |
1035 | |
|
1036 | 0 | size = states->maxState * 2; |
1037 | 0 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
1038 | 0 | (size) * |
1039 | 0 | sizeof |
1040 | 0 | (xmlRelaxNGValidStatePtr)); |
1041 | 0 | if (tmp == NULL) { |
1042 | 0 | xmlRngVErrMemory(ctxt); |
1043 | 0 | return (-1); |
1044 | 0 | } |
1045 | 0 | states->tabState = tmp; |
1046 | 0 | states->maxState = size; |
1047 | 0 | } |
1048 | 0 | for (i = 0; i < states->nbState; i++) { |
1049 | 0 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
1050 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
1051 | 0 | return (0); |
1052 | 0 | } |
1053 | 0 | } |
1054 | 0 | states->tabState[states->nbState++] = state; |
1055 | 0 | return (1); |
1056 | 0 | } |
1057 | | |
1058 | | /** |
1059 | | * Free a RelaxNG validation state container |
1060 | | * |
1061 | | * @param ctxt a Relax-NG validation context |
1062 | | * @param states the container |
1063 | | */ |
1064 | | static void |
1065 | | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
1066 | | xmlRelaxNGStatesPtr states) |
1067 | 0 | { |
1068 | 0 | if (states == NULL) |
1069 | 0 | return; |
1070 | 0 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
1071 | 0 | ctxt->freeStatesMax = 40; |
1072 | 0 | ctxt->freeStatesNr = 0; |
1073 | 0 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
1074 | 0 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
1075 | 0 | if (ctxt->freeStates == NULL) { |
1076 | 0 | xmlRngVErrMemory(ctxt); |
1077 | 0 | } |
1078 | 0 | } else if ((ctxt != NULL) |
1079 | 0 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
1080 | 0 | xmlRelaxNGStatesPtr *tmp; |
1081 | |
|
1082 | 0 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
1083 | 0 | 2 * ctxt->freeStatesMax * |
1084 | 0 | sizeof |
1085 | 0 | (xmlRelaxNGStatesPtr)); |
1086 | 0 | if (tmp == NULL) { |
1087 | 0 | xmlRngVErrMemory(ctxt); |
1088 | 0 | xmlFree(states->tabState); |
1089 | 0 | xmlFree(states); |
1090 | 0 | return; |
1091 | 0 | } |
1092 | 0 | ctxt->freeStates = tmp; |
1093 | 0 | ctxt->freeStatesMax *= 2; |
1094 | 0 | } |
1095 | 0 | if ((ctxt == NULL) || (ctxt->freeStates == NULL)) { |
1096 | 0 | xmlFree(states->tabState); |
1097 | 0 | xmlFree(states); |
1098 | 0 | } else { |
1099 | 0 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
1100 | 0 | } |
1101 | 0 | } |
1102 | | |
1103 | | /** |
1104 | | * Allocate a new RelaxNG validation state |
1105 | | * |
1106 | | * @param ctxt a Relax-NG validation context |
1107 | | * @param node the current node or NULL for the document |
1108 | | * @returns the newly allocated structure or NULL in case or error |
1109 | | */ |
1110 | | static xmlRelaxNGValidStatePtr |
1111 | | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
1112 | 0 | { |
1113 | 0 | xmlRelaxNGValidStatePtr ret; |
1114 | 0 | xmlAttrPtr attr; |
1115 | 0 | xmlAttrPtr attrs[MAX_ATTR]; |
1116 | 0 | int nbAttrs = 0; |
1117 | 0 | xmlNodePtr root = NULL; |
1118 | |
|
1119 | 0 | if (node == NULL) { |
1120 | 0 | root = xmlDocGetRootElement(ctxt->doc); |
1121 | 0 | if (root == NULL) |
1122 | 0 | return (NULL); |
1123 | 0 | } else { |
1124 | 0 | attr = node->properties; |
1125 | 0 | while (attr != NULL) { |
1126 | 0 | if (nbAttrs < MAX_ATTR) |
1127 | 0 | attrs[nbAttrs++] = attr; |
1128 | 0 | else |
1129 | 0 | nbAttrs++; |
1130 | 0 | attr = attr->next; |
1131 | 0 | } |
1132 | 0 | } |
1133 | 0 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
1134 | 0 | ctxt->freeState->nbState--; |
1135 | 0 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
1136 | 0 | } else { |
1137 | 0 | ret = |
1138 | 0 | (xmlRelaxNGValidStatePtr) |
1139 | 0 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
1140 | 0 | if (ret == NULL) { |
1141 | 0 | xmlRngVErrMemory(ctxt); |
1142 | 0 | return (NULL); |
1143 | 0 | } |
1144 | 0 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
1145 | 0 | } |
1146 | 0 | ret->value = NULL; |
1147 | 0 | ret->endvalue = NULL; |
1148 | 0 | if (node == NULL) { |
1149 | 0 | ret->node = (xmlNodePtr) ctxt->doc; |
1150 | 0 | ret->seq = root; |
1151 | 0 | } else { |
1152 | 0 | ret->node = node; |
1153 | 0 | ret->seq = node->children; |
1154 | 0 | } |
1155 | 0 | ret->nbAttrs = 0; |
1156 | 0 | if (nbAttrs > 0) { |
1157 | 0 | if (ret->attrs == NULL) { |
1158 | 0 | if (nbAttrs < 4) |
1159 | 0 | ret->maxAttrs = 4; |
1160 | 0 | else |
1161 | 0 | ret->maxAttrs = nbAttrs; |
1162 | 0 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
1163 | 0 | sizeof(xmlAttrPtr)); |
1164 | 0 | if (ret->attrs == NULL) { |
1165 | 0 | xmlRngVErrMemory(ctxt); |
1166 | 0 | return (ret); |
1167 | 0 | } |
1168 | 0 | } else if (ret->maxAttrs < nbAttrs) { |
1169 | 0 | xmlAttrPtr *tmp; |
1170 | |
|
1171 | 0 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
1172 | 0 | sizeof(xmlAttrPtr)); |
1173 | 0 | if (tmp == NULL) { |
1174 | 0 | xmlRngVErrMemory(ctxt); |
1175 | 0 | return (ret); |
1176 | 0 | } |
1177 | 0 | ret->attrs = tmp; |
1178 | 0 | ret->maxAttrs = nbAttrs; |
1179 | 0 | } |
1180 | 0 | ret->nbAttrs = nbAttrs; |
1181 | 0 | if (nbAttrs < MAX_ATTR) { |
1182 | 0 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
1183 | 0 | } else { |
1184 | 0 | attr = node->properties; |
1185 | 0 | nbAttrs = 0; |
1186 | 0 | while (attr != NULL) { |
1187 | 0 | ret->attrs[nbAttrs++] = attr; |
1188 | 0 | attr = attr->next; |
1189 | 0 | } |
1190 | 0 | } |
1191 | 0 | } |
1192 | 0 | ret->nbAttrLeft = ret->nbAttrs; |
1193 | 0 | return (ret); |
1194 | 0 | } |
1195 | | |
1196 | | /** |
1197 | | * Copy the validation state |
1198 | | * |
1199 | | * @param ctxt a Relax-NG validation context |
1200 | | * @param state a validation state |
1201 | | * @returns the newly allocated structure or NULL in case or error |
1202 | | */ |
1203 | | static xmlRelaxNGValidStatePtr |
1204 | | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
1205 | | xmlRelaxNGValidStatePtr state) |
1206 | 0 | { |
1207 | 0 | xmlRelaxNGValidStatePtr ret; |
1208 | 0 | unsigned int maxAttrs; |
1209 | 0 | xmlAttrPtr *attrs; |
1210 | |
|
1211 | 0 | if (state == NULL) |
1212 | 0 | return (NULL); |
1213 | 0 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
1214 | 0 | ctxt->freeState->nbState--; |
1215 | 0 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
1216 | 0 | } else { |
1217 | 0 | ret = |
1218 | 0 | (xmlRelaxNGValidStatePtr) |
1219 | 0 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
1220 | 0 | if (ret == NULL) { |
1221 | 0 | xmlRngVErrMemory(ctxt); |
1222 | 0 | return (NULL); |
1223 | 0 | } |
1224 | 0 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
1225 | 0 | } |
1226 | 0 | attrs = ret->attrs; |
1227 | 0 | maxAttrs = ret->maxAttrs; |
1228 | 0 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
1229 | 0 | ret->attrs = attrs; |
1230 | 0 | ret->maxAttrs = maxAttrs; |
1231 | 0 | if (state->nbAttrs > 0) { |
1232 | 0 | if (ret->attrs == NULL) { |
1233 | 0 | ret->maxAttrs = state->maxAttrs; |
1234 | 0 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
1235 | 0 | sizeof(xmlAttrPtr)); |
1236 | 0 | if (ret->attrs == NULL) { |
1237 | 0 | xmlRngVErrMemory(ctxt); |
1238 | 0 | ret->nbAttrs = 0; |
1239 | 0 | return (ret); |
1240 | 0 | } |
1241 | 0 | } else if (ret->maxAttrs < state->nbAttrs) { |
1242 | 0 | xmlAttrPtr *tmp; |
1243 | |
|
1244 | 0 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
1245 | 0 | sizeof(xmlAttrPtr)); |
1246 | 0 | if (tmp == NULL) { |
1247 | 0 | xmlRngVErrMemory(ctxt); |
1248 | 0 | ret->nbAttrs = 0; |
1249 | 0 | return (ret); |
1250 | 0 | } |
1251 | 0 | ret->maxAttrs = state->maxAttrs; |
1252 | 0 | ret->attrs = tmp; |
1253 | 0 | } |
1254 | 0 | memcpy(ret->attrs, state->attrs, |
1255 | 0 | state->nbAttrs * sizeof(xmlAttrPtr)); |
1256 | 0 | } |
1257 | 0 | return (ret); |
1258 | 0 | } |
1259 | | |
1260 | | /** |
1261 | | * Compare the validation states for equality |
1262 | | * |
1263 | | * @param ctxt a Relax-NG validation context |
1264 | | * @param state1 a validation state |
1265 | | * @param state2 a validation state |
1266 | | * @returns 1 if equal, 0 otherwise |
1267 | | */ |
1268 | | static int |
1269 | | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
1270 | | xmlRelaxNGValidStatePtr state1, |
1271 | | xmlRelaxNGValidStatePtr state2) |
1272 | 0 | { |
1273 | 0 | int i; |
1274 | |
|
1275 | 0 | if ((state1 == NULL) || (state2 == NULL)) |
1276 | 0 | return (0); |
1277 | 0 | if (state1 == state2) |
1278 | 0 | return (1); |
1279 | 0 | if (state1->node != state2->node) |
1280 | 0 | return (0); |
1281 | 0 | if (state1->seq != state2->seq) |
1282 | 0 | return (0); |
1283 | 0 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
1284 | 0 | return (0); |
1285 | 0 | if (state1->nbAttrs != state2->nbAttrs) |
1286 | 0 | return (0); |
1287 | 0 | if (state1->endvalue != state2->endvalue) |
1288 | 0 | return (0); |
1289 | 0 | if ((state1->value != state2->value) && |
1290 | 0 | (!xmlStrEqual(state1->value, state2->value))) |
1291 | 0 | return (0); |
1292 | 0 | for (i = 0; i < state1->nbAttrs; i++) { |
1293 | 0 | if (state1->attrs[i] != state2->attrs[i]) |
1294 | 0 | return (0); |
1295 | 0 | } |
1296 | 0 | return (1); |
1297 | 0 | } |
1298 | | |
1299 | | /** |
1300 | | * Deallocate a RelaxNG validation state structure. |
1301 | | * |
1302 | | * @param ctxt validation context |
1303 | | * @param state a validation state structure |
1304 | | */ |
1305 | | static void |
1306 | | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
1307 | | xmlRelaxNGValidStatePtr state) |
1308 | 0 | { |
1309 | 0 | if (state == NULL) |
1310 | 0 | return; |
1311 | | |
1312 | 0 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
1313 | 0 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
1314 | 0 | } |
1315 | 0 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
1316 | 0 | if (state->attrs != NULL) |
1317 | 0 | xmlFree(state->attrs); |
1318 | 0 | xmlFree(state); |
1319 | 0 | } else { |
1320 | 0 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
1321 | 0 | } |
1322 | 0 | } |
1323 | | |
1324 | | /************************************************************************ |
1325 | | * * |
1326 | | * Semi internal functions * |
1327 | | * * |
1328 | | ************************************************************************/ |
1329 | | |
1330 | | /** |
1331 | | * Semi private function used to pass information to a parser context |
1332 | | * which are a combination of xmlRelaxNGParserFlag . |
1333 | | * |
1334 | | * @param ctxt a RelaxNG parser context |
1335 | | * @param flags a set of flags values |
1336 | | * @returns 0 if success and -1 in case of error |
1337 | | */ |
1338 | | int |
1339 | | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxt *ctxt, int flags) |
1340 | 0 | { |
1341 | 0 | if (ctxt == NULL) return(-1); |
1342 | 0 | if (flags & XML_RELAXNGP_FREE_DOC) { |
1343 | 0 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
1344 | 0 | flags -= XML_RELAXNGP_FREE_DOC; |
1345 | 0 | } |
1346 | 0 | if (flags & XML_RELAXNGP_CRNG) { |
1347 | 0 | ctxt->crng |= XML_RELAXNGP_CRNG; |
1348 | 0 | flags -= XML_RELAXNGP_CRNG; |
1349 | 0 | } |
1350 | 0 | if (flags != 0) return(-1); |
1351 | 0 | return(0); |
1352 | 0 | } |
1353 | | |
1354 | | /** |
1355 | | * Semi private function used to set the include recursion limit to a |
1356 | | * parser context. Set to 0 to use the default value. |
1357 | | * |
1358 | | * @param ctxt a RelaxNG parser context |
1359 | | * @param limit the new include depth limit |
1360 | | * @returns 0 if success and -1 in case of error |
1361 | | */ |
1362 | | int |
1363 | | xmlRelaxParserSetIncLImit(xmlRelaxNGParserCtxt *ctxt, int limit) |
1364 | 0 | { |
1365 | 0 | if (ctxt == NULL) return(-1); |
1366 | 0 | if (limit < 0) return(-1); |
1367 | 0 | ctxt->incLimit = limit; |
1368 | 0 | return(0); |
1369 | 0 | } |
1370 | | |
1371 | | /************************************************************************ |
1372 | | * * |
1373 | | * Document functions * |
1374 | | * * |
1375 | | ************************************************************************/ |
1376 | | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
1377 | | xmlDocPtr doc); |
1378 | | |
1379 | | static xmlDoc * |
1380 | 0 | xmlRelaxReadFile(xmlRelaxNGParserCtxtPtr ctxt, const char *filename) { |
1381 | 0 | xmlParserCtxtPtr pctxt; |
1382 | 0 | xmlDocPtr doc; |
1383 | |
|
1384 | 0 | pctxt = xmlNewParserCtxt(); |
1385 | 0 | if (pctxt == NULL) { |
1386 | 0 | xmlRngPErrMemory(ctxt); |
1387 | 0 | return(NULL); |
1388 | 0 | } |
1389 | 0 | if (ctxt->serror != NULL) |
1390 | 0 | xmlCtxtSetErrorHandler(pctxt, ctxt->serror, ctxt->userData); |
1391 | 0 | if (ctxt->resourceLoader != NULL) |
1392 | 0 | xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader, |
1393 | 0 | ctxt->resourceCtxt); |
1394 | 0 | doc = xmlCtxtReadFile(pctxt, filename, NULL, 0); |
1395 | 0 | xmlFreeParserCtxt(pctxt); |
1396 | |
|
1397 | 0 | return(doc); |
1398 | 0 | } |
1399 | | |
1400 | | static xmlDoc * |
1401 | 0 | xmlRelaxReadMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *buf, int size) { |
1402 | 0 | xmlParserCtxtPtr pctxt; |
1403 | 0 | xmlDocPtr doc; |
1404 | |
|
1405 | 0 | pctxt = xmlNewParserCtxt(); |
1406 | 0 | if (pctxt == NULL) { |
1407 | 0 | xmlRngPErrMemory(ctxt); |
1408 | 0 | return(NULL); |
1409 | 0 | } |
1410 | 0 | if (ctxt->serror != NULL) |
1411 | 0 | xmlCtxtSetErrorHandler(pctxt, ctxt->serror, ctxt->userData); |
1412 | 0 | if (ctxt->resourceLoader != NULL) |
1413 | 0 | xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader, |
1414 | 0 | ctxt->resourceCtxt); |
1415 | 0 | doc = xmlCtxtReadMemory(pctxt, buf, size, NULL, NULL, 0); |
1416 | 0 | xmlFreeParserCtxt(pctxt); |
1417 | |
|
1418 | 0 | return(doc); |
1419 | 0 | } |
1420 | | |
1421 | | /** |
1422 | | * Pushes a new include on top of the include stack |
1423 | | * |
1424 | | * @param ctxt the parser context |
1425 | | * @param value the element doc |
1426 | | * @returns -1 in case of error, the index in the stack otherwise |
1427 | | */ |
1428 | | static int |
1429 | | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
1430 | | xmlRelaxNGIncludePtr value) |
1431 | 0 | { |
1432 | 0 | if (ctxt->incTab == NULL) { |
1433 | 0 | ctxt->incMax = 4; |
1434 | 0 | ctxt->incNr = 0; |
1435 | 0 | ctxt->incTab = |
1436 | 0 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
1437 | 0 | sizeof(ctxt->incTab[0])); |
1438 | 0 | if (ctxt->incTab == NULL) { |
1439 | 0 | xmlRngPErrMemory(ctxt); |
1440 | 0 | return (-1); |
1441 | 0 | } |
1442 | 0 | } |
1443 | 0 | if (ctxt->incNr >= ctxt->incLimit) { |
1444 | 0 | xmlRngPErr(ctxt, (xmlNodePtr)value->doc, XML_RNGP_PARSE_ERROR, |
1445 | 0 | "xmlRelaxNG: inclusion recursion limit reached\n", NULL, NULL); |
1446 | 0 | return(-1); |
1447 | 0 | } |
1448 | | |
1449 | 0 | if (ctxt->incNr >= ctxt->incMax) { |
1450 | 0 | ctxt->incMax *= 2; |
1451 | 0 | ctxt->incTab = |
1452 | 0 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
1453 | 0 | ctxt->incMax * |
1454 | 0 | sizeof(ctxt->incTab[0])); |
1455 | 0 | if (ctxt->incTab == NULL) { |
1456 | 0 | xmlRngPErrMemory(ctxt); |
1457 | 0 | return (-1); |
1458 | 0 | } |
1459 | 0 | } |
1460 | 0 | ctxt->incTab[ctxt->incNr] = value; |
1461 | 0 | ctxt->inc = value; |
1462 | 0 | return (ctxt->incNr++); |
1463 | 0 | } |
1464 | | |
1465 | | /** |
1466 | | * Pops the top include from the include stack |
1467 | | * |
1468 | | * @param ctxt the parser context |
1469 | | * @returns the include just removed |
1470 | | */ |
1471 | | static xmlRelaxNGIncludePtr |
1472 | | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
1473 | 0 | { |
1474 | 0 | xmlRelaxNGIncludePtr ret; |
1475 | |
|
1476 | 0 | if (ctxt->incNr <= 0) |
1477 | 0 | return (NULL); |
1478 | 0 | ctxt->incNr--; |
1479 | 0 | if (ctxt->incNr > 0) |
1480 | 0 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
1481 | 0 | else |
1482 | 0 | ctxt->inc = NULL; |
1483 | 0 | ret = ctxt->incTab[ctxt->incNr]; |
1484 | 0 | ctxt->incTab[ctxt->incNr] = NULL; |
1485 | 0 | return (ret); |
1486 | 0 | } |
1487 | | |
1488 | | /** |
1489 | | * Applies the elimination algorithm of 4.7 |
1490 | | * |
1491 | | * @param ctxt the parser context |
1492 | | * @param URL the normalized URL |
1493 | | * @param target the included target |
1494 | | * @param name the define name to eliminate |
1495 | | * @returns 0 in case of error, 1 in case of success. |
1496 | | */ |
1497 | | static int |
1498 | | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
1499 | | const xmlChar * URL ATTRIBUTE_UNUSED, |
1500 | | xmlNodePtr target, const xmlChar * name) |
1501 | 0 | { |
1502 | 0 | int found = 0; |
1503 | 0 | xmlNodePtr tmp, tmp2; |
1504 | 0 | xmlChar *name2; |
1505 | |
|
1506 | 0 | tmp = target; |
1507 | 0 | while (tmp != NULL) { |
1508 | 0 | tmp2 = tmp->next; |
1509 | 0 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
1510 | 0 | found = 1; |
1511 | 0 | xmlUnlinkNode(tmp); |
1512 | 0 | xmlFreeNode(tmp); |
1513 | 0 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
1514 | 0 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
1515 | 0 | xmlRelaxNGNormExtSpace(name2); |
1516 | 0 | if (name2 != NULL) { |
1517 | 0 | if (xmlStrEqual(name, name2)) { |
1518 | 0 | found = 1; |
1519 | 0 | xmlUnlinkNode(tmp); |
1520 | 0 | xmlFreeNode(tmp); |
1521 | 0 | } |
1522 | 0 | xmlFree(name2); |
1523 | 0 | } |
1524 | 0 | } else if (IS_RELAXNG(tmp, "include")) { |
1525 | 0 | xmlChar *href = NULL; |
1526 | 0 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
1527 | |
|
1528 | 0 | if ((inc != NULL) && (inc->doc != NULL) && |
1529 | 0 | (inc->doc->children != NULL)) { |
1530 | |
|
1531 | 0 | if (xmlStrEqual |
1532 | 0 | (inc->doc->children->name, BAD_CAST "grammar")) { |
1533 | 0 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
1534 | 0 | xmlDocGetRootElement(inc->doc)->children, |
1535 | 0 | name) == 1) { |
1536 | 0 | found = 1; |
1537 | 0 | } |
1538 | 0 | } |
1539 | 0 | } |
1540 | 0 | if (xmlRelaxNGRemoveRedefine(ctxt, URL, tmp->children, name) == 1) { |
1541 | 0 | found = 1; |
1542 | 0 | } |
1543 | 0 | } |
1544 | 0 | tmp = tmp2; |
1545 | 0 | } |
1546 | 0 | return (found); |
1547 | 0 | } |
1548 | | |
1549 | | /** |
1550 | | * First lookup if the document is already loaded into the parser context, |
1551 | | * check against recursion. If not found the resource is loaded and |
1552 | | * the content is preprocessed before being returned back to the caller. |
1553 | | * |
1554 | | * @param ctxt the parser context |
1555 | | * @param URL the normalized URL |
1556 | | * @param node the include node. |
1557 | | * @param ns the namespace passed from the context. |
1558 | | * @returns the xmlRelaxNGInclude or NULL in case of error |
1559 | | */ |
1560 | | static xmlRelaxNGIncludePtr |
1561 | | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
1562 | | xmlNodePtr node, const xmlChar * ns) |
1563 | 0 | { |
1564 | 0 | xmlRelaxNGIncludePtr ret = NULL; |
1565 | 0 | xmlDocPtr doc; |
1566 | 0 | int i; |
1567 | 0 | xmlNodePtr root, cur; |
1568 | | |
1569 | | /* |
1570 | | * check against recursion in the stack |
1571 | | */ |
1572 | 0 | for (i = 0; i < ctxt->incNr; i++) { |
1573 | 0 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
1574 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
1575 | 0 | "Detected an Include recursion for %s\n", URL, |
1576 | 0 | NULL); |
1577 | 0 | return (NULL); |
1578 | 0 | } |
1579 | 0 | } |
1580 | | |
1581 | | /* |
1582 | | * load the document |
1583 | | */ |
1584 | 0 | doc = xmlRelaxReadFile(ctxt, (const char *) URL); |
1585 | 0 | if (doc == NULL) { |
1586 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
1587 | 0 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
1588 | 0 | return (NULL); |
1589 | 0 | } |
1590 | | |
1591 | | /* |
1592 | | * Allocate the document structures and register it first. |
1593 | | */ |
1594 | 0 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
1595 | 0 | if (ret == NULL) { |
1596 | 0 | xmlRngPErrMemory(ctxt); |
1597 | 0 | xmlFreeDoc(doc); |
1598 | 0 | return (NULL); |
1599 | 0 | } |
1600 | 0 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
1601 | 0 | ret->doc = doc; |
1602 | 0 | ret->href = xmlStrdup(URL); |
1603 | 0 | ret->next = ctxt->includes; |
1604 | 0 | ctxt->includes = ret; |
1605 | | |
1606 | | /* |
1607 | | * transmit the ns if needed |
1608 | | */ |
1609 | 0 | if (ns != NULL) { |
1610 | 0 | root = xmlDocGetRootElement(doc); |
1611 | 0 | if (root != NULL) { |
1612 | 0 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
1613 | 0 | xmlSetProp(root, BAD_CAST "ns", ns); |
1614 | 0 | } |
1615 | 0 | } |
1616 | 0 | } |
1617 | | |
1618 | | /* |
1619 | | * push it on the stack |
1620 | | */ |
1621 | 0 | if (xmlRelaxNGIncludePush(ctxt, ret) < 0) { |
1622 | 0 | return (NULL); |
1623 | 0 | } |
1624 | | |
1625 | | /* |
1626 | | * Some preprocessing of the document content, this include recursing |
1627 | | * in the include stack. |
1628 | | */ |
1629 | | |
1630 | 0 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
1631 | 0 | if (doc == NULL) { |
1632 | 0 | ctxt->inc = NULL; |
1633 | 0 | return (NULL); |
1634 | 0 | } |
1635 | | |
1636 | | /* |
1637 | | * Pop up the include from the stack |
1638 | | */ |
1639 | 0 | xmlRelaxNGIncludePop(ctxt); |
1640 | | |
1641 | | /* |
1642 | | * Check that the top element is a grammar |
1643 | | */ |
1644 | 0 | root = xmlDocGetRootElement(doc); |
1645 | 0 | if (root == NULL) { |
1646 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
1647 | 0 | "xmlRelaxNG: included document is empty %s\n", URL, |
1648 | 0 | NULL); |
1649 | 0 | return (NULL); |
1650 | 0 | } |
1651 | 0 | if (!IS_RELAXNG(root, "grammar")) { |
1652 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
1653 | 0 | "xmlRelaxNG: included document %s root is not a grammar\n", |
1654 | 0 | URL, NULL); |
1655 | 0 | return (NULL); |
1656 | 0 | } |
1657 | | |
1658 | | /* |
1659 | | * Elimination of redefined rules in the include. |
1660 | | */ |
1661 | 0 | cur = node->children; |
1662 | 0 | while (cur != NULL) { |
1663 | 0 | if (IS_RELAXNG(cur, "start")) { |
1664 | 0 | int found = 0; |
1665 | |
|
1666 | 0 | found = |
1667 | 0 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
1668 | 0 | if (!found) { |
1669 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
1670 | 0 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
1671 | 0 | URL, NULL); |
1672 | 0 | } |
1673 | 0 | } else if (IS_RELAXNG(cur, "define")) { |
1674 | 0 | xmlChar *name; |
1675 | |
|
1676 | 0 | name = xmlGetProp(cur, BAD_CAST "name"); |
1677 | 0 | if (name == NULL) { |
1678 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
1679 | 0 | "xmlRelaxNG: include %s has define without name\n", |
1680 | 0 | URL, NULL); |
1681 | 0 | } else { |
1682 | 0 | int found; |
1683 | |
|
1684 | 0 | xmlRelaxNGNormExtSpace(name); |
1685 | 0 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
1686 | 0 | root->children, name); |
1687 | 0 | if (!found) { |
1688 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
1689 | 0 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
1690 | 0 | URL, name); |
1691 | 0 | } |
1692 | 0 | xmlFree(name); |
1693 | 0 | } |
1694 | 0 | } |
1695 | 0 | if (IS_RELAXNG(cur, "div") && cur->children != NULL) { |
1696 | 0 | cur = cur->children; |
1697 | 0 | } else { |
1698 | 0 | if (cur->next != NULL) { |
1699 | 0 | cur = cur->next; |
1700 | 0 | } else { |
1701 | 0 | while (cur->parent != node && cur->parent->next == NULL) { |
1702 | 0 | cur = cur->parent; |
1703 | 0 | } |
1704 | 0 | cur = cur->parent != node ? cur->parent->next : NULL; |
1705 | 0 | } |
1706 | 0 | } |
1707 | 0 | } |
1708 | | |
1709 | |
|
1710 | 0 | return (ret); |
1711 | 0 | } |
1712 | | |
1713 | | /** |
1714 | | * Pushes a new error on top of the error stack |
1715 | | * |
1716 | | * @param ctxt the validation context |
1717 | | * @param err the error code |
1718 | | * @param arg1 the first string argument |
1719 | | * @param arg2 the second string argument |
1720 | | * @param dup arg need to be duplicated |
1721 | | * @returns 0 in case of error, the index in the stack otherwise |
1722 | | */ |
1723 | | static int |
1724 | | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
1725 | | xmlRelaxNGValidErr err, const xmlChar * arg1, |
1726 | | const xmlChar * arg2, int dup) |
1727 | 0 | { |
1728 | 0 | xmlRelaxNGValidErrorPtr cur; |
1729 | |
|
1730 | 0 | if (ctxt->errTab == NULL) { |
1731 | 0 | ctxt->errMax = 8; |
1732 | 0 | ctxt->errNr = 0; |
1733 | 0 | ctxt->errTab = |
1734 | 0 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
1735 | 0 | sizeof |
1736 | 0 | (xmlRelaxNGValidError)); |
1737 | 0 | if (ctxt->errTab == NULL) { |
1738 | 0 | xmlRngVErrMemory(ctxt); |
1739 | 0 | return (0); |
1740 | 0 | } |
1741 | 0 | ctxt->err = NULL; |
1742 | 0 | } |
1743 | 0 | if (ctxt->errNr >= ctxt->errMax) { |
1744 | 0 | ctxt->errMax *= 2; |
1745 | 0 | ctxt->errTab = |
1746 | 0 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
1747 | 0 | ctxt->errMax * |
1748 | 0 | sizeof |
1749 | 0 | (xmlRelaxNGValidError)); |
1750 | 0 | if (ctxt->errTab == NULL) { |
1751 | 0 | xmlRngVErrMemory(ctxt); |
1752 | 0 | return (0); |
1753 | 0 | } |
1754 | 0 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
1755 | 0 | } |
1756 | 0 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
1757 | 0 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
1758 | 0 | return (ctxt->errNr); |
1759 | 0 | cur = &ctxt->errTab[ctxt->errNr]; |
1760 | 0 | cur->err = err; |
1761 | 0 | if (dup) { |
1762 | 0 | cur->arg1 = xmlStrdup(arg1); |
1763 | 0 | cur->arg2 = xmlStrdup(arg2); |
1764 | 0 | cur->flags = ERROR_IS_DUP; |
1765 | 0 | } else { |
1766 | 0 | cur->arg1 = arg1; |
1767 | 0 | cur->arg2 = arg2; |
1768 | 0 | cur->flags = 0; |
1769 | 0 | } |
1770 | 0 | if (ctxt->state != NULL) { |
1771 | 0 | cur->node = ctxt->state->node; |
1772 | 0 | cur->seq = ctxt->state->seq; |
1773 | 0 | } else { |
1774 | 0 | cur->node = NULL; |
1775 | 0 | cur->seq = NULL; |
1776 | 0 | } |
1777 | 0 | ctxt->err = cur; |
1778 | 0 | return (ctxt->errNr++); |
1779 | 0 | } |
1780 | | |
1781 | | /** |
1782 | | * Pops the top error from the error stack |
1783 | | * |
1784 | | * @param ctxt the validation context |
1785 | | */ |
1786 | | static void |
1787 | | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
1788 | 0 | { |
1789 | 0 | xmlRelaxNGValidErrorPtr cur; |
1790 | |
|
1791 | 0 | if (ctxt->errNr <= 0) { |
1792 | 0 | ctxt->err = NULL; |
1793 | 0 | return; |
1794 | 0 | } |
1795 | 0 | ctxt->errNr--; |
1796 | 0 | if (ctxt->errNr > 0) |
1797 | 0 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
1798 | 0 | else |
1799 | 0 | ctxt->err = NULL; |
1800 | 0 | cur = &ctxt->errTab[ctxt->errNr]; |
1801 | 0 | if (cur->flags & ERROR_IS_DUP) { |
1802 | 0 | if (cur->arg1 != NULL) |
1803 | 0 | xmlFree((xmlChar *) cur->arg1); |
1804 | 0 | cur->arg1 = NULL; |
1805 | 0 | if (cur->arg2 != NULL) |
1806 | 0 | xmlFree((xmlChar *) cur->arg2); |
1807 | 0 | cur->arg2 = NULL; |
1808 | 0 | cur->flags = 0; |
1809 | 0 | } |
1810 | 0 | } |
1811 | | |
1812 | | /** |
1813 | | * Pushes a new doc on top of the doc stack |
1814 | | * |
1815 | | * @param ctxt the parser context |
1816 | | * @param value the element doc |
1817 | | * @returns 0 in case of error, the index in the stack otherwise |
1818 | | */ |
1819 | | static int |
1820 | | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
1821 | | xmlRelaxNGDocumentPtr value) |
1822 | 0 | { |
1823 | 0 | if (ctxt->docTab == NULL) { |
1824 | 0 | ctxt->docMax = 4; |
1825 | 0 | ctxt->docNr = 0; |
1826 | 0 | ctxt->docTab = |
1827 | 0 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
1828 | 0 | sizeof(ctxt->docTab[0])); |
1829 | 0 | if (ctxt->docTab == NULL) { |
1830 | 0 | xmlRngPErrMemory(ctxt); |
1831 | 0 | return (0); |
1832 | 0 | } |
1833 | 0 | } |
1834 | 0 | if (ctxt->docNr >= ctxt->docMax) { |
1835 | 0 | ctxt->docMax *= 2; |
1836 | 0 | ctxt->docTab = |
1837 | 0 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
1838 | 0 | ctxt->docMax * |
1839 | 0 | sizeof(ctxt->docTab[0])); |
1840 | 0 | if (ctxt->docTab == NULL) { |
1841 | 0 | xmlRngPErrMemory(ctxt); |
1842 | 0 | return (0); |
1843 | 0 | } |
1844 | 0 | } |
1845 | 0 | ctxt->docTab[ctxt->docNr] = value; |
1846 | 0 | ctxt->doc = value; |
1847 | 0 | return (ctxt->docNr++); |
1848 | 0 | } |
1849 | | |
1850 | | /** |
1851 | | * Pops the top doc from the doc stack |
1852 | | * |
1853 | | * @param ctxt the parser context |
1854 | | * @returns the doc just removed |
1855 | | */ |
1856 | | static xmlRelaxNGDocumentPtr |
1857 | | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
1858 | 0 | { |
1859 | 0 | xmlRelaxNGDocumentPtr ret; |
1860 | |
|
1861 | 0 | if (ctxt->docNr <= 0) |
1862 | 0 | return (NULL); |
1863 | 0 | ctxt->docNr--; |
1864 | 0 | if (ctxt->docNr > 0) |
1865 | 0 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
1866 | 0 | else |
1867 | 0 | ctxt->doc = NULL; |
1868 | 0 | ret = ctxt->docTab[ctxt->docNr]; |
1869 | 0 | ctxt->docTab[ctxt->docNr] = NULL; |
1870 | 0 | return (ret); |
1871 | 0 | } |
1872 | | |
1873 | | /** |
1874 | | * First lookup if the document is already loaded into the parser context, |
1875 | | * check against recursion. If not found the resource is loaded and |
1876 | | * the content is preprocessed before being returned back to the caller. |
1877 | | * |
1878 | | * @param ctxt the parser context |
1879 | | * @param URL the normalized URL |
1880 | | * @param ns the inherited ns if any |
1881 | | * @returns the xmlRelaxNGDocument or NULL in case of error |
1882 | | */ |
1883 | | static xmlRelaxNGDocumentPtr |
1884 | | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
1885 | | const xmlChar * URL, const xmlChar * ns) |
1886 | 0 | { |
1887 | 0 | xmlRelaxNGDocumentPtr ret = NULL; |
1888 | 0 | xmlDocPtr doc; |
1889 | 0 | xmlNodePtr root; |
1890 | 0 | int i; |
1891 | | |
1892 | | /* |
1893 | | * check against recursion in the stack |
1894 | | */ |
1895 | 0 | for (i = 0; i < ctxt->docNr; i++) { |
1896 | 0 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
1897 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
1898 | 0 | "Detected an externalRef recursion for %s\n", URL, |
1899 | 0 | NULL); |
1900 | 0 | return (NULL); |
1901 | 0 | } |
1902 | 0 | } |
1903 | | |
1904 | | /* |
1905 | | * load the document |
1906 | | */ |
1907 | 0 | doc = xmlRelaxReadFile(ctxt, (const char *) URL); |
1908 | 0 | if (doc == NULL) { |
1909 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
1910 | 0 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
1911 | 0 | return (NULL); |
1912 | 0 | } |
1913 | | |
1914 | | /* |
1915 | | * Allocate the document structures and register it first. |
1916 | | */ |
1917 | 0 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
1918 | 0 | if (ret == NULL) { |
1919 | 0 | xmlRngPErrMemory(ctxt); |
1920 | 0 | xmlFreeDoc(doc); |
1921 | 0 | return (NULL); |
1922 | 0 | } |
1923 | 0 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
1924 | 0 | ret->doc = doc; |
1925 | 0 | ret->href = xmlStrdup(URL); |
1926 | 0 | ret->next = ctxt->documents; |
1927 | 0 | ret->externalRef = 1; |
1928 | 0 | ctxt->documents = ret; |
1929 | | |
1930 | | /* |
1931 | | * transmit the ns if needed |
1932 | | */ |
1933 | 0 | if (ns != NULL) { |
1934 | 0 | root = xmlDocGetRootElement(doc); |
1935 | 0 | if (root != NULL) { |
1936 | 0 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
1937 | 0 | xmlSetProp(root, BAD_CAST "ns", ns); |
1938 | 0 | } |
1939 | 0 | } |
1940 | 0 | } |
1941 | | |
1942 | | /* |
1943 | | * push it on the stack and register it in the hash table |
1944 | | */ |
1945 | 0 | xmlRelaxNGDocumentPush(ctxt, ret); |
1946 | | |
1947 | | /* |
1948 | | * Some preprocessing of the document content |
1949 | | */ |
1950 | 0 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
1951 | 0 | if (doc == NULL) { |
1952 | 0 | ctxt->doc = NULL; |
1953 | 0 | return (NULL); |
1954 | 0 | } |
1955 | | |
1956 | 0 | xmlRelaxNGDocumentPop(ctxt); |
1957 | |
|
1958 | 0 | return (ret); |
1959 | 0 | } |
1960 | | |
1961 | | /************************************************************************ |
1962 | | * * |
1963 | | * Error functions * |
1964 | | * * |
1965 | | ************************************************************************/ |
1966 | | |
1967 | 0 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
1968 | 0 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
1969 | 0 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
1970 | 0 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
1971 | 0 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
1972 | | |
1973 | | static const char * |
1974 | | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
1975 | 0 | { |
1976 | 0 | if (def == NULL) |
1977 | 0 | return ("none"); |
1978 | 0 | switch (def->type) { |
1979 | 0 | case XML_RELAXNG_EMPTY: |
1980 | 0 | return ("empty"); |
1981 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
1982 | 0 | return ("notAllowed"); |
1983 | 0 | case XML_RELAXNG_EXCEPT: |
1984 | 0 | return ("except"); |
1985 | 0 | case XML_RELAXNG_TEXT: |
1986 | 0 | return ("text"); |
1987 | 0 | case XML_RELAXNG_ELEMENT: |
1988 | 0 | return ("element"); |
1989 | 0 | case XML_RELAXNG_DATATYPE: |
1990 | 0 | return ("datatype"); |
1991 | 0 | case XML_RELAXNG_VALUE: |
1992 | 0 | return ("value"); |
1993 | 0 | case XML_RELAXNG_LIST: |
1994 | 0 | return ("list"); |
1995 | 0 | case XML_RELAXNG_ATTRIBUTE: |
1996 | 0 | return ("attribute"); |
1997 | 0 | case XML_RELAXNG_DEF: |
1998 | 0 | return ("def"); |
1999 | 0 | case XML_RELAXNG_REF: |
2000 | 0 | return ("ref"); |
2001 | 0 | case XML_RELAXNG_EXTERNALREF: |
2002 | 0 | return ("externalRef"); |
2003 | 0 | case XML_RELAXNG_PARENTREF: |
2004 | 0 | return ("parentRef"); |
2005 | 0 | case XML_RELAXNG_OPTIONAL: |
2006 | 0 | return ("optional"); |
2007 | 0 | case XML_RELAXNG_ZEROORMORE: |
2008 | 0 | return ("zeroOrMore"); |
2009 | 0 | case XML_RELAXNG_ONEORMORE: |
2010 | 0 | return ("oneOrMore"); |
2011 | 0 | case XML_RELAXNG_CHOICE: |
2012 | 0 | return ("choice"); |
2013 | 0 | case XML_RELAXNG_GROUP: |
2014 | 0 | return ("group"); |
2015 | 0 | case XML_RELAXNG_INTERLEAVE: |
2016 | 0 | return ("interleave"); |
2017 | 0 | case XML_RELAXNG_START: |
2018 | 0 | return ("start"); |
2019 | 0 | case XML_RELAXNG_NOOP: |
2020 | 0 | return ("noop"); |
2021 | 0 | case XML_RELAXNG_PARAM: |
2022 | 0 | return ("param"); |
2023 | 0 | } |
2024 | 0 | return ("unknown"); |
2025 | 0 | } |
2026 | | |
2027 | | /** |
2028 | | * computes a formatted error string for the given error code and args |
2029 | | * |
2030 | | * @param err the error code |
2031 | | * @param arg1 the first string argument |
2032 | | * @param arg2 the second string argument |
2033 | | * @returns the error string, it must be deallocated by the caller |
2034 | | */ |
2035 | | static xmlChar * |
2036 | | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
2037 | | const xmlChar * arg2) |
2038 | 0 | { |
2039 | 0 | char msg[1000]; |
2040 | 0 | xmlChar *result; |
2041 | |
|
2042 | 0 | if (arg1 == NULL) |
2043 | 0 | arg1 = BAD_CAST ""; |
2044 | 0 | if (arg2 == NULL) |
2045 | 0 | arg2 = BAD_CAST ""; |
2046 | |
|
2047 | 0 | msg[0] = 0; |
2048 | 0 | switch (err) { |
2049 | 0 | case XML_RELAXNG_OK: |
2050 | 0 | return (NULL); |
2051 | 0 | case XML_RELAXNG_ERR_MEMORY: |
2052 | 0 | return (xmlCharStrdup("out of memory\n")); |
2053 | 0 | case XML_RELAXNG_ERR_TYPE: |
2054 | 0 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
2055 | 0 | break; |
2056 | 0 | case XML_RELAXNG_ERR_TYPEVAL: |
2057 | 0 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
2058 | 0 | arg2); |
2059 | 0 | break; |
2060 | 0 | case XML_RELAXNG_ERR_DUPID: |
2061 | 0 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
2062 | 0 | break; |
2063 | 0 | case XML_RELAXNG_ERR_TYPECMP: |
2064 | 0 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
2065 | 0 | break; |
2066 | 0 | case XML_RELAXNG_ERR_NOSTATE: |
2067 | 0 | return (xmlCharStrdup("Internal error: no state\n")); |
2068 | 0 | case XML_RELAXNG_ERR_NODEFINE: |
2069 | 0 | return (xmlCharStrdup("Internal error: no define\n")); |
2070 | 0 | case XML_RELAXNG_ERR_INTERNAL: |
2071 | 0 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
2072 | 0 | break; |
2073 | 0 | case XML_RELAXNG_ERR_LISTEXTRA: |
2074 | 0 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
2075 | 0 | break; |
2076 | 0 | case XML_RELAXNG_ERR_INTERNODATA: |
2077 | 0 | return (xmlCharStrdup |
2078 | 0 | ("Internal: interleave block has no data\n")); |
2079 | 0 | case XML_RELAXNG_ERR_INTERSEQ: |
2080 | 0 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
2081 | 0 | case XML_RELAXNG_ERR_INTEREXTRA: |
2082 | 0 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
2083 | 0 | break; |
2084 | 0 | case XML_RELAXNG_ERR_ELEMNAME: |
2085 | 0 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
2086 | 0 | arg2); |
2087 | 0 | break; |
2088 | 0 | case XML_RELAXNG_ERR_ELEMNONS: |
2089 | 0 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
2090 | 0 | arg1); |
2091 | 0 | break; |
2092 | 0 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
2093 | 0 | snprintf(msg, 1000, |
2094 | 0 | "Element %s has wrong namespace: expecting %s\n", arg1, |
2095 | 0 | arg2); |
2096 | 0 | break; |
2097 | 0 | case XML_RELAXNG_ERR_ELEMWRONG: |
2098 | 0 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
2099 | 0 | break; |
2100 | 0 | case XML_RELAXNG_ERR_TEXTWRONG: |
2101 | 0 | snprintf(msg, 1000, |
2102 | 0 | "Did not expect text in element %s content\n", arg1); |
2103 | 0 | break; |
2104 | 0 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
2105 | 0 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
2106 | 0 | arg1); |
2107 | 0 | break; |
2108 | 0 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
2109 | 0 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
2110 | 0 | break; |
2111 | 0 | case XML_RELAXNG_ERR_NOELEM: |
2112 | 0 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
2113 | 0 | arg1); |
2114 | 0 | break; |
2115 | 0 | case XML_RELAXNG_ERR_NOTELEM: |
2116 | 0 | return (xmlCharStrdup("Expecting an element got text\n")); |
2117 | 0 | case XML_RELAXNG_ERR_ATTRVALID: |
2118 | 0 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
2119 | 0 | arg1); |
2120 | 0 | break; |
2121 | 0 | case XML_RELAXNG_ERR_CONTENTVALID: |
2122 | 0 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
2123 | 0 | arg1); |
2124 | 0 | break; |
2125 | 0 | case XML_RELAXNG_ERR_EXTRACONTENT: |
2126 | 0 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
2127 | 0 | arg1, arg2); |
2128 | 0 | break; |
2129 | 0 | case XML_RELAXNG_ERR_INVALIDATTR: |
2130 | 0 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
2131 | 0 | arg1, arg2); |
2132 | 0 | break; |
2133 | 0 | case XML_RELAXNG_ERR_LACKDATA: |
2134 | 0 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
2135 | 0 | arg1); |
2136 | 0 | break; |
2137 | 0 | case XML_RELAXNG_ERR_DATAELEM: |
2138 | 0 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
2139 | 0 | arg1); |
2140 | 0 | break; |
2141 | 0 | case XML_RELAXNG_ERR_VALELEM: |
2142 | 0 | snprintf(msg, 1000, "Value element %s has child elements\n", |
2143 | 0 | arg1); |
2144 | 0 | break; |
2145 | 0 | case XML_RELAXNG_ERR_LISTELEM: |
2146 | 0 | snprintf(msg, 1000, "List element %s has child elements\n", |
2147 | 0 | arg1); |
2148 | 0 | break; |
2149 | 0 | case XML_RELAXNG_ERR_DATATYPE: |
2150 | 0 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
2151 | 0 | break; |
2152 | 0 | case XML_RELAXNG_ERR_VALUE: |
2153 | 0 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
2154 | 0 | break; |
2155 | 0 | case XML_RELAXNG_ERR_LIST: |
2156 | 0 | return (xmlCharStrdup("Error validating list\n")); |
2157 | 0 | case XML_RELAXNG_ERR_NOGRAMMAR: |
2158 | 0 | return (xmlCharStrdup("No top grammar defined\n")); |
2159 | 0 | case XML_RELAXNG_ERR_EXTRADATA: |
2160 | 0 | return (xmlCharStrdup("Extra data in the document\n")); |
2161 | 0 | default: |
2162 | 0 | return (xmlCharStrdup("Unknown error !\n")); |
2163 | 0 | } |
2164 | 0 | if (msg[0] == 0) { |
2165 | 0 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
2166 | 0 | } |
2167 | 0 | msg[1000 - 1] = 0; |
2168 | 0 | result = xmlCharStrdup(msg); |
2169 | 0 | return (xmlEscapeFormatString(&result)); |
2170 | 0 | } |
2171 | | |
2172 | | /** |
2173 | | * Show a validation error. |
2174 | | * |
2175 | | * @param ctxt the validation context |
2176 | | * @param err the error number |
2177 | | * @param node the node |
2178 | | * @param child the node child generating the problem. |
2179 | | * @param arg1 the first argument |
2180 | | * @param arg2 the second argument |
2181 | | */ |
2182 | | static void |
2183 | | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
2184 | | xmlRelaxNGValidErr err, xmlNodePtr node, |
2185 | | xmlNodePtr child, const xmlChar * arg1, |
2186 | | const xmlChar * arg2) |
2187 | 0 | { |
2188 | 0 | xmlChar *msg; |
2189 | |
|
2190 | 0 | if (ctxt->flags & FLAGS_NOERROR) |
2191 | 0 | return; |
2192 | | |
2193 | 0 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
2194 | 0 | if (msg == NULL) |
2195 | 0 | return; |
2196 | | |
2197 | 0 | if (ctxt->errNo == XML_RELAXNG_OK) |
2198 | 0 | ctxt->errNo = err; |
2199 | 0 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
2200 | 0 | (const char *) msg, arg1, arg2); |
2201 | 0 | xmlFree(msg); |
2202 | 0 | } |
2203 | | |
2204 | | /** |
2205 | | * pop and discard all errors until the given level is reached |
2206 | | * |
2207 | | * @param ctxt the validation context |
2208 | | * @param level the error level in the stack |
2209 | | */ |
2210 | | static void |
2211 | | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
2212 | 0 | { |
2213 | 0 | int i; |
2214 | 0 | xmlRelaxNGValidErrorPtr err; |
2215 | |
|
2216 | 0 | for (i = level; i < ctxt->errNr; i++) { |
2217 | 0 | err = &ctxt->errTab[i]; |
2218 | 0 | if (err->flags & ERROR_IS_DUP) { |
2219 | 0 | if (err->arg1 != NULL) |
2220 | 0 | xmlFree((xmlChar *) err->arg1); |
2221 | 0 | err->arg1 = NULL; |
2222 | 0 | if (err->arg2 != NULL) |
2223 | 0 | xmlFree((xmlChar *) err->arg2); |
2224 | 0 | err->arg2 = NULL; |
2225 | 0 | err->flags = 0; |
2226 | 0 | } |
2227 | 0 | } |
2228 | 0 | ctxt->errNr = level; |
2229 | 0 | if (ctxt->errNr <= 0) |
2230 | 0 | ctxt->err = NULL; |
2231 | 0 | } |
2232 | | |
2233 | | /** |
2234 | | * Show all validation error over a given index. |
2235 | | * |
2236 | | * @param ctxt the validation context |
2237 | | */ |
2238 | | static void |
2239 | | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
2240 | 0 | { |
2241 | 0 | int i, j, k; |
2242 | 0 | xmlRelaxNGValidErrorPtr err, dup; |
2243 | | |
2244 | 0 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
2245 | 0 | err = &ctxt->errTab[i]; |
2246 | 0 | if (k < MAX_ERROR) { |
2247 | 0 | for (j = 0; j < i; j++) { |
2248 | 0 | dup = &ctxt->errTab[j]; |
2249 | 0 | if ((err->err == dup->err) && (err->node == dup->node) && |
2250 | 0 | (xmlStrEqual(err->arg1, dup->arg1)) && |
2251 | 0 | (xmlStrEqual(err->arg2, dup->arg2))) { |
2252 | 0 | goto skip; |
2253 | 0 | } |
2254 | 0 | } |
2255 | 0 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
2256 | 0 | err->arg1, err->arg2); |
2257 | 0 | k++; |
2258 | 0 | } |
2259 | 0 | skip: |
2260 | 0 | if (err->flags & ERROR_IS_DUP) { |
2261 | 0 | if (err->arg1 != NULL) |
2262 | 0 | xmlFree((xmlChar *) err->arg1); |
2263 | 0 | err->arg1 = NULL; |
2264 | 0 | if (err->arg2 != NULL) |
2265 | 0 | xmlFree((xmlChar *) err->arg2); |
2266 | 0 | err->arg2 = NULL; |
2267 | 0 | err->flags = 0; |
2268 | 0 | } |
2269 | 0 | } |
2270 | 0 | ctxt->errNr = 0; |
2271 | 0 | } |
2272 | | |
2273 | | /** |
2274 | | * Register a validation error, either generating it if it's sure |
2275 | | * or stacking it for later handling if unsure. |
2276 | | * |
2277 | | * @param ctxt the validation context |
2278 | | * @param err the error number |
2279 | | * @param arg1 the first argument |
2280 | | * @param arg2 the second argument |
2281 | | * @param dup need to dup the args |
2282 | | */ |
2283 | | static void |
2284 | | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
2285 | | xmlRelaxNGValidErr err, const xmlChar * arg1, |
2286 | | const xmlChar * arg2, int dup) |
2287 | 0 | { |
2288 | 0 | if (ctxt == NULL) |
2289 | 0 | return; |
2290 | 0 | if (ctxt->flags & FLAGS_NOERROR) |
2291 | 0 | return; |
2292 | | |
2293 | | /* |
2294 | | * generate the error directly |
2295 | | */ |
2296 | 0 | if (((ctxt->flags & FLAGS_IGNORABLE) == 0) || |
2297 | 0 | (ctxt->flags & FLAGS_NEGATIVE)) { |
2298 | 0 | xmlNodePtr node, seq; |
2299 | | |
2300 | | /* |
2301 | | * Flush first any stacked error which might be the |
2302 | | * real cause of the problem. |
2303 | | */ |
2304 | 0 | if (ctxt->errNr != 0) |
2305 | 0 | xmlRelaxNGDumpValidError(ctxt); |
2306 | 0 | if (ctxt->state != NULL) { |
2307 | 0 | node = ctxt->state->node; |
2308 | 0 | seq = ctxt->state->seq; |
2309 | 0 | } else { |
2310 | 0 | node = seq = NULL; |
2311 | 0 | } |
2312 | 0 | if ((node == NULL) && (seq == NULL)) { |
2313 | 0 | node = ctxt->pnode; |
2314 | 0 | } |
2315 | 0 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
2316 | 0 | } |
2317 | | /* |
2318 | | * Stack the error for later processing if needed |
2319 | | */ |
2320 | 0 | else { |
2321 | 0 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
2322 | 0 | } |
2323 | 0 | } |
2324 | | |
2325 | | |
2326 | | /************************************************************************ |
2327 | | * * |
2328 | | * Type library hooks * |
2329 | | * * |
2330 | | ************************************************************************/ |
2331 | | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
2332 | | const xmlChar * str); |
2333 | | |
2334 | | /** |
2335 | | * Check if the given type is provided by |
2336 | | * the W3C XMLSchema Datatype library. |
2337 | | * |
2338 | | * @param data data needed for the library |
2339 | | * @param type the type name |
2340 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2341 | | */ |
2342 | | static int |
2343 | | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
2344 | 0 | { |
2345 | 0 | xmlSchemaTypePtr typ; |
2346 | |
|
2347 | 0 | if (type == NULL) |
2348 | 0 | return (-1); |
2349 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2350 | 0 | BAD_CAST |
2351 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2352 | 0 | if (typ == NULL) |
2353 | 0 | return (0); |
2354 | 0 | return (1); |
2355 | 0 | } |
2356 | | |
2357 | | /** |
2358 | | * Check if the given type and value are validated by |
2359 | | * the W3C XMLSchema Datatype library. |
2360 | | * |
2361 | | * @param data data needed for the library |
2362 | | * @param type the type name |
2363 | | * @param value the value to check |
2364 | | * @param result pointer to result |
2365 | | * @param node the node |
2366 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2367 | | */ |
2368 | | static int |
2369 | | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
2370 | | const xmlChar * type, |
2371 | | const xmlChar * value, |
2372 | | void **result, xmlNodePtr node) |
2373 | 0 | { |
2374 | 0 | xmlSchemaTypePtr typ; |
2375 | 0 | int ret; |
2376 | |
|
2377 | 0 | if ((type == NULL) || (value == NULL)) |
2378 | 0 | return (-1); |
2379 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2380 | 0 | BAD_CAST |
2381 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2382 | 0 | if (typ == NULL) |
2383 | 0 | return (-1); |
2384 | 0 | ret = xmlSchemaValPredefTypeNode(typ, value, |
2385 | 0 | (xmlSchemaValPtr *) result, node); |
2386 | 0 | if (ret == 2) /* special ID error code */ |
2387 | 0 | return (2); |
2388 | 0 | if (ret == 0) |
2389 | 0 | return (1); |
2390 | 0 | if (ret > 0) |
2391 | 0 | return (0); |
2392 | 0 | return (-1); |
2393 | 0 | } |
2394 | | |
2395 | | /** |
2396 | | * Function provided by a type library to check a value facet |
2397 | | * |
2398 | | * @param data data needed for the library |
2399 | | * @param type the type name |
2400 | | * @param facetname the facet name |
2401 | | * @param val the facet value |
2402 | | * @param strval the string value |
2403 | | * @param value the value to check |
2404 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2405 | | */ |
2406 | | static int |
2407 | | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
2408 | | const xmlChar * type, const xmlChar * facetname, |
2409 | | const xmlChar * val, const xmlChar * strval, |
2410 | | void *value) |
2411 | 0 | { |
2412 | 0 | xmlSchemaFacetPtr facet; |
2413 | 0 | xmlSchemaTypePtr typ; |
2414 | 0 | int ret; |
2415 | |
|
2416 | 0 | if ((type == NULL) || (strval == NULL)) |
2417 | 0 | return (-1); |
2418 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2419 | 0 | BAD_CAST |
2420 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2421 | 0 | if (typ == NULL) |
2422 | 0 | return (-1); |
2423 | | |
2424 | 0 | facet = xmlSchemaNewFacet(); |
2425 | 0 | if (facet == NULL) |
2426 | 0 | return (-1); |
2427 | | |
2428 | 0 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
2429 | 0 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
2430 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
2431 | 0 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
2432 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
2433 | 0 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
2434 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
2435 | 0 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
2436 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
2437 | 0 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
2438 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
2439 | 0 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
2440 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
2441 | 0 | facet->type = XML_SCHEMA_FACET_PATTERN; |
2442 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
2443 | 0 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
2444 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
2445 | 0 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
2446 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
2447 | 0 | facet->type = XML_SCHEMA_FACET_LENGTH; |
2448 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
2449 | 0 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
2450 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
2451 | 0 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
2452 | 0 | } else { |
2453 | 0 | xmlSchemaFreeFacet(facet); |
2454 | 0 | return (-1); |
2455 | 0 | } |
2456 | 0 | facet->value = val; |
2457 | 0 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
2458 | 0 | if (ret != 0) { |
2459 | 0 | xmlSchemaFreeFacet(facet); |
2460 | 0 | return (-1); |
2461 | 0 | } |
2462 | 0 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
2463 | 0 | xmlSchemaFreeFacet(facet); |
2464 | 0 | if (ret != 0) |
2465 | 0 | return (-1); |
2466 | 0 | return (0); |
2467 | 0 | } |
2468 | | |
2469 | | /** |
2470 | | * Function provided by a type library to free a Schemas value |
2471 | | * |
2472 | | * @param data data needed for the library |
2473 | | * @param value the value to free |
2474 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2475 | | */ |
2476 | | static void |
2477 | | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
2478 | 0 | { |
2479 | 0 | xmlSchemaFreeValue(value); |
2480 | 0 | } |
2481 | | |
2482 | | /** |
2483 | | * Compare two values for equality accordingly a type from the W3C XMLSchema |
2484 | | * Datatype library. |
2485 | | * |
2486 | | * @param data data needed for the library |
2487 | | * @param type the type name |
2488 | | * @param value1 the first value |
2489 | | * @param ctxt1 the first context node |
2490 | | * @param comp1 value to compare with |
2491 | | * @param value2 the second value |
2492 | | * @param ctxt2 the second context node |
2493 | | * @returns 1 if equal, 0 if no and -1 in case of error. |
2494 | | */ |
2495 | | static int |
2496 | | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
2497 | | const xmlChar * type, |
2498 | | const xmlChar * value1, |
2499 | | xmlNodePtr ctxt1, |
2500 | | void *comp1, |
2501 | | const xmlChar * value2, xmlNodePtr ctxt2) |
2502 | 0 | { |
2503 | 0 | int ret; |
2504 | 0 | xmlSchemaTypePtr typ; |
2505 | 0 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
2506 | |
|
2507 | 0 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
2508 | 0 | return (-1); |
2509 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2510 | 0 | BAD_CAST |
2511 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2512 | 0 | if (typ == NULL) |
2513 | 0 | return (-1); |
2514 | 0 | if (comp1 == NULL) { |
2515 | 0 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
2516 | 0 | if (ret != 0) |
2517 | 0 | return (-1); |
2518 | 0 | if (res1 == NULL) |
2519 | 0 | return (-1); |
2520 | 0 | } else { |
2521 | 0 | res1 = (xmlSchemaValPtr) comp1; |
2522 | 0 | } |
2523 | 0 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
2524 | 0 | if (ret != 0) { |
2525 | 0 | if (res1 != (xmlSchemaValPtr) comp1) |
2526 | 0 | xmlSchemaFreeValue(res1); |
2527 | 0 | return (-1); |
2528 | 0 | } |
2529 | 0 | ret = xmlSchemaCompareValues(res1, res2); |
2530 | 0 | if (res1 != (xmlSchemaValPtr) comp1) |
2531 | 0 | xmlSchemaFreeValue(res1); |
2532 | 0 | xmlSchemaFreeValue(res2); |
2533 | 0 | if (ret == -2) |
2534 | 0 | return (-1); |
2535 | 0 | if (ret == 0) |
2536 | 0 | return (1); |
2537 | 0 | return (0); |
2538 | 0 | } |
2539 | | |
2540 | | /** |
2541 | | * Check if the given type is provided by |
2542 | | * the default datatype library. |
2543 | | * |
2544 | | * @param data data needed for the library |
2545 | | * @param type the type name |
2546 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2547 | | */ |
2548 | | static int |
2549 | | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
2550 | | const xmlChar * type) |
2551 | 0 | { |
2552 | 0 | if (type == NULL) |
2553 | 0 | return (-1); |
2554 | 0 | if (xmlStrEqual(type, BAD_CAST "string")) |
2555 | 0 | return (1); |
2556 | 0 | if (xmlStrEqual(type, BAD_CAST "token")) |
2557 | 0 | return (1); |
2558 | 0 | return (0); |
2559 | 0 | } |
2560 | | |
2561 | | /** |
2562 | | * Check if the given type and value are validated by |
2563 | | * the default datatype library. |
2564 | | * |
2565 | | * @param data data needed for the library |
2566 | | * @param type the type name |
2567 | | * @param value the value to check |
2568 | | * @param result pointer to result |
2569 | | * @param node the node |
2570 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2571 | | */ |
2572 | | static int |
2573 | | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
2574 | | const xmlChar * type ATTRIBUTE_UNUSED, |
2575 | | const xmlChar * value ATTRIBUTE_UNUSED, |
2576 | | void **result ATTRIBUTE_UNUSED, |
2577 | | xmlNodePtr node ATTRIBUTE_UNUSED) |
2578 | 0 | { |
2579 | 0 | if (value == NULL) |
2580 | 0 | return (-1); |
2581 | 0 | if (xmlStrEqual(type, BAD_CAST "string")) |
2582 | 0 | return (1); |
2583 | 0 | if (xmlStrEqual(type, BAD_CAST "token")) { |
2584 | 0 | return (1); |
2585 | 0 | } |
2586 | | |
2587 | 0 | return (0); |
2588 | 0 | } |
2589 | | |
2590 | | /** |
2591 | | * Compare two values accordingly a type from the default |
2592 | | * datatype library. |
2593 | | * |
2594 | | * @param data data needed for the library |
2595 | | * @param type the type name |
2596 | | * @param value1 the first value |
2597 | | * @param ctxt1 the first context node |
2598 | | * @param comp1 value to compare with |
2599 | | * @param value2 the second value |
2600 | | * @param ctxt2 the second context node |
2601 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2602 | | */ |
2603 | | static int |
2604 | | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
2605 | | const xmlChar * type, |
2606 | | const xmlChar * value1, |
2607 | | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
2608 | | void *comp1 ATTRIBUTE_UNUSED, |
2609 | | const xmlChar * value2, |
2610 | | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
2611 | 0 | { |
2612 | 0 | int ret = -1; |
2613 | |
|
2614 | 0 | if (xmlStrEqual(type, BAD_CAST "string")) { |
2615 | 0 | ret = xmlStrEqual(value1, value2); |
2616 | 0 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
2617 | 0 | if (!xmlStrEqual(value1, value2)) { |
2618 | 0 | xmlChar *nval, *nvalue; |
2619 | | |
2620 | | /* |
2621 | | * TODO: trivial optimizations are possible by |
2622 | | * computing at compile-time |
2623 | | */ |
2624 | 0 | nval = xmlRelaxNGNormalize(NULL, value1); |
2625 | 0 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
2626 | |
|
2627 | 0 | if ((nval == NULL) || (nvalue == NULL)) |
2628 | 0 | ret = -1; |
2629 | 0 | else if (xmlStrEqual(nval, nvalue)) |
2630 | 0 | ret = 1; |
2631 | 0 | else |
2632 | 0 | ret = 0; |
2633 | 0 | if (nval != NULL) |
2634 | 0 | xmlFree(nval); |
2635 | 0 | if (nvalue != NULL) |
2636 | 0 | xmlFree(nvalue); |
2637 | 0 | } else |
2638 | 0 | ret = 1; |
2639 | 0 | } |
2640 | 0 | return (ret); |
2641 | 0 | } |
2642 | | |
2643 | | static int xmlRelaxNGTypeInitialized = 0; |
2644 | | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
2645 | | |
2646 | | /** |
2647 | | * Free the structure associated to the type library |
2648 | | * |
2649 | | * @param payload the type library structure |
2650 | | * @param namespace the URI bound to the library |
2651 | | */ |
2652 | | static void |
2653 | | xmlRelaxNGFreeTypeLibrary(void *payload, |
2654 | | const xmlChar * namespace ATTRIBUTE_UNUSED) |
2655 | 0 | { |
2656 | 0 | xmlRelaxNGTypeLibraryPtr lib = (xmlRelaxNGTypeLibraryPtr) payload; |
2657 | 0 | if (lib == NULL) |
2658 | 0 | return; |
2659 | 0 | if (lib->namespace != NULL) |
2660 | 0 | xmlFree((xmlChar *) lib->namespace); |
2661 | 0 | xmlFree(lib); |
2662 | 0 | } |
2663 | | |
2664 | | /** |
2665 | | * Register a new type library |
2666 | | * |
2667 | | * @param namespace the URI bound to the library |
2668 | | * @param data data associated to the library |
2669 | | * @param have the provide function |
2670 | | * @param check the checking function |
2671 | | * @param comp the comparison function |
2672 | | * @param facet facet check function |
2673 | | * @param freef free function |
2674 | | * @returns 0 in case of success and -1 in case of error. |
2675 | | */ |
2676 | | static int |
2677 | | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
2678 | | xmlRelaxNGTypeHave have, |
2679 | | xmlRelaxNGTypeCheck check, |
2680 | | xmlRelaxNGTypeCompare comp, |
2681 | | xmlRelaxNGFacetCheck facet, |
2682 | | xmlRelaxNGTypeFree freef) |
2683 | 0 | { |
2684 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
2685 | 0 | int ret; |
2686 | |
|
2687 | 0 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
2688 | 0 | (check == NULL) || (comp == NULL)) |
2689 | 0 | return (-1); |
2690 | 0 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) |
2691 | 0 | return (-1); |
2692 | 0 | lib = |
2693 | 0 | (xmlRelaxNGTypeLibraryPtr) |
2694 | 0 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
2695 | 0 | if (lib == NULL) { |
2696 | 0 | xmlRngVErrMemory(NULL); |
2697 | 0 | return (-1); |
2698 | 0 | } |
2699 | 0 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
2700 | 0 | lib->namespace = xmlStrdup(namespace); |
2701 | 0 | lib->data = data; |
2702 | 0 | lib->have = have; |
2703 | 0 | lib->comp = comp; |
2704 | 0 | lib->check = check; |
2705 | 0 | lib->facet = facet; |
2706 | 0 | lib->freef = freef; |
2707 | 0 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
2708 | 0 | if (ret < 0) { |
2709 | 0 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
2710 | 0 | return (-1); |
2711 | 0 | } |
2712 | 0 | return (0); |
2713 | 0 | } |
2714 | | |
2715 | | static xmlMutex xmlRelaxNGMutex; |
2716 | | |
2717 | | void |
2718 | | xmlInitRelaxNGInternal(void) |
2719 | 17.1k | { |
2720 | 17.1k | xmlInitMutex(&xmlRelaxNGMutex); |
2721 | 17.1k | } |
2722 | | |
2723 | | void |
2724 | | xmlCleanupRelaxNGInternal(void) |
2725 | 17.1k | { |
2726 | 17.1k | xmlCleanupMutex(&xmlRelaxNGMutex); |
2727 | 17.1k | } |
2728 | | |
2729 | | /** |
2730 | | * Initialize the default type libraries. |
2731 | | * |
2732 | | * @returns 0 in case of success and -1 in case of error. |
2733 | | */ |
2734 | | int |
2735 | | xmlRelaxNGInitTypes(void) |
2736 | 0 | { |
2737 | 0 | xmlInitParser(); |
2738 | |
|
2739 | 0 | xmlMutexLock(&xmlRelaxNGMutex); |
2740 | 0 | if (xmlRelaxNGTypeInitialized != 0) { |
2741 | 0 | xmlMutexUnlock(&xmlRelaxNGMutex); |
2742 | 0 | return (0); |
2743 | 0 | } |
2744 | | |
2745 | 0 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
2746 | 0 | if (xmlRelaxNGRegisteredTypes == NULL) { |
2747 | 0 | xmlMutexUnlock(&xmlRelaxNGMutex); |
2748 | 0 | return (-1); |
2749 | 0 | } |
2750 | 0 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
2751 | 0 | "http://www.w3.org/2001/XMLSchema-datatypes", |
2752 | 0 | NULL, xmlRelaxNGSchemaTypeHave, |
2753 | 0 | xmlRelaxNGSchemaTypeCheck, |
2754 | 0 | xmlRelaxNGSchemaTypeCompare, |
2755 | 0 | xmlRelaxNGSchemaFacetCheck, |
2756 | 0 | xmlRelaxNGSchemaFreeValue); |
2757 | 0 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
2758 | 0 | xmlRelaxNGDefaultTypeHave, |
2759 | 0 | xmlRelaxNGDefaultTypeCheck, |
2760 | 0 | xmlRelaxNGDefaultTypeCompare, NULL, |
2761 | 0 | NULL); |
2762 | 0 | xmlRelaxNGTypeInitialized = 1; |
2763 | 0 | xmlMutexUnlock(&xmlRelaxNGMutex); |
2764 | 0 | return (0); |
2765 | 0 | } |
2766 | | |
2767 | | /** |
2768 | | * Cleanup the default Schemas type library associated to RelaxNG |
2769 | | * |
2770 | | * @deprecated This function will be made private. Call #xmlCleanupParser |
2771 | | * to free global state but see the warnings there. #xmlCleanupParser |
2772 | | * should be only called once at program exit. In most cases, you don't |
2773 | | * have call cleanup functions at all. |
2774 | | * |
2775 | | */ |
2776 | | void |
2777 | | xmlRelaxNGCleanupTypes(void) |
2778 | 17.1k | { |
2779 | 17.1k | xmlSchemaCleanupTypes(); |
2780 | 17.1k | xmlMutexLock(&xmlRelaxNGMutex); |
2781 | 17.1k | if (xmlRelaxNGTypeInitialized == 0) { |
2782 | 17.1k | xmlMutexUnlock(&xmlRelaxNGMutex); |
2783 | 17.1k | return; |
2784 | 17.1k | } |
2785 | 0 | xmlHashFree(xmlRelaxNGRegisteredTypes, xmlRelaxNGFreeTypeLibrary); |
2786 | 0 | xmlRelaxNGTypeInitialized = 0; |
2787 | 0 | xmlMutexUnlock(&xmlRelaxNGMutex); |
2788 | 0 | } |
2789 | | |
2790 | | /************************************************************************ |
2791 | | * * |
2792 | | * Compiling element content into regexp * |
2793 | | * * |
2794 | | * Sometime the element content can be compiled into a pure regexp, * |
2795 | | * This allows a faster execution and streamability at that level * |
2796 | | * * |
2797 | | ************************************************************************/ |
2798 | | |
2799 | | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
2800 | | xmlRelaxNGDefinePtr def); |
2801 | | |
2802 | | /** |
2803 | | * Check if a definition is nullable. |
2804 | | * |
2805 | | * @param def the definition to check |
2806 | | * @returns 1 if yes, 0 if no and -1 in case of error |
2807 | | */ |
2808 | | static int |
2809 | | xmlRelaxNGIsCompilable(xmlRelaxNGDefinePtr def) |
2810 | 0 | { |
2811 | 0 | int ret = -1; |
2812 | |
|
2813 | 0 | if (def == NULL) { |
2814 | 0 | return (-1); |
2815 | 0 | } |
2816 | 0 | if ((def->type != XML_RELAXNG_ELEMENT) && |
2817 | 0 | (def->dflags & IS_COMPILABLE)) |
2818 | 0 | return (1); |
2819 | 0 | if ((def->type != XML_RELAXNG_ELEMENT) && |
2820 | 0 | (def->dflags & IS_NOT_COMPILABLE)) |
2821 | 0 | return (0); |
2822 | 0 | switch (def->type) { |
2823 | 0 | case XML_RELAXNG_NOOP: |
2824 | 0 | ret = xmlRelaxNGIsCompilable(def->content); |
2825 | 0 | break; |
2826 | 0 | case XML_RELAXNG_TEXT: |
2827 | 0 | case XML_RELAXNG_EMPTY: |
2828 | 0 | ret = 1; |
2829 | 0 | break; |
2830 | 0 | case XML_RELAXNG_ELEMENT: |
2831 | | /* |
2832 | | * Check if the element content is compilable |
2833 | | */ |
2834 | 0 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
2835 | 0 | ((def->dflags & IS_COMPILABLE) == 0)) { |
2836 | 0 | xmlRelaxNGDefinePtr list; |
2837 | |
|
2838 | 0 | list = def->content; |
2839 | 0 | while (list != NULL) { |
2840 | 0 | ret = xmlRelaxNGIsCompilable(list); |
2841 | 0 | if (ret != 1) |
2842 | 0 | break; |
2843 | 0 | list = list->next; |
2844 | 0 | } |
2845 | | /* |
2846 | | * Because the routine is recursive, we must guard against |
2847 | | * discovering both COMPILABLE and NOT_COMPILABLE |
2848 | | */ |
2849 | 0 | if (ret == 0) { |
2850 | 0 | def->dflags &= ~IS_COMPILABLE; |
2851 | 0 | def->dflags |= IS_NOT_COMPILABLE; |
2852 | 0 | } |
2853 | 0 | if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE)) |
2854 | 0 | def->dflags |= IS_COMPILABLE; |
2855 | 0 | } |
2856 | | /* |
2857 | | * All elements return a compilable status unless they |
2858 | | * are generic like anyName |
2859 | | */ |
2860 | 0 | if ((def->nameClass != NULL) || (def->name == NULL)) |
2861 | 0 | ret = 0; |
2862 | 0 | else |
2863 | 0 | ret = 1; |
2864 | 0 | return (ret); |
2865 | 0 | case XML_RELAXNG_REF: |
2866 | 0 | case XML_RELAXNG_EXTERNALREF: |
2867 | 0 | case XML_RELAXNG_PARENTREF: |
2868 | 0 | if (def->depth == -20) { |
2869 | 0 | return (1); |
2870 | 0 | } else { |
2871 | 0 | xmlRelaxNGDefinePtr list; |
2872 | |
|
2873 | 0 | def->depth = -20; |
2874 | 0 | list = def->content; |
2875 | 0 | while (list != NULL) { |
2876 | 0 | ret = xmlRelaxNGIsCompilable(list); |
2877 | 0 | if (ret != 1) |
2878 | 0 | break; |
2879 | 0 | list = list->next; |
2880 | 0 | } |
2881 | 0 | } |
2882 | 0 | break; |
2883 | 0 | case XML_RELAXNG_START: |
2884 | 0 | case XML_RELAXNG_OPTIONAL: |
2885 | 0 | case XML_RELAXNG_ZEROORMORE: |
2886 | 0 | case XML_RELAXNG_ONEORMORE: |
2887 | 0 | case XML_RELAXNG_CHOICE: |
2888 | 0 | case XML_RELAXNG_GROUP: |
2889 | 0 | case XML_RELAXNG_DEF:{ |
2890 | 0 | xmlRelaxNGDefinePtr list; |
2891 | |
|
2892 | 0 | list = def->content; |
2893 | 0 | while (list != NULL) { |
2894 | 0 | ret = xmlRelaxNGIsCompilable(list); |
2895 | 0 | if (ret != 1) |
2896 | 0 | break; |
2897 | 0 | list = list->next; |
2898 | 0 | } |
2899 | 0 | break; |
2900 | 0 | } |
2901 | 0 | case XML_RELAXNG_EXCEPT: |
2902 | 0 | case XML_RELAXNG_ATTRIBUTE: |
2903 | 0 | case XML_RELAXNG_INTERLEAVE: |
2904 | 0 | case XML_RELAXNG_DATATYPE: |
2905 | 0 | case XML_RELAXNG_LIST: |
2906 | 0 | case XML_RELAXNG_PARAM: |
2907 | 0 | case XML_RELAXNG_VALUE: |
2908 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
2909 | 0 | ret = 0; |
2910 | 0 | break; |
2911 | 0 | } |
2912 | 0 | if (ret == 0) |
2913 | 0 | def->dflags |= IS_NOT_COMPILABLE; |
2914 | 0 | if (ret == 1) |
2915 | 0 | def->dflags |= IS_COMPILABLE; |
2916 | 0 | return (ret); |
2917 | 0 | } |
2918 | | |
2919 | | /** |
2920 | | * Compile the set of definitions, it works recursively, till the |
2921 | | * element boundaries, where it tries to compile the content if possible |
2922 | | * |
2923 | | * @param ctxt the RelaxNG parser context |
2924 | | * @param def the definition tree to compile |
2925 | | * @returns 0 if success and -1 in case of error |
2926 | | */ |
2927 | | static int |
2928 | | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
2929 | 0 | { |
2930 | 0 | int ret = 0; |
2931 | 0 | xmlRelaxNGDefinePtr list; |
2932 | |
|
2933 | 0 | if ((ctxt == NULL) || (def == NULL)) |
2934 | 0 | return (-1); |
2935 | | |
2936 | 0 | switch (def->type) { |
2937 | 0 | case XML_RELAXNG_START: |
2938 | 0 | if ((xmlRelaxNGIsCompilable(def) == 1) && (def->depth != -25)) { |
2939 | 0 | xmlAutomataPtr oldam = ctxt->am; |
2940 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
2941 | |
|
2942 | 0 | def->depth = -25; |
2943 | |
|
2944 | 0 | list = def->content; |
2945 | 0 | ctxt->am = xmlNewAutomata(); |
2946 | 0 | if (ctxt->am == NULL) |
2947 | 0 | return (-1); |
2948 | | |
2949 | | /* |
2950 | | * assume identical strings but not same pointer are different |
2951 | | * atoms, needed for non-determinism detection |
2952 | | * That way if 2 elements with the same name are in a choice |
2953 | | * branch the automata is found non-deterministic and |
2954 | | * we fallback to the normal validation which does the right |
2955 | | * thing of exploring both choices. |
2956 | | */ |
2957 | 0 | xmlAutomataSetFlags(ctxt->am, 1); |
2958 | |
|
2959 | 0 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
2960 | 0 | while (list != NULL) { |
2961 | 0 | xmlRelaxNGCompile(ctxt, list); |
2962 | 0 | list = list->next; |
2963 | 0 | } |
2964 | 0 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
2965 | 0 | if (xmlAutomataIsDeterminist(ctxt->am)) |
2966 | 0 | def->contModel = xmlAutomataCompile(ctxt->am); |
2967 | |
|
2968 | 0 | xmlFreeAutomata(ctxt->am); |
2969 | 0 | ctxt->state = oldstate; |
2970 | 0 | ctxt->am = oldam; |
2971 | 0 | } |
2972 | 0 | break; |
2973 | 0 | case XML_RELAXNG_ELEMENT: |
2974 | 0 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
2975 | 0 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
2976 | 0 | ctxt->state, NULL, |
2977 | 0 | def->name, def->ns, |
2978 | 0 | def); |
2979 | 0 | } |
2980 | 0 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
2981 | 0 | xmlAutomataPtr oldam = ctxt->am; |
2982 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
2983 | |
|
2984 | 0 | def->depth = -25; |
2985 | |
|
2986 | 0 | list = def->content; |
2987 | 0 | ctxt->am = xmlNewAutomata(); |
2988 | 0 | if (ctxt->am == NULL) |
2989 | 0 | return (-1); |
2990 | 0 | xmlAutomataSetFlags(ctxt->am, 1); |
2991 | 0 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
2992 | 0 | while (list != NULL) { |
2993 | 0 | xmlRelaxNGCompile(ctxt, list); |
2994 | 0 | list = list->next; |
2995 | 0 | } |
2996 | 0 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
2997 | 0 | def->contModel = xmlAutomataCompile(ctxt->am); |
2998 | 0 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
2999 | | /* |
3000 | | * we can only use the automata if it is determinist |
3001 | | */ |
3002 | 0 | xmlRegFreeRegexp(def->contModel); |
3003 | 0 | def->contModel = NULL; |
3004 | 0 | } |
3005 | 0 | xmlFreeAutomata(ctxt->am); |
3006 | 0 | ctxt->state = oldstate; |
3007 | 0 | ctxt->am = oldam; |
3008 | 0 | } else { |
3009 | 0 | xmlAutomataPtr oldam = ctxt->am; |
3010 | | |
3011 | | /* |
3012 | | * we can't build the content model for this element content |
3013 | | * but it still might be possible to build it for some of its |
3014 | | * children, recurse. |
3015 | | */ |
3016 | 0 | ret = xmlRelaxNGTryCompile(ctxt, def); |
3017 | 0 | ctxt->am = oldam; |
3018 | 0 | } |
3019 | 0 | break; |
3020 | 0 | case XML_RELAXNG_NOOP: |
3021 | 0 | ret = xmlRelaxNGCompile(ctxt, def->content); |
3022 | 0 | break; |
3023 | 0 | case XML_RELAXNG_OPTIONAL:{ |
3024 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
3025 | |
|
3026 | 0 | list = def->content; |
3027 | 0 | while (list != NULL) { |
3028 | 0 | xmlRelaxNGCompile(ctxt, list); |
3029 | 0 | list = list->next; |
3030 | 0 | } |
3031 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
3032 | 0 | break; |
3033 | 0 | } |
3034 | 0 | case XML_RELAXNG_ZEROORMORE:{ |
3035 | 0 | xmlAutomataStatePtr oldstate; |
3036 | |
|
3037 | 0 | ctxt->state = |
3038 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
3039 | 0 | oldstate = ctxt->state; |
3040 | 0 | list = def->content; |
3041 | 0 | while (list != NULL) { |
3042 | 0 | xmlRelaxNGCompile(ctxt, list); |
3043 | 0 | list = list->next; |
3044 | 0 | } |
3045 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
3046 | 0 | ctxt->state = |
3047 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
3048 | 0 | break; |
3049 | 0 | } |
3050 | 0 | case XML_RELAXNG_ONEORMORE:{ |
3051 | 0 | xmlAutomataStatePtr oldstate; |
3052 | |
|
3053 | 0 | list = def->content; |
3054 | 0 | while (list != NULL) { |
3055 | 0 | xmlRelaxNGCompile(ctxt, list); |
3056 | 0 | list = list->next; |
3057 | 0 | } |
3058 | 0 | oldstate = ctxt->state; |
3059 | 0 | list = def->content; |
3060 | 0 | while (list != NULL) { |
3061 | 0 | xmlRelaxNGCompile(ctxt, list); |
3062 | 0 | list = list->next; |
3063 | 0 | } |
3064 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
3065 | 0 | ctxt->state = |
3066 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
3067 | 0 | break; |
3068 | 0 | } |
3069 | 0 | case XML_RELAXNG_CHOICE:{ |
3070 | 0 | xmlAutomataStatePtr target = NULL; |
3071 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
3072 | |
|
3073 | 0 | list = def->content; |
3074 | 0 | while (list != NULL) { |
3075 | 0 | ctxt->state = oldstate; |
3076 | 0 | ret = xmlRelaxNGCompile(ctxt, list); |
3077 | 0 | if (ret != 0) |
3078 | 0 | break; |
3079 | 0 | if (target == NULL) |
3080 | 0 | target = ctxt->state; |
3081 | 0 | else { |
3082 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
3083 | 0 | target); |
3084 | 0 | } |
3085 | 0 | list = list->next; |
3086 | 0 | } |
3087 | 0 | ctxt->state = target; |
3088 | |
|
3089 | 0 | break; |
3090 | 0 | } |
3091 | 0 | case XML_RELAXNG_REF: |
3092 | 0 | case XML_RELAXNG_EXTERNALREF: |
3093 | 0 | case XML_RELAXNG_PARENTREF: |
3094 | 0 | case XML_RELAXNG_GROUP: |
3095 | 0 | case XML_RELAXNG_DEF: |
3096 | 0 | list = def->content; |
3097 | 0 | while (list != NULL) { |
3098 | 0 | ret = xmlRelaxNGCompile(ctxt, list); |
3099 | 0 | if (ret != 0) |
3100 | 0 | break; |
3101 | 0 | list = list->next; |
3102 | 0 | } |
3103 | 0 | break; |
3104 | 0 | case XML_RELAXNG_TEXT:{ |
3105 | 0 | xmlAutomataStatePtr oldstate; |
3106 | |
|
3107 | 0 | ctxt->state = |
3108 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
3109 | 0 | oldstate = ctxt->state; |
3110 | 0 | xmlRelaxNGCompile(ctxt, def->content); |
3111 | 0 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
3112 | 0 | ctxt->state, BAD_CAST "#text", |
3113 | 0 | NULL); |
3114 | 0 | ctxt->state = |
3115 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
3116 | 0 | break; |
3117 | 0 | } |
3118 | 0 | case XML_RELAXNG_EMPTY: |
3119 | 0 | ctxt->state = |
3120 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
3121 | 0 | break; |
3122 | 0 | case XML_RELAXNG_EXCEPT: |
3123 | 0 | case XML_RELAXNG_ATTRIBUTE: |
3124 | 0 | case XML_RELAXNG_INTERLEAVE: |
3125 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
3126 | 0 | case XML_RELAXNG_DATATYPE: |
3127 | 0 | case XML_RELAXNG_LIST: |
3128 | 0 | case XML_RELAXNG_PARAM: |
3129 | 0 | case XML_RELAXNG_VALUE: |
3130 | 0 | xmlRngPErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
3131 | 0 | "RNG internal error trying to compile %s\n", |
3132 | 0 | BAD_CAST xmlRelaxNGDefName(def), NULL); |
3133 | 0 | break; |
3134 | 0 | } |
3135 | 0 | return (ret); |
3136 | 0 | } |
3137 | | |
3138 | | /** |
3139 | | * Try to compile the set of definitions, it works recursively, |
3140 | | * possibly ignoring parts which cannot be compiled. |
3141 | | * |
3142 | | * @param ctxt the RelaxNG parser context |
3143 | | * @param def the definition tree to compile |
3144 | | * @returns 0 if success and -1 in case of error |
3145 | | */ |
3146 | | static int |
3147 | | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
3148 | 0 | { |
3149 | 0 | int ret = 0; |
3150 | 0 | xmlRelaxNGDefinePtr list; |
3151 | |
|
3152 | 0 | if ((ctxt == NULL) || (def == NULL)) |
3153 | 0 | return (-1); |
3154 | | |
3155 | 0 | if ((def->type == XML_RELAXNG_START) || |
3156 | 0 | (def->type == XML_RELAXNG_ELEMENT)) { |
3157 | 0 | ret = xmlRelaxNGIsCompilable(def); |
3158 | 0 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
3159 | 0 | ctxt->am = NULL; |
3160 | 0 | ret = xmlRelaxNGCompile(ctxt, def); |
3161 | 0 | return (ret); |
3162 | 0 | } |
3163 | 0 | } |
3164 | 0 | switch (def->type) { |
3165 | 0 | case XML_RELAXNG_NOOP: |
3166 | 0 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
3167 | 0 | break; |
3168 | 0 | case XML_RELAXNG_TEXT: |
3169 | 0 | case XML_RELAXNG_DATATYPE: |
3170 | 0 | case XML_RELAXNG_LIST: |
3171 | 0 | case XML_RELAXNG_PARAM: |
3172 | 0 | case XML_RELAXNG_VALUE: |
3173 | 0 | case XML_RELAXNG_EMPTY: |
3174 | 0 | case XML_RELAXNG_ELEMENT: |
3175 | 0 | ret = 0; |
3176 | 0 | break; |
3177 | 0 | case XML_RELAXNG_OPTIONAL: |
3178 | 0 | case XML_RELAXNG_ZEROORMORE: |
3179 | 0 | case XML_RELAXNG_ONEORMORE: |
3180 | 0 | case XML_RELAXNG_CHOICE: |
3181 | 0 | case XML_RELAXNG_GROUP: |
3182 | 0 | case XML_RELAXNG_DEF: |
3183 | 0 | case XML_RELAXNG_START: |
3184 | 0 | case XML_RELAXNG_REF: |
3185 | 0 | case XML_RELAXNG_EXTERNALREF: |
3186 | 0 | case XML_RELAXNG_PARENTREF: |
3187 | 0 | list = def->content; |
3188 | 0 | while (list != NULL) { |
3189 | 0 | ret = xmlRelaxNGTryCompile(ctxt, list); |
3190 | 0 | if (ret != 0) |
3191 | 0 | break; |
3192 | 0 | list = list->next; |
3193 | 0 | } |
3194 | 0 | break; |
3195 | 0 | case XML_RELAXNG_EXCEPT: |
3196 | 0 | case XML_RELAXNG_ATTRIBUTE: |
3197 | 0 | case XML_RELAXNG_INTERLEAVE: |
3198 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
3199 | 0 | ret = 0; |
3200 | 0 | break; |
3201 | 0 | } |
3202 | 0 | return (ret); |
3203 | 0 | } |
3204 | | |
3205 | | /************************************************************************ |
3206 | | * * |
3207 | | * Parsing functions * |
3208 | | * * |
3209 | | ************************************************************************/ |
3210 | | |
3211 | | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
3212 | | ctxt, xmlNodePtr node); |
3213 | | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
3214 | | ctxt, xmlNodePtr node); |
3215 | | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
3216 | | ctxt, xmlNodePtr nodes, |
3217 | | int group); |
3218 | | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
3219 | | ctxt, xmlNodePtr node); |
3220 | | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
3221 | | xmlNodePtr node); |
3222 | | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
3223 | | xmlNodePtr nodes); |
3224 | | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
3225 | | ctxt, xmlNodePtr node, |
3226 | | xmlRelaxNGDefinePtr |
3227 | | def); |
3228 | | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
3229 | | ctxt, xmlNodePtr nodes); |
3230 | | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
3231 | | xmlRelaxNGDefinePtr define, |
3232 | | xmlNodePtr elem); |
3233 | | |
3234 | | |
3235 | 0 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
3236 | | |
3237 | | /** |
3238 | | * Check if a definition is nullable. |
3239 | | * |
3240 | | * @param define the definition to verify |
3241 | | * @returns 1 if yes, 0 if no and -1 in case of error |
3242 | | */ |
3243 | | static int |
3244 | | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
3245 | 0 | { |
3246 | 0 | int ret; |
3247 | |
|
3248 | 0 | if (define == NULL) |
3249 | 0 | return (-1); |
3250 | | |
3251 | 0 | if (define->dflags & IS_NULLABLE) |
3252 | 0 | return (1); |
3253 | 0 | if (define->dflags & IS_NOT_NULLABLE) |
3254 | 0 | return (0); |
3255 | 0 | switch (define->type) { |
3256 | 0 | case XML_RELAXNG_EMPTY: |
3257 | 0 | case XML_RELAXNG_TEXT: |
3258 | 0 | ret = 1; |
3259 | 0 | break; |
3260 | 0 | case XML_RELAXNG_NOOP: |
3261 | 0 | case XML_RELAXNG_DEF: |
3262 | 0 | case XML_RELAXNG_REF: |
3263 | 0 | case XML_RELAXNG_EXTERNALREF: |
3264 | 0 | case XML_RELAXNG_PARENTREF: |
3265 | 0 | case XML_RELAXNG_ONEORMORE: |
3266 | 0 | ret = xmlRelaxNGIsNullable(define->content); |
3267 | 0 | break; |
3268 | 0 | case XML_RELAXNG_EXCEPT: |
3269 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
3270 | 0 | case XML_RELAXNG_ELEMENT: |
3271 | 0 | case XML_RELAXNG_DATATYPE: |
3272 | 0 | case XML_RELAXNG_PARAM: |
3273 | 0 | case XML_RELAXNG_VALUE: |
3274 | 0 | case XML_RELAXNG_LIST: |
3275 | 0 | case XML_RELAXNG_ATTRIBUTE: |
3276 | 0 | ret = 0; |
3277 | 0 | break; |
3278 | 0 | case XML_RELAXNG_CHOICE:{ |
3279 | 0 | xmlRelaxNGDefinePtr list = define->content; |
3280 | |
|
3281 | 0 | while (list != NULL) { |
3282 | 0 | ret = xmlRelaxNGIsNullable(list); |
3283 | 0 | if (ret != 0) |
3284 | 0 | goto done; |
3285 | 0 | list = list->next; |
3286 | 0 | } |
3287 | 0 | ret = 0; |
3288 | 0 | break; |
3289 | 0 | } |
3290 | 0 | case XML_RELAXNG_START: |
3291 | 0 | case XML_RELAXNG_INTERLEAVE: |
3292 | 0 | case XML_RELAXNG_GROUP:{ |
3293 | 0 | xmlRelaxNGDefinePtr list = define->content; |
3294 | |
|
3295 | 0 | while (list != NULL) { |
3296 | 0 | ret = xmlRelaxNGIsNullable(list); |
3297 | 0 | if (ret != 1) |
3298 | 0 | goto done; |
3299 | 0 | list = list->next; |
3300 | 0 | } |
3301 | 0 | return (1); |
3302 | 0 | } |
3303 | 0 | default: |
3304 | 0 | return (-1); |
3305 | 0 | } |
3306 | 0 | done: |
3307 | 0 | if (ret == 0) |
3308 | 0 | define->dflags |= IS_NOT_NULLABLE; |
3309 | 0 | if (ret == 1) |
3310 | 0 | define->dflags |= IS_NULLABLE; |
3311 | 0 | return (ret); |
3312 | 0 | } |
3313 | | |
3314 | | /** |
3315 | | * Check if a string is ignorable c.f. 4.2. Whitespace |
3316 | | * |
3317 | | * @param str a string |
3318 | | * @returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
3319 | | */ |
3320 | | static int |
3321 | | xmlRelaxNGIsBlank(xmlChar * str) |
3322 | 0 | { |
3323 | 0 | if (str == NULL) |
3324 | 0 | return (1); |
3325 | 0 | while (*str != 0) { |
3326 | 0 | if (!(IS_BLANK_CH(*str))) |
3327 | 0 | return (0); |
3328 | 0 | str++; |
3329 | 0 | } |
3330 | 0 | return (1); |
3331 | 0 | } |
3332 | | |
3333 | | /** |
3334 | | * Applies algorithm from 4.3. datatypeLibrary attribute |
3335 | | * |
3336 | | * @param ctxt a Relax-NG parser context |
3337 | | * @param node the current data or value element |
3338 | | * @returns the datatypeLibrary value or NULL if not found |
3339 | | */ |
3340 | | static xmlChar * |
3341 | | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
3342 | | xmlNodePtr node) |
3343 | 0 | { |
3344 | 0 | xmlChar *ret, *escape; |
3345 | |
|
3346 | 0 | if (node == NULL) |
3347 | 0 | return(NULL); |
3348 | | |
3349 | 0 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
3350 | 0 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
3351 | 0 | if (ret != NULL) { |
3352 | 0 | if (ret[0] == 0) { |
3353 | 0 | xmlFree(ret); |
3354 | 0 | return (NULL); |
3355 | 0 | } |
3356 | 0 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
3357 | 0 | if (escape == NULL) { |
3358 | 0 | return (ret); |
3359 | 0 | } |
3360 | 0 | xmlFree(ret); |
3361 | 0 | return (escape); |
3362 | 0 | } |
3363 | 0 | } |
3364 | 0 | node = node->parent; |
3365 | 0 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
3366 | 0 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
3367 | 0 | if (ret != NULL) { |
3368 | 0 | if (ret[0] == 0) { |
3369 | 0 | xmlFree(ret); |
3370 | 0 | return (NULL); |
3371 | 0 | } |
3372 | 0 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
3373 | 0 | if (escape == NULL) { |
3374 | 0 | return (ret); |
3375 | 0 | } |
3376 | 0 | xmlFree(ret); |
3377 | 0 | return (escape); |
3378 | 0 | } |
3379 | 0 | node = node->parent; |
3380 | 0 | } |
3381 | 0 | return (NULL); |
3382 | 0 | } |
3383 | | |
3384 | | /** |
3385 | | * parse the content of a RelaxNG value node. |
3386 | | * |
3387 | | * @param ctxt a Relax-NG parser context |
3388 | | * @param node the data node. |
3389 | | * @returns the definition pointer or NULL in case of error |
3390 | | */ |
3391 | | static xmlRelaxNGDefinePtr |
3392 | | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
3393 | 0 | { |
3394 | 0 | xmlRelaxNGDefinePtr def = NULL; |
3395 | 0 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
3396 | 0 | xmlChar *type; |
3397 | 0 | xmlChar *library; |
3398 | 0 | int success = 0; |
3399 | |
|
3400 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
3401 | 0 | if (def == NULL) |
3402 | 0 | return (NULL); |
3403 | 0 | def->type = XML_RELAXNG_VALUE; |
3404 | |
|
3405 | 0 | type = xmlGetProp(node, BAD_CAST "type"); |
3406 | 0 | if (type != NULL) { |
3407 | 0 | xmlRelaxNGNormExtSpace(type); |
3408 | 0 | if (xmlValidateNCName(type, 0)) { |
3409 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
3410 | 0 | "value type '%s' is not an NCName\n", type, NULL); |
3411 | 0 | } |
3412 | 0 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
3413 | 0 | if (library == NULL) |
3414 | 0 | library = |
3415 | 0 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
3416 | |
|
3417 | 0 | def->name = type; |
3418 | 0 | def->ns = library; |
3419 | |
|
3420 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) |
3421 | 0 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
3422 | 0 | if (lib == NULL) { |
3423 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
3424 | 0 | "Use of unregistered type library '%s'\n", library, |
3425 | 0 | NULL); |
3426 | 0 | def->data = NULL; |
3427 | 0 | } else { |
3428 | 0 | def->data = lib; |
3429 | 0 | if (lib->have == NULL) { |
3430 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
3431 | 0 | "Internal error with type library '%s': no 'have'\n", |
3432 | 0 | library, NULL); |
3433 | 0 | } else { |
3434 | 0 | success = lib->have(lib->data, def->name); |
3435 | 0 | if (success != 1) { |
3436 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
3437 | 0 | "Error type '%s' is not exported by type library '%s'\n", |
3438 | 0 | def->name, library); |
3439 | 0 | } |
3440 | 0 | } |
3441 | 0 | } |
3442 | 0 | } |
3443 | 0 | if (node->children == NULL) { |
3444 | 0 | def->value = xmlStrdup(BAD_CAST ""); |
3445 | 0 | } else if (((node->children->type != XML_TEXT_NODE) && |
3446 | 0 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
3447 | 0 | (node->children->next != NULL)) { |
3448 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
3449 | 0 | "Expecting a single text value for <value>content\n", |
3450 | 0 | NULL, NULL); |
3451 | 0 | } else if (def != NULL) { |
3452 | 0 | def->value = xmlNodeGetContent(node); |
3453 | 0 | if (def->value == NULL) { |
3454 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
3455 | 0 | "Element <value> has no content\n", NULL, NULL); |
3456 | 0 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
3457 | 0 | void *val = NULL; |
3458 | |
|
3459 | 0 | success = |
3460 | 0 | lib->check(lib->data, def->name, def->value, &val, node); |
3461 | 0 | if (success != 1) { |
3462 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
3463 | 0 | "Value '%s' is not acceptable for type '%s'\n", |
3464 | 0 | def->value, def->name); |
3465 | 0 | } else { |
3466 | 0 | if (val != NULL) |
3467 | 0 | def->attrs = val; |
3468 | 0 | } |
3469 | 0 | } |
3470 | 0 | } |
3471 | 0 | return (def); |
3472 | 0 | } |
3473 | | |
3474 | | /** |
3475 | | * parse the content of a RelaxNG data node. |
3476 | | * |
3477 | | * @param ctxt a Relax-NG parser context |
3478 | | * @param node the data node. |
3479 | | * @returns the definition pointer or NULL in case of error |
3480 | | */ |
3481 | | static xmlRelaxNGDefinePtr |
3482 | | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
3483 | 0 | { |
3484 | 0 | xmlRelaxNGDefinePtr def = NULL, except; |
3485 | 0 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
3486 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
3487 | 0 | xmlChar *type; |
3488 | 0 | xmlChar *library; |
3489 | 0 | xmlNodePtr content; |
3490 | 0 | int tmp; |
3491 | |
|
3492 | 0 | type = xmlGetProp(node, BAD_CAST "type"); |
3493 | 0 | if (type == NULL) { |
3494 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
3495 | 0 | NULL); |
3496 | 0 | return (NULL); |
3497 | 0 | } |
3498 | 0 | xmlRelaxNGNormExtSpace(type); |
3499 | 0 | if (xmlValidateNCName(type, 0)) { |
3500 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
3501 | 0 | "data type '%s' is not an NCName\n", type, NULL); |
3502 | 0 | } |
3503 | 0 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
3504 | 0 | if (library == NULL) |
3505 | 0 | library = |
3506 | 0 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
3507 | |
|
3508 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
3509 | 0 | if (def == NULL) { |
3510 | 0 | xmlFree(library); |
3511 | 0 | xmlFree(type); |
3512 | 0 | return (NULL); |
3513 | 0 | } |
3514 | 0 | def->type = XML_RELAXNG_DATATYPE; |
3515 | 0 | def->name = type; |
3516 | 0 | def->ns = library; |
3517 | |
|
3518 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) |
3519 | 0 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
3520 | 0 | if (lib == NULL) { |
3521 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
3522 | 0 | "Use of unregistered type library '%s'\n", library, |
3523 | 0 | NULL); |
3524 | 0 | def->data = NULL; |
3525 | 0 | } else { |
3526 | 0 | def->data = lib; |
3527 | 0 | if (lib->have == NULL) { |
3528 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
3529 | 0 | "Internal error with type library '%s': no 'have'\n", |
3530 | 0 | library, NULL); |
3531 | 0 | } else { |
3532 | 0 | tmp = lib->have(lib->data, def->name); |
3533 | 0 | if (tmp != 1) { |
3534 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
3535 | 0 | "Error type '%s' is not exported by type library '%s'\n", |
3536 | 0 | def->name, library); |
3537 | 0 | } else |
3538 | 0 | if ((xmlStrEqual |
3539 | 0 | (library, |
3540 | 0 | BAD_CAST |
3541 | 0 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
3542 | 0 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
3543 | 0 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
3544 | 0 | ctxt->idref = 1; |
3545 | 0 | } |
3546 | 0 | } |
3547 | 0 | } |
3548 | 0 | content = node->children; |
3549 | | |
3550 | | /* |
3551 | | * Handle optional params |
3552 | | */ |
3553 | 0 | while (content != NULL) { |
3554 | 0 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
3555 | 0 | break; |
3556 | 0 | if (xmlStrEqual(library, |
3557 | 0 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
3558 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
3559 | 0 | "Type library '%s' does not allow type parameters\n", |
3560 | 0 | library, NULL); |
3561 | 0 | content = content->next; |
3562 | 0 | while ((content != NULL) && |
3563 | 0 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
3564 | 0 | content = content->next; |
3565 | 0 | } else { |
3566 | 0 | param = xmlRelaxNGNewDefine(ctxt, node); |
3567 | 0 | if (param != NULL) { |
3568 | 0 | param->type = XML_RELAXNG_PARAM; |
3569 | 0 | param->name = xmlGetProp(content, BAD_CAST "name"); |
3570 | 0 | if (param->name == NULL) { |
3571 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
3572 | 0 | "param has no name\n", NULL, NULL); |
3573 | 0 | } |
3574 | 0 | param->value = xmlNodeGetContent(content); |
3575 | 0 | if (lastparam == NULL) { |
3576 | 0 | def->attrs = lastparam = param; |
3577 | 0 | } else { |
3578 | 0 | lastparam->next = param; |
3579 | 0 | lastparam = param; |
3580 | 0 | } |
3581 | 0 | if (lib != NULL) { |
3582 | 0 | } |
3583 | 0 | } |
3584 | 0 | content = content->next; |
3585 | 0 | } |
3586 | 0 | } |
3587 | | /* |
3588 | | * Handle optional except |
3589 | | */ |
3590 | 0 | if ((content != NULL) |
3591 | 0 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
3592 | 0 | xmlNodePtr child; |
3593 | 0 | xmlRelaxNGDefinePtr tmp2, last = NULL; |
3594 | |
|
3595 | 0 | except = xmlRelaxNGNewDefine(ctxt, node); |
3596 | 0 | if (except == NULL) { |
3597 | 0 | return (def); |
3598 | 0 | } |
3599 | 0 | except->type = XML_RELAXNG_EXCEPT; |
3600 | 0 | child = content->children; |
3601 | 0 | def->content = except; |
3602 | 0 | if (child == NULL) { |
3603 | 0 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
3604 | 0 | "except has no content\n", NULL, NULL); |
3605 | 0 | } |
3606 | 0 | while (child != NULL) { |
3607 | 0 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
3608 | 0 | if (tmp2 != NULL) { |
3609 | 0 | if (last == NULL) { |
3610 | 0 | except->content = last = tmp2; |
3611 | 0 | } else { |
3612 | 0 | last->next = tmp2; |
3613 | 0 | last = tmp2; |
3614 | 0 | } |
3615 | 0 | } |
3616 | 0 | child = child->next; |
3617 | 0 | } |
3618 | 0 | content = content->next; |
3619 | 0 | } |
3620 | | /* |
3621 | | * Check there is no unhandled data |
3622 | | */ |
3623 | 0 | if (content != NULL) { |
3624 | 0 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
3625 | 0 | "Element data has unexpected content %s\n", |
3626 | 0 | content->name, NULL); |
3627 | 0 | } |
3628 | |
|
3629 | 0 | return (def); |
3630 | 0 | } |
3631 | | |
3632 | | static const xmlChar *invalidName = BAD_CAST "\1"; |
3633 | | |
3634 | | /** |
3635 | | * Compare the 2 lists of element definitions. The comparison is |
3636 | | * that if both lists do not accept the same QNames, it returns 1 |
3637 | | * If the 2 lists can accept the same QName the comparison returns 0 |
3638 | | * |
3639 | | * @param def1 the first element/attribute defs |
3640 | | * @param def2 the second element/attribute defs |
3641 | | * @returns 1 distinct, 0 if equal |
3642 | | */ |
3643 | | static int |
3644 | | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
3645 | | xmlRelaxNGDefinePtr def2) |
3646 | 0 | { |
3647 | 0 | int ret = 1; |
3648 | 0 | xmlNode node; |
3649 | 0 | xmlNs ns; |
3650 | 0 | xmlRelaxNGValidCtxt ctxt; |
3651 | |
|
3652 | 0 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
3653 | |
|
3654 | 0 | ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR; |
3655 | |
|
3656 | 0 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
3657 | 0 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
3658 | 0 | if (def2->type == XML_RELAXNG_TEXT) |
3659 | 0 | return (1); |
3660 | 0 | if (def1->name != NULL) { |
3661 | 0 | node.name = def1->name; |
3662 | 0 | } else { |
3663 | 0 | node.name = invalidName; |
3664 | 0 | } |
3665 | 0 | if (def1->ns != NULL) { |
3666 | 0 | if (def1->ns[0] == 0) { |
3667 | 0 | node.ns = NULL; |
3668 | 0 | } else { |
3669 | 0 | node.ns = &ns; |
3670 | 0 | ns.href = def1->ns; |
3671 | 0 | } |
3672 | 0 | } else { |
3673 | 0 | node.ns = NULL; |
3674 | 0 | } |
3675 | 0 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
3676 | 0 | if (def1->nameClass != NULL) { |
3677 | 0 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
3678 | 0 | } else { |
3679 | 0 | ret = 0; |
3680 | 0 | } |
3681 | 0 | } else { |
3682 | 0 | ret = 1; |
3683 | 0 | } |
3684 | 0 | } else if (def1->type == XML_RELAXNG_TEXT) { |
3685 | 0 | if (def2->type == XML_RELAXNG_TEXT) |
3686 | 0 | return (0); |
3687 | 0 | return (1); |
3688 | 0 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
3689 | 0 | ret = xmlRelaxNGCompareNameClasses(def1->content, def2); |
3690 | 0 | if (ret == 0) |
3691 | 0 | ret = 1; |
3692 | 0 | else if (ret == 1) |
3693 | 0 | ret = 0; |
3694 | 0 | } else { |
3695 | | /* TODO */ |
3696 | 0 | ret = 0; |
3697 | 0 | } |
3698 | 0 | if (ret == 0) |
3699 | 0 | return (ret); |
3700 | 0 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
3701 | 0 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
3702 | 0 | if (def2->name != NULL) { |
3703 | 0 | node.name = def2->name; |
3704 | 0 | } else { |
3705 | 0 | node.name = invalidName; |
3706 | 0 | } |
3707 | 0 | node.ns = &ns; |
3708 | 0 | if (def2->ns != NULL) { |
3709 | 0 | if (def2->ns[0] == 0) { |
3710 | 0 | node.ns = NULL; |
3711 | 0 | } else { |
3712 | 0 | ns.href = def2->ns; |
3713 | 0 | } |
3714 | 0 | } else { |
3715 | 0 | ns.href = invalidName; |
3716 | 0 | } |
3717 | 0 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
3718 | 0 | if (def2->nameClass != NULL) { |
3719 | 0 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
3720 | 0 | } else { |
3721 | 0 | ret = 0; |
3722 | 0 | } |
3723 | 0 | } else { |
3724 | 0 | ret = 1; |
3725 | 0 | } |
3726 | 0 | } else { |
3727 | | /* TODO */ |
3728 | 0 | ret = 0; |
3729 | 0 | } |
3730 | |
|
3731 | 0 | return (ret); |
3732 | 0 | } |
3733 | | |
3734 | | /** |
3735 | | * Compare the 2 lists of element or attribute definitions. The comparison |
3736 | | * is that if both lists do not accept the same QNames, it returns 1 |
3737 | | * If the 2 lists can accept the same QName the comparison returns 0 |
3738 | | * |
3739 | | * @param ctxt a Relax-NG parser context |
3740 | | * @param def1 the first list of element/attribute defs |
3741 | | * @param def2 the second list of element/attribute defs |
3742 | | * @returns 1 distinct, 0 if equal |
3743 | | */ |
3744 | | static int |
3745 | | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
3746 | | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
3747 | | xmlRelaxNGDefinePtr * def2) |
3748 | 0 | { |
3749 | 0 | xmlRelaxNGDefinePtr *basedef2 = def2; |
3750 | |
|
3751 | 0 | if ((def1 == NULL) || (def2 == NULL)) |
3752 | 0 | return (1); |
3753 | 0 | if ((*def1 == NULL) || (*def2 == NULL)) |
3754 | 0 | return (1); |
3755 | 0 | while (*def1 != NULL) { |
3756 | 0 | while ((*def2) != NULL) { |
3757 | 0 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
3758 | 0 | return (0); |
3759 | 0 | def2++; |
3760 | 0 | } |
3761 | 0 | def2 = basedef2; |
3762 | 0 | def1++; |
3763 | 0 | } |
3764 | 0 | return (1); |
3765 | 0 | } |
3766 | | |
3767 | | /** |
3768 | | * Check if the definition can only generate attributes |
3769 | | * |
3770 | | * @param ctxt a Relax-NG parser context |
3771 | | * @param def the definition definition |
3772 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
3773 | | */ |
3774 | | static int |
3775 | | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
3776 | | xmlRelaxNGDefinePtr def) |
3777 | 0 | { |
3778 | 0 | xmlRelaxNGDefinePtr parent, cur, tmp; |
3779 | | |
3780 | | /* |
3781 | | * Don't run that check in case of error. Infinite recursion |
3782 | | * becomes possible. |
3783 | | */ |
3784 | 0 | if (ctxt->nbErrors != 0) |
3785 | 0 | return (-1); |
3786 | | |
3787 | 0 | parent = NULL; |
3788 | 0 | cur = def; |
3789 | 0 | while (cur != NULL) { |
3790 | 0 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
3791 | 0 | (cur->type == XML_RELAXNG_TEXT) || |
3792 | 0 | (cur->type == XML_RELAXNG_DATATYPE) || |
3793 | 0 | (cur->type == XML_RELAXNG_PARAM) || |
3794 | 0 | (cur->type == XML_RELAXNG_LIST) || |
3795 | 0 | (cur->type == XML_RELAXNG_VALUE) || |
3796 | 0 | (cur->type == XML_RELAXNG_EMPTY)) |
3797 | 0 | return (0); |
3798 | 0 | if ((cur->type == XML_RELAXNG_CHOICE) || |
3799 | 0 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
3800 | 0 | (cur->type == XML_RELAXNG_GROUP) || |
3801 | 0 | (cur->type == XML_RELAXNG_ONEORMORE) || |
3802 | 0 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
3803 | 0 | (cur->type == XML_RELAXNG_OPTIONAL) || |
3804 | 0 | (cur->type == XML_RELAXNG_PARENTREF) || |
3805 | 0 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
3806 | 0 | (cur->type == XML_RELAXNG_REF) || |
3807 | 0 | (cur->type == XML_RELAXNG_DEF)) { |
3808 | 0 | if (cur->content != NULL) { |
3809 | 0 | parent = cur; |
3810 | 0 | cur = cur->content; |
3811 | 0 | tmp = cur; |
3812 | 0 | while (tmp != NULL) { |
3813 | 0 | tmp->parent = parent; |
3814 | 0 | tmp = tmp->next; |
3815 | 0 | } |
3816 | 0 | continue; |
3817 | 0 | } |
3818 | 0 | } |
3819 | 0 | if (cur == def) |
3820 | 0 | break; |
3821 | 0 | if (cur->next != NULL) { |
3822 | 0 | cur = cur->next; |
3823 | 0 | continue; |
3824 | 0 | } |
3825 | 0 | do { |
3826 | 0 | cur = cur->parent; |
3827 | 0 | if (cur == NULL) |
3828 | 0 | break; |
3829 | 0 | if (cur == def) |
3830 | 0 | return (1); |
3831 | 0 | if (cur->next != NULL) { |
3832 | 0 | cur = cur->next; |
3833 | 0 | break; |
3834 | 0 | } |
3835 | 0 | } while (cur != NULL); |
3836 | 0 | } |
3837 | 0 | return (1); |
3838 | 0 | } |
3839 | | |
3840 | | /** |
3841 | | * Compute the list of top elements a definition can generate |
3842 | | * |
3843 | | * @param ctxt a Relax-NG parser context |
3844 | | * @param def the definition definition |
3845 | | * @param eora gather elements (0), attributes (1) or elements and text (2) |
3846 | | * @returns a list of elements or NULL if none was found. |
3847 | | */ |
3848 | | static xmlRelaxNGDefinePtr * |
3849 | | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
3850 | | xmlRelaxNGDefinePtr def, int eora) |
3851 | 0 | { |
3852 | 0 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
3853 | 0 | int len = 0; |
3854 | 0 | int max = 0; |
3855 | | |
3856 | | /* |
3857 | | * Don't run that check in case of error. Infinite recursion |
3858 | | * becomes possible. |
3859 | | */ |
3860 | 0 | if (ctxt->nbErrors != 0) |
3861 | 0 | return (NULL); |
3862 | | |
3863 | 0 | parent = NULL; |
3864 | 0 | cur = def; |
3865 | 0 | while (cur != NULL) { |
3866 | 0 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
3867 | 0 | (cur->type == XML_RELAXNG_TEXT))) || |
3868 | 0 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE)) || |
3869 | 0 | ((eora == 2) && ((cur->type == XML_RELAXNG_DATATYPE) || |
3870 | 0 | (cur->type == XML_RELAXNG_ELEMENT) || |
3871 | 0 | (cur->type == XML_RELAXNG_LIST) || |
3872 | 0 | (cur->type == XML_RELAXNG_TEXT) || |
3873 | 0 | (cur->type == XML_RELAXNG_VALUE)))) { |
3874 | 0 | if (ret == NULL) { |
3875 | 0 | max = 10; |
3876 | 0 | ret = (xmlRelaxNGDefinePtr *) |
3877 | 0 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
3878 | 0 | if (ret == NULL) { |
3879 | 0 | xmlRngPErrMemory(ctxt); |
3880 | 0 | return (NULL); |
3881 | 0 | } |
3882 | 0 | } else if (max <= len) { |
3883 | 0 | xmlRelaxNGDefinePtr *temp; |
3884 | |
|
3885 | 0 | max *= 2; |
3886 | 0 | temp = xmlRealloc(ret, |
3887 | 0 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
3888 | 0 | if (temp == NULL) { |
3889 | 0 | xmlRngPErrMemory(ctxt); |
3890 | 0 | xmlFree(ret); |
3891 | 0 | return (NULL); |
3892 | 0 | } |
3893 | 0 | ret = temp; |
3894 | 0 | } |
3895 | 0 | ret[len++] = cur; |
3896 | 0 | ret[len] = NULL; |
3897 | 0 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
3898 | 0 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
3899 | 0 | (cur->type == XML_RELAXNG_GROUP) || |
3900 | 0 | (cur->type == XML_RELAXNG_ONEORMORE) || |
3901 | 0 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
3902 | 0 | (cur->type == XML_RELAXNG_OPTIONAL) || |
3903 | 0 | (cur->type == XML_RELAXNG_PARENTREF) || |
3904 | 0 | (cur->type == XML_RELAXNG_REF) || |
3905 | 0 | (cur->type == XML_RELAXNG_DEF) || |
3906 | 0 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
3907 | | /* |
3908 | | * Don't go within elements or attributes or string values. |
3909 | | * Just gather the element top list |
3910 | | */ |
3911 | 0 | if (cur->content != NULL) { |
3912 | 0 | parent = cur; |
3913 | 0 | cur = cur->content; |
3914 | 0 | tmp = cur; |
3915 | 0 | while (tmp != NULL) { |
3916 | 0 | tmp->parent = parent; |
3917 | 0 | tmp = tmp->next; |
3918 | 0 | } |
3919 | 0 | continue; |
3920 | 0 | } |
3921 | 0 | } |
3922 | 0 | if (cur == def) |
3923 | 0 | break; |
3924 | 0 | if (cur->next != NULL) { |
3925 | 0 | cur = cur->next; |
3926 | 0 | continue; |
3927 | 0 | } |
3928 | 0 | do { |
3929 | 0 | cur = cur->parent; |
3930 | 0 | if (cur == NULL) |
3931 | 0 | break; |
3932 | 0 | if (cur == def) |
3933 | 0 | return (ret); |
3934 | 0 | if (cur->next != NULL) { |
3935 | 0 | cur = cur->next; |
3936 | 0 | break; |
3937 | 0 | } |
3938 | 0 | } while (cur != NULL); |
3939 | 0 | } |
3940 | 0 | return (ret); |
3941 | 0 | } |
3942 | | |
3943 | | /** |
3944 | | * Also used to find indeterministic pattern in choice |
3945 | | * |
3946 | | * @param ctxt a Relax-NG parser context |
3947 | | * @param def the choice definition |
3948 | | */ |
3949 | | static void |
3950 | | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
3951 | | xmlRelaxNGDefinePtr def) |
3952 | 0 | { |
3953 | 0 | xmlRelaxNGDefinePtr **list; |
3954 | 0 | xmlRelaxNGDefinePtr cur; |
3955 | 0 | int nbchild = 0, i, j, ret; |
3956 | 0 | int is_nullable = 0; |
3957 | 0 | int is_indeterminist = 0; |
3958 | 0 | xmlHashTablePtr triage = NULL; |
3959 | 0 | int is_triable = 1; |
3960 | |
|
3961 | 0 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
3962 | 0 | return; |
3963 | | |
3964 | 0 | if (def->dflags & IS_PROCESSED) |
3965 | 0 | return; |
3966 | | |
3967 | | /* |
3968 | | * Don't run that check in case of error. Infinite recursion |
3969 | | * becomes possible. |
3970 | | */ |
3971 | 0 | if (ctxt->nbErrors != 0) |
3972 | 0 | return; |
3973 | | |
3974 | 0 | is_nullable = xmlRelaxNGIsNullable(def); |
3975 | |
|
3976 | 0 | cur = def->content; |
3977 | 0 | while (cur != NULL) { |
3978 | 0 | nbchild++; |
3979 | 0 | cur = cur->next; |
3980 | 0 | } |
3981 | |
|
3982 | 0 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
3983 | 0 | sizeof(xmlRelaxNGDefinePtr |
3984 | 0 | *)); |
3985 | 0 | if (list == NULL) { |
3986 | 0 | xmlRngPErrMemory(ctxt); |
3987 | 0 | return; |
3988 | 0 | } |
3989 | 0 | i = 0; |
3990 | | /* |
3991 | | * a bit strong but safe |
3992 | | */ |
3993 | 0 | if (is_nullable == 0) { |
3994 | 0 | triage = xmlHashCreate(10); |
3995 | 0 | } else { |
3996 | 0 | is_triable = 0; |
3997 | 0 | } |
3998 | 0 | cur = def->content; |
3999 | 0 | while (cur != NULL) { |
4000 | 0 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
4001 | 0 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
4002 | 0 | is_triable = 0; |
4003 | 0 | } else if (is_triable == 1) { |
4004 | 0 | xmlRelaxNGDefinePtr *tmp; |
4005 | 0 | int res; |
4006 | |
|
4007 | 0 | tmp = list[i]; |
4008 | 0 | while ((*tmp != NULL) && (is_triable == 1)) { |
4009 | 0 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
4010 | 0 | res = xmlHashAddEntry2(triage, |
4011 | 0 | BAD_CAST "#text", NULL, |
4012 | 0 | (void *) cur); |
4013 | 0 | if (res != 0) |
4014 | 0 | is_triable = -1; |
4015 | 0 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
4016 | 0 | ((*tmp)->name != NULL)) { |
4017 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4018 | 0 | res = xmlHashAddEntry2(triage, |
4019 | 0 | (*tmp)->name, NULL, |
4020 | 0 | (void *) cur); |
4021 | 0 | else |
4022 | 0 | res = xmlHashAddEntry2(triage, |
4023 | 0 | (*tmp)->name, (*tmp)->ns, |
4024 | 0 | (void *) cur); |
4025 | 0 | if (res != 0) |
4026 | 0 | is_triable = -1; |
4027 | 0 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
4028 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4029 | 0 | res = xmlHashAddEntry2(triage, |
4030 | 0 | BAD_CAST "#any", NULL, |
4031 | 0 | (void *) cur); |
4032 | 0 | else |
4033 | 0 | res = xmlHashAddEntry2(triage, |
4034 | 0 | BAD_CAST "#any", (*tmp)->ns, |
4035 | 0 | (void *) cur); |
4036 | 0 | if (res != 0) |
4037 | 0 | is_triable = -1; |
4038 | 0 | } else { |
4039 | 0 | is_triable = -1; |
4040 | 0 | } |
4041 | 0 | tmp++; |
4042 | 0 | } |
4043 | 0 | } |
4044 | 0 | i++; |
4045 | 0 | cur = cur->next; |
4046 | 0 | } |
4047 | |
|
4048 | 0 | for (i = 0; i < nbchild; i++) { |
4049 | 0 | if (list[i] == NULL) |
4050 | 0 | continue; |
4051 | 0 | for (j = 0; j < i; j++) { |
4052 | 0 | if (list[j] == NULL) |
4053 | 0 | continue; |
4054 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
4055 | 0 | if (ret == 0) { |
4056 | 0 | is_indeterminist = 1; |
4057 | 0 | } |
4058 | 0 | } |
4059 | 0 | } |
4060 | 0 | for (i = 0; i < nbchild; i++) { |
4061 | 0 | if (list[i] != NULL) |
4062 | 0 | xmlFree(list[i]); |
4063 | 0 | } |
4064 | |
|
4065 | 0 | xmlFree(list); |
4066 | 0 | if (is_indeterminist) { |
4067 | 0 | def->dflags |= IS_INDETERMINIST; |
4068 | 0 | } |
4069 | 0 | if (is_triable == 1) { |
4070 | 0 | def->dflags |= IS_TRIABLE; |
4071 | 0 | def->data = triage; |
4072 | 0 | } else if (triage != NULL) { |
4073 | 0 | xmlHashFree(triage, NULL); |
4074 | 0 | } |
4075 | 0 | def->dflags |= IS_PROCESSED; |
4076 | 0 | } |
4077 | | |
4078 | | /** |
4079 | | * Detects violations of rule 7.3 |
4080 | | * |
4081 | | * @param ctxt a Relax-NG parser context |
4082 | | * @param def the group definition |
4083 | | */ |
4084 | | static void |
4085 | | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
4086 | | xmlRelaxNGDefinePtr def) |
4087 | 0 | { |
4088 | 0 | xmlRelaxNGDefinePtr **list; |
4089 | 0 | xmlRelaxNGDefinePtr cur; |
4090 | 0 | int nbchild = 0, i, j, ret; |
4091 | |
|
4092 | 0 | if ((def == NULL) || |
4093 | 0 | ((def->type != XML_RELAXNG_GROUP) && |
4094 | 0 | (def->type != XML_RELAXNG_ELEMENT))) |
4095 | 0 | return; |
4096 | | |
4097 | 0 | if (def->dflags & IS_PROCESSED) |
4098 | 0 | return; |
4099 | | |
4100 | | /* |
4101 | | * Don't run that check in case of error. Infinite recursion |
4102 | | * becomes possible. |
4103 | | */ |
4104 | 0 | if (ctxt->nbErrors != 0) |
4105 | 0 | return; |
4106 | | |
4107 | 0 | cur = def->attrs; |
4108 | 0 | while (cur != NULL) { |
4109 | 0 | nbchild++; |
4110 | 0 | cur = cur->next; |
4111 | 0 | } |
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); |
4123 | 0 | return; |
4124 | 0 | } |
4125 | 0 | i = 0; |
4126 | 0 | cur = def->attrs; |
4127 | 0 | while (cur != NULL) { |
4128 | 0 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
4129 | 0 | i++; |
4130 | 0 | cur = cur->next; |
4131 | 0 | } |
4132 | 0 | cur = def->content; |
4133 | 0 | while (cur != NULL) { |
4134 | 0 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
4135 | 0 | i++; |
4136 | 0 | cur = cur->next; |
4137 | 0 | } |
4138 | |
|
4139 | 0 | for (i = 0; i < nbchild; i++) { |
4140 | 0 | if (list[i] == NULL) |
4141 | 0 | continue; |
4142 | 0 | for (j = 0; j < i; j++) { |
4143 | 0 | if (list[j] == NULL) |
4144 | 0 | continue; |
4145 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
4146 | 0 | if (ret == 0) { |
4147 | 0 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
4148 | 0 | "Attributes conflicts in group\n", NULL, NULL); |
4149 | 0 | } |
4150 | 0 | } |
4151 | 0 | } |
4152 | 0 | for (i = 0; i < nbchild; i++) { |
4153 | 0 | if (list[i] != NULL) |
4154 | 0 | xmlFree(list[i]); |
4155 | 0 | } |
4156 | |
|
4157 | 0 | xmlFree(list); |
4158 | 0 | def->dflags |= IS_PROCESSED; |
4159 | 0 | } |
4160 | | |
4161 | | /** |
4162 | | * A lot of work for preprocessing interleave definitions |
4163 | | * is potentially needed to get a decent execution speed at runtime |
4164 | | * - trying to get a total order on the element nodes generated |
4165 | | * by the interleaves, order the list of interleave definitions |
4166 | | * following that order. |
4167 | | * - if `<text/>` is used to handle mixed content, it is better to |
4168 | | * flag this in the define and simplify the runtime checking |
4169 | | * algorithm |
4170 | | * |
4171 | | * @param payload the interleave definition |
4172 | | * @param data a Relax-NG parser context |
4173 | | * @param name the definition name |
4174 | | */ |
4175 | | static void |
4176 | | xmlRelaxNGComputeInterleaves(void *payload, void *data, |
4177 | | const xmlChar * name ATTRIBUTE_UNUSED) |
4178 | 0 | { |
4179 | 0 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
4180 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
4181 | 0 | xmlRelaxNGDefinePtr cur, *tmp; |
4182 | |
|
4183 | 0 | xmlRelaxNGPartitionPtr partitions = NULL; |
4184 | 0 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
4185 | 0 | xmlRelaxNGInterleaveGroupPtr group; |
4186 | 0 | int i, j, ret, res; |
4187 | 0 | int nbgroups = 0; |
4188 | 0 | int nbchild = 0; |
4189 | 0 | int is_mixed = 0; |
4190 | 0 | int is_determinist = 1; |
4191 | | |
4192 | | /* |
4193 | | * Don't run that check in case of error. Infinite recursion |
4194 | | * becomes possible. |
4195 | | */ |
4196 | 0 | if (ctxt->nbErrors != 0) |
4197 | 0 | return; |
4198 | | |
4199 | 0 | cur = def->content; |
4200 | 0 | while (cur != NULL) { |
4201 | 0 | nbchild++; |
4202 | 0 | cur = cur->next; |
4203 | 0 | } |
4204 | |
|
4205 | 0 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
4206 | 0 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
4207 | 0 | if (groups == NULL) |
4208 | 0 | goto error; |
4209 | 0 | cur = def->content; |
4210 | 0 | while (cur != NULL) { |
4211 | 0 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
4212 | 0 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
4213 | 0 | if (groups[nbgroups] == NULL) |
4214 | 0 | goto error; |
4215 | 0 | if (cur->type == XML_RELAXNG_TEXT) |
4216 | 0 | is_mixed++; |
4217 | 0 | groups[nbgroups]->rule = cur; |
4218 | 0 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 2); |
4219 | 0 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
4220 | 0 | nbgroups++; |
4221 | 0 | cur = cur->next; |
4222 | 0 | } |
4223 | | |
4224 | | /* |
4225 | | * Let's check that all rules makes a partitions according to 7.4 |
4226 | | */ |
4227 | 0 | partitions = (xmlRelaxNGPartitionPtr) |
4228 | 0 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
4229 | 0 | if (partitions == NULL) |
4230 | 0 | goto error; |
4231 | 0 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
4232 | 0 | partitions->nbgroups = nbgroups; |
4233 | 0 | partitions->triage = xmlHashCreate(nbgroups); |
4234 | 0 | for (i = 0; i < nbgroups; i++) { |
4235 | 0 | group = groups[i]; |
4236 | 0 | for (j = i + 1; j < nbgroups; j++) { |
4237 | 0 | if (groups[j] == NULL) |
4238 | 0 | continue; |
4239 | | |
4240 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
4241 | 0 | groups[j]->defs); |
4242 | 0 | if (ret == 0) { |
4243 | 0 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
4244 | 0 | "Element or text conflicts in interleave\n", |
4245 | 0 | NULL, NULL); |
4246 | 0 | } |
4247 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
4248 | 0 | groups[j]->attrs); |
4249 | 0 | if (ret == 0) { |
4250 | 0 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
4251 | 0 | "Attributes conflicts in interleave\n", NULL, |
4252 | 0 | NULL); |
4253 | 0 | } |
4254 | 0 | } |
4255 | 0 | tmp = group->defs; |
4256 | 0 | if ((tmp != NULL) && (*tmp != NULL)) { |
4257 | 0 | while (*tmp != NULL) { |
4258 | 0 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
4259 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4260 | 0 | BAD_CAST "#text", NULL, |
4261 | 0 | XML_INT_TO_PTR(i + 1)); |
4262 | 0 | if (res != 0) |
4263 | 0 | is_determinist = -1; |
4264 | 0 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
4265 | 0 | ((*tmp)->name != NULL)) { |
4266 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4267 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4268 | 0 | (*tmp)->name, NULL, |
4269 | 0 | XML_INT_TO_PTR(i + 1)); |
4270 | 0 | else |
4271 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4272 | 0 | (*tmp)->name, (*tmp)->ns, |
4273 | 0 | XML_INT_TO_PTR(i + 1)); |
4274 | 0 | if (res != 0) |
4275 | 0 | is_determinist = -1; |
4276 | 0 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
4277 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4278 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4279 | 0 | BAD_CAST "#any", NULL, |
4280 | 0 | XML_INT_TO_PTR(i + 1)); |
4281 | 0 | else |
4282 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4283 | 0 | BAD_CAST "#any", (*tmp)->ns, |
4284 | 0 | XML_INT_TO_PTR(i + 1)); |
4285 | 0 | if ((*tmp)->nameClass != NULL) |
4286 | 0 | is_determinist = 2; |
4287 | 0 | if (res != 0) |
4288 | 0 | is_determinist = -1; |
4289 | 0 | } else { |
4290 | 0 | is_determinist = -1; |
4291 | 0 | } |
4292 | 0 | tmp++; |
4293 | 0 | } |
4294 | 0 | } else { |
4295 | 0 | is_determinist = 0; |
4296 | 0 | } |
4297 | 0 | } |
4298 | 0 | partitions->groups = groups; |
4299 | | |
4300 | | /* |
4301 | | * and save the partition list back in the def |
4302 | | */ |
4303 | 0 | def->data = partitions; |
4304 | 0 | if (is_mixed != 0) |
4305 | 0 | def->dflags |= IS_MIXED; |
4306 | 0 | if (is_determinist == 1) |
4307 | 0 | partitions->flags = IS_DETERMINIST; |
4308 | 0 | if (is_determinist == 2) |
4309 | 0 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
4310 | 0 | return; |
4311 | | |
4312 | 0 | error: |
4313 | 0 | xmlRngPErrMemory(ctxt); |
4314 | 0 | if (groups != NULL) { |
4315 | 0 | for (i = 0; i < nbgroups; i++) |
4316 | 0 | if (groups[i] != NULL) { |
4317 | 0 | if (groups[i]->defs != NULL) |
4318 | 0 | xmlFree(groups[i]->defs); |
4319 | 0 | xmlFree(groups[i]); |
4320 | 0 | } |
4321 | 0 | xmlFree(groups); |
4322 | 0 | } |
4323 | 0 | xmlRelaxNGFreePartition(partitions); |
4324 | 0 | } |
4325 | | |
4326 | | /** |
4327 | | * parse the content of a RelaxNG interleave node. |
4328 | | * |
4329 | | * @param ctxt a Relax-NG parser context |
4330 | | * @param node the data node. |
4331 | | * @returns the definition pointer or NULL in case of error |
4332 | | */ |
4333 | | static xmlRelaxNGDefinePtr |
4334 | | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4335 | 0 | { |
4336 | 0 | xmlRelaxNGDefinePtr def = NULL; |
4337 | 0 | xmlRelaxNGDefinePtr last = NULL, cur; |
4338 | 0 | xmlNodePtr child; |
4339 | |
|
4340 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4341 | 0 | if (def == NULL) { |
4342 | 0 | return (NULL); |
4343 | 0 | } |
4344 | 0 | def->type = XML_RELAXNG_INTERLEAVE; |
4345 | |
|
4346 | 0 | if (ctxt->interleaves == NULL) |
4347 | 0 | ctxt->interleaves = xmlHashCreate(10); |
4348 | 0 | if (ctxt->interleaves == NULL) { |
4349 | 0 | xmlRngPErrMemory(ctxt); |
4350 | 0 | } else { |
4351 | 0 | char name[32]; |
4352 | |
|
4353 | 0 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
4354 | 0 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
4355 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
4356 | 0 | "Failed to add %s to hash table\n", |
4357 | 0 | (const xmlChar *) name, NULL); |
4358 | 0 | } |
4359 | 0 | } |
4360 | 0 | child = node->children; |
4361 | 0 | if (child == NULL) { |
4362 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
4363 | 0 | "Element interleave is empty\n", NULL, NULL); |
4364 | 0 | } |
4365 | 0 | while (child != NULL) { |
4366 | 0 | if (IS_RELAXNG(child, "element")) { |
4367 | 0 | cur = xmlRelaxNGParseElement(ctxt, child); |
4368 | 0 | } else { |
4369 | 0 | cur = xmlRelaxNGParsePattern(ctxt, child); |
4370 | 0 | } |
4371 | 0 | if (cur != NULL) { |
4372 | 0 | cur->parent = def; |
4373 | 0 | if (last == NULL) { |
4374 | 0 | def->content = last = cur; |
4375 | 0 | } else { |
4376 | 0 | last->next = cur; |
4377 | 0 | last = cur; |
4378 | 0 | } |
4379 | 0 | } |
4380 | 0 | child = child->next; |
4381 | 0 | } |
4382 | |
|
4383 | 0 | return (def); |
4384 | 0 | } |
4385 | | |
4386 | | /** |
4387 | | * Integrate the content of an include node in the current grammar |
4388 | | * |
4389 | | * @param ctxt a Relax-NG parser context |
4390 | | * @param node the include node |
4391 | | * @returns 0 in case of success or -1 in case of error |
4392 | | */ |
4393 | | static int |
4394 | | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4395 | 0 | { |
4396 | 0 | xmlRelaxNGIncludePtr incl; |
4397 | 0 | xmlNodePtr root; |
4398 | 0 | int ret = 0, tmp; |
4399 | |
|
4400 | 0 | incl = node->psvi; |
4401 | 0 | if (incl == NULL) { |
4402 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
4403 | 0 | "Include node has no data\n", NULL, NULL); |
4404 | 0 | return (-1); |
4405 | 0 | } |
4406 | 0 | root = xmlDocGetRootElement(incl->doc); |
4407 | 0 | if (root == NULL) { |
4408 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
4409 | 0 | NULL, NULL); |
4410 | 0 | return (-1); |
4411 | 0 | } |
4412 | 0 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
4413 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
4414 | 0 | "Include document root is not a grammar\n", NULL, NULL); |
4415 | 0 | return (-1); |
4416 | 0 | } |
4417 | | |
4418 | | /* |
4419 | | * Merge the definition from both the include and the internal list |
4420 | | */ |
4421 | 0 | if (root->children != NULL) { |
4422 | 0 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
4423 | 0 | if (tmp != 0) |
4424 | 0 | ret = -1; |
4425 | 0 | } |
4426 | 0 | if (node->children != NULL) { |
4427 | 0 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
4428 | 0 | if (tmp != 0) |
4429 | 0 | ret = -1; |
4430 | 0 | } |
4431 | 0 | return (ret); |
4432 | 0 | } |
4433 | | |
4434 | | /** |
4435 | | * parse the content of a RelaxNG define element node. |
4436 | | * |
4437 | | * @param ctxt a Relax-NG parser context |
4438 | | * @param node the define node |
4439 | | * @returns 0 in case of success or -1 in case of error |
4440 | | */ |
4441 | | static int |
4442 | | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4443 | 0 | { |
4444 | 0 | xmlChar *name; |
4445 | 0 | int ret = 0, tmp; |
4446 | 0 | xmlRelaxNGDefinePtr def; |
4447 | 0 | const xmlChar *olddefine; |
4448 | |
|
4449 | 0 | name = xmlGetProp(node, BAD_CAST "name"); |
4450 | 0 | if (name == NULL) { |
4451 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
4452 | 0 | "define has no name\n", NULL, NULL); |
4453 | 0 | } else { |
4454 | 0 | xmlRelaxNGNormExtSpace(name); |
4455 | 0 | if (xmlValidateNCName(name, 0)) { |
4456 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
4457 | 0 | "define name '%s' is not an NCName\n", name, NULL); |
4458 | 0 | } |
4459 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4460 | 0 | if (def == NULL) { |
4461 | 0 | xmlFree(name); |
4462 | 0 | return (-1); |
4463 | 0 | } |
4464 | 0 | def->type = XML_RELAXNG_DEF; |
4465 | 0 | def->name = name; |
4466 | 0 | if (node->children == NULL) { |
4467 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
4468 | 0 | "define has no children\n", NULL, NULL); |
4469 | 0 | } else { |
4470 | 0 | olddefine = ctxt->define; |
4471 | 0 | ctxt->define = name; |
4472 | 0 | def->content = |
4473 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4474 | 0 | ctxt->define = olddefine; |
4475 | 0 | } |
4476 | 0 | if (ctxt->grammar->defs == NULL) |
4477 | 0 | ctxt->grammar->defs = xmlHashCreate(10); |
4478 | 0 | if (ctxt->grammar->defs == NULL) { |
4479 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
4480 | 0 | "Could not create definition hash\n", NULL, NULL); |
4481 | 0 | ret = -1; |
4482 | 0 | } else { |
4483 | 0 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
4484 | 0 | if (tmp < 0) { |
4485 | 0 | xmlRelaxNGDefinePtr prev; |
4486 | |
|
4487 | 0 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
4488 | 0 | if (prev == NULL) { |
4489 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
4490 | 0 | "Internal error on define aggregation of %s\n", |
4491 | 0 | name, NULL); |
4492 | 0 | ret = -1; |
4493 | 0 | } else { |
4494 | 0 | while (prev->nextHash != NULL) |
4495 | 0 | prev = prev->nextHash; |
4496 | 0 | prev->nextHash = def; |
4497 | 0 | } |
4498 | 0 | } |
4499 | 0 | } |
4500 | 0 | } |
4501 | 0 | return (ret); |
4502 | 0 | } |
4503 | | |
4504 | | /** |
4505 | | * Import import one references into the current grammar |
4506 | | * |
4507 | | * @param payload the parser context |
4508 | | * @param data the current grammar |
4509 | | * @param name the reference name |
4510 | | */ |
4511 | | static void |
4512 | 0 | xmlRelaxNGParseImportRef(void *payload, void *data, const xmlChar *name) { |
4513 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
4514 | 0 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
4515 | 0 | int tmp; |
4516 | |
|
4517 | 0 | def->dflags |= IS_EXTERNAL_REF; |
4518 | |
|
4519 | 0 | tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def); |
4520 | 0 | if (tmp < 0) { |
4521 | 0 | xmlRelaxNGDefinePtr prev; |
4522 | |
|
4523 | 0 | prev = (xmlRelaxNGDefinePtr) |
4524 | 0 | xmlHashLookup(ctxt->grammar->refs, def->name); |
4525 | 0 | if (prev == NULL) { |
4526 | 0 | if (def->name != NULL) { |
4527 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
4528 | 0 | "Error refs definitions '%s'\n", |
4529 | 0 | def->name, NULL); |
4530 | 0 | } else { |
4531 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
4532 | 0 | "Error refs definitions\n", |
4533 | 0 | NULL, NULL); |
4534 | 0 | } |
4535 | 0 | } else { |
4536 | 0 | def->nextHash = prev->nextHash; |
4537 | 0 | prev->nextHash = def; |
4538 | 0 | } |
4539 | 0 | } |
4540 | 0 | } |
4541 | | |
4542 | | /** |
4543 | | * Import references from the subgrammar into the current grammar |
4544 | | * |
4545 | | * @param ctxt the parser context |
4546 | | * @param grammar the sub grammar |
4547 | | * @returns 0 in case of success, -1 in case of failure |
4548 | | */ |
4549 | | static int |
4550 | | xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt, |
4551 | 0 | xmlRelaxNGGrammarPtr grammar) { |
4552 | 0 | if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL)) |
4553 | 0 | return(-1); |
4554 | 0 | if (grammar->refs == NULL) |
4555 | 0 | return(0); |
4556 | 0 | if (ctxt->grammar->refs == NULL) |
4557 | 0 | ctxt->grammar->refs = xmlHashCreate(10); |
4558 | 0 | if (ctxt->grammar->refs == NULL) { |
4559 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
4560 | 0 | "Could not create references hash\n", NULL, NULL); |
4561 | 0 | return(-1); |
4562 | 0 | } |
4563 | 0 | xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt); |
4564 | 0 | return(0); |
4565 | 0 | } |
4566 | | |
4567 | | /** |
4568 | | * Process and compile an externalRef node |
4569 | | * |
4570 | | * @param ctxt the parser context |
4571 | | * @param node the externalRef node |
4572 | | * @returns the xmlRelaxNGDefine or NULL in case of error |
4573 | | */ |
4574 | | static xmlRelaxNGDefinePtr |
4575 | | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4576 | 0 | { |
4577 | 0 | xmlRelaxNGDocumentPtr docu; |
4578 | 0 | xmlNodePtr root, tmp; |
4579 | 0 | xmlChar *ns; |
4580 | 0 | int newNs = 0, oldflags; |
4581 | 0 | xmlRelaxNGDefinePtr def; |
4582 | |
|
4583 | 0 | docu = node->psvi; |
4584 | 0 | if (docu != NULL) { |
4585 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4586 | 0 | if (def == NULL) |
4587 | 0 | return (NULL); |
4588 | 0 | def->type = XML_RELAXNG_EXTERNALREF; |
4589 | |
|
4590 | 0 | if (docu->content == NULL) { |
4591 | | /* |
4592 | | * Then do the parsing for good |
4593 | | */ |
4594 | 0 | root = xmlDocGetRootElement(docu->doc); |
4595 | 0 | if (root == NULL) { |
4596 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
4597 | 0 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
4598 | 0 | NULL); |
4599 | 0 | return (NULL); |
4600 | 0 | } |
4601 | | /* |
4602 | | * ns transmission rules |
4603 | | */ |
4604 | 0 | ns = xmlGetProp(root, BAD_CAST "ns"); |
4605 | 0 | if (ns == NULL) { |
4606 | 0 | tmp = node; |
4607 | 0 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
4608 | 0 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
4609 | 0 | if (ns != NULL) { |
4610 | 0 | break; |
4611 | 0 | } |
4612 | 0 | tmp = tmp->parent; |
4613 | 0 | } |
4614 | 0 | if (ns != NULL) { |
4615 | 0 | xmlSetProp(root, BAD_CAST "ns", ns); |
4616 | 0 | newNs = 1; |
4617 | 0 | xmlFree(ns); |
4618 | 0 | } |
4619 | 0 | } else { |
4620 | 0 | xmlFree(ns); |
4621 | 0 | } |
4622 | | |
4623 | | /* |
4624 | | * Parsing to get a precompiled schemas. |
4625 | | */ |
4626 | 0 | oldflags = ctxt->flags; |
4627 | 0 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
4628 | 0 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
4629 | 0 | ctxt->flags = oldflags; |
4630 | 0 | if ((docu->schema != NULL) && |
4631 | 0 | (docu->schema->topgrammar != NULL)) { |
4632 | 0 | docu->content = docu->schema->topgrammar->start; |
4633 | 0 | if (docu->schema->topgrammar->refs) |
4634 | 0 | xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar); |
4635 | 0 | } |
4636 | | |
4637 | | /* |
4638 | | * the externalRef may be reused in a different ns context |
4639 | | */ |
4640 | 0 | if (newNs == 1) { |
4641 | 0 | xmlUnsetProp(root, BAD_CAST "ns"); |
4642 | 0 | } |
4643 | 0 | } |
4644 | 0 | def->content = docu->content; |
4645 | 0 | } else { |
4646 | 0 | def = NULL; |
4647 | 0 | } |
4648 | 0 | return (def); |
4649 | 0 | } |
4650 | | |
4651 | | /** |
4652 | | * parse the content of a RelaxNG pattern node. |
4653 | | * |
4654 | | * @param ctxt a Relax-NG parser context |
4655 | | * @param node the pattern node. |
4656 | | * @returns the definition pointer or NULL in case of error or if no |
4657 | | * pattern is generated. |
4658 | | */ |
4659 | | static xmlRelaxNGDefinePtr |
4660 | | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4661 | 0 | { |
4662 | 0 | xmlRelaxNGDefinePtr def = NULL; |
4663 | |
|
4664 | 0 | if (node == NULL) { |
4665 | 0 | return (NULL); |
4666 | 0 | } |
4667 | 0 | if (IS_RELAXNG(node, "element")) { |
4668 | 0 | def = xmlRelaxNGParseElement(ctxt, node); |
4669 | 0 | } else if (IS_RELAXNG(node, "attribute")) { |
4670 | 0 | def = xmlRelaxNGParseAttribute(ctxt, node); |
4671 | 0 | } else if (IS_RELAXNG(node, "empty")) { |
4672 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4673 | 0 | if (def == NULL) |
4674 | 0 | return (NULL); |
4675 | 0 | def->type = XML_RELAXNG_EMPTY; |
4676 | 0 | if (node->children != NULL) { |
4677 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
4678 | 0 | "empty: had a child node\n", NULL, NULL); |
4679 | 0 | } |
4680 | 0 | } else if (IS_RELAXNG(node, "text")) { |
4681 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4682 | 0 | if (def == NULL) |
4683 | 0 | return (NULL); |
4684 | 0 | def->type = XML_RELAXNG_TEXT; |
4685 | 0 | if (node->children != NULL) { |
4686 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
4687 | 0 | "text: had a child node\n", NULL, NULL); |
4688 | 0 | } |
4689 | 0 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
4690 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4691 | 0 | if (def == NULL) |
4692 | 0 | return (NULL); |
4693 | 0 | def->type = XML_RELAXNG_ZEROORMORE; |
4694 | 0 | if (node->children == NULL) { |
4695 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4696 | 0 | "Element %s is empty\n", node->name, NULL); |
4697 | 0 | } else { |
4698 | 0 | def->content = |
4699 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
4700 | 0 | } |
4701 | 0 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
4702 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4703 | 0 | if (def == NULL) |
4704 | 0 | return (NULL); |
4705 | 0 | def->type = XML_RELAXNG_ONEORMORE; |
4706 | 0 | if (node->children == NULL) { |
4707 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4708 | 0 | "Element %s is empty\n", node->name, NULL); |
4709 | 0 | } else { |
4710 | 0 | def->content = |
4711 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
4712 | 0 | } |
4713 | 0 | } else if (IS_RELAXNG(node, "optional")) { |
4714 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4715 | 0 | if (def == NULL) |
4716 | 0 | return (NULL); |
4717 | 0 | def->type = XML_RELAXNG_OPTIONAL; |
4718 | 0 | if (node->children == NULL) { |
4719 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4720 | 0 | "Element %s is empty\n", node->name, NULL); |
4721 | 0 | } else { |
4722 | 0 | def->content = |
4723 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
4724 | 0 | } |
4725 | 0 | } else if (IS_RELAXNG(node, "choice")) { |
4726 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4727 | 0 | if (def == NULL) |
4728 | 0 | return (NULL); |
4729 | 0 | def->type = XML_RELAXNG_CHOICE; |
4730 | 0 | if (node->children == NULL) { |
4731 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4732 | 0 | "Element %s is empty\n", node->name, NULL); |
4733 | 0 | } else { |
4734 | 0 | ctxt->def = def; |
4735 | 0 | def->content = |
4736 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4737 | 0 | } |
4738 | 0 | } else if (IS_RELAXNG(node, "group")) { |
4739 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4740 | 0 | if (def == NULL) |
4741 | 0 | return (NULL); |
4742 | 0 | def->type = XML_RELAXNG_GROUP; |
4743 | 0 | if (node->children == NULL) { |
4744 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4745 | 0 | "Element %s is empty\n", node->name, NULL); |
4746 | 0 | } else { |
4747 | 0 | def->content = |
4748 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4749 | 0 | } |
4750 | 0 | } else if (IS_RELAXNG(node, "ref")) { |
4751 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4752 | 0 | if (def == NULL) |
4753 | 0 | return (NULL); |
4754 | 0 | def->type = XML_RELAXNG_REF; |
4755 | 0 | def->name = xmlGetProp(node, BAD_CAST "name"); |
4756 | 0 | if (def->name == NULL) { |
4757 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
4758 | 0 | NULL, NULL); |
4759 | 0 | } else { |
4760 | 0 | xmlRelaxNGNormExtSpace(def->name); |
4761 | 0 | if (xmlValidateNCName(def->name, 0)) { |
4762 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
4763 | 0 | "ref name '%s' is not an NCName\n", def->name, |
4764 | 0 | NULL); |
4765 | 0 | } |
4766 | 0 | } |
4767 | 0 | if (node->children != NULL) { |
4768 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
4769 | 0 | NULL, NULL); |
4770 | 0 | } |
4771 | 0 | if (ctxt->grammar->refs == NULL) |
4772 | 0 | ctxt->grammar->refs = xmlHashCreate(10); |
4773 | 0 | if (ctxt->grammar->refs == NULL) { |
4774 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
4775 | 0 | "Could not create references hash\n", NULL, NULL); |
4776 | 0 | def = NULL; |
4777 | 0 | } else { |
4778 | 0 | int tmp; |
4779 | |
|
4780 | 0 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
4781 | 0 | if (tmp < 0) { |
4782 | 0 | xmlRelaxNGDefinePtr prev; |
4783 | |
|
4784 | 0 | prev = (xmlRelaxNGDefinePtr) |
4785 | 0 | xmlHashLookup(ctxt->grammar->refs, def->name); |
4786 | 0 | if (prev == NULL) { |
4787 | 0 | if (def->name != NULL) { |
4788 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
4789 | 0 | "Error refs definitions '%s'\n", |
4790 | 0 | def->name, NULL); |
4791 | 0 | } else { |
4792 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
4793 | 0 | "Error refs definitions\n", |
4794 | 0 | NULL, NULL); |
4795 | 0 | } |
4796 | 0 | def = NULL; |
4797 | 0 | } else { |
4798 | 0 | def->nextHash = prev->nextHash; |
4799 | 0 | prev->nextHash = def; |
4800 | 0 | } |
4801 | 0 | } |
4802 | 0 | } |
4803 | 0 | } else if (IS_RELAXNG(node, "data")) { |
4804 | 0 | def = xmlRelaxNGParseData(ctxt, node); |
4805 | 0 | } else if (IS_RELAXNG(node, "value")) { |
4806 | 0 | def = xmlRelaxNGParseValue(ctxt, node); |
4807 | 0 | } else if (IS_RELAXNG(node, "list")) { |
4808 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4809 | 0 | if (def == NULL) |
4810 | 0 | return (NULL); |
4811 | 0 | def->type = XML_RELAXNG_LIST; |
4812 | 0 | if (node->children == NULL) { |
4813 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4814 | 0 | "Element %s is empty\n", node->name, NULL); |
4815 | 0 | } else { |
4816 | 0 | def->content = |
4817 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4818 | 0 | } |
4819 | 0 | } else if (IS_RELAXNG(node, "interleave")) { |
4820 | 0 | def = xmlRelaxNGParseInterleave(ctxt, node); |
4821 | 0 | } else if (IS_RELAXNG(node, "externalRef")) { |
4822 | 0 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
4823 | 0 | } else if (IS_RELAXNG(node, "notAllowed")) { |
4824 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4825 | 0 | if (def == NULL) |
4826 | 0 | return (NULL); |
4827 | 0 | def->type = XML_RELAXNG_NOT_ALLOWED; |
4828 | 0 | if (node->children != NULL) { |
4829 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
4830 | 0 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
4831 | 0 | NULL, NULL); |
4832 | 0 | } |
4833 | 0 | } else if (IS_RELAXNG(node, "grammar")) { |
4834 | 0 | xmlRelaxNGGrammarPtr grammar, old; |
4835 | 0 | xmlRelaxNGGrammarPtr oldparent; |
4836 | |
|
4837 | 0 | oldparent = ctxt->parentgrammar; |
4838 | 0 | old = ctxt->grammar; |
4839 | 0 | ctxt->parentgrammar = old; |
4840 | 0 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
4841 | 0 | if (old != NULL) { |
4842 | 0 | ctxt->grammar = old; |
4843 | 0 | ctxt->parentgrammar = oldparent; |
4844 | | #if 0 |
4845 | | if (grammar != NULL) { |
4846 | | grammar->next = old->next; |
4847 | | old->next = grammar; |
4848 | | } |
4849 | | #endif |
4850 | 0 | } |
4851 | 0 | if (grammar != NULL) |
4852 | 0 | def = grammar->start; |
4853 | 0 | else |
4854 | 0 | def = NULL; |
4855 | 0 | } else if (IS_RELAXNG(node, "parentRef")) { |
4856 | 0 | if (ctxt->parentgrammar == NULL) { |
4857 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
4858 | 0 | "Use of parentRef without a parent grammar\n", NULL, |
4859 | 0 | NULL); |
4860 | 0 | return (NULL); |
4861 | 0 | } |
4862 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4863 | 0 | if (def == NULL) |
4864 | 0 | return (NULL); |
4865 | 0 | def->type = XML_RELAXNG_PARENTREF; |
4866 | 0 | def->name = xmlGetProp(node, BAD_CAST "name"); |
4867 | 0 | if (def->name == NULL) { |
4868 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
4869 | 0 | "parentRef has no name\n", NULL, NULL); |
4870 | 0 | } else { |
4871 | 0 | xmlRelaxNGNormExtSpace(def->name); |
4872 | 0 | if (xmlValidateNCName(def->name, 0)) { |
4873 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
4874 | 0 | "parentRef name '%s' is not an NCName\n", |
4875 | 0 | def->name, NULL); |
4876 | 0 | } |
4877 | 0 | } |
4878 | 0 | if (node->children != NULL) { |
4879 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
4880 | 0 | "parentRef is not empty\n", NULL, NULL); |
4881 | 0 | } |
4882 | 0 | if (ctxt->parentgrammar->refs == NULL) |
4883 | 0 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
4884 | 0 | if (ctxt->parentgrammar->refs == NULL) { |
4885 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
4886 | 0 | "Could not create references hash\n", NULL, NULL); |
4887 | 0 | def = NULL; |
4888 | 0 | } else if (def->name != NULL) { |
4889 | 0 | int tmp; |
4890 | |
|
4891 | 0 | tmp = |
4892 | 0 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
4893 | 0 | if (tmp < 0) { |
4894 | 0 | xmlRelaxNGDefinePtr prev; |
4895 | |
|
4896 | 0 | prev = (xmlRelaxNGDefinePtr) |
4897 | 0 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
4898 | 0 | if (prev == NULL) { |
4899 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
4900 | 0 | "Internal error parentRef definitions '%s'\n", |
4901 | 0 | def->name, NULL); |
4902 | 0 | def = NULL; |
4903 | 0 | } else { |
4904 | 0 | def->nextHash = prev->nextHash; |
4905 | 0 | prev->nextHash = def; |
4906 | 0 | } |
4907 | 0 | } |
4908 | 0 | } |
4909 | 0 | } else if (IS_RELAXNG(node, "mixed")) { |
4910 | 0 | if (node->children == NULL) { |
4911 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
4912 | 0 | NULL, NULL); |
4913 | 0 | def = NULL; |
4914 | 0 | } else { |
4915 | 0 | def = xmlRelaxNGParseInterleave(ctxt, node); |
4916 | 0 | if (def != NULL) { |
4917 | 0 | xmlRelaxNGDefinePtr tmp; |
4918 | |
|
4919 | 0 | if ((def->content != NULL) && (def->content->next != NULL)) { |
4920 | 0 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
4921 | 0 | if (tmp != NULL) { |
4922 | 0 | tmp->type = XML_RELAXNG_GROUP; |
4923 | 0 | tmp->content = def->content; |
4924 | 0 | def->content = tmp; |
4925 | 0 | } |
4926 | 0 | } |
4927 | |
|
4928 | 0 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
4929 | 0 | if (tmp == NULL) |
4930 | 0 | return (def); |
4931 | 0 | tmp->type = XML_RELAXNG_TEXT; |
4932 | 0 | tmp->next = def->content; |
4933 | 0 | def->content = tmp; |
4934 | 0 | } |
4935 | 0 | } |
4936 | 0 | } else { |
4937 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
4938 | 0 | "Unexpected node %s is not a pattern\n", node->name, |
4939 | 0 | NULL); |
4940 | 0 | def = NULL; |
4941 | 0 | } |
4942 | 0 | return (def); |
4943 | 0 | } |
4944 | | |
4945 | | /** |
4946 | | * parse the content of a RelaxNG attribute node. |
4947 | | * |
4948 | | * @param ctxt a Relax-NG parser context |
4949 | | * @param node the element node |
4950 | | * @returns the definition pointer or NULL in case of error. |
4951 | | */ |
4952 | | static xmlRelaxNGDefinePtr |
4953 | | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4954 | 0 | { |
4955 | 0 | xmlRelaxNGDefinePtr ret, cur; |
4956 | 0 | xmlNodePtr child; |
4957 | 0 | int old_flags; |
4958 | |
|
4959 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
4960 | 0 | if (ret == NULL) |
4961 | 0 | return (NULL); |
4962 | 0 | ret->type = XML_RELAXNG_ATTRIBUTE; |
4963 | 0 | ret->parent = ctxt->def; |
4964 | 0 | child = node->children; |
4965 | 0 | if (child == NULL) { |
4966 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
4967 | 0 | "xmlRelaxNGParseattribute: attribute has no children\n", |
4968 | 0 | NULL, NULL); |
4969 | 0 | return (ret); |
4970 | 0 | } |
4971 | 0 | old_flags = ctxt->flags; |
4972 | 0 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
4973 | 0 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
4974 | 0 | if (cur != NULL) |
4975 | 0 | child = child->next; |
4976 | |
|
4977 | 0 | if (child != NULL) { |
4978 | 0 | cur = xmlRelaxNGParsePattern(ctxt, child); |
4979 | 0 | if (cur != NULL) { |
4980 | 0 | switch (cur->type) { |
4981 | 0 | case XML_RELAXNG_EMPTY: |
4982 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
4983 | 0 | case XML_RELAXNG_TEXT: |
4984 | 0 | case XML_RELAXNG_ELEMENT: |
4985 | 0 | case XML_RELAXNG_DATATYPE: |
4986 | 0 | case XML_RELAXNG_VALUE: |
4987 | 0 | case XML_RELAXNG_LIST: |
4988 | 0 | case XML_RELAXNG_REF: |
4989 | 0 | case XML_RELAXNG_PARENTREF: |
4990 | 0 | case XML_RELAXNG_EXTERNALREF: |
4991 | 0 | case XML_RELAXNG_DEF: |
4992 | 0 | case XML_RELAXNG_ONEORMORE: |
4993 | 0 | case XML_RELAXNG_ZEROORMORE: |
4994 | 0 | case XML_RELAXNG_OPTIONAL: |
4995 | 0 | case XML_RELAXNG_CHOICE: |
4996 | 0 | case XML_RELAXNG_GROUP: |
4997 | 0 | case XML_RELAXNG_INTERLEAVE: |
4998 | 0 | case XML_RELAXNG_ATTRIBUTE: |
4999 | 0 | ret->content = cur; |
5000 | 0 | cur->parent = ret; |
5001 | 0 | break; |
5002 | 0 | case XML_RELAXNG_START: |
5003 | 0 | case XML_RELAXNG_PARAM: |
5004 | 0 | case XML_RELAXNG_EXCEPT: |
5005 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
5006 | 0 | "attribute has invalid content\n", NULL, |
5007 | 0 | NULL); |
5008 | 0 | break; |
5009 | 0 | case XML_RELAXNG_NOOP: |
5010 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
5011 | 0 | "RNG Internal error, noop found in attribute\n", |
5012 | 0 | NULL, NULL); |
5013 | 0 | break; |
5014 | 0 | } |
5015 | 0 | } |
5016 | 0 | child = child->next; |
5017 | 0 | } |
5018 | 0 | if (child != NULL) { |
5019 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
5020 | 0 | "attribute has multiple children\n", NULL, NULL); |
5021 | 0 | } |
5022 | 0 | ctxt->flags = old_flags; |
5023 | 0 | return (ret); |
5024 | 0 | } |
5025 | | |
5026 | | /** |
5027 | | * parse the content of a RelaxNG nameClass node. |
5028 | | * |
5029 | | * @param ctxt a Relax-NG parser context |
5030 | | * @param node the except node |
5031 | | * @param attr 1 if within an attribute, 0 if within an element |
5032 | | * @returns the definition pointer or NULL in case of error. |
5033 | | */ |
5034 | | static xmlRelaxNGDefinePtr |
5035 | | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
5036 | | xmlNodePtr node, int attr) |
5037 | 0 | { |
5038 | 0 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
5039 | 0 | xmlNodePtr child; |
5040 | |
|
5041 | 0 | if (!IS_RELAXNG(node, "except")) { |
5042 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
5043 | 0 | "Expecting an except node\n", NULL, NULL); |
5044 | 0 | return (NULL); |
5045 | 0 | } |
5046 | 0 | if (node->next != NULL) { |
5047 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
5048 | 0 | "exceptNameClass allows only a single except node\n", |
5049 | 0 | NULL, NULL); |
5050 | 0 | } |
5051 | 0 | if (node->children == NULL) { |
5052 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
5053 | 0 | NULL, NULL); |
5054 | 0 | return (NULL); |
5055 | 0 | } |
5056 | | |
5057 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5058 | 0 | if (ret == NULL) |
5059 | 0 | return (NULL); |
5060 | 0 | ret->type = XML_RELAXNG_EXCEPT; |
5061 | 0 | child = node->children; |
5062 | 0 | while (child != NULL) { |
5063 | 0 | cur = xmlRelaxNGNewDefine(ctxt, child); |
5064 | 0 | if (cur == NULL) |
5065 | 0 | break; |
5066 | 0 | if (attr) |
5067 | 0 | cur->type = XML_RELAXNG_ATTRIBUTE; |
5068 | 0 | else |
5069 | 0 | cur->type = XML_RELAXNG_ELEMENT; |
5070 | |
|
5071 | 0 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
5072 | 0 | if (last == NULL) { |
5073 | 0 | ret->content = cur; |
5074 | 0 | } else { |
5075 | 0 | last->next = cur; |
5076 | 0 | } |
5077 | 0 | last = cur; |
5078 | 0 | } |
5079 | 0 | child = child->next; |
5080 | 0 | } |
5081 | |
|
5082 | 0 | return (ret); |
5083 | 0 | } |
5084 | | |
5085 | | /** |
5086 | | * parse the content of a RelaxNG nameClass node. |
5087 | | * |
5088 | | * @param ctxt a Relax-NG parser context |
5089 | | * @param node the nameClass node |
5090 | | * @param def the current definition |
5091 | | * @returns the definition pointer or NULL in case of error. |
5092 | | */ |
5093 | | static xmlRelaxNGDefinePtr |
5094 | | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
5095 | | xmlRelaxNGDefinePtr def) |
5096 | 0 | { |
5097 | 0 | xmlRelaxNGDefinePtr ret, tmp; |
5098 | 0 | xmlChar *val; |
5099 | |
|
5100 | 0 | ret = def; |
5101 | 0 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
5102 | 0 | (IS_RELAXNG(node, "nsName"))) { |
5103 | 0 | if ((def->type != XML_RELAXNG_ELEMENT) && |
5104 | 0 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
5105 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5106 | 0 | if (ret == NULL) |
5107 | 0 | return (NULL); |
5108 | 0 | ret->parent = def; |
5109 | 0 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
5110 | 0 | ret->type = XML_RELAXNG_ATTRIBUTE; |
5111 | 0 | else |
5112 | 0 | ret->type = XML_RELAXNG_ELEMENT; |
5113 | 0 | } |
5114 | 0 | } |
5115 | 0 | if (IS_RELAXNG(node, "name")) { |
5116 | 0 | val = xmlNodeGetContent(node); |
5117 | 0 | xmlRelaxNGNormExtSpace(val); |
5118 | 0 | if (xmlValidateNCName(val, 0)) { |
5119 | 0 | if (node->parent != NULL) |
5120 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
5121 | 0 | "Element %s name '%s' is not an NCName\n", |
5122 | 0 | node->parent->name, val); |
5123 | 0 | else |
5124 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
5125 | 0 | "name '%s' is not an NCName\n", |
5126 | 0 | val, NULL); |
5127 | 0 | } |
5128 | 0 | ret->name = val; |
5129 | 0 | val = xmlGetProp(node, BAD_CAST "ns"); |
5130 | 0 | ret->ns = val; |
5131 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
5132 | 0 | (val != NULL) && |
5133 | 0 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
5134 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
5135 | 0 | "Attribute with namespace '%s' is not allowed\n", |
5136 | 0 | val, NULL); |
5137 | 0 | } |
5138 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
5139 | 0 | (val != NULL) && |
5140 | 0 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
5141 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
5142 | 0 | "Attribute with QName 'xmlns' is not allowed\n", |
5143 | 0 | val, NULL); |
5144 | 0 | } |
5145 | 0 | } else if (IS_RELAXNG(node, "anyName")) { |
5146 | 0 | ret->name = NULL; |
5147 | 0 | ret->ns = NULL; |
5148 | 0 | if (node->children != NULL) { |
5149 | 0 | ret->nameClass = |
5150 | 0 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
5151 | 0 | (def->type == |
5152 | 0 | XML_RELAXNG_ATTRIBUTE)); |
5153 | 0 | } |
5154 | 0 | } else if (IS_RELAXNG(node, "nsName")) { |
5155 | 0 | ret->name = NULL; |
5156 | 0 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
5157 | 0 | if (ret->ns == NULL) { |
5158 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
5159 | 0 | "nsName has no ns attribute\n", NULL, NULL); |
5160 | 0 | } |
5161 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
5162 | 0 | (ret->ns != NULL) && |
5163 | 0 | (xmlStrEqual |
5164 | 0 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
5165 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
5166 | 0 | "Attribute with namespace '%s' is not allowed\n", |
5167 | 0 | ret->ns, NULL); |
5168 | 0 | } |
5169 | 0 | if (node->children != NULL) { |
5170 | 0 | ret->nameClass = |
5171 | 0 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
5172 | 0 | (def->type == |
5173 | 0 | XML_RELAXNG_ATTRIBUTE)); |
5174 | 0 | } |
5175 | 0 | } else if (IS_RELAXNG(node, "choice")) { |
5176 | 0 | xmlNodePtr child; |
5177 | 0 | xmlRelaxNGDefinePtr last = NULL; |
5178 | |
|
5179 | 0 | if (def->type == XML_RELAXNG_CHOICE) { |
5180 | 0 | ret = def; |
5181 | 0 | } else { |
5182 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5183 | 0 | if (ret == NULL) |
5184 | 0 | return (NULL); |
5185 | 0 | ret->parent = def; |
5186 | 0 | ret->type = XML_RELAXNG_CHOICE; |
5187 | 0 | } |
5188 | | |
5189 | 0 | if (node->children == NULL) { |
5190 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
5191 | 0 | "Element choice is empty\n", NULL, NULL); |
5192 | 0 | } else { |
5193 | |
|
5194 | 0 | child = node->children; |
5195 | 0 | while (child != NULL) { |
5196 | 0 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
5197 | 0 | if (tmp != NULL) { |
5198 | 0 | if (last == NULL) { |
5199 | 0 | last = tmp; |
5200 | 0 | } else if (tmp != ret) { |
5201 | 0 | last->next = tmp; |
5202 | 0 | last = tmp; |
5203 | 0 | } |
5204 | 0 | } |
5205 | 0 | child = child->next; |
5206 | 0 | } |
5207 | 0 | } |
5208 | 0 | } else { |
5209 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
5210 | 0 | "expecting name, anyName, nsName or choice : got %s\n", |
5211 | 0 | (node == NULL ? (const xmlChar *) "nothing" : node->name), |
5212 | 0 | NULL); |
5213 | 0 | return (NULL); |
5214 | 0 | } |
5215 | 0 | if (ret != def) { |
5216 | 0 | if (def->nameClass == NULL) { |
5217 | 0 | def->nameClass = ret; |
5218 | 0 | } else { |
5219 | 0 | tmp = def->nameClass; |
5220 | 0 | while (tmp->next != NULL) { |
5221 | 0 | tmp = tmp->next; |
5222 | 0 | } |
5223 | 0 | tmp->next = ret; |
5224 | 0 | } |
5225 | 0 | } |
5226 | 0 | return (ret); |
5227 | 0 | } |
5228 | | |
5229 | | /** |
5230 | | * parse the content of a RelaxNG element node. |
5231 | | * |
5232 | | * @param ctxt a Relax-NG parser context |
5233 | | * @param node the element node |
5234 | | * @returns the definition pointer or NULL in case of error. |
5235 | | */ |
5236 | | static xmlRelaxNGDefinePtr |
5237 | | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
5238 | 0 | { |
5239 | 0 | xmlRelaxNGDefinePtr ret, cur, last; |
5240 | 0 | xmlNodePtr child; |
5241 | 0 | const xmlChar *olddefine; |
5242 | |
|
5243 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5244 | 0 | if (ret == NULL) |
5245 | 0 | return (NULL); |
5246 | 0 | ret->type = XML_RELAXNG_ELEMENT; |
5247 | 0 | ret->parent = ctxt->def; |
5248 | 0 | child = node->children; |
5249 | 0 | if (child == NULL) { |
5250 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
5251 | 0 | "xmlRelaxNGParseElement: element has no children\n", |
5252 | 0 | NULL, NULL); |
5253 | 0 | return (ret); |
5254 | 0 | } |
5255 | 0 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
5256 | 0 | if (cur != NULL) |
5257 | 0 | child = child->next; |
5258 | |
|
5259 | 0 | if (child == NULL) { |
5260 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
5261 | 0 | "xmlRelaxNGParseElement: element has no content\n", |
5262 | 0 | NULL, NULL); |
5263 | 0 | return (ret); |
5264 | 0 | } |
5265 | 0 | olddefine = ctxt->define; |
5266 | 0 | ctxt->define = NULL; |
5267 | 0 | last = NULL; |
5268 | 0 | while (child != NULL) { |
5269 | 0 | cur = xmlRelaxNGParsePattern(ctxt, child); |
5270 | 0 | if (cur != NULL) { |
5271 | 0 | cur->parent = ret; |
5272 | 0 | switch (cur->type) { |
5273 | 0 | case XML_RELAXNG_EMPTY: |
5274 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
5275 | 0 | case XML_RELAXNG_TEXT: |
5276 | 0 | case XML_RELAXNG_ELEMENT: |
5277 | 0 | case XML_RELAXNG_DATATYPE: |
5278 | 0 | case XML_RELAXNG_VALUE: |
5279 | 0 | case XML_RELAXNG_LIST: |
5280 | 0 | case XML_RELAXNG_REF: |
5281 | 0 | case XML_RELAXNG_PARENTREF: |
5282 | 0 | case XML_RELAXNG_EXTERNALREF: |
5283 | 0 | case XML_RELAXNG_DEF: |
5284 | 0 | case XML_RELAXNG_ZEROORMORE: |
5285 | 0 | case XML_RELAXNG_ONEORMORE: |
5286 | 0 | case XML_RELAXNG_OPTIONAL: |
5287 | 0 | case XML_RELAXNG_CHOICE: |
5288 | 0 | case XML_RELAXNG_GROUP: |
5289 | 0 | case XML_RELAXNG_INTERLEAVE: |
5290 | 0 | if (last == NULL) { |
5291 | 0 | ret->content = last = cur; |
5292 | 0 | } else { |
5293 | 0 | if ((last->type == XML_RELAXNG_ELEMENT) && |
5294 | 0 | (ret->content == last)) { |
5295 | 0 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
5296 | 0 | if (ret->content != NULL) { |
5297 | 0 | ret->content->type = XML_RELAXNG_GROUP; |
5298 | 0 | ret->content->content = last; |
5299 | 0 | } else { |
5300 | 0 | ret->content = last; |
5301 | 0 | } |
5302 | 0 | } |
5303 | 0 | last->next = cur; |
5304 | 0 | last = cur; |
5305 | 0 | } |
5306 | 0 | break; |
5307 | 0 | case XML_RELAXNG_ATTRIBUTE: |
5308 | 0 | cur->next = ret->attrs; |
5309 | 0 | ret->attrs = cur; |
5310 | 0 | break; |
5311 | 0 | case XML_RELAXNG_START: |
5312 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5313 | 0 | "RNG Internal error, start found in element\n", |
5314 | 0 | NULL, NULL); |
5315 | 0 | break; |
5316 | 0 | case XML_RELAXNG_PARAM: |
5317 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5318 | 0 | "RNG Internal error, param found in element\n", |
5319 | 0 | NULL, NULL); |
5320 | 0 | break; |
5321 | 0 | case XML_RELAXNG_EXCEPT: |
5322 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5323 | 0 | "RNG Internal error, except found in element\n", |
5324 | 0 | NULL, NULL); |
5325 | 0 | break; |
5326 | 0 | case XML_RELAXNG_NOOP: |
5327 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5328 | 0 | "RNG Internal error, noop found in element\n", |
5329 | 0 | NULL, NULL); |
5330 | 0 | break; |
5331 | 0 | } |
5332 | 0 | } |
5333 | 0 | child = child->next; |
5334 | 0 | } |
5335 | 0 | ctxt->define = olddefine; |
5336 | 0 | return (ret); |
5337 | 0 | } |
5338 | | |
5339 | | /** |
5340 | | * parse the content of a RelaxNG start node. |
5341 | | * |
5342 | | * @param ctxt a Relax-NG parser context |
5343 | | * @param nodes list of nodes |
5344 | | * @param group use an implicit `<group>` for elements |
5345 | | * @returns the definition pointer or NULL in case of error. |
5346 | | */ |
5347 | | static xmlRelaxNGDefinePtr |
5348 | | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
5349 | | int group) |
5350 | 0 | { |
5351 | 0 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
5352 | |
|
5353 | 0 | parent = ctxt->def; |
5354 | 0 | while (nodes != NULL) { |
5355 | 0 | if (IS_RELAXNG(nodes, "element")) { |
5356 | 0 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
5357 | 0 | if (cur == NULL) |
5358 | 0 | return (NULL); |
5359 | 0 | if (def == NULL) { |
5360 | 0 | def = last = cur; |
5361 | 0 | } else { |
5362 | 0 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
5363 | 0 | (def == last)) { |
5364 | 0 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
5365 | 0 | if (def == NULL) |
5366 | 0 | return (NULL); |
5367 | 0 | def->type = XML_RELAXNG_GROUP; |
5368 | 0 | def->content = last; |
5369 | 0 | } |
5370 | 0 | last->next = cur; |
5371 | 0 | last = cur; |
5372 | 0 | } |
5373 | 0 | cur->parent = parent; |
5374 | 0 | } else { |
5375 | 0 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
5376 | 0 | if (cur != NULL) { |
5377 | 0 | if (def == NULL) { |
5378 | 0 | def = last = cur; |
5379 | 0 | } else { |
5380 | 0 | last->next = cur; |
5381 | 0 | last = cur; |
5382 | 0 | } |
5383 | 0 | cur->parent = parent; |
5384 | 0 | } |
5385 | 0 | } |
5386 | 0 | nodes = nodes->next; |
5387 | 0 | } |
5388 | 0 | return (def); |
5389 | 0 | } |
5390 | | |
5391 | | /** |
5392 | | * parse the content of a RelaxNG start node. |
5393 | | * |
5394 | | * @param ctxt a Relax-NG parser context |
5395 | | * @param nodes start children nodes |
5396 | | * @returns 0 in case of success, -1 in case of error |
5397 | | */ |
5398 | | static int |
5399 | | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
5400 | 0 | { |
5401 | 0 | int ret = 0; |
5402 | 0 | xmlRelaxNGDefinePtr def = NULL, last; |
5403 | |
|
5404 | 0 | if (nodes == NULL) { |
5405 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
5406 | 0 | NULL, NULL); |
5407 | 0 | return (-1); |
5408 | 0 | } |
5409 | 0 | if (IS_RELAXNG(nodes, "empty")) { |
5410 | 0 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
5411 | 0 | if (def == NULL) |
5412 | 0 | return (-1); |
5413 | 0 | def->type = XML_RELAXNG_EMPTY; |
5414 | 0 | if (nodes->children != NULL) { |
5415 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
5416 | 0 | "element empty is not empty\n", NULL, NULL); |
5417 | 0 | } |
5418 | 0 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
5419 | 0 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
5420 | 0 | if (def == NULL) |
5421 | 0 | return (-1); |
5422 | 0 | def->type = XML_RELAXNG_NOT_ALLOWED; |
5423 | 0 | if (nodes->children != NULL) { |
5424 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
5425 | 0 | "element notAllowed is not empty\n", NULL, NULL); |
5426 | 0 | } |
5427 | 0 | } else { |
5428 | 0 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
5429 | 0 | } |
5430 | 0 | if (ctxt->grammar->start != NULL) { |
5431 | 0 | last = ctxt->grammar->start; |
5432 | 0 | while (last->next != NULL) |
5433 | 0 | last = last->next; |
5434 | 0 | last->next = def; |
5435 | 0 | } else { |
5436 | 0 | ctxt->grammar->start = def; |
5437 | 0 | } |
5438 | 0 | nodes = nodes->next; |
5439 | 0 | if (nodes != NULL) { |
5440 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
5441 | 0 | "start more than one children\n", NULL, NULL); |
5442 | 0 | return (-1); |
5443 | 0 | } |
5444 | 0 | return (ret); |
5445 | 0 | } |
5446 | | |
5447 | | /** |
5448 | | * parse the content of a RelaxNG grammar node. |
5449 | | * |
5450 | | * @param ctxt a Relax-NG parser context |
5451 | | * @param nodes grammar children nodes |
5452 | | * @returns 0 in case of success, -1 in case of error |
5453 | | */ |
5454 | | static int |
5455 | | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
5456 | | xmlNodePtr nodes) |
5457 | 0 | { |
5458 | 0 | int ret = 0, tmp; |
5459 | |
|
5460 | 0 | if (nodes == NULL) { |
5461 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
5462 | 0 | "grammar has no children\n", NULL, NULL); |
5463 | 0 | return (-1); |
5464 | 0 | } |
5465 | 0 | while (nodes != NULL) { |
5466 | 0 | if (IS_RELAXNG(nodes, "start")) { |
5467 | 0 | if (nodes->children == NULL) { |
5468 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
5469 | 0 | "start has no children\n", NULL, NULL); |
5470 | 0 | } else { |
5471 | 0 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
5472 | 0 | if (tmp != 0) |
5473 | 0 | ret = -1; |
5474 | 0 | } |
5475 | 0 | } else if (IS_RELAXNG(nodes, "define")) { |
5476 | 0 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
5477 | 0 | if (tmp != 0) |
5478 | 0 | ret = -1; |
5479 | 0 | } else if (IS_RELAXNG(nodes, "include")) { |
5480 | 0 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
5481 | 0 | if (tmp != 0) |
5482 | 0 | ret = -1; |
5483 | 0 | } else { |
5484 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
5485 | 0 | "grammar has unexpected child %s\n", nodes->name, |
5486 | 0 | NULL); |
5487 | 0 | ret = -1; |
5488 | 0 | } |
5489 | 0 | nodes = nodes->next; |
5490 | 0 | } |
5491 | 0 | return (ret); |
5492 | 0 | } |
5493 | | |
5494 | | /** |
5495 | | * Applies the 4.17. combine attribute rule for all the define |
5496 | | * element of a given grammar using the same name. |
5497 | | * |
5498 | | * @param payload the ref |
5499 | | * @param data a Relax-NG parser context |
5500 | | * @param name the name associated to the defines |
5501 | | */ |
5502 | | static void |
5503 | | xmlRelaxNGCheckReference(void *payload, void *data, const xmlChar * name) |
5504 | 0 | { |
5505 | 0 | xmlRelaxNGDefinePtr ref = (xmlRelaxNGDefinePtr) payload; |
5506 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
5507 | 0 | xmlRelaxNGGrammarPtr grammar; |
5508 | 0 | xmlRelaxNGDefinePtr def, cur; |
5509 | | |
5510 | | /* |
5511 | | * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef |
5512 | | */ |
5513 | 0 | if (ref->dflags & IS_EXTERNAL_REF) |
5514 | 0 | return; |
5515 | | |
5516 | 0 | grammar = ctxt->grammar; |
5517 | 0 | if (grammar == NULL) { |
5518 | 0 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
5519 | 0 | "Internal error: no grammar in CheckReference %s\n", |
5520 | 0 | name, NULL); |
5521 | 0 | return; |
5522 | 0 | } |
5523 | 0 | if (ref->content != NULL) { |
5524 | 0 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
5525 | 0 | "Internal error: reference has content in CheckReference %s\n", |
5526 | 0 | name, NULL); |
5527 | 0 | return; |
5528 | 0 | } |
5529 | 0 | if (grammar->defs != NULL) { |
5530 | 0 | def = xmlHashLookup(grammar->defs, name); |
5531 | 0 | if (def != NULL) { |
5532 | 0 | cur = ref; |
5533 | 0 | while (cur != NULL) { |
5534 | 0 | cur->content = def; |
5535 | 0 | cur = cur->nextHash; |
5536 | 0 | } |
5537 | 0 | } else { |
5538 | 0 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
5539 | 0 | "Reference %s has no matching definition\n", name, |
5540 | 0 | NULL); |
5541 | 0 | } |
5542 | 0 | } else { |
5543 | 0 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
5544 | 0 | "Reference %s has no matching definition\n", name, |
5545 | 0 | NULL); |
5546 | 0 | } |
5547 | 0 | } |
5548 | | |
5549 | | /** |
5550 | | * Applies the 4.17. combine attribute rule for all the define |
5551 | | * element of a given grammar using the same name. |
5552 | | * |
5553 | | * @param payload the define(s) list |
5554 | | * @param data a Relax-NG parser context |
5555 | | * @param name the name associated to the defines |
5556 | | */ |
5557 | | static void |
5558 | | xmlRelaxNGCheckCombine(void *payload, void *data, const xmlChar * name) |
5559 | 0 | { |
5560 | 0 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) payload; |
5561 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
5562 | 0 | xmlChar *combine; |
5563 | 0 | int choiceOrInterleave = -1; |
5564 | 0 | int missing = 0; |
5565 | 0 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
5566 | |
|
5567 | 0 | if (define->nextHash == NULL) |
5568 | 0 | return; |
5569 | 0 | cur = define; |
5570 | 0 | while (cur != NULL) { |
5571 | 0 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
5572 | 0 | if (combine != NULL) { |
5573 | 0 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
5574 | 0 | if (choiceOrInterleave == -1) |
5575 | 0 | choiceOrInterleave = 1; |
5576 | 0 | else if (choiceOrInterleave == 0) { |
5577 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
5578 | 0 | "Defines for %s use both 'choice' and 'interleave'\n", |
5579 | 0 | name, NULL); |
5580 | 0 | } |
5581 | 0 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
5582 | 0 | if (choiceOrInterleave == -1) |
5583 | 0 | choiceOrInterleave = 0; |
5584 | 0 | else if (choiceOrInterleave == 1) { |
5585 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
5586 | 0 | "Defines for %s use both 'choice' and 'interleave'\n", |
5587 | 0 | name, NULL); |
5588 | 0 | } |
5589 | 0 | } else { |
5590 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
5591 | 0 | "Defines for %s use unknown combine value '%s''\n", |
5592 | 0 | name, combine); |
5593 | 0 | } |
5594 | 0 | xmlFree(combine); |
5595 | 0 | } else { |
5596 | 0 | if (missing == 0) |
5597 | 0 | missing = 1; |
5598 | 0 | else { |
5599 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
5600 | 0 | "Some defines for %s needs the combine attribute\n", |
5601 | 0 | name, NULL); |
5602 | 0 | } |
5603 | 0 | } |
5604 | |
|
5605 | 0 | cur = cur->nextHash; |
5606 | 0 | } |
5607 | 0 | if (choiceOrInterleave == -1) |
5608 | 0 | choiceOrInterleave = 0; |
5609 | 0 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
5610 | 0 | if (cur == NULL) |
5611 | 0 | return; |
5612 | 0 | if (choiceOrInterleave == 0) |
5613 | 0 | cur->type = XML_RELAXNG_INTERLEAVE; |
5614 | 0 | else |
5615 | 0 | cur->type = XML_RELAXNG_CHOICE; |
5616 | 0 | tmp = define; |
5617 | 0 | last = NULL; |
5618 | 0 | while (tmp != NULL) { |
5619 | 0 | if (tmp->content != NULL) { |
5620 | 0 | if (tmp->content->next != NULL) { |
5621 | | /* |
5622 | | * we need first to create a wrapper. |
5623 | | */ |
5624 | 0 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
5625 | 0 | if (tmp2 == NULL) |
5626 | 0 | break; |
5627 | 0 | tmp2->type = XML_RELAXNG_GROUP; |
5628 | 0 | tmp2->content = tmp->content; |
5629 | 0 | } else { |
5630 | 0 | tmp2 = tmp->content; |
5631 | 0 | } |
5632 | 0 | if (last == NULL) { |
5633 | 0 | cur->content = tmp2; |
5634 | 0 | } else { |
5635 | 0 | last->next = tmp2; |
5636 | 0 | } |
5637 | 0 | last = tmp2; |
5638 | 0 | } |
5639 | 0 | tmp->content = cur; |
5640 | 0 | tmp = tmp->nextHash; |
5641 | 0 | } |
5642 | 0 | define->content = cur; |
5643 | 0 | if (choiceOrInterleave == 0) { |
5644 | 0 | if (ctxt->interleaves == NULL) |
5645 | 0 | ctxt->interleaves = xmlHashCreate(10); |
5646 | 0 | if (ctxt->interleaves == NULL) { |
5647 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5648 | 0 | "Failed to create interleaves hash table\n", NULL, |
5649 | 0 | NULL); |
5650 | 0 | } else { |
5651 | 0 | char tmpname[32]; |
5652 | |
|
5653 | 0 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
5654 | 0 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
5655 | 0 | 0) { |
5656 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5657 | 0 | "Failed to add %s to hash table\n", |
5658 | 0 | (const xmlChar *) tmpname, NULL); |
5659 | 0 | } |
5660 | 0 | } |
5661 | 0 | } |
5662 | 0 | } |
5663 | | |
5664 | | /** |
5665 | | * Applies the 4.17. combine rule for all the start |
5666 | | * element of a given grammar. |
5667 | | * |
5668 | | * @param ctxt a Relax-NG parser context |
5669 | | * @param grammar the grammar |
5670 | | */ |
5671 | | static void |
5672 | | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
5673 | | xmlRelaxNGGrammarPtr grammar) |
5674 | 0 | { |
5675 | 0 | xmlRelaxNGDefinePtr starts; |
5676 | 0 | xmlChar *combine; |
5677 | 0 | int choiceOrInterleave = -1; |
5678 | 0 | int missing = 0; |
5679 | 0 | xmlRelaxNGDefinePtr cur; |
5680 | |
|
5681 | 0 | starts = grammar->start; |
5682 | 0 | if ((starts == NULL) || (starts->next == NULL)) |
5683 | 0 | return; |
5684 | 0 | cur = starts; |
5685 | 0 | while (cur != NULL) { |
5686 | 0 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
5687 | 0 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
5688 | 0 | combine = NULL; |
5689 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
5690 | 0 | "Internal error: start element not found\n", NULL, |
5691 | 0 | NULL); |
5692 | 0 | } else { |
5693 | 0 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
5694 | 0 | } |
5695 | |
|
5696 | 0 | if (combine != NULL) { |
5697 | 0 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
5698 | 0 | if (choiceOrInterleave == -1) |
5699 | 0 | choiceOrInterleave = 1; |
5700 | 0 | else if (choiceOrInterleave == 0) { |
5701 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
5702 | 0 | "<start> use both 'choice' and 'interleave'\n", |
5703 | 0 | NULL, NULL); |
5704 | 0 | } |
5705 | 0 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
5706 | 0 | if (choiceOrInterleave == -1) |
5707 | 0 | choiceOrInterleave = 0; |
5708 | 0 | else if (choiceOrInterleave == 1) { |
5709 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
5710 | 0 | "<start> use both 'choice' and 'interleave'\n", |
5711 | 0 | NULL, NULL); |
5712 | 0 | } |
5713 | 0 | } else { |
5714 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
5715 | 0 | "<start> uses unknown combine value '%s''\n", |
5716 | 0 | combine, NULL); |
5717 | 0 | } |
5718 | 0 | xmlFree(combine); |
5719 | 0 | } else { |
5720 | 0 | if (missing == 0) |
5721 | 0 | missing = 1; |
5722 | 0 | else { |
5723 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
5724 | 0 | "Some <start> element miss the combine attribute\n", |
5725 | 0 | NULL, NULL); |
5726 | 0 | } |
5727 | 0 | } |
5728 | |
|
5729 | 0 | cur = cur->next; |
5730 | 0 | } |
5731 | 0 | if (choiceOrInterleave == -1) |
5732 | 0 | choiceOrInterleave = 0; |
5733 | 0 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
5734 | 0 | if (cur == NULL) |
5735 | 0 | return; |
5736 | 0 | if (choiceOrInterleave == 0) |
5737 | 0 | cur->type = XML_RELAXNG_INTERLEAVE; |
5738 | 0 | else |
5739 | 0 | cur->type = XML_RELAXNG_CHOICE; |
5740 | 0 | cur->content = grammar->start; |
5741 | 0 | grammar->start = cur; |
5742 | 0 | if (choiceOrInterleave == 0) { |
5743 | 0 | if (ctxt->interleaves == NULL) |
5744 | 0 | ctxt->interleaves = xmlHashCreate(10); |
5745 | 0 | if (ctxt->interleaves == NULL) { |
5746 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5747 | 0 | "Failed to create interleaves hash table\n", NULL, |
5748 | 0 | NULL); |
5749 | 0 | } else { |
5750 | 0 | char tmpname[32]; |
5751 | |
|
5752 | 0 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
5753 | 0 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
5754 | 0 | 0) { |
5755 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5756 | 0 | "Failed to add %s to hash table\n", |
5757 | 0 | (const xmlChar *) tmpname, NULL); |
5758 | 0 | } |
5759 | 0 | } |
5760 | 0 | } |
5761 | 0 | } |
5762 | | |
5763 | | /** |
5764 | | * Check for cycles. |
5765 | | * |
5766 | | * @param ctxt a Relax-NG parser context |
5767 | | * @param cur grammar children nodes |
5768 | | * @param depth the counter |
5769 | | * @returns 0 if check passed, and -1 in case of error |
5770 | | */ |
5771 | | static int |
5772 | | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
5773 | | xmlRelaxNGDefinePtr cur, int depth) |
5774 | 0 | { |
5775 | 0 | int ret = 0; |
5776 | |
|
5777 | 0 | while ((ret == 0) && (cur != NULL)) { |
5778 | 0 | if ((cur->type == XML_RELAXNG_REF) || |
5779 | 0 | (cur->type == XML_RELAXNG_PARENTREF)) { |
5780 | 0 | if (cur->depth == -1) { |
5781 | 0 | cur->depth = depth; |
5782 | 0 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
5783 | 0 | cur->depth = -2; |
5784 | 0 | } else if (depth == cur->depth) { |
5785 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
5786 | 0 | "Detected a cycle in %s references\n", |
5787 | 0 | cur->name, NULL); |
5788 | 0 | return (-1); |
5789 | 0 | } |
5790 | 0 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
5791 | 0 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
5792 | 0 | } else { |
5793 | 0 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
5794 | 0 | } |
5795 | 0 | cur = cur->next; |
5796 | 0 | } |
5797 | 0 | return (ret); |
5798 | 0 | } |
5799 | | |
5800 | | /** |
5801 | | * Try to unlink a definition. If not possible make it a NOOP |
5802 | | * |
5803 | | * @param ctxt a Relax-NG parser context |
5804 | | * @param cur the definition to unlink |
5805 | | * @param parent the parent definition |
5806 | | * @param prev the previous sibling definition |
5807 | | * @returns the new prev definition |
5808 | | */ |
5809 | | static xmlRelaxNGDefinePtr |
5810 | | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
5811 | | xmlRelaxNGDefinePtr cur, |
5812 | | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
5813 | 0 | { |
5814 | 0 | if (prev != NULL) { |
5815 | 0 | prev->next = cur->next; |
5816 | 0 | } else { |
5817 | 0 | if (parent != NULL) { |
5818 | 0 | if (parent->attrs == cur) |
5819 | 0 | parent->attrs = cur->next; |
5820 | 0 | else if (parent->nameClass == cur) |
5821 | 0 | parent->nameClass = cur->next; |
5822 | 0 | else { |
5823 | 0 | xmlRelaxNGDefinePtr content, last = NULL; |
5824 | 0 | content = parent->content; |
5825 | 0 | while (content != NULL) { |
5826 | 0 | if (content == cur) { |
5827 | 0 | if (last == NULL) { |
5828 | 0 | parent->content = cur->next; |
5829 | 0 | } else { |
5830 | 0 | last->next = cur->next; |
5831 | 0 | } |
5832 | 0 | break; |
5833 | 0 | } |
5834 | 0 | last = content; |
5835 | 0 | content = content->next; |
5836 | 0 | } |
5837 | 0 | } |
5838 | 0 | } else { |
5839 | 0 | cur->type = XML_RELAXNG_NOOP; |
5840 | 0 | prev = cur; |
5841 | 0 | } |
5842 | 0 | } |
5843 | 0 | return (prev); |
5844 | 0 | } |
5845 | | |
5846 | | /** |
5847 | | * Check for simplification of empty and notAllowed |
5848 | | * |
5849 | | * @param ctxt a Relax-NG parser context |
5850 | | * @param cur grammar children nodes |
5851 | | * @param parent parent |
5852 | | */ |
5853 | | static void |
5854 | | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
5855 | | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
5856 | 0 | { |
5857 | 0 | xmlRelaxNGDefinePtr prev = NULL; |
5858 | |
|
5859 | 0 | while (cur != NULL) { |
5860 | 0 | if ((cur->type == XML_RELAXNG_REF) || |
5861 | 0 | (cur->type == XML_RELAXNG_PARENTREF)) { |
5862 | 0 | if (cur->depth != -3) { |
5863 | 0 | cur->depth = -3; |
5864 | 0 | ctxt->def = cur; |
5865 | 0 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
5866 | 0 | ctxt->def = NULL; |
5867 | 0 | } |
5868 | 0 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
5869 | 0 | cur->parent = parent; |
5870 | 0 | if ((parent != NULL) && |
5871 | 0 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
5872 | 0 | (parent->type == XML_RELAXNG_LIST) || |
5873 | 0 | (parent->type == XML_RELAXNG_GROUP) || |
5874 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
5875 | 0 | (parent->type == XML_RELAXNG_ONEORMORE) || |
5876 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
5877 | 0 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
5878 | 0 | break; |
5879 | 0 | } |
5880 | 0 | if ((parent != NULL) && ((parent->type == XML_RELAXNG_CHOICE) || |
5881 | 0 | ((parent->type == XML_RELAXNG_DEF) && |
5882 | 0 | (ctxt->def != NULL && ctxt->def->parent != NULL) && (ctxt->def->parent->type == XML_RELAXNG_CHOICE)))) { |
5883 | 0 | if (parent->type == XML_RELAXNG_CHOICE) |
5884 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5885 | 0 | else if (ctxt->def->parent->type == XML_RELAXNG_CHOICE) { |
5886 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, ctxt->def, ctxt->def->parent, prev); |
5887 | 0 | } |
5888 | 0 | } else |
5889 | 0 | prev = cur; |
5890 | 0 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
5891 | 0 | cur->parent = parent; |
5892 | 0 | if ((parent != NULL) && |
5893 | 0 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
5894 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
5895 | 0 | parent->type = XML_RELAXNG_EMPTY; |
5896 | 0 | break; |
5897 | 0 | } |
5898 | 0 | if ((parent != NULL) && |
5899 | 0 | ((parent->type == XML_RELAXNG_GROUP) || |
5900 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
5901 | 0 | ((parent->type == XML_RELAXNG_DEF) && |
5902 | 0 | (ctxt->def != NULL && ctxt->def->parent != NULL) && |
5903 | 0 | (ctxt->def->parent->type == XML_RELAXNG_GROUP || |
5904 | 0 | ctxt->def->parent->type == XML_RELAXNG_INTERLEAVE)))) { |
5905 | 0 | if (parent->type == XML_RELAXNG_GROUP || parent->type == XML_RELAXNG_INTERLEAVE) |
5906 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5907 | 0 | else if (ctxt->def->parent->type == XML_RELAXNG_GROUP || ctxt->def->parent->type == XML_RELAXNG_INTERLEAVE) |
5908 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, ctxt->def, ctxt->def->parent, prev); |
5909 | 0 | } else |
5910 | 0 | prev = cur; |
5911 | 0 | } else { |
5912 | 0 | cur->parent = parent; |
5913 | 0 | if (cur->content != NULL) |
5914 | 0 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
5915 | 0 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
5916 | 0 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
5917 | 0 | if (cur->nameClass != NULL) |
5918 | 0 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
5919 | | /* |
5920 | | * On Elements, try to move attribute only generating rules on |
5921 | | * the attrs rules. |
5922 | | */ |
5923 | 0 | if (cur->type == XML_RELAXNG_ELEMENT) { |
5924 | 0 | int attronly; |
5925 | 0 | xmlRelaxNGDefinePtr tmp, pre; |
5926 | |
|
5927 | 0 | while (cur->content != NULL) { |
5928 | 0 | attronly = |
5929 | 0 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
5930 | 0 | if (attronly == 1) { |
5931 | | /* |
5932 | | * migrate cur->content to attrs |
5933 | | */ |
5934 | 0 | tmp = cur->content; |
5935 | 0 | cur->content = tmp->next; |
5936 | 0 | tmp->next = cur->attrs; |
5937 | 0 | cur->attrs = tmp; |
5938 | 0 | } else { |
5939 | | /* |
5940 | | * cur->content can generate elements or text |
5941 | | */ |
5942 | 0 | break; |
5943 | 0 | } |
5944 | 0 | } |
5945 | 0 | pre = cur->content; |
5946 | 0 | while ((pre != NULL) && (pre->next != NULL)) { |
5947 | 0 | tmp = pre->next; |
5948 | 0 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
5949 | 0 | if (attronly == 1) { |
5950 | | /* |
5951 | | * migrate tmp to attrs |
5952 | | * if this runs twice an infinite attrs->next loop can be created |
5953 | | */ |
5954 | 0 | pre->next = tmp->next; |
5955 | 0 | tmp->next = cur->attrs; |
5956 | 0 | cur->attrs = tmp; |
5957 | 0 | } else { |
5958 | 0 | pre = tmp; |
5959 | 0 | } |
5960 | 0 | } |
5961 | 0 | } |
5962 | | /* |
5963 | | * This may result in a simplification |
5964 | | */ |
5965 | 0 | if ((cur->type == XML_RELAXNG_GROUP) || |
5966 | 0 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
5967 | 0 | if (cur->content == NULL) |
5968 | 0 | cur->type = XML_RELAXNG_EMPTY; |
5969 | 0 | else if (cur->content->next == NULL) { |
5970 | 0 | if ((parent == NULL) && (prev == NULL)) { |
5971 | 0 | cur->type = XML_RELAXNG_NOOP; |
5972 | 0 | } else if (prev == NULL) { |
5973 | | /* |
5974 | | * this simplification may already have happened |
5975 | | * if this is done twice this leads to an infinite loop of attrs->next |
5976 | | */ |
5977 | 0 | if (parent->content != cur->content) { |
5978 | 0 | parent->content = cur->content; |
5979 | 0 | cur->content->next = cur->next; |
5980 | 0 | cur = cur->content; |
5981 | 0 | } |
5982 | 0 | } else { |
5983 | 0 | cur->content->next = cur->next; |
5984 | 0 | prev->next = cur->content; |
5985 | 0 | cur = cur->content; |
5986 | 0 | } |
5987 | 0 | } |
5988 | 0 | } |
5989 | | /* |
5990 | | * the current node may have been transformed back |
5991 | | */ |
5992 | 0 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
5993 | 0 | (cur->content != NULL) && |
5994 | 0 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
5995 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5996 | 0 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
5997 | 0 | if ((parent != NULL) && |
5998 | 0 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
5999 | 0 | (parent->type == XML_RELAXNG_LIST) || |
6000 | 0 | (parent->type == XML_RELAXNG_GROUP) || |
6001 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
6002 | 0 | (parent->type == XML_RELAXNG_ONEORMORE) || |
6003 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
6004 | 0 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
6005 | 0 | break; |
6006 | 0 | } |
6007 | 0 | if ((parent != NULL) && |
6008 | 0 | (parent->type == XML_RELAXNG_CHOICE)) { |
6009 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
6010 | 0 | } else |
6011 | 0 | prev = cur; |
6012 | 0 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
6013 | 0 | if ((parent != NULL) && |
6014 | 0 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
6015 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
6016 | 0 | parent->type = XML_RELAXNG_EMPTY; |
6017 | 0 | break; |
6018 | 0 | } |
6019 | 0 | if ((parent != NULL) && |
6020 | 0 | ((parent->type == XML_RELAXNG_GROUP) || |
6021 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
6022 | 0 | (parent->type == XML_RELAXNG_CHOICE))) { |
6023 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
6024 | 0 | } else |
6025 | 0 | prev = cur; |
6026 | 0 | } else { |
6027 | 0 | prev = cur; |
6028 | 0 | } |
6029 | 0 | } |
6030 | 0 | cur = cur->next; |
6031 | 0 | } |
6032 | 0 | } |
6033 | | |
6034 | | /** |
6035 | | * Try to group 2 content types |
6036 | | * |
6037 | | * @param ct1 the first content type |
6038 | | * @param ct2 the second content type |
6039 | | * @returns the content type |
6040 | | */ |
6041 | | static xmlRelaxNGContentType |
6042 | | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
6043 | | xmlRelaxNGContentType ct2) |
6044 | 0 | { |
6045 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
6046 | 0 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
6047 | 0 | return (XML_RELAXNG_CONTENT_ERROR); |
6048 | 0 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
6049 | 0 | return (ct2); |
6050 | 0 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
6051 | 0 | return (ct1); |
6052 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
6053 | 0 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
6054 | 0 | return (XML_RELAXNG_CONTENT_COMPLEX); |
6055 | 0 | return (XML_RELAXNG_CONTENT_ERROR); |
6056 | 0 | } |
6057 | | |
6058 | | /** |
6059 | | * Compute the max content-type |
6060 | | * |
6061 | | * @param ct1 the first content type |
6062 | | * @param ct2 the second content type |
6063 | | * @returns the content type |
6064 | | */ |
6065 | | static xmlRelaxNGContentType |
6066 | | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
6067 | | xmlRelaxNGContentType ct2) |
6068 | 0 | { |
6069 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
6070 | 0 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
6071 | 0 | return (XML_RELAXNG_CONTENT_ERROR); |
6072 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
6073 | 0 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
6074 | 0 | return (XML_RELAXNG_CONTENT_SIMPLE); |
6075 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
6076 | 0 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
6077 | 0 | return (XML_RELAXNG_CONTENT_COMPLEX); |
6078 | 0 | return (XML_RELAXNG_CONTENT_EMPTY); |
6079 | 0 | } |
6080 | | |
6081 | | /** |
6082 | | * Check for rules in section 7.1 and 7.2 |
6083 | | * |
6084 | | * @param ctxt a Relax-NG parser context |
6085 | | * @param cur the current definition |
6086 | | * @param flags some accumulated flags |
6087 | | * @param ptype the parent type |
6088 | | * @returns the content type of `cur` |
6089 | | */ |
6090 | | static xmlRelaxNGContentType |
6091 | | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
6092 | | xmlRelaxNGDefinePtr cur, int flags, |
6093 | | xmlRelaxNGType ptype) |
6094 | 0 | { |
6095 | 0 | int nflags; |
6096 | 0 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
6097 | |
|
6098 | 0 | while (cur != NULL) { |
6099 | 0 | ret = XML_RELAXNG_CONTENT_EMPTY; |
6100 | 0 | if ((cur->type == XML_RELAXNG_REF) || |
6101 | 0 | (cur->type == XML_RELAXNG_PARENTREF)) { |
6102 | | /* |
6103 | | * This should actually be caught by list//element(ref) at the |
6104 | | * element boundaries, c.f. Bug #159968 local refs are dropped |
6105 | | * in step 4.19. |
6106 | | */ |
6107 | | #if 0 |
6108 | | if (flags & XML_RELAXNG_IN_LIST) { |
6109 | | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
6110 | | "Found forbidden pattern list//ref\n", NULL, |
6111 | | NULL); |
6112 | | } |
6113 | | #endif |
6114 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6115 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
6116 | 0 | "Found forbidden pattern data/except//ref\n", |
6117 | 0 | NULL, NULL); |
6118 | 0 | } |
6119 | 0 | if (cur->content == NULL) { |
6120 | 0 | if (cur->type == XML_RELAXNG_PARENTREF) |
6121 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
6122 | 0 | "Internal found no define for parent refs\n", |
6123 | 0 | NULL, NULL); |
6124 | 0 | else |
6125 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
6126 | 0 | "Internal found no define for ref %s\n", |
6127 | 0 | (cur->name ? cur->name: BAD_CAST "null"), NULL); |
6128 | 0 | } |
6129 | 0 | if (cur->depth > -4) { |
6130 | 0 | cur->depth = -4; |
6131 | 0 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
6132 | 0 | flags, cur->type); |
6133 | 0 | cur->depth = ret - 15; |
6134 | 0 | } else if (cur->depth == -4) { |
6135 | 0 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
6136 | 0 | } else { |
6137 | 0 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
6138 | 0 | } |
6139 | 0 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
6140 | | /* |
6141 | | * The 7.3 Attribute derivation rule for groups is plugged there |
6142 | | */ |
6143 | 0 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
6144 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6145 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
6146 | 0 | "Found forbidden pattern data/except//element(ref)\n", |
6147 | 0 | NULL, NULL); |
6148 | 0 | } |
6149 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6150 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
6151 | 0 | "Found forbidden pattern list//element(ref)\n", |
6152 | 0 | NULL, NULL); |
6153 | 0 | } |
6154 | 0 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
6155 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
6156 | 0 | "Found forbidden pattern attribute//element(ref)\n", |
6157 | 0 | NULL, NULL); |
6158 | 0 | } |
6159 | 0 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
6160 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
6161 | 0 | "Found forbidden pattern attribute//element(ref)\n", |
6162 | 0 | NULL, NULL); |
6163 | 0 | } |
6164 | | /* |
6165 | | * reset since in the simple form elements are only child |
6166 | | * of grammar/define |
6167 | | */ |
6168 | 0 | nflags = 0; |
6169 | 0 | ret = |
6170 | 0 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
6171 | 0 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
6172 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
6173 | 0 | "Element %s attributes have a content type error\n", |
6174 | 0 | cur->name, NULL); |
6175 | 0 | } |
6176 | 0 | ret = |
6177 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6178 | 0 | cur->type); |
6179 | 0 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
6180 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
6181 | 0 | "Element %s has a content type error\n", |
6182 | 0 | cur->name, NULL); |
6183 | 0 | } else { |
6184 | 0 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
6185 | 0 | } |
6186 | 0 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
6187 | 0 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
6188 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
6189 | 0 | "Found forbidden pattern attribute//attribute\n", |
6190 | 0 | NULL, NULL); |
6191 | 0 | } |
6192 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6193 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
6194 | 0 | "Found forbidden pattern list//attribute\n", |
6195 | 0 | NULL, NULL); |
6196 | 0 | } |
6197 | 0 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
6198 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
6199 | 0 | "Found forbidden pattern oneOrMore//group//attribute\n", |
6200 | 0 | NULL, NULL); |
6201 | 0 | } |
6202 | 0 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
6203 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
6204 | 0 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
6205 | 0 | NULL, NULL); |
6206 | 0 | } |
6207 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6208 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
6209 | 0 | "Found forbidden pattern data/except//attribute\n", |
6210 | 0 | NULL, NULL); |
6211 | 0 | } |
6212 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6213 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
6214 | 0 | "Found forbidden pattern start//attribute\n", |
6215 | 0 | NULL, NULL); |
6216 | 0 | } |
6217 | 0 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
6218 | 0 | && cur->name == NULL |
6219 | | /* following is checking alternative name class readiness |
6220 | | in case it went the "choice" route */ |
6221 | 0 | && cur->nameClass == NULL) { |
6222 | 0 | if (cur->ns == NULL) { |
6223 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
6224 | 0 | "Found anyName attribute without oneOrMore ancestor\n", |
6225 | 0 | NULL, NULL); |
6226 | 0 | } else { |
6227 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
6228 | 0 | "Found nsName attribute without oneOrMore ancestor\n", |
6229 | 0 | NULL, NULL); |
6230 | 0 | } |
6231 | 0 | } |
6232 | 0 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
6233 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
6234 | 0 | ret = XML_RELAXNG_CONTENT_EMPTY; |
6235 | 0 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
6236 | 0 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
6237 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6238 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
6239 | 0 | "Found forbidden pattern data/except//oneOrMore\n", |
6240 | 0 | NULL, NULL); |
6241 | 0 | } |
6242 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6243 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
6244 | 0 | "Found forbidden pattern start//oneOrMore\n", |
6245 | 0 | NULL, NULL); |
6246 | 0 | } |
6247 | 0 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
6248 | 0 | ret = |
6249 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6250 | 0 | cur->type); |
6251 | 0 | ret = xmlRelaxNGGroupContentType(ret, ret); |
6252 | 0 | } else if (cur->type == XML_RELAXNG_LIST) { |
6253 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6254 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
6255 | 0 | "Found forbidden pattern list//list\n", NULL, |
6256 | 0 | NULL); |
6257 | 0 | } |
6258 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6259 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
6260 | 0 | "Found forbidden pattern data/except//list\n", |
6261 | 0 | NULL, NULL); |
6262 | 0 | } |
6263 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6264 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
6265 | 0 | "Found forbidden pattern start//list\n", NULL, |
6266 | 0 | NULL); |
6267 | 0 | } |
6268 | 0 | nflags = flags | XML_RELAXNG_IN_LIST; |
6269 | 0 | ret = |
6270 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6271 | 0 | cur->type); |
6272 | 0 | } else if (cur->type == XML_RELAXNG_GROUP) { |
6273 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6274 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
6275 | 0 | "Found forbidden pattern data/except//group\n", |
6276 | 0 | NULL, NULL); |
6277 | 0 | } |
6278 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6279 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
6280 | 0 | "Found forbidden pattern start//group\n", NULL, |
6281 | 0 | NULL); |
6282 | 0 | } |
6283 | 0 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
6284 | 0 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
6285 | 0 | else |
6286 | 0 | nflags = flags; |
6287 | 0 | ret = |
6288 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6289 | 0 | cur->type); |
6290 | | /* |
6291 | | * The 7.3 Attribute derivation rule for groups is plugged there |
6292 | | */ |
6293 | 0 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
6294 | 0 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
6295 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6296 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
6297 | 0 | "Found forbidden pattern list//interleave\n", |
6298 | 0 | NULL, NULL); |
6299 | 0 | } |
6300 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6301 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
6302 | 0 | "Found forbidden pattern data/except//interleave\n", |
6303 | 0 | NULL, NULL); |
6304 | 0 | } |
6305 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6306 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
6307 | 0 | "Found forbidden pattern start//interleave\n", |
6308 | 0 | NULL, NULL); |
6309 | 0 | } |
6310 | 0 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
6311 | 0 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
6312 | 0 | else |
6313 | 0 | nflags = flags; |
6314 | 0 | ret = |
6315 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6316 | 0 | cur->type); |
6317 | 0 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
6318 | 0 | if ((cur->parent != NULL) && |
6319 | 0 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
6320 | 0 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
6321 | 0 | else |
6322 | 0 | nflags = flags; |
6323 | 0 | ret = |
6324 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6325 | 0 | cur->type); |
6326 | 0 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
6327 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6328 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
6329 | 0 | "Found forbidden pattern start//data\n", NULL, |
6330 | 0 | NULL); |
6331 | 0 | } |
6332 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6333 | 0 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
6334 | 0 | } else if (cur->type == XML_RELAXNG_VALUE) { |
6335 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6336 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
6337 | 0 | "Found forbidden pattern start//value\n", NULL, |
6338 | 0 | NULL); |
6339 | 0 | } |
6340 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6341 | 0 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
6342 | 0 | } else if (cur->type == XML_RELAXNG_TEXT) { |
6343 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6344 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
6345 | 0 | "Found forbidden pattern list//text\n", NULL, |
6346 | 0 | NULL); |
6347 | 0 | } |
6348 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6349 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
6350 | 0 | "Found forbidden pattern data/except//text\n", |
6351 | 0 | NULL, NULL); |
6352 | 0 | } |
6353 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6354 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
6355 | 0 | "Found forbidden pattern start//text\n", NULL, |
6356 | 0 | NULL); |
6357 | 0 | } |
6358 | 0 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
6359 | 0 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
6360 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6361 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
6362 | 0 | "Found forbidden pattern data/except//empty\n", |
6363 | 0 | NULL, NULL); |
6364 | 0 | } |
6365 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6366 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
6367 | 0 | "Found forbidden pattern start//empty\n", NULL, |
6368 | 0 | NULL); |
6369 | 0 | } |
6370 | 0 | ret = XML_RELAXNG_CONTENT_EMPTY; |
6371 | 0 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
6372 | 0 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
6373 | 0 | ret = |
6374 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6375 | 0 | } else { |
6376 | 0 | ret = |
6377 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6378 | 0 | } |
6379 | 0 | cur = cur->next; |
6380 | 0 | if (ptype == XML_RELAXNG_GROUP) { |
6381 | 0 | val = xmlRelaxNGGroupContentType(val, ret); |
6382 | 0 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
6383 | | /* |
6384 | | * TODO: scan complain that tmp is never used, seems on purpose |
6385 | | * need double-checking |
6386 | | */ |
6387 | 0 | tmp = xmlRelaxNGGroupContentType(val, ret); |
6388 | 0 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
6389 | 0 | tmp = xmlRelaxNGMaxContentType(val, ret); |
6390 | 0 | } else if (ptype == XML_RELAXNG_CHOICE) { |
6391 | 0 | val = xmlRelaxNGMaxContentType(val, ret); |
6392 | 0 | } else if (ptype == XML_RELAXNG_LIST) { |
6393 | 0 | val = XML_RELAXNG_CONTENT_SIMPLE; |
6394 | 0 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
6395 | 0 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
6396 | 0 | val = XML_RELAXNG_CONTENT_ERROR; |
6397 | 0 | else |
6398 | 0 | val = XML_RELAXNG_CONTENT_SIMPLE; |
6399 | 0 | } else { |
6400 | 0 | val = xmlRelaxNGGroupContentType(val, ret); |
6401 | 0 | } |
6402 | |
|
6403 | 0 | } |
6404 | 0 | return (val); |
6405 | 0 | } |
6406 | | |
6407 | | /** |
6408 | | * parse a Relax-NG `<grammar>` node |
6409 | | * |
6410 | | * @param ctxt a Relax-NG parser context |
6411 | | * @param nodes grammar children nodes |
6412 | | * @returns the internal xmlRelaxNGGrammar built or |
6413 | | * NULL in case of error |
6414 | | */ |
6415 | | static xmlRelaxNGGrammarPtr |
6416 | | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
6417 | 0 | { |
6418 | 0 | xmlRelaxNGGrammarPtr ret, tmp, old; |
6419 | |
|
6420 | 0 | ret = xmlRelaxNGNewGrammar(ctxt); |
6421 | 0 | if (ret == NULL) |
6422 | 0 | return (NULL); |
6423 | | |
6424 | | /* |
6425 | | * Link the new grammar in the tree |
6426 | | */ |
6427 | 0 | ret->parent = ctxt->grammar; |
6428 | 0 | if (ctxt->grammar != NULL) { |
6429 | 0 | tmp = ctxt->grammar->children; |
6430 | 0 | if (tmp == NULL) { |
6431 | 0 | ctxt->grammar->children = ret; |
6432 | 0 | } else { |
6433 | 0 | while (tmp->next != NULL) |
6434 | 0 | tmp = tmp->next; |
6435 | 0 | tmp->next = ret; |
6436 | 0 | } |
6437 | 0 | } |
6438 | |
|
6439 | 0 | old = ctxt->grammar; |
6440 | 0 | ctxt->grammar = ret; |
6441 | 0 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
6442 | 0 | ctxt->grammar = ret; |
6443 | 0 | if (ctxt->grammar == NULL) { |
6444 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
6445 | 0 | "Failed to parse <grammar> content\n", NULL, NULL); |
6446 | 0 | } else if (ctxt->grammar->start == NULL) { |
6447 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
6448 | 0 | "Element <grammar> has no <start>\n", NULL, NULL); |
6449 | 0 | } |
6450 | | |
6451 | | /* |
6452 | | * Apply 4.17 merging rules to defines and starts |
6453 | | */ |
6454 | 0 | xmlRelaxNGCombineStart(ctxt, ret); |
6455 | 0 | if (ret->defs != NULL) { |
6456 | 0 | xmlHashScan(ret->defs, xmlRelaxNGCheckCombine, ctxt); |
6457 | 0 | } |
6458 | | |
6459 | | /* |
6460 | | * link together defines and refs in this grammar |
6461 | | */ |
6462 | 0 | if (ret->refs != NULL) { |
6463 | 0 | xmlHashScan(ret->refs, xmlRelaxNGCheckReference, ctxt); |
6464 | 0 | } |
6465 | | |
6466 | | |
6467 | | /* @@@@ */ |
6468 | |
|
6469 | 0 | ctxt->grammar = old; |
6470 | 0 | return (ret); |
6471 | 0 | } |
6472 | | |
6473 | | /** |
6474 | | * parse a Relax-NG definition resource and build an internal |
6475 | | * xmlRelaxNG structure which can be used to validate instances. |
6476 | | * |
6477 | | * @param ctxt a Relax-NG parser context |
6478 | | * @param node the root node of the RelaxNG schema |
6479 | | * @returns the internal XML RelaxNG structure built or |
6480 | | * NULL in case of error |
6481 | | */ |
6482 | | static xmlRelaxNGPtr |
6483 | | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
6484 | 0 | { |
6485 | 0 | xmlRelaxNGPtr schema = NULL; |
6486 | 0 | const xmlChar *olddefine; |
6487 | 0 | xmlRelaxNGGrammarPtr old; |
6488 | |
|
6489 | 0 | if ((ctxt == NULL) || (node == NULL)) |
6490 | 0 | return (NULL); |
6491 | | |
6492 | 0 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
6493 | 0 | if (schema == NULL) |
6494 | 0 | return (NULL); |
6495 | | |
6496 | 0 | olddefine = ctxt->define; |
6497 | 0 | ctxt->define = NULL; |
6498 | 0 | if (IS_RELAXNG(node, "grammar")) { |
6499 | 0 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
6500 | 0 | if (schema->topgrammar == NULL) { |
6501 | 0 | xmlRelaxNGFree(schema); |
6502 | 0 | return (NULL); |
6503 | 0 | } |
6504 | 0 | } else { |
6505 | 0 | xmlRelaxNGGrammarPtr tmp, ret; |
6506 | |
|
6507 | 0 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
6508 | 0 | if (schema->topgrammar == NULL) { |
6509 | 0 | xmlRelaxNGFree(schema); |
6510 | 0 | return (NULL); |
6511 | 0 | } |
6512 | | /* |
6513 | | * Link the new grammar in the tree |
6514 | | */ |
6515 | 0 | ret->parent = ctxt->grammar; |
6516 | 0 | if (ctxt->grammar != NULL) { |
6517 | 0 | tmp = ctxt->grammar->children; |
6518 | 0 | if (tmp == NULL) { |
6519 | 0 | ctxt->grammar->children = ret; |
6520 | 0 | } else { |
6521 | 0 | while (tmp->next != NULL) |
6522 | 0 | tmp = tmp->next; |
6523 | 0 | tmp->next = ret; |
6524 | 0 | } |
6525 | 0 | } |
6526 | 0 | old = ctxt->grammar; |
6527 | 0 | ctxt->grammar = ret; |
6528 | 0 | xmlRelaxNGParseStart(ctxt, node); |
6529 | 0 | if (old != NULL) |
6530 | 0 | ctxt->grammar = old; |
6531 | 0 | } |
6532 | 0 | ctxt->define = olddefine; |
6533 | 0 | if (schema->topgrammar->start != NULL) { |
6534 | 0 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
6535 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
6536 | 0 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
6537 | 0 | while ((schema->topgrammar->start != NULL) && |
6538 | 0 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
6539 | 0 | (schema->topgrammar->start->next != NULL)) |
6540 | 0 | schema->topgrammar->start = |
6541 | 0 | schema->topgrammar->start->content; |
6542 | 0 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
6543 | 0 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
6544 | 0 | } |
6545 | 0 | } |
6546 | |
|
6547 | 0 | return (schema); |
6548 | 0 | } |
6549 | | |
6550 | | /************************************************************************ |
6551 | | * * |
6552 | | * Reading RelaxNGs * |
6553 | | * * |
6554 | | ************************************************************************/ |
6555 | | |
6556 | | /** |
6557 | | * Create an XML RelaxNGs parse context for that file/resource expected |
6558 | | * to contain an XML RelaxNGs file. |
6559 | | * |
6560 | | * @param URL the location of the schema |
6561 | | * @returns the parser context or NULL in case of error |
6562 | | */ |
6563 | | xmlRelaxNGParserCtxt * |
6564 | | xmlRelaxNGNewParserCtxt(const char *URL) |
6565 | 0 | { |
6566 | 0 | xmlRelaxNGParserCtxtPtr ret; |
6567 | |
|
6568 | 0 | if (URL == NULL) |
6569 | 0 | return (NULL); |
6570 | | |
6571 | 0 | ret = |
6572 | 0 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
6573 | 0 | if (ret == NULL) { |
6574 | 0 | xmlRngPErrMemory(NULL); |
6575 | 0 | return (NULL); |
6576 | 0 | } |
6577 | 0 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
6578 | 0 | ret->URL = xmlStrdup((const xmlChar *) URL); |
6579 | 0 | return (ret); |
6580 | 0 | } |
6581 | | |
6582 | | /** |
6583 | | * Create an XML RelaxNGs parse context for that memory buffer expected |
6584 | | * to contain an XML RelaxNGs file. |
6585 | | * |
6586 | | * @param buffer a pointer to a char array containing the schemas |
6587 | | * @param size the size of the array |
6588 | | * @returns the parser context or NULL in case of error |
6589 | | */ |
6590 | | xmlRelaxNGParserCtxt * |
6591 | | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
6592 | 0 | { |
6593 | 0 | xmlRelaxNGParserCtxtPtr ret; |
6594 | |
|
6595 | 0 | if ((buffer == NULL) || (size <= 0)) |
6596 | 0 | return (NULL); |
6597 | | |
6598 | 0 | ret = |
6599 | 0 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
6600 | 0 | if (ret == NULL) { |
6601 | 0 | xmlRngPErrMemory(NULL); |
6602 | 0 | return (NULL); |
6603 | 0 | } |
6604 | 0 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
6605 | 0 | ret->buffer = buffer; |
6606 | 0 | ret->size = size; |
6607 | 0 | return (ret); |
6608 | 0 | } |
6609 | | |
6610 | | /** |
6611 | | * Create an XML RelaxNGs parser context for that document. |
6612 | | * Note: since the process of compiling a RelaxNG schemas modifies the |
6613 | | * document, the `doc` parameter is duplicated internally. |
6614 | | * |
6615 | | * @param doc a preparsed document tree |
6616 | | * @returns the parser context or NULL in case of error |
6617 | | */ |
6618 | | xmlRelaxNGParserCtxt * |
6619 | | xmlRelaxNGNewDocParserCtxt(xmlDoc *doc) |
6620 | 0 | { |
6621 | 0 | xmlRelaxNGParserCtxtPtr ret; |
6622 | 0 | xmlDocPtr copy; |
6623 | |
|
6624 | 0 | if (doc == NULL) |
6625 | 0 | return (NULL); |
6626 | 0 | copy = xmlCopyDoc(doc, 1); |
6627 | 0 | if (copy == NULL) |
6628 | 0 | return (NULL); |
6629 | | |
6630 | 0 | ret = |
6631 | 0 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
6632 | 0 | if (ret == NULL) { |
6633 | 0 | xmlRngPErrMemory(NULL); |
6634 | 0 | xmlFreeDoc(copy); |
6635 | 0 | return (NULL); |
6636 | 0 | } |
6637 | 0 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
6638 | 0 | ret->document = copy; |
6639 | 0 | ret->freedoc = 1; |
6640 | 0 | ret->userData = xmlGenericErrorContext; |
6641 | 0 | return (ret); |
6642 | 0 | } |
6643 | | |
6644 | | /** |
6645 | | * Free the resources associated to the schema parser context |
6646 | | * |
6647 | | * @param ctxt the schema parser context |
6648 | | */ |
6649 | | void |
6650 | | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxt *ctxt) |
6651 | 0 | { |
6652 | 0 | if (ctxt == NULL) |
6653 | 0 | return; |
6654 | 0 | if (ctxt->URL != NULL) |
6655 | 0 | xmlFree(ctxt->URL); |
6656 | 0 | if (ctxt->doc != NULL) |
6657 | 0 | xmlRelaxNGFreeDocument(ctxt->doc); |
6658 | 0 | if (ctxt->interleaves != NULL) |
6659 | 0 | xmlHashFree(ctxt->interleaves, NULL); |
6660 | 0 | if (ctxt->documents != NULL) |
6661 | 0 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
6662 | 0 | if (ctxt->includes != NULL) |
6663 | 0 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
6664 | 0 | if (ctxt->docTab != NULL) |
6665 | 0 | xmlFree(ctxt->docTab); |
6666 | 0 | if (ctxt->incTab != NULL) |
6667 | 0 | xmlFree(ctxt->incTab); |
6668 | 0 | if (ctxt->defTab != NULL) { |
6669 | 0 | int i; |
6670 | |
|
6671 | 0 | for (i = 0; i < ctxt->defNr; i++) |
6672 | 0 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
6673 | 0 | xmlFree(ctxt->defTab); |
6674 | 0 | } |
6675 | 0 | if ((ctxt->document != NULL) && (ctxt->freedoc)) |
6676 | 0 | xmlFreeDoc(ctxt->document); |
6677 | 0 | xmlFree(ctxt); |
6678 | 0 | } |
6679 | | |
6680 | | /** |
6681 | | * Removes the leading and ending spaces of the value |
6682 | | * The string is modified "in situ" |
6683 | | * |
6684 | | * @param value a value |
6685 | | */ |
6686 | | static void |
6687 | | xmlRelaxNGNormExtSpace(xmlChar * value) |
6688 | 0 | { |
6689 | 0 | xmlChar *start = value; |
6690 | 0 | xmlChar *cur = value; |
6691 | |
|
6692 | 0 | if (value == NULL) |
6693 | 0 | return; |
6694 | | |
6695 | 0 | while (IS_BLANK_CH(*cur)) |
6696 | 0 | cur++; |
6697 | 0 | if (cur == start) { |
6698 | 0 | do { |
6699 | 0 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
6700 | 0 | cur++; |
6701 | 0 | if (*cur == 0) |
6702 | 0 | return; |
6703 | 0 | start = cur; |
6704 | 0 | while (IS_BLANK_CH(*cur)) |
6705 | 0 | cur++; |
6706 | 0 | if (*cur == 0) { |
6707 | 0 | *start = 0; |
6708 | 0 | return; |
6709 | 0 | } |
6710 | 0 | } while (1); |
6711 | 0 | } else { |
6712 | 0 | do { |
6713 | 0 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
6714 | 0 | *start++ = *cur++; |
6715 | 0 | if (*cur == 0) { |
6716 | 0 | *start = 0; |
6717 | 0 | return; |
6718 | 0 | } |
6719 | | /* don't try to normalize the inner spaces */ |
6720 | 0 | while (IS_BLANK_CH(*cur)) |
6721 | 0 | cur++; |
6722 | 0 | if (*cur == 0) { |
6723 | 0 | *start = 0; |
6724 | 0 | return; |
6725 | 0 | } |
6726 | 0 | *start++ = *cur++; |
6727 | 0 | } while (1); |
6728 | 0 | } |
6729 | 0 | } |
6730 | | |
6731 | | /** |
6732 | | * Check all the attributes on the given node |
6733 | | * |
6734 | | * @param ctxt a Relax-NG parser context |
6735 | | * @param node a Relax-NG node |
6736 | | */ |
6737 | | static void |
6738 | | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
6739 | 0 | { |
6740 | 0 | xmlAttrPtr cur, next; |
6741 | |
|
6742 | 0 | cur = node->properties; |
6743 | 0 | while (cur != NULL) { |
6744 | 0 | next = cur->next; |
6745 | 0 | if ((cur->ns == NULL) || |
6746 | 0 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
6747 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
6748 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
6749 | 0 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
6750 | 0 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
6751 | 0 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
6752 | 0 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
6753 | 0 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
6754 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6755 | 0 | "Attribute %s is not allowed on %s\n", |
6756 | 0 | cur->name, node->name); |
6757 | 0 | } |
6758 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
6759 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
6760 | 0 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
6761 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6762 | 0 | "Attribute %s is not allowed on %s\n", |
6763 | 0 | cur->name, node->name); |
6764 | 0 | } |
6765 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
6766 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
6767 | 0 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
6768 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6769 | 0 | "Attribute %s is not allowed on %s\n", |
6770 | 0 | cur->name, node->name); |
6771 | 0 | } |
6772 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
6773 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
6774 | 0 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
6775 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6776 | 0 | "Attribute %s is not allowed on %s\n", |
6777 | 0 | cur->name, node->name); |
6778 | 0 | } |
6779 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
6780 | 0 | xmlChar *val; |
6781 | 0 | xmlURIPtr uri; |
6782 | |
|
6783 | 0 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
6784 | 0 | if (val != NULL) { |
6785 | 0 | if (val[0] != 0) { |
6786 | 0 | uri = xmlParseURI((const char *) val); |
6787 | 0 | if (uri == NULL) { |
6788 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
6789 | 0 | "Attribute %s contains invalid URI %s\n", |
6790 | 0 | cur->name, val); |
6791 | 0 | } else { |
6792 | 0 | if (uri->scheme == NULL) { |
6793 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
6794 | 0 | "Attribute %s URI %s is not absolute\n", |
6795 | 0 | cur->name, val); |
6796 | 0 | } |
6797 | 0 | if (uri->fragment != NULL) { |
6798 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
6799 | 0 | "Attribute %s URI %s has a fragment ID\n", |
6800 | 0 | cur->name, val); |
6801 | 0 | } |
6802 | 0 | xmlFreeURI(uri); |
6803 | 0 | } |
6804 | 0 | } |
6805 | 0 | xmlFree(val); |
6806 | 0 | } |
6807 | 0 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
6808 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
6809 | 0 | "Unknown attribute %s on %s\n", cur->name, |
6810 | 0 | node->name); |
6811 | 0 | } |
6812 | 0 | } |
6813 | 0 | cur = next; |
6814 | 0 | } |
6815 | 0 | } |
6816 | | |
6817 | | /** |
6818 | | * Cleanup the subtree from unwanted nodes for parsing, resolve |
6819 | | * Include and externalRef lookups. |
6820 | | * |
6821 | | * @param ctxt a Relax-NG parser context |
6822 | | * @param root an xmlNode subtree |
6823 | | */ |
6824 | | static void |
6825 | | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
6826 | 0 | { |
6827 | 0 | xmlNodePtr cur, delete; |
6828 | |
|
6829 | 0 | delete = NULL; |
6830 | 0 | cur = root; |
6831 | 0 | while (cur != NULL) { |
6832 | 0 | if (delete != NULL) { |
6833 | 0 | xmlUnlinkNode(delete); |
6834 | 0 | xmlFreeNode(delete); |
6835 | 0 | delete = NULL; |
6836 | 0 | } |
6837 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
6838 | | /* |
6839 | | * Simplification 4.1. Annotations |
6840 | | */ |
6841 | 0 | if ((cur->ns == NULL) || |
6842 | 0 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
6843 | 0 | if ((cur->parent != NULL) && |
6844 | 0 | (cur->parent->type == XML_ELEMENT_NODE) && |
6845 | 0 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
6846 | 0 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
6847 | 0 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
6848 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
6849 | 0 | "element %s doesn't allow foreign elements\n", |
6850 | 0 | cur->parent->name, NULL); |
6851 | 0 | } |
6852 | 0 | delete = cur; |
6853 | 0 | goto skip_children; |
6854 | 0 | } else { |
6855 | 0 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
6856 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
6857 | 0 | xmlChar *href, *ns, *base, *URL; |
6858 | 0 | xmlRelaxNGDocumentPtr docu; |
6859 | 0 | xmlNodePtr tmp; |
6860 | 0 | xmlURIPtr uri; |
6861 | |
|
6862 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
6863 | 0 | if (ns == NULL) { |
6864 | 0 | tmp = cur->parent; |
6865 | 0 | while ((tmp != NULL) && |
6866 | 0 | (tmp->type == XML_ELEMENT_NODE)) { |
6867 | 0 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
6868 | 0 | if (ns != NULL) |
6869 | 0 | break; |
6870 | 0 | tmp = tmp->parent; |
6871 | 0 | } |
6872 | 0 | } |
6873 | 0 | href = xmlGetProp(cur, BAD_CAST "href"); |
6874 | 0 | if (href == NULL) { |
6875 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
6876 | 0 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
6877 | 0 | NULL, NULL); |
6878 | 0 | if (ns != NULL) |
6879 | 0 | xmlFree(ns); |
6880 | 0 | delete = cur; |
6881 | 0 | goto skip_children; |
6882 | 0 | } |
6883 | 0 | uri = xmlParseURI((const char *) href); |
6884 | 0 | if (uri == NULL) { |
6885 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6886 | 0 | "Incorrect URI for externalRef %s\n", |
6887 | 0 | href, NULL); |
6888 | 0 | if (ns != NULL) |
6889 | 0 | xmlFree(ns); |
6890 | 0 | if (href != NULL) |
6891 | 0 | xmlFree(href); |
6892 | 0 | delete = cur; |
6893 | 0 | goto skip_children; |
6894 | 0 | } |
6895 | 0 | if (uri->fragment != NULL) { |
6896 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6897 | 0 | "Fragment forbidden in URI for externalRef %s\n", |
6898 | 0 | href, NULL); |
6899 | 0 | if (ns != NULL) |
6900 | 0 | xmlFree(ns); |
6901 | 0 | xmlFreeURI(uri); |
6902 | 0 | if (href != NULL) |
6903 | 0 | xmlFree(href); |
6904 | 0 | delete = cur; |
6905 | 0 | goto skip_children; |
6906 | 0 | } |
6907 | 0 | xmlFreeURI(uri); |
6908 | 0 | base = xmlNodeGetBase(cur->doc, cur); |
6909 | 0 | URL = xmlBuildURI(href, base); |
6910 | 0 | if (URL == NULL) { |
6911 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6912 | 0 | "Failed to compute URL for externalRef %s\n", |
6913 | 0 | href, NULL); |
6914 | 0 | if (ns != NULL) |
6915 | 0 | xmlFree(ns); |
6916 | 0 | if (href != NULL) |
6917 | 0 | xmlFree(href); |
6918 | 0 | if (base != NULL) |
6919 | 0 | xmlFree(base); |
6920 | 0 | delete = cur; |
6921 | 0 | goto skip_children; |
6922 | 0 | } |
6923 | 0 | if (href != NULL) |
6924 | 0 | xmlFree(href); |
6925 | 0 | if (base != NULL) |
6926 | 0 | xmlFree(base); |
6927 | 0 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
6928 | 0 | if (docu == NULL) { |
6929 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
6930 | 0 | "Failed to load externalRef %s\n", URL, |
6931 | 0 | NULL); |
6932 | 0 | if (ns != NULL) |
6933 | 0 | xmlFree(ns); |
6934 | 0 | xmlFree(URL); |
6935 | 0 | delete = cur; |
6936 | 0 | goto skip_children; |
6937 | 0 | } |
6938 | 0 | if (ns != NULL) |
6939 | 0 | xmlFree(ns); |
6940 | 0 | xmlFree(URL); |
6941 | 0 | cur->psvi = docu; |
6942 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
6943 | 0 | xmlChar *href, *ns, *base, *URL; |
6944 | 0 | xmlRelaxNGIncludePtr incl; |
6945 | 0 | xmlNodePtr tmp; |
6946 | |
|
6947 | 0 | href = xmlGetProp(cur, BAD_CAST "href"); |
6948 | 0 | if (href == NULL) { |
6949 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
6950 | 0 | "xmlRelaxNGParse: include has no href attribute\n", |
6951 | 0 | NULL, NULL); |
6952 | 0 | delete = cur; |
6953 | 0 | goto skip_children; |
6954 | 0 | } |
6955 | 0 | base = xmlNodeGetBase(cur->doc, cur); |
6956 | 0 | URL = xmlBuildURI(href, base); |
6957 | 0 | if (URL == NULL) { |
6958 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6959 | 0 | "Failed to compute URL for include %s\n", |
6960 | 0 | href, NULL); |
6961 | 0 | if (href != NULL) |
6962 | 0 | xmlFree(href); |
6963 | 0 | if (base != NULL) |
6964 | 0 | xmlFree(base); |
6965 | 0 | delete = cur; |
6966 | 0 | goto skip_children; |
6967 | 0 | } |
6968 | 0 | if (href != NULL) |
6969 | 0 | xmlFree(href); |
6970 | 0 | if (base != NULL) |
6971 | 0 | xmlFree(base); |
6972 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
6973 | 0 | if (ns == NULL) { |
6974 | 0 | tmp = cur->parent; |
6975 | 0 | while ((tmp != NULL) && |
6976 | 0 | (tmp->type == XML_ELEMENT_NODE)) { |
6977 | 0 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
6978 | 0 | if (ns != NULL) |
6979 | 0 | break; |
6980 | 0 | tmp = tmp->parent; |
6981 | 0 | } |
6982 | 0 | } |
6983 | 0 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
6984 | 0 | if (ns != NULL) |
6985 | 0 | xmlFree(ns); |
6986 | 0 | if (incl == NULL) { |
6987 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
6988 | 0 | "Failed to load include %s\n", URL, |
6989 | 0 | NULL); |
6990 | 0 | xmlFree(URL); |
6991 | 0 | delete = cur; |
6992 | 0 | goto skip_children; |
6993 | 0 | } |
6994 | 0 | xmlFree(URL); |
6995 | 0 | cur->psvi = incl; |
6996 | 0 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
6997 | 0 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
6998 | 0 | { |
6999 | 0 | xmlChar *name, *ns; |
7000 | 0 | xmlNodePtr text = NULL; |
7001 | | |
7002 | | /* |
7003 | | * Simplification 4.8. name attribute of element |
7004 | | * and attribute elements |
7005 | | */ |
7006 | 0 | name = xmlGetProp(cur, BAD_CAST "name"); |
7007 | 0 | if (name != NULL) { |
7008 | 0 | if (cur->children == NULL) { |
7009 | 0 | text = |
7010 | 0 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
7011 | 0 | name); |
7012 | 0 | } else { |
7013 | 0 | xmlNodePtr node; |
7014 | |
|
7015 | 0 | node = xmlNewDocNode(cur->doc, cur->ns, |
7016 | 0 | BAD_CAST "name", NULL); |
7017 | 0 | if (node != NULL) { |
7018 | 0 | xmlAddPrevSibling(cur->children, node); |
7019 | 0 | text = xmlNewDocText(node->doc, name); |
7020 | 0 | xmlAddChild(node, text); |
7021 | 0 | text = node; |
7022 | 0 | } |
7023 | 0 | } |
7024 | 0 | if (text == NULL) { |
7025 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
7026 | 0 | "Failed to create a name %s element\n", |
7027 | 0 | name, NULL); |
7028 | 0 | } |
7029 | 0 | xmlUnsetProp(cur, BAD_CAST "name"); |
7030 | 0 | xmlFree(name); |
7031 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
7032 | 0 | if (ns != NULL) { |
7033 | 0 | if (text != NULL) { |
7034 | 0 | xmlSetProp(text, BAD_CAST "ns", ns); |
7035 | | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
7036 | 0 | } |
7037 | 0 | xmlFree(ns); |
7038 | 0 | } else if (xmlStrEqual(cur->name, |
7039 | 0 | BAD_CAST "attribute")) { |
7040 | 0 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
7041 | 0 | } |
7042 | 0 | } |
7043 | 0 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
7044 | 0 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
7045 | 0 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
7046 | | /* |
7047 | | * Simplification 4.8. name attribute of element |
7048 | | * and attribute elements |
7049 | | */ |
7050 | 0 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
7051 | 0 | xmlNodePtr node; |
7052 | 0 | xmlChar *ns = NULL; |
7053 | |
|
7054 | 0 | node = cur->parent; |
7055 | 0 | while ((node != NULL) && |
7056 | 0 | (node->type == XML_ELEMENT_NODE)) { |
7057 | 0 | ns = xmlGetProp(node, BAD_CAST "ns"); |
7058 | 0 | if (ns != NULL) { |
7059 | 0 | break; |
7060 | 0 | } |
7061 | 0 | node = node->parent; |
7062 | 0 | } |
7063 | 0 | if (ns == NULL) { |
7064 | 0 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
7065 | 0 | } else { |
7066 | 0 | xmlSetProp(cur, BAD_CAST "ns", ns); |
7067 | 0 | xmlFree(ns); |
7068 | 0 | } |
7069 | 0 | } |
7070 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
7071 | 0 | xmlChar *name, *local, *prefix; |
7072 | | |
7073 | | /* |
7074 | | * Simplification: 4.10. QNames |
7075 | | */ |
7076 | 0 | name = xmlNodeGetContent(cur); |
7077 | 0 | if (name != NULL) { |
7078 | 0 | local = xmlSplitQName2(name, &prefix); |
7079 | 0 | if (local != NULL) { |
7080 | 0 | xmlNsPtr ns; |
7081 | |
|
7082 | 0 | ns = xmlSearchNs(cur->doc, cur, prefix); |
7083 | 0 | if (ns == NULL) { |
7084 | 0 | xmlRngPErr(ctxt, cur, |
7085 | 0 | XML_RNGP_PREFIX_UNDEFINED, |
7086 | 0 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
7087 | 0 | prefix, NULL); |
7088 | 0 | } else { |
7089 | 0 | xmlSetProp(cur, BAD_CAST "ns", |
7090 | 0 | ns->href); |
7091 | 0 | xmlNodeSetContent(cur, local); |
7092 | 0 | } |
7093 | 0 | xmlFree(local); |
7094 | 0 | xmlFree(prefix); |
7095 | 0 | } |
7096 | 0 | xmlFree(name); |
7097 | 0 | } |
7098 | 0 | } |
7099 | | /* |
7100 | | * 4.16 |
7101 | | */ |
7102 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
7103 | 0 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
7104 | 0 | xmlRngPErr(ctxt, cur, |
7105 | 0 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
7106 | 0 | "Found nsName/except//nsName forbidden construct\n", |
7107 | 0 | NULL, NULL); |
7108 | 0 | } |
7109 | 0 | } |
7110 | 0 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
7111 | 0 | (cur != root)) { |
7112 | 0 | int oldflags = ctxt->flags; |
7113 | | |
7114 | | /* |
7115 | | * 4.16 |
7116 | | */ |
7117 | 0 | if ((cur->parent != NULL) && |
7118 | 0 | (xmlStrEqual |
7119 | 0 | (cur->parent->name, BAD_CAST "anyName"))) { |
7120 | 0 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
7121 | 0 | xmlRelaxNGCleanupTree(ctxt, cur); |
7122 | 0 | ctxt->flags = oldflags; |
7123 | 0 | goto skip_children; |
7124 | 0 | } else if ((cur->parent != NULL) && |
7125 | 0 | (xmlStrEqual |
7126 | 0 | (cur->parent->name, BAD_CAST "nsName"))) { |
7127 | 0 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
7128 | 0 | xmlRelaxNGCleanupTree(ctxt, cur); |
7129 | 0 | ctxt->flags = oldflags; |
7130 | 0 | goto skip_children; |
7131 | 0 | } |
7132 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
7133 | | /* |
7134 | | * 4.16 |
7135 | | */ |
7136 | 0 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
7137 | 0 | xmlRngPErr(ctxt, cur, |
7138 | 0 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
7139 | 0 | "Found anyName/except//anyName forbidden construct\n", |
7140 | 0 | NULL, NULL); |
7141 | 0 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
7142 | 0 | xmlRngPErr(ctxt, cur, |
7143 | 0 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
7144 | 0 | "Found nsName/except//anyName forbidden construct\n", |
7145 | 0 | NULL, NULL); |
7146 | 0 | } |
7147 | 0 | } |
7148 | | /* |
7149 | | * This is not an else since "include" is transformed |
7150 | | * into a div |
7151 | | */ |
7152 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
7153 | 0 | xmlChar *ns; |
7154 | 0 | xmlNodePtr child, ins, tmp; |
7155 | | |
7156 | | /* |
7157 | | * implements rule 4.11 |
7158 | | */ |
7159 | |
|
7160 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
7161 | |
|
7162 | 0 | child = cur->children; |
7163 | 0 | ins = cur; |
7164 | 0 | while (child != NULL) { |
7165 | 0 | if (ns != NULL) { |
7166 | 0 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
7167 | 0 | xmlSetProp(child, BAD_CAST "ns", ns); |
7168 | 0 | } |
7169 | 0 | } |
7170 | 0 | tmp = child->next; |
7171 | 0 | xmlUnlinkNode(child); |
7172 | 0 | ins = xmlAddNextSibling(ins, child); |
7173 | 0 | child = tmp; |
7174 | 0 | } |
7175 | 0 | if (ns != NULL) |
7176 | 0 | xmlFree(ns); |
7177 | | /* |
7178 | | * Since we are about to delete cur, if its nsDef is non-NULL we |
7179 | | * need to preserve it (it contains the ns definitions for the |
7180 | | * children we just moved). We'll just stick it on to the end |
7181 | | * of cur->parent's list, since it's never going to be re-serialized |
7182 | | * (bug 143738). |
7183 | | */ |
7184 | 0 | if ((cur->nsDef != NULL) && (cur->parent != NULL)) { |
7185 | 0 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
7186 | 0 | while (parDef->next != NULL) |
7187 | 0 | parDef = parDef->next; |
7188 | 0 | parDef->next = cur->nsDef; |
7189 | 0 | cur->nsDef = NULL; |
7190 | 0 | } |
7191 | 0 | delete = cur; |
7192 | 0 | goto skip_children; |
7193 | 0 | } |
7194 | 0 | } |
7195 | 0 | } |
7196 | | /* |
7197 | | * Simplification 4.2 whitespaces |
7198 | | */ |
7199 | 0 | else if ((cur->type == XML_TEXT_NODE) || |
7200 | 0 | (cur->type == XML_CDATA_SECTION_NODE)) { |
7201 | 0 | if (IS_BLANK_NODE(cur)) { |
7202 | 0 | if ((cur->parent != NULL) && |
7203 | 0 | (cur->parent->type == XML_ELEMENT_NODE)) { |
7204 | 0 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
7205 | 0 | && |
7206 | 0 | (!xmlStrEqual |
7207 | 0 | (cur->parent->name, BAD_CAST "param"))) |
7208 | 0 | delete = cur; |
7209 | 0 | } else { |
7210 | 0 | delete = cur; |
7211 | 0 | goto skip_children; |
7212 | 0 | } |
7213 | 0 | } |
7214 | 0 | } else { |
7215 | 0 | delete = cur; |
7216 | 0 | goto skip_children; |
7217 | 0 | } |
7218 | | |
7219 | | /* |
7220 | | * Skip to next node |
7221 | | */ |
7222 | 0 | if (cur->children != NULL) { |
7223 | 0 | if ((cur->children->type != XML_ENTITY_DECL) && |
7224 | 0 | (cur->children->type != XML_ENTITY_REF_NODE) && |
7225 | 0 | (cur->children->type != XML_ENTITY_NODE)) { |
7226 | 0 | cur = cur->children; |
7227 | 0 | continue; |
7228 | 0 | } |
7229 | 0 | } |
7230 | 0 | skip_children: |
7231 | 0 | if (cur->next != NULL) { |
7232 | 0 | cur = cur->next; |
7233 | 0 | continue; |
7234 | 0 | } |
7235 | | |
7236 | 0 | do { |
7237 | 0 | cur = cur->parent; |
7238 | 0 | if (cur == NULL) |
7239 | 0 | break; |
7240 | 0 | if (cur == root) { |
7241 | 0 | cur = NULL; |
7242 | 0 | break; |
7243 | 0 | } |
7244 | 0 | if (cur->next != NULL) { |
7245 | 0 | cur = cur->next; |
7246 | 0 | break; |
7247 | 0 | } |
7248 | 0 | } while (cur != NULL); |
7249 | 0 | } |
7250 | 0 | if (delete != NULL) { |
7251 | 0 | xmlUnlinkNode(delete); |
7252 | 0 | xmlFreeNode(delete); |
7253 | 0 | delete = NULL; |
7254 | 0 | } |
7255 | 0 | } |
7256 | | |
7257 | | /** |
7258 | | * Cleanup the document from unwanted nodes for parsing, resolve |
7259 | | * Include and externalRef lookups. |
7260 | | * |
7261 | | * @param ctxt a Relax-NG parser context |
7262 | | * @param doc an xmldoc document pointer |
7263 | | * @returns the cleaned up document or NULL in case of error |
7264 | | */ |
7265 | | static xmlDocPtr |
7266 | | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
7267 | 0 | { |
7268 | 0 | xmlNodePtr root; |
7269 | | |
7270 | | /* |
7271 | | * Extract the root |
7272 | | */ |
7273 | 0 | root = xmlDocGetRootElement(doc); |
7274 | 0 | if (root == NULL) { |
7275 | 0 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
7276 | 0 | ctxt->URL, NULL); |
7277 | 0 | return (NULL); |
7278 | 0 | } |
7279 | 0 | xmlRelaxNGCleanupTree(ctxt, root); |
7280 | 0 | return (doc); |
7281 | 0 | } |
7282 | | |
7283 | | /** |
7284 | | * parse a schema definition resource and build an internal |
7285 | | * XML Schema structure which can be used to validate instances. |
7286 | | * |
7287 | | * @param ctxt a Relax-NG parser context |
7288 | | * @returns the internal XML RelaxNG structure built from the resource or |
7289 | | * NULL in case of error |
7290 | | */ |
7291 | | xmlRelaxNG * |
7292 | | xmlRelaxNGParse(xmlRelaxNGParserCtxt *ctxt) |
7293 | 0 | { |
7294 | 0 | xmlRelaxNGPtr ret = NULL; |
7295 | 0 | xmlDocPtr doc; |
7296 | 0 | xmlNodePtr root; |
7297 | |
|
7298 | 0 | const char *include_limit_env = getenv("RNG_INCLUDE_LIMIT"); |
7299 | |
|
7300 | 0 | xmlRelaxNGInitTypes(); |
7301 | |
|
7302 | 0 | if (ctxt == NULL) |
7303 | 0 | return (NULL); |
7304 | | |
7305 | 0 | if (ctxt->incLimit == 0) { |
7306 | 0 | ctxt->incLimit = _xmlRelaxNGIncludeLimit; |
7307 | 0 | if (include_limit_env != NULL) { |
7308 | 0 | char *strEnd; |
7309 | 0 | unsigned long val = 0; |
7310 | 0 | errno = 0; |
7311 | 0 | val = strtoul(include_limit_env, &strEnd, 10); |
7312 | 0 | if (errno != 0 || *strEnd != 0 || val > INT_MAX) { |
7313 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
7314 | 0 | "xmlRelaxNGParse: invalid RNG_INCLUDE_LIMIT %s\n", |
7315 | 0 | (const xmlChar*)include_limit_env, |
7316 | 0 | NULL); |
7317 | 0 | return(NULL); |
7318 | 0 | } |
7319 | 0 | if (val) |
7320 | 0 | ctxt->incLimit = val; |
7321 | 0 | } |
7322 | 0 | } |
7323 | | |
7324 | | /* |
7325 | | * First step is to parse the input document into an DOM/Infoset |
7326 | | */ |
7327 | 0 | if (ctxt->URL != NULL) { |
7328 | 0 | doc = xmlRelaxReadFile(ctxt, (const char *) ctxt->URL); |
7329 | 0 | if (doc == NULL) { |
7330 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
7331 | 0 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
7332 | 0 | NULL); |
7333 | 0 | return (NULL); |
7334 | 0 | } |
7335 | 0 | } else if (ctxt->buffer != NULL) { |
7336 | 0 | doc = xmlRelaxReadMemory(ctxt, ctxt->buffer, ctxt->size); |
7337 | 0 | if (doc == NULL) { |
7338 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
7339 | 0 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
7340 | 0 | NULL); |
7341 | 0 | return (NULL); |
7342 | 0 | } |
7343 | 0 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
7344 | 0 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
7345 | 0 | } else if (ctxt->document != NULL) { |
7346 | 0 | doc = ctxt->document; |
7347 | 0 | } else { |
7348 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
7349 | 0 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
7350 | 0 | return (NULL); |
7351 | 0 | } |
7352 | 0 | ctxt->document = doc; |
7353 | | |
7354 | | /* |
7355 | | * Some preprocessing of the document content |
7356 | | */ |
7357 | 0 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
7358 | 0 | if (doc == NULL) { |
7359 | 0 | xmlFreeDoc(ctxt->document); |
7360 | 0 | ctxt->document = NULL; |
7361 | 0 | return (NULL); |
7362 | 0 | } |
7363 | | |
7364 | | /* |
7365 | | * Then do the parsing for good |
7366 | | */ |
7367 | 0 | root = xmlDocGetRootElement(doc); |
7368 | 0 | if (root == NULL) { |
7369 | 0 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
7370 | 0 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
7371 | 0 | (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL); |
7372 | |
|
7373 | 0 | xmlFreeDoc(ctxt->document); |
7374 | 0 | ctxt->document = NULL; |
7375 | 0 | return (NULL); |
7376 | 0 | } |
7377 | 0 | ret = xmlRelaxNGParseDocument(ctxt, root); |
7378 | 0 | if (ret == NULL) { |
7379 | 0 | xmlFreeDoc(ctxt->document); |
7380 | 0 | ctxt->document = NULL; |
7381 | 0 | return (NULL); |
7382 | 0 | } |
7383 | | |
7384 | | /* |
7385 | | * Check the ref/defines links |
7386 | | */ |
7387 | | /* |
7388 | | * try to preprocess interleaves |
7389 | | */ |
7390 | 0 | if (ctxt->interleaves != NULL) { |
7391 | 0 | xmlHashScan(ctxt->interleaves, xmlRelaxNGComputeInterleaves, ctxt); |
7392 | 0 | } |
7393 | | |
7394 | | /* |
7395 | | * if there was a parsing error return NULL |
7396 | | */ |
7397 | 0 | if (ctxt->nbErrors > 0) { |
7398 | 0 | xmlRelaxNGFree(ret); |
7399 | 0 | ctxt->document = NULL; |
7400 | 0 | xmlFreeDoc(doc); |
7401 | 0 | return (NULL); |
7402 | 0 | } |
7403 | | |
7404 | | /* |
7405 | | * try to compile (parts of) the schemas |
7406 | | */ |
7407 | 0 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
7408 | 0 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
7409 | 0 | xmlRelaxNGDefinePtr def; |
7410 | |
|
7411 | 0 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
7412 | 0 | if (def != NULL) { |
7413 | 0 | def->type = XML_RELAXNG_START; |
7414 | 0 | def->content = ret->topgrammar->start; |
7415 | 0 | ret->topgrammar->start = def; |
7416 | 0 | } |
7417 | 0 | } |
7418 | 0 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
7419 | 0 | } |
7420 | | |
7421 | | /* |
7422 | | * Transfer the pointer for cleanup at the schema level. |
7423 | | */ |
7424 | 0 | ret->doc = doc; |
7425 | 0 | ctxt->document = NULL; |
7426 | 0 | ret->documents = ctxt->documents; |
7427 | 0 | ctxt->documents = NULL; |
7428 | |
|
7429 | 0 | ret->includes = ctxt->includes; |
7430 | 0 | ctxt->includes = NULL; |
7431 | 0 | ret->defNr = ctxt->defNr; |
7432 | 0 | ret->defTab = ctxt->defTab; |
7433 | 0 | ctxt->defTab = NULL; |
7434 | 0 | if (ctxt->idref == 1) |
7435 | 0 | ret->idref = 1; |
7436 | |
|
7437 | 0 | return (ret); |
7438 | 0 | } |
7439 | | |
7440 | | /** |
7441 | | * Set the callback functions used to handle errors for a validation context |
7442 | | * |
7443 | | * @deprecated Use #xmlRelaxNGSetParserStructuredErrors. |
7444 | | * |
7445 | | * @param ctxt a Relax-NG validation context |
7446 | | * @param err the error callback |
7447 | | * @param warn the warning callback |
7448 | | * @param ctx contextual data for the callbacks |
7449 | | */ |
7450 | | void |
7451 | | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxt *ctxt, |
7452 | | xmlRelaxNGValidityErrorFunc err, |
7453 | | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
7454 | 0 | { |
7455 | 0 | if (ctxt == NULL) |
7456 | 0 | return; |
7457 | 0 | ctxt->error = err; |
7458 | 0 | ctxt->warning = warn; |
7459 | 0 | ctxt->serror = NULL; |
7460 | 0 | ctxt->userData = ctx; |
7461 | 0 | } |
7462 | | |
7463 | | /** |
7464 | | * Get the callback information used to handle errors for a validation context |
7465 | | * |
7466 | | * @param ctxt a Relax-NG validation context |
7467 | | * @param err the error callback result |
7468 | | * @param warn the warning callback result |
7469 | | * @param ctx contextual data for the callbacks result |
7470 | | * @returns -1 in case of failure, 0 otherwise. |
7471 | | */ |
7472 | | int |
7473 | | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxt *ctxt, |
7474 | | xmlRelaxNGValidityErrorFunc * err, |
7475 | | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
7476 | 0 | { |
7477 | 0 | if (ctxt == NULL) |
7478 | 0 | return (-1); |
7479 | 0 | if (err != NULL) |
7480 | 0 | *err = ctxt->error; |
7481 | 0 | if (warn != NULL) |
7482 | 0 | *warn = ctxt->warning; |
7483 | 0 | if (ctx != NULL) |
7484 | 0 | *ctx = ctxt->userData; |
7485 | 0 | return (0); |
7486 | 0 | } |
7487 | | |
7488 | | /** |
7489 | | * Set the callback functions used to handle errors for a parsing context |
7490 | | * |
7491 | | * @param ctxt a Relax-NG parser context |
7492 | | * @param serror the error callback |
7493 | | * @param ctx contextual data for the callbacks |
7494 | | */ |
7495 | | void |
7496 | | xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxt *ctxt, |
7497 | | xmlStructuredErrorFunc serror, |
7498 | | void *ctx) |
7499 | 0 | { |
7500 | 0 | if (ctxt == NULL) |
7501 | 0 | return; |
7502 | 0 | ctxt->serror = serror; |
7503 | 0 | ctxt->error = NULL; |
7504 | 0 | ctxt->warning = NULL; |
7505 | 0 | ctxt->userData = ctx; |
7506 | 0 | } |
7507 | | |
7508 | | /** |
7509 | | * Set the callback function used to load external resources. |
7510 | | * |
7511 | | * @param ctxt a Relax-NG parser context |
7512 | | * @param loader the callback |
7513 | | * @param vctxt contextual data for the callbacks |
7514 | | */ |
7515 | | void |
7516 | | xmlRelaxNGSetResourceLoader(xmlRelaxNGParserCtxt *ctxt, |
7517 | 0 | xmlResourceLoader loader, void *vctxt) { |
7518 | 0 | if (ctxt == NULL) |
7519 | 0 | return; |
7520 | 0 | ctxt->resourceLoader = loader; |
7521 | 0 | ctxt->resourceCtxt = vctxt; |
7522 | 0 | } |
7523 | | |
7524 | | #ifdef LIBXML_DEBUG_ENABLED |
7525 | | |
7526 | | /************************************************************************ |
7527 | | * * |
7528 | | * Dump back a compiled form * |
7529 | | * * |
7530 | | ************************************************************************/ |
7531 | | static void xmlRelaxNGDumpDefine(FILE * output, |
7532 | | xmlRelaxNGDefinePtr define); |
7533 | | |
7534 | | /** |
7535 | | * Dump a RelaxNG structure back |
7536 | | * |
7537 | | * @param output the file output |
7538 | | * @param defines a list of define structures |
7539 | | */ |
7540 | | static void |
7541 | | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
7542 | | { |
7543 | | while (defines != NULL) { |
7544 | | xmlRelaxNGDumpDefine(output, defines); |
7545 | | defines = defines->next; |
7546 | | } |
7547 | | } |
7548 | | |
7549 | | /** |
7550 | | * Dump a RelaxNG structure back |
7551 | | * |
7552 | | * @param output the file output |
7553 | | * @param define a define structure |
7554 | | */ |
7555 | | static void |
7556 | | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
7557 | | { |
7558 | | if (define == NULL) |
7559 | | return; |
7560 | | switch (define->type) { |
7561 | | case XML_RELAXNG_EMPTY: |
7562 | | fprintf(output, "<empty/>\n"); |
7563 | | break; |
7564 | | case XML_RELAXNG_NOT_ALLOWED: |
7565 | | fprintf(output, "<notAllowed/>\n"); |
7566 | | break; |
7567 | | case XML_RELAXNG_TEXT: |
7568 | | fprintf(output, "<text/>\n"); |
7569 | | break; |
7570 | | case XML_RELAXNG_ELEMENT: |
7571 | | fprintf(output, "<element>\n"); |
7572 | | if (define->name != NULL) { |
7573 | | fprintf(output, "<name"); |
7574 | | if (define->ns != NULL) |
7575 | | fprintf(output, " ns=\"%s\"", define->ns); |
7576 | | fprintf(output, ">%s</name>\n", define->name); |
7577 | | } |
7578 | | xmlRelaxNGDumpDefines(output, define->attrs); |
7579 | | xmlRelaxNGDumpDefines(output, define->content); |
7580 | | fprintf(output, "</element>\n"); |
7581 | | break; |
7582 | | case XML_RELAXNG_LIST: |
7583 | | fprintf(output, "<list>\n"); |
7584 | | xmlRelaxNGDumpDefines(output, define->content); |
7585 | | fprintf(output, "</list>\n"); |
7586 | | break; |
7587 | | case XML_RELAXNG_ONEORMORE: |
7588 | | fprintf(output, "<oneOrMore>\n"); |
7589 | | xmlRelaxNGDumpDefines(output, define->content); |
7590 | | fprintf(output, "</oneOrMore>\n"); |
7591 | | break; |
7592 | | case XML_RELAXNG_ZEROORMORE: |
7593 | | fprintf(output, "<zeroOrMore>\n"); |
7594 | | xmlRelaxNGDumpDefines(output, define->content); |
7595 | | fprintf(output, "</zeroOrMore>\n"); |
7596 | | break; |
7597 | | case XML_RELAXNG_CHOICE: |
7598 | | fprintf(output, "<choice>\n"); |
7599 | | xmlRelaxNGDumpDefines(output, define->content); |
7600 | | fprintf(output, "</choice>\n"); |
7601 | | break; |
7602 | | case XML_RELAXNG_GROUP: |
7603 | | fprintf(output, "<group>\n"); |
7604 | | xmlRelaxNGDumpDefines(output, define->content); |
7605 | | fprintf(output, "</group>\n"); |
7606 | | break; |
7607 | | case XML_RELAXNG_INTERLEAVE: |
7608 | | fprintf(output, "<interleave>\n"); |
7609 | | xmlRelaxNGDumpDefines(output, define->content); |
7610 | | fprintf(output, "</interleave>\n"); |
7611 | | break; |
7612 | | case XML_RELAXNG_OPTIONAL: |
7613 | | fprintf(output, "<optional>\n"); |
7614 | | xmlRelaxNGDumpDefines(output, define->content); |
7615 | | fprintf(output, "</optional>\n"); |
7616 | | break; |
7617 | | case XML_RELAXNG_ATTRIBUTE: |
7618 | | fprintf(output, "<attribute>\n"); |
7619 | | xmlRelaxNGDumpDefines(output, define->content); |
7620 | | fprintf(output, "</attribute>\n"); |
7621 | | break; |
7622 | | case XML_RELAXNG_DEF: |
7623 | | fprintf(output, "<define"); |
7624 | | if (define->name != NULL) |
7625 | | fprintf(output, " name=\"%s\"", define->name); |
7626 | | fprintf(output, ">\n"); |
7627 | | xmlRelaxNGDumpDefines(output, define->content); |
7628 | | fprintf(output, "</define>\n"); |
7629 | | break; |
7630 | | case XML_RELAXNG_REF: |
7631 | | fprintf(output, "<ref"); |
7632 | | if (define->name != NULL) |
7633 | | fprintf(output, " name=\"%s\"", define->name); |
7634 | | fprintf(output, ">\n"); |
7635 | | xmlRelaxNGDumpDefines(output, define->content); |
7636 | | fprintf(output, "</ref>\n"); |
7637 | | break; |
7638 | | case XML_RELAXNG_PARENTREF: |
7639 | | fprintf(output, "<parentRef"); |
7640 | | if (define->name != NULL) |
7641 | | fprintf(output, " name=\"%s\"", define->name); |
7642 | | fprintf(output, ">\n"); |
7643 | | xmlRelaxNGDumpDefines(output, define->content); |
7644 | | fprintf(output, "</parentRef>\n"); |
7645 | | break; |
7646 | | case XML_RELAXNG_EXTERNALREF: |
7647 | | fprintf(output, "<externalRef>"); |
7648 | | xmlRelaxNGDumpDefines(output, define->content); |
7649 | | fprintf(output, "</externalRef>\n"); |
7650 | | break; |
7651 | | case XML_RELAXNG_DATATYPE: |
7652 | | case XML_RELAXNG_VALUE: |
7653 | | /* TODO */ |
7654 | | break; |
7655 | | case XML_RELAXNG_START: |
7656 | | case XML_RELAXNG_EXCEPT: |
7657 | | case XML_RELAXNG_PARAM: |
7658 | | /* TODO */ |
7659 | | break; |
7660 | | case XML_RELAXNG_NOOP: |
7661 | | xmlRelaxNGDumpDefines(output, define->content); |
7662 | | break; |
7663 | | } |
7664 | | } |
7665 | | |
7666 | | /** |
7667 | | * Dump a RelaxNG structure back |
7668 | | * |
7669 | | * @param output the file output |
7670 | | * @param grammar a grammar structure |
7671 | | * @param top is this a top grammar |
7672 | | */ |
7673 | | static void |
7674 | | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
7675 | | { |
7676 | | if (grammar == NULL) |
7677 | | return; |
7678 | | |
7679 | | fprintf(output, "<grammar"); |
7680 | | if (top) |
7681 | | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
7682 | | switch (grammar->combine) { |
7683 | | case XML_RELAXNG_COMBINE_UNDEFINED: |
7684 | | break; |
7685 | | case XML_RELAXNG_COMBINE_CHOICE: |
7686 | | fprintf(output, " combine=\"choice\""); |
7687 | | break; |
7688 | | case XML_RELAXNG_COMBINE_INTERLEAVE: |
7689 | | fprintf(output, " combine=\"interleave\""); |
7690 | | break; |
7691 | | default: |
7692 | | fprintf(output, " <!-- invalid combine value -->"); |
7693 | | } |
7694 | | fprintf(output, ">\n"); |
7695 | | if (grammar->start == NULL) { |
7696 | | fprintf(output, " <!-- grammar had no start -->"); |
7697 | | } else { |
7698 | | fprintf(output, "<start>\n"); |
7699 | | xmlRelaxNGDumpDefine(output, grammar->start); |
7700 | | fprintf(output, "</start>\n"); |
7701 | | } |
7702 | | /* TODO ? Dump the defines ? */ |
7703 | | fprintf(output, "</grammar>\n"); |
7704 | | } |
7705 | | |
7706 | | /** |
7707 | | * Dump a RelaxNG structure back |
7708 | | * |
7709 | | * @param output the file output |
7710 | | * @param schema a schema structure |
7711 | | */ |
7712 | | void |
7713 | | xmlRelaxNGDump(FILE * output, xmlRelaxNG *schema) |
7714 | | { |
7715 | | if (output == NULL) |
7716 | | return; |
7717 | | if (schema == NULL) { |
7718 | | fprintf(output, "RelaxNG empty or failed to compile\n"); |
7719 | | return; |
7720 | | } |
7721 | | fprintf(output, "RelaxNG: "); |
7722 | | if (schema->doc == NULL) { |
7723 | | fprintf(output, "no document\n"); |
7724 | | } else if (schema->doc->URL != NULL) { |
7725 | | fprintf(output, "%s\n", schema->doc->URL); |
7726 | | } else { |
7727 | | fprintf(output, "\n"); |
7728 | | } |
7729 | | if (schema->topgrammar == NULL) { |
7730 | | fprintf(output, "RelaxNG has no top grammar\n"); |
7731 | | return; |
7732 | | } |
7733 | | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
7734 | | } |
7735 | | #endif /* LIBXML_DEBUG_ENABLED */ |
7736 | | |
7737 | | #ifdef LIBXML_OUTPUT_ENABLED |
7738 | | /** |
7739 | | * Dump the transformed RelaxNG tree. |
7740 | | * |
7741 | | * @param output the file output |
7742 | | * @param schema a schema structure |
7743 | | */ |
7744 | | void |
7745 | | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNG *schema) |
7746 | 0 | { |
7747 | 0 | if (output == NULL) |
7748 | 0 | return; |
7749 | 0 | if (schema == NULL) { |
7750 | 0 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
7751 | 0 | return; |
7752 | 0 | } |
7753 | 0 | if (schema->doc == NULL) { |
7754 | 0 | fprintf(output, "no document\n"); |
7755 | 0 | } else { |
7756 | 0 | xmlDocDump(output, schema->doc); |
7757 | 0 | } |
7758 | 0 | } |
7759 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
7760 | | |
7761 | | /************************************************************************ |
7762 | | * * |
7763 | | * Validation of compiled content * |
7764 | | * * |
7765 | | ************************************************************************/ |
7766 | | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
7767 | | xmlRelaxNGDefinePtr define); |
7768 | | |
7769 | | /** |
7770 | | * Handle the callback and if needed validate the element children. |
7771 | | * |
7772 | | * @param exec the regular expression instance |
7773 | | * @param token the token which matched |
7774 | | * @param transdata callback data, the define for the subelement if available |
7775 | | * @param inputdata callback data, the Relax NG validation context |
7776 | | */ |
7777 | | static void |
7778 | | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
7779 | | const xmlChar * token, |
7780 | | void *transdata, void *inputdata) |
7781 | 0 | { |
7782 | 0 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
7783 | 0 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
7784 | 0 | int ret; |
7785 | |
|
7786 | 0 | if (ctxt == NULL) { |
7787 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7788 | 0 | "callback on %s missing context\n", token, NULL); |
7789 | 0 | return; |
7790 | 0 | } |
7791 | 0 | if (define == NULL) { |
7792 | 0 | if (token[0] == '#') |
7793 | 0 | return; |
7794 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7795 | 0 | "callback on %s missing define\n", token, NULL); |
7796 | 0 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
7797 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
7798 | 0 | return; |
7799 | 0 | } |
7800 | 0 | if (define->type != XML_RELAXNG_ELEMENT) { |
7801 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7802 | 0 | "callback on %s define is not element\n", token, NULL); |
7803 | 0 | if (ctxt->errNo == XML_RELAXNG_OK) |
7804 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
7805 | 0 | return; |
7806 | 0 | } |
7807 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
7808 | 0 | if (ret != 0) |
7809 | 0 | ctxt->perr = ret; |
7810 | 0 | } |
7811 | | |
7812 | | /** |
7813 | | * Validate the content model of an element or start using the regexp |
7814 | | * |
7815 | | * @param ctxt the RelaxNG validation context |
7816 | | * @param regexp the regular expression as compiled |
7817 | | * @param content list of children to test against the regexp |
7818 | | * @returns 0 in case of success, -1 in case of error. |
7819 | | */ |
7820 | | static int |
7821 | | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
7822 | | xmlRegexpPtr regexp, xmlNodePtr content) |
7823 | 0 | { |
7824 | 0 | xmlRegExecCtxtPtr exec; |
7825 | 0 | xmlNodePtr cur; |
7826 | 0 | int ret = 0; |
7827 | 0 | int oldperr; |
7828 | |
|
7829 | 0 | if ((ctxt == NULL) || (regexp == NULL)) |
7830 | 0 | return (-1); |
7831 | 0 | oldperr = ctxt->perr; |
7832 | 0 | exec = xmlRegNewExecCtxt(regexp, |
7833 | 0 | xmlRelaxNGValidateCompiledCallback, ctxt); |
7834 | 0 | ctxt->perr = 0; |
7835 | 0 | cur = content; |
7836 | 0 | while (cur != NULL) { |
7837 | 0 | ctxt->state->seq = cur; |
7838 | 0 | switch (cur->type) { |
7839 | 0 | case XML_TEXT_NODE: |
7840 | 0 | case XML_CDATA_SECTION_NODE: |
7841 | 0 | if (xmlIsBlankNode(cur)) |
7842 | 0 | break; |
7843 | 0 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
7844 | 0 | if (ret < 0) { |
7845 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
7846 | 0 | cur->parent->name); |
7847 | 0 | } |
7848 | 0 | break; |
7849 | 0 | case XML_ELEMENT_NODE: |
7850 | 0 | if (cur->ns != NULL) { |
7851 | 0 | ret = xmlRegExecPushString2(exec, cur->name, |
7852 | 0 | cur->ns->href, ctxt); |
7853 | 0 | } else { |
7854 | 0 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
7855 | 0 | } |
7856 | 0 | if (ret < 0) { |
7857 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
7858 | 0 | } |
7859 | 0 | break; |
7860 | 0 | default: |
7861 | 0 | break; |
7862 | 0 | } |
7863 | 0 | if (ret < 0) |
7864 | 0 | break; |
7865 | | /* |
7866 | | * Switch to next element |
7867 | | */ |
7868 | 0 | cur = cur->next; |
7869 | 0 | } |
7870 | 0 | ret = xmlRegExecPushString(exec, NULL, NULL); |
7871 | 0 | if (ret == 1) { |
7872 | 0 | ret = 0; |
7873 | 0 | ctxt->state->seq = NULL; |
7874 | 0 | } else if (ret == 0) { |
7875 | | /* |
7876 | | * TODO: get some of the names needed to exit the current state of exec |
7877 | | */ |
7878 | 0 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
7879 | 0 | ret = -1; |
7880 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
7881 | 0 | xmlRelaxNGDumpValidError(ctxt); |
7882 | 0 | } else { |
7883 | 0 | ret = -1; |
7884 | 0 | } |
7885 | 0 | xmlRegFreeExecCtxt(exec); |
7886 | | /* |
7887 | | * There might be content model errors outside of the pure |
7888 | | * regexp validation, e.g. for attribute values. |
7889 | | */ |
7890 | 0 | if ((ret == 0) && (ctxt->perr != 0)) { |
7891 | 0 | ret = ctxt->perr; |
7892 | 0 | } |
7893 | 0 | ctxt->perr = oldperr; |
7894 | 0 | return (ret); |
7895 | 0 | } |
7896 | | |
7897 | | /************************************************************************ |
7898 | | * * |
7899 | | * Progressive validation of when possible * |
7900 | | * * |
7901 | | ************************************************************************/ |
7902 | | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
7903 | | xmlRelaxNGDefinePtr defines); |
7904 | | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
7905 | | int dolog); |
7906 | | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
7907 | | |
7908 | | /** |
7909 | | * Push a new regexp for the current node content model on the stack |
7910 | | * |
7911 | | * @param ctxt the validation context |
7912 | | * @param exec the regexp runtime for the new content model |
7913 | | * @returns 0 in case of success and -1 in case of error. |
7914 | | */ |
7915 | | static int |
7916 | | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
7917 | 0 | { |
7918 | 0 | if (ctxt->elemTab == NULL) { |
7919 | 0 | ctxt->elemMax = 10; |
7920 | 0 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
7921 | 0 | sizeof |
7922 | 0 | (xmlRegExecCtxtPtr)); |
7923 | 0 | if (ctxt->elemTab == NULL) { |
7924 | 0 | xmlRngVErrMemory(ctxt); |
7925 | 0 | return (-1); |
7926 | 0 | } |
7927 | 0 | } |
7928 | 0 | if (ctxt->elemNr >= ctxt->elemMax) { |
7929 | 0 | ctxt->elemMax *= 2; |
7930 | 0 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
7931 | 0 | ctxt->elemMax * |
7932 | 0 | sizeof |
7933 | 0 | (xmlRegExecCtxtPtr)); |
7934 | 0 | if (ctxt->elemTab == NULL) { |
7935 | 0 | xmlRngVErrMemory(ctxt); |
7936 | 0 | return (-1); |
7937 | 0 | } |
7938 | 0 | } |
7939 | 0 | ctxt->elemTab[ctxt->elemNr++] = exec; |
7940 | 0 | ctxt->elem = exec; |
7941 | 0 | return (0); |
7942 | 0 | } |
7943 | | |
7944 | | /** |
7945 | | * Pop the regexp of the current node content model from the stack |
7946 | | * |
7947 | | * @param ctxt the validation context |
7948 | | * @returns the exec or NULL if empty |
7949 | | */ |
7950 | | static xmlRegExecCtxtPtr |
7951 | | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
7952 | 0 | { |
7953 | 0 | xmlRegExecCtxtPtr ret; |
7954 | |
|
7955 | 0 | if (ctxt->elemNr <= 0) |
7956 | 0 | return (NULL); |
7957 | 0 | ctxt->elemNr--; |
7958 | 0 | ret = ctxt->elemTab[ctxt->elemNr]; |
7959 | 0 | ctxt->elemTab[ctxt->elemNr] = NULL; |
7960 | 0 | if (ctxt->elemNr > 0) |
7961 | 0 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
7962 | 0 | else |
7963 | 0 | ctxt->elem = NULL; |
7964 | 0 | return (ret); |
7965 | 0 | } |
7966 | | |
7967 | | /** |
7968 | | * Handle the callback and if needed validate the element children. |
7969 | | * some of the in/out information are passed via the context in `inputdata`. |
7970 | | * |
7971 | | * @param exec the regular expression instance |
7972 | | * @param token the token which matched |
7973 | | * @param transdata callback data, the define for the subelement if available |
7974 | | * @param inputdata callback data, the Relax NG validation context |
7975 | | */ |
7976 | | static void |
7977 | | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
7978 | | ATTRIBUTE_UNUSED, |
7979 | | const xmlChar * token, |
7980 | | void *transdata, void *inputdata) |
7981 | 0 | { |
7982 | 0 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
7983 | 0 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
7984 | 0 | xmlRelaxNGValidStatePtr state, oldstate; |
7985 | 0 | xmlNodePtr node; |
7986 | 0 | int ret = 0, oldflags; |
7987 | |
|
7988 | 0 | if (ctxt == NULL) { |
7989 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7990 | 0 | "callback on %s missing context\n", token, NULL); |
7991 | 0 | return; |
7992 | 0 | } |
7993 | 0 | node = ctxt->pnode; |
7994 | 0 | ctxt->pstate = 1; |
7995 | 0 | if (define == NULL) { |
7996 | 0 | if (token[0] == '#') |
7997 | 0 | return; |
7998 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7999 | 0 | "callback on %s missing define\n", token, NULL); |
8000 | 0 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
8001 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
8002 | 0 | ctxt->pstate = -1; |
8003 | 0 | return; |
8004 | 0 | } |
8005 | 0 | if (define->type != XML_RELAXNG_ELEMENT) { |
8006 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
8007 | 0 | "callback on %s define is not element\n", token, NULL); |
8008 | 0 | if (ctxt->errNo == XML_RELAXNG_OK) |
8009 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
8010 | 0 | ctxt->pstate = -1; |
8011 | 0 | return; |
8012 | 0 | } |
8013 | 0 | if (node->type != XML_ELEMENT_NODE) { |
8014 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
8015 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
8016 | 0 | xmlRelaxNGDumpValidError(ctxt); |
8017 | 0 | ctxt->pstate = -1; |
8018 | 0 | return; |
8019 | 0 | } |
8020 | 0 | if (define->contModel == NULL) { |
8021 | | /* |
8022 | | * this node cannot be validated in a streamable fashion |
8023 | | */ |
8024 | 0 | ctxt->pstate = 0; |
8025 | 0 | ctxt->pdef = define; |
8026 | 0 | return; |
8027 | 0 | } |
8028 | 0 | exec = xmlRegNewExecCtxt(define->contModel, |
8029 | 0 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
8030 | 0 | if (exec == NULL) { |
8031 | 0 | ctxt->pstate = -1; |
8032 | 0 | return; |
8033 | 0 | } |
8034 | 0 | xmlRelaxNGElemPush(ctxt, exec); |
8035 | | |
8036 | | /* |
8037 | | * Validate the attributes part of the content. |
8038 | | */ |
8039 | 0 | state = xmlRelaxNGNewValidState(ctxt, node); |
8040 | 0 | if (state == NULL) { |
8041 | 0 | ctxt->pstate = -1; |
8042 | 0 | return; |
8043 | 0 | } |
8044 | 0 | oldstate = ctxt->state; |
8045 | 0 | ctxt->state = state; |
8046 | 0 | if (define->attrs != NULL) { |
8047 | 0 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
8048 | 0 | if (ret != 0) { |
8049 | 0 | ctxt->pstate = -1; |
8050 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
8051 | 0 | } |
8052 | 0 | } |
8053 | 0 | if (ctxt->state != NULL) { |
8054 | 0 | ctxt->state->seq = NULL; |
8055 | 0 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
8056 | 0 | if (ret != 0) { |
8057 | 0 | ctxt->pstate = -1; |
8058 | 0 | } |
8059 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
8060 | 0 | } else if (ctxt->states != NULL) { |
8061 | 0 | int tmp = -1, i; |
8062 | |
|
8063 | 0 | oldflags = ctxt->flags; |
8064 | |
|
8065 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
8066 | 0 | state = ctxt->states->tabState[i]; |
8067 | 0 | ctxt->state = state; |
8068 | 0 | ctxt->state->seq = NULL; |
8069 | |
|
8070 | 0 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
8071 | 0 | tmp = 0; |
8072 | 0 | break; |
8073 | 0 | } |
8074 | 0 | } |
8075 | 0 | if (tmp != 0) { |
8076 | | /* |
8077 | | * validation error, log the message for the "best" one |
8078 | | */ |
8079 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8080 | 0 | xmlRelaxNGLogBestError(ctxt); |
8081 | 0 | } |
8082 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
8083 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
8084 | 0 | } |
8085 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
8086 | 0 | ctxt->states = NULL; |
8087 | 0 | if ((ret == 0) && (tmp == -1)) |
8088 | 0 | ctxt->pstate = -1; |
8089 | 0 | ctxt->flags = oldflags; |
8090 | 0 | } |
8091 | 0 | if (ctxt->pstate == -1) { |
8092 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
8093 | 0 | xmlRelaxNGDumpValidError(ctxt); |
8094 | 0 | } |
8095 | 0 | } |
8096 | 0 | ctxt->state = oldstate; |
8097 | 0 | } |
8098 | | |
8099 | | /** |
8100 | | * Push a new element start on the RelaxNG validation stack. |
8101 | | * |
8102 | | * @param ctxt the validation context |
8103 | | * @param doc a document instance |
8104 | | * @param elem an element instance |
8105 | | * @returns 1 if no validation problem was found or 0 if validating the |
8106 | | * element requires a full node, and -1 in case of error. |
8107 | | */ |
8108 | | int |
8109 | | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxt *ctxt, |
8110 | | xmlDoc *doc ATTRIBUTE_UNUSED, |
8111 | | xmlNode *elem) |
8112 | 0 | { |
8113 | 0 | int ret = 1; |
8114 | 0 | xmlNodePtr prevPNode; |
8115 | 0 | int prevPState; |
8116 | |
|
8117 | 0 | if ((ctxt == NULL) || (elem == NULL)) |
8118 | 0 | return (-1); |
8119 | | |
8120 | 0 | if (ctxt->elem == 0) { |
8121 | 0 | xmlRelaxNGPtr schema; |
8122 | 0 | xmlRelaxNGGrammarPtr grammar; |
8123 | 0 | xmlRegExecCtxtPtr exec; |
8124 | 0 | xmlRelaxNGDefinePtr define; |
8125 | |
|
8126 | 0 | schema = ctxt->schema; |
8127 | 0 | if (schema == NULL) { |
8128 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
8129 | 0 | return (-1); |
8130 | 0 | } |
8131 | 0 | grammar = schema->topgrammar; |
8132 | 0 | if ((grammar == NULL) || (grammar->start == NULL)) { |
8133 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
8134 | 0 | return (-1); |
8135 | 0 | } |
8136 | 0 | define = grammar->start; |
8137 | 0 | if (define->contModel == NULL) { |
8138 | 0 | ctxt->pdef = define; |
8139 | 0 | return (0); |
8140 | 0 | } |
8141 | 0 | exec = xmlRegNewExecCtxt(define->contModel, |
8142 | 0 | xmlRelaxNGValidateProgressiveCallback, |
8143 | 0 | ctxt); |
8144 | 0 | if (exec == NULL) { |
8145 | 0 | return (-1); |
8146 | 0 | } |
8147 | 0 | xmlRelaxNGElemPush(ctxt, exec); |
8148 | 0 | } |
8149 | 0 | prevPNode = ctxt->pnode; |
8150 | 0 | prevPState = ctxt->pstate; |
8151 | |
|
8152 | 0 | ctxt->pnode = elem; |
8153 | 0 | ctxt->pstate = 0; |
8154 | 0 | if (elem->ns != NULL) { |
8155 | 0 | ret = |
8156 | 0 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
8157 | 0 | ctxt); |
8158 | 0 | } else { |
8159 | 0 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
8160 | 0 | } |
8161 | 0 | if (ret < 0) { |
8162 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
8163 | 0 | goto Recovery; |
8164 | 0 | } else { |
8165 | 0 | if (ctxt->pstate == 0) |
8166 | 0 | ret = 0; |
8167 | 0 | else if (ctxt->pstate < 0) { |
8168 | 0 | ret = -1; |
8169 | 0 | goto Recovery; |
8170 | 0 | } |
8171 | 0 | else |
8172 | 0 | ret = 1; |
8173 | 0 | } |
8174 | 0 | return (ret); |
8175 | | |
8176 | 0 | Recovery: |
8177 | 0 | ctxt->pnode = prevPNode; |
8178 | 0 | ctxt->pstate = prevPState; |
8179 | 0 | return (ret); |
8180 | 0 | } |
8181 | | |
8182 | | /** |
8183 | | * check the CData parsed for validation in the current stack |
8184 | | * |
8185 | | * @param ctxt the RelaxNG validation context |
8186 | | * @param data some character data read |
8187 | | * @param len the length of the data |
8188 | | * @returns 1 if no validation problem was found or -1 otherwise |
8189 | | */ |
8190 | | int |
8191 | | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxt *ctxt, |
8192 | | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
8193 | 0 | { |
8194 | 0 | int ret = 1; |
8195 | |
|
8196 | 0 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
8197 | 0 | return (-1); |
8198 | | |
8199 | 0 | while (*data != 0) { |
8200 | 0 | if (!IS_BLANK_CH(*data)) |
8201 | 0 | break; |
8202 | 0 | data++; |
8203 | 0 | } |
8204 | 0 | if (*data == 0) |
8205 | 0 | return (1); |
8206 | | |
8207 | 0 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
8208 | 0 | if (ret < 0) { |
8209 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
8210 | |
|
8211 | 0 | return (-1); |
8212 | 0 | } |
8213 | 0 | return (1); |
8214 | 0 | } |
8215 | | |
8216 | | /** |
8217 | | * Pop the element end from the RelaxNG validation stack. |
8218 | | * |
8219 | | * @param ctxt the RelaxNG validation context |
8220 | | * @param doc a document instance |
8221 | | * @param elem an element instance |
8222 | | * @returns 1 if no validation problem was found or 0 otherwise |
8223 | | */ |
8224 | | int |
8225 | | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxt *ctxt, |
8226 | | xmlDoc *doc ATTRIBUTE_UNUSED, |
8227 | | xmlNode *elem) |
8228 | 0 | { |
8229 | 0 | int ret; |
8230 | 0 | xmlRegExecCtxtPtr exec; |
8231 | |
|
8232 | 0 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
8233 | 0 | return (-1); |
8234 | | /* |
8235 | | * verify that we reached a terminal state of the content model. |
8236 | | */ |
8237 | 0 | exec = xmlRelaxNGElemPop(ctxt); |
8238 | 0 | ret = xmlRegExecPushString(exec, NULL, NULL); |
8239 | 0 | if (ret == 0) { |
8240 | | /* |
8241 | | * TODO: get some of the names needed to exit the current state of exec |
8242 | | */ |
8243 | 0 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
8244 | 0 | ret = -1; |
8245 | 0 | } else if (ret < 0) { |
8246 | 0 | ret = -1; |
8247 | 0 | } else { |
8248 | 0 | ret = 1; |
8249 | 0 | } |
8250 | 0 | xmlRegFreeExecCtxt(exec); |
8251 | 0 | return (ret); |
8252 | 0 | } |
8253 | | |
8254 | | /** |
8255 | | * Validate a full subtree when #xmlRelaxNGValidatePushElement returned |
8256 | | * 0 and the content of the node has been expanded. |
8257 | | * |
8258 | | * @param ctxt the validation context |
8259 | | * @param doc a document instance |
8260 | | * @param elem an element instance |
8261 | | * @returns 1 if no validation problem was found or -1 in case of error. |
8262 | | */ |
8263 | | int |
8264 | | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxt *ctxt, |
8265 | | xmlDoc *doc ATTRIBUTE_UNUSED, |
8266 | | xmlNode *elem) |
8267 | 0 | { |
8268 | 0 | int ret; |
8269 | 0 | xmlRelaxNGValidStatePtr state; |
8270 | |
|
8271 | 0 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
8272 | 0 | return (-1); |
8273 | 0 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
8274 | 0 | if (state == NULL) { |
8275 | 0 | return (-1); |
8276 | 0 | } |
8277 | 0 | state->seq = elem; |
8278 | 0 | ctxt->state = state; |
8279 | 0 | ctxt->errNo = XML_RELAXNG_OK; |
8280 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
8281 | 0 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
8282 | 0 | ret = -1; |
8283 | 0 | else |
8284 | 0 | ret = 1; |
8285 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
8286 | 0 | ctxt->state = NULL; |
8287 | 0 | return (ret); |
8288 | 0 | } |
8289 | | |
8290 | | /** |
8291 | | * Clear errors in the context, allowing to recover |
8292 | | * from errors during streaming validation and continue it |
8293 | | * |
8294 | | * @remarks it doesn's reset the last internal libxml2 error |
8295 | | * @param ctxt the validation context |
8296 | | */ |
8297 | | void |
8298 | | xmlRelaxNGValidCtxtClearErrors(xmlRelaxNGValidCtxt* ctxt) |
8299 | 0 | { |
8300 | 0 | xmlRegExecClearErrors(ctxt->elem); |
8301 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8302 | 0 | ctxt->err = NULL; |
8303 | 0 | ctxt->nbErrors = 0; |
8304 | 0 | ctxt->errNo = XML_RELAXNG_OK; |
8305 | 0 | } |
8306 | | |
8307 | | /************************************************************************ |
8308 | | * * |
8309 | | * Generic interpreted validation implementation * |
8310 | | * * |
8311 | | ************************************************************************/ |
8312 | | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
8313 | | xmlRelaxNGDefinePtr define); |
8314 | | |
8315 | | /** |
8316 | | * Skip ignorable nodes in that context |
8317 | | * |
8318 | | * @param ctxt a schema validation context |
8319 | | * @param node the top node. |
8320 | | * @returns the new sibling or NULL in case of error. |
8321 | | */ |
8322 | | static xmlNodePtr |
8323 | | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
8324 | | xmlNodePtr node) |
8325 | 0 | { |
8326 | | /* |
8327 | | * TODO complete and handle entities |
8328 | | */ |
8329 | 0 | while ((node != NULL) && |
8330 | 0 | ((node->type == XML_COMMENT_NODE) || |
8331 | 0 | (node->type == XML_PI_NODE) || |
8332 | 0 | (node->type == XML_XINCLUDE_START) || |
8333 | 0 | (node->type == XML_XINCLUDE_END) || |
8334 | 0 | (((node->type == XML_TEXT_NODE) || |
8335 | 0 | (node->type == XML_CDATA_SECTION_NODE)) && |
8336 | 0 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
8337 | 0 | (IS_BLANK_NODE(node)))))) { |
8338 | 0 | node = node->next; |
8339 | 0 | } |
8340 | 0 | return (node); |
8341 | 0 | } |
8342 | | |
8343 | | /** |
8344 | | * Implements the normalizeWhiteSpace( s ) function from |
8345 | | * section 6.2.9 of the spec |
8346 | | * |
8347 | | * @param ctxt a schema validation context |
8348 | | * @param str the string to normalize |
8349 | | * @returns the new string or NULL in case of error. |
8350 | | */ |
8351 | | static xmlChar * |
8352 | | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
8353 | 0 | { |
8354 | 0 | xmlChar *ret, *p; |
8355 | 0 | const xmlChar *tmp; |
8356 | 0 | int len; |
8357 | |
|
8358 | 0 | if (str == NULL) |
8359 | 0 | return (NULL); |
8360 | 0 | tmp = str; |
8361 | 0 | while (*tmp != 0) |
8362 | 0 | tmp++; |
8363 | 0 | len = tmp - str; |
8364 | |
|
8365 | 0 | ret = xmlMalloc(len + 1); |
8366 | 0 | if (ret == NULL) { |
8367 | 0 | xmlRngVErrMemory(ctxt); |
8368 | 0 | return (NULL); |
8369 | 0 | } |
8370 | 0 | p = ret; |
8371 | 0 | while (IS_BLANK_CH(*str)) |
8372 | 0 | str++; |
8373 | 0 | while (*str != 0) { |
8374 | 0 | if (IS_BLANK_CH(*str)) { |
8375 | 0 | while (IS_BLANK_CH(*str)) |
8376 | 0 | str++; |
8377 | 0 | if (*str == 0) |
8378 | 0 | break; |
8379 | 0 | *p++ = ' '; |
8380 | 0 | } else |
8381 | 0 | *p++ = *str++; |
8382 | 0 | } |
8383 | 0 | *p = 0; |
8384 | 0 | return (ret); |
8385 | 0 | } |
8386 | | |
8387 | | /** |
8388 | | * Validate the given value against the datatype |
8389 | | * |
8390 | | * @param ctxt a Relax-NG validation context |
8391 | | * @param value the string value |
8392 | | * @param define the datatype definition |
8393 | | * @param node the node |
8394 | | * @returns 0 if the validation succeeded or an error code. |
8395 | | */ |
8396 | | static int |
8397 | | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
8398 | | const xmlChar * value, |
8399 | | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
8400 | 0 | { |
8401 | 0 | int ret, tmp; |
8402 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
8403 | 0 | void *result = NULL; |
8404 | 0 | xmlRelaxNGDefinePtr cur; |
8405 | |
|
8406 | 0 | if ((define == NULL) || (define->data == NULL)) { |
8407 | 0 | return (-1); |
8408 | 0 | } |
8409 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
8410 | 0 | if (lib->check != NULL) { |
8411 | 0 | if ((define->attrs != NULL) && |
8412 | 0 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
8413 | 0 | ret = |
8414 | 0 | lib->check(lib->data, define->name, value, &result, node); |
8415 | 0 | } else { |
8416 | 0 | ret = lib->check(lib->data, define->name, value, NULL, node); |
8417 | 0 | } |
8418 | 0 | } else |
8419 | 0 | ret = -1; |
8420 | 0 | if (ret < 0) { |
8421 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
8422 | 0 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
8423 | 0 | lib->freef(lib->data, result); |
8424 | 0 | return (-1); |
8425 | 0 | } else if (ret == 1) { |
8426 | 0 | ret = 0; |
8427 | 0 | } else if (ret == 2) { |
8428 | 0 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
8429 | 0 | } else { |
8430 | 0 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
8431 | 0 | ret = -1; |
8432 | 0 | } |
8433 | 0 | cur = define->attrs; |
8434 | 0 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
8435 | 0 | if (lib->facet != NULL) { |
8436 | 0 | tmp = lib->facet(lib->data, define->name, cur->name, |
8437 | 0 | cur->value, value, result); |
8438 | 0 | if (tmp != 0) |
8439 | 0 | ret = -1; |
8440 | 0 | } |
8441 | 0 | cur = cur->next; |
8442 | 0 | } |
8443 | 0 | if ((ret == 0) && (define->content != NULL)) { |
8444 | 0 | const xmlChar *oldvalue, *oldendvalue; |
8445 | |
|
8446 | 0 | oldvalue = ctxt->state->value; |
8447 | 0 | oldendvalue = ctxt->state->endvalue; |
8448 | 0 | ctxt->state->value = (xmlChar *) value; |
8449 | 0 | ctxt->state->endvalue = NULL; |
8450 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
8451 | 0 | ctxt->state->value = (xmlChar *) oldvalue; |
8452 | 0 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
8453 | 0 | } |
8454 | 0 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
8455 | 0 | lib->freef(lib->data, result); |
8456 | 0 | return (ret); |
8457 | 0 | } |
8458 | | |
8459 | | /** |
8460 | | * Skip to the next value when validating within a list |
8461 | | * |
8462 | | * @param ctxt a Relax-NG validation context |
8463 | | * @returns 0 if the operation succeeded or an error code. |
8464 | | */ |
8465 | | static int |
8466 | | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
8467 | 0 | { |
8468 | 0 | xmlChar *cur; |
8469 | |
|
8470 | 0 | cur = ctxt->state->value; |
8471 | 0 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
8472 | 0 | ctxt->state->value = NULL; |
8473 | 0 | ctxt->state->endvalue = NULL; |
8474 | 0 | return (0); |
8475 | 0 | } |
8476 | 0 | while (*cur != 0) |
8477 | 0 | cur++; |
8478 | 0 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
8479 | 0 | cur++; |
8480 | 0 | if (cur == ctxt->state->endvalue) |
8481 | 0 | ctxt->state->value = NULL; |
8482 | 0 | else |
8483 | 0 | ctxt->state->value = cur; |
8484 | 0 | return (0); |
8485 | 0 | } |
8486 | | |
8487 | | /** |
8488 | | * Validate the given set of definitions for the current value |
8489 | | * |
8490 | | * @param ctxt a Relax-NG validation context |
8491 | | * @param defines the list of definitions to verify |
8492 | | * @returns 0 if the validation succeeded or an error code. |
8493 | | */ |
8494 | | static int |
8495 | | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
8496 | | xmlRelaxNGDefinePtr defines) |
8497 | 0 | { |
8498 | 0 | int ret = 0; |
8499 | |
|
8500 | 0 | while (defines != NULL) { |
8501 | 0 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
8502 | 0 | if (ret != 0) |
8503 | 0 | break; |
8504 | 0 | defines = defines->next; |
8505 | 0 | } |
8506 | 0 | return (ret); |
8507 | 0 | } |
8508 | | |
8509 | | /** |
8510 | | * Validate the given definition for the current value |
8511 | | * |
8512 | | * @param ctxt a Relax-NG validation context |
8513 | | * @param define the definition to verify |
8514 | | * @returns 0 if the validation succeeded or an error code. |
8515 | | */ |
8516 | | static int |
8517 | | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
8518 | | xmlRelaxNGDefinePtr define) |
8519 | 0 | { |
8520 | 0 | int ret = 0, oldflags; |
8521 | 0 | xmlChar *value; |
8522 | |
|
8523 | 0 | value = ctxt->state->value; |
8524 | 0 | switch (define->type) { |
8525 | 0 | case XML_RELAXNG_EMPTY:{ |
8526 | 0 | if ((value != NULL) && (value[0] != 0)) { |
8527 | 0 | int idx = 0; |
8528 | |
|
8529 | 0 | while (IS_BLANK_CH(value[idx])) |
8530 | 0 | idx++; |
8531 | 0 | if (value[idx] != 0) |
8532 | 0 | ret = -1; |
8533 | 0 | } |
8534 | 0 | break; |
8535 | 0 | } |
8536 | 0 | case XML_RELAXNG_TEXT: |
8537 | 0 | break; |
8538 | 0 | case XML_RELAXNG_VALUE:{ |
8539 | 0 | if (!xmlStrEqual(value, define->value)) { |
8540 | 0 | if (define->name != NULL) { |
8541 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
8542 | |
|
8543 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
8544 | 0 | if ((lib != NULL) && (lib->comp != NULL)) { |
8545 | 0 | ret = lib->comp(lib->data, define->name, |
8546 | 0 | define->value, define->node, |
8547 | 0 | (void *) define->attrs, |
8548 | 0 | value, ctxt->state->node); |
8549 | 0 | } else |
8550 | 0 | ret = -1; |
8551 | 0 | if (ret < 0) { |
8552 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
8553 | 0 | define->name); |
8554 | 0 | return (-1); |
8555 | 0 | } else if (ret == 1) { |
8556 | 0 | ret = 0; |
8557 | 0 | } else { |
8558 | 0 | ret = -1; |
8559 | 0 | } |
8560 | 0 | } else { |
8561 | 0 | xmlChar *nval, *nvalue; |
8562 | | |
8563 | | /* |
8564 | | * TODO: trivial optimizations are possible by |
8565 | | * computing at compile-time |
8566 | | */ |
8567 | 0 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
8568 | 0 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
8569 | |
|
8570 | 0 | if ((nval == NULL) || (nvalue == NULL) || |
8571 | 0 | (!xmlStrEqual(nval, nvalue))) |
8572 | 0 | ret = -1; |
8573 | 0 | if (nval != NULL) |
8574 | 0 | xmlFree(nval); |
8575 | 0 | if (nvalue != NULL) |
8576 | 0 | xmlFree(nvalue); |
8577 | 0 | } |
8578 | 0 | } |
8579 | 0 | if (ret == 0) |
8580 | 0 | xmlRelaxNGNextValue(ctxt); |
8581 | 0 | break; |
8582 | 0 | } |
8583 | 0 | case XML_RELAXNG_DATATYPE:{ |
8584 | 0 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
8585 | 0 | ctxt->state->seq); |
8586 | 0 | if (ret == 0) |
8587 | 0 | xmlRelaxNGNextValue(ctxt); |
8588 | |
|
8589 | 0 | break; |
8590 | 0 | } |
8591 | 0 | case XML_RELAXNG_CHOICE:{ |
8592 | 0 | xmlRelaxNGDefinePtr list = define->content; |
8593 | 0 | xmlChar *oldvalue; |
8594 | |
|
8595 | 0 | oldflags = ctxt->flags; |
8596 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8597 | |
|
8598 | 0 | oldvalue = ctxt->state->value; |
8599 | 0 | while (list != NULL) { |
8600 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8601 | 0 | if (ret == 0) { |
8602 | 0 | break; |
8603 | 0 | } |
8604 | 0 | ctxt->state->value = oldvalue; |
8605 | 0 | list = list->next; |
8606 | 0 | } |
8607 | 0 | ctxt->flags = oldflags; |
8608 | 0 | if (ret != 0) { |
8609 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
8610 | 0 | xmlRelaxNGDumpValidError(ctxt); |
8611 | 0 | } else { |
8612 | 0 | if (ctxt->errNr > 0) |
8613 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8614 | 0 | } |
8615 | 0 | break; |
8616 | 0 | } |
8617 | 0 | case XML_RELAXNG_LIST:{ |
8618 | 0 | xmlRelaxNGDefinePtr list = define->content; |
8619 | 0 | xmlChar *oldvalue, *oldend, *val, *cur; |
8620 | |
|
8621 | 0 | oldvalue = ctxt->state->value; |
8622 | 0 | oldend = ctxt->state->endvalue; |
8623 | |
|
8624 | 0 | val = xmlStrdup(oldvalue); |
8625 | 0 | if (val == NULL) { |
8626 | 0 | val = xmlStrdup(BAD_CAST ""); |
8627 | 0 | } |
8628 | 0 | if (val == NULL) { |
8629 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
8630 | 0 | return (-1); |
8631 | 0 | } |
8632 | 0 | cur = val; |
8633 | 0 | while (*cur != 0) { |
8634 | 0 | if (IS_BLANK_CH(*cur)) { |
8635 | 0 | *cur = 0; |
8636 | 0 | cur++; |
8637 | 0 | while (IS_BLANK_CH(*cur)) |
8638 | 0 | *cur++ = 0; |
8639 | 0 | } else |
8640 | 0 | cur++; |
8641 | 0 | } |
8642 | 0 | ctxt->state->endvalue = cur; |
8643 | 0 | cur = val; |
8644 | 0 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
8645 | 0 | cur++; |
8646 | |
|
8647 | 0 | ctxt->state->value = cur; |
8648 | |
|
8649 | 0 | while (list != NULL) { |
8650 | 0 | if (ctxt->state->value == ctxt->state->endvalue) |
8651 | 0 | ctxt->state->value = NULL; |
8652 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8653 | 0 | if (ret != 0) { |
8654 | 0 | break; |
8655 | 0 | } |
8656 | 0 | list = list->next; |
8657 | 0 | } |
8658 | |
|
8659 | 0 | if ((ret == 0) && (ctxt->state->value != NULL) && |
8660 | 0 | (ctxt->state->value != ctxt->state->endvalue)) { |
8661 | 0 | VALID_ERR2P(XML_RELAXNG_ERR_LISTEXTRA, |
8662 | 0 | ctxt->state->value); |
8663 | 0 | ret = -1; |
8664 | 0 | } |
8665 | 0 | xmlFree(val); |
8666 | 0 | ctxt->state->value = oldvalue; |
8667 | 0 | ctxt->state->endvalue = oldend; |
8668 | 0 | break; |
8669 | 0 | } |
8670 | 0 | case XML_RELAXNG_ONEORMORE: |
8671 | 0 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
8672 | 0 | if (ret != 0) { |
8673 | 0 | break; |
8674 | 0 | } |
8675 | | /* Falls through. */ |
8676 | 0 | case XML_RELAXNG_ZEROORMORE:{ |
8677 | 0 | xmlChar *cur, *temp; |
8678 | |
|
8679 | 0 | if ((ctxt->state->value == NULL) || |
8680 | 0 | (*ctxt->state->value == 0)) { |
8681 | 0 | ret = 0; |
8682 | 0 | break; |
8683 | 0 | } |
8684 | 0 | oldflags = ctxt->flags; |
8685 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8686 | 0 | cur = ctxt->state->value; |
8687 | 0 | temp = NULL; |
8688 | 0 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
8689 | 0 | (temp != cur)) { |
8690 | 0 | temp = cur; |
8691 | 0 | ret = |
8692 | 0 | xmlRelaxNGValidateValueList(ctxt, define->content); |
8693 | 0 | if (ret != 0) { |
8694 | 0 | ctxt->state->value = temp; |
8695 | 0 | ret = 0; |
8696 | 0 | break; |
8697 | 0 | } |
8698 | 0 | cur = ctxt->state->value; |
8699 | 0 | } |
8700 | 0 | ctxt->flags = oldflags; |
8701 | 0 | if (ctxt->errNr > 0) |
8702 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8703 | 0 | break; |
8704 | 0 | } |
8705 | 0 | case XML_RELAXNG_OPTIONAL:{ |
8706 | 0 | xmlChar *temp; |
8707 | |
|
8708 | 0 | if ((ctxt->state->value == NULL) || |
8709 | 0 | (*ctxt->state->value == 0)) { |
8710 | 0 | ret = 0; |
8711 | 0 | break; |
8712 | 0 | } |
8713 | 0 | oldflags = ctxt->flags; |
8714 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8715 | 0 | temp = ctxt->state->value; |
8716 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
8717 | 0 | ctxt->flags = oldflags; |
8718 | 0 | if (ret != 0) { |
8719 | 0 | ctxt->state->value = temp; |
8720 | 0 | if (ctxt->errNr > 0) |
8721 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8722 | 0 | ret = 0; |
8723 | 0 | break; |
8724 | 0 | } |
8725 | 0 | if (ctxt->errNr > 0) |
8726 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8727 | 0 | break; |
8728 | 0 | } |
8729 | 0 | case XML_RELAXNG_EXCEPT:{ |
8730 | 0 | xmlRelaxNGDefinePtr list; |
8731 | |
|
8732 | 0 | list = define->content; |
8733 | 0 | while (list != NULL) { |
8734 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8735 | 0 | if (ret == 0) { |
8736 | 0 | ret = -1; |
8737 | 0 | break; |
8738 | 0 | } else |
8739 | 0 | ret = 0; |
8740 | 0 | list = list->next; |
8741 | 0 | } |
8742 | 0 | break; |
8743 | 0 | } |
8744 | 0 | case XML_RELAXNG_DEF: |
8745 | 0 | case XML_RELAXNG_GROUP:{ |
8746 | 0 | xmlRelaxNGDefinePtr list; |
8747 | |
|
8748 | 0 | list = define->content; |
8749 | 0 | while (list != NULL) { |
8750 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8751 | 0 | if (ret != 0) { |
8752 | 0 | ret = -1; |
8753 | 0 | break; |
8754 | 0 | } else |
8755 | 0 | ret = 0; |
8756 | 0 | list = list->next; |
8757 | 0 | } |
8758 | 0 | break; |
8759 | 0 | } |
8760 | 0 | case XML_RELAXNG_REF: |
8761 | 0 | case XML_RELAXNG_PARENTREF: |
8762 | 0 | if (define->content == NULL) { |
8763 | 0 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
8764 | 0 | ret = -1; |
8765 | 0 | } else { |
8766 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
8767 | 0 | } |
8768 | 0 | break; |
8769 | 0 | default: |
8770 | | /* TODO */ |
8771 | 0 | ret = -1; |
8772 | 0 | } |
8773 | 0 | return (ret); |
8774 | 0 | } |
8775 | | |
8776 | | /** |
8777 | | * Validate the given definitions for the current value |
8778 | | * |
8779 | | * @param ctxt a Relax-NG validation context |
8780 | | * @param defines the list of definitions to verify |
8781 | | * @returns 0 if the validation succeeded or an error code. |
8782 | | */ |
8783 | | static int |
8784 | | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
8785 | | xmlRelaxNGDefinePtr defines) |
8786 | 0 | { |
8787 | 0 | int ret = 0; |
8788 | |
|
8789 | 0 | while (defines != NULL) { |
8790 | 0 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
8791 | 0 | if (ret != 0) |
8792 | 0 | break; |
8793 | 0 | defines = defines->next; |
8794 | 0 | } |
8795 | 0 | return (ret); |
8796 | 0 | } |
8797 | | |
8798 | | /** |
8799 | | * Check if the attribute matches the definition nameClass |
8800 | | * |
8801 | | * @param ctxt a Relax-NG validation context |
8802 | | * @param define the definition to check |
8803 | | * @param prop the attribute |
8804 | | * @returns 1 if the attribute matches, 0 if no, or -1 in case of error |
8805 | | */ |
8806 | | static int |
8807 | | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
8808 | | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
8809 | 0 | { |
8810 | 0 | int ret; |
8811 | |
|
8812 | 0 | if (define->name != NULL) { |
8813 | 0 | if (!xmlStrEqual(define->name, prop->name)) |
8814 | 0 | return (0); |
8815 | 0 | } |
8816 | 0 | if (define->ns != NULL) { |
8817 | 0 | if (define->ns[0] == 0) { |
8818 | 0 | if (prop->ns != NULL) |
8819 | 0 | return (0); |
8820 | 0 | } else { |
8821 | 0 | if ((prop->ns == NULL) || |
8822 | 0 | (!xmlStrEqual(define->ns, prop->ns->href))) |
8823 | 0 | return (0); |
8824 | 0 | } |
8825 | 0 | } |
8826 | 0 | if (define->nameClass == NULL) |
8827 | 0 | return (1); |
8828 | 0 | define = define->nameClass; |
8829 | 0 | if (define->type == XML_RELAXNG_EXCEPT) { |
8830 | 0 | xmlRelaxNGDefinePtr list; |
8831 | |
|
8832 | 0 | list = define->content; |
8833 | 0 | while (list != NULL) { |
8834 | 0 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
8835 | 0 | if (ret == 1) |
8836 | 0 | return (0); |
8837 | 0 | if (ret < 0) |
8838 | 0 | return (ret); |
8839 | 0 | list = list->next; |
8840 | 0 | } |
8841 | 0 | } else if (define->type == XML_RELAXNG_CHOICE) { |
8842 | 0 | xmlRelaxNGDefinePtr list; |
8843 | |
|
8844 | 0 | list = define->nameClass; |
8845 | 0 | while (list != NULL) { |
8846 | 0 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
8847 | 0 | if (ret == 1) |
8848 | 0 | return (1); |
8849 | 0 | if (ret < 0) |
8850 | 0 | return (ret); |
8851 | 0 | list = list->next; |
8852 | 0 | } |
8853 | 0 | return (0); |
8854 | 0 | } else { |
8855 | | /* TODO */ |
8856 | 0 | return (0); |
8857 | 0 | } |
8858 | 0 | return (1); |
8859 | 0 | } |
8860 | | |
8861 | | /** |
8862 | | * Validate the given attribute definition for that node |
8863 | | * |
8864 | | * @param ctxt a Relax-NG validation context |
8865 | | * @param define the definition to verify |
8866 | | * @returns 0 if the validation succeeded or an error code. |
8867 | | */ |
8868 | | static int |
8869 | | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
8870 | | xmlRelaxNGDefinePtr define) |
8871 | 0 | { |
8872 | 0 | int ret = 0, i; |
8873 | 0 | xmlChar *value, *oldvalue; |
8874 | 0 | xmlAttrPtr prop = NULL, tmp; |
8875 | 0 | xmlNodePtr oldseq; |
8876 | |
|
8877 | 0 | if (ctxt->state->nbAttrLeft <= 0) |
8878 | 0 | return (-1); |
8879 | 0 | if (define->name != NULL) { |
8880 | 0 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
8881 | 0 | tmp = ctxt->state->attrs[i]; |
8882 | 0 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
8883 | 0 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
8884 | 0 | (tmp->ns == NULL)) || |
8885 | 0 | ((tmp->ns != NULL) && |
8886 | 0 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
8887 | 0 | prop = tmp; |
8888 | 0 | break; |
8889 | 0 | } |
8890 | 0 | } |
8891 | 0 | } |
8892 | 0 | if (prop != NULL) { |
8893 | 0 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
8894 | 0 | oldvalue = ctxt->state->value; |
8895 | 0 | oldseq = ctxt->state->seq; |
8896 | 0 | ctxt->state->seq = (xmlNodePtr) prop; |
8897 | 0 | ctxt->state->value = value; |
8898 | 0 | ctxt->state->endvalue = NULL; |
8899 | 0 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
8900 | 0 | if (ctxt->state->value != NULL) |
8901 | 0 | value = ctxt->state->value; |
8902 | 0 | if (value != NULL) |
8903 | 0 | xmlFree(value); |
8904 | 0 | ctxt->state->value = oldvalue; |
8905 | 0 | ctxt->state->seq = oldseq; |
8906 | 0 | if (ret == 0) { |
8907 | | /* |
8908 | | * flag the attribute as processed |
8909 | | */ |
8910 | 0 | ctxt->state->attrs[i] = NULL; |
8911 | 0 | ctxt->state->nbAttrLeft--; |
8912 | 0 | } |
8913 | 0 | } else { |
8914 | 0 | ret = -1; |
8915 | 0 | } |
8916 | 0 | } else { |
8917 | 0 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
8918 | 0 | tmp = ctxt->state->attrs[i]; |
8919 | 0 | if ((tmp != NULL) && |
8920 | 0 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
8921 | 0 | prop = tmp; |
8922 | 0 | break; |
8923 | 0 | } |
8924 | 0 | } |
8925 | 0 | if (prop != NULL) { |
8926 | 0 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
8927 | 0 | oldvalue = ctxt->state->value; |
8928 | 0 | oldseq = ctxt->state->seq; |
8929 | 0 | ctxt->state->seq = (xmlNodePtr) prop; |
8930 | 0 | ctxt->state->value = value; |
8931 | 0 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
8932 | 0 | if (ctxt->state->value != NULL) |
8933 | 0 | value = ctxt->state->value; |
8934 | 0 | if (value != NULL) |
8935 | 0 | xmlFree(value); |
8936 | 0 | ctxt->state->value = oldvalue; |
8937 | 0 | ctxt->state->seq = oldseq; |
8938 | 0 | if (ret == 0) { |
8939 | | /* |
8940 | | * flag the attribute as processed |
8941 | | */ |
8942 | 0 | ctxt->state->attrs[i] = NULL; |
8943 | 0 | ctxt->state->nbAttrLeft--; |
8944 | 0 | } |
8945 | 0 | } else { |
8946 | 0 | ret = -1; |
8947 | 0 | } |
8948 | 0 | } |
8949 | |
|
8950 | 0 | return (ret); |
8951 | 0 | } |
8952 | | |
8953 | | /** |
8954 | | * Validate the given node against the list of attribute definitions |
8955 | | * |
8956 | | * @param ctxt a Relax-NG validation context |
8957 | | * @param defines the list of definition to verify |
8958 | | * @returns 0 if the validation succeeded or an error code. |
8959 | | */ |
8960 | | static int |
8961 | | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
8962 | | xmlRelaxNGDefinePtr defines) |
8963 | 0 | { |
8964 | 0 | int ret = 0, res; |
8965 | 0 | int needmore = 0; |
8966 | 0 | xmlRelaxNGDefinePtr cur; |
8967 | |
|
8968 | 0 | cur = defines; |
8969 | 0 | while (cur != NULL) { |
8970 | 0 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
8971 | 0 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
8972 | 0 | ret = -1; |
8973 | 0 | } else |
8974 | 0 | needmore = 1; |
8975 | 0 | cur = cur->next; |
8976 | 0 | } |
8977 | 0 | if (!needmore) |
8978 | 0 | return (ret); |
8979 | 0 | cur = defines; |
8980 | 0 | while (cur != NULL) { |
8981 | 0 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
8982 | 0 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
8983 | 0 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
8984 | 0 | if (res < 0) |
8985 | 0 | ret = -1; |
8986 | 0 | } else { |
8987 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
8988 | 0 | return (-1); |
8989 | 0 | } |
8990 | 0 | if (res == -1) /* continues on -2 */ |
8991 | 0 | break; |
8992 | 0 | } |
8993 | 0 | cur = cur->next; |
8994 | 0 | } |
8995 | | |
8996 | 0 | return (ret); |
8997 | 0 | } |
8998 | | |
8999 | | /** |
9000 | | * Check if a node can be matched by one of the definitions |
9001 | | * |
9002 | | * @param node the node |
9003 | | * @param list a NULL terminated array of definitions |
9004 | | * @returns 1 if matches 0 otherwise |
9005 | | */ |
9006 | | static int |
9007 | | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
9008 | 0 | { |
9009 | 0 | xmlRelaxNGDefinePtr cur; |
9010 | 0 | int i = 0, tmp; |
9011 | |
|
9012 | 0 | if ((node == NULL) || (list == NULL)) |
9013 | 0 | return (0); |
9014 | | |
9015 | 0 | cur = list[i++]; |
9016 | 0 | while (cur != NULL) { |
9017 | 0 | if ((node->type == XML_ELEMENT_NODE) && |
9018 | 0 | (cur->type == XML_RELAXNG_ELEMENT)) { |
9019 | 0 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
9020 | 0 | if (tmp == 1) |
9021 | 0 | return (1); |
9022 | 0 | } else if (((node->type == XML_TEXT_NODE) || |
9023 | 0 | (node->type == XML_CDATA_SECTION_NODE)) && |
9024 | 0 | ((cur->type == XML_RELAXNG_DATATYPE) || |
9025 | 0 | (cur->type == XML_RELAXNG_LIST) || |
9026 | 0 | (cur->type == XML_RELAXNG_TEXT) || |
9027 | 0 | (cur->type == XML_RELAXNG_VALUE))) { |
9028 | 0 | return (1); |
9029 | 0 | } |
9030 | 0 | cur = list[i++]; |
9031 | 0 | } |
9032 | 0 | return (0); |
9033 | 0 | } |
9034 | | |
9035 | | /** |
9036 | | * Validate an interleave definition for a node. |
9037 | | * |
9038 | | * @param ctxt a Relax-NG validation context |
9039 | | * @param define the definition to verify |
9040 | | * @returns 0 if the validation succeeded or an error code. |
9041 | | */ |
9042 | | static int |
9043 | | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
9044 | | xmlRelaxNGDefinePtr define) |
9045 | 0 | { |
9046 | 0 | int ret = 0, i, nbgroups; |
9047 | 0 | int errNr = ctxt->errNr; |
9048 | 0 | int oldflags; |
9049 | |
|
9050 | 0 | xmlRelaxNGValidStatePtr oldstate; |
9051 | 0 | xmlRelaxNGPartitionPtr partitions; |
9052 | 0 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
9053 | 0 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
9054 | 0 | xmlNodePtr *list = NULL, *lasts = NULL; |
9055 | |
|
9056 | 0 | if (define->data != NULL) { |
9057 | 0 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
9058 | 0 | nbgroups = partitions->nbgroups; |
9059 | 0 | } else { |
9060 | 0 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
9061 | 0 | return (-1); |
9062 | 0 | } |
9063 | | /* |
9064 | | * Optimizations for MIXED |
9065 | | */ |
9066 | 0 | oldflags = ctxt->flags; |
9067 | 0 | if (define->dflags & IS_MIXED) { |
9068 | 0 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
9069 | 0 | if (nbgroups == 2) { |
9070 | | /* |
9071 | | * this is a pure <mixed> case |
9072 | | */ |
9073 | 0 | if (ctxt->state != NULL) |
9074 | 0 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
9075 | 0 | ctxt->state->seq); |
9076 | 0 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
9077 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, |
9078 | 0 | partitions->groups[1]-> |
9079 | 0 | rule); |
9080 | 0 | else |
9081 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, |
9082 | 0 | partitions->groups[0]-> |
9083 | 0 | rule); |
9084 | 0 | if (ret == 0) { |
9085 | 0 | if (ctxt->state != NULL) |
9086 | 0 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
9087 | 0 | ctxt->state-> |
9088 | 0 | seq); |
9089 | 0 | } |
9090 | 0 | ctxt->flags = oldflags; |
9091 | 0 | return (ret); |
9092 | 0 | } |
9093 | 0 | } |
9094 | | |
9095 | | /* |
9096 | | * Build arrays to store the first and last node of the chain |
9097 | | * pertaining to each group |
9098 | | */ |
9099 | 0 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
9100 | 0 | if (list == NULL) { |
9101 | 0 | xmlRngVErrMemory(ctxt); |
9102 | 0 | return (-1); |
9103 | 0 | } |
9104 | 0 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
9105 | 0 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
9106 | 0 | if (lasts == NULL) { |
9107 | 0 | xmlRngVErrMemory(ctxt); |
9108 | 0 | return (-1); |
9109 | 0 | } |
9110 | 0 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
9111 | | |
9112 | | /* |
9113 | | * Walk the sequence of children finding the right group and |
9114 | | * sorting them in sequences. |
9115 | | */ |
9116 | 0 | cur = ctxt->state->seq; |
9117 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
9118 | 0 | start = cur; |
9119 | 0 | while (cur != NULL) { |
9120 | 0 | ctxt->state->seq = cur; |
9121 | 0 | if ((partitions->triage != NULL) && |
9122 | 0 | (partitions->flags & IS_DETERMINIST)) { |
9123 | 0 | void *tmp = NULL; |
9124 | |
|
9125 | 0 | if ((cur->type == XML_TEXT_NODE) || |
9126 | 0 | (cur->type == XML_CDATA_SECTION_NODE)) { |
9127 | 0 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
9128 | 0 | NULL); |
9129 | 0 | } else if (cur->type == XML_ELEMENT_NODE) { |
9130 | 0 | if (cur->ns != NULL) { |
9131 | 0 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
9132 | 0 | cur->ns->href); |
9133 | 0 | if (tmp == NULL) |
9134 | 0 | tmp = xmlHashLookup2(partitions->triage, |
9135 | 0 | BAD_CAST "#any", |
9136 | 0 | cur->ns->href); |
9137 | 0 | } else |
9138 | 0 | tmp = |
9139 | 0 | xmlHashLookup2(partitions->triage, cur->name, |
9140 | 0 | NULL); |
9141 | 0 | if (tmp == NULL) |
9142 | 0 | tmp = |
9143 | 0 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
9144 | 0 | NULL); |
9145 | 0 | } |
9146 | |
|
9147 | 0 | if (tmp == NULL) { |
9148 | 0 | i = nbgroups; |
9149 | 0 | } else { |
9150 | 0 | i = XML_PTR_TO_INT(tmp) - 1; |
9151 | 0 | if (partitions->flags & IS_NEEDCHECK) { |
9152 | 0 | group = partitions->groups[i]; |
9153 | 0 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
9154 | 0 | i = nbgroups; |
9155 | 0 | } |
9156 | 0 | } |
9157 | 0 | } else { |
9158 | 0 | for (i = 0; i < nbgroups; i++) { |
9159 | 0 | group = partitions->groups[i]; |
9160 | 0 | if (group == NULL) |
9161 | 0 | continue; |
9162 | 0 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
9163 | 0 | break; |
9164 | 0 | } |
9165 | 0 | } |
9166 | | /* |
9167 | | * We break as soon as an element not matched is found |
9168 | | */ |
9169 | 0 | if (i >= nbgroups) { |
9170 | 0 | break; |
9171 | 0 | } |
9172 | 0 | if (lasts[i] != NULL) { |
9173 | 0 | lasts[i]->next = cur; |
9174 | 0 | lasts[i] = cur; |
9175 | 0 | } else { |
9176 | 0 | list[i] = cur; |
9177 | 0 | lasts[i] = cur; |
9178 | 0 | } |
9179 | 0 | if (cur->next != NULL) |
9180 | 0 | lastchg = cur->next; |
9181 | 0 | else |
9182 | 0 | lastchg = cur; |
9183 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
9184 | 0 | } |
9185 | 0 | if (ret != 0) { |
9186 | 0 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
9187 | 0 | ret = -1; |
9188 | 0 | goto done; |
9189 | 0 | } |
9190 | 0 | lastelem = cur; |
9191 | 0 | oldstate = ctxt->state; |
9192 | 0 | for (i = 0; i < nbgroups; i++) { |
9193 | 0 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
9194 | 0 | if (ctxt->state == NULL) { |
9195 | 0 | ret = -1; |
9196 | 0 | break; |
9197 | 0 | } |
9198 | 0 | group = partitions->groups[i]; |
9199 | 0 | if (lasts[i] != NULL) { |
9200 | 0 | last = lasts[i]->next; |
9201 | 0 | lasts[i]->next = NULL; |
9202 | 0 | } |
9203 | 0 | ctxt->state->seq = list[i]; |
9204 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
9205 | 0 | if (ret != 0) |
9206 | 0 | break; |
9207 | 0 | if (ctxt->state != NULL) { |
9208 | 0 | cur = ctxt->state->seq; |
9209 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
9210 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
9211 | 0 | oldstate = ctxt->state; |
9212 | 0 | ctxt->state = NULL; |
9213 | 0 | if (cur != NULL |
9214 | | /* there's a nasty violation of context-free unambiguities, |
9215 | | since in open-name-class context, interleave in the |
9216 | | production shall finish without caring about anything |
9217 | | else that is OK to follow in that case -- it would |
9218 | | otherwise get marked as "extra content" and would |
9219 | | hence fail the validation, hence this perhaps |
9220 | | dirty attempt to rectify such a situation */ |
9221 | 0 | && (define->parent->type != XML_RELAXNG_DEF |
9222 | 0 | || !xmlStrEqual(define->parent->name, |
9223 | 0 | (const xmlChar *) "open-name-class"))) { |
9224 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
9225 | 0 | ret = -1; |
9226 | 0 | ctxt->state = oldstate; |
9227 | 0 | goto done; |
9228 | 0 | } |
9229 | 0 | } else if (ctxt->states != NULL) { |
9230 | 0 | int j; |
9231 | 0 | int found = 0; |
9232 | 0 | int best = -1; |
9233 | 0 | int lowattr = -1; |
9234 | | |
9235 | | /* |
9236 | | * PBM: what happen if there is attributes checks in the interleaves |
9237 | | */ |
9238 | |
|
9239 | 0 | for (j = 0; j < ctxt->states->nbState; j++) { |
9240 | 0 | cur = ctxt->states->tabState[j]->seq; |
9241 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
9242 | 0 | if (cur == NULL) { |
9243 | 0 | if (found == 0) { |
9244 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9245 | 0 | best = j; |
9246 | 0 | } |
9247 | 0 | found = 1; |
9248 | 0 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
9249 | | /* try to keep the latest one to mach old heuristic */ |
9250 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9251 | 0 | best = j; |
9252 | 0 | } |
9253 | 0 | if (lowattr == 0) |
9254 | 0 | break; |
9255 | 0 | } else if (found == 0) { |
9256 | 0 | if (lowattr == -1) { |
9257 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9258 | 0 | best = j; |
9259 | 0 | } else |
9260 | 0 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
9261 | | /* try to keep the latest one to mach old heuristic */ |
9262 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9263 | 0 | best = j; |
9264 | 0 | } |
9265 | 0 | } |
9266 | 0 | } |
9267 | | /* |
9268 | | * BIG PBM: here we pick only one restarting point :-( |
9269 | | */ |
9270 | 0 | if (ctxt->states->nbState > 0) { |
9271 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
9272 | 0 | if (best != -1) { |
9273 | 0 | oldstate = ctxt->states->tabState[best]; |
9274 | 0 | ctxt->states->tabState[best] = NULL; |
9275 | 0 | } else { |
9276 | 0 | oldstate = |
9277 | 0 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
9278 | 0 | ctxt->states->tabState[ctxt->states->nbState - 1] = NULL; |
9279 | 0 | ctxt->states->nbState--; |
9280 | 0 | } |
9281 | 0 | } |
9282 | 0 | for (j = 0; j < ctxt->states->nbState ; j++) { |
9283 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
9284 | 0 | } |
9285 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
9286 | 0 | ctxt->states = NULL; |
9287 | 0 | if (found == 0) { |
9288 | 0 | if (cur == NULL) { |
9289 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, |
9290 | 0 | (const xmlChar *) "noname"); |
9291 | 0 | } else { |
9292 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
9293 | 0 | } |
9294 | 0 | ret = -1; |
9295 | 0 | ctxt->state = oldstate; |
9296 | 0 | goto done; |
9297 | 0 | } |
9298 | 0 | } else { |
9299 | 0 | ret = -1; |
9300 | 0 | break; |
9301 | 0 | } |
9302 | 0 | if (lasts[i] != NULL) { |
9303 | 0 | lasts[i]->next = last; |
9304 | 0 | } |
9305 | 0 | } |
9306 | 0 | if (ctxt->state != NULL) |
9307 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
9308 | 0 | ctxt->state = oldstate; |
9309 | 0 | ctxt->state->seq = lastelem; |
9310 | 0 | if (ret != 0) { |
9311 | 0 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
9312 | 0 | ret = -1; |
9313 | 0 | goto done; |
9314 | 0 | } |
9315 | | |
9316 | 0 | done: |
9317 | 0 | ctxt->flags = oldflags; |
9318 | | /* |
9319 | | * builds the next links chain from the prev one |
9320 | | */ |
9321 | 0 | cur = lastchg; |
9322 | 0 | while (cur != NULL) { |
9323 | 0 | if ((cur == start) || (cur->prev == NULL)) |
9324 | 0 | break; |
9325 | 0 | cur->prev->next = cur; |
9326 | 0 | cur = cur->prev; |
9327 | 0 | } |
9328 | 0 | if (ret == 0) { |
9329 | 0 | if (ctxt->errNr > errNr) |
9330 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9331 | 0 | } |
9332 | |
|
9333 | 0 | xmlFree(list); |
9334 | 0 | xmlFree(lasts); |
9335 | 0 | return (ret); |
9336 | 0 | } |
9337 | | |
9338 | | /** |
9339 | | * Validate the given node content against the (list) of definitions |
9340 | | * |
9341 | | * @param ctxt a Relax-NG validation context |
9342 | | * @param defines the list of definition to verify |
9343 | | * @returns 0 if the validation succeeded or an error code. |
9344 | | */ |
9345 | | static int |
9346 | | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
9347 | | xmlRelaxNGDefinePtr defines) |
9348 | 0 | { |
9349 | 0 | int ret = 0, res; |
9350 | | |
9351 | |
|
9352 | 0 | if (defines == NULL) { |
9353 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
9354 | 0 | BAD_CAST "NULL definition list"); |
9355 | 0 | return (-1); |
9356 | 0 | } |
9357 | 0 | while (defines != NULL) { |
9358 | 0 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
9359 | 0 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
9360 | 0 | if (res < 0) |
9361 | 0 | ret = -1; |
9362 | 0 | } else { |
9363 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
9364 | 0 | return (-1); |
9365 | 0 | } |
9366 | 0 | if (res == -1) /* continues on -2 */ |
9367 | 0 | break; |
9368 | 0 | defines = defines->next; |
9369 | 0 | } |
9370 | | |
9371 | 0 | return (ret); |
9372 | 0 | } |
9373 | | |
9374 | | /** |
9375 | | * Check if the element matches the definition nameClass |
9376 | | * |
9377 | | * @param ctxt a Relax-NG validation context |
9378 | | * @param define the definition to check |
9379 | | * @param elem the element |
9380 | | * @returns 1 if the element matches, 0 if no, or -1 in case of error |
9381 | | */ |
9382 | | static int |
9383 | | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
9384 | | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
9385 | 0 | { |
9386 | 0 | int ret = 0, oldflags = 0; |
9387 | |
|
9388 | 0 | if (define->name != NULL) { |
9389 | 0 | if (!xmlStrEqual(elem->name, define->name)) { |
9390 | 0 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
9391 | 0 | return (0); |
9392 | 0 | } |
9393 | 0 | } |
9394 | 0 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
9395 | 0 | if (elem->ns == NULL) { |
9396 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
9397 | 0 | return (0); |
9398 | 0 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
9399 | 0 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
9400 | 0 | elem->name, define->ns); |
9401 | 0 | return (0); |
9402 | 0 | } |
9403 | 0 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
9404 | 0 | (define->name == NULL)) { |
9405 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
9406 | 0 | return (0); |
9407 | 0 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
9408 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
9409 | 0 | return (0); |
9410 | 0 | } |
9411 | | |
9412 | 0 | if (define->nameClass == NULL) |
9413 | 0 | return (1); |
9414 | | |
9415 | 0 | define = define->nameClass; |
9416 | 0 | if (define->type == XML_RELAXNG_EXCEPT) { |
9417 | 0 | xmlRelaxNGDefinePtr list; |
9418 | |
|
9419 | 0 | if (ctxt != NULL) { |
9420 | 0 | oldflags = ctxt->flags; |
9421 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9422 | 0 | } |
9423 | |
|
9424 | 0 | list = define->content; |
9425 | 0 | while (list != NULL) { |
9426 | 0 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
9427 | 0 | if (ret == 1) { |
9428 | 0 | if (ctxt != NULL) |
9429 | 0 | ctxt->flags = oldflags; |
9430 | 0 | return (0); |
9431 | 0 | } |
9432 | 0 | if (ret < 0) { |
9433 | 0 | if (ctxt != NULL) |
9434 | 0 | ctxt->flags = oldflags; |
9435 | 0 | return (ret); |
9436 | 0 | } |
9437 | 0 | list = list->next; |
9438 | 0 | } |
9439 | 0 | ret = 1; |
9440 | 0 | if (ctxt != NULL) { |
9441 | 0 | ctxt->flags = oldflags; |
9442 | 0 | } |
9443 | 0 | } else if (define->type == XML_RELAXNG_CHOICE) { |
9444 | 0 | xmlRelaxNGDefinePtr list; |
9445 | |
|
9446 | 0 | if (ctxt != NULL) { |
9447 | 0 | oldflags = ctxt->flags; |
9448 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9449 | 0 | } |
9450 | |
|
9451 | 0 | list = define->nameClass; |
9452 | 0 | while (list != NULL) { |
9453 | 0 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
9454 | 0 | if (ret == 1) { |
9455 | 0 | if (ctxt != NULL) |
9456 | 0 | ctxt->flags = oldflags; |
9457 | 0 | return (1); |
9458 | 0 | } |
9459 | 0 | if (ret < 0) { |
9460 | 0 | if (ctxt != NULL) |
9461 | 0 | ctxt->flags = oldflags; |
9462 | 0 | return (ret); |
9463 | 0 | } |
9464 | 0 | list = list->next; |
9465 | 0 | } |
9466 | 0 | if (ctxt != NULL) { |
9467 | 0 | if (ret != 0) { |
9468 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9469 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9470 | 0 | } else { |
9471 | 0 | if (ctxt->errNr > 0) |
9472 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
9473 | 0 | } |
9474 | 0 | } |
9475 | 0 | ret = 0; |
9476 | 0 | if (ctxt != NULL) { |
9477 | 0 | ctxt->flags = oldflags; |
9478 | 0 | } |
9479 | 0 | } else { |
9480 | | /* TODO */ |
9481 | 0 | ret = -1; |
9482 | 0 | } |
9483 | 0 | return (ret); |
9484 | 0 | } |
9485 | | |
9486 | | /** |
9487 | | * Find the "best" state in the ctxt->states list of states to report |
9488 | | * errors about. I.e. a state with no element left in the child list |
9489 | | * or the one with the less attributes left. |
9490 | | * This is called only if a validation error was detected |
9491 | | * |
9492 | | * @param ctxt a Relax-NG validation context |
9493 | | * @returns the index of the "best" state or -1 in case of error |
9494 | | */ |
9495 | | static int |
9496 | | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
9497 | 0 | { |
9498 | 0 | xmlRelaxNGValidStatePtr state; |
9499 | 0 | int i, tmp; |
9500 | 0 | int best = -1; |
9501 | 0 | int value = 1000000; |
9502 | |
|
9503 | 0 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
9504 | 0 | (ctxt->states->nbState <= 0)) |
9505 | 0 | return (-1); |
9506 | | |
9507 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9508 | 0 | state = ctxt->states->tabState[i]; |
9509 | 0 | if (state == NULL) |
9510 | 0 | continue; |
9511 | 0 | if (state->seq != NULL) { |
9512 | 0 | if ((best == -1) || (value > 100000)) { |
9513 | 0 | value = 100000; |
9514 | 0 | best = i; |
9515 | 0 | } |
9516 | 0 | } else { |
9517 | 0 | tmp = state->nbAttrLeft; |
9518 | 0 | if ((best == -1) || (value > tmp)) { |
9519 | 0 | value = tmp; |
9520 | 0 | best = i; |
9521 | 0 | } |
9522 | 0 | } |
9523 | 0 | } |
9524 | 0 | return (best); |
9525 | 0 | } |
9526 | | |
9527 | | /** |
9528 | | * Find the "best" state in the ctxt->states list of states to report |
9529 | | * errors about and log it. |
9530 | | * |
9531 | | * @param ctxt a Relax-NG validation context |
9532 | | */ |
9533 | | static void |
9534 | | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
9535 | 0 | { |
9536 | 0 | int best; |
9537 | |
|
9538 | 0 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
9539 | 0 | (ctxt->states->nbState <= 0)) |
9540 | 0 | return; |
9541 | | |
9542 | 0 | best = xmlRelaxNGBestState(ctxt); |
9543 | 0 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
9544 | 0 | ctxt->state = ctxt->states->tabState[best]; |
9545 | |
|
9546 | 0 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
9547 | 0 | } |
9548 | 0 | } |
9549 | | |
9550 | | /** |
9551 | | * Validate the end of the element, implements check that |
9552 | | * there is nothing left not consumed in the element content |
9553 | | * or in the attribute list. |
9554 | | * |
9555 | | * @param ctxt a Relax-NG validation context |
9556 | | * @param dolog indicate that error logging should be done |
9557 | | * @returns 0 if the validation succeeded or an error code. |
9558 | | */ |
9559 | | static int |
9560 | | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
9561 | 0 | { |
9562 | 0 | int i; |
9563 | 0 | xmlRelaxNGValidStatePtr state; |
9564 | |
|
9565 | 0 | state = ctxt->state; |
9566 | 0 | if (state->seq != NULL) { |
9567 | 0 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
9568 | 0 | if (state->seq != NULL) { |
9569 | 0 | if (dolog) { |
9570 | 0 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
9571 | 0 | state->node->name, state->seq->name); |
9572 | 0 | } |
9573 | 0 | return (-1); |
9574 | 0 | } |
9575 | 0 | } |
9576 | 0 | for (i = 0; i < state->nbAttrs; i++) { |
9577 | 0 | if (state->attrs[i] != NULL) { |
9578 | 0 | if (dolog) { |
9579 | 0 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
9580 | 0 | state->attrs[i]->name, state->node->name); |
9581 | 0 | } |
9582 | 0 | return (-1 - i); |
9583 | 0 | } |
9584 | 0 | } |
9585 | 0 | return (0); |
9586 | 0 | } |
9587 | | |
9588 | | /** |
9589 | | * Validate the current state against the definition |
9590 | | * |
9591 | | * @param ctxt a Relax-NG validation context |
9592 | | * @param define the definition to verify |
9593 | | * @returns 0 if the validation succeeded or an error code. |
9594 | | */ |
9595 | | static int |
9596 | | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
9597 | | xmlRelaxNGDefinePtr define) |
9598 | 0 | { |
9599 | 0 | xmlNodePtr node; |
9600 | 0 | int ret = 0, i, tmp, oldflags, errNr; |
9601 | 0 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
9602 | |
|
9603 | 0 | if (define == NULL) { |
9604 | 0 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
9605 | 0 | return (-1); |
9606 | 0 | } |
9607 | | |
9608 | 0 | if (ctxt->state != NULL) { |
9609 | 0 | node = ctxt->state->seq; |
9610 | 0 | } else { |
9611 | 0 | node = NULL; |
9612 | 0 | } |
9613 | 0 | ctxt->depth++; |
9614 | 0 | switch (define->type) { |
9615 | 0 | case XML_RELAXNG_EMPTY: |
9616 | 0 | ret = 0; |
9617 | 0 | break; |
9618 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
9619 | 0 | ret = -1; |
9620 | 0 | break; |
9621 | 0 | case XML_RELAXNG_TEXT: |
9622 | 0 | while ((node != NULL) && |
9623 | 0 | ((node->type == XML_TEXT_NODE) || |
9624 | 0 | (node->type == XML_COMMENT_NODE) || |
9625 | 0 | (node->type == XML_PI_NODE) || |
9626 | 0 | (node->type == XML_CDATA_SECTION_NODE))) |
9627 | 0 | node = node->next; |
9628 | 0 | ctxt->state->seq = node; |
9629 | 0 | break; |
9630 | 0 | case XML_RELAXNG_ELEMENT: |
9631 | 0 | errNr = ctxt->errNr; |
9632 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
9633 | 0 | if (node == NULL) { |
9634 | 0 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
9635 | 0 | ret = -1; |
9636 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9637 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9638 | 0 | break; |
9639 | 0 | } |
9640 | 0 | if (node->type != XML_ELEMENT_NODE) { |
9641 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
9642 | 0 | ret = -1; |
9643 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9644 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9645 | 0 | break; |
9646 | 0 | } |
9647 | | /* |
9648 | | * This node was already validated successfully against |
9649 | | * this definition. |
9650 | | */ |
9651 | 0 | if (node->psvi == define) { |
9652 | 0 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
9653 | 0 | if (ctxt->errNr > errNr) |
9654 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9655 | 0 | if (ctxt->errNr != 0) { |
9656 | 0 | while ((ctxt->err != NULL) && |
9657 | 0 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
9658 | 0 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
9659 | 0 | || |
9660 | 0 | ((ctxt->err->err == |
9661 | 0 | XML_RELAXNG_ERR_ELEMEXTRANS) |
9662 | 0 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
9663 | 0 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
9664 | 0 | || (ctxt->err->err == |
9665 | 0 | XML_RELAXNG_ERR_NOTELEM))) |
9666 | 0 | xmlRelaxNGValidErrorPop(ctxt); |
9667 | 0 | } |
9668 | 0 | break; |
9669 | 0 | } |
9670 | | |
9671 | 0 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
9672 | 0 | if (ret <= 0) { |
9673 | 0 | ret = -1; |
9674 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9675 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9676 | 0 | break; |
9677 | 0 | } |
9678 | 0 | ret = 0; |
9679 | 0 | if (ctxt->errNr != 0) { |
9680 | 0 | if (ctxt->errNr > errNr) |
9681 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9682 | 0 | while ((ctxt->err != NULL) && |
9683 | 0 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
9684 | 0 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
9685 | 0 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
9686 | 0 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
9687 | 0 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
9688 | 0 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
9689 | 0 | xmlRelaxNGValidErrorPop(ctxt); |
9690 | 0 | } |
9691 | 0 | errNr = ctxt->errNr; |
9692 | |
|
9693 | 0 | oldflags = ctxt->flags; |
9694 | 0 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
9695 | 0 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
9696 | 0 | } |
9697 | 0 | state = xmlRelaxNGNewValidState(ctxt, node); |
9698 | 0 | if (state == NULL) { |
9699 | 0 | ret = -1; |
9700 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9701 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9702 | 0 | break; |
9703 | 0 | } |
9704 | | |
9705 | 0 | oldstate = ctxt->state; |
9706 | 0 | ctxt->state = state; |
9707 | 0 | if (define->attrs != NULL) { |
9708 | 0 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
9709 | 0 | if (tmp != 0) { |
9710 | 0 | ret = -1; |
9711 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
9712 | 0 | } |
9713 | 0 | } |
9714 | 0 | if (define->contModel != NULL) { |
9715 | 0 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
9716 | 0 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
9717 | 0 | xmlNodePtr nseq; |
9718 | |
|
9719 | 0 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
9720 | 0 | ctxt->state = nstate; |
9721 | 0 | ctxt->states = NULL; |
9722 | |
|
9723 | 0 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
9724 | 0 | define->contModel, |
9725 | 0 | ctxt->state->seq); |
9726 | 0 | nseq = ctxt->state->seq; |
9727 | 0 | ctxt->state = tmpstate; |
9728 | 0 | ctxt->states = tmpstates; |
9729 | 0 | xmlRelaxNGFreeValidState(ctxt, nstate); |
9730 | |
|
9731 | 0 | if (tmp != 0) |
9732 | 0 | ret = -1; |
9733 | |
|
9734 | 0 | if (ctxt->states != NULL) { |
9735 | 0 | tmp = -1; |
9736 | |
|
9737 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9738 | 0 | state = ctxt->states->tabState[i]; |
9739 | 0 | ctxt->state = state; |
9740 | 0 | ctxt->state->seq = nseq; |
9741 | |
|
9742 | 0 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
9743 | 0 | tmp = 0; |
9744 | 0 | break; |
9745 | 0 | } |
9746 | 0 | } |
9747 | 0 | if (tmp != 0) { |
9748 | | /* |
9749 | | * validation error, log the message for the "best" one |
9750 | | */ |
9751 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9752 | 0 | xmlRelaxNGLogBestError(ctxt); |
9753 | 0 | } |
9754 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9755 | 0 | xmlRelaxNGFreeValidState(ctxt, |
9756 | 0 | ctxt->states-> |
9757 | 0 | tabState[i]); |
9758 | 0 | } |
9759 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
9760 | 0 | ctxt->flags = oldflags; |
9761 | 0 | ctxt->states = NULL; |
9762 | 0 | if ((ret == 0) && (tmp == -1)) |
9763 | 0 | ret = -1; |
9764 | 0 | } else { |
9765 | 0 | state = ctxt->state; |
9766 | 0 | if (ctxt->state != NULL) |
9767 | 0 | ctxt->state->seq = nseq; |
9768 | 0 | if (ret == 0) |
9769 | 0 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
9770 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
9771 | 0 | } |
9772 | 0 | } else { |
9773 | 0 | if (define->content != NULL) { |
9774 | 0 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
9775 | 0 | define-> |
9776 | 0 | content); |
9777 | 0 | if (tmp != 0) { |
9778 | 0 | ret = -1; |
9779 | 0 | if (ctxt->state == NULL) { |
9780 | 0 | ctxt->state = oldstate; |
9781 | 0 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
9782 | 0 | node->name); |
9783 | 0 | ctxt->state = NULL; |
9784 | 0 | } else { |
9785 | 0 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
9786 | 0 | node->name); |
9787 | 0 | } |
9788 | |
|
9789 | 0 | } |
9790 | 0 | } |
9791 | 0 | if (ctxt->states != NULL) { |
9792 | 0 | tmp = -1; |
9793 | |
|
9794 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9795 | 0 | state = ctxt->states->tabState[i]; |
9796 | 0 | ctxt->state = state; |
9797 | |
|
9798 | 0 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
9799 | 0 | tmp = 0; |
9800 | 0 | break; |
9801 | 0 | } |
9802 | 0 | } |
9803 | 0 | if (tmp != 0) { |
9804 | | /* |
9805 | | * validation error, log the message for the "best" one |
9806 | | */ |
9807 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9808 | 0 | xmlRelaxNGLogBestError(ctxt); |
9809 | 0 | } |
9810 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9811 | 0 | xmlRelaxNGFreeValidState(ctxt, |
9812 | 0 | ctxt->states->tabState[i]); |
9813 | 0 | ctxt->states->tabState[i] = NULL; |
9814 | 0 | } |
9815 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
9816 | 0 | ctxt->flags = oldflags; |
9817 | 0 | ctxt->states = NULL; |
9818 | 0 | if ((ret == 0) && (tmp == -1)) |
9819 | 0 | ret = -1; |
9820 | 0 | } else { |
9821 | 0 | state = ctxt->state; |
9822 | 0 | if (ret == 0) |
9823 | 0 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
9824 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
9825 | 0 | } |
9826 | 0 | } |
9827 | 0 | if (ret == 0) { |
9828 | 0 | node->psvi = define; |
9829 | 0 | } |
9830 | 0 | ctxt->flags = oldflags; |
9831 | 0 | ctxt->state = oldstate; |
9832 | 0 | if (oldstate != NULL) |
9833 | 0 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
9834 | 0 | if (ret != 0) { |
9835 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
9836 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9837 | 0 | ret = 0; |
9838 | | #if 0 |
9839 | | } else { |
9840 | | ret = -2; |
9841 | | #endif |
9842 | 0 | } |
9843 | 0 | } else { |
9844 | 0 | if (ctxt->errNr > errNr) |
9845 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9846 | 0 | } |
9847 | |
|
9848 | 0 | break; |
9849 | 0 | case XML_RELAXNG_OPTIONAL:{ |
9850 | 0 | errNr = ctxt->errNr; |
9851 | 0 | oldflags = ctxt->flags; |
9852 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9853 | 0 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
9854 | 0 | ret = |
9855 | 0 | xmlRelaxNGValidateDefinitionList(ctxt, |
9856 | 0 | define->content); |
9857 | 0 | if (ret != 0) { |
9858 | 0 | if (ctxt->state != NULL) |
9859 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
9860 | 0 | ctxt->state = oldstate; |
9861 | 0 | ctxt->flags = oldflags; |
9862 | 0 | ret = 0; |
9863 | 0 | if (ctxt->errNr > errNr) |
9864 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9865 | 0 | break; |
9866 | 0 | } |
9867 | 0 | if (ctxt->states != NULL) { |
9868 | 0 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
9869 | 0 | } else { |
9870 | 0 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
9871 | 0 | if (ctxt->states == NULL) { |
9872 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
9873 | 0 | ctxt->flags = oldflags; |
9874 | 0 | ret = -1; |
9875 | 0 | if (ctxt->errNr > errNr) |
9876 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9877 | 0 | break; |
9878 | 0 | } |
9879 | 0 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
9880 | 0 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
9881 | 0 | ctxt->state = NULL; |
9882 | 0 | } |
9883 | 0 | ctxt->flags = oldflags; |
9884 | 0 | ret = 0; |
9885 | 0 | if (ctxt->errNr > errNr) |
9886 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9887 | 0 | break; |
9888 | 0 | } |
9889 | 0 | case XML_RELAXNG_ONEORMORE: |
9890 | 0 | errNr = ctxt->errNr; |
9891 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
9892 | 0 | if (ret != 0) { |
9893 | 0 | break; |
9894 | 0 | } |
9895 | 0 | if (ctxt->errNr > errNr) |
9896 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9897 | | /* Falls through. */ |
9898 | 0 | case XML_RELAXNG_ZEROORMORE:{ |
9899 | 0 | int progress; |
9900 | 0 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
9901 | 0 | int base, j; |
9902 | |
|
9903 | 0 | errNr = ctxt->errNr; |
9904 | 0 | res = xmlRelaxNGNewStates(ctxt, 1); |
9905 | 0 | if (res == NULL) { |
9906 | 0 | ret = -1; |
9907 | 0 | break; |
9908 | 0 | } |
9909 | | /* |
9910 | | * All the input states are also exit states |
9911 | | */ |
9912 | 0 | if (ctxt->state != NULL) { |
9913 | 0 | xmlRelaxNGAddStates(ctxt, res, |
9914 | 0 | xmlRelaxNGCopyValidState(ctxt, |
9915 | 0 | ctxt-> |
9916 | 0 | state)); |
9917 | 0 | } else { |
9918 | 0 | for (j = 0; j < ctxt->states->nbState; j++) { |
9919 | 0 | xmlRelaxNGAddStates(ctxt, res, |
9920 | 0 | xmlRelaxNGCopyValidState(ctxt, |
9921 | 0 | ctxt->states->tabState[j])); |
9922 | 0 | } |
9923 | 0 | } |
9924 | 0 | oldflags = ctxt->flags; |
9925 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9926 | 0 | do { |
9927 | 0 | progress = 0; |
9928 | 0 | base = res->nbState; |
9929 | |
|
9930 | 0 | if (ctxt->states != NULL) { |
9931 | 0 | states = ctxt->states; |
9932 | 0 | for (i = 0; i < states->nbState; i++) { |
9933 | 0 | ctxt->state = states->tabState[i]; |
9934 | 0 | ctxt->states = NULL; |
9935 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
9936 | 0 | define-> |
9937 | 0 | content); |
9938 | 0 | if (ret == 0) { |
9939 | 0 | if (ctxt->state != NULL) { |
9940 | 0 | tmp = xmlRelaxNGAddStates(ctxt, res, |
9941 | 0 | ctxt->state); |
9942 | 0 | ctxt->state = NULL; |
9943 | 0 | if (tmp == 1) |
9944 | 0 | progress = 1; |
9945 | 0 | } else if (ctxt->states != NULL) { |
9946 | 0 | for (j = 0; j < ctxt->states->nbState; |
9947 | 0 | j++) { |
9948 | 0 | tmp = |
9949 | 0 | xmlRelaxNGAddStates(ctxt, res, |
9950 | 0 | ctxt->states->tabState[j]); |
9951 | 0 | if (tmp == 1) |
9952 | 0 | progress = 1; |
9953 | 0 | } |
9954 | 0 | xmlRelaxNGFreeStates(ctxt, |
9955 | 0 | ctxt->states); |
9956 | 0 | ctxt->states = NULL; |
9957 | 0 | } |
9958 | 0 | } else { |
9959 | 0 | if (ctxt->state != NULL) { |
9960 | 0 | xmlRelaxNGFreeValidState(ctxt, |
9961 | 0 | ctxt->state); |
9962 | 0 | ctxt->state = NULL; |
9963 | 0 | } |
9964 | 0 | } |
9965 | 0 | } |
9966 | 0 | } else { |
9967 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
9968 | 0 | define-> |
9969 | 0 | content); |
9970 | 0 | if (ret != 0) { |
9971 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
9972 | 0 | ctxt->state = NULL; |
9973 | 0 | } else { |
9974 | 0 | base = res->nbState; |
9975 | 0 | if (ctxt->state != NULL) { |
9976 | 0 | tmp = xmlRelaxNGAddStates(ctxt, res, |
9977 | 0 | ctxt->state); |
9978 | 0 | ctxt->state = NULL; |
9979 | 0 | if (tmp == 1) |
9980 | 0 | progress = 1; |
9981 | 0 | } else if (ctxt->states != NULL) { |
9982 | 0 | for (j = 0; j < ctxt->states->nbState; j++) { |
9983 | 0 | tmp = xmlRelaxNGAddStates(ctxt, res, |
9984 | 0 | ctxt->states->tabState[j]); |
9985 | 0 | if (tmp == 1) |
9986 | 0 | progress = 1; |
9987 | 0 | } |
9988 | 0 | if (states == NULL) { |
9989 | 0 | states = ctxt->states; |
9990 | 0 | } else { |
9991 | 0 | xmlRelaxNGFreeStates(ctxt, |
9992 | 0 | ctxt->states); |
9993 | 0 | } |
9994 | 0 | ctxt->states = NULL; |
9995 | 0 | } |
9996 | 0 | } |
9997 | 0 | } |
9998 | 0 | if (progress) { |
9999 | | /* |
10000 | | * Collect all the new nodes added at that step |
10001 | | * and make them the new node set |
10002 | | */ |
10003 | 0 | if (res->nbState - base == 1) { |
10004 | 0 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
10005 | 0 | res-> |
10006 | 0 | tabState |
10007 | 0 | [base]); |
10008 | 0 | } else { |
10009 | 0 | if (states == NULL) { |
10010 | 0 | xmlRelaxNGNewStates(ctxt, |
10011 | 0 | res->nbState - base); |
10012 | 0 | states = ctxt->states; |
10013 | 0 | if (states == NULL) { |
10014 | 0 | progress = 0; |
10015 | 0 | break; |
10016 | 0 | } |
10017 | 0 | } |
10018 | 0 | states->nbState = 0; |
10019 | 0 | for (i = base; i < res->nbState; i++) |
10020 | 0 | xmlRelaxNGAddStates(ctxt, states, |
10021 | 0 | xmlRelaxNGCopyValidState |
10022 | 0 | (ctxt, res->tabState[i])); |
10023 | 0 | ctxt->states = states; |
10024 | 0 | } |
10025 | 0 | } |
10026 | 0 | } while (progress == 1); |
10027 | 0 | if (states != NULL) { |
10028 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10029 | 0 | } |
10030 | 0 | ctxt->states = res; |
10031 | 0 | ctxt->flags = oldflags; |
10032 | | #if 0 |
10033 | | /* |
10034 | | * errors may have to be propagated back... |
10035 | | */ |
10036 | | if (ctxt->errNr > errNr) |
10037 | | xmlRelaxNGPopErrors(ctxt, errNr); |
10038 | | #endif |
10039 | 0 | ret = 0; |
10040 | 0 | break; |
10041 | 0 | } |
10042 | 0 | case XML_RELAXNG_CHOICE:{ |
10043 | 0 | xmlRelaxNGDefinePtr list = NULL; |
10044 | 0 | xmlRelaxNGStatesPtr states = NULL; |
10045 | |
|
10046 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
10047 | |
|
10048 | 0 | errNr = ctxt->errNr; |
10049 | 0 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL) && |
10050 | 0 | (node != NULL)) { |
10051 | | /* |
10052 | | * node == NULL can't be optimized since IS_TRIABLE |
10053 | | * doesn't account for choice which may lead to |
10054 | | * only attributes. |
10055 | | */ |
10056 | 0 | xmlHashTablePtr triage = |
10057 | 0 | (xmlHashTablePtr) define->data; |
10058 | | |
10059 | | /* |
10060 | | * Something we can optimize cleanly there is only one |
10061 | | * possible branch out ! |
10062 | | */ |
10063 | 0 | if ((node->type == XML_TEXT_NODE) || |
10064 | 0 | (node->type == XML_CDATA_SECTION_NODE)) { |
10065 | 0 | list = |
10066 | 0 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
10067 | 0 | } else if (node->type == XML_ELEMENT_NODE) { |
10068 | 0 | if (node->ns != NULL) { |
10069 | 0 | list = xmlHashLookup2(triage, node->name, |
10070 | 0 | node->ns->href); |
10071 | 0 | if (list == NULL) |
10072 | 0 | list = |
10073 | 0 | xmlHashLookup2(triage, BAD_CAST "#any", |
10074 | 0 | node->ns->href); |
10075 | 0 | } else |
10076 | 0 | list = |
10077 | 0 | xmlHashLookup2(triage, node->name, NULL); |
10078 | 0 | if (list == NULL) |
10079 | 0 | list = |
10080 | 0 | xmlHashLookup2(triage, BAD_CAST "#any", |
10081 | 0 | NULL); |
10082 | 0 | } |
10083 | 0 | if (list == NULL) { |
10084 | 0 | ret = -1; |
10085 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
10086 | 0 | break; |
10087 | 0 | } |
10088 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
10089 | 0 | if (ret == 0) { |
10090 | 0 | } |
10091 | 0 | break; |
10092 | 0 | } |
10093 | | |
10094 | 0 | list = define->content; |
10095 | 0 | oldflags = ctxt->flags; |
10096 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
10097 | |
|
10098 | 0 | while (list != NULL) { |
10099 | 0 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
10100 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
10101 | 0 | if (ret == 0) { |
10102 | 0 | if (states == NULL) { |
10103 | 0 | states = xmlRelaxNGNewStates(ctxt, 1); |
10104 | 0 | } |
10105 | 0 | if (ctxt->state != NULL) { |
10106 | 0 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
10107 | 0 | } else if (ctxt->states != NULL) { |
10108 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
10109 | 0 | xmlRelaxNGAddStates(ctxt, states, |
10110 | 0 | ctxt->states-> |
10111 | 0 | tabState[i]); |
10112 | 0 | } |
10113 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10114 | 0 | ctxt->states = NULL; |
10115 | 0 | } |
10116 | 0 | } else { |
10117 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10118 | 0 | } |
10119 | 0 | ctxt->state = oldstate; |
10120 | 0 | list = list->next; |
10121 | 0 | } |
10122 | 0 | if (states != NULL) { |
10123 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
10124 | 0 | ctxt->states = states; |
10125 | 0 | ctxt->state = NULL; |
10126 | 0 | ret = 0; |
10127 | 0 | } else { |
10128 | 0 | ctxt->states = NULL; |
10129 | 0 | } |
10130 | 0 | ctxt->flags = oldflags; |
10131 | 0 | if (ret != 0) { |
10132 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
10133 | 0 | xmlRelaxNGDumpValidError(ctxt); |
10134 | 0 | } |
10135 | 0 | } else { |
10136 | 0 | if (ctxt->errNr > errNr) |
10137 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
10138 | 0 | } |
10139 | 0 | break; |
10140 | 0 | } |
10141 | 0 | case XML_RELAXNG_DEF: |
10142 | 0 | case XML_RELAXNG_GROUP: |
10143 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
10144 | 0 | break; |
10145 | 0 | case XML_RELAXNG_INTERLEAVE: |
10146 | 0 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
10147 | 0 | break; |
10148 | 0 | case XML_RELAXNG_ATTRIBUTE: |
10149 | 0 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
10150 | 0 | break; |
10151 | 0 | case XML_RELAXNG_START: |
10152 | 0 | case XML_RELAXNG_NOOP: |
10153 | 0 | case XML_RELAXNG_REF: |
10154 | 0 | case XML_RELAXNG_EXTERNALREF: |
10155 | 0 | case XML_RELAXNG_PARENTREF: |
10156 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
10157 | 0 | break; |
10158 | 0 | case XML_RELAXNG_DATATYPE:{ |
10159 | 0 | xmlNodePtr child; |
10160 | 0 | xmlChar *content = NULL; |
10161 | |
|
10162 | 0 | child = node; |
10163 | 0 | while (child != NULL) { |
10164 | 0 | if (child->type == XML_ELEMENT_NODE) { |
10165 | 0 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
10166 | 0 | node->parent->name); |
10167 | 0 | ret = -1; |
10168 | 0 | break; |
10169 | 0 | } else if ((child->type == XML_TEXT_NODE) || |
10170 | 0 | (child->type == XML_CDATA_SECTION_NODE)) { |
10171 | 0 | content = xmlStrcat(content, child->content); |
10172 | 0 | } |
10173 | | /* TODO: handle entities ... */ |
10174 | 0 | child = child->next; |
10175 | 0 | } |
10176 | 0 | if (ret == -1) { |
10177 | 0 | if (content != NULL) |
10178 | 0 | xmlFree(content); |
10179 | 0 | break; |
10180 | 0 | } |
10181 | 0 | if (content == NULL) { |
10182 | 0 | content = xmlStrdup(BAD_CAST ""); |
10183 | 0 | if (content == NULL) { |
10184 | 0 | xmlRngVErrMemory(ctxt); |
10185 | 0 | ret = -1; |
10186 | 0 | break; |
10187 | 0 | } |
10188 | 0 | } |
10189 | 0 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
10190 | 0 | ctxt->state->seq); |
10191 | 0 | if (ret == -1) { |
10192 | 0 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
10193 | 0 | } else if (ret == 0) { |
10194 | 0 | ctxt->state->seq = NULL; |
10195 | 0 | } |
10196 | 0 | if (content != NULL) |
10197 | 0 | xmlFree(content); |
10198 | 0 | break; |
10199 | 0 | } |
10200 | 0 | case XML_RELAXNG_VALUE:{ |
10201 | 0 | xmlChar *content = NULL; |
10202 | 0 | xmlChar *oldvalue; |
10203 | 0 | xmlNodePtr child; |
10204 | |
|
10205 | 0 | child = node; |
10206 | 0 | while (child != NULL) { |
10207 | 0 | if (child->type == XML_ELEMENT_NODE) { |
10208 | 0 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
10209 | 0 | node->parent->name); |
10210 | 0 | ret = -1; |
10211 | 0 | break; |
10212 | 0 | } else if ((child->type == XML_TEXT_NODE) || |
10213 | 0 | (child->type == XML_CDATA_SECTION_NODE)) { |
10214 | 0 | content = xmlStrcat(content, child->content); |
10215 | 0 | } |
10216 | | /* TODO: handle entities ... */ |
10217 | 0 | child = child->next; |
10218 | 0 | } |
10219 | 0 | if (ret == -1) { |
10220 | 0 | if (content != NULL) |
10221 | 0 | xmlFree(content); |
10222 | 0 | break; |
10223 | 0 | } |
10224 | 0 | if (content == NULL) { |
10225 | 0 | content = xmlStrdup(BAD_CAST ""); |
10226 | 0 | if (content == NULL) { |
10227 | 0 | xmlRngVErrMemory(ctxt); |
10228 | 0 | ret = -1; |
10229 | 0 | break; |
10230 | 0 | } |
10231 | 0 | } |
10232 | 0 | oldvalue = ctxt->state->value; |
10233 | 0 | ctxt->state->value = content; |
10234 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define); |
10235 | 0 | ctxt->state->value = oldvalue; |
10236 | 0 | if (ret == -1) { |
10237 | 0 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
10238 | 0 | } else if (ret == 0) { |
10239 | 0 | ctxt->state->seq = NULL; |
10240 | 0 | } |
10241 | 0 | if (content != NULL) |
10242 | 0 | xmlFree(content); |
10243 | 0 | break; |
10244 | 0 | } |
10245 | 0 | case XML_RELAXNG_LIST:{ |
10246 | 0 | xmlChar *content; |
10247 | 0 | xmlNodePtr child; |
10248 | 0 | xmlChar *oldvalue, *oldendvalue; |
10249 | 0 | int len; |
10250 | | |
10251 | | /* |
10252 | | * Make sure it's only text nodes |
10253 | | */ |
10254 | |
|
10255 | 0 | content = NULL; |
10256 | 0 | child = node; |
10257 | 0 | while (child != NULL) { |
10258 | 0 | if (child->type == XML_ELEMENT_NODE) { |
10259 | 0 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
10260 | 0 | node->parent->name); |
10261 | 0 | ret = -1; |
10262 | 0 | break; |
10263 | 0 | } else if ((child->type == XML_TEXT_NODE) || |
10264 | 0 | (child->type == XML_CDATA_SECTION_NODE)) { |
10265 | 0 | content = xmlStrcat(content, child->content); |
10266 | 0 | } |
10267 | | /* TODO: handle entities ... */ |
10268 | 0 | child = child->next; |
10269 | 0 | } |
10270 | 0 | if (ret == -1) { |
10271 | 0 | if (content != NULL) |
10272 | 0 | xmlFree(content); |
10273 | 0 | break; |
10274 | 0 | } |
10275 | 0 | if (content == NULL) { |
10276 | 0 | content = xmlStrdup(BAD_CAST ""); |
10277 | 0 | if (content == NULL) { |
10278 | 0 | xmlRngVErrMemory(ctxt); |
10279 | 0 | ret = -1; |
10280 | 0 | break; |
10281 | 0 | } |
10282 | 0 | } |
10283 | 0 | len = xmlStrlen(content); |
10284 | 0 | oldvalue = ctxt->state->value; |
10285 | 0 | oldendvalue = ctxt->state->endvalue; |
10286 | 0 | ctxt->state->value = content; |
10287 | 0 | ctxt->state->endvalue = content + len; |
10288 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define); |
10289 | 0 | ctxt->state->value = oldvalue; |
10290 | 0 | ctxt->state->endvalue = oldendvalue; |
10291 | 0 | if (ret == -1) { |
10292 | 0 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
10293 | 0 | } else if ((ret == 0) && (node != NULL)) { |
10294 | 0 | ctxt->state->seq = node->next; |
10295 | 0 | } |
10296 | 0 | if (content != NULL) |
10297 | 0 | xmlFree(content); |
10298 | 0 | break; |
10299 | 0 | } |
10300 | 0 | case XML_RELAXNG_EXCEPT: |
10301 | 0 | case XML_RELAXNG_PARAM: |
10302 | | /* TODO */ |
10303 | 0 | ret = -1; |
10304 | 0 | break; |
10305 | 0 | } |
10306 | 0 | ctxt->depth--; |
10307 | 0 | return (ret); |
10308 | 0 | } |
10309 | | |
10310 | | /** |
10311 | | * Validate the current node lists against the definition |
10312 | | * |
10313 | | * @param ctxt a Relax-NG validation context |
10314 | | * @param define the definition to verify |
10315 | | * @returns 0 if the validation succeeded or an error code. |
10316 | | */ |
10317 | | static int |
10318 | | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
10319 | | xmlRelaxNGDefinePtr define) |
10320 | 0 | { |
10321 | 0 | xmlRelaxNGStatesPtr states, res; |
10322 | 0 | int i, j, k, ret, oldflags; |
10323 | | |
10324 | | /* |
10325 | | * We should NOT have both ctxt->state and ctxt->states |
10326 | | */ |
10327 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10328 | | /* TODO */ |
10329 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10330 | 0 | ctxt->state = NULL; |
10331 | 0 | } |
10332 | |
|
10333 | 0 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
10334 | 0 | if (ctxt->states != NULL) { |
10335 | 0 | ctxt->state = ctxt->states->tabState[0]; |
10336 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10337 | 0 | ctxt->states = NULL; |
10338 | 0 | } |
10339 | 0 | ret = xmlRelaxNGValidateState(ctxt, define); |
10340 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10341 | | /* TODO */ |
10342 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10343 | 0 | ctxt->state = NULL; |
10344 | 0 | } |
10345 | 0 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
10346 | 0 | ctxt->state = ctxt->states->tabState[0]; |
10347 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10348 | 0 | ctxt->states = NULL; |
10349 | 0 | } |
10350 | 0 | return (ret); |
10351 | 0 | } |
10352 | | |
10353 | 0 | states = ctxt->states; |
10354 | 0 | ctxt->states = NULL; |
10355 | 0 | res = NULL; |
10356 | 0 | j = 0; |
10357 | 0 | oldflags = ctxt->flags; |
10358 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
10359 | 0 | for (i = 0; i < states->nbState; i++) { |
10360 | 0 | ctxt->state = states->tabState[i]; |
10361 | 0 | ctxt->states = NULL; |
10362 | 0 | ret = xmlRelaxNGValidateState(ctxt, define); |
10363 | | /* |
10364 | | * We should NOT have both ctxt->state and ctxt->states |
10365 | | */ |
10366 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10367 | | /* TODO */ |
10368 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10369 | 0 | ctxt->state = NULL; |
10370 | 0 | } |
10371 | 0 | if (ret == 0) { |
10372 | 0 | if (ctxt->states == NULL) { |
10373 | 0 | if (res != NULL) { |
10374 | | /* add the state to the container */ |
10375 | 0 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
10376 | 0 | ctxt->state = NULL; |
10377 | 0 | } else { |
10378 | | /* add the state directly in states */ |
10379 | 0 | states->tabState[j++] = ctxt->state; |
10380 | 0 | ctxt->state = NULL; |
10381 | 0 | } |
10382 | 0 | } else { |
10383 | 0 | if (res == NULL) { |
10384 | | /* make it the new container and copy other results */ |
10385 | 0 | res = ctxt->states; |
10386 | 0 | ctxt->states = NULL; |
10387 | 0 | for (k = 0; k < j; k++) |
10388 | 0 | xmlRelaxNGAddStates(ctxt, res, |
10389 | 0 | states->tabState[k]); |
10390 | 0 | } else { |
10391 | | /* add all the new results to res and reff the container */ |
10392 | 0 | for (k = 0; k < ctxt->states->nbState; k++) |
10393 | 0 | xmlRelaxNGAddStates(ctxt, res, |
10394 | 0 | ctxt->states->tabState[k]); |
10395 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10396 | 0 | ctxt->states = NULL; |
10397 | 0 | } |
10398 | 0 | } |
10399 | 0 | } else { |
10400 | 0 | if (ctxt->state != NULL) { |
10401 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10402 | 0 | ctxt->state = NULL; |
10403 | 0 | } else if (ctxt->states != NULL) { |
10404 | 0 | for (k = 0; k < ctxt->states->nbState; k++) |
10405 | 0 | xmlRelaxNGFreeValidState(ctxt, |
10406 | 0 | ctxt->states->tabState[k]); |
10407 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10408 | 0 | ctxt->states = NULL; |
10409 | 0 | } |
10410 | 0 | } |
10411 | 0 | } |
10412 | 0 | ctxt->flags = oldflags; |
10413 | 0 | if (res != NULL) { |
10414 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10415 | 0 | ctxt->states = res; |
10416 | 0 | ret = 0; |
10417 | 0 | } else if (j > 1) { |
10418 | 0 | states->nbState = j; |
10419 | 0 | ctxt->states = states; |
10420 | 0 | ret = 0; |
10421 | 0 | } else if (j == 1) { |
10422 | 0 | ctxt->state = states->tabState[0]; |
10423 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10424 | 0 | ret = 0; |
10425 | 0 | } else { |
10426 | 0 | ret = -1; |
10427 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10428 | 0 | if (ctxt->states != NULL) { |
10429 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10430 | 0 | ctxt->states = NULL; |
10431 | 0 | } |
10432 | 0 | } |
10433 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10434 | | /* TODO */ |
10435 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10436 | 0 | ctxt->state = NULL; |
10437 | 0 | } |
10438 | 0 | return (ret); |
10439 | 0 | } |
10440 | | |
10441 | | /** |
10442 | | * Validate the given document |
10443 | | * |
10444 | | * @param ctxt a Relax-NG validation context |
10445 | | * @param doc the document |
10446 | | * @returns 0 if the validation succeeded or an error code. |
10447 | | */ |
10448 | | static int |
10449 | | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
10450 | 0 | { |
10451 | 0 | int ret; |
10452 | 0 | xmlRelaxNGPtr schema; |
10453 | 0 | xmlRelaxNGGrammarPtr grammar; |
10454 | 0 | xmlRelaxNGValidStatePtr state; |
10455 | 0 | xmlNodePtr node; |
10456 | |
|
10457 | 0 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
10458 | 0 | return (-1); |
10459 | | |
10460 | 0 | ctxt->errNo = XML_RELAXNG_OK; |
10461 | 0 | schema = ctxt->schema; |
10462 | 0 | grammar = schema->topgrammar; |
10463 | 0 | if (grammar == NULL) { |
10464 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
10465 | 0 | return (-1); |
10466 | 0 | } |
10467 | 0 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
10468 | 0 | ctxt->state = state; |
10469 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
10470 | 0 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
10471 | 0 | state = ctxt->state; |
10472 | 0 | node = state->seq; |
10473 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
10474 | 0 | if (node != NULL) { |
10475 | 0 | if (ret != -1) { |
10476 | 0 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
10477 | 0 | ret = -1; |
10478 | 0 | } |
10479 | 0 | } |
10480 | 0 | } else if (ctxt->states != NULL) { |
10481 | 0 | int i; |
10482 | 0 | int tmp = -1; |
10483 | |
|
10484 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
10485 | 0 | state = ctxt->states->tabState[i]; |
10486 | 0 | node = state->seq; |
10487 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
10488 | 0 | if (node == NULL) |
10489 | 0 | tmp = 0; |
10490 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
10491 | 0 | } |
10492 | 0 | if (tmp == -1) { |
10493 | 0 | if (ret != -1) { |
10494 | 0 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
10495 | 0 | ret = -1; |
10496 | 0 | } |
10497 | 0 | } |
10498 | 0 | } |
10499 | 0 | if (ctxt->state != NULL) { |
10500 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10501 | 0 | ctxt->state = NULL; |
10502 | 0 | } |
10503 | 0 | if (ret != 0) |
10504 | 0 | xmlRelaxNGDumpValidError(ctxt); |
10505 | 0 | #ifdef LIBXML_VALID_ENABLED |
10506 | 0 | if (ctxt->idref == 1) { |
10507 | 0 | xmlValidCtxt vctxt; |
10508 | |
|
10509 | 0 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
10510 | 0 | vctxt.valid = 1; |
10511 | |
|
10512 | 0 | if (ctxt->error == NULL) { |
10513 | 0 | vctxt.error = xmlGenericError; |
10514 | 0 | vctxt.warning = xmlGenericError; |
10515 | 0 | vctxt.userData = xmlGenericErrorContext; |
10516 | 0 | } else { |
10517 | 0 | vctxt.error = ctxt->error; |
10518 | 0 | vctxt.warning = ctxt->warning; |
10519 | 0 | vctxt.userData = ctxt->userData; |
10520 | 0 | } |
10521 | |
|
10522 | 0 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
10523 | 0 | ret = -1; |
10524 | 0 | } |
10525 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
10526 | 0 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
10527 | 0 | ret = -1; |
10528 | |
|
10529 | 0 | return (ret); |
10530 | 0 | } |
10531 | | |
10532 | | /** |
10533 | | * Call this routine to speed up XPath computation on static documents. |
10534 | | * This stamps all the element nodes with the document order |
10535 | | * Like for line information, the order is kept in the element->content |
10536 | | * field, the value stored is actually - the node number (starting at -1) |
10537 | | * to be able to differentiate from line numbers. |
10538 | | * |
10539 | | * @param node an input element or document |
10540 | | * @returns the number of elements found in the document or -1 in case |
10541 | | * of error. |
10542 | | */ |
10543 | | static void |
10544 | 0 | xmlRelaxNGCleanPSVI(xmlNodePtr node) { |
10545 | 0 | xmlNodePtr cur; |
10546 | |
|
10547 | 0 | if ((node == NULL) || |
10548 | 0 | ((node->type != XML_ELEMENT_NODE) && |
10549 | 0 | (node->type != XML_DOCUMENT_NODE) && |
10550 | 0 | (node->type != XML_HTML_DOCUMENT_NODE))) |
10551 | 0 | return; |
10552 | 0 | if (node->type == XML_ELEMENT_NODE) |
10553 | 0 | node->psvi = NULL; |
10554 | |
|
10555 | 0 | cur = node->children; |
10556 | 0 | while (cur != NULL) { |
10557 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
10558 | 0 | cur->psvi = NULL; |
10559 | 0 | if (cur->children != NULL) { |
10560 | 0 | cur = cur->children; |
10561 | 0 | continue; |
10562 | 0 | } |
10563 | 0 | } |
10564 | 0 | if (cur->next != NULL) { |
10565 | 0 | cur = cur->next; |
10566 | 0 | continue; |
10567 | 0 | } |
10568 | 0 | do { |
10569 | 0 | cur = cur->parent; |
10570 | 0 | if (cur == NULL) |
10571 | 0 | break; |
10572 | 0 | if (cur == node) { |
10573 | 0 | cur = NULL; |
10574 | 0 | break; |
10575 | 0 | } |
10576 | 0 | if (cur->next != NULL) { |
10577 | 0 | cur = cur->next; |
10578 | 0 | break; |
10579 | 0 | } |
10580 | 0 | } while (cur != NULL); |
10581 | 0 | } |
10582 | 0 | } |
10583 | | /************************************************************************ |
10584 | | * * |
10585 | | * Validation interfaces * |
10586 | | * * |
10587 | | ************************************************************************/ |
10588 | | |
10589 | | /** |
10590 | | * Create an XML RelaxNGs validation context based on the given schema |
10591 | | * |
10592 | | * @param schema a precompiled XML RelaxNGs |
10593 | | * @returns the validation context or NULL in case of error |
10594 | | */ |
10595 | | xmlRelaxNGValidCtxt * |
10596 | | xmlRelaxNGNewValidCtxt(xmlRelaxNG *schema) |
10597 | 0 | { |
10598 | 0 | xmlRelaxNGValidCtxtPtr ret; |
10599 | |
|
10600 | 0 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
10601 | 0 | if (ret == NULL) { |
10602 | 0 | xmlRngVErrMemory(NULL); |
10603 | 0 | return (NULL); |
10604 | 0 | } |
10605 | 0 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
10606 | 0 | ret->schema = schema; |
10607 | 0 | ret->errNr = 0; |
10608 | 0 | ret->errMax = 0; |
10609 | 0 | ret->err = NULL; |
10610 | 0 | ret->errTab = NULL; |
10611 | 0 | if (schema != NULL) |
10612 | 0 | ret->idref = schema->idref; |
10613 | 0 | ret->states = NULL; |
10614 | 0 | ret->freeState = NULL; |
10615 | 0 | ret->freeStates = NULL; |
10616 | 0 | ret->errNo = XML_RELAXNG_OK; |
10617 | 0 | return (ret); |
10618 | 0 | } |
10619 | | |
10620 | | /** |
10621 | | * Free the resources associated to the schema validation context |
10622 | | * |
10623 | | * @param ctxt the schema validation context |
10624 | | */ |
10625 | | void |
10626 | | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxt *ctxt) |
10627 | 0 | { |
10628 | 0 | int k; |
10629 | |
|
10630 | 0 | if (ctxt == NULL) |
10631 | 0 | return; |
10632 | 0 | if (ctxt->states != NULL) |
10633 | 0 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
10634 | 0 | if (ctxt->freeState != NULL) { |
10635 | 0 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
10636 | 0 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
10637 | 0 | } |
10638 | 0 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
10639 | 0 | } |
10640 | 0 | if (ctxt->freeStates != NULL) { |
10641 | 0 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
10642 | 0 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
10643 | 0 | } |
10644 | 0 | xmlFree(ctxt->freeStates); |
10645 | 0 | } |
10646 | 0 | if (ctxt->errTab != NULL) |
10647 | 0 | xmlFree(ctxt->errTab); |
10648 | 0 | if (ctxt->elemTab != NULL) { |
10649 | 0 | xmlRegExecCtxtPtr exec; |
10650 | |
|
10651 | 0 | exec = xmlRelaxNGElemPop(ctxt); |
10652 | 0 | while (exec != NULL) { |
10653 | 0 | xmlRegFreeExecCtxt(exec); |
10654 | 0 | exec = xmlRelaxNGElemPop(ctxt); |
10655 | 0 | } |
10656 | 0 | xmlFree(ctxt->elemTab); |
10657 | 0 | } |
10658 | 0 | xmlFree(ctxt); |
10659 | 0 | } |
10660 | | |
10661 | | /** |
10662 | | * Set the error and warning callback information |
10663 | | * |
10664 | | * @deprecated Use #xmlRelaxNGSetValidStructuredErrors. |
10665 | | * |
10666 | | * @param ctxt a Relax-NG validation context |
10667 | | * @param err the error function |
10668 | | * @param warn the warning function |
10669 | | * @param ctx the functions context |
10670 | | */ |
10671 | | void |
10672 | | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxt *ctxt, |
10673 | | xmlRelaxNGValidityErrorFunc err, |
10674 | | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
10675 | 0 | { |
10676 | 0 | if (ctxt == NULL) |
10677 | 0 | return; |
10678 | 0 | ctxt->error = err; |
10679 | 0 | ctxt->warning = warn; |
10680 | 0 | ctxt->userData = ctx; |
10681 | 0 | ctxt->serror = NULL; |
10682 | 0 | } |
10683 | | |
10684 | | /** |
10685 | | * Set the structured error callback |
10686 | | * |
10687 | | * @param ctxt a Relax-NG validation context |
10688 | | * @param serror the structured error function |
10689 | | * @param ctx the functions context |
10690 | | */ |
10691 | | void |
10692 | | xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxt *ctxt, |
10693 | | xmlStructuredErrorFunc serror, void *ctx) |
10694 | 0 | { |
10695 | 0 | if (ctxt == NULL) |
10696 | 0 | return; |
10697 | 0 | ctxt->serror = serror; |
10698 | 0 | ctxt->error = NULL; |
10699 | 0 | ctxt->warning = NULL; |
10700 | 0 | ctxt->userData = ctx; |
10701 | 0 | } |
10702 | | |
10703 | | /** |
10704 | | * Get the error and warning callback information |
10705 | | * |
10706 | | * @param ctxt a Relax-NG validation context |
10707 | | * @param err the error function result |
10708 | | * @param warn the warning function result |
10709 | | * @param ctx the functions context result |
10710 | | * @returns -1 in case of error and 0 otherwise |
10711 | | */ |
10712 | | int |
10713 | | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxt *ctxt, |
10714 | | xmlRelaxNGValidityErrorFunc * err, |
10715 | | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
10716 | 0 | { |
10717 | 0 | if (ctxt == NULL) |
10718 | 0 | return (-1); |
10719 | 0 | if (err != NULL) |
10720 | 0 | *err = ctxt->error; |
10721 | 0 | if (warn != NULL) |
10722 | 0 | *warn = ctxt->warning; |
10723 | 0 | if (ctx != NULL) |
10724 | 0 | *ctx = ctxt->userData; |
10725 | 0 | return (0); |
10726 | 0 | } |
10727 | | |
10728 | | /** |
10729 | | * Validate a document tree in memory. |
10730 | | * |
10731 | | * @param ctxt a Relax-NG validation context |
10732 | | * @param doc a parsed document tree |
10733 | | * @returns 0 if the document is valid, a positive error code |
10734 | | * number otherwise and -1 in case of internal or API error. |
10735 | | */ |
10736 | | int |
10737 | | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxt *ctxt, xmlDoc *doc) |
10738 | 0 | { |
10739 | 0 | int ret; |
10740 | |
|
10741 | 0 | if ((ctxt == NULL) || (doc == NULL)) |
10742 | 0 | return (-1); |
10743 | | |
10744 | 0 | ctxt->doc = doc; |
10745 | |
|
10746 | 0 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
10747 | | /* |
10748 | | * Remove all left PSVI |
10749 | | */ |
10750 | 0 | xmlRelaxNGCleanPSVI((xmlNodePtr) doc); |
10751 | | |
10752 | | /* |
10753 | | * TODO: build error codes |
10754 | | */ |
10755 | 0 | if (ret == -1) |
10756 | 0 | return (1); |
10757 | 0 | return (ret); |
10758 | 0 | } |
10759 | | |
10760 | | #endif /* LIBXML_RELAXNG_ENABLED */ |