/src/fontconfig/subprojects/libxml2-2.12.6/relaxng.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * relaxng.c : implementation of the Relax-NG handling and validity checking |
3 | | * |
4 | | * See Copyright for the status of this software. |
5 | | * |
6 | | * Daniel Veillard <veillard@redhat.com> |
7 | | */ |
8 | | |
9 | | /** |
10 | | * TODO: |
11 | | * - add support for DTD compatibility spec |
12 | | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
13 | | * - report better mem allocations pbms at runtime and abort immediately. |
14 | | */ |
15 | | |
16 | | #define IN_LIBXML |
17 | | #include "libxml.h" |
18 | | |
19 | | #ifdef LIBXML_SCHEMAS_ENABLED |
20 | | |
21 | | #include <string.h> |
22 | | #include <stdio.h> |
23 | | #include <stddef.h> |
24 | | #include <libxml/xmlmemory.h> |
25 | | #include <libxml/parser.h> |
26 | | #include <libxml/parserInternals.h> |
27 | | #include <libxml/hash.h> |
28 | | #include <libxml/uri.h> |
29 | | |
30 | | #include <libxml/relaxng.h> |
31 | | |
32 | | #include <libxml/xmlschemastypes.h> |
33 | | #include <libxml/xmlautomata.h> |
34 | | #include <libxml/xmlregexp.h> |
35 | | #include <libxml/xmlschemastypes.h> |
36 | | |
37 | | #include "private/error.h" |
38 | | #include "private/regexp.h" |
39 | | #include "private/string.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 | | #define TODO \ |
57 | 0 | xmlGenericError(xmlGenericErrorContext, \ |
58 | 0 | "Unimplemented block at %s:%d\n", \ |
59 | 0 | __FILE__, __LINE__); |
60 | | |
61 | | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
62 | | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
63 | | |
64 | | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
65 | | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
66 | | |
67 | | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
68 | | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
69 | | |
70 | | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
71 | | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
72 | | |
73 | | typedef enum { |
74 | | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
75 | | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
76 | | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
77 | | } xmlRelaxNGCombine; |
78 | | |
79 | | typedef enum { |
80 | | XML_RELAXNG_CONTENT_ERROR = -1, |
81 | | XML_RELAXNG_CONTENT_EMPTY = 0, |
82 | | XML_RELAXNG_CONTENT_SIMPLE, |
83 | | XML_RELAXNG_CONTENT_COMPLEX |
84 | | } xmlRelaxNGContentType; |
85 | | |
86 | | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
87 | | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
88 | | |
89 | | struct _xmlRelaxNGGrammar { |
90 | | xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */ |
91 | | xmlRelaxNGGrammarPtr children; /* the children grammar if any */ |
92 | | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
93 | | xmlRelaxNGDefinePtr start; /* <start> content */ |
94 | | xmlRelaxNGCombine combine; /* the default combine value */ |
95 | | xmlRelaxNGDefinePtr startList; /* list of <start> definitions */ |
96 | | xmlHashTablePtr defs; /* define* */ |
97 | | xmlHashTablePtr refs; /* references */ |
98 | | }; |
99 | | |
100 | | |
101 | | typedef enum { |
102 | | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
103 | | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
104 | | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
105 | | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
106 | | XML_RELAXNG_TEXT, /* textual content */ |
107 | | XML_RELAXNG_ELEMENT, /* an element */ |
108 | | XML_RELAXNG_DATATYPE, /* external data type definition */ |
109 | | XML_RELAXNG_PARAM, /* external data type parameter */ |
110 | | XML_RELAXNG_VALUE, /* value from an external data type definition */ |
111 | | XML_RELAXNG_LIST, /* a list of patterns */ |
112 | | XML_RELAXNG_ATTRIBUTE, /* an attribute following a pattern */ |
113 | | XML_RELAXNG_DEF, /* a definition */ |
114 | | XML_RELAXNG_REF, /* reference to a definition */ |
115 | | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
116 | | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
117 | | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
118 | | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
119 | | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
120 | | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
121 | | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
122 | | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
123 | | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
124 | | } xmlRelaxNGType; |
125 | | |
126 | 0 | #define IS_NULLABLE (1 << 0) |
127 | 0 | #define IS_NOT_NULLABLE (1 << 1) |
128 | 0 | #define IS_INDETERMINIST (1 << 2) |
129 | 0 | #define IS_MIXED (1 << 3) |
130 | 0 | #define IS_TRIABLE (1 << 4) |
131 | 0 | #define IS_PROCESSED (1 << 5) |
132 | 0 | #define IS_COMPILABLE (1 << 6) |
133 | 0 | #define IS_NOT_COMPILABLE (1 << 7) |
134 | 0 | #define IS_EXTERNAL_REF (1 << 8) |
135 | | |
136 | | struct _xmlRelaxNGDefine { |
137 | | xmlRelaxNGType type; /* the type of definition */ |
138 | | xmlNodePtr node; /* the node in the source */ |
139 | | xmlChar *name; /* the element local name if present */ |
140 | | xmlChar *ns; /* the namespace local name if present */ |
141 | | xmlChar *value; /* value when available */ |
142 | | void *data; /* data lib or specific pointer */ |
143 | | xmlRelaxNGDefinePtr content; /* the expected content */ |
144 | | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
145 | | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
146 | | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
147 | | xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */ |
148 | | xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */ |
149 | | short depth; /* used for the cycle detection */ |
150 | | short dflags; /* define related flags */ |
151 | | xmlRegexpPtr contModel; /* a compiled content model if available */ |
152 | | }; |
153 | | |
154 | | /** |
155 | | * _xmlRelaxNG: |
156 | | * |
157 | | * A RelaxNGs definition |
158 | | */ |
159 | | struct _xmlRelaxNG { |
160 | | void *_private; /* unused by the library for users or bindings */ |
161 | | xmlRelaxNGGrammarPtr topgrammar; |
162 | | xmlDocPtr doc; |
163 | | |
164 | | int idref; /* requires idref checking */ |
165 | | |
166 | | xmlHashTablePtr defs; /* define */ |
167 | | xmlHashTablePtr refs; /* references */ |
168 | | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
169 | | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
170 | | int defNr; /* number of defines used */ |
171 | | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
172 | | |
173 | | }; |
174 | | |
175 | 0 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
176 | 0 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
177 | 0 | #define XML_RELAXNG_IN_LIST (1 << 2) |
178 | 0 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
179 | 0 | #define XML_RELAXNG_IN_START (1 << 4) |
180 | 0 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
181 | 0 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
182 | 0 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
183 | 0 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
184 | 0 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
185 | | |
186 | | struct _xmlRelaxNGParserCtxt { |
187 | | void *userData; /* user specific data block */ |
188 | | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
189 | | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
190 | | xmlStructuredErrorFunc serror; |
191 | | xmlRelaxNGValidErr err; |
192 | | |
193 | | xmlRelaxNGPtr schema; /* The schema in use */ |
194 | | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
195 | | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
196 | | int flags; /* parser flags */ |
197 | | int nbErrors; /* number of errors at parse time */ |
198 | | int nbWarnings; /* number of warnings at parse time */ |
199 | | const xmlChar *define; /* the current define scope */ |
200 | | xmlRelaxNGDefinePtr def; /* the current define */ |
201 | | |
202 | | int nbInterleaves; |
203 | | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
204 | | |
205 | | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
206 | | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
207 | | xmlChar *URL; |
208 | | xmlDocPtr document; |
209 | | |
210 | | int defNr; /* number of defines used */ |
211 | | int defMax; /* number of defines allocated */ |
212 | | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
213 | | |
214 | | const char *buffer; |
215 | | int size; |
216 | | |
217 | | /* the document stack */ |
218 | | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
219 | | int docNr; /* Depth of the parsing stack */ |
220 | | int docMax; /* Max depth of the parsing stack */ |
221 | | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
222 | | |
223 | | /* the include stack */ |
224 | | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
225 | | int incNr; /* Depth of the include parsing stack */ |
226 | | int incMax; /* Max depth of the parsing stack */ |
227 | | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
228 | | |
229 | | int idref; /* requires idref checking */ |
230 | | |
231 | | /* used to compile content models */ |
232 | | xmlAutomataPtr am; /* the automata */ |
233 | | xmlAutomataStatePtr state; /* used to build the automata */ |
234 | | |
235 | | int crng; /* compact syntax and other flags */ |
236 | | int freedoc; /* need to free the document */ |
237 | | }; |
238 | | |
239 | 0 | #define FLAGS_IGNORABLE 1 |
240 | 0 | #define FLAGS_NEGATIVE 2 |
241 | 0 | #define FLAGS_MIXED_CONTENT 4 |
242 | 0 | #define FLAGS_NOERROR 8 |
243 | | |
244 | | /** |
245 | | * xmlRelaxNGInterleaveGroup: |
246 | | * |
247 | | * A RelaxNGs partition set associated to lists of definitions |
248 | | */ |
249 | | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
250 | | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
251 | | struct _xmlRelaxNGInterleaveGroup { |
252 | | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
253 | | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
254 | | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
255 | | }; |
256 | | |
257 | 0 | #define IS_DETERMINIST 1 |
258 | 0 | #define IS_NEEDCHECK 2 |
259 | | |
260 | | /** |
261 | | * xmlRelaxNGPartitions: |
262 | | * |
263 | | * A RelaxNGs partition associated to an interleave group |
264 | | */ |
265 | | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
266 | | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
267 | | struct _xmlRelaxNGPartition { |
268 | | int nbgroups; /* number of groups in the partitions */ |
269 | | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
270 | | * right group when possible */ |
271 | | int flags; /* determinist ? */ |
272 | | xmlRelaxNGInterleaveGroupPtr *groups; |
273 | | }; |
274 | | |
275 | | /** |
276 | | * xmlRelaxNGValidState: |
277 | | * |
278 | | * A RelaxNGs validation state |
279 | | */ |
280 | 0 | #define MAX_ATTR 20 |
281 | | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
282 | | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
283 | | struct _xmlRelaxNGValidState { |
284 | | xmlNodePtr node; /* the current node */ |
285 | | xmlNodePtr seq; /* the sequence of children left to validate */ |
286 | | int nbAttrs; /* the number of attributes */ |
287 | | int maxAttrs; /* the size of attrs */ |
288 | | int nbAttrLeft; /* the number of attributes left to validate */ |
289 | | xmlChar *value; /* the value when operating on string */ |
290 | | xmlChar *endvalue; /* the end value when operating on string */ |
291 | | xmlAttrPtr *attrs; /* the array of attributes */ |
292 | | }; |
293 | | |
294 | | /** |
295 | | * xmlRelaxNGStates: |
296 | | * |
297 | | * A RelaxNGs container for validation state |
298 | | */ |
299 | | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
300 | | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
301 | | struct _xmlRelaxNGStates { |
302 | | int nbState; /* the number of states */ |
303 | | int maxState; /* the size of the array */ |
304 | | xmlRelaxNGValidStatePtr *tabState; |
305 | | }; |
306 | | |
307 | 0 | #define ERROR_IS_DUP 1 |
308 | | |
309 | | /** |
310 | | * xmlRelaxNGValidError: |
311 | | * |
312 | | * A RelaxNGs validation error |
313 | | */ |
314 | | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
315 | | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
316 | | struct _xmlRelaxNGValidError { |
317 | | xmlRelaxNGValidErr err; /* the error number */ |
318 | | int flags; /* flags */ |
319 | | xmlNodePtr node; /* the current node */ |
320 | | xmlNodePtr seq; /* the current child */ |
321 | | const xmlChar *arg1; /* first arg */ |
322 | | const xmlChar *arg2; /* second arg */ |
323 | | }; |
324 | | |
325 | | /** |
326 | | * xmlRelaxNGValidCtxt: |
327 | | * |
328 | | * A RelaxNGs validation context |
329 | | */ |
330 | | |
331 | | struct _xmlRelaxNGValidCtxt { |
332 | | void *userData; /* user specific data block */ |
333 | | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
334 | | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
335 | | xmlStructuredErrorFunc serror; |
336 | | int nbErrors; /* number of errors in validation */ |
337 | | |
338 | | xmlRelaxNGPtr schema; /* The schema in use */ |
339 | | xmlDocPtr doc; /* the document being validated */ |
340 | | int flags; /* validation flags */ |
341 | | int depth; /* validation depth */ |
342 | | int idref; /* requires idref checking */ |
343 | | int errNo; /* the first error found */ |
344 | | |
345 | | /* |
346 | | * Errors accumulated in branches may have to be stacked to be |
347 | | * provided back when it's sure they affect validation. |
348 | | */ |
349 | | xmlRelaxNGValidErrorPtr err; /* Last error */ |
350 | | int errNr; /* Depth of the error stack */ |
351 | | int errMax; /* Max depth of the error stack */ |
352 | | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
353 | | |
354 | | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
355 | | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
356 | | |
357 | | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
358 | | int freeStatesNr; |
359 | | int freeStatesMax; |
360 | | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
361 | | |
362 | | /* |
363 | | * This is used for "progressive" validation |
364 | | */ |
365 | | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
366 | | int elemNr; /* the number of element validated */ |
367 | | int elemMax; /* the max depth of elements */ |
368 | | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
369 | | int pstate; /* progressive state */ |
370 | | xmlNodePtr pnode; /* the current node */ |
371 | | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
372 | | int perr; /* signal error in content model |
373 | | * outside the regexp */ |
374 | | }; |
375 | | |
376 | | /** |
377 | | * xmlRelaxNGInclude: |
378 | | * |
379 | | * Structure associated to a RelaxNGs document element |
380 | | */ |
381 | | struct _xmlRelaxNGInclude { |
382 | | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
383 | | xmlChar *href; /* the normalized href value */ |
384 | | xmlDocPtr doc; /* the associated XML document */ |
385 | | xmlRelaxNGDefinePtr content; /* the definitions */ |
386 | | xmlRelaxNGPtr schema; /* the schema */ |
387 | | }; |
388 | | |
389 | | /** |
390 | | * xmlRelaxNGDocument: |
391 | | * |
392 | | * Structure associated to a RelaxNGs document element |
393 | | */ |
394 | | struct _xmlRelaxNGDocument { |
395 | | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
396 | | xmlChar *href; /* the normalized href value */ |
397 | | xmlDocPtr doc; /* the associated XML document */ |
398 | | xmlRelaxNGDefinePtr content; /* the definitions */ |
399 | | xmlRelaxNGPtr schema; /* the schema */ |
400 | | int externalRef; /* 1 if an external ref */ |
401 | | }; |
402 | | |
403 | | |
404 | | /************************************************************************ |
405 | | * * |
406 | | * Some factorized error routines * |
407 | | * * |
408 | | ************************************************************************/ |
409 | | |
410 | | /** |
411 | | * xmlRngPErrMemory: |
412 | | * @ctxt: an Relax-NG parser context |
413 | | * @extra: extra information |
414 | | * |
415 | | * Handle a redefinition of attribute error |
416 | | */ |
417 | | static void |
418 | | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
419 | 0 | { |
420 | 0 | xmlStructuredErrorFunc schannel = NULL; |
421 | 0 | xmlGenericErrorFunc channel = NULL; |
422 | 0 | void *data = NULL; |
423 | |
|
424 | 0 | if (ctxt != NULL) { |
425 | 0 | if (ctxt->serror != NULL) |
426 | 0 | schannel = ctxt->serror; |
427 | 0 | else |
428 | 0 | channel = ctxt->error; |
429 | 0 | data = ctxt->userData; |
430 | 0 | ctxt->nbErrors++; |
431 | 0 | } |
432 | 0 | if (extra) |
433 | 0 | __xmlRaiseError(schannel, channel, data, |
434 | 0 | NULL, NULL, XML_FROM_RELAXNGP, |
435 | 0 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
436 | 0 | NULL, NULL, 0, 0, |
437 | 0 | "Memory allocation failed : %s\n", extra); |
438 | 0 | else |
439 | 0 | __xmlRaiseError(schannel, channel, data, |
440 | 0 | NULL, NULL, XML_FROM_RELAXNGP, |
441 | 0 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
442 | 0 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
443 | 0 | } |
444 | | |
445 | | /** |
446 | | * xmlRngVErrMemory: |
447 | | * @ctxt: a Relax-NG validation context |
448 | | * @extra: extra information |
449 | | * |
450 | | * Handle a redefinition of attribute error |
451 | | */ |
452 | | static void |
453 | | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
454 | 0 | { |
455 | 0 | xmlStructuredErrorFunc schannel = NULL; |
456 | 0 | xmlGenericErrorFunc channel = NULL; |
457 | 0 | void *data = NULL; |
458 | |
|
459 | 0 | if (ctxt != NULL) { |
460 | 0 | if (ctxt->serror != NULL) |
461 | 0 | schannel = ctxt->serror; |
462 | 0 | else |
463 | 0 | channel = ctxt->error; |
464 | 0 | data = ctxt->userData; |
465 | 0 | ctxt->nbErrors++; |
466 | 0 | } |
467 | 0 | if (extra) |
468 | 0 | __xmlRaiseError(schannel, channel, data, |
469 | 0 | NULL, NULL, XML_FROM_RELAXNGV, |
470 | 0 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
471 | 0 | NULL, NULL, 0, 0, |
472 | 0 | "Memory allocation failed : %s\n", extra); |
473 | 0 | else |
474 | 0 | __xmlRaiseError(schannel, channel, data, |
475 | 0 | NULL, NULL, XML_FROM_RELAXNGV, |
476 | 0 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
477 | 0 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
478 | 0 | } |
479 | | |
480 | | /** |
481 | | * xmlRngPErr: |
482 | | * @ctxt: a Relax-NG parser context |
483 | | * @node: the node raising the error |
484 | | * @error: the error code |
485 | | * @msg: message |
486 | | * @str1: extra info |
487 | | * @str2: extra info |
488 | | * |
489 | | * Handle a Relax NG Parsing error |
490 | | */ |
491 | | static void LIBXML_ATTR_FORMAT(4,0) |
492 | | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
493 | | const char *msg, const xmlChar * str1, const xmlChar * str2) |
494 | 0 | { |
495 | 0 | xmlStructuredErrorFunc schannel = NULL; |
496 | 0 | xmlGenericErrorFunc channel = NULL; |
497 | 0 | void *data = NULL; |
498 | |
|
499 | 0 | if (ctxt != NULL) { |
500 | 0 | if (ctxt->serror != NULL) |
501 | 0 | schannel = ctxt->serror; |
502 | 0 | else |
503 | 0 | channel = ctxt->error; |
504 | 0 | data = ctxt->userData; |
505 | 0 | ctxt->nbErrors++; |
506 | 0 | } |
507 | 0 | __xmlRaiseError(schannel, channel, data, |
508 | 0 | NULL, node, XML_FROM_RELAXNGP, |
509 | 0 | error, XML_ERR_ERROR, NULL, 0, |
510 | 0 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
511 | 0 | msg, str1, str2); |
512 | 0 | } |
513 | | |
514 | | /** |
515 | | * xmlRngVErr: |
516 | | * @ctxt: a Relax-NG validation context |
517 | | * @node: the node raising the error |
518 | | * @error: the error code |
519 | | * @msg: message |
520 | | * @str1: extra info |
521 | | * @str2: extra info |
522 | | * |
523 | | * Handle a Relax NG Validation error |
524 | | */ |
525 | | static void LIBXML_ATTR_FORMAT(4,0) |
526 | | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
527 | | const char *msg, const xmlChar * str1, const xmlChar * str2) |
528 | 0 | { |
529 | 0 | xmlStructuredErrorFunc schannel = NULL; |
530 | 0 | xmlGenericErrorFunc channel = NULL; |
531 | 0 | void *data = NULL; |
532 | |
|
533 | 0 | if (ctxt != NULL) { |
534 | 0 | if (ctxt->serror != NULL) |
535 | 0 | schannel = ctxt->serror; |
536 | 0 | else |
537 | 0 | channel = ctxt->error; |
538 | 0 | data = ctxt->userData; |
539 | 0 | ctxt->nbErrors++; |
540 | 0 | } |
541 | 0 | __xmlRaiseError(schannel, channel, data, |
542 | 0 | NULL, node, XML_FROM_RELAXNGV, |
543 | 0 | error, XML_ERR_ERROR, NULL, 0, |
544 | 0 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
545 | 0 | msg, str1, str2); |
546 | 0 | } |
547 | | |
548 | | /************************************************************************ |
549 | | * * |
550 | | * Preliminary type checking interfaces * |
551 | | * * |
552 | | ************************************************************************/ |
553 | | |
554 | | /** |
555 | | * xmlRelaxNGTypeHave: |
556 | | * @data: data needed for the library |
557 | | * @type: the type name |
558 | | * @value: the value to check |
559 | | * |
560 | | * Function provided by a type library to check if a type is exported |
561 | | * |
562 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
563 | | */ |
564 | | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
565 | | |
566 | | /** |
567 | | * xmlRelaxNGTypeCheck: |
568 | | * @data: data needed for the library |
569 | | * @type: the type name |
570 | | * @value: the value to check |
571 | | * @result: place to store the result if needed |
572 | | * |
573 | | * Function provided by a type library to check if a value match a type |
574 | | * |
575 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
576 | | */ |
577 | | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
578 | | const xmlChar * value, void **result, |
579 | | xmlNodePtr node); |
580 | | |
581 | | /** |
582 | | * xmlRelaxNGFacetCheck: |
583 | | * @data: data needed for the library |
584 | | * @type: the type name |
585 | | * @facet: the facet name |
586 | | * @val: the facet value |
587 | | * @strval: the string value |
588 | | * @value: the value to check |
589 | | * |
590 | | * Function provided by a type library to check a value facet |
591 | | * |
592 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
593 | | */ |
594 | | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
595 | | const xmlChar * facet, |
596 | | const xmlChar * val, |
597 | | const xmlChar * strval, void *value); |
598 | | |
599 | | /** |
600 | | * xmlRelaxNGTypeFree: |
601 | | * @data: data needed for the library |
602 | | * @result: the value to free |
603 | | * |
604 | | * Function provided by a type library to free a returned result |
605 | | */ |
606 | | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
607 | | |
608 | | /** |
609 | | * xmlRelaxNGTypeCompare: |
610 | | * @data: data needed for the library |
611 | | * @type: the type name |
612 | | * @value1: the first value |
613 | | * @value2: the second value |
614 | | * |
615 | | * Function provided by a type library to compare two values accordingly |
616 | | * to a type. |
617 | | * |
618 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
619 | | */ |
620 | | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
621 | | const xmlChar * value1, |
622 | | xmlNodePtr ctxt1, |
623 | | void *comp1, |
624 | | const xmlChar * value2, |
625 | | xmlNodePtr ctxt2); |
626 | | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
627 | | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
628 | | struct _xmlRelaxNGTypeLibrary { |
629 | | const xmlChar *namespace; /* the datatypeLibrary value */ |
630 | | void *data; /* data needed for the library */ |
631 | | xmlRelaxNGTypeHave have; /* the export function */ |
632 | | xmlRelaxNGTypeCheck check; /* the checking function */ |
633 | | xmlRelaxNGTypeCompare comp; /* the compare function */ |
634 | | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
635 | | xmlRelaxNGTypeFree freef; /* the freeing function */ |
636 | | }; |
637 | | |
638 | | /************************************************************************ |
639 | | * * |
640 | | * Allocation functions * |
641 | | * * |
642 | | ************************************************************************/ |
643 | | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
644 | | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
645 | | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
646 | | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
647 | | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
648 | | ATTRIBUTE_UNUSED, |
649 | | xmlRelaxNGValidStatePtr state1, |
650 | | xmlRelaxNGValidStatePtr state2); |
651 | | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
652 | | xmlRelaxNGValidStatePtr state); |
653 | | |
654 | | /** |
655 | | * xmlRelaxNGFreeDocument: |
656 | | * @docu: a document structure |
657 | | * |
658 | | * Deallocate a RelaxNG document structure. |
659 | | */ |
660 | | static void |
661 | | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
662 | 0 | { |
663 | 0 | if (docu == NULL) |
664 | 0 | return; |
665 | | |
666 | 0 | if (docu->href != NULL) |
667 | 0 | xmlFree(docu->href); |
668 | 0 | if (docu->doc != NULL) |
669 | 0 | xmlFreeDoc(docu->doc); |
670 | 0 | if (docu->schema != NULL) |
671 | 0 | xmlRelaxNGFreeInnerSchema(docu->schema); |
672 | 0 | xmlFree(docu); |
673 | 0 | } |
674 | | |
675 | | /** |
676 | | * xmlRelaxNGFreeDocumentList: |
677 | | * @docu: a list of document structure |
678 | | * |
679 | | * Deallocate a RelaxNG document structures. |
680 | | */ |
681 | | static void |
682 | | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
683 | 0 | { |
684 | 0 | xmlRelaxNGDocumentPtr next; |
685 | |
|
686 | 0 | while (docu != NULL) { |
687 | 0 | next = docu->next; |
688 | 0 | xmlRelaxNGFreeDocument(docu); |
689 | 0 | docu = next; |
690 | 0 | } |
691 | 0 | } |
692 | | |
693 | | /** |
694 | | * xmlRelaxNGFreeInclude: |
695 | | * @incl: a include structure |
696 | | * |
697 | | * Deallocate a RelaxNG include structure. |
698 | | */ |
699 | | static void |
700 | | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
701 | 0 | { |
702 | 0 | if (incl == NULL) |
703 | 0 | return; |
704 | | |
705 | 0 | if (incl->href != NULL) |
706 | 0 | xmlFree(incl->href); |
707 | 0 | if (incl->doc != NULL) |
708 | 0 | xmlFreeDoc(incl->doc); |
709 | 0 | if (incl->schema != NULL) |
710 | 0 | xmlRelaxNGFree(incl->schema); |
711 | 0 | xmlFree(incl); |
712 | 0 | } |
713 | | |
714 | | /** |
715 | | * xmlRelaxNGFreeIncludeList: |
716 | | * @incl: a include structure list |
717 | | * |
718 | | * Deallocate a RelaxNG include structure. |
719 | | */ |
720 | | static void |
721 | | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
722 | 0 | { |
723 | 0 | xmlRelaxNGIncludePtr next; |
724 | |
|
725 | 0 | while (incl != NULL) { |
726 | 0 | next = incl->next; |
727 | 0 | xmlRelaxNGFreeInclude(incl); |
728 | 0 | incl = next; |
729 | 0 | } |
730 | 0 | } |
731 | | |
732 | | /** |
733 | | * xmlRelaxNGNewRelaxNG: |
734 | | * @ctxt: a Relax-NG validation context (optional) |
735 | | * |
736 | | * Allocate a new RelaxNG structure. |
737 | | * |
738 | | * Returns the newly allocated structure or NULL in case or error |
739 | | */ |
740 | | static xmlRelaxNGPtr |
741 | | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
742 | 0 | { |
743 | 0 | xmlRelaxNGPtr ret; |
744 | |
|
745 | 0 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
746 | 0 | if (ret == NULL) { |
747 | 0 | xmlRngPErrMemory(ctxt, NULL); |
748 | 0 | return (NULL); |
749 | 0 | } |
750 | 0 | memset(ret, 0, sizeof(xmlRelaxNG)); |
751 | |
|
752 | 0 | return (ret); |
753 | 0 | } |
754 | | |
755 | | /** |
756 | | * xmlRelaxNGFreeInnerSchema: |
757 | | * @schema: a schema structure |
758 | | * |
759 | | * Deallocate a RelaxNG schema structure. |
760 | | */ |
761 | | static void |
762 | | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
763 | 0 | { |
764 | 0 | if (schema == NULL) |
765 | 0 | return; |
766 | | |
767 | 0 | if (schema->doc != NULL) |
768 | 0 | xmlFreeDoc(schema->doc); |
769 | 0 | if (schema->defTab != NULL) { |
770 | 0 | int i; |
771 | |
|
772 | 0 | for (i = 0; i < schema->defNr; i++) |
773 | 0 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
774 | 0 | xmlFree(schema->defTab); |
775 | 0 | } |
776 | |
|
777 | 0 | xmlFree(schema); |
778 | 0 | } |
779 | | |
780 | | /** |
781 | | * xmlRelaxNGFree: |
782 | | * @schema: a schema structure |
783 | | * |
784 | | * Deallocate a RelaxNG structure. |
785 | | */ |
786 | | void |
787 | | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
788 | 0 | { |
789 | 0 | if (schema == NULL) |
790 | 0 | return; |
791 | | |
792 | 0 | if (schema->topgrammar != NULL) |
793 | 0 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
794 | 0 | if (schema->doc != NULL) |
795 | 0 | xmlFreeDoc(schema->doc); |
796 | 0 | if (schema->documents != NULL) |
797 | 0 | xmlRelaxNGFreeDocumentList(schema->documents); |
798 | 0 | if (schema->includes != NULL) |
799 | 0 | xmlRelaxNGFreeIncludeList(schema->includes); |
800 | 0 | if (schema->defTab != NULL) { |
801 | 0 | int i; |
802 | |
|
803 | 0 | for (i = 0; i < schema->defNr; i++) |
804 | 0 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
805 | 0 | xmlFree(schema->defTab); |
806 | 0 | } |
807 | |
|
808 | 0 | xmlFree(schema); |
809 | 0 | } |
810 | | |
811 | | /** |
812 | | * xmlRelaxNGNewGrammar: |
813 | | * @ctxt: a Relax-NG validation context (optional) |
814 | | * |
815 | | * Allocate a new RelaxNG grammar. |
816 | | * |
817 | | * Returns the newly allocated structure or NULL in case or error |
818 | | */ |
819 | | static xmlRelaxNGGrammarPtr |
820 | | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
821 | 0 | { |
822 | 0 | xmlRelaxNGGrammarPtr ret; |
823 | |
|
824 | 0 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
825 | 0 | if (ret == NULL) { |
826 | 0 | xmlRngPErrMemory(ctxt, NULL); |
827 | 0 | return (NULL); |
828 | 0 | } |
829 | 0 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
830 | |
|
831 | 0 | return (ret); |
832 | 0 | } |
833 | | |
834 | | /** |
835 | | * xmlRelaxNGFreeGrammar: |
836 | | * @grammar: a grammar structure |
837 | | * |
838 | | * Deallocate a RelaxNG grammar structure. |
839 | | */ |
840 | | static void |
841 | | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
842 | 0 | { |
843 | 0 | if (grammar == NULL) |
844 | 0 | return; |
845 | | |
846 | 0 | if (grammar->children != NULL) { |
847 | 0 | xmlRelaxNGFreeGrammar(grammar->children); |
848 | 0 | } |
849 | 0 | if (grammar->next != NULL) { |
850 | 0 | xmlRelaxNGFreeGrammar(grammar->next); |
851 | 0 | } |
852 | 0 | if (grammar->refs != NULL) { |
853 | 0 | xmlHashFree(grammar->refs, NULL); |
854 | 0 | } |
855 | 0 | if (grammar->defs != NULL) { |
856 | 0 | xmlHashFree(grammar->defs, NULL); |
857 | 0 | } |
858 | |
|
859 | 0 | xmlFree(grammar); |
860 | 0 | } |
861 | | |
862 | | /** |
863 | | * xmlRelaxNGNewDefine: |
864 | | * @ctxt: a Relax-NG validation context |
865 | | * @node: the node in the input document. |
866 | | * |
867 | | * Allocate a new RelaxNG define. |
868 | | * |
869 | | * Returns the newly allocated structure or NULL in case or error |
870 | | */ |
871 | | static xmlRelaxNGDefinePtr |
872 | | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
873 | 0 | { |
874 | 0 | xmlRelaxNGDefinePtr ret; |
875 | |
|
876 | 0 | if (ctxt->defMax == 0) { |
877 | 0 | ctxt->defMax = 16; |
878 | 0 | ctxt->defNr = 0; |
879 | 0 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
880 | 0 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
881 | 0 | if (ctxt->defTab == NULL) { |
882 | 0 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
883 | 0 | return (NULL); |
884 | 0 | } |
885 | 0 | } else if (ctxt->defMax <= ctxt->defNr) { |
886 | 0 | xmlRelaxNGDefinePtr *tmp; |
887 | |
|
888 | 0 | ctxt->defMax *= 2; |
889 | 0 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
890 | 0 | ctxt->defMax * |
891 | 0 | sizeof |
892 | 0 | (xmlRelaxNGDefinePtr)); |
893 | 0 | if (tmp == NULL) { |
894 | 0 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
895 | 0 | return (NULL); |
896 | 0 | } |
897 | 0 | ctxt->defTab = tmp; |
898 | 0 | } |
899 | 0 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
900 | 0 | if (ret == NULL) { |
901 | 0 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
902 | 0 | return (NULL); |
903 | 0 | } |
904 | 0 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
905 | 0 | ctxt->defTab[ctxt->defNr++] = ret; |
906 | 0 | ret->node = node; |
907 | 0 | ret->depth = -1; |
908 | 0 | return (ret); |
909 | 0 | } |
910 | | |
911 | | /** |
912 | | * xmlRelaxNGFreePartition: |
913 | | * @partitions: a partition set structure |
914 | | * |
915 | | * Deallocate RelaxNG partition set structures. |
916 | | */ |
917 | | static void |
918 | | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
919 | 0 | { |
920 | 0 | xmlRelaxNGInterleaveGroupPtr group; |
921 | 0 | int j; |
922 | |
|
923 | 0 | if (partitions != NULL) { |
924 | 0 | if (partitions->groups != NULL) { |
925 | 0 | for (j = 0; j < partitions->nbgroups; j++) { |
926 | 0 | group = partitions->groups[j]; |
927 | 0 | if (group != NULL) { |
928 | 0 | if (group->defs != NULL) |
929 | 0 | xmlFree(group->defs); |
930 | 0 | if (group->attrs != NULL) |
931 | 0 | xmlFree(group->attrs); |
932 | 0 | xmlFree(group); |
933 | 0 | } |
934 | 0 | } |
935 | 0 | xmlFree(partitions->groups); |
936 | 0 | } |
937 | 0 | if (partitions->triage != NULL) { |
938 | 0 | xmlHashFree(partitions->triage, NULL); |
939 | 0 | } |
940 | 0 | xmlFree(partitions); |
941 | 0 | } |
942 | 0 | } |
943 | | |
944 | | /** |
945 | | * xmlRelaxNGFreeDefine: |
946 | | * @define: a define structure |
947 | | * |
948 | | * Deallocate a RelaxNG define structure. |
949 | | */ |
950 | | static void |
951 | | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
952 | 0 | { |
953 | 0 | if (define == NULL) |
954 | 0 | return; |
955 | | |
956 | 0 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
957 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
958 | |
|
959 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
960 | 0 | if ((lib != NULL) && (lib->freef != NULL)) |
961 | 0 | lib->freef(lib->data, (void *) define->attrs); |
962 | 0 | } |
963 | 0 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
964 | 0 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
965 | 0 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
966 | 0 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
967 | 0 | if (define->name != NULL) |
968 | 0 | xmlFree(define->name); |
969 | 0 | if (define->ns != NULL) |
970 | 0 | xmlFree(define->ns); |
971 | 0 | if (define->value != NULL) |
972 | 0 | xmlFree(define->value); |
973 | 0 | if (define->contModel != NULL) |
974 | 0 | xmlRegFreeRegexp(define->contModel); |
975 | 0 | xmlFree(define); |
976 | 0 | } |
977 | | |
978 | | /** |
979 | | * xmlRelaxNGNewStates: |
980 | | * @ctxt: a Relax-NG validation context |
981 | | * @size: the default size for the container |
982 | | * |
983 | | * Allocate a new RelaxNG validation state container |
984 | | * |
985 | | * Returns the newly allocated structure or NULL in case or error |
986 | | */ |
987 | | static xmlRelaxNGStatesPtr |
988 | | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
989 | 0 | { |
990 | 0 | xmlRelaxNGStatesPtr ret; |
991 | |
|
992 | 0 | if ((ctxt != NULL) && |
993 | 0 | (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) { |
994 | 0 | ctxt->freeStatesNr--; |
995 | 0 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
996 | 0 | ret->nbState = 0; |
997 | 0 | return (ret); |
998 | 0 | } |
999 | 0 | if (size < 16) |
1000 | 0 | size = 16; |
1001 | |
|
1002 | 0 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
1003 | 0 | (size - |
1004 | 0 | 1) * |
1005 | 0 | sizeof(xmlRelaxNGValidStatePtr)); |
1006 | 0 | if (ret == NULL) { |
1007 | 0 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
1008 | 0 | return (NULL); |
1009 | 0 | } |
1010 | 0 | ret->nbState = 0; |
1011 | 0 | ret->maxState = size; |
1012 | 0 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
1013 | 0 | sizeof |
1014 | 0 | (xmlRelaxNGValidStatePtr)); |
1015 | 0 | if (ret->tabState == NULL) { |
1016 | 0 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
1017 | 0 | xmlFree(ret); |
1018 | 0 | return (NULL); |
1019 | 0 | } |
1020 | 0 | return (ret); |
1021 | 0 | } |
1022 | | |
1023 | | /** |
1024 | | * xmlRelaxNGAddStateUniq: |
1025 | | * @ctxt: a Relax-NG validation context |
1026 | | * @states: the states container |
1027 | | * @state: the validation state |
1028 | | * |
1029 | | * Add a RelaxNG validation state to the container without checking |
1030 | | * for unicity. |
1031 | | * |
1032 | | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
1033 | | */ |
1034 | | static int |
1035 | | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
1036 | | xmlRelaxNGStatesPtr states, |
1037 | | xmlRelaxNGValidStatePtr state) |
1038 | 0 | { |
1039 | 0 | if (state == NULL) { |
1040 | 0 | return (-1); |
1041 | 0 | } |
1042 | 0 | if (states->nbState >= states->maxState) { |
1043 | 0 | xmlRelaxNGValidStatePtr *tmp; |
1044 | 0 | int size; |
1045 | |
|
1046 | 0 | size = states->maxState * 2; |
1047 | 0 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
1048 | 0 | (size) * |
1049 | 0 | sizeof |
1050 | 0 | (xmlRelaxNGValidStatePtr)); |
1051 | 0 | if (tmp == NULL) { |
1052 | 0 | xmlRngVErrMemory(ctxt, "adding states\n"); |
1053 | 0 | return (-1); |
1054 | 0 | } |
1055 | 0 | states->tabState = tmp; |
1056 | 0 | states->maxState = size; |
1057 | 0 | } |
1058 | 0 | states->tabState[states->nbState++] = state; |
1059 | 0 | return (1); |
1060 | 0 | } |
1061 | | |
1062 | | /** |
1063 | | * xmlRelaxNGAddState: |
1064 | | * @ctxt: a Relax-NG validation context |
1065 | | * @states: the states container |
1066 | | * @state: the validation state |
1067 | | * |
1068 | | * Add a RelaxNG validation state to the container |
1069 | | * |
1070 | | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
1071 | | */ |
1072 | | static int |
1073 | | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
1074 | | xmlRelaxNGStatesPtr states, |
1075 | | xmlRelaxNGValidStatePtr state) |
1076 | 0 | { |
1077 | 0 | int i; |
1078 | |
|
1079 | 0 | if (state == NULL || states == NULL) { |
1080 | 0 | return (-1); |
1081 | 0 | } |
1082 | 0 | if (states->nbState >= states->maxState) { |
1083 | 0 | xmlRelaxNGValidStatePtr *tmp; |
1084 | 0 | int size; |
1085 | |
|
1086 | 0 | size = states->maxState * 2; |
1087 | 0 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
1088 | 0 | (size) * |
1089 | 0 | sizeof |
1090 | 0 | (xmlRelaxNGValidStatePtr)); |
1091 | 0 | if (tmp == NULL) { |
1092 | 0 | xmlRngVErrMemory(ctxt, "adding states\n"); |
1093 | 0 | return (-1); |
1094 | 0 | } |
1095 | 0 | states->tabState = tmp; |
1096 | 0 | states->maxState = size; |
1097 | 0 | } |
1098 | 0 | for (i = 0; i < states->nbState; i++) { |
1099 | 0 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
1100 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
1101 | 0 | return (0); |
1102 | 0 | } |
1103 | 0 | } |
1104 | 0 | states->tabState[states->nbState++] = state; |
1105 | 0 | return (1); |
1106 | 0 | } |
1107 | | |
1108 | | /** |
1109 | | * xmlRelaxNGFreeStates: |
1110 | | * @ctxt: a Relax-NG validation context |
1111 | | * @states: the container |
1112 | | * |
1113 | | * Free a RelaxNG validation state container |
1114 | | */ |
1115 | | static void |
1116 | | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
1117 | | xmlRelaxNGStatesPtr states) |
1118 | 0 | { |
1119 | 0 | if (states == NULL) |
1120 | 0 | return; |
1121 | 0 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
1122 | 0 | ctxt->freeStatesMax = 40; |
1123 | 0 | ctxt->freeStatesNr = 0; |
1124 | 0 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
1125 | 0 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
1126 | 0 | if (ctxt->freeStates == NULL) { |
1127 | 0 | xmlRngVErrMemory(ctxt, "storing states\n"); |
1128 | 0 | } |
1129 | 0 | } else if ((ctxt != NULL) |
1130 | 0 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
1131 | 0 | xmlRelaxNGStatesPtr *tmp; |
1132 | |
|
1133 | 0 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
1134 | 0 | 2 * ctxt->freeStatesMax * |
1135 | 0 | sizeof |
1136 | 0 | (xmlRelaxNGStatesPtr)); |
1137 | 0 | if (tmp == NULL) { |
1138 | 0 | xmlRngVErrMemory(ctxt, "storing states\n"); |
1139 | 0 | xmlFree(states->tabState); |
1140 | 0 | xmlFree(states); |
1141 | 0 | return; |
1142 | 0 | } |
1143 | 0 | ctxt->freeStates = tmp; |
1144 | 0 | ctxt->freeStatesMax *= 2; |
1145 | 0 | } |
1146 | 0 | if ((ctxt == NULL) || (ctxt->freeStates == NULL)) { |
1147 | 0 | xmlFree(states->tabState); |
1148 | 0 | xmlFree(states); |
1149 | 0 | } else { |
1150 | 0 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
1151 | 0 | } |
1152 | 0 | } |
1153 | | |
1154 | | /** |
1155 | | * xmlRelaxNGNewValidState: |
1156 | | * @ctxt: a Relax-NG validation context |
1157 | | * @node: the current node or NULL for the document |
1158 | | * |
1159 | | * Allocate a new RelaxNG validation state |
1160 | | * |
1161 | | * Returns the newly allocated structure or NULL in case or error |
1162 | | */ |
1163 | | static xmlRelaxNGValidStatePtr |
1164 | | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
1165 | 0 | { |
1166 | 0 | xmlRelaxNGValidStatePtr ret; |
1167 | 0 | xmlAttrPtr attr; |
1168 | 0 | xmlAttrPtr attrs[MAX_ATTR]; |
1169 | 0 | int nbAttrs = 0; |
1170 | 0 | xmlNodePtr root = NULL; |
1171 | |
|
1172 | 0 | if (node == NULL) { |
1173 | 0 | root = xmlDocGetRootElement(ctxt->doc); |
1174 | 0 | if (root == NULL) |
1175 | 0 | return (NULL); |
1176 | 0 | } else { |
1177 | 0 | attr = node->properties; |
1178 | 0 | while (attr != NULL) { |
1179 | 0 | if (nbAttrs < MAX_ATTR) |
1180 | 0 | attrs[nbAttrs++] = attr; |
1181 | 0 | else |
1182 | 0 | nbAttrs++; |
1183 | 0 | attr = attr->next; |
1184 | 0 | } |
1185 | 0 | } |
1186 | 0 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
1187 | 0 | ctxt->freeState->nbState--; |
1188 | 0 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
1189 | 0 | } else { |
1190 | 0 | ret = |
1191 | 0 | (xmlRelaxNGValidStatePtr) |
1192 | 0 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
1193 | 0 | if (ret == NULL) { |
1194 | 0 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
1195 | 0 | return (NULL); |
1196 | 0 | } |
1197 | 0 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
1198 | 0 | } |
1199 | 0 | ret->value = NULL; |
1200 | 0 | ret->endvalue = NULL; |
1201 | 0 | if (node == NULL) { |
1202 | 0 | ret->node = (xmlNodePtr) ctxt->doc; |
1203 | 0 | ret->seq = root; |
1204 | 0 | } else { |
1205 | 0 | ret->node = node; |
1206 | 0 | ret->seq = node->children; |
1207 | 0 | } |
1208 | 0 | ret->nbAttrs = 0; |
1209 | 0 | if (nbAttrs > 0) { |
1210 | 0 | if (ret->attrs == NULL) { |
1211 | 0 | if (nbAttrs < 4) |
1212 | 0 | ret->maxAttrs = 4; |
1213 | 0 | else |
1214 | 0 | ret->maxAttrs = nbAttrs; |
1215 | 0 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
1216 | 0 | sizeof(xmlAttrPtr)); |
1217 | 0 | if (ret->attrs == NULL) { |
1218 | 0 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
1219 | 0 | return (ret); |
1220 | 0 | } |
1221 | 0 | } else if (ret->maxAttrs < nbAttrs) { |
1222 | 0 | xmlAttrPtr *tmp; |
1223 | |
|
1224 | 0 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
1225 | 0 | sizeof(xmlAttrPtr)); |
1226 | 0 | if (tmp == NULL) { |
1227 | 0 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
1228 | 0 | return (ret); |
1229 | 0 | } |
1230 | 0 | ret->attrs = tmp; |
1231 | 0 | ret->maxAttrs = nbAttrs; |
1232 | 0 | } |
1233 | 0 | ret->nbAttrs = nbAttrs; |
1234 | 0 | if (nbAttrs < MAX_ATTR) { |
1235 | 0 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
1236 | 0 | } else { |
1237 | 0 | attr = node->properties; |
1238 | 0 | nbAttrs = 0; |
1239 | 0 | while (attr != NULL) { |
1240 | 0 | ret->attrs[nbAttrs++] = attr; |
1241 | 0 | attr = attr->next; |
1242 | 0 | } |
1243 | 0 | } |
1244 | 0 | } |
1245 | 0 | ret->nbAttrLeft = ret->nbAttrs; |
1246 | 0 | return (ret); |
1247 | 0 | } |
1248 | | |
1249 | | /** |
1250 | | * xmlRelaxNGCopyValidState: |
1251 | | * @ctxt: a Relax-NG validation context |
1252 | | * @state: a validation state |
1253 | | * |
1254 | | * Copy the validation state |
1255 | | * |
1256 | | * Returns the newly allocated structure or NULL in case or error |
1257 | | */ |
1258 | | static xmlRelaxNGValidStatePtr |
1259 | | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
1260 | | xmlRelaxNGValidStatePtr state) |
1261 | 0 | { |
1262 | 0 | xmlRelaxNGValidStatePtr ret; |
1263 | 0 | unsigned int maxAttrs; |
1264 | 0 | xmlAttrPtr *attrs; |
1265 | |
|
1266 | 0 | if (state == NULL) |
1267 | 0 | return (NULL); |
1268 | 0 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
1269 | 0 | ctxt->freeState->nbState--; |
1270 | 0 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
1271 | 0 | } else { |
1272 | 0 | ret = |
1273 | 0 | (xmlRelaxNGValidStatePtr) |
1274 | 0 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
1275 | 0 | if (ret == NULL) { |
1276 | 0 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
1277 | 0 | return (NULL); |
1278 | 0 | } |
1279 | 0 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
1280 | 0 | } |
1281 | 0 | attrs = ret->attrs; |
1282 | 0 | maxAttrs = ret->maxAttrs; |
1283 | 0 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
1284 | 0 | ret->attrs = attrs; |
1285 | 0 | ret->maxAttrs = maxAttrs; |
1286 | 0 | if (state->nbAttrs > 0) { |
1287 | 0 | if (ret->attrs == NULL) { |
1288 | 0 | ret->maxAttrs = state->maxAttrs; |
1289 | 0 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
1290 | 0 | sizeof(xmlAttrPtr)); |
1291 | 0 | if (ret->attrs == NULL) { |
1292 | 0 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
1293 | 0 | ret->nbAttrs = 0; |
1294 | 0 | return (ret); |
1295 | 0 | } |
1296 | 0 | } else if (ret->maxAttrs < state->nbAttrs) { |
1297 | 0 | xmlAttrPtr *tmp; |
1298 | |
|
1299 | 0 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
1300 | 0 | sizeof(xmlAttrPtr)); |
1301 | 0 | if (tmp == NULL) { |
1302 | 0 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
1303 | 0 | ret->nbAttrs = 0; |
1304 | 0 | return (ret); |
1305 | 0 | } |
1306 | 0 | ret->maxAttrs = state->maxAttrs; |
1307 | 0 | ret->attrs = tmp; |
1308 | 0 | } |
1309 | 0 | memcpy(ret->attrs, state->attrs, |
1310 | 0 | state->nbAttrs * sizeof(xmlAttrPtr)); |
1311 | 0 | } |
1312 | 0 | return (ret); |
1313 | 0 | } |
1314 | | |
1315 | | /** |
1316 | | * xmlRelaxNGEqualValidState: |
1317 | | * @ctxt: a Relax-NG validation context |
1318 | | * @state1: a validation state |
1319 | | * @state2: a validation state |
1320 | | * |
1321 | | * Compare the validation states for equality |
1322 | | * |
1323 | | * Returns 1 if equal, 0 otherwise |
1324 | | */ |
1325 | | static int |
1326 | | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
1327 | | xmlRelaxNGValidStatePtr state1, |
1328 | | xmlRelaxNGValidStatePtr state2) |
1329 | 0 | { |
1330 | 0 | int i; |
1331 | |
|
1332 | 0 | if ((state1 == NULL) || (state2 == NULL)) |
1333 | 0 | return (0); |
1334 | 0 | if (state1 == state2) |
1335 | 0 | return (1); |
1336 | 0 | if (state1->node != state2->node) |
1337 | 0 | return (0); |
1338 | 0 | if (state1->seq != state2->seq) |
1339 | 0 | return (0); |
1340 | 0 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
1341 | 0 | return (0); |
1342 | 0 | if (state1->nbAttrs != state2->nbAttrs) |
1343 | 0 | return (0); |
1344 | 0 | if (state1->endvalue != state2->endvalue) |
1345 | 0 | return (0); |
1346 | 0 | if ((state1->value != state2->value) && |
1347 | 0 | (!xmlStrEqual(state1->value, state2->value))) |
1348 | 0 | return (0); |
1349 | 0 | for (i = 0; i < state1->nbAttrs; i++) { |
1350 | 0 | if (state1->attrs[i] != state2->attrs[i]) |
1351 | 0 | return (0); |
1352 | 0 | } |
1353 | 0 | return (1); |
1354 | 0 | } |
1355 | | |
1356 | | /** |
1357 | | * xmlRelaxNGFreeValidState: |
1358 | | * @state: a validation state structure |
1359 | | * |
1360 | | * Deallocate a RelaxNG validation state structure. |
1361 | | */ |
1362 | | static void |
1363 | | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
1364 | | xmlRelaxNGValidStatePtr state) |
1365 | 0 | { |
1366 | 0 | if (state == NULL) |
1367 | 0 | return; |
1368 | | |
1369 | 0 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
1370 | 0 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
1371 | 0 | } |
1372 | 0 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
1373 | 0 | if (state->attrs != NULL) |
1374 | 0 | xmlFree(state->attrs); |
1375 | 0 | xmlFree(state); |
1376 | 0 | } else { |
1377 | 0 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
1378 | 0 | } |
1379 | 0 | } |
1380 | | |
1381 | | /************************************************************************ |
1382 | | * * |
1383 | | * Semi internal functions * |
1384 | | * * |
1385 | | ************************************************************************/ |
1386 | | |
1387 | | /** |
1388 | | * xmlRelaxParserSetFlag: |
1389 | | * @ctxt: a RelaxNG parser context |
1390 | | * @flags: a set of flags values |
1391 | | * |
1392 | | * Semi private function used to pass information to a parser context |
1393 | | * which are a combination of xmlRelaxNGParserFlag . |
1394 | | * |
1395 | | * Returns 0 if success and -1 in case of error |
1396 | | */ |
1397 | | int |
1398 | | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags) |
1399 | 0 | { |
1400 | 0 | if (ctxt == NULL) return(-1); |
1401 | 0 | if (flags & XML_RELAXNGP_FREE_DOC) { |
1402 | 0 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
1403 | 0 | flags -= XML_RELAXNGP_FREE_DOC; |
1404 | 0 | } |
1405 | 0 | if (flags & XML_RELAXNGP_CRNG) { |
1406 | 0 | ctxt->crng |= XML_RELAXNGP_CRNG; |
1407 | 0 | flags -= XML_RELAXNGP_CRNG; |
1408 | 0 | } |
1409 | 0 | if (flags != 0) return(-1); |
1410 | 0 | return(0); |
1411 | 0 | } |
1412 | | |
1413 | | /************************************************************************ |
1414 | | * * |
1415 | | * Document functions * |
1416 | | * * |
1417 | | ************************************************************************/ |
1418 | | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
1419 | | xmlDocPtr doc); |
1420 | | |
1421 | | /** |
1422 | | * xmlRelaxNGIncludePush: |
1423 | | * @ctxt: the parser context |
1424 | | * @value: the element doc |
1425 | | * |
1426 | | * Pushes a new include on top of the include stack |
1427 | | * |
1428 | | * Returns 0 in case of error, the index in the stack otherwise |
1429 | | */ |
1430 | | static int |
1431 | | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
1432 | | xmlRelaxNGIncludePtr value) |
1433 | 0 | { |
1434 | 0 | if (ctxt->incTab == NULL) { |
1435 | 0 | ctxt->incMax = 4; |
1436 | 0 | ctxt->incNr = 0; |
1437 | 0 | ctxt->incTab = |
1438 | 0 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
1439 | 0 | sizeof(ctxt->incTab[0])); |
1440 | 0 | if (ctxt->incTab == NULL) { |
1441 | 0 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
1442 | 0 | return (0); |
1443 | 0 | } |
1444 | 0 | } |
1445 | 0 | if (ctxt->incNr >= ctxt->incMax) { |
1446 | 0 | ctxt->incMax *= 2; |
1447 | 0 | ctxt->incTab = |
1448 | 0 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
1449 | 0 | ctxt->incMax * |
1450 | 0 | sizeof(ctxt->incTab[0])); |
1451 | 0 | if (ctxt->incTab == NULL) { |
1452 | 0 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
1453 | 0 | return (0); |
1454 | 0 | } |
1455 | 0 | } |
1456 | 0 | ctxt->incTab[ctxt->incNr] = value; |
1457 | 0 | ctxt->inc = value; |
1458 | 0 | return (ctxt->incNr++); |
1459 | 0 | } |
1460 | | |
1461 | | /** |
1462 | | * xmlRelaxNGIncludePop: |
1463 | | * @ctxt: the parser context |
1464 | | * |
1465 | | * Pops the top include from the include stack |
1466 | | * |
1467 | | * Returns the include just removed |
1468 | | */ |
1469 | | static xmlRelaxNGIncludePtr |
1470 | | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
1471 | 0 | { |
1472 | 0 | xmlRelaxNGIncludePtr ret; |
1473 | |
|
1474 | 0 | if (ctxt->incNr <= 0) |
1475 | 0 | return (NULL); |
1476 | 0 | ctxt->incNr--; |
1477 | 0 | if (ctxt->incNr > 0) |
1478 | 0 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
1479 | 0 | else |
1480 | 0 | ctxt->inc = NULL; |
1481 | 0 | ret = ctxt->incTab[ctxt->incNr]; |
1482 | 0 | ctxt->incTab[ctxt->incNr] = NULL; |
1483 | 0 | return (ret); |
1484 | 0 | } |
1485 | | |
1486 | | /** |
1487 | | * xmlRelaxNGRemoveRedefine: |
1488 | | * @ctxt: the parser context |
1489 | | * @URL: the normalized URL |
1490 | | * @target: the included target |
1491 | | * @name: the define name to eliminate |
1492 | | * |
1493 | | * Applies the elimination algorithm of 4.7 |
1494 | | * |
1495 | | * Returns 0 in case of error, 1 in case of success. |
1496 | | */ |
1497 | | static int |
1498 | | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
1499 | | const xmlChar * URL ATTRIBUTE_UNUSED, |
1500 | | xmlNodePtr target, const xmlChar * name) |
1501 | 0 | { |
1502 | 0 | int found = 0; |
1503 | 0 | xmlNodePtr tmp, tmp2; |
1504 | 0 | xmlChar *name2; |
1505 | |
|
1506 | 0 | tmp = target; |
1507 | 0 | while (tmp != NULL) { |
1508 | 0 | tmp2 = tmp->next; |
1509 | 0 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
1510 | 0 | found = 1; |
1511 | 0 | xmlUnlinkNode(tmp); |
1512 | 0 | xmlFreeNode(tmp); |
1513 | 0 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
1514 | 0 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
1515 | 0 | xmlRelaxNGNormExtSpace(name2); |
1516 | 0 | if (name2 != NULL) { |
1517 | 0 | if (xmlStrEqual(name, name2)) { |
1518 | 0 | found = 1; |
1519 | 0 | xmlUnlinkNode(tmp); |
1520 | 0 | xmlFreeNode(tmp); |
1521 | 0 | } |
1522 | 0 | xmlFree(name2); |
1523 | 0 | } |
1524 | 0 | } else if (IS_RELAXNG(tmp, "include")) { |
1525 | 0 | xmlChar *href = NULL; |
1526 | 0 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
1527 | |
|
1528 | 0 | if ((inc != NULL) && (inc->doc != NULL) && |
1529 | 0 | (inc->doc->children != NULL)) { |
1530 | |
|
1531 | 0 | if (xmlStrEqual |
1532 | 0 | (inc->doc->children->name, BAD_CAST "grammar")) { |
1533 | 0 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
1534 | 0 | xmlDocGetRootElement(inc->doc)->children, |
1535 | 0 | name) == 1) { |
1536 | 0 | found = 1; |
1537 | 0 | } |
1538 | 0 | } |
1539 | 0 | } |
1540 | 0 | if (xmlRelaxNGRemoveRedefine(ctxt, URL, tmp->children, name) == 1) { |
1541 | 0 | found = 1; |
1542 | 0 | } |
1543 | 0 | } |
1544 | 0 | tmp = tmp2; |
1545 | 0 | } |
1546 | 0 | return (found); |
1547 | 0 | } |
1548 | | |
1549 | | /** |
1550 | | * xmlRelaxNGLoadInclude: |
1551 | | * @ctxt: the parser context |
1552 | | * @URL: the normalized URL |
1553 | | * @node: the include node. |
1554 | | * @ns: the namespace passed from the context. |
1555 | | * |
1556 | | * First lookup if the document is already loaded into the parser context, |
1557 | | * check against recursion. If not found the resource is loaded and |
1558 | | * the content is preprocessed before being returned back to the caller. |
1559 | | * |
1560 | | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
1561 | | */ |
1562 | | static xmlRelaxNGIncludePtr |
1563 | | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
1564 | | xmlNodePtr node, const xmlChar * ns) |
1565 | 0 | { |
1566 | 0 | xmlRelaxNGIncludePtr ret = NULL; |
1567 | 0 | xmlDocPtr doc; |
1568 | 0 | int i; |
1569 | 0 | xmlNodePtr root, cur; |
1570 | | |
1571 | | /* |
1572 | | * check against recursion in the stack |
1573 | | */ |
1574 | 0 | for (i = 0; i < ctxt->incNr; i++) { |
1575 | 0 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
1576 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
1577 | 0 | "Detected an Include recursion for %s\n", URL, |
1578 | 0 | NULL); |
1579 | 0 | return (NULL); |
1580 | 0 | } |
1581 | 0 | } |
1582 | | |
1583 | | /* |
1584 | | * load the document |
1585 | | */ |
1586 | 0 | doc = xmlReadFile((const char *) URL,NULL,0); |
1587 | 0 | if (doc == NULL) { |
1588 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
1589 | 0 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
1590 | 0 | return (NULL); |
1591 | 0 | } |
1592 | | |
1593 | | /* |
1594 | | * Allocate the document structures and register it first. |
1595 | | */ |
1596 | 0 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
1597 | 0 | if (ret == NULL) { |
1598 | 0 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
1599 | 0 | xmlFreeDoc(doc); |
1600 | 0 | return (NULL); |
1601 | 0 | } |
1602 | 0 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
1603 | 0 | ret->doc = doc; |
1604 | 0 | ret->href = xmlStrdup(URL); |
1605 | 0 | ret->next = ctxt->includes; |
1606 | 0 | ctxt->includes = ret; |
1607 | | |
1608 | | /* |
1609 | | * transmit the ns if needed |
1610 | | */ |
1611 | 0 | if (ns != NULL) { |
1612 | 0 | root = xmlDocGetRootElement(doc); |
1613 | 0 | if (root != NULL) { |
1614 | 0 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
1615 | 0 | xmlSetProp(root, BAD_CAST "ns", ns); |
1616 | 0 | } |
1617 | 0 | } |
1618 | 0 | } |
1619 | | |
1620 | | /* |
1621 | | * push it on the stack |
1622 | | */ |
1623 | 0 | xmlRelaxNGIncludePush(ctxt, ret); |
1624 | | |
1625 | | /* |
1626 | | * Some preprocessing of the document content, this include recursing |
1627 | | * in the include stack. |
1628 | | */ |
1629 | |
|
1630 | 0 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
1631 | 0 | if (doc == NULL) { |
1632 | 0 | ctxt->inc = NULL; |
1633 | 0 | return (NULL); |
1634 | 0 | } |
1635 | | |
1636 | | /* |
1637 | | * Pop up the include from the stack |
1638 | | */ |
1639 | 0 | xmlRelaxNGIncludePop(ctxt); |
1640 | | |
1641 | | /* |
1642 | | * Check that the top element is a grammar |
1643 | | */ |
1644 | 0 | root = xmlDocGetRootElement(doc); |
1645 | 0 | if (root == NULL) { |
1646 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
1647 | 0 | "xmlRelaxNG: included document is empty %s\n", URL, |
1648 | 0 | NULL); |
1649 | 0 | return (NULL); |
1650 | 0 | } |
1651 | 0 | if (!IS_RELAXNG(root, "grammar")) { |
1652 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
1653 | 0 | "xmlRelaxNG: included document %s root is not a grammar\n", |
1654 | 0 | URL, NULL); |
1655 | 0 | return (NULL); |
1656 | 0 | } |
1657 | | |
1658 | | /* |
1659 | | * Elimination of redefined rules in the include. |
1660 | | */ |
1661 | 0 | cur = node->children; |
1662 | 0 | while (cur != NULL) { |
1663 | 0 | if (IS_RELAXNG(cur, "start")) { |
1664 | 0 | int found = 0; |
1665 | |
|
1666 | 0 | found = |
1667 | 0 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
1668 | 0 | if (!found) { |
1669 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
1670 | 0 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
1671 | 0 | URL, NULL); |
1672 | 0 | } |
1673 | 0 | } else if (IS_RELAXNG(cur, "define")) { |
1674 | 0 | xmlChar *name; |
1675 | |
|
1676 | 0 | name = xmlGetProp(cur, BAD_CAST "name"); |
1677 | 0 | if (name == NULL) { |
1678 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
1679 | 0 | "xmlRelaxNG: include %s has define without name\n", |
1680 | 0 | URL, NULL); |
1681 | 0 | } else { |
1682 | 0 | int found; |
1683 | |
|
1684 | 0 | xmlRelaxNGNormExtSpace(name); |
1685 | 0 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
1686 | 0 | root->children, name); |
1687 | 0 | if (!found) { |
1688 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
1689 | 0 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
1690 | 0 | URL, name); |
1691 | 0 | } |
1692 | 0 | xmlFree(name); |
1693 | 0 | } |
1694 | 0 | } |
1695 | 0 | if (IS_RELAXNG(cur, "div") && cur->children != NULL) { |
1696 | 0 | cur = cur->children; |
1697 | 0 | } else { |
1698 | 0 | if (cur->next != NULL) { |
1699 | 0 | cur = cur->next; |
1700 | 0 | } else { |
1701 | 0 | while (cur->parent != node && cur->parent->next == NULL) { |
1702 | 0 | cur = cur->parent; |
1703 | 0 | } |
1704 | 0 | cur = cur->parent != node ? cur->parent->next : NULL; |
1705 | 0 | } |
1706 | 0 | } |
1707 | 0 | } |
1708 | | |
1709 | |
|
1710 | 0 | return (ret); |
1711 | 0 | } |
1712 | | |
1713 | | /** |
1714 | | * xmlRelaxNGValidErrorPush: |
1715 | | * @ctxt: the validation context |
1716 | | * @err: the error code |
1717 | | * @arg1: the first string argument |
1718 | | * @arg2: the second string argument |
1719 | | * @dup: arg need to be duplicated |
1720 | | * |
1721 | | * Pushes a new error on top of the error stack |
1722 | | * |
1723 | | * Returns 0 in case of error, the index in the stack otherwise |
1724 | | */ |
1725 | | static int |
1726 | | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
1727 | | xmlRelaxNGValidErr err, const xmlChar * arg1, |
1728 | | const xmlChar * arg2, int dup) |
1729 | 0 | { |
1730 | 0 | xmlRelaxNGValidErrorPtr cur; |
1731 | |
|
1732 | 0 | if (ctxt->errTab == NULL) { |
1733 | 0 | ctxt->errMax = 8; |
1734 | 0 | ctxt->errNr = 0; |
1735 | 0 | ctxt->errTab = |
1736 | 0 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
1737 | 0 | sizeof |
1738 | 0 | (xmlRelaxNGValidError)); |
1739 | 0 | if (ctxt->errTab == NULL) { |
1740 | 0 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
1741 | 0 | return (0); |
1742 | 0 | } |
1743 | 0 | ctxt->err = NULL; |
1744 | 0 | } |
1745 | 0 | if (ctxt->errNr >= ctxt->errMax) { |
1746 | 0 | ctxt->errMax *= 2; |
1747 | 0 | ctxt->errTab = |
1748 | 0 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
1749 | 0 | ctxt->errMax * |
1750 | 0 | sizeof |
1751 | 0 | (xmlRelaxNGValidError)); |
1752 | 0 | if (ctxt->errTab == NULL) { |
1753 | 0 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
1754 | 0 | return (0); |
1755 | 0 | } |
1756 | 0 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
1757 | 0 | } |
1758 | 0 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
1759 | 0 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
1760 | 0 | return (ctxt->errNr); |
1761 | 0 | cur = &ctxt->errTab[ctxt->errNr]; |
1762 | 0 | cur->err = err; |
1763 | 0 | if (dup) { |
1764 | 0 | cur->arg1 = xmlStrdup(arg1); |
1765 | 0 | cur->arg2 = xmlStrdup(arg2); |
1766 | 0 | cur->flags = ERROR_IS_DUP; |
1767 | 0 | } else { |
1768 | 0 | cur->arg1 = arg1; |
1769 | 0 | cur->arg2 = arg2; |
1770 | 0 | cur->flags = 0; |
1771 | 0 | } |
1772 | 0 | if (ctxt->state != NULL) { |
1773 | 0 | cur->node = ctxt->state->node; |
1774 | 0 | cur->seq = ctxt->state->seq; |
1775 | 0 | } else { |
1776 | 0 | cur->node = NULL; |
1777 | 0 | cur->seq = NULL; |
1778 | 0 | } |
1779 | 0 | ctxt->err = cur; |
1780 | 0 | return (ctxt->errNr++); |
1781 | 0 | } |
1782 | | |
1783 | | /** |
1784 | | * xmlRelaxNGValidErrorPop: |
1785 | | * @ctxt: the validation context |
1786 | | * |
1787 | | * Pops the top error from the error stack |
1788 | | */ |
1789 | | static void |
1790 | | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
1791 | 0 | { |
1792 | 0 | xmlRelaxNGValidErrorPtr cur; |
1793 | |
|
1794 | 0 | if (ctxt->errNr <= 0) { |
1795 | 0 | ctxt->err = NULL; |
1796 | 0 | return; |
1797 | 0 | } |
1798 | 0 | ctxt->errNr--; |
1799 | 0 | if (ctxt->errNr > 0) |
1800 | 0 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
1801 | 0 | else |
1802 | 0 | ctxt->err = NULL; |
1803 | 0 | cur = &ctxt->errTab[ctxt->errNr]; |
1804 | 0 | if (cur->flags & ERROR_IS_DUP) { |
1805 | 0 | if (cur->arg1 != NULL) |
1806 | 0 | xmlFree((xmlChar *) cur->arg1); |
1807 | 0 | cur->arg1 = NULL; |
1808 | 0 | if (cur->arg2 != NULL) |
1809 | 0 | xmlFree((xmlChar *) cur->arg2); |
1810 | 0 | cur->arg2 = NULL; |
1811 | 0 | cur->flags = 0; |
1812 | 0 | } |
1813 | 0 | } |
1814 | | |
1815 | | /** |
1816 | | * xmlRelaxNGDocumentPush: |
1817 | | * @ctxt: the parser context |
1818 | | * @value: the element doc |
1819 | | * |
1820 | | * Pushes a new doc on top of the doc stack |
1821 | | * |
1822 | | * Returns 0 in case of error, the index in the stack otherwise |
1823 | | */ |
1824 | | static int |
1825 | | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
1826 | | xmlRelaxNGDocumentPtr value) |
1827 | 0 | { |
1828 | 0 | if (ctxt->docTab == NULL) { |
1829 | 0 | ctxt->docMax = 4; |
1830 | 0 | ctxt->docNr = 0; |
1831 | 0 | ctxt->docTab = |
1832 | 0 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
1833 | 0 | sizeof(ctxt->docTab[0])); |
1834 | 0 | if (ctxt->docTab == NULL) { |
1835 | 0 | xmlRngPErrMemory(ctxt, "adding document\n"); |
1836 | 0 | return (0); |
1837 | 0 | } |
1838 | 0 | } |
1839 | 0 | if (ctxt->docNr >= ctxt->docMax) { |
1840 | 0 | ctxt->docMax *= 2; |
1841 | 0 | ctxt->docTab = |
1842 | 0 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
1843 | 0 | ctxt->docMax * |
1844 | 0 | sizeof(ctxt->docTab[0])); |
1845 | 0 | if (ctxt->docTab == NULL) { |
1846 | 0 | xmlRngPErrMemory(ctxt, "adding document\n"); |
1847 | 0 | return (0); |
1848 | 0 | } |
1849 | 0 | } |
1850 | 0 | ctxt->docTab[ctxt->docNr] = value; |
1851 | 0 | ctxt->doc = value; |
1852 | 0 | return (ctxt->docNr++); |
1853 | 0 | } |
1854 | | |
1855 | | /** |
1856 | | * xmlRelaxNGDocumentPop: |
1857 | | * @ctxt: the parser context |
1858 | | * |
1859 | | * Pops the top doc from the doc stack |
1860 | | * |
1861 | | * Returns the doc just removed |
1862 | | */ |
1863 | | static xmlRelaxNGDocumentPtr |
1864 | | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
1865 | 0 | { |
1866 | 0 | xmlRelaxNGDocumentPtr ret; |
1867 | |
|
1868 | 0 | if (ctxt->docNr <= 0) |
1869 | 0 | return (NULL); |
1870 | 0 | ctxt->docNr--; |
1871 | 0 | if (ctxt->docNr > 0) |
1872 | 0 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
1873 | 0 | else |
1874 | 0 | ctxt->doc = NULL; |
1875 | 0 | ret = ctxt->docTab[ctxt->docNr]; |
1876 | 0 | ctxt->docTab[ctxt->docNr] = NULL; |
1877 | 0 | return (ret); |
1878 | 0 | } |
1879 | | |
1880 | | /** |
1881 | | * xmlRelaxNGLoadExternalRef: |
1882 | | * @ctxt: the parser context |
1883 | | * @URL: the normalized URL |
1884 | | * @ns: the inherited ns if any |
1885 | | * |
1886 | | * First lookup if the document is already loaded into the parser context, |
1887 | | * check against recursion. If not found the resource is loaded and |
1888 | | * the content is preprocessed before being returned back to the caller. |
1889 | | * |
1890 | | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
1891 | | */ |
1892 | | static xmlRelaxNGDocumentPtr |
1893 | | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
1894 | | const xmlChar * URL, const xmlChar * ns) |
1895 | 0 | { |
1896 | 0 | xmlRelaxNGDocumentPtr ret = NULL; |
1897 | 0 | xmlDocPtr doc; |
1898 | 0 | xmlNodePtr root; |
1899 | 0 | int i; |
1900 | | |
1901 | | /* |
1902 | | * check against recursion in the stack |
1903 | | */ |
1904 | 0 | for (i = 0; i < ctxt->docNr; i++) { |
1905 | 0 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
1906 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
1907 | 0 | "Detected an externalRef recursion for %s\n", URL, |
1908 | 0 | NULL); |
1909 | 0 | return (NULL); |
1910 | 0 | } |
1911 | 0 | } |
1912 | | |
1913 | | /* |
1914 | | * load the document |
1915 | | */ |
1916 | 0 | doc = xmlReadFile((const char *) URL,NULL,0); |
1917 | 0 | if (doc == NULL) { |
1918 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
1919 | 0 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
1920 | 0 | return (NULL); |
1921 | 0 | } |
1922 | | |
1923 | | /* |
1924 | | * Allocate the document structures and register it first. |
1925 | | */ |
1926 | 0 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
1927 | 0 | if (ret == NULL) { |
1928 | 0 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
1929 | 0 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
1930 | 0 | xmlFreeDoc(doc); |
1931 | 0 | return (NULL); |
1932 | 0 | } |
1933 | 0 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
1934 | 0 | ret->doc = doc; |
1935 | 0 | ret->href = xmlStrdup(URL); |
1936 | 0 | ret->next = ctxt->documents; |
1937 | 0 | ret->externalRef = 1; |
1938 | 0 | ctxt->documents = ret; |
1939 | | |
1940 | | /* |
1941 | | * transmit the ns if needed |
1942 | | */ |
1943 | 0 | if (ns != NULL) { |
1944 | 0 | root = xmlDocGetRootElement(doc); |
1945 | 0 | if (root != NULL) { |
1946 | 0 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
1947 | 0 | xmlSetProp(root, BAD_CAST "ns", ns); |
1948 | 0 | } |
1949 | 0 | } |
1950 | 0 | } |
1951 | | |
1952 | | /* |
1953 | | * push it on the stack and register it in the hash table |
1954 | | */ |
1955 | 0 | xmlRelaxNGDocumentPush(ctxt, ret); |
1956 | | |
1957 | | /* |
1958 | | * Some preprocessing of the document content |
1959 | | */ |
1960 | 0 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
1961 | 0 | if (doc == NULL) { |
1962 | 0 | ctxt->doc = NULL; |
1963 | 0 | return (NULL); |
1964 | 0 | } |
1965 | | |
1966 | 0 | xmlRelaxNGDocumentPop(ctxt); |
1967 | |
|
1968 | 0 | return (ret); |
1969 | 0 | } |
1970 | | |
1971 | | /************************************************************************ |
1972 | | * * |
1973 | | * Error functions * |
1974 | | * * |
1975 | | ************************************************************************/ |
1976 | | |
1977 | 0 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
1978 | 0 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
1979 | 0 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
1980 | 0 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
1981 | 0 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
1982 | | |
1983 | | static const char * |
1984 | | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
1985 | 0 | { |
1986 | 0 | if (def == NULL) |
1987 | 0 | return ("none"); |
1988 | 0 | switch (def->type) { |
1989 | 0 | case XML_RELAXNG_EMPTY: |
1990 | 0 | return ("empty"); |
1991 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
1992 | 0 | return ("notAllowed"); |
1993 | 0 | case XML_RELAXNG_EXCEPT: |
1994 | 0 | return ("except"); |
1995 | 0 | case XML_RELAXNG_TEXT: |
1996 | 0 | return ("text"); |
1997 | 0 | case XML_RELAXNG_ELEMENT: |
1998 | 0 | return ("element"); |
1999 | 0 | case XML_RELAXNG_DATATYPE: |
2000 | 0 | return ("datatype"); |
2001 | 0 | case XML_RELAXNG_VALUE: |
2002 | 0 | return ("value"); |
2003 | 0 | case XML_RELAXNG_LIST: |
2004 | 0 | return ("list"); |
2005 | 0 | case XML_RELAXNG_ATTRIBUTE: |
2006 | 0 | return ("attribute"); |
2007 | 0 | case XML_RELAXNG_DEF: |
2008 | 0 | return ("def"); |
2009 | 0 | case XML_RELAXNG_REF: |
2010 | 0 | return ("ref"); |
2011 | 0 | case XML_RELAXNG_EXTERNALREF: |
2012 | 0 | return ("externalRef"); |
2013 | 0 | case XML_RELAXNG_PARENTREF: |
2014 | 0 | return ("parentRef"); |
2015 | 0 | case XML_RELAXNG_OPTIONAL: |
2016 | 0 | return ("optional"); |
2017 | 0 | case XML_RELAXNG_ZEROORMORE: |
2018 | 0 | return ("zeroOrMore"); |
2019 | 0 | case XML_RELAXNG_ONEORMORE: |
2020 | 0 | return ("oneOrMore"); |
2021 | 0 | case XML_RELAXNG_CHOICE: |
2022 | 0 | return ("choice"); |
2023 | 0 | case XML_RELAXNG_GROUP: |
2024 | 0 | return ("group"); |
2025 | 0 | case XML_RELAXNG_INTERLEAVE: |
2026 | 0 | return ("interleave"); |
2027 | 0 | case XML_RELAXNG_START: |
2028 | 0 | return ("start"); |
2029 | 0 | case XML_RELAXNG_NOOP: |
2030 | 0 | return ("noop"); |
2031 | 0 | case XML_RELAXNG_PARAM: |
2032 | 0 | return ("param"); |
2033 | 0 | } |
2034 | 0 | return ("unknown"); |
2035 | 0 | } |
2036 | | |
2037 | | /** |
2038 | | * xmlRelaxNGGetErrorString: |
2039 | | * @err: the error code |
2040 | | * @arg1: the first string argument |
2041 | | * @arg2: the second string argument |
2042 | | * |
2043 | | * computes a formatted error string for the given error code and args |
2044 | | * |
2045 | | * Returns the error string, it must be deallocated by the caller |
2046 | | */ |
2047 | | static xmlChar * |
2048 | | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
2049 | | const xmlChar * arg2) |
2050 | 0 | { |
2051 | 0 | char msg[1000]; |
2052 | 0 | xmlChar *result; |
2053 | |
|
2054 | 0 | if (arg1 == NULL) |
2055 | 0 | arg1 = BAD_CAST ""; |
2056 | 0 | if (arg2 == NULL) |
2057 | 0 | arg2 = BAD_CAST ""; |
2058 | |
|
2059 | 0 | msg[0] = 0; |
2060 | 0 | switch (err) { |
2061 | 0 | case XML_RELAXNG_OK: |
2062 | 0 | return (NULL); |
2063 | 0 | case XML_RELAXNG_ERR_MEMORY: |
2064 | 0 | return (xmlCharStrdup("out of memory\n")); |
2065 | 0 | case XML_RELAXNG_ERR_TYPE: |
2066 | 0 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
2067 | 0 | break; |
2068 | 0 | case XML_RELAXNG_ERR_TYPEVAL: |
2069 | 0 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
2070 | 0 | arg2); |
2071 | 0 | break; |
2072 | 0 | case XML_RELAXNG_ERR_DUPID: |
2073 | 0 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
2074 | 0 | break; |
2075 | 0 | case XML_RELAXNG_ERR_TYPECMP: |
2076 | 0 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
2077 | 0 | break; |
2078 | 0 | case XML_RELAXNG_ERR_NOSTATE: |
2079 | 0 | return (xmlCharStrdup("Internal error: no state\n")); |
2080 | 0 | case XML_RELAXNG_ERR_NODEFINE: |
2081 | 0 | return (xmlCharStrdup("Internal error: no define\n")); |
2082 | 0 | case XML_RELAXNG_ERR_INTERNAL: |
2083 | 0 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
2084 | 0 | break; |
2085 | 0 | case XML_RELAXNG_ERR_LISTEXTRA: |
2086 | 0 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
2087 | 0 | break; |
2088 | 0 | case XML_RELAXNG_ERR_INTERNODATA: |
2089 | 0 | return (xmlCharStrdup |
2090 | 0 | ("Internal: interleave block has no data\n")); |
2091 | 0 | case XML_RELAXNG_ERR_INTERSEQ: |
2092 | 0 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
2093 | 0 | case XML_RELAXNG_ERR_INTEREXTRA: |
2094 | 0 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
2095 | 0 | break; |
2096 | 0 | case XML_RELAXNG_ERR_ELEMNAME: |
2097 | 0 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
2098 | 0 | arg2); |
2099 | 0 | break; |
2100 | 0 | case XML_RELAXNG_ERR_ELEMNONS: |
2101 | 0 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
2102 | 0 | arg1); |
2103 | 0 | break; |
2104 | 0 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
2105 | 0 | snprintf(msg, 1000, |
2106 | 0 | "Element %s has wrong namespace: expecting %s\n", arg1, |
2107 | 0 | arg2); |
2108 | 0 | break; |
2109 | 0 | case XML_RELAXNG_ERR_ELEMWRONG: |
2110 | 0 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
2111 | 0 | break; |
2112 | 0 | case XML_RELAXNG_ERR_TEXTWRONG: |
2113 | 0 | snprintf(msg, 1000, |
2114 | 0 | "Did not expect text in element %s content\n", arg1); |
2115 | 0 | break; |
2116 | 0 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
2117 | 0 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
2118 | 0 | arg1); |
2119 | 0 | break; |
2120 | 0 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
2121 | 0 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
2122 | 0 | break; |
2123 | 0 | case XML_RELAXNG_ERR_NOELEM: |
2124 | 0 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
2125 | 0 | arg1); |
2126 | 0 | break; |
2127 | 0 | case XML_RELAXNG_ERR_NOTELEM: |
2128 | 0 | return (xmlCharStrdup("Expecting an element got text\n")); |
2129 | 0 | case XML_RELAXNG_ERR_ATTRVALID: |
2130 | 0 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
2131 | 0 | arg1); |
2132 | 0 | break; |
2133 | 0 | case XML_RELAXNG_ERR_CONTENTVALID: |
2134 | 0 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
2135 | 0 | arg1); |
2136 | 0 | break; |
2137 | 0 | case XML_RELAXNG_ERR_EXTRACONTENT: |
2138 | 0 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
2139 | 0 | arg1, arg2); |
2140 | 0 | break; |
2141 | 0 | case XML_RELAXNG_ERR_INVALIDATTR: |
2142 | 0 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
2143 | 0 | arg1, arg2); |
2144 | 0 | break; |
2145 | 0 | case XML_RELAXNG_ERR_LACKDATA: |
2146 | 0 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
2147 | 0 | arg1); |
2148 | 0 | break; |
2149 | 0 | case XML_RELAXNG_ERR_DATAELEM: |
2150 | 0 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
2151 | 0 | arg1); |
2152 | 0 | break; |
2153 | 0 | case XML_RELAXNG_ERR_VALELEM: |
2154 | 0 | snprintf(msg, 1000, "Value element %s has child elements\n", |
2155 | 0 | arg1); |
2156 | 0 | break; |
2157 | 0 | case XML_RELAXNG_ERR_LISTELEM: |
2158 | 0 | snprintf(msg, 1000, "List element %s has child elements\n", |
2159 | 0 | arg1); |
2160 | 0 | break; |
2161 | 0 | case XML_RELAXNG_ERR_DATATYPE: |
2162 | 0 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
2163 | 0 | break; |
2164 | 0 | case XML_RELAXNG_ERR_VALUE: |
2165 | 0 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
2166 | 0 | break; |
2167 | 0 | case XML_RELAXNG_ERR_LIST: |
2168 | 0 | return (xmlCharStrdup("Error validating list\n")); |
2169 | 0 | case XML_RELAXNG_ERR_NOGRAMMAR: |
2170 | 0 | return (xmlCharStrdup("No top grammar defined\n")); |
2171 | 0 | case XML_RELAXNG_ERR_EXTRADATA: |
2172 | 0 | return (xmlCharStrdup("Extra data in the document\n")); |
2173 | 0 | default: |
2174 | 0 | return (xmlCharStrdup("Unknown error !\n")); |
2175 | 0 | } |
2176 | 0 | if (msg[0] == 0) { |
2177 | 0 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
2178 | 0 | } |
2179 | 0 | msg[1000 - 1] = 0; |
2180 | 0 | result = xmlCharStrdup(msg); |
2181 | 0 | return (xmlEscapeFormatString(&result)); |
2182 | 0 | } |
2183 | | |
2184 | | /** |
2185 | | * xmlRelaxNGShowValidError: |
2186 | | * @ctxt: the validation context |
2187 | | * @err: the error number |
2188 | | * @node: the node |
2189 | | * @child: the node child generating the problem. |
2190 | | * @arg1: the first argument |
2191 | | * @arg2: the second argument |
2192 | | * |
2193 | | * Show a validation error. |
2194 | | */ |
2195 | | static void |
2196 | | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
2197 | | xmlRelaxNGValidErr err, xmlNodePtr node, |
2198 | | xmlNodePtr child, const xmlChar * arg1, |
2199 | | const xmlChar * arg2) |
2200 | 0 | { |
2201 | 0 | xmlChar *msg; |
2202 | |
|
2203 | 0 | if (ctxt->flags & FLAGS_NOERROR) |
2204 | 0 | return; |
2205 | | |
2206 | 0 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
2207 | 0 | if (msg == NULL) |
2208 | 0 | return; |
2209 | | |
2210 | 0 | if (ctxt->errNo == XML_RELAXNG_OK) |
2211 | 0 | ctxt->errNo = err; |
2212 | 0 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
2213 | 0 | (const char *) msg, arg1, arg2); |
2214 | 0 | xmlFree(msg); |
2215 | 0 | } |
2216 | | |
2217 | | /** |
2218 | | * xmlRelaxNGPopErrors: |
2219 | | * @ctxt: the validation context |
2220 | | * @level: the error level in the stack |
2221 | | * |
2222 | | * pop and discard all errors until the given level is reached |
2223 | | */ |
2224 | | static void |
2225 | | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
2226 | 0 | { |
2227 | 0 | int i; |
2228 | 0 | xmlRelaxNGValidErrorPtr err; |
2229 | |
|
2230 | 0 | for (i = level; i < ctxt->errNr; i++) { |
2231 | 0 | err = &ctxt->errTab[i]; |
2232 | 0 | if (err->flags & ERROR_IS_DUP) { |
2233 | 0 | if (err->arg1 != NULL) |
2234 | 0 | xmlFree((xmlChar *) err->arg1); |
2235 | 0 | err->arg1 = NULL; |
2236 | 0 | if (err->arg2 != NULL) |
2237 | 0 | xmlFree((xmlChar *) err->arg2); |
2238 | 0 | err->arg2 = NULL; |
2239 | 0 | err->flags = 0; |
2240 | 0 | } |
2241 | 0 | } |
2242 | 0 | ctxt->errNr = level; |
2243 | 0 | if (ctxt->errNr <= 0) |
2244 | 0 | ctxt->err = NULL; |
2245 | 0 | } |
2246 | | |
2247 | | /** |
2248 | | * xmlRelaxNGDumpValidError: |
2249 | | * @ctxt: the validation context |
2250 | | * |
2251 | | * Show all validation error over a given index. |
2252 | | */ |
2253 | | static void |
2254 | | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
2255 | 0 | { |
2256 | 0 | int i, j, k; |
2257 | 0 | xmlRelaxNGValidErrorPtr err, dup; |
2258 | |
|
2259 | 0 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
2260 | 0 | err = &ctxt->errTab[i]; |
2261 | 0 | if (k < MAX_ERROR) { |
2262 | 0 | for (j = 0; j < i; j++) { |
2263 | 0 | dup = &ctxt->errTab[j]; |
2264 | 0 | if ((err->err == dup->err) && (err->node == dup->node) && |
2265 | 0 | (xmlStrEqual(err->arg1, dup->arg1)) && |
2266 | 0 | (xmlStrEqual(err->arg2, dup->arg2))) { |
2267 | 0 | goto skip; |
2268 | 0 | } |
2269 | 0 | } |
2270 | 0 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
2271 | 0 | err->arg1, err->arg2); |
2272 | 0 | k++; |
2273 | 0 | } |
2274 | 0 | skip: |
2275 | 0 | if (err->flags & ERROR_IS_DUP) { |
2276 | 0 | if (err->arg1 != NULL) |
2277 | 0 | xmlFree((xmlChar *) err->arg1); |
2278 | 0 | err->arg1 = NULL; |
2279 | 0 | if (err->arg2 != NULL) |
2280 | 0 | xmlFree((xmlChar *) err->arg2); |
2281 | 0 | err->arg2 = NULL; |
2282 | 0 | err->flags = 0; |
2283 | 0 | } |
2284 | 0 | } |
2285 | 0 | ctxt->errNr = 0; |
2286 | 0 | } |
2287 | | |
2288 | | /** |
2289 | | * xmlRelaxNGAddValidError: |
2290 | | * @ctxt: the validation context |
2291 | | * @err: the error number |
2292 | | * @arg1: the first argument |
2293 | | * @arg2: the second argument |
2294 | | * @dup: need to dup the args |
2295 | | * |
2296 | | * Register a validation error, either generating it if it's sure |
2297 | | * or stacking it for later handling if unsure. |
2298 | | */ |
2299 | | static void |
2300 | | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
2301 | | xmlRelaxNGValidErr err, const xmlChar * arg1, |
2302 | | const xmlChar * arg2, int dup) |
2303 | 0 | { |
2304 | 0 | if (ctxt == NULL) |
2305 | 0 | return; |
2306 | 0 | if (ctxt->flags & FLAGS_NOERROR) |
2307 | 0 | return; |
2308 | | |
2309 | | /* |
2310 | | * generate the error directly |
2311 | | */ |
2312 | 0 | if (((ctxt->flags & FLAGS_IGNORABLE) == 0) || |
2313 | 0 | (ctxt->flags & FLAGS_NEGATIVE)) { |
2314 | 0 | xmlNodePtr node, seq; |
2315 | | |
2316 | | /* |
2317 | | * Flush first any stacked error which might be the |
2318 | | * real cause of the problem. |
2319 | | */ |
2320 | 0 | if (ctxt->errNr != 0) |
2321 | 0 | xmlRelaxNGDumpValidError(ctxt); |
2322 | 0 | if (ctxt->state != NULL) { |
2323 | 0 | node = ctxt->state->node; |
2324 | 0 | seq = ctxt->state->seq; |
2325 | 0 | } else { |
2326 | 0 | node = seq = NULL; |
2327 | 0 | } |
2328 | 0 | if ((node == NULL) && (seq == NULL)) { |
2329 | 0 | node = ctxt->pnode; |
2330 | 0 | } |
2331 | 0 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
2332 | 0 | } |
2333 | | /* |
2334 | | * Stack the error for later processing if needed |
2335 | | */ |
2336 | 0 | else { |
2337 | 0 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
2338 | 0 | } |
2339 | 0 | } |
2340 | | |
2341 | | |
2342 | | /************************************************************************ |
2343 | | * * |
2344 | | * Type library hooks * |
2345 | | * * |
2346 | | ************************************************************************/ |
2347 | | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
2348 | | const xmlChar * str); |
2349 | | |
2350 | | /** |
2351 | | * xmlRelaxNGSchemaTypeHave: |
2352 | | * @data: data needed for the library |
2353 | | * @type: the type name |
2354 | | * |
2355 | | * Check if the given type is provided by |
2356 | | * the W3C XMLSchema Datatype library. |
2357 | | * |
2358 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
2359 | | */ |
2360 | | static int |
2361 | | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
2362 | 0 | { |
2363 | 0 | xmlSchemaTypePtr typ; |
2364 | |
|
2365 | 0 | if (type == NULL) |
2366 | 0 | return (-1); |
2367 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2368 | 0 | BAD_CAST |
2369 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2370 | 0 | if (typ == NULL) |
2371 | 0 | return (0); |
2372 | 0 | return (1); |
2373 | 0 | } |
2374 | | |
2375 | | /** |
2376 | | * xmlRelaxNGSchemaTypeCheck: |
2377 | | * @data: data needed for the library |
2378 | | * @type: the type name |
2379 | | * @value: the value to check |
2380 | | * @node: the node |
2381 | | * |
2382 | | * Check if the given type and value are validated by |
2383 | | * the W3C XMLSchema Datatype library. |
2384 | | * |
2385 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
2386 | | */ |
2387 | | static int |
2388 | | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
2389 | | const xmlChar * type, |
2390 | | const xmlChar * value, |
2391 | | void **result, xmlNodePtr node) |
2392 | 0 | { |
2393 | 0 | xmlSchemaTypePtr typ; |
2394 | 0 | int ret; |
2395 | |
|
2396 | 0 | if ((type == NULL) || (value == NULL)) |
2397 | 0 | return (-1); |
2398 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2399 | 0 | BAD_CAST |
2400 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2401 | 0 | if (typ == NULL) |
2402 | 0 | return (-1); |
2403 | 0 | ret = xmlSchemaValPredefTypeNode(typ, value, |
2404 | 0 | (xmlSchemaValPtr *) result, node); |
2405 | 0 | if (ret == 2) /* special ID error code */ |
2406 | 0 | return (2); |
2407 | 0 | if (ret == 0) |
2408 | 0 | return (1); |
2409 | 0 | if (ret > 0) |
2410 | 0 | return (0); |
2411 | 0 | return (-1); |
2412 | 0 | } |
2413 | | |
2414 | | /** |
2415 | | * xmlRelaxNGSchemaFacetCheck: |
2416 | | * @data: data needed for the library |
2417 | | * @type: the type name |
2418 | | * @facet: the facet name |
2419 | | * @val: the facet value |
2420 | | * @strval: the string value |
2421 | | * @value: the value to check |
2422 | | * |
2423 | | * Function provided by a type library to check a value facet |
2424 | | * |
2425 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
2426 | | */ |
2427 | | static int |
2428 | | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
2429 | | const xmlChar * type, const xmlChar * facetname, |
2430 | | const xmlChar * val, const xmlChar * strval, |
2431 | | void *value) |
2432 | 0 | { |
2433 | 0 | xmlSchemaFacetPtr facet; |
2434 | 0 | xmlSchemaTypePtr typ; |
2435 | 0 | int ret; |
2436 | |
|
2437 | 0 | if ((type == NULL) || (strval == NULL)) |
2438 | 0 | return (-1); |
2439 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2440 | 0 | BAD_CAST |
2441 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2442 | 0 | if (typ == NULL) |
2443 | 0 | return (-1); |
2444 | | |
2445 | 0 | facet = xmlSchemaNewFacet(); |
2446 | 0 | if (facet == NULL) |
2447 | 0 | return (-1); |
2448 | | |
2449 | 0 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
2450 | 0 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
2451 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
2452 | 0 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
2453 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
2454 | 0 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
2455 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
2456 | 0 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
2457 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
2458 | 0 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
2459 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
2460 | 0 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
2461 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
2462 | 0 | facet->type = XML_SCHEMA_FACET_PATTERN; |
2463 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
2464 | 0 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
2465 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
2466 | 0 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
2467 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
2468 | 0 | facet->type = XML_SCHEMA_FACET_LENGTH; |
2469 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
2470 | 0 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
2471 | 0 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
2472 | 0 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
2473 | 0 | } else { |
2474 | 0 | xmlSchemaFreeFacet(facet); |
2475 | 0 | return (-1); |
2476 | 0 | } |
2477 | 0 | facet->value = val; |
2478 | 0 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
2479 | 0 | if (ret != 0) { |
2480 | 0 | xmlSchemaFreeFacet(facet); |
2481 | 0 | return (-1); |
2482 | 0 | } |
2483 | 0 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
2484 | 0 | xmlSchemaFreeFacet(facet); |
2485 | 0 | if (ret != 0) |
2486 | 0 | return (-1); |
2487 | 0 | return (0); |
2488 | 0 | } |
2489 | | |
2490 | | /** |
2491 | | * xmlRelaxNGSchemaFreeValue: |
2492 | | * @data: data needed for the library |
2493 | | * @value: the value to free |
2494 | | * |
2495 | | * Function provided by a type library to free a Schemas value |
2496 | | * |
2497 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
2498 | | */ |
2499 | | static void |
2500 | | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
2501 | 0 | { |
2502 | 0 | xmlSchemaFreeValue(value); |
2503 | 0 | } |
2504 | | |
2505 | | /** |
2506 | | * xmlRelaxNGSchemaTypeCompare: |
2507 | | * @data: data needed for the library |
2508 | | * @type: the type name |
2509 | | * @value1: the first value |
2510 | | * @value2: the second value |
2511 | | * |
2512 | | * Compare two values for equality accordingly a type from the W3C XMLSchema |
2513 | | * Datatype library. |
2514 | | * |
2515 | | * Returns 1 if equal, 0 if no and -1 in case of error. |
2516 | | */ |
2517 | | static int |
2518 | | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
2519 | | const xmlChar * type, |
2520 | | const xmlChar * value1, |
2521 | | xmlNodePtr ctxt1, |
2522 | | void *comp1, |
2523 | | const xmlChar * value2, xmlNodePtr ctxt2) |
2524 | 0 | { |
2525 | 0 | int ret; |
2526 | 0 | xmlSchemaTypePtr typ; |
2527 | 0 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
2528 | |
|
2529 | 0 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
2530 | 0 | return (-1); |
2531 | 0 | typ = xmlSchemaGetPredefinedType(type, |
2532 | 0 | BAD_CAST |
2533 | 0 | "http://www.w3.org/2001/XMLSchema"); |
2534 | 0 | if (typ == NULL) |
2535 | 0 | return (-1); |
2536 | 0 | if (comp1 == NULL) { |
2537 | 0 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
2538 | 0 | if (ret != 0) |
2539 | 0 | return (-1); |
2540 | 0 | if (res1 == NULL) |
2541 | 0 | return (-1); |
2542 | 0 | } else { |
2543 | 0 | res1 = (xmlSchemaValPtr) comp1; |
2544 | 0 | } |
2545 | 0 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
2546 | 0 | if (ret != 0) { |
2547 | 0 | if (res1 != (xmlSchemaValPtr) comp1) |
2548 | 0 | xmlSchemaFreeValue(res1); |
2549 | 0 | return (-1); |
2550 | 0 | } |
2551 | 0 | ret = xmlSchemaCompareValues(res1, res2); |
2552 | 0 | if (res1 != (xmlSchemaValPtr) comp1) |
2553 | 0 | xmlSchemaFreeValue(res1); |
2554 | 0 | xmlSchemaFreeValue(res2); |
2555 | 0 | if (ret == -2) |
2556 | 0 | return (-1); |
2557 | 0 | if (ret == 0) |
2558 | 0 | return (1); |
2559 | 0 | return (0); |
2560 | 0 | } |
2561 | | |
2562 | | /** |
2563 | | * xmlRelaxNGDefaultTypeHave: |
2564 | | * @data: data needed for the library |
2565 | | * @type: the type name |
2566 | | * |
2567 | | * Check if the given type is provided by |
2568 | | * the default datatype library. |
2569 | | * |
2570 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
2571 | | */ |
2572 | | static int |
2573 | | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
2574 | | const xmlChar * type) |
2575 | 0 | { |
2576 | 0 | if (type == NULL) |
2577 | 0 | return (-1); |
2578 | 0 | if (xmlStrEqual(type, BAD_CAST "string")) |
2579 | 0 | return (1); |
2580 | 0 | if (xmlStrEqual(type, BAD_CAST "token")) |
2581 | 0 | return (1); |
2582 | 0 | return (0); |
2583 | 0 | } |
2584 | | |
2585 | | /** |
2586 | | * xmlRelaxNGDefaultTypeCheck: |
2587 | | * @data: data needed for the library |
2588 | | * @type: the type name |
2589 | | * @value: the value to check |
2590 | | * @node: the node |
2591 | | * |
2592 | | * Check if the given type and value are validated by |
2593 | | * the default datatype library. |
2594 | | * |
2595 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
2596 | | */ |
2597 | | static int |
2598 | | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
2599 | | const xmlChar * type ATTRIBUTE_UNUSED, |
2600 | | const xmlChar * value ATTRIBUTE_UNUSED, |
2601 | | void **result ATTRIBUTE_UNUSED, |
2602 | | xmlNodePtr node ATTRIBUTE_UNUSED) |
2603 | 0 | { |
2604 | 0 | if (value == NULL) |
2605 | 0 | return (-1); |
2606 | 0 | if (xmlStrEqual(type, BAD_CAST "string")) |
2607 | 0 | return (1); |
2608 | 0 | if (xmlStrEqual(type, BAD_CAST "token")) { |
2609 | 0 | return (1); |
2610 | 0 | } |
2611 | | |
2612 | 0 | return (0); |
2613 | 0 | } |
2614 | | |
2615 | | /** |
2616 | | * xmlRelaxNGDefaultTypeCompare: |
2617 | | * @data: data needed for the library |
2618 | | * @type: the type name |
2619 | | * @value1: the first value |
2620 | | * @value2: the second value |
2621 | | * |
2622 | | * Compare two values accordingly a type from the default |
2623 | | * datatype library. |
2624 | | * |
2625 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
2626 | | */ |
2627 | | static int |
2628 | | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
2629 | | const xmlChar * type, |
2630 | | const xmlChar * value1, |
2631 | | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
2632 | | void *comp1 ATTRIBUTE_UNUSED, |
2633 | | const xmlChar * value2, |
2634 | | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
2635 | 0 | { |
2636 | 0 | int ret = -1; |
2637 | |
|
2638 | 0 | if (xmlStrEqual(type, BAD_CAST "string")) { |
2639 | 0 | ret = xmlStrEqual(value1, value2); |
2640 | 0 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
2641 | 0 | if (!xmlStrEqual(value1, value2)) { |
2642 | 0 | xmlChar *nval, *nvalue; |
2643 | | |
2644 | | /* |
2645 | | * TODO: trivial optimizations are possible by |
2646 | | * computing at compile-time |
2647 | | */ |
2648 | 0 | nval = xmlRelaxNGNormalize(NULL, value1); |
2649 | 0 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
2650 | |
|
2651 | 0 | if ((nval == NULL) || (nvalue == NULL)) |
2652 | 0 | ret = -1; |
2653 | 0 | else if (xmlStrEqual(nval, nvalue)) |
2654 | 0 | ret = 1; |
2655 | 0 | else |
2656 | 0 | ret = 0; |
2657 | 0 | if (nval != NULL) |
2658 | 0 | xmlFree(nval); |
2659 | 0 | if (nvalue != NULL) |
2660 | 0 | xmlFree(nvalue); |
2661 | 0 | } else |
2662 | 0 | ret = 1; |
2663 | 0 | } |
2664 | 0 | return (ret); |
2665 | 0 | } |
2666 | | |
2667 | | static int xmlRelaxNGTypeInitialized = 0; |
2668 | | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
2669 | | |
2670 | | /** |
2671 | | * xmlRelaxNGFreeTypeLibrary: |
2672 | | * @lib: the type library structure |
2673 | | * @namespace: the URI bound to the library |
2674 | | * |
2675 | | * Free the structure associated to the type library |
2676 | | */ |
2677 | | static void |
2678 | | xmlRelaxNGFreeTypeLibrary(void *payload, |
2679 | | const xmlChar * namespace ATTRIBUTE_UNUSED) |
2680 | 0 | { |
2681 | 0 | xmlRelaxNGTypeLibraryPtr lib = (xmlRelaxNGTypeLibraryPtr) payload; |
2682 | 0 | if (lib == NULL) |
2683 | 0 | return; |
2684 | 0 | if (lib->namespace != NULL) |
2685 | 0 | xmlFree((xmlChar *) lib->namespace); |
2686 | 0 | xmlFree(lib); |
2687 | 0 | } |
2688 | | |
2689 | | /** |
2690 | | * xmlRelaxNGRegisterTypeLibrary: |
2691 | | * @namespace: the URI bound to the library |
2692 | | * @data: data associated to the library |
2693 | | * @have: the provide function |
2694 | | * @check: the checking function |
2695 | | * @comp: the comparison function |
2696 | | * |
2697 | | * Register a new type library |
2698 | | * |
2699 | | * Returns 0 in case of success and -1 in case of error. |
2700 | | */ |
2701 | | static int |
2702 | | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
2703 | | xmlRelaxNGTypeHave have, |
2704 | | xmlRelaxNGTypeCheck check, |
2705 | | xmlRelaxNGTypeCompare comp, |
2706 | | xmlRelaxNGFacetCheck facet, |
2707 | | xmlRelaxNGTypeFree freef) |
2708 | 0 | { |
2709 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
2710 | 0 | int ret; |
2711 | |
|
2712 | 0 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
2713 | 0 | (check == NULL) || (comp == NULL)) |
2714 | 0 | return (-1); |
2715 | 0 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
2716 | 0 | xmlGenericError(xmlGenericErrorContext, |
2717 | 0 | "Relax-NG types library '%s' already registered\n", |
2718 | 0 | namespace); |
2719 | 0 | return (-1); |
2720 | 0 | } |
2721 | 0 | lib = |
2722 | 0 | (xmlRelaxNGTypeLibraryPtr) |
2723 | 0 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
2724 | 0 | if (lib == NULL) { |
2725 | 0 | xmlRngVErrMemory(NULL, "adding types library\n"); |
2726 | 0 | return (-1); |
2727 | 0 | } |
2728 | 0 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
2729 | 0 | lib->namespace = xmlStrdup(namespace); |
2730 | 0 | lib->data = data; |
2731 | 0 | lib->have = have; |
2732 | 0 | lib->comp = comp; |
2733 | 0 | lib->check = check; |
2734 | 0 | lib->facet = facet; |
2735 | 0 | lib->freef = freef; |
2736 | 0 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
2737 | 0 | if (ret < 0) { |
2738 | 0 | xmlGenericError(xmlGenericErrorContext, |
2739 | 0 | "Relax-NG types library failed to register '%s'\n", |
2740 | 0 | namespace); |
2741 | 0 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
2742 | 0 | return (-1); |
2743 | 0 | } |
2744 | 0 | return (0); |
2745 | 0 | } |
2746 | | |
2747 | | /** |
2748 | | * xmlRelaxNGInitTypes: |
2749 | | * |
2750 | | * Initialize the default type libraries. |
2751 | | * |
2752 | | * Returns 0 in case of success and -1 in case of error. |
2753 | | */ |
2754 | | int |
2755 | | xmlRelaxNGInitTypes(void) |
2756 | 0 | { |
2757 | 0 | if (xmlRelaxNGTypeInitialized != 0) |
2758 | 0 | return (0); |
2759 | 0 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
2760 | 0 | if (xmlRelaxNGRegisteredTypes == NULL) { |
2761 | 0 | xmlGenericError(xmlGenericErrorContext, |
2762 | 0 | "Failed to allocate sh table for Relax-NG types\n"); |
2763 | 0 | return (-1); |
2764 | 0 | } |
2765 | 0 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
2766 | 0 | "http://www.w3.org/2001/XMLSchema-datatypes", |
2767 | 0 | NULL, xmlRelaxNGSchemaTypeHave, |
2768 | 0 | xmlRelaxNGSchemaTypeCheck, |
2769 | 0 | xmlRelaxNGSchemaTypeCompare, |
2770 | 0 | xmlRelaxNGSchemaFacetCheck, |
2771 | 0 | xmlRelaxNGSchemaFreeValue); |
2772 | 0 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
2773 | 0 | xmlRelaxNGDefaultTypeHave, |
2774 | 0 | xmlRelaxNGDefaultTypeCheck, |
2775 | 0 | xmlRelaxNGDefaultTypeCompare, NULL, |
2776 | 0 | NULL); |
2777 | 0 | xmlRelaxNGTypeInitialized = 1; |
2778 | 0 | return (0); |
2779 | 0 | } |
2780 | | |
2781 | | /** |
2782 | | * xmlRelaxNGCleanupTypes: |
2783 | | * |
2784 | | * DEPRECATED: This function will be made private. Call xmlCleanupParser |
2785 | | * to free global state but see the warnings there. xmlCleanupParser |
2786 | | * should be only called once at program exit. In most cases, you don't |
2787 | | * have call cleanup functions at all. |
2788 | | * |
2789 | | * Cleanup the default Schemas type library associated to RelaxNG |
2790 | | */ |
2791 | | void |
2792 | | xmlRelaxNGCleanupTypes(void) |
2793 | 0 | { |
2794 | 0 | xmlSchemaCleanupTypes(); |
2795 | 0 | if (xmlRelaxNGTypeInitialized == 0) |
2796 | 0 | return; |
2797 | 0 | xmlHashFree(xmlRelaxNGRegisteredTypes, xmlRelaxNGFreeTypeLibrary); |
2798 | 0 | xmlRelaxNGTypeInitialized = 0; |
2799 | 0 | } |
2800 | | |
2801 | | /************************************************************************ |
2802 | | * * |
2803 | | * Compiling element content into regexp * |
2804 | | * * |
2805 | | * Sometime the element content can be compiled into a pure regexp, * |
2806 | | * This allows a faster execution and streamability at that level * |
2807 | | * * |
2808 | | ************************************************************************/ |
2809 | | |
2810 | | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
2811 | | xmlRelaxNGDefinePtr def); |
2812 | | |
2813 | | /** |
2814 | | * xmlRelaxNGIsCompilable: |
2815 | | * @define: the definition to check |
2816 | | * |
2817 | | * Check if a definition is nullable. |
2818 | | * |
2819 | | * Returns 1 if yes, 0 if no and -1 in case of error |
2820 | | */ |
2821 | | static int |
2822 | | xmlRelaxNGIsCompilable(xmlRelaxNGDefinePtr def) |
2823 | 0 | { |
2824 | 0 | int ret = -1; |
2825 | |
|
2826 | 0 | if (def == NULL) { |
2827 | 0 | return (-1); |
2828 | 0 | } |
2829 | 0 | if ((def->type != XML_RELAXNG_ELEMENT) && |
2830 | 0 | (def->dflags & IS_COMPILABLE)) |
2831 | 0 | return (1); |
2832 | 0 | if ((def->type != XML_RELAXNG_ELEMENT) && |
2833 | 0 | (def->dflags & IS_NOT_COMPILABLE)) |
2834 | 0 | return (0); |
2835 | 0 | switch (def->type) { |
2836 | 0 | case XML_RELAXNG_NOOP: |
2837 | 0 | ret = xmlRelaxNGIsCompilable(def->content); |
2838 | 0 | break; |
2839 | 0 | case XML_RELAXNG_TEXT: |
2840 | 0 | case XML_RELAXNG_EMPTY: |
2841 | 0 | ret = 1; |
2842 | 0 | break; |
2843 | 0 | case XML_RELAXNG_ELEMENT: |
2844 | | /* |
2845 | | * Check if the element content is compilable |
2846 | | */ |
2847 | 0 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
2848 | 0 | ((def->dflags & IS_COMPILABLE) == 0)) { |
2849 | 0 | xmlRelaxNGDefinePtr list; |
2850 | |
|
2851 | 0 | list = def->content; |
2852 | 0 | while (list != NULL) { |
2853 | 0 | ret = xmlRelaxNGIsCompilable(list); |
2854 | 0 | if (ret != 1) |
2855 | 0 | break; |
2856 | 0 | list = list->next; |
2857 | 0 | } |
2858 | | /* |
2859 | | * Because the routine is recursive, we must guard against |
2860 | | * discovering both COMPILABLE and NOT_COMPILABLE |
2861 | | */ |
2862 | 0 | if (ret == 0) { |
2863 | 0 | def->dflags &= ~IS_COMPILABLE; |
2864 | 0 | def->dflags |= IS_NOT_COMPILABLE; |
2865 | 0 | } |
2866 | 0 | if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE)) |
2867 | 0 | def->dflags |= IS_COMPILABLE; |
2868 | 0 | } |
2869 | | /* |
2870 | | * All elements return a compilable status unless they |
2871 | | * are generic like anyName |
2872 | | */ |
2873 | 0 | if ((def->nameClass != NULL) || (def->name == NULL)) |
2874 | 0 | ret = 0; |
2875 | 0 | else |
2876 | 0 | ret = 1; |
2877 | 0 | return (ret); |
2878 | 0 | case XML_RELAXNG_REF: |
2879 | 0 | case XML_RELAXNG_EXTERNALREF: |
2880 | 0 | case XML_RELAXNG_PARENTREF: |
2881 | 0 | if (def->depth == -20) { |
2882 | 0 | return (1); |
2883 | 0 | } else { |
2884 | 0 | xmlRelaxNGDefinePtr list; |
2885 | |
|
2886 | 0 | def->depth = -20; |
2887 | 0 | list = def->content; |
2888 | 0 | while (list != NULL) { |
2889 | 0 | ret = xmlRelaxNGIsCompilable(list); |
2890 | 0 | if (ret != 1) |
2891 | 0 | break; |
2892 | 0 | list = list->next; |
2893 | 0 | } |
2894 | 0 | } |
2895 | 0 | break; |
2896 | 0 | case XML_RELAXNG_START: |
2897 | 0 | case XML_RELAXNG_OPTIONAL: |
2898 | 0 | case XML_RELAXNG_ZEROORMORE: |
2899 | 0 | case XML_RELAXNG_ONEORMORE: |
2900 | 0 | case XML_RELAXNG_CHOICE: |
2901 | 0 | case XML_RELAXNG_GROUP: |
2902 | 0 | case XML_RELAXNG_DEF:{ |
2903 | 0 | xmlRelaxNGDefinePtr list; |
2904 | |
|
2905 | 0 | list = def->content; |
2906 | 0 | while (list != NULL) { |
2907 | 0 | ret = xmlRelaxNGIsCompilable(list); |
2908 | 0 | if (ret != 1) |
2909 | 0 | break; |
2910 | 0 | list = list->next; |
2911 | 0 | } |
2912 | 0 | break; |
2913 | 0 | } |
2914 | 0 | case XML_RELAXNG_EXCEPT: |
2915 | 0 | case XML_RELAXNG_ATTRIBUTE: |
2916 | 0 | case XML_RELAXNG_INTERLEAVE: |
2917 | 0 | case XML_RELAXNG_DATATYPE: |
2918 | 0 | case XML_RELAXNG_LIST: |
2919 | 0 | case XML_RELAXNG_PARAM: |
2920 | 0 | case XML_RELAXNG_VALUE: |
2921 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
2922 | 0 | ret = 0; |
2923 | 0 | break; |
2924 | 0 | } |
2925 | 0 | if (ret == 0) |
2926 | 0 | def->dflags |= IS_NOT_COMPILABLE; |
2927 | 0 | if (ret == 1) |
2928 | 0 | def->dflags |= IS_COMPILABLE; |
2929 | 0 | return (ret); |
2930 | 0 | } |
2931 | | |
2932 | | /** |
2933 | | * xmlRelaxNGCompile: |
2934 | | * ctxt: the RelaxNG parser context |
2935 | | * @define: the definition tree to compile |
2936 | | * |
2937 | | * Compile the set of definitions, it works recursively, till the |
2938 | | * element boundaries, where it tries to compile the content if possible |
2939 | | * |
2940 | | * Returns 0 if success and -1 in case of error |
2941 | | */ |
2942 | | static int |
2943 | | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
2944 | 0 | { |
2945 | 0 | int ret = 0; |
2946 | 0 | xmlRelaxNGDefinePtr list; |
2947 | |
|
2948 | 0 | if ((ctxt == NULL) || (def == NULL)) |
2949 | 0 | return (-1); |
2950 | | |
2951 | 0 | switch (def->type) { |
2952 | 0 | case XML_RELAXNG_START: |
2953 | 0 | if ((xmlRelaxNGIsCompilable(def) == 1) && (def->depth != -25)) { |
2954 | 0 | xmlAutomataPtr oldam = ctxt->am; |
2955 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
2956 | |
|
2957 | 0 | def->depth = -25; |
2958 | |
|
2959 | 0 | list = def->content; |
2960 | 0 | ctxt->am = xmlNewAutomata(); |
2961 | 0 | if (ctxt->am == NULL) |
2962 | 0 | return (-1); |
2963 | | |
2964 | | /* |
2965 | | * assume identical strings but not same pointer are different |
2966 | | * atoms, needed for non-determinism detection |
2967 | | * That way if 2 elements with the same name are in a choice |
2968 | | * branch the automata is found non-deterministic and |
2969 | | * we fallback to the normal validation which does the right |
2970 | | * thing of exploring both choices. |
2971 | | */ |
2972 | 0 | xmlAutomataSetFlags(ctxt->am, 1); |
2973 | |
|
2974 | 0 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
2975 | 0 | while (list != NULL) { |
2976 | 0 | xmlRelaxNGCompile(ctxt, list); |
2977 | 0 | list = list->next; |
2978 | 0 | } |
2979 | 0 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
2980 | 0 | if (xmlAutomataIsDeterminist(ctxt->am)) |
2981 | 0 | def->contModel = xmlAutomataCompile(ctxt->am); |
2982 | |
|
2983 | 0 | xmlFreeAutomata(ctxt->am); |
2984 | 0 | ctxt->state = oldstate; |
2985 | 0 | ctxt->am = oldam; |
2986 | 0 | } |
2987 | 0 | break; |
2988 | 0 | case XML_RELAXNG_ELEMENT: |
2989 | 0 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
2990 | 0 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
2991 | 0 | ctxt->state, NULL, |
2992 | 0 | def->name, def->ns, |
2993 | 0 | def); |
2994 | 0 | } |
2995 | 0 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
2996 | 0 | xmlAutomataPtr oldam = ctxt->am; |
2997 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
2998 | |
|
2999 | 0 | def->depth = -25; |
3000 | |
|
3001 | 0 | list = def->content; |
3002 | 0 | ctxt->am = xmlNewAutomata(); |
3003 | 0 | if (ctxt->am == NULL) |
3004 | 0 | return (-1); |
3005 | 0 | xmlAutomataSetFlags(ctxt->am, 1); |
3006 | 0 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
3007 | 0 | while (list != NULL) { |
3008 | 0 | xmlRelaxNGCompile(ctxt, list); |
3009 | 0 | list = list->next; |
3010 | 0 | } |
3011 | 0 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
3012 | 0 | def->contModel = xmlAutomataCompile(ctxt->am); |
3013 | 0 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
3014 | | /* |
3015 | | * we can only use the automata if it is determinist |
3016 | | */ |
3017 | 0 | xmlRegFreeRegexp(def->contModel); |
3018 | 0 | def->contModel = NULL; |
3019 | 0 | } |
3020 | 0 | xmlFreeAutomata(ctxt->am); |
3021 | 0 | ctxt->state = oldstate; |
3022 | 0 | ctxt->am = oldam; |
3023 | 0 | } else { |
3024 | 0 | xmlAutomataPtr oldam = ctxt->am; |
3025 | | |
3026 | | /* |
3027 | | * we can't build the content model for this element content |
3028 | | * but it still might be possible to build it for some of its |
3029 | | * children, recurse. |
3030 | | */ |
3031 | 0 | ret = xmlRelaxNGTryCompile(ctxt, def); |
3032 | 0 | ctxt->am = oldam; |
3033 | 0 | } |
3034 | 0 | break; |
3035 | 0 | case XML_RELAXNG_NOOP: |
3036 | 0 | ret = xmlRelaxNGCompile(ctxt, def->content); |
3037 | 0 | break; |
3038 | 0 | case XML_RELAXNG_OPTIONAL:{ |
3039 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
3040 | |
|
3041 | 0 | list = def->content; |
3042 | 0 | while (list != NULL) { |
3043 | 0 | xmlRelaxNGCompile(ctxt, list); |
3044 | 0 | list = list->next; |
3045 | 0 | } |
3046 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
3047 | 0 | break; |
3048 | 0 | } |
3049 | 0 | case XML_RELAXNG_ZEROORMORE:{ |
3050 | 0 | xmlAutomataStatePtr oldstate; |
3051 | |
|
3052 | 0 | ctxt->state = |
3053 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
3054 | 0 | oldstate = ctxt->state; |
3055 | 0 | list = def->content; |
3056 | 0 | while (list != NULL) { |
3057 | 0 | xmlRelaxNGCompile(ctxt, list); |
3058 | 0 | list = list->next; |
3059 | 0 | } |
3060 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
3061 | 0 | ctxt->state = |
3062 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
3063 | 0 | break; |
3064 | 0 | } |
3065 | 0 | case XML_RELAXNG_ONEORMORE:{ |
3066 | 0 | xmlAutomataStatePtr oldstate; |
3067 | |
|
3068 | 0 | list = def->content; |
3069 | 0 | while (list != NULL) { |
3070 | 0 | xmlRelaxNGCompile(ctxt, list); |
3071 | 0 | list = list->next; |
3072 | 0 | } |
3073 | 0 | oldstate = ctxt->state; |
3074 | 0 | list = def->content; |
3075 | 0 | while (list != NULL) { |
3076 | 0 | xmlRelaxNGCompile(ctxt, list); |
3077 | 0 | list = list->next; |
3078 | 0 | } |
3079 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
3080 | 0 | ctxt->state = |
3081 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
3082 | 0 | break; |
3083 | 0 | } |
3084 | 0 | case XML_RELAXNG_CHOICE:{ |
3085 | 0 | xmlAutomataStatePtr target = NULL; |
3086 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
3087 | |
|
3088 | 0 | list = def->content; |
3089 | 0 | while (list != NULL) { |
3090 | 0 | ctxt->state = oldstate; |
3091 | 0 | ret = xmlRelaxNGCompile(ctxt, list); |
3092 | 0 | if (ret != 0) |
3093 | 0 | break; |
3094 | 0 | if (target == NULL) |
3095 | 0 | target = ctxt->state; |
3096 | 0 | else { |
3097 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
3098 | 0 | target); |
3099 | 0 | } |
3100 | 0 | list = list->next; |
3101 | 0 | } |
3102 | 0 | ctxt->state = target; |
3103 | |
|
3104 | 0 | break; |
3105 | 0 | } |
3106 | 0 | case XML_RELAXNG_REF: |
3107 | 0 | case XML_RELAXNG_EXTERNALREF: |
3108 | 0 | case XML_RELAXNG_PARENTREF: |
3109 | 0 | case XML_RELAXNG_GROUP: |
3110 | 0 | case XML_RELAXNG_DEF: |
3111 | 0 | list = def->content; |
3112 | 0 | while (list != NULL) { |
3113 | 0 | ret = xmlRelaxNGCompile(ctxt, list); |
3114 | 0 | if (ret != 0) |
3115 | 0 | break; |
3116 | 0 | list = list->next; |
3117 | 0 | } |
3118 | 0 | break; |
3119 | 0 | case XML_RELAXNG_TEXT:{ |
3120 | 0 | xmlAutomataStatePtr oldstate; |
3121 | |
|
3122 | 0 | ctxt->state = |
3123 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
3124 | 0 | oldstate = ctxt->state; |
3125 | 0 | xmlRelaxNGCompile(ctxt, def->content); |
3126 | 0 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
3127 | 0 | ctxt->state, BAD_CAST "#text", |
3128 | 0 | NULL); |
3129 | 0 | ctxt->state = |
3130 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
3131 | 0 | break; |
3132 | 0 | } |
3133 | 0 | case XML_RELAXNG_EMPTY: |
3134 | 0 | ctxt->state = |
3135 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
3136 | 0 | break; |
3137 | 0 | case XML_RELAXNG_EXCEPT: |
3138 | 0 | case XML_RELAXNG_ATTRIBUTE: |
3139 | 0 | case XML_RELAXNG_INTERLEAVE: |
3140 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
3141 | 0 | case XML_RELAXNG_DATATYPE: |
3142 | 0 | case XML_RELAXNG_LIST: |
3143 | 0 | case XML_RELAXNG_PARAM: |
3144 | 0 | case XML_RELAXNG_VALUE: |
3145 | | /* This should not happen and generate an internal error */ |
3146 | 0 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
3147 | 0 | xmlRelaxNGDefName(def)); |
3148 | 0 | break; |
3149 | 0 | } |
3150 | 0 | return (ret); |
3151 | 0 | } |
3152 | | |
3153 | | /** |
3154 | | * xmlRelaxNGTryCompile: |
3155 | | * ctxt: the RelaxNG parser context |
3156 | | * @define: the definition tree to compile |
3157 | | * |
3158 | | * Try to compile the set of definitions, it works recursively, |
3159 | | * possibly ignoring parts which cannot be compiled. |
3160 | | * |
3161 | | * Returns 0 if success and -1 in case of error |
3162 | | */ |
3163 | | static int |
3164 | | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
3165 | 0 | { |
3166 | 0 | int ret = 0; |
3167 | 0 | xmlRelaxNGDefinePtr list; |
3168 | |
|
3169 | 0 | if ((ctxt == NULL) || (def == NULL)) |
3170 | 0 | return (-1); |
3171 | | |
3172 | 0 | if ((def->type == XML_RELAXNG_START) || |
3173 | 0 | (def->type == XML_RELAXNG_ELEMENT)) { |
3174 | 0 | ret = xmlRelaxNGIsCompilable(def); |
3175 | 0 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
3176 | 0 | ctxt->am = NULL; |
3177 | 0 | ret = xmlRelaxNGCompile(ctxt, def); |
3178 | 0 | return (ret); |
3179 | 0 | } |
3180 | 0 | } |
3181 | 0 | switch (def->type) { |
3182 | 0 | case XML_RELAXNG_NOOP: |
3183 | 0 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
3184 | 0 | break; |
3185 | 0 | case XML_RELAXNG_TEXT: |
3186 | 0 | case XML_RELAXNG_DATATYPE: |
3187 | 0 | case XML_RELAXNG_LIST: |
3188 | 0 | case XML_RELAXNG_PARAM: |
3189 | 0 | case XML_RELAXNG_VALUE: |
3190 | 0 | case XML_RELAXNG_EMPTY: |
3191 | 0 | case XML_RELAXNG_ELEMENT: |
3192 | 0 | ret = 0; |
3193 | 0 | break; |
3194 | 0 | case XML_RELAXNG_OPTIONAL: |
3195 | 0 | case XML_RELAXNG_ZEROORMORE: |
3196 | 0 | case XML_RELAXNG_ONEORMORE: |
3197 | 0 | case XML_RELAXNG_CHOICE: |
3198 | 0 | case XML_RELAXNG_GROUP: |
3199 | 0 | case XML_RELAXNG_DEF: |
3200 | 0 | case XML_RELAXNG_START: |
3201 | 0 | case XML_RELAXNG_REF: |
3202 | 0 | case XML_RELAXNG_EXTERNALREF: |
3203 | 0 | case XML_RELAXNG_PARENTREF: |
3204 | 0 | list = def->content; |
3205 | 0 | while (list != NULL) { |
3206 | 0 | ret = xmlRelaxNGTryCompile(ctxt, list); |
3207 | 0 | if (ret != 0) |
3208 | 0 | break; |
3209 | 0 | list = list->next; |
3210 | 0 | } |
3211 | 0 | break; |
3212 | 0 | case XML_RELAXNG_EXCEPT: |
3213 | 0 | case XML_RELAXNG_ATTRIBUTE: |
3214 | 0 | case XML_RELAXNG_INTERLEAVE: |
3215 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
3216 | 0 | ret = 0; |
3217 | 0 | break; |
3218 | 0 | } |
3219 | 0 | return (ret); |
3220 | 0 | } |
3221 | | |
3222 | | /************************************************************************ |
3223 | | * * |
3224 | | * Parsing functions * |
3225 | | * * |
3226 | | ************************************************************************/ |
3227 | | |
3228 | | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
3229 | | ctxt, xmlNodePtr node); |
3230 | | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
3231 | | ctxt, xmlNodePtr node); |
3232 | | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
3233 | | ctxt, xmlNodePtr nodes, |
3234 | | int group); |
3235 | | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
3236 | | ctxt, xmlNodePtr node); |
3237 | | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
3238 | | xmlNodePtr node); |
3239 | | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
3240 | | xmlNodePtr nodes); |
3241 | | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
3242 | | ctxt, xmlNodePtr node, |
3243 | | xmlRelaxNGDefinePtr |
3244 | | def); |
3245 | | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
3246 | | ctxt, xmlNodePtr nodes); |
3247 | | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
3248 | | xmlRelaxNGDefinePtr define, |
3249 | | xmlNodePtr elem); |
3250 | | |
3251 | | |
3252 | 0 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
3253 | | |
3254 | | /** |
3255 | | * xmlRelaxNGIsNullable: |
3256 | | * @define: the definition to verify |
3257 | | * |
3258 | | * Check if a definition is nullable. |
3259 | | * |
3260 | | * Returns 1 if yes, 0 if no and -1 in case of error |
3261 | | */ |
3262 | | static int |
3263 | | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
3264 | 0 | { |
3265 | 0 | int ret; |
3266 | |
|
3267 | 0 | if (define == NULL) |
3268 | 0 | return (-1); |
3269 | | |
3270 | 0 | if (define->dflags & IS_NULLABLE) |
3271 | 0 | return (1); |
3272 | 0 | if (define->dflags & IS_NOT_NULLABLE) |
3273 | 0 | return (0); |
3274 | 0 | switch (define->type) { |
3275 | 0 | case XML_RELAXNG_EMPTY: |
3276 | 0 | case XML_RELAXNG_TEXT: |
3277 | 0 | ret = 1; |
3278 | 0 | break; |
3279 | 0 | case XML_RELAXNG_NOOP: |
3280 | 0 | case XML_RELAXNG_DEF: |
3281 | 0 | case XML_RELAXNG_REF: |
3282 | 0 | case XML_RELAXNG_EXTERNALREF: |
3283 | 0 | case XML_RELAXNG_PARENTREF: |
3284 | 0 | case XML_RELAXNG_ONEORMORE: |
3285 | 0 | ret = xmlRelaxNGIsNullable(define->content); |
3286 | 0 | break; |
3287 | 0 | case XML_RELAXNG_EXCEPT: |
3288 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
3289 | 0 | case XML_RELAXNG_ELEMENT: |
3290 | 0 | case XML_RELAXNG_DATATYPE: |
3291 | 0 | case XML_RELAXNG_PARAM: |
3292 | 0 | case XML_RELAXNG_VALUE: |
3293 | 0 | case XML_RELAXNG_LIST: |
3294 | 0 | case XML_RELAXNG_ATTRIBUTE: |
3295 | 0 | ret = 0; |
3296 | 0 | break; |
3297 | 0 | case XML_RELAXNG_CHOICE:{ |
3298 | 0 | xmlRelaxNGDefinePtr list = define->content; |
3299 | |
|
3300 | 0 | while (list != NULL) { |
3301 | 0 | ret = xmlRelaxNGIsNullable(list); |
3302 | 0 | if (ret != 0) |
3303 | 0 | goto done; |
3304 | 0 | list = list->next; |
3305 | 0 | } |
3306 | 0 | ret = 0; |
3307 | 0 | break; |
3308 | 0 | } |
3309 | 0 | case XML_RELAXNG_START: |
3310 | 0 | case XML_RELAXNG_INTERLEAVE: |
3311 | 0 | case XML_RELAXNG_GROUP:{ |
3312 | 0 | xmlRelaxNGDefinePtr list = define->content; |
3313 | |
|
3314 | 0 | while (list != NULL) { |
3315 | 0 | ret = xmlRelaxNGIsNullable(list); |
3316 | 0 | if (ret != 1) |
3317 | 0 | goto done; |
3318 | 0 | list = list->next; |
3319 | 0 | } |
3320 | 0 | return (1); |
3321 | 0 | } |
3322 | 0 | default: |
3323 | 0 | return (-1); |
3324 | 0 | } |
3325 | 0 | done: |
3326 | 0 | if (ret == 0) |
3327 | 0 | define->dflags |= IS_NOT_NULLABLE; |
3328 | 0 | if (ret == 1) |
3329 | 0 | define->dflags |= IS_NULLABLE; |
3330 | 0 | return (ret); |
3331 | 0 | } |
3332 | | |
3333 | | /** |
3334 | | * xmlRelaxNGIsBlank: |
3335 | | * @str: a string |
3336 | | * |
3337 | | * Check if a string is ignorable c.f. 4.2. Whitespace |
3338 | | * |
3339 | | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
3340 | | */ |
3341 | | static int |
3342 | | xmlRelaxNGIsBlank(xmlChar * str) |
3343 | 0 | { |
3344 | 0 | if (str == NULL) |
3345 | 0 | return (1); |
3346 | 0 | while (*str != 0) { |
3347 | 0 | if (!(IS_BLANK_CH(*str))) |
3348 | 0 | return (0); |
3349 | 0 | str++; |
3350 | 0 | } |
3351 | 0 | return (1); |
3352 | 0 | } |
3353 | | |
3354 | | /** |
3355 | | * xmlRelaxNGGetDataTypeLibrary: |
3356 | | * @ctxt: a Relax-NG parser context |
3357 | | * @node: the current data or value element |
3358 | | * |
3359 | | * Applies algorithm from 4.3. datatypeLibrary attribute |
3360 | | * |
3361 | | * Returns the datatypeLibrary value or NULL if not found |
3362 | | */ |
3363 | | static xmlChar * |
3364 | | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
3365 | | xmlNodePtr node) |
3366 | 0 | { |
3367 | 0 | xmlChar *ret, *escape; |
3368 | |
|
3369 | 0 | if (node == NULL) |
3370 | 0 | return(NULL); |
3371 | | |
3372 | 0 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
3373 | 0 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
3374 | 0 | if (ret != NULL) { |
3375 | 0 | if (ret[0] == 0) { |
3376 | 0 | xmlFree(ret); |
3377 | 0 | return (NULL); |
3378 | 0 | } |
3379 | 0 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
3380 | 0 | if (escape == NULL) { |
3381 | 0 | return (ret); |
3382 | 0 | } |
3383 | 0 | xmlFree(ret); |
3384 | 0 | return (escape); |
3385 | 0 | } |
3386 | 0 | } |
3387 | 0 | node = node->parent; |
3388 | 0 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
3389 | 0 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
3390 | 0 | if (ret != NULL) { |
3391 | 0 | if (ret[0] == 0) { |
3392 | 0 | xmlFree(ret); |
3393 | 0 | return (NULL); |
3394 | 0 | } |
3395 | 0 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
3396 | 0 | if (escape == NULL) { |
3397 | 0 | return (ret); |
3398 | 0 | } |
3399 | 0 | xmlFree(ret); |
3400 | 0 | return (escape); |
3401 | 0 | } |
3402 | 0 | node = node->parent; |
3403 | 0 | } |
3404 | 0 | return (NULL); |
3405 | 0 | } |
3406 | | |
3407 | | /** |
3408 | | * xmlRelaxNGParseValue: |
3409 | | * @ctxt: a Relax-NG parser context |
3410 | | * @node: the data node. |
3411 | | * |
3412 | | * parse the content of a RelaxNG value node. |
3413 | | * |
3414 | | * Returns the definition pointer or NULL in case of error |
3415 | | */ |
3416 | | static xmlRelaxNGDefinePtr |
3417 | | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
3418 | 0 | { |
3419 | 0 | xmlRelaxNGDefinePtr def = NULL; |
3420 | 0 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
3421 | 0 | xmlChar *type; |
3422 | 0 | xmlChar *library; |
3423 | 0 | int success = 0; |
3424 | |
|
3425 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
3426 | 0 | if (def == NULL) |
3427 | 0 | return (NULL); |
3428 | 0 | def->type = XML_RELAXNG_VALUE; |
3429 | |
|
3430 | 0 | type = xmlGetProp(node, BAD_CAST "type"); |
3431 | 0 | if (type != NULL) { |
3432 | 0 | xmlRelaxNGNormExtSpace(type); |
3433 | 0 | if (xmlValidateNCName(type, 0)) { |
3434 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
3435 | 0 | "value type '%s' is not an NCName\n", type, NULL); |
3436 | 0 | } |
3437 | 0 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
3438 | 0 | if (library == NULL) |
3439 | 0 | library = |
3440 | 0 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
3441 | |
|
3442 | 0 | def->name = type; |
3443 | 0 | def->ns = library; |
3444 | |
|
3445 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) |
3446 | 0 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
3447 | 0 | if (lib == NULL) { |
3448 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
3449 | 0 | "Use of unregistered type library '%s'\n", library, |
3450 | 0 | NULL); |
3451 | 0 | def->data = NULL; |
3452 | 0 | } else { |
3453 | 0 | def->data = lib; |
3454 | 0 | if (lib->have == NULL) { |
3455 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
3456 | 0 | "Internal error with type library '%s': no 'have'\n", |
3457 | 0 | library, NULL); |
3458 | 0 | } else { |
3459 | 0 | success = lib->have(lib->data, def->name); |
3460 | 0 | if (success != 1) { |
3461 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
3462 | 0 | "Error type '%s' is not exported by type library '%s'\n", |
3463 | 0 | def->name, library); |
3464 | 0 | } |
3465 | 0 | } |
3466 | 0 | } |
3467 | 0 | } |
3468 | 0 | if (node->children == NULL) { |
3469 | 0 | def->value = xmlStrdup(BAD_CAST ""); |
3470 | 0 | } else if (((node->children->type != XML_TEXT_NODE) && |
3471 | 0 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
3472 | 0 | (node->children->next != NULL)) { |
3473 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
3474 | 0 | "Expecting a single text value for <value>content\n", |
3475 | 0 | NULL, NULL); |
3476 | 0 | } else if (def != NULL) { |
3477 | 0 | def->value = xmlNodeGetContent(node); |
3478 | 0 | if (def->value == NULL) { |
3479 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
3480 | 0 | "Element <value> has no content\n", NULL, NULL); |
3481 | 0 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
3482 | 0 | void *val = NULL; |
3483 | |
|
3484 | 0 | success = |
3485 | 0 | lib->check(lib->data, def->name, def->value, &val, node); |
3486 | 0 | if (success != 1) { |
3487 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
3488 | 0 | "Value '%s' is not acceptable for type '%s'\n", |
3489 | 0 | def->value, def->name); |
3490 | 0 | } else { |
3491 | 0 | if (val != NULL) |
3492 | 0 | def->attrs = val; |
3493 | 0 | } |
3494 | 0 | } |
3495 | 0 | } |
3496 | 0 | return (def); |
3497 | 0 | } |
3498 | | |
3499 | | /** |
3500 | | * xmlRelaxNGParseData: |
3501 | | * @ctxt: a Relax-NG parser context |
3502 | | * @node: the data node. |
3503 | | * |
3504 | | * parse the content of a RelaxNG data node. |
3505 | | * |
3506 | | * Returns the definition pointer or NULL in case of error |
3507 | | */ |
3508 | | static xmlRelaxNGDefinePtr |
3509 | | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
3510 | 0 | { |
3511 | 0 | xmlRelaxNGDefinePtr def = NULL, except; |
3512 | 0 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
3513 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
3514 | 0 | xmlChar *type; |
3515 | 0 | xmlChar *library; |
3516 | 0 | xmlNodePtr content; |
3517 | 0 | int tmp; |
3518 | |
|
3519 | 0 | type = xmlGetProp(node, BAD_CAST "type"); |
3520 | 0 | if (type == NULL) { |
3521 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
3522 | 0 | NULL); |
3523 | 0 | return (NULL); |
3524 | 0 | } |
3525 | 0 | xmlRelaxNGNormExtSpace(type); |
3526 | 0 | if (xmlValidateNCName(type, 0)) { |
3527 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
3528 | 0 | "data type '%s' is not an NCName\n", type, NULL); |
3529 | 0 | } |
3530 | 0 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
3531 | 0 | if (library == NULL) |
3532 | 0 | library = |
3533 | 0 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
3534 | |
|
3535 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
3536 | 0 | if (def == NULL) { |
3537 | 0 | xmlFree(library); |
3538 | 0 | xmlFree(type); |
3539 | 0 | return (NULL); |
3540 | 0 | } |
3541 | 0 | def->type = XML_RELAXNG_DATATYPE; |
3542 | 0 | def->name = type; |
3543 | 0 | def->ns = library; |
3544 | |
|
3545 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) |
3546 | 0 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
3547 | 0 | if (lib == NULL) { |
3548 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
3549 | 0 | "Use of unregistered type library '%s'\n", library, |
3550 | 0 | NULL); |
3551 | 0 | def->data = NULL; |
3552 | 0 | } else { |
3553 | 0 | def->data = lib; |
3554 | 0 | if (lib->have == NULL) { |
3555 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
3556 | 0 | "Internal error with type library '%s': no 'have'\n", |
3557 | 0 | library, NULL); |
3558 | 0 | } else { |
3559 | 0 | tmp = lib->have(lib->data, def->name); |
3560 | 0 | if (tmp != 1) { |
3561 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
3562 | 0 | "Error type '%s' is not exported by type library '%s'\n", |
3563 | 0 | def->name, library); |
3564 | 0 | } else |
3565 | 0 | if ((xmlStrEqual |
3566 | 0 | (library, |
3567 | 0 | BAD_CAST |
3568 | 0 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
3569 | 0 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
3570 | 0 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
3571 | 0 | ctxt->idref = 1; |
3572 | 0 | } |
3573 | 0 | } |
3574 | 0 | } |
3575 | 0 | content = node->children; |
3576 | | |
3577 | | /* |
3578 | | * Handle optional params |
3579 | | */ |
3580 | 0 | while (content != NULL) { |
3581 | 0 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
3582 | 0 | break; |
3583 | 0 | if (xmlStrEqual(library, |
3584 | 0 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
3585 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
3586 | 0 | "Type library '%s' does not allow type parameters\n", |
3587 | 0 | library, NULL); |
3588 | 0 | content = content->next; |
3589 | 0 | while ((content != NULL) && |
3590 | 0 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
3591 | 0 | content = content->next; |
3592 | 0 | } else { |
3593 | 0 | param = xmlRelaxNGNewDefine(ctxt, node); |
3594 | 0 | if (param != NULL) { |
3595 | 0 | param->type = XML_RELAXNG_PARAM; |
3596 | 0 | param->name = xmlGetProp(content, BAD_CAST "name"); |
3597 | 0 | if (param->name == NULL) { |
3598 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
3599 | 0 | "param has no name\n", NULL, NULL); |
3600 | 0 | } |
3601 | 0 | param->value = xmlNodeGetContent(content); |
3602 | 0 | if (lastparam == NULL) { |
3603 | 0 | def->attrs = lastparam = param; |
3604 | 0 | } else { |
3605 | 0 | lastparam->next = param; |
3606 | 0 | lastparam = param; |
3607 | 0 | } |
3608 | 0 | if (lib != NULL) { |
3609 | 0 | } |
3610 | 0 | } |
3611 | 0 | content = content->next; |
3612 | 0 | } |
3613 | 0 | } |
3614 | | /* |
3615 | | * Handle optional except |
3616 | | */ |
3617 | 0 | if ((content != NULL) |
3618 | 0 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
3619 | 0 | xmlNodePtr child; |
3620 | 0 | xmlRelaxNGDefinePtr tmp2, last = NULL; |
3621 | |
|
3622 | 0 | except = xmlRelaxNGNewDefine(ctxt, node); |
3623 | 0 | if (except == NULL) { |
3624 | 0 | return (def); |
3625 | 0 | } |
3626 | 0 | except->type = XML_RELAXNG_EXCEPT; |
3627 | 0 | child = content->children; |
3628 | 0 | def->content = except; |
3629 | 0 | if (child == NULL) { |
3630 | 0 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
3631 | 0 | "except has no content\n", NULL, NULL); |
3632 | 0 | } |
3633 | 0 | while (child != NULL) { |
3634 | 0 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
3635 | 0 | if (tmp2 != NULL) { |
3636 | 0 | if (last == NULL) { |
3637 | 0 | except->content = last = tmp2; |
3638 | 0 | } else { |
3639 | 0 | last->next = tmp2; |
3640 | 0 | last = tmp2; |
3641 | 0 | } |
3642 | 0 | } |
3643 | 0 | child = child->next; |
3644 | 0 | } |
3645 | 0 | content = content->next; |
3646 | 0 | } |
3647 | | /* |
3648 | | * Check there is no unhandled data |
3649 | | */ |
3650 | 0 | if (content != NULL) { |
3651 | 0 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
3652 | 0 | "Element data has unexpected content %s\n", |
3653 | 0 | content->name, NULL); |
3654 | 0 | } |
3655 | |
|
3656 | 0 | return (def); |
3657 | 0 | } |
3658 | | |
3659 | | static const xmlChar *invalidName = BAD_CAST "\1"; |
3660 | | |
3661 | | /** |
3662 | | * xmlRelaxNGCompareNameClasses: |
3663 | | * @defs1: the first element/attribute defs |
3664 | | * @defs2: the second element/attribute defs |
3665 | | * @name: the restriction on the name |
3666 | | * @ns: the restriction on the namespace |
3667 | | * |
3668 | | * Compare the 2 lists of element definitions. The comparison is |
3669 | | * that if both lists do not accept the same QNames, it returns 1 |
3670 | | * If the 2 lists can accept the same QName the comparison returns 0 |
3671 | | * |
3672 | | * Returns 1 distinct, 0 if equal |
3673 | | */ |
3674 | | static int |
3675 | | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
3676 | | xmlRelaxNGDefinePtr def2) |
3677 | 0 | { |
3678 | 0 | int ret = 1; |
3679 | 0 | xmlNode node; |
3680 | 0 | xmlNs ns; |
3681 | 0 | xmlRelaxNGValidCtxt ctxt; |
3682 | |
|
3683 | 0 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
3684 | |
|
3685 | 0 | ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR; |
3686 | |
|
3687 | 0 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
3688 | 0 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
3689 | 0 | if (def2->type == XML_RELAXNG_TEXT) |
3690 | 0 | return (1); |
3691 | 0 | if (def1->name != NULL) { |
3692 | 0 | node.name = def1->name; |
3693 | 0 | } else { |
3694 | 0 | node.name = invalidName; |
3695 | 0 | } |
3696 | 0 | if (def1->ns != NULL) { |
3697 | 0 | if (def1->ns[0] == 0) { |
3698 | 0 | node.ns = NULL; |
3699 | 0 | } else { |
3700 | 0 | node.ns = &ns; |
3701 | 0 | ns.href = def1->ns; |
3702 | 0 | } |
3703 | 0 | } else { |
3704 | 0 | node.ns = NULL; |
3705 | 0 | } |
3706 | 0 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
3707 | 0 | if (def1->nameClass != NULL) { |
3708 | 0 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
3709 | 0 | } else { |
3710 | 0 | ret = 0; |
3711 | 0 | } |
3712 | 0 | } else { |
3713 | 0 | ret = 1; |
3714 | 0 | } |
3715 | 0 | } else if (def1->type == XML_RELAXNG_TEXT) { |
3716 | 0 | if (def2->type == XML_RELAXNG_TEXT) |
3717 | 0 | return (0); |
3718 | 0 | return (1); |
3719 | 0 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
3720 | 0 | ret = xmlRelaxNGCompareNameClasses(def1->content, def2); |
3721 | 0 | if (ret == 0) |
3722 | 0 | ret = 1; |
3723 | 0 | else if (ret == 1) |
3724 | 0 | ret = 0; |
3725 | 0 | } else { |
3726 | 0 | TODO ret = 0; |
3727 | 0 | } |
3728 | 0 | if (ret == 0) |
3729 | 0 | return (ret); |
3730 | 0 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
3731 | 0 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
3732 | 0 | if (def2->name != NULL) { |
3733 | 0 | node.name = def2->name; |
3734 | 0 | } else { |
3735 | 0 | node.name = invalidName; |
3736 | 0 | } |
3737 | 0 | node.ns = &ns; |
3738 | 0 | if (def2->ns != NULL) { |
3739 | 0 | if (def2->ns[0] == 0) { |
3740 | 0 | node.ns = NULL; |
3741 | 0 | } else { |
3742 | 0 | ns.href = def2->ns; |
3743 | 0 | } |
3744 | 0 | } else { |
3745 | 0 | ns.href = invalidName; |
3746 | 0 | } |
3747 | 0 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
3748 | 0 | if (def2->nameClass != NULL) { |
3749 | 0 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
3750 | 0 | } else { |
3751 | 0 | ret = 0; |
3752 | 0 | } |
3753 | 0 | } else { |
3754 | 0 | ret = 1; |
3755 | 0 | } |
3756 | 0 | } else { |
3757 | 0 | TODO ret = 0; |
3758 | 0 | } |
3759 | |
|
3760 | 0 | return (ret); |
3761 | 0 | } |
3762 | | |
3763 | | /** |
3764 | | * xmlRelaxNGCompareElemDefLists: |
3765 | | * @ctxt: a Relax-NG parser context |
3766 | | * @defs1: the first list of element/attribute defs |
3767 | | * @defs2: the second list of element/attribute defs |
3768 | | * |
3769 | | * Compare the 2 lists of element or attribute definitions. The comparison |
3770 | | * is that if both lists do not accept the same QNames, it returns 1 |
3771 | | * If the 2 lists can accept the same QName the comparison returns 0 |
3772 | | * |
3773 | | * Returns 1 distinct, 0 if equal |
3774 | | */ |
3775 | | static int |
3776 | | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
3777 | | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
3778 | | xmlRelaxNGDefinePtr * def2) |
3779 | 0 | { |
3780 | 0 | xmlRelaxNGDefinePtr *basedef2 = def2; |
3781 | |
|
3782 | 0 | if ((def1 == NULL) || (def2 == NULL)) |
3783 | 0 | return (1); |
3784 | 0 | if ((*def1 == NULL) || (*def2 == NULL)) |
3785 | 0 | return (1); |
3786 | 0 | while (*def1 != NULL) { |
3787 | 0 | while ((*def2) != NULL) { |
3788 | 0 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
3789 | 0 | return (0); |
3790 | 0 | def2++; |
3791 | 0 | } |
3792 | 0 | def2 = basedef2; |
3793 | 0 | def1++; |
3794 | 0 | } |
3795 | 0 | return (1); |
3796 | 0 | } |
3797 | | |
3798 | | /** |
3799 | | * xmlRelaxNGGenerateAttributes: |
3800 | | * @ctxt: a Relax-NG parser context |
3801 | | * @def: the definition definition |
3802 | | * |
3803 | | * Check if the definition can only generate attributes |
3804 | | * |
3805 | | * Returns 1 if yes, 0 if no and -1 in case of error. |
3806 | | */ |
3807 | | static int |
3808 | | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
3809 | | xmlRelaxNGDefinePtr def) |
3810 | 0 | { |
3811 | 0 | xmlRelaxNGDefinePtr parent, cur, tmp; |
3812 | | |
3813 | | /* |
3814 | | * Don't run that check in case of error. Infinite recursion |
3815 | | * becomes possible. |
3816 | | */ |
3817 | 0 | if (ctxt->nbErrors != 0) |
3818 | 0 | return (-1); |
3819 | | |
3820 | 0 | parent = NULL; |
3821 | 0 | cur = def; |
3822 | 0 | while (cur != NULL) { |
3823 | 0 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
3824 | 0 | (cur->type == XML_RELAXNG_TEXT) || |
3825 | 0 | (cur->type == XML_RELAXNG_DATATYPE) || |
3826 | 0 | (cur->type == XML_RELAXNG_PARAM) || |
3827 | 0 | (cur->type == XML_RELAXNG_LIST) || |
3828 | 0 | (cur->type == XML_RELAXNG_VALUE) || |
3829 | 0 | (cur->type == XML_RELAXNG_EMPTY)) |
3830 | 0 | return (0); |
3831 | 0 | if ((cur->type == XML_RELAXNG_CHOICE) || |
3832 | 0 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
3833 | 0 | (cur->type == XML_RELAXNG_GROUP) || |
3834 | 0 | (cur->type == XML_RELAXNG_ONEORMORE) || |
3835 | 0 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
3836 | 0 | (cur->type == XML_RELAXNG_OPTIONAL) || |
3837 | 0 | (cur->type == XML_RELAXNG_PARENTREF) || |
3838 | 0 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
3839 | 0 | (cur->type == XML_RELAXNG_REF) || |
3840 | 0 | (cur->type == XML_RELAXNG_DEF)) { |
3841 | 0 | if (cur->content != NULL) { |
3842 | 0 | parent = cur; |
3843 | 0 | cur = cur->content; |
3844 | 0 | tmp = cur; |
3845 | 0 | while (tmp != NULL) { |
3846 | 0 | tmp->parent = parent; |
3847 | 0 | tmp = tmp->next; |
3848 | 0 | } |
3849 | 0 | continue; |
3850 | 0 | } |
3851 | 0 | } |
3852 | 0 | if (cur == def) |
3853 | 0 | break; |
3854 | 0 | if (cur->next != NULL) { |
3855 | 0 | cur = cur->next; |
3856 | 0 | continue; |
3857 | 0 | } |
3858 | 0 | do { |
3859 | 0 | cur = cur->parent; |
3860 | 0 | if (cur == NULL) |
3861 | 0 | break; |
3862 | 0 | if (cur == def) |
3863 | 0 | return (1); |
3864 | 0 | if (cur->next != NULL) { |
3865 | 0 | cur = cur->next; |
3866 | 0 | break; |
3867 | 0 | } |
3868 | 0 | } while (cur != NULL); |
3869 | 0 | } |
3870 | 0 | return (1); |
3871 | 0 | } |
3872 | | |
3873 | | /** |
3874 | | * xmlRelaxNGGetElements: |
3875 | | * @ctxt: a Relax-NG parser context |
3876 | | * @def: the definition definition |
3877 | | * @eora: gather elements (0), attributes (1) or elements and text (2) |
3878 | | * |
3879 | | * Compute the list of top elements a definition can generate |
3880 | | * |
3881 | | * Returns a list of elements or NULL if none was found. |
3882 | | */ |
3883 | | static xmlRelaxNGDefinePtr * |
3884 | | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
3885 | | xmlRelaxNGDefinePtr def, int eora) |
3886 | 0 | { |
3887 | 0 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
3888 | 0 | int len = 0; |
3889 | 0 | int max = 0; |
3890 | | |
3891 | | /* |
3892 | | * Don't run that check in case of error. Infinite recursion |
3893 | | * becomes possible. |
3894 | | */ |
3895 | 0 | if (ctxt->nbErrors != 0) |
3896 | 0 | return (NULL); |
3897 | | |
3898 | 0 | parent = NULL; |
3899 | 0 | cur = def; |
3900 | 0 | while (cur != NULL) { |
3901 | 0 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
3902 | 0 | (cur->type == XML_RELAXNG_TEXT))) || |
3903 | 0 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE)) || |
3904 | 0 | ((eora == 2) && ((cur->type == XML_RELAXNG_DATATYPE) || |
3905 | 0 | (cur->type == XML_RELAXNG_ELEMENT) || |
3906 | 0 | (cur->type == XML_RELAXNG_LIST) || |
3907 | 0 | (cur->type == XML_RELAXNG_TEXT) || |
3908 | 0 | (cur->type == XML_RELAXNG_VALUE)))) { |
3909 | 0 | if (ret == NULL) { |
3910 | 0 | max = 10; |
3911 | 0 | ret = (xmlRelaxNGDefinePtr *) |
3912 | 0 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
3913 | 0 | if (ret == NULL) { |
3914 | 0 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
3915 | 0 | return (NULL); |
3916 | 0 | } |
3917 | 0 | } else if (max <= len) { |
3918 | 0 | xmlRelaxNGDefinePtr *temp; |
3919 | |
|
3920 | 0 | max *= 2; |
3921 | 0 | temp = xmlRealloc(ret, |
3922 | 0 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
3923 | 0 | if (temp == NULL) { |
3924 | 0 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
3925 | 0 | xmlFree(ret); |
3926 | 0 | return (NULL); |
3927 | 0 | } |
3928 | 0 | ret = temp; |
3929 | 0 | } |
3930 | 0 | ret[len++] = cur; |
3931 | 0 | ret[len] = NULL; |
3932 | 0 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
3933 | 0 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
3934 | 0 | (cur->type == XML_RELAXNG_GROUP) || |
3935 | 0 | (cur->type == XML_RELAXNG_ONEORMORE) || |
3936 | 0 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
3937 | 0 | (cur->type == XML_RELAXNG_OPTIONAL) || |
3938 | 0 | (cur->type == XML_RELAXNG_PARENTREF) || |
3939 | 0 | (cur->type == XML_RELAXNG_REF) || |
3940 | 0 | (cur->type == XML_RELAXNG_DEF) || |
3941 | 0 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
3942 | | /* |
3943 | | * Don't go within elements or attributes or string values. |
3944 | | * Just gather the element top list |
3945 | | */ |
3946 | 0 | if (cur->content != NULL) { |
3947 | 0 | parent = cur; |
3948 | 0 | cur = cur->content; |
3949 | 0 | tmp = cur; |
3950 | 0 | while (tmp != NULL) { |
3951 | 0 | tmp->parent = parent; |
3952 | 0 | tmp = tmp->next; |
3953 | 0 | } |
3954 | 0 | continue; |
3955 | 0 | } |
3956 | 0 | } |
3957 | 0 | if (cur == def) |
3958 | 0 | break; |
3959 | 0 | if (cur->next != NULL) { |
3960 | 0 | cur = cur->next; |
3961 | 0 | continue; |
3962 | 0 | } |
3963 | 0 | do { |
3964 | 0 | cur = cur->parent; |
3965 | 0 | if (cur == NULL) |
3966 | 0 | break; |
3967 | 0 | if (cur == def) |
3968 | 0 | return (ret); |
3969 | 0 | if (cur->next != NULL) { |
3970 | 0 | cur = cur->next; |
3971 | 0 | break; |
3972 | 0 | } |
3973 | 0 | } while (cur != NULL); |
3974 | 0 | } |
3975 | 0 | return (ret); |
3976 | 0 | } |
3977 | | |
3978 | | /** |
3979 | | * xmlRelaxNGCheckChoiceDeterminism: |
3980 | | * @ctxt: a Relax-NG parser context |
3981 | | * @def: the choice definition |
3982 | | * |
3983 | | * Also used to find indeterministic pattern in choice |
3984 | | */ |
3985 | | static void |
3986 | | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
3987 | | xmlRelaxNGDefinePtr def) |
3988 | 0 | { |
3989 | 0 | xmlRelaxNGDefinePtr **list; |
3990 | 0 | xmlRelaxNGDefinePtr cur; |
3991 | 0 | int nbchild = 0, i, j, ret; |
3992 | 0 | int is_nullable = 0; |
3993 | 0 | int is_indeterminist = 0; |
3994 | 0 | xmlHashTablePtr triage = NULL; |
3995 | 0 | int is_triable = 1; |
3996 | |
|
3997 | 0 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
3998 | 0 | return; |
3999 | | |
4000 | 0 | if (def->dflags & IS_PROCESSED) |
4001 | 0 | return; |
4002 | | |
4003 | | /* |
4004 | | * Don't run that check in case of error. Infinite recursion |
4005 | | * becomes possible. |
4006 | | */ |
4007 | 0 | if (ctxt->nbErrors != 0) |
4008 | 0 | return; |
4009 | | |
4010 | 0 | is_nullable = xmlRelaxNGIsNullable(def); |
4011 | |
|
4012 | 0 | cur = def->content; |
4013 | 0 | while (cur != NULL) { |
4014 | 0 | nbchild++; |
4015 | 0 | cur = cur->next; |
4016 | 0 | } |
4017 | |
|
4018 | 0 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
4019 | 0 | sizeof(xmlRelaxNGDefinePtr |
4020 | 0 | *)); |
4021 | 0 | if (list == NULL) { |
4022 | 0 | xmlRngPErrMemory(ctxt, "building choice\n"); |
4023 | 0 | return; |
4024 | 0 | } |
4025 | 0 | i = 0; |
4026 | | /* |
4027 | | * a bit strong but safe |
4028 | | */ |
4029 | 0 | if (is_nullable == 0) { |
4030 | 0 | triage = xmlHashCreate(10); |
4031 | 0 | } else { |
4032 | 0 | is_triable = 0; |
4033 | 0 | } |
4034 | 0 | cur = def->content; |
4035 | 0 | while (cur != NULL) { |
4036 | 0 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
4037 | 0 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
4038 | 0 | is_triable = 0; |
4039 | 0 | } else if (is_triable == 1) { |
4040 | 0 | xmlRelaxNGDefinePtr *tmp; |
4041 | 0 | int res; |
4042 | |
|
4043 | 0 | tmp = list[i]; |
4044 | 0 | while ((*tmp != NULL) && (is_triable == 1)) { |
4045 | 0 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
4046 | 0 | res = xmlHashAddEntry2(triage, |
4047 | 0 | BAD_CAST "#text", NULL, |
4048 | 0 | (void *) cur); |
4049 | 0 | if (res != 0) |
4050 | 0 | is_triable = -1; |
4051 | 0 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
4052 | 0 | ((*tmp)->name != NULL)) { |
4053 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4054 | 0 | res = xmlHashAddEntry2(triage, |
4055 | 0 | (*tmp)->name, NULL, |
4056 | 0 | (void *) cur); |
4057 | 0 | else |
4058 | 0 | res = xmlHashAddEntry2(triage, |
4059 | 0 | (*tmp)->name, (*tmp)->ns, |
4060 | 0 | (void *) cur); |
4061 | 0 | if (res != 0) |
4062 | 0 | is_triable = -1; |
4063 | 0 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
4064 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4065 | 0 | res = xmlHashAddEntry2(triage, |
4066 | 0 | BAD_CAST "#any", NULL, |
4067 | 0 | (void *) cur); |
4068 | 0 | else |
4069 | 0 | res = xmlHashAddEntry2(triage, |
4070 | 0 | BAD_CAST "#any", (*tmp)->ns, |
4071 | 0 | (void *) cur); |
4072 | 0 | if (res != 0) |
4073 | 0 | is_triable = -1; |
4074 | 0 | } else { |
4075 | 0 | is_triable = -1; |
4076 | 0 | } |
4077 | 0 | tmp++; |
4078 | 0 | } |
4079 | 0 | } |
4080 | 0 | i++; |
4081 | 0 | cur = cur->next; |
4082 | 0 | } |
4083 | |
|
4084 | 0 | for (i = 0; i < nbchild; i++) { |
4085 | 0 | if (list[i] == NULL) |
4086 | 0 | continue; |
4087 | 0 | for (j = 0; j < i; j++) { |
4088 | 0 | if (list[j] == NULL) |
4089 | 0 | continue; |
4090 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
4091 | 0 | if (ret == 0) { |
4092 | 0 | is_indeterminist = 1; |
4093 | 0 | } |
4094 | 0 | } |
4095 | 0 | } |
4096 | 0 | for (i = 0; i < nbchild; i++) { |
4097 | 0 | if (list[i] != NULL) |
4098 | 0 | xmlFree(list[i]); |
4099 | 0 | } |
4100 | |
|
4101 | 0 | xmlFree(list); |
4102 | 0 | if (is_indeterminist) { |
4103 | 0 | def->dflags |= IS_INDETERMINIST; |
4104 | 0 | } |
4105 | 0 | if (is_triable == 1) { |
4106 | 0 | def->dflags |= IS_TRIABLE; |
4107 | 0 | def->data = triage; |
4108 | 0 | } else if (triage != NULL) { |
4109 | 0 | xmlHashFree(triage, NULL); |
4110 | 0 | } |
4111 | 0 | def->dflags |= IS_PROCESSED; |
4112 | 0 | } |
4113 | | |
4114 | | /** |
4115 | | * xmlRelaxNGCheckGroupAttrs: |
4116 | | * @ctxt: a Relax-NG parser context |
4117 | | * @def: the group definition |
4118 | | * |
4119 | | * Detects violations of rule 7.3 |
4120 | | */ |
4121 | | static void |
4122 | | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
4123 | | xmlRelaxNGDefinePtr def) |
4124 | 0 | { |
4125 | 0 | xmlRelaxNGDefinePtr **list; |
4126 | 0 | xmlRelaxNGDefinePtr cur; |
4127 | 0 | int nbchild = 0, i, j, ret; |
4128 | |
|
4129 | 0 | if ((def == NULL) || |
4130 | 0 | ((def->type != XML_RELAXNG_GROUP) && |
4131 | 0 | (def->type != XML_RELAXNG_ELEMENT))) |
4132 | 0 | return; |
4133 | | |
4134 | 0 | if (def->dflags & IS_PROCESSED) |
4135 | 0 | return; |
4136 | | |
4137 | | /* |
4138 | | * Don't run that check in case of error. Infinite recursion |
4139 | | * becomes possible. |
4140 | | */ |
4141 | 0 | if (ctxt->nbErrors != 0) |
4142 | 0 | return; |
4143 | | |
4144 | 0 | cur = def->attrs; |
4145 | 0 | while (cur != NULL) { |
4146 | 0 | nbchild++; |
4147 | 0 | cur = cur->next; |
4148 | 0 | } |
4149 | 0 | cur = def->content; |
4150 | 0 | while (cur != NULL) { |
4151 | 0 | nbchild++; |
4152 | 0 | cur = cur->next; |
4153 | 0 | } |
4154 | |
|
4155 | 0 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
4156 | 0 | sizeof(xmlRelaxNGDefinePtr |
4157 | 0 | *)); |
4158 | 0 | if (list == NULL) { |
4159 | 0 | xmlRngPErrMemory(ctxt, "building group\n"); |
4160 | 0 | return; |
4161 | 0 | } |
4162 | 0 | i = 0; |
4163 | 0 | cur = def->attrs; |
4164 | 0 | while (cur != NULL) { |
4165 | 0 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
4166 | 0 | i++; |
4167 | 0 | cur = cur->next; |
4168 | 0 | } |
4169 | 0 | cur = def->content; |
4170 | 0 | while (cur != NULL) { |
4171 | 0 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
4172 | 0 | i++; |
4173 | 0 | cur = cur->next; |
4174 | 0 | } |
4175 | |
|
4176 | 0 | for (i = 0; i < nbchild; i++) { |
4177 | 0 | if (list[i] == NULL) |
4178 | 0 | continue; |
4179 | 0 | for (j = 0; j < i; j++) { |
4180 | 0 | if (list[j] == NULL) |
4181 | 0 | continue; |
4182 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
4183 | 0 | if (ret == 0) { |
4184 | 0 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
4185 | 0 | "Attributes conflicts in group\n", NULL, NULL); |
4186 | 0 | } |
4187 | 0 | } |
4188 | 0 | } |
4189 | 0 | for (i = 0; i < nbchild; i++) { |
4190 | 0 | if (list[i] != NULL) |
4191 | 0 | xmlFree(list[i]); |
4192 | 0 | } |
4193 | |
|
4194 | 0 | xmlFree(list); |
4195 | 0 | def->dflags |= IS_PROCESSED; |
4196 | 0 | } |
4197 | | |
4198 | | /** |
4199 | | * xmlRelaxNGComputeInterleaves: |
4200 | | * @def: the interleave definition |
4201 | | * @ctxt: a Relax-NG parser context |
4202 | | * @name: the definition name |
4203 | | * |
4204 | | * A lot of work for preprocessing interleave definitions |
4205 | | * is potentially needed to get a decent execution speed at runtime |
4206 | | * - trying to get a total order on the element nodes generated |
4207 | | * by the interleaves, order the list of interleave definitions |
4208 | | * following that order. |
4209 | | * - if <text/> is used to handle mixed content, it is better to |
4210 | | * flag this in the define and simplify the runtime checking |
4211 | | * algorithm |
4212 | | */ |
4213 | | static void |
4214 | | xmlRelaxNGComputeInterleaves(void *payload, void *data, |
4215 | | const xmlChar * name ATTRIBUTE_UNUSED) |
4216 | 0 | { |
4217 | 0 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
4218 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
4219 | 0 | xmlRelaxNGDefinePtr cur, *tmp; |
4220 | |
|
4221 | 0 | xmlRelaxNGPartitionPtr partitions = NULL; |
4222 | 0 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
4223 | 0 | xmlRelaxNGInterleaveGroupPtr group; |
4224 | 0 | int i, j, ret, res; |
4225 | 0 | int nbgroups = 0; |
4226 | 0 | int nbchild = 0; |
4227 | 0 | int is_mixed = 0; |
4228 | 0 | int is_determinist = 1; |
4229 | | |
4230 | | /* |
4231 | | * Don't run that check in case of error. Infinite recursion |
4232 | | * becomes possible. |
4233 | | */ |
4234 | 0 | if (ctxt->nbErrors != 0) |
4235 | 0 | return; |
4236 | | |
4237 | 0 | cur = def->content; |
4238 | 0 | while (cur != NULL) { |
4239 | 0 | nbchild++; |
4240 | 0 | cur = cur->next; |
4241 | 0 | } |
4242 | |
|
4243 | 0 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
4244 | 0 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
4245 | 0 | if (groups == NULL) |
4246 | 0 | goto error; |
4247 | 0 | cur = def->content; |
4248 | 0 | while (cur != NULL) { |
4249 | 0 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
4250 | 0 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
4251 | 0 | if (groups[nbgroups] == NULL) |
4252 | 0 | goto error; |
4253 | 0 | if (cur->type == XML_RELAXNG_TEXT) |
4254 | 0 | is_mixed++; |
4255 | 0 | groups[nbgroups]->rule = cur; |
4256 | 0 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 2); |
4257 | 0 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
4258 | 0 | nbgroups++; |
4259 | 0 | cur = cur->next; |
4260 | 0 | } |
4261 | | |
4262 | | /* |
4263 | | * Let's check that all rules makes a partitions according to 7.4 |
4264 | | */ |
4265 | 0 | partitions = (xmlRelaxNGPartitionPtr) |
4266 | 0 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
4267 | 0 | if (partitions == NULL) |
4268 | 0 | goto error; |
4269 | 0 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
4270 | 0 | partitions->nbgroups = nbgroups; |
4271 | 0 | partitions->triage = xmlHashCreate(nbgroups); |
4272 | 0 | for (i = 0; i < nbgroups; i++) { |
4273 | 0 | group = groups[i]; |
4274 | 0 | for (j = i + 1; j < nbgroups; j++) { |
4275 | 0 | if (groups[j] == NULL) |
4276 | 0 | continue; |
4277 | | |
4278 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
4279 | 0 | groups[j]->defs); |
4280 | 0 | if (ret == 0) { |
4281 | 0 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
4282 | 0 | "Element or text conflicts in interleave\n", |
4283 | 0 | NULL, NULL); |
4284 | 0 | } |
4285 | 0 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
4286 | 0 | groups[j]->attrs); |
4287 | 0 | if (ret == 0) { |
4288 | 0 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
4289 | 0 | "Attributes conflicts in interleave\n", NULL, |
4290 | 0 | NULL); |
4291 | 0 | } |
4292 | 0 | } |
4293 | 0 | tmp = group->defs; |
4294 | 0 | if ((tmp != NULL) && (*tmp != NULL)) { |
4295 | 0 | while (*tmp != NULL) { |
4296 | 0 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
4297 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4298 | 0 | BAD_CAST "#text", NULL, |
4299 | 0 | (void *) (ptrdiff_t) (i + 1)); |
4300 | 0 | if (res != 0) |
4301 | 0 | is_determinist = -1; |
4302 | 0 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
4303 | 0 | ((*tmp)->name != NULL)) { |
4304 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4305 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4306 | 0 | (*tmp)->name, NULL, |
4307 | 0 | (void *) (ptrdiff_t) (i + 1)); |
4308 | 0 | else |
4309 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4310 | 0 | (*tmp)->name, (*tmp)->ns, |
4311 | 0 | (void *) (ptrdiff_t) (i + 1)); |
4312 | 0 | if (res != 0) |
4313 | 0 | is_determinist = -1; |
4314 | 0 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
4315 | 0 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
4316 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4317 | 0 | BAD_CAST "#any", NULL, |
4318 | 0 | (void *) (ptrdiff_t) (i + 1)); |
4319 | 0 | else |
4320 | 0 | res = xmlHashAddEntry2(partitions->triage, |
4321 | 0 | BAD_CAST "#any", (*tmp)->ns, |
4322 | 0 | (void *) (ptrdiff_t) (i + 1)); |
4323 | 0 | if ((*tmp)->nameClass != NULL) |
4324 | 0 | is_determinist = 2; |
4325 | 0 | if (res != 0) |
4326 | 0 | is_determinist = -1; |
4327 | 0 | } else { |
4328 | 0 | is_determinist = -1; |
4329 | 0 | } |
4330 | 0 | tmp++; |
4331 | 0 | } |
4332 | 0 | } else { |
4333 | 0 | is_determinist = 0; |
4334 | 0 | } |
4335 | 0 | } |
4336 | 0 | partitions->groups = groups; |
4337 | | |
4338 | | /* |
4339 | | * and save the partition list back in the def |
4340 | | */ |
4341 | 0 | def->data = partitions; |
4342 | 0 | if (is_mixed != 0) |
4343 | 0 | def->dflags |= IS_MIXED; |
4344 | 0 | if (is_determinist == 1) |
4345 | 0 | partitions->flags = IS_DETERMINIST; |
4346 | 0 | if (is_determinist == 2) |
4347 | 0 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
4348 | 0 | return; |
4349 | | |
4350 | 0 | error: |
4351 | 0 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
4352 | 0 | if (groups != NULL) { |
4353 | 0 | for (i = 0; i < nbgroups; i++) |
4354 | 0 | if (groups[i] != NULL) { |
4355 | 0 | if (groups[i]->defs != NULL) |
4356 | 0 | xmlFree(groups[i]->defs); |
4357 | 0 | xmlFree(groups[i]); |
4358 | 0 | } |
4359 | 0 | xmlFree(groups); |
4360 | 0 | } |
4361 | 0 | xmlRelaxNGFreePartition(partitions); |
4362 | 0 | } |
4363 | | |
4364 | | /** |
4365 | | * xmlRelaxNGParseInterleave: |
4366 | | * @ctxt: a Relax-NG parser context |
4367 | | * @node: the data node. |
4368 | | * |
4369 | | * parse the content of a RelaxNG interleave node. |
4370 | | * |
4371 | | * Returns the definition pointer or NULL in case of error |
4372 | | */ |
4373 | | static xmlRelaxNGDefinePtr |
4374 | | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4375 | 0 | { |
4376 | 0 | xmlRelaxNGDefinePtr def = NULL; |
4377 | 0 | xmlRelaxNGDefinePtr last = NULL, cur; |
4378 | 0 | xmlNodePtr child; |
4379 | |
|
4380 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4381 | 0 | if (def == NULL) { |
4382 | 0 | return (NULL); |
4383 | 0 | } |
4384 | 0 | def->type = XML_RELAXNG_INTERLEAVE; |
4385 | |
|
4386 | 0 | if (ctxt->interleaves == NULL) |
4387 | 0 | ctxt->interleaves = xmlHashCreate(10); |
4388 | 0 | if (ctxt->interleaves == NULL) { |
4389 | 0 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
4390 | 0 | } else { |
4391 | 0 | char name[32]; |
4392 | |
|
4393 | 0 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
4394 | 0 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
4395 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
4396 | 0 | "Failed to add %s to hash table\n", |
4397 | 0 | (const xmlChar *) name, NULL); |
4398 | 0 | } |
4399 | 0 | } |
4400 | 0 | child = node->children; |
4401 | 0 | if (child == NULL) { |
4402 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
4403 | 0 | "Element interleave is empty\n", NULL, NULL); |
4404 | 0 | } |
4405 | 0 | while (child != NULL) { |
4406 | 0 | if (IS_RELAXNG(child, "element")) { |
4407 | 0 | cur = xmlRelaxNGParseElement(ctxt, child); |
4408 | 0 | } else { |
4409 | 0 | cur = xmlRelaxNGParsePattern(ctxt, child); |
4410 | 0 | } |
4411 | 0 | if (cur != NULL) { |
4412 | 0 | cur->parent = def; |
4413 | 0 | if (last == NULL) { |
4414 | 0 | def->content = last = cur; |
4415 | 0 | } else { |
4416 | 0 | last->next = cur; |
4417 | 0 | last = cur; |
4418 | 0 | } |
4419 | 0 | } |
4420 | 0 | child = child->next; |
4421 | 0 | } |
4422 | |
|
4423 | 0 | return (def); |
4424 | 0 | } |
4425 | | |
4426 | | /** |
4427 | | * xmlRelaxNGParseInclude: |
4428 | | * @ctxt: a Relax-NG parser context |
4429 | | * @node: the include node |
4430 | | * |
4431 | | * Integrate the content of an include node in the current grammar |
4432 | | * |
4433 | | * Returns 0 in case of success or -1 in case of error |
4434 | | */ |
4435 | | static int |
4436 | | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4437 | 0 | { |
4438 | 0 | xmlRelaxNGIncludePtr incl; |
4439 | 0 | xmlNodePtr root; |
4440 | 0 | int ret = 0, tmp; |
4441 | |
|
4442 | 0 | incl = node->psvi; |
4443 | 0 | if (incl == NULL) { |
4444 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
4445 | 0 | "Include node has no data\n", NULL, NULL); |
4446 | 0 | return (-1); |
4447 | 0 | } |
4448 | 0 | root = xmlDocGetRootElement(incl->doc); |
4449 | 0 | if (root == NULL) { |
4450 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
4451 | 0 | NULL, NULL); |
4452 | 0 | return (-1); |
4453 | 0 | } |
4454 | 0 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
4455 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
4456 | 0 | "Include document root is not a grammar\n", NULL, NULL); |
4457 | 0 | return (-1); |
4458 | 0 | } |
4459 | | |
4460 | | /* |
4461 | | * Merge the definition from both the include and the internal list |
4462 | | */ |
4463 | 0 | if (root->children != NULL) { |
4464 | 0 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
4465 | 0 | if (tmp != 0) |
4466 | 0 | ret = -1; |
4467 | 0 | } |
4468 | 0 | if (node->children != NULL) { |
4469 | 0 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
4470 | 0 | if (tmp != 0) |
4471 | 0 | ret = -1; |
4472 | 0 | } |
4473 | 0 | return (ret); |
4474 | 0 | } |
4475 | | |
4476 | | /** |
4477 | | * xmlRelaxNGParseDefine: |
4478 | | * @ctxt: a Relax-NG parser context |
4479 | | * @node: the define node |
4480 | | * |
4481 | | * parse the content of a RelaxNG define element node. |
4482 | | * |
4483 | | * Returns 0 in case of success or -1 in case of error |
4484 | | */ |
4485 | | static int |
4486 | | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4487 | 0 | { |
4488 | 0 | xmlChar *name; |
4489 | 0 | int ret = 0, tmp; |
4490 | 0 | xmlRelaxNGDefinePtr def; |
4491 | 0 | const xmlChar *olddefine; |
4492 | |
|
4493 | 0 | name = xmlGetProp(node, BAD_CAST "name"); |
4494 | 0 | if (name == NULL) { |
4495 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
4496 | 0 | "define has no name\n", NULL, NULL); |
4497 | 0 | } else { |
4498 | 0 | xmlRelaxNGNormExtSpace(name); |
4499 | 0 | if (xmlValidateNCName(name, 0)) { |
4500 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
4501 | 0 | "define name '%s' is not an NCName\n", name, NULL); |
4502 | 0 | } |
4503 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4504 | 0 | if (def == NULL) { |
4505 | 0 | xmlFree(name); |
4506 | 0 | return (-1); |
4507 | 0 | } |
4508 | 0 | def->type = XML_RELAXNG_DEF; |
4509 | 0 | def->name = name; |
4510 | 0 | if (node->children == NULL) { |
4511 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
4512 | 0 | "define has no children\n", NULL, NULL); |
4513 | 0 | } else { |
4514 | 0 | olddefine = ctxt->define; |
4515 | 0 | ctxt->define = name; |
4516 | 0 | def->content = |
4517 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4518 | 0 | ctxt->define = olddefine; |
4519 | 0 | } |
4520 | 0 | if (ctxt->grammar->defs == NULL) |
4521 | 0 | ctxt->grammar->defs = xmlHashCreate(10); |
4522 | 0 | if (ctxt->grammar->defs == NULL) { |
4523 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
4524 | 0 | "Could not create definition hash\n", NULL, NULL); |
4525 | 0 | ret = -1; |
4526 | 0 | } else { |
4527 | 0 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
4528 | 0 | if (tmp < 0) { |
4529 | 0 | xmlRelaxNGDefinePtr prev; |
4530 | |
|
4531 | 0 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
4532 | 0 | if (prev == NULL) { |
4533 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
4534 | 0 | "Internal error on define aggregation of %s\n", |
4535 | 0 | name, NULL); |
4536 | 0 | ret = -1; |
4537 | 0 | } else { |
4538 | 0 | while (prev->nextHash != NULL) |
4539 | 0 | prev = prev->nextHash; |
4540 | 0 | prev->nextHash = def; |
4541 | 0 | } |
4542 | 0 | } |
4543 | 0 | } |
4544 | 0 | } |
4545 | 0 | return (ret); |
4546 | 0 | } |
4547 | | |
4548 | | /** |
4549 | | * xmlRelaxNGParseImportRef: |
4550 | | * @payload: the parser context |
4551 | | * @data: the current grammar |
4552 | | * @name: the reference name |
4553 | | * |
4554 | | * Import import one references into the current grammar |
4555 | | */ |
4556 | | static void |
4557 | 0 | xmlRelaxNGParseImportRef(void *payload, void *data, const xmlChar *name) { |
4558 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
4559 | 0 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
4560 | 0 | int tmp; |
4561 | |
|
4562 | 0 | def->dflags |= IS_EXTERNAL_REF; |
4563 | |
|
4564 | 0 | tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def); |
4565 | 0 | if (tmp < 0) { |
4566 | 0 | xmlRelaxNGDefinePtr prev; |
4567 | |
|
4568 | 0 | prev = (xmlRelaxNGDefinePtr) |
4569 | 0 | xmlHashLookup(ctxt->grammar->refs, def->name); |
4570 | 0 | if (prev == NULL) { |
4571 | 0 | if (def->name != NULL) { |
4572 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
4573 | 0 | "Error refs definitions '%s'\n", |
4574 | 0 | def->name, NULL); |
4575 | 0 | } else { |
4576 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
4577 | 0 | "Error refs definitions\n", |
4578 | 0 | NULL, NULL); |
4579 | 0 | } |
4580 | 0 | } else { |
4581 | 0 | def->nextHash = prev->nextHash; |
4582 | 0 | prev->nextHash = def; |
4583 | 0 | } |
4584 | 0 | } |
4585 | 0 | } |
4586 | | |
4587 | | /** |
4588 | | * xmlRelaxNGParseImportRefs: |
4589 | | * @ctxt: the parser context |
4590 | | * @grammar: the sub grammar |
4591 | | * |
4592 | | * Import references from the subgrammar into the current grammar |
4593 | | * |
4594 | | * Returns 0 in case of success, -1 in case of failure |
4595 | | */ |
4596 | | static int |
4597 | | xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt, |
4598 | 0 | xmlRelaxNGGrammarPtr grammar) { |
4599 | 0 | if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL)) |
4600 | 0 | return(-1); |
4601 | 0 | if (grammar->refs == NULL) |
4602 | 0 | return(0); |
4603 | 0 | if (ctxt->grammar->refs == NULL) |
4604 | 0 | ctxt->grammar->refs = xmlHashCreate(10); |
4605 | 0 | if (ctxt->grammar->refs == NULL) { |
4606 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
4607 | 0 | "Could not create references hash\n", NULL, NULL); |
4608 | 0 | return(-1); |
4609 | 0 | } |
4610 | 0 | xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt); |
4611 | 0 | return(0); |
4612 | 0 | } |
4613 | | |
4614 | | /** |
4615 | | * xmlRelaxNGProcessExternalRef: |
4616 | | * @ctxt: the parser context |
4617 | | * @node: the externalRef node |
4618 | | * |
4619 | | * Process and compile an externalRef node |
4620 | | * |
4621 | | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
4622 | | */ |
4623 | | static xmlRelaxNGDefinePtr |
4624 | | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4625 | 0 | { |
4626 | 0 | xmlRelaxNGDocumentPtr docu; |
4627 | 0 | xmlNodePtr root, tmp; |
4628 | 0 | xmlChar *ns; |
4629 | 0 | int newNs = 0, oldflags; |
4630 | 0 | xmlRelaxNGDefinePtr def; |
4631 | |
|
4632 | 0 | docu = node->psvi; |
4633 | 0 | if (docu != NULL) { |
4634 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4635 | 0 | if (def == NULL) |
4636 | 0 | return (NULL); |
4637 | 0 | def->type = XML_RELAXNG_EXTERNALREF; |
4638 | |
|
4639 | 0 | if (docu->content == NULL) { |
4640 | | /* |
4641 | | * Then do the parsing for good |
4642 | | */ |
4643 | 0 | root = xmlDocGetRootElement(docu->doc); |
4644 | 0 | if (root == NULL) { |
4645 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
4646 | 0 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
4647 | 0 | NULL); |
4648 | 0 | return (NULL); |
4649 | 0 | } |
4650 | | /* |
4651 | | * ns transmission rules |
4652 | | */ |
4653 | 0 | ns = xmlGetProp(root, BAD_CAST "ns"); |
4654 | 0 | if (ns == NULL) { |
4655 | 0 | tmp = node; |
4656 | 0 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
4657 | 0 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
4658 | 0 | if (ns != NULL) { |
4659 | 0 | break; |
4660 | 0 | } |
4661 | 0 | tmp = tmp->parent; |
4662 | 0 | } |
4663 | 0 | if (ns != NULL) { |
4664 | 0 | xmlSetProp(root, BAD_CAST "ns", ns); |
4665 | 0 | newNs = 1; |
4666 | 0 | xmlFree(ns); |
4667 | 0 | } |
4668 | 0 | } else { |
4669 | 0 | xmlFree(ns); |
4670 | 0 | } |
4671 | | |
4672 | | /* |
4673 | | * Parsing to get a precompiled schemas. |
4674 | | */ |
4675 | 0 | oldflags = ctxt->flags; |
4676 | 0 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
4677 | 0 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
4678 | 0 | ctxt->flags = oldflags; |
4679 | 0 | if ((docu->schema != NULL) && |
4680 | 0 | (docu->schema->topgrammar != NULL)) { |
4681 | 0 | docu->content = docu->schema->topgrammar->start; |
4682 | 0 | if (docu->schema->topgrammar->refs) |
4683 | 0 | xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar); |
4684 | 0 | } |
4685 | | |
4686 | | /* |
4687 | | * the externalRef may be reused in a different ns context |
4688 | | */ |
4689 | 0 | if (newNs == 1) { |
4690 | 0 | xmlUnsetProp(root, BAD_CAST "ns"); |
4691 | 0 | } |
4692 | 0 | } |
4693 | 0 | def->content = docu->content; |
4694 | 0 | } else { |
4695 | 0 | def = NULL; |
4696 | 0 | } |
4697 | 0 | return (def); |
4698 | 0 | } |
4699 | | |
4700 | | /** |
4701 | | * xmlRelaxNGParsePattern: |
4702 | | * @ctxt: a Relax-NG parser context |
4703 | | * @node: the pattern node. |
4704 | | * |
4705 | | * parse the content of a RelaxNG pattern node. |
4706 | | * |
4707 | | * Returns the definition pointer or NULL in case of error or if no |
4708 | | * pattern is generated. |
4709 | | */ |
4710 | | static xmlRelaxNGDefinePtr |
4711 | | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
4712 | 0 | { |
4713 | 0 | xmlRelaxNGDefinePtr def = NULL; |
4714 | |
|
4715 | 0 | if (node == NULL) { |
4716 | 0 | return (NULL); |
4717 | 0 | } |
4718 | 0 | if (IS_RELAXNG(node, "element")) { |
4719 | 0 | def = xmlRelaxNGParseElement(ctxt, node); |
4720 | 0 | } else if (IS_RELAXNG(node, "attribute")) { |
4721 | 0 | def = xmlRelaxNGParseAttribute(ctxt, node); |
4722 | 0 | } else if (IS_RELAXNG(node, "empty")) { |
4723 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4724 | 0 | if (def == NULL) |
4725 | 0 | return (NULL); |
4726 | 0 | def->type = XML_RELAXNG_EMPTY; |
4727 | 0 | if (node->children != NULL) { |
4728 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
4729 | 0 | "empty: had a child node\n", NULL, NULL); |
4730 | 0 | } |
4731 | 0 | } else if (IS_RELAXNG(node, "text")) { |
4732 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4733 | 0 | if (def == NULL) |
4734 | 0 | return (NULL); |
4735 | 0 | def->type = XML_RELAXNG_TEXT; |
4736 | 0 | if (node->children != NULL) { |
4737 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
4738 | 0 | "text: had a child node\n", NULL, NULL); |
4739 | 0 | } |
4740 | 0 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
4741 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4742 | 0 | if (def == NULL) |
4743 | 0 | return (NULL); |
4744 | 0 | def->type = XML_RELAXNG_ZEROORMORE; |
4745 | 0 | if (node->children == NULL) { |
4746 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4747 | 0 | "Element %s is empty\n", node->name, NULL); |
4748 | 0 | } else { |
4749 | 0 | def->content = |
4750 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
4751 | 0 | } |
4752 | 0 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
4753 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4754 | 0 | if (def == NULL) |
4755 | 0 | return (NULL); |
4756 | 0 | def->type = XML_RELAXNG_ONEORMORE; |
4757 | 0 | if (node->children == NULL) { |
4758 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4759 | 0 | "Element %s is empty\n", node->name, NULL); |
4760 | 0 | } else { |
4761 | 0 | def->content = |
4762 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
4763 | 0 | } |
4764 | 0 | } else if (IS_RELAXNG(node, "optional")) { |
4765 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4766 | 0 | if (def == NULL) |
4767 | 0 | return (NULL); |
4768 | 0 | def->type = XML_RELAXNG_OPTIONAL; |
4769 | 0 | if (node->children == NULL) { |
4770 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4771 | 0 | "Element %s is empty\n", node->name, NULL); |
4772 | 0 | } else { |
4773 | 0 | def->content = |
4774 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
4775 | 0 | } |
4776 | 0 | } else if (IS_RELAXNG(node, "choice")) { |
4777 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4778 | 0 | if (def == NULL) |
4779 | 0 | return (NULL); |
4780 | 0 | def->type = XML_RELAXNG_CHOICE; |
4781 | 0 | if (node->children == NULL) { |
4782 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4783 | 0 | "Element %s is empty\n", node->name, NULL); |
4784 | 0 | } else { |
4785 | 0 | def->content = |
4786 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4787 | 0 | } |
4788 | 0 | } else if (IS_RELAXNG(node, "group")) { |
4789 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4790 | 0 | if (def == NULL) |
4791 | 0 | return (NULL); |
4792 | 0 | def->type = XML_RELAXNG_GROUP; |
4793 | 0 | if (node->children == NULL) { |
4794 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4795 | 0 | "Element %s is empty\n", node->name, NULL); |
4796 | 0 | } else { |
4797 | 0 | def->content = |
4798 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4799 | 0 | } |
4800 | 0 | } else if (IS_RELAXNG(node, "ref")) { |
4801 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4802 | 0 | if (def == NULL) |
4803 | 0 | return (NULL); |
4804 | 0 | def->type = XML_RELAXNG_REF; |
4805 | 0 | def->name = xmlGetProp(node, BAD_CAST "name"); |
4806 | 0 | if (def->name == NULL) { |
4807 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
4808 | 0 | NULL, NULL); |
4809 | 0 | } else { |
4810 | 0 | xmlRelaxNGNormExtSpace(def->name); |
4811 | 0 | if (xmlValidateNCName(def->name, 0)) { |
4812 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
4813 | 0 | "ref name '%s' is not an NCName\n", def->name, |
4814 | 0 | NULL); |
4815 | 0 | } |
4816 | 0 | } |
4817 | 0 | if (node->children != NULL) { |
4818 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
4819 | 0 | NULL, NULL); |
4820 | 0 | } |
4821 | 0 | if (ctxt->grammar->refs == NULL) |
4822 | 0 | ctxt->grammar->refs = xmlHashCreate(10); |
4823 | 0 | if (ctxt->grammar->refs == NULL) { |
4824 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
4825 | 0 | "Could not create references hash\n", NULL, NULL); |
4826 | 0 | def = NULL; |
4827 | 0 | } else { |
4828 | 0 | int tmp; |
4829 | |
|
4830 | 0 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
4831 | 0 | if (tmp < 0) { |
4832 | 0 | xmlRelaxNGDefinePtr prev; |
4833 | |
|
4834 | 0 | prev = (xmlRelaxNGDefinePtr) |
4835 | 0 | xmlHashLookup(ctxt->grammar->refs, def->name); |
4836 | 0 | if (prev == NULL) { |
4837 | 0 | if (def->name != NULL) { |
4838 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
4839 | 0 | "Error refs definitions '%s'\n", |
4840 | 0 | def->name, NULL); |
4841 | 0 | } else { |
4842 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
4843 | 0 | "Error refs definitions\n", |
4844 | 0 | NULL, NULL); |
4845 | 0 | } |
4846 | 0 | def = NULL; |
4847 | 0 | } else { |
4848 | 0 | def->nextHash = prev->nextHash; |
4849 | 0 | prev->nextHash = def; |
4850 | 0 | } |
4851 | 0 | } |
4852 | 0 | } |
4853 | 0 | } else if (IS_RELAXNG(node, "data")) { |
4854 | 0 | def = xmlRelaxNGParseData(ctxt, node); |
4855 | 0 | } else if (IS_RELAXNG(node, "value")) { |
4856 | 0 | def = xmlRelaxNGParseValue(ctxt, node); |
4857 | 0 | } else if (IS_RELAXNG(node, "list")) { |
4858 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4859 | 0 | if (def == NULL) |
4860 | 0 | return (NULL); |
4861 | 0 | def->type = XML_RELAXNG_LIST; |
4862 | 0 | if (node->children == NULL) { |
4863 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
4864 | 0 | "Element %s is empty\n", node->name, NULL); |
4865 | 0 | } else { |
4866 | 0 | def->content = |
4867 | 0 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
4868 | 0 | } |
4869 | 0 | } else if (IS_RELAXNG(node, "interleave")) { |
4870 | 0 | def = xmlRelaxNGParseInterleave(ctxt, node); |
4871 | 0 | } else if (IS_RELAXNG(node, "externalRef")) { |
4872 | 0 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
4873 | 0 | } else if (IS_RELAXNG(node, "notAllowed")) { |
4874 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4875 | 0 | if (def == NULL) |
4876 | 0 | return (NULL); |
4877 | 0 | def->type = XML_RELAXNG_NOT_ALLOWED; |
4878 | 0 | if (node->children != NULL) { |
4879 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
4880 | 0 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
4881 | 0 | NULL, NULL); |
4882 | 0 | } |
4883 | 0 | } else if (IS_RELAXNG(node, "grammar")) { |
4884 | 0 | xmlRelaxNGGrammarPtr grammar, old; |
4885 | 0 | xmlRelaxNGGrammarPtr oldparent; |
4886 | |
|
4887 | 0 | oldparent = ctxt->parentgrammar; |
4888 | 0 | old = ctxt->grammar; |
4889 | 0 | ctxt->parentgrammar = old; |
4890 | 0 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
4891 | 0 | if (old != NULL) { |
4892 | 0 | ctxt->grammar = old; |
4893 | 0 | ctxt->parentgrammar = oldparent; |
4894 | | #if 0 |
4895 | | if (grammar != NULL) { |
4896 | | grammar->next = old->next; |
4897 | | old->next = grammar; |
4898 | | } |
4899 | | #endif |
4900 | 0 | } |
4901 | 0 | if (grammar != NULL) |
4902 | 0 | def = grammar->start; |
4903 | 0 | else |
4904 | 0 | def = NULL; |
4905 | 0 | } else if (IS_RELAXNG(node, "parentRef")) { |
4906 | 0 | if (ctxt->parentgrammar == NULL) { |
4907 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
4908 | 0 | "Use of parentRef without a parent grammar\n", NULL, |
4909 | 0 | NULL); |
4910 | 0 | return (NULL); |
4911 | 0 | } |
4912 | 0 | def = xmlRelaxNGNewDefine(ctxt, node); |
4913 | 0 | if (def == NULL) |
4914 | 0 | return (NULL); |
4915 | 0 | def->type = XML_RELAXNG_PARENTREF; |
4916 | 0 | def->name = xmlGetProp(node, BAD_CAST "name"); |
4917 | 0 | if (def->name == NULL) { |
4918 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
4919 | 0 | "parentRef has no name\n", NULL, NULL); |
4920 | 0 | } else { |
4921 | 0 | xmlRelaxNGNormExtSpace(def->name); |
4922 | 0 | if (xmlValidateNCName(def->name, 0)) { |
4923 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
4924 | 0 | "parentRef name '%s' is not an NCName\n", |
4925 | 0 | def->name, NULL); |
4926 | 0 | } |
4927 | 0 | } |
4928 | 0 | if (node->children != NULL) { |
4929 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
4930 | 0 | "parentRef is not empty\n", NULL, NULL); |
4931 | 0 | } |
4932 | 0 | if (ctxt->parentgrammar->refs == NULL) |
4933 | 0 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
4934 | 0 | if (ctxt->parentgrammar->refs == NULL) { |
4935 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
4936 | 0 | "Could not create references hash\n", NULL, NULL); |
4937 | 0 | def = NULL; |
4938 | 0 | } else if (def->name != NULL) { |
4939 | 0 | int tmp; |
4940 | |
|
4941 | 0 | tmp = |
4942 | 0 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
4943 | 0 | if (tmp < 0) { |
4944 | 0 | xmlRelaxNGDefinePtr prev; |
4945 | |
|
4946 | 0 | prev = (xmlRelaxNGDefinePtr) |
4947 | 0 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
4948 | 0 | if (prev == NULL) { |
4949 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
4950 | 0 | "Internal error parentRef definitions '%s'\n", |
4951 | 0 | def->name, NULL); |
4952 | 0 | def = NULL; |
4953 | 0 | } else { |
4954 | 0 | def->nextHash = prev->nextHash; |
4955 | 0 | prev->nextHash = def; |
4956 | 0 | } |
4957 | 0 | } |
4958 | 0 | } |
4959 | 0 | } else if (IS_RELAXNG(node, "mixed")) { |
4960 | 0 | if (node->children == NULL) { |
4961 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
4962 | 0 | NULL, NULL); |
4963 | 0 | def = NULL; |
4964 | 0 | } else { |
4965 | 0 | def = xmlRelaxNGParseInterleave(ctxt, node); |
4966 | 0 | if (def != NULL) { |
4967 | 0 | xmlRelaxNGDefinePtr tmp; |
4968 | |
|
4969 | 0 | if ((def->content != NULL) && (def->content->next != NULL)) { |
4970 | 0 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
4971 | 0 | if (tmp != NULL) { |
4972 | 0 | tmp->type = XML_RELAXNG_GROUP; |
4973 | 0 | tmp->content = def->content; |
4974 | 0 | def->content = tmp; |
4975 | 0 | } |
4976 | 0 | } |
4977 | |
|
4978 | 0 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
4979 | 0 | if (tmp == NULL) |
4980 | 0 | return (def); |
4981 | 0 | tmp->type = XML_RELAXNG_TEXT; |
4982 | 0 | tmp->next = def->content; |
4983 | 0 | def->content = tmp; |
4984 | 0 | } |
4985 | 0 | } |
4986 | 0 | } else { |
4987 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
4988 | 0 | "Unexpected node %s is not a pattern\n", node->name, |
4989 | 0 | NULL); |
4990 | 0 | def = NULL; |
4991 | 0 | } |
4992 | 0 | return (def); |
4993 | 0 | } |
4994 | | |
4995 | | /** |
4996 | | * xmlRelaxNGParseAttribute: |
4997 | | * @ctxt: a Relax-NG parser context |
4998 | | * @node: the element node |
4999 | | * |
5000 | | * parse the content of a RelaxNG attribute node. |
5001 | | * |
5002 | | * Returns the definition pointer or NULL in case of error. |
5003 | | */ |
5004 | | static xmlRelaxNGDefinePtr |
5005 | | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
5006 | 0 | { |
5007 | 0 | xmlRelaxNGDefinePtr ret, cur; |
5008 | 0 | xmlNodePtr child; |
5009 | 0 | int old_flags; |
5010 | |
|
5011 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5012 | 0 | if (ret == NULL) |
5013 | 0 | return (NULL); |
5014 | 0 | ret->type = XML_RELAXNG_ATTRIBUTE; |
5015 | 0 | ret->parent = ctxt->def; |
5016 | 0 | child = node->children; |
5017 | 0 | if (child == NULL) { |
5018 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
5019 | 0 | "xmlRelaxNGParseattribute: attribute has no children\n", |
5020 | 0 | NULL, NULL); |
5021 | 0 | return (ret); |
5022 | 0 | } |
5023 | 0 | old_flags = ctxt->flags; |
5024 | 0 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
5025 | 0 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
5026 | 0 | if (cur != NULL) |
5027 | 0 | child = child->next; |
5028 | |
|
5029 | 0 | if (child != NULL) { |
5030 | 0 | cur = xmlRelaxNGParsePattern(ctxt, child); |
5031 | 0 | if (cur != NULL) { |
5032 | 0 | switch (cur->type) { |
5033 | 0 | case XML_RELAXNG_EMPTY: |
5034 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
5035 | 0 | case XML_RELAXNG_TEXT: |
5036 | 0 | case XML_RELAXNG_ELEMENT: |
5037 | 0 | case XML_RELAXNG_DATATYPE: |
5038 | 0 | case XML_RELAXNG_VALUE: |
5039 | 0 | case XML_RELAXNG_LIST: |
5040 | 0 | case XML_RELAXNG_REF: |
5041 | 0 | case XML_RELAXNG_PARENTREF: |
5042 | 0 | case XML_RELAXNG_EXTERNALREF: |
5043 | 0 | case XML_RELAXNG_DEF: |
5044 | 0 | case XML_RELAXNG_ONEORMORE: |
5045 | 0 | case XML_RELAXNG_ZEROORMORE: |
5046 | 0 | case XML_RELAXNG_OPTIONAL: |
5047 | 0 | case XML_RELAXNG_CHOICE: |
5048 | 0 | case XML_RELAXNG_GROUP: |
5049 | 0 | case XML_RELAXNG_INTERLEAVE: |
5050 | 0 | case XML_RELAXNG_ATTRIBUTE: |
5051 | 0 | ret->content = cur; |
5052 | 0 | cur->parent = ret; |
5053 | 0 | break; |
5054 | 0 | case XML_RELAXNG_START: |
5055 | 0 | case XML_RELAXNG_PARAM: |
5056 | 0 | case XML_RELAXNG_EXCEPT: |
5057 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
5058 | 0 | "attribute has invalid content\n", NULL, |
5059 | 0 | NULL); |
5060 | 0 | break; |
5061 | 0 | case XML_RELAXNG_NOOP: |
5062 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
5063 | 0 | "RNG Internal error, noop found in attribute\n", |
5064 | 0 | NULL, NULL); |
5065 | 0 | break; |
5066 | 0 | } |
5067 | 0 | } |
5068 | 0 | child = child->next; |
5069 | 0 | } |
5070 | 0 | if (child != NULL) { |
5071 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
5072 | 0 | "attribute has multiple children\n", NULL, NULL); |
5073 | 0 | } |
5074 | 0 | ctxt->flags = old_flags; |
5075 | 0 | return (ret); |
5076 | 0 | } |
5077 | | |
5078 | | /** |
5079 | | * xmlRelaxNGParseExceptNameClass: |
5080 | | * @ctxt: a Relax-NG parser context |
5081 | | * @node: the except node |
5082 | | * @attr: 1 if within an attribute, 0 if within an element |
5083 | | * |
5084 | | * parse the content of a RelaxNG nameClass node. |
5085 | | * |
5086 | | * Returns the definition pointer or NULL in case of error. |
5087 | | */ |
5088 | | static xmlRelaxNGDefinePtr |
5089 | | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
5090 | | xmlNodePtr node, int attr) |
5091 | 0 | { |
5092 | 0 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
5093 | 0 | xmlNodePtr child; |
5094 | |
|
5095 | 0 | if (!IS_RELAXNG(node, "except")) { |
5096 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
5097 | 0 | "Expecting an except node\n", NULL, NULL); |
5098 | 0 | return (NULL); |
5099 | 0 | } |
5100 | 0 | if (node->next != NULL) { |
5101 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
5102 | 0 | "exceptNameClass allows only a single except node\n", |
5103 | 0 | NULL, NULL); |
5104 | 0 | } |
5105 | 0 | if (node->children == NULL) { |
5106 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
5107 | 0 | NULL, NULL); |
5108 | 0 | return (NULL); |
5109 | 0 | } |
5110 | | |
5111 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5112 | 0 | if (ret == NULL) |
5113 | 0 | return (NULL); |
5114 | 0 | ret->type = XML_RELAXNG_EXCEPT; |
5115 | 0 | child = node->children; |
5116 | 0 | while (child != NULL) { |
5117 | 0 | cur = xmlRelaxNGNewDefine(ctxt, child); |
5118 | 0 | if (cur == NULL) |
5119 | 0 | break; |
5120 | 0 | if (attr) |
5121 | 0 | cur->type = XML_RELAXNG_ATTRIBUTE; |
5122 | 0 | else |
5123 | 0 | cur->type = XML_RELAXNG_ELEMENT; |
5124 | |
|
5125 | 0 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
5126 | 0 | if (last == NULL) { |
5127 | 0 | ret->content = cur; |
5128 | 0 | } else { |
5129 | 0 | last->next = cur; |
5130 | 0 | } |
5131 | 0 | last = cur; |
5132 | 0 | } |
5133 | 0 | child = child->next; |
5134 | 0 | } |
5135 | |
|
5136 | 0 | return (ret); |
5137 | 0 | } |
5138 | | |
5139 | | /** |
5140 | | * xmlRelaxNGParseNameClass: |
5141 | | * @ctxt: a Relax-NG parser context |
5142 | | * @node: the nameClass node |
5143 | | * @def: the current definition |
5144 | | * |
5145 | | * parse the content of a RelaxNG nameClass node. |
5146 | | * |
5147 | | * Returns the definition pointer or NULL in case of error. |
5148 | | */ |
5149 | | static xmlRelaxNGDefinePtr |
5150 | | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
5151 | | xmlRelaxNGDefinePtr def) |
5152 | 0 | { |
5153 | 0 | xmlRelaxNGDefinePtr ret, tmp; |
5154 | 0 | xmlChar *val; |
5155 | |
|
5156 | 0 | ret = def; |
5157 | 0 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
5158 | 0 | (IS_RELAXNG(node, "nsName"))) { |
5159 | 0 | if ((def->type != XML_RELAXNG_ELEMENT) && |
5160 | 0 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
5161 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5162 | 0 | if (ret == NULL) |
5163 | 0 | return (NULL); |
5164 | 0 | ret->parent = def; |
5165 | 0 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
5166 | 0 | ret->type = XML_RELAXNG_ATTRIBUTE; |
5167 | 0 | else |
5168 | 0 | ret->type = XML_RELAXNG_ELEMENT; |
5169 | 0 | } |
5170 | 0 | } |
5171 | 0 | if (IS_RELAXNG(node, "name")) { |
5172 | 0 | val = xmlNodeGetContent(node); |
5173 | 0 | xmlRelaxNGNormExtSpace(val); |
5174 | 0 | if (xmlValidateNCName(val, 0)) { |
5175 | 0 | if (node->parent != NULL) |
5176 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
5177 | 0 | "Element %s name '%s' is not an NCName\n", |
5178 | 0 | node->parent->name, val); |
5179 | 0 | else |
5180 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
5181 | 0 | "name '%s' is not an NCName\n", |
5182 | 0 | val, NULL); |
5183 | 0 | } |
5184 | 0 | ret->name = val; |
5185 | 0 | val = xmlGetProp(node, BAD_CAST "ns"); |
5186 | 0 | ret->ns = val; |
5187 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
5188 | 0 | (val != NULL) && |
5189 | 0 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
5190 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
5191 | 0 | "Attribute with namespace '%s' is not allowed\n", |
5192 | 0 | val, NULL); |
5193 | 0 | } |
5194 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
5195 | 0 | (val != NULL) && |
5196 | 0 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
5197 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
5198 | 0 | "Attribute with QName 'xmlns' is not allowed\n", |
5199 | 0 | val, NULL); |
5200 | 0 | } |
5201 | 0 | } else if (IS_RELAXNG(node, "anyName")) { |
5202 | 0 | ret->name = NULL; |
5203 | 0 | ret->ns = NULL; |
5204 | 0 | if (node->children != NULL) { |
5205 | 0 | ret->nameClass = |
5206 | 0 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
5207 | 0 | (def->type == |
5208 | 0 | XML_RELAXNG_ATTRIBUTE)); |
5209 | 0 | } |
5210 | 0 | } else if (IS_RELAXNG(node, "nsName")) { |
5211 | 0 | ret->name = NULL; |
5212 | 0 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
5213 | 0 | if (ret->ns == NULL) { |
5214 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
5215 | 0 | "nsName has no ns attribute\n", NULL, NULL); |
5216 | 0 | } |
5217 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
5218 | 0 | (ret->ns != NULL) && |
5219 | 0 | (xmlStrEqual |
5220 | 0 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
5221 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
5222 | 0 | "Attribute with namespace '%s' is not allowed\n", |
5223 | 0 | ret->ns, NULL); |
5224 | 0 | } |
5225 | 0 | if (node->children != NULL) { |
5226 | 0 | ret->nameClass = |
5227 | 0 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
5228 | 0 | (def->type == |
5229 | 0 | XML_RELAXNG_ATTRIBUTE)); |
5230 | 0 | } |
5231 | 0 | } else if (IS_RELAXNG(node, "choice")) { |
5232 | 0 | xmlNodePtr child; |
5233 | 0 | xmlRelaxNGDefinePtr last = NULL; |
5234 | |
|
5235 | 0 | if (def->type == XML_RELAXNG_CHOICE) { |
5236 | 0 | ret = def; |
5237 | 0 | } else { |
5238 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5239 | 0 | if (ret == NULL) |
5240 | 0 | return (NULL); |
5241 | 0 | ret->parent = def; |
5242 | 0 | ret->type = XML_RELAXNG_CHOICE; |
5243 | 0 | } |
5244 | | |
5245 | 0 | if (node->children == NULL) { |
5246 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
5247 | 0 | "Element choice is empty\n", NULL, NULL); |
5248 | 0 | } else { |
5249 | |
|
5250 | 0 | child = node->children; |
5251 | 0 | while (child != NULL) { |
5252 | 0 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
5253 | 0 | if (tmp != NULL) { |
5254 | 0 | if (last == NULL) { |
5255 | 0 | last = tmp; |
5256 | 0 | } else { |
5257 | 0 | last->next = tmp; |
5258 | 0 | last = tmp; |
5259 | 0 | } |
5260 | 0 | } |
5261 | 0 | child = child->next; |
5262 | 0 | } |
5263 | 0 | } |
5264 | 0 | } else { |
5265 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
5266 | 0 | "expecting name, anyName, nsName or choice : got %s\n", |
5267 | 0 | (node == NULL ? (const xmlChar *) "nothing" : node->name), |
5268 | 0 | NULL); |
5269 | 0 | return (NULL); |
5270 | 0 | } |
5271 | 0 | if (ret != def) { |
5272 | 0 | if (def->nameClass == NULL) { |
5273 | 0 | def->nameClass = ret; |
5274 | 0 | } else { |
5275 | 0 | tmp = def->nameClass; |
5276 | 0 | while (tmp->next != NULL) { |
5277 | 0 | tmp = tmp->next; |
5278 | 0 | } |
5279 | 0 | tmp->next = ret; |
5280 | 0 | } |
5281 | 0 | } |
5282 | 0 | return (ret); |
5283 | 0 | } |
5284 | | |
5285 | | /** |
5286 | | * xmlRelaxNGParseElement: |
5287 | | * @ctxt: a Relax-NG parser context |
5288 | | * @node: the element node |
5289 | | * |
5290 | | * parse the content of a RelaxNG element node. |
5291 | | * |
5292 | | * Returns the definition pointer or NULL in case of error. |
5293 | | */ |
5294 | | static xmlRelaxNGDefinePtr |
5295 | | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
5296 | 0 | { |
5297 | 0 | xmlRelaxNGDefinePtr ret, cur, last; |
5298 | 0 | xmlNodePtr child; |
5299 | 0 | const xmlChar *olddefine; |
5300 | |
|
5301 | 0 | ret = xmlRelaxNGNewDefine(ctxt, node); |
5302 | 0 | if (ret == NULL) |
5303 | 0 | return (NULL); |
5304 | 0 | ret->type = XML_RELAXNG_ELEMENT; |
5305 | 0 | ret->parent = ctxt->def; |
5306 | 0 | child = node->children; |
5307 | 0 | if (child == NULL) { |
5308 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
5309 | 0 | "xmlRelaxNGParseElement: element has no children\n", |
5310 | 0 | NULL, NULL); |
5311 | 0 | return (ret); |
5312 | 0 | } |
5313 | 0 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
5314 | 0 | if (cur != NULL) |
5315 | 0 | child = child->next; |
5316 | |
|
5317 | 0 | if (child == NULL) { |
5318 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
5319 | 0 | "xmlRelaxNGParseElement: element has no content\n", |
5320 | 0 | NULL, NULL); |
5321 | 0 | return (ret); |
5322 | 0 | } |
5323 | 0 | olddefine = ctxt->define; |
5324 | 0 | ctxt->define = NULL; |
5325 | 0 | last = NULL; |
5326 | 0 | while (child != NULL) { |
5327 | 0 | cur = xmlRelaxNGParsePattern(ctxt, child); |
5328 | 0 | if (cur != NULL) { |
5329 | 0 | cur->parent = ret; |
5330 | 0 | switch (cur->type) { |
5331 | 0 | case XML_RELAXNG_EMPTY: |
5332 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
5333 | 0 | case XML_RELAXNG_TEXT: |
5334 | 0 | case XML_RELAXNG_ELEMENT: |
5335 | 0 | case XML_RELAXNG_DATATYPE: |
5336 | 0 | case XML_RELAXNG_VALUE: |
5337 | 0 | case XML_RELAXNG_LIST: |
5338 | 0 | case XML_RELAXNG_REF: |
5339 | 0 | case XML_RELAXNG_PARENTREF: |
5340 | 0 | case XML_RELAXNG_EXTERNALREF: |
5341 | 0 | case XML_RELAXNG_DEF: |
5342 | 0 | case XML_RELAXNG_ZEROORMORE: |
5343 | 0 | case XML_RELAXNG_ONEORMORE: |
5344 | 0 | case XML_RELAXNG_OPTIONAL: |
5345 | 0 | case XML_RELAXNG_CHOICE: |
5346 | 0 | case XML_RELAXNG_GROUP: |
5347 | 0 | case XML_RELAXNG_INTERLEAVE: |
5348 | 0 | if (last == NULL) { |
5349 | 0 | ret->content = last = cur; |
5350 | 0 | } else { |
5351 | 0 | if ((last->type == XML_RELAXNG_ELEMENT) && |
5352 | 0 | (ret->content == last)) { |
5353 | 0 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
5354 | 0 | if (ret->content != NULL) { |
5355 | 0 | ret->content->type = XML_RELAXNG_GROUP; |
5356 | 0 | ret->content->content = last; |
5357 | 0 | } else { |
5358 | 0 | ret->content = last; |
5359 | 0 | } |
5360 | 0 | } |
5361 | 0 | last->next = cur; |
5362 | 0 | last = cur; |
5363 | 0 | } |
5364 | 0 | break; |
5365 | 0 | case XML_RELAXNG_ATTRIBUTE: |
5366 | 0 | cur->next = ret->attrs; |
5367 | 0 | ret->attrs = cur; |
5368 | 0 | break; |
5369 | 0 | case XML_RELAXNG_START: |
5370 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5371 | 0 | "RNG Internal error, start found in element\n", |
5372 | 0 | NULL, NULL); |
5373 | 0 | break; |
5374 | 0 | case XML_RELAXNG_PARAM: |
5375 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5376 | 0 | "RNG Internal error, param found in element\n", |
5377 | 0 | NULL, NULL); |
5378 | 0 | break; |
5379 | 0 | case XML_RELAXNG_EXCEPT: |
5380 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5381 | 0 | "RNG Internal error, except found in element\n", |
5382 | 0 | NULL, NULL); |
5383 | 0 | break; |
5384 | 0 | case XML_RELAXNG_NOOP: |
5385 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
5386 | 0 | "RNG Internal error, noop found in element\n", |
5387 | 0 | NULL, NULL); |
5388 | 0 | break; |
5389 | 0 | } |
5390 | 0 | } |
5391 | 0 | child = child->next; |
5392 | 0 | } |
5393 | 0 | ctxt->define = olddefine; |
5394 | 0 | return (ret); |
5395 | 0 | } |
5396 | | |
5397 | | /** |
5398 | | * xmlRelaxNGParsePatterns: |
5399 | | * @ctxt: a Relax-NG parser context |
5400 | | * @nodes: list of nodes |
5401 | | * @group: use an implicit <group> for elements |
5402 | | * |
5403 | | * parse the content of a RelaxNG start node. |
5404 | | * |
5405 | | * Returns the definition pointer or NULL in case of error. |
5406 | | */ |
5407 | | static xmlRelaxNGDefinePtr |
5408 | | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
5409 | | int group) |
5410 | 0 | { |
5411 | 0 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
5412 | |
|
5413 | 0 | parent = ctxt->def; |
5414 | 0 | while (nodes != NULL) { |
5415 | 0 | if (IS_RELAXNG(nodes, "element")) { |
5416 | 0 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
5417 | 0 | if (cur == NULL) |
5418 | 0 | return (NULL); |
5419 | 0 | if (def == NULL) { |
5420 | 0 | def = last = cur; |
5421 | 0 | } else { |
5422 | 0 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
5423 | 0 | (def == last)) { |
5424 | 0 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
5425 | 0 | if (def == NULL) |
5426 | 0 | return (NULL); |
5427 | 0 | def->type = XML_RELAXNG_GROUP; |
5428 | 0 | def->content = last; |
5429 | 0 | } |
5430 | 0 | last->next = cur; |
5431 | 0 | last = cur; |
5432 | 0 | } |
5433 | 0 | cur->parent = parent; |
5434 | 0 | } else { |
5435 | 0 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
5436 | 0 | if (cur != NULL) { |
5437 | 0 | if (def == NULL) { |
5438 | 0 | def = last = cur; |
5439 | 0 | } else { |
5440 | 0 | last->next = cur; |
5441 | 0 | last = cur; |
5442 | 0 | } |
5443 | 0 | } |
5444 | 0 | } |
5445 | 0 | nodes = nodes->next; |
5446 | 0 | } |
5447 | 0 | return (def); |
5448 | 0 | } |
5449 | | |
5450 | | /** |
5451 | | * xmlRelaxNGParseStart: |
5452 | | * @ctxt: a Relax-NG parser context |
5453 | | * @nodes: start children nodes |
5454 | | * |
5455 | | * parse the content of a RelaxNG start node. |
5456 | | * |
5457 | | * Returns 0 in case of success, -1 in case of error |
5458 | | */ |
5459 | | static int |
5460 | | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
5461 | 0 | { |
5462 | 0 | int ret = 0; |
5463 | 0 | xmlRelaxNGDefinePtr def = NULL, last; |
5464 | |
|
5465 | 0 | if (nodes == NULL) { |
5466 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
5467 | 0 | NULL, NULL); |
5468 | 0 | return (-1); |
5469 | 0 | } |
5470 | 0 | if (IS_RELAXNG(nodes, "empty")) { |
5471 | 0 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
5472 | 0 | if (def == NULL) |
5473 | 0 | return (-1); |
5474 | 0 | def->type = XML_RELAXNG_EMPTY; |
5475 | 0 | if (nodes->children != NULL) { |
5476 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
5477 | 0 | "element empty is not empty\n", NULL, NULL); |
5478 | 0 | } |
5479 | 0 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
5480 | 0 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
5481 | 0 | if (def == NULL) |
5482 | 0 | return (-1); |
5483 | 0 | def->type = XML_RELAXNG_NOT_ALLOWED; |
5484 | 0 | if (nodes->children != NULL) { |
5485 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
5486 | 0 | "element notAllowed is not empty\n", NULL, NULL); |
5487 | 0 | } |
5488 | 0 | } else { |
5489 | 0 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
5490 | 0 | } |
5491 | 0 | if (ctxt->grammar->start != NULL) { |
5492 | 0 | last = ctxt->grammar->start; |
5493 | 0 | while (last->next != NULL) |
5494 | 0 | last = last->next; |
5495 | 0 | last->next = def; |
5496 | 0 | } else { |
5497 | 0 | ctxt->grammar->start = def; |
5498 | 0 | } |
5499 | 0 | nodes = nodes->next; |
5500 | 0 | if (nodes != NULL) { |
5501 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
5502 | 0 | "start more than one children\n", NULL, NULL); |
5503 | 0 | return (-1); |
5504 | 0 | } |
5505 | 0 | return (ret); |
5506 | 0 | } |
5507 | | |
5508 | | /** |
5509 | | * xmlRelaxNGParseGrammarContent: |
5510 | | * @ctxt: a Relax-NG parser context |
5511 | | * @nodes: grammar children nodes |
5512 | | * |
5513 | | * parse the content of a RelaxNG grammar node. |
5514 | | * |
5515 | | * Returns 0 in case of success, -1 in case of error |
5516 | | */ |
5517 | | static int |
5518 | | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
5519 | | xmlNodePtr nodes) |
5520 | 0 | { |
5521 | 0 | int ret = 0, tmp; |
5522 | |
|
5523 | 0 | if (nodes == NULL) { |
5524 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
5525 | 0 | "grammar has no children\n", NULL, NULL); |
5526 | 0 | return (-1); |
5527 | 0 | } |
5528 | 0 | while (nodes != NULL) { |
5529 | 0 | if (IS_RELAXNG(nodes, "start")) { |
5530 | 0 | if (nodes->children == NULL) { |
5531 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
5532 | 0 | "start has no children\n", NULL, NULL); |
5533 | 0 | } else { |
5534 | 0 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
5535 | 0 | if (tmp != 0) |
5536 | 0 | ret = -1; |
5537 | 0 | } |
5538 | 0 | } else if (IS_RELAXNG(nodes, "define")) { |
5539 | 0 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
5540 | 0 | if (tmp != 0) |
5541 | 0 | ret = -1; |
5542 | 0 | } else if (IS_RELAXNG(nodes, "include")) { |
5543 | 0 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
5544 | 0 | if (tmp != 0) |
5545 | 0 | ret = -1; |
5546 | 0 | } else { |
5547 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
5548 | 0 | "grammar has unexpected child %s\n", nodes->name, |
5549 | 0 | NULL); |
5550 | 0 | ret = -1; |
5551 | 0 | } |
5552 | 0 | nodes = nodes->next; |
5553 | 0 | } |
5554 | 0 | return (ret); |
5555 | 0 | } |
5556 | | |
5557 | | /** |
5558 | | * xmlRelaxNGCheckReference: |
5559 | | * @ref: the ref |
5560 | | * @ctxt: a Relax-NG parser context |
5561 | | * @name: the name associated to the defines |
5562 | | * |
5563 | | * Applies the 4.17. combine attribute rule for all the define |
5564 | | * element of a given grammar using the same name. |
5565 | | */ |
5566 | | static void |
5567 | | xmlRelaxNGCheckReference(void *payload, void *data, const xmlChar * name) |
5568 | 0 | { |
5569 | 0 | xmlRelaxNGDefinePtr ref = (xmlRelaxNGDefinePtr) payload; |
5570 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
5571 | 0 | xmlRelaxNGGrammarPtr grammar; |
5572 | 0 | xmlRelaxNGDefinePtr def, cur; |
5573 | | |
5574 | | /* |
5575 | | * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef |
5576 | | */ |
5577 | 0 | if (ref->dflags & IS_EXTERNAL_REF) |
5578 | 0 | return; |
5579 | | |
5580 | 0 | grammar = ctxt->grammar; |
5581 | 0 | if (grammar == NULL) { |
5582 | 0 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
5583 | 0 | "Internal error: no grammar in CheckReference %s\n", |
5584 | 0 | name, NULL); |
5585 | 0 | return; |
5586 | 0 | } |
5587 | 0 | if (ref->content != NULL) { |
5588 | 0 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
5589 | 0 | "Internal error: reference has content in CheckReference %s\n", |
5590 | 0 | name, NULL); |
5591 | 0 | return; |
5592 | 0 | } |
5593 | 0 | if (grammar->defs != NULL) { |
5594 | 0 | def = xmlHashLookup(grammar->defs, name); |
5595 | 0 | if (def != NULL) { |
5596 | 0 | cur = ref; |
5597 | 0 | while (cur != NULL) { |
5598 | 0 | cur->content = def; |
5599 | 0 | cur = cur->nextHash; |
5600 | 0 | } |
5601 | 0 | } else { |
5602 | 0 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
5603 | 0 | "Reference %s has no matching definition\n", name, |
5604 | 0 | NULL); |
5605 | 0 | } |
5606 | 0 | } else { |
5607 | 0 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
5608 | 0 | "Reference %s has no matching definition\n", name, |
5609 | 0 | NULL); |
5610 | 0 | } |
5611 | 0 | } |
5612 | | |
5613 | | /** |
5614 | | * xmlRelaxNGCheckCombine: |
5615 | | * @define: the define(s) list |
5616 | | * @ctxt: a Relax-NG parser context |
5617 | | * @name: the name associated to the defines |
5618 | | * |
5619 | | * Applies the 4.17. combine attribute rule for all the define |
5620 | | * element of a given grammar using the same name. |
5621 | | */ |
5622 | | static void |
5623 | | xmlRelaxNGCheckCombine(void *payload, void *data, const xmlChar * name) |
5624 | 0 | { |
5625 | 0 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) payload; |
5626 | 0 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
5627 | 0 | xmlChar *combine; |
5628 | 0 | int choiceOrInterleave = -1; |
5629 | 0 | int missing = 0; |
5630 | 0 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
5631 | |
|
5632 | 0 | if (define->nextHash == NULL) |
5633 | 0 | return; |
5634 | 0 | cur = define; |
5635 | 0 | while (cur != NULL) { |
5636 | 0 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
5637 | 0 | if (combine != NULL) { |
5638 | 0 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
5639 | 0 | if (choiceOrInterleave == -1) |
5640 | 0 | choiceOrInterleave = 1; |
5641 | 0 | else if (choiceOrInterleave == 0) { |
5642 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
5643 | 0 | "Defines for %s use both 'choice' and 'interleave'\n", |
5644 | 0 | name, NULL); |
5645 | 0 | } |
5646 | 0 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
5647 | 0 | if (choiceOrInterleave == -1) |
5648 | 0 | choiceOrInterleave = 0; |
5649 | 0 | else if (choiceOrInterleave == 1) { |
5650 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
5651 | 0 | "Defines for %s use both 'choice' and 'interleave'\n", |
5652 | 0 | name, NULL); |
5653 | 0 | } |
5654 | 0 | } else { |
5655 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
5656 | 0 | "Defines for %s use unknown combine value '%s''\n", |
5657 | 0 | name, combine); |
5658 | 0 | } |
5659 | 0 | xmlFree(combine); |
5660 | 0 | } else { |
5661 | 0 | if (missing == 0) |
5662 | 0 | missing = 1; |
5663 | 0 | else { |
5664 | 0 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
5665 | 0 | "Some defines for %s needs the combine attribute\n", |
5666 | 0 | name, NULL); |
5667 | 0 | } |
5668 | 0 | } |
5669 | |
|
5670 | 0 | cur = cur->nextHash; |
5671 | 0 | } |
5672 | 0 | if (choiceOrInterleave == -1) |
5673 | 0 | choiceOrInterleave = 0; |
5674 | 0 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
5675 | 0 | if (cur == NULL) |
5676 | 0 | return; |
5677 | 0 | if (choiceOrInterleave == 0) |
5678 | 0 | cur->type = XML_RELAXNG_INTERLEAVE; |
5679 | 0 | else |
5680 | 0 | cur->type = XML_RELAXNG_CHOICE; |
5681 | 0 | tmp = define; |
5682 | 0 | last = NULL; |
5683 | 0 | while (tmp != NULL) { |
5684 | 0 | if (tmp->content != NULL) { |
5685 | 0 | if (tmp->content->next != NULL) { |
5686 | | /* |
5687 | | * we need first to create a wrapper. |
5688 | | */ |
5689 | 0 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
5690 | 0 | if (tmp2 == NULL) |
5691 | 0 | break; |
5692 | 0 | tmp2->type = XML_RELAXNG_GROUP; |
5693 | 0 | tmp2->content = tmp->content; |
5694 | 0 | } else { |
5695 | 0 | tmp2 = tmp->content; |
5696 | 0 | } |
5697 | 0 | if (last == NULL) { |
5698 | 0 | cur->content = tmp2; |
5699 | 0 | } else { |
5700 | 0 | last->next = tmp2; |
5701 | 0 | } |
5702 | 0 | last = tmp2; |
5703 | 0 | } |
5704 | 0 | tmp->content = cur; |
5705 | 0 | tmp = tmp->nextHash; |
5706 | 0 | } |
5707 | 0 | define->content = 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, define->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, define->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 | | * xmlRelaxNGCombineStart: |
5731 | | * @ctxt: a Relax-NG parser context |
5732 | | * @grammar: the grammar |
5733 | | * |
5734 | | * Applies the 4.17. combine rule for all the start |
5735 | | * element of a given grammar. |
5736 | | */ |
5737 | | static void |
5738 | | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
5739 | | xmlRelaxNGGrammarPtr grammar) |
5740 | 0 | { |
5741 | 0 | xmlRelaxNGDefinePtr starts; |
5742 | 0 | xmlChar *combine; |
5743 | 0 | int choiceOrInterleave = -1; |
5744 | 0 | int missing = 0; |
5745 | 0 | xmlRelaxNGDefinePtr cur; |
5746 | |
|
5747 | 0 | starts = grammar->start; |
5748 | 0 | if ((starts == NULL) || (starts->next == NULL)) |
5749 | 0 | return; |
5750 | 0 | cur = starts; |
5751 | 0 | while (cur != NULL) { |
5752 | 0 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
5753 | 0 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
5754 | 0 | combine = NULL; |
5755 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
5756 | 0 | "Internal error: start element not found\n", NULL, |
5757 | 0 | NULL); |
5758 | 0 | } else { |
5759 | 0 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
5760 | 0 | } |
5761 | |
|
5762 | 0 | if (combine != NULL) { |
5763 | 0 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
5764 | 0 | if (choiceOrInterleave == -1) |
5765 | 0 | choiceOrInterleave = 1; |
5766 | 0 | else if (choiceOrInterleave == 0) { |
5767 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
5768 | 0 | "<start> use both 'choice' and 'interleave'\n", |
5769 | 0 | NULL, NULL); |
5770 | 0 | } |
5771 | 0 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
5772 | 0 | if (choiceOrInterleave == -1) |
5773 | 0 | choiceOrInterleave = 0; |
5774 | 0 | else if (choiceOrInterleave == 1) { |
5775 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
5776 | 0 | "<start> use both 'choice' and 'interleave'\n", |
5777 | 0 | NULL, NULL); |
5778 | 0 | } |
5779 | 0 | } else { |
5780 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
5781 | 0 | "<start> uses unknown combine value '%s''\n", |
5782 | 0 | combine, NULL); |
5783 | 0 | } |
5784 | 0 | xmlFree(combine); |
5785 | 0 | } else { |
5786 | 0 | if (missing == 0) |
5787 | 0 | missing = 1; |
5788 | 0 | else { |
5789 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
5790 | 0 | "Some <start> element miss the combine attribute\n", |
5791 | 0 | NULL, NULL); |
5792 | 0 | } |
5793 | 0 | } |
5794 | |
|
5795 | 0 | cur = cur->next; |
5796 | 0 | } |
5797 | 0 | if (choiceOrInterleave == -1) |
5798 | 0 | choiceOrInterleave = 0; |
5799 | 0 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
5800 | 0 | if (cur == NULL) |
5801 | 0 | return; |
5802 | 0 | if (choiceOrInterleave == 0) |
5803 | 0 | cur->type = XML_RELAXNG_INTERLEAVE; |
5804 | 0 | else |
5805 | 0 | cur->type = XML_RELAXNG_CHOICE; |
5806 | 0 | cur->content = grammar->start; |
5807 | 0 | grammar->start = cur; |
5808 | 0 | if (choiceOrInterleave == 0) { |
5809 | 0 | if (ctxt->interleaves == NULL) |
5810 | 0 | ctxt->interleaves = xmlHashCreate(10); |
5811 | 0 | if (ctxt->interleaves == NULL) { |
5812 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5813 | 0 | "Failed to create interleaves hash table\n", NULL, |
5814 | 0 | NULL); |
5815 | 0 | } else { |
5816 | 0 | char tmpname[32]; |
5817 | |
|
5818 | 0 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
5819 | 0 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
5820 | 0 | 0) { |
5821 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
5822 | 0 | "Failed to add %s to hash table\n", |
5823 | 0 | (const xmlChar *) tmpname, NULL); |
5824 | 0 | } |
5825 | 0 | } |
5826 | 0 | } |
5827 | 0 | } |
5828 | | |
5829 | | /** |
5830 | | * xmlRelaxNGCheckCycles: |
5831 | | * @ctxt: a Relax-NG parser context |
5832 | | * @nodes: grammar children nodes |
5833 | | * @depth: the counter |
5834 | | * |
5835 | | * Check for cycles. |
5836 | | * |
5837 | | * Returns 0 if check passed, and -1 in case of error |
5838 | | */ |
5839 | | static int |
5840 | | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
5841 | | xmlRelaxNGDefinePtr cur, int depth) |
5842 | 0 | { |
5843 | 0 | int ret = 0; |
5844 | |
|
5845 | 0 | while ((ret == 0) && (cur != NULL)) { |
5846 | 0 | if ((cur->type == XML_RELAXNG_REF) || |
5847 | 0 | (cur->type == XML_RELAXNG_PARENTREF)) { |
5848 | 0 | if (cur->depth == -1) { |
5849 | 0 | cur->depth = depth; |
5850 | 0 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
5851 | 0 | cur->depth = -2; |
5852 | 0 | } else if (depth == cur->depth) { |
5853 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
5854 | 0 | "Detected a cycle in %s references\n", |
5855 | 0 | cur->name, NULL); |
5856 | 0 | return (-1); |
5857 | 0 | } |
5858 | 0 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
5859 | 0 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
5860 | 0 | } else { |
5861 | 0 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
5862 | 0 | } |
5863 | 0 | cur = cur->next; |
5864 | 0 | } |
5865 | 0 | return (ret); |
5866 | 0 | } |
5867 | | |
5868 | | /** |
5869 | | * xmlRelaxNGTryUnlink: |
5870 | | * @ctxt: a Relax-NG parser context |
5871 | | * @cur: the definition to unlink |
5872 | | * @parent: the parent definition |
5873 | | * @prev: the previous sibling definition |
5874 | | * |
5875 | | * Try to unlink a definition. If not possible make it a NOOP |
5876 | | * |
5877 | | * Returns the new prev definition |
5878 | | */ |
5879 | | static xmlRelaxNGDefinePtr |
5880 | | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
5881 | | xmlRelaxNGDefinePtr cur, |
5882 | | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
5883 | 0 | { |
5884 | 0 | if (prev != NULL) { |
5885 | 0 | prev->next = cur->next; |
5886 | 0 | } else { |
5887 | 0 | if (parent != NULL) { |
5888 | 0 | if (parent->content == cur) |
5889 | 0 | parent->content = cur->next; |
5890 | 0 | else if (parent->attrs == cur) |
5891 | 0 | parent->attrs = cur->next; |
5892 | 0 | else if (parent->nameClass == cur) |
5893 | 0 | parent->nameClass = cur->next; |
5894 | 0 | } else { |
5895 | 0 | cur->type = XML_RELAXNG_NOOP; |
5896 | 0 | prev = cur; |
5897 | 0 | } |
5898 | 0 | } |
5899 | 0 | return (prev); |
5900 | 0 | } |
5901 | | |
5902 | | /** |
5903 | | * xmlRelaxNGSimplify: |
5904 | | * @ctxt: a Relax-NG parser context |
5905 | | * @nodes: grammar children nodes |
5906 | | * |
5907 | | * Check for simplification of empty and notAllowed |
5908 | | */ |
5909 | | static void |
5910 | | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
5911 | | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
5912 | 0 | { |
5913 | 0 | xmlRelaxNGDefinePtr prev = NULL; |
5914 | |
|
5915 | 0 | while (cur != NULL) { |
5916 | 0 | if ((cur->type == XML_RELAXNG_REF) || |
5917 | 0 | (cur->type == XML_RELAXNG_PARENTREF)) { |
5918 | 0 | if (cur->depth != -3) { |
5919 | 0 | cur->depth = -3; |
5920 | 0 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
5921 | 0 | } |
5922 | 0 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
5923 | 0 | cur->parent = parent; |
5924 | 0 | if ((parent != NULL) && |
5925 | 0 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
5926 | 0 | (parent->type == XML_RELAXNG_LIST) || |
5927 | 0 | (parent->type == XML_RELAXNG_GROUP) || |
5928 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
5929 | 0 | (parent->type == XML_RELAXNG_ONEORMORE) || |
5930 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
5931 | 0 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
5932 | 0 | break; |
5933 | 0 | } |
5934 | 0 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
5935 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5936 | 0 | } else |
5937 | 0 | prev = cur; |
5938 | 0 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
5939 | 0 | cur->parent = parent; |
5940 | 0 | if ((parent != NULL) && |
5941 | 0 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
5942 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
5943 | 0 | parent->type = XML_RELAXNG_EMPTY; |
5944 | 0 | break; |
5945 | 0 | } |
5946 | 0 | if ((parent != NULL) && |
5947 | 0 | ((parent->type == XML_RELAXNG_GROUP) || |
5948 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
5949 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
5950 | 0 | } else |
5951 | 0 | prev = cur; |
5952 | 0 | } else { |
5953 | 0 | cur->parent = parent; |
5954 | 0 | if (cur->content != NULL) |
5955 | 0 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
5956 | 0 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
5957 | 0 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
5958 | 0 | if (cur->nameClass != NULL) |
5959 | 0 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
5960 | | /* |
5961 | | * On Elements, try to move attribute only generating rules on |
5962 | | * the attrs rules. |
5963 | | */ |
5964 | 0 | if (cur->type == XML_RELAXNG_ELEMENT) { |
5965 | 0 | int attronly; |
5966 | 0 | xmlRelaxNGDefinePtr tmp, pre; |
5967 | |
|
5968 | 0 | while (cur->content != NULL) { |
5969 | 0 | attronly = |
5970 | 0 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
5971 | 0 | if (attronly == 1) { |
5972 | | /* |
5973 | | * migrate cur->content to attrs |
5974 | | */ |
5975 | 0 | tmp = cur->content; |
5976 | 0 | cur->content = tmp->next; |
5977 | 0 | tmp->next = cur->attrs; |
5978 | 0 | cur->attrs = tmp; |
5979 | 0 | } else { |
5980 | | /* |
5981 | | * cur->content can generate elements or text |
5982 | | */ |
5983 | 0 | break; |
5984 | 0 | } |
5985 | 0 | } |
5986 | 0 | pre = cur->content; |
5987 | 0 | while ((pre != NULL) && (pre->next != NULL)) { |
5988 | 0 | tmp = pre->next; |
5989 | 0 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
5990 | 0 | if (attronly == 1) { |
5991 | | /* |
5992 | | * migrate tmp to attrs |
5993 | | */ |
5994 | 0 | pre->next = tmp->next; |
5995 | 0 | tmp->next = cur->attrs; |
5996 | 0 | cur->attrs = tmp; |
5997 | 0 | } else { |
5998 | 0 | pre = tmp; |
5999 | 0 | } |
6000 | 0 | } |
6001 | 0 | } |
6002 | | /* |
6003 | | * This may result in a simplification |
6004 | | */ |
6005 | 0 | if ((cur->type == XML_RELAXNG_GROUP) || |
6006 | 0 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
6007 | 0 | if (cur->content == NULL) |
6008 | 0 | cur->type = XML_RELAXNG_EMPTY; |
6009 | 0 | else if (cur->content->next == NULL) { |
6010 | 0 | if ((parent == NULL) && (prev == NULL)) { |
6011 | 0 | cur->type = XML_RELAXNG_NOOP; |
6012 | 0 | } else if (prev == NULL) { |
6013 | 0 | parent->content = cur->content; |
6014 | 0 | cur->content->next = cur->next; |
6015 | 0 | cur = cur->content; |
6016 | 0 | } else { |
6017 | 0 | cur->content->next = cur->next; |
6018 | 0 | prev->next = cur->content; |
6019 | 0 | cur = cur->content; |
6020 | 0 | } |
6021 | 0 | } |
6022 | 0 | } |
6023 | | /* |
6024 | | * the current node may have been transformed back |
6025 | | */ |
6026 | 0 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
6027 | 0 | (cur->content != NULL) && |
6028 | 0 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
6029 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
6030 | 0 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
6031 | 0 | if ((parent != NULL) && |
6032 | 0 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
6033 | 0 | (parent->type == XML_RELAXNG_LIST) || |
6034 | 0 | (parent->type == XML_RELAXNG_GROUP) || |
6035 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
6036 | 0 | (parent->type == XML_RELAXNG_ONEORMORE) || |
6037 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
6038 | 0 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
6039 | 0 | break; |
6040 | 0 | } |
6041 | 0 | if ((parent != NULL) && |
6042 | 0 | (parent->type == XML_RELAXNG_CHOICE)) { |
6043 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
6044 | 0 | } else |
6045 | 0 | prev = cur; |
6046 | 0 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
6047 | 0 | if ((parent != NULL) && |
6048 | 0 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
6049 | 0 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
6050 | 0 | parent->type = XML_RELAXNG_EMPTY; |
6051 | 0 | break; |
6052 | 0 | } |
6053 | 0 | if ((parent != NULL) && |
6054 | 0 | ((parent->type == XML_RELAXNG_GROUP) || |
6055 | 0 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
6056 | 0 | (parent->type == XML_RELAXNG_CHOICE))) { |
6057 | 0 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
6058 | 0 | } else |
6059 | 0 | prev = cur; |
6060 | 0 | } else { |
6061 | 0 | prev = cur; |
6062 | 0 | } |
6063 | 0 | } |
6064 | 0 | cur = cur->next; |
6065 | 0 | } |
6066 | 0 | } |
6067 | | |
6068 | | /** |
6069 | | * xmlRelaxNGGroupContentType: |
6070 | | * @ct1: the first content type |
6071 | | * @ct2: the second content type |
6072 | | * |
6073 | | * Try to group 2 content types |
6074 | | * |
6075 | | * Returns the content type |
6076 | | */ |
6077 | | static xmlRelaxNGContentType |
6078 | | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
6079 | | xmlRelaxNGContentType ct2) |
6080 | 0 | { |
6081 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
6082 | 0 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
6083 | 0 | return (XML_RELAXNG_CONTENT_ERROR); |
6084 | 0 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
6085 | 0 | return (ct2); |
6086 | 0 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
6087 | 0 | return (ct1); |
6088 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
6089 | 0 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
6090 | 0 | return (XML_RELAXNG_CONTENT_COMPLEX); |
6091 | 0 | return (XML_RELAXNG_CONTENT_ERROR); |
6092 | 0 | } |
6093 | | |
6094 | | /** |
6095 | | * xmlRelaxNGMaxContentType: |
6096 | | * @ct1: the first content type |
6097 | | * @ct2: the second content type |
6098 | | * |
6099 | | * Compute the max content-type |
6100 | | * |
6101 | | * Returns the content type |
6102 | | */ |
6103 | | static xmlRelaxNGContentType |
6104 | | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
6105 | | xmlRelaxNGContentType ct2) |
6106 | 0 | { |
6107 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
6108 | 0 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
6109 | 0 | return (XML_RELAXNG_CONTENT_ERROR); |
6110 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
6111 | 0 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
6112 | 0 | return (XML_RELAXNG_CONTENT_SIMPLE); |
6113 | 0 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
6114 | 0 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
6115 | 0 | return (XML_RELAXNG_CONTENT_COMPLEX); |
6116 | 0 | return (XML_RELAXNG_CONTENT_EMPTY); |
6117 | 0 | } |
6118 | | |
6119 | | /** |
6120 | | * xmlRelaxNGCheckRules: |
6121 | | * @ctxt: a Relax-NG parser context |
6122 | | * @cur: the current definition |
6123 | | * @flags: some accumulated flags |
6124 | | * @ptype: the parent type |
6125 | | * |
6126 | | * Check for rules in section 7.1 and 7.2 |
6127 | | * |
6128 | | * Returns the content type of @cur |
6129 | | */ |
6130 | | static xmlRelaxNGContentType |
6131 | | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
6132 | | xmlRelaxNGDefinePtr cur, int flags, |
6133 | | xmlRelaxNGType ptype) |
6134 | 0 | { |
6135 | 0 | int nflags; |
6136 | 0 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
6137 | |
|
6138 | 0 | while (cur != NULL) { |
6139 | 0 | ret = XML_RELAXNG_CONTENT_EMPTY; |
6140 | 0 | if ((cur->type == XML_RELAXNG_REF) || |
6141 | 0 | (cur->type == XML_RELAXNG_PARENTREF)) { |
6142 | | /* |
6143 | | * This should actually be caught by list//element(ref) at the |
6144 | | * element boundaries, c.f. Bug #159968 local refs are dropped |
6145 | | * in step 4.19. |
6146 | | */ |
6147 | | #if 0 |
6148 | | if (flags & XML_RELAXNG_IN_LIST) { |
6149 | | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
6150 | | "Found forbidden pattern list//ref\n", NULL, |
6151 | | NULL); |
6152 | | } |
6153 | | #endif |
6154 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6155 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
6156 | 0 | "Found forbidden pattern data/except//ref\n", |
6157 | 0 | NULL, NULL); |
6158 | 0 | } |
6159 | 0 | if (cur->content == NULL) { |
6160 | 0 | if (cur->type == XML_RELAXNG_PARENTREF) |
6161 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
6162 | 0 | "Internal found no define for parent refs\n", |
6163 | 0 | NULL, NULL); |
6164 | 0 | else |
6165 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
6166 | 0 | "Internal found no define for ref %s\n", |
6167 | 0 | (cur->name ? cur->name: BAD_CAST "null"), NULL); |
6168 | 0 | } |
6169 | 0 | if (cur->depth > -4) { |
6170 | 0 | cur->depth = -4; |
6171 | 0 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
6172 | 0 | flags, cur->type); |
6173 | 0 | cur->depth = ret - 15; |
6174 | 0 | } else if (cur->depth == -4) { |
6175 | 0 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
6176 | 0 | } else { |
6177 | 0 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
6178 | 0 | } |
6179 | 0 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
6180 | | /* |
6181 | | * The 7.3 Attribute derivation rule for groups is plugged there |
6182 | | */ |
6183 | 0 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
6184 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6185 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
6186 | 0 | "Found forbidden pattern data/except//element(ref)\n", |
6187 | 0 | NULL, NULL); |
6188 | 0 | } |
6189 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6190 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
6191 | 0 | "Found forbidden pattern list//element(ref)\n", |
6192 | 0 | NULL, NULL); |
6193 | 0 | } |
6194 | 0 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
6195 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
6196 | 0 | "Found forbidden pattern attribute//element(ref)\n", |
6197 | 0 | NULL, NULL); |
6198 | 0 | } |
6199 | 0 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
6200 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
6201 | 0 | "Found forbidden pattern attribute//element(ref)\n", |
6202 | 0 | NULL, NULL); |
6203 | 0 | } |
6204 | | /* |
6205 | | * reset since in the simple form elements are only child |
6206 | | * of grammar/define |
6207 | | */ |
6208 | 0 | nflags = 0; |
6209 | 0 | ret = |
6210 | 0 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
6211 | 0 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
6212 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
6213 | 0 | "Element %s attributes have a content type error\n", |
6214 | 0 | cur->name, NULL); |
6215 | 0 | } |
6216 | 0 | ret = |
6217 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6218 | 0 | cur->type); |
6219 | 0 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
6220 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
6221 | 0 | "Element %s has a content type error\n", |
6222 | 0 | cur->name, NULL); |
6223 | 0 | } else { |
6224 | 0 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
6225 | 0 | } |
6226 | 0 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
6227 | 0 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
6228 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
6229 | 0 | "Found forbidden pattern attribute//attribute\n", |
6230 | 0 | NULL, NULL); |
6231 | 0 | } |
6232 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6233 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
6234 | 0 | "Found forbidden pattern list//attribute\n", |
6235 | 0 | NULL, NULL); |
6236 | 0 | } |
6237 | 0 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
6238 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
6239 | 0 | "Found forbidden pattern oneOrMore//group//attribute\n", |
6240 | 0 | NULL, NULL); |
6241 | 0 | } |
6242 | 0 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
6243 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
6244 | 0 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
6245 | 0 | NULL, NULL); |
6246 | 0 | } |
6247 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6248 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
6249 | 0 | "Found forbidden pattern data/except//attribute\n", |
6250 | 0 | NULL, NULL); |
6251 | 0 | } |
6252 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6253 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
6254 | 0 | "Found forbidden pattern start//attribute\n", |
6255 | 0 | NULL, NULL); |
6256 | 0 | } |
6257 | 0 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
6258 | 0 | && cur->name == NULL |
6259 | | /* following is checking alternative name class readiness |
6260 | | in case it went the "choice" route */ |
6261 | 0 | && cur->nameClass == NULL) { |
6262 | 0 | if (cur->ns == NULL) { |
6263 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
6264 | 0 | "Found anyName attribute without oneOrMore ancestor\n", |
6265 | 0 | NULL, NULL); |
6266 | 0 | } else { |
6267 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
6268 | 0 | "Found nsName attribute without oneOrMore ancestor\n", |
6269 | 0 | NULL, NULL); |
6270 | 0 | } |
6271 | 0 | } |
6272 | 0 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
6273 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
6274 | 0 | ret = XML_RELAXNG_CONTENT_EMPTY; |
6275 | 0 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
6276 | 0 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
6277 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6278 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
6279 | 0 | "Found forbidden pattern data/except//oneOrMore\n", |
6280 | 0 | NULL, NULL); |
6281 | 0 | } |
6282 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6283 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
6284 | 0 | "Found forbidden pattern start//oneOrMore\n", |
6285 | 0 | NULL, NULL); |
6286 | 0 | } |
6287 | 0 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
6288 | 0 | ret = |
6289 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6290 | 0 | cur->type); |
6291 | 0 | ret = xmlRelaxNGGroupContentType(ret, ret); |
6292 | 0 | } else if (cur->type == XML_RELAXNG_LIST) { |
6293 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6294 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
6295 | 0 | "Found forbidden pattern list//list\n", NULL, |
6296 | 0 | NULL); |
6297 | 0 | } |
6298 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6299 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
6300 | 0 | "Found forbidden pattern data/except//list\n", |
6301 | 0 | NULL, NULL); |
6302 | 0 | } |
6303 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6304 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
6305 | 0 | "Found forbidden pattern start//list\n", NULL, |
6306 | 0 | NULL); |
6307 | 0 | } |
6308 | 0 | nflags = flags | XML_RELAXNG_IN_LIST; |
6309 | 0 | ret = |
6310 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6311 | 0 | cur->type); |
6312 | 0 | } else if (cur->type == XML_RELAXNG_GROUP) { |
6313 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6314 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
6315 | 0 | "Found forbidden pattern data/except//group\n", |
6316 | 0 | NULL, NULL); |
6317 | 0 | } |
6318 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6319 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
6320 | 0 | "Found forbidden pattern start//group\n", NULL, |
6321 | 0 | NULL); |
6322 | 0 | } |
6323 | 0 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
6324 | 0 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
6325 | 0 | else |
6326 | 0 | nflags = flags; |
6327 | 0 | ret = |
6328 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6329 | 0 | cur->type); |
6330 | | /* |
6331 | | * The 7.3 Attribute derivation rule for groups is plugged there |
6332 | | */ |
6333 | 0 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
6334 | 0 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
6335 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6336 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
6337 | 0 | "Found forbidden pattern list//interleave\n", |
6338 | 0 | NULL, NULL); |
6339 | 0 | } |
6340 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6341 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
6342 | 0 | "Found forbidden pattern data/except//interleave\n", |
6343 | 0 | NULL, NULL); |
6344 | 0 | } |
6345 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6346 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
6347 | 0 | "Found forbidden pattern start//interleave\n", |
6348 | 0 | NULL, NULL); |
6349 | 0 | } |
6350 | 0 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
6351 | 0 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
6352 | 0 | else |
6353 | 0 | nflags = flags; |
6354 | 0 | ret = |
6355 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6356 | 0 | cur->type); |
6357 | 0 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
6358 | 0 | if ((cur->parent != NULL) && |
6359 | 0 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
6360 | 0 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
6361 | 0 | else |
6362 | 0 | nflags = flags; |
6363 | 0 | ret = |
6364 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
6365 | 0 | cur->type); |
6366 | 0 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
6367 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6368 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
6369 | 0 | "Found forbidden pattern start//data\n", NULL, |
6370 | 0 | NULL); |
6371 | 0 | } |
6372 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6373 | 0 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
6374 | 0 | } else if (cur->type == XML_RELAXNG_VALUE) { |
6375 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6376 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
6377 | 0 | "Found forbidden pattern start//value\n", NULL, |
6378 | 0 | NULL); |
6379 | 0 | } |
6380 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6381 | 0 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
6382 | 0 | } else if (cur->type == XML_RELAXNG_TEXT) { |
6383 | 0 | if (flags & XML_RELAXNG_IN_LIST) { |
6384 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
6385 | 0 | "Found forbidden pattern list//text\n", NULL, |
6386 | 0 | NULL); |
6387 | 0 | } |
6388 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6389 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
6390 | 0 | "Found forbidden pattern data/except//text\n", |
6391 | 0 | NULL, NULL); |
6392 | 0 | } |
6393 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6394 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
6395 | 0 | "Found forbidden pattern start//text\n", NULL, |
6396 | 0 | NULL); |
6397 | 0 | } |
6398 | 0 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
6399 | 0 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
6400 | 0 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
6401 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
6402 | 0 | "Found forbidden pattern data/except//empty\n", |
6403 | 0 | NULL, NULL); |
6404 | 0 | } |
6405 | 0 | if (flags & XML_RELAXNG_IN_START) { |
6406 | 0 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
6407 | 0 | "Found forbidden pattern start//empty\n", NULL, |
6408 | 0 | NULL); |
6409 | 0 | } |
6410 | 0 | ret = XML_RELAXNG_CONTENT_EMPTY; |
6411 | 0 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
6412 | 0 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
6413 | 0 | ret = |
6414 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6415 | 0 | } else { |
6416 | 0 | ret = |
6417 | 0 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
6418 | 0 | } |
6419 | 0 | cur = cur->next; |
6420 | 0 | if (ptype == XML_RELAXNG_GROUP) { |
6421 | 0 | val = xmlRelaxNGGroupContentType(val, ret); |
6422 | 0 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
6423 | | /* |
6424 | | * TODO: scan complain that tmp is never used, seems on purpose |
6425 | | * need double-checking |
6426 | | */ |
6427 | 0 | tmp = xmlRelaxNGGroupContentType(val, ret); |
6428 | 0 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
6429 | 0 | tmp = xmlRelaxNGMaxContentType(val, ret); |
6430 | 0 | } else if (ptype == XML_RELAXNG_CHOICE) { |
6431 | 0 | val = xmlRelaxNGMaxContentType(val, ret); |
6432 | 0 | } else if (ptype == XML_RELAXNG_LIST) { |
6433 | 0 | val = XML_RELAXNG_CONTENT_SIMPLE; |
6434 | 0 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
6435 | 0 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
6436 | 0 | val = XML_RELAXNG_CONTENT_ERROR; |
6437 | 0 | else |
6438 | 0 | val = XML_RELAXNG_CONTENT_SIMPLE; |
6439 | 0 | } else { |
6440 | 0 | val = xmlRelaxNGGroupContentType(val, ret); |
6441 | 0 | } |
6442 | |
|
6443 | 0 | } |
6444 | 0 | return (val); |
6445 | 0 | } |
6446 | | |
6447 | | /** |
6448 | | * xmlRelaxNGParseGrammar: |
6449 | | * @ctxt: a Relax-NG parser context |
6450 | | * @nodes: grammar children nodes |
6451 | | * |
6452 | | * parse a Relax-NG <grammar> node |
6453 | | * |
6454 | | * Returns the internal xmlRelaxNGGrammarPtr built or |
6455 | | * NULL in case of error |
6456 | | */ |
6457 | | static xmlRelaxNGGrammarPtr |
6458 | | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
6459 | 0 | { |
6460 | 0 | xmlRelaxNGGrammarPtr ret, tmp, old; |
6461 | |
|
6462 | 0 | ret = xmlRelaxNGNewGrammar(ctxt); |
6463 | 0 | if (ret == NULL) |
6464 | 0 | return (NULL); |
6465 | | |
6466 | | /* |
6467 | | * Link the new grammar in the tree |
6468 | | */ |
6469 | 0 | ret->parent = ctxt->grammar; |
6470 | 0 | if (ctxt->grammar != NULL) { |
6471 | 0 | tmp = ctxt->grammar->children; |
6472 | 0 | if (tmp == NULL) { |
6473 | 0 | ctxt->grammar->children = ret; |
6474 | 0 | } else { |
6475 | 0 | while (tmp->next != NULL) |
6476 | 0 | tmp = tmp->next; |
6477 | 0 | tmp->next = ret; |
6478 | 0 | } |
6479 | 0 | } |
6480 | |
|
6481 | 0 | old = ctxt->grammar; |
6482 | 0 | ctxt->grammar = ret; |
6483 | 0 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
6484 | 0 | ctxt->grammar = ret; |
6485 | 0 | if (ctxt->grammar == NULL) { |
6486 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
6487 | 0 | "Failed to parse <grammar> content\n", NULL, NULL); |
6488 | 0 | } else if (ctxt->grammar->start == NULL) { |
6489 | 0 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
6490 | 0 | "Element <grammar> has no <start>\n", NULL, NULL); |
6491 | 0 | } |
6492 | | |
6493 | | /* |
6494 | | * Apply 4.17 merging rules to defines and starts |
6495 | | */ |
6496 | 0 | xmlRelaxNGCombineStart(ctxt, ret); |
6497 | 0 | if (ret->defs != NULL) { |
6498 | 0 | xmlHashScan(ret->defs, xmlRelaxNGCheckCombine, ctxt); |
6499 | 0 | } |
6500 | | |
6501 | | /* |
6502 | | * link together defines and refs in this grammar |
6503 | | */ |
6504 | 0 | if (ret->refs != NULL) { |
6505 | 0 | xmlHashScan(ret->refs, xmlRelaxNGCheckReference, ctxt); |
6506 | 0 | } |
6507 | | |
6508 | | |
6509 | | /* @@@@ */ |
6510 | |
|
6511 | 0 | ctxt->grammar = old; |
6512 | 0 | return (ret); |
6513 | 0 | } |
6514 | | |
6515 | | /** |
6516 | | * xmlRelaxNGParseDocument: |
6517 | | * @ctxt: a Relax-NG parser context |
6518 | | * @node: the root node of the RelaxNG schema |
6519 | | * |
6520 | | * parse a Relax-NG definition resource and build an internal |
6521 | | * xmlRelaxNG structure which can be used to validate instances. |
6522 | | * |
6523 | | * Returns the internal XML RelaxNG structure built or |
6524 | | * NULL in case of error |
6525 | | */ |
6526 | | static xmlRelaxNGPtr |
6527 | | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
6528 | 0 | { |
6529 | 0 | xmlRelaxNGPtr schema = NULL; |
6530 | 0 | const xmlChar *olddefine; |
6531 | 0 | xmlRelaxNGGrammarPtr old; |
6532 | |
|
6533 | 0 | if ((ctxt == NULL) || (node == NULL)) |
6534 | 0 | return (NULL); |
6535 | | |
6536 | 0 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
6537 | 0 | if (schema == NULL) |
6538 | 0 | return (NULL); |
6539 | | |
6540 | 0 | olddefine = ctxt->define; |
6541 | 0 | ctxt->define = NULL; |
6542 | 0 | if (IS_RELAXNG(node, "grammar")) { |
6543 | 0 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
6544 | 0 | if (schema->topgrammar == NULL) { |
6545 | 0 | xmlRelaxNGFree(schema); |
6546 | 0 | return (NULL); |
6547 | 0 | } |
6548 | 0 | } else { |
6549 | 0 | xmlRelaxNGGrammarPtr tmp, ret; |
6550 | |
|
6551 | 0 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
6552 | 0 | if (schema->topgrammar == NULL) { |
6553 | 0 | xmlRelaxNGFree(schema); |
6554 | 0 | return (NULL); |
6555 | 0 | } |
6556 | | /* |
6557 | | * Link the new grammar in the tree |
6558 | | */ |
6559 | 0 | ret->parent = ctxt->grammar; |
6560 | 0 | if (ctxt->grammar != NULL) { |
6561 | 0 | tmp = ctxt->grammar->children; |
6562 | 0 | if (tmp == NULL) { |
6563 | 0 | ctxt->grammar->children = ret; |
6564 | 0 | } else { |
6565 | 0 | while (tmp->next != NULL) |
6566 | 0 | tmp = tmp->next; |
6567 | 0 | tmp->next = ret; |
6568 | 0 | } |
6569 | 0 | } |
6570 | 0 | old = ctxt->grammar; |
6571 | 0 | ctxt->grammar = ret; |
6572 | 0 | xmlRelaxNGParseStart(ctxt, node); |
6573 | 0 | if (old != NULL) |
6574 | 0 | ctxt->grammar = old; |
6575 | 0 | } |
6576 | 0 | ctxt->define = olddefine; |
6577 | 0 | if (schema->topgrammar->start != NULL) { |
6578 | 0 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
6579 | 0 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
6580 | 0 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
6581 | 0 | while ((schema->topgrammar->start != NULL) && |
6582 | 0 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
6583 | 0 | (schema->topgrammar->start->next != NULL)) |
6584 | 0 | schema->topgrammar->start = |
6585 | 0 | schema->topgrammar->start->content; |
6586 | 0 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
6587 | 0 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
6588 | 0 | } |
6589 | 0 | } |
6590 | |
|
6591 | 0 | return (schema); |
6592 | 0 | } |
6593 | | |
6594 | | /************************************************************************ |
6595 | | * * |
6596 | | * Reading RelaxNGs * |
6597 | | * * |
6598 | | ************************************************************************/ |
6599 | | |
6600 | | /** |
6601 | | * xmlRelaxNGNewParserCtxt: |
6602 | | * @URL: the location of the schema |
6603 | | * |
6604 | | * Create an XML RelaxNGs parse context for that file/resource expected |
6605 | | * to contain an XML RelaxNGs file. |
6606 | | * |
6607 | | * Returns the parser context or NULL in case of error |
6608 | | */ |
6609 | | xmlRelaxNGParserCtxtPtr |
6610 | | xmlRelaxNGNewParserCtxt(const char *URL) |
6611 | 0 | { |
6612 | 0 | xmlRelaxNGParserCtxtPtr ret; |
6613 | |
|
6614 | 0 | if (URL == NULL) |
6615 | 0 | return (NULL); |
6616 | | |
6617 | 0 | ret = |
6618 | 0 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
6619 | 0 | if (ret == NULL) { |
6620 | 0 | xmlRngPErrMemory(NULL, "building parser\n"); |
6621 | 0 | return (NULL); |
6622 | 0 | } |
6623 | 0 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
6624 | 0 | ret->URL = xmlStrdup((const xmlChar *) URL); |
6625 | 0 | ret->error = xmlGenericError; |
6626 | 0 | ret->userData = xmlGenericErrorContext; |
6627 | 0 | return (ret); |
6628 | 0 | } |
6629 | | |
6630 | | /** |
6631 | | * xmlRelaxNGNewMemParserCtxt: |
6632 | | * @buffer: a pointer to a char array containing the schemas |
6633 | | * @size: the size of the array |
6634 | | * |
6635 | | * Create an XML RelaxNGs parse context for that memory buffer expected |
6636 | | * to contain an XML RelaxNGs file. |
6637 | | * |
6638 | | * Returns the parser context or NULL in case of error |
6639 | | */ |
6640 | | xmlRelaxNGParserCtxtPtr |
6641 | | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
6642 | 0 | { |
6643 | 0 | xmlRelaxNGParserCtxtPtr ret; |
6644 | |
|
6645 | 0 | if ((buffer == NULL) || (size <= 0)) |
6646 | 0 | return (NULL); |
6647 | | |
6648 | 0 | ret = |
6649 | 0 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
6650 | 0 | if (ret == NULL) { |
6651 | 0 | xmlRngPErrMemory(NULL, "building parser\n"); |
6652 | 0 | return (NULL); |
6653 | 0 | } |
6654 | 0 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
6655 | 0 | ret->buffer = buffer; |
6656 | 0 | ret->size = size; |
6657 | 0 | ret->error = xmlGenericError; |
6658 | 0 | ret->userData = xmlGenericErrorContext; |
6659 | 0 | return (ret); |
6660 | 0 | } |
6661 | | |
6662 | | /** |
6663 | | * xmlRelaxNGNewDocParserCtxt: |
6664 | | * @doc: a preparsed document tree |
6665 | | * |
6666 | | * Create an XML RelaxNGs parser context for that document. |
6667 | | * Note: since the process of compiling a RelaxNG schemas modifies the |
6668 | | * document, the @doc parameter is duplicated internally. |
6669 | | * |
6670 | | * Returns the parser context or NULL in case of error |
6671 | | */ |
6672 | | xmlRelaxNGParserCtxtPtr |
6673 | | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
6674 | 0 | { |
6675 | 0 | xmlRelaxNGParserCtxtPtr ret; |
6676 | 0 | xmlDocPtr copy; |
6677 | |
|
6678 | 0 | if (doc == NULL) |
6679 | 0 | return (NULL); |
6680 | 0 | copy = xmlCopyDoc(doc, 1); |
6681 | 0 | if (copy == NULL) |
6682 | 0 | return (NULL); |
6683 | | |
6684 | 0 | ret = |
6685 | 0 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
6686 | 0 | if (ret == NULL) { |
6687 | 0 | xmlRngPErrMemory(NULL, "building parser\n"); |
6688 | 0 | xmlFreeDoc(copy); |
6689 | 0 | return (NULL); |
6690 | 0 | } |
6691 | 0 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
6692 | 0 | ret->document = copy; |
6693 | 0 | ret->freedoc = 1; |
6694 | 0 | ret->userData = xmlGenericErrorContext; |
6695 | 0 | return (ret); |
6696 | 0 | } |
6697 | | |
6698 | | /** |
6699 | | * xmlRelaxNGFreeParserCtxt: |
6700 | | * @ctxt: the schema parser context |
6701 | | * |
6702 | | * Free the resources associated to the schema parser context |
6703 | | */ |
6704 | | void |
6705 | | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
6706 | 0 | { |
6707 | 0 | if (ctxt == NULL) |
6708 | 0 | return; |
6709 | 0 | if (ctxt->URL != NULL) |
6710 | 0 | xmlFree(ctxt->URL); |
6711 | 0 | if (ctxt->doc != NULL) |
6712 | 0 | xmlRelaxNGFreeDocument(ctxt->doc); |
6713 | 0 | if (ctxt->interleaves != NULL) |
6714 | 0 | xmlHashFree(ctxt->interleaves, NULL); |
6715 | 0 | if (ctxt->documents != NULL) |
6716 | 0 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
6717 | 0 | if (ctxt->includes != NULL) |
6718 | 0 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
6719 | 0 | if (ctxt->docTab != NULL) |
6720 | 0 | xmlFree(ctxt->docTab); |
6721 | 0 | if (ctxt->incTab != NULL) |
6722 | 0 | xmlFree(ctxt->incTab); |
6723 | 0 | if (ctxt->defTab != NULL) { |
6724 | 0 | int i; |
6725 | |
|
6726 | 0 | for (i = 0; i < ctxt->defNr; i++) |
6727 | 0 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
6728 | 0 | xmlFree(ctxt->defTab); |
6729 | 0 | } |
6730 | 0 | if ((ctxt->document != NULL) && (ctxt->freedoc)) |
6731 | 0 | xmlFreeDoc(ctxt->document); |
6732 | 0 | xmlFree(ctxt); |
6733 | 0 | } |
6734 | | |
6735 | | /** |
6736 | | * xmlRelaxNGNormExtSpace: |
6737 | | * @value: a value |
6738 | | * |
6739 | | * Removes the leading and ending spaces of the value |
6740 | | * The string is modified "in situ" |
6741 | | */ |
6742 | | static void |
6743 | | xmlRelaxNGNormExtSpace(xmlChar * value) |
6744 | 0 | { |
6745 | 0 | xmlChar *start = value; |
6746 | 0 | xmlChar *cur = value; |
6747 | |
|
6748 | 0 | if (value == NULL) |
6749 | 0 | return; |
6750 | | |
6751 | 0 | while (IS_BLANK_CH(*cur)) |
6752 | 0 | cur++; |
6753 | 0 | if (cur == start) { |
6754 | 0 | do { |
6755 | 0 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
6756 | 0 | cur++; |
6757 | 0 | if (*cur == 0) |
6758 | 0 | return; |
6759 | 0 | start = cur; |
6760 | 0 | while (IS_BLANK_CH(*cur)) |
6761 | 0 | cur++; |
6762 | 0 | if (*cur == 0) { |
6763 | 0 | *start = 0; |
6764 | 0 | return; |
6765 | 0 | } |
6766 | 0 | } while (1); |
6767 | 0 | } else { |
6768 | 0 | do { |
6769 | 0 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
6770 | 0 | *start++ = *cur++; |
6771 | 0 | if (*cur == 0) { |
6772 | 0 | *start = 0; |
6773 | 0 | return; |
6774 | 0 | } |
6775 | | /* don't try to normalize the inner spaces */ |
6776 | 0 | while (IS_BLANK_CH(*cur)) |
6777 | 0 | cur++; |
6778 | 0 | if (*cur == 0) { |
6779 | 0 | *start = 0; |
6780 | 0 | return; |
6781 | 0 | } |
6782 | 0 | *start++ = *cur++; |
6783 | 0 | } while (1); |
6784 | 0 | } |
6785 | 0 | } |
6786 | | |
6787 | | /** |
6788 | | * xmlRelaxNGCleanupAttributes: |
6789 | | * @ctxt: a Relax-NG parser context |
6790 | | * @node: a Relax-NG node |
6791 | | * |
6792 | | * Check all the attributes on the given node |
6793 | | */ |
6794 | | static void |
6795 | | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
6796 | 0 | { |
6797 | 0 | xmlAttrPtr cur, next; |
6798 | |
|
6799 | 0 | cur = node->properties; |
6800 | 0 | while (cur != NULL) { |
6801 | 0 | next = cur->next; |
6802 | 0 | if ((cur->ns == NULL) || |
6803 | 0 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
6804 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
6805 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
6806 | 0 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
6807 | 0 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
6808 | 0 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
6809 | 0 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
6810 | 0 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
6811 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6812 | 0 | "Attribute %s is not allowed on %s\n", |
6813 | 0 | cur->name, node->name); |
6814 | 0 | } |
6815 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
6816 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
6817 | 0 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
6818 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6819 | 0 | "Attribute %s is not allowed on %s\n", |
6820 | 0 | cur->name, node->name); |
6821 | 0 | } |
6822 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
6823 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
6824 | 0 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
6825 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6826 | 0 | "Attribute %s is not allowed on %s\n", |
6827 | 0 | cur->name, node->name); |
6828 | 0 | } |
6829 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
6830 | 0 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
6831 | 0 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
6832 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
6833 | 0 | "Attribute %s is not allowed on %s\n", |
6834 | 0 | cur->name, node->name); |
6835 | 0 | } |
6836 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
6837 | 0 | xmlChar *val; |
6838 | 0 | xmlURIPtr uri; |
6839 | |
|
6840 | 0 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
6841 | 0 | if (val != NULL) { |
6842 | 0 | if (val[0] != 0) { |
6843 | 0 | uri = xmlParseURI((const char *) val); |
6844 | 0 | if (uri == NULL) { |
6845 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
6846 | 0 | "Attribute %s contains invalid URI %s\n", |
6847 | 0 | cur->name, val); |
6848 | 0 | } else { |
6849 | 0 | if (uri->scheme == NULL) { |
6850 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
6851 | 0 | "Attribute %s URI %s is not absolute\n", |
6852 | 0 | cur->name, val); |
6853 | 0 | } |
6854 | 0 | if (uri->fragment != NULL) { |
6855 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
6856 | 0 | "Attribute %s URI %s has a fragment ID\n", |
6857 | 0 | cur->name, val); |
6858 | 0 | } |
6859 | 0 | xmlFreeURI(uri); |
6860 | 0 | } |
6861 | 0 | } |
6862 | 0 | xmlFree(val); |
6863 | 0 | } |
6864 | 0 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
6865 | 0 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
6866 | 0 | "Unknown attribute %s on %s\n", cur->name, |
6867 | 0 | node->name); |
6868 | 0 | } |
6869 | 0 | } |
6870 | 0 | cur = next; |
6871 | 0 | } |
6872 | 0 | } |
6873 | | |
6874 | | /** |
6875 | | * xmlRelaxNGCleanupTree: |
6876 | | * @ctxt: a Relax-NG parser context |
6877 | | * @root: an xmlNodePtr subtree |
6878 | | * |
6879 | | * Cleanup the subtree from unwanted nodes for parsing, resolve |
6880 | | * Include and externalRef lookups. |
6881 | | */ |
6882 | | static void |
6883 | | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
6884 | 0 | { |
6885 | 0 | xmlNodePtr cur, delete; |
6886 | |
|
6887 | 0 | delete = NULL; |
6888 | 0 | cur = root; |
6889 | 0 | while (cur != NULL) { |
6890 | 0 | if (delete != NULL) { |
6891 | 0 | xmlUnlinkNode(delete); |
6892 | 0 | xmlFreeNode(delete); |
6893 | 0 | delete = NULL; |
6894 | 0 | } |
6895 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
6896 | | /* |
6897 | | * Simplification 4.1. Annotations |
6898 | | */ |
6899 | 0 | if ((cur->ns == NULL) || |
6900 | 0 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
6901 | 0 | if ((cur->parent != NULL) && |
6902 | 0 | (cur->parent->type == XML_ELEMENT_NODE) && |
6903 | 0 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
6904 | 0 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
6905 | 0 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
6906 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
6907 | 0 | "element %s doesn't allow foreign elements\n", |
6908 | 0 | cur->parent->name, NULL); |
6909 | 0 | } |
6910 | 0 | delete = cur; |
6911 | 0 | goto skip_children; |
6912 | 0 | } else { |
6913 | 0 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
6914 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
6915 | 0 | xmlChar *href, *ns, *base, *URL; |
6916 | 0 | xmlRelaxNGDocumentPtr docu; |
6917 | 0 | xmlNodePtr tmp; |
6918 | 0 | xmlURIPtr uri; |
6919 | |
|
6920 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
6921 | 0 | if (ns == NULL) { |
6922 | 0 | tmp = cur->parent; |
6923 | 0 | while ((tmp != NULL) && |
6924 | 0 | (tmp->type == XML_ELEMENT_NODE)) { |
6925 | 0 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
6926 | 0 | if (ns != NULL) |
6927 | 0 | break; |
6928 | 0 | tmp = tmp->parent; |
6929 | 0 | } |
6930 | 0 | } |
6931 | 0 | href = xmlGetProp(cur, BAD_CAST "href"); |
6932 | 0 | if (href == NULL) { |
6933 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
6934 | 0 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
6935 | 0 | NULL, NULL); |
6936 | 0 | if (ns != NULL) |
6937 | 0 | xmlFree(ns); |
6938 | 0 | delete = cur; |
6939 | 0 | goto skip_children; |
6940 | 0 | } |
6941 | 0 | uri = xmlParseURI((const char *) href); |
6942 | 0 | if (uri == NULL) { |
6943 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6944 | 0 | "Incorrect URI for externalRef %s\n", |
6945 | 0 | href, NULL); |
6946 | 0 | if (ns != NULL) |
6947 | 0 | xmlFree(ns); |
6948 | 0 | if (href != NULL) |
6949 | 0 | xmlFree(href); |
6950 | 0 | delete = cur; |
6951 | 0 | goto skip_children; |
6952 | 0 | } |
6953 | 0 | if (uri->fragment != NULL) { |
6954 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6955 | 0 | "Fragment forbidden in URI for externalRef %s\n", |
6956 | 0 | href, NULL); |
6957 | 0 | if (ns != NULL) |
6958 | 0 | xmlFree(ns); |
6959 | 0 | xmlFreeURI(uri); |
6960 | 0 | if (href != NULL) |
6961 | 0 | xmlFree(href); |
6962 | 0 | delete = cur; |
6963 | 0 | goto skip_children; |
6964 | 0 | } |
6965 | 0 | xmlFreeURI(uri); |
6966 | 0 | base = xmlNodeGetBase(cur->doc, cur); |
6967 | 0 | URL = xmlBuildURI(href, base); |
6968 | 0 | if (URL == NULL) { |
6969 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
6970 | 0 | "Failed to compute URL for externalRef %s\n", |
6971 | 0 | href, NULL); |
6972 | 0 | if (ns != NULL) |
6973 | 0 | xmlFree(ns); |
6974 | 0 | if (href != NULL) |
6975 | 0 | xmlFree(href); |
6976 | 0 | if (base != NULL) |
6977 | 0 | xmlFree(base); |
6978 | 0 | delete = cur; |
6979 | 0 | goto skip_children; |
6980 | 0 | } |
6981 | 0 | if (href != NULL) |
6982 | 0 | xmlFree(href); |
6983 | 0 | if (base != NULL) |
6984 | 0 | xmlFree(base); |
6985 | 0 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
6986 | 0 | if (docu == NULL) { |
6987 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
6988 | 0 | "Failed to load externalRef %s\n", URL, |
6989 | 0 | NULL); |
6990 | 0 | if (ns != NULL) |
6991 | 0 | xmlFree(ns); |
6992 | 0 | xmlFree(URL); |
6993 | 0 | delete = cur; |
6994 | 0 | goto skip_children; |
6995 | 0 | } |
6996 | 0 | if (ns != NULL) |
6997 | 0 | xmlFree(ns); |
6998 | 0 | xmlFree(URL); |
6999 | 0 | cur->psvi = docu; |
7000 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
7001 | 0 | xmlChar *href, *ns, *base, *URL; |
7002 | 0 | xmlRelaxNGIncludePtr incl; |
7003 | 0 | xmlNodePtr tmp; |
7004 | |
|
7005 | 0 | href = xmlGetProp(cur, BAD_CAST "href"); |
7006 | 0 | if (href == NULL) { |
7007 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
7008 | 0 | "xmlRelaxNGParse: include has no href attribute\n", |
7009 | 0 | NULL, NULL); |
7010 | 0 | delete = cur; |
7011 | 0 | goto skip_children; |
7012 | 0 | } |
7013 | 0 | base = xmlNodeGetBase(cur->doc, cur); |
7014 | 0 | URL = xmlBuildURI(href, base); |
7015 | 0 | if (URL == NULL) { |
7016 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
7017 | 0 | "Failed to compute URL for include %s\n", |
7018 | 0 | href, NULL); |
7019 | 0 | if (href != NULL) |
7020 | 0 | xmlFree(href); |
7021 | 0 | if (base != NULL) |
7022 | 0 | xmlFree(base); |
7023 | 0 | delete = cur; |
7024 | 0 | goto skip_children; |
7025 | 0 | } |
7026 | 0 | if (href != NULL) |
7027 | 0 | xmlFree(href); |
7028 | 0 | if (base != NULL) |
7029 | 0 | xmlFree(base); |
7030 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
7031 | 0 | if (ns == NULL) { |
7032 | 0 | tmp = cur->parent; |
7033 | 0 | while ((tmp != NULL) && |
7034 | 0 | (tmp->type == XML_ELEMENT_NODE)) { |
7035 | 0 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
7036 | 0 | if (ns != NULL) |
7037 | 0 | break; |
7038 | 0 | tmp = tmp->parent; |
7039 | 0 | } |
7040 | 0 | } |
7041 | 0 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
7042 | 0 | if (ns != NULL) |
7043 | 0 | xmlFree(ns); |
7044 | 0 | if (incl == NULL) { |
7045 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
7046 | 0 | "Failed to load include %s\n", URL, |
7047 | 0 | NULL); |
7048 | 0 | xmlFree(URL); |
7049 | 0 | delete = cur; |
7050 | 0 | goto skip_children; |
7051 | 0 | } |
7052 | 0 | xmlFree(URL); |
7053 | 0 | cur->psvi = incl; |
7054 | 0 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
7055 | 0 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
7056 | 0 | { |
7057 | 0 | xmlChar *name, *ns; |
7058 | 0 | xmlNodePtr text = NULL; |
7059 | | |
7060 | | /* |
7061 | | * Simplification 4.8. name attribute of element |
7062 | | * and attribute elements |
7063 | | */ |
7064 | 0 | name = xmlGetProp(cur, BAD_CAST "name"); |
7065 | 0 | if (name != NULL) { |
7066 | 0 | if (cur->children == NULL) { |
7067 | 0 | text = |
7068 | 0 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
7069 | 0 | name); |
7070 | 0 | } else { |
7071 | 0 | xmlNodePtr node; |
7072 | |
|
7073 | 0 | node = xmlNewDocNode(cur->doc, cur->ns, |
7074 | 0 | BAD_CAST "name", NULL); |
7075 | 0 | if (node != NULL) { |
7076 | 0 | xmlAddPrevSibling(cur->children, node); |
7077 | 0 | text = xmlNewDocText(node->doc, name); |
7078 | 0 | xmlAddChild(node, text); |
7079 | 0 | text = node; |
7080 | 0 | } |
7081 | 0 | } |
7082 | 0 | if (text == NULL) { |
7083 | 0 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
7084 | 0 | "Failed to create a name %s element\n", |
7085 | 0 | name, NULL); |
7086 | 0 | } |
7087 | 0 | xmlUnsetProp(cur, BAD_CAST "name"); |
7088 | 0 | xmlFree(name); |
7089 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
7090 | 0 | if (ns != NULL) { |
7091 | 0 | if (text != NULL) { |
7092 | 0 | xmlSetProp(text, BAD_CAST "ns", ns); |
7093 | | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
7094 | 0 | } |
7095 | 0 | xmlFree(ns); |
7096 | 0 | } else if (xmlStrEqual(cur->name, |
7097 | 0 | BAD_CAST "attribute")) { |
7098 | 0 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
7099 | 0 | } |
7100 | 0 | } |
7101 | 0 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
7102 | 0 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
7103 | 0 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
7104 | | /* |
7105 | | * Simplification 4.8. name attribute of element |
7106 | | * and attribute elements |
7107 | | */ |
7108 | 0 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
7109 | 0 | xmlNodePtr node; |
7110 | 0 | xmlChar *ns = NULL; |
7111 | |
|
7112 | 0 | node = cur->parent; |
7113 | 0 | while ((node != NULL) && |
7114 | 0 | (node->type == XML_ELEMENT_NODE)) { |
7115 | 0 | ns = xmlGetProp(node, BAD_CAST "ns"); |
7116 | 0 | if (ns != NULL) { |
7117 | 0 | break; |
7118 | 0 | } |
7119 | 0 | node = node->parent; |
7120 | 0 | } |
7121 | 0 | if (ns == NULL) { |
7122 | 0 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
7123 | 0 | } else { |
7124 | 0 | xmlSetProp(cur, BAD_CAST "ns", ns); |
7125 | 0 | xmlFree(ns); |
7126 | 0 | } |
7127 | 0 | } |
7128 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
7129 | 0 | xmlChar *name, *local, *prefix; |
7130 | | |
7131 | | /* |
7132 | | * Simplification: 4.10. QNames |
7133 | | */ |
7134 | 0 | name = xmlNodeGetContent(cur); |
7135 | 0 | if (name != NULL) { |
7136 | 0 | local = xmlSplitQName2(name, &prefix); |
7137 | 0 | if (local != NULL) { |
7138 | 0 | xmlNsPtr ns; |
7139 | |
|
7140 | 0 | ns = xmlSearchNs(cur->doc, cur, prefix); |
7141 | 0 | if (ns == NULL) { |
7142 | 0 | xmlRngPErr(ctxt, cur, |
7143 | 0 | XML_RNGP_PREFIX_UNDEFINED, |
7144 | 0 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
7145 | 0 | prefix, NULL); |
7146 | 0 | } else { |
7147 | 0 | xmlSetProp(cur, BAD_CAST "ns", |
7148 | 0 | ns->href); |
7149 | 0 | xmlNodeSetContent(cur, local); |
7150 | 0 | } |
7151 | 0 | xmlFree(local); |
7152 | 0 | xmlFree(prefix); |
7153 | 0 | } |
7154 | 0 | xmlFree(name); |
7155 | 0 | } |
7156 | 0 | } |
7157 | | /* |
7158 | | * 4.16 |
7159 | | */ |
7160 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
7161 | 0 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
7162 | 0 | xmlRngPErr(ctxt, cur, |
7163 | 0 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
7164 | 0 | "Found nsName/except//nsName forbidden construct\n", |
7165 | 0 | NULL, NULL); |
7166 | 0 | } |
7167 | 0 | } |
7168 | 0 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
7169 | 0 | (cur != root)) { |
7170 | 0 | int oldflags = ctxt->flags; |
7171 | | |
7172 | | /* |
7173 | | * 4.16 |
7174 | | */ |
7175 | 0 | if ((cur->parent != NULL) && |
7176 | 0 | (xmlStrEqual |
7177 | 0 | (cur->parent->name, BAD_CAST "anyName"))) { |
7178 | 0 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
7179 | 0 | xmlRelaxNGCleanupTree(ctxt, cur); |
7180 | 0 | ctxt->flags = oldflags; |
7181 | 0 | goto skip_children; |
7182 | 0 | } else if ((cur->parent != NULL) && |
7183 | 0 | (xmlStrEqual |
7184 | 0 | (cur->parent->name, BAD_CAST "nsName"))) { |
7185 | 0 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
7186 | 0 | xmlRelaxNGCleanupTree(ctxt, cur); |
7187 | 0 | ctxt->flags = oldflags; |
7188 | 0 | goto skip_children; |
7189 | 0 | } |
7190 | 0 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
7191 | | /* |
7192 | | * 4.16 |
7193 | | */ |
7194 | 0 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
7195 | 0 | xmlRngPErr(ctxt, cur, |
7196 | 0 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
7197 | 0 | "Found anyName/except//anyName forbidden construct\n", |
7198 | 0 | NULL, NULL); |
7199 | 0 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
7200 | 0 | xmlRngPErr(ctxt, cur, |
7201 | 0 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
7202 | 0 | "Found nsName/except//anyName forbidden construct\n", |
7203 | 0 | NULL, NULL); |
7204 | 0 | } |
7205 | 0 | } |
7206 | | /* |
7207 | | * This is not an else since "include" is transformed |
7208 | | * into a div |
7209 | | */ |
7210 | 0 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
7211 | 0 | xmlChar *ns; |
7212 | 0 | xmlNodePtr child, ins, tmp; |
7213 | | |
7214 | | /* |
7215 | | * implements rule 4.11 |
7216 | | */ |
7217 | |
|
7218 | 0 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
7219 | |
|
7220 | 0 | child = cur->children; |
7221 | 0 | ins = cur; |
7222 | 0 | while (child != NULL) { |
7223 | 0 | if (ns != NULL) { |
7224 | 0 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
7225 | 0 | xmlSetProp(child, BAD_CAST "ns", ns); |
7226 | 0 | } |
7227 | 0 | } |
7228 | 0 | tmp = child->next; |
7229 | 0 | xmlUnlinkNode(child); |
7230 | 0 | ins = xmlAddNextSibling(ins, child); |
7231 | 0 | child = tmp; |
7232 | 0 | } |
7233 | 0 | if (ns != NULL) |
7234 | 0 | xmlFree(ns); |
7235 | | /* |
7236 | | * Since we are about to delete cur, if its nsDef is non-NULL we |
7237 | | * need to preserve it (it contains the ns definitions for the |
7238 | | * children we just moved). We'll just stick it on to the end |
7239 | | * of cur->parent's list, since it's never going to be re-serialized |
7240 | | * (bug 143738). |
7241 | | */ |
7242 | 0 | if ((cur->nsDef != NULL) && (cur->parent != NULL)) { |
7243 | 0 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
7244 | 0 | while (parDef->next != NULL) |
7245 | 0 | parDef = parDef->next; |
7246 | 0 | parDef->next = cur->nsDef; |
7247 | 0 | cur->nsDef = NULL; |
7248 | 0 | } |
7249 | 0 | delete = cur; |
7250 | 0 | goto skip_children; |
7251 | 0 | } |
7252 | 0 | } |
7253 | 0 | } |
7254 | | /* |
7255 | | * Simplification 4.2 whitespaces |
7256 | | */ |
7257 | 0 | else if ((cur->type == XML_TEXT_NODE) || |
7258 | 0 | (cur->type == XML_CDATA_SECTION_NODE)) { |
7259 | 0 | if (IS_BLANK_NODE(cur)) { |
7260 | 0 | if ((cur->parent != NULL) && |
7261 | 0 | (cur->parent->type == XML_ELEMENT_NODE)) { |
7262 | 0 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
7263 | 0 | && |
7264 | 0 | (!xmlStrEqual |
7265 | 0 | (cur->parent->name, BAD_CAST "param"))) |
7266 | 0 | delete = cur; |
7267 | 0 | } else { |
7268 | 0 | delete = cur; |
7269 | 0 | goto skip_children; |
7270 | 0 | } |
7271 | 0 | } |
7272 | 0 | } else { |
7273 | 0 | delete = cur; |
7274 | 0 | goto skip_children; |
7275 | 0 | } |
7276 | | |
7277 | | /* |
7278 | | * Skip to next node |
7279 | | */ |
7280 | 0 | if (cur->children != NULL) { |
7281 | 0 | if ((cur->children->type != XML_ENTITY_DECL) && |
7282 | 0 | (cur->children->type != XML_ENTITY_REF_NODE) && |
7283 | 0 | (cur->children->type != XML_ENTITY_NODE)) { |
7284 | 0 | cur = cur->children; |
7285 | 0 | continue; |
7286 | 0 | } |
7287 | 0 | } |
7288 | 0 | skip_children: |
7289 | 0 | if (cur->next != NULL) { |
7290 | 0 | cur = cur->next; |
7291 | 0 | continue; |
7292 | 0 | } |
7293 | | |
7294 | 0 | do { |
7295 | 0 | cur = cur->parent; |
7296 | 0 | if (cur == NULL) |
7297 | 0 | break; |
7298 | 0 | if (cur == root) { |
7299 | 0 | cur = NULL; |
7300 | 0 | break; |
7301 | 0 | } |
7302 | 0 | if (cur->next != NULL) { |
7303 | 0 | cur = cur->next; |
7304 | 0 | break; |
7305 | 0 | } |
7306 | 0 | } while (cur != NULL); |
7307 | 0 | } |
7308 | 0 | if (delete != NULL) { |
7309 | 0 | xmlUnlinkNode(delete); |
7310 | 0 | xmlFreeNode(delete); |
7311 | 0 | delete = NULL; |
7312 | 0 | } |
7313 | 0 | } |
7314 | | |
7315 | | /** |
7316 | | * xmlRelaxNGCleanupDoc: |
7317 | | * @ctxt: a Relax-NG parser context |
7318 | | * @doc: an xmldocPtr document pointer |
7319 | | * |
7320 | | * Cleanup the document from unwanted nodes for parsing, resolve |
7321 | | * Include and externalRef lookups. |
7322 | | * |
7323 | | * Returns the cleaned up document or NULL in case of error |
7324 | | */ |
7325 | | static xmlDocPtr |
7326 | | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
7327 | 0 | { |
7328 | 0 | xmlNodePtr root; |
7329 | | |
7330 | | /* |
7331 | | * Extract the root |
7332 | | */ |
7333 | 0 | root = xmlDocGetRootElement(doc); |
7334 | 0 | if (root == NULL) { |
7335 | 0 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
7336 | 0 | ctxt->URL, NULL); |
7337 | 0 | return (NULL); |
7338 | 0 | } |
7339 | 0 | xmlRelaxNGCleanupTree(ctxt, root); |
7340 | 0 | return (doc); |
7341 | 0 | } |
7342 | | |
7343 | | /** |
7344 | | * xmlRelaxNGParse: |
7345 | | * @ctxt: a Relax-NG parser context |
7346 | | * |
7347 | | * parse a schema definition resource and build an internal |
7348 | | * XML Schema structure which can be used to validate instances. |
7349 | | * |
7350 | | * Returns the internal XML RelaxNG structure built from the resource or |
7351 | | * NULL in case of error |
7352 | | */ |
7353 | | xmlRelaxNGPtr |
7354 | | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
7355 | 0 | { |
7356 | 0 | xmlRelaxNGPtr ret = NULL; |
7357 | 0 | xmlDocPtr doc; |
7358 | 0 | xmlNodePtr root; |
7359 | |
|
7360 | 0 | xmlRelaxNGInitTypes(); |
7361 | |
|
7362 | 0 | if (ctxt == NULL) |
7363 | 0 | return (NULL); |
7364 | | |
7365 | | /* |
7366 | | * First step is to parse the input document into an DOM/Infoset |
7367 | | */ |
7368 | 0 | if (ctxt->URL != NULL) { |
7369 | 0 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
7370 | 0 | if (doc == NULL) { |
7371 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
7372 | 0 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
7373 | 0 | NULL); |
7374 | 0 | return (NULL); |
7375 | 0 | } |
7376 | 0 | } else if (ctxt->buffer != NULL) { |
7377 | 0 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
7378 | 0 | if (doc == NULL) { |
7379 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
7380 | 0 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
7381 | 0 | NULL); |
7382 | 0 | return (NULL); |
7383 | 0 | } |
7384 | 0 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
7385 | 0 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
7386 | 0 | } else if (ctxt->document != NULL) { |
7387 | 0 | doc = ctxt->document; |
7388 | 0 | } else { |
7389 | 0 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
7390 | 0 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
7391 | 0 | return (NULL); |
7392 | 0 | } |
7393 | 0 | ctxt->document = doc; |
7394 | | |
7395 | | /* |
7396 | | * Some preprocessing of the document content |
7397 | | */ |
7398 | 0 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
7399 | 0 | if (doc == NULL) { |
7400 | 0 | xmlFreeDoc(ctxt->document); |
7401 | 0 | ctxt->document = NULL; |
7402 | 0 | return (NULL); |
7403 | 0 | } |
7404 | | |
7405 | | /* |
7406 | | * Then do the parsing for good |
7407 | | */ |
7408 | 0 | root = xmlDocGetRootElement(doc); |
7409 | 0 | if (root == NULL) { |
7410 | 0 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
7411 | 0 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
7412 | 0 | (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL); |
7413 | |
|
7414 | 0 | xmlFreeDoc(ctxt->document); |
7415 | 0 | ctxt->document = NULL; |
7416 | 0 | return (NULL); |
7417 | 0 | } |
7418 | 0 | ret = xmlRelaxNGParseDocument(ctxt, root); |
7419 | 0 | if (ret == NULL) { |
7420 | 0 | xmlFreeDoc(ctxt->document); |
7421 | 0 | ctxt->document = NULL; |
7422 | 0 | return (NULL); |
7423 | 0 | } |
7424 | | |
7425 | | /* |
7426 | | * Check the ref/defines links |
7427 | | */ |
7428 | | /* |
7429 | | * try to preprocess interleaves |
7430 | | */ |
7431 | 0 | if (ctxt->interleaves != NULL) { |
7432 | 0 | xmlHashScan(ctxt->interleaves, xmlRelaxNGComputeInterleaves, ctxt); |
7433 | 0 | } |
7434 | | |
7435 | | /* |
7436 | | * if there was a parsing error return NULL |
7437 | | */ |
7438 | 0 | if (ctxt->nbErrors > 0) { |
7439 | 0 | xmlRelaxNGFree(ret); |
7440 | 0 | ctxt->document = NULL; |
7441 | 0 | xmlFreeDoc(doc); |
7442 | 0 | return (NULL); |
7443 | 0 | } |
7444 | | |
7445 | | /* |
7446 | | * try to compile (parts of) the schemas |
7447 | | */ |
7448 | 0 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
7449 | 0 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
7450 | 0 | xmlRelaxNGDefinePtr def; |
7451 | |
|
7452 | 0 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
7453 | 0 | if (def != NULL) { |
7454 | 0 | def->type = XML_RELAXNG_START; |
7455 | 0 | def->content = ret->topgrammar->start; |
7456 | 0 | ret->topgrammar->start = def; |
7457 | 0 | } |
7458 | 0 | } |
7459 | 0 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
7460 | 0 | } |
7461 | | |
7462 | | /* |
7463 | | * Transfer the pointer for cleanup at the schema level. |
7464 | | */ |
7465 | 0 | ret->doc = doc; |
7466 | 0 | ctxt->document = NULL; |
7467 | 0 | ret->documents = ctxt->documents; |
7468 | 0 | ctxt->documents = NULL; |
7469 | |
|
7470 | 0 | ret->includes = ctxt->includes; |
7471 | 0 | ctxt->includes = NULL; |
7472 | 0 | ret->defNr = ctxt->defNr; |
7473 | 0 | ret->defTab = ctxt->defTab; |
7474 | 0 | ctxt->defTab = NULL; |
7475 | 0 | if (ctxt->idref == 1) |
7476 | 0 | ret->idref = 1; |
7477 | |
|
7478 | 0 | return (ret); |
7479 | 0 | } |
7480 | | |
7481 | | /** |
7482 | | * xmlRelaxNGSetParserErrors: |
7483 | | * @ctxt: a Relax-NG validation context |
7484 | | * @err: the error callback |
7485 | | * @warn: the warning callback |
7486 | | * @ctx: contextual data for the callbacks |
7487 | | * |
7488 | | * Set the callback functions used to handle errors for a validation context |
7489 | | */ |
7490 | | void |
7491 | | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
7492 | | xmlRelaxNGValidityErrorFunc err, |
7493 | | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
7494 | 0 | { |
7495 | 0 | if (ctxt == NULL) |
7496 | 0 | return; |
7497 | 0 | ctxt->error = err; |
7498 | 0 | ctxt->warning = warn; |
7499 | 0 | ctxt->serror = NULL; |
7500 | 0 | ctxt->userData = ctx; |
7501 | 0 | } |
7502 | | |
7503 | | /** |
7504 | | * xmlRelaxNGGetParserErrors: |
7505 | | * @ctxt: a Relax-NG validation context |
7506 | | * @err: the error callback result |
7507 | | * @warn: the warning callback result |
7508 | | * @ctx: contextual data for the callbacks result |
7509 | | * |
7510 | | * Get the callback information used to handle errors for a validation context |
7511 | | * |
7512 | | * Returns -1 in case of failure, 0 otherwise. |
7513 | | */ |
7514 | | int |
7515 | | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
7516 | | xmlRelaxNGValidityErrorFunc * err, |
7517 | | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
7518 | 0 | { |
7519 | 0 | if (ctxt == NULL) |
7520 | 0 | return (-1); |
7521 | 0 | if (err != NULL) |
7522 | 0 | *err = ctxt->error; |
7523 | 0 | if (warn != NULL) |
7524 | 0 | *warn = ctxt->warning; |
7525 | 0 | if (ctx != NULL) |
7526 | 0 | *ctx = ctxt->userData; |
7527 | 0 | return (0); |
7528 | 0 | } |
7529 | | |
7530 | | /** |
7531 | | * xmlRelaxNGSetParserStructuredErrors: |
7532 | | * @ctxt: a Relax-NG parser context |
7533 | | * @serror: the error callback |
7534 | | * @ctx: contextual data for the callbacks |
7535 | | * |
7536 | | * Set the callback functions used to handle errors for a parsing context |
7537 | | */ |
7538 | | void |
7539 | | xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt, |
7540 | | xmlStructuredErrorFunc serror, |
7541 | | void *ctx) |
7542 | 0 | { |
7543 | 0 | if (ctxt == NULL) |
7544 | 0 | return; |
7545 | 0 | ctxt->serror = serror; |
7546 | 0 | ctxt->error = NULL; |
7547 | 0 | ctxt->warning = NULL; |
7548 | 0 | ctxt->userData = ctx; |
7549 | 0 | } |
7550 | | |
7551 | | #ifdef LIBXML_OUTPUT_ENABLED |
7552 | | |
7553 | | /************************************************************************ |
7554 | | * * |
7555 | | * Dump back a compiled form * |
7556 | | * * |
7557 | | ************************************************************************/ |
7558 | | static void xmlRelaxNGDumpDefine(FILE * output, |
7559 | | xmlRelaxNGDefinePtr define); |
7560 | | |
7561 | | /** |
7562 | | * xmlRelaxNGDumpDefines: |
7563 | | * @output: the file output |
7564 | | * @defines: a list of define structures |
7565 | | * |
7566 | | * Dump a RelaxNG structure back |
7567 | | */ |
7568 | | static void |
7569 | | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
7570 | 0 | { |
7571 | 0 | while (defines != NULL) { |
7572 | 0 | xmlRelaxNGDumpDefine(output, defines); |
7573 | 0 | defines = defines->next; |
7574 | 0 | } |
7575 | 0 | } |
7576 | | |
7577 | | /** |
7578 | | * xmlRelaxNGDumpDefine: |
7579 | | * @output: the file output |
7580 | | * @define: a define structure |
7581 | | * |
7582 | | * Dump a RelaxNG structure back |
7583 | | */ |
7584 | | static void |
7585 | | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
7586 | 0 | { |
7587 | 0 | if (define == NULL) |
7588 | 0 | return; |
7589 | 0 | switch (define->type) { |
7590 | 0 | case XML_RELAXNG_EMPTY: |
7591 | 0 | fprintf(output, "<empty/>\n"); |
7592 | 0 | break; |
7593 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
7594 | 0 | fprintf(output, "<notAllowed/>\n"); |
7595 | 0 | break; |
7596 | 0 | case XML_RELAXNG_TEXT: |
7597 | 0 | fprintf(output, "<text/>\n"); |
7598 | 0 | break; |
7599 | 0 | case XML_RELAXNG_ELEMENT: |
7600 | 0 | fprintf(output, "<element>\n"); |
7601 | 0 | if (define->name != NULL) { |
7602 | 0 | fprintf(output, "<name"); |
7603 | 0 | if (define->ns != NULL) |
7604 | 0 | fprintf(output, " ns=\"%s\"", define->ns); |
7605 | 0 | fprintf(output, ">%s</name>\n", define->name); |
7606 | 0 | } |
7607 | 0 | xmlRelaxNGDumpDefines(output, define->attrs); |
7608 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7609 | 0 | fprintf(output, "</element>\n"); |
7610 | 0 | break; |
7611 | 0 | case XML_RELAXNG_LIST: |
7612 | 0 | fprintf(output, "<list>\n"); |
7613 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7614 | 0 | fprintf(output, "</list>\n"); |
7615 | 0 | break; |
7616 | 0 | case XML_RELAXNG_ONEORMORE: |
7617 | 0 | fprintf(output, "<oneOrMore>\n"); |
7618 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7619 | 0 | fprintf(output, "</oneOrMore>\n"); |
7620 | 0 | break; |
7621 | 0 | case XML_RELAXNG_ZEROORMORE: |
7622 | 0 | fprintf(output, "<zeroOrMore>\n"); |
7623 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7624 | 0 | fprintf(output, "</zeroOrMore>\n"); |
7625 | 0 | break; |
7626 | 0 | case XML_RELAXNG_CHOICE: |
7627 | 0 | fprintf(output, "<choice>\n"); |
7628 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7629 | 0 | fprintf(output, "</choice>\n"); |
7630 | 0 | break; |
7631 | 0 | case XML_RELAXNG_GROUP: |
7632 | 0 | fprintf(output, "<group>\n"); |
7633 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7634 | 0 | fprintf(output, "</group>\n"); |
7635 | 0 | break; |
7636 | 0 | case XML_RELAXNG_INTERLEAVE: |
7637 | 0 | fprintf(output, "<interleave>\n"); |
7638 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7639 | 0 | fprintf(output, "</interleave>\n"); |
7640 | 0 | break; |
7641 | 0 | case XML_RELAXNG_OPTIONAL: |
7642 | 0 | fprintf(output, "<optional>\n"); |
7643 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7644 | 0 | fprintf(output, "</optional>\n"); |
7645 | 0 | break; |
7646 | 0 | case XML_RELAXNG_ATTRIBUTE: |
7647 | 0 | fprintf(output, "<attribute>\n"); |
7648 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7649 | 0 | fprintf(output, "</attribute>\n"); |
7650 | 0 | break; |
7651 | 0 | case XML_RELAXNG_DEF: |
7652 | 0 | fprintf(output, "<define"); |
7653 | 0 | if (define->name != NULL) |
7654 | 0 | fprintf(output, " name=\"%s\"", define->name); |
7655 | 0 | fprintf(output, ">\n"); |
7656 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7657 | 0 | fprintf(output, "</define>\n"); |
7658 | 0 | break; |
7659 | 0 | case XML_RELAXNG_REF: |
7660 | 0 | fprintf(output, "<ref"); |
7661 | 0 | if (define->name != NULL) |
7662 | 0 | fprintf(output, " name=\"%s\"", define->name); |
7663 | 0 | fprintf(output, ">\n"); |
7664 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7665 | 0 | fprintf(output, "</ref>\n"); |
7666 | 0 | break; |
7667 | 0 | case XML_RELAXNG_PARENTREF: |
7668 | 0 | fprintf(output, "<parentRef"); |
7669 | 0 | if (define->name != NULL) |
7670 | 0 | fprintf(output, " name=\"%s\"", define->name); |
7671 | 0 | fprintf(output, ">\n"); |
7672 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7673 | 0 | fprintf(output, "</parentRef>\n"); |
7674 | 0 | break; |
7675 | 0 | case XML_RELAXNG_EXTERNALREF: |
7676 | 0 | fprintf(output, "<externalRef>"); |
7677 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7678 | 0 | fprintf(output, "</externalRef>\n"); |
7679 | 0 | break; |
7680 | 0 | case XML_RELAXNG_DATATYPE: |
7681 | 0 | case XML_RELAXNG_VALUE: |
7682 | 0 | TODO break; |
7683 | 0 | case XML_RELAXNG_START: |
7684 | 0 | case XML_RELAXNG_EXCEPT: |
7685 | 0 | case XML_RELAXNG_PARAM: |
7686 | 0 | TODO break; |
7687 | 0 | case XML_RELAXNG_NOOP: |
7688 | 0 | xmlRelaxNGDumpDefines(output, define->content); |
7689 | 0 | break; |
7690 | 0 | } |
7691 | 0 | } |
7692 | | |
7693 | | /** |
7694 | | * xmlRelaxNGDumpGrammar: |
7695 | | * @output: the file output |
7696 | | * @grammar: a grammar structure |
7697 | | * @top: is this a top grammar |
7698 | | * |
7699 | | * Dump a RelaxNG structure back |
7700 | | */ |
7701 | | static void |
7702 | | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
7703 | 0 | { |
7704 | 0 | if (grammar == NULL) |
7705 | 0 | return; |
7706 | | |
7707 | 0 | fprintf(output, "<grammar"); |
7708 | 0 | if (top) |
7709 | 0 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
7710 | 0 | switch (grammar->combine) { |
7711 | 0 | case XML_RELAXNG_COMBINE_UNDEFINED: |
7712 | 0 | break; |
7713 | 0 | case XML_RELAXNG_COMBINE_CHOICE: |
7714 | 0 | fprintf(output, " combine=\"choice\""); |
7715 | 0 | break; |
7716 | 0 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
7717 | 0 | fprintf(output, " combine=\"interleave\""); |
7718 | 0 | break; |
7719 | 0 | default: |
7720 | 0 | fprintf(output, " <!-- invalid combine value -->"); |
7721 | 0 | } |
7722 | 0 | fprintf(output, ">\n"); |
7723 | 0 | if (grammar->start == NULL) { |
7724 | 0 | fprintf(output, " <!-- grammar had no start -->"); |
7725 | 0 | } else { |
7726 | 0 | fprintf(output, "<start>\n"); |
7727 | 0 | xmlRelaxNGDumpDefine(output, grammar->start); |
7728 | 0 | fprintf(output, "</start>\n"); |
7729 | 0 | } |
7730 | | /* TODO ? Dump the defines ? */ |
7731 | 0 | fprintf(output, "</grammar>\n"); |
7732 | 0 | } |
7733 | | |
7734 | | /** |
7735 | | * xmlRelaxNGDump: |
7736 | | * @output: the file output |
7737 | | * @schema: a schema structure |
7738 | | * |
7739 | | * Dump a RelaxNG structure back |
7740 | | */ |
7741 | | void |
7742 | | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
7743 | 0 | { |
7744 | 0 | if (output == NULL) |
7745 | 0 | return; |
7746 | 0 | if (schema == NULL) { |
7747 | 0 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
7748 | 0 | return; |
7749 | 0 | } |
7750 | 0 | fprintf(output, "RelaxNG: "); |
7751 | 0 | if (schema->doc == NULL) { |
7752 | 0 | fprintf(output, "no document\n"); |
7753 | 0 | } else if (schema->doc->URL != NULL) { |
7754 | 0 | fprintf(output, "%s\n", schema->doc->URL); |
7755 | 0 | } else { |
7756 | 0 | fprintf(output, "\n"); |
7757 | 0 | } |
7758 | 0 | if (schema->topgrammar == NULL) { |
7759 | 0 | fprintf(output, "RelaxNG has no top grammar\n"); |
7760 | 0 | return; |
7761 | 0 | } |
7762 | 0 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
7763 | 0 | } |
7764 | | |
7765 | | /** |
7766 | | * xmlRelaxNGDumpTree: |
7767 | | * @output: the file output |
7768 | | * @schema: a schema structure |
7769 | | * |
7770 | | * Dump the transformed RelaxNG tree. |
7771 | | */ |
7772 | | void |
7773 | | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
7774 | 0 | { |
7775 | 0 | if (output == NULL) |
7776 | 0 | return; |
7777 | 0 | if (schema == NULL) { |
7778 | 0 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
7779 | 0 | return; |
7780 | 0 | } |
7781 | 0 | if (schema->doc == NULL) { |
7782 | 0 | fprintf(output, "no document\n"); |
7783 | 0 | } else { |
7784 | 0 | xmlDocDump(output, schema->doc); |
7785 | 0 | } |
7786 | 0 | } |
7787 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
7788 | | |
7789 | | /************************************************************************ |
7790 | | * * |
7791 | | * Validation of compiled content * |
7792 | | * * |
7793 | | ************************************************************************/ |
7794 | | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
7795 | | xmlRelaxNGDefinePtr define); |
7796 | | |
7797 | | /** |
7798 | | * xmlRelaxNGValidateCompiledCallback: |
7799 | | * @exec: the regular expression instance |
7800 | | * @token: the token which matched |
7801 | | * @transdata: callback data, the define for the subelement if available |
7802 | | @ @inputdata: callback data, the Relax NG validation context |
7803 | | * |
7804 | | * Handle the callback and if needed validate the element children. |
7805 | | */ |
7806 | | static void |
7807 | | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
7808 | | const xmlChar * token, |
7809 | | void *transdata, void *inputdata) |
7810 | 0 | { |
7811 | 0 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
7812 | 0 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
7813 | 0 | int ret; |
7814 | |
|
7815 | 0 | if (ctxt == NULL) { |
7816 | 0 | fprintf(stderr, "callback on %s missing context\n", token); |
7817 | 0 | return; |
7818 | 0 | } |
7819 | 0 | if (define == NULL) { |
7820 | 0 | if (token[0] == '#') |
7821 | 0 | return; |
7822 | 0 | fprintf(stderr, "callback on %s missing define\n", token); |
7823 | 0 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
7824 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
7825 | 0 | return; |
7826 | 0 | } |
7827 | 0 | if (define->type != XML_RELAXNG_ELEMENT) { |
7828 | 0 | fprintf(stderr, "callback on %s define is not element\n", token); |
7829 | 0 | if (ctxt->errNo == XML_RELAXNG_OK) |
7830 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
7831 | 0 | return; |
7832 | 0 | } |
7833 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
7834 | 0 | if (ret != 0) |
7835 | 0 | ctxt->perr = ret; |
7836 | 0 | } |
7837 | | |
7838 | | /** |
7839 | | * xmlRelaxNGValidateCompiledContent: |
7840 | | * @ctxt: the RelaxNG validation context |
7841 | | * @regexp: the regular expression as compiled |
7842 | | * @content: list of children to test against the regexp |
7843 | | * |
7844 | | * Validate the content model of an element or start using the regexp |
7845 | | * |
7846 | | * Returns 0 in case of success, -1 in case of error. |
7847 | | */ |
7848 | | static int |
7849 | | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
7850 | | xmlRegexpPtr regexp, xmlNodePtr content) |
7851 | 0 | { |
7852 | 0 | xmlRegExecCtxtPtr exec; |
7853 | 0 | xmlNodePtr cur; |
7854 | 0 | int ret = 0; |
7855 | 0 | int oldperr; |
7856 | |
|
7857 | 0 | if ((ctxt == NULL) || (regexp == NULL)) |
7858 | 0 | return (-1); |
7859 | 0 | oldperr = ctxt->perr; |
7860 | 0 | exec = xmlRegNewExecCtxt(regexp, |
7861 | 0 | xmlRelaxNGValidateCompiledCallback, ctxt); |
7862 | 0 | ctxt->perr = 0; |
7863 | 0 | cur = content; |
7864 | 0 | while (cur != NULL) { |
7865 | 0 | ctxt->state->seq = cur; |
7866 | 0 | switch (cur->type) { |
7867 | 0 | case XML_TEXT_NODE: |
7868 | 0 | case XML_CDATA_SECTION_NODE: |
7869 | 0 | if (xmlIsBlankNode(cur)) |
7870 | 0 | break; |
7871 | 0 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
7872 | 0 | if (ret < 0) { |
7873 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
7874 | 0 | cur->parent->name); |
7875 | 0 | } |
7876 | 0 | break; |
7877 | 0 | case XML_ELEMENT_NODE: |
7878 | 0 | if (cur->ns != NULL) { |
7879 | 0 | ret = xmlRegExecPushString2(exec, cur->name, |
7880 | 0 | cur->ns->href, ctxt); |
7881 | 0 | } else { |
7882 | 0 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
7883 | 0 | } |
7884 | 0 | if (ret < 0) { |
7885 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
7886 | 0 | } |
7887 | 0 | break; |
7888 | 0 | default: |
7889 | 0 | break; |
7890 | 0 | } |
7891 | 0 | if (ret < 0) |
7892 | 0 | break; |
7893 | | /* |
7894 | | * Switch to next element |
7895 | | */ |
7896 | 0 | cur = cur->next; |
7897 | 0 | } |
7898 | 0 | ret = xmlRegExecPushString(exec, NULL, NULL); |
7899 | 0 | if (ret == 1) { |
7900 | 0 | ret = 0; |
7901 | 0 | ctxt->state->seq = NULL; |
7902 | 0 | } else if (ret == 0) { |
7903 | | /* |
7904 | | * TODO: get some of the names needed to exit the current state of exec |
7905 | | */ |
7906 | 0 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
7907 | 0 | ret = -1; |
7908 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
7909 | 0 | xmlRelaxNGDumpValidError(ctxt); |
7910 | 0 | } else { |
7911 | 0 | ret = -1; |
7912 | 0 | } |
7913 | 0 | xmlRegFreeExecCtxt(exec); |
7914 | | /* |
7915 | | * There might be content model errors outside of the pure |
7916 | | * regexp validation, e.g. for attribute values. |
7917 | | */ |
7918 | 0 | if ((ret == 0) && (ctxt->perr != 0)) { |
7919 | 0 | ret = ctxt->perr; |
7920 | 0 | } |
7921 | 0 | ctxt->perr = oldperr; |
7922 | 0 | return (ret); |
7923 | 0 | } |
7924 | | |
7925 | | /************************************************************************ |
7926 | | * * |
7927 | | * Progressive validation of when possible * |
7928 | | * * |
7929 | | ************************************************************************/ |
7930 | | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
7931 | | xmlRelaxNGDefinePtr defines); |
7932 | | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
7933 | | int dolog); |
7934 | | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
7935 | | |
7936 | | /** |
7937 | | * xmlRelaxNGElemPush: |
7938 | | * @ctxt: the validation context |
7939 | | * @exec: the regexp runtime for the new content model |
7940 | | * |
7941 | | * Push a new regexp for the current node content model on the stack |
7942 | | * |
7943 | | * Returns 0 in case of success and -1 in case of error. |
7944 | | */ |
7945 | | static int |
7946 | | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
7947 | 0 | { |
7948 | 0 | if (ctxt->elemTab == NULL) { |
7949 | 0 | ctxt->elemMax = 10; |
7950 | 0 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
7951 | 0 | sizeof |
7952 | 0 | (xmlRegExecCtxtPtr)); |
7953 | 0 | if (ctxt->elemTab == NULL) { |
7954 | 0 | xmlRngVErrMemory(ctxt, "validating\n"); |
7955 | 0 | return (-1); |
7956 | 0 | } |
7957 | 0 | } |
7958 | 0 | if (ctxt->elemNr >= ctxt->elemMax) { |
7959 | 0 | ctxt->elemMax *= 2; |
7960 | 0 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
7961 | 0 | ctxt->elemMax * |
7962 | 0 | sizeof |
7963 | 0 | (xmlRegExecCtxtPtr)); |
7964 | 0 | if (ctxt->elemTab == NULL) { |
7965 | 0 | xmlRngVErrMemory(ctxt, "validating\n"); |
7966 | 0 | return (-1); |
7967 | 0 | } |
7968 | 0 | } |
7969 | 0 | ctxt->elemTab[ctxt->elemNr++] = exec; |
7970 | 0 | ctxt->elem = exec; |
7971 | 0 | return (0); |
7972 | 0 | } |
7973 | | |
7974 | | /** |
7975 | | * xmlRelaxNGElemPop: |
7976 | | * @ctxt: the validation context |
7977 | | * |
7978 | | * Pop the regexp of the current node content model from the stack |
7979 | | * |
7980 | | * Returns the exec or NULL if empty |
7981 | | */ |
7982 | | static xmlRegExecCtxtPtr |
7983 | | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
7984 | 0 | { |
7985 | 0 | xmlRegExecCtxtPtr ret; |
7986 | |
|
7987 | 0 | if (ctxt->elemNr <= 0) |
7988 | 0 | return (NULL); |
7989 | 0 | ctxt->elemNr--; |
7990 | 0 | ret = ctxt->elemTab[ctxt->elemNr]; |
7991 | 0 | ctxt->elemTab[ctxt->elemNr] = NULL; |
7992 | 0 | if (ctxt->elemNr > 0) |
7993 | 0 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
7994 | 0 | else |
7995 | 0 | ctxt->elem = NULL; |
7996 | 0 | return (ret); |
7997 | 0 | } |
7998 | | |
7999 | | /** |
8000 | | * xmlRelaxNGValidateProgressiveCallback: |
8001 | | * @exec: the regular expression instance |
8002 | | * @token: the token which matched |
8003 | | * @transdata: callback data, the define for the subelement if available |
8004 | | @ @inputdata: callback data, the Relax NG validation context |
8005 | | * |
8006 | | * Handle the callback and if needed validate the element children. |
8007 | | * some of the in/out information are passed via the context in @inputdata. |
8008 | | */ |
8009 | | static void |
8010 | | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
8011 | | ATTRIBUTE_UNUSED, |
8012 | | const xmlChar * token, |
8013 | | void *transdata, void *inputdata) |
8014 | 0 | { |
8015 | 0 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
8016 | 0 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
8017 | 0 | xmlRelaxNGValidStatePtr state, oldstate; |
8018 | 0 | xmlNodePtr node; |
8019 | 0 | int ret = 0, oldflags; |
8020 | |
|
8021 | 0 | if (ctxt == NULL) { |
8022 | 0 | fprintf(stderr, "callback on %s missing context\n", token); |
8023 | 0 | return; |
8024 | 0 | } |
8025 | 0 | node = ctxt->pnode; |
8026 | 0 | ctxt->pstate = 1; |
8027 | 0 | if (define == NULL) { |
8028 | 0 | if (token[0] == '#') |
8029 | 0 | return; |
8030 | 0 | fprintf(stderr, "callback on %s missing define\n", token); |
8031 | 0 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
8032 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
8033 | 0 | ctxt->pstate = -1; |
8034 | 0 | return; |
8035 | 0 | } |
8036 | 0 | if ((ctxt == NULL) || (define == NULL)) { |
8037 | 0 | fprintf(stderr, "callback on %s missing info\n", token); |
8038 | 0 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
8039 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
8040 | 0 | ctxt->pstate = -1; |
8041 | 0 | return; |
8042 | 0 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
8043 | 0 | fprintf(stderr, "callback on %s define is not element\n", token); |
8044 | 0 | if (ctxt->errNo == XML_RELAXNG_OK) |
8045 | 0 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
8046 | 0 | ctxt->pstate = -1; |
8047 | 0 | return; |
8048 | 0 | } |
8049 | 0 | if (node->type != XML_ELEMENT_NODE) { |
8050 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
8051 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
8052 | 0 | xmlRelaxNGDumpValidError(ctxt); |
8053 | 0 | ctxt->pstate = -1; |
8054 | 0 | return; |
8055 | 0 | } |
8056 | 0 | if (define->contModel == NULL) { |
8057 | | /* |
8058 | | * this node cannot be validated in a streamable fashion |
8059 | | */ |
8060 | 0 | ctxt->pstate = 0; |
8061 | 0 | ctxt->pdef = define; |
8062 | 0 | return; |
8063 | 0 | } |
8064 | 0 | exec = xmlRegNewExecCtxt(define->contModel, |
8065 | 0 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
8066 | 0 | if (exec == NULL) { |
8067 | 0 | ctxt->pstate = -1; |
8068 | 0 | return; |
8069 | 0 | } |
8070 | 0 | xmlRelaxNGElemPush(ctxt, exec); |
8071 | | |
8072 | | /* |
8073 | | * Validate the attributes part of the content. |
8074 | | */ |
8075 | 0 | state = xmlRelaxNGNewValidState(ctxt, node); |
8076 | 0 | if (state == NULL) { |
8077 | 0 | ctxt->pstate = -1; |
8078 | 0 | return; |
8079 | 0 | } |
8080 | 0 | oldstate = ctxt->state; |
8081 | 0 | ctxt->state = state; |
8082 | 0 | if (define->attrs != NULL) { |
8083 | 0 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
8084 | 0 | if (ret != 0) { |
8085 | 0 | ctxt->pstate = -1; |
8086 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
8087 | 0 | } |
8088 | 0 | } |
8089 | 0 | if (ctxt->state != NULL) { |
8090 | 0 | ctxt->state->seq = NULL; |
8091 | 0 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
8092 | 0 | if (ret != 0) { |
8093 | 0 | ctxt->pstate = -1; |
8094 | 0 | } |
8095 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
8096 | 0 | } else if (ctxt->states != NULL) { |
8097 | 0 | int tmp = -1, i; |
8098 | |
|
8099 | 0 | oldflags = ctxt->flags; |
8100 | |
|
8101 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
8102 | 0 | state = ctxt->states->tabState[i]; |
8103 | 0 | ctxt->state = state; |
8104 | 0 | ctxt->state->seq = NULL; |
8105 | |
|
8106 | 0 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
8107 | 0 | tmp = 0; |
8108 | 0 | break; |
8109 | 0 | } |
8110 | 0 | } |
8111 | 0 | if (tmp != 0) { |
8112 | | /* |
8113 | | * validation error, log the message for the "best" one |
8114 | | */ |
8115 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8116 | 0 | xmlRelaxNGLogBestError(ctxt); |
8117 | 0 | } |
8118 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
8119 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
8120 | 0 | } |
8121 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
8122 | 0 | ctxt->states = NULL; |
8123 | 0 | if ((ret == 0) && (tmp == -1)) |
8124 | 0 | ctxt->pstate = -1; |
8125 | 0 | ctxt->flags = oldflags; |
8126 | 0 | } |
8127 | 0 | if (ctxt->pstate == -1) { |
8128 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
8129 | 0 | xmlRelaxNGDumpValidError(ctxt); |
8130 | 0 | } |
8131 | 0 | } |
8132 | 0 | ctxt->state = oldstate; |
8133 | 0 | } |
8134 | | |
8135 | | /** |
8136 | | * xmlRelaxNGValidatePushElement: |
8137 | | * @ctxt: the validation context |
8138 | | * @doc: a document instance |
8139 | | * @elem: an element instance |
8140 | | * |
8141 | | * Push a new element start on the RelaxNG validation stack. |
8142 | | * |
8143 | | * returns 1 if no validation problem was found or 0 if validating the |
8144 | | * element requires a full node, and -1 in case of error. |
8145 | | */ |
8146 | | int |
8147 | | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
8148 | | xmlDocPtr doc ATTRIBUTE_UNUSED, |
8149 | | xmlNodePtr elem) |
8150 | 0 | { |
8151 | 0 | int ret = 1; |
8152 | |
|
8153 | 0 | if ((ctxt == NULL) || (elem == NULL)) |
8154 | 0 | return (-1); |
8155 | | |
8156 | 0 | if (ctxt->elem == 0) { |
8157 | 0 | xmlRelaxNGPtr schema; |
8158 | 0 | xmlRelaxNGGrammarPtr grammar; |
8159 | 0 | xmlRegExecCtxtPtr exec; |
8160 | 0 | xmlRelaxNGDefinePtr define; |
8161 | |
|
8162 | 0 | schema = ctxt->schema; |
8163 | 0 | if (schema == NULL) { |
8164 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
8165 | 0 | return (-1); |
8166 | 0 | } |
8167 | 0 | grammar = schema->topgrammar; |
8168 | 0 | if ((grammar == NULL) || (grammar->start == NULL)) { |
8169 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
8170 | 0 | return (-1); |
8171 | 0 | } |
8172 | 0 | define = grammar->start; |
8173 | 0 | if (define->contModel == NULL) { |
8174 | 0 | ctxt->pdef = define; |
8175 | 0 | return (0); |
8176 | 0 | } |
8177 | 0 | exec = xmlRegNewExecCtxt(define->contModel, |
8178 | 0 | xmlRelaxNGValidateProgressiveCallback, |
8179 | 0 | ctxt); |
8180 | 0 | if (exec == NULL) { |
8181 | 0 | return (-1); |
8182 | 0 | } |
8183 | 0 | xmlRelaxNGElemPush(ctxt, exec); |
8184 | 0 | } |
8185 | 0 | ctxt->pnode = elem; |
8186 | 0 | ctxt->pstate = 0; |
8187 | 0 | if (elem->ns != NULL) { |
8188 | 0 | ret = |
8189 | 0 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
8190 | 0 | ctxt); |
8191 | 0 | } else { |
8192 | 0 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
8193 | 0 | } |
8194 | 0 | if (ret < 0) { |
8195 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
8196 | 0 | } else { |
8197 | 0 | if (ctxt->pstate == 0) |
8198 | 0 | ret = 0; |
8199 | 0 | else if (ctxt->pstate < 0) |
8200 | 0 | ret = -1; |
8201 | 0 | else |
8202 | 0 | ret = 1; |
8203 | 0 | } |
8204 | 0 | return (ret); |
8205 | 0 | } |
8206 | | |
8207 | | /** |
8208 | | * xmlRelaxNGValidatePushCData: |
8209 | | * @ctxt: the RelaxNG validation context |
8210 | | * @data: some character data read |
8211 | | * @len: the length of the data |
8212 | | * |
8213 | | * check the CData parsed for validation in the current stack |
8214 | | * |
8215 | | * returns 1 if no validation problem was found or -1 otherwise |
8216 | | */ |
8217 | | int |
8218 | | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
8219 | | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
8220 | 0 | { |
8221 | 0 | int ret = 1; |
8222 | |
|
8223 | 0 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
8224 | 0 | return (-1); |
8225 | | |
8226 | 0 | while (*data != 0) { |
8227 | 0 | if (!IS_BLANK_CH(*data)) |
8228 | 0 | break; |
8229 | 0 | data++; |
8230 | 0 | } |
8231 | 0 | if (*data == 0) |
8232 | 0 | return (1); |
8233 | | |
8234 | 0 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
8235 | 0 | if (ret < 0) { |
8236 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
8237 | |
|
8238 | 0 | return (-1); |
8239 | 0 | } |
8240 | 0 | return (1); |
8241 | 0 | } |
8242 | | |
8243 | | /** |
8244 | | * xmlRelaxNGValidatePopElement: |
8245 | | * @ctxt: the RelaxNG validation context |
8246 | | * @doc: a document instance |
8247 | | * @elem: an element instance |
8248 | | * |
8249 | | * Pop the element end from the RelaxNG validation stack. |
8250 | | * |
8251 | | * returns 1 if no validation problem was found or 0 otherwise |
8252 | | */ |
8253 | | int |
8254 | | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
8255 | | xmlDocPtr doc ATTRIBUTE_UNUSED, |
8256 | | xmlNodePtr elem) |
8257 | 0 | { |
8258 | 0 | int ret; |
8259 | 0 | xmlRegExecCtxtPtr exec; |
8260 | |
|
8261 | 0 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
8262 | 0 | return (-1); |
8263 | | /* |
8264 | | * verify that we reached a terminal state of the content model. |
8265 | | */ |
8266 | 0 | exec = xmlRelaxNGElemPop(ctxt); |
8267 | 0 | ret = xmlRegExecPushString(exec, NULL, NULL); |
8268 | 0 | if (ret == 0) { |
8269 | | /* |
8270 | | * TODO: get some of the names needed to exit the current state of exec |
8271 | | */ |
8272 | 0 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
8273 | 0 | ret = -1; |
8274 | 0 | } else if (ret < 0) { |
8275 | 0 | ret = -1; |
8276 | 0 | } else { |
8277 | 0 | ret = 1; |
8278 | 0 | } |
8279 | 0 | xmlRegFreeExecCtxt(exec); |
8280 | 0 | return (ret); |
8281 | 0 | } |
8282 | | |
8283 | | /** |
8284 | | * xmlRelaxNGValidateFullElement: |
8285 | | * @ctxt: the validation context |
8286 | | * @doc: a document instance |
8287 | | * @elem: an element instance |
8288 | | * |
8289 | | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
8290 | | * 0 and the content of the node has been expanded. |
8291 | | * |
8292 | | * returns 1 if no validation problem was found or -1 in case of error. |
8293 | | */ |
8294 | | int |
8295 | | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
8296 | | xmlDocPtr doc ATTRIBUTE_UNUSED, |
8297 | | xmlNodePtr elem) |
8298 | 0 | { |
8299 | 0 | int ret; |
8300 | 0 | xmlRelaxNGValidStatePtr state; |
8301 | |
|
8302 | 0 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
8303 | 0 | return (-1); |
8304 | 0 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
8305 | 0 | if (state == NULL) { |
8306 | 0 | return (-1); |
8307 | 0 | } |
8308 | 0 | state->seq = elem; |
8309 | 0 | ctxt->state = state; |
8310 | 0 | ctxt->errNo = XML_RELAXNG_OK; |
8311 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
8312 | 0 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
8313 | 0 | ret = -1; |
8314 | 0 | else |
8315 | 0 | ret = 1; |
8316 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
8317 | 0 | ctxt->state = NULL; |
8318 | 0 | return (ret); |
8319 | 0 | } |
8320 | | |
8321 | | /************************************************************************ |
8322 | | * * |
8323 | | * Generic interpreted validation implementation * |
8324 | | * * |
8325 | | ************************************************************************/ |
8326 | | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
8327 | | xmlRelaxNGDefinePtr define); |
8328 | | |
8329 | | /** |
8330 | | * xmlRelaxNGSkipIgnored: |
8331 | | * @ctxt: a schema validation context |
8332 | | * @node: the top node. |
8333 | | * |
8334 | | * Skip ignorable nodes in that context |
8335 | | * |
8336 | | * Returns the new sibling or NULL in case of error. |
8337 | | */ |
8338 | | static xmlNodePtr |
8339 | | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
8340 | | xmlNodePtr node) |
8341 | 0 | { |
8342 | | /* |
8343 | | * TODO complete and handle entities |
8344 | | */ |
8345 | 0 | while ((node != NULL) && |
8346 | 0 | ((node->type == XML_COMMENT_NODE) || |
8347 | 0 | (node->type == XML_PI_NODE) || |
8348 | 0 | (node->type == XML_XINCLUDE_START) || |
8349 | 0 | (node->type == XML_XINCLUDE_END) || |
8350 | 0 | (((node->type == XML_TEXT_NODE) || |
8351 | 0 | (node->type == XML_CDATA_SECTION_NODE)) && |
8352 | 0 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
8353 | 0 | (IS_BLANK_NODE(node)))))) { |
8354 | 0 | node = node->next; |
8355 | 0 | } |
8356 | 0 | return (node); |
8357 | 0 | } |
8358 | | |
8359 | | /** |
8360 | | * xmlRelaxNGNormalize: |
8361 | | * @ctxt: a schema validation context |
8362 | | * @str: the string to normalize |
8363 | | * |
8364 | | * Implements the normalizeWhiteSpace( s ) function from |
8365 | | * section 6.2.9 of the spec |
8366 | | * |
8367 | | * Returns the new string or NULL in case of error. |
8368 | | */ |
8369 | | static xmlChar * |
8370 | | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
8371 | 0 | { |
8372 | 0 | xmlChar *ret, *p; |
8373 | 0 | const xmlChar *tmp; |
8374 | 0 | int len; |
8375 | |
|
8376 | 0 | if (str == NULL) |
8377 | 0 | return (NULL); |
8378 | 0 | tmp = str; |
8379 | 0 | while (*tmp != 0) |
8380 | 0 | tmp++; |
8381 | 0 | len = tmp - str; |
8382 | |
|
8383 | 0 | ret = (xmlChar *) xmlMallocAtomic(len + 1); |
8384 | 0 | if (ret == NULL) { |
8385 | 0 | xmlRngVErrMemory(ctxt, "validating\n"); |
8386 | 0 | return (NULL); |
8387 | 0 | } |
8388 | 0 | p = ret; |
8389 | 0 | while (IS_BLANK_CH(*str)) |
8390 | 0 | str++; |
8391 | 0 | while (*str != 0) { |
8392 | 0 | if (IS_BLANK_CH(*str)) { |
8393 | 0 | while (IS_BLANK_CH(*str)) |
8394 | 0 | str++; |
8395 | 0 | if (*str == 0) |
8396 | 0 | break; |
8397 | 0 | *p++ = ' '; |
8398 | 0 | } else |
8399 | 0 | *p++ = *str++; |
8400 | 0 | } |
8401 | 0 | *p = 0; |
8402 | 0 | return (ret); |
8403 | 0 | } |
8404 | | |
8405 | | /** |
8406 | | * xmlRelaxNGValidateDatatype: |
8407 | | * @ctxt: a Relax-NG validation context |
8408 | | * @value: the string value |
8409 | | * @type: the datatype definition |
8410 | | * @node: the node |
8411 | | * |
8412 | | * Validate the given value against the datatype |
8413 | | * |
8414 | | * Returns 0 if the validation succeeded or an error code. |
8415 | | */ |
8416 | | static int |
8417 | | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
8418 | | const xmlChar * value, |
8419 | | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
8420 | 0 | { |
8421 | 0 | int ret, tmp; |
8422 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
8423 | 0 | void *result = NULL; |
8424 | 0 | xmlRelaxNGDefinePtr cur; |
8425 | |
|
8426 | 0 | if ((define == NULL) || (define->data == NULL)) { |
8427 | 0 | return (-1); |
8428 | 0 | } |
8429 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
8430 | 0 | if (lib->check != NULL) { |
8431 | 0 | if ((define->attrs != NULL) && |
8432 | 0 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
8433 | 0 | ret = |
8434 | 0 | lib->check(lib->data, define->name, value, &result, node); |
8435 | 0 | } else { |
8436 | 0 | ret = lib->check(lib->data, define->name, value, NULL, node); |
8437 | 0 | } |
8438 | 0 | } else |
8439 | 0 | ret = -1; |
8440 | 0 | if (ret < 0) { |
8441 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
8442 | 0 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
8443 | 0 | lib->freef(lib->data, result); |
8444 | 0 | return (-1); |
8445 | 0 | } else if (ret == 1) { |
8446 | 0 | ret = 0; |
8447 | 0 | } else if (ret == 2) { |
8448 | 0 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
8449 | 0 | } else { |
8450 | 0 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
8451 | 0 | ret = -1; |
8452 | 0 | } |
8453 | 0 | cur = define->attrs; |
8454 | 0 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
8455 | 0 | if (lib->facet != NULL) { |
8456 | 0 | tmp = lib->facet(lib->data, define->name, cur->name, |
8457 | 0 | cur->value, value, result); |
8458 | 0 | if (tmp != 0) |
8459 | 0 | ret = -1; |
8460 | 0 | } |
8461 | 0 | cur = cur->next; |
8462 | 0 | } |
8463 | 0 | if ((ret == 0) && (define->content != NULL)) { |
8464 | 0 | const xmlChar *oldvalue, *oldendvalue; |
8465 | |
|
8466 | 0 | oldvalue = ctxt->state->value; |
8467 | 0 | oldendvalue = ctxt->state->endvalue; |
8468 | 0 | ctxt->state->value = (xmlChar *) value; |
8469 | 0 | ctxt->state->endvalue = NULL; |
8470 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
8471 | 0 | ctxt->state->value = (xmlChar *) oldvalue; |
8472 | 0 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
8473 | 0 | } |
8474 | 0 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
8475 | 0 | lib->freef(lib->data, result); |
8476 | 0 | return (ret); |
8477 | 0 | } |
8478 | | |
8479 | | /** |
8480 | | * xmlRelaxNGNextValue: |
8481 | | * @ctxt: a Relax-NG validation context |
8482 | | * |
8483 | | * Skip to the next value when validating within a list |
8484 | | * |
8485 | | * Returns 0 if the operation succeeded or an error code. |
8486 | | */ |
8487 | | static int |
8488 | | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
8489 | 0 | { |
8490 | 0 | xmlChar *cur; |
8491 | |
|
8492 | 0 | cur = ctxt->state->value; |
8493 | 0 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
8494 | 0 | ctxt->state->value = NULL; |
8495 | 0 | ctxt->state->endvalue = NULL; |
8496 | 0 | return (0); |
8497 | 0 | } |
8498 | 0 | while (*cur != 0) |
8499 | 0 | cur++; |
8500 | 0 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
8501 | 0 | cur++; |
8502 | 0 | if (cur == ctxt->state->endvalue) |
8503 | 0 | ctxt->state->value = NULL; |
8504 | 0 | else |
8505 | 0 | ctxt->state->value = cur; |
8506 | 0 | return (0); |
8507 | 0 | } |
8508 | | |
8509 | | /** |
8510 | | * xmlRelaxNGValidateValueList: |
8511 | | * @ctxt: a Relax-NG validation context |
8512 | | * @defines: the list of definitions to verify |
8513 | | * |
8514 | | * Validate the given set of definitions for the current value |
8515 | | * |
8516 | | * Returns 0 if the validation succeeded or an error code. |
8517 | | */ |
8518 | | static int |
8519 | | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
8520 | | xmlRelaxNGDefinePtr defines) |
8521 | 0 | { |
8522 | 0 | int ret = 0; |
8523 | |
|
8524 | 0 | while (defines != NULL) { |
8525 | 0 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
8526 | 0 | if (ret != 0) |
8527 | 0 | break; |
8528 | 0 | defines = defines->next; |
8529 | 0 | } |
8530 | 0 | return (ret); |
8531 | 0 | } |
8532 | | |
8533 | | /** |
8534 | | * xmlRelaxNGValidateValue: |
8535 | | * @ctxt: a Relax-NG validation context |
8536 | | * @define: the definition to verify |
8537 | | * |
8538 | | * Validate the given definition for the current value |
8539 | | * |
8540 | | * Returns 0 if the validation succeeded or an error code. |
8541 | | */ |
8542 | | static int |
8543 | | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
8544 | | xmlRelaxNGDefinePtr define) |
8545 | 0 | { |
8546 | 0 | int ret = 0, oldflags; |
8547 | 0 | xmlChar *value; |
8548 | |
|
8549 | 0 | value = ctxt->state->value; |
8550 | 0 | switch (define->type) { |
8551 | 0 | case XML_RELAXNG_EMPTY:{ |
8552 | 0 | if ((value != NULL) && (value[0] != 0)) { |
8553 | 0 | int idx = 0; |
8554 | |
|
8555 | 0 | while (IS_BLANK_CH(value[idx])) |
8556 | 0 | idx++; |
8557 | 0 | if (value[idx] != 0) |
8558 | 0 | ret = -1; |
8559 | 0 | } |
8560 | 0 | break; |
8561 | 0 | } |
8562 | 0 | case XML_RELAXNG_TEXT: |
8563 | 0 | break; |
8564 | 0 | case XML_RELAXNG_VALUE:{ |
8565 | 0 | if (!xmlStrEqual(value, define->value)) { |
8566 | 0 | if (define->name != NULL) { |
8567 | 0 | xmlRelaxNGTypeLibraryPtr lib; |
8568 | |
|
8569 | 0 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
8570 | 0 | if ((lib != NULL) && (lib->comp != NULL)) { |
8571 | 0 | ret = lib->comp(lib->data, define->name, |
8572 | 0 | define->value, define->node, |
8573 | 0 | (void *) define->attrs, |
8574 | 0 | value, ctxt->state->node); |
8575 | 0 | } else |
8576 | 0 | ret = -1; |
8577 | 0 | if (ret < 0) { |
8578 | 0 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
8579 | 0 | define->name); |
8580 | 0 | return (-1); |
8581 | 0 | } else if (ret == 1) { |
8582 | 0 | ret = 0; |
8583 | 0 | } else { |
8584 | 0 | ret = -1; |
8585 | 0 | } |
8586 | 0 | } else { |
8587 | 0 | xmlChar *nval, *nvalue; |
8588 | | |
8589 | | /* |
8590 | | * TODO: trivial optimizations are possible by |
8591 | | * computing at compile-time |
8592 | | */ |
8593 | 0 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
8594 | 0 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
8595 | |
|
8596 | 0 | if ((nval == NULL) || (nvalue == NULL) || |
8597 | 0 | (!xmlStrEqual(nval, nvalue))) |
8598 | 0 | ret = -1; |
8599 | 0 | if (nval != NULL) |
8600 | 0 | xmlFree(nval); |
8601 | 0 | if (nvalue != NULL) |
8602 | 0 | xmlFree(nvalue); |
8603 | 0 | } |
8604 | 0 | } |
8605 | 0 | if (ret == 0) |
8606 | 0 | xmlRelaxNGNextValue(ctxt); |
8607 | 0 | break; |
8608 | 0 | } |
8609 | 0 | case XML_RELAXNG_DATATYPE:{ |
8610 | 0 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
8611 | 0 | ctxt->state->seq); |
8612 | 0 | if (ret == 0) |
8613 | 0 | xmlRelaxNGNextValue(ctxt); |
8614 | |
|
8615 | 0 | break; |
8616 | 0 | } |
8617 | 0 | case XML_RELAXNG_CHOICE:{ |
8618 | 0 | xmlRelaxNGDefinePtr list = define->content; |
8619 | 0 | xmlChar *oldvalue; |
8620 | |
|
8621 | 0 | oldflags = ctxt->flags; |
8622 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8623 | |
|
8624 | 0 | oldvalue = ctxt->state->value; |
8625 | 0 | while (list != NULL) { |
8626 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8627 | 0 | if (ret == 0) { |
8628 | 0 | break; |
8629 | 0 | } |
8630 | 0 | ctxt->state->value = oldvalue; |
8631 | 0 | list = list->next; |
8632 | 0 | } |
8633 | 0 | ctxt->flags = oldflags; |
8634 | 0 | if (ret != 0) { |
8635 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
8636 | 0 | xmlRelaxNGDumpValidError(ctxt); |
8637 | 0 | } else { |
8638 | 0 | if (ctxt->errNr > 0) |
8639 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8640 | 0 | } |
8641 | 0 | break; |
8642 | 0 | } |
8643 | 0 | case XML_RELAXNG_LIST:{ |
8644 | 0 | xmlRelaxNGDefinePtr list = define->content; |
8645 | 0 | xmlChar *oldvalue, *oldend, *val, *cur; |
8646 | |
|
8647 | 0 | oldvalue = ctxt->state->value; |
8648 | 0 | oldend = ctxt->state->endvalue; |
8649 | |
|
8650 | 0 | val = xmlStrdup(oldvalue); |
8651 | 0 | if (val == NULL) { |
8652 | 0 | val = xmlStrdup(BAD_CAST ""); |
8653 | 0 | } |
8654 | 0 | if (val == NULL) { |
8655 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
8656 | 0 | return (-1); |
8657 | 0 | } |
8658 | 0 | cur = val; |
8659 | 0 | while (*cur != 0) { |
8660 | 0 | if (IS_BLANK_CH(*cur)) { |
8661 | 0 | *cur = 0; |
8662 | 0 | cur++; |
8663 | 0 | while (IS_BLANK_CH(*cur)) |
8664 | 0 | *cur++ = 0; |
8665 | 0 | } else |
8666 | 0 | cur++; |
8667 | 0 | } |
8668 | 0 | ctxt->state->endvalue = cur; |
8669 | 0 | cur = val; |
8670 | 0 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
8671 | 0 | cur++; |
8672 | |
|
8673 | 0 | ctxt->state->value = cur; |
8674 | |
|
8675 | 0 | while (list != NULL) { |
8676 | 0 | if (ctxt->state->value == ctxt->state->endvalue) |
8677 | 0 | ctxt->state->value = NULL; |
8678 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8679 | 0 | if (ret != 0) { |
8680 | 0 | break; |
8681 | 0 | } |
8682 | 0 | list = list->next; |
8683 | 0 | } |
8684 | |
|
8685 | 0 | if ((ret == 0) && (ctxt->state->value != NULL) && |
8686 | 0 | (ctxt->state->value != ctxt->state->endvalue)) { |
8687 | 0 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
8688 | 0 | ctxt->state->value); |
8689 | 0 | ret = -1; |
8690 | 0 | } |
8691 | 0 | xmlFree(val); |
8692 | 0 | ctxt->state->value = oldvalue; |
8693 | 0 | ctxt->state->endvalue = oldend; |
8694 | 0 | break; |
8695 | 0 | } |
8696 | 0 | case XML_RELAXNG_ONEORMORE: |
8697 | 0 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
8698 | 0 | if (ret != 0) { |
8699 | 0 | break; |
8700 | 0 | } |
8701 | | /* Falls through. */ |
8702 | 0 | case XML_RELAXNG_ZEROORMORE:{ |
8703 | 0 | xmlChar *cur, *temp; |
8704 | |
|
8705 | 0 | if ((ctxt->state->value == NULL) || |
8706 | 0 | (*ctxt->state->value == 0)) { |
8707 | 0 | ret = 0; |
8708 | 0 | break; |
8709 | 0 | } |
8710 | 0 | oldflags = ctxt->flags; |
8711 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8712 | 0 | cur = ctxt->state->value; |
8713 | 0 | temp = NULL; |
8714 | 0 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
8715 | 0 | (temp != cur)) { |
8716 | 0 | temp = cur; |
8717 | 0 | ret = |
8718 | 0 | xmlRelaxNGValidateValueList(ctxt, define->content); |
8719 | 0 | if (ret != 0) { |
8720 | 0 | ctxt->state->value = temp; |
8721 | 0 | ret = 0; |
8722 | 0 | break; |
8723 | 0 | } |
8724 | 0 | cur = ctxt->state->value; |
8725 | 0 | } |
8726 | 0 | ctxt->flags = oldflags; |
8727 | 0 | if (ctxt->errNr > 0) |
8728 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8729 | 0 | break; |
8730 | 0 | } |
8731 | 0 | case XML_RELAXNG_OPTIONAL:{ |
8732 | 0 | xmlChar *temp; |
8733 | |
|
8734 | 0 | if ((ctxt->state->value == NULL) || |
8735 | 0 | (*ctxt->state->value == 0)) { |
8736 | 0 | ret = 0; |
8737 | 0 | break; |
8738 | 0 | } |
8739 | 0 | oldflags = ctxt->flags; |
8740 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
8741 | 0 | temp = ctxt->state->value; |
8742 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
8743 | 0 | ctxt->flags = oldflags; |
8744 | 0 | if (ret != 0) { |
8745 | 0 | ctxt->state->value = temp; |
8746 | 0 | if (ctxt->errNr > 0) |
8747 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8748 | 0 | ret = 0; |
8749 | 0 | break; |
8750 | 0 | } |
8751 | 0 | if (ctxt->errNr > 0) |
8752 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
8753 | 0 | break; |
8754 | 0 | } |
8755 | 0 | case XML_RELAXNG_EXCEPT:{ |
8756 | 0 | xmlRelaxNGDefinePtr list; |
8757 | |
|
8758 | 0 | list = define->content; |
8759 | 0 | while (list != NULL) { |
8760 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8761 | 0 | if (ret == 0) { |
8762 | 0 | ret = -1; |
8763 | 0 | break; |
8764 | 0 | } else |
8765 | 0 | ret = 0; |
8766 | 0 | list = list->next; |
8767 | 0 | } |
8768 | 0 | break; |
8769 | 0 | } |
8770 | 0 | case XML_RELAXNG_DEF: |
8771 | 0 | case XML_RELAXNG_GROUP:{ |
8772 | 0 | xmlRelaxNGDefinePtr list; |
8773 | |
|
8774 | 0 | list = define->content; |
8775 | 0 | while (list != NULL) { |
8776 | 0 | ret = xmlRelaxNGValidateValue(ctxt, list); |
8777 | 0 | if (ret != 0) { |
8778 | 0 | ret = -1; |
8779 | 0 | break; |
8780 | 0 | } else |
8781 | 0 | ret = 0; |
8782 | 0 | list = list->next; |
8783 | 0 | } |
8784 | 0 | break; |
8785 | 0 | } |
8786 | 0 | case XML_RELAXNG_REF: |
8787 | 0 | case XML_RELAXNG_PARENTREF: |
8788 | 0 | if (define->content == NULL) { |
8789 | 0 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
8790 | 0 | ret = -1; |
8791 | 0 | } else { |
8792 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
8793 | 0 | } |
8794 | 0 | break; |
8795 | 0 | default: |
8796 | 0 | TODO ret = -1; |
8797 | 0 | } |
8798 | 0 | return (ret); |
8799 | 0 | } |
8800 | | |
8801 | | /** |
8802 | | * xmlRelaxNGValidateValueContent: |
8803 | | * @ctxt: a Relax-NG validation context |
8804 | | * @defines: the list of definitions to verify |
8805 | | * |
8806 | | * Validate the given definitions for the current value |
8807 | | * |
8808 | | * Returns 0 if the validation succeeded or an error code. |
8809 | | */ |
8810 | | static int |
8811 | | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
8812 | | xmlRelaxNGDefinePtr defines) |
8813 | 0 | { |
8814 | 0 | int ret = 0; |
8815 | |
|
8816 | 0 | while (defines != NULL) { |
8817 | 0 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
8818 | 0 | if (ret != 0) |
8819 | 0 | break; |
8820 | 0 | defines = defines->next; |
8821 | 0 | } |
8822 | 0 | return (ret); |
8823 | 0 | } |
8824 | | |
8825 | | /** |
8826 | | * xmlRelaxNGAttributeMatch: |
8827 | | * @ctxt: a Relax-NG validation context |
8828 | | * @define: the definition to check |
8829 | | * @prop: the attribute |
8830 | | * |
8831 | | * Check if the attribute matches the definition nameClass |
8832 | | * |
8833 | | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
8834 | | */ |
8835 | | static int |
8836 | | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
8837 | | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
8838 | 0 | { |
8839 | 0 | int ret; |
8840 | |
|
8841 | 0 | if (define->name != NULL) { |
8842 | 0 | if (!xmlStrEqual(define->name, prop->name)) |
8843 | 0 | return (0); |
8844 | 0 | } |
8845 | 0 | if (define->ns != NULL) { |
8846 | 0 | if (define->ns[0] == 0) { |
8847 | 0 | if (prop->ns != NULL) |
8848 | 0 | return (0); |
8849 | 0 | } else { |
8850 | 0 | if ((prop->ns == NULL) || |
8851 | 0 | (!xmlStrEqual(define->ns, prop->ns->href))) |
8852 | 0 | return (0); |
8853 | 0 | } |
8854 | 0 | } |
8855 | 0 | if (define->nameClass == NULL) |
8856 | 0 | return (1); |
8857 | 0 | define = define->nameClass; |
8858 | 0 | if (define->type == XML_RELAXNG_EXCEPT) { |
8859 | 0 | xmlRelaxNGDefinePtr list; |
8860 | |
|
8861 | 0 | list = define->content; |
8862 | 0 | while (list != NULL) { |
8863 | 0 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
8864 | 0 | if (ret == 1) |
8865 | 0 | return (0); |
8866 | 0 | if (ret < 0) |
8867 | 0 | return (ret); |
8868 | 0 | list = list->next; |
8869 | 0 | } |
8870 | 0 | } else if (define->type == XML_RELAXNG_CHOICE) { |
8871 | 0 | xmlRelaxNGDefinePtr list; |
8872 | |
|
8873 | 0 | list = define->nameClass; |
8874 | 0 | while (list != NULL) { |
8875 | 0 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
8876 | 0 | if (ret == 1) |
8877 | 0 | return (1); |
8878 | 0 | if (ret < 0) |
8879 | 0 | return (ret); |
8880 | 0 | list = list->next; |
8881 | 0 | } |
8882 | 0 | return (0); |
8883 | 0 | } else { |
8884 | 0 | TODO} |
8885 | 0 | return (1); |
8886 | 0 | } |
8887 | | |
8888 | | /** |
8889 | | * xmlRelaxNGValidateAttribute: |
8890 | | * @ctxt: a Relax-NG validation context |
8891 | | * @define: the definition to verify |
8892 | | * |
8893 | | * Validate the given attribute definition for that node |
8894 | | * |
8895 | | * Returns 0 if the validation succeeded or an error code. |
8896 | | */ |
8897 | | static int |
8898 | | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
8899 | | xmlRelaxNGDefinePtr define) |
8900 | 0 | { |
8901 | 0 | int ret = 0, i; |
8902 | 0 | xmlChar *value, *oldvalue; |
8903 | 0 | xmlAttrPtr prop = NULL, tmp; |
8904 | 0 | xmlNodePtr oldseq; |
8905 | |
|
8906 | 0 | if (ctxt->state->nbAttrLeft <= 0) |
8907 | 0 | return (-1); |
8908 | 0 | if (define->name != NULL) { |
8909 | 0 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
8910 | 0 | tmp = ctxt->state->attrs[i]; |
8911 | 0 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
8912 | 0 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
8913 | 0 | (tmp->ns == NULL)) || |
8914 | 0 | ((tmp->ns != NULL) && |
8915 | 0 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
8916 | 0 | prop = tmp; |
8917 | 0 | break; |
8918 | 0 | } |
8919 | 0 | } |
8920 | 0 | } |
8921 | 0 | if (prop != NULL) { |
8922 | 0 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
8923 | 0 | oldvalue = ctxt->state->value; |
8924 | 0 | oldseq = ctxt->state->seq; |
8925 | 0 | ctxt->state->seq = (xmlNodePtr) prop; |
8926 | 0 | ctxt->state->value = value; |
8927 | 0 | ctxt->state->endvalue = NULL; |
8928 | 0 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
8929 | 0 | if (ctxt->state->value != NULL) |
8930 | 0 | value = ctxt->state->value; |
8931 | 0 | if (value != NULL) |
8932 | 0 | xmlFree(value); |
8933 | 0 | ctxt->state->value = oldvalue; |
8934 | 0 | ctxt->state->seq = oldseq; |
8935 | 0 | if (ret == 0) { |
8936 | | /* |
8937 | | * flag the attribute as processed |
8938 | | */ |
8939 | 0 | ctxt->state->attrs[i] = NULL; |
8940 | 0 | ctxt->state->nbAttrLeft--; |
8941 | 0 | } |
8942 | 0 | } else { |
8943 | 0 | ret = -1; |
8944 | 0 | } |
8945 | 0 | } else { |
8946 | 0 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
8947 | 0 | tmp = ctxt->state->attrs[i]; |
8948 | 0 | if ((tmp != NULL) && |
8949 | 0 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
8950 | 0 | prop = tmp; |
8951 | 0 | break; |
8952 | 0 | } |
8953 | 0 | } |
8954 | 0 | if (prop != NULL) { |
8955 | 0 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
8956 | 0 | oldvalue = ctxt->state->value; |
8957 | 0 | oldseq = ctxt->state->seq; |
8958 | 0 | ctxt->state->seq = (xmlNodePtr) prop; |
8959 | 0 | ctxt->state->value = value; |
8960 | 0 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
8961 | 0 | if (ctxt->state->value != NULL) |
8962 | 0 | value = ctxt->state->value; |
8963 | 0 | if (value != NULL) |
8964 | 0 | xmlFree(value); |
8965 | 0 | ctxt->state->value = oldvalue; |
8966 | 0 | ctxt->state->seq = oldseq; |
8967 | 0 | if (ret == 0) { |
8968 | | /* |
8969 | | * flag the attribute as processed |
8970 | | */ |
8971 | 0 | ctxt->state->attrs[i] = NULL; |
8972 | 0 | ctxt->state->nbAttrLeft--; |
8973 | 0 | } |
8974 | 0 | } else { |
8975 | 0 | ret = -1; |
8976 | 0 | } |
8977 | 0 | } |
8978 | |
|
8979 | 0 | return (ret); |
8980 | 0 | } |
8981 | | |
8982 | | /** |
8983 | | * xmlRelaxNGValidateAttributeList: |
8984 | | * @ctxt: a Relax-NG validation context |
8985 | | * @define: the list of definition to verify |
8986 | | * |
8987 | | * Validate the given node against the list of attribute definitions |
8988 | | * |
8989 | | * Returns 0 if the validation succeeded or an error code. |
8990 | | */ |
8991 | | static int |
8992 | | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
8993 | | xmlRelaxNGDefinePtr defines) |
8994 | 0 | { |
8995 | 0 | int ret = 0, res; |
8996 | 0 | int needmore = 0; |
8997 | 0 | xmlRelaxNGDefinePtr cur; |
8998 | |
|
8999 | 0 | cur = defines; |
9000 | 0 | while (cur != NULL) { |
9001 | 0 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
9002 | 0 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
9003 | 0 | ret = -1; |
9004 | 0 | } else |
9005 | 0 | needmore = 1; |
9006 | 0 | cur = cur->next; |
9007 | 0 | } |
9008 | 0 | if (!needmore) |
9009 | 0 | return (ret); |
9010 | 0 | cur = defines; |
9011 | 0 | while (cur != NULL) { |
9012 | 0 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
9013 | 0 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
9014 | 0 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
9015 | 0 | if (res < 0) |
9016 | 0 | ret = -1; |
9017 | 0 | } else { |
9018 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
9019 | 0 | return (-1); |
9020 | 0 | } |
9021 | 0 | if (res == -1) /* continues on -2 */ |
9022 | 0 | break; |
9023 | 0 | } |
9024 | 0 | cur = cur->next; |
9025 | 0 | } |
9026 | | |
9027 | 0 | return (ret); |
9028 | 0 | } |
9029 | | |
9030 | | /** |
9031 | | * xmlRelaxNGNodeMatchesList: |
9032 | | * @node: the node |
9033 | | * @list: a NULL terminated array of definitions |
9034 | | * |
9035 | | * Check if a node can be matched by one of the definitions |
9036 | | * |
9037 | | * Returns 1 if matches 0 otherwise |
9038 | | */ |
9039 | | static int |
9040 | | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
9041 | 0 | { |
9042 | 0 | xmlRelaxNGDefinePtr cur; |
9043 | 0 | int i = 0, tmp; |
9044 | |
|
9045 | 0 | if ((node == NULL) || (list == NULL)) |
9046 | 0 | return (0); |
9047 | | |
9048 | 0 | cur = list[i++]; |
9049 | 0 | while (cur != NULL) { |
9050 | 0 | if ((node->type == XML_ELEMENT_NODE) && |
9051 | 0 | (cur->type == XML_RELAXNG_ELEMENT)) { |
9052 | 0 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
9053 | 0 | if (tmp == 1) |
9054 | 0 | return (1); |
9055 | 0 | } else if (((node->type == XML_TEXT_NODE) || |
9056 | 0 | (node->type == XML_CDATA_SECTION_NODE)) && |
9057 | 0 | ((cur->type == XML_RELAXNG_DATATYPE) || |
9058 | 0 | (cur->type == XML_RELAXNG_LIST) || |
9059 | 0 | (cur->type == XML_RELAXNG_TEXT) || |
9060 | 0 | (cur->type == XML_RELAXNG_VALUE))) { |
9061 | 0 | return (1); |
9062 | 0 | } |
9063 | 0 | cur = list[i++]; |
9064 | 0 | } |
9065 | 0 | return (0); |
9066 | 0 | } |
9067 | | |
9068 | | /** |
9069 | | * xmlRelaxNGValidateInterleave: |
9070 | | * @ctxt: a Relax-NG validation context |
9071 | | * @define: the definition to verify |
9072 | | * |
9073 | | * Validate an interleave definition for a node. |
9074 | | * |
9075 | | * Returns 0 if the validation succeeded or an error code. |
9076 | | */ |
9077 | | static int |
9078 | | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
9079 | | xmlRelaxNGDefinePtr define) |
9080 | 0 | { |
9081 | 0 | int ret = 0, i, nbgroups; |
9082 | 0 | int errNr = ctxt->errNr; |
9083 | 0 | int oldflags; |
9084 | |
|
9085 | 0 | xmlRelaxNGValidStatePtr oldstate; |
9086 | 0 | xmlRelaxNGPartitionPtr partitions; |
9087 | 0 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
9088 | 0 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
9089 | 0 | xmlNodePtr *list = NULL, *lasts = NULL; |
9090 | |
|
9091 | 0 | if (define->data != NULL) { |
9092 | 0 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
9093 | 0 | nbgroups = partitions->nbgroups; |
9094 | 0 | } else { |
9095 | 0 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
9096 | 0 | return (-1); |
9097 | 0 | } |
9098 | | /* |
9099 | | * Optimizations for MIXED |
9100 | | */ |
9101 | 0 | oldflags = ctxt->flags; |
9102 | 0 | if (define->dflags & IS_MIXED) { |
9103 | 0 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
9104 | 0 | if (nbgroups == 2) { |
9105 | | /* |
9106 | | * this is a pure <mixed> case |
9107 | | */ |
9108 | 0 | if (ctxt->state != NULL) |
9109 | 0 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
9110 | 0 | ctxt->state->seq); |
9111 | 0 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
9112 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, |
9113 | 0 | partitions->groups[1]-> |
9114 | 0 | rule); |
9115 | 0 | else |
9116 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, |
9117 | 0 | partitions->groups[0]-> |
9118 | 0 | rule); |
9119 | 0 | if (ret == 0) { |
9120 | 0 | if (ctxt->state != NULL) |
9121 | 0 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
9122 | 0 | ctxt->state-> |
9123 | 0 | seq); |
9124 | 0 | } |
9125 | 0 | ctxt->flags = oldflags; |
9126 | 0 | return (ret); |
9127 | 0 | } |
9128 | 0 | } |
9129 | | |
9130 | | /* |
9131 | | * Build arrays to store the first and last node of the chain |
9132 | | * pertaining to each group |
9133 | | */ |
9134 | 0 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
9135 | 0 | if (list == NULL) { |
9136 | 0 | xmlRngVErrMemory(ctxt, "validating\n"); |
9137 | 0 | return (-1); |
9138 | 0 | } |
9139 | 0 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
9140 | 0 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
9141 | 0 | if (lasts == NULL) { |
9142 | 0 | xmlRngVErrMemory(ctxt, "validating\n"); |
9143 | 0 | return (-1); |
9144 | 0 | } |
9145 | 0 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
9146 | | |
9147 | | /* |
9148 | | * Walk the sequence of children finding the right group and |
9149 | | * sorting them in sequences. |
9150 | | */ |
9151 | 0 | cur = ctxt->state->seq; |
9152 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
9153 | 0 | start = cur; |
9154 | 0 | while (cur != NULL) { |
9155 | 0 | ctxt->state->seq = cur; |
9156 | 0 | if ((partitions->triage != NULL) && |
9157 | 0 | (partitions->flags & IS_DETERMINIST)) { |
9158 | 0 | void *tmp = NULL; |
9159 | |
|
9160 | 0 | if ((cur->type == XML_TEXT_NODE) || |
9161 | 0 | (cur->type == XML_CDATA_SECTION_NODE)) { |
9162 | 0 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
9163 | 0 | NULL); |
9164 | 0 | } else if (cur->type == XML_ELEMENT_NODE) { |
9165 | 0 | if (cur->ns != NULL) { |
9166 | 0 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
9167 | 0 | cur->ns->href); |
9168 | 0 | if (tmp == NULL) |
9169 | 0 | tmp = xmlHashLookup2(partitions->triage, |
9170 | 0 | BAD_CAST "#any", |
9171 | 0 | cur->ns->href); |
9172 | 0 | } else |
9173 | 0 | tmp = |
9174 | 0 | xmlHashLookup2(partitions->triage, cur->name, |
9175 | 0 | NULL); |
9176 | 0 | if (tmp == NULL) |
9177 | 0 | tmp = |
9178 | 0 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
9179 | 0 | NULL); |
9180 | 0 | } |
9181 | |
|
9182 | 0 | if (tmp == NULL) { |
9183 | 0 | i = nbgroups; |
9184 | 0 | } else { |
9185 | 0 | i = ((ptrdiff_t) tmp) - 1; |
9186 | 0 | if (partitions->flags & IS_NEEDCHECK) { |
9187 | 0 | group = partitions->groups[i]; |
9188 | 0 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
9189 | 0 | i = nbgroups; |
9190 | 0 | } |
9191 | 0 | } |
9192 | 0 | } else { |
9193 | 0 | for (i = 0; i < nbgroups; i++) { |
9194 | 0 | group = partitions->groups[i]; |
9195 | 0 | if (group == NULL) |
9196 | 0 | continue; |
9197 | 0 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
9198 | 0 | break; |
9199 | 0 | } |
9200 | 0 | } |
9201 | | /* |
9202 | | * We break as soon as an element not matched is found |
9203 | | */ |
9204 | 0 | if (i >= nbgroups) { |
9205 | 0 | break; |
9206 | 0 | } |
9207 | 0 | if (lasts[i] != NULL) { |
9208 | 0 | lasts[i]->next = cur; |
9209 | 0 | lasts[i] = cur; |
9210 | 0 | } else { |
9211 | 0 | list[i] = cur; |
9212 | 0 | lasts[i] = cur; |
9213 | 0 | } |
9214 | 0 | if (cur->next != NULL) |
9215 | 0 | lastchg = cur->next; |
9216 | 0 | else |
9217 | 0 | lastchg = cur; |
9218 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
9219 | 0 | } |
9220 | 0 | if (ret != 0) { |
9221 | 0 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
9222 | 0 | ret = -1; |
9223 | 0 | goto done; |
9224 | 0 | } |
9225 | 0 | lastelem = cur; |
9226 | 0 | oldstate = ctxt->state; |
9227 | 0 | for (i = 0; i < nbgroups; i++) { |
9228 | 0 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
9229 | 0 | if (ctxt->state == NULL) { |
9230 | 0 | ret = -1; |
9231 | 0 | break; |
9232 | 0 | } |
9233 | 0 | group = partitions->groups[i]; |
9234 | 0 | if (lasts[i] != NULL) { |
9235 | 0 | last = lasts[i]->next; |
9236 | 0 | lasts[i]->next = NULL; |
9237 | 0 | } |
9238 | 0 | ctxt->state->seq = list[i]; |
9239 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
9240 | 0 | if (ret != 0) |
9241 | 0 | break; |
9242 | 0 | if (ctxt->state != NULL) { |
9243 | 0 | cur = ctxt->state->seq; |
9244 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
9245 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
9246 | 0 | oldstate = ctxt->state; |
9247 | 0 | ctxt->state = NULL; |
9248 | 0 | if (cur != NULL |
9249 | | /* there's a nasty violation of context-free unambiguities, |
9250 | | since in open-name-class context, interleave in the |
9251 | | production shall finish without caring about anything |
9252 | | else that is OK to follow in that case -- it would |
9253 | | otherwise get marked as "extra content" and would |
9254 | | hence fail the validation, hence this perhaps |
9255 | | dirty attempt to rectify such a situation */ |
9256 | 0 | && (define->parent->type != XML_RELAXNG_DEF |
9257 | 0 | || !xmlStrEqual(define->parent->name, |
9258 | 0 | (const xmlChar *) "open-name-class"))) { |
9259 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
9260 | 0 | ret = -1; |
9261 | 0 | ctxt->state = oldstate; |
9262 | 0 | goto done; |
9263 | 0 | } |
9264 | 0 | } else if (ctxt->states != NULL) { |
9265 | 0 | int j; |
9266 | 0 | int found = 0; |
9267 | 0 | int best = -1; |
9268 | 0 | int lowattr = -1; |
9269 | | |
9270 | | /* |
9271 | | * PBM: what happen if there is attributes checks in the interleaves |
9272 | | */ |
9273 | |
|
9274 | 0 | for (j = 0; j < ctxt->states->nbState; j++) { |
9275 | 0 | cur = ctxt->states->tabState[j]->seq; |
9276 | 0 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
9277 | 0 | if (cur == NULL) { |
9278 | 0 | if (found == 0) { |
9279 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9280 | 0 | best = j; |
9281 | 0 | } |
9282 | 0 | found = 1; |
9283 | 0 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
9284 | | /* try to keep the latest one to mach old heuristic */ |
9285 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9286 | 0 | best = j; |
9287 | 0 | } |
9288 | 0 | if (lowattr == 0) |
9289 | 0 | break; |
9290 | 0 | } else if (found == 0) { |
9291 | 0 | if (lowattr == -1) { |
9292 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9293 | 0 | best = j; |
9294 | 0 | } else |
9295 | 0 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
9296 | | /* try to keep the latest one to mach old heuristic */ |
9297 | 0 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
9298 | 0 | best = j; |
9299 | 0 | } |
9300 | 0 | } |
9301 | 0 | } |
9302 | | /* |
9303 | | * BIG PBM: here we pick only one restarting point :-( |
9304 | | */ |
9305 | 0 | if (ctxt->states->nbState > 0) { |
9306 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
9307 | 0 | if (best != -1) { |
9308 | 0 | oldstate = ctxt->states->tabState[best]; |
9309 | 0 | ctxt->states->tabState[best] = NULL; |
9310 | 0 | } else { |
9311 | 0 | oldstate = |
9312 | 0 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
9313 | 0 | ctxt->states->tabState[ctxt->states->nbState - 1] = NULL; |
9314 | 0 | ctxt->states->nbState--; |
9315 | 0 | } |
9316 | 0 | } |
9317 | 0 | for (j = 0; j < ctxt->states->nbState ; j++) { |
9318 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
9319 | 0 | } |
9320 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
9321 | 0 | ctxt->states = NULL; |
9322 | 0 | if (found == 0) { |
9323 | 0 | if (cur == NULL) { |
9324 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, |
9325 | 0 | (const xmlChar *) "noname"); |
9326 | 0 | } else { |
9327 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
9328 | 0 | } |
9329 | 0 | ret = -1; |
9330 | 0 | ctxt->state = oldstate; |
9331 | 0 | goto done; |
9332 | 0 | } |
9333 | 0 | } else { |
9334 | 0 | ret = -1; |
9335 | 0 | break; |
9336 | 0 | } |
9337 | 0 | if (lasts[i] != NULL) { |
9338 | 0 | lasts[i]->next = last; |
9339 | 0 | } |
9340 | 0 | } |
9341 | 0 | if (ctxt->state != NULL) |
9342 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
9343 | 0 | ctxt->state = oldstate; |
9344 | 0 | ctxt->state->seq = lastelem; |
9345 | 0 | if (ret != 0) { |
9346 | 0 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
9347 | 0 | ret = -1; |
9348 | 0 | goto done; |
9349 | 0 | } |
9350 | | |
9351 | 0 | done: |
9352 | 0 | ctxt->flags = oldflags; |
9353 | | /* |
9354 | | * builds the next links chain from the prev one |
9355 | | */ |
9356 | 0 | cur = lastchg; |
9357 | 0 | while (cur != NULL) { |
9358 | 0 | if ((cur == start) || (cur->prev == NULL)) |
9359 | 0 | break; |
9360 | 0 | cur->prev->next = cur; |
9361 | 0 | cur = cur->prev; |
9362 | 0 | } |
9363 | 0 | if (ret == 0) { |
9364 | 0 | if (ctxt->errNr > errNr) |
9365 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9366 | 0 | } |
9367 | |
|
9368 | 0 | xmlFree(list); |
9369 | 0 | xmlFree(lasts); |
9370 | 0 | return (ret); |
9371 | 0 | } |
9372 | | |
9373 | | /** |
9374 | | * xmlRelaxNGValidateDefinitionList: |
9375 | | * @ctxt: a Relax-NG validation context |
9376 | | * @define: the list of definition to verify |
9377 | | * |
9378 | | * Validate the given node content against the (list) of definitions |
9379 | | * |
9380 | | * Returns 0 if the validation succeeded or an error code. |
9381 | | */ |
9382 | | static int |
9383 | | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
9384 | | xmlRelaxNGDefinePtr defines) |
9385 | 0 | { |
9386 | 0 | int ret = 0, res; |
9387 | | |
9388 | |
|
9389 | 0 | if (defines == NULL) { |
9390 | 0 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
9391 | 0 | BAD_CAST "NULL definition list"); |
9392 | 0 | return (-1); |
9393 | 0 | } |
9394 | 0 | while (defines != NULL) { |
9395 | 0 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
9396 | 0 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
9397 | 0 | if (res < 0) |
9398 | 0 | ret = -1; |
9399 | 0 | } else { |
9400 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
9401 | 0 | return (-1); |
9402 | 0 | } |
9403 | 0 | if (res == -1) /* continues on -2 */ |
9404 | 0 | break; |
9405 | 0 | defines = defines->next; |
9406 | 0 | } |
9407 | | |
9408 | 0 | return (ret); |
9409 | 0 | } |
9410 | | |
9411 | | /** |
9412 | | * xmlRelaxNGElementMatch: |
9413 | | * @ctxt: a Relax-NG validation context |
9414 | | * @define: the definition to check |
9415 | | * @elem: the element |
9416 | | * |
9417 | | * Check if the element matches the definition nameClass |
9418 | | * |
9419 | | * Returns 1 if the element matches, 0 if no, or -1 in case of error |
9420 | | */ |
9421 | | static int |
9422 | | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
9423 | | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
9424 | 0 | { |
9425 | 0 | int ret = 0, oldflags = 0; |
9426 | |
|
9427 | 0 | if (define->name != NULL) { |
9428 | 0 | if (!xmlStrEqual(elem->name, define->name)) { |
9429 | 0 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
9430 | 0 | return (0); |
9431 | 0 | } |
9432 | 0 | } |
9433 | 0 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
9434 | 0 | if (elem->ns == NULL) { |
9435 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
9436 | 0 | return (0); |
9437 | 0 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
9438 | 0 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
9439 | 0 | elem->name, define->ns); |
9440 | 0 | return (0); |
9441 | 0 | } |
9442 | 0 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
9443 | 0 | (define->name == NULL)) { |
9444 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
9445 | 0 | return (0); |
9446 | 0 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
9447 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
9448 | 0 | return (0); |
9449 | 0 | } |
9450 | | |
9451 | 0 | if (define->nameClass == NULL) |
9452 | 0 | return (1); |
9453 | | |
9454 | 0 | define = define->nameClass; |
9455 | 0 | if (define->type == XML_RELAXNG_EXCEPT) { |
9456 | 0 | xmlRelaxNGDefinePtr list; |
9457 | |
|
9458 | 0 | if (ctxt != NULL) { |
9459 | 0 | oldflags = ctxt->flags; |
9460 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9461 | 0 | } |
9462 | |
|
9463 | 0 | list = define->content; |
9464 | 0 | while (list != NULL) { |
9465 | 0 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
9466 | 0 | if (ret == 1) { |
9467 | 0 | if (ctxt != NULL) |
9468 | 0 | ctxt->flags = oldflags; |
9469 | 0 | return (0); |
9470 | 0 | } |
9471 | 0 | if (ret < 0) { |
9472 | 0 | if (ctxt != NULL) |
9473 | 0 | ctxt->flags = oldflags; |
9474 | 0 | return (ret); |
9475 | 0 | } |
9476 | 0 | list = list->next; |
9477 | 0 | } |
9478 | 0 | ret = 1; |
9479 | 0 | if (ctxt != NULL) { |
9480 | 0 | ctxt->flags = oldflags; |
9481 | 0 | } |
9482 | 0 | } else if (define->type == XML_RELAXNG_CHOICE) { |
9483 | 0 | xmlRelaxNGDefinePtr list; |
9484 | |
|
9485 | 0 | if (ctxt != NULL) { |
9486 | 0 | oldflags = ctxt->flags; |
9487 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9488 | 0 | } |
9489 | |
|
9490 | 0 | list = define->nameClass; |
9491 | 0 | while (list != NULL) { |
9492 | 0 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
9493 | 0 | if (ret == 1) { |
9494 | 0 | if (ctxt != NULL) |
9495 | 0 | ctxt->flags = oldflags; |
9496 | 0 | return (1); |
9497 | 0 | } |
9498 | 0 | if (ret < 0) { |
9499 | 0 | if (ctxt != NULL) |
9500 | 0 | ctxt->flags = oldflags; |
9501 | 0 | return (ret); |
9502 | 0 | } |
9503 | 0 | list = list->next; |
9504 | 0 | } |
9505 | 0 | if (ctxt != NULL) { |
9506 | 0 | if (ret != 0) { |
9507 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9508 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9509 | 0 | } else { |
9510 | 0 | if (ctxt->errNr > 0) |
9511 | 0 | xmlRelaxNGPopErrors(ctxt, 0); |
9512 | 0 | } |
9513 | 0 | } |
9514 | 0 | ret = 0; |
9515 | 0 | if (ctxt != NULL) { |
9516 | 0 | ctxt->flags = oldflags; |
9517 | 0 | } |
9518 | 0 | } else { |
9519 | 0 | TODO ret = -1; |
9520 | 0 | } |
9521 | 0 | return (ret); |
9522 | 0 | } |
9523 | | |
9524 | | /** |
9525 | | * xmlRelaxNGBestState: |
9526 | | * @ctxt: a Relax-NG validation context |
9527 | | * |
9528 | | * Find the "best" state in the ctxt->states list of states to report |
9529 | | * errors about. I.e. a state with no element left in the child list |
9530 | | * or the one with the less attributes left. |
9531 | | * This is called only if a validation error was detected |
9532 | | * |
9533 | | * Returns the index of the "best" state or -1 in case of error |
9534 | | */ |
9535 | | static int |
9536 | | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
9537 | 0 | { |
9538 | 0 | xmlRelaxNGValidStatePtr state; |
9539 | 0 | int i, tmp; |
9540 | 0 | int best = -1; |
9541 | 0 | int value = 1000000; |
9542 | |
|
9543 | 0 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
9544 | 0 | (ctxt->states->nbState <= 0)) |
9545 | 0 | return (-1); |
9546 | | |
9547 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9548 | 0 | state = ctxt->states->tabState[i]; |
9549 | 0 | if (state == NULL) |
9550 | 0 | continue; |
9551 | 0 | if (state->seq != NULL) { |
9552 | 0 | if ((best == -1) || (value > 100000)) { |
9553 | 0 | value = 100000; |
9554 | 0 | best = i; |
9555 | 0 | } |
9556 | 0 | } else { |
9557 | 0 | tmp = state->nbAttrLeft; |
9558 | 0 | if ((best == -1) || (value > tmp)) { |
9559 | 0 | value = tmp; |
9560 | 0 | best = i; |
9561 | 0 | } |
9562 | 0 | } |
9563 | 0 | } |
9564 | 0 | return (best); |
9565 | 0 | } |
9566 | | |
9567 | | /** |
9568 | | * xmlRelaxNGLogBestError: |
9569 | | * @ctxt: a Relax-NG validation context |
9570 | | * |
9571 | | * Find the "best" state in the ctxt->states list of states to report |
9572 | | * errors about and log it. |
9573 | | */ |
9574 | | static void |
9575 | | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
9576 | 0 | { |
9577 | 0 | int best; |
9578 | |
|
9579 | 0 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
9580 | 0 | (ctxt->states->nbState <= 0)) |
9581 | 0 | return; |
9582 | | |
9583 | 0 | best = xmlRelaxNGBestState(ctxt); |
9584 | 0 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
9585 | 0 | ctxt->state = ctxt->states->tabState[best]; |
9586 | |
|
9587 | 0 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
9588 | 0 | } |
9589 | 0 | } |
9590 | | |
9591 | | /** |
9592 | | * xmlRelaxNGValidateElementEnd: |
9593 | | * @ctxt: a Relax-NG validation context |
9594 | | * @dolog: indicate that error logging should be done |
9595 | | * |
9596 | | * Validate the end of the element, implements check that |
9597 | | * there is nothing left not consumed in the element content |
9598 | | * or in the attribute list. |
9599 | | * |
9600 | | * Returns 0 if the validation succeeded or an error code. |
9601 | | */ |
9602 | | static int |
9603 | | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
9604 | 0 | { |
9605 | 0 | int i; |
9606 | 0 | xmlRelaxNGValidStatePtr state; |
9607 | |
|
9608 | 0 | state = ctxt->state; |
9609 | 0 | if (state->seq != NULL) { |
9610 | 0 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
9611 | 0 | if (state->seq != NULL) { |
9612 | 0 | if (dolog) { |
9613 | 0 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
9614 | 0 | state->node->name, state->seq->name); |
9615 | 0 | } |
9616 | 0 | return (-1); |
9617 | 0 | } |
9618 | 0 | } |
9619 | 0 | for (i = 0; i < state->nbAttrs; i++) { |
9620 | 0 | if (state->attrs[i] != NULL) { |
9621 | 0 | if (dolog) { |
9622 | 0 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
9623 | 0 | state->attrs[i]->name, state->node->name); |
9624 | 0 | } |
9625 | 0 | return (-1 - i); |
9626 | 0 | } |
9627 | 0 | } |
9628 | 0 | return (0); |
9629 | 0 | } |
9630 | | |
9631 | | /** |
9632 | | * xmlRelaxNGValidateState: |
9633 | | * @ctxt: a Relax-NG validation context |
9634 | | * @define: the definition to verify |
9635 | | * |
9636 | | * Validate the current state against the definition |
9637 | | * |
9638 | | * Returns 0 if the validation succeeded or an error code. |
9639 | | */ |
9640 | | static int |
9641 | | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
9642 | | xmlRelaxNGDefinePtr define) |
9643 | 0 | { |
9644 | 0 | xmlNodePtr node; |
9645 | 0 | int ret = 0, i, tmp, oldflags, errNr; |
9646 | 0 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
9647 | |
|
9648 | 0 | if (define == NULL) { |
9649 | 0 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
9650 | 0 | return (-1); |
9651 | 0 | } |
9652 | | |
9653 | 0 | if (ctxt->state != NULL) { |
9654 | 0 | node = ctxt->state->seq; |
9655 | 0 | } else { |
9656 | 0 | node = NULL; |
9657 | 0 | } |
9658 | 0 | ctxt->depth++; |
9659 | 0 | switch (define->type) { |
9660 | 0 | case XML_RELAXNG_EMPTY: |
9661 | 0 | ret = 0; |
9662 | 0 | break; |
9663 | 0 | case XML_RELAXNG_NOT_ALLOWED: |
9664 | 0 | ret = -1; |
9665 | 0 | break; |
9666 | 0 | case XML_RELAXNG_TEXT: |
9667 | 0 | while ((node != NULL) && |
9668 | 0 | ((node->type == XML_TEXT_NODE) || |
9669 | 0 | (node->type == XML_COMMENT_NODE) || |
9670 | 0 | (node->type == XML_PI_NODE) || |
9671 | 0 | (node->type == XML_CDATA_SECTION_NODE))) |
9672 | 0 | node = node->next; |
9673 | 0 | ctxt->state->seq = node; |
9674 | 0 | break; |
9675 | 0 | case XML_RELAXNG_ELEMENT: |
9676 | 0 | errNr = ctxt->errNr; |
9677 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
9678 | 0 | if (node == NULL) { |
9679 | 0 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
9680 | 0 | ret = -1; |
9681 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9682 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9683 | 0 | break; |
9684 | 0 | } |
9685 | 0 | if (node->type != XML_ELEMENT_NODE) { |
9686 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
9687 | 0 | ret = -1; |
9688 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9689 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9690 | 0 | break; |
9691 | 0 | } |
9692 | | /* |
9693 | | * This node was already validated successfully against |
9694 | | * this definition. |
9695 | | */ |
9696 | 0 | if (node->psvi == define) { |
9697 | 0 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
9698 | 0 | if (ctxt->errNr > errNr) |
9699 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9700 | 0 | if (ctxt->errNr != 0) { |
9701 | 0 | while ((ctxt->err != NULL) && |
9702 | 0 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
9703 | 0 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
9704 | 0 | || |
9705 | 0 | ((ctxt->err->err == |
9706 | 0 | XML_RELAXNG_ERR_ELEMEXTRANS) |
9707 | 0 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
9708 | 0 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
9709 | 0 | || (ctxt->err->err == |
9710 | 0 | XML_RELAXNG_ERR_NOTELEM))) |
9711 | 0 | xmlRelaxNGValidErrorPop(ctxt); |
9712 | 0 | } |
9713 | 0 | break; |
9714 | 0 | } |
9715 | | |
9716 | 0 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
9717 | 0 | if (ret <= 0) { |
9718 | 0 | ret = -1; |
9719 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9720 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9721 | 0 | break; |
9722 | 0 | } |
9723 | 0 | ret = 0; |
9724 | 0 | if (ctxt->errNr != 0) { |
9725 | 0 | if (ctxt->errNr > errNr) |
9726 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9727 | 0 | while ((ctxt->err != NULL) && |
9728 | 0 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
9729 | 0 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
9730 | 0 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
9731 | 0 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
9732 | 0 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
9733 | 0 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
9734 | 0 | xmlRelaxNGValidErrorPop(ctxt); |
9735 | 0 | } |
9736 | 0 | errNr = ctxt->errNr; |
9737 | |
|
9738 | 0 | oldflags = ctxt->flags; |
9739 | 0 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
9740 | 0 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
9741 | 0 | } |
9742 | 0 | state = xmlRelaxNGNewValidState(ctxt, node); |
9743 | 0 | if (state == NULL) { |
9744 | 0 | ret = -1; |
9745 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
9746 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9747 | 0 | break; |
9748 | 0 | } |
9749 | | |
9750 | 0 | oldstate = ctxt->state; |
9751 | 0 | ctxt->state = state; |
9752 | 0 | if (define->attrs != NULL) { |
9753 | 0 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
9754 | 0 | if (tmp != 0) { |
9755 | 0 | ret = -1; |
9756 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
9757 | 0 | } |
9758 | 0 | } |
9759 | 0 | if (define->contModel != NULL) { |
9760 | 0 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
9761 | 0 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
9762 | 0 | xmlNodePtr nseq; |
9763 | |
|
9764 | 0 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
9765 | 0 | ctxt->state = nstate; |
9766 | 0 | ctxt->states = NULL; |
9767 | |
|
9768 | 0 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
9769 | 0 | define->contModel, |
9770 | 0 | ctxt->state->seq); |
9771 | 0 | nseq = ctxt->state->seq; |
9772 | 0 | ctxt->state = tmpstate; |
9773 | 0 | ctxt->states = tmpstates; |
9774 | 0 | xmlRelaxNGFreeValidState(ctxt, nstate); |
9775 | |
|
9776 | 0 | if (tmp != 0) |
9777 | 0 | ret = -1; |
9778 | |
|
9779 | 0 | if (ctxt->states != NULL) { |
9780 | 0 | tmp = -1; |
9781 | |
|
9782 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9783 | 0 | state = ctxt->states->tabState[i]; |
9784 | 0 | ctxt->state = state; |
9785 | 0 | ctxt->state->seq = nseq; |
9786 | |
|
9787 | 0 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
9788 | 0 | tmp = 0; |
9789 | 0 | break; |
9790 | 0 | } |
9791 | 0 | } |
9792 | 0 | if (tmp != 0) { |
9793 | | /* |
9794 | | * validation error, log the message for the "best" one |
9795 | | */ |
9796 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9797 | 0 | xmlRelaxNGLogBestError(ctxt); |
9798 | 0 | } |
9799 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9800 | 0 | xmlRelaxNGFreeValidState(ctxt, |
9801 | 0 | ctxt->states-> |
9802 | 0 | tabState[i]); |
9803 | 0 | } |
9804 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
9805 | 0 | ctxt->flags = oldflags; |
9806 | 0 | ctxt->states = NULL; |
9807 | 0 | if ((ret == 0) && (tmp == -1)) |
9808 | 0 | ret = -1; |
9809 | 0 | } else { |
9810 | 0 | state = ctxt->state; |
9811 | 0 | if (ctxt->state != NULL) |
9812 | 0 | ctxt->state->seq = nseq; |
9813 | 0 | if (ret == 0) |
9814 | 0 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
9815 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
9816 | 0 | } |
9817 | 0 | } else { |
9818 | 0 | if (define->content != NULL) { |
9819 | 0 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
9820 | 0 | define-> |
9821 | 0 | content); |
9822 | 0 | if (tmp != 0) { |
9823 | 0 | ret = -1; |
9824 | 0 | if (ctxt->state == NULL) { |
9825 | 0 | ctxt->state = oldstate; |
9826 | 0 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
9827 | 0 | node->name); |
9828 | 0 | ctxt->state = NULL; |
9829 | 0 | } else { |
9830 | 0 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
9831 | 0 | node->name); |
9832 | 0 | } |
9833 | |
|
9834 | 0 | } |
9835 | 0 | } |
9836 | 0 | if (ctxt->states != NULL) { |
9837 | 0 | tmp = -1; |
9838 | |
|
9839 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9840 | 0 | state = ctxt->states->tabState[i]; |
9841 | 0 | ctxt->state = state; |
9842 | |
|
9843 | 0 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
9844 | 0 | tmp = 0; |
9845 | 0 | break; |
9846 | 0 | } |
9847 | 0 | } |
9848 | 0 | if (tmp != 0) { |
9849 | | /* |
9850 | | * validation error, log the message for the "best" one |
9851 | | */ |
9852 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9853 | 0 | xmlRelaxNGLogBestError(ctxt); |
9854 | 0 | } |
9855 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
9856 | 0 | xmlRelaxNGFreeValidState(ctxt, |
9857 | 0 | ctxt->states->tabState[i]); |
9858 | 0 | ctxt->states->tabState[i] = NULL; |
9859 | 0 | } |
9860 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
9861 | 0 | ctxt->flags = oldflags; |
9862 | 0 | ctxt->states = NULL; |
9863 | 0 | if ((ret == 0) && (tmp == -1)) |
9864 | 0 | ret = -1; |
9865 | 0 | } else { |
9866 | 0 | state = ctxt->state; |
9867 | 0 | if (ret == 0) |
9868 | 0 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
9869 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
9870 | 0 | } |
9871 | 0 | } |
9872 | 0 | if (ret == 0) { |
9873 | 0 | node->psvi = define; |
9874 | 0 | } |
9875 | 0 | ctxt->flags = oldflags; |
9876 | 0 | ctxt->state = oldstate; |
9877 | 0 | if (oldstate != NULL) |
9878 | 0 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
9879 | 0 | if (ret != 0) { |
9880 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
9881 | 0 | xmlRelaxNGDumpValidError(ctxt); |
9882 | 0 | ret = 0; |
9883 | | #if 0 |
9884 | | } else { |
9885 | | ret = -2; |
9886 | | #endif |
9887 | 0 | } |
9888 | 0 | } else { |
9889 | 0 | if (ctxt->errNr > errNr) |
9890 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9891 | 0 | } |
9892 | |
|
9893 | 0 | break; |
9894 | 0 | case XML_RELAXNG_OPTIONAL:{ |
9895 | 0 | errNr = ctxt->errNr; |
9896 | 0 | oldflags = ctxt->flags; |
9897 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9898 | 0 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
9899 | 0 | ret = |
9900 | 0 | xmlRelaxNGValidateDefinitionList(ctxt, |
9901 | 0 | define->content); |
9902 | 0 | if (ret != 0) { |
9903 | 0 | if (ctxt->state != NULL) |
9904 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
9905 | 0 | ctxt->state = oldstate; |
9906 | 0 | ctxt->flags = oldflags; |
9907 | 0 | ret = 0; |
9908 | 0 | if (ctxt->errNr > errNr) |
9909 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9910 | 0 | break; |
9911 | 0 | } |
9912 | 0 | if (ctxt->states != NULL) { |
9913 | 0 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
9914 | 0 | } else { |
9915 | 0 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
9916 | 0 | if (ctxt->states == NULL) { |
9917 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
9918 | 0 | ctxt->flags = oldflags; |
9919 | 0 | ret = -1; |
9920 | 0 | if (ctxt->errNr > errNr) |
9921 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9922 | 0 | break; |
9923 | 0 | } |
9924 | 0 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
9925 | 0 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
9926 | 0 | ctxt->state = NULL; |
9927 | 0 | } |
9928 | 0 | ctxt->flags = oldflags; |
9929 | 0 | ret = 0; |
9930 | 0 | if (ctxt->errNr > errNr) |
9931 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9932 | 0 | break; |
9933 | 0 | } |
9934 | 0 | case XML_RELAXNG_ONEORMORE: |
9935 | 0 | errNr = ctxt->errNr; |
9936 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
9937 | 0 | if (ret != 0) { |
9938 | 0 | break; |
9939 | 0 | } |
9940 | 0 | if (ctxt->errNr > errNr) |
9941 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
9942 | | /* Falls through. */ |
9943 | 0 | case XML_RELAXNG_ZEROORMORE:{ |
9944 | 0 | int progress; |
9945 | 0 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
9946 | 0 | int base, j; |
9947 | |
|
9948 | 0 | errNr = ctxt->errNr; |
9949 | 0 | res = xmlRelaxNGNewStates(ctxt, 1); |
9950 | 0 | if (res == NULL) { |
9951 | 0 | ret = -1; |
9952 | 0 | break; |
9953 | 0 | } |
9954 | | /* |
9955 | | * All the input states are also exit states |
9956 | | */ |
9957 | 0 | if (ctxt->state != NULL) { |
9958 | 0 | xmlRelaxNGAddStates(ctxt, res, |
9959 | 0 | xmlRelaxNGCopyValidState(ctxt, |
9960 | 0 | ctxt-> |
9961 | 0 | state)); |
9962 | 0 | } else { |
9963 | 0 | for (j = 0; j < ctxt->states->nbState; j++) { |
9964 | 0 | xmlRelaxNGAddStates(ctxt, res, |
9965 | 0 | xmlRelaxNGCopyValidState(ctxt, |
9966 | 0 | ctxt->states->tabState[j])); |
9967 | 0 | } |
9968 | 0 | } |
9969 | 0 | oldflags = ctxt->flags; |
9970 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
9971 | 0 | do { |
9972 | 0 | progress = 0; |
9973 | 0 | base = res->nbState; |
9974 | |
|
9975 | 0 | if (ctxt->states != NULL) { |
9976 | 0 | states = ctxt->states; |
9977 | 0 | for (i = 0; i < states->nbState; i++) { |
9978 | 0 | ctxt->state = states->tabState[i]; |
9979 | 0 | ctxt->states = NULL; |
9980 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
9981 | 0 | define-> |
9982 | 0 | content); |
9983 | 0 | if (ret == 0) { |
9984 | 0 | if (ctxt->state != NULL) { |
9985 | 0 | tmp = xmlRelaxNGAddStates(ctxt, res, |
9986 | 0 | ctxt->state); |
9987 | 0 | ctxt->state = NULL; |
9988 | 0 | if (tmp == 1) |
9989 | 0 | progress = 1; |
9990 | 0 | } else if (ctxt->states != NULL) { |
9991 | 0 | for (j = 0; j < ctxt->states->nbState; |
9992 | 0 | j++) { |
9993 | 0 | tmp = |
9994 | 0 | xmlRelaxNGAddStates(ctxt, res, |
9995 | 0 | ctxt->states->tabState[j]); |
9996 | 0 | if (tmp == 1) |
9997 | 0 | progress = 1; |
9998 | 0 | } |
9999 | 0 | xmlRelaxNGFreeStates(ctxt, |
10000 | 0 | ctxt->states); |
10001 | 0 | ctxt->states = NULL; |
10002 | 0 | } |
10003 | 0 | } else { |
10004 | 0 | if (ctxt->state != NULL) { |
10005 | 0 | xmlRelaxNGFreeValidState(ctxt, |
10006 | 0 | ctxt->state); |
10007 | 0 | ctxt->state = NULL; |
10008 | 0 | } |
10009 | 0 | } |
10010 | 0 | } |
10011 | 0 | } else { |
10012 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
10013 | 0 | define-> |
10014 | 0 | content); |
10015 | 0 | if (ret != 0) { |
10016 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10017 | 0 | ctxt->state = NULL; |
10018 | 0 | } else { |
10019 | 0 | base = res->nbState; |
10020 | 0 | if (ctxt->state != NULL) { |
10021 | 0 | tmp = xmlRelaxNGAddStates(ctxt, res, |
10022 | 0 | ctxt->state); |
10023 | 0 | ctxt->state = NULL; |
10024 | 0 | if (tmp == 1) |
10025 | 0 | progress = 1; |
10026 | 0 | } else if (ctxt->states != NULL) { |
10027 | 0 | for (j = 0; j < ctxt->states->nbState; j++) { |
10028 | 0 | tmp = xmlRelaxNGAddStates(ctxt, res, |
10029 | 0 | ctxt->states->tabState[j]); |
10030 | 0 | if (tmp == 1) |
10031 | 0 | progress = 1; |
10032 | 0 | } |
10033 | 0 | if (states == NULL) { |
10034 | 0 | states = ctxt->states; |
10035 | 0 | } else { |
10036 | 0 | xmlRelaxNGFreeStates(ctxt, |
10037 | 0 | ctxt->states); |
10038 | 0 | } |
10039 | 0 | ctxt->states = NULL; |
10040 | 0 | } |
10041 | 0 | } |
10042 | 0 | } |
10043 | 0 | if (progress) { |
10044 | | /* |
10045 | | * Collect all the new nodes added at that step |
10046 | | * and make them the new node set |
10047 | | */ |
10048 | 0 | if (res->nbState - base == 1) { |
10049 | 0 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
10050 | 0 | res-> |
10051 | 0 | tabState |
10052 | 0 | [base]); |
10053 | 0 | } else { |
10054 | 0 | if (states == NULL) { |
10055 | 0 | xmlRelaxNGNewStates(ctxt, |
10056 | 0 | res->nbState - base); |
10057 | 0 | states = ctxt->states; |
10058 | 0 | if (states == NULL) { |
10059 | 0 | progress = 0; |
10060 | 0 | break; |
10061 | 0 | } |
10062 | 0 | } |
10063 | 0 | states->nbState = 0; |
10064 | 0 | for (i = base; i < res->nbState; i++) |
10065 | 0 | xmlRelaxNGAddStates(ctxt, states, |
10066 | 0 | xmlRelaxNGCopyValidState |
10067 | 0 | (ctxt, res->tabState[i])); |
10068 | 0 | ctxt->states = states; |
10069 | 0 | } |
10070 | 0 | } |
10071 | 0 | } while (progress == 1); |
10072 | 0 | if (states != NULL) { |
10073 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10074 | 0 | } |
10075 | 0 | ctxt->states = res; |
10076 | 0 | ctxt->flags = oldflags; |
10077 | | #if 0 |
10078 | | /* |
10079 | | * errors may have to be propagated back... |
10080 | | */ |
10081 | | if (ctxt->errNr > errNr) |
10082 | | xmlRelaxNGPopErrors(ctxt, errNr); |
10083 | | #endif |
10084 | 0 | ret = 0; |
10085 | 0 | break; |
10086 | 0 | } |
10087 | 0 | case XML_RELAXNG_CHOICE:{ |
10088 | 0 | xmlRelaxNGDefinePtr list = NULL; |
10089 | 0 | xmlRelaxNGStatesPtr states = NULL; |
10090 | |
|
10091 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
10092 | |
|
10093 | 0 | errNr = ctxt->errNr; |
10094 | 0 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL) && |
10095 | 0 | (node != NULL)) { |
10096 | | /* |
10097 | | * node == NULL can't be optimized since IS_TRIABLE |
10098 | | * doesn't account for choice which may lead to |
10099 | | * only attributes. |
10100 | | */ |
10101 | 0 | xmlHashTablePtr triage = |
10102 | 0 | (xmlHashTablePtr) define->data; |
10103 | | |
10104 | | /* |
10105 | | * Something we can optimize cleanly there is only one |
10106 | | * possible branch out ! |
10107 | | */ |
10108 | 0 | if ((node->type == XML_TEXT_NODE) || |
10109 | 0 | (node->type == XML_CDATA_SECTION_NODE)) { |
10110 | 0 | list = |
10111 | 0 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
10112 | 0 | } else if (node->type == XML_ELEMENT_NODE) { |
10113 | 0 | if (node->ns != NULL) { |
10114 | 0 | list = xmlHashLookup2(triage, node->name, |
10115 | 0 | node->ns->href); |
10116 | 0 | if (list == NULL) |
10117 | 0 | list = |
10118 | 0 | xmlHashLookup2(triage, BAD_CAST "#any", |
10119 | 0 | node->ns->href); |
10120 | 0 | } else |
10121 | 0 | list = |
10122 | 0 | xmlHashLookup2(triage, node->name, NULL); |
10123 | 0 | if (list == NULL) |
10124 | 0 | list = |
10125 | 0 | xmlHashLookup2(triage, BAD_CAST "#any", |
10126 | 0 | NULL); |
10127 | 0 | } |
10128 | 0 | if (list == NULL) { |
10129 | 0 | ret = -1; |
10130 | 0 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
10131 | 0 | break; |
10132 | 0 | } |
10133 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
10134 | 0 | if (ret == 0) { |
10135 | 0 | } |
10136 | 0 | break; |
10137 | 0 | } |
10138 | | |
10139 | 0 | list = define->content; |
10140 | 0 | oldflags = ctxt->flags; |
10141 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
10142 | |
|
10143 | 0 | while (list != NULL) { |
10144 | 0 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
10145 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
10146 | 0 | if (ret == 0) { |
10147 | 0 | if (states == NULL) { |
10148 | 0 | states = xmlRelaxNGNewStates(ctxt, 1); |
10149 | 0 | } |
10150 | 0 | if (ctxt->state != NULL) { |
10151 | 0 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
10152 | 0 | } else if (ctxt->states != NULL) { |
10153 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
10154 | 0 | xmlRelaxNGAddStates(ctxt, states, |
10155 | 0 | ctxt->states-> |
10156 | 0 | tabState[i]); |
10157 | 0 | } |
10158 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10159 | 0 | ctxt->states = NULL; |
10160 | 0 | } |
10161 | 0 | } else { |
10162 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10163 | 0 | } |
10164 | 0 | ctxt->state = oldstate; |
10165 | 0 | list = list->next; |
10166 | 0 | } |
10167 | 0 | if (states != NULL) { |
10168 | 0 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
10169 | 0 | ctxt->states = states; |
10170 | 0 | ctxt->state = NULL; |
10171 | 0 | ret = 0; |
10172 | 0 | } else { |
10173 | 0 | ctxt->states = NULL; |
10174 | 0 | } |
10175 | 0 | ctxt->flags = oldflags; |
10176 | 0 | if (ret != 0) { |
10177 | 0 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
10178 | 0 | xmlRelaxNGDumpValidError(ctxt); |
10179 | 0 | } |
10180 | 0 | } else { |
10181 | 0 | if (ctxt->errNr > errNr) |
10182 | 0 | xmlRelaxNGPopErrors(ctxt, errNr); |
10183 | 0 | } |
10184 | 0 | break; |
10185 | 0 | } |
10186 | 0 | case XML_RELAXNG_DEF: |
10187 | 0 | case XML_RELAXNG_GROUP: |
10188 | 0 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
10189 | 0 | break; |
10190 | 0 | case XML_RELAXNG_INTERLEAVE: |
10191 | 0 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
10192 | 0 | break; |
10193 | 0 | case XML_RELAXNG_ATTRIBUTE: |
10194 | 0 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
10195 | 0 | break; |
10196 | 0 | case XML_RELAXNG_START: |
10197 | 0 | case XML_RELAXNG_NOOP: |
10198 | 0 | case XML_RELAXNG_REF: |
10199 | 0 | case XML_RELAXNG_EXTERNALREF: |
10200 | 0 | case XML_RELAXNG_PARENTREF: |
10201 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
10202 | 0 | break; |
10203 | 0 | case XML_RELAXNG_DATATYPE:{ |
10204 | 0 | xmlNodePtr child; |
10205 | 0 | xmlChar *content = NULL; |
10206 | |
|
10207 | 0 | child = node; |
10208 | 0 | while (child != NULL) { |
10209 | 0 | if (child->type == XML_ELEMENT_NODE) { |
10210 | 0 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
10211 | 0 | node->parent->name); |
10212 | 0 | ret = -1; |
10213 | 0 | break; |
10214 | 0 | } else if ((child->type == XML_TEXT_NODE) || |
10215 | 0 | (child->type == XML_CDATA_SECTION_NODE)) { |
10216 | 0 | content = xmlStrcat(content, child->content); |
10217 | 0 | } |
10218 | | /* TODO: handle entities ... */ |
10219 | 0 | child = child->next; |
10220 | 0 | } |
10221 | 0 | if (ret == -1) { |
10222 | 0 | if (content != NULL) |
10223 | 0 | xmlFree(content); |
10224 | 0 | break; |
10225 | 0 | } |
10226 | 0 | if (content == NULL) { |
10227 | 0 | content = xmlStrdup(BAD_CAST ""); |
10228 | 0 | if (content == NULL) { |
10229 | 0 | xmlRngVErrMemory(ctxt, "validating\n"); |
10230 | 0 | ret = -1; |
10231 | 0 | break; |
10232 | 0 | } |
10233 | 0 | } |
10234 | 0 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
10235 | 0 | ctxt->state->seq); |
10236 | 0 | if (ret == -1) { |
10237 | 0 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
10238 | 0 | } else if (ret == 0) { |
10239 | 0 | ctxt->state->seq = NULL; |
10240 | 0 | } |
10241 | 0 | if (content != NULL) |
10242 | 0 | xmlFree(content); |
10243 | 0 | break; |
10244 | 0 | } |
10245 | 0 | case XML_RELAXNG_VALUE:{ |
10246 | 0 | xmlChar *content = NULL; |
10247 | 0 | xmlChar *oldvalue; |
10248 | 0 | xmlNodePtr child; |
10249 | |
|
10250 | 0 | child = node; |
10251 | 0 | while (child != NULL) { |
10252 | 0 | if (child->type == XML_ELEMENT_NODE) { |
10253 | 0 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
10254 | 0 | node->parent->name); |
10255 | 0 | ret = -1; |
10256 | 0 | break; |
10257 | 0 | } else if ((child->type == XML_TEXT_NODE) || |
10258 | 0 | (child->type == XML_CDATA_SECTION_NODE)) { |
10259 | 0 | content = xmlStrcat(content, child->content); |
10260 | 0 | } |
10261 | | /* TODO: handle entities ... */ |
10262 | 0 | child = child->next; |
10263 | 0 | } |
10264 | 0 | if (ret == -1) { |
10265 | 0 | if (content != NULL) |
10266 | 0 | xmlFree(content); |
10267 | 0 | break; |
10268 | 0 | } |
10269 | 0 | if (content == NULL) { |
10270 | 0 | content = xmlStrdup(BAD_CAST ""); |
10271 | 0 | if (content == NULL) { |
10272 | 0 | xmlRngVErrMemory(ctxt, "validating\n"); |
10273 | 0 | ret = -1; |
10274 | 0 | break; |
10275 | 0 | } |
10276 | 0 | } |
10277 | 0 | oldvalue = ctxt->state->value; |
10278 | 0 | ctxt->state->value = content; |
10279 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define); |
10280 | 0 | ctxt->state->value = oldvalue; |
10281 | 0 | if (ret == -1) { |
10282 | 0 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
10283 | 0 | } else if (ret == 0) { |
10284 | 0 | ctxt->state->seq = NULL; |
10285 | 0 | } |
10286 | 0 | if (content != NULL) |
10287 | 0 | xmlFree(content); |
10288 | 0 | break; |
10289 | 0 | } |
10290 | 0 | case XML_RELAXNG_LIST:{ |
10291 | 0 | xmlChar *content; |
10292 | 0 | xmlNodePtr child; |
10293 | 0 | xmlChar *oldvalue, *oldendvalue; |
10294 | 0 | int len; |
10295 | | |
10296 | | /* |
10297 | | * Make sure it's only text nodes |
10298 | | */ |
10299 | |
|
10300 | 0 | content = NULL; |
10301 | 0 | child = node; |
10302 | 0 | while (child != NULL) { |
10303 | 0 | if (child->type == XML_ELEMENT_NODE) { |
10304 | 0 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
10305 | 0 | node->parent->name); |
10306 | 0 | ret = -1; |
10307 | 0 | break; |
10308 | 0 | } else if ((child->type == XML_TEXT_NODE) || |
10309 | 0 | (child->type == XML_CDATA_SECTION_NODE)) { |
10310 | 0 | content = xmlStrcat(content, child->content); |
10311 | 0 | } |
10312 | | /* TODO: handle entities ... */ |
10313 | 0 | child = child->next; |
10314 | 0 | } |
10315 | 0 | if (ret == -1) { |
10316 | 0 | if (content != NULL) |
10317 | 0 | xmlFree(content); |
10318 | 0 | break; |
10319 | 0 | } |
10320 | 0 | if (content == NULL) { |
10321 | 0 | content = xmlStrdup(BAD_CAST ""); |
10322 | 0 | if (content == NULL) { |
10323 | 0 | xmlRngVErrMemory(ctxt, "validating\n"); |
10324 | 0 | ret = -1; |
10325 | 0 | break; |
10326 | 0 | } |
10327 | 0 | } |
10328 | 0 | len = xmlStrlen(content); |
10329 | 0 | oldvalue = ctxt->state->value; |
10330 | 0 | oldendvalue = ctxt->state->endvalue; |
10331 | 0 | ctxt->state->value = content; |
10332 | 0 | ctxt->state->endvalue = content + len; |
10333 | 0 | ret = xmlRelaxNGValidateValue(ctxt, define); |
10334 | 0 | ctxt->state->value = oldvalue; |
10335 | 0 | ctxt->state->endvalue = oldendvalue; |
10336 | 0 | if (ret == -1) { |
10337 | 0 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
10338 | 0 | } else if ((ret == 0) && (node != NULL)) { |
10339 | 0 | ctxt->state->seq = node->next; |
10340 | 0 | } |
10341 | 0 | if (content != NULL) |
10342 | 0 | xmlFree(content); |
10343 | 0 | break; |
10344 | 0 | } |
10345 | 0 | case XML_RELAXNG_EXCEPT: |
10346 | 0 | case XML_RELAXNG_PARAM: |
10347 | 0 | TODO ret = -1; |
10348 | 0 | break; |
10349 | 0 | } |
10350 | 0 | ctxt->depth--; |
10351 | 0 | return (ret); |
10352 | 0 | } |
10353 | | |
10354 | | /** |
10355 | | * xmlRelaxNGValidateDefinition: |
10356 | | * @ctxt: a Relax-NG validation context |
10357 | | * @define: the definition to verify |
10358 | | * |
10359 | | * Validate the current node lists against the definition |
10360 | | * |
10361 | | * Returns 0 if the validation succeeded or an error code. |
10362 | | */ |
10363 | | static int |
10364 | | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
10365 | | xmlRelaxNGDefinePtr define) |
10366 | 0 | { |
10367 | 0 | xmlRelaxNGStatesPtr states, res; |
10368 | 0 | int i, j, k, ret, oldflags; |
10369 | | |
10370 | | /* |
10371 | | * We should NOT have both ctxt->state and ctxt->states |
10372 | | */ |
10373 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10374 | 0 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10375 | 0 | ctxt->state = NULL; |
10376 | 0 | } |
10377 | |
|
10378 | 0 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
10379 | 0 | if (ctxt->states != NULL) { |
10380 | 0 | ctxt->state = ctxt->states->tabState[0]; |
10381 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10382 | 0 | ctxt->states = NULL; |
10383 | 0 | } |
10384 | 0 | ret = xmlRelaxNGValidateState(ctxt, define); |
10385 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10386 | 0 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10387 | 0 | ctxt->state = NULL; |
10388 | 0 | } |
10389 | 0 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
10390 | 0 | ctxt->state = ctxt->states->tabState[0]; |
10391 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10392 | 0 | ctxt->states = NULL; |
10393 | 0 | } |
10394 | 0 | return (ret); |
10395 | 0 | } |
10396 | | |
10397 | 0 | states = ctxt->states; |
10398 | 0 | ctxt->states = NULL; |
10399 | 0 | res = NULL; |
10400 | 0 | j = 0; |
10401 | 0 | oldflags = ctxt->flags; |
10402 | 0 | ctxt->flags |= FLAGS_IGNORABLE; |
10403 | 0 | for (i = 0; i < states->nbState; i++) { |
10404 | 0 | ctxt->state = states->tabState[i]; |
10405 | 0 | ctxt->states = NULL; |
10406 | 0 | ret = xmlRelaxNGValidateState(ctxt, define); |
10407 | | /* |
10408 | | * We should NOT have both ctxt->state and ctxt->states |
10409 | | */ |
10410 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10411 | 0 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10412 | 0 | ctxt->state = NULL; |
10413 | 0 | } |
10414 | 0 | if (ret == 0) { |
10415 | 0 | if (ctxt->states == NULL) { |
10416 | 0 | if (res != NULL) { |
10417 | | /* add the state to the container */ |
10418 | 0 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
10419 | 0 | ctxt->state = NULL; |
10420 | 0 | } else { |
10421 | | /* add the state directly in states */ |
10422 | 0 | states->tabState[j++] = ctxt->state; |
10423 | 0 | ctxt->state = NULL; |
10424 | 0 | } |
10425 | 0 | } else { |
10426 | 0 | if (res == NULL) { |
10427 | | /* make it the new container and copy other results */ |
10428 | 0 | res = ctxt->states; |
10429 | 0 | ctxt->states = NULL; |
10430 | 0 | for (k = 0; k < j; k++) |
10431 | 0 | xmlRelaxNGAddStates(ctxt, res, |
10432 | 0 | states->tabState[k]); |
10433 | 0 | } else { |
10434 | | /* add all the new results to res and reff the container */ |
10435 | 0 | for (k = 0; k < ctxt->states->nbState; k++) |
10436 | 0 | xmlRelaxNGAddStates(ctxt, res, |
10437 | 0 | ctxt->states->tabState[k]); |
10438 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10439 | 0 | ctxt->states = NULL; |
10440 | 0 | } |
10441 | 0 | } |
10442 | 0 | } else { |
10443 | 0 | if (ctxt->state != NULL) { |
10444 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10445 | 0 | ctxt->state = NULL; |
10446 | 0 | } else if (ctxt->states != NULL) { |
10447 | 0 | for (k = 0; k < ctxt->states->nbState; k++) |
10448 | 0 | xmlRelaxNGFreeValidState(ctxt, |
10449 | 0 | ctxt->states->tabState[k]); |
10450 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10451 | 0 | ctxt->states = NULL; |
10452 | 0 | } |
10453 | 0 | } |
10454 | 0 | } |
10455 | 0 | ctxt->flags = oldflags; |
10456 | 0 | if (res != NULL) { |
10457 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10458 | 0 | ctxt->states = res; |
10459 | 0 | ret = 0; |
10460 | 0 | } else if (j > 1) { |
10461 | 0 | states->nbState = j; |
10462 | 0 | ctxt->states = states; |
10463 | 0 | ret = 0; |
10464 | 0 | } else if (j == 1) { |
10465 | 0 | ctxt->state = states->tabState[0]; |
10466 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10467 | 0 | ret = 0; |
10468 | 0 | } else { |
10469 | 0 | ret = -1; |
10470 | 0 | xmlRelaxNGFreeStates(ctxt, states); |
10471 | 0 | if (ctxt->states != NULL) { |
10472 | 0 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
10473 | 0 | ctxt->states = NULL; |
10474 | 0 | } |
10475 | 0 | } |
10476 | 0 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
10477 | 0 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10478 | 0 | ctxt->state = NULL; |
10479 | 0 | } |
10480 | 0 | return (ret); |
10481 | 0 | } |
10482 | | |
10483 | | /** |
10484 | | * xmlRelaxNGValidateDocument: |
10485 | | * @ctxt: a Relax-NG validation context |
10486 | | * @doc: the document |
10487 | | * |
10488 | | * Validate the given document |
10489 | | * |
10490 | | * Returns 0 if the validation succeeded or an error code. |
10491 | | */ |
10492 | | static int |
10493 | | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
10494 | 0 | { |
10495 | 0 | int ret; |
10496 | 0 | xmlRelaxNGPtr schema; |
10497 | 0 | xmlRelaxNGGrammarPtr grammar; |
10498 | 0 | xmlRelaxNGValidStatePtr state; |
10499 | 0 | xmlNodePtr node; |
10500 | |
|
10501 | 0 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
10502 | 0 | return (-1); |
10503 | | |
10504 | 0 | ctxt->errNo = XML_RELAXNG_OK; |
10505 | 0 | schema = ctxt->schema; |
10506 | 0 | grammar = schema->topgrammar; |
10507 | 0 | if (grammar == NULL) { |
10508 | 0 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
10509 | 0 | return (-1); |
10510 | 0 | } |
10511 | 0 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
10512 | 0 | ctxt->state = state; |
10513 | 0 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
10514 | 0 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
10515 | 0 | state = ctxt->state; |
10516 | 0 | node = state->seq; |
10517 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
10518 | 0 | if (node != NULL) { |
10519 | 0 | if (ret != -1) { |
10520 | 0 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
10521 | 0 | ret = -1; |
10522 | 0 | } |
10523 | 0 | } |
10524 | 0 | } else if (ctxt->states != NULL) { |
10525 | 0 | int i; |
10526 | 0 | int tmp = -1; |
10527 | |
|
10528 | 0 | for (i = 0; i < ctxt->states->nbState; i++) { |
10529 | 0 | state = ctxt->states->tabState[i]; |
10530 | 0 | node = state->seq; |
10531 | 0 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
10532 | 0 | if (node == NULL) |
10533 | 0 | tmp = 0; |
10534 | 0 | xmlRelaxNGFreeValidState(ctxt, state); |
10535 | 0 | } |
10536 | 0 | if (tmp == -1) { |
10537 | 0 | if (ret != -1) { |
10538 | 0 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
10539 | 0 | ret = -1; |
10540 | 0 | } |
10541 | 0 | } |
10542 | 0 | } |
10543 | 0 | if (ctxt->state != NULL) { |
10544 | 0 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
10545 | 0 | ctxt->state = NULL; |
10546 | 0 | } |
10547 | 0 | if (ret != 0) |
10548 | 0 | xmlRelaxNGDumpValidError(ctxt); |
10549 | 0 | #ifdef LIBXML_VALID_ENABLED |
10550 | 0 | if (ctxt->idref == 1) { |
10551 | 0 | xmlValidCtxt vctxt; |
10552 | |
|
10553 | 0 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
10554 | 0 | vctxt.valid = 1; |
10555 | 0 | vctxt.error = ctxt->error; |
10556 | 0 | vctxt.warning = ctxt->warning; |
10557 | 0 | vctxt.userData = ctxt->userData; |
10558 | |
|
10559 | 0 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
10560 | 0 | ret = -1; |
10561 | 0 | } |
10562 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
10563 | 0 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
10564 | 0 | ret = -1; |
10565 | |
|
10566 | 0 | return (ret); |
10567 | 0 | } |
10568 | | |
10569 | | /** |
10570 | | * xmlRelaxNGCleanPSVI: |
10571 | | * @node: an input element or document |
10572 | | * |
10573 | | * Call this routine to speed up XPath computation on static documents. |
10574 | | * This stamps all the element nodes with the document order |
10575 | | * Like for line information, the order is kept in the element->content |
10576 | | * field, the value stored is actually - the node number (starting at -1) |
10577 | | * to be able to differentiate from line numbers. |
10578 | | * |
10579 | | * Returns the number of elements found in the document or -1 in case |
10580 | | * of error. |
10581 | | */ |
10582 | | static void |
10583 | 0 | xmlRelaxNGCleanPSVI(xmlNodePtr node) { |
10584 | 0 | xmlNodePtr cur; |
10585 | |
|
10586 | 0 | if ((node == NULL) || |
10587 | 0 | ((node->type != XML_ELEMENT_NODE) && |
10588 | 0 | (node->type != XML_DOCUMENT_NODE) && |
10589 | 0 | (node->type != XML_HTML_DOCUMENT_NODE))) |
10590 | 0 | return; |
10591 | 0 | if (node->type == XML_ELEMENT_NODE) |
10592 | 0 | node->psvi = NULL; |
10593 | |
|
10594 | 0 | cur = node->children; |
10595 | 0 | while (cur != NULL) { |
10596 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
10597 | 0 | cur->psvi = NULL; |
10598 | 0 | if (cur->children != NULL) { |
10599 | 0 | cur = cur->children; |
10600 | 0 | continue; |
10601 | 0 | } |
10602 | 0 | } |
10603 | 0 | if (cur->next != NULL) { |
10604 | 0 | cur = cur->next; |
10605 | 0 | continue; |
10606 | 0 | } |
10607 | 0 | do { |
10608 | 0 | cur = cur->parent; |
10609 | 0 | if (cur == NULL) |
10610 | 0 | break; |
10611 | 0 | if (cur == node) { |
10612 | 0 | cur = NULL; |
10613 | 0 | break; |
10614 | 0 | } |
10615 | 0 | if (cur->next != NULL) { |
10616 | 0 | cur = cur->next; |
10617 | 0 | break; |
10618 | 0 | } |
10619 | 0 | } while (cur != NULL); |
10620 | 0 | } |
10621 | 0 | return; |
10622 | 0 | } |
10623 | | /************************************************************************ |
10624 | | * * |
10625 | | * Validation interfaces * |
10626 | | * * |
10627 | | ************************************************************************/ |
10628 | | |
10629 | | /** |
10630 | | * xmlRelaxNGNewValidCtxt: |
10631 | | * @schema: a precompiled XML RelaxNGs |
10632 | | * |
10633 | | * Create an XML RelaxNGs validation context based on the given schema |
10634 | | * |
10635 | | * Returns the validation context or NULL in case of error |
10636 | | */ |
10637 | | xmlRelaxNGValidCtxtPtr |
10638 | | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
10639 | 0 | { |
10640 | 0 | xmlRelaxNGValidCtxtPtr ret; |
10641 | |
|
10642 | 0 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
10643 | 0 | if (ret == NULL) { |
10644 | 0 | xmlRngVErrMemory(NULL, "building context\n"); |
10645 | 0 | return (NULL); |
10646 | 0 | } |
10647 | 0 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
10648 | 0 | ret->schema = schema; |
10649 | 0 | ret->error = xmlGenericError; |
10650 | 0 | ret->userData = xmlGenericErrorContext; |
10651 | 0 | ret->errNr = 0; |
10652 | 0 | ret->errMax = 0; |
10653 | 0 | ret->err = NULL; |
10654 | 0 | ret->errTab = NULL; |
10655 | 0 | if (schema != NULL) |
10656 | 0 | ret->idref = schema->idref; |
10657 | 0 | ret->states = NULL; |
10658 | 0 | ret->freeState = NULL; |
10659 | 0 | ret->freeStates = NULL; |
10660 | 0 | ret->errNo = XML_RELAXNG_OK; |
10661 | 0 | return (ret); |
10662 | 0 | } |
10663 | | |
10664 | | /** |
10665 | | * xmlRelaxNGFreeValidCtxt: |
10666 | | * @ctxt: the schema validation context |
10667 | | * |
10668 | | * Free the resources associated to the schema validation context |
10669 | | */ |
10670 | | void |
10671 | | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
10672 | 0 | { |
10673 | 0 | int k; |
10674 | |
|
10675 | 0 | if (ctxt == NULL) |
10676 | 0 | return; |
10677 | 0 | if (ctxt->states != NULL) |
10678 | 0 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
10679 | 0 | if (ctxt->freeState != NULL) { |
10680 | 0 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
10681 | 0 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
10682 | 0 | } |
10683 | 0 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
10684 | 0 | } |
10685 | 0 | if (ctxt->freeStates != NULL) { |
10686 | 0 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
10687 | 0 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
10688 | 0 | } |
10689 | 0 | xmlFree(ctxt->freeStates); |
10690 | 0 | } |
10691 | 0 | if (ctxt->errTab != NULL) |
10692 | 0 | xmlFree(ctxt->errTab); |
10693 | 0 | if (ctxt->elemTab != NULL) { |
10694 | 0 | xmlRegExecCtxtPtr exec; |
10695 | |
|
10696 | 0 | exec = xmlRelaxNGElemPop(ctxt); |
10697 | 0 | while (exec != NULL) { |
10698 | 0 | xmlRegFreeExecCtxt(exec); |
10699 | 0 | exec = xmlRelaxNGElemPop(ctxt); |
10700 | 0 | } |
10701 | 0 | xmlFree(ctxt->elemTab); |
10702 | 0 | } |
10703 | 0 | xmlFree(ctxt); |
10704 | 0 | } |
10705 | | |
10706 | | /** |
10707 | | * xmlRelaxNGSetValidErrors: |
10708 | | * @ctxt: a Relax-NG validation context |
10709 | | * @err: the error function |
10710 | | * @warn: the warning function |
10711 | | * @ctx: the functions context |
10712 | | * |
10713 | | * Set the error and warning callback information |
10714 | | */ |
10715 | | void |
10716 | | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
10717 | | xmlRelaxNGValidityErrorFunc err, |
10718 | | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
10719 | 0 | { |
10720 | 0 | if (ctxt == NULL) |
10721 | 0 | return; |
10722 | 0 | ctxt->error = err; |
10723 | 0 | ctxt->warning = warn; |
10724 | 0 | ctxt->userData = ctx; |
10725 | 0 | ctxt->serror = NULL; |
10726 | 0 | } |
10727 | | |
10728 | | /** |
10729 | | * xmlRelaxNGSetValidStructuredErrors: |
10730 | | * @ctxt: a Relax-NG validation context |
10731 | | * @serror: the structured error function |
10732 | | * @ctx: the functions context |
10733 | | * |
10734 | | * Set the structured error callback |
10735 | | */ |
10736 | | void |
10737 | | xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt, |
10738 | | xmlStructuredErrorFunc serror, void *ctx) |
10739 | 0 | { |
10740 | 0 | if (ctxt == NULL) |
10741 | 0 | return; |
10742 | 0 | ctxt->serror = serror; |
10743 | 0 | ctxt->error = NULL; |
10744 | 0 | ctxt->warning = NULL; |
10745 | 0 | ctxt->userData = ctx; |
10746 | 0 | } |
10747 | | |
10748 | | /** |
10749 | | * xmlRelaxNGGetValidErrors: |
10750 | | * @ctxt: a Relax-NG validation context |
10751 | | * @err: the error function result |
10752 | | * @warn: the warning function result |
10753 | | * @ctx: the functions context result |
10754 | | * |
10755 | | * Get the error and warning callback information |
10756 | | * |
10757 | | * Returns -1 in case of error and 0 otherwise |
10758 | | */ |
10759 | | int |
10760 | | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
10761 | | xmlRelaxNGValidityErrorFunc * err, |
10762 | | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
10763 | 0 | { |
10764 | 0 | if (ctxt == NULL) |
10765 | 0 | return (-1); |
10766 | 0 | if (err != NULL) |
10767 | 0 | *err = ctxt->error; |
10768 | 0 | if (warn != NULL) |
10769 | 0 | *warn = ctxt->warning; |
10770 | 0 | if (ctx != NULL) |
10771 | 0 | *ctx = ctxt->userData; |
10772 | 0 | return (0); |
10773 | 0 | } |
10774 | | |
10775 | | /** |
10776 | | * xmlRelaxNGValidateDoc: |
10777 | | * @ctxt: a Relax-NG validation context |
10778 | | * @doc: a parsed document tree |
10779 | | * |
10780 | | * Validate a document tree in memory. |
10781 | | * |
10782 | | * Returns 0 if the document is valid, a positive error code |
10783 | | * number otherwise and -1 in case of internal or API error. |
10784 | | */ |
10785 | | int |
10786 | | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
10787 | 0 | { |
10788 | 0 | int ret; |
10789 | |
|
10790 | 0 | if ((ctxt == NULL) || (doc == NULL)) |
10791 | 0 | return (-1); |
10792 | | |
10793 | 0 | ctxt->doc = doc; |
10794 | |
|
10795 | 0 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
10796 | | /* |
10797 | | * Remove all left PSVI |
10798 | | */ |
10799 | 0 | xmlRelaxNGCleanPSVI((xmlNodePtr) doc); |
10800 | | |
10801 | | /* |
10802 | | * TODO: build error codes |
10803 | | */ |
10804 | 0 | if (ret == -1) |
10805 | 0 | return (1); |
10806 | 0 | return (ret); |
10807 | 0 | } |
10808 | | |
10809 | | #endif /* LIBXML_SCHEMAS_ENABLED */ |