Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * relaxng.c : implementation of the Relax-NG handling and validity checking |
3 | | * |
4 | | * See Copyright for the status of this software. |
5 | | * |
6 | | * 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 <string.h> |
22 | | #include <stdio.h> |
23 | | #include <stddef.h> |
24 | | #include <libxml/xmlmemory.h> |
25 | | #include <libxml/parser.h> |
26 | | #include <libxml/parserInternals.h> |
27 | | #include <libxml/hash.h> |
28 | | #include <libxml/uri.h> |
29 | | |
30 | | #include <libxml/relaxng.h> |
31 | | |
32 | | #include <libxml/xmlautomata.h> |
33 | | #include <libxml/xmlregexp.h> |
34 | | #include <libxml/xmlschemastypes.h> |
35 | | |
36 | | #include "private/error.h" |
37 | | #include "private/regexp.h" |
38 | | #include "private/string.h" |
39 | | #include "private/threads.h" |
40 | | |
41 | | /* |
42 | | * The Relax-NG namespace |
43 | | */ |
44 | | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
45 | | "http://relaxng.org/ns/structure/1.0"; |
46 | | |
47 | | #define IS_RELAXNG(node, typ) \ |
48 | 0 | ((node != NULL) && (node->ns != NULL) && \ |
49 | 0 | (node->type == XML_ELEMENT_NODE) && \ |
50 | 0 | (xmlStrEqual(node->name, (const xmlChar *) typ)) && \ |
51 | 0 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
52 | | |
53 | | |
54 | 0 | #define MAX_ERROR 5 |
55 | | |
56 | | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
57 | | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
58 | | |
59 | | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
60 | | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
61 | | |
62 | | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
63 | | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
64 | | |
65 | | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
66 | | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
67 | | |
68 | | typedef enum { |
69 | | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
70 | | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
71 | | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
72 | | } xmlRelaxNGCombine; |
73 | | |
74 | | typedef enum { |
75 | | XML_RELAXNG_CONTENT_ERROR = -1, |
76 | | XML_RELAXNG_CONTENT_EMPTY = 0, |
77 | | XML_RELAXNG_CONTENT_SIMPLE, |
78 | | XML_RELAXNG_CONTENT_COMPLEX |
79 | | } xmlRelaxNGContentType; |
80 | | |
81 | | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
82 | | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
83 | | |
84 | | struct _xmlRelaxNGGrammar { |
85 | | xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */ |
86 | | xmlRelaxNGGrammarPtr children; /* the children grammar if any */ |
87 | | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
88 | | xmlRelaxNGDefinePtr start; /* <start> content */ |
89 | | xmlRelaxNGCombine combine; /* the default combine value */ |
90 | | xmlRelaxNGDefinePtr startList; /* list of <start> definitions */ |
91 | | xmlHashTablePtr defs; /* define* */ |
92 | | xmlHashTablePtr refs; /* references */ |
93 | | }; |
94 | | |
95 | | |
96 | | typedef enum { |
97 | | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
98 | | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
99 | | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
100 | | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
101 | | XML_RELAXNG_TEXT, /* textual content */ |
102 | | XML_RELAXNG_ELEMENT, /* an element */ |
103 | | XML_RELAXNG_DATATYPE, /* external data type definition */ |
104 | | XML_RELAXNG_PARAM, /* external data type parameter */ |
105 | | XML_RELAXNG_VALUE, /* value from an external data type definition */ |
106 | | XML_RELAXNG_LIST, /* a list of patterns */ |
107 | | XML_RELAXNG_ATTRIBUTE, /* an attribute following a pattern */ |
108 | | XML_RELAXNG_DEF, /* a definition */ |
109 | | XML_RELAXNG_REF, /* reference to a definition */ |
110 | | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
111 | | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
112 | | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
113 | | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
114 | | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
115 | | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
116 | | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
117 | | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
118 | | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
119 | | } xmlRelaxNGType; |
120 | | |
121 | 0 | #define IS_NULLABLE (1 << 0) |
122 | 0 | #define IS_NOT_NULLABLE (1 << 1) |
123 | 0 | #define IS_INDETERMINIST (1 << 2) |
124 | 0 | #define IS_MIXED (1 << 3) |
125 | 0 | #define IS_TRIABLE (1 << 4) |
126 | 0 | #define IS_PROCESSED (1 << 5) |
127 | 0 | #define IS_COMPILABLE (1 << 6) |
128 | 0 | #define IS_NOT_COMPILABLE (1 << 7) |
129 | 0 | #define IS_EXTERNAL_REF (1 << 8) |
130 | | |
131 | | struct _xmlRelaxNGDefine { |
132 | | xmlRelaxNGType type; /* the type of definition */ |
133 | | xmlNodePtr node; /* the node in the source */ |
134 | | xmlChar *name; /* the element local name if present */ |
135 | | xmlChar *ns; /* the namespace local name if present */ |
136 | | xmlChar *value; /* value when available */ |
137 | | void *data; /* data lib or specific pointer */ |
138 | | xmlRelaxNGDefinePtr content; /* the expected content */ |
139 | | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
140 | | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
141 | | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
142 | | xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */ |
143 | | xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */ |
144 | | short depth; /* used for the cycle detection */ |
145 | | short dflags; /* define related flags */ |
146 | | xmlRegexpPtr contModel; /* a compiled content model if available */ |
147 | | }; |
148 | | |
149 | | /** |
150 | | * A RelaxNGs definition |
151 | | */ |
152 | | struct _xmlRelaxNG { |
153 | | void *_private; /* unused by the library for users or bindings */ |
154 | | xmlRelaxNGGrammarPtr topgrammar; |
155 | | xmlDocPtr doc; |
156 | | |
157 | | int idref; /* requires idref checking */ |
158 | | |
159 | | xmlHashTablePtr defs; /* define */ |
160 | | xmlHashTablePtr refs; /* references */ |
161 | | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
162 | | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
163 | | int defNr; /* number of defines used */ |
164 | | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
165 | | |
166 | | }; |
167 | | |
168 | 0 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
169 | 0 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
170 | 0 | #define XML_RELAXNG_IN_LIST (1 << 2) |
171 | 0 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
172 | 0 | #define XML_RELAXNG_IN_START (1 << 4) |
173 | 0 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
174 | 0 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
175 | 0 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
176 | 0 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
177 | 0 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
178 | | |
179 | | struct _xmlRelaxNGParserCtxt { |
180 | | void *userData; /* user specific data block */ |
181 | | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
182 | | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
183 | | xmlStructuredErrorFunc serror; |
184 | | xmlRelaxNGValidErr err; |
185 | | |
186 | | xmlRelaxNGPtr schema; /* The schema in use */ |
187 | | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
188 | | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
189 | | int flags; /* parser flags */ |
190 | | int nbErrors; /* number of errors at parse time */ |
191 | | int nbWarnings; /* number of warnings at parse time */ |
192 | | const xmlChar *define; /* the current define scope */ |
193 | | xmlRelaxNGDefinePtr def; /* the current define */ |
194 | | |
195 | | int nbInterleaves; |
196 | | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
197 | | |
198 | | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
199 | | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
200 | | xmlChar *URL; |
201 | | xmlDocPtr document; |
202 | | |
203 | | int defNr; /* number of defines used */ |
204 | | int defMax; /* number of defines allocated */ |
205 | | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
206 | | |
207 | | const char *buffer; |
208 | | int size; |
209 | | |
210 | | /* the document stack */ |
211 | | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
212 | | int docNr; /* Depth of the parsing stack */ |
213 | | int docMax; /* Max depth of the parsing stack */ |
214 | | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
215 | | |
216 | | /* the include stack */ |
217 | | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
218 | | int incNr; /* Depth of the include parsing stack */ |
219 | | int incMax; /* Max depth of the parsing stack */ |
220 | | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
221 | | |
222 | | int idref; /* requires idref checking */ |
223 | | |
224 | | /* used to compile content models */ |
225 | | xmlAutomataPtr am; /* the automata */ |
226 | | xmlAutomataStatePtr state; /* used to build the automata */ |
227 | | |
228 | | int crng; /* compact syntax and other flags */ |
229 | | int freedoc; /* need to free the document */ |
230 | | |
231 | | xmlResourceLoader resourceLoader; |
232 | | void *resourceCtxt; |
233 | | }; |
234 | | |
235 | 0 | #define FLAGS_IGNORABLE 1 |
236 | 0 | #define FLAGS_NEGATIVE 2 |
237 | 0 | #define FLAGS_MIXED_CONTENT 4 |
238 | 0 | #define FLAGS_NOERROR 8 |
239 | | |
240 | | /** |
241 | | * A RelaxNGs partition set associated to lists of definitions |
242 | | */ |
243 | | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
244 | | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
245 | | struct _xmlRelaxNGInterleaveGroup { |
246 | | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
247 | | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
248 | | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
249 | | }; |
250 | | |
251 | 0 | #define IS_DETERMINIST 1 |
252 | 0 | #define IS_NEEDCHECK 2 |
253 | | |
254 | | /** |
255 | | * A RelaxNGs partition associated to an interleave group |
256 | | */ |
257 | | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
258 | | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
259 | | struct _xmlRelaxNGPartition { |
260 | | int nbgroups; /* number of groups in the partitions */ |
261 | | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
262 | | * right group when possible */ |
263 | | int flags; /* determinist ? */ |
264 | | xmlRelaxNGInterleaveGroupPtr *groups; |
265 | | }; |
266 | | |
267 | | /** |
268 | | * A RelaxNGs validation state |
269 | | */ |
270 | 0 | #define MAX_ATTR 20 |
271 | | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
272 | | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
273 | | struct _xmlRelaxNGValidState { |
274 | | xmlNodePtr node; /* the current node */ |
275 | | xmlNodePtr seq; /* the sequence of children left to validate */ |
276 | | int nbAttrs; /* the number of attributes */ |
277 | | int maxAttrs; /* the size of attrs */ |
278 | | int nbAttrLeft; /* the number of attributes left to validate */ |
279 | | xmlChar *value; /* the value when operating on string */ |
280 | | xmlChar *endvalue; /* the end value when operating on string */ |
281 | | xmlAttrPtr *attrs; /* the array of attributes */ |
282 | | }; |
283 | | |
284 | | /** |
285 | | * A RelaxNGs container for validation state |
286 | | */ |
287 | | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
288 | | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
289 | | struct _xmlRelaxNGStates { |
290 | | int nbState; /* the number of states */ |
291 | | int maxState; /* the size of the array */ |
292 | | xmlRelaxNGValidStatePtr *tabState; |
293 | | }; |
294 | | |
295 | 0 | #define ERROR_IS_DUP 1 |
296 | | |
297 | | /** |
298 | | * A RelaxNGs validation error |
299 | | */ |
300 | | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
301 | | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
302 | | struct _xmlRelaxNGValidError { |
303 | | xmlRelaxNGValidErr err; /* the error number */ |
304 | | int flags; /* flags */ |
305 | | xmlNodePtr node; /* the current node */ |
306 | | xmlNodePtr seq; /* the current child */ |
307 | | const xmlChar *arg1; /* first arg */ |
308 | | const xmlChar *arg2; /* second arg */ |
309 | | }; |
310 | | |
311 | | /** |
312 | | * A RelaxNGs validation context |
313 | | */ |
314 | | |
315 | | struct _xmlRelaxNGValidCtxt { |
316 | | void *userData; /* user specific data block */ |
317 | | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
318 | | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
319 | | xmlStructuredErrorFunc serror; |
320 | | int nbErrors; /* number of errors in validation */ |
321 | | |
322 | | xmlRelaxNGPtr schema; /* The schema in use */ |
323 | | xmlDocPtr doc; /* the document being validated */ |
324 | | int flags; /* validation flags */ |
325 | | int depth; /* validation depth */ |
326 | | int idref; /* requires idref checking */ |
327 | | int errNo; /* the first error found */ |
328 | | |
329 | | /* |
330 | | * Errors accumulated in branches may have to be stacked to be |
331 | | * provided back when it's sure they affect validation. |
332 | | */ |
333 | | xmlRelaxNGValidErrorPtr err; /* Last error */ |
334 | | int errNr; /* Depth of the error stack */ |
335 | | int errMax; /* Max depth of the error stack */ |
336 | | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
337 | | |
338 | | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
339 | | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
340 | | |
341 | | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
342 | | int freeStatesNr; |
343 | | int freeStatesMax; |
344 | | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
345 | | |
346 | | /* |
347 | | * This is used for "progressive" validation |
348 | | */ |
349 | | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
350 | | int elemNr; /* the number of element validated */ |
351 | | int elemMax; /* the max depth of elements */ |
352 | | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
353 | | int pstate; /* progressive state */ |
354 | | xmlNodePtr pnode; /* the current node */ |
355 | | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
356 | | int perr; /* signal error in content model |
357 | | * outside the regexp */ |
358 | | }; |
359 | | |
360 | | /** |
361 | | * Structure associated to a RelaxNGs document element |
362 | | */ |
363 | | struct _xmlRelaxNGInclude { |
364 | | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
365 | | xmlChar *href; /* the normalized href value */ |
366 | | xmlDocPtr doc; /* the associated XML document */ |
367 | | xmlRelaxNGDefinePtr content; /* the definitions */ |
368 | | xmlRelaxNGPtr schema; /* the schema */ |
369 | | }; |
370 | | |
371 | | /** |
372 | | * Structure associated to a RelaxNGs document element |
373 | | */ |
374 | | struct _xmlRelaxNGDocument { |
375 | | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
376 | | xmlChar *href; /* the normalized href value */ |
377 | | xmlDocPtr doc; /* the associated XML document */ |
378 | | xmlRelaxNGDefinePtr content; /* the definitions */ |
379 | | xmlRelaxNGPtr schema; /* the schema */ |
380 | | int externalRef; /* 1 if an external ref */ |
381 | | }; |
382 | | |
383 | | |
384 | | /************************************************************************ |
385 | | * * |
386 | | * Some factorized error routines * |
387 | | * * |
388 | | ************************************************************************/ |
389 | | |
390 | | /** |
391 | | * Handle a redefinition of attribute error |
392 | | * |
393 | | * @param ctxt an Relax-NG parser context |
394 | | */ |
395 | | static void |
396 | | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt) |
397 | 0 | { |
398 | 0 | xmlStructuredErrorFunc schannel = NULL; |
399 | 0 | xmlGenericErrorFunc channel = NULL; |
400 | 0 | void *data = NULL; |
401 | |
|
402 | 0 | if (ctxt != NULL) { |
403 | 0 | if (ctxt->serror != NULL) |
404 | 0 | schannel = ctxt->serror; |
405 | 0 | else |
406 | 0 | channel = ctxt->error; |
407 | 0 | data = ctxt->userData; |
408 | 0 | ctxt->nbErrors++; |
409 | 0 | } |
410 | |
|
411 | 0 | xmlRaiseMemoryError(schannel, channel, data, XML_FROM_RELAXNGP, NULL); |
412 | 0 | } |
413 | | |
414 | | /** |
415 | | * Handle a redefinition of attribute error |
416 | | * |
417 | | * @param ctxt a Relax-NG validation context |
418 | | */ |
419 | | static void |
420 | | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt) |
421 | 0 | { |
422 | 0 | xmlStructuredErrorFunc schannel = NULL; |
423 | 0 | xmlGenericErrorFunc channel = NULL; |
424 | 0 | void *data = NULL; |
425 | |
|
426 | 0 | if (ctxt != NULL) { |
427 | 0 | if (ctxt->serror != NULL) |
428 | 0 | schannel = ctxt->serror; |
429 | 0 | else |
430 | 0 | channel = ctxt->error; |
431 | 0 | data = ctxt->userData; |
432 | 0 | ctxt->nbErrors++; |
433 | 0 | } |
434 | |
|
435 | 0 | xmlRaiseMemoryError(schannel, channel, data, XML_FROM_RELAXNGV, NULL); |
436 | 0 | } |
437 | | |
438 | | /** |
439 | | * Handle a Relax NG Parsing error |
440 | | * |
441 | | * @param ctxt a Relax-NG parser context |
442 | | * @param node the node raising the error |
443 | | * @param error the error code |
444 | | * @param msg message |
445 | | * @param str1 extra info |
446 | | * @param str2 extra info |
447 | | */ |
448 | | static void LIBXML_ATTR_FORMAT(4,0) |
449 | | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
450 | | const char *msg, const xmlChar * str1, const xmlChar * str2) |
451 | 0 | { |
452 | 0 | xmlStructuredErrorFunc schannel = NULL; |
453 | 0 | xmlGenericErrorFunc channel = NULL; |
454 | 0 | void *data = NULL; |
455 | 0 | int res; |
456 | |
|
457 | 0 | if (ctxt != NULL) { |
458 | 0 | if (ctxt->serror != NULL) |
459 | 0 | schannel = ctxt->serror; |
460 | 0 | else |
461 | 0 | channel = ctxt->error; |
462 | 0 | data = ctxt->userData; |
463 | 0 | ctxt->nbErrors++; |
464 | 0 | } |
465 | |
|
466 | 0 | if ((channel == NULL) && (schannel == NULL)) { |
467 | 0 | channel = xmlGenericError; |
468 | 0 | data = xmlGenericErrorContext; |
469 | 0 | } |
470 | |
|
471 | 0 | res = xmlRaiseError(schannel, channel, data, NULL, node, |
472 | 0 | XML_FROM_RELAXNGP, error, XML_ERR_ERROR, NULL, 0, |
473 | 0 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
474 | 0 | msg, str1, str2); |
475 | 0 | if (res < 0) |
476 | 0 | xmlRngPErrMemory(ctxt); |
477 | 0 | } |
478 | | |
479 | | /** |
480 | | * Handle a Relax NG Validation error |
481 | | * |
482 | | * @param ctxt a Relax-NG validation context |
483 | | * @param node the node raising the error |
484 | | * @param error the error code |
485 | | * @param msg message |
486 | | * @param str1 extra info |
487 | | * @param str2 extra info |
488 | | */ |
489 | | static void LIBXML_ATTR_FORMAT(4,0) |
490 | | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
491 | | const char *msg, const xmlChar * str1, const xmlChar * str2) |
492 | 0 | { |
493 | 0 | xmlStructuredErrorFunc schannel = NULL; |
494 | 0 | xmlGenericErrorFunc channel = NULL; |
495 | 0 | void *data = NULL; |
496 | 0 | int res; |
497 | |
|
498 | 0 | if (ctxt != NULL) { |
499 | 0 | if (ctxt->serror != NULL) |
500 | 0 | schannel = ctxt->serror; |
501 | 0 | else |
502 | 0 | channel = ctxt->error; |
503 | 0 | data = ctxt->userData; |
504 | 0 | ctxt->nbErrors++; |
505 | 0 | } |
506 | |
|
507 | 0 | if ((channel == NULL) && (schannel == NULL)) { |
508 | 0 | channel = xmlGenericError; |
509 | 0 | data = xmlGenericErrorContext; |
510 | 0 | } |
511 | |
|
512 | 0 | res = xmlRaiseError(schannel, channel, data, NULL, node, |
513 | 0 | XML_FROM_RELAXNGV, error, XML_ERR_ERROR, NULL, 0, |
514 | 0 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
515 | 0 | msg, str1, str2); |
516 | 0 | if (res < 0) |
517 | 0 | xmlRngVErrMemory(ctxt); |
518 | 0 | } |
519 | | |
520 | | /************************************************************************ |
521 | | * * |
522 | | * Preliminary type checking interfaces * |
523 | | * * |
524 | | ************************************************************************/ |
525 | | |
526 | | /** |
527 | | * Function provided by a type library to check if a type is exported |
528 | | * |
529 | | * @param data data needed for the library |
530 | | * @param type the type name |
531 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
532 | | */ |
533 | | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
534 | | |
535 | | /** |
536 | | * Function provided by a type library to check if a value match a type |
537 | | * |
538 | | * @param data data needed for the library |
539 | | * @param type the type name |
540 | | * @param value the value to check |
541 | | * @param result place to store the result if needed |
542 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
543 | | */ |
544 | | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
545 | | const xmlChar * value, void **result, |
546 | | xmlNode *node); |
547 | | |
548 | | /** |
549 | | * Function provided by a type library to check a value facet |
550 | | * |
551 | | * @param data data needed for the library |
552 | | * @param type the type name |
553 | | * @param facet the facet name |
554 | | * @param val the facet value |
555 | | * @param strval the string value |
556 | | * @param value the value to check |
557 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
558 | | */ |
559 | | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
560 | | const xmlChar * facet, |
561 | | const xmlChar * val, |
562 | | const xmlChar * strval, void *value); |
563 | | |
564 | | /** |
565 | | * Function provided by a type library to free a returned result |
566 | | * |
567 | | * @param data data needed for the library |
568 | | * @param result the value to free |
569 | | */ |
570 | | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
571 | | |
572 | | /** |
573 | | * Function provided by a type library to compare two values accordingly |
574 | | * to a type. |
575 | | * |
576 | | * @param data data needed for the library |
577 | | * @param type the type name |
578 | | * @param value1 the first value |
579 | | * @param value2 the second value |
580 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
581 | | */ |
582 | | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
583 | | const xmlChar * value1, |
584 | | xmlNode *ctxt1, |
585 | | void *comp1, |
586 | | const xmlChar * value2, |
587 | | xmlNode *ctxt2); |
588 | | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
589 | | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
590 | | struct _xmlRelaxNGTypeLibrary { |
591 | | const xmlChar *namespace; /* the datatypeLibrary value */ |
592 | | void *data; /* data needed for the library */ |
593 | | xmlRelaxNGTypeHave have; /* the export function */ |
594 | | xmlRelaxNGTypeCheck check; /* the checking function */ |
595 | | xmlRelaxNGTypeCompare comp; /* the compare function */ |
596 | | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
597 | | xmlRelaxNGTypeFree freef; /* the freeing function */ |
598 | | }; |
599 | | |
600 | | /************************************************************************ |
601 | | * * |
602 | | * Allocation functions * |
603 | | * * |
604 | | ************************************************************************/ |
605 | | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
606 | | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
607 | | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
608 | | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
609 | | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
610 | | ATTRIBUTE_UNUSED, |
611 | | xmlRelaxNGValidStatePtr state1, |
612 | | xmlRelaxNGValidStatePtr state2); |
613 | | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
614 | | xmlRelaxNGValidStatePtr state); |
615 | | |
616 | | /** |
617 | | * Deallocate a RelaxNG document structure. |
618 | | * |
619 | | * @param docu a document structure |
620 | | */ |
621 | | static void |
622 | | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
623 | 0 | { |
624 | 0 | if (docu == NULL) |
625 | 0 | return; |
626 | | |
627 | 0 | if (docu->href != NULL) |
628 | 0 | xmlFree(docu->href); |
629 | 0 | if (docu->doc != NULL) |
630 | 0 | xmlFreeDoc(docu->doc); |
631 | 0 | if (docu->schema != NULL) |
632 | 0 | xmlRelaxNGFreeInnerSchema(docu->schema); |
633 | 0 | xmlFree(docu); |
634 | 0 | } |
635 | | |
636 | | /** |
637 | | * Deallocate a RelaxNG document structures. |
638 | | * |
639 | | * @param docu a list of document structure |
640 | | */ |
641 | | static void |
642 | | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
643 | 0 | { |
644 | 0 | xmlRelaxNGDocumentPtr next; |
645 | |
|
646 | 0 | while (docu != NULL) { |
647 | 0 | next = docu->next; |
648 | 0 | xmlRelaxNGFreeDocument(docu); |
649 | 0 | docu = next; |
650 | 0 | } |
651 | 0 | } |
652 | | |
653 | | /** |
654 | | * Deallocate a RelaxNG include structure. |
655 | | * |
656 | | * @param incl a include structure |
657 | | */ |
658 | | static void |
659 | | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
660 | 0 | { |
661 | 0 | if (incl == NULL) |
662 | 0 | return; |
663 | | |
664 | 0 | if (incl->href != NULL) |
665 | 0 | xmlFree(incl->href); |
666 | 0 | if (incl->doc != NULL) |
667 | 0 | xmlFreeDoc(incl->doc); |
668 | 0 | if (incl->schema != NULL) |
669 | 0 | xmlRelaxNGFree(incl->schema); |
670 | 0 | xmlFree(incl); |
671 | 0 | } |
672 | | |
673 | | /** |
674 | | * Deallocate a RelaxNG include structure. |
675 | | * |
676 | | * @param incl a include structure list |
677 | | */ |
678 | | static void |
679 | | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
680 | 0 | { |
681 | 0 | xmlRelaxNGIncludePtr next; |
682 | |
|
683 | 0 | while (incl != NULL) { |
684 | 0 | next = incl->next; |
685 | 0 | xmlRelaxNGFreeInclude(incl); |
686 | 0 | incl = next; |
687 | 0 | } |
688 | 0 | } |
689 | | |
690 | | /** |
691 | | * Allocate a new RelaxNG structure. |
692 | | * |
693 | | * @param ctxt a Relax-NG validation context (optional) |
694 | | * @returns the newly allocated structure or NULL in case or error |
695 | | */ |
696 | | static xmlRelaxNGPtr |
697 | | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
698 | 0 | { |
699 | 0 | xmlRelaxNGPtr ret; |
700 | |
|
701 | 0 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
702 | 0 | if (ret == NULL) { |
703 | 0 | xmlRngPErrMemory(ctxt); |
704 | 0 | return (NULL); |
705 | 0 | } |
706 | 0 | memset(ret, 0, sizeof(xmlRelaxNG)); |
707 | |
|
708 | 0 | return (ret); |
709 | 0 | } |
710 | | |
711 | | /** |
712 | | * Deallocate a RelaxNG schema structure. |
713 | | * |
714 | | * @param schema a schema structure |
715 | | */ |
716 | | static void |
717 | | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
718 | 0 | { |
719 | 0 | if (schema == NULL) |
720 | 0 | return; |
721 | | |
722 | 0 | if (schema->doc != NULL) |
723 | 0 | xmlFreeDoc(schema->doc); |
724 | 0 | if (schema->defTab != NULL) { |
725 | 0 | int i; |
726 | |
|
727 | 0 | for (i = 0; i < schema->defNr; i++) |
728 | 0 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
729 | 0 | xmlFree(schema->defTab); |
730 | 0 | } |
731 | |
|
732 | 0 | xmlFree(schema); |
733 | 0 | } |
734 | | |
735 | | /** |
736 | | * Deallocate a RelaxNG structure. |
737 | | * |
738 | | * @param schema a schema structure |
739 | | */ |
740 | | void |
741 | | xmlRelaxNGFree(xmlRelaxNG *schema) |
742 | 0 | { |
743 | 0 | if (schema == NULL) |
744 | 0 | return; |
745 | | |
746 | 0 | if (schema->topgrammar != NULL) |
747 | 0 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
748 | 0 | if (schema->doc != NULL) |
749 | 0 | xmlFreeDoc(schema->doc); |
750 | 0 | if (schema->documents != NULL) |
751 | 0 | xmlRelaxNGFreeDocumentList(schema->documents); |
752 | 0 | if (schema->includes != NULL) |
753 | 0 | xmlRelaxNGFreeIncludeList(schema->includes); |
754 | 0 | if (schema->defTab != NULL) { |
755 | 0 | int i; |
756 | |
|
757 | 0 | for (i = 0; i < schema->defNr; i++) |
758 | 0 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
759 | 0 | xmlFree(schema->defTab); |
760 | 0 | } |
761 | |
|
762 | 0 | xmlFree(schema); |
763 | 0 | } |
764 | | |
765 | | /** |
766 | | * Allocate a new RelaxNG grammar. |
767 | | * |
768 | | * @param ctxt a Relax-NG validation context (optional) |
769 | | * @returns the newly allocated structure or NULL in case or error |
770 | | */ |
771 | | static xmlRelaxNGGrammarPtr |
772 | | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
773 | 0 | { |
774 | 0 | xmlRelaxNGGrammarPtr ret; |
775 | |
|
776 | 0 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
777 | 0 | if (ret == NULL) { |
778 | 0 | xmlRngPErrMemory(ctxt); |
779 | 0 | return (NULL); |
780 | 0 | } |
781 | 0 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
782 | |
|
783 | 0 | return (ret); |
784 | 0 | } |
785 | | |
786 | | /** |
787 | | * Deallocate a RelaxNG grammar structure. |
788 | | * |
789 | | * @param grammar a grammar structure |
790 | | */ |
791 | | static void |
792 | | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
793 | 0 | { |
794 | 0 | if (grammar == NULL) |
795 | 0 | return; |
796 | | |
797 | 0 | if (grammar->children != NULL) { |
798 | 0 | xmlRelaxNGFreeGrammar(grammar->children); |
799 | 0 | } |
800 | 0 | if (grammar->next != NULL) { |
801 | 0 | xmlRelaxNGFreeGrammar(grammar->next); |
802 | 0 | } |
803 | 0 | if (grammar->refs != NULL) { |
804 | 0 | xmlHashFree(grammar->refs, NULL); |
805 | 0 | } |
806 | 0 | if (grammar->defs != NULL) { |
807 | 0 | xmlHashFree(grammar->defs, NULL); |
808 | 0 | } |
809 | |
|
810 | 0 | xmlFree(grammar); |
811 | 0 | } |
812 | | |
813 | | /** |
814 | | * Allocate a new RelaxNG define. |
815 | | * |
816 | | * @param ctxt a Relax-NG validation context |
817 | | * @param node the node in the input document. |
818 | | * @returns the newly allocated structure or NULL in case or error |
819 | | */ |
820 | | static xmlRelaxNGDefinePtr |
821 | | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
822 | 0 | { |
823 | 0 | xmlRelaxNGDefinePtr ret; |
824 | |
|
825 | 0 | if (ctxt->defMax == 0) { |
826 | 0 | ctxt->defMax = 16; |
827 | 0 | ctxt->defNr = 0; |
828 | 0 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
829 | 0 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
830 | 0 | if (ctxt->defTab == NULL) { |
831 | 0 | xmlRngPErrMemory(ctxt); |
832 | 0 | return (NULL); |
833 | 0 | } |
834 | 0 | } else if (ctxt->defMax <= ctxt->defNr) { |
835 | 0 | xmlRelaxNGDefinePtr *tmp; |
836 | |
|
837 | 0 | ctxt->defMax *= 2; |
838 | 0 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
839 | 0 | ctxt->defMax * |
840 | 0 | sizeof |
841 | 0 | (xmlRelaxNGDefinePtr)); |
842 | 0 | if (tmp == NULL) { |
843 | 0 | xmlRngPErrMemory(ctxt); |
844 | 0 | return (NULL); |
845 | 0 | } |
846 | 0 | ctxt->defTab = tmp; |
847 | 0 | } |
848 | 0 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
849 | 0 | if (ret == NULL) { |
850 | 0 | xmlRngPErrMemory(ctxt); |
851 | 0 | return (NULL); |
852 | 0 | } |
853 | 0 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
854 | 0 | ctxt->defTab[ctxt->defNr++] = ret; |
855 | 0 | ret->node = node; |
856 | 0 | ret->depth = -1; |
857 | 0 | return (ret); |
858 | 0 | } |
859 | | |
860 | | /** |
861 | | * Deallocate RelaxNG partition set structures. |
862 | | * |
863 | | * @param partitions a partition set structure |
864 | | */ |
865 | | static void |
866 | | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
867 | 0 | { |
868 | 0 | xmlRelaxNGInterleaveGroupPtr group; |
869 | 0 | int j; |
870 | |
|
871 | 0 | if (partitions != NULL) { |
872 | 0 | if (partitions->groups != NULL) { |
873 | 0 | for (j = 0; j < partitions->nbgroups; j++) { |
874 | 0 | group = partitions->groups[j]; |
875 | 0 | if (group != NULL) { |
876 | 0 | if (group->defs != NULL) |
877 | 0 | xmlFree(group->defs); |
878 | 0 | if (group->attrs != NULL) |
879 | 0 | xmlFree(group->attrs); |
880 | 0 | xmlFree(group); |
881 | 0 | } |
882 | 0 | } |
883 | 0 | xmlFree(partitions->groups); |
884 | 0 | } |
885 | 0 | if (partitions->triage != NULL) { |
886 | 0 | xmlHashFree(partitions->triage, NULL); |
887 | 0 | } |
888 | 0 | xmlFree(partitions); |
889 | 0 | } |
890 | 0 | } |
891 | | |
892 | | /** |
893 | | * Deallocate a RelaxNG define structure. |
894 | | * |
895 | | * @param define a define structure |
896 | | */ |
897 | | static void |
898 | | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
899 | 0 | { |
900 | 0 | if (define == NULL) |
901 | 0 | return; |
902 | | |
903 | 0 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
904 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
905 | |
|
906 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
907 | 0 | if ((lib != NULL) && (lib->freef != NULL)) |
908 | 0 | lib->freef(lib->data, (void *) define->attrs); |
909 | 0 | } |
910 | 0 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
911 | 0 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
912 | 0 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
913 | 0 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
914 | 0 | if (define->name != NULL) |
915 | 0 | xmlFree(define->name); |
916 | 0 | if (define->ns != NULL) |
917 | 0 | xmlFree(define->ns); |
918 | 0 | if (define->value != NULL) |
919 | 0 | xmlFree(define->value); |
920 | 0 | if (define->contModel != NULL) |
921 | 0 | xmlRegFreeRegexp(define->contModel); |
922 | 0 | xmlFree(define); |
923 | 0 | } |
924 | | |
925 | | /** |
926 | | * Allocate a new RelaxNG validation state container |
927 | | * |
928 | | * @param ctxt a Relax-NG validation context |
929 | | * @param size the default size for the container |
930 | | * @returns the newly allocated structure or NULL in case or error |
931 | | */ |
932 | | static xmlRelaxNGStatesPtr |
933 | | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
934 | 0 | { |
935 | 0 | xmlRelaxNGStatesPtr ret; |
936 | |
|
937 | 0 | if ((ctxt != NULL) && |
938 | 0 | (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) { |
939 | 0 | ctxt->freeStatesNr--; |
940 | 0 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
941 | 0 | ret->nbState = 0; |
942 | 0 | return (ret); |
943 | 0 | } |
944 | 0 | if (size < 16) |
945 | 0 | size = 16; |
946 | |
|
947 | 0 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
948 | 0 | (size - |
949 | 0 | 1) * |
950 | 0 | sizeof(xmlRelaxNGValidStatePtr)); |
951 | 0 | if (ret == NULL) { |
952 | 0 | xmlRngVErrMemory(ctxt); |
953 | 0 | return (NULL); |
954 | 0 | } |
955 | 0 | ret->nbState = 0; |
956 | 0 | ret->maxState = size; |
957 | 0 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
958 | 0 | sizeof |
959 | 0 | (xmlRelaxNGValidStatePtr)); |
960 | 0 | if (ret->tabState == NULL) { |
961 | 0 | xmlRngVErrMemory(ctxt); |
962 | 0 | xmlFree(ret); |
963 | 0 | return (NULL); |
964 | 0 | } |
965 | 0 | return (ret); |
966 | 0 | } |
967 | | |
968 | | /** |
969 | | * Add a RelaxNG validation state to the container without checking |
970 | | * for unicity. |
971 | | * |
972 | | * @param ctxt a Relax-NG validation context |
973 | | * @param states the states container |
974 | | * @param state the validation state |
975 | | * @returns 1 in case of success and 0 if this is a duplicate and -1 on error |
976 | | */ |
977 | | static int |
978 | | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
979 | | xmlRelaxNGStatesPtr states, |
980 | | xmlRelaxNGValidStatePtr state) |
981 | 0 | { |
982 | 0 | if (state == NULL) { |
983 | 0 | return (-1); |
984 | 0 | } |
985 | 0 | if (states->nbState >= states->maxState) { |
986 | 0 | xmlRelaxNGValidStatePtr *tmp; |
987 | 0 | int size; |
988 | |
|
989 | 0 | size = states->maxState * 2; |
990 | 0 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
991 | 0 | (size) * |
992 | 0 | sizeof |
993 | 0 | (xmlRelaxNGValidStatePtr)); |
994 | 0 | if (tmp == NULL) { |
995 | 0 | xmlRngVErrMemory(ctxt); |
996 | 0 | return (-1); |
997 | 0 | } |
998 | 0 | states->tabState = tmp; |
999 | 0 | states->maxState = size; |
1000 | 0 | } |
1001 | 0 | states->tabState[states->nbState++] = state; |
1002 | 0 | return (1); |
1003 | 0 | } |
1004 | | |
1005 | | /** |
1006 | | * Add a RelaxNG validation state to the container |
1007 | | * |
1008 | | * @param ctxt a Relax-NG validation context |
1009 | | * @param states the states container |
1010 | | * @param state the validation state |
1011 | | * @returns 1 in case of success and 0 if this is a duplicate and -1 on error |
1012 | | */ |
1013 | | static int |
1014 | | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
1015 | | xmlRelaxNGStatesPtr states, |
1016 | | xmlRelaxNGValidStatePtr state) |
1017 | 0 | { |
1018 | 0 | int i; |
1019 | |
|
1020 | 0 | if (state == NULL || states == NULL) { |
1021 | 0 | return (-1); |
1022 | 0 | } |
1023 | 0 | if (states->nbState >= states->maxState) { |
1024 | 0 | xmlRelaxNGValidStatePtr *tmp; |
1025 | 0 | int size; |
1026 | |
|
1027 | 0 | size = states->maxState * 2; |
1028 | 0 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
1029 | 0 | (size) * |
1030 | 0 | sizeof |
1031 | 0 | (xmlRelaxNGValidStatePtr)); |
1032 | 0 | if (tmp == NULL) { |
1033 | 0 | xmlRngVErrMemory(ctxt); |
1034 | 0 | return (-1); |
1035 | 0 | } |
1036 | 0 | states->tabState = tmp; |
1037 | 0 | states->maxState = size; |
1038 | 0 | } |
1039 | 0 | for (i = 0; i < states->nbState; i++) { |
1040 | 0 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
1041 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
1042 | 0 | return (0); |
1043 | 0 | } |
1044 | 0 | } |
1045 | 0 | states->tabState[states->nbState++] = state; |
1046 | 0 | return (1); |
1047 | 0 | } |
1048 | | |
1049 | | /** |
1050 | | * Free a RelaxNG validation state container |
1051 | | * |
1052 | | * @param ctxt a Relax-NG validation context |
1053 | | * @param states the container |
1054 | | */ |
1055 | | static void |
1056 | | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
1057 | | xmlRelaxNGStatesPtr states) |
1058 | 0 | { |
1059 | 0 | if (states == NULL) |
1060 | 0 | return; |
1061 | 0 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
1062 | 0 | ctxt->freeStatesMax = 40; |
1063 | 0 | ctxt->freeStatesNr = 0; |
1064 | 0 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
1065 | 0 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
1066 | 0 | if (ctxt->freeStates == NULL) { |
1067 | 0 | xmlRngVErrMemory(ctxt); |
1068 | 0 | } |
1069 | 0 | } else if ((ctxt != NULL) |
1070 | 0 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
1071 | 0 | xmlRelaxNGStatesPtr *tmp; |
1072 | |
|
1073 | 0 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
1074 | 0 | 2 * ctxt->freeStatesMax * |
1075 | 0 | sizeof |
1076 | 0 | (xmlRelaxNGStatesPtr)); |
1077 | 0 | if (tmp == NULL) { |
1078 | 0 | xmlRngVErrMemory(ctxt); |
1079 | 0 | xmlFree(states->tabState); |
1080 | 0 | xmlFree(states); |
1081 | 0 | return; |
1082 | 0 | } |
1083 | 0 | ctxt->freeStates = tmp; |
1084 | 0 | ctxt->freeStatesMax *= 2; |
1085 | 0 | } |
1086 | 0 | if ((ctxt == NULL) || (ctxt->freeStates == NULL)) { |
1087 | 0 | xmlFree(states->tabState); |
1088 | 0 | xmlFree(states); |
1089 | 0 | } else { |
1090 | 0 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
1091 | 0 | } |
1092 | 0 | } |
1093 | | |
1094 | | /** |
1095 | | * Allocate a new RelaxNG validation state |
1096 | | * |
1097 | | * @param ctxt a Relax-NG validation context |
1098 | | * @param node the current node or NULL for the document |
1099 | | * @returns the newly allocated structure or NULL in case or error |
1100 | | */ |
1101 | | static xmlRelaxNGValidStatePtr |
1102 | | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
1103 | 0 | { |
1104 | 0 | xmlRelaxNGValidStatePtr ret; |
1105 | 0 | xmlAttrPtr attr; |
1106 | 0 | xmlAttrPtr attrs[MAX_ATTR]; |
1107 | 0 | int nbAttrs = 0; |
1108 | 0 | xmlNodePtr root = NULL; |
1109 | |
|
1110 | 0 | if (node == NULL) { |
1111 | 0 | root = xmlDocGetRootElement(ctxt->doc); |
1112 | 0 | if (root == NULL) |
1113 | 0 | return (NULL); |
1114 | 0 | } else { |
1115 | 0 | attr = node->properties; |
1116 | 0 | while (attr != NULL) { |
1117 | 0 | if (nbAttrs < MAX_ATTR) |
1118 | 0 | attrs[nbAttrs++] = attr; |
1119 | 0 | else |
1120 | 0 | nbAttrs++; |
1121 | 0 | attr = attr->next; |
1122 | 0 | } |
1123 | 0 | } |
1124 | 0 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
1125 | 0 | ctxt->freeState->nbState--; |
1126 | 0 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
1127 | 0 | } else { |
1128 | 0 | ret = |
1129 | 0 | (xmlRelaxNGValidStatePtr) |
1130 | 0 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
1131 | 0 | if (ret == NULL) { |
1132 | 0 | xmlRngVErrMemory(ctxt); |
1133 | 0 | return (NULL); |
1134 | 0 | } |
1135 | 0 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
1136 | 0 | } |
1137 | 0 | ret->value = NULL; |
1138 | 0 | ret->endvalue = NULL; |
1139 | 0 | if (node == NULL) { |
1140 | 0 | ret->node = (xmlNodePtr) ctxt->doc; |
1141 | 0 | ret->seq = root; |
1142 | 0 | } else { |
1143 | 0 | ret->node = node; |
1144 | 0 | ret->seq = node->children; |
1145 | 0 | } |
1146 | 0 | ret->nbAttrs = 0; |
1147 | 0 | if (nbAttrs > 0) { |
1148 | 0 | if (ret->attrs == NULL) { |
1149 | 0 | if (nbAttrs < 4) |
1150 | 0 | ret->maxAttrs = 4; |
1151 | 0 | else |
1152 | 0 | ret->maxAttrs = nbAttrs; |
1153 | 0 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
1154 | 0 | sizeof(xmlAttrPtr)); |
1155 | 0 | if (ret->attrs == NULL) { |
1156 | 0 | xmlRngVErrMemory(ctxt); |
1157 | 0 | return (ret); |
1158 | 0 | } |
1159 | 0 | } else if (ret->maxAttrs < nbAttrs) { |
1160 | 0 | xmlAttrPtr *tmp; |
1161 | |
|
1162 | 0 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
1163 | 0 | sizeof(xmlAttrPtr)); |
1164 | 0 | if (tmp == NULL) { |
1165 | 0 | xmlRngVErrMemory(ctxt); |
1166 | 0 | return (ret); |
1167 | 0 | } |
1168 | 0 | ret->attrs = tmp; |
1169 | 0 | ret->maxAttrs = nbAttrs; |
1170 | 0 | } |
1171 | 0 | ret->nbAttrs = nbAttrs; |
1172 | 0 | if (nbAttrs < MAX_ATTR) { |
1173 | 0 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
1174 | 0 | } else { |
1175 | 0 | attr = node->properties; |
1176 | 0 | nbAttrs = 0; |
1177 | 0 | while (attr != NULL) { |
1178 | 0 | ret->attrs[nbAttrs++] = attr; |
1179 | 0 | attr = attr->next; |
1180 | 0 | } |
1181 | 0 | } |
1182 | 0 | } |
1183 | 0 | ret->nbAttrLeft = ret->nbAttrs; |
1184 | 0 | return (ret); |
1185 | 0 | } |
1186 | | |
1187 | | /** |
1188 | | * Copy the validation state |
1189 | | * |
1190 | | * @param ctxt a Relax-NG validation context |
1191 | | * @param state a validation state |
1192 | | * @returns the newly allocated structure or NULL in case or error |
1193 | | */ |
1194 | | static xmlRelaxNGValidStatePtr |
1195 | | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
1196 | | xmlRelaxNGValidStatePtr state) |
1197 | 0 | { |
1198 | 0 | xmlRelaxNGValidStatePtr ret; |
1199 | 0 | unsigned int maxAttrs; |
1200 | 0 | xmlAttrPtr *attrs; |
1201 | |
|
1202 | 0 | if (state == NULL) |
1203 | 0 | return (NULL); |
1204 | 0 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
1205 | 0 | ctxt->freeState->nbState--; |
1206 | 0 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
1207 | 0 | } else { |
1208 | 0 | ret = |
1209 | 0 | (xmlRelaxNGValidStatePtr) |
1210 | 0 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
1211 | 0 | if (ret == NULL) { |
1212 | 0 | xmlRngVErrMemory(ctxt); |
1213 | 0 | return (NULL); |
1214 | 0 | } |
1215 | 0 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
1216 | 0 | } |
1217 | 0 | attrs = ret->attrs; |
1218 | 0 | maxAttrs = ret->maxAttrs; |
1219 | 0 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
1220 | 0 | ret->attrs = attrs; |
1221 | 0 | ret->maxAttrs = maxAttrs; |
1222 | 0 | if (state->nbAttrs > 0) { |
1223 | 0 | if (ret->attrs == NULL) { |
1224 | 0 | ret->maxAttrs = state->maxAttrs; |
1225 | 0 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
1226 | 0 | sizeof(xmlAttrPtr)); |
1227 | 0 | if (ret->attrs == NULL) { |
1228 | 0 | xmlRngVErrMemory(ctxt); |
1229 | 0 | ret->nbAttrs = 0; |
1230 | 0 | return (ret); |
1231 | 0 | } |
1232 | 0 | } else if (ret->maxAttrs < state->nbAttrs) { |
1233 | 0 | xmlAttrPtr *tmp; |
1234 | |
|
1235 | 0 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
1236 | 0 | sizeof(xmlAttrPtr)); |
1237 | 0 | if (tmp == NULL) { |
1238 | 0 | xmlRngVErrMemory(ctxt); |
1239 | 0 | ret->nbAttrs = 0; |
1240 | 0 | return (ret); |
1241 | 0 | } |
1242 | 0 | ret->maxAttrs = state->maxAttrs; |
1243 | 0 | ret->attrs = tmp; |
1244 | 0 | } |
1245 | 0 | memcpy(ret->attrs, state->attrs, |
1246 | 0 | state->nbAttrs * sizeof(xmlAttrPtr)); |
1247 | 0 | } |
1248 | 0 | return (ret); |
1249 | 0 | } |
1250 | | |
1251 | | /** |
1252 | | * Compare the validation states for equality |
1253 | | * |
1254 | | * @param ctxt a Relax-NG validation context |
1255 | | * @param state1 a validation state |
1256 | | * @param state2 a validation state |
1257 | | * @returns 1 if equal, 0 otherwise |
1258 | | */ |
1259 | | static int |
1260 | | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
1261 | | xmlRelaxNGValidStatePtr state1, |
1262 | | xmlRelaxNGValidStatePtr state2) |
1263 | 0 | { |
1264 | 0 | int i; |
1265 | |
|
1266 | 0 | if ((state1 == NULL) || (state2 == NULL)) |
1267 | 0 | return (0); |
1268 | 0 | if (state1 == state2) |
1269 | 0 | return (1); |
1270 | 0 | if (state1->node != state2->node) |
1271 | 0 | return (0); |
1272 | 0 | if (state1->seq != state2->seq) |
1273 | 0 | return (0); |
1274 | 0 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
1275 | 0 | return (0); |
1276 | 0 | if (state1->nbAttrs != state2->nbAttrs) |
1277 | 0 | return (0); |
1278 | 0 | if (state1->endvalue != state2->endvalue) |
1279 | 0 | return (0); |
1280 | 0 | if ((state1->value != state2->value) && |
1281 | 0 | (!xmlStrEqual(state1->value, state2->value))) |
1282 | 0 | return (0); |
1283 | 0 | for (i = 0; i < state1->nbAttrs; i++) { |
1284 | 0 | if (state1->attrs[i] != state2->attrs[i]) |
1285 | 0 | return (0); |
1286 | 0 | } |
1287 | 0 | return (1); |
1288 | 0 | } |
1289 | | |
1290 | | /** |
1291 | | * Deallocate a RelaxNG validation state structure. |
1292 | | * |
1293 | | * @param ctxt validation context |
1294 | | * @param state a validation state structure |
1295 | | */ |
1296 | | static void |
1297 | | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
1298 | | xmlRelaxNGValidStatePtr state) |
1299 | 0 | { |
1300 | 0 | if (state == NULL) |
1301 | 0 | return; |
1302 | | |
1303 | 0 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
1304 | 0 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
1305 | 0 | } |
1306 | 0 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
1307 | 0 | if (state->attrs != NULL) |
1308 | 0 | xmlFree(state->attrs); |
1309 | 0 | xmlFree(state); |
1310 | 0 | } else { |
1311 | 0 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
1312 | 0 | } |
1313 | 0 | } |
1314 | | |
1315 | | /************************************************************************ |
1316 | | * * |
1317 | | * Semi internal functions * |
1318 | | * * |
1319 | | ************************************************************************/ |
1320 | | |
1321 | | /** |
1322 | | * Semi private function used to pass information to a parser context |
1323 | | * which are a combination of xmlRelaxNGParserFlag . |
1324 | | * |
1325 | | * @param ctxt a RelaxNG parser context |
1326 | | * @param flags a set of flags values |
1327 | | * @returns 0 if success and -1 in case of error |
1328 | | */ |
1329 | | int |
1330 | | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxt *ctxt, int flags) |
1331 | 0 | { |
1332 | 0 | if (ctxt == NULL) return(-1); |
1333 | 0 | if (flags & XML_RELAXNGP_FREE_DOC) { |
1334 | 0 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
1335 | 0 | flags -= XML_RELAXNGP_FREE_DOC; |
1336 | 0 | } |
1337 | 0 | if (flags & XML_RELAXNGP_CRNG) { |
1338 | 0 | ctxt->crng |= XML_RELAXNGP_CRNG; |
1339 | 0 | flags -= XML_RELAXNGP_CRNG; |
1340 | 0 | } |
1341 | 0 | if (flags != 0) return(-1); |
1342 | 0 | return(0); |
1343 | 0 | } |
1344 | | |
1345 | | /************************************************************************ |
1346 | | * * |
1347 | | * Document functions * |
1348 | | * * |
1349 | | ************************************************************************/ |
1350 | | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
1351 | | xmlDocPtr doc); |
1352 | | |
1353 | | static xmlDoc * |
1354 | 0 | xmlRelaxReadFile(xmlRelaxNGParserCtxtPtr ctxt, const char *filename) { |
1355 | 0 | xmlParserCtxtPtr pctxt; |
1356 | 0 | xmlDocPtr doc; |
1357 | |
|
1358 | 0 | pctxt = xmlNewParserCtxt(); |
1359 | 0 | if (pctxt == NULL) { |
1360 | 0 | xmlRngPErrMemory(ctxt); |
1361 | 0 | return(NULL); |
1362 | 0 | } |
1363 | 0 | if (ctxt->serror != NULL) |
1364 | 0 | xmlCtxtSetErrorHandler(pctxt, ctxt->serror, ctxt->userData); |
1365 | 0 | if (ctxt->resourceLoader != NULL) |
1366 | 0 | xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader, |
1367 | 0 | ctxt->resourceCtxt); |
1368 | 0 | doc = xmlCtxtReadFile(pctxt, filename, NULL, 0); |
1369 | 0 | xmlFreeParserCtxt(pctxt); |
1370 | |
|
1371 | 0 | return(doc); |
1372 | 0 | } |
1373 | | |
1374 | | static xmlDoc * |
1375 | 0 | xmlRelaxReadMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *buf, int size) { |
1376 | 0 | xmlParserCtxtPtr pctxt; |
1377 | 0 | xmlDocPtr doc; |
1378 | |
|
1379 | 0 | pctxt = xmlNewParserCtxt(); |
1380 | 0 | if (pctxt == NULL) { |
1381 | 0 | xmlRngPErrMemory(ctxt); |
1382 | 0 | return(NULL); |
1383 | 0 | } |
1384 | 0 | if (ctxt->serror != NULL) |
1385 | 0 | xmlCtxtSetErrorHandler(pctxt, ctxt->serror, ctxt->userData); |
1386 | 0 | if (ctxt->resourceLoader != NULL) |
1387 | 0 | xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader, |
1388 | 0 | ctxt->resourceCtxt); |
1389 | 0 | doc = xmlCtxtReadMemory(pctxt, buf, size, NULL, NULL, 0); |
1390 | 0 | xmlFreeParserCtxt(pctxt); |
1391 | |
|
1392 | 0 | return(doc); |
1393 | 0 | } |
1394 | | |
1395 | | /** |
1396 | | * Pushes a new include on top of the include stack |
1397 | | * |
1398 | | * @param ctxt the parser context |
1399 | | * @param value the element doc |
1400 | | * @returns 0 in case of error, the index in the stack otherwise |
1401 | | */ |
1402 | | static int |
1403 | | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
1404 | | xmlRelaxNGIncludePtr value) |
1405 | 0 | { |
1406 | 0 | if (ctxt->incTab == NULL) { |
1407 | 0 | ctxt->incMax = 4; |
1408 | 0 | ctxt->incNr = 0; |
1409 | 0 | ctxt->incTab = |
1410 | 0 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
1411 | 0 | sizeof(ctxt->incTab[0])); |
1412 | 0 | if (ctxt->incTab == NULL) { |
1413 | 0 | xmlRngPErrMemory(ctxt); |
1414 | 0 | return (0); |
1415 | 0 | } |
1416 | 0 | } |
1417 | 0 | if (ctxt->incNr >= ctxt->incMax) { |
1418 | 0 | ctxt->incMax *= 2; |
1419 | 0 | ctxt->incTab = |
1420 | 0 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
1421 | 0 | ctxt->incMax * |
1422 | 0 | sizeof(ctxt->incTab[0])); |
1423 | 0 | if (ctxt->incTab == NULL) { |
1424 | 0 | xmlRngPErrMemory(ctxt); |
1425 | 0 | return (0); |
1426 | 0 | } |
1427 | 0 | } |
1428 | 0 | ctxt->incTab[ctxt->incNr] = value; |
1429 | 0 | ctxt->inc = value; |
1430 | 0 | return (ctxt->incNr++); |
1431 | 0 | } |
1432 | | |
1433 | | /** |
1434 | | * Pops the top include from the include stack |
1435 | | * |
1436 | | * @param ctxt the parser context |
1437 | | * @returns the include just removed |
1438 | | */ |
1439 | | static xmlRelaxNGIncludePtr |
1440 | | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
1441 | 0 | { |
1442 | 0 | xmlRelaxNGIncludePtr ret; |
1443 | |
|
1444 | 0 | if (ctxt->incNr <= 0) |
1445 | 0 | return (NULL); |
1446 | 0 | ctxt->incNr--; |
1447 | 0 | if (ctxt->incNr > 0) |
1448 | 0 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
1449 | 0 | else |
1450 | 0 | ctxt->inc = NULL; |
1451 | 0 | ret = ctxt->incTab[ctxt->incNr]; |
1452 | 0 | ctxt->incTab[ctxt->incNr] = NULL; |
1453 | 0 | return (ret); |
1454 | 0 | } |
1455 | | |
1456 | | /** |
1457 | | * Applies the elimination algorithm of 4.7 |
1458 | | * |
1459 | | * @param ctxt the parser context |
1460 | | * @param URL the normalized URL |
1461 | | * @param target the included target |
1462 | | * @param name the define name to eliminate |
1463 | | * @returns 0 in case of error, 1 in case of success. |
1464 | | */ |
1465 | | static int |
1466 | | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
1467 | | const xmlChar * URL ATTRIBUTE_UNUSED, |
1468 | | xmlNodePtr target, const xmlChar * name) |
1469 | 0 | { |
1470 | 0 | int found = 0; |
1471 | 0 | xmlNodePtr tmp, tmp2; |
1472 | 0 | xmlChar *name2; |
1473 | |
|
1474 | 0 | tmp = target; |
1475 | 0 | while (tmp != NULL) { |
1476 | 0 | tmp2 = tmp->next; |
1477 | 0 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
1478 | 0 | found = 1; |
1479 | 0 | xmlUnlinkNode(tmp); |
1480 | 0 | xmlFreeNode(tmp); |
1481 | 0 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
1482 | 0 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
1483 | 0 | xmlRelaxNGNormExtSpace(name2); |
1484 | 0 | if (name2 != NULL) { |
1485 | 0 | if (xmlStrEqual(name, name2)) { |
1486 | 0 | found = 1; |
1487 | 0 | xmlUnlinkNode(tmp); |
1488 | 0 | xmlFreeNode(tmp); |
1489 | 0 | } |
1490 | 0 | xmlFree(name2); |
1491 | 0 | } |
1492 | 0 | } else if (IS_RELAXNG(tmp, "include")) { |
1493 | 0 | xmlChar *href = NULL; |
1494 | 0 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
1495 | |
|
1496 | 0 | if ((inc != NULL) && (inc->doc != NULL) && |
1497 | 0 | (inc->doc->children != NULL)) { |
1498 | |
|
1499 | 0 | if (xmlStrEqual |
1500 | 0 | (inc->doc->children->name, BAD_CAST "grammar")) { |
1501 | 0 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
1502 | 0 | xmlDocGetRootElement(inc->doc)->children, |
1503 | 0 | name) == 1) { |
1504 | 0 | found = 1; |
1505 | 0 | } |
1506 | 0 | } |
1507 | 0 | } |
1508 | 0 | if (xmlRelaxNGRemoveRedefine(ctxt, URL, tmp->children, name) == 1) { |
1509 | 0 | found = 1; |
1510 | 0 | } |
1511 | 0 | } |
1512 | 0 | tmp = tmp2; |
1513 | 0 | } |
1514 | 0 | return (found); |
1515 | 0 | } |
1516 | | |
1517 | | /** |
1518 | | * First lookup if the document is already loaded into the parser context, |
1519 | | * check against recursion. If not found the resource is loaded and |
1520 | | * the content is preprocessed before being returned back to the caller. |
1521 | | * |
1522 | | * @param ctxt the parser context |
1523 | | * @param URL the normalized URL |
1524 | | * @param node the include node. |
1525 | | * @param ns the namespace passed from the context. |
1526 | | * @returns the xmlRelaxNGInclude or NULL in case of error |
1527 | | */ |
1528 | | static xmlRelaxNGIncludePtr |
1529 | | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
1530 | | xmlNodePtr node, const xmlChar * ns) |
1531 | 0 | { |
1532 | 0 | xmlRelaxNGIncludePtr ret = NULL; |
1533 | 0 | xmlDocPtr doc; |
1534 | 0 | int i; |
1535 | 0 | xmlNodePtr root, cur; |
1536 | | |
1537 | | /* |
1538 | | * check against recursion in the stack |
1539 | | */ |
1540 | 0 | for (i = 0; i < ctxt->incNr; i++) { |
1541 | 0 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
1542 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
1543 | 0 | "Detected an Include recursion for %s\n", URL, |
1544 | 0 | NULL); |
1545 | 0 | return (NULL); |
1546 | 0 | } |
1547 | 0 | } |
1548 | | |
1549 | | /* |
1550 | | * load the document |
1551 | | */ |
1552 | 0 | doc = xmlRelaxReadFile(ctxt, (const char *) URL); |
1553 | 0 | if (doc == NULL) { |
1554 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
1555 | 0 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
1556 | 0 | return (NULL); |
1557 | 0 | } |
1558 | | |
1559 | | /* |
1560 | | * Allocate the document structures and register it first. |
1561 | | */ |
1562 | 0 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
1563 | 0 | if (ret == NULL) { |
1564 | 0 | xmlRngPErrMemory(ctxt); |
1565 | 0 | xmlFreeDoc(doc); |
1566 | 0 | return (NULL); |
1567 | 0 | } |
1568 | 0 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
1569 | 0 | ret->doc = doc; |
1570 | 0 | ret->href = xmlStrdup(URL); |
1571 | 0 | ret->next = ctxt->includes; |
1572 | 0 | ctxt->includes = ret; |
1573 | | |
1574 | | /* |
1575 | | * transmit the ns if needed |
1576 | | */ |
1577 | 0 | if (ns != NULL) { |
1578 | 0 | root = xmlDocGetRootElement(doc); |
1579 | 0 | if (root != NULL) { |
1580 | 0 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
1581 | 0 | xmlSetProp(root, BAD_CAST "ns", ns); |
1582 | 0 | } |
1583 | 0 | } |
1584 | 0 | } |
1585 | | |
1586 | | /* |
1587 | | * push it on the stack |
1588 | | */ |
1589 | 0 | xmlRelaxNGIncludePush(ctxt, ret); |
1590 | | |
1591 | | /* |
1592 | | * Some preprocessing of the document content, this include recursing |
1593 | | * in the include stack. |
1594 | | */ |
1595 | |
|
1596 | 0 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
1597 | 0 | if (doc == NULL) { |
1598 | 0 | ctxt->inc = NULL; |
1599 | 0 | return (NULL); |
1600 | 0 | } |
1601 | | |
1602 | | /* |
1603 | | * Pop up the include from the stack |
1604 | | */ |
1605 | 0 | xmlRelaxNGIncludePop(ctxt); |
1606 | | |
1607 | | /* |
1608 | | * Check that the top element is a grammar |
1609 | | */ |
1610 | 0 | root = xmlDocGetRootElement(doc); |
1611 | 0 | if (root == NULL) { |
1612 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
1613 | 0 | "xmlRelaxNG: included document is empty %s\n", URL, |
1614 | 0 | NULL); |
1615 | 0 | return (NULL); |
1616 | 0 | } |
1617 | 0 | if (!IS_RELAXNG(root, "grammar")) { |
1618 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
1619 | 0 | "xmlRelaxNG: included document %s root is not a grammar\n", |
1620 | 0 | URL, NULL); |
1621 | 0 | return (NULL); |
1622 | 0 | } |
1623 | | |
1624 | | /* |
1625 | | * Elimination of redefined rules in the include. |
1626 | | */ |
1627 | 0 | cur = node->children; |
1628 | 0 | while (cur != NULL) { |
1629 | 0 | if (IS_RELAXNG(cur, "start")) { |
1630 | 0 | int found = 0; |
1631 | |
|
1632 | 0 | found = |
1633 | 0 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
1634 | 0 | if (!found) { |
1635 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
1636 | 0 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
1637 | 0 | URL, NULL); |
1638 | 0 | } |
1639 | 0 | } else if (IS_RELAXNG(cur, "define")) { |
1640 | 0 | xmlChar *name; |
1641 | |
|
1642 | 0 | name = xmlGetProp(cur, BAD_CAST "name"); |
1643 | 0 | if (name == NULL) { |
1644 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
1645 | 0 | "xmlRelaxNG: include %s has define without name\n", |
1646 | 0 | URL, NULL); |
1647 | 0 | } else { |
1648 | 0 | int found; |
1649 | |
|
1650 | 0 | xmlRelaxNGNormExtSpace(name); |
1651 | 0 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
1652 | 0 | root->children, name); |
1653 | 0 | if (!found) { |
1654 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
1655 | 0 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
1656 | 0 | URL, name); |
1657 | 0 | } |
1658 | 0 | xmlFree(name); |
1659 | 0 | } |
1660 | 0 | } |
1661 | 0 | if (IS_RELAXNG(cur, "div") && cur->children != NULL) { |
1662 | 0 | cur = cur->children; |
1663 | 0 | } else { |
1664 | 0 | if (cur->next != NULL) { |
1665 | 0 | cur = cur->next; |
1666 | 0 | } else { |
1667 | 0 | while (cur->parent != node && cur->parent->next == NULL) { |
1668 | 0 | cur = cur->parent; |
1669 | 0 | } |
1670 | 0 | cur = cur->parent != node ? cur->parent->next : NULL; |
1671 | 0 | } |
1672 | 0 | } |
1673 | 0 | } |
1674 | | |
1675 | |
|
1676 | 0 | return (ret); |
1677 | 0 | } |
1678 | | |
1679 | | /** |
1680 | | * Pushes a new error on top of the error stack |
1681 | | * |
1682 | | * @param ctxt the validation context |
1683 | | * @param err the error code |
1684 | | * @param arg1 the first string argument |
1685 | | * @param arg2 the second string argument |
1686 | | * @param dup arg need to be duplicated |
1687 | | * @returns 0 in case of error, the index in the stack otherwise |
1688 | | */ |
1689 | | static int |
1690 | | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
1691 | | xmlRelaxNGValidErr err, const xmlChar * arg1, |
1692 | | const xmlChar * arg2, int dup) |
1693 | 0 | { |
1694 | 0 | xmlRelaxNGValidErrorPtr cur; |
1695 | |
|
1696 | 0 | if (ctxt->errTab == NULL) { |
1697 | 0 | ctxt->errMax = 8; |
1698 | 0 | ctxt->errNr = 0; |
1699 | 0 | ctxt->errTab = |
1700 | 0 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
1701 | 0 | sizeof |
1702 | 0 | (xmlRelaxNGValidError)); |
1703 | 0 | if (ctxt->errTab == NULL) { |
1704 | 0 | xmlRngVErrMemory(ctxt); |
1705 | 0 | return (0); |
1706 | 0 | } |
1707 | 0 | ctxt->err = NULL; |
1708 | 0 | } |
1709 | 0 | if (ctxt->errNr >= ctxt->errMax) { |
1710 | 0 | ctxt->errMax *= 2; |
1711 | 0 | ctxt->errTab = |
1712 | 0 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
1713 | 0 | ctxt->errMax * |
1714 | 0 | sizeof |
1715 | 0 | (xmlRelaxNGValidError)); |
1716 | 0 | if (ctxt->errTab == NULL) { |
1717 | 0 | xmlRngVErrMemory(ctxt); |
1718 | 0 | return (0); |
1719 | 0 | } |
1720 | 0 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
1721 | 0 | } |
1722 | 0 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
1723 | 0 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
1724 | 0 | return (ctxt->errNr); |
1725 | 0 | cur = &ctxt->errTab[ctxt->errNr]; |
1726 | 0 | cur->err = err; |
1727 | 0 | if (dup) { |
1728 | 0 | cur->arg1 = xmlStrdup(arg1); |
1729 | 0 | cur->arg2 = xmlStrdup(arg2); |
1730 | 0 | cur->flags = ERROR_IS_DUP; |
1731 | 0 | } else { |
1732 | 0 | cur->arg1 = arg1; |
1733 | 0 | cur->arg2 = arg2; |
1734 | 0 | cur->flags = 0; |
1735 | 0 | } |
1736 | 0 | if (ctxt->state != NULL) { |
1737 | 0 | cur->node = ctxt->state->node; |
1738 | 0 | cur->seq = ctxt->state->seq; |
1739 | 0 | } else { |
1740 | 0 | cur->node = NULL; |
1741 | 0 | cur->seq = NULL; |
1742 | 0 | } |
1743 | 0 | ctxt->err = cur; |
1744 | 0 | return (ctxt->errNr++); |
1745 | 0 | } |
1746 | | |
1747 | | /** |
1748 | | * Pops the top error from the error stack |
1749 | | * |
1750 | | * @param ctxt the validation context |
1751 | | */ |
1752 | | static void |
1753 | | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
1754 | 0 | { |
1755 | 0 | xmlRelaxNGValidErrorPtr cur; |
1756 | |
|
1757 | 0 | if (ctxt->errNr <= 0) { |
1758 | 0 | ctxt->err = NULL; |
1759 | 0 | return; |
1760 | 0 | } |
1761 | 0 | ctxt->errNr--; |
1762 | 0 | if (ctxt->errNr > 0) |
1763 | 0 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
1764 | 0 | else |
1765 | 0 | ctxt->err = NULL; |
1766 | 0 | cur = &ctxt->errTab[ctxt->errNr]; |
1767 | 0 | if (cur->flags & ERROR_IS_DUP) { |
1768 | 0 | if (cur->arg1 != NULL) |
1769 | 0 | xmlFree((xmlChar *) cur->arg1); |
1770 | 0 | cur->arg1 = NULL; |
1771 | 0 | if (cur->arg2 != NULL) |
1772 | 0 | xmlFree((xmlChar *) cur->arg2); |
1773 | 0 | cur->arg2 = NULL; |
1774 | 0 | cur->flags = 0; |
1775 | 0 | } |
1776 | 0 | } |
1777 | | |
1778 | | /** |
1779 | | * Pushes a new doc on top of the doc stack |
1780 | | * |
1781 | | * @param ctxt the parser context |
1782 | | * @param value the element doc |
1783 | | * @returns 0 in case of error, the index in the stack otherwise |
1784 | | */ |
1785 | | static int |
1786 | | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
1787 | | xmlRelaxNGDocumentPtr value) |
1788 | 0 | { |
1789 | 0 | if (ctxt->docTab == NULL) { |
1790 | 0 | ctxt->docMax = 4; |
1791 | 0 | ctxt->docNr = 0; |
1792 | 0 | ctxt->docTab = |
1793 | 0 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
1794 | 0 | sizeof(ctxt->docTab[0])); |
1795 | 0 | if (ctxt->docTab == NULL) { |
1796 | 0 | xmlRngPErrMemory(ctxt); |
1797 | 0 | return (0); |
1798 | 0 | } |
1799 | 0 | } |
1800 | 0 | if (ctxt->docNr >= ctxt->docMax) { |
1801 | 0 | ctxt->docMax *= 2; |
1802 | 0 | ctxt->docTab = |
1803 | 0 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
1804 | 0 | ctxt->docMax * |
1805 | 0 | sizeof(ctxt->docTab[0])); |
1806 | 0 | if (ctxt->docTab == NULL) { |
1807 | 0 | xmlRngPErrMemory(ctxt); |
1808 | 0 | return (0); |
1809 | 0 | } |
1810 | 0 | } |
1811 | 0 | ctxt->docTab[ctxt->docNr] = value; |
1812 | 0 | ctxt->doc = value; |
1813 | 0 | return (ctxt->docNr++); |
1814 | 0 | } |
1815 | | |
1816 | | /** |
1817 | | * Pops the top doc from the doc stack |
1818 | | * |
1819 | | * @param ctxt the parser context |
1820 | | * @returns the doc just removed |
1821 | | */ |
1822 | | static xmlRelaxNGDocumentPtr |
1823 | | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
1824 | 0 | { |
1825 | 0 | xmlRelaxNGDocumentPtr ret; |
1826 | |
|
1827 | 0 | if (ctxt->docNr <= 0) |
1828 | 0 | return (NULL); |
1829 | 0 | ctxt->docNr--; |
1830 | 0 | if (ctxt->docNr > 0) |
1831 | 0 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
1832 | 0 | else |
1833 | 0 | ctxt->doc = NULL; |
1834 | 0 | ret = ctxt->docTab[ctxt->docNr]; |
1835 | 0 | ctxt->docTab[ctxt->docNr] = NULL; |
1836 | 0 | return (ret); |
1837 | 0 | } |
1838 | | |
1839 | | /** |
1840 | | * First lookup if the document is already loaded into the parser context, |
1841 | | * check against recursion. If not found the resource is loaded and |
1842 | | * the content is preprocessed before being returned back to the caller. |
1843 | | * |
1844 | | * @param ctxt the parser context |
1845 | | * @param URL the normalized URL |
1846 | | * @param ns the inherited ns if any |
1847 | | * @returns the xmlRelaxNGDocument or NULL in case of error |
1848 | | */ |
1849 | | static xmlRelaxNGDocumentPtr |
1850 | | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
1851 | | const xmlChar * URL, const xmlChar * ns) |
1852 | 0 | { |
1853 | 0 | xmlRelaxNGDocumentPtr ret = NULL; |
1854 | 0 | xmlDocPtr doc; |
1855 | 0 | xmlNodePtr root; |
1856 | 0 | int i; |
1857 | | |
1858 | | /* |
1859 | | * check against recursion in the stack |
1860 | | */ |
1861 | 0 | for (i = 0; i < ctxt->docNr; i++) { |
1862 | 0 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
1863 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
1864 | 0 | "Detected an externalRef recursion for %s\n", URL, |
1865 | 0 | NULL); |
1866 | 0 | return (NULL); |
1867 | 0 | } |
1868 | 0 | } |
1869 | | |
1870 | | /* |
1871 | | * load the document |
1872 | | */ |
1873 | 0 | doc = xmlRelaxReadFile(ctxt, (const char *) URL); |
1874 | 0 | if (doc == NULL) { |
1875 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
1876 | 0 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
1877 | 0 | return (NULL); |
1878 | 0 | } |
1879 | | |
1880 | | /* |
1881 | | * Allocate the document structures and register it first. |
1882 | | */ |
1883 | 0 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
1884 | 0 | if (ret == NULL) { |
1885 | 0 | xmlRngPErrMemory(ctxt); |
1886 | 0 | xmlFreeDoc(doc); |
1887 | 0 | return (NULL); |
1888 | 0 | } |
1889 | 0 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
1890 | 0 | ret->doc = doc; |
1891 | 0 | ret->href = xmlStrdup(URL); |
1892 | 0 | ret->next = ctxt->documents; |
1893 | 0 | ret->externalRef = 1; |
1894 | 0 | ctxt->documents = ret; |
1895 | | |
1896 | | /* |
1897 | | * transmit the ns if needed |
1898 | | */ |
1899 | 0 | if (ns != NULL) { |
1900 | 0 | root = xmlDocGetRootElement(doc); |
1901 | 0 | if (root != NULL) { |
1902 | 0 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
1903 | 0 | xmlSetProp(root, BAD_CAST "ns", ns); |
1904 | 0 | } |
1905 | 0 | } |
1906 | 0 | } |
1907 | | |
1908 | | /* |
1909 | | * push it on the stack and register it in the hash table |
1910 | | */ |
1911 | 0 | xmlRelaxNGDocumentPush(ctxt, ret); |
1912 | | |
1913 | | /* |
1914 | | * Some preprocessing of the document content |
1915 | | */ |
1916 | 0 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
1917 | 0 | if (doc == NULL) { |
1918 | 0 | ctxt->doc = NULL; |
1919 | 0 | return (NULL); |
1920 | 0 | } |
1921 | | |
1922 | 0 | xmlRelaxNGDocumentPop(ctxt); |
1923 | |
|
1924 | 0 | return (ret); |
1925 | 0 | } |
1926 | | |
1927 | | /************************************************************************ |
1928 | | * * |
1929 | | * Error functions * |
1930 | | * * |
1931 | | ************************************************************************/ |
1932 | | |
1933 | 0 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
1934 | 0 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
1935 | 0 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
1936 | 0 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
1937 | 0 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
1938 | | |
1939 | | static const char * |
1940 | | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
1941 | 0 | { |
1942 | 0 | if (def == NULL) |
1943 | 0 | return ("none"); |
1944 | 0 | switch (def->type) { |
1945 | 0 | case XML_RELAXNG_EMPTY: |
1946 | 0 | return ("empty"); |
1947 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
1948 | 0 | return ("notAllowed"); |
1949 | 0 | case XML_RELAXNG_EXCEPT: |
1950 | 0 | return ("except"); |
1951 | 0 | case XML_RELAXNG_TEXT: |
1952 | 0 | return ("text"); |
1953 | 0 | case XML_RELAXNG_ELEMENT: |
1954 | 0 | return ("element"); |
1955 | 0 | case XML_RELAXNG_DATATYPE: |
1956 | 0 | return ("datatype"); |
1957 | 0 | case XML_RELAXNG_VALUE: |
1958 | 0 | return ("value"); |
1959 | 0 | case XML_RELAXNG_LIST: |
1960 | 0 | return ("list"); |
1961 | 0 | case XML_RELAXNG_ATTRIBUTE: |
1962 | 0 | return ("attribute"); |
1963 | 0 | case XML_RELAXNG_DEF: |
1964 | 0 | return ("def"); |
1965 | 0 | case XML_RELAXNG_REF: |
1966 | 0 | return ("ref"); |
1967 | 0 | case XML_RELAXNG_EXTERNALREF: |
1968 | 0 | return ("externalRef"); |
1969 | 0 | case XML_RELAXNG_PARENTREF: |
1970 | 0 | return ("parentRef"); |
1971 | 0 | case XML_RELAXNG_OPTIONAL: |
1972 | 0 | return ("optional"); |
1973 | 0 | case XML_RELAXNG_ZEROORMORE: |
1974 | 0 | return ("zeroOrMore"); |
1975 | 0 | case XML_RELAXNG_ONEORMORE: |
1976 | 0 | return ("oneOrMore"); |
1977 | 0 | case XML_RELAXNG_CHOICE: |
1978 | 0 | return ("choice"); |
1979 | 0 | case XML_RELAXNG_GROUP: |
1980 | 0 | return ("group"); |
1981 | 0 | case XML_RELAXNG_INTERLEAVE: |
1982 | 0 | return ("interleave"); |
1983 | 0 | case XML_RELAXNG_START: |
1984 | 0 | return ("start"); |
1985 | 0 | case XML_RELAXNG_NOOP: |
1986 | 0 | return ("noop"); |
1987 | 0 | case XML_RELAXNG_PARAM: |
1988 | 0 | return ("param"); |
1989 | 0 | } |
1990 | 0 | return ("unknown"); |
1991 | 0 | } |
1992 | | |
1993 | | /** |
1994 | | * computes a formatted error string for the given error code and args |
1995 | | * |
1996 | | * @param err the error code |
1997 | | * @param arg1 the first string argument |
1998 | | * @param arg2 the second string argument |
1999 | | * @returns the error string, it must be deallocated by the caller |
2000 | | */ |
2001 | | static xmlChar * |
2002 | | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
2003 | | const xmlChar * arg2) |
2004 | 0 | { |
2005 | 0 | char msg[1000]; |
2006 | 0 | xmlChar *result; |
2007 | |
|
2008 | 0 | if (arg1 == NULL) |
2009 | 0 | arg1 = BAD_CAST ""; |
2010 | 0 | if (arg2 == NULL) |
2011 | 0 | arg2 = BAD_CAST ""; |
2012 | |
|
2013 | 0 | msg[0] = 0; |
2014 | 0 | switch (err) { |
2015 | 0 | case XML_RELAXNG_OK: |
2016 | 0 | return (NULL); |
2017 | 0 | case XML_RELAXNG_ERR_MEMORY: |
2018 | 0 | return (xmlCharStrdup("out of memory\n")); |
2019 | 0 | case XML_RELAXNG_ERR_TYPE: |
2020 | 0 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
2021 | 0 | break; |
2022 | 0 | case XML_RELAXNG_ERR_TYPEVAL: |
2023 | 0 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
2024 | 0 | arg2); |
2025 | 0 | break; |
2026 | 0 | case XML_RELAXNG_ERR_DUPID: |
2027 | 0 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
2028 | 0 | break; |
2029 | 0 | case XML_RELAXNG_ERR_TYPECMP: |
2030 | 0 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
2031 | 0 | break; |
2032 | 0 | case XML_RELAXNG_ERR_NOSTATE: |
2033 | 0 | return (xmlCharStrdup("Internal error: no state\n")); |
2034 | 0 | case XML_RELAXNG_ERR_NODEFINE: |
2035 | 0 | return (xmlCharStrdup("Internal error: no define\n")); |
2036 | 0 | case XML_RELAXNG_ERR_INTERNAL: |
2037 | 0 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
2038 | 0 | break; |
2039 | 0 | case XML_RELAXNG_ERR_LISTEXTRA: |
2040 | 0 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
2041 | 0 | break; |
2042 | 0 | case XML_RELAXNG_ERR_INTERNODATA: |
2043 | 0 | return (xmlCharStrdup |
2044 | 0 | ("Internal: interleave block has no data\n")); |
2045 | 0 | case XML_RELAXNG_ERR_INTERSEQ: |
2046 | 0 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
2047 | 0 | case XML_RELAXNG_ERR_INTEREXTRA: |
2048 | 0 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
2049 | 0 | break; |
2050 | 0 | case XML_RELAXNG_ERR_ELEMNAME: |
2051 | 0 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
2052 | 0 | arg2); |
2053 | 0 | break; |
2054 | 0 | case XML_RELAXNG_ERR_ELEMNONS: |
2055 | 0 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
2056 | 0 | arg1); |
2057 | 0 | break; |
2058 | 0 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
2059 | 0 | snprintf(msg, 1000, |
2060 | 0 | "Element %s has wrong namespace: expecting %s\n", arg1, |
2061 | 0 | arg2); |
2062 | 0 | break; |
2063 | 0 | case XML_RELAXNG_ERR_ELEMWRONG: |
2064 | 0 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
2065 | 0 | break; |
2066 | 0 | case XML_RELAXNG_ERR_TEXTWRONG: |
2067 | 0 | snprintf(msg, 1000, |
2068 | 0 | "Did not expect text in element %s content\n", arg1); |
2069 | 0 | break; |
2070 | 0 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
2071 | 0 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
2072 | 0 | arg1); |
2073 | 0 | break; |
2074 | 0 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
2075 | 0 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
2076 | 0 | break; |
2077 | 0 | case XML_RELAXNG_ERR_NOELEM: |
2078 | 0 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
2079 | 0 | arg1); |
2080 | 0 | break; |
2081 | 0 | case XML_RELAXNG_ERR_NOTELEM: |
2082 | 0 | return (xmlCharStrdup("Expecting an element got text\n")); |
2083 | 0 | case XML_RELAXNG_ERR_ATTRVALID: |
2084 | 0 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
2085 | 0 | arg1); |
2086 | 0 | break; |
2087 | 0 | case XML_RELAXNG_ERR_CONTENTVALID: |
2088 | 0 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
2089 | 0 | arg1); |
2090 | 0 | break; |
2091 | 0 | case XML_RELAXNG_ERR_EXTRACONTENT: |
2092 | 0 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
2093 | 0 | arg1, arg2); |
2094 | 0 | break; |
2095 | 0 | case XML_RELAXNG_ERR_INVALIDATTR: |
2096 | 0 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
2097 | 0 | arg1, arg2); |
2098 | 0 | break; |
2099 | 0 | case XML_RELAXNG_ERR_LACKDATA: |
2100 | 0 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
2101 | 0 | arg1); |
2102 | 0 | break; |
2103 | 0 | case XML_RELAXNG_ERR_DATAELEM: |
2104 | 0 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
2105 | 0 | arg1); |
2106 | 0 | break; |
2107 | 0 | case XML_RELAXNG_ERR_VALELEM: |
2108 | 0 | snprintf(msg, 1000, "Value element %s has child elements\n", |
2109 | 0 | arg1); |
2110 | 0 | break; |
2111 | 0 | case XML_RELAXNG_ERR_LISTELEM: |
2112 | 0 | snprintf(msg, 1000, "List element %s has child elements\n", |
2113 | 0 | arg1); |
2114 | 0 | break; |
2115 | 0 | case XML_RELAXNG_ERR_DATATYPE: |
2116 | 0 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
2117 | 0 | break; |
2118 | 0 | case XML_RELAXNG_ERR_VALUE: |
2119 | 0 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
2120 | 0 | break; |
2121 | 0 | case XML_RELAXNG_ERR_LIST: |
2122 | 0 | return (xmlCharStrdup("Error validating list\n")); |
2123 | 0 | case XML_RELAXNG_ERR_NOGRAMMAR: |
2124 | 0 | return (xmlCharStrdup("No top grammar defined\n")); |
2125 | 0 | case XML_RELAXNG_ERR_EXTRADATA: |
2126 | 0 | return (xmlCharStrdup("Extra data in the document\n")); |
2127 | 0 | default: |
2128 | 0 | return (xmlCharStrdup("Unknown error !\n")); |
2129 | 0 | } |
2130 | 0 | if (msg[0] == 0) { |
2131 | 0 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
2132 | 0 | } |
2133 | 0 | msg[1000 - 1] = 0; |
2134 | 0 | result = xmlCharStrdup(msg); |
2135 | 0 | return (xmlEscapeFormatString(&result)); |
2136 | 0 | } |
2137 | | |
2138 | | /** |
2139 | | * Show a validation error. |
2140 | | * |
2141 | | * @param ctxt the validation context |
2142 | | * @param err the error number |
2143 | | * @param node the node |
2144 | | * @param child the node child generating the problem. |
2145 | | * @param arg1 the first argument |
2146 | | * @param arg2 the second argument |
2147 | | */ |
2148 | | static void |
2149 | | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
2150 | | xmlRelaxNGValidErr err, xmlNodePtr node, |
2151 | | xmlNodePtr child, const xmlChar * arg1, |
2152 | | const xmlChar * arg2) |
2153 | 0 | { |
2154 | 0 | xmlChar *msg; |
2155 | |
|
2156 | 0 | if (ctxt->flags & FLAGS_NOERROR) |
2157 | 0 | return; |
2158 | | |
2159 | 0 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
2160 | 0 | if (msg == NULL) |
2161 | 0 | return; |
2162 | | |
2163 | 0 | if (ctxt->errNo == XML_RELAXNG_OK) |
2164 | 0 | ctxt->errNo = err; |
2165 | 0 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
2166 | 0 | (const char *) msg, arg1, arg2); |
2167 | 0 | xmlFree(msg); |
2168 | 0 | } |
2169 | | |
2170 | | /** |
2171 | | * pop and discard all errors until the given level is reached |
2172 | | * |
2173 | | * @param ctxt the validation context |
2174 | | * @param level the error level in the stack |
2175 | | */ |
2176 | | static void |
2177 | | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
2178 | 0 | { |
2179 | 0 | int i; |
2180 | 0 | xmlRelaxNGValidErrorPtr err; |
2181 | |
|
2182 | 0 | for (i = level; i < ctxt->errNr; i++) { |
2183 | 0 | err = &ctxt->errTab[i]; |
2184 | 0 | if (err->flags & ERROR_IS_DUP) { |
2185 | 0 | if (err->arg1 != NULL) |
2186 | 0 | xmlFree((xmlChar *) err->arg1); |
2187 | 0 | err->arg1 = NULL; |
2188 | 0 | if (err->arg2 != NULL) |
2189 | 0 | xmlFree((xmlChar *) err->arg2); |
2190 | 0 | err->arg2 = NULL; |
2191 | 0 | err->flags = 0; |
2192 | 0 | } |
2193 | 0 | } |
2194 | 0 | ctxt->errNr = level; |
2195 | 0 | if (ctxt->errNr <= 0) |
2196 | 0 | ctxt->err = NULL; |
2197 | 0 | } |
2198 | | |
2199 | | /** |
2200 | | * Show all validation error over a given index. |
2201 | | * |
2202 | | * @param ctxt the validation context |
2203 | | */ |
2204 | | static void |
2205 | | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
2206 | 0 | { |
2207 | 0 | int i, j, k; |
2208 | 0 | xmlRelaxNGValidErrorPtr err, dup; |
2209 | | |
2210 | 0 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
2211 | 0 | err = &ctxt->errTab[i]; |
2212 | 0 | if (k < MAX_ERROR) { |
2213 | 0 | for (j = 0; j < i; j++) { |
2214 | 0 | dup = &ctxt->errTab[j]; |
2215 | 0 | if ((err->err == dup->err) && (err->node == dup->node) && |
2216 | 0 | (xmlStrEqual(err->arg1, dup->arg1)) && |
2217 | 0 | (xmlStrEqual(err->arg2, dup->arg2))) { |
2218 | 0 | goto skip; |
2219 | 0 | } |
2220 | 0 | } |
2221 | 0 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
2222 | 0 | err->arg1, err->arg2); |
2223 | 0 | k++; |
2224 | 0 | } |
2225 | 0 | skip: |
2226 | 0 | if (err->flags & ERROR_IS_DUP) { |
2227 | 0 | if (err->arg1 != NULL) |
2228 | 0 | xmlFree((xmlChar *) err->arg1); |
2229 | 0 | err->arg1 = NULL; |
2230 | 0 | if (err->arg2 != NULL) |
2231 | 0 | xmlFree((xmlChar *) err->arg2); |
2232 | 0 | err->arg2 = NULL; |
2233 | 0 | err->flags = 0; |
2234 | 0 | } |
2235 | 0 | } |
2236 | 0 | ctxt->errNr = 0; |
2237 | 0 | } |
2238 | | |
2239 | | /** |
2240 | | * Register a validation error, either generating it if it's sure |
2241 | | * or stacking it for later handling if unsure. |
2242 | | * |
2243 | | * @param ctxt the validation context |
2244 | | * @param err the error number |
2245 | | * @param arg1 the first argument |
2246 | | * @param arg2 the second argument |
2247 | | * @param dup need to dup the args |
2248 | | */ |
2249 | | static void |
2250 | | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
2251 | | xmlRelaxNGValidErr err, const xmlChar * arg1, |
2252 | | const xmlChar * arg2, int dup) |
2253 | 0 | { |
2254 | 0 | if (ctxt == NULL) |
2255 | 0 | return; |
2256 | 0 | if (ctxt->flags & FLAGS_NOERROR) |
2257 | 0 | return; |
2258 | | |
2259 | | /* |
2260 | | * generate the error directly |
2261 | | */ |
2262 | 0 | if (((ctxt->flags & FLAGS_IGNORABLE) == 0) || |
2263 | 0 | (ctxt->flags & FLAGS_NEGATIVE)) { |
2264 | 0 | xmlNodePtr node, seq; |
2265 | | |
2266 | | /* |
2267 | | * Flush first any stacked error which might be the |
2268 | | * real cause of the problem. |
2269 | | */ |
2270 | 0 | if (ctxt->errNr != 0) |
2271 | 0 | xmlRelaxNGDumpValidError(ctxt); |
2272 | 0 | if (ctxt->state != NULL) { |
2273 | 0 | node = ctxt->state->node; |
2274 | 0 | seq = ctxt->state->seq; |
2275 | 0 | } else { |
2276 | 0 | node = seq = NULL; |
2277 | 0 | } |
2278 | 0 | if ((node == NULL) && (seq == NULL)) { |
2279 | 0 | node = ctxt->pnode; |
2280 | 0 | } |
2281 | 0 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
2282 | 0 | } |
2283 | | /* |
2284 | | * Stack the error for later processing if needed |
2285 | | */ |
2286 | 0 | else { |
2287 | 0 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
2288 | 0 | } |
2289 | 0 | } |
2290 | | |
2291 | | |
2292 | | /************************************************************************ |
2293 | | * * |
2294 | | * Type library hooks * |
2295 | | * * |
2296 | | ************************************************************************/ |
2297 | | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
2298 | | const xmlChar * str); |
2299 | | |
2300 | | /** |
2301 | | * Check if the given type is provided by |
2302 | | * the W3C XMLSchema Datatype library. |
2303 | | * |
2304 | | * @param data data needed for the library |
2305 | | * @param type the type name |
2306 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2307 | | */ |
2308 | | static int |
2309 | | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
2310 | 0 | { |
2311 | 0 | xmlSchemaTypePtr typ; |
2312 | |
|
2313 | 0 | if (type == NULL) |
2314 | 0 | return (-1); |
2315 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2316 | 0 | BAD_CAST |
2317 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2318 | 0 | if (typ == NULL) |
2319 | 0 | return (0); |
2320 | 0 | return (1); |
2321 | 0 | } |
2322 | | |
2323 | | /** |
2324 | | * Check if the given type and value are validated by |
2325 | | * the W3C XMLSchema Datatype library. |
2326 | | * |
2327 | | * @param data data needed for the library |
2328 | | * @param type the type name |
2329 | | * @param value the value to check |
2330 | | * @param result pointer to result |
2331 | | * @param node the node |
2332 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2333 | | */ |
2334 | | static int |
2335 | | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
2336 | | const xmlChar * type, |
2337 | | const xmlChar * value, |
2338 | | void **result, xmlNodePtr node) |
2339 | 0 | { |
2340 | 0 | xmlSchemaTypePtr typ; |
2341 | 0 | int ret; |
2342 | |
|
2343 | 0 | if ((type == NULL) || (value == NULL)) |
2344 | 0 | return (-1); |
2345 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2346 | 0 | BAD_CAST |
2347 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2348 | 0 | if (typ == NULL) |
2349 | 0 | return (-1); |
2350 | 0 | ret = xmlSchemaValPredefTypeNode(typ, value, |
2351 | 0 | (xmlSchemaValPtr *) result, node); |
2352 | 0 | if (ret == 2) /* special ID error code */ |
2353 | 0 | return (2); |
2354 | 0 | if (ret == 0) |
2355 | 0 | return (1); |
2356 | 0 | if (ret > 0) |
2357 | 0 | return (0); |
2358 | 0 | return (-1); |
2359 | 0 | } |
2360 | | |
2361 | | /** |
2362 | | * Function provided by a type library to check a value facet |
2363 | | * |
2364 | | * @param data data needed for the library |
2365 | | * @param type the type name |
2366 | | * @param facetname the facet name |
2367 | | * @param val the facet value |
2368 | | * @param strval the string value |
2369 | | * @param value the value to check |
2370 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2371 | | */ |
2372 | | static int |
2373 | | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
2374 | | const xmlChar * type, const xmlChar * facetname, |
2375 | | const xmlChar * val, const xmlChar * strval, |
2376 | | void *value) |
2377 | 0 | { |
2378 | 0 | xmlSchemaFacetPtr facet; |
2379 | 0 | xmlSchemaTypePtr typ; |
2380 | 0 | int ret; |
2381 | |
|
2382 | 0 | if ((type == NULL) || (strval == NULL)) |
2383 | 0 | return (-1); |
2384 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2385 | 0 | BAD_CAST |
2386 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2387 | 0 | if (typ == NULL) |
2388 | 0 | return (-1); |
2389 | | |
2390 | 0 | facet = xmlSchemaNewFacet(); |
2391 | 0 | if (facet == NULL) |
2392 | 0 | return (-1); |
2393 | | |
2394 | 0 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
2395 | 0 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
2396 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
2397 | 0 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
2398 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
2399 | 0 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
2400 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
2401 | 0 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
2402 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
2403 | 0 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
2404 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
2405 | 0 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
2406 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
2407 | 0 | facet->type = XML_SCHEMA_FACET_PATTERN; |
2408 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
2409 | 0 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
2410 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
2411 | 0 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
2412 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
2413 | 0 | facet->type = XML_SCHEMA_FACET_LENGTH; |
2414 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
2415 | 0 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
2416 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
2417 | 0 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
2418 | 0 | } else { |
2419 | 0 | xmlSchemaFreeFacet(facet); |
2420 | 0 | return (-1); |
2421 | 0 | } |
2422 | 0 | facet->value = val; |
2423 | 0 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
2424 | 0 | if (ret != 0) { |
2425 | 0 | xmlSchemaFreeFacet(facet); |
2426 | 0 | return (-1); |
2427 | 0 | } |
2428 | 0 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
2429 | 0 | xmlSchemaFreeFacet(facet); |
2430 | 0 | if (ret != 0) |
2431 | 0 | return (-1); |
2432 | 0 | return (0); |
2433 | 0 | } |
2434 | | |
2435 | | /** |
2436 | | * Function provided by a type library to free a Schemas value |
2437 | | * |
2438 | | * @param data data needed for the library |
2439 | | * @param value the value to free |
2440 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2441 | | */ |
2442 | | static void |
2443 | | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
2444 | 0 | { |
2445 | 0 | xmlSchemaFreeValue(value); |
2446 | 0 | } |
2447 | | |
2448 | | /** |
2449 | | * Compare two values for equality accordingly a type from the W3C XMLSchema |
2450 | | * Datatype library. |
2451 | | * |
2452 | | * @param data data needed for the library |
2453 | | * @param type the type name |
2454 | | * @param value1 the first value |
2455 | | * @param ctxt1 the first context node |
2456 | | * @param comp1 value to compare with |
2457 | | * @param value2 the second value |
2458 | | * @param ctxt2 the second context node |
2459 | | * @returns 1 if equal, 0 if no and -1 in case of error. |
2460 | | */ |
2461 | | static int |
2462 | | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
2463 | | const xmlChar * type, |
2464 | | const xmlChar * value1, |
2465 | | xmlNodePtr ctxt1, |
2466 | | void *comp1, |
2467 | | const xmlChar * value2, xmlNodePtr ctxt2) |
2468 | 0 | { |
2469 | 0 | int ret; |
2470 | 0 | xmlSchemaTypePtr typ; |
2471 | 0 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
2472 | |
|
2473 | 0 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
2474 | 0 | return (-1); |
2475 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2476 | 0 | BAD_CAST |
2477 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2478 | 0 | if (typ == NULL) |
2479 | 0 | return (-1); |
2480 | 0 | if (comp1 == NULL) { |
2481 | 0 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
2482 | 0 | if (ret != 0) |
2483 | 0 | return (-1); |
2484 | 0 | if (res1 == NULL) |
2485 | 0 | return (-1); |
2486 | 0 | } else { |
2487 | 0 | res1 = (xmlSchemaValPtr) comp1; |
2488 | 0 | } |
2489 | 0 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
2490 | 0 | if (ret != 0) { |
2491 | 0 | if (res1 != (xmlSchemaValPtr) comp1) |
2492 | 0 | xmlSchemaFreeValue(res1); |
2493 | 0 | return (-1); |
2494 | 0 | } |
2495 | 0 | ret = xmlSchemaCompareValues(res1, res2); |
2496 | 0 | if (res1 != (xmlSchemaValPtr) comp1) |
2497 | 0 | xmlSchemaFreeValue(res1); |
2498 | 0 | xmlSchemaFreeValue(res2); |
2499 | 0 | if (ret == -2) |
2500 | 0 | return (-1); |
2501 | 0 | if (ret == 0) |
2502 | 0 | return (1); |
2503 | 0 | return (0); |
2504 | 0 | } |
2505 | | |
2506 | | /** |
2507 | | * Check if the given type is provided by |
2508 | | * the default datatype library. |
2509 | | * |
2510 | | * @param data data needed for the library |
2511 | | * @param type the type name |
2512 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2513 | | */ |
2514 | | static int |
2515 | | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
2516 | | const xmlChar * type) |
2517 | 0 | { |
2518 | 0 | if (type == NULL) |
2519 | 0 | return (-1); |
2520 | 0 | if (xmlStrEqual(type, BAD_CAST "string")) |
2521 | 0 | return (1); |
2522 | 0 | if (xmlStrEqual(type, BAD_CAST "token")) |
2523 | 0 | return (1); |
2524 | 0 | return (0); |
2525 | 0 | } |
2526 | | |
2527 | | /** |
2528 | | * Check if the given type and value are validated by |
2529 | | * the default datatype library. |
2530 | | * |
2531 | | * @param data data needed for the library |
2532 | | * @param type the type name |
2533 | | * @param value the value to check |
2534 | | * @param result pointer to result |
2535 | | * @param node the node |
2536 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2537 | | */ |
2538 | | static int |
2539 | | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
2540 | | const xmlChar * type ATTRIBUTE_UNUSED, |
2541 | | const xmlChar * value ATTRIBUTE_UNUSED, |
2542 | | void **result ATTRIBUTE_UNUSED, |
2543 | | xmlNodePtr node ATTRIBUTE_UNUSED) |
2544 | 0 | { |
2545 | 0 | if (value == NULL) |
2546 | 0 | return (-1); |
2547 | 0 | if (xmlStrEqual(type, BAD_CAST "string")) |
2548 | 0 | return (1); |
2549 | 0 | if (xmlStrEqual(type, BAD_CAST "token")) { |
2550 | 0 | return (1); |
2551 | 0 | } |
2552 | | |
2553 | 0 | return (0); |
2554 | 0 | } |
2555 | | |
2556 | | /** |
2557 | | * Compare two values accordingly a type from the default |
2558 | | * datatype library. |
2559 | | * |
2560 | | * @param data data needed for the library |
2561 | | * @param type the type name |
2562 | | * @param value1 the first value |
2563 | | * @param ctxt1 the first context node |
2564 | | * @param comp1 value to compare with |
2565 | | * @param value2 the second value |
2566 | | * @param ctxt2 the second context node |
2567 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
2568 | | */ |
2569 | | static int |
2570 | | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
2571 | | const xmlChar * type, |
2572 | | const xmlChar * value1, |
2573 | | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
2574 | | void *comp1 ATTRIBUTE_UNUSED, |
2575 | | const xmlChar * value2, |
2576 | | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
2577 | 0 | { |
2578 | 0 | int ret = -1; |
2579 | |
|
2580 | 0 | if (xmlStrEqual(type, BAD_CAST "string")) { |
2581 | 0 | ret = xmlStrEqual(value1, value2); |
2582 | 0 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
2583 | 0 | if (!xmlStrEqual(value1, value2)) { |
2584 | 0 | xmlChar *nval, *nvalue; |
2585 | | |
2586 | | /* |
2587 | | * TODO: trivial optimizations are possible by |
2588 | | * computing at compile-time |
2589 | | */ |
2590 | 0 | nval = xmlRelaxNGNormalize(NULL, value1); |
2591 | 0 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
2592 | |
|
2593 | 0 | if ((nval == NULL) || (nvalue == NULL)) |
2594 | 0 | ret = -1; |
2595 | 0 | else if (xmlStrEqual(nval, nvalue)) |
2596 | 0 | ret = 1; |
2597 | 0 | else |
2598 | 0 | ret = 0; |
2599 | 0 | if (nval != NULL) |
2600 | 0 | xmlFree(nval); |
2601 | 0 | if (nvalue != NULL) |
2602 | 0 | xmlFree(nvalue); |
2603 | 0 | } else |
2604 | 0 | ret = 1; |
2605 | 0 | } |
2606 | 0 | return (ret); |
2607 | 0 | } |
2608 | | |
2609 | | static int xmlRelaxNGTypeInitialized = 0; |
2610 | | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
2611 | | |
2612 | | /** |
2613 | | * Free the structure associated to the type library |
2614 | | * |
2615 | | * @param payload the type library structure |
2616 | | * @param namespace the URI bound to the library |
2617 | | */ |
2618 | | static void |
2619 | | xmlRelaxNGFreeTypeLibrary(void *payload, |
2620 | | const xmlChar * namespace ATTRIBUTE_UNUSED) |
2621 | 0 | { |
2622 | 0 | xmlRelaxNGTypeLibraryPtr lib = (xmlRelaxNGTypeLibraryPtr) payload; |
2623 | 0 | if (lib == NULL) |
2624 | 0 | return; |
2625 | 0 | if (lib->namespace != NULL) |
2626 | 0 | xmlFree((xmlChar *) lib->namespace); |
2627 | 0 | xmlFree(lib); |
2628 | 0 | } |
2629 | | |
2630 | | /** |
2631 | | * Register a new type library |
2632 | | * |
2633 | | * @param namespace the URI bound to the library |
2634 | | * @param data data associated to the library |
2635 | | * @param have the provide function |
2636 | | * @param check the checking function |
2637 | | * @param comp the comparison function |
2638 | | * @param facet facet check function |
2639 | | * @param freef free function |
2640 | | * @returns 0 in case of success and -1 in case of error. |
2641 | | */ |
2642 | | static int |
2643 | | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
2644 | | xmlRelaxNGTypeHave have, |
2645 | | xmlRelaxNGTypeCheck check, |
2646 | | xmlRelaxNGTypeCompare comp, |
2647 | | xmlRelaxNGFacetCheck facet, |
2648 | | xmlRelaxNGTypeFree freef) |
2649 | 0 | { |
2650 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
2651 | 0 | int ret; |
2652 | |
|
2653 | 0 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
2654 | 0 | (check == NULL) || (comp == NULL)) |
2655 | 0 | return (-1); |
2656 | 0 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) |
2657 | 0 | return (-1); |
2658 | 0 | lib = |
2659 | 0 | (xmlRelaxNGTypeLibraryPtr) |
2660 | 0 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
2661 | 0 | if (lib == NULL) { |
2662 | 0 | xmlRngVErrMemory(NULL); |
2663 | 0 | return (-1); |
2664 | 0 | } |
2665 | 0 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
2666 | 0 | lib->namespace = xmlStrdup(namespace); |
2667 | 0 | lib->data = data; |
2668 | 0 | lib->have = have; |
2669 | 0 | lib->comp = comp; |
2670 | 0 | lib->check = check; |
2671 | 0 | lib->facet = facet; |
2672 | 0 | lib->freef = freef; |
2673 | 0 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
2674 | 0 | if (ret < 0) { |
2675 | 0 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
2676 | 0 | return (-1); |
2677 | 0 | } |
2678 | 0 | return (0); |
2679 | 0 | } |
2680 | | |
2681 | | static xmlMutex xmlRelaxNGMutex; |
2682 | | |
2683 | | void |
2684 | | xmlInitRelaxNGInternal(void) |
2685 | 1 | { |
2686 | 1 | xmlInitMutex(&xmlRelaxNGMutex); |
2687 | 1 | } |
2688 | | |
2689 | | void |
2690 | | xmlCleanupRelaxNGInternal(void) |
2691 | 0 | { |
2692 | 0 | xmlCleanupMutex(&xmlRelaxNGMutex); |
2693 | 0 | } |
2694 | | |
2695 | | /** |
2696 | | * Initialize the default type libraries. |
2697 | | * |
2698 | | * @returns 0 in case of success and -1 in case of error. |
2699 | | */ |
2700 | | int |
2701 | | xmlRelaxNGInitTypes(void) |
2702 | 0 | { |
2703 | 0 | xmlInitParser(); |
2704 | |
|
2705 | 0 | xmlMutexLock(&xmlRelaxNGMutex); |
2706 | 0 | if (xmlRelaxNGTypeInitialized != 0) { |
2707 | 0 | xmlMutexUnlock(&xmlRelaxNGMutex); |
2708 | 0 | return (0); |
2709 | 0 | } |
2710 | | |
2711 | 0 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
2712 | 0 | if (xmlRelaxNGRegisteredTypes == NULL) { |
2713 | 0 | xmlMutexUnlock(&xmlRelaxNGMutex); |
2714 | 0 | return (-1); |
2715 | 0 | } |
2716 | 0 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
2717 | 0 | "http://www.w3.org/2001/XMLSchema-datatypes", |
2718 | 0 | NULL, xmlRelaxNGSchemaTypeHave, |
2719 | 0 | xmlRelaxNGSchemaTypeCheck, |
2720 | 0 | xmlRelaxNGSchemaTypeCompare, |
2721 | 0 | xmlRelaxNGSchemaFacetCheck, |
2722 | 0 | xmlRelaxNGSchemaFreeValue); |
2723 | 0 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
2724 | 0 | xmlRelaxNGDefaultTypeHave, |
2725 | 0 | xmlRelaxNGDefaultTypeCheck, |
2726 | 0 | xmlRelaxNGDefaultTypeCompare, NULL, |
2727 | 0 | NULL); |
2728 | 0 | xmlRelaxNGTypeInitialized = 1; |
2729 | 0 | xmlMutexUnlock(&xmlRelaxNGMutex); |
2730 | 0 | return (0); |
2731 | 0 | } |
2732 | | |
2733 | | /** |
2734 | | * Cleanup the default Schemas type library associated to RelaxNG |
2735 | | * |
2736 | | * @deprecated This function will be made private. Call #xmlCleanupParser |
2737 | | * to free global state but see the warnings there. #xmlCleanupParser |
2738 | | * should be only called once at program exit. In most cases, you don't |
2739 | | * have call cleanup functions at all. |
2740 | | * |
2741 | | */ |
2742 | | void |
2743 | | xmlRelaxNGCleanupTypes(void) |
2744 | 0 | { |
2745 | 0 | xmlSchemaCleanupTypes(); |
2746 | 0 | xmlMutexLock(&xmlRelaxNGMutex); |
2747 | 0 | if (xmlRelaxNGTypeInitialized == 0) { |
2748 | 0 | xmlMutexUnlock(&xmlRelaxNGMutex); |
2749 | 0 | return; |
2750 | 0 | } |
2751 | 0 | xmlHashFree(xmlRelaxNGRegisteredTypes, xmlRelaxNGFreeTypeLibrary); |
2752 | 0 | xmlRelaxNGTypeInitialized = 0; |
2753 | 0 | xmlMutexUnlock(&xmlRelaxNGMutex); |
2754 | 0 | } |
2755 | | |
2756 | | /************************************************************************ |
2757 | | * * |
2758 | | * Compiling element content into regexp * |
2759 | | * * |
2760 | | * Sometime the element content can be compiled into a pure regexp, * |
2761 | | * This allows a faster execution and streamability at that level * |
2762 | | * * |
2763 | | ************************************************************************/ |
2764 | | |
2765 | | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
2766 | | xmlRelaxNGDefinePtr def); |
2767 | | |
2768 | | /** |
2769 | | * Check if a definition is nullable. |
2770 | | * |
2771 | | * @param def the definition to check |
2772 | | * @returns 1 if yes, 0 if no and -1 in case of error |
2773 | | */ |
2774 | | static int |
2775 | | xmlRelaxNGIsCompilable(xmlRelaxNGDefinePtr def) |
2776 | 0 | { |
2777 | 0 | int ret = -1; |
2778 | |
|
2779 | 0 | if (def == NULL) { |
2780 | 0 | return (-1); |
2781 | 0 | } |
2782 | 0 | if ((def->type != XML_RELAXNG_ELEMENT) && |
2783 | 0 | (def->dflags & IS_COMPILABLE)) |
2784 | 0 | return (1); |
2785 | 0 | if ((def->type != XML_RELAXNG_ELEMENT) && |
2786 | 0 | (def->dflags & IS_NOT_COMPILABLE)) |
2787 | 0 | return (0); |
2788 | 0 | switch (def->type) { |
2789 | 0 | case XML_RELAXNG_NOOP: |
2790 | 0 | ret = xmlRelaxNGIsCompilable(def->content); |
2791 | 0 | break; |
2792 | 0 | case XML_RELAXNG_TEXT: |
2793 | 0 | case XML_RELAXNG_EMPTY: |
2794 | 0 | ret = 1; |
2795 | 0 | break; |
2796 | 0 | case XML_RELAXNG_ELEMENT: |
2797 | | /* |
2798 | | * Check if the element content is compilable |
2799 | | */ |
2800 | 0 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
2801 | 0 | ((def->dflags & IS_COMPILABLE) == 0)) { |
2802 | 0 | xmlRelaxNGDefinePtr list; |
2803 | |
|
2804 | 0 | list = def->content; |
2805 | 0 | while (list != NULL) { |
2806 | 0 | ret = xmlRelaxNGIsCompilable(list); |
2807 | 0 | if (ret != 1) |
2808 | 0 | break; |
2809 | 0 | list = list->next; |
2810 | 0 | } |
2811 | | /* |
2812 | | * Because the routine is recursive, we must guard against |
2813 | | * discovering both COMPILABLE and NOT_COMPILABLE |
2814 | | */ |
2815 | 0 | if (ret == 0) { |
2816 | 0 | def->dflags &= ~IS_COMPILABLE; |
2817 | 0 | def->dflags |= IS_NOT_COMPILABLE; |
2818 | 0 | } |
2819 | 0 | if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE)) |
2820 | 0 | def->dflags |= IS_COMPILABLE; |
2821 | 0 | } |
2822 | | /* |
2823 | | * All elements return a compilable status unless they |
2824 | | * are generic like anyName |
2825 | | */ |
2826 | 0 | if ((def->nameClass != NULL) || (def->name == NULL)) |
2827 | 0 | ret = 0; |
2828 | 0 | else |
2829 | 0 | ret = 1; |
2830 | 0 | return (ret); |
2831 | 0 | case XML_RELAXNG_REF: |
2832 | 0 | case XML_RELAXNG_EXTERNALREF: |
2833 | 0 | case XML_RELAXNG_PARENTREF: |
2834 | 0 | if (def->depth == -20) { |
2835 | 0 | return (1); |
2836 | 0 | } else { |
2837 | 0 | xmlRelaxNGDefinePtr list; |
2838 | |
|
2839 | 0 | def->depth = -20; |
2840 | 0 | list = def->content; |
2841 | 0 | while (list != NULL) { |
2842 | 0 | ret = xmlRelaxNGIsCompilable(list); |
2843 | 0 | if (ret != 1) |
2844 | 0 | break; |
2845 | 0 | list = list->next; |
2846 | 0 | } |
2847 | 0 | } |
2848 | 0 | break; |
2849 | 0 | case XML_RELAXNG_START: |
2850 | 0 | case XML_RELAXNG_OPTIONAL: |
2851 | 0 | case XML_RELAXNG_ZEROORMORE: |
2852 | 0 | case XML_RELAXNG_ONEORMORE: |
2853 | 0 | case XML_RELAXNG_CHOICE: |
2854 | 0 | case XML_RELAXNG_GROUP: |
2855 | 0 | case XML_RELAXNG_DEF:{ |
2856 | 0 | xmlRelaxNGDefinePtr list; |
2857 | |
|
2858 | 0 | list = def->content; |
2859 | 0 | while (list != NULL) { |
2860 | 0 | ret = xmlRelaxNGIsCompilable(list); |
2861 | 0 | if (ret != 1) |
2862 | 0 | break; |
2863 | 0 | list = list->next; |
2864 | 0 | } |
2865 | 0 | break; |
2866 | 0 | } |
2867 | 0 | case XML_RELAXNG_EXCEPT: |
2868 | 0 | case XML_RELAXNG_ATTRIBUTE: |
2869 | 0 | case XML_RELAXNG_INTERLEAVE: |
2870 | 0 | case XML_RELAXNG_DATATYPE: |
2871 | 0 | case XML_RELAXNG_LIST: |
2872 | 0 | case XML_RELAXNG_PARAM: |
2873 | 0 | case XML_RELAXNG_VALUE: |
2874 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
2875 | 0 | ret = 0; |
2876 | 0 | break; |
2877 | 0 | } |
2878 | 0 | if (ret == 0) |
2879 | 0 | def->dflags |= IS_NOT_COMPILABLE; |
2880 | 0 | if (ret == 1) |
2881 | 0 | def->dflags |= IS_COMPILABLE; |
2882 | 0 | return (ret); |
2883 | 0 | } |
2884 | | |
2885 | | /** |
2886 | | * Compile the set of definitions, it works recursively, till the |
2887 | | * element boundaries, where it tries to compile the content if possible |
2888 | | * |
2889 | | * @param ctxt the RelaxNG parser context |
2890 | | * @param def the definition tree to compile |
2891 | | * @returns 0 if success and -1 in case of error |
2892 | | */ |
2893 | | static int |
2894 | | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
2895 | 0 | { |
2896 | 0 | int ret = 0; |
2897 | 0 | xmlRelaxNGDefinePtr list; |
2898 | |
|
2899 | 0 | if ((ctxt == NULL) || (def == NULL)) |
2900 | 0 | return (-1); |
2901 | | |
2902 | 0 | switch (def->type) { |
2903 | 0 | case XML_RELAXNG_START: |
2904 | 0 | if ((xmlRelaxNGIsCompilable(def) == 1) && (def->depth != -25)) { |
2905 | 0 | xmlAutomataPtr oldam = ctxt->am; |
2906 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
2907 | |
|
2908 | 0 | def->depth = -25; |
2909 | |
|
2910 | 0 | list = def->content; |
2911 | 0 | ctxt->am = xmlNewAutomata(); |
2912 | 0 | if (ctxt->am == NULL) |
2913 | 0 | return (-1); |
2914 | | |
2915 | | /* |
2916 | | * assume identical strings but not same pointer are different |
2917 | | * atoms, needed for non-determinism detection |
2918 | | * That way if 2 elements with the same name are in a choice |
2919 | | * branch the automata is found non-deterministic and |
2920 | | * we fallback to the normal validation which does the right |
2921 | | * thing of exploring both choices. |
2922 | | */ |
2923 | 0 | xmlAutomataSetFlags(ctxt->am, 1); |
2924 | |
|
2925 | 0 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
2926 | 0 | while (list != NULL) { |
2927 | 0 | xmlRelaxNGCompile(ctxt, list); |
2928 | 0 | list = list->next; |
2929 | 0 | } |
2930 | 0 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
2931 | 0 | if (xmlAutomataIsDeterminist(ctxt->am)) |
2932 | 0 | def->contModel = xmlAutomataCompile(ctxt->am); |
2933 | |
|
2934 | 0 | xmlFreeAutomata(ctxt->am); |
2935 | 0 | ctxt->state = oldstate; |
2936 | 0 | ctxt->am = oldam; |
2937 | 0 | } |
2938 | 0 | break; |
2939 | 0 | case XML_RELAXNG_ELEMENT: |
2940 | 0 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
2941 | 0 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
2942 | 0 | ctxt->state, NULL, |
2943 | 0 | def->name, def->ns, |
2944 | 0 | def); |
2945 | 0 | } |
2946 | 0 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
2947 | 0 | xmlAutomataPtr oldam = ctxt->am; |
2948 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
2949 | |
|
2950 | 0 | def->depth = -25; |
2951 | |
|
2952 | 0 | list = def->content; |
2953 | 0 | ctxt->am = xmlNewAutomata(); |
2954 | 0 | if (ctxt->am == NULL) |
2955 | 0 | return (-1); |
2956 | 0 | xmlAutomataSetFlags(ctxt->am, 1); |
2957 | 0 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
2958 | 0 | while (list != NULL) { |
2959 | 0 | xmlRelaxNGCompile(ctxt, list); |
2960 | 0 | list = list->next; |
2961 | 0 | } |
2962 | 0 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
2963 | 0 | def->contModel = xmlAutomataCompile(ctxt->am); |
2964 | 0 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
2965 | | /* |
2966 | | * we can only use the automata if it is determinist |
2967 | | */ |
2968 | 0 | xmlRegFreeRegexp(def->contModel); |
2969 | 0 | def->contModel = NULL; |
2970 | 0 | } |
2971 | 0 | xmlFreeAutomata(ctxt->am); |
2972 | 0 | ctxt->state = oldstate; |
2973 | 0 | ctxt->am = oldam; |
2974 | 0 | } else { |
2975 | 0 | xmlAutomataPtr oldam = ctxt->am; |
2976 | | |
2977 | | /* |
2978 | | * we can't build the content model for this element content |
2979 | | * but it still might be possible to build it for some of its |
2980 | | * children, recurse. |
2981 | | */ |
2982 | 0 | ret = xmlRelaxNGTryCompile(ctxt, def); |
2983 | 0 | ctxt->am = oldam; |
2984 | 0 | } |
2985 | 0 | break; |
2986 | 0 | case XML_RELAXNG_NOOP: |
2987 | 0 | ret = xmlRelaxNGCompile(ctxt, def->content); |
2988 | 0 | break; |
2989 | 0 | case XML_RELAXNG_OPTIONAL:{ |
2990 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
2991 | |
|
2992 | 0 | list = def->content; |
2993 | 0 | while (list != NULL) { |
2994 | 0 | xmlRelaxNGCompile(ctxt, list); |
2995 | 0 | list = list->next; |
2996 | 0 | } |
2997 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
2998 | 0 | break; |
2999 | 0 | } |
3000 | 0 | case XML_RELAXNG_ZEROORMORE:{ |
3001 | 0 | xmlAutomataStatePtr oldstate; |
3002 | |
|
3003 | 0 | ctxt->state = |
3004 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
3005 | 0 | oldstate = ctxt->state; |
3006 | 0 | list = def->content; |
3007 | 0 | while (list != NULL) { |
3008 | 0 | xmlRelaxNGCompile(ctxt, list); |
3009 | 0 | list = list->next; |
3010 | 0 | } |
3011 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
3012 | 0 | ctxt->state = |
3013 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
3014 | 0 | break; |
3015 | 0 | } |
3016 | 0 | case XML_RELAXNG_ONEORMORE:{ |
3017 | 0 | xmlAutomataStatePtr oldstate; |
3018 | |
|
3019 | 0 | list = def->content; |
3020 | 0 | while (list != NULL) { |
3021 | 0 | xmlRelaxNGCompile(ctxt, list); |
3022 | 0 | list = list->next; |
3023 | 0 | } |
3024 | 0 | oldstate = ctxt->state; |
3025 | 0 | list = def->content; |
3026 | 0 | while (list != NULL) { |
3027 | 0 | xmlRelaxNGCompile(ctxt, list); |
3028 | 0 | list = list->next; |
3029 | 0 | } |
3030 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
3031 | 0 | ctxt->state = |
3032 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
3033 | 0 | break; |
3034 | 0 | } |
3035 | 0 | case XML_RELAXNG_CHOICE:{ |
3036 | 0 | xmlAutomataStatePtr target = NULL; |
3037 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
3038 | |
|
3039 | 0 | list = def->content; |
3040 | 0 | while (list != NULL) { |
3041 | 0 | ctxt->state = oldstate; |
3042 | 0 | ret = xmlRelaxNGCompile(ctxt, list); |
3043 | 0 | if (ret != 0) |
3044 | 0 | break; |
3045 | 0 | if (target == NULL) |
3046 | 0 | target = ctxt->state; |
3047 | 0 | else { |
3048 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
3049 | 0 | target); |
3050 | 0 | } |
3051 | 0 | list = list->next; |
3052 | 0 | } |
3053 | 0 | ctxt->state = target; |
3054 | |
|
3055 | 0 | break; |
3056 | 0 | } |
3057 | 0 | case XML_RELAXNG_REF: |
3058 | 0 | case XML_RELAXNG_EXTERNALREF: |
3059 | 0 | case XML_RELAXNG_PARENTREF: |
3060 | 0 | case XML_RELAXNG_GROUP: |
3061 | 0 | case XML_RELAXNG_DEF: |
3062 | 0 | list = def->content; |
3063 | 0 | while (list != NULL) { |
3064 | 0 | ret = xmlRelaxNGCompile(ctxt, list); |
3065 | 0 | if (ret != 0) |
3066 | 0 | break; |
3067 | 0 | list = list->next; |
3068 | 0 | } |
3069 | 0 | break; |
3070 | 0 | case XML_RELAXNG_TEXT:{ |
3071 | 0 | xmlAutomataStatePtr oldstate; |
3072 | |
|
3073 | 0 | ctxt->state = |
3074 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
3075 | 0 | oldstate = ctxt->state; |
3076 | 0 | xmlRelaxNGCompile(ctxt, def->content); |
3077 | 0 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
3078 | 0 | ctxt->state, BAD_CAST "#text", |
3079 | 0 | NULL); |
3080 | 0 | ctxt->state = |
3081 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
3082 | 0 | break; |
3083 | 0 | } |
3084 | 0 | case XML_RELAXNG_EMPTY: |
3085 | 0 | ctxt->state = |
3086 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
3087 | 0 | break; |
3088 | 0 | case XML_RELAXNG_EXCEPT: |
3089 | 0 | case XML_RELAXNG_ATTRIBUTE: |
3090 | 0 | case XML_RELAXNG_INTERLEAVE: |
3091 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
3092 | 0 | case XML_RELAXNG_DATATYPE: |
3093 | 0 | case XML_RELAXNG_LIST: |
3094 | 0 | case XML_RELAXNG_PARAM: |
3095 | 0 | case XML_RELAXNG_VALUE: |
3096 | 0 | xmlRngPErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
3097 | 0 | "RNG internal error trying to compile %s\n", |
3098 | 0 | BAD_CAST xmlRelaxNGDefName(def), NULL); |
3099 | 0 | break; |
3100 | 0 | } |
3101 | 0 | return (ret); |
3102 | 0 | } |
3103 | | |
3104 | | /** |
3105 | | * Try to compile the set of definitions, it works recursively, |
3106 | | * possibly ignoring parts which cannot be compiled. |
3107 | | * |
3108 | | * @param ctxt the RelaxNG parser context |
3109 | | * @param def the definition tree to compile |
3110 | | * @returns 0 if success and -1 in case of error |
3111 | | */ |
3112 | | static int |
3113 | | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
3114 | 0 | { |
3115 | 0 | int ret = 0; |
3116 | 0 | xmlRelaxNGDefinePtr list; |
3117 | |
|
3118 | 0 | if ((ctxt == NULL) || (def == NULL)) |
3119 | 0 | return (-1); |
3120 | | |
3121 | 0 | if ((def->type == XML_RELAXNG_START) || |
3122 | 0 | (def->type == XML_RELAXNG_ELEMENT)) { |
3123 | 0 | ret = xmlRelaxNGIsCompilable(def); |
3124 | 0 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
3125 | 0 | ctxt->am = NULL; |
3126 | 0 | ret = xmlRelaxNGCompile(ctxt, def); |
3127 | 0 | return (ret); |
3128 | 0 | } |
3129 | 0 | } |
3130 | 0 | switch (def->type) { |
3131 | 0 | case XML_RELAXNG_NOOP: |
3132 | 0 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
3133 | 0 | break; |
3134 | 0 | case XML_RELAXNG_TEXT: |
3135 | 0 | case XML_RELAXNG_DATATYPE: |
3136 | 0 | case XML_RELAXNG_LIST: |
3137 | 0 | case XML_RELAXNG_PARAM: |
3138 | 0 | case XML_RELAXNG_VALUE: |
3139 | 0 | case XML_RELAXNG_EMPTY: |
3140 | 0 | case XML_RELAXNG_ELEMENT: |
3141 | 0 | ret = 0; |
3142 | 0 | break; |
3143 | 0 | case XML_RELAXNG_OPTIONAL: |
3144 | 0 | case XML_RELAXNG_ZEROORMORE: |
3145 | 0 | case XML_RELAXNG_ONEORMORE: |
3146 | 0 | case XML_RELAXNG_CHOICE: |
3147 | 0 | case XML_RELAXNG_GROUP: |
3148 | 0 | case XML_RELAXNG_DEF: |
3149 | 0 | case XML_RELAXNG_START: |
3150 | 0 | case XML_RELAXNG_REF: |
3151 | 0 | case XML_RELAXNG_EXTERNALREF: |
3152 | 0 | case XML_RELAXNG_PARENTREF: |
3153 | 0 | list = def->content; |
3154 | 0 | while (list != NULL) { |
3155 | 0 | ret = xmlRelaxNGTryCompile(ctxt, list); |
3156 | 0 | if (ret != 0) |
3157 | 0 | break; |
3158 | 0 | list = list->next; |
3159 | 0 | } |
3160 | 0 | break; |
3161 | 0 | case XML_RELAXNG_EXCEPT: |
3162 | 0 | case XML_RELAXNG_ATTRIBUTE: |
3163 | 0 | case XML_RELAXNG_INTERLEAVE: |
3164 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
3165 | 0 | ret = 0; |
3166 | 0 | break; |
3167 | 0 | } |
3168 | 0 | return (ret); |
3169 | 0 | } |
3170 | | |
3171 | | /************************************************************************ |
3172 | | * * |
3173 | | * Parsing functions * |
3174 | | * * |
3175 | | ************************************************************************/ |
3176 | | |
3177 | | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
3178 | | ctxt, xmlNodePtr node); |
3179 | | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
3180 | | ctxt, xmlNodePtr node); |
3181 | | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
3182 | | ctxt, xmlNodePtr nodes, |
3183 | | int group); |
3184 | | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
3185 | | ctxt, xmlNodePtr node); |
3186 | | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
3187 | | xmlNodePtr node); |
3188 | | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
3189 | | xmlNodePtr nodes); |
3190 | | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
3191 | | ctxt, xmlNodePtr node, |
3192 | | xmlRelaxNGDefinePtr |
3193 | | def); |
3194 | | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
3195 | | ctxt, xmlNodePtr nodes); |
3196 | | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
3197 | | xmlRelaxNGDefinePtr define, |
3198 | | xmlNodePtr elem); |
3199 | | |
3200 | | |
3201 | 0 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
3202 | | |
3203 | | /** |
3204 | | * Check if a definition is nullable. |
3205 | | * |
3206 | | * @param define the definition to verify |
3207 | | * @returns 1 if yes, 0 if no and -1 in case of error |
3208 | | */ |
3209 | | static int |
3210 | | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
3211 | 0 | { |
3212 | 0 | int ret; |
3213 | |
|
3214 | 0 | if (define == NULL) |
3215 | 0 | return (-1); |
3216 | | |
3217 | 0 | if (define->dflags & IS_NULLABLE) |
3218 | 0 | return (1); |
3219 | 0 | if (define->dflags & IS_NOT_NULLABLE) |
3220 | 0 | return (0); |
3221 | 0 | switch (define->type) { |
3222 | 0 | case XML_RELAXNG_EMPTY: |
3223 | 0 | case XML_RELAXNG_TEXT: |
3224 | 0 | ret = 1; |
3225 | 0 | break; |
3226 | 0 | case XML_RELAXNG_NOOP: |
3227 | 0 | case XML_RELAXNG_DEF: |
3228 | 0 | case XML_RELAXNG_REF: |
3229 | 0 | case XML_RELAXNG_EXTERNALREF: |
3230 | 0 | case XML_RELAXNG_PARENTREF: |
3231 | 0 | case XML_RELAXNG_ONEORMORE: |
3232 | 0 | ret = xmlRelaxNGIsNullable(define->content); |
3233 | 0 | break; |
3234 | 0 | case XML_RELAXNG_EXCEPT: |
3235 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
3236 | 0 | case XML_RELAXNG_ELEMENT: |
3237 | 0 | case XML_RELAXNG_DATATYPE: |
3238 | 0 | case XML_RELAXNG_PARAM: |
3239 | 0 | case XML_RELAXNG_VALUE: |
3240 | 0 | case XML_RELAXNG_LIST: |
3241 | 0 | case XML_RELAXNG_ATTRIBUTE: |
3242 | 0 | ret = 0; |
3243 | 0 | break; |
3244 | 0 | case XML_RELAXNG_CHOICE:{ |
3245 | 0 | xmlRelaxNGDefinePtr list = define->content; |
3246 | |
|
3247 | 0 | while (list != NULL) { |
3248 | 0 | ret = xmlRelaxNGIsNullable(list); |
3249 | 0 | if (ret != 0) |
3250 | 0 | goto done; |
3251 | 0 | list = list->next; |
3252 | 0 | } |
3253 | 0 | ret = 0; |
3254 | 0 | break; |
3255 | 0 | } |
3256 | 0 | case XML_RELAXNG_START: |
3257 | 0 | case XML_RELAXNG_INTERLEAVE: |
3258 | 0 | case XML_RELAXNG_GROUP:{ |
3259 | 0 | xmlRelaxNGDefinePtr list = define->content; |
3260 | |
|
3261 | 0 | while (list != NULL) { |
3262 | 0 | ret = xmlRelaxNGIsNullable(list); |
3263 | 0 | if (ret != 1) |
3264 | 0 | goto done; |
3265 | 0 | list = list->next; |
3266 | 0 | } |
3267 | 0 | return (1); |
3268 | 0 | } |
3269 | 0 | default: |
3270 | 0 | return (-1); |
3271 | 0 | } |
3272 | 0 | done: |
3273 | 0 | if (ret == 0) |
3274 | 0 | define->dflags |= IS_NOT_NULLABLE; |
3275 | 0 | if (ret == 1) |
3276 | 0 | define->dflags |= IS_NULLABLE; |
3277 | 0 | return (ret); |
3278 | 0 | } |
3279 | | |
3280 | | /** |
3281 | | * Check if a string is ignorable c.f. 4.2. Whitespace |
3282 | | * |
3283 | | * @param str a string |
3284 | | * @returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
3285 | | */ |
3286 | | static int |
3287 | | xmlRelaxNGIsBlank(xmlChar * str) |
3288 | 0 | { |
3289 | 0 | if (str == NULL) |
3290 | 0 | return (1); |
3291 | 0 | while (*str != 0) { |
3292 | 0 | if (!(IS_BLANK_CH(*str))) |
3293 | 0 | return (0); |
3294 | 0 | str++; |
3295 | 0 | } |
3296 | 0 | return (1); |
3297 | 0 | } |
3298 | | |
3299 | | /** |
3300 | | * Applies algorithm from 4.3. datatypeLibrary attribute |
3301 | | * |
3302 | | * @param ctxt a Relax-NG parser context |
3303 | | * @param node the current data or value element |
3304 | | * @returns the datatypeLibrary value or NULL if not found |
3305 | | */ |
3306 | | static xmlChar * |
3307 | | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
3308 | | xmlNodePtr node) |
3309 | 0 | { |
3310 | 0 | xmlChar *ret, *escape; |
3311 | |
|
3312 | 0 | if (node == NULL) |
3313 | 0 | return(NULL); |
3314 | | |
3315 | 0 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
3316 | 0 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
3317 | 0 | if (ret != NULL) { |
3318 | 0 | if (ret[0] == 0) { |
3319 | 0 | xmlFree(ret); |
3320 | 0 | return (NULL); |
3321 | 0 | } |
3322 | 0 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
3323 | 0 | if (escape == NULL) { |
3324 | 0 | return (ret); |
3325 | 0 | } |
3326 | 0 | xmlFree(ret); |
3327 | 0 | return (escape); |
3328 | 0 | } |
3329 | 0 | } |
3330 | 0 | node = node->parent; |
3331 | 0 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
3332 | 0 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
3333 | 0 | if (ret != NULL) { |
3334 | 0 | if (ret[0] == 0) { |
3335 | 0 | xmlFree(ret); |
3336 | 0 | return (NULL); |
3337 | 0 | } |
3338 | 0 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
3339 | 0 | if (escape == NULL) { |
3340 | 0 | return (ret); |
3341 | 0 | } |
3342 | 0 | xmlFree(ret); |
3343 | 0 | return (escape); |
3344 | 0 | } |
3345 | 0 | node = node->parent; |
3346 | 0 | } |
3347 | 0 | return (NULL); |
3348 | 0 | } |
3349 | | |
3350 | | /** |
3351 | | * parse the content of a RelaxNG value node. |
3352 | | * |
3353 | | * @param ctxt a Relax-NG parser context |
3354 | | * @param node the data node. |
3355 | | * @returns the definition pointer or NULL in case of error |
3356 | | */ |
3357 | | static xmlRelaxNGDefinePtr |
3358 | | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
3359 | 0 | { |
3360 | 0 | xmlRelaxNGDefinePtr def = NULL; |
3361 | 0 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
3362 | 0 | xmlChar *type; |
3363 | 0 | xmlChar *library; |
3364 | 0 | int success = 0; |
3365 | |
|
3366 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
3367 | 0 | if (def == NULL) |
3368 | 0 | return (NULL); |
3369 | 0 | def->type = XML_RELAXNG_VALUE; |
3370 | |
|
3371 | 0 | type = xmlGetProp(node, BAD_CAST "type"); |
3372 | 0 | if (type != NULL) { |
3373 | 0 | xmlRelaxNGNormExtSpace(type); |
3374 | 0 | if (xmlValidateNCName(type, 0)) { |
3375 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
3376 | 0 | "value type '%s' is not an NCName\n", type, NULL); |
3377 | 0 | } |
3378 | 0 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
3379 | 0 | if (library == NULL) |
3380 | 0 | library = |
3381 | 0 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
3382 | |
|
3383 | 0 | def->name = type; |
3384 | 0 | def->ns = library; |
3385 | |
|
3386 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) |
3387 | 0 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
3388 | 0 | if (lib == NULL) { |
3389 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
3390 | 0 | "Use of unregistered type library '%s'\n", library, |
3391 | 0 | NULL); |
3392 | 0 | def->data = NULL; |
3393 | 0 | } else { |
3394 | 0 | def->data = lib; |
3395 | 0 | if (lib->have == NULL) { |
3396 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
3397 | 0 | "Internal error with type library '%s': no 'have'\n", |
3398 | 0 | library, NULL); |
3399 | 0 | } else { |
3400 | 0 | success = lib->have(lib->data, def->name); |
3401 | 0 | if (success != 1) { |
3402 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
3403 | 0 | "Error type '%s' is not exported by type library '%s'\n", |
3404 | 0 | def->name, library); |
3405 | 0 | } |
3406 | 0 | } |
3407 | 0 | } |
3408 | 0 | } |
3409 | 0 | if (node->children == NULL) { |
3410 | 0 | def->value = xmlStrdup(BAD_CAST ""); |
3411 | 0 | } else if (((node->children->type != XML_TEXT_NODE) && |
3412 | 0 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
3413 | 0 | (node->children->next != NULL)) { |
3414 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
3415 | 0 | "Expecting a single text value for <value>content\n", |
3416 | 0 | NULL, NULL); |
3417 | 0 | } else if (def != NULL) { |
3418 | 0 | def->value = xmlNodeGetContent(node); |
3419 | 0 | if (def->value == NULL) { |
3420 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
3421 | 0 | "Element <value> has no content\n", NULL, NULL); |
3422 | 0 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
3423 | 0 | void *val = NULL; |
3424 | |
|
3425 | 0 | success = |
3426 | 0 | lib->check(lib->data, def->name, def->value, &val, node); |
3427 | 0 | if (success != 1) { |
3428 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
3429 | 0 | "Value '%s' is not acceptable for type '%s'\n", |
3430 | 0 | def->value, def->name); |
3431 | 0 | } else { |
3432 | 0 | if (val != NULL) |
3433 | 0 | def->attrs = val; |
3434 | 0 | } |
3435 | 0 | } |
3436 | 0 | } |
3437 | 0 | return (def); |
3438 | 0 | } |
3439 | | |
3440 | | /** |
3441 | | * parse the content of a RelaxNG data node. |
3442 | | * |
3443 | | * @param ctxt a Relax-NG parser context |
3444 | | * @param node the data node. |
3445 | | * @returns the definition pointer or NULL in case of error |
3446 | | */ |
3447 | | static xmlRelaxNGDefinePtr |
3448 | | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
3449 | 0 | { |
3450 | 0 | xmlRelaxNGDefinePtr def = NULL, except; |
3451 | 0 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
3452 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
3453 | 0 | xmlChar *type; |
3454 | 0 | xmlChar *library; |
3455 | 0 | xmlNodePtr content; |
3456 | 0 | int tmp; |
3457 | |
|
3458 | 0 | type = xmlGetProp(node, BAD_CAST "type"); |
3459 | 0 | if (type == NULL) { |
3460 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
3461 | 0 | NULL); |
3462 | 0 | return (NULL); |
3463 | 0 | } |
3464 | 0 | xmlRelaxNGNormExtSpace(type); |
3465 | 0 | if (xmlValidateNCName(type, 0)) { |
3466 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
3467 | 0 | "data type '%s' is not an NCName\n", type, NULL); |
3468 | 0 | } |
3469 | 0 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
3470 | 0 | if (library == NULL) |
3471 | 0 | library = |
3472 | 0 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
3473 | |
|
3474 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
3475 | 0 | if (def == NULL) { |
3476 | 0 | xmlFree(library); |
3477 | 0 | xmlFree(type); |
3478 | 0 | return (NULL); |
3479 | 0 | } |
3480 | 0 | def->type = XML_RELAXNG_DATATYPE; |
3481 | 0 | def->name = type; |
3482 | 0 | def->ns = library; |
3483 | |
|
3484 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) |
3485 | 0 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
3486 | 0 | if (lib == NULL) { |
3487 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
3488 | 0 | "Use of unregistered type library '%s'\n", library, |
3489 | 0 | NULL); |
3490 | 0 | def->data = NULL; |
3491 | 0 | } else { |
3492 | 0 | def->data = lib; |
3493 | 0 | if (lib->have == NULL) { |
3494 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
3495 | 0 | "Internal error with type library '%s': no 'have'\n", |
3496 | 0 | library, NULL); |
3497 | 0 | } else { |
3498 | 0 | tmp = lib->have(lib->data, def->name); |
3499 | 0 | if (tmp != 1) { |
3500 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
3501 | 0 | "Error type '%s' is not exported by type library '%s'\n", |
3502 | 0 | def->name, library); |
3503 | 0 | } else |
3504 | 0 | if ((xmlStrEqual |
3505 | 0 | (library, |
3506 | 0 | BAD_CAST |
3507 | 0 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
3508 | 0 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
3509 | 0 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
3510 | 0 | ctxt->idref = 1; |
3511 | 0 | } |
3512 | 0 | } |
3513 | 0 | } |
3514 | 0 | content = node->children; |
3515 | | |
3516 | | /* |
3517 | | * Handle optional params |
3518 | | */ |
3519 | 0 | while (content != NULL) { |
3520 | 0 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
3521 | 0 | break; |
3522 | 0 | if (xmlStrEqual(library, |
3523 | 0 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
3524 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
3525 | 0 | "Type library '%s' does not allow type parameters\n", |
3526 | 0 | library, NULL); |
3527 | 0 | content = content->next; |
3528 | 0 | while ((content != NULL) && |
3529 | 0 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
3530 | 0 | content = content->next; |
3531 | 0 | } else { |
3532 | 0 | param = xmlRelaxNGNewDefine(ctxt, node); |
3533 | 0 | if (param != NULL) { |
3534 | 0 | param->type = XML_RELAXNG_PARAM; |
3535 | 0 | param->name = xmlGetProp(content, BAD_CAST "name"); |
3536 | 0 | if (param->name == NULL) { |
3537 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
3538 | 0 | "param has no name\n", NULL, NULL); |
3539 | 0 | } |
3540 | 0 | param->value = xmlNodeGetContent(content); |
3541 | 0 | if (lastparam == NULL) { |
3542 | 0 | def->attrs = lastparam = param; |
3543 | 0 | } else { |
3544 | 0 | lastparam->next = param; |
3545 | 0 | lastparam = param; |
3546 | 0 | } |
3547 | 0 | if (lib != NULL) { |
3548 | 0 | } |
3549 | 0 | } |
3550 | 0 | content = content->next; |
3551 | 0 | } |
3552 | 0 | } |
3553 | | /* |
3554 | | * Handle optional except |
3555 | | */ |
3556 | 0 | if ((content != NULL) |
3557 | 0 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
3558 | 0 | xmlNodePtr child; |
3559 | 0 | xmlRelaxNGDefinePtr tmp2, last = NULL; |
3560 | |
|
3561 | 0 | except = xmlRelaxNGNewDefine(ctxt, node); |
3562 | 0 | if (except == NULL) { |
3563 | 0 | return (def); |
3564 | 0 | } |
3565 | 0 | except->type = XML_RELAXNG_EXCEPT; |
3566 | 0 | child = content->children; |
3567 | 0 | def->content = except; |
3568 | 0 | if (child == NULL) { |
3569 | 0 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
3570 | 0 | "except has no content\n", NULL, NULL); |
3571 | 0 | } |
3572 | 0 | while (child != NULL) { |
3573 | 0 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
3574 | 0 | if (tmp2 != NULL) { |
3575 | 0 | if (last == NULL) { |
3576 | 0 | except->content = last = tmp2; |
3577 | 0 | } else { |
3578 | 0 | last->next = tmp2; |
3579 | 0 | last = tmp2; |
3580 | 0 | } |
3581 | 0 | } |
3582 | 0 | child = child->next; |
3583 | 0 | } |
3584 | 0 | content = content->next; |
3585 | 0 | } |
3586 | | /* |
3587 | | * Check there is no unhandled data |
3588 | | */ |
3589 | 0 | if (content != NULL) { |
3590 | 0 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
3591 | 0 | "Element data has unexpected content %s\n", |
3592 | 0 | content->name, NULL); |
3593 | 0 | } |
3594 | |
|
3595 | 0 | return (def); |
3596 | 0 | } |
3597 | | |
3598 | | static const xmlChar *invalidName = BAD_CAST "\1"; |
3599 | | |
3600 | | /** |
3601 | | * Compare the 2 lists of element definitions. The comparison is |
3602 | | * that if both lists do not accept the same QNames, it returns 1 |
3603 | | * If the 2 lists can accept the same QName the comparison returns 0 |
3604 | | * |
3605 | | * @param def1 the first element/attribute defs |
3606 | | * @param def2 the second element/attribute defs |
3607 | | * @returns 1 distinct, 0 if equal |
3608 | | */ |
3609 | | static int |
3610 | | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
3611 | | xmlRelaxNGDefinePtr def2) |
3612 | 0 | { |
3613 | 0 | int ret = 1; |
3614 | 0 | xmlNode node; |
3615 | 0 | xmlNs ns; |
3616 | 0 | xmlRelaxNGValidCtxt ctxt; |
3617 | |
|
3618 | 0 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
3619 | |
|
3620 | 0 | ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR; |
3621 | |
|
3622 | 0 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
3623 | 0 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
3624 | 0 | if (def2->type == XML_RELAXNG_TEXT) |
3625 | 0 | return (1); |
3626 | 0 | if (def1->name != NULL) { |
3627 | 0 | node.name = def1->name; |
3628 | 0 | } else { |
3629 | 0 | node.name = invalidName; |
3630 | 0 | } |
3631 | 0 | if (def1->ns != NULL) { |
3632 | 0 | if (def1->ns[0] == 0) { |
3633 | 0 | node.ns = NULL; |
3634 | 0 | } else { |
3635 | 0 | node.ns = &ns; |
3636 | 0 | ns.href = def1->ns; |
3637 | 0 | } |
3638 | 0 | } else { |
3639 | 0 | node.ns = NULL; |
3640 | 0 | } |
3641 | 0 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
3642 | 0 | if (def1->nameClass != NULL) { |
3643 | 0 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
3644 | 0 | } else { |
3645 | 0 | ret = 0; |
3646 | 0 | } |
3647 | 0 | } else { |
3648 | 0 | ret = 1; |
3649 | 0 | } |
3650 | 0 | } else if (def1->type == XML_RELAXNG_TEXT) { |
3651 | 0 | if (def2->type == XML_RELAXNG_TEXT) |
3652 | 0 | return (0); |
3653 | 0 | return (1); |
3654 | 0 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
3655 | 0 | ret = xmlRelaxNGCompareNameClasses(def1->content, def2); |
3656 | 0 | if (ret == 0) |
3657 | 0 | ret = 1; |
3658 | 0 | else if (ret == 1) |
3659 | 0 | ret = 0; |
3660 | 0 | } else { |
3661 | | /* TODO */ |
3662 | 0 | ret = 0; |
3663 | 0 | } |
3664 | 0 | if (ret == 0) |
3665 | 0 | return (ret); |
3666 | 0 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
3667 | 0 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
3668 | 0 | if (def2->name != NULL) { |
3669 | 0 | node.name = def2->name; |
3670 | 0 | } else { |
3671 | 0 | node.name = invalidName; |
3672 | 0 | } |
3673 | 0 | node.ns = &ns; |
3674 | 0 | if (def2->ns != NULL) { |
3675 | 0 | if (def2->ns[0] == 0) { |
3676 | 0 | node.ns = NULL; |
3677 | 0 | } else { |
3678 | 0 | ns.href = def2->ns; |
3679 | 0 | } |
3680 | 0 | } else { |
3681 | 0 | ns.href = invalidName; |
3682 | 0 | } |
3683 | 0 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
3684 | 0 | if (def2->nameClass != NULL) { |
3685 | 0 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
3686 | 0 | } else { |
3687 | 0 | ret = 0; |
3688 | 0 | } |
3689 | 0 | } else { |
3690 | 0 | ret = 1; |
3691 | 0 | } |
3692 | 0 | } else { |
3693 | | /* TODO */ |
3694 | 0 | ret = 0; |
3695 | 0 | } |
3696 | |
|
3697 | 0 | return (ret); |
3698 | 0 | } |
3699 | | |
3700 | | /** |
3701 | | * Compare the 2 lists of element or attribute definitions. The comparison |
3702 | | * is that if both lists do not accept the same QNames, it returns 1 |
3703 | | * If the 2 lists can accept the same QName the comparison returns 0 |
3704 | | * |
3705 | | * @param ctxt a Relax-NG parser context |
3706 | | * @param def1 the first list of element/attribute defs |
3707 | | * @param def2 the second list of element/attribute defs |
3708 | | * @returns 1 distinct, 0 if equal |
3709 | | */ |
3710 | | static int |
3711 | | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
3712 | | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
3713 | | xmlRelaxNGDefinePtr * def2) |
3714 | 0 | { |
3715 | 0 | xmlRelaxNGDefinePtr *basedef2 = def2; |
3716 | |
|
3717 | 0 | if ((def1 == NULL) || (def2 == NULL)) |
3718 | 0 | return (1); |
3719 | 0 | if ((*def1 == NULL) || (*def2 == NULL)) |
3720 | 0 | return (1); |
3721 | 0 | while (*def1 != NULL) { |
3722 | 0 | while ((*def2) != NULL) { |
3723 | 0 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
3724 | 0 | return (0); |
3725 | 0 | def2++; |
3726 | 0 | } |
3727 | 0 | def2 = basedef2; |
3728 | 0 | def1++; |
3729 | 0 | } |
3730 | 0 | return (1); |
3731 | 0 | } |
3732 | | |
3733 | | /** |
3734 | | * Check if the definition can only generate attributes |
3735 | | * |
3736 | | * @param ctxt a Relax-NG parser context |
3737 | | * @param def the definition definition |
3738 | | * @returns 1 if yes, 0 if no and -1 in case of error. |
3739 | | */ |
3740 | | static int |
3741 | | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
3742 | | xmlRelaxNGDefinePtr def) |
3743 | 0 | { |
3744 | 0 | xmlRelaxNGDefinePtr parent, cur, tmp; |
3745 | | |
3746 | | /* |
3747 | | * Don't run that check in case of error. Infinite recursion |
3748 | | * becomes possible. |
3749 | | */ |
3750 | 0 | if (ctxt->nbErrors != 0) |
3751 | 0 | return (-1); |
3752 | | |
3753 | 0 | parent = NULL; |
3754 | 0 | cur = def; |
3755 | 0 | while (cur != NULL) { |
3756 | 0 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
3757 | 0 | (cur->type == XML_RELAXNG_TEXT) || |
3758 | 0 | (cur->type == XML_RELAXNG_DATATYPE) || |
3759 | 0 | (cur->type == XML_RELAXNG_PARAM) || |
3760 | 0 | (cur->type == XML_RELAXNG_LIST) || |
3761 | 0 | (cur->type == XML_RELAXNG_VALUE) || |
3762 | 0 | (cur->type == XML_RELAXNG_EMPTY)) |
3763 | 0 | return (0); |
3764 | 0 | if ((cur->type == XML_RELAXNG_CHOICE) || |
3765 | 0 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
3766 | 0 | (cur->type == XML_RELAXNG_GROUP) || |
3767 | 0 | (cur->type == XML_RELAXNG_ONEORMORE) || |
3768 | 0 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
3769 | 0 | (cur->type == XML_RELAXNG_OPTIONAL) || |
3770 | 0 | (cur->type == XML_RELAXNG_PARENTREF) || |
3771 | 0 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
3772 | 0 | (cur->type == XML_RELAXNG_REF) || |
3773 | 0 | (cur->type == XML_RELAXNG_DEF)) { |
3774 | 0 | if (cur->content != NULL) { |
3775 | 0 | parent = cur; |
3776 | 0 | cur = cur->content; |
3777 | 0 | tmp = cur; |
3778 | 0 | while (tmp != NULL) { |
3779 | 0 | tmp->parent = parent; |
3780 | 0 | tmp = tmp->next; |
3781 | 0 | } |
3782 | 0 | continue; |
3783 | 0 | } |
3784 | 0 | } |
3785 | 0 | if (cur == def) |
3786 | 0 | break; |
3787 | 0 | if (cur->next != NULL) { |
3788 | 0 | cur = cur->next; |
3789 | 0 | continue; |
3790 | 0 | } |
3791 | 0 | do { |
3792 | 0 | cur = cur->parent; |
3793 | 0 | if (cur == NULL) |
3794 | 0 | break; |
3795 | 0 | if (cur == def) |
3796 | 0 | return (1); |
3797 | 0 | if (cur->next != NULL) { |
3798 | 0 | cur = cur->next; |
3799 | 0 | break; |
3800 | 0 | } |
3801 | 0 | } while (cur != NULL); |
3802 | 0 | } |
3803 | 0 | return (1); |
3804 | 0 | } |
3805 | | |
3806 | | /** |
3807 | | * Compute the list of top elements a definition can generate |
3808 | | * |
3809 | | * @param ctxt a Relax-NG parser context |
3810 | | * @param def the definition definition |
3811 | | * @param eora gather elements (0), attributes (1) or elements and text (2) |
3812 | | * @returns a list of elements or NULL if none was found. |
3813 | | */ |
3814 | | static xmlRelaxNGDefinePtr * |
3815 | | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
3816 | | xmlRelaxNGDefinePtr def, int eora) |
3817 | 0 | { |
3818 | 0 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
3819 | 0 | int len = 0; |
3820 | 0 | int max = 0; |
3821 | | |
3822 | | /* |
3823 | | * Don't run that check in case of error. Infinite recursion |
3824 | | * becomes possible. |
3825 | | */ |
3826 | 0 | if (ctxt->nbErrors != 0) |
3827 | 0 | return (NULL); |
3828 | | |
3829 | 0 | parent = NULL; |
3830 | 0 | cur = def; |
3831 | 0 | while (cur != NULL) { |
3832 | 0 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
3833 | 0 | (cur->type == XML_RELAXNG_TEXT))) || |
3834 | 0 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE)) || |
3835 | 0 | ((eora == 2) && ((cur->type == XML_RELAXNG_DATATYPE) || |
3836 | 0 | (cur->type == XML_RELAXNG_ELEMENT) || |
3837 | 0 | (cur->type == XML_RELAXNG_LIST) || |
3838 | 0 | (cur->type == XML_RELAXNG_TEXT) || |
3839 | 0 | (cur->type == XML_RELAXNG_VALUE)))) { |
3840 | 0 | if (ret == NULL) { |
3841 | 0 | max = 10; |
3842 | 0 | ret = (xmlRelaxNGDefinePtr *) |
3843 | 0 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
3844 | 0 | if (ret == NULL) { |
3845 | 0 | xmlRngPErrMemory(ctxt); |
3846 | 0 | return (NULL); |
3847 | 0 | } |
3848 | 0 | } else if (max <= len) { |
3849 | 0 | xmlRelaxNGDefinePtr *temp; |
3850 | |
|
3851 | 0 | max *= 2; |
3852 | 0 | temp = xmlRealloc(ret, |
3853 | 0 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
3854 | 0 | if (temp == NULL) { |
3855 | 0 | xmlRngPErrMemory(ctxt); |
3856 | 0 | xmlFree(ret); |
3857 | 0 | return (NULL); |
3858 | 0 | } |
3859 | 0 | ret = temp; |
3860 | 0 | } |
3861 | 0 | ret[len++] = cur; |
3862 | 0 | ret[len] = NULL; |
3863 | 0 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
3864 | 0 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
3865 | 0 | (cur->type == XML_RELAXNG_GROUP) || |
3866 | 0 | (cur->type == XML_RELAXNG_ONEORMORE) || |
3867 | 0 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
3868 | 0 | (cur->type == XML_RELAXNG_OPTIONAL) || |
3869 | 0 | (cur->type == XML_RELAXNG_PARENTREF) || |
3870 | 0 | (cur->type == XML_RELAXNG_REF) || |
3871 | 0 | (cur->type == XML_RELAXNG_DEF) || |
3872 | 0 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
3873 | | /* |
3874 | | * Don't go within elements or attributes or string values. |
3875 | | * Just gather the element top list |
3876 | | */ |
3877 | 0 | if (cur->content != NULL) { |
3878 | 0 | parent = cur; |
3879 | 0 | cur = cur->content; |
3880 | 0 | tmp = cur; |
3881 | 0 | while (tmp != NULL) { |
3882 | 0 | tmp->parent = parent; |
3883 | 0 | tmp = tmp->next; |
3884 | 0 | } |
3885 | 0 | continue; |
3886 | 0 | } |
3887 | 0 | } |
3888 | 0 | if (cur == def) |
3889 | 0 | break; |
3890 | 0 | if (cur->next != NULL) { |
3891 | 0 | cur = cur->next; |
3892 | 0 | continue; |
3893 | 0 | } |
3894 | 0 | do { |
3895 | 0 | cur = cur->parent; |
3896 | 0 | if (cur == NULL) |
3897 | 0 | break; |
3898 | 0 | if (cur == def) |
3899 | 0 | return (ret); |
3900 | 0 | if (cur->next != NULL) { |
3901 | 0 | cur = cur->next; |
3902 | 0 | break; |
3903 | 0 | } |
3904 | 0 | } while (cur != NULL); |
3905 | 0 | } |
3906 | 0 | return (ret); |
3907 | 0 | } |
3908 | | |
3909 | | /** |
3910 | | * Also used to find indeterministic pattern in choice |
3911 | | * |
3912 | | * @param ctxt a Relax-NG parser context |
3913 | | * @param def the choice definition |
3914 | | */ |
3915 | | static void |
3916 | | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
3917 | | xmlRelaxNGDefinePtr def) |
3918 | 0 | { |
3919 | 0 | xmlRelaxNGDefinePtr **list; |
3920 | 0 | xmlRelaxNGDefinePtr cur; |
3921 | 0 | int nbchild = 0, i, j, ret; |
3922 | 0 | int is_nullable = 0; |
3923 | 0 | int is_indeterminist = 0; |
3924 | 0 | xmlHashTablePtr triage = NULL; |
3925 | 0 | int is_triable = 1; |
3926 | |
|
3927 | 0 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
3928 | 0 | return; |
3929 | | |
3930 | 0 | if (def->dflags & IS_PROCESSED) |
3931 | 0 | return; |
3932 | | |
3933 | | /* |
3934 | | * Don't run that check in case of error. Infinite recursion |
3935 | | * becomes possible. |
3936 | | */ |
3937 | 0 | if (ctxt->nbErrors != 0) |
3938 | 0 | return; |
3939 | | |
3940 | 0 | is_nullable = xmlRelaxNGIsNullable(def); |
3941 | |
|
3942 | 0 | cur = def->content; |
3943 | 0 | while (cur != NULL) { |
3944 | 0 | nbchild++; |
3945 | 0 | cur = cur->next; |
3946 | 0 | } |
3947 | |
|
3948 | 0 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
3949 | 0 | sizeof(xmlRelaxNGDefinePtr |
3950 | 0 | *)); |
3951 | 0 | if (list == NULL) { |
3952 | 0 | xmlRngPErrMemory(ctxt); |
3953 | 0 | return; |
3954 | 0 | } |
3955 | 0 | i = 0; |
3956 | | /* |
3957 | | * a bit strong but safe |
3958 | | */ |
3959 | 0 | if (is_nullable == 0) { |
3960 | 0 | triage = xmlHashCreate(10); |
3961 | 0 | } else { |
3962 | 0 | is_triable = 0; |
3963 | 0 | } |
3964 | 0 | cur = def->content; |
3965 | 0 | while (cur != NULL) { |
3966 | 0 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
3967 | 0 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
3968 | 0 | is_triable = 0; |
3969 | 0 | } else if (is_triable == 1) { |
3970 | 0 | xmlRelaxNGDefinePtr *tmp; |
3971 | 0 | int res; |
3972 | |
|
3973 | 0 | tmp = list[i]; |
3974 | 0 | while ((*tmp != NULL) && (is_triable == 1)) { |
3975 | 0 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
3976 | 0 | res = xmlHashAddEntry2(triage, |
3977 | 0 | BAD_CAST "#text", NULL, |
3978 | 0 | (void *) cur); |
3979 | 0 | if (res != 0) |
3980 | 0 | is_triable = -1; |
3981 | 0 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
3982 | 0 | ((*tmp)->name != NULL)) { |
3983 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
3984 | 0 | res = xmlHashAddEntry2(triage, |
3985 | 0 | (*tmp)->name, NULL, |
3986 | 0 | (void *) cur); |
3987 | 0 | else |
3988 | 0 | res = xmlHashAddEntry2(triage, |
3989 | 0 | (*tmp)->name, (*tmp)->ns, |
3990 | 0 | (void *) cur); |
3991 | 0 | if (res != 0) |
3992 | 0 | is_triable = -1; |
3993 | 0 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
3994 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
3995 | 0 | res = xmlHashAddEntry2(triage, |
3996 | 0 | BAD_CAST "#any", NULL, |
3997 | 0 | (void *) cur); |
3998 | 0 | else |
3999 | 0 | res = xmlHashAddEntry2(triage, |
4000 | 0 | BAD_CAST "#any", (*tmp)->ns, |
4001 | 0 | (void *) cur); |
4002 | 0 | if (res != 0) |
4003 | 0 | is_triable = -1; |
4004 | 0 | } else { |
4005 | 0 | is_triable = -1; |
4006 | 0 | } |
4007 | 0 | tmp++; |
4008 | 0 | } |
4009 | 0 | } |
4010 | 0 | i++; |
4011 | 0 | cur = cur->next; |
4012 | 0 | } |
4013 | |
|
4014 | 0 | for (i = 0; i < nbchild; i++) { |
4015 | 0 | if (list[i] == NULL) |
4016 | 0 | continue; |
4017 | 0 | for (j = 0; j < i; j++) { |
4018 | 0 | if (list[j] == NULL) |
4019 | 0 | continue; |
4020 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
4021 | 0 | if (ret == 0) { |
4022 | 0 | is_indeterminist = 1; |
4023 | 0 | } |
4024 | 0 | } |
4025 | 0 | } |
4026 | 0 | for (i = 0; i < nbchild; i++) { |
4027 | 0 | if (list[i] != NULL) |
4028 | 0 | xmlFree(list[i]); |
4029 | 0 | } |
4030 | |
|
4031 | 0 | xmlFree(list); |
4032 | 0 | if (is_indeterminist) { |
4033 | 0 | def->dflags |= IS_INDETERMINIST; |
4034 | 0 | } |
4035 | 0 | if (is_triable == 1) { |
4036 | 0 | def->dflags |= IS_TRIABLE; |
4037 | 0 | def->data = triage; |
4038 | 0 | } else if (triage != NULL) { |
4039 | 0 | xmlHashFree(triage, NULL); |
4040 | 0 | } |
4041 | 0 | def->dflags |= IS_PROCESSED; |
4042 | 0 | } |
4043 | | |
4044 | | /** |
4045 | | * Detects violations of rule 7.3 |
4046 | | * |
4047 | | * @param ctxt a Relax-NG parser context |
4048 | | * @param def the group definition |
4049 | | */ |
4050 | | static void |
4051 | | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
4052 | | xmlRelaxNGDefinePtr def) |
4053 | 0 | { |
4054 | 0 | xmlRelaxNGDefinePtr **list; |
4055 | 0 | xmlRelaxNGDefinePtr cur; |
4056 | 0 | int nbchild = 0, i, j, ret; |
4057 | |
|
4058 | 0 | if ((def == NULL) || |
4059 | 0 | ((def->type != XML_RELAXNG_GROUP) && |
4060 | 0 | (def->type != XML_RELAXNG_ELEMENT))) |
4061 | 0 | return; |
4062 | | |
4063 | 0 | if (def->dflags & IS_PROCESSED) |
4064 | 0 | return; |
4065 | | |
4066 | | /* |
4067 | | * Don't run that check in case of error. Infinite recursion |
4068 | | * becomes possible. |
4069 | | */ |
4070 | 0 | if (ctxt->nbErrors != 0) |
4071 | 0 | return; |
4072 | | |
4073 | 0 | cur = def->attrs; |
4074 | 0 | while (cur != NULL) { |
4075 | 0 | nbchild++; |
4076 | 0 | cur = cur->next; |
4077 | 0 | } |
4078 | 0 | cur = def->content; |
4079 | 0 | while (cur != NULL) { |
4080 | 0 | nbchild++; |
4081 | 0 | cur = cur->next; |
4082 | 0 | } |
4083 | |
|
4084 | 0 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
4085 | 0 | sizeof(xmlRelaxNGDefinePtr |
4086 | 0 | *)); |
4087 | 0 | if (list == NULL) { |
4088 | 0 | xmlRngPErrMemory(ctxt); |
4089 | 0 | return; |
4090 | 0 | } |
4091 | 0 | i = 0; |
4092 | 0 | cur = def->attrs; |
4093 | 0 | while (cur != NULL) { |
4094 | 0 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
4095 | 0 | i++; |
4096 | 0 | cur = cur->next; |
4097 | 0 | } |
4098 | 0 | cur = def->content; |
4099 | 0 | while (cur != NULL) { |
4100 | 0 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
4101 | 0 | i++; |
4102 | 0 | cur = cur->next; |
4103 | 0 | } |
4104 | |
|
4105 | 0 | for (i = 0; i < nbchild; i++) { |
4106 | 0 | if (list[i] == NULL) |
4107 | 0 | continue; |
4108 | 0 | for (j = 0; j < i; j++) { |
4109 | 0 | if (list[j] == NULL) |
4110 | 0 | continue; |
4111 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
4112 | 0 | if (ret == 0) { |
4113 | 0 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
4114 | 0 | "Attributes conflicts in group\n", NULL, NULL); |
4115 | 0 | } |
4116 | 0 | } |
4117 | 0 | } |
4118 | 0 | for (i = 0; i < nbchild; i++) { |
4119 | 0 | if (list[i] != NULL) |
4120 | 0 | xmlFree(list[i]); |
4121 | 0 | } |
4122 | |
|
4123 | 0 | xmlFree(list); |
4124 | 0 | def->dflags |= IS_PROCESSED; |
4125 | 0 | } |
4126 | | |
4127 | | /** |
4128 | | * A lot of work for preprocessing interleave definitions |
4129 | | * is potentially needed to get a decent execution speed at runtime |
4130 | | * - trying to get a total order on the element nodes generated |
4131 | | * by the interleaves, order the list of interleave definitions |
4132 | | * following that order. |
4133 | | * - if `<text/>` is used to handle mixed content, it is better to |
4134 | | * flag this in the define and simplify the runtime checking |
4135 | | * algorithm |
4136 | | * |
4137 | | * @param payload the interleave definition |
4138 | | * @param data a Relax-NG parser context |
4139 | | * @param name the definition name |
4140 | | */ |
4141 | | static void |
4142 | | xmlRelaxNGComputeInterleaves(void *payload, void *data, |
4143 | | const xmlChar * name ATTRIBUTE_UNUSED) |
4144 | 0 | { |
4145 | 0 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
4146 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
4147 | 0 | xmlRelaxNGDefinePtr cur, *tmp; |
4148 | |
|
4149 | 0 | xmlRelaxNGPartitionPtr partitions = NULL; |
4150 | 0 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
4151 | 0 | xmlRelaxNGInterleaveGroupPtr group; |
4152 | 0 | int i, j, ret, res; |
4153 | 0 | int nbgroups = 0; |
4154 | 0 | int nbchild = 0; |
4155 | 0 | int is_mixed = 0; |
4156 | 0 | int is_determinist = 1; |
4157 | | |
4158 | | /* |
4159 | | * Don't run that check in case of error. Infinite recursion |
4160 | | * becomes possible. |
4161 | | */ |
4162 | 0 | if (ctxt->nbErrors != 0) |
4163 | 0 | return; |
4164 | | |
4165 | 0 | cur = def->content; |
4166 | 0 | while (cur != NULL) { |
4167 | 0 | nbchild++; |
4168 | 0 | cur = cur->next; |
4169 | 0 | } |
4170 | |
|
4171 | 0 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
4172 | 0 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
4173 | 0 | if (groups == NULL) |
4174 | 0 | goto error; |
4175 | 0 | cur = def->content; |
4176 | 0 | while (cur != NULL) { |
4177 | 0 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
4178 | 0 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
4179 | 0 | if (groups[nbgroups] == NULL) |
4180 | 0 | goto error; |
4181 | 0 | if (cur->type == XML_RELAXNG_TEXT) |
4182 | 0 | is_mixed++; |
4183 | 0 | groups[nbgroups]->rule = cur; |
4184 | 0 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 2); |
4185 | 0 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
4186 | 0 | nbgroups++; |
4187 | 0 | cur = cur->next; |
4188 | 0 | } |
4189 | | |
4190 | | /* |
4191 | | * Let's check that all rules makes a partitions according to 7.4 |
4192 | | */ |
4193 | 0 | partitions = (xmlRelaxNGPartitionPtr) |
4194 | 0 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
4195 | 0 | if (partitions == NULL) |
4196 | 0 | goto error; |
4197 | 0 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
4198 | 0 | partitions->nbgroups = nbgroups; |
4199 | 0 | partitions->triage = xmlHashCreate(nbgroups); |
4200 | 0 | for (i = 0; i < nbgroups; i++) { |
4201 | 0 | group = groups[i]; |
4202 | 0 | for (j = i + 1; j < nbgroups; j++) { |
4203 | 0 | if (groups[j] == NULL) |
4204 | 0 | continue; |
4205 | | |
4206 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
4207 | 0 | groups[j]->defs); |
4208 | 0 | if (ret == 0) { |
4209 | 0 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
4210 | 0 | "Element or text conflicts in interleave\n", |
4211 | 0 | NULL, NULL); |
4212 | 0 | } |
4213 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
4214 | 0 | groups[j]->attrs); |
4215 | 0 | if (ret == 0) { |
4216 | 0 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
4217 | 0 | "Attributes conflicts in interleave\n", NULL, |
4218 | 0 | NULL); |
4219 | 0 | } |
4220 | 0 | } |
4221 | 0 | tmp = group->defs; |
4222 | 0 | if ((tmp != NULL) && (*tmp != NULL)) { |
4223 | 0 | while (*tmp != NULL) { |
4224 | 0 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
4225 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4226 | 0 | BAD_CAST "#text", NULL, |
4227 | 0 | XML_INT_TO_PTR(i + 1)); |
4228 | 0 | if (res != 0) |
4229 | 0 | is_determinist = -1; |
4230 | 0 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
4231 | 0 | ((*tmp)->name != NULL)) { |
4232 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4233 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4234 | 0 | (*tmp)->name, NULL, |
4235 | 0 | XML_INT_TO_PTR(i + 1)); |
4236 | 0 | else |
4237 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4238 | 0 | (*tmp)->name, (*tmp)->ns, |
4239 | 0 | XML_INT_TO_PTR(i + 1)); |
4240 | 0 | if (res != 0) |
4241 | 0 | is_determinist = -1; |
4242 | 0 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
4243 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4244 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4245 | 0 | BAD_CAST "#any", NULL, |
4246 | 0 | XML_INT_TO_PTR(i + 1)); |
4247 | 0 | else |
4248 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4249 | 0 | BAD_CAST "#any", (*tmp)->ns, |
4250 | 0 | XML_INT_TO_PTR(i + 1)); |
4251 | 0 | if ((*tmp)->nameClass != NULL) |
4252 | 0 | is_determinist = 2; |
4253 | 0 | if (res != 0) |
4254 | 0 | is_determinist = -1; |
4255 | 0 | } else { |
4256 | 0 | is_determinist = -1; |
4257 | 0 | } |
4258 | 0 | tmp++; |
4259 | 0 | } |
4260 | 0 | } else { |
4261 | 0 | is_determinist = 0; |
4262 | 0 | } |
4263 | 0 | } |
4264 | 0 | partitions->groups = groups; |
4265 | | |
4266 | | /* |
4267 | | * and save the partition list back in the def |
4268 | | */ |
4269 | 0 | def->data = partitions; |
4270 | 0 | if (is_mixed != 0) |
4271 | 0 | def->dflags |= IS_MIXED; |
4272 | 0 | if (is_determinist == 1) |
4273 | 0 | partitions->flags = IS_DETERMINIST; |
4274 | 0 | if (is_determinist == 2) |
4275 | 0 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
4276 | 0 | return; |
4277 | | |
4278 | 0 | error: |
4279 | 0 | xmlRngPErrMemory(ctxt); |
4280 | 0 | if (groups != NULL) { |
4281 | 0 | for (i = 0; i < nbgroups; i++) |
4282 | 0 | if (groups[i] != NULL) { |
4283 | 0 | if (groups[i]->defs != NULL) |
4284 | 0 | xmlFree(groups[i]->defs); |
4285 | 0 | xmlFree(groups[i]); |
4286 | 0 | } |
4287 | 0 | xmlFree(groups); |
4288 | 0 | } |
4289 | 0 | xmlRelaxNGFreePartition(partitions); |
4290 | 0 | } |
4291 | | |
4292 | | /** |
4293 | | * parse the content of a RelaxNG interleave node. |
4294 | | * |
4295 | | * @param ctxt a Relax-NG parser context |
4296 | | * @param node the data node. |
4297 | | * @returns the definition pointer or NULL in case of error |
4298 | | */ |
4299 | | static xmlRelaxNGDefinePtr |
4300 | | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4301 | 0 | { |
4302 | 0 | xmlRelaxNGDefinePtr def = NULL; |
4303 | 0 | xmlRelaxNGDefinePtr last = NULL, cur; |
4304 | 0 | xmlNodePtr child; |
4305 | |
|
4306 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4307 | 0 | if (def == NULL) { |
4308 | 0 | return (NULL); |
4309 | 0 | } |
4310 | 0 | def->type = XML_RELAXNG_INTERLEAVE; |
4311 | |
|
4312 | 0 | if (ctxt->interleaves == NULL) |
4313 | 0 | ctxt->interleaves = xmlHashCreate(10); |
4314 | 0 | if (ctxt->interleaves == NULL) { |
4315 | 0 | xmlRngPErrMemory(ctxt); |
4316 | 0 | } else { |
4317 | 0 | char name[32]; |
4318 | |
|
4319 | 0 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
4320 | 0 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
4321 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
4322 | 0 | "Failed to add %s to hash table\n", |
4323 | 0 | (const xmlChar *) name, NULL); |
4324 | 0 | } |
4325 | 0 | } |
4326 | 0 | child = node->children; |
4327 | 0 | if (child == NULL) { |
4328 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
4329 | 0 | "Element interleave is empty\n", NULL, NULL); |
4330 | 0 | } |
4331 | 0 | while (child != NULL) { |
4332 | 0 | if (IS_RELAXNG(child, "element")) { |
4333 | 0 | cur = xmlRelaxNGParseElement(ctxt, child); |
4334 | 0 | } else { |
4335 | 0 | cur = xmlRelaxNGParsePattern(ctxt, child); |
4336 | 0 | } |
4337 | 0 | if (cur != NULL) { |
4338 | 0 | cur->parent = def; |
4339 | 0 | if (last == NULL) { |
4340 | 0 | def->content = last = cur; |
4341 | 0 | } else { |
4342 | 0 | last->next = cur; |
4343 | 0 | last = cur; |
4344 | 0 | } |
4345 | 0 | } |
4346 | 0 | child = child->next; |
4347 | 0 | } |
4348 | |
|
4349 | 0 | return (def); |
4350 | 0 | } |
4351 | | |
4352 | | /** |
4353 | | * Integrate the content of an include node in the current grammar |
4354 | | * |
4355 | | * @param ctxt a Relax-NG parser context |
4356 | | * @param node the include node |
4357 | | * @returns 0 in case of success or -1 in case of error |
4358 | | */ |
4359 | | static int |
4360 | | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4361 | 0 | { |
4362 | 0 | xmlRelaxNGIncludePtr incl; |
4363 | 0 | xmlNodePtr root; |
4364 | 0 | int ret = 0, tmp; |
4365 | |
|
4366 | 0 | incl = node->psvi; |
4367 | 0 | if (incl == NULL) { |
4368 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
4369 | 0 | "Include node has no data\n", NULL, NULL); |
4370 | 0 | return (-1); |
4371 | 0 | } |
4372 | 0 | root = xmlDocGetRootElement(incl->doc); |
4373 | 0 | if (root == NULL) { |
4374 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
4375 | 0 | NULL, NULL); |
4376 | 0 | return (-1); |
4377 | 0 | } |
4378 | 0 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
4379 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
4380 | 0 | "Include document root is not a grammar\n", NULL, NULL); |
4381 | 0 | return (-1); |
4382 | 0 | } |
4383 | | |
4384 | | /* |
4385 | | * Merge the definition from both the include and the internal list |
4386 | | */ |
4387 | 0 | if (root->children != NULL) { |
4388 | 0 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
4389 | 0 | if (tmp != 0) |
4390 | 0 | ret = -1; |
4391 | 0 | } |
4392 | 0 | if (node->children != NULL) { |
4393 | 0 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
4394 | 0 | if (tmp != 0) |
4395 | 0 | ret = -1; |
4396 | 0 | } |
4397 | 0 | return (ret); |
4398 | 0 | } |
4399 | | |
4400 | | /** |
4401 | | * parse the content of a RelaxNG define element node. |
4402 | | * |
4403 | | * @param ctxt a Relax-NG parser context |
4404 | | * @param node the define node |
4405 | | * @returns 0 in case of success or -1 in case of error |
4406 | | */ |
4407 | | static int |
4408 | | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4409 | 0 | { |
4410 | 0 | xmlChar *name; |
4411 | 0 | int ret = 0, tmp; |
4412 | 0 | xmlRelaxNGDefinePtr def; |
4413 | 0 | const xmlChar *olddefine; |
4414 | |
|
4415 | 0 | name = xmlGetProp(node, BAD_CAST "name"); |
4416 | 0 | if (name == NULL) { |
4417 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
4418 | 0 | "define has no name\n", NULL, NULL); |
4419 | 0 | } else { |
4420 | 0 | xmlRelaxNGNormExtSpace(name); |
4421 | 0 | if (xmlValidateNCName(name, 0)) { |
4422 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
4423 | 0 | "define name '%s' is not an NCName\n", name, NULL); |
4424 | 0 | } |
4425 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4426 | 0 | if (def == NULL) { |
4427 | 0 | xmlFree(name); |
4428 | 0 | return (-1); |
4429 | 0 | } |
4430 | 0 | def->type = XML_RELAXNG_DEF; |
4431 | 0 | def->name = name; |
4432 | 0 | if (node->children == NULL) { |
4433 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
4434 | 0 | "define has no children\n", NULL, NULL); |
4435 | 0 | } else { |
4436 | 0 | olddefine = ctxt->define; |
4437 | 0 | ctxt->define = name; |
4438 | 0 | def->content = |
4439 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4440 | 0 | ctxt->define = olddefine; |
4441 | 0 | } |
4442 | 0 | if (ctxt->grammar->defs == NULL) |
4443 | 0 | ctxt->grammar->defs = xmlHashCreate(10); |
4444 | 0 | if (ctxt->grammar->defs == NULL) { |
4445 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
4446 | 0 | "Could not create definition hash\n", NULL, NULL); |
4447 | 0 | ret = -1; |
4448 | 0 | } else { |
4449 | 0 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
4450 | 0 | if (tmp < 0) { |
4451 | 0 | xmlRelaxNGDefinePtr prev; |
4452 | |
|
4453 | 0 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
4454 | 0 | if (prev == NULL) { |
4455 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
4456 | 0 | "Internal error on define aggregation of %s\n", |
4457 | 0 | name, NULL); |
4458 | 0 | ret = -1; |
4459 | 0 | } else { |
4460 | 0 | while (prev->nextHash != NULL) |
4461 | 0 | prev = prev->nextHash; |
4462 | 0 | prev->nextHash = def; |
4463 | 0 | } |
4464 | 0 | } |
4465 | 0 | } |
4466 | 0 | } |
4467 | 0 | return (ret); |
4468 | 0 | } |
4469 | | |
4470 | | /** |
4471 | | * Import import one references into the current grammar |
4472 | | * |
4473 | | * @param payload the parser context |
4474 | | * @param data the current grammar |
4475 | | * @param name the reference name |
4476 | | */ |
4477 | | static void |
4478 | 0 | xmlRelaxNGParseImportRef(void *payload, void *data, const xmlChar *name) { |
4479 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
4480 | 0 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
4481 | 0 | int tmp; |
4482 | |
|
4483 | 0 | def->dflags |= IS_EXTERNAL_REF; |
4484 | |
|
4485 | 0 | tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def); |
4486 | 0 | if (tmp < 0) { |
4487 | 0 | xmlRelaxNGDefinePtr prev; |
4488 | |
|
4489 | 0 | prev = (xmlRelaxNGDefinePtr) |
4490 | 0 | xmlHashLookup(ctxt->grammar->refs, def->name); |
4491 | 0 | if (prev == NULL) { |
4492 | 0 | if (def->name != NULL) { |
4493 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
4494 | 0 | "Error refs definitions '%s'\n", |
4495 | 0 | def->name, NULL); |
4496 | 0 | } else { |
4497 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
4498 | 0 | "Error refs definitions\n", |
4499 | 0 | NULL, NULL); |
4500 | 0 | } |
4501 | 0 | } else { |
4502 | 0 | def->nextHash = prev->nextHash; |
4503 | 0 | prev->nextHash = def; |
4504 | 0 | } |
4505 | 0 | } |
4506 | 0 | } |
4507 | | |
4508 | | /** |
4509 | | * Import references from the subgrammar into the current grammar |
4510 | | * |
4511 | | * @param ctxt the parser context |
4512 | | * @param grammar the sub grammar |
4513 | | * @returns 0 in case of success, -1 in case of failure |
4514 | | */ |
4515 | | static int |
4516 | | xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt, |
4517 | 0 | xmlRelaxNGGrammarPtr grammar) { |
4518 | 0 | if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL)) |
4519 | 0 | return(-1); |
4520 | 0 | if (grammar->refs == NULL) |
4521 | 0 | return(0); |
4522 | 0 | if (ctxt->grammar->refs == NULL) |
4523 | 0 | ctxt->grammar->refs = xmlHashCreate(10); |
4524 | 0 | if (ctxt->grammar->refs == NULL) { |
4525 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
4526 | 0 | "Could not create references hash\n", NULL, NULL); |
4527 | 0 | return(-1); |
4528 | 0 | } |
4529 | 0 | xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt); |
4530 | 0 | return(0); |
4531 | 0 | } |
4532 | | |
4533 | | /** |
4534 | | * Process and compile an externalRef node |
4535 | | * |
4536 | | * @param ctxt the parser context |
4537 | | * @param node the externalRef node |
4538 | | * @returns the xmlRelaxNGDefine or NULL in case of error |
4539 | | */ |
4540 | | static xmlRelaxNGDefinePtr |
4541 | | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4542 | 0 | { |
4543 | 0 | xmlRelaxNGDocumentPtr docu; |
4544 | 0 | xmlNodePtr root, tmp; |
4545 | 0 | xmlChar *ns; |
4546 | 0 | int newNs = 0, oldflags; |
4547 | 0 | xmlRelaxNGDefinePtr def; |
4548 | |
|
4549 | 0 | docu = node->psvi; |
4550 | 0 | if (docu != NULL) { |
4551 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4552 | 0 | if (def == NULL) |
4553 | 0 | return (NULL); |
4554 | 0 | def->type = XML_RELAXNG_EXTERNALREF; |
4555 | |
|
4556 | 0 | if (docu->content == NULL) { |
4557 | | /* |
4558 | | * Then do the parsing for good |
4559 | | */ |
4560 | 0 | root = xmlDocGetRootElement(docu->doc); |
4561 | 0 | if (root == NULL) { |
4562 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
4563 | 0 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
4564 | 0 | NULL); |
4565 | 0 | return (NULL); |
4566 | 0 | } |
4567 | | /* |
4568 | | * ns transmission rules |
4569 | | */ |
4570 | 0 | ns = xmlGetProp(root, BAD_CAST "ns"); |
4571 | 0 | if (ns == NULL) { |
4572 | 0 | tmp = node; |
4573 | 0 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
4574 | 0 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
4575 | 0 | if (ns != NULL) { |
4576 | 0 | break; |
4577 | 0 | } |
4578 | 0 | tmp = tmp->parent; |
4579 | 0 | } |
4580 | 0 | if (ns != NULL) { |
4581 | 0 | xmlSetProp(root, BAD_CAST "ns", ns); |
4582 | 0 | newNs = 1; |
4583 | 0 | xmlFree(ns); |
4584 | 0 | } |
4585 | 0 | } else { |
4586 | 0 | xmlFree(ns); |
4587 | 0 | } |
4588 | | |
4589 | | /* |
4590 | | * Parsing to get a precompiled schemas. |
4591 | | */ |
4592 | 0 | oldflags = ctxt->flags; |
4593 | 0 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
4594 | 0 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
4595 | 0 | ctxt->flags = oldflags; |
4596 | 0 | if ((docu->schema != NULL) && |
4597 | 0 | (docu->schema->topgrammar != NULL)) { |
4598 | 0 | docu->content = docu->schema->topgrammar->start; |
4599 | 0 | if (docu->schema->topgrammar->refs) |
4600 | 0 | xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar); |
4601 | 0 | } |
4602 | | |
4603 | | /* |
4604 | | * the externalRef may be reused in a different ns context |
4605 | | */ |
4606 | 0 | if (newNs == 1) { |
4607 | 0 | xmlUnsetProp(root, BAD_CAST "ns"); |
4608 | 0 | } |
4609 | 0 | } |
4610 | 0 | def->content = docu->content; |
4611 | 0 | } else { |
4612 | 0 | def = NULL; |
4613 | 0 | } |
4614 | 0 | return (def); |
4615 | 0 | } |
4616 | | |
4617 | | /** |
4618 | | * parse the content of a RelaxNG pattern node. |
4619 | | * |
4620 | | * @param ctxt a Relax-NG parser context |
4621 | | * @param node the pattern node. |
4622 | | * @returns the definition pointer or NULL in case of error or if no |
4623 | | * pattern is generated. |
4624 | | */ |
4625 | | static xmlRelaxNGDefinePtr |
4626 | | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4627 | 0 | { |
4628 | 0 | xmlRelaxNGDefinePtr def = NULL; |
4629 | |
|
4630 | 0 | if (node == NULL) { |
4631 | 0 | return (NULL); |
4632 | 0 | } |
4633 | 0 | if (IS_RELAXNG(node, "element")) { |
4634 | 0 | def = xmlRelaxNGParseElement(ctxt, node); |
4635 | 0 | } else if (IS_RELAXNG(node, "attribute")) { |
4636 | 0 | def = xmlRelaxNGParseAttribute(ctxt, node); |
4637 | 0 | } else if (IS_RELAXNG(node, "empty")) { |
4638 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4639 | 0 | if (def == NULL) |
4640 | 0 | return (NULL); |
4641 | 0 | def->type = XML_RELAXNG_EMPTY; |
4642 | 0 | if (node->children != NULL) { |
4643 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
4644 | 0 | "empty: had a child node\n", NULL, NULL); |
4645 | 0 | } |
4646 | 0 | } else if (IS_RELAXNG(node, "text")) { |
4647 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4648 | 0 | if (def == NULL) |
4649 | 0 | return (NULL); |
4650 | 0 | def->type = XML_RELAXNG_TEXT; |
4651 | 0 | if (node->children != NULL) { |
4652 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
4653 | 0 | "text: had a child node\n", NULL, NULL); |
4654 | 0 | } |
4655 | 0 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
4656 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4657 | 0 | if (def == NULL) |
4658 | 0 | return (NULL); |
4659 | 0 | def->type = XML_RELAXNG_ZEROORMORE; |
4660 | 0 | if (node->children == NULL) { |
4661 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4662 | 0 | "Element %s is empty\n", node->name, NULL); |
4663 | 0 | } else { |
4664 | 0 | def->content = |
4665 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
4666 | 0 | } |
4667 | 0 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
4668 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4669 | 0 | if (def == NULL) |
4670 | 0 | return (NULL); |
4671 | 0 | def->type = XML_RELAXNG_ONEORMORE; |
4672 | 0 | if (node->children == NULL) { |
4673 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4674 | 0 | "Element %s is empty\n", node->name, NULL); |
4675 | 0 | } else { |
4676 | 0 | def->content = |
4677 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
4678 | 0 | } |
4679 | 0 | } else if (IS_RELAXNG(node, "optional")) { |
4680 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4681 | 0 | if (def == NULL) |
4682 | 0 | return (NULL); |
4683 | 0 | def->type = XML_RELAXNG_OPTIONAL; |
4684 | 0 | if (node->children == NULL) { |
4685 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4686 | 0 | "Element %s is empty\n", node->name, NULL); |
4687 | 0 | } else { |
4688 | 0 | def->content = |
4689 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
4690 | 0 | } |
4691 | 0 | } else if (IS_RELAXNG(node, "choice")) { |
4692 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4693 | 0 | if (def == NULL) |
4694 | 0 | return (NULL); |
4695 | 0 | def->type = XML_RELAXNG_CHOICE; |
4696 | 0 | if (node->children == NULL) { |
4697 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4698 | 0 | "Element %s is empty\n", node->name, NULL); |
4699 | 0 | } else { |
4700 | 0 | ctxt->def = def; |
4701 | 0 | def->content = |
4702 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4703 | 0 | } |
4704 | 0 | } else if (IS_RELAXNG(node, "group")) { |
4705 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4706 | 0 | if (def == NULL) |
4707 | 0 | return (NULL); |
4708 | 0 | def->type = XML_RELAXNG_GROUP; |
4709 | 0 | if (node->children == NULL) { |
4710 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4711 | 0 | "Element %s is empty\n", node->name, NULL); |
4712 | 0 | } else { |
4713 | 0 | def->content = |
4714 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4715 | 0 | } |
4716 | 0 | } else if (IS_RELAXNG(node, "ref")) { |
4717 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4718 | 0 | if (def == NULL) |
4719 | 0 | return (NULL); |
4720 | 0 | def->type = XML_RELAXNG_REF; |
4721 | 0 | def->name = xmlGetProp(node, BAD_CAST "name"); |
4722 | 0 | if (def->name == NULL) { |
4723 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
4724 | 0 | NULL, NULL); |
4725 | 0 | } else { |
4726 | 0 | xmlRelaxNGNormExtSpace(def->name); |
4727 | 0 | if (xmlValidateNCName(def->name, 0)) { |
4728 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
4729 | 0 | "ref name '%s' is not an NCName\n", def->name, |
4730 | 0 | NULL); |
4731 | 0 | } |
4732 | 0 | } |
4733 | 0 | if (node->children != NULL) { |
4734 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
4735 | 0 | NULL, NULL); |
4736 | 0 | } |
4737 | 0 | if (ctxt->grammar->refs == NULL) |
4738 | 0 | ctxt->grammar->refs = xmlHashCreate(10); |
4739 | 0 | if (ctxt->grammar->refs == NULL) { |
4740 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
4741 | 0 | "Could not create references hash\n", NULL, NULL); |
4742 | 0 | def = NULL; |
4743 | 0 | } else { |
4744 | 0 | int tmp; |
4745 | |
|
4746 | 0 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
4747 | 0 | if (tmp < 0) { |
4748 | 0 | xmlRelaxNGDefinePtr prev; |
4749 | |
|
4750 | 0 | prev = (xmlRelaxNGDefinePtr) |
4751 | 0 | xmlHashLookup(ctxt->grammar->refs, def->name); |
4752 | 0 | if (prev == NULL) { |
4753 | 0 | if (def->name != NULL) { |
4754 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
4755 | 0 | "Error refs definitions '%s'\n", |
4756 | 0 | def->name, NULL); |
4757 | 0 | } else { |
4758 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
4759 | 0 | "Error refs definitions\n", |
4760 | 0 | NULL, NULL); |
4761 | 0 | } |
4762 | 0 | def = NULL; |
4763 | 0 | } else { |
4764 | 0 | def->nextHash = prev->nextHash; |
4765 | 0 | prev->nextHash = def; |
4766 | 0 | } |
4767 | 0 | } |
4768 | 0 | } |
4769 | 0 | } else if (IS_RELAXNG(node, "data")) { |
4770 | 0 | def = xmlRelaxNGParseData(ctxt, node); |
4771 | 0 | } else if (IS_RELAXNG(node, "value")) { |
4772 | 0 | def = xmlRelaxNGParseValue(ctxt, node); |
4773 | 0 | } else if (IS_RELAXNG(node, "list")) { |
4774 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4775 | 0 | if (def == NULL) |
4776 | 0 | return (NULL); |
4777 | 0 | def->type = XML_RELAXNG_LIST; |
4778 | 0 | if (node->children == NULL) { |
4779 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4780 | 0 | "Element %s is empty\n", node->name, NULL); |
4781 | 0 | } else { |
4782 | 0 | def->content = |
4783 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4784 | 0 | } |
4785 | 0 | } else if (IS_RELAXNG(node, "interleave")) { |
4786 | 0 | def = xmlRelaxNGParseInterleave(ctxt, node); |
4787 | 0 | } else if (IS_RELAXNG(node, "externalRef")) { |
4788 | 0 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
4789 | 0 | } else if (IS_RELAXNG(node, "notAllowed")) { |
4790 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4791 | 0 | if (def == NULL) |
4792 | 0 | return (NULL); |
4793 | 0 | def->type = XML_RELAXNG_NOT_ALLOWED; |
4794 | 0 | if (node->children != NULL) { |
4795 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
4796 | 0 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
4797 | 0 | NULL, NULL); |
4798 | 0 | } |
4799 | 0 | } else if (IS_RELAXNG(node, "grammar")) { |
4800 | 0 | xmlRelaxNGGrammarPtr grammar, old; |
4801 | 0 | xmlRelaxNGGrammarPtr oldparent; |
4802 | |
|
4803 | 0 | oldparent = ctxt->parentgrammar; |
4804 | 0 | old = ctxt->grammar; |
4805 | 0 | ctxt->parentgrammar = old; |
4806 | 0 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
4807 | 0 | if (old != NULL) { |
4808 | 0 | ctxt->grammar = old; |
4809 | 0 | ctxt->parentgrammar = oldparent; |
4810 | | #if 0 |
4811 | | if (grammar != NULL) { |
4812 | | grammar->next = old->next; |
4813 | | old->next = grammar; |
4814 | | } |
4815 | | #endif |
4816 | 0 | } |
4817 | 0 | if (grammar != NULL) |
4818 | 0 | def = grammar->start; |
4819 | 0 | else |
4820 | 0 | def = NULL; |
4821 | 0 | } else if (IS_RELAXNG(node, "parentRef")) { |
4822 | 0 | if (ctxt->parentgrammar == NULL) { |
4823 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
4824 | 0 | "Use of parentRef without a parent grammar\n", NULL, |
4825 | 0 | NULL); |
4826 | 0 | return (NULL); |
4827 | 0 | } |
4828 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4829 | 0 | if (def == NULL) |
4830 | 0 | return (NULL); |
4831 | 0 | def->type = XML_RELAXNG_PARENTREF; |
4832 | 0 | def->name = xmlGetProp(node, BAD_CAST "name"); |
4833 | 0 | if (def->name == NULL) { |
4834 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
4835 | 0 | "parentRef has no name\n", NULL, NULL); |
4836 | 0 | } else { |
4837 | 0 | xmlRelaxNGNormExtSpace(def->name); |
4838 | 0 | if (xmlValidateNCName(def->name, 0)) { |
4839 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
4840 | 0 | "parentRef name '%s' is not an NCName\n", |
4841 | 0 | def->name, NULL); |
4842 | 0 | } |
4843 | 0 | } |
4844 | 0 | if (node->children != NULL) { |
4845 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
4846 | 0 | "parentRef is not empty\n", NULL, NULL); |
4847 | 0 | } |
4848 | 0 | if (ctxt->parentgrammar->refs == NULL) |
4849 | 0 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
4850 | 0 | if (ctxt->parentgrammar->refs == NULL) { |
4851 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
4852 | 0 | "Could not create references hash\n", NULL, NULL); |
4853 | 0 | def = NULL; |
4854 | 0 | } else if (def->name != NULL) { |
4855 | 0 | int tmp; |
4856 | |
|
4857 | 0 | tmp = |
4858 | 0 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
4859 | 0 | if (tmp < 0) { |
4860 | 0 | xmlRelaxNGDefinePtr prev; |
4861 | |
|
4862 | 0 | prev = (xmlRelaxNGDefinePtr) |
4863 | 0 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
4864 | 0 | if (prev == NULL) { |
4865 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
4866 | 0 | "Internal error parentRef definitions '%s'\n", |
4867 | 0 | def->name, NULL); |
4868 | 0 | def = NULL; |
4869 | 0 | } else { |
4870 | 0 | def->nextHash = prev->nextHash; |
4871 | 0 | prev->nextHash = def; |
4872 | 0 | } |
4873 | 0 | } |
4874 | 0 | } |
4875 | 0 | } else if (IS_RELAXNG(node, "mixed")) { |
4876 | 0 | if (node->children == NULL) { |
4877 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
4878 | 0 | NULL, NULL); |
4879 | 0 | def = NULL; |
4880 | 0 | } else { |
4881 | 0 | def = xmlRelaxNGParseInterleave(ctxt, node); |
4882 | 0 | if (def != NULL) { |
4883 | 0 | xmlRelaxNGDefinePtr tmp; |
4884 | |
|
4885 | 0 | if ((def->content != NULL) && (def->content->next != NULL)) { |
4886 | 0 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
4887 | 0 | if (tmp != NULL) { |
4888 | 0 | tmp->type = XML_RELAXNG_GROUP; |
4889 | 0 | tmp->content = def->content; |
4890 | 0 | def->content = tmp; |
4891 | 0 | } |
4892 | 0 | } |
4893 | |
|
4894 | 0 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
4895 | 0 | if (tmp == NULL) |
4896 | 0 | return (def); |
4897 | 0 | tmp->type = XML_RELAXNG_TEXT; |
4898 | 0 | tmp->next = def->content; |
4899 | 0 | def->content = tmp; |
4900 | 0 | } |
4901 | 0 | } |
4902 | 0 | } else { |
4903 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
4904 | 0 | "Unexpected node %s is not a pattern\n", node->name, |
4905 | 0 | NULL); |
4906 | 0 | def = NULL; |
4907 | 0 | } |
4908 | 0 | return (def); |
4909 | 0 | } |
4910 | | |
4911 | | /** |
4912 | | * parse the content of a RelaxNG attribute node. |
4913 | | * |
4914 | | * @param ctxt a Relax-NG parser context |
4915 | | * @param node the element node |
4916 | | * @returns the definition pointer or NULL in case of error. |
4917 | | */ |
4918 | | static xmlRelaxNGDefinePtr |
4919 | | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4920 | 0 | { |
4921 | 0 | xmlRelaxNGDefinePtr ret, cur; |
4922 | 0 | xmlNodePtr child; |
4923 | 0 | int old_flags; |
4924 | |
|
4925 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
4926 | 0 | if (ret == NULL) |
4927 | 0 | return (NULL); |
4928 | 0 | ret->type = XML_RELAXNG_ATTRIBUTE; |
4929 | 0 | ret->parent = ctxt->def; |
4930 | 0 | child = node->children; |
4931 | 0 | if (child == NULL) { |
4932 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
4933 | 0 | "xmlRelaxNGParseattribute: attribute has no children\n", |
4934 | 0 | NULL, NULL); |
4935 | 0 | return (ret); |
4936 | 0 | } |
4937 | 0 | old_flags = ctxt->flags; |
4938 | 0 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
4939 | 0 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
4940 | 0 | if (cur != NULL) |
4941 | 0 | child = child->next; |
4942 | |
|
4943 | 0 | if (child != NULL) { |
4944 | 0 | cur = xmlRelaxNGParsePattern(ctxt, child); |
4945 | 0 | if (cur != NULL) { |
4946 | 0 | switch (cur->type) { |
4947 | 0 | case XML_RELAXNG_EMPTY: |
4948 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
4949 | 0 | case XML_RELAXNG_TEXT: |
4950 | 0 | case XML_RELAXNG_ELEMENT: |
4951 | 0 | case XML_RELAXNG_DATATYPE: |
4952 | 0 | case XML_RELAXNG_VALUE: |
4953 | 0 | case XML_RELAXNG_LIST: |
4954 | 0 | case XML_RELAXNG_REF: |
4955 | 0 | case XML_RELAXNG_PARENTREF: |
4956 | 0 | case XML_RELAXNG_EXTERNALREF: |
4957 | 0 | case XML_RELAXNG_DEF: |
4958 | 0 | case XML_RELAXNG_ONEORMORE: |
4959 | 0 | case XML_RELAXNG_ZEROORMORE: |
4960 | 0 | case XML_RELAXNG_OPTIONAL: |
4961 | 0 | case XML_RELAXNG_CHOICE: |
4962 | 0 | case XML_RELAXNG_GROUP: |
4963 | 0 | case XML_RELAXNG_INTERLEAVE: |
4964 | 0 | case XML_RELAXNG_ATTRIBUTE: |
4965 | 0 | ret->content = cur; |
4966 | 0 | cur->parent = ret; |
4967 | 0 | break; |
4968 | 0 | case XML_RELAXNG_START: |
4969 | 0 | case XML_RELAXNG_PARAM: |
4970 | 0 | case XML_RELAXNG_EXCEPT: |
4971 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
4972 | 0 | "attribute has invalid content\n", NULL, |
4973 | 0 | NULL); |
4974 | 0 | break; |
4975 | 0 | case XML_RELAXNG_NOOP: |
4976 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
4977 | 0 | "RNG Internal error, noop found in attribute\n", |
4978 | 0 | NULL, NULL); |
4979 | 0 | break; |
4980 | 0 | } |
4981 | 0 | } |
4982 | 0 | child = child->next; |
4983 | 0 | } |
4984 | 0 | if (child != NULL) { |
4985 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
4986 | 0 | "attribute has multiple children\n", NULL, NULL); |
4987 | 0 | } |
4988 | 0 | ctxt->flags = old_flags; |
4989 | 0 | return (ret); |
4990 | 0 | } |
4991 | | |
4992 | | /** |
4993 | | * parse the content of a RelaxNG nameClass node. |
4994 | | * |
4995 | | * @param ctxt a Relax-NG parser context |
4996 | | * @param node the except node |
4997 | | * @param attr 1 if within an attribute, 0 if within an element |
4998 | | * @returns the definition pointer or NULL in case of error. |
4999 | | */ |
5000 | | static xmlRelaxNGDefinePtr |
5001 | | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
5002 | | xmlNodePtr node, int attr) |
5003 | 0 | { |
5004 | 0 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
5005 | 0 | xmlNodePtr child; |
5006 | |
|
5007 | 0 | if (!IS_RELAXNG(node, "except")) { |
5008 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
5009 | 0 | "Expecting an except node\n", NULL, NULL); |
5010 | 0 | return (NULL); |
5011 | 0 | } |
5012 | 0 | if (node->next != NULL) { |
5013 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
5014 | 0 | "exceptNameClass allows only a single except node\n", |
5015 | 0 | NULL, NULL); |
5016 | 0 | } |
5017 | 0 | if (node->children == NULL) { |
5018 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
5019 | 0 | NULL, NULL); |
5020 | 0 | return (NULL); |
5021 | 0 | } |
5022 | | |
5023 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5024 | 0 | if (ret == NULL) |
5025 | 0 | return (NULL); |
5026 | 0 | ret->type = XML_RELAXNG_EXCEPT; |
5027 | 0 | child = node->children; |
5028 | 0 | while (child != NULL) { |
5029 | 0 | cur = xmlRelaxNGNewDefine(ctxt, child); |
5030 | 0 | if (cur == NULL) |
5031 | 0 | break; |
5032 | 0 | if (attr) |
5033 | 0 | cur->type = XML_RELAXNG_ATTRIBUTE; |
5034 | 0 | else |
5035 | 0 | cur->type = XML_RELAXNG_ELEMENT; |
5036 | |
|
5037 | 0 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
5038 | 0 | if (last == NULL) { |
5039 | 0 | ret->content = cur; |
5040 | 0 | } else { |
5041 | 0 | last->next = cur; |
5042 | 0 | } |
5043 | 0 | last = cur; |
5044 | 0 | } |
5045 | 0 | child = child->next; |
5046 | 0 | } |
5047 | |
|
5048 | 0 | return (ret); |
5049 | 0 | } |
5050 | | |
5051 | | /** |
5052 | | * parse the content of a RelaxNG nameClass node. |
5053 | | * |
5054 | | * @param ctxt a Relax-NG parser context |
5055 | | * @param node the nameClass node |
5056 | | * @param def the current definition |
5057 | | * @returns the definition pointer or NULL in case of error. |
5058 | | */ |
5059 | | static xmlRelaxNGDefinePtr |
5060 | | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
5061 | | xmlRelaxNGDefinePtr def) |
5062 | 0 | { |
5063 | 0 | xmlRelaxNGDefinePtr ret, tmp; |
5064 | 0 | xmlChar *val; |
5065 | |
|
5066 | 0 | ret = def; |
5067 | 0 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
5068 | 0 | (IS_RELAXNG(node, "nsName"))) { |
5069 | 0 | if ((def->type != XML_RELAXNG_ELEMENT) && |
5070 | 0 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
5071 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5072 | 0 | if (ret == NULL) |
5073 | 0 | return (NULL); |
5074 | 0 | ret->parent = def; |
5075 | 0 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
5076 | 0 | ret->type = XML_RELAXNG_ATTRIBUTE; |
5077 | 0 | else |
5078 | 0 | ret->type = XML_RELAXNG_ELEMENT; |
5079 | 0 | } |
5080 | 0 | } |
5081 | 0 | if (IS_RELAXNG(node, "name")) { |
5082 | 0 | val = xmlNodeGetContent(node); |
5083 | 0 | xmlRelaxNGNormExtSpace(val); |
5084 | 0 | if (xmlValidateNCName(val, 0)) { |
5085 | 0 | if (node->parent != NULL) |
5086 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
5087 | 0 | "Element %s name '%s' is not an NCName\n", |
5088 | 0 | node->parent->name, val); |
5089 | 0 | else |
5090 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
5091 | 0 | "name '%s' is not an NCName\n", |
5092 | 0 | val, NULL); |
5093 | 0 | } |
5094 | 0 | ret->name = val; |
5095 | 0 | val = xmlGetProp(node, BAD_CAST "ns"); |
5096 | 0 | ret->ns = val; |
5097 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
5098 | 0 | (val != NULL) && |
5099 | 0 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
5100 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
5101 | 0 | "Attribute with namespace '%s' is not allowed\n", |
5102 | 0 | val, NULL); |
5103 | 0 | } |
5104 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
5105 | 0 | (val != NULL) && |
5106 | 0 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
5107 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
5108 | 0 | "Attribute with QName 'xmlns' is not allowed\n", |
5109 | 0 | val, NULL); |
5110 | 0 | } |
5111 | 0 | } else if (IS_RELAXNG(node, "anyName")) { |
5112 | 0 | ret->name = NULL; |
5113 | 0 | ret->ns = NULL; |
5114 | 0 | if (node->children != NULL) { |
5115 | 0 | ret->nameClass = |
5116 | 0 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
5117 | 0 | (def->type == |
5118 | 0 | XML_RELAXNG_ATTRIBUTE)); |
5119 | 0 | } |
5120 | 0 | } else if (IS_RELAXNG(node, "nsName")) { |
5121 | 0 | ret->name = NULL; |
5122 | 0 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
5123 | 0 | if (ret->ns == NULL) { |
5124 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
5125 | 0 | "nsName has no ns attribute\n", NULL, NULL); |
5126 | 0 | } |
5127 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
5128 | 0 | (ret->ns != NULL) && |
5129 | 0 | (xmlStrEqual |
5130 | 0 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
5131 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
5132 | 0 | "Attribute with namespace '%s' is not allowed\n", |
5133 | 0 | ret->ns, NULL); |
5134 | 0 | } |
5135 | 0 | if (node->children != NULL) { |
5136 | 0 | ret->nameClass = |
5137 | 0 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
5138 | 0 | (def->type == |
5139 | 0 | XML_RELAXNG_ATTRIBUTE)); |
5140 | 0 | } |
5141 | 0 | } else if (IS_RELAXNG(node, "choice")) { |
5142 | 0 | xmlNodePtr child; |
5143 | 0 | xmlRelaxNGDefinePtr last = NULL; |
5144 | |
|
5145 | 0 | if (def->type == XML_RELAXNG_CHOICE) { |
5146 | 0 | ret = def; |
5147 | 0 | } else { |
5148 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5149 | 0 | if (ret == NULL) |
5150 | 0 | return (NULL); |
5151 | 0 | ret->parent = def; |
5152 | 0 | ret->type = XML_RELAXNG_CHOICE; |
5153 | 0 | } |
5154 | | |
5155 | 0 | if (node->children == NULL) { |
5156 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
5157 | 0 | "Element choice is empty\n", NULL, NULL); |
5158 | 0 | } else { |
5159 | |
|
5160 | 0 | child = node->children; |
5161 | 0 | while (child != NULL) { |
5162 | 0 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
5163 | 0 | if (tmp != NULL) { |
5164 | 0 | if (last == NULL) { |
5165 | 0 | last = tmp; |
5166 | 0 | } else if (tmp != ret) { |
5167 | 0 | last->next = tmp; |
5168 | 0 | last = tmp; |
5169 | 0 | } |
5170 | 0 | } |
5171 | 0 | child = child->next; |
5172 | 0 | } |
5173 | 0 | } |
5174 | 0 | } else { |
5175 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
5176 | 0 | "expecting name, anyName, nsName or choice : got %s\n", |
5177 | 0 | (node == NULL ? (const xmlChar *) "nothing" : node->name), |
5178 | 0 | NULL); |
5179 | 0 | return (NULL); |
5180 | 0 | } |
5181 | 0 | if (ret != def) { |
5182 | 0 | if (def->nameClass == NULL) { |
5183 | 0 | def->nameClass = ret; |
5184 | 0 | } else { |
5185 | 0 | tmp = def->nameClass; |
5186 | 0 | while (tmp->next != NULL) { |
5187 | 0 | tmp = tmp->next; |
5188 | 0 | } |
5189 | 0 | tmp->next = ret; |
5190 | 0 | } |
5191 | 0 | } |
5192 | 0 | return (ret); |
5193 | 0 | } |
5194 | | |
5195 | | /** |
5196 | | * parse the content of a RelaxNG element node. |
5197 | | * |
5198 | | * @param ctxt a Relax-NG parser context |
5199 | | * @param node the element node |
5200 | | * @returns the definition pointer or NULL in case of error. |
5201 | | */ |
5202 | | static xmlRelaxNGDefinePtr |
5203 | | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
5204 | 0 | { |
5205 | 0 | xmlRelaxNGDefinePtr ret, cur, last; |
5206 | 0 | xmlNodePtr child; |
5207 | 0 | const xmlChar *olddefine; |
5208 | |
|
5209 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5210 | 0 | if (ret == NULL) |
5211 | 0 | return (NULL); |
5212 | 0 | ret->type = XML_RELAXNG_ELEMENT; |
5213 | 0 | ret->parent = ctxt->def; |
5214 | 0 | child = node->children; |
5215 | 0 | if (child == NULL) { |
5216 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
5217 | 0 | "xmlRelaxNGParseElement: element has no children\n", |
5218 | 0 | NULL, NULL); |
5219 | 0 | return (ret); |
5220 | 0 | } |
5221 | 0 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
5222 | 0 | if (cur != NULL) |
5223 | 0 | child = child->next; |
5224 | |
|
5225 | 0 | if (child == NULL) { |
5226 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
5227 | 0 | "xmlRelaxNGParseElement: element has no content\n", |
5228 | 0 | NULL, NULL); |
5229 | 0 | return (ret); |
5230 | 0 | } |
5231 | 0 | olddefine = ctxt->define; |
5232 | 0 | ctxt->define = NULL; |
5233 | 0 | last = NULL; |
5234 | 0 | while (child != NULL) { |
5235 | 0 | cur = xmlRelaxNGParsePattern(ctxt, child); |
5236 | 0 | if (cur != NULL) { |
5237 | 0 | cur->parent = ret; |
5238 | 0 | switch (cur->type) { |
5239 | 0 | case XML_RELAXNG_EMPTY: |
5240 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
5241 | 0 | case XML_RELAXNG_TEXT: |
5242 | 0 | case XML_RELAXNG_ELEMENT: |
5243 | 0 | case XML_RELAXNG_DATATYPE: |
5244 | 0 | case XML_RELAXNG_VALUE: |
5245 | 0 | case XML_RELAXNG_LIST: |
5246 | 0 | case XML_RELAXNG_REF: |
5247 | 0 | case XML_RELAXNG_PARENTREF: |
5248 | 0 | case XML_RELAXNG_EXTERNALREF: |
5249 | 0 | case XML_RELAXNG_DEF: |
5250 | 0 | case XML_RELAXNG_ZEROORMORE: |
5251 | 0 | case XML_RELAXNG_ONEORMORE: |
5252 | 0 | case XML_RELAXNG_OPTIONAL: |
5253 | 0 | case XML_RELAXNG_CHOICE: |
5254 | 0 | case XML_RELAXNG_GROUP: |
5255 | 0 | case XML_RELAXNG_INTERLEAVE: |
5256 | 0 | if (last == NULL) { |
5257 | 0 | ret->content = last = cur; |
5258 | 0 | } else { |
5259 | 0 | if ((last->type == XML_RELAXNG_ELEMENT) && |
5260 | 0 | (ret->content == last)) { |
5261 | 0 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
5262 | 0 | if (ret->content != NULL) { |
5263 | 0 | ret->content->type = XML_RELAXNG_GROUP; |
5264 | 0 | ret->content->content = last; |
5265 | 0 | } else { |
5266 | 0 | ret->content = last; |
5267 | 0 | } |
5268 | 0 | } |
5269 | 0 | last->next = cur; |
5270 | 0 | last = cur; |
5271 | 0 | } |
5272 | 0 | break; |
5273 | 0 | case XML_RELAXNG_ATTRIBUTE: |
5274 | 0 | cur->next = ret->attrs; |
5275 | 0 | ret->attrs = cur; |
5276 | 0 | break; |
5277 | 0 | case XML_RELAXNG_START: |
5278 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5279 | 0 | "RNG Internal error, start found in element\n", |
5280 | 0 | NULL, NULL); |
5281 | 0 | break; |
5282 | 0 | case XML_RELAXNG_PARAM: |
5283 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5284 | 0 | "RNG Internal error, param found in element\n", |
5285 | 0 | NULL, NULL); |
5286 | 0 | break; |
5287 | 0 | case XML_RELAXNG_EXCEPT: |
5288 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5289 | 0 | "RNG Internal error, except found in element\n", |
5290 | 0 | NULL, NULL); |
5291 | 0 | break; |
5292 | 0 | case XML_RELAXNG_NOOP: |
5293 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5294 | 0 | "RNG Internal error, noop found in element\n", |
5295 | 0 | NULL, NULL); |
5296 | 0 | break; |
5297 | 0 | } |
5298 | 0 | } |
5299 | 0 | child = child->next; |
5300 | 0 | } |
5301 | 0 | ctxt->define = olddefine; |
5302 | 0 | return (ret); |
5303 | 0 | } |
5304 | | |
5305 | | /** |
5306 | | * parse the content of a RelaxNG start node. |
5307 | | * |
5308 | | * @param ctxt a Relax-NG parser context |
5309 | | * @param nodes list of nodes |
5310 | | * @param group use an implicit `<group>` for elements |
5311 | | * @returns the definition pointer or NULL in case of error. |
5312 | | */ |
5313 | | static xmlRelaxNGDefinePtr |
5314 | | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
5315 | | int group) |
5316 | 0 | { |
5317 | 0 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
5318 | |
|
5319 | 0 | parent = ctxt->def; |
5320 | 0 | while (nodes != NULL) { |
5321 | 0 | if (IS_RELAXNG(nodes, "element")) { |
5322 | 0 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
5323 | 0 | if (cur == NULL) |
5324 | 0 | return (NULL); |
5325 | 0 | if (def == NULL) { |
5326 | 0 | def = last = cur; |
5327 | 0 | } else { |
5328 | 0 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
5329 | 0 | (def == last)) { |
5330 | 0 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
5331 | 0 | if (def == NULL) |
5332 | 0 | return (NULL); |
5333 | 0 | def->type = XML_RELAXNG_GROUP; |
5334 | 0 | def->content = last; |
5335 | 0 | } |
5336 | 0 | last->next = cur; |
5337 | 0 | last = cur; |
5338 | 0 | } |
5339 | 0 | cur->parent = parent; |
5340 | 0 | } else { |
5341 | 0 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
5342 | 0 | if (cur != NULL) { |
5343 | 0 | if (def == NULL) { |
5344 | 0 | def = last = cur; |
5345 | 0 | } else { |
5346 | 0 | last->next = cur; |
5347 | 0 | last = cur; |
5348 | 0 | } |
5349 | 0 | cur->parent = parent; |
5350 | 0 | } |
5351 | 0 | } |
5352 | 0 | nodes = nodes->next; |
5353 | 0 | } |
5354 | 0 | return (def); |
5355 | 0 | } |
5356 | | |
5357 | | /** |
5358 | | * parse the content of a RelaxNG start node. |
5359 | | * |
5360 | | * @param ctxt a Relax-NG parser context |
5361 | | * @param nodes start children nodes |
5362 | | * @returns 0 in case of success, -1 in case of error |
5363 | | */ |
5364 | | static int |
5365 | | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
5366 | 0 | { |
5367 | 0 | int ret = 0; |
5368 | 0 | xmlRelaxNGDefinePtr def = NULL, last; |
5369 | |
|
5370 | 0 | if (nodes == NULL) { |
5371 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
5372 | 0 | NULL, NULL); |
5373 | 0 | return (-1); |
5374 | 0 | } |
5375 | 0 | if (IS_RELAXNG(nodes, "empty")) { |
5376 | 0 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
5377 | 0 | if (def == NULL) |
5378 | 0 | return (-1); |
5379 | 0 | def->type = XML_RELAXNG_EMPTY; |
5380 | 0 | if (nodes->children != NULL) { |
5381 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
5382 | 0 | "element empty is not empty\n", NULL, NULL); |
5383 | 0 | } |
5384 | 0 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
5385 | 0 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
5386 | 0 | if (def == NULL) |
5387 | 0 | return (-1); |
5388 | 0 | def->type = XML_RELAXNG_NOT_ALLOWED; |
5389 | 0 | if (nodes->children != NULL) { |
5390 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
5391 | 0 | "element notAllowed is not empty\n", NULL, NULL); |
5392 | 0 | } |
5393 | 0 | } else { |
5394 | 0 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
5395 | 0 | } |
5396 | 0 | if (ctxt->grammar->start != NULL) { |
5397 | 0 | last = ctxt->grammar->start; |
5398 | 0 | while (last->next != NULL) |
5399 | 0 | last = last->next; |
5400 | 0 | last->next = def; |
5401 | 0 | } else { |
5402 | 0 | ctxt->grammar->start = def; |
5403 | 0 | } |
5404 | 0 | nodes = nodes->next; |
5405 | 0 | if (nodes != NULL) { |
5406 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
5407 | 0 | "start more than one children\n", NULL, NULL); |
5408 | 0 | return (-1); |
5409 | 0 | } |
5410 | 0 | return (ret); |
5411 | 0 | } |
5412 | | |
5413 | | /** |
5414 | | * parse the content of a RelaxNG grammar node. |
5415 | | * |
5416 | | * @param ctxt a Relax-NG parser context |
5417 | | * @param nodes grammar children nodes |
5418 | | * @returns 0 in case of success, -1 in case of error |
5419 | | */ |
5420 | | static int |
5421 | | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
5422 | | xmlNodePtr nodes) |
5423 | 0 | { |
5424 | 0 | int ret = 0, tmp; |
5425 | |
|
5426 | 0 | if (nodes == NULL) { |
5427 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
5428 | 0 | "grammar has no children\n", NULL, NULL); |
5429 | 0 | return (-1); |
5430 | 0 | } |
5431 | 0 | while (nodes != NULL) { |
5432 | 0 | if (IS_RELAXNG(nodes, "start")) { |
5433 | 0 | if (nodes->children == NULL) { |
5434 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
5435 | 0 | "start has no children\n", NULL, NULL); |
5436 | 0 | } else { |
5437 | 0 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
5438 | 0 | if (tmp != 0) |
5439 | 0 | ret = -1; |
5440 | 0 | } |
5441 | 0 | } else if (IS_RELAXNG(nodes, "define")) { |
5442 | 0 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
5443 | 0 | if (tmp != 0) |
5444 | 0 | ret = -1; |
5445 | 0 | } else if (IS_RELAXNG(nodes, "include")) { |
5446 | 0 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
5447 | 0 | if (tmp != 0) |
5448 | 0 | ret = -1; |
5449 | 0 | } else { |
5450 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
5451 | 0 | "grammar has unexpected child %s\n", nodes->name, |
5452 | 0 | NULL); |
5453 | 0 | ret = -1; |
5454 | 0 | } |
5455 | 0 | nodes = nodes->next; |
5456 | 0 | } |
5457 | 0 | return (ret); |
5458 | 0 | } |
5459 | | |
5460 | | /** |
5461 | | * Applies the 4.17. combine attribute rule for all the define |
5462 | | * element of a given grammar using the same name. |
5463 | | * |
5464 | | * @param payload the ref |
5465 | | * @param data a Relax-NG parser context |
5466 | | * @param name the name associated to the defines |
5467 | | */ |
5468 | | static void |
5469 | | xmlRelaxNGCheckReference(void *payload, void *data, const xmlChar * name) |
5470 | 0 | { |
5471 | 0 | xmlRelaxNGDefinePtr ref = (xmlRelaxNGDefinePtr) payload; |
5472 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
5473 | 0 | xmlRelaxNGGrammarPtr grammar; |
5474 | 0 | xmlRelaxNGDefinePtr def, cur; |
5475 | | |
5476 | | /* |
5477 | | * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef |
5478 | | */ |
5479 | 0 | if (ref->dflags & IS_EXTERNAL_REF) |
5480 | 0 | return; |
5481 | | |
5482 | 0 | grammar = ctxt->grammar; |
5483 | 0 | if (grammar == NULL) { |
5484 | 0 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
5485 | 0 | "Internal error: no grammar in CheckReference %s\n", |
5486 | 0 | name, NULL); |
5487 | 0 | return; |
5488 | 0 | } |
5489 | 0 | if (ref->content != NULL) { |
5490 | 0 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
5491 | 0 | "Internal error: reference has content in CheckReference %s\n", |
5492 | 0 | name, NULL); |
5493 | 0 | return; |
5494 | 0 | } |
5495 | 0 | if (grammar->defs != NULL) { |
5496 | 0 | def = xmlHashLookup(grammar->defs, name); |
5497 | 0 | if (def != NULL) { |
5498 | 0 | cur = ref; |
5499 | 0 | while (cur != NULL) { |
5500 | 0 | cur->content = def; |
5501 | 0 | cur = cur->nextHash; |
5502 | 0 | } |
5503 | 0 | } else { |
5504 | 0 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
5505 | 0 | "Reference %s has no matching definition\n", name, |
5506 | 0 | NULL); |
5507 | 0 | } |
5508 | 0 | } else { |
5509 | 0 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
5510 | 0 | "Reference %s has no matching definition\n", name, |
5511 | 0 | NULL); |
5512 | 0 | } |
5513 | 0 | } |
5514 | | |
5515 | | /** |
5516 | | * Applies the 4.17. combine attribute rule for all the define |
5517 | | * element of a given grammar using the same name. |
5518 | | * |
5519 | | * @param payload the define(s) list |
5520 | | * @param data a Relax-NG parser context |
5521 | | * @param name the name associated to the defines |
5522 | | */ |
5523 | | static void |
5524 | | xmlRelaxNGCheckCombine(void *payload, void *data, const xmlChar * name) |
5525 | 0 | { |
5526 | 0 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) payload; |
5527 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
5528 | 0 | xmlChar *combine; |
5529 | 0 | int choiceOrInterleave = -1; |
5530 | 0 | int missing = 0; |
5531 | 0 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
5532 | |
|
5533 | 0 | if (define->nextHash == NULL) |
5534 | 0 | return; |
5535 | 0 | cur = define; |
5536 | 0 | while (cur != NULL) { |
5537 | 0 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
5538 | 0 | if (combine != NULL) { |
5539 | 0 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
5540 | 0 | if (choiceOrInterleave == -1) |
5541 | 0 | choiceOrInterleave = 1; |
5542 | 0 | else if (choiceOrInterleave == 0) { |
5543 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
5544 | 0 | "Defines for %s use both 'choice' and 'interleave'\n", |
5545 | 0 | name, NULL); |
5546 | 0 | } |
5547 | 0 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
5548 | 0 | if (choiceOrInterleave == -1) |
5549 | 0 | choiceOrInterleave = 0; |
5550 | 0 | else if (choiceOrInterleave == 1) { |
5551 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
5552 | 0 | "Defines for %s use both 'choice' and 'interleave'\n", |
5553 | 0 | name, NULL); |
5554 | 0 | } |
5555 | 0 | } else { |
5556 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
5557 | 0 | "Defines for %s use unknown combine value '%s''\n", |
5558 | 0 | name, combine); |
5559 | 0 | } |
5560 | 0 | xmlFree(combine); |
5561 | 0 | } else { |
5562 | 0 | if (missing == 0) |
5563 | 0 | missing = 1; |
5564 | 0 | else { |
5565 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
5566 | 0 | "Some defines for %s needs the combine attribute\n", |
5567 | 0 | name, NULL); |
5568 | 0 | } |
5569 | 0 | } |
5570 | |
|
5571 | 0 | cur = cur->nextHash; |
5572 | 0 | } |
5573 | 0 | if (choiceOrInterleave == -1) |
5574 | 0 | choiceOrInterleave = 0; |
5575 | 0 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
5576 | 0 | if (cur == NULL) |
5577 | 0 | return; |
5578 | 0 | if (choiceOrInterleave == 0) |
5579 | 0 | cur->type = XML_RELAXNG_INTERLEAVE; |
5580 | 0 | else |
5581 | 0 | cur->type = XML_RELAXNG_CHOICE; |
5582 | 0 | tmp = define; |
5583 | 0 | last = NULL; |
5584 | 0 | while (tmp != NULL) { |
5585 | 0 | if (tmp->content != NULL) { |
5586 | 0 | if (tmp->content->next != NULL) { |
5587 | | /* |
5588 | | * we need first to create a wrapper. |
5589 | | */ |
5590 | 0 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
5591 | 0 | if (tmp2 == NULL) |
5592 | 0 | break; |
5593 | 0 | tmp2->type = XML_RELAXNG_GROUP; |
5594 | 0 | tmp2->content = tmp->content; |
5595 | 0 | } else { |
5596 | 0 | tmp2 = tmp->content; |
5597 | 0 | } |
5598 | 0 | if (last == NULL) { |
5599 | 0 | cur->content = tmp2; |
5600 | 0 | } else { |
5601 | 0 | last->next = tmp2; |
5602 | 0 | } |
5603 | 0 | last = tmp2; |
5604 | 0 | } |
5605 | 0 | tmp->content = cur; |
5606 | 0 | tmp = tmp->nextHash; |
5607 | 0 | } |
5608 | 0 | define->content = cur; |
5609 | 0 | if (choiceOrInterleave == 0) { |
5610 | 0 | if (ctxt->interleaves == NULL) |
5611 | 0 | ctxt->interleaves = xmlHashCreate(10); |
5612 | 0 | if (ctxt->interleaves == NULL) { |
5613 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5614 | 0 | "Failed to create interleaves hash table\n", NULL, |
5615 | 0 | NULL); |
5616 | 0 | } else { |
5617 | 0 | char tmpname[32]; |
5618 | |
|
5619 | 0 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
5620 | 0 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
5621 | 0 | 0) { |
5622 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5623 | 0 | "Failed to add %s to hash table\n", |
5624 | 0 | (const xmlChar *) tmpname, NULL); |
5625 | 0 | } |
5626 | 0 | } |
5627 | 0 | } |
5628 | 0 | } |
5629 | | |
5630 | | /** |
5631 | | * Applies the 4.17. combine rule for all the start |
5632 | | * element of a given grammar. |
5633 | | * |
5634 | | * @param ctxt a Relax-NG parser context |
5635 | | * @param grammar the grammar |
5636 | | */ |
5637 | | static void |
5638 | | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
5639 | | xmlRelaxNGGrammarPtr grammar) |
5640 | 0 | { |
5641 | 0 | xmlRelaxNGDefinePtr starts; |
5642 | 0 | xmlChar *combine; |
5643 | 0 | int choiceOrInterleave = -1; |
5644 | 0 | int missing = 0; |
5645 | 0 | xmlRelaxNGDefinePtr cur; |
5646 | |
|
5647 | 0 | starts = grammar->start; |
5648 | 0 | if ((starts == NULL) || (starts->next == NULL)) |
5649 | 0 | return; |
5650 | 0 | cur = starts; |
5651 | 0 | while (cur != NULL) { |
5652 | 0 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
5653 | 0 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
5654 | 0 | combine = NULL; |
5655 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
5656 | 0 | "Internal error: start element not found\n", NULL, |
5657 | 0 | NULL); |
5658 | 0 | } else { |
5659 | 0 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
5660 | 0 | } |
5661 | |
|
5662 | 0 | if (combine != NULL) { |
5663 | 0 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
5664 | 0 | if (choiceOrInterleave == -1) |
5665 | 0 | choiceOrInterleave = 1; |
5666 | 0 | else if (choiceOrInterleave == 0) { |
5667 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
5668 | 0 | "<start> use both 'choice' and 'interleave'\n", |
5669 | 0 | NULL, NULL); |
5670 | 0 | } |
5671 | 0 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
5672 | 0 | if (choiceOrInterleave == -1) |
5673 | 0 | choiceOrInterleave = 0; |
5674 | 0 | else if (choiceOrInterleave == 1) { |
5675 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
5676 | 0 | "<start> use both 'choice' and 'interleave'\n", |
5677 | 0 | NULL, NULL); |
5678 | 0 | } |
5679 | 0 | } else { |
5680 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
5681 | 0 | "<start> uses unknown combine value '%s''\n", |
5682 | 0 | combine, NULL); |
5683 | 0 | } |
5684 | 0 | xmlFree(combine); |
5685 | 0 | } else { |
5686 | 0 | if (missing == 0) |
5687 | 0 | missing = 1; |
5688 | 0 | else { |
5689 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
5690 | 0 | "Some <start> element miss the combine attribute\n", |
5691 | 0 | NULL, NULL); |
5692 | 0 | } |
5693 | 0 | } |
5694 | |
|
5695 | 0 | cur = cur->next; |
5696 | 0 | } |
5697 | 0 | if (choiceOrInterleave == -1) |
5698 | 0 | choiceOrInterleave = 0; |
5699 | 0 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
5700 | 0 | if (cur == NULL) |
5701 | 0 | return; |
5702 | 0 | if (choiceOrInterleave == 0) |
5703 | 0 | cur->type = XML_RELAXNG_INTERLEAVE; |
5704 | 0 | else |
5705 | 0 | cur->type = XML_RELAXNG_CHOICE; |
5706 | 0 | cur->content = grammar->start; |
5707 | 0 | grammar->start = cur; |
5708 | 0 | if (choiceOrInterleave == 0) { |
5709 | 0 | if (ctxt->interleaves == NULL) |
5710 | 0 | ctxt->interleaves = xmlHashCreate(10); |
5711 | 0 | if (ctxt->interleaves == NULL) { |
5712 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5713 | 0 | "Failed to create interleaves hash table\n", NULL, |
5714 | 0 | NULL); |
5715 | 0 | } else { |
5716 | 0 | char tmpname[32]; |
5717 | |
|
5718 | 0 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
5719 | 0 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
5720 | 0 | 0) { |
5721 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5722 | 0 | "Failed to add %s to hash table\n", |
5723 | 0 | (const xmlChar *) tmpname, NULL); |
5724 | 0 | } |
5725 | 0 | } |
5726 | 0 | } |
5727 | 0 | } |
5728 | | |
5729 | | /** |
5730 | | * Check for cycles. |
5731 | | * |
5732 | | * @param ctxt a Relax-NG parser context |
5733 | | * @param cur grammar children nodes |
5734 | | * @param depth the counter |
5735 | | * @returns 0 if check passed, and -1 in case of error |
5736 | | */ |
5737 | | static int |
5738 | | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
5739 | | xmlRelaxNGDefinePtr cur, int depth) |
5740 | 0 | { |
5741 | 0 | int ret = 0; |
5742 | |
|
5743 | 0 | while ((ret == 0) && (cur != NULL)) { |
5744 | 0 | if ((cur->type == XML_RELAXNG_REF) || |
5745 | 0 | (cur->type == XML_RELAXNG_PARENTREF)) { |
5746 | 0 | if (cur->depth == -1) { |
5747 | 0 | cur->depth = depth; |
5748 | 0 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
5749 | 0 | cur->depth = -2; |
5750 | 0 | } else if (depth == cur->depth) { |
5751 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
5752 | 0 | "Detected a cycle in %s references\n", |
5753 | 0 | cur->name, NULL); |
5754 | 0 | return (-1); |
5755 | 0 | } |
5756 | 0 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
5757 | 0 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
5758 | 0 | } else { |
5759 | 0 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
5760 | 0 | } |
5761 | 0 | cur = cur->next; |
5762 | 0 | } |
5763 | 0 | return (ret); |
5764 | 0 | } |
5765 | | |
5766 | | /** |
5767 | | * Try to unlink a definition. If not possible make it a NOOP |
5768 | | * |
5769 | | * @param ctxt a Relax-NG parser context |
5770 | | * @param cur the definition to unlink |
5771 | | * @param parent the parent definition |
5772 | | * @param prev the previous sibling definition |
5773 | | * @returns the new prev definition |
5774 | | */ |
5775 | | static xmlRelaxNGDefinePtr |
5776 | | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
5777 | | xmlRelaxNGDefinePtr cur, |
5778 | | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
5779 | 0 | { |
5780 | 0 | if (prev != NULL) { |
5781 | 0 | prev->next = cur->next; |
5782 | 0 | } else { |
5783 | 0 | if (parent != NULL) { |
5784 | 0 | if (parent->attrs == cur) |
5785 | 0 | parent->attrs = cur->next; |
5786 | 0 | else if (parent->nameClass == cur) |
5787 | 0 | parent->nameClass = cur->next; |
5788 | 0 | else { |
5789 | 0 | xmlRelaxNGDefinePtr content, last = NULL; |
5790 | 0 | content = parent->content; |
5791 | 0 | while (content != NULL) { |
5792 | 0 | if (content == cur) { |
5793 | 0 | if (last == NULL) { |
5794 | 0 | parent->content = cur->next; |
5795 | 0 | } else { |
5796 | 0 | last->next = cur->next; |
5797 | 0 | } |
5798 | 0 | break; |
5799 | 0 | } |
5800 | 0 | last = content; |
5801 | 0 | content = content->next; |
5802 | 0 | } |
5803 | 0 | } |
5804 | 0 | } else { |
5805 | 0 | cur->type = XML_RELAXNG_NOOP; |
5806 | 0 | prev = cur; |
5807 | 0 | } |
5808 | 0 | } |
5809 | 0 | return (prev); |
5810 | 0 | } |
5811 | | |
5812 | | /** |
5813 | | * Check for simplification of empty and notAllowed |
5814 | | * |
5815 | | * @param ctxt a Relax-NG parser context |
5816 | | * @param cur grammar children nodes |
5817 | | * @param parent parent |
5818 | | */ |
5819 | | static void |
5820 | | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
5821 | | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
5822 | 0 | { |
5823 | 0 | xmlRelaxNGDefinePtr prev = NULL; |
5824 | |
|
5825 | 0 | while (cur != NULL) { |
5826 | 0 | if ((cur->type == XML_RELAXNG_REF) || |
5827 | 0 | (cur->type == XML_RELAXNG_PARENTREF)) { |
5828 | 0 | if (cur->depth != -3) { |
5829 | 0 | cur->depth = -3; |
5830 | 0 | ctxt->def = cur; |
5831 | 0 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
5832 | 0 | ctxt->def = NULL; |
5833 | 0 | } |
5834 | 0 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
5835 | 0 | cur->parent = parent; |
5836 | 0 | if ((parent != NULL) && |
5837 | 0 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
5838 | 0 | (parent->type == XML_RELAXNG_LIST) || |
5839 | 0 | (parent->type == XML_RELAXNG_GROUP) || |
5840 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
5841 | 0 | (parent->type == XML_RELAXNG_ONEORMORE) || |
5842 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
5843 | 0 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
5844 | 0 | break; |
5845 | 0 | } |
5846 | 0 | if ((parent != NULL) && ((parent->type == XML_RELAXNG_CHOICE) || |
5847 | 0 | ((parent->type == XML_RELAXNG_DEF) && |
5848 | 0 | (ctxt->def != NULL && ctxt->def->parent != NULL) && (ctxt->def->parent->type == XML_RELAXNG_CHOICE)))) { |
5849 | 0 | if (parent->type == XML_RELAXNG_CHOICE) |
5850 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5851 | 0 | else if (ctxt->def->parent->type == XML_RELAXNG_CHOICE) { |
5852 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, ctxt->def, ctxt->def->parent, prev); |
5853 | 0 | } |
5854 | 0 | } else |
5855 | 0 | prev = cur; |
5856 | 0 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
5857 | 0 | cur->parent = parent; |
5858 | 0 | if ((parent != NULL) && |
5859 | 0 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
5860 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
5861 | 0 | parent->type = XML_RELAXNG_EMPTY; |
5862 | 0 | break; |
5863 | 0 | } |
5864 | 0 | if ((parent != NULL) && |
5865 | 0 | ((parent->type == XML_RELAXNG_GROUP) || |
5866 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
5867 | 0 | ((parent->type == XML_RELAXNG_DEF) && |
5868 | 0 | (ctxt->def != NULL && ctxt->def->parent != NULL) && |
5869 | 0 | (ctxt->def->parent->type == XML_RELAXNG_GROUP || |
5870 | 0 | ctxt->def->parent->type == XML_RELAXNG_INTERLEAVE)))) { |
5871 | 0 | if (parent->type == XML_RELAXNG_GROUP || parent->type == XML_RELAXNG_INTERLEAVE) |
5872 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5873 | 0 | else if (ctxt->def->parent->type == XML_RELAXNG_GROUP || ctxt->def->parent->type == XML_RELAXNG_INTERLEAVE) |
5874 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, ctxt->def, ctxt->def->parent, prev); |
5875 | 0 | } else |
5876 | 0 | prev = cur; |
5877 | 0 | } else { |
5878 | 0 | cur->parent = parent; |
5879 | 0 | if (cur->content != NULL) |
5880 | 0 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
5881 | 0 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
5882 | 0 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
5883 | 0 | if (cur->nameClass != NULL) |
5884 | 0 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
5885 | | /* |
5886 | | * On Elements, try to move attribute only generating rules on |
5887 | | * the attrs rules. |
5888 | | */ |
5889 | 0 | if (cur->type == XML_RELAXNG_ELEMENT) { |
5890 | 0 | int attronly; |
5891 | 0 | xmlRelaxNGDefinePtr tmp, pre; |
5892 | |
|
5893 | 0 | while (cur->content != NULL) { |
5894 | 0 | attronly = |
5895 | 0 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
5896 | 0 | if (attronly == 1) { |
5897 | | /* |
5898 | | * migrate cur->content to attrs |
5899 | | */ |
5900 | 0 | tmp = cur->content; |
5901 | 0 | cur->content = tmp->next; |
5902 | 0 | tmp->next = cur->attrs; |
5903 | 0 | cur->attrs = tmp; |
5904 | 0 | } else { |
5905 | | /* |
5906 | | * cur->content can generate elements or text |
5907 | | */ |
5908 | 0 | break; |
5909 | 0 | } |
5910 | 0 | } |
5911 | 0 | pre = cur->content; |
5912 | 0 | while ((pre != NULL) && (pre->next != NULL)) { |
5913 | 0 | tmp = pre->next; |
5914 | 0 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
5915 | 0 | if (attronly == 1) { |
5916 | | /* |
5917 | | * migrate tmp to attrs |
5918 | | * if this runs twice an infinite attrs->next loop can be created |
5919 | | */ |
5920 | 0 | pre->next = tmp->next; |
5921 | 0 | tmp->next = cur->attrs; |
5922 | 0 | cur->attrs = tmp; |
5923 | 0 | } else { |
5924 | 0 | pre = tmp; |
5925 | 0 | } |
5926 | 0 | } |
5927 | 0 | } |
5928 | | /* |
5929 | | * This may result in a simplification |
5930 | | */ |
5931 | 0 | if ((cur->type == XML_RELAXNG_GROUP) || |
5932 | 0 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
5933 | 0 | if (cur->content == NULL) |
5934 | 0 | cur->type = XML_RELAXNG_EMPTY; |
5935 | 0 | else if (cur->content->next == NULL) { |
5936 | 0 | if ((parent == NULL) && (prev == NULL)) { |
5937 | 0 | cur->type = XML_RELAXNG_NOOP; |
5938 | 0 | } else if (prev == NULL) { |
5939 | | /* |
5940 | | * this simplification may already have happened |
5941 | | * if this is done twice this leads to an infinite loop of attrs->next |
5942 | | */ |
5943 | 0 | if (parent->content != cur->content) { |
5944 | 0 | parent->content = cur->content; |
5945 | 0 | cur->content->next = cur->next; |
5946 | 0 | cur = cur->content; |
5947 | 0 | } |
5948 | 0 | } else { |
5949 | 0 | cur->content->next = cur->next; |
5950 | 0 | prev->next = cur->content; |
5951 | 0 | cur = cur->content; |
5952 | 0 | } |
5953 | 0 | } |
5954 | 0 | } |
5955 | | /* |
5956 | | * the current node may have been transformed back |
5957 | | */ |
5958 | 0 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
5959 | 0 | (cur->content != NULL) && |
5960 | 0 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
5961 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5962 | 0 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
5963 | 0 | if ((parent != NULL) && |
5964 | 0 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
5965 | 0 | (parent->type == XML_RELAXNG_LIST) || |
5966 | 0 | (parent->type == XML_RELAXNG_GROUP) || |
5967 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
5968 | 0 | (parent->type == XML_RELAXNG_ONEORMORE) || |
5969 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
5970 | 0 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
5971 | 0 | break; |
5972 | 0 | } |
5973 | 0 | if ((parent != NULL) && |
5974 | 0 | (parent->type == XML_RELAXNG_CHOICE)) { |
5975 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5976 | 0 | } else |
5977 | 0 | prev = cur; |
5978 | 0 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
5979 | 0 | if ((parent != NULL) && |
5980 | 0 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
5981 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
5982 | 0 | parent->type = XML_RELAXNG_EMPTY; |
5983 | 0 | break; |
5984 | 0 | } |
5985 | 0 | if ((parent != NULL) && |
5986 | 0 | ((parent->type == XML_RELAXNG_GROUP) || |
5987 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
5988 | 0 | (parent->type == XML_RELAXNG_CHOICE))) { |
5989 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5990 | 0 | } else |
5991 | 0 | prev = cur; |
5992 | 0 | } else { |
5993 | 0 | prev = cur; |
5994 | 0 | } |
5995 | 0 | } |
5996 | 0 | cur = cur->next; |
5997 | 0 | } |
5998 | 0 | } |
5999 | | |
6000 | | /** |
6001 | | * Try to group 2 content types |
6002 | | * |
6003 | | * @param ct1 the first content type |
6004 | | * @param ct2 the second content type |
6005 | | * @returns the content type |
6006 | | */ |
6007 | | static xmlRelaxNGContentType |
6008 | | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
6009 | | xmlRelaxNGContentType ct2) |
6010 | 0 | { |
6011 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
6012 | 0 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
6013 | 0 | return (XML_RELAXNG_CONTENT_ERROR); |
6014 | 0 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
6015 | 0 | return (ct2); |
6016 | 0 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
6017 | 0 | return (ct1); |
6018 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
6019 | 0 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
6020 | 0 | return (XML_RELAXNG_CONTENT_COMPLEX); |
6021 | 0 | return (XML_RELAXNG_CONTENT_ERROR); |
6022 | 0 | } |
6023 | | |
6024 | | /** |
6025 | | * Compute the max content-type |
6026 | | * |
6027 | | * @param ct1 the first content type |
6028 | | * @param ct2 the second content type |
6029 | | * @returns the content type |
6030 | | */ |
6031 | | static xmlRelaxNGContentType |
6032 | | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
6033 | | xmlRelaxNGContentType ct2) |
6034 | 0 | { |
6035 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
6036 | 0 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
6037 | 0 | return (XML_RELAXNG_CONTENT_ERROR); |
6038 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
6039 | 0 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
6040 | 0 | return (XML_RELAXNG_CONTENT_SIMPLE); |
6041 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
6042 | 0 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
6043 | 0 | return (XML_RELAXNG_CONTENT_COMPLEX); |
6044 | 0 | return (XML_RELAXNG_CONTENT_EMPTY); |
6045 | 0 | } |
6046 | | |
6047 | | /** |
6048 | | * Check for rules in section 7.1 and 7.2 |
6049 | | * |
6050 | | * @param ctxt a Relax-NG parser context |
6051 | | * @param cur the current definition |
6052 | | * @param flags some accumulated flags |
6053 | | * @param ptype the parent type |
6054 | | * @returns the content type of `cur` |
6055 | | */ |
6056 | | static xmlRelaxNGContentType |
6057 | | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
6058 | | xmlRelaxNGDefinePtr cur, int flags, |
6059 | | xmlRelaxNGType ptype) |
6060 | 0 | { |
6061 | 0 | int nflags; |
6062 | 0 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
6063 | |
|
6064 | 0 | while (cur != NULL) { |
6065 | 0 | ret = XML_RELAXNG_CONTENT_EMPTY; |
6066 | 0 | if ((cur->type == XML_RELAXNG_REF) || |
6067 | 0 | (cur->type == XML_RELAXNG_PARENTREF)) { |
6068 | | /* |
6069 | | * This should actually be caught by list//element(ref) at the |
6070 | | * element boundaries, c.f. Bug #159968 local refs are dropped |
6071 | | * in step 4.19. |
6072 | | */ |
6073 | | #if 0 |
6074 | | if (flags & XML_RELAXNG_IN_LIST) { |
6075 | | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
6076 | | "Found forbidden pattern list//ref\n", NULL, |
6077 | | NULL); |
6078 | | } |
6079 | | #endif |
6080 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6081 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
6082 | 0 | "Found forbidden pattern data/except//ref\n", |
6083 | 0 | NULL, NULL); |
6084 | 0 | } |
6085 | 0 | if (cur->content == NULL) { |
6086 | 0 | if (cur->type == XML_RELAXNG_PARENTREF) |
6087 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
6088 | 0 | "Internal found no define for parent refs\n", |
6089 | 0 | NULL, NULL); |
6090 | 0 | else |
6091 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
6092 | 0 | "Internal found no define for ref %s\n", |
6093 | 0 | (cur->name ? cur->name: BAD_CAST "null"), NULL); |
6094 | 0 | } |
6095 | 0 | if (cur->depth > -4) { |
6096 | 0 | cur->depth = -4; |
6097 | 0 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
6098 | 0 | flags, cur->type); |
6099 | 0 | cur->depth = ret - 15; |
6100 | 0 | } else if (cur->depth == -4) { |
6101 | 0 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
6102 | 0 | } else { |
6103 | 0 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
6104 | 0 | } |
6105 | 0 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
6106 | | /* |
6107 | | * The 7.3 Attribute derivation rule for groups is plugged there |
6108 | | */ |
6109 | 0 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
6110 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6111 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
6112 | 0 | "Found forbidden pattern data/except//element(ref)\n", |
6113 | 0 | NULL, NULL); |
6114 | 0 | } |
6115 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6116 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
6117 | 0 | "Found forbidden pattern list//element(ref)\n", |
6118 | 0 | NULL, NULL); |
6119 | 0 | } |
6120 | 0 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
6121 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
6122 | 0 | "Found forbidden pattern attribute//element(ref)\n", |
6123 | 0 | NULL, NULL); |
6124 | 0 | } |
6125 | 0 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
6126 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
6127 | 0 | "Found forbidden pattern attribute//element(ref)\n", |
6128 | 0 | NULL, NULL); |
6129 | 0 | } |
6130 | | /* |
6131 | | * reset since in the simple form elements are only child |
6132 | | * of grammar/define |
6133 | | */ |
6134 | 0 | nflags = 0; |
6135 | 0 | ret = |
6136 | 0 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
6137 | 0 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
6138 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
6139 | 0 | "Element %s attributes have a content type error\n", |
6140 | 0 | cur->name, NULL); |
6141 | 0 | } |
6142 | 0 | ret = |
6143 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6144 | 0 | cur->type); |
6145 | 0 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
6146 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
6147 | 0 | "Element %s has a content type error\n", |
6148 | 0 | cur->name, NULL); |
6149 | 0 | } else { |
6150 | 0 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
6151 | 0 | } |
6152 | 0 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
6153 | 0 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
6154 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
6155 | 0 | "Found forbidden pattern attribute//attribute\n", |
6156 | 0 | NULL, NULL); |
6157 | 0 | } |
6158 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6159 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
6160 | 0 | "Found forbidden pattern list//attribute\n", |
6161 | 0 | NULL, NULL); |
6162 | 0 | } |
6163 | 0 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
6164 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
6165 | 0 | "Found forbidden pattern oneOrMore//group//attribute\n", |
6166 | 0 | NULL, NULL); |
6167 | 0 | } |
6168 | 0 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
6169 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
6170 | 0 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
6171 | 0 | NULL, NULL); |
6172 | 0 | } |
6173 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6174 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
6175 | 0 | "Found forbidden pattern data/except//attribute\n", |
6176 | 0 | NULL, NULL); |
6177 | 0 | } |
6178 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6179 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
6180 | 0 | "Found forbidden pattern start//attribute\n", |
6181 | 0 | NULL, NULL); |
6182 | 0 | } |
6183 | 0 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
6184 | 0 | && cur->name == NULL |
6185 | | /* following is checking alternative name class readiness |
6186 | | in case it went the "choice" route */ |
6187 | 0 | && cur->nameClass == NULL) { |
6188 | 0 | if (cur->ns == NULL) { |
6189 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
6190 | 0 | "Found anyName attribute without oneOrMore ancestor\n", |
6191 | 0 | NULL, NULL); |
6192 | 0 | } else { |
6193 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
6194 | 0 | "Found nsName attribute without oneOrMore ancestor\n", |
6195 | 0 | NULL, NULL); |
6196 | 0 | } |
6197 | 0 | } |
6198 | 0 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
6199 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
6200 | 0 | ret = XML_RELAXNG_CONTENT_EMPTY; |
6201 | 0 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
6202 | 0 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
6203 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6204 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
6205 | 0 | "Found forbidden pattern data/except//oneOrMore\n", |
6206 | 0 | NULL, NULL); |
6207 | 0 | } |
6208 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6209 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
6210 | 0 | "Found forbidden pattern start//oneOrMore\n", |
6211 | 0 | NULL, NULL); |
6212 | 0 | } |
6213 | 0 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
6214 | 0 | ret = |
6215 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6216 | 0 | cur->type); |
6217 | 0 | ret = xmlRelaxNGGroupContentType(ret, ret); |
6218 | 0 | } else if (cur->type == XML_RELAXNG_LIST) { |
6219 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6220 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
6221 | 0 | "Found forbidden pattern list//list\n", NULL, |
6222 | 0 | NULL); |
6223 | 0 | } |
6224 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6225 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
6226 | 0 | "Found forbidden pattern data/except//list\n", |
6227 | 0 | NULL, NULL); |
6228 | 0 | } |
6229 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6230 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
6231 | 0 | "Found forbidden pattern start//list\n", NULL, |
6232 | 0 | NULL); |
6233 | 0 | } |
6234 | 0 | nflags = flags | XML_RELAXNG_IN_LIST; |
6235 | 0 | ret = |
6236 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6237 | 0 | cur->type); |
6238 | 0 | } else if (cur->type == XML_RELAXNG_GROUP) { |
6239 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6240 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
6241 | 0 | "Found forbidden pattern data/except//group\n", |
6242 | 0 | NULL, NULL); |
6243 | 0 | } |
6244 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6245 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
6246 | 0 | "Found forbidden pattern start//group\n", NULL, |
6247 | 0 | NULL); |
6248 | 0 | } |
6249 | 0 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
6250 | 0 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
6251 | 0 | else |
6252 | 0 | nflags = flags; |
6253 | 0 | ret = |
6254 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6255 | 0 | cur->type); |
6256 | | /* |
6257 | | * The 7.3 Attribute derivation rule for groups is plugged there |
6258 | | */ |
6259 | 0 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
6260 | 0 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
6261 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6262 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
6263 | 0 | "Found forbidden pattern list//interleave\n", |
6264 | 0 | NULL, NULL); |
6265 | 0 | } |
6266 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6267 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
6268 | 0 | "Found forbidden pattern data/except//interleave\n", |
6269 | 0 | NULL, NULL); |
6270 | 0 | } |
6271 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6272 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
6273 | 0 | "Found forbidden pattern start//interleave\n", |
6274 | 0 | NULL, NULL); |
6275 | 0 | } |
6276 | 0 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
6277 | 0 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
6278 | 0 | else |
6279 | 0 | nflags = flags; |
6280 | 0 | ret = |
6281 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6282 | 0 | cur->type); |
6283 | 0 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
6284 | 0 | if ((cur->parent != NULL) && |
6285 | 0 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
6286 | 0 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
6287 | 0 | else |
6288 | 0 | nflags = flags; |
6289 | 0 | ret = |
6290 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6291 | 0 | cur->type); |
6292 | 0 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
6293 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6294 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
6295 | 0 | "Found forbidden pattern start//data\n", NULL, |
6296 | 0 | NULL); |
6297 | 0 | } |
6298 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6299 | 0 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
6300 | 0 | } else if (cur->type == XML_RELAXNG_VALUE) { |
6301 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6302 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
6303 | 0 | "Found forbidden pattern start//value\n", NULL, |
6304 | 0 | NULL); |
6305 | 0 | } |
6306 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6307 | 0 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
6308 | 0 | } else if (cur->type == XML_RELAXNG_TEXT) { |
6309 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6310 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
6311 | 0 | "Found forbidden pattern list//text\n", NULL, |
6312 | 0 | NULL); |
6313 | 0 | } |
6314 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6315 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
6316 | 0 | "Found forbidden pattern data/except//text\n", |
6317 | 0 | NULL, NULL); |
6318 | 0 | } |
6319 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6320 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
6321 | 0 | "Found forbidden pattern start//text\n", NULL, |
6322 | 0 | NULL); |
6323 | 0 | } |
6324 | 0 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
6325 | 0 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
6326 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6327 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
6328 | 0 | "Found forbidden pattern data/except//empty\n", |
6329 | 0 | NULL, NULL); |
6330 | 0 | } |
6331 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6332 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
6333 | 0 | "Found forbidden pattern start//empty\n", NULL, |
6334 | 0 | NULL); |
6335 | 0 | } |
6336 | 0 | ret = XML_RELAXNG_CONTENT_EMPTY; |
6337 | 0 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
6338 | 0 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
6339 | 0 | ret = |
6340 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6341 | 0 | } else { |
6342 | 0 | ret = |
6343 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6344 | 0 | } |
6345 | 0 | cur = cur->next; |
6346 | 0 | if (ptype == XML_RELAXNG_GROUP) { |
6347 | 0 | val = xmlRelaxNGGroupContentType(val, ret); |
6348 | 0 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
6349 | | /* |
6350 | | * TODO: scan complain that tmp is never used, seems on purpose |
6351 | | * need double-checking |
6352 | | */ |
6353 | 0 | tmp = xmlRelaxNGGroupContentType(val, ret); |
6354 | 0 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
6355 | 0 | tmp = xmlRelaxNGMaxContentType(val, ret); |
6356 | 0 | } else if (ptype == XML_RELAXNG_CHOICE) { |
6357 | 0 | val = xmlRelaxNGMaxContentType(val, ret); |
6358 | 0 | } else if (ptype == XML_RELAXNG_LIST) { |
6359 | 0 | val = XML_RELAXNG_CONTENT_SIMPLE; |
6360 | 0 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
6361 | 0 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
6362 | 0 | val = XML_RELAXNG_CONTENT_ERROR; |
6363 | 0 | else |
6364 | 0 | val = XML_RELAXNG_CONTENT_SIMPLE; |
6365 | 0 | } else { |
6366 | 0 | val = xmlRelaxNGGroupContentType(val, ret); |
6367 | 0 | } |
6368 | |
|
6369 | 0 | } |
6370 | 0 | return (val); |
6371 | 0 | } |
6372 | | |
6373 | | /** |
6374 | | * parse a Relax-NG `<grammar>` node |
6375 | | * |
6376 | | * @param ctxt a Relax-NG parser context |
6377 | | * @param nodes grammar children nodes |
6378 | | * @returns the internal xmlRelaxNGGrammar built or |
6379 | | * NULL in case of error |
6380 | | */ |
6381 | | static xmlRelaxNGGrammarPtr |
6382 | | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
6383 | 0 | { |
6384 | 0 | xmlRelaxNGGrammarPtr ret, tmp, old; |
6385 | |
|
6386 | 0 | ret = xmlRelaxNGNewGrammar(ctxt); |
6387 | 0 | if (ret == NULL) |
6388 | 0 | return (NULL); |
6389 | | |
6390 | | /* |
6391 | | * Link the new grammar in the tree |
6392 | | */ |
6393 | 0 | ret->parent = ctxt->grammar; |
6394 | 0 | if (ctxt->grammar != NULL) { |
6395 | 0 | tmp = ctxt->grammar->children; |
6396 | 0 | if (tmp == NULL) { |
6397 | 0 | ctxt->grammar->children = ret; |
6398 | 0 | } else { |
6399 | 0 | while (tmp->next != NULL) |
6400 | 0 | tmp = tmp->next; |
6401 | 0 | tmp->next = ret; |
6402 | 0 | } |
6403 | 0 | } |
6404 | |
|
6405 | 0 | old = ctxt->grammar; |
6406 | 0 | ctxt->grammar = ret; |
6407 | 0 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
6408 | 0 | ctxt->grammar = ret; |
6409 | 0 | if (ctxt->grammar == NULL) { |
6410 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
6411 | 0 | "Failed to parse <grammar> content\n", NULL, NULL); |
6412 | 0 | } else if (ctxt->grammar->start == NULL) { |
6413 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
6414 | 0 | "Element <grammar> has no <start>\n", NULL, NULL); |
6415 | 0 | } |
6416 | | |
6417 | | /* |
6418 | | * Apply 4.17 merging rules to defines and starts |
6419 | | */ |
6420 | 0 | xmlRelaxNGCombineStart(ctxt, ret); |
6421 | 0 | if (ret->defs != NULL) { |
6422 | 0 | xmlHashScan(ret->defs, xmlRelaxNGCheckCombine, ctxt); |
6423 | 0 | } |
6424 | | |
6425 | | /* |
6426 | | * link together defines and refs in this grammar |
6427 | | */ |
6428 | 0 | if (ret->refs != NULL) { |
6429 | 0 | xmlHashScan(ret->refs, xmlRelaxNGCheckReference, ctxt); |
6430 | 0 | } |
6431 | | |
6432 | | |
6433 | | /* @@@@ */ |
6434 | |
|
6435 | 0 | ctxt->grammar = old; |
6436 | 0 | return (ret); |
6437 | 0 | } |
6438 | | |
6439 | | /** |
6440 | | * parse a Relax-NG definition resource and build an internal |
6441 | | * xmlRelaxNG structure which can be used to validate instances. |
6442 | | * |
6443 | | * @param ctxt a Relax-NG parser context |
6444 | | * @param node the root node of the RelaxNG schema |
6445 | | * @returns the internal XML RelaxNG structure built or |
6446 | | * NULL in case of error |
6447 | | */ |
6448 | | static xmlRelaxNGPtr |
6449 | | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
6450 | 0 | { |
6451 | 0 | xmlRelaxNGPtr schema = NULL; |
6452 | 0 | const xmlChar *olddefine; |
6453 | 0 | xmlRelaxNGGrammarPtr old; |
6454 | |
|
6455 | 0 | if ((ctxt == NULL) || (node == NULL)) |
6456 | 0 | return (NULL); |
6457 | | |
6458 | 0 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
6459 | 0 | if (schema == NULL) |
6460 | 0 | return (NULL); |
6461 | | |
6462 | 0 | olddefine = ctxt->define; |
6463 | 0 | ctxt->define = NULL; |
6464 | 0 | if (IS_RELAXNG(node, "grammar")) { |
6465 | 0 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
6466 | 0 | if (schema->topgrammar == NULL) { |
6467 | 0 | xmlRelaxNGFree(schema); |
6468 | 0 | return (NULL); |
6469 | 0 | } |
6470 | 0 | } else { |
6471 | 0 | xmlRelaxNGGrammarPtr tmp, ret; |
6472 | |
|
6473 | 0 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
6474 | 0 | if (schema->topgrammar == NULL) { |
6475 | 0 | xmlRelaxNGFree(schema); |
6476 | 0 | return (NULL); |
6477 | 0 | } |
6478 | | /* |
6479 | | * Link the new grammar in the tree |
6480 | | */ |
6481 | 0 | ret->parent = ctxt->grammar; |
6482 | 0 | if (ctxt->grammar != NULL) { |
6483 | 0 | tmp = ctxt->grammar->children; |
6484 | 0 | if (tmp == NULL) { |
6485 | 0 | ctxt->grammar->children = ret; |
6486 | 0 | } else { |
6487 | 0 | while (tmp->next != NULL) |
6488 | 0 | tmp = tmp->next; |
6489 | 0 | tmp->next = ret; |
6490 | 0 | } |
6491 | 0 | } |
6492 | 0 | old = ctxt->grammar; |
6493 | 0 | ctxt->grammar = ret; |
6494 | 0 | xmlRelaxNGParseStart(ctxt, node); |
6495 | 0 | if (old != NULL) |
6496 | 0 | ctxt->grammar = old; |
6497 | 0 | } |
6498 | 0 | ctxt->define = olddefine; |
6499 | 0 | if (schema->topgrammar->start != NULL) { |
6500 | 0 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
6501 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
6502 | 0 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
6503 | 0 | while ((schema->topgrammar->start != NULL) && |
6504 | 0 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
6505 | 0 | (schema->topgrammar->start->next != NULL)) |
6506 | 0 | schema->topgrammar->start = |
6507 | 0 | schema->topgrammar->start->content; |
6508 | 0 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
6509 | 0 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
6510 | 0 | } |
6511 | 0 | } |
6512 | |
|
6513 | 0 | return (schema); |
6514 | 0 | } |
6515 | | |
6516 | | /************************************************************************ |
6517 | | * * |
6518 | | * Reading RelaxNGs * |
6519 | | * * |
6520 | | ************************************************************************/ |
6521 | | |
6522 | | /** |
6523 | | * Create an XML RelaxNGs parse context for that file/resource expected |
6524 | | * to contain an XML RelaxNGs file. |
6525 | | * |
6526 | | * @param URL the location of the schema |
6527 | | * @returns the parser context or NULL in case of error |
6528 | | */ |
6529 | | xmlRelaxNGParserCtxt * |
6530 | | xmlRelaxNGNewParserCtxt(const char *URL) |
6531 | 0 | { |
6532 | 0 | xmlRelaxNGParserCtxtPtr ret; |
6533 | |
|
6534 | 0 | if (URL == NULL) |
6535 | 0 | return (NULL); |
6536 | | |
6537 | 0 | ret = |
6538 | 0 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
6539 | 0 | if (ret == NULL) { |
6540 | 0 | xmlRngPErrMemory(NULL); |
6541 | 0 | return (NULL); |
6542 | 0 | } |
6543 | 0 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
6544 | 0 | ret->URL = xmlStrdup((const xmlChar *) URL); |
6545 | 0 | return (ret); |
6546 | 0 | } |
6547 | | |
6548 | | /** |
6549 | | * Create an XML RelaxNGs parse context for that memory buffer expected |
6550 | | * to contain an XML RelaxNGs file. |
6551 | | * |
6552 | | * @param buffer a pointer to a char array containing the schemas |
6553 | | * @param size the size of the array |
6554 | | * @returns the parser context or NULL in case of error |
6555 | | */ |
6556 | | xmlRelaxNGParserCtxt * |
6557 | | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
6558 | 0 | { |
6559 | 0 | xmlRelaxNGParserCtxtPtr ret; |
6560 | |
|
6561 | 0 | if ((buffer == NULL) || (size <= 0)) |
6562 | 0 | return (NULL); |
6563 | | |
6564 | 0 | ret = |
6565 | 0 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
6566 | 0 | if (ret == NULL) { |
6567 | 0 | xmlRngPErrMemory(NULL); |
6568 | 0 | return (NULL); |
6569 | 0 | } |
6570 | 0 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
6571 | 0 | ret->buffer = buffer; |
6572 | 0 | ret->size = size; |
6573 | 0 | return (ret); |
6574 | 0 | } |
6575 | | |
6576 | | /** |
6577 | | * Create an XML RelaxNGs parser context for that document. |
6578 | | * Note: since the process of compiling a RelaxNG schemas modifies the |
6579 | | * document, the `doc` parameter is duplicated internally. |
6580 | | * |
6581 | | * @param doc a preparsed document tree |
6582 | | * @returns the parser context or NULL in case of error |
6583 | | */ |
6584 | | xmlRelaxNGParserCtxt * |
6585 | | xmlRelaxNGNewDocParserCtxt(xmlDoc *doc) |
6586 | 0 | { |
6587 | 0 | xmlRelaxNGParserCtxtPtr ret; |
6588 | 0 | xmlDocPtr copy; |
6589 | |
|
6590 | 0 | if (doc == NULL) |
6591 | 0 | return (NULL); |
6592 | 0 | copy = xmlCopyDoc(doc, 1); |
6593 | 0 | if (copy == NULL) |
6594 | 0 | return (NULL); |
6595 | | |
6596 | 0 | ret = |
6597 | 0 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
6598 | 0 | if (ret == NULL) { |
6599 | 0 | xmlRngPErrMemory(NULL); |
6600 | 0 | xmlFreeDoc(copy); |
6601 | 0 | return (NULL); |
6602 | 0 | } |
6603 | 0 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
6604 | 0 | ret->document = copy; |
6605 | 0 | ret->freedoc = 1; |
6606 | 0 | ret->userData = xmlGenericErrorContext; |
6607 | 0 | return (ret); |
6608 | 0 | } |
6609 | | |
6610 | | /** |
6611 | | * Free the resources associated to the schema parser context |
6612 | | * |
6613 | | * @param ctxt the schema parser context |
6614 | | */ |
6615 | | void |
6616 | | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxt *ctxt) |
6617 | 0 | { |
6618 | 0 | if (ctxt == NULL) |
6619 | 0 | return; |
6620 | 0 | if (ctxt->URL != NULL) |
6621 | 0 | xmlFree(ctxt->URL); |
6622 | 0 | if (ctxt->doc != NULL) |
6623 | 0 | xmlRelaxNGFreeDocument(ctxt->doc); |
6624 | 0 | if (ctxt->interleaves != NULL) |
6625 | 0 | xmlHashFree(ctxt->interleaves, NULL); |
6626 | 0 | if (ctxt->documents != NULL) |
6627 | 0 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
6628 | 0 | if (ctxt->includes != NULL) |
6629 | 0 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
6630 | 0 | if (ctxt->docTab != NULL) |
6631 | 0 | xmlFree(ctxt->docTab); |
6632 | 0 | if (ctxt->incTab != NULL) |
6633 | 0 | xmlFree(ctxt->incTab); |
6634 | 0 | if (ctxt->defTab != NULL) { |
6635 | 0 | int i; |
6636 | |
|
6637 | 0 | for (i = 0; i < ctxt->defNr; i++) |
6638 | 0 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
6639 | 0 | xmlFree(ctxt->defTab); |
6640 | 0 | } |
6641 | 0 | if ((ctxt->document != NULL) && (ctxt->freedoc)) |
6642 | 0 | xmlFreeDoc(ctxt->document); |
6643 | 0 | xmlFree(ctxt); |
6644 | 0 | } |
6645 | | |
6646 | | /** |
6647 | | * Removes the leading and ending spaces of the value |
6648 | | * The string is modified "in situ" |
6649 | | * |
6650 | | * @param value a value |
6651 | | */ |
6652 | | static void |
6653 | | xmlRelaxNGNormExtSpace(xmlChar * value) |
6654 | 0 | { |
6655 | 0 | xmlChar *start = value; |
6656 | 0 | xmlChar *cur = value; |
6657 | |
|
6658 | 0 | if (value == NULL) |
6659 | 0 | return; |
6660 | | |
6661 | 0 | while (IS_BLANK_CH(*cur)) |
6662 | 0 | cur++; |
6663 | 0 | if (cur == start) { |
6664 | 0 | do { |
6665 | 0 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
6666 | 0 | cur++; |
6667 | 0 | if (*cur == 0) |
6668 | 0 | return; |
6669 | 0 | start = cur; |
6670 | 0 | while (IS_BLANK_CH(*cur)) |
6671 | 0 | cur++; |
6672 | 0 | if (*cur == 0) { |
6673 | 0 | *start = 0; |
6674 | 0 | return; |
6675 | 0 | } |
6676 | 0 | } while (1); |
6677 | 0 | } else { |
6678 | 0 | do { |
6679 | 0 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
6680 | 0 | *start++ = *cur++; |
6681 | 0 | if (*cur == 0) { |
6682 | 0 | *start = 0; |
6683 | 0 | return; |
6684 | 0 | } |
6685 | | /* don't try to normalize the inner spaces */ |
6686 | 0 | while (IS_BLANK_CH(*cur)) |
6687 | 0 | cur++; |
6688 | 0 | if (*cur == 0) { |
6689 | 0 | *start = 0; |
6690 | 0 | return; |
6691 | 0 | } |
6692 | 0 | *start++ = *cur++; |
6693 | 0 | } while (1); |
6694 | 0 | } |
6695 | 0 | } |
6696 | | |
6697 | | /** |
6698 | | * Check all the attributes on the given node |
6699 | | * |
6700 | | * @param ctxt a Relax-NG parser context |
6701 | | * @param node a Relax-NG node |
6702 | | */ |
6703 | | static void |
6704 | | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
6705 | 0 | { |
6706 | 0 | xmlAttrPtr cur, next; |
6707 | |
|
6708 | 0 | cur = node->properties; |
6709 | 0 | while (cur != NULL) { |
6710 | 0 | next = cur->next; |
6711 | 0 | if ((cur->ns == NULL) || |
6712 | 0 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
6713 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
6714 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
6715 | 0 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
6716 | 0 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
6717 | 0 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
6718 | 0 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
6719 | 0 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
6720 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6721 | 0 | "Attribute %s is not allowed on %s\n", |
6722 | 0 | cur->name, node->name); |
6723 | 0 | } |
6724 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
6725 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
6726 | 0 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
6727 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6728 | 0 | "Attribute %s is not allowed on %s\n", |
6729 | 0 | cur->name, node->name); |
6730 | 0 | } |
6731 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
6732 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
6733 | 0 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
6734 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6735 | 0 | "Attribute %s is not allowed on %s\n", |
6736 | 0 | cur->name, node->name); |
6737 | 0 | } |
6738 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
6739 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
6740 | 0 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
6741 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6742 | 0 | "Attribute %s is not allowed on %s\n", |
6743 | 0 | cur->name, node->name); |
6744 | 0 | } |
6745 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
6746 | 0 | xmlChar *val; |
6747 | 0 | xmlURIPtr uri; |
6748 | |
|
6749 | 0 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
6750 | 0 | if (val != NULL) { |
6751 | 0 | if (val[0] != 0) { |
6752 | 0 | uri = xmlParseURI((const char *) val); |
6753 | 0 | if (uri == NULL) { |
6754 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
6755 | 0 | "Attribute %s contains invalid URI %s\n", |
6756 | 0 | cur->name, val); |
6757 | 0 | } else { |
6758 | 0 | if (uri->scheme == NULL) { |
6759 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
6760 | 0 | "Attribute %s URI %s is not absolute\n", |
6761 | 0 | cur->name, val); |
6762 | 0 | } |
6763 | 0 | if (uri->fragment != NULL) { |
6764 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
6765 | 0 | "Attribute %s URI %s has a fragment ID\n", |
6766 | 0 | cur->name, val); |
6767 | 0 | } |
6768 | 0 | xmlFreeURI(uri); |
6769 | 0 | } |
6770 | 0 | } |
6771 | 0 | xmlFree(val); |
6772 | 0 | } |
6773 | 0 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
6774 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
6775 | 0 | "Unknown attribute %s on %s\n", cur->name, |
6776 | 0 | node->name); |
6777 | 0 | } |
6778 | 0 | } |
6779 | 0 | cur = next; |
6780 | 0 | } |
6781 | 0 | } |
6782 | | |
6783 | | /** |
6784 | | * Cleanup the subtree from unwanted nodes for parsing, resolve |
6785 | | * Include and externalRef lookups. |
6786 | | * |
6787 | | * @param ctxt a Relax-NG parser context |
6788 | | * @param root an xmlNode subtree |
6789 | | */ |
6790 | | static void |
6791 | | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
6792 | 0 | { |
6793 | 0 | xmlNodePtr cur, delete; |
6794 | |
|
6795 | 0 | delete = NULL; |
6796 | 0 | cur = root; |
6797 | 0 | while (cur != NULL) { |
6798 | 0 | if (delete != NULL) { |
6799 | 0 | xmlUnlinkNode(delete); |
6800 | 0 | xmlFreeNode(delete); |
6801 | 0 | delete = NULL; |
6802 | 0 | } |
6803 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
6804 | | /* |
6805 | | * Simplification 4.1. Annotations |
6806 | | */ |
6807 | 0 | if ((cur->ns == NULL) || |
6808 | 0 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
6809 | 0 | if ((cur->parent != NULL) && |
6810 | 0 | (cur->parent->type == XML_ELEMENT_NODE) && |
6811 | 0 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
6812 | 0 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
6813 | 0 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
6814 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
6815 | 0 | "element %s doesn't allow foreign elements\n", |
6816 | 0 | cur->parent->name, NULL); |
6817 | 0 | } |
6818 | 0 | delete = cur; |
6819 | 0 | goto skip_children; |
6820 | 0 | } else { |
6821 | 0 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
6822 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
6823 | 0 | xmlChar *href, *ns, *base, *URL; |
6824 | 0 | xmlRelaxNGDocumentPtr docu; |
6825 | 0 | xmlNodePtr tmp; |
6826 | 0 | xmlURIPtr uri; |
6827 | |
|
6828 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
6829 | 0 | if (ns == NULL) { |
6830 | 0 | tmp = cur->parent; |
6831 | 0 | while ((tmp != NULL) && |
6832 | 0 | (tmp->type == XML_ELEMENT_NODE)) { |
6833 | 0 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
6834 | 0 | if (ns != NULL) |
6835 | 0 | break; |
6836 | 0 | tmp = tmp->parent; |
6837 | 0 | } |
6838 | 0 | } |
6839 | 0 | href = xmlGetProp(cur, BAD_CAST "href"); |
6840 | 0 | if (href == NULL) { |
6841 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
6842 | 0 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
6843 | 0 | NULL, NULL); |
6844 | 0 | if (ns != NULL) |
6845 | 0 | xmlFree(ns); |
6846 | 0 | delete = cur; |
6847 | 0 | goto skip_children; |
6848 | 0 | } |
6849 | 0 | uri = xmlParseURI((const char *) href); |
6850 | 0 | if (uri == NULL) { |
6851 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6852 | 0 | "Incorrect URI for externalRef %s\n", |
6853 | 0 | href, NULL); |
6854 | 0 | if (ns != NULL) |
6855 | 0 | xmlFree(ns); |
6856 | 0 | if (href != NULL) |
6857 | 0 | xmlFree(href); |
6858 | 0 | delete = cur; |
6859 | 0 | goto skip_children; |
6860 | 0 | } |
6861 | 0 | if (uri->fragment != NULL) { |
6862 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6863 | 0 | "Fragment forbidden in URI for externalRef %s\n", |
6864 | 0 | href, NULL); |
6865 | 0 | if (ns != NULL) |
6866 | 0 | xmlFree(ns); |
6867 | 0 | xmlFreeURI(uri); |
6868 | 0 | if (href != NULL) |
6869 | 0 | xmlFree(href); |
6870 | 0 | delete = cur; |
6871 | 0 | goto skip_children; |
6872 | 0 | } |
6873 | 0 | xmlFreeURI(uri); |
6874 | 0 | base = xmlNodeGetBase(cur->doc, cur); |
6875 | 0 | URL = xmlBuildURI(href, base); |
6876 | 0 | if (URL == NULL) { |
6877 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6878 | 0 | "Failed to compute URL for externalRef %s\n", |
6879 | 0 | href, NULL); |
6880 | 0 | if (ns != NULL) |
6881 | 0 | xmlFree(ns); |
6882 | 0 | if (href != NULL) |
6883 | 0 | xmlFree(href); |
6884 | 0 | if (base != NULL) |
6885 | 0 | xmlFree(base); |
6886 | 0 | delete = cur; |
6887 | 0 | goto skip_children; |
6888 | 0 | } |
6889 | 0 | if (href != NULL) |
6890 | 0 | xmlFree(href); |
6891 | 0 | if (base != NULL) |
6892 | 0 | xmlFree(base); |
6893 | 0 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
6894 | 0 | if (docu == NULL) { |
6895 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
6896 | 0 | "Failed to load externalRef %s\n", URL, |
6897 | 0 | NULL); |
6898 | 0 | if (ns != NULL) |
6899 | 0 | xmlFree(ns); |
6900 | 0 | xmlFree(URL); |
6901 | 0 | delete = cur; |
6902 | 0 | goto skip_children; |
6903 | 0 | } |
6904 | 0 | if (ns != NULL) |
6905 | 0 | xmlFree(ns); |
6906 | 0 | xmlFree(URL); |
6907 | 0 | cur->psvi = docu; |
6908 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
6909 | 0 | xmlChar *href, *ns, *base, *URL; |
6910 | 0 | xmlRelaxNGIncludePtr incl; |
6911 | 0 | xmlNodePtr tmp; |
6912 | |
|
6913 | 0 | href = xmlGetProp(cur, BAD_CAST "href"); |
6914 | 0 | if (href == NULL) { |
6915 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
6916 | 0 | "xmlRelaxNGParse: include has no href attribute\n", |
6917 | 0 | NULL, NULL); |
6918 | 0 | delete = cur; |
6919 | 0 | goto skip_children; |
6920 | 0 | } |
6921 | 0 | base = xmlNodeGetBase(cur->doc, cur); |
6922 | 0 | URL = xmlBuildURI(href, base); |
6923 | 0 | if (URL == NULL) { |
6924 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6925 | 0 | "Failed to compute URL for include %s\n", |
6926 | 0 | href, NULL); |
6927 | 0 | if (href != NULL) |
6928 | 0 | xmlFree(href); |
6929 | 0 | if (base != NULL) |
6930 | 0 | xmlFree(base); |
6931 | 0 | delete = cur; |
6932 | 0 | goto skip_children; |
6933 | 0 | } |
6934 | 0 | if (href != NULL) |
6935 | 0 | xmlFree(href); |
6936 | 0 | if (base != NULL) |
6937 | 0 | xmlFree(base); |
6938 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
6939 | 0 | if (ns == NULL) { |
6940 | 0 | tmp = cur->parent; |
6941 | 0 | while ((tmp != NULL) && |
6942 | 0 | (tmp->type == XML_ELEMENT_NODE)) { |
6943 | 0 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
6944 | 0 | if (ns != NULL) |
6945 | 0 | break; |
6946 | 0 | tmp = tmp->parent; |
6947 | 0 | } |
6948 | 0 | } |
6949 | 0 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
6950 | 0 | if (ns != NULL) |
6951 | 0 | xmlFree(ns); |
6952 | 0 | if (incl == NULL) { |
6953 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
6954 | 0 | "Failed to load include %s\n", URL, |
6955 | 0 | NULL); |
6956 | 0 | xmlFree(URL); |
6957 | 0 | delete = cur; |
6958 | 0 | goto skip_children; |
6959 | 0 | } |
6960 | 0 | xmlFree(URL); |
6961 | 0 | cur->psvi = incl; |
6962 | 0 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
6963 | 0 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
6964 | 0 | { |
6965 | 0 | xmlChar *name, *ns; |
6966 | 0 | xmlNodePtr text = NULL; |
6967 | | |
6968 | | /* |
6969 | | * Simplification 4.8. name attribute of element |
6970 | | * and attribute elements |
6971 | | */ |
6972 | 0 | name = xmlGetProp(cur, BAD_CAST "name"); |
6973 | 0 | if (name != NULL) { |
6974 | 0 | if (cur->children == NULL) { |
6975 | 0 | text = |
6976 | 0 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
6977 | 0 | name); |
6978 | 0 | } else { |
6979 | 0 | xmlNodePtr node; |
6980 | |
|
6981 | 0 | node = xmlNewDocNode(cur->doc, cur->ns, |
6982 | 0 | BAD_CAST "name", NULL); |
6983 | 0 | if (node != NULL) { |
6984 | 0 | xmlAddPrevSibling(cur->children, node); |
6985 | 0 | text = xmlNewDocText(node->doc, name); |
6986 | 0 | xmlAddChild(node, text); |
6987 | 0 | text = node; |
6988 | 0 | } |
6989 | 0 | } |
6990 | 0 | if (text == NULL) { |
6991 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
6992 | 0 | "Failed to create a name %s element\n", |
6993 | 0 | name, NULL); |
6994 | 0 | } |
6995 | 0 | xmlUnsetProp(cur, BAD_CAST "name"); |
6996 | 0 | xmlFree(name); |
6997 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
6998 | 0 | if (ns != NULL) { |
6999 | 0 | if (text != NULL) { |
7000 | 0 | xmlSetProp(text, BAD_CAST "ns", ns); |
7001 | | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
7002 | 0 | } |
7003 | 0 | xmlFree(ns); |
7004 | 0 | } else if (xmlStrEqual(cur->name, |
7005 | 0 | BAD_CAST "attribute")) { |
7006 | 0 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
7007 | 0 | } |
7008 | 0 | } |
7009 | 0 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
7010 | 0 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
7011 | 0 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
7012 | | /* |
7013 | | * Simplification 4.8. name attribute of element |
7014 | | * and attribute elements |
7015 | | */ |
7016 | 0 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
7017 | 0 | xmlNodePtr node; |
7018 | 0 | xmlChar *ns = NULL; |
7019 | |
|
7020 | 0 | node = cur->parent; |
7021 | 0 | while ((node != NULL) && |
7022 | 0 | (node->type == XML_ELEMENT_NODE)) { |
7023 | 0 | ns = xmlGetProp(node, BAD_CAST "ns"); |
7024 | 0 | if (ns != NULL) { |
7025 | 0 | break; |
7026 | 0 | } |
7027 | 0 | node = node->parent; |
7028 | 0 | } |
7029 | 0 | if (ns == NULL) { |
7030 | 0 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
7031 | 0 | } else { |
7032 | 0 | xmlSetProp(cur, BAD_CAST "ns", ns); |
7033 | 0 | xmlFree(ns); |
7034 | 0 | } |
7035 | 0 | } |
7036 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
7037 | 0 | xmlChar *name, *local, *prefix; |
7038 | | |
7039 | | /* |
7040 | | * Simplification: 4.10. QNames |
7041 | | */ |
7042 | 0 | name = xmlNodeGetContent(cur); |
7043 | 0 | if (name != NULL) { |
7044 | 0 | local = xmlSplitQName2(name, &prefix); |
7045 | 0 | if (local != NULL) { |
7046 | 0 | xmlNsPtr ns; |
7047 | |
|
7048 | 0 | ns = xmlSearchNs(cur->doc, cur, prefix); |
7049 | 0 | if (ns == NULL) { |
7050 | 0 | xmlRngPErr(ctxt, cur, |
7051 | 0 | XML_RNGP_PREFIX_UNDEFINED, |
7052 | 0 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
7053 | 0 | prefix, NULL); |
7054 | 0 | } else { |
7055 | 0 | xmlSetProp(cur, BAD_CAST "ns", |
7056 | 0 | ns->href); |
7057 | 0 | xmlNodeSetContent(cur, local); |
7058 | 0 | } |
7059 | 0 | xmlFree(local); |
7060 | 0 | xmlFree(prefix); |
7061 | 0 | } |
7062 | 0 | xmlFree(name); |
7063 | 0 | } |
7064 | 0 | } |
7065 | | /* |
7066 | | * 4.16 |
7067 | | */ |
7068 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
7069 | 0 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
7070 | 0 | xmlRngPErr(ctxt, cur, |
7071 | 0 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
7072 | 0 | "Found nsName/except//nsName forbidden construct\n", |
7073 | 0 | NULL, NULL); |
7074 | 0 | } |
7075 | 0 | } |
7076 | 0 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
7077 | 0 | (cur != root)) { |
7078 | 0 | int oldflags = ctxt->flags; |
7079 | | |
7080 | | /* |
7081 | | * 4.16 |
7082 | | */ |
7083 | 0 | if ((cur->parent != NULL) && |
7084 | 0 | (xmlStrEqual |
7085 | 0 | (cur->parent->name, BAD_CAST "anyName"))) { |
7086 | 0 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
7087 | 0 | xmlRelaxNGCleanupTree(ctxt, cur); |
7088 | 0 | ctxt->flags = oldflags; |
7089 | 0 | goto skip_children; |
7090 | 0 | } else if ((cur->parent != NULL) && |
7091 | 0 | (xmlStrEqual |
7092 | 0 | (cur->parent->name, BAD_CAST "nsName"))) { |
7093 | 0 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
7094 | 0 | xmlRelaxNGCleanupTree(ctxt, cur); |
7095 | 0 | ctxt->flags = oldflags; |
7096 | 0 | goto skip_children; |
7097 | 0 | } |
7098 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
7099 | | /* |
7100 | | * 4.16 |
7101 | | */ |
7102 | 0 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
7103 | 0 | xmlRngPErr(ctxt, cur, |
7104 | 0 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
7105 | 0 | "Found anyName/except//anyName forbidden construct\n", |
7106 | 0 | NULL, NULL); |
7107 | 0 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
7108 | 0 | xmlRngPErr(ctxt, cur, |
7109 | 0 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
7110 | 0 | "Found nsName/except//anyName forbidden construct\n", |
7111 | 0 | NULL, NULL); |
7112 | 0 | } |
7113 | 0 | } |
7114 | | /* |
7115 | | * This is not an else since "include" is transformed |
7116 | | * into a div |
7117 | | */ |
7118 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
7119 | 0 | xmlChar *ns; |
7120 | 0 | xmlNodePtr child, ins, tmp; |
7121 | | |
7122 | | /* |
7123 | | * implements rule 4.11 |
7124 | | */ |
7125 | |
|
7126 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
7127 | |
|
7128 | 0 | child = cur->children; |
7129 | 0 | ins = cur; |
7130 | 0 | while (child != NULL) { |
7131 | 0 | if (ns != NULL) { |
7132 | 0 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
7133 | 0 | xmlSetProp(child, BAD_CAST "ns", ns); |
7134 | 0 | } |
7135 | 0 | } |
7136 | 0 | tmp = child->next; |
7137 | 0 | xmlUnlinkNode(child); |
7138 | 0 | ins = xmlAddNextSibling(ins, child); |
7139 | 0 | child = tmp; |
7140 | 0 | } |
7141 | 0 | if (ns != NULL) |
7142 | 0 | xmlFree(ns); |
7143 | | /* |
7144 | | * Since we are about to delete cur, if its nsDef is non-NULL we |
7145 | | * need to preserve it (it contains the ns definitions for the |
7146 | | * children we just moved). We'll just stick it on to the end |
7147 | | * of cur->parent's list, since it's never going to be re-serialized |
7148 | | * (bug 143738). |
7149 | | */ |
7150 | 0 | if ((cur->nsDef != NULL) && (cur->parent != NULL)) { |
7151 | 0 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
7152 | 0 | while (parDef->next != NULL) |
7153 | 0 | parDef = parDef->next; |
7154 | 0 | parDef->next = cur->nsDef; |
7155 | 0 | cur->nsDef = NULL; |
7156 | 0 | } |
7157 | 0 | delete = cur; |
7158 | 0 | goto skip_children; |
7159 | 0 | } |
7160 | 0 | } |
7161 | 0 | } |
7162 | | /* |
7163 | | * Simplification 4.2 whitespaces |
7164 | | */ |
7165 | 0 | else if ((cur->type == XML_TEXT_NODE) || |
7166 | 0 | (cur->type == XML_CDATA_SECTION_NODE)) { |
7167 | 0 | if (IS_BLANK_NODE(cur)) { |
7168 | 0 | if ((cur->parent != NULL) && |
7169 | 0 | (cur->parent->type == XML_ELEMENT_NODE)) { |
7170 | 0 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
7171 | 0 | && |
7172 | 0 | (!xmlStrEqual |
7173 | 0 | (cur->parent->name, BAD_CAST "param"))) |
7174 | 0 | delete = cur; |
7175 | 0 | } else { |
7176 | 0 | delete = cur; |
7177 | 0 | goto skip_children; |
7178 | 0 | } |
7179 | 0 | } |
7180 | 0 | } else { |
7181 | 0 | delete = cur; |
7182 | 0 | goto skip_children; |
7183 | 0 | } |
7184 | | |
7185 | | /* |
7186 | | * Skip to next node |
7187 | | */ |
7188 | 0 | if (cur->children != NULL) { |
7189 | 0 | if ((cur->children->type != XML_ENTITY_DECL) && |
7190 | 0 | (cur->children->type != XML_ENTITY_REF_NODE) && |
7191 | 0 | (cur->children->type != XML_ENTITY_NODE)) { |
7192 | 0 | cur = cur->children; |
7193 | 0 | continue; |
7194 | 0 | } |
7195 | 0 | } |
7196 | 0 | skip_children: |
7197 | 0 | if (cur->next != NULL) { |
7198 | 0 | cur = cur->next; |
7199 | 0 | continue; |
7200 | 0 | } |
7201 | | |
7202 | 0 | do { |
7203 | 0 | cur = cur->parent; |
7204 | 0 | if (cur == NULL) |
7205 | 0 | break; |
7206 | 0 | if (cur == root) { |
7207 | 0 | cur = NULL; |
7208 | 0 | break; |
7209 | 0 | } |
7210 | 0 | if (cur->next != NULL) { |
7211 | 0 | cur = cur->next; |
7212 | 0 | break; |
7213 | 0 | } |
7214 | 0 | } while (cur != NULL); |
7215 | 0 | } |
7216 | 0 | if (delete != NULL) { |
7217 | 0 | xmlUnlinkNode(delete); |
7218 | 0 | xmlFreeNode(delete); |
7219 | 0 | delete = NULL; |
7220 | 0 | } |
7221 | 0 | } |
7222 | | |
7223 | | /** |
7224 | | * Cleanup the document from unwanted nodes for parsing, resolve |
7225 | | * Include and externalRef lookups. |
7226 | | * |
7227 | | * @param ctxt a Relax-NG parser context |
7228 | | * @param doc an xmldoc document pointer |
7229 | | * @returns the cleaned up document or NULL in case of error |
7230 | | */ |
7231 | | static xmlDocPtr |
7232 | | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
7233 | 0 | { |
7234 | 0 | xmlNodePtr root; |
7235 | | |
7236 | | /* |
7237 | | * Extract the root |
7238 | | */ |
7239 | 0 | root = xmlDocGetRootElement(doc); |
7240 | 0 | if (root == NULL) { |
7241 | 0 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
7242 | 0 | ctxt->URL, NULL); |
7243 | 0 | return (NULL); |
7244 | 0 | } |
7245 | 0 | xmlRelaxNGCleanupTree(ctxt, root); |
7246 | 0 | return (doc); |
7247 | 0 | } |
7248 | | |
7249 | | /** |
7250 | | * parse a schema definition resource and build an internal |
7251 | | * XML Schema structure which can be used to validate instances. |
7252 | | * |
7253 | | * @param ctxt a Relax-NG parser context |
7254 | | * @returns the internal XML RelaxNG structure built from the resource or |
7255 | | * NULL in case of error |
7256 | | */ |
7257 | | xmlRelaxNG * |
7258 | | xmlRelaxNGParse(xmlRelaxNGParserCtxt *ctxt) |
7259 | 0 | { |
7260 | 0 | xmlRelaxNGPtr ret = NULL; |
7261 | 0 | xmlDocPtr doc; |
7262 | 0 | xmlNodePtr root; |
7263 | |
|
7264 | 0 | xmlRelaxNGInitTypes(); |
7265 | |
|
7266 | 0 | if (ctxt == NULL) |
7267 | 0 | return (NULL); |
7268 | | |
7269 | | /* |
7270 | | * First step is to parse the input document into an DOM/Infoset |
7271 | | */ |
7272 | 0 | if (ctxt->URL != NULL) { |
7273 | 0 | doc = xmlRelaxReadFile(ctxt, (const char *) ctxt->URL); |
7274 | 0 | if (doc == NULL) { |
7275 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
7276 | 0 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
7277 | 0 | NULL); |
7278 | 0 | return (NULL); |
7279 | 0 | } |
7280 | 0 | } else if (ctxt->buffer != NULL) { |
7281 | 0 | doc = xmlRelaxReadMemory(ctxt, ctxt->buffer, ctxt->size); |
7282 | 0 | if (doc == NULL) { |
7283 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
7284 | 0 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
7285 | 0 | NULL); |
7286 | 0 | return (NULL); |
7287 | 0 | } |
7288 | 0 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
7289 | 0 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
7290 | 0 | } else if (ctxt->document != NULL) { |
7291 | 0 | doc = ctxt->document; |
7292 | 0 | } else { |
7293 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
7294 | 0 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
7295 | 0 | return (NULL); |
7296 | 0 | } |
7297 | 0 | ctxt->document = doc; |
7298 | | |
7299 | | /* |
7300 | | * Some preprocessing of the document content |
7301 | | */ |
7302 | 0 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
7303 | 0 | if (doc == NULL) { |
7304 | 0 | xmlFreeDoc(ctxt->document); |
7305 | 0 | ctxt->document = NULL; |
7306 | 0 | return (NULL); |
7307 | 0 | } |
7308 | | |
7309 | | /* |
7310 | | * Then do the parsing for good |
7311 | | */ |
7312 | 0 | root = xmlDocGetRootElement(doc); |
7313 | 0 | if (root == NULL) { |
7314 | 0 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
7315 | 0 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
7316 | 0 | (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL); |
7317 | |
|
7318 | 0 | xmlFreeDoc(ctxt->document); |
7319 | 0 | ctxt->document = NULL; |
7320 | 0 | return (NULL); |
7321 | 0 | } |
7322 | 0 | ret = xmlRelaxNGParseDocument(ctxt, root); |
7323 | 0 | if (ret == NULL) { |
7324 | 0 | xmlFreeDoc(ctxt->document); |
7325 | 0 | ctxt->document = NULL; |
7326 | 0 | return (NULL); |
7327 | 0 | } |
7328 | | |
7329 | | /* |
7330 | | * Check the ref/defines links |
7331 | | */ |
7332 | | /* |
7333 | | * try to preprocess interleaves |
7334 | | */ |
7335 | 0 | if (ctxt->interleaves != NULL) { |
7336 | 0 | xmlHashScan(ctxt->interleaves, xmlRelaxNGComputeInterleaves, ctxt); |
7337 | 0 | } |
7338 | | |
7339 | | /* |
7340 | | * if there was a parsing error return NULL |
7341 | | */ |
7342 | 0 | if (ctxt->nbErrors > 0) { |
7343 | 0 | xmlRelaxNGFree(ret); |
7344 | 0 | ctxt->document = NULL; |
7345 | 0 | xmlFreeDoc(doc); |
7346 | 0 | return (NULL); |
7347 | 0 | } |
7348 | | |
7349 | | /* |
7350 | | * try to compile (parts of) the schemas |
7351 | | */ |
7352 | 0 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
7353 | 0 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
7354 | 0 | xmlRelaxNGDefinePtr def; |
7355 | |
|
7356 | 0 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
7357 | 0 | if (def != NULL) { |
7358 | 0 | def->type = XML_RELAXNG_START; |
7359 | 0 | def->content = ret->topgrammar->start; |
7360 | 0 | ret->topgrammar->start = def; |
7361 | 0 | } |
7362 | 0 | } |
7363 | 0 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
7364 | 0 | } |
7365 | | |
7366 | | /* |
7367 | | * Transfer the pointer for cleanup at the schema level. |
7368 | | */ |
7369 | 0 | ret->doc = doc; |
7370 | 0 | ctxt->document = NULL; |
7371 | 0 | ret->documents = ctxt->documents; |
7372 | 0 | ctxt->documents = NULL; |
7373 | |
|
7374 | 0 | ret->includes = ctxt->includes; |
7375 | 0 | ctxt->includes = NULL; |
7376 | 0 | ret->defNr = ctxt->defNr; |
7377 | 0 | ret->defTab = ctxt->defTab; |
7378 | 0 | ctxt->defTab = NULL; |
7379 | 0 | if (ctxt->idref == 1) |
7380 | 0 | ret->idref = 1; |
7381 | |
|
7382 | 0 | return (ret); |
7383 | 0 | } |
7384 | | |
7385 | | /** |
7386 | | * Set the callback functions used to handle errors for a validation context |
7387 | | * |
7388 | | * @deprecated Use #xmlRelaxNGSetParserStructuredErrors. |
7389 | | * |
7390 | | * @param ctxt a Relax-NG validation context |
7391 | | * @param err the error callback |
7392 | | * @param warn the warning callback |
7393 | | * @param ctx contextual data for the callbacks |
7394 | | */ |
7395 | | void |
7396 | | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxt *ctxt, |
7397 | | xmlRelaxNGValidityErrorFunc err, |
7398 | | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
7399 | 0 | { |
7400 | 0 | if (ctxt == NULL) |
7401 | 0 | return; |
7402 | 0 | ctxt->error = err; |
7403 | 0 | ctxt->warning = warn; |
7404 | 0 | ctxt->serror = NULL; |
7405 | 0 | ctxt->userData = ctx; |
7406 | 0 | } |
7407 | | |
7408 | | /** |
7409 | | * Get the callback information used to handle errors for a validation context |
7410 | | * |
7411 | | * @param ctxt a Relax-NG validation context |
7412 | | * @param err the error callback result |
7413 | | * @param warn the warning callback result |
7414 | | * @param ctx contextual data for the callbacks result |
7415 | | * @returns -1 in case of failure, 0 otherwise. |
7416 | | */ |
7417 | | int |
7418 | | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxt *ctxt, |
7419 | | xmlRelaxNGValidityErrorFunc * err, |
7420 | | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
7421 | 0 | { |
7422 | 0 | if (ctxt == NULL) |
7423 | 0 | return (-1); |
7424 | 0 | if (err != NULL) |
7425 | 0 | *err = ctxt->error; |
7426 | 0 | if (warn != NULL) |
7427 | 0 | *warn = ctxt->warning; |
7428 | 0 | if (ctx != NULL) |
7429 | 0 | *ctx = ctxt->userData; |
7430 | 0 | return (0); |
7431 | 0 | } |
7432 | | |
7433 | | /** |
7434 | | * Set the callback functions used to handle errors for a parsing context |
7435 | | * |
7436 | | * @param ctxt a Relax-NG parser context |
7437 | | * @param serror the error callback |
7438 | | * @param ctx contextual data for the callbacks |
7439 | | */ |
7440 | | void |
7441 | | xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxt *ctxt, |
7442 | | xmlStructuredErrorFunc serror, |
7443 | | void *ctx) |
7444 | 0 | { |
7445 | 0 | if (ctxt == NULL) |
7446 | 0 | return; |
7447 | 0 | ctxt->serror = serror; |
7448 | 0 | ctxt->error = NULL; |
7449 | 0 | ctxt->warning = NULL; |
7450 | 0 | ctxt->userData = ctx; |
7451 | 0 | } |
7452 | | |
7453 | | /** |
7454 | | * Set the callback function used to load external resources. |
7455 | | * |
7456 | | * @param ctxt a Relax-NG parser context |
7457 | | * @param loader the callback |
7458 | | * @param vctxt contextual data for the callbacks |
7459 | | */ |
7460 | | void |
7461 | | xmlRelaxNGSetResourceLoader(xmlRelaxNGParserCtxt *ctxt, |
7462 | 0 | xmlResourceLoader loader, void *vctxt) { |
7463 | 0 | if (ctxt == NULL) |
7464 | 0 | return; |
7465 | 0 | ctxt->resourceLoader = loader; |
7466 | 0 | ctxt->resourceCtxt = vctxt; |
7467 | 0 | } |
7468 | | |
7469 | | #ifdef LIBXML_DEBUG_ENABLED |
7470 | | |
7471 | | /************************************************************************ |
7472 | | * * |
7473 | | * Dump back a compiled form * |
7474 | | * * |
7475 | | ************************************************************************/ |
7476 | | static void xmlRelaxNGDumpDefine(FILE * output, |
7477 | | xmlRelaxNGDefinePtr define); |
7478 | | |
7479 | | /** |
7480 | | * Dump a RelaxNG structure back |
7481 | | * |
7482 | | * @param output the file output |
7483 | | * @param defines a list of define structures |
7484 | | */ |
7485 | | static void |
7486 | | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
7487 | | { |
7488 | | while (defines != NULL) { |
7489 | | xmlRelaxNGDumpDefine(output, defines); |
7490 | | defines = defines->next; |
7491 | | } |
7492 | | } |
7493 | | |
7494 | | /** |
7495 | | * Dump a RelaxNG structure back |
7496 | | * |
7497 | | * @param output the file output |
7498 | | * @param define a define structure |
7499 | | */ |
7500 | | static void |
7501 | | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
7502 | | { |
7503 | | if (define == NULL) |
7504 | | return; |
7505 | | switch (define->type) { |
7506 | | case XML_RELAXNG_EMPTY: |
7507 | | fprintf(output, "<empty/>\n"); |
7508 | | break; |
7509 | | case XML_RELAXNG_NOT_ALLOWED: |
7510 | | fprintf(output, "<notAllowed/>\n"); |
7511 | | break; |
7512 | | case XML_RELAXNG_TEXT: |
7513 | | fprintf(output, "<text/>\n"); |
7514 | | break; |
7515 | | case XML_RELAXNG_ELEMENT: |
7516 | | fprintf(output, "<element>\n"); |
7517 | | if (define->name != NULL) { |
7518 | | fprintf(output, "<name"); |
7519 | | if (define->ns != NULL) |
7520 | | fprintf(output, " ns=\"%s\"", define->ns); |
7521 | | fprintf(output, ">%s</name>\n", define->name); |
7522 | | } |
7523 | | xmlRelaxNGDumpDefines(output, define->attrs); |
7524 | | xmlRelaxNGDumpDefines(output, define->content); |
7525 | | fprintf(output, "</element>\n"); |
7526 | | break; |
7527 | | case XML_RELAXNG_LIST: |
7528 | | fprintf(output, "<list>\n"); |
7529 | | xmlRelaxNGDumpDefines(output, define->content); |
7530 | | fprintf(output, "</list>\n"); |
7531 | | break; |
7532 | | case XML_RELAXNG_ONEORMORE: |
7533 | | fprintf(output, "<oneOrMore>\n"); |
7534 | | xmlRelaxNGDumpDefines(output, define->content); |
7535 | | fprintf(output, "</oneOrMore>\n"); |
7536 | | break; |
7537 | | case XML_RELAXNG_ZEROORMORE: |
7538 | | fprintf(output, "<zeroOrMore>\n"); |
7539 | | xmlRelaxNGDumpDefines(output, define->content); |
7540 | | fprintf(output, "</zeroOrMore>\n"); |
7541 | | break; |
7542 | | case XML_RELAXNG_CHOICE: |
7543 | | fprintf(output, "<choice>\n"); |
7544 | | xmlRelaxNGDumpDefines(output, define->content); |
7545 | | fprintf(output, "</choice>\n"); |
7546 | | break; |
7547 | | case XML_RELAXNG_GROUP: |
7548 | | fprintf(output, "<group>\n"); |
7549 | | xmlRelaxNGDumpDefines(output, define->content); |
7550 | | fprintf(output, "</group>\n"); |
7551 | | break; |
7552 | | case XML_RELAXNG_INTERLEAVE: |
7553 | | fprintf(output, "<interleave>\n"); |
7554 | | xmlRelaxNGDumpDefines(output, define->content); |
7555 | | fprintf(output, "</interleave>\n"); |
7556 | | break; |
7557 | | case XML_RELAXNG_OPTIONAL: |
7558 | | fprintf(output, "<optional>\n"); |
7559 | | xmlRelaxNGDumpDefines(output, define->content); |
7560 | | fprintf(output, "</optional>\n"); |
7561 | | break; |
7562 | | case XML_RELAXNG_ATTRIBUTE: |
7563 | | fprintf(output, "<attribute>\n"); |
7564 | | xmlRelaxNGDumpDefines(output, define->content); |
7565 | | fprintf(output, "</attribute>\n"); |
7566 | | break; |
7567 | | case XML_RELAXNG_DEF: |
7568 | | fprintf(output, "<define"); |
7569 | | if (define->name != NULL) |
7570 | | fprintf(output, " name=\"%s\"", define->name); |
7571 | | fprintf(output, ">\n"); |
7572 | | xmlRelaxNGDumpDefines(output, define->content); |
7573 | | fprintf(output, "</define>\n"); |
7574 | | break; |
7575 | | case XML_RELAXNG_REF: |
7576 | | fprintf(output, "<ref"); |
7577 | | if (define->name != NULL) |
7578 | | fprintf(output, " name=\"%s\"", define->name); |
7579 | | fprintf(output, ">\n"); |
7580 | | xmlRelaxNGDumpDefines(output, define->content); |
7581 | | fprintf(output, "</ref>\n"); |
7582 | | break; |
7583 | | case XML_RELAXNG_PARENTREF: |
7584 | | fprintf(output, "<parentRef"); |
7585 | | if (define->name != NULL) |
7586 | | fprintf(output, " name=\"%s\"", define->name); |
7587 | | fprintf(output, ">\n"); |
7588 | | xmlRelaxNGDumpDefines(output, define->content); |
7589 | | fprintf(output, "</parentRef>\n"); |
7590 | | break; |
7591 | | case XML_RELAXNG_EXTERNALREF: |
7592 | | fprintf(output, "<externalRef>"); |
7593 | | xmlRelaxNGDumpDefines(output, define->content); |
7594 | | fprintf(output, "</externalRef>\n"); |
7595 | | break; |
7596 | | case XML_RELAXNG_DATATYPE: |
7597 | | case XML_RELAXNG_VALUE: |
7598 | | /* TODO */ |
7599 | | break; |
7600 | | case XML_RELAXNG_START: |
7601 | | case XML_RELAXNG_EXCEPT: |
7602 | | case XML_RELAXNG_PARAM: |
7603 | | /* TODO */ |
7604 | | break; |
7605 | | case XML_RELAXNG_NOOP: |
7606 | | xmlRelaxNGDumpDefines(output, define->content); |
7607 | | break; |
7608 | | } |
7609 | | } |
7610 | | |
7611 | | /** |
7612 | | * Dump a RelaxNG structure back |
7613 | | * |
7614 | | * @param output the file output |
7615 | | * @param grammar a grammar structure |
7616 | | * @param top is this a top grammar |
7617 | | */ |
7618 | | static void |
7619 | | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
7620 | | { |
7621 | | if (grammar == NULL) |
7622 | | return; |
7623 | | |
7624 | | fprintf(output, "<grammar"); |
7625 | | if (top) |
7626 | | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
7627 | | switch (grammar->combine) { |
7628 | | case XML_RELAXNG_COMBINE_UNDEFINED: |
7629 | | break; |
7630 | | case XML_RELAXNG_COMBINE_CHOICE: |
7631 | | fprintf(output, " combine=\"choice\""); |
7632 | | break; |
7633 | | case XML_RELAXNG_COMBINE_INTERLEAVE: |
7634 | | fprintf(output, " combine=\"interleave\""); |
7635 | | break; |
7636 | | default: |
7637 | | fprintf(output, " <!-- invalid combine value -->"); |
7638 | | } |
7639 | | fprintf(output, ">\n"); |
7640 | | if (grammar->start == NULL) { |
7641 | | fprintf(output, " <!-- grammar had no start -->"); |
7642 | | } else { |
7643 | | fprintf(output, "<start>\n"); |
7644 | | xmlRelaxNGDumpDefine(output, grammar->start); |
7645 | | fprintf(output, "</start>\n"); |
7646 | | } |
7647 | | /* TODO ? Dump the defines ? */ |
7648 | | fprintf(output, "</grammar>\n"); |
7649 | | } |
7650 | | |
7651 | | /** |
7652 | | * Dump a RelaxNG structure back |
7653 | | * |
7654 | | * @param output the file output |
7655 | | * @param schema a schema structure |
7656 | | */ |
7657 | | void |
7658 | | xmlRelaxNGDump(FILE * output, xmlRelaxNG *schema) |
7659 | | { |
7660 | | if (output == NULL) |
7661 | | return; |
7662 | | if (schema == NULL) { |
7663 | | fprintf(output, "RelaxNG empty or failed to compile\n"); |
7664 | | return; |
7665 | | } |
7666 | | fprintf(output, "RelaxNG: "); |
7667 | | if (schema->doc == NULL) { |
7668 | | fprintf(output, "no document\n"); |
7669 | | } else if (schema->doc->URL != NULL) { |
7670 | | fprintf(output, "%s\n", schema->doc->URL); |
7671 | | } else { |
7672 | | fprintf(output, "\n"); |
7673 | | } |
7674 | | if (schema->topgrammar == NULL) { |
7675 | | fprintf(output, "RelaxNG has no top grammar\n"); |
7676 | | return; |
7677 | | } |
7678 | | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
7679 | | } |
7680 | | #endif /* LIBXML_DEBUG_ENABLED */ |
7681 | | |
7682 | | #ifdef LIBXML_OUTPUT_ENABLED |
7683 | | /** |
7684 | | * Dump the transformed RelaxNG tree. |
7685 | | * |
7686 | | * @param output the file output |
7687 | | * @param schema a schema structure |
7688 | | */ |
7689 | | void |
7690 | | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNG *schema) |
7691 | 0 | { |
7692 | 0 | if (output == NULL) |
7693 | 0 | return; |
7694 | 0 | if (schema == NULL) { |
7695 | 0 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
7696 | 0 | return; |
7697 | 0 | } |
7698 | 0 | if (schema->doc == NULL) { |
7699 | 0 | fprintf(output, "no document\n"); |
7700 | 0 | } else { |
7701 | 0 | xmlDocDump(output, schema->doc); |
7702 | 0 | } |
7703 | 0 | } |
7704 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
7705 | | |
7706 | | /************************************************************************ |
7707 | | * * |
7708 | | * Validation of compiled content * |
7709 | | * * |
7710 | | ************************************************************************/ |
7711 | | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
7712 | | xmlRelaxNGDefinePtr define); |
7713 | | |
7714 | | /** |
7715 | | * Handle the callback and if needed validate the element children. |
7716 | | * |
7717 | | * @param exec the regular expression instance |
7718 | | * @param token the token which matched |
7719 | | * @param transdata callback data, the define for the subelement if available |
7720 | | * @param inputdata callback data, the Relax NG validation context |
7721 | | */ |
7722 | | static void |
7723 | | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
7724 | | const xmlChar * token, |
7725 | | void *transdata, void *inputdata) |
7726 | 0 | { |
7727 | 0 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
7728 | 0 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
7729 | 0 | int ret; |
7730 | |
|
7731 | 0 | if (ctxt == NULL) { |
7732 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7733 | 0 | "callback on %s missing context\n", token, NULL); |
7734 | 0 | return; |
7735 | 0 | } |
7736 | 0 | if (define == NULL) { |
7737 | 0 | if (token[0] == '#') |
7738 | 0 | return; |
7739 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7740 | 0 | "callback on %s missing define\n", token, NULL); |
7741 | 0 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
7742 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
7743 | 0 | return; |
7744 | 0 | } |
7745 | 0 | if (define->type != XML_RELAXNG_ELEMENT) { |
7746 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7747 | 0 | "callback on %s define is not element\n", token, NULL); |
7748 | 0 | if (ctxt->errNo == XML_RELAXNG_OK) |
7749 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
7750 | 0 | return; |
7751 | 0 | } |
7752 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
7753 | 0 | if (ret != 0) |
7754 | 0 | ctxt->perr = ret; |
7755 | 0 | } |
7756 | | |
7757 | | /** |
7758 | | * Validate the content model of an element or start using the regexp |
7759 | | * |
7760 | | * @param ctxt the RelaxNG validation context |
7761 | | * @param regexp the regular expression as compiled |
7762 | | * @param content list of children to test against the regexp |
7763 | | * @returns 0 in case of success, -1 in case of error. |
7764 | | */ |
7765 | | static int |
7766 | | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
7767 | | xmlRegexpPtr regexp, xmlNodePtr content) |
7768 | 0 | { |
7769 | 0 | xmlRegExecCtxtPtr exec; |
7770 | 0 | xmlNodePtr cur; |
7771 | 0 | int ret = 0; |
7772 | 0 | int oldperr; |
7773 | |
|
7774 | 0 | if ((ctxt == NULL) || (regexp == NULL)) |
7775 | 0 | return (-1); |
7776 | 0 | oldperr = ctxt->perr; |
7777 | 0 | exec = xmlRegNewExecCtxt(regexp, |
7778 | 0 | xmlRelaxNGValidateCompiledCallback, ctxt); |
7779 | 0 | ctxt->perr = 0; |
7780 | 0 | cur = content; |
7781 | 0 | while (cur != NULL) { |
7782 | 0 | ctxt->state->seq = cur; |
7783 | 0 | switch (cur->type) { |
7784 | 0 | case XML_TEXT_NODE: |
7785 | 0 | case XML_CDATA_SECTION_NODE: |
7786 | 0 | if (xmlIsBlankNode(cur)) |
7787 | 0 | break; |
7788 | 0 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
7789 | 0 | if (ret < 0) { |
7790 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
7791 | 0 | cur->parent->name); |
7792 | 0 | } |
7793 | 0 | break; |
7794 | 0 | case XML_ELEMENT_NODE: |
7795 | 0 | if (cur->ns != NULL) { |
7796 | 0 | ret = xmlRegExecPushString2(exec, cur->name, |
7797 | 0 | cur->ns->href, ctxt); |
7798 | 0 | } else { |
7799 | 0 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
7800 | 0 | } |
7801 | 0 | if (ret < 0) { |
7802 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
7803 | 0 | } |
7804 | 0 | break; |
7805 | 0 | default: |
7806 | 0 | break; |
7807 | 0 | } |
7808 | 0 | if (ret < 0) |
7809 | 0 | break; |
7810 | | /* |
7811 | | * Switch to next element |
7812 | | */ |
7813 | 0 | cur = cur->next; |
7814 | 0 | } |
7815 | 0 | ret = xmlRegExecPushString(exec, NULL, NULL); |
7816 | 0 | if (ret == 1) { |
7817 | 0 | ret = 0; |
7818 | 0 | ctxt->state->seq = NULL; |
7819 | 0 | } else if (ret == 0) { |
7820 | | /* |
7821 | | * TODO: get some of the names needed to exit the current state of exec |
7822 | | */ |
7823 | 0 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
7824 | 0 | ret = -1; |
7825 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
7826 | 0 | xmlRelaxNGDumpValidError(ctxt); |
7827 | 0 | } else { |
7828 | 0 | ret = -1; |
7829 | 0 | } |
7830 | 0 | xmlRegFreeExecCtxt(exec); |
7831 | | /* |
7832 | | * There might be content model errors outside of the pure |
7833 | | * regexp validation, e.g. for attribute values. |
7834 | | */ |
7835 | 0 | if ((ret == 0) && (ctxt->perr != 0)) { |
7836 | 0 | ret = ctxt->perr; |
7837 | 0 | } |
7838 | 0 | ctxt->perr = oldperr; |
7839 | 0 | return (ret); |
7840 | 0 | } |
7841 | | |
7842 | | /************************************************************************ |
7843 | | * * |
7844 | | * Progressive validation of when possible * |
7845 | | * * |
7846 | | ************************************************************************/ |
7847 | | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
7848 | | xmlRelaxNGDefinePtr defines); |
7849 | | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
7850 | | int dolog); |
7851 | | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
7852 | | |
7853 | | /** |
7854 | | * Push a new regexp for the current node content model on the stack |
7855 | | * |
7856 | | * @param ctxt the validation context |
7857 | | * @param exec the regexp runtime for the new content model |
7858 | | * @returns 0 in case of success and -1 in case of error. |
7859 | | */ |
7860 | | static int |
7861 | | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
7862 | 0 | { |
7863 | 0 | if (ctxt->elemTab == NULL) { |
7864 | 0 | ctxt->elemMax = 10; |
7865 | 0 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
7866 | 0 | sizeof |
7867 | 0 | (xmlRegExecCtxtPtr)); |
7868 | 0 | if (ctxt->elemTab == NULL) { |
7869 | 0 | xmlRngVErrMemory(ctxt); |
7870 | 0 | return (-1); |
7871 | 0 | } |
7872 | 0 | } |
7873 | 0 | if (ctxt->elemNr >= ctxt->elemMax) { |
7874 | 0 | ctxt->elemMax *= 2; |
7875 | 0 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
7876 | 0 | ctxt->elemMax * |
7877 | 0 | sizeof |
7878 | 0 | (xmlRegExecCtxtPtr)); |
7879 | 0 | if (ctxt->elemTab == NULL) { |
7880 | 0 | xmlRngVErrMemory(ctxt); |
7881 | 0 | return (-1); |
7882 | 0 | } |
7883 | 0 | } |
7884 | 0 | ctxt->elemTab[ctxt->elemNr++] = exec; |
7885 | 0 | ctxt->elem = exec; |
7886 | 0 | return (0); |
7887 | 0 | } |
7888 | | |
7889 | | /** |
7890 | | * Pop the regexp of the current node content model from the stack |
7891 | | * |
7892 | | * @param ctxt the validation context |
7893 | | * @returns the exec or NULL if empty |
7894 | | */ |
7895 | | static xmlRegExecCtxtPtr |
7896 | | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
7897 | 0 | { |
7898 | 0 | xmlRegExecCtxtPtr ret; |
7899 | |
|
7900 | 0 | if (ctxt->elemNr <= 0) |
7901 | 0 | return (NULL); |
7902 | 0 | ctxt->elemNr--; |
7903 | 0 | ret = ctxt->elemTab[ctxt->elemNr]; |
7904 | 0 | ctxt->elemTab[ctxt->elemNr] = NULL; |
7905 | 0 | if (ctxt->elemNr > 0) |
7906 | 0 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
7907 | 0 | else |
7908 | 0 | ctxt->elem = NULL; |
7909 | 0 | return (ret); |
7910 | 0 | } |
7911 | | |
7912 | | /** |
7913 | | * Handle the callback and if needed validate the element children. |
7914 | | * some of the in/out information are passed via the context in `inputdata`. |
7915 | | * |
7916 | | * @param exec the regular expression instance |
7917 | | * @param token the token which matched |
7918 | | * @param transdata callback data, the define for the subelement if available |
7919 | | * @param inputdata callback data, the Relax NG validation context |
7920 | | */ |
7921 | | static void |
7922 | | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
7923 | | ATTRIBUTE_UNUSED, |
7924 | | const xmlChar * token, |
7925 | | void *transdata, void *inputdata) |
7926 | 0 | { |
7927 | 0 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
7928 | 0 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
7929 | 0 | xmlRelaxNGValidStatePtr state, oldstate; |
7930 | 0 | xmlNodePtr node; |
7931 | 0 | int ret = 0, oldflags; |
7932 | |
|
7933 | 0 | if (ctxt == NULL) { |
7934 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7935 | 0 | "callback on %s missing context\n", token, NULL); |
7936 | 0 | return; |
7937 | 0 | } |
7938 | 0 | node = ctxt->pnode; |
7939 | 0 | ctxt->pstate = 1; |
7940 | 0 | if (define == NULL) { |
7941 | 0 | if (token[0] == '#') |
7942 | 0 | return; |
7943 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7944 | 0 | "callback on %s missing define\n", token, NULL); |
7945 | 0 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
7946 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
7947 | 0 | ctxt->pstate = -1; |
7948 | 0 | return; |
7949 | 0 | } |
7950 | 0 | if (define->type != XML_RELAXNG_ELEMENT) { |
7951 | 0 | xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
7952 | 0 | "callback on %s define is not element\n", token, NULL); |
7953 | 0 | if (ctxt->errNo == XML_RELAXNG_OK) |
7954 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
7955 | 0 | ctxt->pstate = -1; |
7956 | 0 | return; |
7957 | 0 | } |
7958 | 0 | if (node->type != XML_ELEMENT_NODE) { |
7959 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
7960 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
7961 | 0 | xmlRelaxNGDumpValidError(ctxt); |
7962 | 0 | ctxt->pstate = -1; |
7963 | 0 | return; |
7964 | 0 | } |
7965 | 0 | if (define->contModel == NULL) { |
7966 | | /* |
7967 | | * this node cannot be validated in a streamable fashion |
7968 | | */ |
7969 | 0 | ctxt->pstate = 0; |
7970 | 0 | ctxt->pdef = define; |
7971 | 0 | return; |
7972 | 0 | } |
7973 | 0 | exec = xmlRegNewExecCtxt(define->contModel, |
7974 | 0 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
7975 | 0 | if (exec == NULL) { |
7976 | 0 | ctxt->pstate = -1; |
7977 | 0 | return; |
7978 | 0 | } |
7979 | 0 | xmlRelaxNGElemPush(ctxt, exec); |
7980 | | |
7981 | | /* |
7982 | | * Validate the attributes part of the content. |
7983 | | */ |
7984 | 0 | state = xmlRelaxNGNewValidState(ctxt, node); |
7985 | 0 | if (state == NULL) { |
7986 | 0 | ctxt->pstate = -1; |
7987 | 0 | return; |
7988 | 0 | } |
7989 | 0 | oldstate = ctxt->state; |
7990 | 0 | ctxt->state = state; |
7991 | 0 | if (define->attrs != NULL) { |
7992 | 0 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
7993 | 0 | if (ret != 0) { |
7994 | 0 | ctxt->pstate = -1; |
7995 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
7996 | 0 | } |
7997 | 0 | } |
7998 | 0 | if (ctxt->state != NULL) { |
7999 | 0 | ctxt->state->seq = NULL; |
8000 | 0 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
8001 | 0 | if (ret != 0) { |
8002 | 0 | ctxt->pstate = -1; |
8003 | 0 | } |
8004 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
8005 | 0 | } else if (ctxt->states != NULL) { |
8006 | 0 | int tmp = -1, i; |
8007 | |
|
8008 | 0 | oldflags = ctxt->flags; |
8009 | |
|
8010 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
8011 | 0 | state = ctxt->states->tabState[i]; |
8012 | 0 | ctxt->state = state; |
8013 | 0 | ctxt->state->seq = NULL; |
8014 | |
|
8015 | 0 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
8016 | 0 | tmp = 0; |
8017 | 0 | break; |
8018 | 0 | } |
8019 | 0 | } |
8020 | 0 | if (tmp != 0) { |
8021 | | /* |
8022 | | * validation error, log the message for the "best" one |
8023 | | */ |
8024 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8025 | 0 | xmlRelaxNGLogBestError(ctxt); |
8026 | 0 | } |
8027 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
8028 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
8029 | 0 | } |
8030 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
8031 | 0 | ctxt->states = NULL; |
8032 | 0 | if ((ret == 0) && (tmp == -1)) |
8033 | 0 | ctxt->pstate = -1; |
8034 | 0 | ctxt->flags = oldflags; |
8035 | 0 | } |
8036 | 0 | if (ctxt->pstate == -1) { |
8037 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
8038 | 0 | xmlRelaxNGDumpValidError(ctxt); |
8039 | 0 | } |
8040 | 0 | } |
8041 | 0 | ctxt->state = oldstate; |
8042 | 0 | } |
8043 | | |
8044 | | /** |
8045 | | * Push a new element start on the RelaxNG validation stack. |
8046 | | * |
8047 | | * @param ctxt the validation context |
8048 | | * @param doc a document instance |
8049 | | * @param elem an element instance |
8050 | | * @returns 1 if no validation problem was found or 0 if validating the |
8051 | | * element requires a full node, and -1 in case of error. |
8052 | | */ |
8053 | | int |
8054 | | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxt *ctxt, |
8055 | | xmlDoc *doc ATTRIBUTE_UNUSED, |
8056 | | xmlNode *elem) |
8057 | 0 | { |
8058 | 0 | int ret = 1; |
8059 | |
|
8060 | 0 | if ((ctxt == NULL) || (elem == NULL)) |
8061 | 0 | return (-1); |
8062 | | |
8063 | 0 | if (ctxt->elem == 0) { |
8064 | 0 | xmlRelaxNGPtr schema; |
8065 | 0 | xmlRelaxNGGrammarPtr grammar; |
8066 | 0 | xmlRegExecCtxtPtr exec; |
8067 | 0 | xmlRelaxNGDefinePtr define; |
8068 | |
|
8069 | 0 | schema = ctxt->schema; |
8070 | 0 | if (schema == NULL) { |
8071 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
8072 | 0 | return (-1); |
8073 | 0 | } |
8074 | 0 | grammar = schema->topgrammar; |
8075 | 0 | if ((grammar == NULL) || (grammar->start == NULL)) { |
8076 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
8077 | 0 | return (-1); |
8078 | 0 | } |
8079 | 0 | define = grammar->start; |
8080 | 0 | if (define->contModel == NULL) { |
8081 | 0 | ctxt->pdef = define; |
8082 | 0 | return (0); |
8083 | 0 | } |
8084 | 0 | exec = xmlRegNewExecCtxt(define->contModel, |
8085 | 0 | xmlRelaxNGValidateProgressiveCallback, |
8086 | 0 | ctxt); |
8087 | 0 | if (exec == NULL) { |
8088 | 0 | return (-1); |
8089 | 0 | } |
8090 | 0 | xmlRelaxNGElemPush(ctxt, exec); |
8091 | 0 | } |
8092 | 0 | ctxt->pnode = elem; |
8093 | 0 | ctxt->pstate = 0; |
8094 | 0 | if (elem->ns != NULL) { |
8095 | 0 | ret = |
8096 | 0 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
8097 | 0 | ctxt); |
8098 | 0 | } else { |
8099 | 0 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
8100 | 0 | } |
8101 | 0 | if (ret < 0) { |
8102 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
8103 | 0 | } else { |
8104 | 0 | if (ctxt->pstate == 0) |
8105 | 0 | ret = 0; |
8106 | 0 | else if (ctxt->pstate < 0) |
8107 | 0 | ret = -1; |
8108 | 0 | else |
8109 | 0 | ret = 1; |
8110 | 0 | } |
8111 | 0 | return (ret); |
8112 | 0 | } |
8113 | | |
8114 | | /** |
8115 | | * check the CData parsed for validation in the current stack |
8116 | | * |
8117 | | * @param ctxt the RelaxNG validation context |
8118 | | * @param data some character data read |
8119 | | * @param len the length of the data |
8120 | | * @returns 1 if no validation problem was found or -1 otherwise |
8121 | | */ |
8122 | | int |
8123 | | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxt *ctxt, |
8124 | | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
8125 | 0 | { |
8126 | 0 | int ret = 1; |
8127 | |
|
8128 | 0 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
8129 | 0 | return (-1); |
8130 | | |
8131 | 0 | while (*data != 0) { |
8132 | 0 | if (!IS_BLANK_CH(*data)) |
8133 | 0 | break; |
8134 | 0 | data++; |
8135 | 0 | } |
8136 | 0 | if (*data == 0) |
8137 | 0 | return (1); |
8138 | | |
8139 | 0 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
8140 | 0 | if (ret < 0) { |
8141 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
8142 | |
|
8143 | 0 | return (-1); |
8144 | 0 | } |
8145 | 0 | return (1); |
8146 | 0 | } |
8147 | | |
8148 | | /** |
8149 | | * Pop the element end from the RelaxNG validation stack. |
8150 | | * |
8151 | | * @param ctxt the RelaxNG validation context |
8152 | | * @param doc a document instance |
8153 | | * @param elem an element instance |
8154 | | * @returns 1 if no validation problem was found or 0 otherwise |
8155 | | */ |
8156 | | int |
8157 | | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxt *ctxt, |
8158 | | xmlDoc *doc ATTRIBUTE_UNUSED, |
8159 | | xmlNode *elem) |
8160 | 0 | { |
8161 | 0 | int ret; |
8162 | 0 | xmlRegExecCtxtPtr exec; |
8163 | |
|
8164 | 0 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
8165 | 0 | return (-1); |
8166 | | /* |
8167 | | * verify that we reached a terminal state of the content model. |
8168 | | */ |
8169 | 0 | exec = xmlRelaxNGElemPop(ctxt); |
8170 | 0 | ret = xmlRegExecPushString(exec, NULL, NULL); |
8171 | 0 | if (ret == 0) { |
8172 | | /* |
8173 | | * TODO: get some of the names needed to exit the current state of exec |
8174 | | */ |
8175 | 0 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
8176 | 0 | ret = -1; |
8177 | 0 | } else if (ret < 0) { |
8178 | 0 | ret = -1; |
8179 | 0 | } else { |
8180 | 0 | ret = 1; |
8181 | 0 | } |
8182 | 0 | xmlRegFreeExecCtxt(exec); |
8183 | 0 | return (ret); |
8184 | 0 | } |
8185 | | |
8186 | | /** |
8187 | | * Validate a full subtree when #xmlRelaxNGValidatePushElement returned |
8188 | | * 0 and the content of the node has been expanded. |
8189 | | * |
8190 | | * @param ctxt the validation context |
8191 | | * @param doc a document instance |
8192 | | * @param elem an element instance |
8193 | | * @returns 1 if no validation problem was found or -1 in case of error. |
8194 | | */ |
8195 | | int |
8196 | | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxt *ctxt, |
8197 | | xmlDoc *doc ATTRIBUTE_UNUSED, |
8198 | | xmlNode *elem) |
8199 | 0 | { |
8200 | 0 | int ret; |
8201 | 0 | xmlRelaxNGValidStatePtr state; |
8202 | |
|
8203 | 0 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
8204 | 0 | return (-1); |
8205 | 0 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
8206 | 0 | if (state == NULL) { |
8207 | 0 | return (-1); |
8208 | 0 | } |
8209 | 0 | state->seq = elem; |
8210 | 0 | ctxt->state = state; |
8211 | 0 | ctxt->errNo = XML_RELAXNG_OK; |
8212 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
8213 | 0 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
8214 | 0 | ret = -1; |
8215 | 0 | else |
8216 | 0 | ret = 1; |
8217 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
8218 | 0 | ctxt->state = NULL; |
8219 | 0 | return (ret); |
8220 | 0 | } |
8221 | | |
8222 | | /************************************************************************ |
8223 | | * * |
8224 | | * Generic interpreted validation implementation * |
8225 | | * * |
8226 | | ************************************************************************/ |
8227 | | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
8228 | | xmlRelaxNGDefinePtr define); |
8229 | | |
8230 | | /** |
8231 | | * Skip ignorable nodes in that context |
8232 | | * |
8233 | | * @param ctxt a schema validation context |
8234 | | * @param node the top node. |
8235 | | * @returns the new sibling or NULL in case of error. |
8236 | | */ |
8237 | | static xmlNodePtr |
8238 | | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
8239 | | xmlNodePtr node) |
8240 | 0 | { |
8241 | | /* |
8242 | | * TODO complete and handle entities |
8243 | | */ |
8244 | 0 | while ((node != NULL) && |
8245 | 0 | ((node->type == XML_COMMENT_NODE) || |
8246 | 0 | (node->type == XML_PI_NODE) || |
8247 | 0 | (node->type == XML_XINCLUDE_START) || |
8248 | 0 | (node->type == XML_XINCLUDE_END) || |
8249 | 0 | (((node->type == XML_TEXT_NODE) || |
8250 | 0 | (node->type == XML_CDATA_SECTION_NODE)) && |
8251 | 0 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
8252 | 0 | (IS_BLANK_NODE(node)))))) { |
8253 | 0 | node = node->next; |
8254 | 0 | } |
8255 | 0 | return (node); |
8256 | 0 | } |
8257 | | |
8258 | | /** |
8259 | | * Implements the normalizeWhiteSpace( s ) function from |
8260 | | * section 6.2.9 of the spec |
8261 | | * |
8262 | | * @param ctxt a schema validation context |
8263 | | * @param str the string to normalize |
8264 | | * @returns the new string or NULL in case of error. |
8265 | | */ |
8266 | | static xmlChar * |
8267 | | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
8268 | 0 | { |
8269 | 0 | xmlChar *ret, *p; |
8270 | 0 | const xmlChar *tmp; |
8271 | 0 | int len; |
8272 | |
|
8273 | 0 | if (str == NULL) |
8274 | 0 | return (NULL); |
8275 | 0 | tmp = str; |
8276 | 0 | while (*tmp != 0) |
8277 | 0 | tmp++; |
8278 | 0 | len = tmp - str; |
8279 | |
|
8280 | 0 | ret = xmlMalloc(len + 1); |
8281 | 0 | if (ret == NULL) { |
8282 | 0 | xmlRngVErrMemory(ctxt); |
8283 | 0 | return (NULL); |
8284 | 0 | } |
8285 | 0 | p = ret; |
8286 | 0 | while (IS_BLANK_CH(*str)) |
8287 | 0 | str++; |
8288 | 0 | while (*str != 0) { |
8289 | 0 | if (IS_BLANK_CH(*str)) { |
8290 | 0 | while (IS_BLANK_CH(*str)) |
8291 | 0 | str++; |
8292 | 0 | if (*str == 0) |
8293 | 0 | break; |
8294 | 0 | *p++ = ' '; |
8295 | 0 | } else |
8296 | 0 | *p++ = *str++; |
8297 | 0 | } |
8298 | 0 | *p = 0; |
8299 | 0 | return (ret); |
8300 | 0 | } |
8301 | | |
8302 | | /** |
8303 | | * Validate the given value against the datatype |
8304 | | * |
8305 | | * @param ctxt a Relax-NG validation context |
8306 | | * @param value the string value |
8307 | | * @param define the datatype definition |
8308 | | * @param node the node |
8309 | | * @returns 0 if the validation succeeded or an error code. |
8310 | | */ |
8311 | | static int |
8312 | | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
8313 | | const xmlChar * value, |
8314 | | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
8315 | 0 | { |
8316 | 0 | int ret, tmp; |
8317 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
8318 | 0 | void *result = NULL; |
8319 | 0 | xmlRelaxNGDefinePtr cur; |
8320 | |
|
8321 | 0 | if ((define == NULL) || (define->data == NULL)) { |
8322 | 0 | return (-1); |
8323 | 0 | } |
8324 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
8325 | 0 | if (lib->check != NULL) { |
8326 | 0 | if ((define->attrs != NULL) && |
8327 | 0 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
8328 | 0 | ret = |
8329 | 0 | lib->check(lib->data, define->name, value, &result, node); |
8330 | 0 | } else { |
8331 | 0 | ret = lib->check(lib->data, define->name, value, NULL, node); |
8332 | 0 | } |
8333 | 0 | } else |
8334 | 0 | ret = -1; |
8335 | 0 | if (ret < 0) { |
8336 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
8337 | 0 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
8338 | 0 | lib->freef(lib->data, result); |
8339 | 0 | return (-1); |
8340 | 0 | } else if (ret == 1) { |
8341 | 0 | ret = 0; |
8342 | 0 | } else if (ret == 2) { |
8343 | 0 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
8344 | 0 | } else { |
8345 | 0 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
8346 | 0 | ret = -1; |
8347 | 0 | } |
8348 | 0 | cur = define->attrs; |
8349 | 0 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
8350 | 0 | if (lib->facet != NULL) { |
8351 | 0 | tmp = lib->facet(lib->data, define->name, cur->name, |
8352 | 0 | cur->value, value, result); |
8353 | 0 | if (tmp != 0) |
8354 | 0 | ret = -1; |
8355 | 0 | } |
8356 | 0 | cur = cur->next; |
8357 | 0 | } |
8358 | 0 | if ((ret == 0) && (define->content != NULL)) { |
8359 | 0 | const xmlChar *oldvalue, *oldendvalue; |
8360 | |
|
8361 | 0 | oldvalue = ctxt->state->value; |
8362 | 0 | oldendvalue = ctxt->state->endvalue; |
8363 | 0 | ctxt->state->value = (xmlChar *) value; |
8364 | 0 | ctxt->state->endvalue = NULL; |
8365 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
8366 | 0 | ctxt->state->value = (xmlChar *) oldvalue; |
8367 | 0 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
8368 | 0 | } |
8369 | 0 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
8370 | 0 | lib->freef(lib->data, result); |
8371 | 0 | return (ret); |
8372 | 0 | } |
8373 | | |
8374 | | /** |
8375 | | * Skip to the next value when validating within a list |
8376 | | * |
8377 | | * @param ctxt a Relax-NG validation context |
8378 | | * @returns 0 if the operation succeeded or an error code. |
8379 | | */ |
8380 | | static int |
8381 | | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
8382 | 0 | { |
8383 | 0 | xmlChar *cur; |
8384 | |
|
8385 | 0 | cur = ctxt->state->value; |
8386 | 0 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
8387 | 0 | ctxt->state->value = NULL; |
8388 | 0 | ctxt->state->endvalue = NULL; |
8389 | 0 | return (0); |
8390 | 0 | } |
8391 | 0 | while (*cur != 0) |
8392 | 0 | cur++; |
8393 | 0 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
8394 | 0 | cur++; |
8395 | 0 | if (cur == ctxt->state->endvalue) |
8396 | 0 | ctxt->state->value = NULL; |
8397 | 0 | else |
8398 | 0 | ctxt->state->value = cur; |
8399 | 0 | return (0); |
8400 | 0 | } |
8401 | | |
8402 | | /** |
8403 | | * Validate the given set of definitions for the current value |
8404 | | * |
8405 | | * @param ctxt a Relax-NG validation context |
8406 | | * @param defines the list of definitions to verify |
8407 | | * @returns 0 if the validation succeeded or an error code. |
8408 | | */ |
8409 | | static int |
8410 | | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
8411 | | xmlRelaxNGDefinePtr defines) |
8412 | 0 | { |
8413 | 0 | int ret = 0; |
8414 | |
|
8415 | 0 | while (defines != NULL) { |
8416 | 0 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
8417 | 0 | if (ret != 0) |
8418 | 0 | break; |
8419 | 0 | defines = defines->next; |
8420 | 0 | } |
8421 | 0 | return (ret); |
8422 | 0 | } |
8423 | | |
8424 | | /** |
8425 | | * Validate the given definition for the current value |
8426 | | * |
8427 | | * @param ctxt a Relax-NG validation context |
8428 | | * @param define the definition to verify |
8429 | | * @returns 0 if the validation succeeded or an error code. |
8430 | | */ |
8431 | | static int |
8432 | | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
8433 | | xmlRelaxNGDefinePtr define) |
8434 | 0 | { |
8435 | 0 | int ret = 0, oldflags; |
8436 | 0 | xmlChar *value; |
8437 | |
|
8438 | 0 | value = ctxt->state->value; |
8439 | 0 | switch (define->type) { |
8440 | 0 | case XML_RELAXNG_EMPTY:{ |
8441 | 0 | if ((value != NULL) && (value[0] != 0)) { |
8442 | 0 | int idx = 0; |
8443 | |
|
8444 | 0 | while (IS_BLANK_CH(value[idx])) |
8445 | 0 | idx++; |
8446 | 0 | if (value[idx] != 0) |
8447 | 0 | ret = -1; |
8448 | 0 | } |
8449 | 0 | break; |
8450 | 0 | } |
8451 | 0 | case XML_RELAXNG_TEXT: |
8452 | 0 | break; |
8453 | 0 | case XML_RELAXNG_VALUE:{ |
8454 | 0 | if (!xmlStrEqual(value, define->value)) { |
8455 | 0 | if (define->name != NULL) { |
8456 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
8457 | |
|
8458 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
8459 | 0 | if ((lib != NULL) && (lib->comp != NULL)) { |
8460 | 0 | ret = lib->comp(lib->data, define->name, |
8461 | 0 | define->value, define->node, |
8462 | 0 | (void *) define->attrs, |
8463 | 0 | value, ctxt->state->node); |
8464 | 0 | } else |
8465 | 0 | ret = -1; |
8466 | 0 | if (ret < 0) { |
8467 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
8468 | 0 | define->name); |
8469 | 0 | return (-1); |
8470 | 0 | } else if (ret == 1) { |
8471 | 0 | ret = 0; |
8472 | 0 | } else { |
8473 | 0 | ret = -1; |
8474 | 0 | } |
8475 | 0 | } else { |
8476 | 0 | xmlChar *nval, *nvalue; |
8477 | | |
8478 | | /* |
8479 | | * TODO: trivial optimizations are possible by |
8480 | | * computing at compile-time |
8481 | | */ |
8482 | 0 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
8483 | 0 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
8484 | |
|
8485 | 0 | if ((nval == NULL) || (nvalue == NULL) || |
8486 | 0 | (!xmlStrEqual(nval, nvalue))) |
8487 | 0 | ret = -1; |
8488 | 0 | if (nval != NULL) |
8489 | 0 | xmlFree(nval); |
8490 | 0 | if (nvalue != NULL) |
8491 | 0 | xmlFree(nvalue); |
8492 | 0 | } |
8493 | 0 | } |
8494 | 0 | if (ret == 0) |
8495 | 0 | xmlRelaxNGNextValue(ctxt); |
8496 | 0 | break; |
8497 | 0 | } |
8498 | 0 | case XML_RELAXNG_DATATYPE:{ |
8499 | 0 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
8500 | 0 | ctxt->state->seq); |
8501 | 0 | if (ret == 0) |
8502 | 0 | xmlRelaxNGNextValue(ctxt); |
8503 | |
|
8504 | 0 | break; |
8505 | 0 | } |
8506 | 0 | case XML_RELAXNG_CHOICE:{ |
8507 | 0 | xmlRelaxNGDefinePtr list = define->content; |
8508 | 0 | xmlChar *oldvalue; |
8509 | |
|
8510 | 0 | oldflags = ctxt->flags; |
8511 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8512 | |
|
8513 | 0 | oldvalue = ctxt->state->value; |
8514 | 0 | while (list != NULL) { |
8515 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8516 | 0 | if (ret == 0) { |
8517 | 0 | break; |
8518 | 0 | } |
8519 | 0 | ctxt->state->value = oldvalue; |
8520 | 0 | list = list->next; |
8521 | 0 | } |
8522 | 0 | ctxt->flags = oldflags; |
8523 | 0 | if (ret != 0) { |
8524 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
8525 | 0 | xmlRelaxNGDumpValidError(ctxt); |
8526 | 0 | } else { |
8527 | 0 | if (ctxt->errNr > 0) |
8528 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8529 | 0 | } |
8530 | 0 | break; |
8531 | 0 | } |
8532 | 0 | case XML_RELAXNG_LIST:{ |
8533 | 0 | xmlRelaxNGDefinePtr list = define->content; |
8534 | 0 | xmlChar *oldvalue, *oldend, *val, *cur; |
8535 | |
|
8536 | 0 | oldvalue = ctxt->state->value; |
8537 | 0 | oldend = ctxt->state->endvalue; |
8538 | |
|
8539 | 0 | val = xmlStrdup(oldvalue); |
8540 | 0 | if (val == NULL) { |
8541 | 0 | val = xmlStrdup(BAD_CAST ""); |
8542 | 0 | } |
8543 | 0 | if (val == NULL) { |
8544 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
8545 | 0 | return (-1); |
8546 | 0 | } |
8547 | 0 | cur = val; |
8548 | 0 | while (*cur != 0) { |
8549 | 0 | if (IS_BLANK_CH(*cur)) { |
8550 | 0 | *cur = 0; |
8551 | 0 | cur++; |
8552 | 0 | while (IS_BLANK_CH(*cur)) |
8553 | 0 | *cur++ = 0; |
8554 | 0 | } else |
8555 | 0 | cur++; |
8556 | 0 | } |
8557 | 0 | ctxt->state->endvalue = cur; |
8558 | 0 | cur = val; |
8559 | 0 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
8560 | 0 | cur++; |
8561 | |
|
8562 | 0 | ctxt->state->value = cur; |
8563 | |
|
8564 | 0 | while (list != NULL) { |
8565 | 0 | if (ctxt->state->value == ctxt->state->endvalue) |
8566 | 0 | ctxt->state->value = NULL; |
8567 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8568 | 0 | if (ret != 0) { |
8569 | 0 | break; |
8570 | 0 | } |
8571 | 0 | list = list->next; |
8572 | 0 | } |
8573 | |
|
8574 | 0 | if ((ret == 0) && (ctxt->state->value != NULL) && |
8575 | 0 | (ctxt->state->value != ctxt->state->endvalue)) { |
8576 | 0 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
8577 | 0 | ctxt->state->value); |
8578 | 0 | ret = -1; |
8579 | 0 | } |
8580 | 0 | xmlFree(val); |
8581 | 0 | ctxt->state->value = oldvalue; |
8582 | 0 | ctxt->state->endvalue = oldend; |
8583 | 0 | break; |
8584 | 0 | } |
8585 | 0 | case XML_RELAXNG_ONEORMORE: |
8586 | 0 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
8587 | 0 | if (ret != 0) { |
8588 | 0 | break; |
8589 | 0 | } |
8590 | | /* Falls through. */ |
8591 | 0 | case XML_RELAXNG_ZEROORMORE:{ |
8592 | 0 | xmlChar *cur, *temp; |
8593 | |
|
8594 | 0 | if ((ctxt->state->value == NULL) || |
8595 | 0 | (*ctxt->state->value == 0)) { |
8596 | 0 | ret = 0; |
8597 | 0 | break; |
8598 | 0 | } |
8599 | 0 | oldflags = ctxt->flags; |
8600 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8601 | 0 | cur = ctxt->state->value; |
8602 | 0 | temp = NULL; |
8603 | 0 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
8604 | 0 | (temp != cur)) { |
8605 | 0 | temp = cur; |
8606 | 0 | ret = |
8607 | 0 | xmlRelaxNGValidateValueList(ctxt, define->content); |
8608 | 0 | if (ret != 0) { |
8609 | 0 | ctxt->state->value = temp; |
8610 | 0 | ret = 0; |
8611 | 0 | break; |
8612 | 0 | } |
8613 | 0 | cur = ctxt->state->value; |
8614 | 0 | } |
8615 | 0 | ctxt->flags = oldflags; |
8616 | 0 | if (ctxt->errNr > 0) |
8617 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8618 | 0 | break; |
8619 | 0 | } |
8620 | 0 | case XML_RELAXNG_OPTIONAL:{ |
8621 | 0 | xmlChar *temp; |
8622 | |
|
8623 | 0 | if ((ctxt->state->value == NULL) || |
8624 | 0 | (*ctxt->state->value == 0)) { |
8625 | 0 | ret = 0; |
8626 | 0 | break; |
8627 | 0 | } |
8628 | 0 | oldflags = ctxt->flags; |
8629 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8630 | 0 | temp = ctxt->state->value; |
8631 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
8632 | 0 | ctxt->flags = oldflags; |
8633 | 0 | if (ret != 0) { |
8634 | 0 | ctxt->state->value = temp; |
8635 | 0 | if (ctxt->errNr > 0) |
8636 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8637 | 0 | ret = 0; |
8638 | 0 | break; |
8639 | 0 | } |
8640 | 0 | if (ctxt->errNr > 0) |
8641 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8642 | 0 | break; |
8643 | 0 | } |
8644 | 0 | case XML_RELAXNG_EXCEPT:{ |
8645 | 0 | xmlRelaxNGDefinePtr list; |
8646 | |
|
8647 | 0 | list = define->content; |
8648 | 0 | while (list != NULL) { |
8649 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8650 | 0 | if (ret == 0) { |
8651 | 0 | ret = -1; |
8652 | 0 | break; |
8653 | 0 | } else |
8654 | 0 | ret = 0; |
8655 | 0 | list = list->next; |
8656 | 0 | } |
8657 | 0 | break; |
8658 | 0 | } |
8659 | 0 | case XML_RELAXNG_DEF: |
8660 | 0 | case XML_RELAXNG_GROUP:{ |
8661 | 0 | xmlRelaxNGDefinePtr list; |
8662 | |
|
8663 | 0 | list = define->content; |
8664 | 0 | while (list != NULL) { |
8665 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8666 | 0 | if (ret != 0) { |
8667 | 0 | ret = -1; |
8668 | 0 | break; |
8669 | 0 | } else |
8670 | 0 | ret = 0; |
8671 | 0 | list = list->next; |
8672 | 0 | } |
8673 | 0 | break; |
8674 | 0 | } |
8675 | 0 | case XML_RELAXNG_REF: |
8676 | 0 | case XML_RELAXNG_PARENTREF: |
8677 | 0 | if (define->content == NULL) { |
8678 | 0 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
8679 | 0 | ret = -1; |
8680 | 0 | } else { |
8681 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
8682 | 0 | } |
8683 | 0 | break; |
8684 | 0 | default: |
8685 | | /* TODO */ |
8686 | 0 | ret = -1; |
8687 | 0 | } |
8688 | 0 | return (ret); |
8689 | 0 | } |
8690 | | |
8691 | | /** |
8692 | | * Validate the given definitions for the current value |
8693 | | * |
8694 | | * @param ctxt a Relax-NG validation context |
8695 | | * @param defines the list of definitions to verify |
8696 | | * @returns 0 if the validation succeeded or an error code. |
8697 | | */ |
8698 | | static int |
8699 | | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
8700 | | xmlRelaxNGDefinePtr defines) |
8701 | 0 | { |
8702 | 0 | int ret = 0; |
8703 | |
|
8704 | 0 | while (defines != NULL) { |
8705 | 0 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
8706 | 0 | if (ret != 0) |
8707 | 0 | break; |
8708 | 0 | defines = defines->next; |
8709 | 0 | } |
8710 | 0 | return (ret); |
8711 | 0 | } |
8712 | | |
8713 | | /** |
8714 | | * Check if the attribute matches the definition nameClass |
8715 | | * |
8716 | | * @param ctxt a Relax-NG validation context |
8717 | | * @param define the definition to check |
8718 | | * @param prop the attribute |
8719 | | * @returns 1 if the attribute matches, 0 if no, or -1 in case of error |
8720 | | */ |
8721 | | static int |
8722 | | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
8723 | | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
8724 | 0 | { |
8725 | 0 | int ret; |
8726 | |
|
8727 | 0 | if (define->name != NULL) { |
8728 | 0 | if (!xmlStrEqual(define->name, prop->name)) |
8729 | 0 | return (0); |
8730 | 0 | } |
8731 | 0 | if (define->ns != NULL) { |
8732 | 0 | if (define->ns[0] == 0) { |
8733 | 0 | if (prop->ns != NULL) |
8734 | 0 | return (0); |
8735 | 0 | } else { |
8736 | 0 | if ((prop->ns == NULL) || |
8737 | 0 | (!xmlStrEqual(define->ns, prop->ns->href))) |
8738 | 0 | return (0); |
8739 | 0 | } |
8740 | 0 | } |
8741 | 0 | if (define->nameClass == NULL) |
8742 | 0 | return (1); |
8743 | 0 | define = define->nameClass; |
8744 | 0 | if (define->type == XML_RELAXNG_EXCEPT) { |
8745 | 0 | xmlRelaxNGDefinePtr list; |
8746 | |
|
8747 | 0 | list = define->content; |
8748 | 0 | while (list != NULL) { |
8749 | 0 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
8750 | 0 | if (ret == 1) |
8751 | 0 | return (0); |
8752 | 0 | if (ret < 0) |
8753 | 0 | return (ret); |
8754 | 0 | list = list->next; |
8755 | 0 | } |
8756 | 0 | } else if (define->type == XML_RELAXNG_CHOICE) { |
8757 | 0 | xmlRelaxNGDefinePtr list; |
8758 | |
|
8759 | 0 | list = define->nameClass; |
8760 | 0 | while (list != NULL) { |
8761 | 0 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
8762 | 0 | if (ret == 1) |
8763 | 0 | return (1); |
8764 | 0 | if (ret < 0) |
8765 | 0 | return (ret); |
8766 | 0 | list = list->next; |
8767 | 0 | } |
8768 | 0 | return (0); |
8769 | 0 | } else { |
8770 | | /* TODO */ |
8771 | 0 | return (0); |
8772 | 0 | } |
8773 | 0 | return (1); |
8774 | 0 | } |
8775 | | |
8776 | | /** |
8777 | | * Validate the given attribute definition for that node |
8778 | | * |
8779 | | * @param ctxt a Relax-NG validation context |
8780 | | * @param define the definition to verify |
8781 | | * @returns 0 if the validation succeeded or an error code. |
8782 | | */ |
8783 | | static int |
8784 | | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
8785 | | xmlRelaxNGDefinePtr define) |
8786 | 0 | { |
8787 | 0 | int ret = 0, i; |
8788 | 0 | xmlChar *value, *oldvalue; |
8789 | 0 | xmlAttrPtr prop = NULL, tmp; |
8790 | 0 | xmlNodePtr oldseq; |
8791 | |
|
8792 | 0 | if (ctxt->state->nbAttrLeft <= 0) |
8793 | 0 | return (-1); |
8794 | 0 | if (define->name != NULL) { |
8795 | 0 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
8796 | 0 | tmp = ctxt->state->attrs[i]; |
8797 | 0 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
8798 | 0 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
8799 | 0 | (tmp->ns == NULL)) || |
8800 | 0 | ((tmp->ns != NULL) && |
8801 | 0 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
8802 | 0 | prop = tmp; |
8803 | 0 | break; |
8804 | 0 | } |
8805 | 0 | } |
8806 | 0 | } |
8807 | 0 | if (prop != NULL) { |
8808 | 0 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
8809 | 0 | oldvalue = ctxt->state->value; |
8810 | 0 | oldseq = ctxt->state->seq; |
8811 | 0 | ctxt->state->seq = (xmlNodePtr) prop; |
8812 | 0 | ctxt->state->value = value; |
8813 | 0 | ctxt->state->endvalue = NULL; |
8814 | 0 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
8815 | 0 | if (ctxt->state->value != NULL) |
8816 | 0 | value = ctxt->state->value; |
8817 | 0 | if (value != NULL) |
8818 | 0 | xmlFree(value); |
8819 | 0 | ctxt->state->value = oldvalue; |
8820 | 0 | ctxt->state->seq = oldseq; |
8821 | 0 | if (ret == 0) { |
8822 | | /* |
8823 | | * flag the attribute as processed |
8824 | | */ |
8825 | 0 | ctxt->state->attrs[i] = NULL; |
8826 | 0 | ctxt->state->nbAttrLeft--; |
8827 | 0 | } |
8828 | 0 | } else { |
8829 | 0 | ret = -1; |
8830 | 0 | } |
8831 | 0 | } else { |
8832 | 0 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
8833 | 0 | tmp = ctxt->state->attrs[i]; |
8834 | 0 | if ((tmp != NULL) && |
8835 | 0 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
8836 | 0 | prop = tmp; |
8837 | 0 | break; |
8838 | 0 | } |
8839 | 0 | } |
8840 | 0 | if (prop != NULL) { |
8841 | 0 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
8842 | 0 | oldvalue = ctxt->state->value; |
8843 | 0 | oldseq = ctxt->state->seq; |
8844 | 0 | ctxt->state->seq = (xmlNodePtr) prop; |
8845 | 0 | ctxt->state->value = value; |
8846 | 0 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
8847 | 0 | if (ctxt->state->value != NULL) |
8848 | 0 | value = ctxt->state->value; |
8849 | 0 | if (value != NULL) |
8850 | 0 | xmlFree(value); |
8851 | 0 | ctxt->state->value = oldvalue; |
8852 | 0 | ctxt->state->seq = oldseq; |
8853 | 0 | if (ret == 0) { |
8854 | | /* |
8855 | | * flag the attribute as processed |
8856 | | */ |
8857 | 0 | ctxt->state->attrs[i] = NULL; |
8858 | 0 | ctxt->state->nbAttrLeft--; |
8859 | 0 | } |
8860 | 0 | } else { |
8861 | 0 | ret = -1; |
8862 | 0 | } |
8863 | 0 | } |
8864 | |
|
8865 | 0 | return (ret); |
8866 | 0 | } |
8867 | | |
8868 | | /** |
8869 | | * Validate the given node against the list of attribute definitions |
8870 | | * |
8871 | | * @param ctxt a Relax-NG validation context |
8872 | | * @param defines the list of definition to verify |
8873 | | * @returns 0 if the validation succeeded or an error code. |
8874 | | */ |
8875 | | static int |
8876 | | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
8877 | | xmlRelaxNGDefinePtr defines) |
8878 | 0 | { |
8879 | 0 | int ret = 0, res; |
8880 | 0 | int needmore = 0; |
8881 | 0 | xmlRelaxNGDefinePtr cur; |
8882 | |
|
8883 | 0 | cur = defines; |
8884 | 0 | while (cur != NULL) { |
8885 | 0 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
8886 | 0 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
8887 | 0 | ret = -1; |
8888 | 0 | } else |
8889 | 0 | needmore = 1; |
8890 | 0 | cur = cur->next; |
8891 | 0 | } |
8892 | 0 | if (!needmore) |
8893 | 0 | return (ret); |
8894 | 0 | cur = defines; |
8895 | 0 | while (cur != NULL) { |
8896 | 0 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
8897 | 0 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
8898 | 0 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
8899 | 0 | if (res < 0) |
8900 | 0 | ret = -1; |
8901 | 0 | } else { |
8902 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
8903 | 0 | return (-1); |
8904 | 0 | } |
8905 | 0 | if (res == -1) /* continues on -2 */ |
8906 | 0 | break; |
8907 | 0 | } |
8908 | 0 | cur = cur->next; |
8909 | 0 | } |
8910 | | |
8911 | 0 | return (ret); |
8912 | 0 | } |
8913 | | |
8914 | | /** |
8915 | | * Check if a node can be matched by one of the definitions |
8916 | | * |
8917 | | * @param node the node |
8918 | | * @param list a NULL terminated array of definitions |
8919 | | * @returns 1 if matches 0 otherwise |
8920 | | */ |
8921 | | static int |
8922 | | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
8923 | 0 | { |
8924 | 0 | xmlRelaxNGDefinePtr cur; |
8925 | 0 | int i = 0, tmp; |
8926 | |
|
8927 | 0 | if ((node == NULL) || (list == NULL)) |
8928 | 0 | return (0); |
8929 | | |
8930 | 0 | cur = list[i++]; |
8931 | 0 | while (cur != NULL) { |
8932 | 0 | if ((node->type == XML_ELEMENT_NODE) && |
8933 | 0 | (cur->type == XML_RELAXNG_ELEMENT)) { |
8934 | 0 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
8935 | 0 | if (tmp == 1) |
8936 | 0 | return (1); |
8937 | 0 | } else if (((node->type == XML_TEXT_NODE) || |
8938 | 0 | (node->type == XML_CDATA_SECTION_NODE)) && |
8939 | 0 | ((cur->type == XML_RELAXNG_DATATYPE) || |
8940 | 0 | (cur->type == XML_RELAXNG_LIST) || |
8941 | 0 | (cur->type == XML_RELAXNG_TEXT) || |
8942 | 0 | (cur->type == XML_RELAXNG_VALUE))) { |
8943 | 0 | return (1); |
8944 | 0 | } |
8945 | 0 | cur = list[i++]; |
8946 | 0 | } |
8947 | 0 | return (0); |
8948 | 0 | } |
8949 | | |
8950 | | /** |
8951 | | * Validate an interleave definition for a node. |
8952 | | * |
8953 | | * @param ctxt a Relax-NG validation context |
8954 | | * @param define the definition to verify |
8955 | | * @returns 0 if the validation succeeded or an error code. |
8956 | | */ |
8957 | | static int |
8958 | | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
8959 | | xmlRelaxNGDefinePtr define) |
8960 | 0 | { |
8961 | 0 | int ret = 0, i, nbgroups; |
8962 | 0 | int errNr = ctxt->errNr; |
8963 | 0 | int oldflags; |
8964 | |
|
8965 | 0 | xmlRelaxNGValidStatePtr oldstate; |
8966 | 0 | xmlRelaxNGPartitionPtr partitions; |
8967 | 0 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
8968 | 0 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
8969 | 0 | xmlNodePtr *list = NULL, *lasts = NULL; |
8970 | |
|
8971 | 0 | if (define->data != NULL) { |
8972 | 0 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
8973 | 0 | nbgroups = partitions->nbgroups; |
8974 | 0 | } else { |
8975 | 0 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
8976 | 0 | return (-1); |
8977 | 0 | } |
8978 | | /* |
8979 | | * Optimizations for MIXED |
8980 | | */ |
8981 | 0 | oldflags = ctxt->flags; |
8982 | 0 | if (define->dflags & IS_MIXED) { |
8983 | 0 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
8984 | 0 | if (nbgroups == 2) { |
8985 | | /* |
8986 | | * this is a pure <mixed> case |
8987 | | */ |
8988 | 0 | if (ctxt->state != NULL) |
8989 | 0 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
8990 | 0 | ctxt->state->seq); |
8991 | 0 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
8992 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, |
8993 | 0 | partitions->groups[1]-> |
8994 | 0 | rule); |
8995 | 0 | else |
8996 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, |
8997 | 0 | partitions->groups[0]-> |
8998 | 0 | rule); |
8999 | 0 | if (ret == 0) { |
9000 | 0 | if (ctxt->state != NULL) |
9001 | 0 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
9002 | 0 | ctxt->state-> |
9003 | 0 | seq); |
9004 | 0 | } |
9005 | 0 | ctxt->flags = oldflags; |
9006 | 0 | return (ret); |
9007 | 0 | } |
9008 | 0 | } |
9009 | | |
9010 | | /* |
9011 | | * Build arrays to store the first and last node of the chain |
9012 | | * pertaining to each group |
9013 | | */ |
9014 | 0 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
9015 | 0 | if (list == NULL) { |
9016 | 0 | xmlRngVErrMemory(ctxt); |
9017 | 0 | return (-1); |
9018 | 0 | } |
9019 | 0 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
9020 | 0 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
9021 | 0 | if (lasts == NULL) { |
9022 | 0 | xmlRngVErrMemory(ctxt); |
9023 | 0 | return (-1); |
9024 | 0 | } |
9025 | 0 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
9026 | | |
9027 | | /* |
9028 | | * Walk the sequence of children finding the right group and |
9029 | | * sorting them in sequences. |
9030 | | */ |
9031 | 0 | cur = ctxt->state->seq; |
9032 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
9033 | 0 | start = cur; |
9034 | 0 | while (cur != NULL) { |
9035 | 0 | ctxt->state->seq = cur; |
9036 | 0 | if ((partitions->triage != NULL) && |
9037 | 0 | (partitions->flags & IS_DETERMINIST)) { |
9038 | 0 | void *tmp = NULL; |
9039 | |
|
9040 | 0 | if ((cur->type == XML_TEXT_NODE) || |
9041 | 0 | (cur->type == XML_CDATA_SECTION_NODE)) { |
9042 | 0 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
9043 | 0 | NULL); |
9044 | 0 | } else if (cur->type == XML_ELEMENT_NODE) { |
9045 | 0 | if (cur->ns != NULL) { |
9046 | 0 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
9047 | 0 | cur->ns->href); |
9048 | 0 | if (tmp == NULL) |
9049 | 0 | tmp = xmlHashLookup2(partitions->triage, |
9050 | 0 | BAD_CAST "#any", |
9051 | 0 | cur->ns->href); |
9052 | 0 | } else |
9053 | 0 | tmp = |
9054 | 0 | xmlHashLookup2(partitions->triage, cur->name, |
9055 | 0 | NULL); |
9056 | 0 | if (tmp == NULL) |
9057 | 0 | tmp = |
9058 | 0 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
9059 | 0 | NULL); |
9060 | 0 | } |
9061 | |
|
9062 | 0 | if (tmp == NULL) { |
9063 | 0 | i = nbgroups; |
9064 | 0 | } else { |
9065 | 0 | i = XML_PTR_TO_INT(tmp) - 1; |
9066 | 0 | if (partitions->flags & IS_NEEDCHECK) { |
9067 | 0 | group = partitions->groups[i]; |
9068 | 0 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
9069 | 0 | i = nbgroups; |
9070 | 0 | } |
9071 | 0 | } |
9072 | 0 | } else { |
9073 | 0 | for (i = 0; i < nbgroups; i++) { |
9074 | 0 | group = partitions->groups[i]; |
9075 | 0 | if (group == NULL) |
9076 | 0 | continue; |
9077 | 0 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
9078 | 0 | break; |
9079 | 0 | } |
9080 | 0 | } |
9081 | | /* |
9082 | | * We break as soon as an element not matched is found |
9083 | | */ |
9084 | 0 | if (i >= nbgroups) { |
9085 | 0 | break; |
9086 | 0 | } |
9087 | 0 | if (lasts[i] != NULL) { |
9088 | 0 | lasts[i]->next = cur; |
9089 | 0 | lasts[i] = cur; |
9090 | 0 | } else { |
9091 | 0 | list[i] = cur; |
9092 | 0 | lasts[i] = cur; |
9093 | 0 | } |
9094 | 0 | if (cur->next != NULL) |
9095 | 0 | lastchg = cur->next; |
9096 | 0 | else |
9097 | 0 | lastchg = cur; |
9098 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
9099 | 0 | } |
9100 | 0 | if (ret != 0) { |
9101 | 0 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
9102 | 0 | ret = -1; |
9103 | 0 | goto done; |
9104 | 0 | } |
9105 | 0 | lastelem = cur; |
9106 | 0 | oldstate = ctxt->state; |
9107 | 0 | for (i = 0; i < nbgroups; i++) { |
9108 | 0 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
9109 | 0 | if (ctxt->state == NULL) { |
9110 | 0 | ret = -1; |
9111 | 0 | break; |
9112 | 0 | } |
9113 | 0 | group = partitions->groups[i]; |
9114 | 0 | if (lasts[i] != NULL) { |
9115 | 0 | last = lasts[i]->next; |
9116 | 0 | lasts[i]->next = NULL; |
9117 | 0 | } |
9118 | 0 | ctxt->state->seq = list[i]; |
9119 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
9120 | 0 | if (ret != 0) |
9121 | 0 | break; |
9122 | 0 | if (ctxt->state != NULL) { |
9123 | 0 | cur = ctxt->state->seq; |
9124 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
9125 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
9126 | 0 | oldstate = ctxt->state; |
9127 | 0 | ctxt->state = NULL; |
9128 | 0 | if (cur != NULL |
9129 | | /* there's a nasty violation of context-free unambiguities, |
9130 | | since in open-name-class context, interleave in the |
9131 | | production shall finish without caring about anything |
9132 | | else that is OK to follow in that case -- it would |
9133 | | otherwise get marked as "extra content" and would |
9134 | | hence fail the validation, hence this perhaps |
9135 | | dirty attempt to rectify such a situation */ |
9136 | 0 | && (define->parent->type != XML_RELAXNG_DEF |
9137 | 0 | || !xmlStrEqual(define->parent->name, |
9138 | 0 | (const xmlChar *) "open-name-class"))) { |
9139 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
9140 | 0 | ret = -1; |
9141 | 0 | ctxt->state = oldstate; |
9142 | 0 | goto done; |
9143 | 0 | } |
9144 | 0 | } else if (ctxt->states != NULL) { |
9145 | 0 | int j; |
9146 | 0 | int found = 0; |
9147 | 0 | int best = -1; |
9148 | 0 | int lowattr = -1; |
9149 | | |
9150 | | /* |
9151 | | * PBM: what happen if there is attributes checks in the interleaves |
9152 | | */ |
9153 | |
|
9154 | 0 | for (j = 0; j < ctxt->states->nbState; j++) { |
9155 | 0 | cur = ctxt->states->tabState[j]->seq; |
9156 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
9157 | 0 | if (cur == NULL) { |
9158 | 0 | if (found == 0) { |
9159 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9160 | 0 | best = j; |
9161 | 0 | } |
9162 | 0 | found = 1; |
9163 | 0 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
9164 | | /* try to keep the latest one to mach old heuristic */ |
9165 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9166 | 0 | best = j; |
9167 | 0 | } |
9168 | 0 | if (lowattr == 0) |
9169 | 0 | break; |
9170 | 0 | } else if (found == 0) { |
9171 | 0 | if (lowattr == -1) { |
9172 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9173 | 0 | best = j; |
9174 | 0 | } else |
9175 | 0 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
9176 | | /* try to keep the latest one to mach old heuristic */ |
9177 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9178 | 0 | best = j; |
9179 | 0 | } |
9180 | 0 | } |
9181 | 0 | } |
9182 | | /* |
9183 | | * BIG PBM: here we pick only one restarting point :-( |
9184 | | */ |
9185 | 0 | if (ctxt->states->nbState > 0) { |
9186 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
9187 | 0 | if (best != -1) { |
9188 | 0 | oldstate = ctxt->states->tabState[best]; |
9189 | 0 | ctxt->states->tabState[best] = NULL; |
9190 | 0 | } else { |
9191 | 0 | oldstate = |
9192 | 0 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
9193 | 0 | ctxt->states->tabState[ctxt->states->nbState - 1] = NULL; |
9194 | 0 | ctxt->states->nbState--; |
9195 | 0 | } |
9196 | 0 | } |
9197 | 0 | for (j = 0; j < ctxt->states->nbState ; j++) { |
9198 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
9199 | 0 | } |
9200 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
9201 | 0 | ctxt->states = NULL; |
9202 | 0 | if (found == 0) { |
9203 | 0 | if (cur == NULL) { |
9204 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, |
9205 | 0 | (const xmlChar *) "noname"); |
9206 | 0 | } else { |
9207 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
9208 | 0 | } |
9209 | 0 | ret = -1; |
9210 | 0 | ctxt->state = oldstate; |
9211 | 0 | goto done; |
9212 | 0 | } |
9213 | 0 | } else { |
9214 | 0 | ret = -1; |
9215 | 0 | break; |
9216 | 0 | } |
9217 | 0 | if (lasts[i] != NULL) { |
9218 | 0 | lasts[i]->next = last; |
9219 | 0 | } |
9220 | 0 | } |
9221 | 0 | if (ctxt->state != NULL) |
9222 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
9223 | 0 | ctxt->state = oldstate; |
9224 | 0 | ctxt->state->seq = lastelem; |
9225 | 0 | if (ret != 0) { |
9226 | 0 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
9227 | 0 | ret = -1; |
9228 | 0 | goto done; |
9229 | 0 | } |
9230 | | |
9231 | 0 | done: |
9232 | 0 | ctxt->flags = oldflags; |
9233 | | /* |
9234 | | * builds the next links chain from the prev one |
9235 | | */ |
9236 | 0 | cur = lastchg; |
9237 | 0 | while (cur != NULL) { |
9238 | 0 | if ((cur == start) || (cur->prev == NULL)) |
9239 | 0 | break; |
9240 | 0 | cur->prev->next = cur; |
9241 | 0 | cur = cur->prev; |
9242 | 0 | } |
9243 | 0 | if (ret == 0) { |
9244 | 0 | if (ctxt->errNr > errNr) |
9245 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9246 | 0 | } |
9247 | |
|
9248 | 0 | xmlFree(list); |
9249 | 0 | xmlFree(lasts); |
9250 | 0 | return (ret); |
9251 | 0 | } |
9252 | | |
9253 | | /** |
9254 | | * Validate the given node content against the (list) of definitions |
9255 | | * |
9256 | | * @param ctxt a Relax-NG validation context |
9257 | | * @param defines the list of definition to verify |
9258 | | * @returns 0 if the validation succeeded or an error code. |
9259 | | */ |
9260 | | static int |
9261 | | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
9262 | | xmlRelaxNGDefinePtr defines) |
9263 | 0 | { |
9264 | 0 | int ret = 0, res; |
9265 | | |
9266 | |
|
9267 | 0 | if (defines == NULL) { |
9268 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
9269 | 0 | BAD_CAST "NULL definition list"); |
9270 | 0 | return (-1); |
9271 | 0 | } |
9272 | 0 | while (defines != NULL) { |
9273 | 0 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
9274 | 0 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
9275 | 0 | if (res < 0) |
9276 | 0 | ret = -1; |
9277 | 0 | } else { |
9278 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
9279 | 0 | return (-1); |
9280 | 0 | } |
9281 | 0 | if (res == -1) /* continues on -2 */ |
9282 | 0 | break; |
9283 | 0 | defines = defines->next; |
9284 | 0 | } |
9285 | | |
9286 | 0 | return (ret); |
9287 | 0 | } |
9288 | | |
9289 | | /** |
9290 | | * Check if the element matches the definition nameClass |
9291 | | * |
9292 | | * @param ctxt a Relax-NG validation context |
9293 | | * @param define the definition to check |
9294 | | * @param elem the element |
9295 | | * @returns 1 if the element matches, 0 if no, or -1 in case of error |
9296 | | */ |
9297 | | static int |
9298 | | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
9299 | | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
9300 | 0 | { |
9301 | 0 | int ret = 0, oldflags = 0; |
9302 | |
|
9303 | 0 | if (define->name != NULL) { |
9304 | 0 | if (!xmlStrEqual(elem->name, define->name)) { |
9305 | 0 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
9306 | 0 | return (0); |
9307 | 0 | } |
9308 | 0 | } |
9309 | 0 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
9310 | 0 | if (elem->ns == NULL) { |
9311 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
9312 | 0 | return (0); |
9313 | 0 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
9314 | 0 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
9315 | 0 | elem->name, define->ns); |
9316 | 0 | return (0); |
9317 | 0 | } |
9318 | 0 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
9319 | 0 | (define->name == NULL)) { |
9320 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
9321 | 0 | return (0); |
9322 | 0 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
9323 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
9324 | 0 | return (0); |
9325 | 0 | } |
9326 | | |
9327 | 0 | if (define->nameClass == NULL) |
9328 | 0 | return (1); |
9329 | | |
9330 | 0 | define = define->nameClass; |
9331 | 0 | if (define->type == XML_RELAXNG_EXCEPT) { |
9332 | 0 | xmlRelaxNGDefinePtr list; |
9333 | |
|
9334 | 0 | if (ctxt != NULL) { |
9335 | 0 | oldflags = ctxt->flags; |
9336 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9337 | 0 | } |
9338 | |
|
9339 | 0 | list = define->content; |
9340 | 0 | while (list != NULL) { |
9341 | 0 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
9342 | 0 | if (ret == 1) { |
9343 | 0 | if (ctxt != NULL) |
9344 | 0 | ctxt->flags = oldflags; |
9345 | 0 | return (0); |
9346 | 0 | } |
9347 | 0 | if (ret < 0) { |
9348 | 0 | if (ctxt != NULL) |
9349 | 0 | ctxt->flags = oldflags; |
9350 | 0 | return (ret); |
9351 | 0 | } |
9352 | 0 | list = list->next; |
9353 | 0 | } |
9354 | 0 | ret = 1; |
9355 | 0 | if (ctxt != NULL) { |
9356 | 0 | ctxt->flags = oldflags; |
9357 | 0 | } |
9358 | 0 | } else if (define->type == XML_RELAXNG_CHOICE) { |
9359 | 0 | xmlRelaxNGDefinePtr list; |
9360 | |
|
9361 | 0 | if (ctxt != NULL) { |
9362 | 0 | oldflags = ctxt->flags; |
9363 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9364 | 0 | } |
9365 | |
|
9366 | 0 | list = define->nameClass; |
9367 | 0 | while (list != NULL) { |
9368 | 0 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
9369 | 0 | if (ret == 1) { |
9370 | 0 | if (ctxt != NULL) |
9371 | 0 | ctxt->flags = oldflags; |
9372 | 0 | return (1); |
9373 | 0 | } |
9374 | 0 | if (ret < 0) { |
9375 | 0 | if (ctxt != NULL) |
9376 | 0 | ctxt->flags = oldflags; |
9377 | 0 | return (ret); |
9378 | 0 | } |
9379 | 0 | list = list->next; |
9380 | 0 | } |
9381 | 0 | if (ctxt != NULL) { |
9382 | 0 | if (ret != 0) { |
9383 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9384 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9385 | 0 | } else { |
9386 | 0 | if (ctxt->errNr > 0) |
9387 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
9388 | 0 | } |
9389 | 0 | } |
9390 | 0 | ret = 0; |
9391 | 0 | if (ctxt != NULL) { |
9392 | 0 | ctxt->flags = oldflags; |
9393 | 0 | } |
9394 | 0 | } else { |
9395 | | /* TODO */ |
9396 | 0 | ret = -1; |
9397 | 0 | } |
9398 | 0 | return (ret); |
9399 | 0 | } |
9400 | | |
9401 | | /** |
9402 | | * Find the "best" state in the ctxt->states list of states to report |
9403 | | * errors about. I.e. a state with no element left in the child list |
9404 | | * or the one with the less attributes left. |
9405 | | * This is called only if a validation error was detected |
9406 | | * |
9407 | | * @param ctxt a Relax-NG validation context |
9408 | | * @returns the index of the "best" state or -1 in case of error |
9409 | | */ |
9410 | | static int |
9411 | | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
9412 | 0 | { |
9413 | 0 | xmlRelaxNGValidStatePtr state; |
9414 | 0 | int i, tmp; |
9415 | 0 | int best = -1; |
9416 | 0 | int value = 1000000; |
9417 | |
|
9418 | 0 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
9419 | 0 | (ctxt->states->nbState <= 0)) |
9420 | 0 | return (-1); |
9421 | | |
9422 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9423 | 0 | state = ctxt->states->tabState[i]; |
9424 | 0 | if (state == NULL) |
9425 | 0 | continue; |
9426 | 0 | if (state->seq != NULL) { |
9427 | 0 | if ((best == -1) || (value > 100000)) { |
9428 | 0 | value = 100000; |
9429 | 0 | best = i; |
9430 | 0 | } |
9431 | 0 | } else { |
9432 | 0 | tmp = state->nbAttrLeft; |
9433 | 0 | if ((best == -1) || (value > tmp)) { |
9434 | 0 | value = tmp; |
9435 | 0 | best = i; |
9436 | 0 | } |
9437 | 0 | } |
9438 | 0 | } |
9439 | 0 | return (best); |
9440 | 0 | } |
9441 | | |
9442 | | /** |
9443 | | * Find the "best" state in the ctxt->states list of states to report |
9444 | | * errors about and log it. |
9445 | | * |
9446 | | * @param ctxt a Relax-NG validation context |
9447 | | */ |
9448 | | static void |
9449 | | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
9450 | 0 | { |
9451 | 0 | int best; |
9452 | |
|
9453 | 0 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
9454 | 0 | (ctxt->states->nbState <= 0)) |
9455 | 0 | return; |
9456 | | |
9457 | 0 | best = xmlRelaxNGBestState(ctxt); |
9458 | 0 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
9459 | 0 | ctxt->state = ctxt->states->tabState[best]; |
9460 | |
|
9461 | 0 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
9462 | 0 | } |
9463 | 0 | } |
9464 | | |
9465 | | /** |
9466 | | * Validate the end of the element, implements check that |
9467 | | * there is nothing left not consumed in the element content |
9468 | | * or in the attribute list. |
9469 | | * |
9470 | | * @param ctxt a Relax-NG validation context |
9471 | | * @param dolog indicate that error logging should be done |
9472 | | * @returns 0 if the validation succeeded or an error code. |
9473 | | */ |
9474 | | static int |
9475 | | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
9476 | 0 | { |
9477 | 0 | int i; |
9478 | 0 | xmlRelaxNGValidStatePtr state; |
9479 | |
|
9480 | 0 | state = ctxt->state; |
9481 | 0 | if (state->seq != NULL) { |
9482 | 0 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
9483 | 0 | if (state->seq != NULL) { |
9484 | 0 | if (dolog) { |
9485 | 0 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
9486 | 0 | state->node->name, state->seq->name); |
9487 | 0 | } |
9488 | 0 | return (-1); |
9489 | 0 | } |
9490 | 0 | } |
9491 | 0 | for (i = 0; i < state->nbAttrs; i++) { |
9492 | 0 | if (state->attrs[i] != NULL) { |
9493 | 0 | if (dolog) { |
9494 | 0 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
9495 | 0 | state->attrs[i]->name, state->node->name); |
9496 | 0 | } |
9497 | 0 | return (-1 - i); |
9498 | 0 | } |
9499 | 0 | } |
9500 | 0 | return (0); |
9501 | 0 | } |
9502 | | |
9503 | | /** |
9504 | | * Validate the current state against the definition |
9505 | | * |
9506 | | * @param ctxt a Relax-NG validation context |
9507 | | * @param define the definition to verify |
9508 | | * @returns 0 if the validation succeeded or an error code. |
9509 | | */ |
9510 | | static int |
9511 | | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
9512 | | xmlRelaxNGDefinePtr define) |
9513 | 0 | { |
9514 | 0 | xmlNodePtr node; |
9515 | 0 | int ret = 0, i, tmp, oldflags, errNr; |
9516 | 0 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
9517 | |
|
9518 | 0 | if (define == NULL) { |
9519 | 0 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
9520 | 0 | return (-1); |
9521 | 0 | } |
9522 | | |
9523 | 0 | if (ctxt->state != NULL) { |
9524 | 0 | node = ctxt->state->seq; |
9525 | 0 | } else { |
9526 | 0 | node = NULL; |
9527 | 0 | } |
9528 | 0 | ctxt->depth++; |
9529 | 0 | switch (define->type) { |
9530 | 0 | case XML_RELAXNG_EMPTY: |
9531 | 0 | ret = 0; |
9532 | 0 | break; |
9533 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
9534 | 0 | ret = -1; |
9535 | 0 | break; |
9536 | 0 | case XML_RELAXNG_TEXT: |
9537 | 0 | while ((node != NULL) && |
9538 | 0 | ((node->type == XML_TEXT_NODE) || |
9539 | 0 | (node->type == XML_COMMENT_NODE) || |
9540 | 0 | (node->type == XML_PI_NODE) || |
9541 | 0 | (node->type == XML_CDATA_SECTION_NODE))) |
9542 | 0 | node = node->next; |
9543 | 0 | ctxt->state->seq = node; |
9544 | 0 | break; |
9545 | 0 | case XML_RELAXNG_ELEMENT: |
9546 | 0 | errNr = ctxt->errNr; |
9547 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
9548 | 0 | if (node == NULL) { |
9549 | 0 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
9550 | 0 | ret = -1; |
9551 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9552 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9553 | 0 | break; |
9554 | 0 | } |
9555 | 0 | if (node->type != XML_ELEMENT_NODE) { |
9556 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
9557 | 0 | ret = -1; |
9558 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9559 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9560 | 0 | break; |
9561 | 0 | } |
9562 | | /* |
9563 | | * This node was already validated successfully against |
9564 | | * this definition. |
9565 | | */ |
9566 | 0 | if (node->psvi == define) { |
9567 | 0 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
9568 | 0 | if (ctxt->errNr > errNr) |
9569 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9570 | 0 | if (ctxt->errNr != 0) { |
9571 | 0 | while ((ctxt->err != NULL) && |
9572 | 0 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
9573 | 0 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
9574 | 0 | || |
9575 | 0 | ((ctxt->err->err == |
9576 | 0 | XML_RELAXNG_ERR_ELEMEXTRANS) |
9577 | 0 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
9578 | 0 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
9579 | 0 | || (ctxt->err->err == |
9580 | 0 | XML_RELAXNG_ERR_NOTELEM))) |
9581 | 0 | xmlRelaxNGValidErrorPop(ctxt); |
9582 | 0 | } |
9583 | 0 | break; |
9584 | 0 | } |
9585 | | |
9586 | 0 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
9587 | 0 | if (ret <= 0) { |
9588 | 0 | ret = -1; |
9589 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9590 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9591 | 0 | break; |
9592 | 0 | } |
9593 | 0 | ret = 0; |
9594 | 0 | if (ctxt->errNr != 0) { |
9595 | 0 | if (ctxt->errNr > errNr) |
9596 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9597 | 0 | while ((ctxt->err != NULL) && |
9598 | 0 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
9599 | 0 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
9600 | 0 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
9601 | 0 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
9602 | 0 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
9603 | 0 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
9604 | 0 | xmlRelaxNGValidErrorPop(ctxt); |
9605 | 0 | } |
9606 | 0 | errNr = ctxt->errNr; |
9607 | |
|
9608 | 0 | oldflags = ctxt->flags; |
9609 | 0 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
9610 | 0 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
9611 | 0 | } |
9612 | 0 | state = xmlRelaxNGNewValidState(ctxt, node); |
9613 | 0 | if (state == NULL) { |
9614 | 0 | ret = -1; |
9615 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9616 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9617 | 0 | break; |
9618 | 0 | } |
9619 | | |
9620 | 0 | oldstate = ctxt->state; |
9621 | 0 | ctxt->state = state; |
9622 | 0 | if (define->attrs != NULL) { |
9623 | 0 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
9624 | 0 | if (tmp != 0) { |
9625 | 0 | ret = -1; |
9626 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
9627 | 0 | } |
9628 | 0 | } |
9629 | 0 | if (define->contModel != NULL) { |
9630 | 0 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
9631 | 0 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
9632 | 0 | xmlNodePtr nseq; |
9633 | |
|
9634 | 0 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
9635 | 0 | ctxt->state = nstate; |
9636 | 0 | ctxt->states = NULL; |
9637 | |
|
9638 | 0 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
9639 | 0 | define->contModel, |
9640 | 0 | ctxt->state->seq); |
9641 | 0 | nseq = ctxt->state->seq; |
9642 | 0 | ctxt->state = tmpstate; |
9643 | 0 | ctxt->states = tmpstates; |
9644 | 0 | xmlRelaxNGFreeValidState(ctxt, nstate); |
9645 | |
|
9646 | 0 | if (tmp != 0) |
9647 | 0 | ret = -1; |
9648 | |
|
9649 | 0 | if (ctxt->states != NULL) { |
9650 | 0 | tmp = -1; |
9651 | |
|
9652 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9653 | 0 | state = ctxt->states->tabState[i]; |
9654 | 0 | ctxt->state = state; |
9655 | 0 | ctxt->state->seq = nseq; |
9656 | |
|
9657 | 0 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
9658 | 0 | tmp = 0; |
9659 | 0 | break; |
9660 | 0 | } |
9661 | 0 | } |
9662 | 0 | if (tmp != 0) { |
9663 | | /* |
9664 | | * validation error, log the message for the "best" one |
9665 | | */ |
9666 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9667 | 0 | xmlRelaxNGLogBestError(ctxt); |
9668 | 0 | } |
9669 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9670 | 0 | xmlRelaxNGFreeValidState(ctxt, |
9671 | 0 | ctxt->states-> |
9672 | 0 | tabState[i]); |
9673 | 0 | } |
9674 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
9675 | 0 | ctxt->flags = oldflags; |
9676 | 0 | ctxt->states = NULL; |
9677 | 0 | if ((ret == 0) && (tmp == -1)) |
9678 | 0 | ret = -1; |
9679 | 0 | } else { |
9680 | 0 | state = ctxt->state; |
9681 | 0 | if (ctxt->state != NULL) |
9682 | 0 | ctxt->state->seq = nseq; |
9683 | 0 | if (ret == 0) |
9684 | 0 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
9685 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
9686 | 0 | } |
9687 | 0 | } else { |
9688 | 0 | if (define->content != NULL) { |
9689 | 0 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
9690 | 0 | define-> |
9691 | 0 | content); |
9692 | 0 | if (tmp != 0) { |
9693 | 0 | ret = -1; |
9694 | 0 | if (ctxt->state == NULL) { |
9695 | 0 | ctxt->state = oldstate; |
9696 | 0 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
9697 | 0 | node->name); |
9698 | 0 | ctxt->state = NULL; |
9699 | 0 | } else { |
9700 | 0 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
9701 | 0 | node->name); |
9702 | 0 | } |
9703 | |
|
9704 | 0 | } |
9705 | 0 | } |
9706 | 0 | if (ctxt->states != NULL) { |
9707 | 0 | tmp = -1; |
9708 | |
|
9709 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9710 | 0 | state = ctxt->states->tabState[i]; |
9711 | 0 | ctxt->state = state; |
9712 | |
|
9713 | 0 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
9714 | 0 | tmp = 0; |
9715 | 0 | break; |
9716 | 0 | } |
9717 | 0 | } |
9718 | 0 | if (tmp != 0) { |
9719 | | /* |
9720 | | * validation error, log the message for the "best" one |
9721 | | */ |
9722 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9723 | 0 | xmlRelaxNGLogBestError(ctxt); |
9724 | 0 | } |
9725 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9726 | 0 | xmlRelaxNGFreeValidState(ctxt, |
9727 | 0 | ctxt->states->tabState[i]); |
9728 | 0 | ctxt->states->tabState[i] = NULL; |
9729 | 0 | } |
9730 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
9731 | 0 | ctxt->flags = oldflags; |
9732 | 0 | ctxt->states = NULL; |
9733 | 0 | if ((ret == 0) && (tmp == -1)) |
9734 | 0 | ret = -1; |
9735 | 0 | } else { |
9736 | 0 | state = ctxt->state; |
9737 | 0 | if (ret == 0) |
9738 | 0 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
9739 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
9740 | 0 | } |
9741 | 0 | } |
9742 | 0 | if (ret == 0) { |
9743 | 0 | node->psvi = define; |
9744 | 0 | } |
9745 | 0 | ctxt->flags = oldflags; |
9746 | 0 | ctxt->state = oldstate; |
9747 | 0 | if (oldstate != NULL) |
9748 | 0 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
9749 | 0 | if (ret != 0) { |
9750 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
9751 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9752 | 0 | ret = 0; |
9753 | | #if 0 |
9754 | | } else { |
9755 | | ret = -2; |
9756 | | #endif |
9757 | 0 | } |
9758 | 0 | } else { |
9759 | 0 | if (ctxt->errNr > errNr) |
9760 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9761 | 0 | } |
9762 | |
|
9763 | 0 | break; |
9764 | 0 | case XML_RELAXNG_OPTIONAL:{ |
9765 | 0 | errNr = ctxt->errNr; |
9766 | 0 | oldflags = ctxt->flags; |
9767 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9768 | 0 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
9769 | 0 | ret = |
9770 | 0 | xmlRelaxNGValidateDefinitionList(ctxt, |
9771 | 0 | define->content); |
9772 | 0 | if (ret != 0) { |
9773 | 0 | if (ctxt->state != NULL) |
9774 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
9775 | 0 | ctxt->state = oldstate; |
9776 | 0 | ctxt->flags = oldflags; |
9777 | 0 | ret = 0; |
9778 | 0 | if (ctxt->errNr > errNr) |
9779 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9780 | 0 | break; |
9781 | 0 | } |
9782 | 0 | if (ctxt->states != NULL) { |
9783 | 0 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
9784 | 0 | } else { |
9785 | 0 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
9786 | 0 | if (ctxt->states == NULL) { |
9787 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
9788 | 0 | ctxt->flags = oldflags; |
9789 | 0 | ret = -1; |
9790 | 0 | if (ctxt->errNr > errNr) |
9791 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9792 | 0 | break; |
9793 | 0 | } |
9794 | 0 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
9795 | 0 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
9796 | 0 | ctxt->state = NULL; |
9797 | 0 | } |
9798 | 0 | ctxt->flags = oldflags; |
9799 | 0 | ret = 0; |
9800 | 0 | if (ctxt->errNr > errNr) |
9801 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9802 | 0 | break; |
9803 | 0 | } |
9804 | 0 | case XML_RELAXNG_ONEORMORE: |
9805 | 0 | errNr = ctxt->errNr; |
9806 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
9807 | 0 | if (ret != 0) { |
9808 | 0 | break; |
9809 | 0 | } |
9810 | 0 | if (ctxt->errNr > errNr) |
9811 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9812 | | /* Falls through. */ |
9813 | 0 | case XML_RELAXNG_ZEROORMORE:{ |
9814 | 0 | int progress; |
9815 | 0 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
9816 | 0 | int base, j; |
9817 | |
|
9818 | 0 | errNr = ctxt->errNr; |
9819 | 0 | res = xmlRelaxNGNewStates(ctxt, 1); |
9820 | 0 | if (res == NULL) { |
9821 | 0 | ret = -1; |
9822 | 0 | break; |
9823 | 0 | } |
9824 | | /* |
9825 | | * All the input states are also exit states |
9826 | | */ |
9827 | 0 | if (ctxt->state != NULL) { |
9828 | 0 | xmlRelaxNGAddStates(ctxt, res, |
9829 | 0 | xmlRelaxNGCopyValidState(ctxt, |
9830 | 0 | ctxt-> |
9831 | 0 | state)); |
9832 | 0 | } else { |
9833 | 0 | for (j = 0; j < ctxt->states->nbState; j++) { |
9834 | 0 | xmlRelaxNGAddStates(ctxt, res, |
9835 | 0 | xmlRelaxNGCopyValidState(ctxt, |
9836 | 0 | ctxt->states->tabState[j])); |
9837 | 0 | } |
9838 | 0 | } |
9839 | 0 | oldflags = ctxt->flags; |
9840 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9841 | 0 | do { |
9842 | 0 | progress = 0; |
9843 | 0 | base = res->nbState; |
9844 | |
|
9845 | 0 | if (ctxt->states != NULL) { |
9846 | 0 | states = ctxt->states; |
9847 | 0 | for (i = 0; i < states->nbState; i++) { |
9848 | 0 | ctxt->state = states->tabState[i]; |
9849 | 0 | ctxt->states = NULL; |
9850 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
9851 | 0 | define-> |
9852 | 0 | content); |
9853 | 0 | if (ret == 0) { |
9854 | 0 | if (ctxt->state != NULL) { |
9855 | 0 | tmp = xmlRelaxNGAddStates(ctxt, res, |
9856 | 0 | ctxt->state); |
9857 | 0 | ctxt->state = NULL; |
9858 | 0 | if (tmp == 1) |
9859 | 0 | progress = 1; |
9860 | 0 | } else if (ctxt->states != NULL) { |
9861 | 0 | for (j = 0; j < ctxt->states->nbState; |
9862 | 0 | j++) { |
9863 | 0 | tmp = |
9864 | 0 | xmlRelaxNGAddStates(ctxt, res, |
9865 | 0 | ctxt->states->tabState[j]); |
9866 | 0 | if (tmp == 1) |
9867 | 0 | progress = 1; |
9868 | 0 | } |
9869 | 0 | xmlRelaxNGFreeStates(ctxt, |
9870 | 0 | ctxt->states); |
9871 | 0 | ctxt->states = NULL; |
9872 | 0 | } |
9873 | 0 | } else { |
9874 | 0 | if (ctxt->state != NULL) { |
9875 | 0 | xmlRelaxNGFreeValidState(ctxt, |
9876 | 0 | ctxt->state); |
9877 | 0 | ctxt->state = NULL; |
9878 | 0 | } |
9879 | 0 | } |
9880 | 0 | } |
9881 | 0 | } else { |
9882 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
9883 | 0 | define-> |
9884 | 0 | content); |
9885 | 0 | if (ret != 0) { |
9886 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
9887 | 0 | ctxt->state = NULL; |
9888 | 0 | } else { |
9889 | 0 | base = res->nbState; |
9890 | 0 | if (ctxt->state != NULL) { |
9891 | 0 | tmp = xmlRelaxNGAddStates(ctxt, res, |
9892 | 0 | ctxt->state); |
9893 | 0 | ctxt->state = NULL; |
9894 | 0 | if (tmp == 1) |
9895 | 0 | progress = 1; |
9896 | 0 | } else if (ctxt->states != NULL) { |
9897 | 0 | for (j = 0; j < ctxt->states->nbState; j++) { |
9898 | 0 | tmp = xmlRelaxNGAddStates(ctxt, res, |
9899 | 0 | ctxt->states->tabState[j]); |
9900 | 0 | if (tmp == 1) |
9901 | 0 | progress = 1; |
9902 | 0 | } |
9903 | 0 | if (states == NULL) { |
9904 | 0 | states = ctxt->states; |
9905 | 0 | } else { |
9906 | 0 | xmlRelaxNGFreeStates(ctxt, |
9907 | 0 | ctxt->states); |
9908 | 0 | } |
9909 | 0 | ctxt->states = NULL; |
9910 | 0 | } |
9911 | 0 | } |
9912 | 0 | } |
9913 | 0 | if (progress) { |
9914 | | /* |
9915 | | * Collect all the new nodes added at that step |
9916 | | * and make them the new node set |
9917 | | */ |
9918 | 0 | if (res->nbState - base == 1) { |
9919 | 0 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
9920 | 0 | res-> |
9921 | 0 | tabState |
9922 | 0 | [base]); |
9923 | 0 | } else { |
9924 | 0 | if (states == NULL) { |
9925 | 0 | xmlRelaxNGNewStates(ctxt, |
9926 | 0 | res->nbState - base); |
9927 | 0 | states = ctxt->states; |
9928 | 0 | if (states == NULL) { |
9929 | 0 | progress = 0; |
9930 | 0 | break; |
9931 | 0 | } |
9932 | 0 | } |
9933 | 0 | states->nbState = 0; |
9934 | 0 | for (i = base; i < res->nbState; i++) |
9935 | 0 | xmlRelaxNGAddStates(ctxt, states, |
9936 | 0 | xmlRelaxNGCopyValidState |
9937 | 0 | (ctxt, res->tabState[i])); |
9938 | 0 | ctxt->states = states; |
9939 | 0 | } |
9940 | 0 | } |
9941 | 0 | } while (progress == 1); |
9942 | 0 | if (states != NULL) { |
9943 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
9944 | 0 | } |
9945 | 0 | ctxt->states = res; |
9946 | 0 | ctxt->flags = oldflags; |
9947 | | #if 0 |
9948 | | /* |
9949 | | * errors may have to be propagated back... |
9950 | | */ |
9951 | | if (ctxt->errNr > errNr) |
9952 | | xmlRelaxNGPopErrors(ctxt, errNr); |
9953 | | #endif |
9954 | 0 | ret = 0; |
9955 | 0 | break; |
9956 | 0 | } |
9957 | 0 | case XML_RELAXNG_CHOICE:{ |
9958 | 0 | xmlRelaxNGDefinePtr list = NULL; |
9959 | 0 | xmlRelaxNGStatesPtr states = NULL; |
9960 | |
|
9961 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
9962 | |
|
9963 | 0 | errNr = ctxt->errNr; |
9964 | 0 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL) && |
9965 | 0 | (node != NULL)) { |
9966 | | /* |
9967 | | * node == NULL can't be optimized since IS_TRIABLE |
9968 | | * doesn't account for choice which may lead to |
9969 | | * only attributes. |
9970 | | */ |
9971 | 0 | xmlHashTablePtr triage = |
9972 | 0 | (xmlHashTablePtr) define->data; |
9973 | | |
9974 | | /* |
9975 | | * Something we can optimize cleanly there is only one |
9976 | | * possible branch out ! |
9977 | | */ |
9978 | 0 | if ((node->type == XML_TEXT_NODE) || |
9979 | 0 | (node->type == XML_CDATA_SECTION_NODE)) { |
9980 | 0 | list = |
9981 | 0 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
9982 | 0 | } else if (node->type == XML_ELEMENT_NODE) { |
9983 | 0 | if (node->ns != NULL) { |
9984 | 0 | list = xmlHashLookup2(triage, node->name, |
9985 | 0 | node->ns->href); |
9986 | 0 | if (list == NULL) |
9987 | 0 | list = |
9988 | 0 | xmlHashLookup2(triage, BAD_CAST "#any", |
9989 | 0 | node->ns->href); |
9990 | 0 | } else |
9991 | 0 | list = |
9992 | 0 | xmlHashLookup2(triage, node->name, NULL); |
9993 | 0 | if (list == NULL) |
9994 | 0 | list = |
9995 | 0 | xmlHashLookup2(triage, BAD_CAST "#any", |
9996 | 0 | NULL); |
9997 | 0 | } |
9998 | 0 | if (list == NULL) { |
9999 | 0 | ret = -1; |
10000 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
10001 | 0 | break; |
10002 | 0 | } |
10003 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
10004 | 0 | if (ret == 0) { |
10005 | 0 | } |
10006 | 0 | break; |
10007 | 0 | } |
10008 | | |
10009 | 0 | list = define->content; |
10010 | 0 | oldflags = ctxt->flags; |
10011 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
10012 | |
|
10013 | 0 | while (list != NULL) { |
10014 | 0 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
10015 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
10016 | 0 | if (ret == 0) { |
10017 | 0 | if (states == NULL) { |
10018 | 0 | states = xmlRelaxNGNewStates(ctxt, 1); |
10019 | 0 | } |
10020 | 0 | if (ctxt->state != NULL) { |
10021 | 0 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
10022 | 0 | } else if (ctxt->states != NULL) { |
10023 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
10024 | 0 | xmlRelaxNGAddStates(ctxt, states, |
10025 | 0 | ctxt->states-> |
10026 | 0 | tabState[i]); |
10027 | 0 | } |
10028 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10029 | 0 | ctxt->states = NULL; |
10030 | 0 | } |
10031 | 0 | } else { |
10032 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10033 | 0 | } |
10034 | 0 | ctxt->state = oldstate; |
10035 | 0 | list = list->next; |
10036 | 0 | } |
10037 | 0 | if (states != NULL) { |
10038 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
10039 | 0 | ctxt->states = states; |
10040 | 0 | ctxt->state = NULL; |
10041 | 0 | ret = 0; |
10042 | 0 | } else { |
10043 | 0 | ctxt->states = NULL; |
10044 | 0 | } |
10045 | 0 | ctxt->flags = oldflags; |
10046 | 0 | if (ret != 0) { |
10047 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
10048 | 0 | xmlRelaxNGDumpValidError(ctxt); |
10049 | 0 | } |
10050 | 0 | } else { |
10051 | 0 | if (ctxt->errNr > errNr) |
10052 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
10053 | 0 | } |
10054 | 0 | break; |
10055 | 0 | } |
10056 | 0 | case XML_RELAXNG_DEF: |
10057 | 0 | case XML_RELAXNG_GROUP: |
10058 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
10059 | 0 | break; |
10060 | 0 | case XML_RELAXNG_INTERLEAVE: |
10061 | 0 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
10062 | 0 | break; |
10063 | 0 | case XML_RELAXNG_ATTRIBUTE: |
10064 | 0 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
10065 | 0 | break; |
10066 | 0 | case XML_RELAXNG_START: |
10067 | 0 | case XML_RELAXNG_NOOP: |
10068 | 0 | case XML_RELAXNG_REF: |
10069 | 0 | case XML_RELAXNG_EXTERNALREF: |
10070 | 0 | case XML_RELAXNG_PARENTREF: |
10071 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
10072 | 0 | break; |
10073 | 0 | case XML_RELAXNG_DATATYPE:{ |
10074 | 0 | xmlNodePtr child; |
10075 | 0 | xmlChar *content = NULL; |
10076 | |
|
10077 | 0 | child = node; |
10078 | 0 | while (child != NULL) { |
10079 | 0 | if (child->type == XML_ELEMENT_NODE) { |
10080 | 0 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
10081 | 0 | node->parent->name); |
10082 | 0 | ret = -1; |
10083 | 0 | break; |
10084 | 0 | } else if ((child->type == XML_TEXT_NODE) || |
10085 | 0 | (child->type == XML_CDATA_SECTION_NODE)) { |
10086 | 0 | content = xmlStrcat(content, child->content); |
10087 | 0 | } |
10088 | | /* TODO: handle entities ... */ |
10089 | 0 | child = child->next; |
10090 | 0 | } |
10091 | 0 | if (ret == -1) { |
10092 | 0 | if (content != NULL) |
10093 | 0 | xmlFree(content); |
10094 | 0 | break; |
10095 | 0 | } |
10096 | 0 | if (content == NULL) { |
10097 | 0 | content = xmlStrdup(BAD_CAST ""); |
10098 | 0 | if (content == NULL) { |
10099 | 0 | xmlRngVErrMemory(ctxt); |
10100 | 0 | ret = -1; |
10101 | 0 | break; |
10102 | 0 | } |
10103 | 0 | } |
10104 | 0 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
10105 | 0 | ctxt->state->seq); |
10106 | 0 | if (ret == -1) { |
10107 | 0 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
10108 | 0 | } else if (ret == 0) { |
10109 | 0 | ctxt->state->seq = NULL; |
10110 | 0 | } |
10111 | 0 | if (content != NULL) |
10112 | 0 | xmlFree(content); |
10113 | 0 | break; |
10114 | 0 | } |
10115 | 0 | case XML_RELAXNG_VALUE:{ |
10116 | 0 | xmlChar *content = NULL; |
10117 | 0 | xmlChar *oldvalue; |
10118 | 0 | xmlNodePtr child; |
10119 | |
|
10120 | 0 | child = node; |
10121 | 0 | while (child != NULL) { |
10122 | 0 | if (child->type == XML_ELEMENT_NODE) { |
10123 | 0 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
10124 | 0 | node->parent->name); |
10125 | 0 | ret = -1; |
10126 | 0 | break; |
10127 | 0 | } else if ((child->type == XML_TEXT_NODE) || |
10128 | 0 | (child->type == XML_CDATA_SECTION_NODE)) { |
10129 | 0 | content = xmlStrcat(content, child->content); |
10130 | 0 | } |
10131 | | /* TODO: handle entities ... */ |
10132 | 0 | child = child->next; |
10133 | 0 | } |
10134 | 0 | if (ret == -1) { |
10135 | 0 | if (content != NULL) |
10136 | 0 | xmlFree(content); |
10137 | 0 | break; |
10138 | 0 | } |
10139 | 0 | if (content == NULL) { |
10140 | 0 | content = xmlStrdup(BAD_CAST ""); |
10141 | 0 | if (content == NULL) { |
10142 | 0 | xmlRngVErrMemory(ctxt); |
10143 | 0 | ret = -1; |
10144 | 0 | break; |
10145 | 0 | } |
10146 | 0 | } |
10147 | 0 | oldvalue = ctxt->state->value; |
10148 | 0 | ctxt->state->value = content; |
10149 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define); |
10150 | 0 | ctxt->state->value = oldvalue; |
10151 | 0 | if (ret == -1) { |
10152 | 0 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
10153 | 0 | } else if (ret == 0) { |
10154 | 0 | ctxt->state->seq = NULL; |
10155 | 0 | } |
10156 | 0 | if (content != NULL) |
10157 | 0 | xmlFree(content); |
10158 | 0 | break; |
10159 | 0 | } |
10160 | 0 | case XML_RELAXNG_LIST:{ |
10161 | 0 | xmlChar *content; |
10162 | 0 | xmlNodePtr child; |
10163 | 0 | xmlChar *oldvalue, *oldendvalue; |
10164 | 0 | int len; |
10165 | | |
10166 | | /* |
10167 | | * Make sure it's only text nodes |
10168 | | */ |
10169 | |
|
10170 | 0 | content = NULL; |
10171 | 0 | child = node; |
10172 | 0 | while (child != NULL) { |
10173 | 0 | if (child->type == XML_ELEMENT_NODE) { |
10174 | 0 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
10175 | 0 | node->parent->name); |
10176 | 0 | ret = -1; |
10177 | 0 | break; |
10178 | 0 | } else if ((child->type == XML_TEXT_NODE) || |
10179 | 0 | (child->type == XML_CDATA_SECTION_NODE)) { |
10180 | 0 | content = xmlStrcat(content, child->content); |
10181 | 0 | } |
10182 | | /* TODO: handle entities ... */ |
10183 | 0 | child = child->next; |
10184 | 0 | } |
10185 | 0 | if (ret == -1) { |
10186 | 0 | if (content != NULL) |
10187 | 0 | xmlFree(content); |
10188 | 0 | break; |
10189 | 0 | } |
10190 | 0 | if (content == NULL) { |
10191 | 0 | content = xmlStrdup(BAD_CAST ""); |
10192 | 0 | if (content == NULL) { |
10193 | 0 | xmlRngVErrMemory(ctxt); |
10194 | 0 | ret = -1; |
10195 | 0 | break; |
10196 | 0 | } |
10197 | 0 | } |
10198 | 0 | len = xmlStrlen(content); |
10199 | 0 | oldvalue = ctxt->state->value; |
10200 | 0 | oldendvalue = ctxt->state->endvalue; |
10201 | 0 | ctxt->state->value = content; |
10202 | 0 | ctxt->state->endvalue = content + len; |
10203 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define); |
10204 | 0 | ctxt->state->value = oldvalue; |
10205 | 0 | ctxt->state->endvalue = oldendvalue; |
10206 | 0 | if (ret == -1) { |
10207 | 0 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
10208 | 0 | } else if ((ret == 0) && (node != NULL)) { |
10209 | 0 | ctxt->state->seq = node->next; |
10210 | 0 | } |
10211 | 0 | if (content != NULL) |
10212 | 0 | xmlFree(content); |
10213 | 0 | break; |
10214 | 0 | } |
10215 | 0 | case XML_RELAXNG_EXCEPT: |
10216 | 0 | case XML_RELAXNG_PARAM: |
10217 | | /* TODO */ |
10218 | 0 | ret = -1; |
10219 | 0 | break; |
10220 | 0 | } |
10221 | 0 | ctxt->depth--; |
10222 | 0 | return (ret); |
10223 | 0 | } |
10224 | | |
10225 | | /** |
10226 | | * Validate the current node lists against the definition |
10227 | | * |
10228 | | * @param ctxt a Relax-NG validation context |
10229 | | * @param define the definition to verify |
10230 | | * @returns 0 if the validation succeeded or an error code. |
10231 | | */ |
10232 | | static int |
10233 | | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
10234 | | xmlRelaxNGDefinePtr define) |
10235 | 0 | { |
10236 | 0 | xmlRelaxNGStatesPtr states, res; |
10237 | 0 | int i, j, k, ret, oldflags; |
10238 | | |
10239 | | /* |
10240 | | * We should NOT have both ctxt->state and ctxt->states |
10241 | | */ |
10242 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10243 | | /* TODO */ |
10244 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10245 | 0 | ctxt->state = NULL; |
10246 | 0 | } |
10247 | |
|
10248 | 0 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
10249 | 0 | if (ctxt->states != NULL) { |
10250 | 0 | ctxt->state = ctxt->states->tabState[0]; |
10251 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10252 | 0 | ctxt->states = NULL; |
10253 | 0 | } |
10254 | 0 | ret = xmlRelaxNGValidateState(ctxt, define); |
10255 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10256 | | /* TODO */ |
10257 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10258 | 0 | ctxt->state = NULL; |
10259 | 0 | } |
10260 | 0 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
10261 | 0 | ctxt->state = ctxt->states->tabState[0]; |
10262 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10263 | 0 | ctxt->states = NULL; |
10264 | 0 | } |
10265 | 0 | return (ret); |
10266 | 0 | } |
10267 | | |
10268 | 0 | states = ctxt->states; |
10269 | 0 | ctxt->states = NULL; |
10270 | 0 | res = NULL; |
10271 | 0 | j = 0; |
10272 | 0 | oldflags = ctxt->flags; |
10273 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
10274 | 0 | for (i = 0; i < states->nbState; i++) { |
10275 | 0 | ctxt->state = states->tabState[i]; |
10276 | 0 | ctxt->states = NULL; |
10277 | 0 | ret = xmlRelaxNGValidateState(ctxt, define); |
10278 | | /* |
10279 | | * We should NOT have both ctxt->state and ctxt->states |
10280 | | */ |
10281 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10282 | | /* TODO */ |
10283 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10284 | 0 | ctxt->state = NULL; |
10285 | 0 | } |
10286 | 0 | if (ret == 0) { |
10287 | 0 | if (ctxt->states == NULL) { |
10288 | 0 | if (res != NULL) { |
10289 | | /* add the state to the container */ |
10290 | 0 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
10291 | 0 | ctxt->state = NULL; |
10292 | 0 | } else { |
10293 | | /* add the state directly in states */ |
10294 | 0 | states->tabState[j++] = ctxt->state; |
10295 | 0 | ctxt->state = NULL; |
10296 | 0 | } |
10297 | 0 | } else { |
10298 | 0 | if (res == NULL) { |
10299 | | /* make it the new container and copy other results */ |
10300 | 0 | res = ctxt->states; |
10301 | 0 | ctxt->states = NULL; |
10302 | 0 | for (k = 0; k < j; k++) |
10303 | 0 | xmlRelaxNGAddStates(ctxt, res, |
10304 | 0 | states->tabState[k]); |
10305 | 0 | } else { |
10306 | | /* add all the new results to res and reff the container */ |
10307 | 0 | for (k = 0; k < ctxt->states->nbState; k++) |
10308 | 0 | xmlRelaxNGAddStates(ctxt, res, |
10309 | 0 | ctxt->states->tabState[k]); |
10310 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10311 | 0 | ctxt->states = NULL; |
10312 | 0 | } |
10313 | 0 | } |
10314 | 0 | } else { |
10315 | 0 | if (ctxt->state != NULL) { |
10316 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10317 | 0 | ctxt->state = NULL; |
10318 | 0 | } else if (ctxt->states != NULL) { |
10319 | 0 | for (k = 0; k < ctxt->states->nbState; k++) |
10320 | 0 | xmlRelaxNGFreeValidState(ctxt, |
10321 | 0 | ctxt->states->tabState[k]); |
10322 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10323 | 0 | ctxt->states = NULL; |
10324 | 0 | } |
10325 | 0 | } |
10326 | 0 | } |
10327 | 0 | ctxt->flags = oldflags; |
10328 | 0 | if (res != NULL) { |
10329 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10330 | 0 | ctxt->states = res; |
10331 | 0 | ret = 0; |
10332 | 0 | } else if (j > 1) { |
10333 | 0 | states->nbState = j; |
10334 | 0 | ctxt->states = states; |
10335 | 0 | ret = 0; |
10336 | 0 | } else if (j == 1) { |
10337 | 0 | ctxt->state = states->tabState[0]; |
10338 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10339 | 0 | ret = 0; |
10340 | 0 | } else { |
10341 | 0 | ret = -1; |
10342 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10343 | 0 | if (ctxt->states != NULL) { |
10344 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10345 | 0 | ctxt->states = NULL; |
10346 | 0 | } |
10347 | 0 | } |
10348 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10349 | | /* TODO */ |
10350 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10351 | 0 | ctxt->state = NULL; |
10352 | 0 | } |
10353 | 0 | return (ret); |
10354 | 0 | } |
10355 | | |
10356 | | /** |
10357 | | * Validate the given document |
10358 | | * |
10359 | | * @param ctxt a Relax-NG validation context |
10360 | | * @param doc the document |
10361 | | * @returns 0 if the validation succeeded or an error code. |
10362 | | */ |
10363 | | static int |
10364 | | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
10365 | 0 | { |
10366 | 0 | int ret; |
10367 | 0 | xmlRelaxNGPtr schema; |
10368 | 0 | xmlRelaxNGGrammarPtr grammar; |
10369 | 0 | xmlRelaxNGValidStatePtr state; |
10370 | 0 | xmlNodePtr node; |
10371 | |
|
10372 | 0 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
10373 | 0 | return (-1); |
10374 | | |
10375 | 0 | ctxt->errNo = XML_RELAXNG_OK; |
10376 | 0 | schema = ctxt->schema; |
10377 | 0 | grammar = schema->topgrammar; |
10378 | 0 | if (grammar == NULL) { |
10379 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
10380 | 0 | return (-1); |
10381 | 0 | } |
10382 | 0 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
10383 | 0 | ctxt->state = state; |
10384 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
10385 | 0 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
10386 | 0 | state = ctxt->state; |
10387 | 0 | node = state->seq; |
10388 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
10389 | 0 | if (node != NULL) { |
10390 | 0 | if (ret != -1) { |
10391 | 0 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
10392 | 0 | ret = -1; |
10393 | 0 | } |
10394 | 0 | } |
10395 | 0 | } else if (ctxt->states != NULL) { |
10396 | 0 | int i; |
10397 | 0 | int tmp = -1; |
10398 | |
|
10399 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
10400 | 0 | state = ctxt->states->tabState[i]; |
10401 | 0 | node = state->seq; |
10402 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
10403 | 0 | if (node == NULL) |
10404 | 0 | tmp = 0; |
10405 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
10406 | 0 | } |
10407 | 0 | if (tmp == -1) { |
10408 | 0 | if (ret != -1) { |
10409 | 0 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
10410 | 0 | ret = -1; |
10411 | 0 | } |
10412 | 0 | } |
10413 | 0 | } |
10414 | 0 | if (ctxt->state != NULL) { |
10415 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10416 | 0 | ctxt->state = NULL; |
10417 | 0 | } |
10418 | 0 | if (ret != 0) |
10419 | 0 | xmlRelaxNGDumpValidError(ctxt); |
10420 | 0 | #ifdef LIBXML_VALID_ENABLED |
10421 | 0 | if (ctxt->idref == 1) { |
10422 | 0 | xmlValidCtxt vctxt; |
10423 | |
|
10424 | 0 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
10425 | 0 | vctxt.valid = 1; |
10426 | |
|
10427 | 0 | if (ctxt->error == NULL) { |
10428 | 0 | vctxt.error = xmlGenericError; |
10429 | 0 | vctxt.warning = xmlGenericError; |
10430 | 0 | vctxt.userData = xmlGenericErrorContext; |
10431 | 0 | } else { |
10432 | 0 | vctxt.error = ctxt->error; |
10433 | 0 | vctxt.warning = ctxt->warning; |
10434 | 0 | vctxt.userData = ctxt->userData; |
10435 | 0 | } |
10436 | |
|
10437 | 0 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
10438 | 0 | ret = -1; |
10439 | 0 | } |
10440 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
10441 | 0 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
10442 | 0 | ret = -1; |
10443 | |
|
10444 | 0 | return (ret); |
10445 | 0 | } |
10446 | | |
10447 | | /** |
10448 | | * Call this routine to speed up XPath computation on static documents. |
10449 | | * This stamps all the element nodes with the document order |
10450 | | * Like for line information, the order is kept in the element->content |
10451 | | * field, the value stored is actually - the node number (starting at -1) |
10452 | | * to be able to differentiate from line numbers. |
10453 | | * |
10454 | | * @param node an input element or document |
10455 | | * @returns the number of elements found in the document or -1 in case |
10456 | | * of error. |
10457 | | */ |
10458 | | static void |
10459 | 0 | xmlRelaxNGCleanPSVI(xmlNodePtr node) { |
10460 | 0 | xmlNodePtr cur; |
10461 | |
|
10462 | 0 | if ((node == NULL) || |
10463 | 0 | ((node->type != XML_ELEMENT_NODE) && |
10464 | 0 | (node->type != XML_DOCUMENT_NODE) && |
10465 | 0 | (node->type != XML_HTML_DOCUMENT_NODE))) |
10466 | 0 | return; |
10467 | 0 | if (node->type == XML_ELEMENT_NODE) |
10468 | 0 | node->psvi = NULL; |
10469 | |
|
10470 | 0 | cur = node->children; |
10471 | 0 | while (cur != NULL) { |
10472 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
10473 | 0 | cur->psvi = NULL; |
10474 | 0 | if (cur->children != NULL) { |
10475 | 0 | cur = cur->children; |
10476 | 0 | continue; |
10477 | 0 | } |
10478 | 0 | } |
10479 | 0 | if (cur->next != NULL) { |
10480 | 0 | cur = cur->next; |
10481 | 0 | continue; |
10482 | 0 | } |
10483 | 0 | do { |
10484 | 0 | cur = cur->parent; |
10485 | 0 | if (cur == NULL) |
10486 | 0 | break; |
10487 | 0 | if (cur == node) { |
10488 | 0 | cur = NULL; |
10489 | 0 | break; |
10490 | 0 | } |
10491 | 0 | if (cur->next != NULL) { |
10492 | 0 | cur = cur->next; |
10493 | 0 | break; |
10494 | 0 | } |
10495 | 0 | } while (cur != NULL); |
10496 | 0 | } |
10497 | 0 | } |
10498 | | /************************************************************************ |
10499 | | * * |
10500 | | * Validation interfaces * |
10501 | | * * |
10502 | | ************************************************************************/ |
10503 | | |
10504 | | /** |
10505 | | * Create an XML RelaxNGs validation context based on the given schema |
10506 | | * |
10507 | | * @param schema a precompiled XML RelaxNGs |
10508 | | * @returns the validation context or NULL in case of error |
10509 | | */ |
10510 | | xmlRelaxNGValidCtxt * |
10511 | | xmlRelaxNGNewValidCtxt(xmlRelaxNG *schema) |
10512 | 0 | { |
10513 | 0 | xmlRelaxNGValidCtxtPtr ret; |
10514 | |
|
10515 | 0 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
10516 | 0 | if (ret == NULL) { |
10517 | 0 | xmlRngVErrMemory(NULL); |
10518 | 0 | return (NULL); |
10519 | 0 | } |
10520 | 0 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
10521 | 0 | ret->schema = schema; |
10522 | 0 | ret->errNr = 0; |
10523 | 0 | ret->errMax = 0; |
10524 | 0 | ret->err = NULL; |
10525 | 0 | ret->errTab = NULL; |
10526 | 0 | if (schema != NULL) |
10527 | 0 | ret->idref = schema->idref; |
10528 | 0 | ret->states = NULL; |
10529 | 0 | ret->freeState = NULL; |
10530 | 0 | ret->freeStates = NULL; |
10531 | 0 | ret->errNo = XML_RELAXNG_OK; |
10532 | 0 | return (ret); |
10533 | 0 | } |
10534 | | |
10535 | | /** |
10536 | | * Free the resources associated to the schema validation context |
10537 | | * |
10538 | | * @param ctxt the schema validation context |
10539 | | */ |
10540 | | void |
10541 | | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxt *ctxt) |
10542 | 0 | { |
10543 | 0 | int k; |
10544 | |
|
10545 | 0 | if (ctxt == NULL) |
10546 | 0 | return; |
10547 | 0 | if (ctxt->states != NULL) |
10548 | 0 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
10549 | 0 | if (ctxt->freeState != NULL) { |
10550 | 0 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
10551 | 0 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
10552 | 0 | } |
10553 | 0 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
10554 | 0 | } |
10555 | 0 | if (ctxt->freeStates != NULL) { |
10556 | 0 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
10557 | 0 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
10558 | 0 | } |
10559 | 0 | xmlFree(ctxt->freeStates); |
10560 | 0 | } |
10561 | 0 | if (ctxt->errTab != NULL) |
10562 | 0 | xmlFree(ctxt->errTab); |
10563 | 0 | if (ctxt->elemTab != NULL) { |
10564 | 0 | xmlRegExecCtxtPtr exec; |
10565 | |
|
10566 | 0 | exec = xmlRelaxNGElemPop(ctxt); |
10567 | 0 | while (exec != NULL) { |
10568 | 0 | xmlRegFreeExecCtxt(exec); |
10569 | 0 | exec = xmlRelaxNGElemPop(ctxt); |
10570 | 0 | } |
10571 | 0 | xmlFree(ctxt->elemTab); |
10572 | 0 | } |
10573 | 0 | xmlFree(ctxt); |
10574 | 0 | } |
10575 | | |
10576 | | /** |
10577 | | * Set the error and warning callback information |
10578 | | * |
10579 | | * @deprecated Use #xmlRelaxNGSetValidStructuredErrors. |
10580 | | * |
10581 | | * @param ctxt a Relax-NG validation context |
10582 | | * @param err the error function |
10583 | | * @param warn the warning function |
10584 | | * @param ctx the functions context |
10585 | | */ |
10586 | | void |
10587 | | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxt *ctxt, |
10588 | | xmlRelaxNGValidityErrorFunc err, |
10589 | | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
10590 | 0 | { |
10591 | 0 | if (ctxt == NULL) |
10592 | 0 | return; |
10593 | 0 | ctxt->error = err; |
10594 | 0 | ctxt->warning = warn; |
10595 | 0 | ctxt->userData = ctx; |
10596 | 0 | ctxt->serror = NULL; |
10597 | 0 | } |
10598 | | |
10599 | | /** |
10600 | | * Set the structured error callback |
10601 | | * |
10602 | | * @param ctxt a Relax-NG validation context |
10603 | | * @param serror the structured error function |
10604 | | * @param ctx the functions context |
10605 | | */ |
10606 | | void |
10607 | | xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxt *ctxt, |
10608 | | xmlStructuredErrorFunc serror, void *ctx) |
10609 | 0 | { |
10610 | 0 | if (ctxt == NULL) |
10611 | 0 | return; |
10612 | 0 | ctxt->serror = serror; |
10613 | 0 | ctxt->error = NULL; |
10614 | 0 | ctxt->warning = NULL; |
10615 | 0 | ctxt->userData = ctx; |
10616 | 0 | } |
10617 | | |
10618 | | /** |
10619 | | * Get the error and warning callback information |
10620 | | * |
10621 | | * @param ctxt a Relax-NG validation context |
10622 | | * @param err the error function result |
10623 | | * @param warn the warning function result |
10624 | | * @param ctx the functions context result |
10625 | | * @returns -1 in case of error and 0 otherwise |
10626 | | */ |
10627 | | int |
10628 | | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxt *ctxt, |
10629 | | xmlRelaxNGValidityErrorFunc * err, |
10630 | | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
10631 | 0 | { |
10632 | 0 | if (ctxt == NULL) |
10633 | 0 | return (-1); |
10634 | 0 | if (err != NULL) |
10635 | 0 | *err = ctxt->error; |
10636 | 0 | if (warn != NULL) |
10637 | 0 | *warn = ctxt->warning; |
10638 | 0 | if (ctx != NULL) |
10639 | 0 | *ctx = ctxt->userData; |
10640 | 0 | return (0); |
10641 | 0 | } |
10642 | | |
10643 | | /** |
10644 | | * Validate a document tree in memory. |
10645 | | * |
10646 | | * @param ctxt a Relax-NG validation context |
10647 | | * @param doc a parsed document tree |
10648 | | * @returns 0 if the document is valid, a positive error code |
10649 | | * number otherwise and -1 in case of internal or API error. |
10650 | | */ |
10651 | | int |
10652 | | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxt *ctxt, xmlDoc *doc) |
10653 | 0 | { |
10654 | 0 | int ret; |
10655 | |
|
10656 | 0 | if ((ctxt == NULL) || (doc == NULL)) |
10657 | 0 | return (-1); |
10658 | | |
10659 | 0 | ctxt->doc = doc; |
10660 | |
|
10661 | 0 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
10662 | | /* |
10663 | | * Remove all left PSVI |
10664 | | */ |
10665 | 0 | xmlRelaxNGCleanPSVI((xmlNodePtr) doc); |
10666 | | |
10667 | | /* |
10668 | | * TODO: build error codes |
10669 | | */ |
10670 | 0 | if (ret == -1) |
10671 | 0 | return (1); |
10672 | 0 | return (ret); |
10673 | 0 | } |
10674 | | |
10675 | | #endif /* LIBXML_RELAXNG_ENABLED */ |