/src/libxml2-2.10.3/valid.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * valid.c : part of the code use to do the DTD handling and the validity |
3 | | * checking |
4 | | * |
5 | | * See Copyright for the status of this software. |
6 | | * |
7 | | * daniel@veillard.com |
8 | | */ |
9 | | |
10 | | #define IN_LIBXML |
11 | | #include "libxml.h" |
12 | | |
13 | | #include <string.h> |
14 | | #include <stdlib.h> |
15 | | |
16 | | #include <libxml/xmlmemory.h> |
17 | | #include <libxml/hash.h> |
18 | | #include <libxml/uri.h> |
19 | | #include <libxml/valid.h> |
20 | | #include <libxml/parser.h> |
21 | | #include <libxml/parserInternals.h> |
22 | | #include <libxml/xmlerror.h> |
23 | | #include <libxml/list.h> |
24 | | #include <libxml/globals.h> |
25 | | |
26 | | static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, |
27 | | int create); |
28 | | /* #define DEBUG_VALID_ALGO */ |
29 | | /* #define DEBUG_REGEXP_ALGO */ |
30 | | |
31 | | #define TODO \ |
32 | | xmlGenericError(xmlGenericErrorContext, \ |
33 | | "Unimplemented block at %s:%d\n", \ |
34 | | __FILE__, __LINE__); |
35 | | |
36 | | #ifdef LIBXML_VALID_ENABLED |
37 | | static int |
38 | | xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type, |
39 | | const xmlChar *value); |
40 | | #endif |
41 | | /************************************************************************ |
42 | | * * |
43 | | * Error handling routines * |
44 | | * * |
45 | | ************************************************************************/ |
46 | | |
47 | | /** |
48 | | * xmlVErrMemory: |
49 | | * @ctxt: an XML validation parser context |
50 | | * @extra: extra information |
51 | | * |
52 | | * Handle an out of memory error |
53 | | */ |
54 | | static void |
55 | | xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra) |
56 | 0 | { |
57 | 0 | xmlGenericErrorFunc channel = NULL; |
58 | 0 | xmlParserCtxtPtr pctxt = NULL; |
59 | 0 | void *data = NULL; |
60 | |
|
61 | 0 | if (ctxt != NULL) { |
62 | 0 | channel = ctxt->error; |
63 | 0 | data = ctxt->userData; |
64 | | /* Look up flag to detect if it is part of a parsing |
65 | | context */ |
66 | 0 | if (ctxt->flags & XML_VCTXT_USE_PCTXT) { |
67 | 0 | long delta = (char *) ctxt - (char *) ctxt->userData; |
68 | 0 | if ((delta > 0) && (delta < 250)) |
69 | 0 | pctxt = ctxt->userData; |
70 | 0 | } |
71 | 0 | } |
72 | 0 | if (extra) |
73 | 0 | __xmlRaiseError(NULL, channel, data, |
74 | 0 | pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY, |
75 | 0 | XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0, |
76 | 0 | "Memory allocation failed : %s\n", extra); |
77 | 0 | else |
78 | 0 | __xmlRaiseError(NULL, channel, data, |
79 | 0 | pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY, |
80 | 0 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, |
81 | 0 | "Memory allocation failed\n"); |
82 | 0 | } |
83 | | |
84 | | /** |
85 | | * xmlErrValid: |
86 | | * @ctxt: an XML validation parser context |
87 | | * @error: the error number |
88 | | * @extra: extra information |
89 | | * |
90 | | * Handle a validation error |
91 | | */ |
92 | | static void LIBXML_ATTR_FORMAT(3,0) |
93 | | xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error, |
94 | | const char *msg, const char *extra) |
95 | 0 | { |
96 | 0 | xmlGenericErrorFunc channel = NULL; |
97 | 0 | xmlParserCtxtPtr pctxt = NULL; |
98 | 0 | void *data = NULL; |
99 | |
|
100 | 0 | if (ctxt != NULL) { |
101 | 0 | channel = ctxt->error; |
102 | 0 | data = ctxt->userData; |
103 | | /* Look up flag to detect if it is part of a parsing |
104 | | context */ |
105 | 0 | if (ctxt->flags & XML_VCTXT_USE_PCTXT) { |
106 | 0 | long delta = (char *) ctxt - (char *) ctxt->userData; |
107 | 0 | if ((delta > 0) && (delta < 250)) |
108 | 0 | pctxt = ctxt->userData; |
109 | 0 | } |
110 | 0 | } |
111 | 0 | if (extra) |
112 | 0 | __xmlRaiseError(NULL, channel, data, |
113 | 0 | pctxt, NULL, XML_FROM_VALID, error, |
114 | 0 | XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0, |
115 | 0 | msg, extra); |
116 | 0 | else |
117 | 0 | __xmlRaiseError(NULL, channel, data, |
118 | 0 | pctxt, NULL, XML_FROM_VALID, error, |
119 | 0 | XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0, |
120 | 0 | "%s", msg); |
121 | 0 | } |
122 | | |
123 | | #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
124 | | /** |
125 | | * xmlErrValidNode: |
126 | | * @ctxt: an XML validation parser context |
127 | | * @node: the node raising the error |
128 | | * @error: the error number |
129 | | * @str1: extra information |
130 | | * @str2: extra information |
131 | | * @str3: extra information |
132 | | * |
133 | | * Handle a validation error, provide contextual information |
134 | | */ |
135 | | static void LIBXML_ATTR_FORMAT(4,0) |
136 | | xmlErrValidNode(xmlValidCtxtPtr ctxt, |
137 | | xmlNodePtr node, xmlParserErrors error, |
138 | | const char *msg, const xmlChar * str1, |
139 | | const xmlChar * str2, const xmlChar * str3) |
140 | 0 | { |
141 | 0 | xmlStructuredErrorFunc schannel = NULL; |
142 | 0 | xmlGenericErrorFunc channel = NULL; |
143 | 0 | xmlParserCtxtPtr pctxt = NULL; |
144 | 0 | void *data = NULL; |
145 | |
|
146 | 0 | if (ctxt != NULL) { |
147 | 0 | channel = ctxt->error; |
148 | 0 | data = ctxt->userData; |
149 | | /* Look up flag to detect if it is part of a parsing |
150 | | context */ |
151 | 0 | if (ctxt->flags & XML_VCTXT_USE_PCTXT) { |
152 | 0 | long delta = (char *) ctxt - (char *) ctxt->userData; |
153 | 0 | if ((delta > 0) && (delta < 250)) |
154 | 0 | pctxt = ctxt->userData; |
155 | 0 | } |
156 | 0 | } |
157 | 0 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
158 | 0 | XML_ERR_ERROR, NULL, 0, |
159 | 0 | (const char *) str1, |
160 | 0 | (const char *) str2, |
161 | 0 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
162 | 0 | } |
163 | | #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */ |
164 | | |
165 | | #ifdef LIBXML_VALID_ENABLED |
166 | | /** |
167 | | * xmlErrValidNodeNr: |
168 | | * @ctxt: an XML validation parser context |
169 | | * @node: the node raising the error |
170 | | * @error: the error number |
171 | | * @str1: extra information |
172 | | * @int2: extra information |
173 | | * @str3: extra information |
174 | | * |
175 | | * Handle a validation error, provide contextual information |
176 | | */ |
177 | | static void LIBXML_ATTR_FORMAT(4,0) |
178 | | xmlErrValidNodeNr(xmlValidCtxtPtr ctxt, |
179 | | xmlNodePtr node, xmlParserErrors error, |
180 | | const char *msg, const xmlChar * str1, |
181 | | int int2, const xmlChar * str3) |
182 | 0 | { |
183 | 0 | xmlStructuredErrorFunc schannel = NULL; |
184 | 0 | xmlGenericErrorFunc channel = NULL; |
185 | 0 | xmlParserCtxtPtr pctxt = NULL; |
186 | 0 | void *data = NULL; |
187 | |
|
188 | 0 | if (ctxt != NULL) { |
189 | 0 | channel = ctxt->error; |
190 | 0 | data = ctxt->userData; |
191 | | /* Look up flag to detect if it is part of a parsing |
192 | | context */ |
193 | 0 | if (ctxt->flags & XML_VCTXT_USE_PCTXT) { |
194 | 0 | long delta = (char *) ctxt - (char *) ctxt->userData; |
195 | 0 | if ((delta > 0) && (delta < 250)) |
196 | 0 | pctxt = ctxt->userData; |
197 | 0 | } |
198 | 0 | } |
199 | 0 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
200 | 0 | XML_ERR_ERROR, NULL, 0, |
201 | 0 | (const char *) str1, |
202 | 0 | (const char *) str3, |
203 | 0 | NULL, int2, 0, msg, str1, int2, str3); |
204 | 0 | } |
205 | | |
206 | | /** |
207 | | * xmlErrValidWarning: |
208 | | * @ctxt: an XML validation parser context |
209 | | * @node: the node raising the error |
210 | | * @error: the error number |
211 | | * @str1: extra information |
212 | | * @str2: extra information |
213 | | * @str3: extra information |
214 | | * |
215 | | * Handle a validation error, provide contextual information |
216 | | */ |
217 | | static void LIBXML_ATTR_FORMAT(4,0) |
218 | | xmlErrValidWarning(xmlValidCtxtPtr ctxt, |
219 | | xmlNodePtr node, xmlParserErrors error, |
220 | | const char *msg, const xmlChar * str1, |
221 | | const xmlChar * str2, const xmlChar * str3) |
222 | 0 | { |
223 | 0 | xmlStructuredErrorFunc schannel = NULL; |
224 | 0 | xmlGenericErrorFunc channel = NULL; |
225 | 0 | xmlParserCtxtPtr pctxt = NULL; |
226 | 0 | void *data = NULL; |
227 | |
|
228 | 0 | if (ctxt != NULL) { |
229 | 0 | channel = ctxt->warning; |
230 | 0 | data = ctxt->userData; |
231 | | /* Look up flag to detect if it is part of a parsing |
232 | | context */ |
233 | 0 | if (ctxt->flags & XML_VCTXT_USE_PCTXT) { |
234 | 0 | long delta = (char *) ctxt - (char *) ctxt->userData; |
235 | 0 | if ((delta > 0) && (delta < 250)) |
236 | 0 | pctxt = ctxt->userData; |
237 | 0 | } |
238 | 0 | } |
239 | 0 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
240 | 0 | XML_ERR_WARNING, NULL, 0, |
241 | 0 | (const char *) str1, |
242 | 0 | (const char *) str2, |
243 | 0 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
244 | 0 | } |
245 | | |
246 | | |
247 | | |
248 | | #ifdef LIBXML_REGEXP_ENABLED |
249 | | /* |
250 | | * If regexp are enabled we can do continuous validation without the |
251 | | * need of a tree to validate the content model. this is done in each |
252 | | * callbacks. |
253 | | * Each xmlValidState represent the validation state associated to the |
254 | | * set of nodes currently open from the document root to the current element. |
255 | | */ |
256 | | |
257 | | |
258 | | typedef struct _xmlValidState { |
259 | | xmlElementPtr elemDecl; /* pointer to the content model */ |
260 | | xmlNodePtr node; /* pointer to the current node */ |
261 | | xmlRegExecCtxtPtr exec; /* regexp runtime */ |
262 | | } _xmlValidState; |
263 | | |
264 | | |
265 | | static int |
266 | 0 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) { |
267 | 0 | if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) { |
268 | 0 | ctxt->vstateMax = 10; |
269 | 0 | ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax * |
270 | 0 | sizeof(ctxt->vstateTab[0])); |
271 | 0 | if (ctxt->vstateTab == NULL) { |
272 | 0 | xmlVErrMemory(ctxt, "malloc failed"); |
273 | 0 | return(-1); |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | 0 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
278 | 0 | xmlValidState *tmp; |
279 | |
|
280 | 0 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
281 | 0 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
282 | 0 | if (tmp == NULL) { |
283 | 0 | xmlVErrMemory(ctxt, "realloc failed"); |
284 | 0 | return(-1); |
285 | 0 | } |
286 | 0 | ctxt->vstateMax *= 2; |
287 | 0 | ctxt->vstateTab = tmp; |
288 | 0 | } |
289 | 0 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr]; |
290 | 0 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl; |
291 | 0 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
292 | 0 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
293 | 0 | if (elemDecl->contModel == NULL) |
294 | 0 | xmlValidBuildContentModel(ctxt, elemDecl); |
295 | 0 | if (elemDecl->contModel != NULL) { |
296 | 0 | ctxt->vstateTab[ctxt->vstateNr].exec = |
297 | 0 | xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
298 | 0 | } else { |
299 | 0 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
300 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl, |
301 | 0 | XML_ERR_INTERNAL_ERROR, |
302 | 0 | "Failed to build content model regexp for %s\n", |
303 | 0 | node->name, NULL, NULL); |
304 | 0 | } |
305 | 0 | } |
306 | 0 | return(ctxt->vstateNr++); |
307 | 0 | } |
308 | | |
309 | | static int |
310 | 0 | vstateVPop(xmlValidCtxtPtr ctxt) { |
311 | 0 | xmlElementPtr elemDecl; |
312 | |
|
313 | 0 | if (ctxt->vstateNr < 1) return(-1); |
314 | 0 | ctxt->vstateNr--; |
315 | 0 | elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl; |
316 | 0 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL; |
317 | 0 | ctxt->vstateTab[ctxt->vstateNr].node = NULL; |
318 | 0 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
319 | 0 | xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec); |
320 | 0 | } |
321 | 0 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
322 | 0 | if (ctxt->vstateNr >= 1) |
323 | 0 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1]; |
324 | 0 | else |
325 | 0 | ctxt->vstate = NULL; |
326 | 0 | return(ctxt->vstateNr); |
327 | 0 | } |
328 | | |
329 | | #else /* not LIBXML_REGEXP_ENABLED */ |
330 | | /* |
331 | | * If regexp are not enabled, it uses a home made algorithm less |
332 | | * complex and easier to |
333 | | * debug/maintain than a generic NFA -> DFA state based algo. The |
334 | | * only restriction is on the deepness of the tree limited by the |
335 | | * size of the occurs bitfield |
336 | | * |
337 | | * this is the content of a saved state for rollbacks |
338 | | */ |
339 | | |
340 | | #define ROLLBACK_OR 0 |
341 | | #define ROLLBACK_PARENT 1 |
342 | | |
343 | | typedef struct _xmlValidState { |
344 | | xmlElementContentPtr cont; /* pointer to the content model subtree */ |
345 | | xmlNodePtr node; /* pointer to the current node in the list */ |
346 | | long occurs;/* bitfield for multiple occurrences */ |
347 | | unsigned char depth; /* current depth in the overall tree */ |
348 | | unsigned char state; /* ROLLBACK_XXX */ |
349 | | } _xmlValidState; |
350 | | |
351 | | #define MAX_RECURSE 25000 |
352 | | #define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8) |
353 | | #define CONT ctxt->vstate->cont |
354 | | #define NODE ctxt->vstate->node |
355 | | #define DEPTH ctxt->vstate->depth |
356 | | #define OCCURS ctxt->vstate->occurs |
357 | | #define STATE ctxt->vstate->state |
358 | | |
359 | | #define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH)) |
360 | | #define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1)) |
361 | | |
362 | | #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH) |
363 | | #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1) |
364 | | |
365 | | static int |
366 | | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont, |
367 | | xmlNodePtr node, unsigned char depth, long occurs, |
368 | | unsigned char state) { |
369 | | int i = ctxt->vstateNr - 1; |
370 | | |
371 | | if (ctxt->vstateNr > MAX_RECURSE) { |
372 | | return(-1); |
373 | | } |
374 | | if (ctxt->vstateTab == NULL) { |
375 | | ctxt->vstateMax = 8; |
376 | | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
377 | | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
378 | | if (ctxt->vstateTab == NULL) { |
379 | | xmlVErrMemory(ctxt, "malloc failed"); |
380 | | return(-1); |
381 | | } |
382 | | } |
383 | | if (ctxt->vstateNr >= ctxt->vstateMax) { |
384 | | xmlValidState *tmp; |
385 | | |
386 | | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
387 | | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
388 | | if (tmp == NULL) { |
389 | | xmlVErrMemory(ctxt, "malloc failed"); |
390 | | return(-1); |
391 | | } |
392 | | ctxt->vstateMax *= 2; |
393 | | ctxt->vstateTab = tmp; |
394 | | ctxt->vstate = &ctxt->vstateTab[0]; |
395 | | } |
396 | | /* |
397 | | * Don't push on the stack a state already here |
398 | | */ |
399 | | if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) && |
400 | | (ctxt->vstateTab[i].node == node) && |
401 | | (ctxt->vstateTab[i].depth == depth) && |
402 | | (ctxt->vstateTab[i].occurs == occurs) && |
403 | | (ctxt->vstateTab[i].state == state)) |
404 | | return(ctxt->vstateNr); |
405 | | ctxt->vstateTab[ctxt->vstateNr].cont = cont; |
406 | | ctxt->vstateTab[ctxt->vstateNr].node = node; |
407 | | ctxt->vstateTab[ctxt->vstateNr].depth = depth; |
408 | | ctxt->vstateTab[ctxt->vstateNr].occurs = occurs; |
409 | | ctxt->vstateTab[ctxt->vstateNr].state = state; |
410 | | return(ctxt->vstateNr++); |
411 | | } |
412 | | |
413 | | static int |
414 | | vstateVPop(xmlValidCtxtPtr ctxt) { |
415 | | if (ctxt->vstateNr <= 1) return(-1); |
416 | | ctxt->vstateNr--; |
417 | | ctxt->vstate = &ctxt->vstateTab[0]; |
418 | | ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont; |
419 | | ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node; |
420 | | ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth; |
421 | | ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs; |
422 | | ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state; |
423 | | return(ctxt->vstateNr); |
424 | | } |
425 | | |
426 | | #endif /* LIBXML_REGEXP_ENABLED */ |
427 | | |
428 | | static int |
429 | | nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value) |
430 | 0 | { |
431 | 0 | if (ctxt->nodeMax <= 0) { |
432 | 0 | ctxt->nodeMax = 4; |
433 | 0 | ctxt->nodeTab = |
434 | 0 | (xmlNodePtr *) xmlMalloc(ctxt->nodeMax * |
435 | 0 | sizeof(ctxt->nodeTab[0])); |
436 | 0 | if (ctxt->nodeTab == NULL) { |
437 | 0 | xmlVErrMemory(ctxt, "malloc failed"); |
438 | 0 | ctxt->nodeMax = 0; |
439 | 0 | return (0); |
440 | 0 | } |
441 | 0 | } |
442 | 0 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
443 | 0 | xmlNodePtr *tmp; |
444 | 0 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
445 | 0 | ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0])); |
446 | 0 | if (tmp == NULL) { |
447 | 0 | xmlVErrMemory(ctxt, "realloc failed"); |
448 | 0 | return (0); |
449 | 0 | } |
450 | 0 | ctxt->nodeMax *= 2; |
451 | 0 | ctxt->nodeTab = tmp; |
452 | 0 | } |
453 | 0 | ctxt->nodeTab[ctxt->nodeNr] = value; |
454 | 0 | ctxt->node = value; |
455 | 0 | return (ctxt->nodeNr++); |
456 | 0 | } |
457 | | static xmlNodePtr |
458 | | nodeVPop(xmlValidCtxtPtr ctxt) |
459 | 0 | { |
460 | 0 | xmlNodePtr ret; |
461 | |
|
462 | 0 | if (ctxt->nodeNr <= 0) |
463 | 0 | return (NULL); |
464 | 0 | ctxt->nodeNr--; |
465 | 0 | if (ctxt->nodeNr > 0) |
466 | 0 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
467 | 0 | else |
468 | 0 | ctxt->node = NULL; |
469 | 0 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
470 | 0 | ctxt->nodeTab[ctxt->nodeNr] = NULL; |
471 | 0 | return (ret); |
472 | 0 | } |
473 | | |
474 | | #ifdef DEBUG_VALID_ALGO |
475 | | static void |
476 | | xmlValidPrintNode(xmlNodePtr cur) { |
477 | | if (cur == NULL) { |
478 | | xmlGenericError(xmlGenericErrorContext, "null"); |
479 | | return; |
480 | | } |
481 | | switch (cur->type) { |
482 | | case XML_ELEMENT_NODE: |
483 | | xmlGenericError(xmlGenericErrorContext, "%s ", cur->name); |
484 | | break; |
485 | | case XML_TEXT_NODE: |
486 | | xmlGenericError(xmlGenericErrorContext, "text "); |
487 | | break; |
488 | | case XML_CDATA_SECTION_NODE: |
489 | | xmlGenericError(xmlGenericErrorContext, "cdata "); |
490 | | break; |
491 | | case XML_ENTITY_REF_NODE: |
492 | | xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name); |
493 | | break; |
494 | | case XML_PI_NODE: |
495 | | xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name); |
496 | | break; |
497 | | case XML_COMMENT_NODE: |
498 | | xmlGenericError(xmlGenericErrorContext, "comment "); |
499 | | break; |
500 | | case XML_ATTRIBUTE_NODE: |
501 | | xmlGenericError(xmlGenericErrorContext, "?attr? "); |
502 | | break; |
503 | | case XML_ENTITY_NODE: |
504 | | xmlGenericError(xmlGenericErrorContext, "?ent? "); |
505 | | break; |
506 | | case XML_DOCUMENT_NODE: |
507 | | xmlGenericError(xmlGenericErrorContext, "?doc? "); |
508 | | break; |
509 | | case XML_DOCUMENT_TYPE_NODE: |
510 | | xmlGenericError(xmlGenericErrorContext, "?doctype? "); |
511 | | break; |
512 | | case XML_DOCUMENT_FRAG_NODE: |
513 | | xmlGenericError(xmlGenericErrorContext, "?frag? "); |
514 | | break; |
515 | | case XML_NOTATION_NODE: |
516 | | xmlGenericError(xmlGenericErrorContext, "?nota? "); |
517 | | break; |
518 | | case XML_HTML_DOCUMENT_NODE: |
519 | | xmlGenericError(xmlGenericErrorContext, "?html? "); |
520 | | break; |
521 | | case XML_DTD_NODE: |
522 | | xmlGenericError(xmlGenericErrorContext, "?dtd? "); |
523 | | break; |
524 | | case XML_ELEMENT_DECL: |
525 | | xmlGenericError(xmlGenericErrorContext, "?edecl? "); |
526 | | break; |
527 | | case XML_ATTRIBUTE_DECL: |
528 | | xmlGenericError(xmlGenericErrorContext, "?adecl? "); |
529 | | break; |
530 | | case XML_ENTITY_DECL: |
531 | | xmlGenericError(xmlGenericErrorContext, "?entdecl? "); |
532 | | break; |
533 | | case XML_NAMESPACE_DECL: |
534 | | xmlGenericError(xmlGenericErrorContext, "?nsdecl? "); |
535 | | break; |
536 | | case XML_XINCLUDE_START: |
537 | | xmlGenericError(xmlGenericErrorContext, "incstart "); |
538 | | break; |
539 | | case XML_XINCLUDE_END: |
540 | | xmlGenericError(xmlGenericErrorContext, "incend "); |
541 | | break; |
542 | | } |
543 | | } |
544 | | |
545 | | static void |
546 | | xmlValidPrintNodeList(xmlNodePtr cur) { |
547 | | if (cur == NULL) |
548 | | xmlGenericError(xmlGenericErrorContext, "null "); |
549 | | while (cur != NULL) { |
550 | | xmlValidPrintNode(cur); |
551 | | cur = cur->next; |
552 | | } |
553 | | } |
554 | | |
555 | | static void |
556 | | xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) { |
557 | | char expr[5000]; |
558 | | |
559 | | expr[0] = 0; |
560 | | xmlGenericError(xmlGenericErrorContext, "valid: "); |
561 | | xmlValidPrintNodeList(cur); |
562 | | xmlGenericError(xmlGenericErrorContext, "against "); |
563 | | xmlSnprintfElementContent(expr, 5000, cont, 1); |
564 | | xmlGenericError(xmlGenericErrorContext, "%s\n", expr); |
565 | | } |
566 | | |
567 | | static void |
568 | | xmlValidDebugState(xmlValidStatePtr state) { |
569 | | xmlGenericError(xmlGenericErrorContext, "("); |
570 | | if (state->cont == NULL) |
571 | | xmlGenericError(xmlGenericErrorContext, "null,"); |
572 | | else |
573 | | switch (state->cont->type) { |
574 | | case XML_ELEMENT_CONTENT_PCDATA: |
575 | | xmlGenericError(xmlGenericErrorContext, "pcdata,"); |
576 | | break; |
577 | | case XML_ELEMENT_CONTENT_ELEMENT: |
578 | | xmlGenericError(xmlGenericErrorContext, "%s,", |
579 | | state->cont->name); |
580 | | break; |
581 | | case XML_ELEMENT_CONTENT_SEQ: |
582 | | xmlGenericError(xmlGenericErrorContext, "seq,"); |
583 | | break; |
584 | | case XML_ELEMENT_CONTENT_OR: |
585 | | xmlGenericError(xmlGenericErrorContext, "or,"); |
586 | | break; |
587 | | } |
588 | | xmlValidPrintNode(state->node); |
589 | | xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)", |
590 | | state->depth, state->occurs, state->state); |
591 | | } |
592 | | |
593 | | static void |
594 | | xmlValidStateDebug(xmlValidCtxtPtr ctxt) { |
595 | | int i, j; |
596 | | |
597 | | xmlGenericError(xmlGenericErrorContext, "state: "); |
598 | | xmlValidDebugState(ctxt->vstate); |
599 | | xmlGenericError(xmlGenericErrorContext, " stack: %d ", |
600 | | ctxt->vstateNr - 1); |
601 | | for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--) |
602 | | xmlValidDebugState(&ctxt->vstateTab[j]); |
603 | | xmlGenericError(xmlGenericErrorContext, "\n"); |
604 | | } |
605 | | |
606 | | /***** |
607 | | #define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c); |
608 | | *****/ |
609 | | |
610 | | #define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt); |
611 | | #define DEBUG_VALID_MSG(m) \ |
612 | | xmlGenericError(xmlGenericErrorContext, "%s\n", m); |
613 | | |
614 | | #else |
615 | | #define DEBUG_VALID_STATE(n,c) |
616 | | #define DEBUG_VALID_MSG(m) |
617 | | #endif |
618 | | |
619 | | /* TODO: use hash table for accesses to elem and attribute definitions */ |
620 | | |
621 | | |
622 | | #define CHECK_DTD \ |
623 | 0 | if (doc == NULL) return(0); \ |
624 | 0 | else if ((doc->intSubset == NULL) && \ |
625 | 0 | (doc->extSubset == NULL)) return(0) |
626 | | |
627 | | #ifdef LIBXML_REGEXP_ENABLED |
628 | | |
629 | | /************************************************************************ |
630 | | * * |
631 | | * Content model validation based on the regexps * |
632 | | * * |
633 | | ************************************************************************/ |
634 | | |
635 | | /** |
636 | | * xmlValidBuildAContentModel: |
637 | | * @content: the content model |
638 | | * @ctxt: the schema parser context |
639 | | * @name: the element name whose content is being built |
640 | | * |
641 | | * Generate the automata sequence needed for that type |
642 | | * |
643 | | * Returns 1 if successful or 0 in case of error. |
644 | | */ |
645 | | static int |
646 | | xmlValidBuildAContentModel(xmlElementContentPtr content, |
647 | | xmlValidCtxtPtr ctxt, |
648 | 0 | const xmlChar *name) { |
649 | 0 | if (content == NULL) { |
650 | 0 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
651 | 0 | "Found NULL content in content model of %s\n", |
652 | 0 | name, NULL, NULL); |
653 | 0 | return(0); |
654 | 0 | } |
655 | 0 | switch (content->type) { |
656 | 0 | case XML_ELEMENT_CONTENT_PCDATA: |
657 | 0 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
658 | 0 | "Found PCDATA in content model of %s\n", |
659 | 0 | name, NULL, NULL); |
660 | 0 | return(0); |
661 | 0 | break; |
662 | 0 | case XML_ELEMENT_CONTENT_ELEMENT: { |
663 | 0 | xmlAutomataStatePtr oldstate = ctxt->state; |
664 | 0 | xmlChar fn[50]; |
665 | 0 | xmlChar *fullname; |
666 | |
|
667 | 0 | fullname = xmlBuildQName(content->name, content->prefix, fn, 50); |
668 | 0 | if (fullname == NULL) { |
669 | 0 | xmlVErrMemory(ctxt, "Building content model"); |
670 | 0 | return(0); |
671 | 0 | } |
672 | | |
673 | 0 | switch (content->ocur) { |
674 | 0 | case XML_ELEMENT_CONTENT_ONCE: |
675 | 0 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
676 | 0 | ctxt->state, NULL, fullname, NULL); |
677 | 0 | break; |
678 | 0 | case XML_ELEMENT_CONTENT_OPT: |
679 | 0 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
680 | 0 | ctxt->state, NULL, fullname, NULL); |
681 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
682 | 0 | break; |
683 | 0 | case XML_ELEMENT_CONTENT_PLUS: |
684 | 0 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
685 | 0 | ctxt->state, NULL, fullname, NULL); |
686 | 0 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
687 | 0 | ctxt->state, fullname, NULL); |
688 | 0 | break; |
689 | 0 | case XML_ELEMENT_CONTENT_MULT: |
690 | 0 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
691 | 0 | ctxt->state, NULL); |
692 | 0 | xmlAutomataNewTransition(ctxt->am, |
693 | 0 | ctxt->state, ctxt->state, fullname, NULL); |
694 | 0 | break; |
695 | 0 | } |
696 | 0 | if ((fullname != fn) && (fullname != content->name)) |
697 | 0 | xmlFree(fullname); |
698 | 0 | break; |
699 | 0 | } |
700 | 0 | case XML_ELEMENT_CONTENT_SEQ: { |
701 | 0 | xmlAutomataStatePtr oldstate, oldend; |
702 | 0 | xmlElementContentOccur ocur; |
703 | | |
704 | | /* |
705 | | * Simply iterate over the content |
706 | | */ |
707 | 0 | oldstate = ctxt->state; |
708 | 0 | ocur = content->ocur; |
709 | 0 | if (ocur != XML_ELEMENT_CONTENT_ONCE) { |
710 | 0 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
711 | 0 | oldstate = ctxt->state; |
712 | 0 | } |
713 | 0 | do { |
714 | 0 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
715 | 0 | content = content->c2; |
716 | 0 | } while ((content->type == XML_ELEMENT_CONTENT_SEQ) && |
717 | 0 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
718 | 0 | xmlValidBuildAContentModel(content, ctxt, name); |
719 | 0 | oldend = ctxt->state; |
720 | 0 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
721 | 0 | switch (ocur) { |
722 | 0 | case XML_ELEMENT_CONTENT_ONCE: |
723 | 0 | break; |
724 | 0 | case XML_ELEMENT_CONTENT_OPT: |
725 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
726 | 0 | break; |
727 | 0 | case XML_ELEMENT_CONTENT_MULT: |
728 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
729 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
730 | 0 | break; |
731 | 0 | case XML_ELEMENT_CONTENT_PLUS: |
732 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
733 | 0 | break; |
734 | 0 | } |
735 | 0 | break; |
736 | 0 | } |
737 | 0 | case XML_ELEMENT_CONTENT_OR: { |
738 | 0 | xmlAutomataStatePtr oldstate, oldend; |
739 | 0 | xmlElementContentOccur ocur; |
740 | |
|
741 | 0 | ocur = content->ocur; |
742 | 0 | if ((ocur == XML_ELEMENT_CONTENT_PLUS) || |
743 | 0 | (ocur == XML_ELEMENT_CONTENT_MULT)) { |
744 | 0 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
745 | 0 | ctxt->state, NULL); |
746 | 0 | } |
747 | 0 | oldstate = ctxt->state; |
748 | 0 | oldend = xmlAutomataNewState(ctxt->am); |
749 | | |
750 | | /* |
751 | | * iterate over the subtypes and remerge the end with an |
752 | | * epsilon transition |
753 | | */ |
754 | 0 | do { |
755 | 0 | ctxt->state = oldstate; |
756 | 0 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
757 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
758 | 0 | content = content->c2; |
759 | 0 | } while ((content->type == XML_ELEMENT_CONTENT_OR) && |
760 | 0 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
761 | 0 | ctxt->state = oldstate; |
762 | 0 | xmlValidBuildAContentModel(content, ctxt, name); |
763 | 0 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
764 | 0 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
765 | 0 | switch (ocur) { |
766 | 0 | case XML_ELEMENT_CONTENT_ONCE: |
767 | 0 | break; |
768 | 0 | case XML_ELEMENT_CONTENT_OPT: |
769 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
770 | 0 | break; |
771 | 0 | case XML_ELEMENT_CONTENT_MULT: |
772 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
773 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
774 | 0 | break; |
775 | 0 | case XML_ELEMENT_CONTENT_PLUS: |
776 | 0 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
777 | 0 | break; |
778 | 0 | } |
779 | 0 | break; |
780 | 0 | } |
781 | 0 | default: |
782 | 0 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
783 | 0 | "ContentModel broken for element %s\n", |
784 | 0 | (const char *) name); |
785 | 0 | return(0); |
786 | 0 | } |
787 | 0 | return(1); |
788 | 0 | } |
789 | | /** |
790 | | * xmlValidBuildContentModel: |
791 | | * @ctxt: a validation context |
792 | | * @elem: an element declaration node |
793 | | * |
794 | | * (Re)Build the automata associated to the content model of this |
795 | | * element |
796 | | * |
797 | | * Returns 1 in case of success, 0 in case of error |
798 | | */ |
799 | | int |
800 | 0 | xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
801 | |
|
802 | 0 | if ((ctxt == NULL) || (elem == NULL)) |
803 | 0 | return(0); |
804 | 0 | if (elem->type != XML_ELEMENT_DECL) |
805 | 0 | return(0); |
806 | 0 | if (elem->etype != XML_ELEMENT_TYPE_ELEMENT) |
807 | 0 | return(1); |
808 | | /* TODO: should we rebuild in this case ? */ |
809 | 0 | if (elem->contModel != NULL) { |
810 | 0 | if (!xmlRegexpIsDeterminist(elem->contModel)) { |
811 | 0 | ctxt->valid = 0; |
812 | 0 | return(0); |
813 | 0 | } |
814 | 0 | return(1); |
815 | 0 | } |
816 | | |
817 | 0 | ctxt->am = xmlNewAutomata(); |
818 | 0 | if (ctxt->am == NULL) { |
819 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
820 | 0 | XML_ERR_INTERNAL_ERROR, |
821 | 0 | "Cannot create automata for element %s\n", |
822 | 0 | elem->name, NULL, NULL); |
823 | 0 | return(0); |
824 | 0 | } |
825 | 0 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
826 | 0 | xmlValidBuildAContentModel(elem->content, ctxt, elem->name); |
827 | 0 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
828 | 0 | elem->contModel = xmlAutomataCompile(ctxt->am); |
829 | 0 | if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
830 | 0 | char expr[5000]; |
831 | 0 | expr[0] = 0; |
832 | 0 | xmlSnprintfElementContent(expr, 5000, elem->content, 1); |
833 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
834 | 0 | XML_DTD_CONTENT_NOT_DETERMINIST, |
835 | 0 | "Content model of %s is not determinist: %s\n", |
836 | 0 | elem->name, BAD_CAST expr, NULL); |
837 | | #ifdef DEBUG_REGEXP_ALGO |
838 | | xmlRegexpPrint(stderr, elem->contModel); |
839 | | #endif |
840 | 0 | ctxt->valid = 0; |
841 | 0 | ctxt->state = NULL; |
842 | 0 | xmlFreeAutomata(ctxt->am); |
843 | 0 | ctxt->am = NULL; |
844 | 0 | return(0); |
845 | 0 | } |
846 | 0 | ctxt->state = NULL; |
847 | 0 | xmlFreeAutomata(ctxt->am); |
848 | 0 | ctxt->am = NULL; |
849 | 0 | return(1); |
850 | 0 | } |
851 | | |
852 | | #endif /* LIBXML_REGEXP_ENABLED */ |
853 | | |
854 | | /**************************************************************** |
855 | | * * |
856 | | * Util functions for data allocation/deallocation * |
857 | | * * |
858 | | ****************************************************************/ |
859 | | |
860 | | /** |
861 | | * xmlNewValidCtxt: |
862 | | * |
863 | | * Allocate a validation context structure. |
864 | | * |
865 | | * Returns NULL if not, otherwise the new validation context structure |
866 | | */ |
867 | 0 | xmlValidCtxtPtr xmlNewValidCtxt(void) { |
868 | 0 | xmlValidCtxtPtr ret; |
869 | |
|
870 | 0 | if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) { |
871 | 0 | xmlVErrMemory(NULL, "malloc failed"); |
872 | 0 | return (NULL); |
873 | 0 | } |
874 | | |
875 | 0 | (void) memset(ret, 0, sizeof (xmlValidCtxt)); |
876 | |
|
877 | 0 | return (ret); |
878 | 0 | } |
879 | | |
880 | | /** |
881 | | * xmlFreeValidCtxt: |
882 | | * @cur: the validation context to free |
883 | | * |
884 | | * Free a validation context structure. |
885 | | */ |
886 | | void |
887 | 0 | xmlFreeValidCtxt(xmlValidCtxtPtr cur) { |
888 | 0 | if (cur->vstateTab != NULL) |
889 | 0 | xmlFree(cur->vstateTab); |
890 | 0 | if (cur->nodeTab != NULL) |
891 | 0 | xmlFree(cur->nodeTab); |
892 | 0 | xmlFree(cur); |
893 | 0 | } |
894 | | |
895 | | #endif /* LIBXML_VALID_ENABLED */ |
896 | | |
897 | | /** |
898 | | * xmlNewDocElementContent: |
899 | | * @doc: the document |
900 | | * @name: the subelement name or NULL |
901 | | * @type: the type of element content decl |
902 | | * |
903 | | * Allocate an element content structure for the document. |
904 | | * |
905 | | * Returns NULL if not, otherwise the new element content structure |
906 | | */ |
907 | | xmlElementContentPtr |
908 | | xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name, |
909 | 31.4k | xmlElementContentType type) { |
910 | 31.4k | xmlElementContentPtr ret; |
911 | 31.4k | xmlDictPtr dict = NULL; |
912 | | |
913 | 31.4k | if (doc != NULL) |
914 | 529 | dict = doc->dict; |
915 | | |
916 | 31.4k | switch(type) { |
917 | 15.7k | case XML_ELEMENT_CONTENT_ELEMENT: |
918 | 15.7k | if (name == NULL) { |
919 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
920 | 0 | "xmlNewElementContent : name == NULL !\n", |
921 | 0 | NULL); |
922 | 0 | } |
923 | 15.7k | break; |
924 | 1.39k | case XML_ELEMENT_CONTENT_PCDATA: |
925 | 3.51k | case XML_ELEMENT_CONTENT_SEQ: |
926 | 15.6k | case XML_ELEMENT_CONTENT_OR: |
927 | 15.6k | if (name != NULL) { |
928 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
929 | 0 | "xmlNewElementContent : name != NULL !\n", |
930 | 0 | NULL); |
931 | 0 | } |
932 | 15.6k | break; |
933 | 0 | default: |
934 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
935 | 0 | "Internal: ELEMENT content corrupted invalid type\n", |
936 | 0 | NULL); |
937 | 0 | return(NULL); |
938 | 31.4k | } |
939 | 31.4k | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
940 | 31.4k | if (ret == NULL) { |
941 | 0 | xmlVErrMemory(NULL, "malloc failed"); |
942 | 0 | return(NULL); |
943 | 0 | } |
944 | 31.4k | memset(ret, 0, sizeof(xmlElementContent)); |
945 | 31.4k | ret->type = type; |
946 | 31.4k | ret->ocur = XML_ELEMENT_CONTENT_ONCE; |
947 | 31.4k | if (name != NULL) { |
948 | 15.7k | int l; |
949 | 15.7k | const xmlChar *tmp; |
950 | | |
951 | 15.7k | tmp = xmlSplitQName3(name, &l); |
952 | 15.7k | if (tmp == NULL) { |
953 | 15.3k | if (dict == NULL) |
954 | 15.3k | ret->name = xmlStrdup(name); |
955 | 0 | else |
956 | 0 | ret->name = xmlDictLookup(dict, name, -1); |
957 | 15.3k | } else { |
958 | 485 | if (dict == NULL) { |
959 | 485 | ret->prefix = xmlStrndup(name, l); |
960 | 485 | ret->name = xmlStrdup(tmp); |
961 | 485 | } else { |
962 | 0 | ret->prefix = xmlDictLookup(dict, name, l); |
963 | 0 | ret->name = xmlDictLookup(dict, tmp, -1); |
964 | 0 | } |
965 | 485 | } |
966 | 15.7k | } |
967 | 31.4k | return(ret); |
968 | 31.4k | } |
969 | | |
970 | | /** |
971 | | * xmlNewElementContent: |
972 | | * @name: the subelement name or NULL |
973 | | * @type: the type of element content decl |
974 | | * |
975 | | * Allocate an element content structure. |
976 | | * Deprecated in favor of xmlNewDocElementContent |
977 | | * |
978 | | * Returns NULL if not, otherwise the new element content structure |
979 | | */ |
980 | | xmlElementContentPtr |
981 | 0 | xmlNewElementContent(const xmlChar *name, xmlElementContentType type) { |
982 | 0 | return(xmlNewDocElementContent(NULL, name, type)); |
983 | 0 | } |
984 | | |
985 | | /** |
986 | | * xmlCopyDocElementContent: |
987 | | * @doc: the document owning the element declaration |
988 | | * @cur: An element content pointer. |
989 | | * |
990 | | * Build a copy of an element content description. |
991 | | * |
992 | | * Returns the new xmlElementContentPtr or NULL in case of error. |
993 | | */ |
994 | | xmlElementContentPtr |
995 | 0 | xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) { |
996 | 0 | xmlElementContentPtr ret = NULL, prev = NULL, tmp; |
997 | 0 | xmlDictPtr dict = NULL; |
998 | |
|
999 | 0 | if (cur == NULL) return(NULL); |
1000 | | |
1001 | 0 | if (doc != NULL) |
1002 | 0 | dict = doc->dict; |
1003 | |
|
1004 | 0 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
1005 | 0 | if (ret == NULL) { |
1006 | 0 | xmlVErrMemory(NULL, "malloc failed"); |
1007 | 0 | return(NULL); |
1008 | 0 | } |
1009 | 0 | memset(ret, 0, sizeof(xmlElementContent)); |
1010 | 0 | ret->type = cur->type; |
1011 | 0 | ret->ocur = cur->ocur; |
1012 | 0 | if (cur->name != NULL) { |
1013 | 0 | if (dict) |
1014 | 0 | ret->name = xmlDictLookup(dict, cur->name, -1); |
1015 | 0 | else |
1016 | 0 | ret->name = xmlStrdup(cur->name); |
1017 | 0 | } |
1018 | |
|
1019 | 0 | if (cur->prefix != NULL) { |
1020 | 0 | if (dict) |
1021 | 0 | ret->prefix = xmlDictLookup(dict, cur->prefix, -1); |
1022 | 0 | else |
1023 | 0 | ret->prefix = xmlStrdup(cur->prefix); |
1024 | 0 | } |
1025 | 0 | if (cur->c1 != NULL) |
1026 | 0 | ret->c1 = xmlCopyDocElementContent(doc, cur->c1); |
1027 | 0 | if (ret->c1 != NULL) |
1028 | 0 | ret->c1->parent = ret; |
1029 | 0 | if (cur->c2 != NULL) { |
1030 | 0 | prev = ret; |
1031 | 0 | cur = cur->c2; |
1032 | 0 | while (cur != NULL) { |
1033 | 0 | tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
1034 | 0 | if (tmp == NULL) { |
1035 | 0 | xmlVErrMemory(NULL, "malloc failed"); |
1036 | 0 | return(ret); |
1037 | 0 | } |
1038 | 0 | memset(tmp, 0, sizeof(xmlElementContent)); |
1039 | 0 | tmp->type = cur->type; |
1040 | 0 | tmp->ocur = cur->ocur; |
1041 | 0 | prev->c2 = tmp; |
1042 | 0 | tmp->parent = prev; |
1043 | 0 | if (cur->name != NULL) { |
1044 | 0 | if (dict) |
1045 | 0 | tmp->name = xmlDictLookup(dict, cur->name, -1); |
1046 | 0 | else |
1047 | 0 | tmp->name = xmlStrdup(cur->name); |
1048 | 0 | } |
1049 | |
|
1050 | 0 | if (cur->prefix != NULL) { |
1051 | 0 | if (dict) |
1052 | 0 | tmp->prefix = xmlDictLookup(dict, cur->prefix, -1); |
1053 | 0 | else |
1054 | 0 | tmp->prefix = xmlStrdup(cur->prefix); |
1055 | 0 | } |
1056 | 0 | if (cur->c1 != NULL) |
1057 | 0 | tmp->c1 = xmlCopyDocElementContent(doc,cur->c1); |
1058 | 0 | if (tmp->c1 != NULL) |
1059 | 0 | tmp->c1->parent = ret; |
1060 | 0 | prev = tmp; |
1061 | 0 | cur = cur->c2; |
1062 | 0 | } |
1063 | 0 | } |
1064 | 0 | return(ret); |
1065 | 0 | } |
1066 | | |
1067 | | /** |
1068 | | * xmlCopyElementContent: |
1069 | | * @cur: An element content pointer. |
1070 | | * |
1071 | | * Build a copy of an element content description. |
1072 | | * Deprecated, use xmlCopyDocElementContent instead |
1073 | | * |
1074 | | * Returns the new xmlElementContentPtr or NULL in case of error. |
1075 | | */ |
1076 | | xmlElementContentPtr |
1077 | 0 | xmlCopyElementContent(xmlElementContentPtr cur) { |
1078 | 0 | return(xmlCopyDocElementContent(NULL, cur)); |
1079 | 0 | } |
1080 | | |
1081 | | /** |
1082 | | * xmlFreeDocElementContent: |
1083 | | * @doc: the document owning the element declaration |
1084 | | * @cur: the element content tree to free |
1085 | | * |
1086 | | * Free an element content structure. The whole subtree is removed. |
1087 | | */ |
1088 | | void |
1089 | 9.49k | xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) { |
1090 | 9.49k | xmlDictPtr dict = NULL; |
1091 | 9.49k | size_t depth = 0; |
1092 | | |
1093 | 9.49k | if (cur == NULL) |
1094 | 479 | return; |
1095 | 9.01k | if (doc != NULL) |
1096 | 253 | dict = doc->dict; |
1097 | | |
1098 | 31.4k | while (1) { |
1099 | 31.4k | xmlElementContentPtr parent; |
1100 | | |
1101 | 45.6k | while ((cur->c1 != NULL) || (cur->c2 != NULL)) { |
1102 | 14.2k | cur = (cur->c1 != NULL) ? cur->c1 : cur->c2; |
1103 | 14.2k | depth += 1; |
1104 | 14.2k | } |
1105 | | |
1106 | 31.4k | switch (cur->type) { |
1107 | 1.39k | case XML_ELEMENT_CONTENT_PCDATA: |
1108 | 17.1k | case XML_ELEMENT_CONTENT_ELEMENT: |
1109 | 19.2k | case XML_ELEMENT_CONTENT_SEQ: |
1110 | 31.4k | case XML_ELEMENT_CONTENT_OR: |
1111 | 31.4k | break; |
1112 | 0 | default: |
1113 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
1114 | 0 | "Internal: ELEMENT content corrupted invalid type\n", |
1115 | 0 | NULL); |
1116 | 0 | return; |
1117 | 31.4k | } |
1118 | 31.4k | if (dict) { |
1119 | 0 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
1120 | 0 | xmlFree((xmlChar *) cur->name); |
1121 | 0 | if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix))) |
1122 | 0 | xmlFree((xmlChar *) cur->prefix); |
1123 | 31.4k | } else { |
1124 | 31.4k | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
1125 | 31.4k | if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix); |
1126 | 31.4k | } |
1127 | 31.4k | parent = cur->parent; |
1128 | 31.4k | if ((depth == 0) || (parent == NULL)) { |
1129 | 9.01k | xmlFree(cur); |
1130 | 9.01k | break; |
1131 | 9.01k | } |
1132 | 22.4k | if (cur == parent->c1) |
1133 | 14.2k | parent->c1 = NULL; |
1134 | 8.17k | else |
1135 | 8.17k | parent->c2 = NULL; |
1136 | 22.4k | xmlFree(cur); |
1137 | | |
1138 | 22.4k | if (parent->c2 != NULL) { |
1139 | 8.17k | cur = parent->c2; |
1140 | 14.2k | } else { |
1141 | 14.2k | depth -= 1; |
1142 | 14.2k | cur = parent; |
1143 | 14.2k | } |
1144 | 22.4k | } |
1145 | 9.01k | } |
1146 | | |
1147 | | /** |
1148 | | * xmlFreeElementContent: |
1149 | | * @cur: the element content tree to free |
1150 | | * |
1151 | | * Free an element content structure. The whole subtree is removed. |
1152 | | * Deprecated, use xmlFreeDocElementContent instead |
1153 | | */ |
1154 | | void |
1155 | 0 | xmlFreeElementContent(xmlElementContentPtr cur) { |
1156 | 0 | xmlFreeDocElementContent(NULL, cur); |
1157 | 0 | } |
1158 | | |
1159 | | #ifdef LIBXML_OUTPUT_ENABLED |
1160 | | /** |
1161 | | * xmlDumpElementOccur: |
1162 | | * @buf: An XML buffer |
1163 | | * @cur: An element table |
1164 | | * |
1165 | | * Dump the occurrence operator of an element. |
1166 | | */ |
1167 | | static void |
1168 | 0 | xmlDumpElementOccur(xmlBufferPtr buf, xmlElementContentPtr cur) { |
1169 | 0 | switch (cur->ocur) { |
1170 | 0 | case XML_ELEMENT_CONTENT_ONCE: |
1171 | 0 | break; |
1172 | 0 | case XML_ELEMENT_CONTENT_OPT: |
1173 | 0 | xmlBufferWriteChar(buf, "?"); |
1174 | 0 | break; |
1175 | 0 | case XML_ELEMENT_CONTENT_MULT: |
1176 | 0 | xmlBufferWriteChar(buf, "*"); |
1177 | 0 | break; |
1178 | 0 | case XML_ELEMENT_CONTENT_PLUS: |
1179 | 0 | xmlBufferWriteChar(buf, "+"); |
1180 | 0 | break; |
1181 | 0 | } |
1182 | 0 | } |
1183 | | |
1184 | | /** |
1185 | | * xmlDumpElementContent: |
1186 | | * @buf: An XML buffer |
1187 | | * @content: An element table |
1188 | | * |
1189 | | * This will dump the content of the element table as an XML DTD definition |
1190 | | */ |
1191 | | static void |
1192 | 0 | xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content) { |
1193 | 0 | xmlElementContentPtr cur; |
1194 | |
|
1195 | 0 | if (content == NULL) return; |
1196 | | |
1197 | 0 | xmlBufferWriteChar(buf, "("); |
1198 | 0 | cur = content; |
1199 | |
|
1200 | 0 | do { |
1201 | 0 | if (cur == NULL) return; |
1202 | | |
1203 | 0 | switch (cur->type) { |
1204 | 0 | case XML_ELEMENT_CONTENT_PCDATA: |
1205 | 0 | xmlBufferWriteChar(buf, "#PCDATA"); |
1206 | 0 | break; |
1207 | 0 | case XML_ELEMENT_CONTENT_ELEMENT: |
1208 | 0 | if (cur->prefix != NULL) { |
1209 | 0 | xmlBufferWriteCHAR(buf, cur->prefix); |
1210 | 0 | xmlBufferWriteChar(buf, ":"); |
1211 | 0 | } |
1212 | 0 | xmlBufferWriteCHAR(buf, cur->name); |
1213 | 0 | break; |
1214 | 0 | case XML_ELEMENT_CONTENT_SEQ: |
1215 | 0 | case XML_ELEMENT_CONTENT_OR: |
1216 | 0 | if ((cur != content) && |
1217 | 0 | (cur->parent != NULL) && |
1218 | 0 | ((cur->type != cur->parent->type) || |
1219 | 0 | (cur->ocur != XML_ELEMENT_CONTENT_ONCE))) |
1220 | 0 | xmlBufferWriteChar(buf, "("); |
1221 | 0 | cur = cur->c1; |
1222 | 0 | continue; |
1223 | 0 | default: |
1224 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
1225 | 0 | "Internal: ELEMENT cur corrupted invalid type\n", |
1226 | 0 | NULL); |
1227 | 0 | } |
1228 | | |
1229 | 0 | while (cur != content) { |
1230 | 0 | xmlElementContentPtr parent = cur->parent; |
1231 | |
|
1232 | 0 | if (parent == NULL) return; |
1233 | | |
1234 | 0 | if (((cur->type == XML_ELEMENT_CONTENT_OR) || |
1235 | 0 | (cur->type == XML_ELEMENT_CONTENT_SEQ)) && |
1236 | 0 | ((cur->type != parent->type) || |
1237 | 0 | (cur->ocur != XML_ELEMENT_CONTENT_ONCE))) |
1238 | 0 | xmlBufferWriteChar(buf, ")"); |
1239 | 0 | xmlDumpElementOccur(buf, cur); |
1240 | |
|
1241 | 0 | if (cur == parent->c1) { |
1242 | 0 | if (parent->type == XML_ELEMENT_CONTENT_SEQ) |
1243 | 0 | xmlBufferWriteChar(buf, " , "); |
1244 | 0 | else if (parent->type == XML_ELEMENT_CONTENT_OR) |
1245 | 0 | xmlBufferWriteChar(buf, " | "); |
1246 | |
|
1247 | 0 | cur = parent->c2; |
1248 | 0 | break; |
1249 | 0 | } |
1250 | | |
1251 | 0 | cur = parent; |
1252 | 0 | } |
1253 | 0 | } while (cur != content); |
1254 | | |
1255 | 0 | xmlBufferWriteChar(buf, ")"); |
1256 | 0 | xmlDumpElementOccur(buf, content); |
1257 | 0 | } |
1258 | | |
1259 | | /** |
1260 | | * xmlSprintfElementContent: |
1261 | | * @buf: an output buffer |
1262 | | * @content: An element table |
1263 | | * @englob: 1 if one must print the englobing parenthesis, 0 otherwise |
1264 | | * |
1265 | | * Deprecated, unsafe, use xmlSnprintfElementContent |
1266 | | */ |
1267 | | void |
1268 | | xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED, |
1269 | | xmlElementContentPtr content ATTRIBUTE_UNUSED, |
1270 | 0 | int englob ATTRIBUTE_UNUSED) { |
1271 | 0 | } |
1272 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1273 | | |
1274 | | /** |
1275 | | * xmlSnprintfElementContent: |
1276 | | * @buf: an output buffer |
1277 | | * @size: the buffer size |
1278 | | * @content: An element table |
1279 | | * @englob: 1 if one must print the englobing parenthesis, 0 otherwise |
1280 | | * |
1281 | | * This will dump the content of the element content definition |
1282 | | * Intended just for the debug routine |
1283 | | */ |
1284 | | void |
1285 | 0 | xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) { |
1286 | 0 | int len; |
1287 | |
|
1288 | 0 | if (content == NULL) return; |
1289 | 0 | len = strlen(buf); |
1290 | 0 | if (size - len < 50) { |
1291 | 0 | if ((size - len > 4) && (buf[len - 1] != '.')) |
1292 | 0 | strcat(buf, " ..."); |
1293 | 0 | return; |
1294 | 0 | } |
1295 | 0 | if (englob) strcat(buf, "("); |
1296 | 0 | switch (content->type) { |
1297 | 0 | case XML_ELEMENT_CONTENT_PCDATA: |
1298 | 0 | strcat(buf, "#PCDATA"); |
1299 | 0 | break; |
1300 | 0 | case XML_ELEMENT_CONTENT_ELEMENT: { |
1301 | 0 | int qnameLen = xmlStrlen(content->name); |
1302 | |
|
1303 | 0 | if (content->prefix != NULL) |
1304 | 0 | qnameLen += xmlStrlen(content->prefix) + 1; |
1305 | 0 | if (size - len < qnameLen + 10) { |
1306 | 0 | strcat(buf, " ..."); |
1307 | 0 | return; |
1308 | 0 | } |
1309 | 0 | if (content->prefix != NULL) { |
1310 | 0 | strcat(buf, (char *) content->prefix); |
1311 | 0 | strcat(buf, ":"); |
1312 | 0 | } |
1313 | 0 | if (content->name != NULL) |
1314 | 0 | strcat(buf, (char *) content->name); |
1315 | 0 | break; |
1316 | 0 | } |
1317 | 0 | case XML_ELEMENT_CONTENT_SEQ: |
1318 | 0 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
1319 | 0 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
1320 | 0 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
1321 | 0 | else |
1322 | 0 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
1323 | 0 | len = strlen(buf); |
1324 | 0 | if (size - len < 50) { |
1325 | 0 | if ((size - len > 4) && (buf[len - 1] != '.')) |
1326 | 0 | strcat(buf, " ..."); |
1327 | 0 | return; |
1328 | 0 | } |
1329 | 0 | strcat(buf, " , "); |
1330 | 0 | if (((content->c2->type == XML_ELEMENT_CONTENT_OR) || |
1331 | 0 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
1332 | 0 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
1333 | 0 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
1334 | 0 | else |
1335 | 0 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
1336 | 0 | break; |
1337 | 0 | case XML_ELEMENT_CONTENT_OR: |
1338 | 0 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
1339 | 0 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
1340 | 0 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
1341 | 0 | else |
1342 | 0 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
1343 | 0 | len = strlen(buf); |
1344 | 0 | if (size - len < 50) { |
1345 | 0 | if ((size - len > 4) && (buf[len - 1] != '.')) |
1346 | 0 | strcat(buf, " ..."); |
1347 | 0 | return; |
1348 | 0 | } |
1349 | 0 | strcat(buf, " | "); |
1350 | 0 | if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) || |
1351 | 0 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
1352 | 0 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
1353 | 0 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
1354 | 0 | else |
1355 | 0 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
1356 | 0 | break; |
1357 | 0 | } |
1358 | 0 | if (size - strlen(buf) <= 2) return; |
1359 | 0 | if (englob) |
1360 | 0 | strcat(buf, ")"); |
1361 | 0 | switch (content->ocur) { |
1362 | 0 | case XML_ELEMENT_CONTENT_ONCE: |
1363 | 0 | break; |
1364 | 0 | case XML_ELEMENT_CONTENT_OPT: |
1365 | 0 | strcat(buf, "?"); |
1366 | 0 | break; |
1367 | 0 | case XML_ELEMENT_CONTENT_MULT: |
1368 | 0 | strcat(buf, "*"); |
1369 | 0 | break; |
1370 | 0 | case XML_ELEMENT_CONTENT_PLUS: |
1371 | 0 | strcat(buf, "+"); |
1372 | 0 | break; |
1373 | 0 | } |
1374 | 0 | } |
1375 | | |
1376 | | /**************************************************************** |
1377 | | * * |
1378 | | * Registration of DTD declarations * |
1379 | | * * |
1380 | | ****************************************************************/ |
1381 | | |
1382 | | /** |
1383 | | * xmlFreeElement: |
1384 | | * @elem: An element |
1385 | | * |
1386 | | * Deallocate the memory used by an element definition |
1387 | | */ |
1388 | | static void |
1389 | 0 | xmlFreeElement(xmlElementPtr elem) { |
1390 | 0 | if (elem == NULL) return; |
1391 | 0 | xmlUnlinkNode((xmlNodePtr) elem); |
1392 | 0 | xmlFreeDocElementContent(elem->doc, elem->content); |
1393 | 0 | if (elem->name != NULL) |
1394 | 0 | xmlFree((xmlChar *) elem->name); |
1395 | 0 | if (elem->prefix != NULL) |
1396 | 0 | xmlFree((xmlChar *) elem->prefix); |
1397 | 0 | #ifdef LIBXML_REGEXP_ENABLED |
1398 | 0 | if (elem->contModel != NULL) |
1399 | 0 | xmlRegFreeRegexp(elem->contModel); |
1400 | 0 | #endif |
1401 | 0 | xmlFree(elem); |
1402 | 0 | } |
1403 | | |
1404 | | |
1405 | | /** |
1406 | | * xmlAddElementDecl: |
1407 | | * @ctxt: the validation context |
1408 | | * @dtd: pointer to the DTD |
1409 | | * @name: the entity name |
1410 | | * @type: the element type |
1411 | | * @content: the element content tree or NULL |
1412 | | * |
1413 | | * Register a new element declaration |
1414 | | * |
1415 | | * Returns NULL if not, otherwise the entity |
1416 | | */ |
1417 | | xmlElementPtr |
1418 | | xmlAddElementDecl(xmlValidCtxtPtr ctxt, |
1419 | | xmlDtdPtr dtd, const xmlChar *name, |
1420 | | xmlElementTypeVal type, |
1421 | 0 | xmlElementContentPtr content) { |
1422 | 0 | xmlElementPtr ret; |
1423 | 0 | xmlElementTablePtr table; |
1424 | 0 | xmlAttributePtr oldAttributes = NULL; |
1425 | 0 | xmlChar *ns, *uqname; |
1426 | |
|
1427 | 0 | if (dtd == NULL) { |
1428 | 0 | return(NULL); |
1429 | 0 | } |
1430 | 0 | if (name == NULL) { |
1431 | 0 | return(NULL); |
1432 | 0 | } |
1433 | | |
1434 | 0 | switch (type) { |
1435 | 0 | case XML_ELEMENT_TYPE_EMPTY: |
1436 | 0 | if (content != NULL) { |
1437 | 0 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
1438 | 0 | "xmlAddElementDecl: content != NULL for EMPTY\n", |
1439 | 0 | NULL); |
1440 | 0 | return(NULL); |
1441 | 0 | } |
1442 | 0 | break; |
1443 | 0 | case XML_ELEMENT_TYPE_ANY: |
1444 | 0 | if (content != NULL) { |
1445 | 0 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
1446 | 0 | "xmlAddElementDecl: content != NULL for ANY\n", |
1447 | 0 | NULL); |
1448 | 0 | return(NULL); |
1449 | 0 | } |
1450 | 0 | break; |
1451 | 0 | case XML_ELEMENT_TYPE_MIXED: |
1452 | 0 | if (content == NULL) { |
1453 | 0 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
1454 | 0 | "xmlAddElementDecl: content == NULL for MIXED\n", |
1455 | 0 | NULL); |
1456 | 0 | return(NULL); |
1457 | 0 | } |
1458 | 0 | break; |
1459 | 0 | case XML_ELEMENT_TYPE_ELEMENT: |
1460 | 0 | if (content == NULL) { |
1461 | 0 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
1462 | 0 | "xmlAddElementDecl: content == NULL for ELEMENT\n", |
1463 | 0 | NULL); |
1464 | 0 | return(NULL); |
1465 | 0 | } |
1466 | 0 | break; |
1467 | 0 | default: |
1468 | 0 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
1469 | 0 | "Internal: ELEMENT decl corrupted invalid type\n", |
1470 | 0 | NULL); |
1471 | 0 | return(NULL); |
1472 | 0 | } |
1473 | | |
1474 | | /* |
1475 | | * check if name is a QName |
1476 | | */ |
1477 | 0 | uqname = xmlSplitQName2(name, &ns); |
1478 | 0 | if (uqname != NULL) |
1479 | 0 | name = uqname; |
1480 | | |
1481 | | /* |
1482 | | * Create the Element table if needed. |
1483 | | */ |
1484 | 0 | table = (xmlElementTablePtr) dtd->elements; |
1485 | 0 | if (table == NULL) { |
1486 | 0 | xmlDictPtr dict = NULL; |
1487 | |
|
1488 | 0 | if (dtd->doc != NULL) |
1489 | 0 | dict = dtd->doc->dict; |
1490 | 0 | table = xmlHashCreateDict(0, dict); |
1491 | 0 | dtd->elements = (void *) table; |
1492 | 0 | } |
1493 | 0 | if (table == NULL) { |
1494 | 0 | xmlVErrMemory(ctxt, |
1495 | 0 | "xmlAddElementDecl: Table creation failed!\n"); |
1496 | 0 | if (uqname != NULL) |
1497 | 0 | xmlFree(uqname); |
1498 | 0 | if (ns != NULL) |
1499 | 0 | xmlFree(ns); |
1500 | 0 | return(NULL); |
1501 | 0 | } |
1502 | | |
1503 | | /* |
1504 | | * lookup old attributes inserted on an undefined element in the |
1505 | | * internal subset. |
1506 | | */ |
1507 | 0 | if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) { |
1508 | 0 | ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns); |
1509 | 0 | if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) { |
1510 | 0 | oldAttributes = ret->attributes; |
1511 | 0 | ret->attributes = NULL; |
1512 | 0 | xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL); |
1513 | 0 | xmlFreeElement(ret); |
1514 | 0 | } |
1515 | 0 | } |
1516 | | |
1517 | | /* |
1518 | | * The element may already be present if one of its attribute |
1519 | | * was registered first |
1520 | | */ |
1521 | 0 | ret = xmlHashLookup2(table, name, ns); |
1522 | 0 | if (ret != NULL) { |
1523 | 0 | if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) { |
1524 | 0 | #ifdef LIBXML_VALID_ENABLED |
1525 | | /* |
1526 | | * The element is already defined in this DTD. |
1527 | | */ |
1528 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
1529 | 0 | "Redefinition of element %s\n", |
1530 | 0 | name, NULL, NULL); |
1531 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
1532 | 0 | if (uqname != NULL) |
1533 | 0 | xmlFree(uqname); |
1534 | 0 | if (ns != NULL) |
1535 | 0 | xmlFree(ns); |
1536 | 0 | return(NULL); |
1537 | 0 | } |
1538 | 0 | if (ns != NULL) { |
1539 | 0 | xmlFree(ns); |
1540 | 0 | ns = NULL; |
1541 | 0 | } |
1542 | 0 | } else { |
1543 | 0 | ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
1544 | 0 | if (ret == NULL) { |
1545 | 0 | xmlVErrMemory(ctxt, "malloc failed"); |
1546 | 0 | if (uqname != NULL) |
1547 | 0 | xmlFree(uqname); |
1548 | 0 | if (ns != NULL) |
1549 | 0 | xmlFree(ns); |
1550 | 0 | return(NULL); |
1551 | 0 | } |
1552 | 0 | memset(ret, 0, sizeof(xmlElement)); |
1553 | 0 | ret->type = XML_ELEMENT_DECL; |
1554 | | |
1555 | | /* |
1556 | | * fill the structure. |
1557 | | */ |
1558 | 0 | ret->name = xmlStrdup(name); |
1559 | 0 | if (ret->name == NULL) { |
1560 | 0 | xmlVErrMemory(ctxt, "malloc failed"); |
1561 | 0 | if (uqname != NULL) |
1562 | 0 | xmlFree(uqname); |
1563 | 0 | if (ns != NULL) |
1564 | 0 | xmlFree(ns); |
1565 | 0 | xmlFree(ret); |
1566 | 0 | return(NULL); |
1567 | 0 | } |
1568 | 0 | ret->prefix = ns; |
1569 | | |
1570 | | /* |
1571 | | * Validity Check: |
1572 | | * Insertion must not fail |
1573 | | */ |
1574 | 0 | if (xmlHashAddEntry2(table, name, ns, ret)) { |
1575 | 0 | #ifdef LIBXML_VALID_ENABLED |
1576 | | /* |
1577 | | * The element is already defined in this DTD. |
1578 | | */ |
1579 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
1580 | 0 | "Redefinition of element %s\n", |
1581 | 0 | name, NULL, NULL); |
1582 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
1583 | 0 | xmlFreeElement(ret); |
1584 | 0 | if (uqname != NULL) |
1585 | 0 | xmlFree(uqname); |
1586 | 0 | return(NULL); |
1587 | 0 | } |
1588 | | /* |
1589 | | * For new element, may have attributes from earlier |
1590 | | * definition in internal subset |
1591 | | */ |
1592 | 0 | ret->attributes = oldAttributes; |
1593 | 0 | } |
1594 | | |
1595 | | /* |
1596 | | * Finish to fill the structure. |
1597 | | */ |
1598 | 0 | ret->etype = type; |
1599 | | /* |
1600 | | * Avoid a stupid copy when called by the parser |
1601 | | * and flag it by setting a special parent value |
1602 | | * so the parser doesn't unallocate it. |
1603 | | */ |
1604 | 0 | if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_USE_PCTXT)) { |
1605 | 0 | ret->content = content; |
1606 | 0 | if (content != NULL) |
1607 | 0 | content->parent = (xmlElementContentPtr) 1; |
1608 | 0 | } else { |
1609 | 0 | ret->content = xmlCopyDocElementContent(dtd->doc, content); |
1610 | 0 | } |
1611 | | |
1612 | | /* |
1613 | | * Link it to the DTD |
1614 | | */ |
1615 | 0 | ret->parent = dtd; |
1616 | 0 | ret->doc = dtd->doc; |
1617 | 0 | if (dtd->last == NULL) { |
1618 | 0 | dtd->children = dtd->last = (xmlNodePtr) ret; |
1619 | 0 | } else { |
1620 | 0 | dtd->last->next = (xmlNodePtr) ret; |
1621 | 0 | ret->prev = dtd->last; |
1622 | 0 | dtd->last = (xmlNodePtr) ret; |
1623 | 0 | } |
1624 | 0 | if (uqname != NULL) |
1625 | 0 | xmlFree(uqname); |
1626 | 0 | return(ret); |
1627 | 0 | } |
1628 | | |
1629 | | static void |
1630 | 0 | xmlFreeElementTableEntry(void *elem, const xmlChar *name ATTRIBUTE_UNUSED) { |
1631 | 0 | xmlFreeElement((xmlElementPtr) elem); |
1632 | 0 | } |
1633 | | |
1634 | | /** |
1635 | | * xmlFreeElementTable: |
1636 | | * @table: An element table |
1637 | | * |
1638 | | * Deallocate the memory used by an element hash table. |
1639 | | */ |
1640 | | void |
1641 | 0 | xmlFreeElementTable(xmlElementTablePtr table) { |
1642 | 0 | xmlHashFree(table, xmlFreeElementTableEntry); |
1643 | 0 | } |
1644 | | |
1645 | | #ifdef LIBXML_TREE_ENABLED |
1646 | | /** |
1647 | | * xmlCopyElement: |
1648 | | * @elem: An element |
1649 | | * |
1650 | | * Build a copy of an element. |
1651 | | * |
1652 | | * Returns the new xmlElementPtr or NULL in case of error. |
1653 | | */ |
1654 | | static void * |
1655 | 0 | xmlCopyElement(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
1656 | 0 | xmlElementPtr elem = (xmlElementPtr) payload; |
1657 | 0 | xmlElementPtr cur; |
1658 | |
|
1659 | 0 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
1660 | 0 | if (cur == NULL) { |
1661 | 0 | xmlVErrMemory(NULL, "malloc failed"); |
1662 | 0 | return(NULL); |
1663 | 0 | } |
1664 | 0 | memset(cur, 0, sizeof(xmlElement)); |
1665 | 0 | cur->type = XML_ELEMENT_DECL; |
1666 | 0 | cur->etype = elem->etype; |
1667 | 0 | if (elem->name != NULL) |
1668 | 0 | cur->name = xmlStrdup(elem->name); |
1669 | 0 | else |
1670 | 0 | cur->name = NULL; |
1671 | 0 | if (elem->prefix != NULL) |
1672 | 0 | cur->prefix = xmlStrdup(elem->prefix); |
1673 | 0 | else |
1674 | 0 | cur->prefix = NULL; |
1675 | 0 | cur->content = xmlCopyElementContent(elem->content); |
1676 | | /* TODO : rebuild the attribute list on the copy */ |
1677 | 0 | cur->attributes = NULL; |
1678 | 0 | return(cur); |
1679 | 0 | } |
1680 | | |
1681 | | /** |
1682 | | * xmlCopyElementTable: |
1683 | | * @table: An element table |
1684 | | * |
1685 | | * Build a copy of an element table. |
1686 | | * |
1687 | | * Returns the new xmlElementTablePtr or NULL in case of error. |
1688 | | */ |
1689 | | xmlElementTablePtr |
1690 | 0 | xmlCopyElementTable(xmlElementTablePtr table) { |
1691 | 0 | return((xmlElementTablePtr) xmlHashCopy(table, xmlCopyElement)); |
1692 | 0 | } |
1693 | | #endif /* LIBXML_TREE_ENABLED */ |
1694 | | |
1695 | | #ifdef LIBXML_OUTPUT_ENABLED |
1696 | | /** |
1697 | | * xmlDumpElementDecl: |
1698 | | * @buf: the XML buffer output |
1699 | | * @elem: An element table |
1700 | | * |
1701 | | * This will dump the content of the element declaration as an XML |
1702 | | * DTD definition |
1703 | | */ |
1704 | | void |
1705 | 0 | xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) { |
1706 | 0 | if ((buf == NULL) || (elem == NULL)) |
1707 | 0 | return; |
1708 | 0 | switch (elem->etype) { |
1709 | 0 | case XML_ELEMENT_TYPE_EMPTY: |
1710 | 0 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
1711 | 0 | if (elem->prefix != NULL) { |
1712 | 0 | xmlBufferWriteCHAR(buf, elem->prefix); |
1713 | 0 | xmlBufferWriteChar(buf, ":"); |
1714 | 0 | } |
1715 | 0 | xmlBufferWriteCHAR(buf, elem->name); |
1716 | 0 | xmlBufferWriteChar(buf, " EMPTY>\n"); |
1717 | 0 | break; |
1718 | 0 | case XML_ELEMENT_TYPE_ANY: |
1719 | 0 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
1720 | 0 | if (elem->prefix != NULL) { |
1721 | 0 | xmlBufferWriteCHAR(buf, elem->prefix); |
1722 | 0 | xmlBufferWriteChar(buf, ":"); |
1723 | 0 | } |
1724 | 0 | xmlBufferWriteCHAR(buf, elem->name); |
1725 | 0 | xmlBufferWriteChar(buf, " ANY>\n"); |
1726 | 0 | break; |
1727 | 0 | case XML_ELEMENT_TYPE_MIXED: |
1728 | 0 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
1729 | 0 | if (elem->prefix != NULL) { |
1730 | 0 | xmlBufferWriteCHAR(buf, elem->prefix); |
1731 | 0 | xmlBufferWriteChar(buf, ":"); |
1732 | 0 | } |
1733 | 0 | xmlBufferWriteCHAR(buf, elem->name); |
1734 | 0 | xmlBufferWriteChar(buf, " "); |
1735 | 0 | xmlDumpElementContent(buf, elem->content); |
1736 | 0 | xmlBufferWriteChar(buf, ">\n"); |
1737 | 0 | break; |
1738 | 0 | case XML_ELEMENT_TYPE_ELEMENT: |
1739 | 0 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
1740 | 0 | if (elem->prefix != NULL) { |
1741 | 0 | xmlBufferWriteCHAR(buf, elem->prefix); |
1742 | 0 | xmlBufferWriteChar(buf, ":"); |
1743 | 0 | } |
1744 | 0 | xmlBufferWriteCHAR(buf, elem->name); |
1745 | 0 | xmlBufferWriteChar(buf, " "); |
1746 | 0 | xmlDumpElementContent(buf, elem->content); |
1747 | 0 | xmlBufferWriteChar(buf, ">\n"); |
1748 | 0 | break; |
1749 | 0 | default: |
1750 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
1751 | 0 | "Internal: ELEMENT struct corrupted invalid type\n", |
1752 | 0 | NULL); |
1753 | 0 | } |
1754 | 0 | } |
1755 | | |
1756 | | /** |
1757 | | * xmlDumpElementDeclScan: |
1758 | | * @elem: An element table |
1759 | | * @buf: the XML buffer output |
1760 | | * |
1761 | | * This routine is used by the hash scan function. It just reverses |
1762 | | * the arguments. |
1763 | | */ |
1764 | | static void |
1765 | | xmlDumpElementDeclScan(void *elem, void *buf, |
1766 | 0 | const xmlChar *name ATTRIBUTE_UNUSED) { |
1767 | 0 | xmlDumpElementDecl((xmlBufferPtr) buf, (xmlElementPtr) elem); |
1768 | 0 | } |
1769 | | |
1770 | | /** |
1771 | | * xmlDumpElementTable: |
1772 | | * @buf: the XML buffer output |
1773 | | * @table: An element table |
1774 | | * |
1775 | | * This will dump the content of the element table as an XML DTD definition |
1776 | | */ |
1777 | | void |
1778 | 0 | xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) { |
1779 | 0 | if ((buf == NULL) || (table == NULL)) |
1780 | 0 | return; |
1781 | 0 | xmlHashScan(table, xmlDumpElementDeclScan, buf); |
1782 | 0 | } |
1783 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1784 | | |
1785 | | /** |
1786 | | * xmlCreateEnumeration: |
1787 | | * @name: the enumeration name or NULL |
1788 | | * |
1789 | | * create and initialize an enumeration attribute node. |
1790 | | * |
1791 | | * Returns the xmlEnumerationPtr just created or NULL in case |
1792 | | * of error. |
1793 | | */ |
1794 | | xmlEnumerationPtr |
1795 | 3.24k | xmlCreateEnumeration(const xmlChar *name) { |
1796 | 3.24k | xmlEnumerationPtr ret; |
1797 | | |
1798 | 3.24k | ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration)); |
1799 | 3.24k | if (ret == NULL) { |
1800 | 0 | xmlVErrMemory(NULL, "malloc failed"); |
1801 | 0 | return(NULL); |
1802 | 0 | } |
1803 | 3.24k | memset(ret, 0, sizeof(xmlEnumeration)); |
1804 | | |
1805 | 3.24k | if (name != NULL) |
1806 | 3.24k | ret->name = xmlStrdup(name); |
1807 | 3.24k | return(ret); |
1808 | 3.24k | } |
1809 | | |
1810 | | /** |
1811 | | * xmlFreeEnumeration: |
1812 | | * @cur: the tree to free. |
1813 | | * |
1814 | | * free an enumeration attribute node (recursive). |
1815 | | */ |
1816 | | void |
1817 | 3.51k | xmlFreeEnumeration(xmlEnumerationPtr cur) { |
1818 | 3.51k | if (cur == NULL) return; |
1819 | | |
1820 | 3.24k | if (cur->next != NULL) xmlFreeEnumeration(cur->next); |
1821 | | |
1822 | 3.24k | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
1823 | 3.24k | xmlFree(cur); |
1824 | 3.24k | } |
1825 | | |
1826 | | #ifdef LIBXML_TREE_ENABLED |
1827 | | /** |
1828 | | * xmlCopyEnumeration: |
1829 | | * @cur: the tree to copy. |
1830 | | * |
1831 | | * Copy an enumeration attribute node (recursive). |
1832 | | * |
1833 | | * Returns the xmlEnumerationPtr just created or NULL in case |
1834 | | * of error. |
1835 | | */ |
1836 | | xmlEnumerationPtr |
1837 | 0 | xmlCopyEnumeration(xmlEnumerationPtr cur) { |
1838 | 0 | xmlEnumerationPtr ret; |
1839 | |
|
1840 | 0 | if (cur == NULL) return(NULL); |
1841 | 0 | ret = xmlCreateEnumeration((xmlChar *) cur->name); |
1842 | 0 | if (ret == NULL) return(NULL); |
1843 | | |
1844 | 0 | if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next); |
1845 | 0 | else ret->next = NULL; |
1846 | |
|
1847 | 0 | return(ret); |
1848 | 0 | } |
1849 | | #endif /* LIBXML_TREE_ENABLED */ |
1850 | | |
1851 | | #ifdef LIBXML_OUTPUT_ENABLED |
1852 | | /** |
1853 | | * xmlDumpEnumeration: |
1854 | | * @buf: the XML buffer output |
1855 | | * @enum: An enumeration |
1856 | | * |
1857 | | * This will dump the content of the enumeration |
1858 | | */ |
1859 | | static void |
1860 | 0 | xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) { |
1861 | 0 | if ((buf == NULL) || (cur == NULL)) |
1862 | 0 | return; |
1863 | | |
1864 | 0 | xmlBufferWriteCHAR(buf, cur->name); |
1865 | 0 | if (cur->next == NULL) |
1866 | 0 | xmlBufferWriteChar(buf, ")"); |
1867 | 0 | else { |
1868 | 0 | xmlBufferWriteChar(buf, " | "); |
1869 | 0 | xmlDumpEnumeration(buf, cur->next); |
1870 | 0 | } |
1871 | 0 | } |
1872 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1873 | | |
1874 | | #ifdef LIBXML_VALID_ENABLED |
1875 | | /** |
1876 | | * xmlScanIDAttributeDecl: |
1877 | | * @ctxt: the validation context |
1878 | | * @elem: the element name |
1879 | | * @err: whether to raise errors here |
1880 | | * |
1881 | | * Verify that the element don't have too many ID attributes |
1882 | | * declared. |
1883 | | * |
1884 | | * Returns the number of ID attributes found. |
1885 | | */ |
1886 | | static int |
1887 | 0 | xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) { |
1888 | 0 | xmlAttributePtr cur; |
1889 | 0 | int ret = 0; |
1890 | |
|
1891 | 0 | if (elem == NULL) return(0); |
1892 | 0 | cur = elem->attributes; |
1893 | 0 | while (cur != NULL) { |
1894 | 0 | if (cur->atype == XML_ATTRIBUTE_ID) { |
1895 | 0 | ret ++; |
1896 | 0 | if ((ret > 1) && (err)) |
1897 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID, |
1898 | 0 | "Element %s has too many ID attributes defined : %s\n", |
1899 | 0 | elem->name, cur->name, NULL); |
1900 | 0 | } |
1901 | 0 | cur = cur->nexth; |
1902 | 0 | } |
1903 | 0 | return(ret); |
1904 | 0 | } |
1905 | | #endif /* LIBXML_VALID_ENABLED */ |
1906 | | |
1907 | | /** |
1908 | | * xmlFreeAttribute: |
1909 | | * @elem: An attribute |
1910 | | * |
1911 | | * Deallocate the memory used by an attribute definition |
1912 | | */ |
1913 | | static void |
1914 | 0 | xmlFreeAttribute(xmlAttributePtr attr) { |
1915 | 0 | xmlDictPtr dict; |
1916 | |
|
1917 | 0 | if (attr == NULL) return; |
1918 | 0 | if (attr->doc != NULL) |
1919 | 0 | dict = attr->doc->dict; |
1920 | 0 | else |
1921 | 0 | dict = NULL; |
1922 | 0 | xmlUnlinkNode((xmlNodePtr) attr); |
1923 | 0 | if (attr->tree != NULL) |
1924 | 0 | xmlFreeEnumeration(attr->tree); |
1925 | 0 | if (dict) { |
1926 | 0 | if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem))) |
1927 | 0 | xmlFree((xmlChar *) attr->elem); |
1928 | 0 | if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name))) |
1929 | 0 | xmlFree((xmlChar *) attr->name); |
1930 | 0 | if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix))) |
1931 | 0 | xmlFree((xmlChar *) attr->prefix); |
1932 | 0 | if ((attr->defaultValue != NULL) && |
1933 | 0 | (!xmlDictOwns(dict, attr->defaultValue))) |
1934 | 0 | xmlFree((xmlChar *) attr->defaultValue); |
1935 | 0 | } else { |
1936 | 0 | if (attr->elem != NULL) |
1937 | 0 | xmlFree((xmlChar *) attr->elem); |
1938 | 0 | if (attr->name != NULL) |
1939 | 0 | xmlFree((xmlChar *) attr->name); |
1940 | 0 | if (attr->defaultValue != NULL) |
1941 | 0 | xmlFree((xmlChar *) attr->defaultValue); |
1942 | 0 | if (attr->prefix != NULL) |
1943 | 0 | xmlFree((xmlChar *) attr->prefix); |
1944 | 0 | } |
1945 | 0 | xmlFree(attr); |
1946 | 0 | } |
1947 | | |
1948 | | |
1949 | | /** |
1950 | | * xmlAddAttributeDecl: |
1951 | | * @ctxt: the validation context |
1952 | | * @dtd: pointer to the DTD |
1953 | | * @elem: the element name |
1954 | | * @name: the attribute name |
1955 | | * @ns: the attribute namespace prefix |
1956 | | * @type: the attribute type |
1957 | | * @def: the attribute default type |
1958 | | * @defaultValue: the attribute default value |
1959 | | * @tree: if it's an enumeration, the associated list |
1960 | | * |
1961 | | * Register a new attribute declaration |
1962 | | * Note that @tree becomes the ownership of the DTD |
1963 | | * |
1964 | | * Returns NULL if not new, otherwise the attribute decl |
1965 | | */ |
1966 | | xmlAttributePtr |
1967 | | xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, |
1968 | | xmlDtdPtr dtd, const xmlChar *elem, |
1969 | | const xmlChar *name, const xmlChar *ns, |
1970 | | xmlAttributeType type, xmlAttributeDefault def, |
1971 | 0 | const xmlChar *defaultValue, xmlEnumerationPtr tree) { |
1972 | 0 | xmlAttributePtr ret; |
1973 | 0 | xmlAttributeTablePtr table; |
1974 | 0 | xmlElementPtr elemDef; |
1975 | 0 | xmlDictPtr dict = NULL; |
1976 | |
|
1977 | 0 | if (dtd == NULL) { |
1978 | 0 | xmlFreeEnumeration(tree); |
1979 | 0 | return(NULL); |
1980 | 0 | } |
1981 | 0 | if (name == NULL) { |
1982 | 0 | xmlFreeEnumeration(tree); |
1983 | 0 | return(NULL); |
1984 | 0 | } |
1985 | 0 | if (elem == NULL) { |
1986 | 0 | xmlFreeEnumeration(tree); |
1987 | 0 | return(NULL); |
1988 | 0 | } |
1989 | 0 | if (dtd->doc != NULL) |
1990 | 0 | dict = dtd->doc->dict; |
1991 | |
|
1992 | 0 | #ifdef LIBXML_VALID_ENABLED |
1993 | | /* |
1994 | | * Check the type and possibly the default value. |
1995 | | */ |
1996 | 0 | switch (type) { |
1997 | 0 | case XML_ATTRIBUTE_CDATA: |
1998 | 0 | break; |
1999 | 0 | case XML_ATTRIBUTE_ID: |
2000 | 0 | break; |
2001 | 0 | case XML_ATTRIBUTE_IDREF: |
2002 | 0 | break; |
2003 | 0 | case XML_ATTRIBUTE_IDREFS: |
2004 | 0 | break; |
2005 | 0 | case XML_ATTRIBUTE_ENTITY: |
2006 | 0 | break; |
2007 | 0 | case XML_ATTRIBUTE_ENTITIES: |
2008 | 0 | break; |
2009 | 0 | case XML_ATTRIBUTE_NMTOKEN: |
2010 | 0 | break; |
2011 | 0 | case XML_ATTRIBUTE_NMTOKENS: |
2012 | 0 | break; |
2013 | 0 | case XML_ATTRIBUTE_ENUMERATION: |
2014 | 0 | break; |
2015 | 0 | case XML_ATTRIBUTE_NOTATION: |
2016 | 0 | break; |
2017 | 0 | default: |
2018 | 0 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
2019 | 0 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
2020 | 0 | NULL); |
2021 | 0 | xmlFreeEnumeration(tree); |
2022 | 0 | return(NULL); |
2023 | 0 | } |
2024 | 0 | if ((defaultValue != NULL) && |
2025 | 0 | (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) { |
2026 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT, |
2027 | 0 | "Attribute %s of %s: invalid default value\n", |
2028 | 0 | elem, name, defaultValue); |
2029 | 0 | defaultValue = NULL; |
2030 | 0 | if (ctxt != NULL) |
2031 | 0 | ctxt->valid = 0; |
2032 | 0 | } |
2033 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
2034 | | |
2035 | | /* |
2036 | | * Check first that an attribute defined in the external subset wasn't |
2037 | | * already defined in the internal subset |
2038 | | */ |
2039 | 0 | if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) && |
2040 | 0 | (dtd->doc->intSubset != NULL) && |
2041 | 0 | (dtd->doc->intSubset->attributes != NULL)) { |
2042 | 0 | ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem); |
2043 | 0 | if (ret != NULL) { |
2044 | 0 | xmlFreeEnumeration(tree); |
2045 | 0 | return(NULL); |
2046 | 0 | } |
2047 | 0 | } |
2048 | | |
2049 | | /* |
2050 | | * Create the Attribute table if needed. |
2051 | | */ |
2052 | 0 | table = (xmlAttributeTablePtr) dtd->attributes; |
2053 | 0 | if (table == NULL) { |
2054 | 0 | table = xmlHashCreateDict(0, dict); |
2055 | 0 | dtd->attributes = (void *) table; |
2056 | 0 | } |
2057 | 0 | if (table == NULL) { |
2058 | 0 | xmlVErrMemory(ctxt, |
2059 | 0 | "xmlAddAttributeDecl: Table creation failed!\n"); |
2060 | 0 | xmlFreeEnumeration(tree); |
2061 | 0 | return(NULL); |
2062 | 0 | } |
2063 | | |
2064 | | |
2065 | 0 | ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
2066 | 0 | if (ret == NULL) { |
2067 | 0 | xmlVErrMemory(ctxt, "malloc failed"); |
2068 | 0 | xmlFreeEnumeration(tree); |
2069 | 0 | return(NULL); |
2070 | 0 | } |
2071 | 0 | memset(ret, 0, sizeof(xmlAttribute)); |
2072 | 0 | ret->type = XML_ATTRIBUTE_DECL; |
2073 | | |
2074 | | /* |
2075 | | * fill the structure. |
2076 | | */ |
2077 | 0 | ret->atype = type; |
2078 | | /* |
2079 | | * doc must be set before possible error causes call |
2080 | | * to xmlFreeAttribute (because it's used to check on |
2081 | | * dict use) |
2082 | | */ |
2083 | 0 | ret->doc = dtd->doc; |
2084 | 0 | if (dict) { |
2085 | 0 | ret->name = xmlDictLookup(dict, name, -1); |
2086 | 0 | ret->prefix = xmlDictLookup(dict, ns, -1); |
2087 | 0 | ret->elem = xmlDictLookup(dict, elem, -1); |
2088 | 0 | } else { |
2089 | 0 | ret->name = xmlStrdup(name); |
2090 | 0 | ret->prefix = xmlStrdup(ns); |
2091 | 0 | ret->elem = xmlStrdup(elem); |
2092 | 0 | } |
2093 | 0 | ret->def = def; |
2094 | 0 | ret->tree = tree; |
2095 | 0 | if (defaultValue != NULL) { |
2096 | 0 | if (dict) |
2097 | 0 | ret->defaultValue = xmlDictLookup(dict, defaultValue, -1); |
2098 | 0 | else |
2099 | 0 | ret->defaultValue = xmlStrdup(defaultValue); |
2100 | 0 | } |
2101 | | |
2102 | | /* |
2103 | | * Validity Check: |
2104 | | * Search the DTD for previous declarations of the ATTLIST |
2105 | | */ |
2106 | 0 | if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) { |
2107 | 0 | #ifdef LIBXML_VALID_ENABLED |
2108 | | /* |
2109 | | * The attribute is already defined in this DTD. |
2110 | | */ |
2111 | 0 | xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED, |
2112 | 0 | "Attribute %s of element %s: already defined\n", |
2113 | 0 | name, elem, NULL); |
2114 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
2115 | 0 | xmlFreeAttribute(ret); |
2116 | 0 | return(NULL); |
2117 | 0 | } |
2118 | | |
2119 | | /* |
2120 | | * Validity Check: |
2121 | | * Multiple ID per element |
2122 | | */ |
2123 | 0 | elemDef = xmlGetDtdElementDesc2(dtd, elem, 1); |
2124 | 0 | if (elemDef != NULL) { |
2125 | |
|
2126 | 0 | #ifdef LIBXML_VALID_ENABLED |
2127 | 0 | if ((type == XML_ATTRIBUTE_ID) && |
2128 | 0 | (xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) { |
2129 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID, |
2130 | 0 | "Element %s has too may ID attributes defined : %s\n", |
2131 | 0 | elem, name, NULL); |
2132 | 0 | if (ctxt != NULL) |
2133 | 0 | ctxt->valid = 0; |
2134 | 0 | } |
2135 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
2136 | | |
2137 | | /* |
2138 | | * Insert namespace default def first they need to be |
2139 | | * processed first. |
2140 | | */ |
2141 | 0 | if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) || |
2142 | 0 | ((ret->prefix != NULL && |
2143 | 0 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) { |
2144 | 0 | ret->nexth = elemDef->attributes; |
2145 | 0 | elemDef->attributes = ret; |
2146 | 0 | } else { |
2147 | 0 | xmlAttributePtr tmp = elemDef->attributes; |
2148 | |
|
2149 | 0 | while ((tmp != NULL) && |
2150 | 0 | ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) || |
2151 | 0 | ((ret->prefix != NULL && |
2152 | 0 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) { |
2153 | 0 | if (tmp->nexth == NULL) |
2154 | 0 | break; |
2155 | 0 | tmp = tmp->nexth; |
2156 | 0 | } |
2157 | 0 | if (tmp != NULL) { |
2158 | 0 | ret->nexth = tmp->nexth; |
2159 | 0 | tmp->nexth = ret; |
2160 | 0 | } else { |
2161 | 0 | ret->nexth = elemDef->attributes; |
2162 | 0 | elemDef->attributes = ret; |
2163 | 0 | } |
2164 | 0 | } |
2165 | 0 | } |
2166 | | |
2167 | | /* |
2168 | | * Link it to the DTD |
2169 | | */ |
2170 | 0 | ret->parent = dtd; |
2171 | 0 | if (dtd->last == NULL) { |
2172 | 0 | dtd->children = dtd->last = (xmlNodePtr) ret; |
2173 | 0 | } else { |
2174 | 0 | dtd->last->next = (xmlNodePtr) ret; |
2175 | 0 | ret->prev = dtd->last; |
2176 | 0 | dtd->last = (xmlNodePtr) ret; |
2177 | 0 | } |
2178 | 0 | return(ret); |
2179 | 0 | } |
2180 | | |
2181 | | static void |
2182 | 0 | xmlFreeAttributeTableEntry(void *attr, const xmlChar *name ATTRIBUTE_UNUSED) { |
2183 | 0 | xmlFreeAttribute((xmlAttributePtr) attr); |
2184 | 0 | } |
2185 | | |
2186 | | /** |
2187 | | * xmlFreeAttributeTable: |
2188 | | * @table: An attribute table |
2189 | | * |
2190 | | * Deallocate the memory used by an entities hash table. |
2191 | | */ |
2192 | | void |
2193 | 0 | xmlFreeAttributeTable(xmlAttributeTablePtr table) { |
2194 | 0 | xmlHashFree(table, xmlFreeAttributeTableEntry); |
2195 | 0 | } |
2196 | | |
2197 | | #ifdef LIBXML_TREE_ENABLED |
2198 | | /** |
2199 | | * xmlCopyAttribute: |
2200 | | * @attr: An attribute |
2201 | | * |
2202 | | * Build a copy of an attribute. |
2203 | | * |
2204 | | * Returns the new xmlAttributePtr or NULL in case of error. |
2205 | | */ |
2206 | | static void * |
2207 | 0 | xmlCopyAttribute(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
2208 | 0 | xmlAttributePtr attr = (xmlAttributePtr) payload; |
2209 | 0 | xmlAttributePtr cur; |
2210 | |
|
2211 | 0 | cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
2212 | 0 | if (cur == NULL) { |
2213 | 0 | xmlVErrMemory(NULL, "malloc failed"); |
2214 | 0 | return(NULL); |
2215 | 0 | } |
2216 | 0 | memset(cur, 0, sizeof(xmlAttribute)); |
2217 | 0 | cur->type = XML_ATTRIBUTE_DECL; |
2218 | 0 | cur->atype = attr->atype; |
2219 | 0 | cur->def = attr->def; |
2220 | 0 | cur->tree = xmlCopyEnumeration(attr->tree); |
2221 | 0 | if (attr->elem != NULL) |
2222 | 0 | cur->elem = xmlStrdup(attr->elem); |
2223 | 0 | if (attr->name != NULL) |
2224 | 0 | cur->name = xmlStrdup(attr->name); |
2225 | 0 | if (attr->prefix != NULL) |
2226 | 0 | cur->prefix = xmlStrdup(attr->prefix); |
2227 | 0 | if (attr->defaultValue != NULL) |
2228 | 0 | cur->defaultValue = xmlStrdup(attr->defaultValue); |
2229 | 0 | return(cur); |
2230 | 0 | } |
2231 | | |
2232 | | /** |
2233 | | * xmlCopyAttributeTable: |
2234 | | * @table: An attribute table |
2235 | | * |
2236 | | * Build a copy of an attribute table. |
2237 | | * |
2238 | | * Returns the new xmlAttributeTablePtr or NULL in case of error. |
2239 | | */ |
2240 | | xmlAttributeTablePtr |
2241 | 0 | xmlCopyAttributeTable(xmlAttributeTablePtr table) { |
2242 | 0 | return((xmlAttributeTablePtr) xmlHashCopy(table, xmlCopyAttribute)); |
2243 | 0 | } |
2244 | | #endif /* LIBXML_TREE_ENABLED */ |
2245 | | |
2246 | | #ifdef LIBXML_OUTPUT_ENABLED |
2247 | | /** |
2248 | | * xmlDumpAttributeDecl: |
2249 | | * @buf: the XML buffer output |
2250 | | * @attr: An attribute declaration |
2251 | | * |
2252 | | * This will dump the content of the attribute declaration as an XML |
2253 | | * DTD definition |
2254 | | */ |
2255 | | void |
2256 | 0 | xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) { |
2257 | 0 | if ((buf == NULL) || (attr == NULL)) |
2258 | 0 | return; |
2259 | 0 | xmlBufferWriteChar(buf, "<!ATTLIST "); |
2260 | 0 | xmlBufferWriteCHAR(buf, attr->elem); |
2261 | 0 | xmlBufferWriteChar(buf, " "); |
2262 | 0 | if (attr->prefix != NULL) { |
2263 | 0 | xmlBufferWriteCHAR(buf, attr->prefix); |
2264 | 0 | xmlBufferWriteChar(buf, ":"); |
2265 | 0 | } |
2266 | 0 | xmlBufferWriteCHAR(buf, attr->name); |
2267 | 0 | switch (attr->atype) { |
2268 | 0 | case XML_ATTRIBUTE_CDATA: |
2269 | 0 | xmlBufferWriteChar(buf, " CDATA"); |
2270 | 0 | break; |
2271 | 0 | case XML_ATTRIBUTE_ID: |
2272 | 0 | xmlBufferWriteChar(buf, " ID"); |
2273 | 0 | break; |
2274 | 0 | case XML_ATTRIBUTE_IDREF: |
2275 | 0 | xmlBufferWriteChar(buf, " IDREF"); |
2276 | 0 | break; |
2277 | 0 | case XML_ATTRIBUTE_IDREFS: |
2278 | 0 | xmlBufferWriteChar(buf, " IDREFS"); |
2279 | 0 | break; |
2280 | 0 | case XML_ATTRIBUTE_ENTITY: |
2281 | 0 | xmlBufferWriteChar(buf, " ENTITY"); |
2282 | 0 | break; |
2283 | 0 | case XML_ATTRIBUTE_ENTITIES: |
2284 | 0 | xmlBufferWriteChar(buf, " ENTITIES"); |
2285 | 0 | break; |
2286 | 0 | case XML_ATTRIBUTE_NMTOKEN: |
2287 | 0 | xmlBufferWriteChar(buf, " NMTOKEN"); |
2288 | 0 | break; |
2289 | 0 | case XML_ATTRIBUTE_NMTOKENS: |
2290 | 0 | xmlBufferWriteChar(buf, " NMTOKENS"); |
2291 | 0 | break; |
2292 | 0 | case XML_ATTRIBUTE_ENUMERATION: |
2293 | 0 | xmlBufferWriteChar(buf, " ("); |
2294 | 0 | xmlDumpEnumeration(buf, attr->tree); |
2295 | 0 | break; |
2296 | 0 | case XML_ATTRIBUTE_NOTATION: |
2297 | 0 | xmlBufferWriteChar(buf, " NOTATION ("); |
2298 | 0 | xmlDumpEnumeration(buf, attr->tree); |
2299 | 0 | break; |
2300 | 0 | default: |
2301 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
2302 | 0 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
2303 | 0 | NULL); |
2304 | 0 | } |
2305 | 0 | switch (attr->def) { |
2306 | 0 | case XML_ATTRIBUTE_NONE: |
2307 | 0 | break; |
2308 | 0 | case XML_ATTRIBUTE_REQUIRED: |
2309 | 0 | xmlBufferWriteChar(buf, " #REQUIRED"); |
2310 | 0 | break; |
2311 | 0 | case XML_ATTRIBUTE_IMPLIED: |
2312 | 0 | xmlBufferWriteChar(buf, " #IMPLIED"); |
2313 | 0 | break; |
2314 | 0 | case XML_ATTRIBUTE_FIXED: |
2315 | 0 | xmlBufferWriteChar(buf, " #FIXED"); |
2316 | 0 | break; |
2317 | 0 | default: |
2318 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
2319 | 0 | "Internal: ATTRIBUTE struct corrupted invalid def\n", |
2320 | 0 | NULL); |
2321 | 0 | } |
2322 | 0 | if (attr->defaultValue != NULL) { |
2323 | 0 | xmlBufferWriteChar(buf, " "); |
2324 | 0 | xmlBufferWriteQuotedString(buf, attr->defaultValue); |
2325 | 0 | } |
2326 | 0 | xmlBufferWriteChar(buf, ">\n"); |
2327 | 0 | } |
2328 | | |
2329 | | /** |
2330 | | * xmlDumpAttributeDeclScan: |
2331 | | * @attr: An attribute declaration |
2332 | | * @buf: the XML buffer output |
2333 | | * |
2334 | | * This is used with the hash scan function - just reverses arguments |
2335 | | */ |
2336 | | static void |
2337 | | xmlDumpAttributeDeclScan(void *attr, void *buf, |
2338 | 0 | const xmlChar *name ATTRIBUTE_UNUSED) { |
2339 | 0 | xmlDumpAttributeDecl((xmlBufferPtr) buf, (xmlAttributePtr) attr); |
2340 | 0 | } |
2341 | | |
2342 | | /** |
2343 | | * xmlDumpAttributeTable: |
2344 | | * @buf: the XML buffer output |
2345 | | * @table: An attribute table |
2346 | | * |
2347 | | * This will dump the content of the attribute table as an XML DTD definition |
2348 | | */ |
2349 | | void |
2350 | 0 | xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) { |
2351 | 0 | if ((buf == NULL) || (table == NULL)) |
2352 | 0 | return; |
2353 | 0 | xmlHashScan(table, xmlDumpAttributeDeclScan, buf); |
2354 | 0 | } |
2355 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
2356 | | |
2357 | | /************************************************************************ |
2358 | | * * |
2359 | | * NOTATIONs * |
2360 | | * * |
2361 | | ************************************************************************/ |
2362 | | /** |
2363 | | * xmlFreeNotation: |
2364 | | * @not: A notation |
2365 | | * |
2366 | | * Deallocate the memory used by an notation definition |
2367 | | */ |
2368 | | static void |
2369 | 0 | xmlFreeNotation(xmlNotationPtr nota) { |
2370 | 0 | if (nota == NULL) return; |
2371 | 0 | if (nota->name != NULL) |
2372 | 0 | xmlFree((xmlChar *) nota->name); |
2373 | 0 | if (nota->PublicID != NULL) |
2374 | 0 | xmlFree((xmlChar *) nota->PublicID); |
2375 | 0 | if (nota->SystemID != NULL) |
2376 | 0 | xmlFree((xmlChar *) nota->SystemID); |
2377 | 0 | xmlFree(nota); |
2378 | 0 | } |
2379 | | |
2380 | | |
2381 | | /** |
2382 | | * xmlAddNotationDecl: |
2383 | | * @dtd: pointer to the DTD |
2384 | | * @ctxt: the validation context |
2385 | | * @name: the entity name |
2386 | | * @PublicID: the public identifier or NULL |
2387 | | * @SystemID: the system identifier or NULL |
2388 | | * |
2389 | | * Register a new notation declaration |
2390 | | * |
2391 | | * Returns NULL if not, otherwise the entity |
2392 | | */ |
2393 | | xmlNotationPtr |
2394 | | xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, |
2395 | | const xmlChar *name, |
2396 | 0 | const xmlChar *PublicID, const xmlChar *SystemID) { |
2397 | 0 | xmlNotationPtr ret; |
2398 | 0 | xmlNotationTablePtr table; |
2399 | |
|
2400 | 0 | if (dtd == NULL) { |
2401 | 0 | return(NULL); |
2402 | 0 | } |
2403 | 0 | if (name == NULL) { |
2404 | 0 | return(NULL); |
2405 | 0 | } |
2406 | 0 | if ((PublicID == NULL) && (SystemID == NULL)) { |
2407 | 0 | return(NULL); |
2408 | 0 | } |
2409 | | |
2410 | | /* |
2411 | | * Create the Notation table if needed. |
2412 | | */ |
2413 | 0 | table = (xmlNotationTablePtr) dtd->notations; |
2414 | 0 | if (table == NULL) { |
2415 | 0 | xmlDictPtr dict = NULL; |
2416 | 0 | if (dtd->doc != NULL) |
2417 | 0 | dict = dtd->doc->dict; |
2418 | |
|
2419 | 0 | dtd->notations = table = xmlHashCreateDict(0, dict); |
2420 | 0 | } |
2421 | 0 | if (table == NULL) { |
2422 | 0 | xmlVErrMemory(ctxt, |
2423 | 0 | "xmlAddNotationDecl: Table creation failed!\n"); |
2424 | 0 | return(NULL); |
2425 | 0 | } |
2426 | | |
2427 | 0 | ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
2428 | 0 | if (ret == NULL) { |
2429 | 0 | xmlVErrMemory(ctxt, "malloc failed"); |
2430 | 0 | return(NULL); |
2431 | 0 | } |
2432 | 0 | memset(ret, 0, sizeof(xmlNotation)); |
2433 | | |
2434 | | /* |
2435 | | * fill the structure. |
2436 | | */ |
2437 | 0 | ret->name = xmlStrdup(name); |
2438 | 0 | if (SystemID != NULL) |
2439 | 0 | ret->SystemID = xmlStrdup(SystemID); |
2440 | 0 | if (PublicID != NULL) |
2441 | 0 | ret->PublicID = xmlStrdup(PublicID); |
2442 | | |
2443 | | /* |
2444 | | * Validity Check: |
2445 | | * Check the DTD for previous declarations of the ATTLIST |
2446 | | */ |
2447 | 0 | if (xmlHashAddEntry(table, name, ret)) { |
2448 | 0 | #ifdef LIBXML_VALID_ENABLED |
2449 | 0 | xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED, |
2450 | 0 | "xmlAddNotationDecl: %s already defined\n", |
2451 | 0 | (const char *) name); |
2452 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
2453 | 0 | xmlFreeNotation(ret); |
2454 | 0 | return(NULL); |
2455 | 0 | } |
2456 | 0 | return(ret); |
2457 | 0 | } |
2458 | | |
2459 | | static void |
2460 | 0 | xmlFreeNotationTableEntry(void *nota, const xmlChar *name ATTRIBUTE_UNUSED) { |
2461 | 0 | xmlFreeNotation((xmlNotationPtr) nota); |
2462 | 0 | } |
2463 | | |
2464 | | /** |
2465 | | * xmlFreeNotationTable: |
2466 | | * @table: An notation table |
2467 | | * |
2468 | | * Deallocate the memory used by an entities hash table. |
2469 | | */ |
2470 | | void |
2471 | 0 | xmlFreeNotationTable(xmlNotationTablePtr table) { |
2472 | 0 | xmlHashFree(table, xmlFreeNotationTableEntry); |
2473 | 0 | } |
2474 | | |
2475 | | #ifdef LIBXML_TREE_ENABLED |
2476 | | /** |
2477 | | * xmlCopyNotation: |
2478 | | * @nota: A notation |
2479 | | * |
2480 | | * Build a copy of a notation. |
2481 | | * |
2482 | | * Returns the new xmlNotationPtr or NULL in case of error. |
2483 | | */ |
2484 | | static void * |
2485 | 0 | xmlCopyNotation(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
2486 | 0 | xmlNotationPtr nota = (xmlNotationPtr) payload; |
2487 | 0 | xmlNotationPtr cur; |
2488 | |
|
2489 | 0 | cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
2490 | 0 | if (cur == NULL) { |
2491 | 0 | xmlVErrMemory(NULL, "malloc failed"); |
2492 | 0 | return(NULL); |
2493 | 0 | } |
2494 | 0 | if (nota->name != NULL) |
2495 | 0 | cur->name = xmlStrdup(nota->name); |
2496 | 0 | else |
2497 | 0 | cur->name = NULL; |
2498 | 0 | if (nota->PublicID != NULL) |
2499 | 0 | cur->PublicID = xmlStrdup(nota->PublicID); |
2500 | 0 | else |
2501 | 0 | cur->PublicID = NULL; |
2502 | 0 | if (nota->SystemID != NULL) |
2503 | 0 | cur->SystemID = xmlStrdup(nota->SystemID); |
2504 | 0 | else |
2505 | 0 | cur->SystemID = NULL; |
2506 | 0 | return(cur); |
2507 | 0 | } |
2508 | | |
2509 | | /** |
2510 | | * xmlCopyNotationTable: |
2511 | | * @table: A notation table |
2512 | | * |
2513 | | * Build a copy of a notation table. |
2514 | | * |
2515 | | * Returns the new xmlNotationTablePtr or NULL in case of error. |
2516 | | */ |
2517 | | xmlNotationTablePtr |
2518 | 0 | xmlCopyNotationTable(xmlNotationTablePtr table) { |
2519 | 0 | return((xmlNotationTablePtr) xmlHashCopy(table, xmlCopyNotation)); |
2520 | 0 | } |
2521 | | #endif /* LIBXML_TREE_ENABLED */ |
2522 | | |
2523 | | #ifdef LIBXML_OUTPUT_ENABLED |
2524 | | /** |
2525 | | * xmlDumpNotationDecl: |
2526 | | * @buf: the XML buffer output |
2527 | | * @nota: A notation declaration |
2528 | | * |
2529 | | * This will dump the content the notation declaration as an XML DTD definition |
2530 | | */ |
2531 | | void |
2532 | 0 | xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) { |
2533 | 0 | if ((buf == NULL) || (nota == NULL)) |
2534 | 0 | return; |
2535 | 0 | xmlBufferWriteChar(buf, "<!NOTATION "); |
2536 | 0 | xmlBufferWriteCHAR(buf, nota->name); |
2537 | 0 | if (nota->PublicID != NULL) { |
2538 | 0 | xmlBufferWriteChar(buf, " PUBLIC "); |
2539 | 0 | xmlBufferWriteQuotedString(buf, nota->PublicID); |
2540 | 0 | if (nota->SystemID != NULL) { |
2541 | 0 | xmlBufferWriteChar(buf, " "); |
2542 | 0 | xmlBufferWriteQuotedString(buf, nota->SystemID); |
2543 | 0 | } |
2544 | 0 | } else { |
2545 | 0 | xmlBufferWriteChar(buf, " SYSTEM "); |
2546 | 0 | xmlBufferWriteQuotedString(buf, nota->SystemID); |
2547 | 0 | } |
2548 | 0 | xmlBufferWriteChar(buf, " >\n"); |
2549 | 0 | } |
2550 | | |
2551 | | /** |
2552 | | * xmlDumpNotationDeclScan: |
2553 | | * @nota: A notation declaration |
2554 | | * @buf: the XML buffer output |
2555 | | * |
2556 | | * This is called with the hash scan function, and just reverses args |
2557 | | */ |
2558 | | static void |
2559 | | xmlDumpNotationDeclScan(void *nota, void *buf, |
2560 | 0 | const xmlChar *name ATTRIBUTE_UNUSED) { |
2561 | 0 | xmlDumpNotationDecl((xmlBufferPtr) buf, (xmlNotationPtr) nota); |
2562 | 0 | } |
2563 | | |
2564 | | /** |
2565 | | * xmlDumpNotationTable: |
2566 | | * @buf: the XML buffer output |
2567 | | * @table: A notation table |
2568 | | * |
2569 | | * This will dump the content of the notation table as an XML DTD definition |
2570 | | */ |
2571 | | void |
2572 | 0 | xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) { |
2573 | 0 | if ((buf == NULL) || (table == NULL)) |
2574 | 0 | return; |
2575 | 0 | xmlHashScan(table, xmlDumpNotationDeclScan, buf); |
2576 | 0 | } |
2577 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
2578 | | |
2579 | | /************************************************************************ |
2580 | | * * |
2581 | | * IDs * |
2582 | | * * |
2583 | | ************************************************************************/ |
2584 | | /** |
2585 | | * DICT_FREE: |
2586 | | * @str: a string |
2587 | | * |
2588 | | * Free a string if it is not owned by the "dict" dictionary in the |
2589 | | * current scope |
2590 | | */ |
2591 | | #define DICT_FREE(str) \ |
2592 | 0 | if ((str) && ((!dict) || \ |
2593 | 0 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
2594 | 0 | xmlFree((char *)(str)); |
2595 | | |
2596 | | /** |
2597 | | * xmlValidNormalizeString: |
2598 | | * @str: a string |
2599 | | * |
2600 | | * Normalize a string in-place. |
2601 | | */ |
2602 | | static void |
2603 | 0 | xmlValidNormalizeString(xmlChar *str) { |
2604 | 0 | xmlChar *dst; |
2605 | 0 | const xmlChar *src; |
2606 | |
|
2607 | 0 | if (str == NULL) |
2608 | 0 | return; |
2609 | 0 | src = str; |
2610 | 0 | dst = str; |
2611 | |
|
2612 | 0 | while (*src == 0x20) src++; |
2613 | 0 | while (*src != 0) { |
2614 | 0 | if (*src == 0x20) { |
2615 | 0 | while (*src == 0x20) src++; |
2616 | 0 | if (*src != 0) |
2617 | 0 | *dst++ = 0x20; |
2618 | 0 | } else { |
2619 | 0 | *dst++ = *src++; |
2620 | 0 | } |
2621 | 0 | } |
2622 | 0 | *dst = 0; |
2623 | 0 | } |
2624 | | |
2625 | | static int |
2626 | 0 | xmlIsStreaming(xmlValidCtxtPtr ctxt) { |
2627 | 0 | xmlParserCtxtPtr pctxt; |
2628 | |
|
2629 | 0 | if (ctxt == NULL) |
2630 | 0 | return(0); |
2631 | 0 | if ((ctxt->flags & XML_VCTXT_USE_PCTXT) == 0) |
2632 | 0 | return(0); |
2633 | 0 | pctxt = ctxt->userData; |
2634 | 0 | return(pctxt->parseMode == XML_PARSE_READER); |
2635 | 0 | } |
2636 | | |
2637 | | /** |
2638 | | * xmlFreeID: |
2639 | | * @not: A id |
2640 | | * |
2641 | | * Deallocate the memory used by an id definition |
2642 | | */ |
2643 | | static void |
2644 | 0 | xmlFreeID(xmlIDPtr id) { |
2645 | 0 | xmlDictPtr dict = NULL; |
2646 | |
|
2647 | 0 | if (id == NULL) return; |
2648 | | |
2649 | 0 | if (id->doc != NULL) |
2650 | 0 | dict = id->doc->dict; |
2651 | |
|
2652 | 0 | if (id->value != NULL) |
2653 | 0 | DICT_FREE(id->value) |
2654 | 0 | if (id->name != NULL) |
2655 | 0 | DICT_FREE(id->name) |
2656 | 0 | xmlFree(id); |
2657 | 0 | } |
2658 | | |
2659 | | |
2660 | | /** |
2661 | | * xmlAddID: |
2662 | | * @ctxt: the validation context |
2663 | | * @doc: pointer to the document |
2664 | | * @value: the value name |
2665 | | * @attr: the attribute holding the ID |
2666 | | * |
2667 | | * Register a new id declaration |
2668 | | * |
2669 | | * Returns NULL if not, otherwise the new xmlIDPtr |
2670 | | */ |
2671 | | xmlIDPtr |
2672 | | xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
2673 | 0 | xmlAttrPtr attr) { |
2674 | 0 | xmlIDPtr ret; |
2675 | 0 | xmlIDTablePtr table; |
2676 | |
|
2677 | 0 | if (doc == NULL) { |
2678 | 0 | return(NULL); |
2679 | 0 | } |
2680 | 0 | if ((value == NULL) || (value[0] == 0)) { |
2681 | 0 | return(NULL); |
2682 | 0 | } |
2683 | 0 | if (attr == NULL) { |
2684 | 0 | return(NULL); |
2685 | 0 | } |
2686 | | |
2687 | | /* |
2688 | | * Create the ID table if needed. |
2689 | | */ |
2690 | 0 | table = (xmlIDTablePtr) doc->ids; |
2691 | 0 | if (table == NULL) { |
2692 | 0 | doc->ids = table = xmlHashCreateDict(0, doc->dict); |
2693 | 0 | } |
2694 | 0 | if (table == NULL) { |
2695 | 0 | xmlVErrMemory(ctxt, |
2696 | 0 | "xmlAddID: Table creation failed!\n"); |
2697 | 0 | return(NULL); |
2698 | 0 | } |
2699 | | |
2700 | 0 | ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
2701 | 0 | if (ret == NULL) { |
2702 | 0 | xmlVErrMemory(ctxt, "malloc failed"); |
2703 | 0 | return(NULL); |
2704 | 0 | } |
2705 | | |
2706 | | /* |
2707 | | * fill the structure. |
2708 | | */ |
2709 | 0 | ret->value = xmlStrdup(value); |
2710 | 0 | ret->doc = doc; |
2711 | 0 | if (xmlIsStreaming(ctxt)) { |
2712 | | /* |
2713 | | * Operating in streaming mode, attr is gonna disappear |
2714 | | */ |
2715 | 0 | if (doc->dict != NULL) |
2716 | 0 | ret->name = xmlDictLookup(doc->dict, attr->name, -1); |
2717 | 0 | else |
2718 | 0 | ret->name = xmlStrdup(attr->name); |
2719 | 0 | ret->attr = NULL; |
2720 | 0 | } else { |
2721 | 0 | ret->attr = attr; |
2722 | 0 | ret->name = NULL; |
2723 | 0 | } |
2724 | 0 | ret->lineno = xmlGetLineNo(attr->parent); |
2725 | |
|
2726 | 0 | if (xmlHashAddEntry(table, value, ret) < 0) { |
2727 | 0 | #ifdef LIBXML_VALID_ENABLED |
2728 | | /* |
2729 | | * The id is already defined in this DTD. |
2730 | | */ |
2731 | 0 | if (ctxt != NULL) { |
2732 | 0 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED, |
2733 | 0 | "ID %s already defined\n", value, NULL, NULL); |
2734 | 0 | } |
2735 | 0 | #endif /* LIBXML_VALID_ENABLED */ |
2736 | 0 | xmlFreeID(ret); |
2737 | 0 | return(NULL); |
2738 | 0 | } |
2739 | 0 | if (attr != NULL) |
2740 | 0 | attr->atype = XML_ATTRIBUTE_ID; |
2741 | 0 | return(ret); |
2742 | 0 | } |
2743 | | |
2744 | | static void |
2745 | 0 | xmlFreeIDTableEntry(void *id, const xmlChar *name ATTRIBUTE_UNUSED) { |
2746 | 0 | xmlFreeID((xmlIDPtr) id); |
2747 | 0 | } |
2748 | | |
2749 | | /** |
2750 | | * xmlFreeIDTable: |
2751 | | * @table: An id table |
2752 | | * |
2753 | | * Deallocate the memory used by an ID hash table. |
2754 | | */ |
2755 | | void |
2756 | 0 | xmlFreeIDTable(xmlIDTablePtr table) { |
2757 | 0 | xmlHashFree(table, xmlFreeIDTableEntry); |
2758 | 0 | } |
2759 | | |
2760 | | /** |
2761 | | * xmlIsID: |
2762 | | * @doc: the document |
2763 | | * @elem: the element carrying the attribute |
2764 | | * @attr: the attribute |
2765 | | * |
2766 | | * Determine whether an attribute is of type ID. In case we have DTD(s) |
2767 | | * then this is done if DTD loading has been requested. In the case |
2768 | | * of HTML documents parsed with the HTML parser, then ID detection is |
2769 | | * done systematically. |
2770 | | * |
2771 | | * Returns 0 or 1 depending on the lookup result |
2772 | | */ |
2773 | | int |
2774 | 0 | xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
2775 | 0 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
2776 | 0 | if ((attr->ns != NULL) && (attr->ns->prefix != NULL) && |
2777 | 0 | (!strcmp((char *) attr->name, "id")) && |
2778 | 0 | (!strcmp((char *) attr->ns->prefix, "xml"))) |
2779 | 0 | return(1); |
2780 | 0 | if (doc == NULL) return(0); |
2781 | 0 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL) && |
2782 | 0 | (doc->type != XML_HTML_DOCUMENT_NODE)) { |
2783 | 0 | return(0); |
2784 | 0 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
2785 | 0 | if ((xmlStrEqual(BAD_CAST "id", attr->name)) || |
2786 | 0 | ((xmlStrEqual(BAD_CAST "name", attr->name)) && |
2787 | 0 | ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a"))))) |
2788 | 0 | return(1); |
2789 | 0 | return(0); |
2790 | 0 | } else if (elem == NULL) { |
2791 | 0 | return(0); |
2792 | 0 | } else { |
2793 | 0 | xmlAttributePtr attrDecl = NULL; |
2794 | |
|
2795 | 0 | xmlChar felem[50], fattr[50]; |
2796 | 0 | xmlChar *fullelemname, *fullattrname; |
2797 | |
|
2798 | 0 | fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ? |
2799 | 0 | xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) : |
2800 | 0 | (xmlChar *)elem->name; |
2801 | |
|
2802 | 0 | fullattrname = (attr->ns != NULL && attr->ns->prefix != NULL) ? |
2803 | 0 | xmlBuildQName(attr->name, attr->ns->prefix, fattr, 50) : |
2804 | 0 | (xmlChar *)attr->name; |
2805 | |
|
2806 | 0 | if (fullelemname != NULL && fullattrname != NULL) { |
2807 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullelemname, |
2808 | 0 | fullattrname); |
2809 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
2810 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullelemname, |
2811 | 0 | fullattrname); |
2812 | 0 | } |
2813 | |
|
2814 | 0 | if ((fullattrname != fattr) && (fullattrname != attr->name)) |
2815 | 0 | xmlFree(fullattrname); |
2816 | 0 | if ((fullelemname != felem) && (fullelemname != elem->name)) |
2817 | 0 | xmlFree(fullelemname); |
2818 | |
|
2819 | 0 | if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID)) |
2820 | 0 | return(1); |
2821 | 0 | } |
2822 | 0 | return(0); |
2823 | 0 | } |
2824 | | |
2825 | | /** |
2826 | | * xmlRemoveID: |
2827 | | * @doc: the document |
2828 | | * @attr: the attribute |
2829 | | * |
2830 | | * Remove the given attribute from the ID table maintained internally. |
2831 | | * |
2832 | | * Returns -1 if the lookup failed and 0 otherwise |
2833 | | */ |
2834 | | int |
2835 | 0 | xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) { |
2836 | 0 | xmlIDTablePtr table; |
2837 | 0 | xmlIDPtr id; |
2838 | 0 | xmlChar *ID; |
2839 | |
|
2840 | 0 | if (doc == NULL) return(-1); |
2841 | 0 | if (attr == NULL) return(-1); |
2842 | | |
2843 | 0 | table = (xmlIDTablePtr) doc->ids; |
2844 | 0 | if (table == NULL) |
2845 | 0 | return(-1); |
2846 | | |
2847 | 0 | ID = xmlNodeListGetString(doc, attr->children, 1); |
2848 | 0 | if (ID == NULL) |
2849 | 0 | return(-1); |
2850 | 0 | xmlValidNormalizeString(ID); |
2851 | |
|
2852 | 0 | id = xmlHashLookup(table, ID); |
2853 | 0 | if (id == NULL || id->attr != attr) { |
2854 | 0 | xmlFree(ID); |
2855 | 0 | return(-1); |
2856 | 0 | } |
2857 | | |
2858 | 0 | xmlHashRemoveEntry(table, ID, xmlFreeIDTableEntry); |
2859 | 0 | xmlFree(ID); |
2860 | 0 | attr->atype = 0; |
2861 | 0 | return(0); |
2862 | 0 | } |
2863 | | |
2864 | | /** |
2865 | | * xmlGetID: |
2866 | | * @doc: pointer to the document |
2867 | | * @ID: the ID value |
2868 | | * |
2869 | | * Search the attribute declaring the given ID |
2870 | | * |
2871 | | * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID |
2872 | | */ |
2873 | | xmlAttrPtr |
2874 | 0 | xmlGetID(xmlDocPtr doc, const xmlChar *ID) { |
2875 | 0 | xmlIDTablePtr table; |
2876 | 0 | xmlIDPtr id; |
2877 | |
|
2878 | 0 | if (doc == NULL) { |
2879 | 0 | return(NULL); |
2880 | 0 | } |
2881 | | |
2882 | 0 | if (ID == NULL) { |
2883 | 0 | return(NULL); |
2884 | 0 | } |
2885 | | |
2886 | 0 | table = (xmlIDTablePtr) doc->ids; |
2887 | 0 | if (table == NULL) |
2888 | 0 | return(NULL); |
2889 | | |
2890 | 0 | id = xmlHashLookup(table, ID); |
2891 | 0 | if (id == NULL) |
2892 | 0 | return(NULL); |
2893 | 0 | if (id->attr == NULL) { |
2894 | | /* |
2895 | | * We are operating on a stream, return a well known reference |
2896 | | * since the attribute node doesn't exist anymore |
2897 | | */ |
2898 | 0 | return((xmlAttrPtr) doc); |
2899 | 0 | } |
2900 | 0 | return(id->attr); |
2901 | 0 | } |
2902 | | |
2903 | | /************************************************************************ |
2904 | | * * |
2905 | | * Refs * |
2906 | | * * |
2907 | | ************************************************************************/ |
2908 | | typedef struct xmlRemoveMemo_t |
2909 | | { |
2910 | | xmlListPtr l; |
2911 | | xmlAttrPtr ap; |
2912 | | } xmlRemoveMemo; |
2913 | | |
2914 | | typedef xmlRemoveMemo *xmlRemoveMemoPtr; |
2915 | | |
2916 | | typedef struct xmlValidateMemo_t |
2917 | | { |
2918 | | xmlValidCtxtPtr ctxt; |
2919 | | const xmlChar *name; |
2920 | | } xmlValidateMemo; |
2921 | | |
2922 | | typedef xmlValidateMemo *xmlValidateMemoPtr; |
2923 | | |
2924 | | /** |
2925 | | * xmlFreeRef: |
2926 | | * @lk: A list link |
2927 | | * |
2928 | | * Deallocate the memory used by a ref definition |
2929 | | */ |
2930 | | static void |
2931 | 0 | xmlFreeRef(xmlLinkPtr lk) { |
2932 | 0 | xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk); |
2933 | 0 | if (ref == NULL) return; |
2934 | 0 | if (ref->value != NULL) |
2935 | 0 | xmlFree((xmlChar *)ref->value); |
2936 | 0 | if (ref->name != NULL) |
2937 | 0 | xmlFree((xmlChar *)ref->name); |
2938 | 0 | xmlFree(ref); |
2939 | 0 | } |
2940 | | |
2941 | | /** |
2942 | | * xmlFreeRefTableEntry: |
2943 | | * @list_ref: A list of references. |
2944 | | * |
2945 | | * Deallocate the memory used by a list of references |
2946 | | */ |
2947 | | static void |
2948 | 0 | xmlFreeRefTableEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
2949 | 0 | xmlListPtr list_ref = (xmlListPtr) payload; |
2950 | 0 | if (list_ref == NULL) return; |
2951 | 0 | xmlListDelete(list_ref); |
2952 | 0 | } |
2953 | | |
2954 | | /** |
2955 | | * xmlWalkRemoveRef: |
2956 | | * @data: Contents of current link |
2957 | | * @user: Value supplied by the user |
2958 | | * |
2959 | | * Returns 0 to abort the walk or 1 to continue |
2960 | | */ |
2961 | | static int |
2962 | | xmlWalkRemoveRef(const void *data, void *user) |
2963 | 0 | { |
2964 | 0 | xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr; |
2965 | 0 | xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap; |
2966 | 0 | xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l; |
2967 | |
|
2968 | 0 | if (attr0 == attr1) { /* Matched: remove and terminate walk */ |
2969 | 0 | xmlListRemoveFirst(ref_list, (void *)data); |
2970 | 0 | return 0; |
2971 | 0 | } |
2972 | 0 | return 1; |
2973 | 0 | } |
2974 | | |
2975 | | /** |
2976 | | * xmlDummyCompare |
2977 | | * @data0: Value supplied by the user |
2978 | | * @data1: Value supplied by the user |
2979 | | * |
2980 | | * Do nothing, return 0. Used to create unordered lists. |
2981 | | */ |
2982 | | static int |
2983 | | xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED, |
2984 | | const void *data1 ATTRIBUTE_UNUSED) |
2985 | 0 | { |
2986 | 0 | return (0); |
2987 | 0 | } |
2988 | | |
2989 | | /** |
2990 | | * xmlAddRef: |
2991 | | * @ctxt: the validation context |
2992 | | * @doc: pointer to the document |
2993 | | * @value: the value name |
2994 | | * @attr: the attribute holding the Ref |
2995 | | * |
2996 | | * DEPRECATED, do not use. This function will be removed from the public API. |
2997 | | * |
2998 | | * Register a new ref declaration |
2999 | | * |
3000 | | * Returns NULL if not, otherwise the new xmlRefPtr |
3001 | | */ |
3002 | | xmlRefPtr |
3003 | | xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
3004 | 0 | xmlAttrPtr attr) { |
3005 | 0 | xmlRefPtr ret; |
3006 | 0 | xmlRefTablePtr table; |
3007 | 0 | xmlListPtr ref_list; |
3008 | |
|
3009 | 0 | if (doc == NULL) { |
3010 | 0 | return(NULL); |
3011 | 0 | } |
3012 | 0 | if (value == NULL) { |
3013 | 0 | return(NULL); |
3014 | 0 | } |
3015 | 0 | if (attr == NULL) { |
3016 | 0 | return(NULL); |
3017 | 0 | } |
3018 | | |
3019 | | /* |
3020 | | * Create the Ref table if needed. |
3021 | | */ |
3022 | 0 | table = (xmlRefTablePtr) doc->refs; |
3023 | 0 | if (table == NULL) { |
3024 | 0 | doc->refs = table = xmlHashCreateDict(0, doc->dict); |
3025 | 0 | } |
3026 | 0 | if (table == NULL) { |
3027 | 0 | xmlVErrMemory(ctxt, |
3028 | 0 | "xmlAddRef: Table creation failed!\n"); |
3029 | 0 | return(NULL); |
3030 | 0 | } |
3031 | | |
3032 | 0 | ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef)); |
3033 | 0 | if (ret == NULL) { |
3034 | 0 | xmlVErrMemory(ctxt, "malloc failed"); |
3035 | 0 | return(NULL); |
3036 | 0 | } |
3037 | | |
3038 | | /* |
3039 | | * fill the structure. |
3040 | | */ |
3041 | 0 | ret->value = xmlStrdup(value); |
3042 | 0 | if (xmlIsStreaming(ctxt)) { |
3043 | | /* |
3044 | | * Operating in streaming mode, attr is gonna disappear |
3045 | | */ |
3046 | 0 | ret->name = xmlStrdup(attr->name); |
3047 | 0 | ret->attr = NULL; |
3048 | 0 | } else { |
3049 | 0 | ret->name = NULL; |
3050 | 0 | ret->attr = attr; |
3051 | 0 | } |
3052 | 0 | ret->lineno = xmlGetLineNo(attr->parent); |
3053 | | |
3054 | | /* To add a reference :- |
3055 | | * References are maintained as a list of references, |
3056 | | * Lookup the entry, if no entry create new nodelist |
3057 | | * Add the owning node to the NodeList |
3058 | | * Return the ref |
3059 | | */ |
3060 | |
|
3061 | 0 | if (NULL == (ref_list = xmlHashLookup(table, value))) { |
3062 | 0 | if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) { |
3063 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
3064 | 0 | "xmlAddRef: Reference list creation failed!\n", |
3065 | 0 | NULL); |
3066 | 0 | goto failed; |
3067 | 0 | } |
3068 | 0 | if (xmlHashAddEntry(table, value, ref_list) < 0) { |
3069 | 0 | xmlListDelete(ref_list); |
3070 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
3071 | 0 | "xmlAddRef: Reference list insertion failed!\n", |
3072 | 0 | NULL); |
3073 | 0 | goto failed; |
3074 | 0 | } |
3075 | 0 | } |
3076 | 0 | if (xmlListAppend(ref_list, ret) != 0) { |
3077 | 0 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
3078 | 0 | "xmlAddRef: Reference list insertion failed!\n", |
3079 | 0 | NULL); |
3080 | 0 | goto failed; |
3081 | 0 | } |
3082 | 0 | return(ret); |
3083 | 0 | failed: |
3084 | 0 | if (ret != NULL) { |
3085 | 0 | if (ret->value != NULL) |
3086 | 0 | xmlFree((char *)ret->value); |
3087 | 0 | if (ret->name != NULL) |
3088 | 0 | xmlFree((char *)ret->name); |
3089 | 0 | xmlFree(ret); |
3090 | 0 | } |
3091 | 0 | return(NULL); |
3092 | 0 | } |
3093 | | |
3094 | | /** |
3095 | | * xmlFreeRefTable: |
3096 | | * @table: An ref table |
3097 | | * |
3098 | | * DEPRECATED, do not use. This function will be removed from the public API. |
3099 | | * |
3100 | | * Deallocate the memory used by an Ref hash table. |
3101 | | */ |
3102 | | void |
3103 | 0 | xmlFreeRefTable(xmlRefTablePtr table) { |
3104 | 0 | xmlHashFree(table, xmlFreeRefTableEntry); |
3105 | 0 | } |
3106 | | |
3107 | | /** |
3108 | | * xmlIsRef: |
3109 | | * @doc: the document |
3110 | | * @elem: the element carrying the attribute |
3111 | | * @attr: the attribute |
3112 | | * |
3113 | | * DEPRECATED, do not use. This function will be removed from the public API. |
3114 | | * |
3115 | | * Determine whether an attribute is of type Ref. In case we have DTD(s) |
3116 | | * then this is simple, otherwise we use an heuristic: name Ref (upper |
3117 | | * or lowercase). |
3118 | | * |
3119 | | * Returns 0 or 1 depending on the lookup result |
3120 | | */ |
3121 | | int |
3122 | 0 | xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
3123 | 0 | if (attr == NULL) |
3124 | 0 | return(0); |
3125 | 0 | if (doc == NULL) { |
3126 | 0 | doc = attr->doc; |
3127 | 0 | if (doc == NULL) return(0); |
3128 | 0 | } |
3129 | | |
3130 | 0 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
3131 | 0 | return(0); |
3132 | 0 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
3133 | | /* TODO @@@ */ |
3134 | 0 | return(0); |
3135 | 0 | } else { |
3136 | 0 | xmlAttributePtr attrDecl; |
3137 | |
|
3138 | 0 | if (elem == NULL) return(0); |
3139 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
3140 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
3141 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
3142 | 0 | elem->name, attr->name); |
3143 | |
|
3144 | 0 | if ((attrDecl != NULL) && |
3145 | 0 | (attrDecl->atype == XML_ATTRIBUTE_IDREF || |
3146 | 0 | attrDecl->atype == XML_ATTRIBUTE_IDREFS)) |
3147 | 0 | return(1); |
3148 | 0 | } |
3149 | 0 | return(0); |
3150 | 0 | } |
3151 | | |
3152 | | /** |
3153 | | * xmlRemoveRef: |
3154 | | * @doc: the document |
3155 | | * @attr: the attribute |
3156 | | * |
3157 | | * DEPRECATED, do not use. This function will be removed from the public API. |
3158 | | * |
3159 | | * Remove the given attribute from the Ref table maintained internally. |
3160 | | * |
3161 | | * Returns -1 if the lookup failed and 0 otherwise |
3162 | | */ |
3163 | | int |
3164 | 0 | xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) { |
3165 | 0 | xmlListPtr ref_list; |
3166 | 0 | xmlRefTablePtr table; |
3167 | 0 | xmlChar *ID; |
3168 | 0 | xmlRemoveMemo target; |
3169 | |
|
3170 | 0 | if (doc == NULL) return(-1); |
3171 | 0 | if (attr == NULL) return(-1); |
3172 | | |
3173 | 0 | table = (xmlRefTablePtr) doc->refs; |
3174 | 0 | if (table == NULL) |
3175 | 0 | return(-1); |
3176 | | |
3177 | 0 | ID = xmlNodeListGetString(doc, attr->children, 1); |
3178 | 0 | if (ID == NULL) |
3179 | 0 | return(-1); |
3180 | | |
3181 | 0 | ref_list = xmlHashLookup(table, ID); |
3182 | 0 | if(ref_list == NULL) { |
3183 | 0 | xmlFree(ID); |
3184 | 0 | return (-1); |
3185 | 0 | } |
3186 | | |
3187 | | /* At this point, ref_list refers to a list of references which |
3188 | | * have the same key as the supplied attr. Our list of references |
3189 | | * is ordered by reference address and we don't have that information |
3190 | | * here to use when removing. We'll have to walk the list and |
3191 | | * check for a matching attribute, when we find one stop the walk |
3192 | | * and remove the entry. |
3193 | | * The list is ordered by reference, so that means we don't have the |
3194 | | * key. Passing the list and the reference to the walker means we |
3195 | | * will have enough data to be able to remove the entry. |
3196 | | */ |
3197 | 0 | target.l = ref_list; |
3198 | 0 | target.ap = attr; |
3199 | | |
3200 | | /* Remove the supplied attr from our list */ |
3201 | 0 | xmlListWalk(ref_list, xmlWalkRemoveRef, &target); |
3202 | | |
3203 | | /*If the list is empty then remove the list entry in the hash */ |
3204 | 0 | if (xmlListEmpty(ref_list)) |
3205 | 0 | xmlHashUpdateEntry(table, ID, NULL, xmlFreeRefTableEntry); |
3206 | 0 | xmlFree(ID); |
3207 | 0 | return(0); |
3208 | 0 | } |
3209 | | |
3210 | | /** |
3211 | | * xmlGetRefs: |
3212 | | * @doc: pointer to the document |
3213 | | * @ID: the ID value |
3214 | | * |
3215 | | * DEPRECATED, do not use. This function will be removed from the public API. |
3216 | | * |
3217 | | * Find the set of references for the supplied ID. |
3218 | | * |
3219 | | * Returns NULL if not found, otherwise node set for the ID. |
3220 | | */ |
3221 | | xmlListPtr |
3222 | 0 | xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) { |
3223 | 0 | xmlRefTablePtr table; |
3224 | |
|
3225 | 0 | if (doc == NULL) { |
3226 | 0 | return(NULL); |
3227 | 0 | } |
3228 | | |
3229 | 0 | if (ID == NULL) { |
3230 | 0 | return(NULL); |
3231 | 0 | } |
3232 | | |
3233 | 0 | table = (xmlRefTablePtr) doc->refs; |
3234 | 0 | if (table == NULL) |
3235 | 0 | return(NULL); |
3236 | | |
3237 | 0 | return (xmlHashLookup(table, ID)); |
3238 | 0 | } |
3239 | | |
3240 | | /************************************************************************ |
3241 | | * * |
3242 | | * Routines for validity checking * |
3243 | | * * |
3244 | | ************************************************************************/ |
3245 | | |
3246 | | /** |
3247 | | * xmlGetDtdElementDesc: |
3248 | | * @dtd: a pointer to the DtD to search |
3249 | | * @name: the element name |
3250 | | * |
3251 | | * Search the DTD for the description of this element |
3252 | | * |
3253 | | * returns the xmlElementPtr if found or NULL |
3254 | | */ |
3255 | | |
3256 | | xmlElementPtr |
3257 | 0 | xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) { |
3258 | 0 | xmlElementTablePtr table; |
3259 | 0 | xmlElementPtr cur; |
3260 | 0 | xmlChar *uqname = NULL, *prefix = NULL; |
3261 | |
|
3262 | 0 | if ((dtd == NULL) || (name == NULL)) return(NULL); |
3263 | 0 | if (dtd->elements == NULL) |
3264 | 0 | return(NULL); |
3265 | 0 | table = (xmlElementTablePtr) dtd->elements; |
3266 | |
|
3267 | 0 | uqname = xmlSplitQName2(name, &prefix); |
3268 | 0 | if (uqname != NULL) |
3269 | 0 | name = uqname; |
3270 | 0 | cur = xmlHashLookup2(table, name, prefix); |
3271 | 0 | if (prefix != NULL) xmlFree(prefix); |
3272 | 0 | if (uqname != NULL) xmlFree(uqname); |
3273 | 0 | return(cur); |
3274 | 0 | } |
3275 | | /** |
3276 | | * xmlGetDtdElementDesc2: |
3277 | | * @dtd: a pointer to the DtD to search |
3278 | | * @name: the element name |
3279 | | * @create: create an empty description if not found |
3280 | | * |
3281 | | * Search the DTD for the description of this element |
3282 | | * |
3283 | | * returns the xmlElementPtr if found or NULL |
3284 | | */ |
3285 | | |
3286 | | static xmlElementPtr |
3287 | 0 | xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) { |
3288 | 0 | xmlElementTablePtr table; |
3289 | 0 | xmlElementPtr cur; |
3290 | 0 | xmlChar *uqname = NULL, *prefix = NULL; |
3291 | |
|
3292 | 0 | if (dtd == NULL) return(NULL); |
3293 | 0 | if (dtd->elements == NULL) { |
3294 | 0 | xmlDictPtr dict = NULL; |
3295 | |
|
3296 | 0 | if (dtd->doc != NULL) |
3297 | 0 | dict = dtd->doc->dict; |
3298 | |
|
3299 | 0 | if (!create) |
3300 | 0 | return(NULL); |
3301 | | /* |
3302 | | * Create the Element table if needed. |
3303 | | */ |
3304 | 0 | table = (xmlElementTablePtr) dtd->elements; |
3305 | 0 | if (table == NULL) { |
3306 | 0 | table = xmlHashCreateDict(0, dict); |
3307 | 0 | dtd->elements = (void *) table; |
3308 | 0 | } |
3309 | 0 | if (table == NULL) { |
3310 | 0 | xmlVErrMemory(NULL, "element table allocation failed"); |
3311 | 0 | return(NULL); |
3312 | 0 | } |
3313 | 0 | } |
3314 | 0 | table = (xmlElementTablePtr) dtd->elements; |
3315 | |
|
3316 | 0 | uqname = xmlSplitQName2(name, &prefix); |
3317 | 0 | if (uqname != NULL) |
3318 | 0 | name = uqname; |
3319 | 0 | cur = xmlHashLookup2(table, name, prefix); |
3320 | 0 | if ((cur == NULL) && (create)) { |
3321 | 0 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
3322 | 0 | if (cur == NULL) { |
3323 | 0 | xmlVErrMemory(NULL, "malloc failed"); |
3324 | 0 | return(NULL); |
3325 | 0 | } |
3326 | 0 | memset(cur, 0, sizeof(xmlElement)); |
3327 | 0 | cur->type = XML_ELEMENT_DECL; |
3328 | | |
3329 | | /* |
3330 | | * fill the structure. |
3331 | | */ |
3332 | 0 | cur->name = xmlStrdup(name); |
3333 | 0 | cur->prefix = xmlStrdup(prefix); |
3334 | 0 | cur->etype = XML_ELEMENT_TYPE_UNDEFINED; |
3335 | |
|
3336 | 0 | xmlHashAddEntry2(table, name, prefix, cur); |
3337 | 0 | } |
3338 | 0 | if (prefix != NULL) xmlFree(prefix); |
3339 | 0 | if (uqname != NULL) xmlFree(uqname); |
3340 | 0 | return(cur); |
3341 | 0 | } |
3342 | | |
3343 | | /** |
3344 | | * xmlGetDtdQElementDesc: |
3345 | | * @dtd: a pointer to the DtD to search |
3346 | | * @name: the element name |
3347 | | * @prefix: the element namespace prefix |
3348 | | * |
3349 | | * Search the DTD for the description of this element |
3350 | | * |
3351 | | * returns the xmlElementPtr if found or NULL |
3352 | | */ |
3353 | | |
3354 | | xmlElementPtr |
3355 | | xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name, |
3356 | 0 | const xmlChar *prefix) { |
3357 | 0 | xmlElementTablePtr table; |
3358 | |
|
3359 | 0 | if (dtd == NULL) return(NULL); |
3360 | 0 | if (dtd->elements == NULL) return(NULL); |
3361 | 0 | table = (xmlElementTablePtr) dtd->elements; |
3362 | |
|
3363 | 0 | return(xmlHashLookup2(table, name, prefix)); |
3364 | 0 | } |
3365 | | |
3366 | | /** |
3367 | | * xmlGetDtdAttrDesc: |
3368 | | * @dtd: a pointer to the DtD to search |
3369 | | * @elem: the element name |
3370 | | * @name: the attribute name |
3371 | | * |
3372 | | * Search the DTD for the description of this attribute on |
3373 | | * this element. |
3374 | | * |
3375 | | * returns the xmlAttributePtr if found or NULL |
3376 | | */ |
3377 | | |
3378 | | xmlAttributePtr |
3379 | 0 | xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) { |
3380 | 0 | xmlAttributeTablePtr table; |
3381 | 0 | xmlAttributePtr cur; |
3382 | 0 | xmlChar *uqname = NULL, *prefix = NULL; |
3383 | |
|
3384 | 0 | if (dtd == NULL) return(NULL); |
3385 | 0 | if (dtd->attributes == NULL) return(NULL); |
3386 | | |
3387 | 0 | table = (xmlAttributeTablePtr) dtd->attributes; |
3388 | 0 | if (table == NULL) |
3389 | 0 | return(NULL); |
3390 | | |
3391 | 0 | uqname = xmlSplitQName2(name, &prefix); |
3392 | |
|
3393 | 0 | if (uqname != NULL) { |
3394 | 0 | cur = xmlHashLookup3(table, uqname, prefix, elem); |
3395 | 0 | if (prefix != NULL) xmlFree(prefix); |
3396 | 0 | if (uqname != NULL) xmlFree(uqname); |
3397 | 0 | } else |
3398 | 0 | cur = xmlHashLookup3(table, name, NULL, elem); |
3399 | 0 | return(cur); |
3400 | 0 | } |
3401 | | |
3402 | | /** |
3403 | | * xmlGetDtdQAttrDesc: |
3404 | | * @dtd: a pointer to the DtD to search |
3405 | | * @elem: the element name |
3406 | | * @name: the attribute name |
3407 | | * @prefix: the attribute namespace prefix |
3408 | | * |
3409 | | * Search the DTD for the description of this qualified attribute on |
3410 | | * this element. |
3411 | | * |
3412 | | * returns the xmlAttributePtr if found or NULL |
3413 | | */ |
3414 | | |
3415 | | xmlAttributePtr |
3416 | | xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name, |
3417 | 0 | const xmlChar *prefix) { |
3418 | 0 | xmlAttributeTablePtr table; |
3419 | |
|
3420 | 0 | if (dtd == NULL) return(NULL); |
3421 | 0 | if (dtd->attributes == NULL) return(NULL); |
3422 | 0 | table = (xmlAttributeTablePtr) dtd->attributes; |
3423 | |
|
3424 | 0 | return(xmlHashLookup3(table, name, prefix, elem)); |
3425 | 0 | } |
3426 | | |
3427 | | /** |
3428 | | * xmlGetDtdNotationDesc: |
3429 | | * @dtd: a pointer to the DtD to search |
3430 | | * @name: the notation name |
3431 | | * |
3432 | | * Search the DTD for the description of this notation |
3433 | | * |
3434 | | * returns the xmlNotationPtr if found or NULL |
3435 | | */ |
3436 | | |
3437 | | xmlNotationPtr |
3438 | 0 | xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) { |
3439 | 0 | xmlNotationTablePtr table; |
3440 | |
|
3441 | 0 | if (dtd == NULL) return(NULL); |
3442 | 0 | if (dtd->notations == NULL) return(NULL); |
3443 | 0 | table = (xmlNotationTablePtr) dtd->notations; |
3444 | |
|
3445 | 0 | return(xmlHashLookup(table, name)); |
3446 | 0 | } |
3447 | | |
3448 | | #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
3449 | | /** |
3450 | | * xmlValidateNotationUse: |
3451 | | * @ctxt: the validation context |
3452 | | * @doc: the document |
3453 | | * @notationName: the notation name to check |
3454 | | * |
3455 | | * Validate that the given name match a notation declaration. |
3456 | | * - [ VC: Notation Declared ] |
3457 | | * |
3458 | | * returns 1 if valid or 0 otherwise |
3459 | | */ |
3460 | | |
3461 | | int |
3462 | | xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
3463 | 0 | const xmlChar *notationName) { |
3464 | 0 | xmlNotationPtr notaDecl; |
3465 | 0 | if ((doc == NULL) || (doc->intSubset == NULL) || |
3466 | 0 | (notationName == NULL)) return(-1); |
3467 | | |
3468 | 0 | notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName); |
3469 | 0 | if ((notaDecl == NULL) && (doc->extSubset != NULL)) |
3470 | 0 | notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName); |
3471 | |
|
3472 | 0 | if ((notaDecl == NULL) && (ctxt != NULL)) { |
3473 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION, |
3474 | 0 | "NOTATION %s is not declared\n", |
3475 | 0 | notationName, NULL, NULL); |
3476 | 0 | return(0); |
3477 | 0 | } |
3478 | 0 | return(1); |
3479 | 0 | } |
3480 | | #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */ |
3481 | | |
3482 | | /** |
3483 | | * xmlIsMixedElement: |
3484 | | * @doc: the document |
3485 | | * @name: the element name |
3486 | | * |
3487 | | * Search in the DtDs whether an element accept Mixed content (or ANY) |
3488 | | * basically if it is supposed to accept text childs |
3489 | | * |
3490 | | * returns 0 if no, 1 if yes, and -1 if no element description is available |
3491 | | */ |
3492 | | |
3493 | | int |
3494 | 0 | xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) { |
3495 | 0 | xmlElementPtr elemDecl; |
3496 | |
|
3497 | 0 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
3498 | | |
3499 | 0 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, name); |
3500 | 0 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
3501 | 0 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, name); |
3502 | 0 | if (elemDecl == NULL) return(-1); |
3503 | 0 | switch (elemDecl->etype) { |
3504 | 0 | case XML_ELEMENT_TYPE_UNDEFINED: |
3505 | 0 | return(-1); |
3506 | 0 | case XML_ELEMENT_TYPE_ELEMENT: |
3507 | 0 | return(0); |
3508 | 0 | case XML_ELEMENT_TYPE_EMPTY: |
3509 | | /* |
3510 | | * return 1 for EMPTY since we want VC error to pop up |
3511 | | * on <empty> </empty> for example |
3512 | | */ |
3513 | 0 | case XML_ELEMENT_TYPE_ANY: |
3514 | 0 | case XML_ELEMENT_TYPE_MIXED: |
3515 | 0 | return(1); |
3516 | 0 | } |
3517 | 0 | return(1); |
3518 | 0 | } |
3519 | | |
3520 | | #ifdef LIBXML_VALID_ENABLED |
3521 | | |
3522 | | static int |
3523 | 0 | xmlIsDocNameStartChar(xmlDocPtr doc, int c) { |
3524 | 0 | if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) { |
3525 | | /* |
3526 | | * Use the new checks of production [4] [4a] amd [5] of the |
3527 | | * Update 5 of XML-1.0 |
3528 | | */ |
3529 | 0 | if (((c >= 'a') && (c <= 'z')) || |
3530 | 0 | ((c >= 'A') && (c <= 'Z')) || |
3531 | 0 | (c == '_') || (c == ':') || |
3532 | 0 | ((c >= 0xC0) && (c <= 0xD6)) || |
3533 | 0 | ((c >= 0xD8) && (c <= 0xF6)) || |
3534 | 0 | ((c >= 0xF8) && (c <= 0x2FF)) || |
3535 | 0 | ((c >= 0x370) && (c <= 0x37D)) || |
3536 | 0 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
3537 | 0 | ((c >= 0x200C) && (c <= 0x200D)) || |
3538 | 0 | ((c >= 0x2070) && (c <= 0x218F)) || |
3539 | 0 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
3540 | 0 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
3541 | 0 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
3542 | 0 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
3543 | 0 | ((c >= 0x10000) && (c <= 0xEFFFF))) |
3544 | 0 | return(1); |
3545 | 0 | } else { |
3546 | 0 | if (IS_LETTER(c) || (c == '_') || (c == ':')) |
3547 | 0 | return(1); |
3548 | 0 | } |
3549 | 0 | return(0); |
3550 | 0 | } |
3551 | | |
3552 | | static int |
3553 | 0 | xmlIsDocNameChar(xmlDocPtr doc, int c) { |
3554 | 0 | if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) { |
3555 | | /* |
3556 | | * Use the new checks of production [4] [4a] amd [5] of the |
3557 | | * Update 5 of XML-1.0 |
3558 | | */ |
3559 | 0 | if (((c >= 'a') && (c <= 'z')) || |
3560 | 0 | ((c >= 'A') && (c <= 'Z')) || |
3561 | 0 | ((c >= '0') && (c <= '9')) || /* !start */ |
3562 | 0 | (c == '_') || (c == ':') || |
3563 | 0 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
3564 | 0 | ((c >= 0xC0) && (c <= 0xD6)) || |
3565 | 0 | ((c >= 0xD8) && (c <= 0xF6)) || |
3566 | 0 | ((c >= 0xF8) && (c <= 0x2FF)) || |
3567 | 0 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
3568 | 0 | ((c >= 0x370) && (c <= 0x37D)) || |
3569 | 0 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
3570 | 0 | ((c >= 0x200C) && (c <= 0x200D)) || |
3571 | 0 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
3572 | 0 | ((c >= 0x2070) && (c <= 0x218F)) || |
3573 | 0 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
3574 | 0 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
3575 | 0 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
3576 | 0 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
3577 | 0 | ((c >= 0x10000) && (c <= 0xEFFFF))) |
3578 | 0 | return(1); |
3579 | 0 | } else { |
3580 | 0 | if ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
3581 | 0 | (c == '.') || (c == '-') || |
3582 | 0 | (c == '_') || (c == ':') || |
3583 | 0 | (IS_COMBINING(c)) || |
3584 | 0 | (IS_EXTENDER(c))) |
3585 | 0 | return(1); |
3586 | 0 | } |
3587 | 0 | return(0); |
3588 | 0 | } |
3589 | | |
3590 | | /** |
3591 | | * xmlValidateNameValue: |
3592 | | * @doc: pointer to the document or NULL |
3593 | | * @value: an Name value |
3594 | | * |
3595 | | * Validate that the given value match Name production |
3596 | | * |
3597 | | * returns 1 if valid or 0 otherwise |
3598 | | */ |
3599 | | |
3600 | | static int |
3601 | 0 | xmlValidateNameValueInternal(xmlDocPtr doc, const xmlChar *value) { |
3602 | 0 | const xmlChar *cur; |
3603 | 0 | int val, len; |
3604 | |
|
3605 | 0 | if (value == NULL) return(0); |
3606 | 0 | cur = value; |
3607 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3608 | 0 | cur += len; |
3609 | 0 | if (!xmlIsDocNameStartChar(doc, val)) |
3610 | 0 | return(0); |
3611 | | |
3612 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3613 | 0 | cur += len; |
3614 | 0 | while (xmlIsDocNameChar(doc, val)) { |
3615 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3616 | 0 | cur += len; |
3617 | 0 | } |
3618 | |
|
3619 | 0 | if (val != 0) return(0); |
3620 | | |
3621 | 0 | return(1); |
3622 | 0 | } |
3623 | | |
3624 | | /** |
3625 | | * xmlValidateNameValue: |
3626 | | * @value: an Name value |
3627 | | * |
3628 | | * Validate that the given value match Name production |
3629 | | * |
3630 | | * returns 1 if valid or 0 otherwise |
3631 | | */ |
3632 | | |
3633 | | int |
3634 | 0 | xmlValidateNameValue(const xmlChar *value) { |
3635 | 0 | return(xmlValidateNameValueInternal(NULL, value)); |
3636 | 0 | } |
3637 | | |
3638 | | /** |
3639 | | * xmlValidateNamesValueInternal: |
3640 | | * @doc: pointer to the document or NULL |
3641 | | * @value: an Names value |
3642 | | * |
3643 | | * Validate that the given value match Names production |
3644 | | * |
3645 | | * returns 1 if valid or 0 otherwise |
3646 | | */ |
3647 | | |
3648 | | static int |
3649 | 0 | xmlValidateNamesValueInternal(xmlDocPtr doc, const xmlChar *value) { |
3650 | 0 | const xmlChar *cur; |
3651 | 0 | int val, len; |
3652 | |
|
3653 | 0 | if (value == NULL) return(0); |
3654 | 0 | cur = value; |
3655 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3656 | 0 | cur += len; |
3657 | |
|
3658 | 0 | if (!xmlIsDocNameStartChar(doc, val)) |
3659 | 0 | return(0); |
3660 | | |
3661 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3662 | 0 | cur += len; |
3663 | 0 | while (xmlIsDocNameChar(doc, val)) { |
3664 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3665 | 0 | cur += len; |
3666 | 0 | } |
3667 | | |
3668 | | /* Should not test IS_BLANK(val) here -- see erratum E20*/ |
3669 | 0 | while (val == 0x20) { |
3670 | 0 | while (val == 0x20) { |
3671 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3672 | 0 | cur += len; |
3673 | 0 | } |
3674 | |
|
3675 | 0 | if (!xmlIsDocNameStartChar(doc, val)) |
3676 | 0 | return(0); |
3677 | | |
3678 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3679 | 0 | cur += len; |
3680 | |
|
3681 | 0 | while (xmlIsDocNameChar(doc, val)) { |
3682 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3683 | 0 | cur += len; |
3684 | 0 | } |
3685 | 0 | } |
3686 | | |
3687 | 0 | if (val != 0) return(0); |
3688 | | |
3689 | 0 | return(1); |
3690 | 0 | } |
3691 | | |
3692 | | /** |
3693 | | * xmlValidateNamesValue: |
3694 | | * @value: an Names value |
3695 | | * |
3696 | | * Validate that the given value match Names production |
3697 | | * |
3698 | | * returns 1 if valid or 0 otherwise |
3699 | | */ |
3700 | | |
3701 | | int |
3702 | 0 | xmlValidateNamesValue(const xmlChar *value) { |
3703 | 0 | return(xmlValidateNamesValueInternal(NULL, value)); |
3704 | 0 | } |
3705 | | |
3706 | | /** |
3707 | | * xmlValidateNmtokenValueInternal: |
3708 | | * @doc: pointer to the document or NULL |
3709 | | * @value: an Nmtoken value |
3710 | | * |
3711 | | * Validate that the given value match Nmtoken production |
3712 | | * |
3713 | | * [ VC: Name Token ] |
3714 | | * |
3715 | | * returns 1 if valid or 0 otherwise |
3716 | | */ |
3717 | | |
3718 | | static int |
3719 | 0 | xmlValidateNmtokenValueInternal(xmlDocPtr doc, const xmlChar *value) { |
3720 | 0 | const xmlChar *cur; |
3721 | 0 | int val, len; |
3722 | |
|
3723 | 0 | if (value == NULL) return(0); |
3724 | 0 | cur = value; |
3725 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3726 | 0 | cur += len; |
3727 | |
|
3728 | 0 | if (!xmlIsDocNameChar(doc, val)) |
3729 | 0 | return(0); |
3730 | | |
3731 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3732 | 0 | cur += len; |
3733 | 0 | while (xmlIsDocNameChar(doc, val)) { |
3734 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3735 | 0 | cur += len; |
3736 | 0 | } |
3737 | |
|
3738 | 0 | if (val != 0) return(0); |
3739 | | |
3740 | 0 | return(1); |
3741 | 0 | } |
3742 | | |
3743 | | /** |
3744 | | * xmlValidateNmtokenValue: |
3745 | | * @value: an Nmtoken value |
3746 | | * |
3747 | | * Validate that the given value match Nmtoken production |
3748 | | * |
3749 | | * [ VC: Name Token ] |
3750 | | * |
3751 | | * returns 1 if valid or 0 otherwise |
3752 | | */ |
3753 | | |
3754 | | int |
3755 | 0 | xmlValidateNmtokenValue(const xmlChar *value) { |
3756 | 0 | return(xmlValidateNmtokenValueInternal(NULL, value)); |
3757 | 0 | } |
3758 | | |
3759 | | /** |
3760 | | * xmlValidateNmtokensValueInternal: |
3761 | | * @doc: pointer to the document or NULL |
3762 | | * @value: an Nmtokens value |
3763 | | * |
3764 | | * Validate that the given value match Nmtokens production |
3765 | | * |
3766 | | * [ VC: Name Token ] |
3767 | | * |
3768 | | * returns 1 if valid or 0 otherwise |
3769 | | */ |
3770 | | |
3771 | | static int |
3772 | 0 | xmlValidateNmtokensValueInternal(xmlDocPtr doc, const xmlChar *value) { |
3773 | 0 | const xmlChar *cur; |
3774 | 0 | int val, len; |
3775 | |
|
3776 | 0 | if (value == NULL) return(0); |
3777 | 0 | cur = value; |
3778 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3779 | 0 | cur += len; |
3780 | |
|
3781 | 0 | while (IS_BLANK(val)) { |
3782 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3783 | 0 | cur += len; |
3784 | 0 | } |
3785 | |
|
3786 | 0 | if (!xmlIsDocNameChar(doc, val)) |
3787 | 0 | return(0); |
3788 | | |
3789 | 0 | while (xmlIsDocNameChar(doc, val)) { |
3790 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3791 | 0 | cur += len; |
3792 | 0 | } |
3793 | | |
3794 | | /* Should not test IS_BLANK(val) here -- see erratum E20*/ |
3795 | 0 | while (val == 0x20) { |
3796 | 0 | while (val == 0x20) { |
3797 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3798 | 0 | cur += len; |
3799 | 0 | } |
3800 | 0 | if (val == 0) return(1); |
3801 | | |
3802 | 0 | if (!xmlIsDocNameChar(doc, val)) |
3803 | 0 | return(0); |
3804 | | |
3805 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3806 | 0 | cur += len; |
3807 | |
|
3808 | 0 | while (xmlIsDocNameChar(doc, val)) { |
3809 | 0 | val = xmlStringCurrentChar(NULL, cur, &len); |
3810 | 0 | cur += len; |
3811 | 0 | } |
3812 | 0 | } |
3813 | | |
3814 | 0 | if (val != 0) return(0); |
3815 | | |
3816 | 0 | return(1); |
3817 | 0 | } |
3818 | | |
3819 | | /** |
3820 | | * xmlValidateNmtokensValue: |
3821 | | * @value: an Nmtokens value |
3822 | | * |
3823 | | * Validate that the given value match Nmtokens production |
3824 | | * |
3825 | | * [ VC: Name Token ] |
3826 | | * |
3827 | | * returns 1 if valid or 0 otherwise |
3828 | | */ |
3829 | | |
3830 | | int |
3831 | 0 | xmlValidateNmtokensValue(const xmlChar *value) { |
3832 | 0 | return(xmlValidateNmtokensValueInternal(NULL, value)); |
3833 | 0 | } |
3834 | | |
3835 | | /** |
3836 | | * xmlValidateNotationDecl: |
3837 | | * @ctxt: the validation context |
3838 | | * @doc: a document instance |
3839 | | * @nota: a notation definition |
3840 | | * |
3841 | | * Try to validate a single notation definition |
3842 | | * basically it does the following checks as described by the |
3843 | | * XML-1.0 recommendation: |
3844 | | * - it seems that no validity constraint exists on notation declarations |
3845 | | * But this function get called anyway ... |
3846 | | * |
3847 | | * returns 1 if valid or 0 otherwise |
3848 | | */ |
3849 | | |
3850 | | int |
3851 | | xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED, |
3852 | 0 | xmlNotationPtr nota ATTRIBUTE_UNUSED) { |
3853 | 0 | int ret = 1; |
3854 | |
|
3855 | 0 | return(ret); |
3856 | 0 | } |
3857 | | |
3858 | | /** |
3859 | | * xmlValidateAttributeValueInternal: |
3860 | | * @doc: the document |
3861 | | * @type: an attribute type |
3862 | | * @value: an attribute value |
3863 | | * |
3864 | | * Validate that the given attribute value match the proper production |
3865 | | * |
3866 | | * returns 1 if valid or 0 otherwise |
3867 | | */ |
3868 | | |
3869 | | static int |
3870 | | xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type, |
3871 | 0 | const xmlChar *value) { |
3872 | 0 | switch (type) { |
3873 | 0 | case XML_ATTRIBUTE_ENTITIES: |
3874 | 0 | case XML_ATTRIBUTE_IDREFS: |
3875 | 0 | return(xmlValidateNamesValueInternal(doc, value)); |
3876 | 0 | case XML_ATTRIBUTE_ENTITY: |
3877 | 0 | case XML_ATTRIBUTE_IDREF: |
3878 | 0 | case XML_ATTRIBUTE_ID: |
3879 | 0 | case XML_ATTRIBUTE_NOTATION: |
3880 | 0 | return(xmlValidateNameValueInternal(doc, value)); |
3881 | 0 | case XML_ATTRIBUTE_NMTOKENS: |
3882 | 0 | case XML_ATTRIBUTE_ENUMERATION: |
3883 | 0 | return(xmlValidateNmtokensValueInternal(doc, value)); |
3884 | 0 | case XML_ATTRIBUTE_NMTOKEN: |
3885 | 0 | return(xmlValidateNmtokenValueInternal(doc, value)); |
3886 | 0 | case XML_ATTRIBUTE_CDATA: |
3887 | 0 | break; |
3888 | 0 | } |
3889 | 0 | return(1); |
3890 | 0 | } |
3891 | | |
3892 | | /** |
3893 | | * xmlValidateAttributeValue: |
3894 | | * @type: an attribute type |
3895 | | * @value: an attribute value |
3896 | | * |
3897 | | * Validate that the given attribute value match the proper production |
3898 | | * |
3899 | | * [ VC: ID ] |
3900 | | * Values of type ID must match the Name production.... |
3901 | | * |
3902 | | * [ VC: IDREF ] |
3903 | | * Values of type IDREF must match the Name production, and values |
3904 | | * of type IDREFS must match Names ... |
3905 | | * |
3906 | | * [ VC: Entity Name ] |
3907 | | * Values of type ENTITY must match the Name production, values |
3908 | | * of type ENTITIES must match Names ... |
3909 | | * |
3910 | | * [ VC: Name Token ] |
3911 | | * Values of type NMTOKEN must match the Nmtoken production; values |
3912 | | * of type NMTOKENS must match Nmtokens. |
3913 | | * |
3914 | | * returns 1 if valid or 0 otherwise |
3915 | | */ |
3916 | | int |
3917 | 0 | xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) { |
3918 | 0 | return(xmlValidateAttributeValueInternal(NULL, type, value)); |
3919 | 0 | } |
3920 | | |
3921 | | /** |
3922 | | * xmlValidateAttributeValue2: |
3923 | | * @ctxt: the validation context |
3924 | | * @doc: the document |
3925 | | * @name: the attribute name (used for error reporting only) |
3926 | | * @type: the attribute type |
3927 | | * @value: the attribute value |
3928 | | * |
3929 | | * Validate that the given attribute value match a given type. |
3930 | | * This typically cannot be done before having finished parsing |
3931 | | * the subsets. |
3932 | | * |
3933 | | * [ VC: IDREF ] |
3934 | | * Values of type IDREF must match one of the declared IDs |
3935 | | * Values of type IDREFS must match a sequence of the declared IDs |
3936 | | * each Name must match the value of an ID attribute on some element |
3937 | | * in the XML document; i.e. IDREF values must match the value of |
3938 | | * some ID attribute |
3939 | | * |
3940 | | * [ VC: Entity Name ] |
3941 | | * Values of type ENTITY must match one declared entity |
3942 | | * Values of type ENTITIES must match a sequence of declared entities |
3943 | | * |
3944 | | * [ VC: Notation Attributes ] |
3945 | | * all notation names in the declaration must be declared. |
3946 | | * |
3947 | | * returns 1 if valid or 0 otherwise |
3948 | | */ |
3949 | | |
3950 | | static int |
3951 | | xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
3952 | 0 | const xmlChar *name, xmlAttributeType type, const xmlChar *value) { |
3953 | 0 | int ret = 1; |
3954 | 0 | switch (type) { |
3955 | 0 | case XML_ATTRIBUTE_IDREFS: |
3956 | 0 | case XML_ATTRIBUTE_IDREF: |
3957 | 0 | case XML_ATTRIBUTE_ID: |
3958 | 0 | case XML_ATTRIBUTE_NMTOKENS: |
3959 | 0 | case XML_ATTRIBUTE_ENUMERATION: |
3960 | 0 | case XML_ATTRIBUTE_NMTOKEN: |
3961 | 0 | case XML_ATTRIBUTE_CDATA: |
3962 | 0 | break; |
3963 | 0 | case XML_ATTRIBUTE_ENTITY: { |
3964 | 0 | xmlEntityPtr ent; |
3965 | |
|
3966 | 0 | ent = xmlGetDocEntity(doc, value); |
3967 | | /* yeah it's a bit messy... */ |
3968 | 0 | if ((ent == NULL) && (doc->standalone == 1)) { |
3969 | 0 | doc->standalone = 0; |
3970 | 0 | ent = xmlGetDocEntity(doc, value); |
3971 | 0 | } |
3972 | 0 | if (ent == NULL) { |
3973 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
3974 | 0 | XML_DTD_UNKNOWN_ENTITY, |
3975 | 0 | "ENTITY attribute %s reference an unknown entity \"%s\"\n", |
3976 | 0 | name, value, NULL); |
3977 | 0 | ret = 0; |
3978 | 0 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
3979 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
3980 | 0 | XML_DTD_ENTITY_TYPE, |
3981 | 0 | "ENTITY attribute %s reference an entity \"%s\" of wrong type\n", |
3982 | 0 | name, value, NULL); |
3983 | 0 | ret = 0; |
3984 | 0 | } |
3985 | 0 | break; |
3986 | 0 | } |
3987 | 0 | case XML_ATTRIBUTE_ENTITIES: { |
3988 | 0 | xmlChar *dup, *nam = NULL, *cur, save; |
3989 | 0 | xmlEntityPtr ent; |
3990 | |
|
3991 | 0 | dup = xmlStrdup(value); |
3992 | 0 | if (dup == NULL) |
3993 | 0 | return(0); |
3994 | 0 | cur = dup; |
3995 | 0 | while (*cur != 0) { |
3996 | 0 | nam = cur; |
3997 | 0 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
3998 | 0 | save = *cur; |
3999 | 0 | *cur = 0; |
4000 | 0 | ent = xmlGetDocEntity(doc, nam); |
4001 | 0 | if (ent == NULL) { |
4002 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
4003 | 0 | XML_DTD_UNKNOWN_ENTITY, |
4004 | 0 | "ENTITIES attribute %s reference an unknown entity \"%s\"\n", |
4005 | 0 | name, nam, NULL); |
4006 | 0 | ret = 0; |
4007 | 0 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
4008 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
4009 | 0 | XML_DTD_ENTITY_TYPE, |
4010 | 0 | "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n", |
4011 | 0 | name, nam, NULL); |
4012 | 0 | ret = 0; |
4013 | 0 | } |
4014 | 0 | if (save == 0) |
4015 | 0 | break; |
4016 | 0 | *cur = save; |
4017 | 0 | while (IS_BLANK_CH(*cur)) cur++; |
4018 | 0 | } |
4019 | 0 | xmlFree(dup); |
4020 | 0 | break; |
4021 | 0 | } |
4022 | 0 | case XML_ATTRIBUTE_NOTATION: { |
4023 | 0 | xmlNotationPtr nota; |
4024 | |
|
4025 | 0 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
4026 | 0 | if ((nota == NULL) && (doc->extSubset != NULL)) |
4027 | 0 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
4028 | |
|
4029 | 0 | if (nota == NULL) { |
4030 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
4031 | 0 | XML_DTD_UNKNOWN_NOTATION, |
4032 | 0 | "NOTATION attribute %s reference an unknown notation \"%s\"\n", |
4033 | 0 | name, value, NULL); |
4034 | 0 | ret = 0; |
4035 | 0 | } |
4036 | 0 | break; |
4037 | 0 | } |
4038 | 0 | } |
4039 | 0 | return(ret); |
4040 | 0 | } |
4041 | | |
4042 | | /** |
4043 | | * xmlValidCtxtNormalizeAttributeValue: |
4044 | | * @ctxt: the validation context |
4045 | | * @doc: the document |
4046 | | * @elem: the parent |
4047 | | * @name: the attribute name |
4048 | | * @value: the attribute value |
4049 | | * @ctxt: the validation context or NULL |
4050 | | * |
4051 | | * Does the validation related extra step of the normalization of attribute |
4052 | | * values: |
4053 | | * |
4054 | | * If the declared value is not CDATA, then the XML processor must further |
4055 | | * process the normalized attribute value by discarding any leading and |
4056 | | * trailing space (#x20) characters, and by replacing sequences of space |
4057 | | * (#x20) characters by single space (#x20) character. |
4058 | | * |
4059 | | * Also check VC: Standalone Document Declaration in P32, and update |
4060 | | * ctxt->valid accordingly |
4061 | | * |
4062 | | * returns a new normalized string if normalization is needed, NULL otherwise |
4063 | | * the caller must free the returned value. |
4064 | | */ |
4065 | | |
4066 | | xmlChar * |
4067 | | xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
4068 | 0 | xmlNodePtr elem, const xmlChar *name, const xmlChar *value) { |
4069 | 0 | xmlChar *ret; |
4070 | 0 | xmlAttributePtr attrDecl = NULL; |
4071 | 0 | int extsubset = 0; |
4072 | |
|
4073 | 0 | if (doc == NULL) return(NULL); |
4074 | 0 | if (elem == NULL) return(NULL); |
4075 | 0 | if (name == NULL) return(NULL); |
4076 | 0 | if (value == NULL) return(NULL); |
4077 | | |
4078 | 0 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
4079 | 0 | xmlChar fn[50]; |
4080 | 0 | xmlChar *fullname; |
4081 | |
|
4082 | 0 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
4083 | 0 | if (fullname == NULL) |
4084 | 0 | return(NULL); |
4085 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
4086 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
4087 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
4088 | 0 | if (attrDecl != NULL) |
4089 | 0 | extsubset = 1; |
4090 | 0 | } |
4091 | 0 | if ((fullname != fn) && (fullname != elem->name)) |
4092 | 0 | xmlFree(fullname); |
4093 | 0 | } |
4094 | 0 | if ((attrDecl == NULL) && (doc->intSubset != NULL)) |
4095 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
4096 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
4097 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
4098 | 0 | if (attrDecl != NULL) |
4099 | 0 | extsubset = 1; |
4100 | 0 | } |
4101 | |
|
4102 | 0 | if (attrDecl == NULL) |
4103 | 0 | return(NULL); |
4104 | 0 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
4105 | 0 | return(NULL); |
4106 | | |
4107 | 0 | ret = xmlStrdup(value); |
4108 | 0 | if (ret == NULL) |
4109 | 0 | return(NULL); |
4110 | 0 | xmlValidNormalizeString(ret); |
4111 | 0 | if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) { |
4112 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE, |
4113 | 0 | "standalone: %s on %s value had to be normalized based on external subset declaration\n", |
4114 | 0 | name, elem->name, NULL); |
4115 | 0 | ctxt->valid = 0; |
4116 | 0 | } |
4117 | 0 | return(ret); |
4118 | 0 | } |
4119 | | |
4120 | | /** |
4121 | | * xmlValidNormalizeAttributeValue: |
4122 | | * @doc: the document |
4123 | | * @elem: the parent |
4124 | | * @name: the attribute name |
4125 | | * @value: the attribute value |
4126 | | * |
4127 | | * Does the validation related extra step of the normalization of attribute |
4128 | | * values: |
4129 | | * |
4130 | | * If the declared value is not CDATA, then the XML processor must further |
4131 | | * process the normalized attribute value by discarding any leading and |
4132 | | * trailing space (#x20) characters, and by replacing sequences of space |
4133 | | * (#x20) characters by single space (#x20) character. |
4134 | | * |
4135 | | * Returns a new normalized string if normalization is needed, NULL otherwise |
4136 | | * the caller must free the returned value. |
4137 | | */ |
4138 | | |
4139 | | xmlChar * |
4140 | | xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem, |
4141 | 0 | const xmlChar *name, const xmlChar *value) { |
4142 | 0 | xmlChar *ret; |
4143 | 0 | xmlAttributePtr attrDecl = NULL; |
4144 | |
|
4145 | 0 | if (doc == NULL) return(NULL); |
4146 | 0 | if (elem == NULL) return(NULL); |
4147 | 0 | if (name == NULL) return(NULL); |
4148 | 0 | if (value == NULL) return(NULL); |
4149 | | |
4150 | 0 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
4151 | 0 | xmlChar fn[50]; |
4152 | 0 | xmlChar *fullname; |
4153 | |
|
4154 | 0 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
4155 | 0 | if (fullname == NULL) |
4156 | 0 | return(NULL); |
4157 | 0 | if ((fullname != fn) && (fullname != elem->name)) |
4158 | 0 | xmlFree(fullname); |
4159 | 0 | } |
4160 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
4161 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4162 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
4163 | |
|
4164 | 0 | if (attrDecl == NULL) |
4165 | 0 | return(NULL); |
4166 | 0 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
4167 | 0 | return(NULL); |
4168 | | |
4169 | 0 | ret = xmlStrdup(value); |
4170 | 0 | if (ret == NULL) |
4171 | 0 | return(NULL); |
4172 | 0 | xmlValidNormalizeString(ret); |
4173 | 0 | return(ret); |
4174 | 0 | } |
4175 | | |
4176 | | static void |
4177 | | xmlValidateAttributeIdCallback(void *payload, void *data, |
4178 | 0 | const xmlChar *name ATTRIBUTE_UNUSED) { |
4179 | 0 | xmlAttributePtr attr = (xmlAttributePtr) payload; |
4180 | 0 | int *count = (int *) data; |
4181 | 0 | if (attr->atype == XML_ATTRIBUTE_ID) (*count)++; |
4182 | 0 | } |
4183 | | |
4184 | | /** |
4185 | | * xmlValidateAttributeDecl: |
4186 | | * @ctxt: the validation context |
4187 | | * @doc: a document instance |
4188 | | * @attr: an attribute definition |
4189 | | * |
4190 | | * Try to validate a single attribute definition |
4191 | | * basically it does the following checks as described by the |
4192 | | * XML-1.0 recommendation: |
4193 | | * - [ VC: Attribute Default Legal ] |
4194 | | * - [ VC: Enumeration ] |
4195 | | * - [ VC: ID Attribute Default ] |
4196 | | * |
4197 | | * The ID/IDREF uniqueness and matching are done separately |
4198 | | * |
4199 | | * returns 1 if valid or 0 otherwise |
4200 | | */ |
4201 | | |
4202 | | int |
4203 | | xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
4204 | 0 | xmlAttributePtr attr) { |
4205 | 0 | int ret = 1; |
4206 | 0 | int val; |
4207 | 0 | CHECK_DTD; |
4208 | 0 | if(attr == NULL) return(1); |
4209 | | |
4210 | | /* Attribute Default Legal */ |
4211 | | /* Enumeration */ |
4212 | 0 | if (attr->defaultValue != NULL) { |
4213 | 0 | val = xmlValidateAttributeValueInternal(doc, attr->atype, |
4214 | 0 | attr->defaultValue); |
4215 | 0 | if (val == 0) { |
4216 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT, |
4217 | 0 | "Syntax of default value for attribute %s of %s is not valid\n", |
4218 | 0 | attr->name, attr->elem, NULL); |
4219 | 0 | } |
4220 | 0 | ret &= val; |
4221 | 0 | } |
4222 | | |
4223 | | /* ID Attribute Default */ |
4224 | 0 | if ((attr->atype == XML_ATTRIBUTE_ID)&& |
4225 | 0 | (attr->def != XML_ATTRIBUTE_IMPLIED) && |
4226 | 0 | (attr->def != XML_ATTRIBUTE_REQUIRED)) { |
4227 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED, |
4228 | 0 | "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n", |
4229 | 0 | attr->name, attr->elem, NULL); |
4230 | 0 | ret = 0; |
4231 | 0 | } |
4232 | | |
4233 | | /* One ID per Element Type */ |
4234 | 0 | if (attr->atype == XML_ATTRIBUTE_ID) { |
4235 | 0 | int nbId; |
4236 | | |
4237 | | /* the trick is that we parse DtD as their own internal subset */ |
4238 | 0 | xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset, |
4239 | 0 | attr->elem); |
4240 | 0 | if (elem != NULL) { |
4241 | 0 | nbId = xmlScanIDAttributeDecl(NULL, elem, 0); |
4242 | 0 | } else { |
4243 | 0 | xmlAttributeTablePtr table; |
4244 | | |
4245 | | /* |
4246 | | * The attribute may be declared in the internal subset and the |
4247 | | * element in the external subset. |
4248 | | */ |
4249 | 0 | nbId = 0; |
4250 | 0 | if (doc->intSubset != NULL) { |
4251 | 0 | table = (xmlAttributeTablePtr) doc->intSubset->attributes; |
4252 | 0 | xmlHashScan3(table, NULL, NULL, attr->elem, |
4253 | 0 | xmlValidateAttributeIdCallback, &nbId); |
4254 | 0 | } |
4255 | 0 | } |
4256 | 0 | if (nbId > 1) { |
4257 | |
|
4258 | 0 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
4259 | 0 | "Element %s has %d ID attribute defined in the internal subset : %s\n", |
4260 | 0 | attr->elem, nbId, attr->name); |
4261 | 0 | } else if (doc->extSubset != NULL) { |
4262 | 0 | int extId = 0; |
4263 | 0 | elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem); |
4264 | 0 | if (elem != NULL) { |
4265 | 0 | extId = xmlScanIDAttributeDecl(NULL, elem, 0); |
4266 | 0 | } |
4267 | 0 | if (extId > 1) { |
4268 | 0 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
4269 | 0 | "Element %s has %d ID attribute defined in the external subset : %s\n", |
4270 | 0 | attr->elem, extId, attr->name); |
4271 | 0 | } else if (extId + nbId > 1) { |
4272 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
4273 | 0 | "Element %s has ID attributes defined in the internal and external subset : %s\n", |
4274 | 0 | attr->elem, attr->name, NULL); |
4275 | 0 | } |
4276 | 0 | } |
4277 | 0 | } |
4278 | | |
4279 | | /* Validity Constraint: Enumeration */ |
4280 | 0 | if ((attr->defaultValue != NULL) && (attr->tree != NULL)) { |
4281 | 0 | xmlEnumerationPtr tree = attr->tree; |
4282 | 0 | while (tree != NULL) { |
4283 | 0 | if (xmlStrEqual(tree->name, attr->defaultValue)) break; |
4284 | 0 | tree = tree->next; |
4285 | 0 | } |
4286 | 0 | if (tree == NULL) { |
4287 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE, |
4288 | 0 | "Default value \"%s\" for attribute %s of %s is not among the enumerated set\n", |
4289 | 0 | attr->defaultValue, attr->name, attr->elem); |
4290 | 0 | ret = 0; |
4291 | 0 | } |
4292 | 0 | } |
4293 | |
|
4294 | 0 | return(ret); |
4295 | 0 | } |
4296 | | |
4297 | | /** |
4298 | | * xmlValidateElementDecl: |
4299 | | * @ctxt: the validation context |
4300 | | * @doc: a document instance |
4301 | | * @elem: an element definition |
4302 | | * |
4303 | | * Try to validate a single element definition |
4304 | | * basically it does the following checks as described by the |
4305 | | * XML-1.0 recommendation: |
4306 | | * - [ VC: One ID per Element Type ] |
4307 | | * - [ VC: No Duplicate Types ] |
4308 | | * - [ VC: Unique Element Type Declaration ] |
4309 | | * |
4310 | | * returns 1 if valid or 0 otherwise |
4311 | | */ |
4312 | | |
4313 | | int |
4314 | | xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
4315 | 0 | xmlElementPtr elem) { |
4316 | 0 | int ret = 1; |
4317 | 0 | xmlElementPtr tst; |
4318 | |
|
4319 | 0 | CHECK_DTD; |
4320 | | |
4321 | 0 | if (elem == NULL) return(1); |
4322 | | |
4323 | | #if 0 |
4324 | | #ifdef LIBXML_REGEXP_ENABLED |
4325 | | /* Build the regexp associated to the content model */ |
4326 | | ret = xmlValidBuildContentModel(ctxt, elem); |
4327 | | #endif |
4328 | | #endif |
4329 | | |
4330 | | /* No Duplicate Types */ |
4331 | 0 | if (elem->etype == XML_ELEMENT_TYPE_MIXED) { |
4332 | 0 | xmlElementContentPtr cur, next; |
4333 | 0 | const xmlChar *name; |
4334 | |
|
4335 | 0 | cur = elem->content; |
4336 | 0 | while (cur != NULL) { |
4337 | 0 | if (cur->type != XML_ELEMENT_CONTENT_OR) break; |
4338 | 0 | if (cur->c1 == NULL) break; |
4339 | 0 | if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
4340 | 0 | name = cur->c1->name; |
4341 | 0 | next = cur->c2; |
4342 | 0 | while (next != NULL) { |
4343 | 0 | if (next->type == XML_ELEMENT_CONTENT_ELEMENT) { |
4344 | 0 | if ((xmlStrEqual(next->name, name)) && |
4345 | 0 | (xmlStrEqual(next->prefix, cur->c1->prefix))) { |
4346 | 0 | if (cur->c1->prefix == NULL) { |
4347 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
4348 | 0 | "Definition of %s has duplicate references of %s\n", |
4349 | 0 | elem->name, name, NULL); |
4350 | 0 | } else { |
4351 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
4352 | 0 | "Definition of %s has duplicate references of %s:%s\n", |
4353 | 0 | elem->name, cur->c1->prefix, name); |
4354 | 0 | } |
4355 | 0 | ret = 0; |
4356 | 0 | } |
4357 | 0 | break; |
4358 | 0 | } |
4359 | 0 | if (next->c1 == NULL) break; |
4360 | 0 | if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break; |
4361 | 0 | if ((xmlStrEqual(next->c1->name, name)) && |
4362 | 0 | (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) { |
4363 | 0 | if (cur->c1->prefix == NULL) { |
4364 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
4365 | 0 | "Definition of %s has duplicate references to %s\n", |
4366 | 0 | elem->name, name, NULL); |
4367 | 0 | } else { |
4368 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
4369 | 0 | "Definition of %s has duplicate references to %s:%s\n", |
4370 | 0 | elem->name, cur->c1->prefix, name); |
4371 | 0 | } |
4372 | 0 | ret = 0; |
4373 | 0 | } |
4374 | 0 | next = next->c2; |
4375 | 0 | } |
4376 | 0 | } |
4377 | 0 | cur = cur->c2; |
4378 | 0 | } |
4379 | 0 | } |
4380 | | |
4381 | | /* VC: Unique Element Type Declaration */ |
4382 | 0 | tst = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
4383 | 0 | if ((tst != NULL ) && (tst != elem) && |
4384 | 0 | ((tst->prefix == elem->prefix) || |
4385 | 0 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
4386 | 0 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
4387 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
4388 | 0 | "Redefinition of element %s\n", |
4389 | 0 | elem->name, NULL, NULL); |
4390 | 0 | ret = 0; |
4391 | 0 | } |
4392 | 0 | tst = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
4393 | 0 | if ((tst != NULL ) && (tst != elem) && |
4394 | 0 | ((tst->prefix == elem->prefix) || |
4395 | 0 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
4396 | 0 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
4397 | 0 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
4398 | 0 | "Redefinition of element %s\n", |
4399 | 0 | elem->name, NULL, NULL); |
4400 | 0 | ret = 0; |
4401 | 0 | } |
4402 | | /* One ID per Element Type |
4403 | | * already done when registering the attribute |
4404 | | if (xmlScanIDAttributeDecl(ctxt, elem) > 1) { |
4405 | | ret = 0; |
4406 | | } */ |
4407 | 0 | return(ret); |
4408 | 0 | } |
4409 | | |
4410 | | /** |
4411 | | * xmlValidateOneAttribute: |
4412 | | * @ctxt: the validation context |
4413 | | * @doc: a document instance |
4414 | | * @elem: an element instance |
4415 | | * @attr: an attribute instance |
4416 | | * @value: the attribute value (without entities processing) |
4417 | | * |
4418 | | * Try to validate a single attribute for an element |
4419 | | * basically it does the following checks as described by the |
4420 | | * XML-1.0 recommendation: |
4421 | | * - [ VC: Attribute Value Type ] |
4422 | | * - [ VC: Fixed Attribute Default ] |
4423 | | * - [ VC: Entity Name ] |
4424 | | * - [ VC: Name Token ] |
4425 | | * - [ VC: ID ] |
4426 | | * - [ VC: IDREF ] |
4427 | | * - [ VC: Entity Name ] |
4428 | | * - [ VC: Notation Attributes ] |
4429 | | * |
4430 | | * The ID/IDREF uniqueness and matching are done separately |
4431 | | * |
4432 | | * returns 1 if valid or 0 otherwise |
4433 | | */ |
4434 | | |
4435 | | int |
4436 | | xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
4437 | | xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) |
4438 | 0 | { |
4439 | 0 | xmlAttributePtr attrDecl = NULL; |
4440 | 0 | int val; |
4441 | 0 | int ret = 1; |
4442 | |
|
4443 | 0 | CHECK_DTD; |
4444 | 0 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
4445 | 0 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
4446 | | |
4447 | 0 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
4448 | 0 | xmlChar fn[50]; |
4449 | 0 | xmlChar *fullname; |
4450 | |
|
4451 | 0 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
4452 | 0 | if (fullname == NULL) |
4453 | 0 | return(0); |
4454 | 0 | if (attr->ns != NULL) { |
4455 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
4456 | 0 | attr->name, attr->ns->prefix); |
4457 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4458 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
4459 | 0 | attr->name, attr->ns->prefix); |
4460 | 0 | } else { |
4461 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name); |
4462 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4463 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
4464 | 0 | fullname, attr->name); |
4465 | 0 | } |
4466 | 0 | if ((fullname != fn) && (fullname != elem->name)) |
4467 | 0 | xmlFree(fullname); |
4468 | 0 | } |
4469 | 0 | if (attrDecl == NULL) { |
4470 | 0 | if (attr->ns != NULL) { |
4471 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
4472 | 0 | attr->name, attr->ns->prefix); |
4473 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4474 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
4475 | 0 | attr->name, attr->ns->prefix); |
4476 | 0 | } else { |
4477 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
4478 | 0 | elem->name, attr->name); |
4479 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4480 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
4481 | 0 | elem->name, attr->name); |
4482 | 0 | } |
4483 | 0 | } |
4484 | | |
4485 | | |
4486 | | /* Validity Constraint: Attribute Value Type */ |
4487 | 0 | if (attrDecl == NULL) { |
4488 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
4489 | 0 | "No declaration for attribute %s of element %s\n", |
4490 | 0 | attr->name, elem->name, NULL); |
4491 | 0 | return(0); |
4492 | 0 | } |
4493 | 0 | attr->atype = attrDecl->atype; |
4494 | |
|
4495 | 0 | val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value); |
4496 | 0 | if (val == 0) { |
4497 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
4498 | 0 | "Syntax of value for attribute %s of %s is not valid\n", |
4499 | 0 | attr->name, elem->name, NULL); |
4500 | 0 | ret = 0; |
4501 | 0 | } |
4502 | | |
4503 | | /* Validity constraint: Fixed Attribute Default */ |
4504 | 0 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
4505 | 0 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
4506 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
4507 | 0 | "Value for attribute %s of %s is different from default \"%s\"\n", |
4508 | 0 | attr->name, elem->name, attrDecl->defaultValue); |
4509 | 0 | ret = 0; |
4510 | 0 | } |
4511 | 0 | } |
4512 | | |
4513 | | /* Validity Constraint: ID uniqueness */ |
4514 | 0 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
4515 | 0 | if (xmlAddID(ctxt, doc, value, attr) == NULL) |
4516 | 0 | ret = 0; |
4517 | 0 | } |
4518 | |
|
4519 | 0 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
4520 | 0 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
4521 | 0 | if (xmlAddRef(ctxt, doc, value, attr) == NULL) |
4522 | 0 | ret = 0; |
4523 | 0 | } |
4524 | | |
4525 | | /* Validity Constraint: Notation Attributes */ |
4526 | 0 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
4527 | 0 | xmlEnumerationPtr tree = attrDecl->tree; |
4528 | 0 | xmlNotationPtr nota; |
4529 | | |
4530 | | /* First check that the given NOTATION was declared */ |
4531 | 0 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
4532 | 0 | if (nota == NULL) |
4533 | 0 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
4534 | |
|
4535 | 0 | if (nota == NULL) { |
4536 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
4537 | 0 | "Value \"%s\" for attribute %s of %s is not a declared Notation\n", |
4538 | 0 | value, attr->name, elem->name); |
4539 | 0 | ret = 0; |
4540 | 0 | } |
4541 | | |
4542 | | /* Second, verify that it's among the list */ |
4543 | 0 | while (tree != NULL) { |
4544 | 0 | if (xmlStrEqual(tree->name, value)) break; |
4545 | 0 | tree = tree->next; |
4546 | 0 | } |
4547 | 0 | if (tree == NULL) { |
4548 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
4549 | 0 | "Value \"%s\" for attribute %s of %s is not among the enumerated notations\n", |
4550 | 0 | value, attr->name, elem->name); |
4551 | 0 | ret = 0; |
4552 | 0 | } |
4553 | 0 | } |
4554 | | |
4555 | | /* Validity Constraint: Enumeration */ |
4556 | 0 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
4557 | 0 | xmlEnumerationPtr tree = attrDecl->tree; |
4558 | 0 | while (tree != NULL) { |
4559 | 0 | if (xmlStrEqual(tree->name, value)) break; |
4560 | 0 | tree = tree->next; |
4561 | 0 | } |
4562 | 0 | if (tree == NULL) { |
4563 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
4564 | 0 | "Value \"%s\" for attribute %s of %s is not among the enumerated set\n", |
4565 | 0 | value, attr->name, elem->name); |
4566 | 0 | ret = 0; |
4567 | 0 | } |
4568 | 0 | } |
4569 | | |
4570 | | /* Fixed Attribute Default */ |
4571 | 0 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
4572 | 0 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
4573 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
4574 | 0 | "Value for attribute %s of %s must be \"%s\"\n", |
4575 | 0 | attr->name, elem->name, attrDecl->defaultValue); |
4576 | 0 | ret = 0; |
4577 | 0 | } |
4578 | | |
4579 | | /* Extra check for the attribute value */ |
4580 | 0 | ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name, |
4581 | 0 | attrDecl->atype, value); |
4582 | |
|
4583 | 0 | return(ret); |
4584 | 0 | } |
4585 | | |
4586 | | /** |
4587 | | * xmlValidateOneNamespace: |
4588 | | * @ctxt: the validation context |
4589 | | * @doc: a document instance |
4590 | | * @elem: an element instance |
4591 | | * @prefix: the namespace prefix |
4592 | | * @ns: an namespace declaration instance |
4593 | | * @value: the attribute value (without entities processing) |
4594 | | * |
4595 | | * Try to validate a single namespace declaration for an element |
4596 | | * basically it does the following checks as described by the |
4597 | | * XML-1.0 recommendation: |
4598 | | * - [ VC: Attribute Value Type ] |
4599 | | * - [ VC: Fixed Attribute Default ] |
4600 | | * - [ VC: Entity Name ] |
4601 | | * - [ VC: Name Token ] |
4602 | | * - [ VC: ID ] |
4603 | | * - [ VC: IDREF ] |
4604 | | * - [ VC: Entity Name ] |
4605 | | * - [ VC: Notation Attributes ] |
4606 | | * |
4607 | | * The ID/IDREF uniqueness and matching are done separately |
4608 | | * |
4609 | | * returns 1 if valid or 0 otherwise |
4610 | | */ |
4611 | | |
4612 | | int |
4613 | | xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
4614 | 0 | xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) { |
4615 | | /* xmlElementPtr elemDecl; */ |
4616 | 0 | xmlAttributePtr attrDecl = NULL; |
4617 | 0 | int val; |
4618 | 0 | int ret = 1; |
4619 | |
|
4620 | 0 | CHECK_DTD; |
4621 | 0 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
4622 | 0 | if ((ns == NULL) || (ns->href == NULL)) return(0); |
4623 | | |
4624 | 0 | if (prefix != NULL) { |
4625 | 0 | xmlChar fn[50]; |
4626 | 0 | xmlChar *fullname; |
4627 | |
|
4628 | 0 | fullname = xmlBuildQName(elem->name, prefix, fn, 50); |
4629 | 0 | if (fullname == NULL) { |
4630 | 0 | xmlVErrMemory(ctxt, "Validating namespace"); |
4631 | 0 | return(0); |
4632 | 0 | } |
4633 | 0 | if (ns->prefix != NULL) { |
4634 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
4635 | 0 | ns->prefix, BAD_CAST "xmlns"); |
4636 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4637 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
4638 | 0 | ns->prefix, BAD_CAST "xmlns"); |
4639 | 0 | } else { |
4640 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
4641 | 0 | BAD_CAST "xmlns"); |
4642 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4643 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
4644 | 0 | BAD_CAST "xmlns"); |
4645 | 0 | } |
4646 | 0 | if ((fullname != fn) && (fullname != elem->name)) |
4647 | 0 | xmlFree(fullname); |
4648 | 0 | } |
4649 | 0 | if (attrDecl == NULL) { |
4650 | 0 | if (ns->prefix != NULL) { |
4651 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
4652 | 0 | ns->prefix, BAD_CAST "xmlns"); |
4653 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4654 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
4655 | 0 | ns->prefix, BAD_CAST "xmlns"); |
4656 | 0 | } else { |
4657 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
4658 | 0 | elem->name, BAD_CAST "xmlns"); |
4659 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4660 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
4661 | 0 | elem->name, BAD_CAST "xmlns"); |
4662 | 0 | } |
4663 | 0 | } |
4664 | | |
4665 | | |
4666 | | /* Validity Constraint: Attribute Value Type */ |
4667 | 0 | if (attrDecl == NULL) { |
4668 | 0 | if (ns->prefix != NULL) { |
4669 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
4670 | 0 | "No declaration for attribute xmlns:%s of element %s\n", |
4671 | 0 | ns->prefix, elem->name, NULL); |
4672 | 0 | } else { |
4673 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
4674 | 0 | "No declaration for attribute xmlns of element %s\n", |
4675 | 0 | elem->name, NULL, NULL); |
4676 | 0 | } |
4677 | 0 | return(0); |
4678 | 0 | } |
4679 | | |
4680 | 0 | val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value); |
4681 | 0 | if (val == 0) { |
4682 | 0 | if (ns->prefix != NULL) { |
4683 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
4684 | 0 | "Syntax of value for attribute xmlns:%s of %s is not valid\n", |
4685 | 0 | ns->prefix, elem->name, NULL); |
4686 | 0 | } else { |
4687 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
4688 | 0 | "Syntax of value for attribute xmlns of %s is not valid\n", |
4689 | 0 | elem->name, NULL, NULL); |
4690 | 0 | } |
4691 | 0 | ret = 0; |
4692 | 0 | } |
4693 | | |
4694 | | /* Validity constraint: Fixed Attribute Default */ |
4695 | 0 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
4696 | 0 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
4697 | 0 | if (ns->prefix != NULL) { |
4698 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
4699 | 0 | "Value for attribute xmlns:%s of %s is different from default \"%s\"\n", |
4700 | 0 | ns->prefix, elem->name, attrDecl->defaultValue); |
4701 | 0 | } else { |
4702 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
4703 | 0 | "Value for attribute xmlns of %s is different from default \"%s\"\n", |
4704 | 0 | elem->name, attrDecl->defaultValue, NULL); |
4705 | 0 | } |
4706 | 0 | ret = 0; |
4707 | 0 | } |
4708 | 0 | } |
4709 | | |
4710 | | /* |
4711 | | * Casting ns to xmlAttrPtr is wrong. We'd need separate functions |
4712 | | * xmlAddID and xmlAddRef for namespace declarations, but it makes |
4713 | | * no practical sense to use ID types anyway. |
4714 | | */ |
4715 | | #if 0 |
4716 | | /* Validity Constraint: ID uniqueness */ |
4717 | | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
4718 | | if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
4719 | | ret = 0; |
4720 | | } |
4721 | | |
4722 | | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
4723 | | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
4724 | | if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
4725 | | ret = 0; |
4726 | | } |
4727 | | #endif |
4728 | | |
4729 | | /* Validity Constraint: Notation Attributes */ |
4730 | 0 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
4731 | 0 | xmlEnumerationPtr tree = attrDecl->tree; |
4732 | 0 | xmlNotationPtr nota; |
4733 | | |
4734 | | /* First check that the given NOTATION was declared */ |
4735 | 0 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
4736 | 0 | if (nota == NULL) |
4737 | 0 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
4738 | |
|
4739 | 0 | if (nota == NULL) { |
4740 | 0 | if (ns->prefix != NULL) { |
4741 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
4742 | 0 | "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n", |
4743 | 0 | value, ns->prefix, elem->name); |
4744 | 0 | } else { |
4745 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
4746 | 0 | "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n", |
4747 | 0 | value, elem->name, NULL); |
4748 | 0 | } |
4749 | 0 | ret = 0; |
4750 | 0 | } |
4751 | | |
4752 | | /* Second, verify that it's among the list */ |
4753 | 0 | while (tree != NULL) { |
4754 | 0 | if (xmlStrEqual(tree->name, value)) break; |
4755 | 0 | tree = tree->next; |
4756 | 0 | } |
4757 | 0 | if (tree == NULL) { |
4758 | 0 | if (ns->prefix != NULL) { |
4759 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
4760 | 0 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n", |
4761 | 0 | value, ns->prefix, elem->name); |
4762 | 0 | } else { |
4763 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
4764 | 0 | "Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n", |
4765 | 0 | value, elem->name, NULL); |
4766 | 0 | } |
4767 | 0 | ret = 0; |
4768 | 0 | } |
4769 | 0 | } |
4770 | | |
4771 | | /* Validity Constraint: Enumeration */ |
4772 | 0 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
4773 | 0 | xmlEnumerationPtr tree = attrDecl->tree; |
4774 | 0 | while (tree != NULL) { |
4775 | 0 | if (xmlStrEqual(tree->name, value)) break; |
4776 | 0 | tree = tree->next; |
4777 | 0 | } |
4778 | 0 | if (tree == NULL) { |
4779 | 0 | if (ns->prefix != NULL) { |
4780 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
4781 | 0 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n", |
4782 | 0 | value, ns->prefix, elem->name); |
4783 | 0 | } else { |
4784 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
4785 | 0 | "Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n", |
4786 | 0 | value, elem->name, NULL); |
4787 | 0 | } |
4788 | 0 | ret = 0; |
4789 | 0 | } |
4790 | 0 | } |
4791 | | |
4792 | | /* Fixed Attribute Default */ |
4793 | 0 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
4794 | 0 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
4795 | 0 | if (ns->prefix != NULL) { |
4796 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
4797 | 0 | "Value for attribute xmlns:%s of %s must be \"%s\"\n", |
4798 | 0 | ns->prefix, elem->name, attrDecl->defaultValue); |
4799 | 0 | } else { |
4800 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
4801 | 0 | "Value for attribute xmlns of %s must be \"%s\"\n", |
4802 | 0 | elem->name, attrDecl->defaultValue, NULL); |
4803 | 0 | } |
4804 | 0 | ret = 0; |
4805 | 0 | } |
4806 | | |
4807 | | /* Extra check for the attribute value */ |
4808 | 0 | if (ns->prefix != NULL) { |
4809 | 0 | ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix, |
4810 | 0 | attrDecl->atype, value); |
4811 | 0 | } else { |
4812 | 0 | ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns", |
4813 | 0 | attrDecl->atype, value); |
4814 | 0 | } |
4815 | |
|
4816 | 0 | return(ret); |
4817 | 0 | } |
4818 | | |
4819 | | #ifndef LIBXML_REGEXP_ENABLED |
4820 | | /** |
4821 | | * xmlValidateSkipIgnorable: |
4822 | | * @ctxt: the validation context |
4823 | | * @child: the child list |
4824 | | * |
4825 | | * Skip ignorable elements w.r.t. the validation process |
4826 | | * |
4827 | | * returns the first element to consider for validation of the content model |
4828 | | */ |
4829 | | |
4830 | | static xmlNodePtr |
4831 | | xmlValidateSkipIgnorable(xmlNodePtr child) { |
4832 | | while (child != NULL) { |
4833 | | switch (child->type) { |
4834 | | /* These things are ignored (skipped) during validation. */ |
4835 | | case XML_PI_NODE: |
4836 | | case XML_COMMENT_NODE: |
4837 | | case XML_XINCLUDE_START: |
4838 | | case XML_XINCLUDE_END: |
4839 | | child = child->next; |
4840 | | break; |
4841 | | case XML_TEXT_NODE: |
4842 | | if (xmlIsBlankNode(child)) |
4843 | | child = child->next; |
4844 | | else |
4845 | | return(child); |
4846 | | break; |
4847 | | /* keep current node */ |
4848 | | default: |
4849 | | return(child); |
4850 | | } |
4851 | | } |
4852 | | return(child); |
4853 | | } |
4854 | | |
4855 | | /** |
4856 | | * xmlValidateElementType: |
4857 | | * @ctxt: the validation context |
4858 | | * |
4859 | | * Try to validate the content model of an element internal function |
4860 | | * |
4861 | | * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity |
4862 | | * reference is found and -3 if the validation succeeded but |
4863 | | * the content model is not determinist. |
4864 | | */ |
4865 | | |
4866 | | static int |
4867 | | xmlValidateElementType(xmlValidCtxtPtr ctxt) { |
4868 | | int ret = -1; |
4869 | | int determinist = 1; |
4870 | | |
4871 | | NODE = xmlValidateSkipIgnorable(NODE); |
4872 | | if ((NODE == NULL) && (CONT == NULL)) |
4873 | | return(1); |
4874 | | if ((NODE == NULL) && |
4875 | | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
4876 | | (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) { |
4877 | | return(1); |
4878 | | } |
4879 | | if (CONT == NULL) return(-1); |
4880 | | if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE)) |
4881 | | return(-2); |
4882 | | |
4883 | | /* |
4884 | | * We arrive here when more states need to be examined |
4885 | | */ |
4886 | | cont: |
4887 | | |
4888 | | /* |
4889 | | * We just recovered from a rollback generated by a possible |
4890 | | * epsilon transition, go directly to the analysis phase |
4891 | | */ |
4892 | | if (STATE == ROLLBACK_PARENT) { |
4893 | | DEBUG_VALID_MSG("restored parent branch"); |
4894 | | DEBUG_VALID_STATE(NODE, CONT) |
4895 | | ret = 1; |
4896 | | goto analyze; |
4897 | | } |
4898 | | |
4899 | | DEBUG_VALID_STATE(NODE, CONT) |
4900 | | /* |
4901 | | * we may have to save a backup state here. This is the equivalent |
4902 | | * of handling epsilon transition in NFAs. |
4903 | | */ |
4904 | | if ((CONT != NULL) && |
4905 | | ((CONT->parent == NULL) || |
4906 | | (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) && |
4907 | | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
4908 | | (CONT->ocur == XML_ELEMENT_CONTENT_OPT) || |
4909 | | ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) { |
4910 | | DEBUG_VALID_MSG("saving parent branch"); |
4911 | | if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0) |
4912 | | return(0); |
4913 | | } |
4914 | | |
4915 | | |
4916 | | /* |
4917 | | * Check first if the content matches |
4918 | | */ |
4919 | | switch (CONT->type) { |
4920 | | case XML_ELEMENT_CONTENT_PCDATA: |
4921 | | if (NODE == NULL) { |
4922 | | DEBUG_VALID_MSG("pcdata failed no node"); |
4923 | | ret = 0; |
4924 | | break; |
4925 | | } |
4926 | | if (NODE->type == XML_TEXT_NODE) { |
4927 | | DEBUG_VALID_MSG("pcdata found, skip to next"); |
4928 | | /* |
4929 | | * go to next element in the content model |
4930 | | * skipping ignorable elems |
4931 | | */ |
4932 | | do { |
4933 | | NODE = NODE->next; |
4934 | | NODE = xmlValidateSkipIgnorable(NODE); |
4935 | | if ((NODE != NULL) && |
4936 | | (NODE->type == XML_ENTITY_REF_NODE)) |
4937 | | return(-2); |
4938 | | } while ((NODE != NULL) && |
4939 | | ((NODE->type != XML_ELEMENT_NODE) && |
4940 | | (NODE->type != XML_TEXT_NODE) && |
4941 | | (NODE->type != XML_CDATA_SECTION_NODE))); |
4942 | | ret = 1; |
4943 | | break; |
4944 | | } else { |
4945 | | DEBUG_VALID_MSG("pcdata failed"); |
4946 | | ret = 0; |
4947 | | break; |
4948 | | } |
4949 | | break; |
4950 | | case XML_ELEMENT_CONTENT_ELEMENT: |
4951 | | if (NODE == NULL) { |
4952 | | DEBUG_VALID_MSG("element failed no node"); |
4953 | | ret = 0; |
4954 | | break; |
4955 | | } |
4956 | | ret = ((NODE->type == XML_ELEMENT_NODE) && |
4957 | | (xmlStrEqual(NODE->name, CONT->name))); |
4958 | | if (ret == 1) { |
4959 | | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
4960 | | ret = (CONT->prefix == NULL); |
4961 | | } else if (CONT->prefix == NULL) { |
4962 | | ret = 0; |
4963 | | } else { |
4964 | | ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix); |
4965 | | } |
4966 | | } |
4967 | | if (ret == 1) { |
4968 | | DEBUG_VALID_MSG("element found, skip to next"); |
4969 | | /* |
4970 | | * go to next element in the content model |
4971 | | * skipping ignorable elems |
4972 | | */ |
4973 | | do { |
4974 | | NODE = NODE->next; |
4975 | | NODE = xmlValidateSkipIgnorable(NODE); |
4976 | | if ((NODE != NULL) && |
4977 | | (NODE->type == XML_ENTITY_REF_NODE)) |
4978 | | return(-2); |
4979 | | } while ((NODE != NULL) && |
4980 | | ((NODE->type != XML_ELEMENT_NODE) && |
4981 | | (NODE->type != XML_TEXT_NODE) && |
4982 | | (NODE->type != XML_CDATA_SECTION_NODE))); |
4983 | | } else { |
4984 | | DEBUG_VALID_MSG("element failed"); |
4985 | | ret = 0; |
4986 | | break; |
4987 | | } |
4988 | | break; |
4989 | | case XML_ELEMENT_CONTENT_OR: |
4990 | | /* |
4991 | | * Small optimization. |
4992 | | */ |
4993 | | if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
4994 | | if ((NODE == NULL) || |
4995 | | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
4996 | | DEPTH++; |
4997 | | CONT = CONT->c2; |
4998 | | goto cont; |
4999 | | } |
5000 | | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
5001 | | ret = (CONT->c1->prefix == NULL); |
5002 | | } else if (CONT->c1->prefix == NULL) { |
5003 | | ret = 0; |
5004 | | } else { |
5005 | | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
5006 | | } |
5007 | | if (ret == 0) { |
5008 | | DEPTH++; |
5009 | | CONT = CONT->c2; |
5010 | | goto cont; |
5011 | | } |
5012 | | } |
5013 | | |
5014 | | /* |
5015 | | * save the second branch 'or' branch |
5016 | | */ |
5017 | | DEBUG_VALID_MSG("saving 'or' branch"); |
5018 | | if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1), |
5019 | | OCCURS, ROLLBACK_OR) < 0) |
5020 | | return(-1); |
5021 | | DEPTH++; |
5022 | | CONT = CONT->c1; |
5023 | | goto cont; |
5024 | | case XML_ELEMENT_CONTENT_SEQ: |
5025 | | /* |
5026 | | * Small optimization. |
5027 | | */ |
5028 | | if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) && |
5029 | | ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
5030 | | (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
5031 | | if ((NODE == NULL) || |
5032 | | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
5033 | | DEPTH++; |
5034 | | CONT = CONT->c2; |
5035 | | goto cont; |
5036 | | } |
5037 | | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
5038 | | ret = (CONT->c1->prefix == NULL); |
5039 | | } else if (CONT->c1->prefix == NULL) { |
5040 | | ret = 0; |
5041 | | } else { |
5042 | | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
5043 | | } |
5044 | | if (ret == 0) { |
5045 | | DEPTH++; |
5046 | | CONT = CONT->c2; |
5047 | | goto cont; |
5048 | | } |
5049 | | } |
5050 | | DEPTH++; |
5051 | | CONT = CONT->c1; |
5052 | | goto cont; |
5053 | | } |
5054 | | |
5055 | | /* |
5056 | | * At this point handle going up in the tree |
5057 | | */ |
5058 | | if (ret == -1) { |
5059 | | DEBUG_VALID_MSG("error found returning"); |
5060 | | return(ret); |
5061 | | } |
5062 | | analyze: |
5063 | | while (CONT != NULL) { |
5064 | | /* |
5065 | | * First do the analysis depending on the occurrence model at |
5066 | | * this level. |
5067 | | */ |
5068 | | if (ret == 0) { |
5069 | | switch (CONT->ocur) { |
5070 | | xmlNodePtr cur; |
5071 | | |
5072 | | case XML_ELEMENT_CONTENT_ONCE: |
5073 | | cur = ctxt->vstate->node; |
5074 | | DEBUG_VALID_MSG("Once branch failed, rollback"); |
5075 | | if (vstateVPop(ctxt) < 0 ) { |
5076 | | DEBUG_VALID_MSG("exhaustion, failed"); |
5077 | | return(0); |
5078 | | } |
5079 | | if (cur != ctxt->vstate->node) |
5080 | | determinist = -3; |
5081 | | goto cont; |
5082 | | case XML_ELEMENT_CONTENT_PLUS: |
5083 | | if (OCCURRENCE == 0) { |
5084 | | cur = ctxt->vstate->node; |
5085 | | DEBUG_VALID_MSG("Plus branch failed, rollback"); |
5086 | | if (vstateVPop(ctxt) < 0 ) { |
5087 | | DEBUG_VALID_MSG("exhaustion, failed"); |
5088 | | return(0); |
5089 | | } |
5090 | | if (cur != ctxt->vstate->node) |
5091 | | determinist = -3; |
5092 | | goto cont; |
5093 | | } |
5094 | | DEBUG_VALID_MSG("Plus branch found"); |
5095 | | ret = 1; |
5096 | | break; |
5097 | | case XML_ELEMENT_CONTENT_MULT: |
5098 | | #ifdef DEBUG_VALID_ALGO |
5099 | | if (OCCURRENCE == 0) { |
5100 | | DEBUG_VALID_MSG("Mult branch failed"); |
5101 | | } else { |
5102 | | DEBUG_VALID_MSG("Mult branch found"); |
5103 | | } |
5104 | | #endif |
5105 | | ret = 1; |
5106 | | break; |
5107 | | case XML_ELEMENT_CONTENT_OPT: |
5108 | | DEBUG_VALID_MSG("Option branch failed"); |
5109 | | ret = 1; |
5110 | | break; |
5111 | | } |
5112 | | } else { |
5113 | | switch (CONT->ocur) { |
5114 | | case XML_ELEMENT_CONTENT_OPT: |
5115 | | DEBUG_VALID_MSG("Option branch succeeded"); |
5116 | | ret = 1; |
5117 | | break; |
5118 | | case XML_ELEMENT_CONTENT_ONCE: |
5119 | | DEBUG_VALID_MSG("Once branch succeeded"); |
5120 | | ret = 1; |
5121 | | break; |
5122 | | case XML_ELEMENT_CONTENT_PLUS: |
5123 | | if (STATE == ROLLBACK_PARENT) { |
5124 | | DEBUG_VALID_MSG("Plus branch rollback"); |
5125 | | ret = 1; |
5126 | | break; |
5127 | | } |
5128 | | if (NODE == NULL) { |
5129 | | DEBUG_VALID_MSG("Plus branch exhausted"); |
5130 | | ret = 1; |
5131 | | break; |
5132 | | } |
5133 | | DEBUG_VALID_MSG("Plus branch succeeded, continuing"); |
5134 | | SET_OCCURRENCE; |
5135 | | goto cont; |
5136 | | case XML_ELEMENT_CONTENT_MULT: |
5137 | | if (STATE == ROLLBACK_PARENT) { |
5138 | | DEBUG_VALID_MSG("Mult branch rollback"); |
5139 | | ret = 1; |
5140 | | break; |
5141 | | } |
5142 | | if (NODE == NULL) { |
5143 | | DEBUG_VALID_MSG("Mult branch exhausted"); |
5144 | | ret = 1; |
5145 | | break; |
5146 | | } |
5147 | | DEBUG_VALID_MSG("Mult branch succeeded, continuing"); |
5148 | | /* SET_OCCURRENCE; */ |
5149 | | goto cont; |
5150 | | } |
5151 | | } |
5152 | | STATE = 0; |
5153 | | |
5154 | | /* |
5155 | | * Then act accordingly at the parent level |
5156 | | */ |
5157 | | RESET_OCCURRENCE; |
5158 | | if (CONT->parent == NULL) |
5159 | | break; |
5160 | | |
5161 | | switch (CONT->parent->type) { |
5162 | | case XML_ELEMENT_CONTENT_PCDATA: |
5163 | | DEBUG_VALID_MSG("Error: parent pcdata"); |
5164 | | return(-1); |
5165 | | case XML_ELEMENT_CONTENT_ELEMENT: |
5166 | | DEBUG_VALID_MSG("Error: parent element"); |
5167 | | return(-1); |
5168 | | case XML_ELEMENT_CONTENT_OR: |
5169 | | if (ret == 1) { |
5170 | | DEBUG_VALID_MSG("Or succeeded"); |
5171 | | CONT = CONT->parent; |
5172 | | DEPTH--; |
5173 | | } else { |
5174 | | DEBUG_VALID_MSG("Or failed"); |
5175 | | CONT = CONT->parent; |
5176 | | DEPTH--; |
5177 | | } |
5178 | | break; |
5179 | | case XML_ELEMENT_CONTENT_SEQ: |
5180 | | if (ret == 0) { |
5181 | | DEBUG_VALID_MSG("Sequence failed"); |
5182 | | CONT = CONT->parent; |
5183 | | DEPTH--; |
5184 | | } else if (CONT == CONT->parent->c1) { |
5185 | | DEBUG_VALID_MSG("Sequence testing 2nd branch"); |
5186 | | CONT = CONT->parent->c2; |
5187 | | goto cont; |
5188 | | } else { |
5189 | | DEBUG_VALID_MSG("Sequence succeeded"); |
5190 | | CONT = CONT->parent; |
5191 | | DEPTH--; |
5192 | | } |
5193 | | } |
5194 | | } |
5195 | | if (NODE != NULL) { |
5196 | | xmlNodePtr cur; |
5197 | | |
5198 | | cur = ctxt->vstate->node; |
5199 | | DEBUG_VALID_MSG("Failed, remaining input, rollback"); |
5200 | | if (vstateVPop(ctxt) < 0 ) { |
5201 | | DEBUG_VALID_MSG("exhaustion, failed"); |
5202 | | return(0); |
5203 | | } |
5204 | | if (cur != ctxt->vstate->node) |
5205 | | determinist = -3; |
5206 | | goto cont; |
5207 | | } |
5208 | | if (ret == 0) { |
5209 | | xmlNodePtr cur; |
5210 | | |
5211 | | cur = ctxt->vstate->node; |
5212 | | DEBUG_VALID_MSG("Failure, rollback"); |
5213 | | if (vstateVPop(ctxt) < 0 ) { |
5214 | | DEBUG_VALID_MSG("exhaustion, failed"); |
5215 | | return(0); |
5216 | | } |
5217 | | if (cur != ctxt->vstate->node) |
5218 | | determinist = -3; |
5219 | | goto cont; |
5220 | | } |
5221 | | return(determinist); |
5222 | | } |
5223 | | #endif |
5224 | | |
5225 | | /** |
5226 | | * xmlSnprintfElements: |
5227 | | * @buf: an output buffer |
5228 | | * @size: the size of the buffer |
5229 | | * @content: An element |
5230 | | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
5231 | | * |
5232 | | * This will dump the list of elements to the buffer |
5233 | | * Intended just for the debug routine |
5234 | | */ |
5235 | | static void |
5236 | 0 | xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) { |
5237 | 0 | xmlNodePtr cur; |
5238 | 0 | int len; |
5239 | |
|
5240 | 0 | if (node == NULL) return; |
5241 | 0 | if (glob) strcat(buf, "("); |
5242 | 0 | cur = node; |
5243 | 0 | while (cur != NULL) { |
5244 | 0 | len = strlen(buf); |
5245 | 0 | if (size - len < 50) { |
5246 | 0 | if ((size - len > 4) && (buf[len - 1] != '.')) |
5247 | 0 | strcat(buf, " ..."); |
5248 | 0 | return; |
5249 | 0 | } |
5250 | 0 | switch (cur->type) { |
5251 | 0 | case XML_ELEMENT_NODE: |
5252 | 0 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
5253 | 0 | if (size - len < xmlStrlen(cur->ns->prefix) + 10) { |
5254 | 0 | if ((size - len > 4) && (buf[len - 1] != '.')) |
5255 | 0 | strcat(buf, " ..."); |
5256 | 0 | return; |
5257 | 0 | } |
5258 | 0 | strcat(buf, (char *) cur->ns->prefix); |
5259 | 0 | strcat(buf, ":"); |
5260 | 0 | } |
5261 | 0 | if (size - len < xmlStrlen(cur->name) + 10) { |
5262 | 0 | if ((size - len > 4) && (buf[len - 1] != '.')) |
5263 | 0 | strcat(buf, " ..."); |
5264 | 0 | return; |
5265 | 0 | } |
5266 | 0 | strcat(buf, (char *) cur->name); |
5267 | 0 | if (cur->next != NULL) |
5268 | 0 | strcat(buf, " "); |
5269 | 0 | break; |
5270 | 0 | case XML_TEXT_NODE: |
5271 | 0 | if (xmlIsBlankNode(cur)) |
5272 | 0 | break; |
5273 | | /* Falls through. */ |
5274 | 0 | case XML_CDATA_SECTION_NODE: |
5275 | 0 | case XML_ENTITY_REF_NODE: |
5276 | 0 | strcat(buf, "CDATA"); |
5277 | 0 | if (cur->next != NULL) |
5278 | 0 | strcat(buf, " "); |
5279 | 0 | break; |
5280 | 0 | case XML_ATTRIBUTE_NODE: |
5281 | 0 | case XML_DOCUMENT_NODE: |
5282 | 0 | case XML_HTML_DOCUMENT_NODE: |
5283 | 0 | case XML_DOCUMENT_TYPE_NODE: |
5284 | 0 | case XML_DOCUMENT_FRAG_NODE: |
5285 | 0 | case XML_NOTATION_NODE: |
5286 | 0 | case XML_NAMESPACE_DECL: |
5287 | 0 | strcat(buf, "???"); |
5288 | 0 | if (cur->next != NULL) |
5289 | 0 | strcat(buf, " "); |
5290 | 0 | break; |
5291 | 0 | case XML_ENTITY_NODE: |
5292 | 0 | case XML_PI_NODE: |
5293 | 0 | case XML_DTD_NODE: |
5294 | 0 | case XML_COMMENT_NODE: |
5295 | 0 | case XML_ELEMENT_DECL: |
5296 | 0 | case XML_ATTRIBUTE_DECL: |
5297 | 0 | case XML_ENTITY_DECL: |
5298 | 0 | case XML_XINCLUDE_START: |
5299 | 0 | case XML_XINCLUDE_END: |
5300 | 0 | break; |
5301 | 0 | } |
5302 | 0 | cur = cur->next; |
5303 | 0 | } |
5304 | 0 | if (glob) strcat(buf, ")"); |
5305 | 0 | } |
5306 | | |
5307 | | /** |
5308 | | * xmlValidateElementContent: |
5309 | | * @ctxt: the validation context |
5310 | | * @child: the child list |
5311 | | * @elemDecl: pointer to the element declaration |
5312 | | * @warn: emit the error message |
5313 | | * @parent: the parent element (for error reporting) |
5314 | | * |
5315 | | * Try to validate the content model of an element |
5316 | | * |
5317 | | * returns 1 if valid or 0 if not and -1 in case of error |
5318 | | */ |
5319 | | |
5320 | | static int |
5321 | | xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child, |
5322 | 0 | xmlElementPtr elemDecl, int warn, xmlNodePtr parent) { |
5323 | 0 | int ret = 1; |
5324 | | #ifndef LIBXML_REGEXP_ENABLED |
5325 | | xmlNodePtr repl = NULL, last = NULL, tmp; |
5326 | | #endif |
5327 | 0 | xmlNodePtr cur; |
5328 | 0 | xmlElementContentPtr cont; |
5329 | 0 | const xmlChar *name; |
5330 | |
|
5331 | 0 | if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL)) |
5332 | 0 | return(-1); |
5333 | 0 | cont = elemDecl->content; |
5334 | 0 | name = elemDecl->name; |
5335 | |
|
5336 | 0 | #ifdef LIBXML_REGEXP_ENABLED |
5337 | | /* Build the regexp associated to the content model */ |
5338 | 0 | if (elemDecl->contModel == NULL) |
5339 | 0 | ret = xmlValidBuildContentModel(ctxt, elemDecl); |
5340 | 0 | if (elemDecl->contModel == NULL) { |
5341 | 0 | return(-1); |
5342 | 0 | } else { |
5343 | 0 | xmlRegExecCtxtPtr exec; |
5344 | |
|
5345 | 0 | if (!xmlRegexpIsDeterminist(elemDecl->contModel)) { |
5346 | 0 | return(-1); |
5347 | 0 | } |
5348 | 0 | ctxt->nodeMax = 0; |
5349 | 0 | ctxt->nodeNr = 0; |
5350 | 0 | ctxt->nodeTab = NULL; |
5351 | 0 | exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
5352 | 0 | if (exec != NULL) { |
5353 | 0 | cur = child; |
5354 | 0 | while (cur != NULL) { |
5355 | 0 | switch (cur->type) { |
5356 | 0 | case XML_ENTITY_REF_NODE: |
5357 | | /* |
5358 | | * Push the current node to be able to roll back |
5359 | | * and process within the entity |
5360 | | */ |
5361 | 0 | if ((cur->children != NULL) && |
5362 | 0 | (cur->children->children != NULL)) { |
5363 | 0 | nodeVPush(ctxt, cur); |
5364 | 0 | cur = cur->children->children; |
5365 | 0 | continue; |
5366 | 0 | } |
5367 | 0 | break; |
5368 | 0 | case XML_TEXT_NODE: |
5369 | 0 | if (xmlIsBlankNode(cur)) |
5370 | 0 | break; |
5371 | 0 | ret = 0; |
5372 | 0 | goto fail; |
5373 | 0 | case XML_CDATA_SECTION_NODE: |
5374 | | /* TODO */ |
5375 | 0 | ret = 0; |
5376 | 0 | goto fail; |
5377 | 0 | case XML_ELEMENT_NODE: |
5378 | 0 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
5379 | 0 | xmlChar fn[50]; |
5380 | 0 | xmlChar *fullname; |
5381 | |
|
5382 | 0 | fullname = xmlBuildQName(cur->name, |
5383 | 0 | cur->ns->prefix, fn, 50); |
5384 | 0 | if (fullname == NULL) { |
5385 | 0 | ret = -1; |
5386 | 0 | goto fail; |
5387 | 0 | } |
5388 | 0 | ret = xmlRegExecPushString(exec, fullname, NULL); |
5389 | 0 | if ((fullname != fn) && (fullname != cur->name)) |
5390 | 0 | xmlFree(fullname); |
5391 | 0 | } else { |
5392 | 0 | ret = xmlRegExecPushString(exec, cur->name, NULL); |
5393 | 0 | } |
5394 | 0 | break; |
5395 | 0 | default: |
5396 | 0 | break; |
5397 | 0 | } |
5398 | | /* |
5399 | | * Switch to next element |
5400 | | */ |
5401 | 0 | cur = cur->next; |
5402 | 0 | while (cur == NULL) { |
5403 | 0 | cur = nodeVPop(ctxt); |
5404 | 0 | if (cur == NULL) |
5405 | 0 | break; |
5406 | 0 | cur = cur->next; |
5407 | 0 | } |
5408 | 0 | } |
5409 | 0 | ret = xmlRegExecPushString(exec, NULL, NULL); |
5410 | 0 | fail: |
5411 | 0 | xmlRegFreeExecCtxt(exec); |
5412 | 0 | } |
5413 | 0 | } |
5414 | | #else /* LIBXML_REGEXP_ENABLED */ |
5415 | | /* |
5416 | | * Allocate the stack |
5417 | | */ |
5418 | | ctxt->vstateMax = 8; |
5419 | | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
5420 | | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
5421 | | if (ctxt->vstateTab == NULL) { |
5422 | | xmlVErrMemory(ctxt, "malloc failed"); |
5423 | | return(-1); |
5424 | | } |
5425 | | /* |
5426 | | * The first entry in the stack is reserved to the current state |
5427 | | */ |
5428 | | ctxt->nodeMax = 0; |
5429 | | ctxt->nodeNr = 0; |
5430 | | ctxt->nodeTab = NULL; |
5431 | | ctxt->vstate = &ctxt->vstateTab[0]; |
5432 | | ctxt->vstateNr = 1; |
5433 | | CONT = cont; |
5434 | | NODE = child; |
5435 | | DEPTH = 0; |
5436 | | OCCURS = 0; |
5437 | | STATE = 0; |
5438 | | ret = xmlValidateElementType(ctxt); |
5439 | | if ((ret == -3) && (warn)) { |
5440 | | xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST, |
5441 | | "Content model for Element %s is ambiguous\n", |
5442 | | name, NULL, NULL); |
5443 | | } else if (ret == -2) { |
5444 | | /* |
5445 | | * An entities reference appeared at this level. |
5446 | | * Build a minimal representation of this node content |
5447 | | * sufficient to run the validation process on it |
5448 | | */ |
5449 | | DEBUG_VALID_MSG("Found an entity reference, linearizing"); |
5450 | | cur = child; |
5451 | | while (cur != NULL) { |
5452 | | switch (cur->type) { |
5453 | | case XML_ENTITY_REF_NODE: |
5454 | | /* |
5455 | | * Push the current node to be able to roll back |
5456 | | * and process within the entity |
5457 | | */ |
5458 | | if ((cur->children != NULL) && |
5459 | | (cur->children->children != NULL)) { |
5460 | | nodeVPush(ctxt, cur); |
5461 | | cur = cur->children->children; |
5462 | | continue; |
5463 | | } |
5464 | | break; |
5465 | | case XML_TEXT_NODE: |
5466 | | if (xmlIsBlankNode(cur)) |
5467 | | break; |
5468 | | /* no break on purpose */ |
5469 | | case XML_CDATA_SECTION_NODE: |
5470 | | /* no break on purpose */ |
5471 | | case XML_ELEMENT_NODE: |
5472 | | /* |
5473 | | * Allocate a new node and minimally fills in |
5474 | | * what's required |
5475 | | */ |
5476 | | tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
5477 | | if (tmp == NULL) { |
5478 | | xmlVErrMemory(ctxt, "malloc failed"); |
5479 | | xmlFreeNodeList(repl); |
5480 | | ret = -1; |
5481 | | goto done; |
5482 | | } |
5483 | | tmp->type = cur->type; |
5484 | | tmp->name = cur->name; |
5485 | | tmp->ns = cur->ns; |
5486 | | tmp->next = NULL; |
5487 | | tmp->content = NULL; |
5488 | | if (repl == NULL) |
5489 | | repl = last = tmp; |
5490 | | else { |
5491 | | last->next = tmp; |
5492 | | last = tmp; |
5493 | | } |
5494 | | if (cur->type == XML_CDATA_SECTION_NODE) { |
5495 | | /* |
5496 | | * E59 spaces in CDATA does not match the |
5497 | | * nonterminal S |
5498 | | */ |
5499 | | tmp->content = xmlStrdup(BAD_CAST "CDATA"); |
5500 | | } |
5501 | | break; |
5502 | | default: |
5503 | | break; |
5504 | | } |
5505 | | /* |
5506 | | * Switch to next element |
5507 | | */ |
5508 | | cur = cur->next; |
5509 | | while (cur == NULL) { |
5510 | | cur = nodeVPop(ctxt); |
5511 | | if (cur == NULL) |
5512 | | break; |
5513 | | cur = cur->next; |
5514 | | } |
5515 | | } |
5516 | | |
5517 | | /* |
5518 | | * Relaunch the validation |
5519 | | */ |
5520 | | ctxt->vstate = &ctxt->vstateTab[0]; |
5521 | | ctxt->vstateNr = 1; |
5522 | | CONT = cont; |
5523 | | NODE = repl; |
5524 | | DEPTH = 0; |
5525 | | OCCURS = 0; |
5526 | | STATE = 0; |
5527 | | ret = xmlValidateElementType(ctxt); |
5528 | | } |
5529 | | #endif /* LIBXML_REGEXP_ENABLED */ |
5530 | 0 | if ((warn) && ((ret != 1) && (ret != -3))) { |
5531 | 0 | if (ctxt != NULL) { |
5532 | 0 | char expr[5000]; |
5533 | 0 | char list[5000]; |
5534 | |
|
5535 | 0 | expr[0] = 0; |
5536 | 0 | xmlSnprintfElementContent(&expr[0], 5000, cont, 1); |
5537 | 0 | list[0] = 0; |
5538 | | #ifndef LIBXML_REGEXP_ENABLED |
5539 | | if (repl != NULL) |
5540 | | xmlSnprintfElements(&list[0], 5000, repl, 1); |
5541 | | else |
5542 | | #endif /* LIBXML_REGEXP_ENABLED */ |
5543 | 0 | xmlSnprintfElements(&list[0], 5000, child, 1); |
5544 | |
|
5545 | 0 | if (name != NULL) { |
5546 | 0 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
5547 | 0 | "Element %s content does not follow the DTD, expecting %s, got %s\n", |
5548 | 0 | name, BAD_CAST expr, BAD_CAST list); |
5549 | 0 | } else { |
5550 | 0 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
5551 | 0 | "Element content does not follow the DTD, expecting %s, got %s\n", |
5552 | 0 | BAD_CAST expr, BAD_CAST list, NULL); |
5553 | 0 | } |
5554 | 0 | } else { |
5555 | 0 | if (name != NULL) { |
5556 | 0 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
5557 | 0 | "Element %s content does not follow the DTD\n", |
5558 | 0 | name, NULL, NULL); |
5559 | 0 | } else { |
5560 | 0 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
5561 | 0 | "Element content does not follow the DTD\n", |
5562 | 0 | NULL, NULL, NULL); |
5563 | 0 | } |
5564 | 0 | } |
5565 | 0 | ret = 0; |
5566 | 0 | } |
5567 | 0 | if (ret == -3) |
5568 | 0 | ret = 1; |
5569 | |
|
5570 | | #ifndef LIBXML_REGEXP_ENABLED |
5571 | | done: |
5572 | | /* |
5573 | | * Deallocate the copy if done, and free up the validation stack |
5574 | | */ |
5575 | | while (repl != NULL) { |
5576 | | tmp = repl->next; |
5577 | | xmlFree(repl); |
5578 | | repl = tmp; |
5579 | | } |
5580 | | ctxt->vstateMax = 0; |
5581 | | if (ctxt->vstateTab != NULL) { |
5582 | | xmlFree(ctxt->vstateTab); |
5583 | | ctxt->vstateTab = NULL; |
5584 | | } |
5585 | | #endif |
5586 | 0 | ctxt->nodeMax = 0; |
5587 | 0 | ctxt->nodeNr = 0; |
5588 | 0 | if (ctxt->nodeTab != NULL) { |
5589 | 0 | xmlFree(ctxt->nodeTab); |
5590 | 0 | ctxt->nodeTab = NULL; |
5591 | 0 | } |
5592 | 0 | return(ret); |
5593 | |
|
5594 | 0 | } |
5595 | | |
5596 | | /** |
5597 | | * xmlValidateCdataElement: |
5598 | | * @ctxt: the validation context |
5599 | | * @doc: a document instance |
5600 | | * @elem: an element instance |
5601 | | * |
5602 | | * Check that an element follows #CDATA |
5603 | | * |
5604 | | * returns 1 if valid or 0 otherwise |
5605 | | */ |
5606 | | static int |
5607 | | xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
5608 | 0 | xmlNodePtr elem) { |
5609 | 0 | int ret = 1; |
5610 | 0 | xmlNodePtr cur, child; |
5611 | |
|
5612 | 0 | if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) || |
5613 | 0 | (elem->type != XML_ELEMENT_NODE)) |
5614 | 0 | return(0); |
5615 | | |
5616 | 0 | child = elem->children; |
5617 | |
|
5618 | 0 | cur = child; |
5619 | 0 | while (cur != NULL) { |
5620 | 0 | switch (cur->type) { |
5621 | 0 | case XML_ENTITY_REF_NODE: |
5622 | | /* |
5623 | | * Push the current node to be able to roll back |
5624 | | * and process within the entity |
5625 | | */ |
5626 | 0 | if ((cur->children != NULL) && |
5627 | 0 | (cur->children->children != NULL)) { |
5628 | 0 | nodeVPush(ctxt, cur); |
5629 | 0 | cur = cur->children->children; |
5630 | 0 | continue; |
5631 | 0 | } |
5632 | 0 | break; |
5633 | 0 | case XML_COMMENT_NODE: |
5634 | 0 | case XML_PI_NODE: |
5635 | 0 | case XML_TEXT_NODE: |
5636 | 0 | case XML_CDATA_SECTION_NODE: |
5637 | 0 | break; |
5638 | 0 | default: |
5639 | 0 | ret = 0; |
5640 | 0 | goto done; |
5641 | 0 | } |
5642 | | /* |
5643 | | * Switch to next element |
5644 | | */ |
5645 | 0 | cur = cur->next; |
5646 | 0 | while (cur == NULL) { |
5647 | 0 | cur = nodeVPop(ctxt); |
5648 | 0 | if (cur == NULL) |
5649 | 0 | break; |
5650 | 0 | cur = cur->next; |
5651 | 0 | } |
5652 | 0 | } |
5653 | 0 | done: |
5654 | 0 | ctxt->nodeMax = 0; |
5655 | 0 | ctxt->nodeNr = 0; |
5656 | 0 | if (ctxt->nodeTab != NULL) { |
5657 | 0 | xmlFree(ctxt->nodeTab); |
5658 | 0 | ctxt->nodeTab = NULL; |
5659 | 0 | } |
5660 | 0 | return(ret); |
5661 | 0 | } |
5662 | | |
5663 | | /** |
5664 | | * xmlValidateCheckMixed: |
5665 | | * @ctxt: the validation context |
5666 | | * @cont: the mixed content model |
5667 | | * @qname: the qualified name as appearing in the serialization |
5668 | | * |
5669 | | * Check if the given node is part of the content model. |
5670 | | * |
5671 | | * Returns 1 if yes, 0 if no, -1 in case of error |
5672 | | */ |
5673 | | static int |
5674 | | xmlValidateCheckMixed(xmlValidCtxtPtr ctxt, |
5675 | 0 | xmlElementContentPtr cont, const xmlChar *qname) { |
5676 | 0 | const xmlChar *name; |
5677 | 0 | int plen; |
5678 | 0 | name = xmlSplitQName3(qname, &plen); |
5679 | |
|
5680 | 0 | if (name == NULL) { |
5681 | 0 | while (cont != NULL) { |
5682 | 0 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
5683 | 0 | if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname))) |
5684 | 0 | return(1); |
5685 | 0 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
5686 | 0 | (cont->c1 != NULL) && |
5687 | 0 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
5688 | 0 | if ((cont->c1->prefix == NULL) && |
5689 | 0 | (xmlStrEqual(cont->c1->name, qname))) |
5690 | 0 | return(1); |
5691 | 0 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
5692 | 0 | (cont->c1 == NULL) || |
5693 | 0 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
5694 | 0 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
5695 | 0 | "Internal: MIXED struct corrupted\n", |
5696 | 0 | NULL); |
5697 | 0 | break; |
5698 | 0 | } |
5699 | 0 | cont = cont->c2; |
5700 | 0 | } |
5701 | 0 | } else { |
5702 | 0 | while (cont != NULL) { |
5703 | 0 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
5704 | 0 | if ((cont->prefix != NULL) && |
5705 | 0 | (xmlStrncmp(cont->prefix, qname, plen) == 0) && |
5706 | 0 | (xmlStrEqual(cont->name, name))) |
5707 | 0 | return(1); |
5708 | 0 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
5709 | 0 | (cont->c1 != NULL) && |
5710 | 0 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
5711 | 0 | if ((cont->c1->prefix != NULL) && |
5712 | 0 | (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) && |
5713 | 0 | (xmlStrEqual(cont->c1->name, name))) |
5714 | 0 | return(1); |
5715 | 0 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
5716 | 0 | (cont->c1 == NULL) || |
5717 | 0 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
5718 | 0 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
5719 | 0 | "Internal: MIXED struct corrupted\n", |
5720 | 0 | NULL); |
5721 | 0 | break; |
5722 | 0 | } |
5723 | 0 | cont = cont->c2; |
5724 | 0 | } |
5725 | 0 | } |
5726 | 0 | return(0); |
5727 | 0 | } |
5728 | | |
5729 | | /** |
5730 | | * xmlValidGetElemDecl: |
5731 | | * @ctxt: the validation context |
5732 | | * @doc: a document instance |
5733 | | * @elem: an element instance |
5734 | | * @extsubset: pointer, (out) indicate if the declaration was found |
5735 | | * in the external subset. |
5736 | | * |
5737 | | * Finds a declaration associated to an element in the document. |
5738 | | * |
5739 | | * returns the pointer to the declaration or NULL if not found. |
5740 | | */ |
5741 | | static xmlElementPtr |
5742 | | xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
5743 | 0 | xmlNodePtr elem, int *extsubset) { |
5744 | 0 | xmlElementPtr elemDecl = NULL; |
5745 | 0 | const xmlChar *prefix = NULL; |
5746 | |
|
5747 | 0 | if ((ctxt == NULL) || (doc == NULL) || |
5748 | 0 | (elem == NULL) || (elem->name == NULL)) |
5749 | 0 | return(NULL); |
5750 | 0 | if (extsubset != NULL) |
5751 | 0 | *extsubset = 0; |
5752 | | |
5753 | | /* |
5754 | | * Fetch the declaration for the qualified name |
5755 | | */ |
5756 | 0 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) |
5757 | 0 | prefix = elem->ns->prefix; |
5758 | |
|
5759 | 0 | if (prefix != NULL) { |
5760 | 0 | elemDecl = xmlGetDtdQElementDesc(doc->intSubset, |
5761 | 0 | elem->name, prefix); |
5762 | 0 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
5763 | 0 | elemDecl = xmlGetDtdQElementDesc(doc->extSubset, |
5764 | 0 | elem->name, prefix); |
5765 | 0 | if ((elemDecl != NULL) && (extsubset != NULL)) |
5766 | 0 | *extsubset = 1; |
5767 | 0 | } |
5768 | 0 | } |
5769 | | |
5770 | | /* |
5771 | | * Fetch the declaration for the non qualified name |
5772 | | * This is "non-strict" validation should be done on the |
5773 | | * full QName but in that case being flexible makes sense. |
5774 | | */ |
5775 | 0 | if (elemDecl == NULL) { |
5776 | 0 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
5777 | 0 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
5778 | 0 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
5779 | 0 | if ((elemDecl != NULL) && (extsubset != NULL)) |
5780 | 0 | *extsubset = 1; |
5781 | 0 | } |
5782 | 0 | } |
5783 | 0 | if (elemDecl == NULL) { |
5784 | 0 | xmlErrValidNode(ctxt, elem, |
5785 | 0 | XML_DTD_UNKNOWN_ELEM, |
5786 | 0 | "No declaration for element %s\n", |
5787 | 0 | elem->name, NULL, NULL); |
5788 | 0 | } |
5789 | 0 | return(elemDecl); |
5790 | 0 | } |
5791 | | |
5792 | | #ifdef LIBXML_REGEXP_ENABLED |
5793 | | /** |
5794 | | * xmlValidatePushElement: |
5795 | | * @ctxt: the validation context |
5796 | | * @doc: a document instance |
5797 | | * @elem: an element instance |
5798 | | * @qname: the qualified name as appearing in the serialization |
5799 | | * |
5800 | | * Push a new element start on the validation stack. |
5801 | | * |
5802 | | * returns 1 if no validation problem was found or 0 otherwise |
5803 | | */ |
5804 | | int |
5805 | | xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
5806 | 0 | xmlNodePtr elem, const xmlChar *qname) { |
5807 | 0 | int ret = 1; |
5808 | 0 | xmlElementPtr eDecl; |
5809 | 0 | int extsubset = 0; |
5810 | |
|
5811 | 0 | if (ctxt == NULL) |
5812 | 0 | return(0); |
5813 | | /* printf("PushElem %s\n", qname); */ |
5814 | 0 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
5815 | 0 | xmlValidStatePtr state = ctxt->vstate; |
5816 | 0 | xmlElementPtr elemDecl; |
5817 | | |
5818 | | /* |
5819 | | * Check the new element against the content model of the new elem. |
5820 | | */ |
5821 | 0 | if (state->elemDecl != NULL) { |
5822 | 0 | elemDecl = state->elemDecl; |
5823 | |
|
5824 | 0 | switch(elemDecl->etype) { |
5825 | 0 | case XML_ELEMENT_TYPE_UNDEFINED: |
5826 | 0 | ret = 0; |
5827 | 0 | break; |
5828 | 0 | case XML_ELEMENT_TYPE_EMPTY: |
5829 | 0 | xmlErrValidNode(ctxt, state->node, |
5830 | 0 | XML_DTD_NOT_EMPTY, |
5831 | 0 | "Element %s was declared EMPTY this one has content\n", |
5832 | 0 | state->node->name, NULL, NULL); |
5833 | 0 | ret = 0; |
5834 | 0 | break; |
5835 | 0 | case XML_ELEMENT_TYPE_ANY: |
5836 | | /* I don't think anything is required then */ |
5837 | 0 | break; |
5838 | 0 | case XML_ELEMENT_TYPE_MIXED: |
5839 | | /* simple case of declared as #PCDATA */ |
5840 | 0 | if ((elemDecl->content != NULL) && |
5841 | 0 | (elemDecl->content->type == |
5842 | 0 | XML_ELEMENT_CONTENT_PCDATA)) { |
5843 | 0 | xmlErrValidNode(ctxt, state->node, |
5844 | 0 | XML_DTD_NOT_PCDATA, |
5845 | 0 | "Element %s was declared #PCDATA but contains non text nodes\n", |
5846 | 0 | state->node->name, NULL, NULL); |
5847 | 0 | ret = 0; |
5848 | 0 | } else { |
5849 | 0 | ret = xmlValidateCheckMixed(ctxt, elemDecl->content, |
5850 | 0 | qname); |
5851 | 0 | if (ret != 1) { |
5852 | 0 | xmlErrValidNode(ctxt, state->node, |
5853 | 0 | XML_DTD_INVALID_CHILD, |
5854 | 0 | "Element %s is not declared in %s list of possible children\n", |
5855 | 0 | qname, state->node->name, NULL); |
5856 | 0 | } |
5857 | 0 | } |
5858 | 0 | break; |
5859 | 0 | case XML_ELEMENT_TYPE_ELEMENT: |
5860 | | /* |
5861 | | * TODO: |
5862 | | * VC: Standalone Document Declaration |
5863 | | * - element types with element content, if white space |
5864 | | * occurs directly within any instance of those types. |
5865 | | */ |
5866 | 0 | if (state->exec != NULL) { |
5867 | 0 | ret = xmlRegExecPushString(state->exec, qname, NULL); |
5868 | 0 | if (ret < 0) { |
5869 | 0 | xmlErrValidNode(ctxt, state->node, |
5870 | 0 | XML_DTD_CONTENT_MODEL, |
5871 | 0 | "Element %s content does not follow the DTD, Misplaced %s\n", |
5872 | 0 | state->node->name, qname, NULL); |
5873 | 0 | ret = 0; |
5874 | 0 | } else { |
5875 | 0 | ret = 1; |
5876 | 0 | } |
5877 | 0 | } |
5878 | 0 | break; |
5879 | 0 | } |
5880 | 0 | } |
5881 | 0 | } |
5882 | 0 | eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
5883 | 0 | vstateVPush(ctxt, eDecl, elem); |
5884 | 0 | return(ret); |
5885 | 0 | } |
5886 | | |
5887 | | /** |
5888 | | * xmlValidatePushCData: |
5889 | | * @ctxt: the validation context |
5890 | | * @data: some character data read |
5891 | | * @len: the length of the data |
5892 | | * |
5893 | | * check the CData parsed for validation in the current stack |
5894 | | * |
5895 | | * returns 1 if no validation problem was found or 0 otherwise |
5896 | | */ |
5897 | | int |
5898 | 0 | xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) { |
5899 | 0 | int ret = 1; |
5900 | | |
5901 | | /* printf("CDATA %s %d\n", data, len); */ |
5902 | 0 | if (ctxt == NULL) |
5903 | 0 | return(0); |
5904 | 0 | if (len <= 0) |
5905 | 0 | return(ret); |
5906 | 0 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
5907 | 0 | xmlValidStatePtr state = ctxt->vstate; |
5908 | 0 | xmlElementPtr elemDecl; |
5909 | | |
5910 | | /* |
5911 | | * Check the new element against the content model of the new elem. |
5912 | | */ |
5913 | 0 | if (state->elemDecl != NULL) { |
5914 | 0 | elemDecl = state->elemDecl; |
5915 | |
|
5916 | 0 | switch(elemDecl->etype) { |
5917 | 0 | case XML_ELEMENT_TYPE_UNDEFINED: |
5918 | 0 | ret = 0; |
5919 | 0 | break; |
5920 | 0 | case XML_ELEMENT_TYPE_EMPTY: |
5921 | 0 | xmlErrValidNode(ctxt, state->node, |
5922 | 0 | XML_DTD_NOT_EMPTY, |
5923 | 0 | "Element %s was declared EMPTY this one has content\n", |
5924 | 0 | state->node->name, NULL, NULL); |
5925 | 0 | ret = 0; |
5926 | 0 | break; |
5927 | 0 | case XML_ELEMENT_TYPE_ANY: |
5928 | 0 | break; |
5929 | 0 | case XML_ELEMENT_TYPE_MIXED: |
5930 | 0 | break; |
5931 | 0 | case XML_ELEMENT_TYPE_ELEMENT: { |
5932 | 0 | int i; |
5933 | |
|
5934 | 0 | for (i = 0;i < len;i++) { |
5935 | 0 | if (!IS_BLANK_CH(data[i])) { |
5936 | 0 | xmlErrValidNode(ctxt, state->node, |
5937 | 0 | XML_DTD_CONTENT_MODEL, |
5938 | 0 | "Element %s content does not follow the DTD, Text not allowed\n", |
5939 | 0 | state->node->name, NULL, NULL); |
5940 | 0 | ret = 0; |
5941 | 0 | goto done; |
5942 | 0 | } |
5943 | 0 | } |
5944 | | /* |
5945 | | * TODO: |
5946 | | * VC: Standalone Document Declaration |
5947 | | * element types with element content, if white space |
5948 | | * occurs directly within any instance of those types. |
5949 | | */ |
5950 | 0 | break; |
5951 | 0 | } |
5952 | 0 | } |
5953 | 0 | } |
5954 | 0 | } |
5955 | 0 | done: |
5956 | 0 | return(ret); |
5957 | 0 | } |
5958 | | |
5959 | | /** |
5960 | | * xmlValidatePopElement: |
5961 | | * @ctxt: the validation context |
5962 | | * @doc: a document instance |
5963 | | * @elem: an element instance |
5964 | | * @qname: the qualified name as appearing in the serialization |
5965 | | * |
5966 | | * Pop the element end from the validation stack. |
5967 | | * |
5968 | | * returns 1 if no validation problem was found or 0 otherwise |
5969 | | */ |
5970 | | int |
5971 | | xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED, |
5972 | | xmlNodePtr elem ATTRIBUTE_UNUSED, |
5973 | 0 | const xmlChar *qname ATTRIBUTE_UNUSED) { |
5974 | 0 | int ret = 1; |
5975 | |
|
5976 | 0 | if (ctxt == NULL) |
5977 | 0 | return(0); |
5978 | | /* printf("PopElem %s\n", qname); */ |
5979 | 0 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
5980 | 0 | xmlValidStatePtr state = ctxt->vstate; |
5981 | 0 | xmlElementPtr elemDecl; |
5982 | | |
5983 | | /* |
5984 | | * Check the new element against the content model of the new elem. |
5985 | | */ |
5986 | 0 | if (state->elemDecl != NULL) { |
5987 | 0 | elemDecl = state->elemDecl; |
5988 | |
|
5989 | 0 | if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) { |
5990 | 0 | if (state->exec != NULL) { |
5991 | 0 | ret = xmlRegExecPushString(state->exec, NULL, NULL); |
5992 | 0 | if (ret == 0) { |
5993 | 0 | xmlErrValidNode(ctxt, state->node, |
5994 | 0 | XML_DTD_CONTENT_MODEL, |
5995 | 0 | "Element %s content does not follow the DTD, Expecting more child\n", |
5996 | 0 | state->node->name, NULL,NULL); |
5997 | 0 | } else { |
5998 | | /* |
5999 | | * previous validation errors should not generate |
6000 | | * a new one here |
6001 | | */ |
6002 | 0 | ret = 1; |
6003 | 0 | } |
6004 | 0 | } |
6005 | 0 | } |
6006 | 0 | } |
6007 | 0 | vstateVPop(ctxt); |
6008 | 0 | } |
6009 | 0 | return(ret); |
6010 | 0 | } |
6011 | | #endif /* LIBXML_REGEXP_ENABLED */ |
6012 | | |
6013 | | /** |
6014 | | * xmlValidateOneElement: |
6015 | | * @ctxt: the validation context |
6016 | | * @doc: a document instance |
6017 | | * @elem: an element instance |
6018 | | * |
6019 | | * Try to validate a single element and it's attributes, |
6020 | | * basically it does the following checks as described by the |
6021 | | * XML-1.0 recommendation: |
6022 | | * - [ VC: Element Valid ] |
6023 | | * - [ VC: Required Attribute ] |
6024 | | * Then call xmlValidateOneAttribute() for each attribute present. |
6025 | | * |
6026 | | * The ID/IDREF checkings are done separately |
6027 | | * |
6028 | | * returns 1 if valid or 0 otherwise |
6029 | | */ |
6030 | | |
6031 | | int |
6032 | | xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
6033 | 0 | xmlNodePtr elem) { |
6034 | 0 | xmlElementPtr elemDecl = NULL; |
6035 | 0 | xmlElementContentPtr cont; |
6036 | 0 | xmlAttributePtr attr; |
6037 | 0 | xmlNodePtr child; |
6038 | 0 | int ret = 1, tmp; |
6039 | 0 | const xmlChar *name; |
6040 | 0 | int extsubset = 0; |
6041 | |
|
6042 | 0 | CHECK_DTD; |
6043 | | |
6044 | 0 | if (elem == NULL) return(0); |
6045 | 0 | switch (elem->type) { |
6046 | 0 | case XML_ATTRIBUTE_NODE: |
6047 | 0 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
6048 | 0 | "Attribute element not expected\n", NULL, NULL ,NULL); |
6049 | 0 | return(0); |
6050 | 0 | case XML_TEXT_NODE: |
6051 | 0 | if (elem->children != NULL) { |
6052 | 0 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
6053 | 0 | "Text element has children !\n", |
6054 | 0 | NULL,NULL,NULL); |
6055 | 0 | return(0); |
6056 | 0 | } |
6057 | 0 | if (elem->ns != NULL) { |
6058 | 0 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
6059 | 0 | "Text element has namespace !\n", |
6060 | 0 | NULL,NULL,NULL); |
6061 | 0 | return(0); |
6062 | 0 | } |
6063 | 0 | if (elem->content == NULL) { |
6064 | 0 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
6065 | 0 | "Text element has no content !\n", |
6066 | 0 | NULL,NULL,NULL); |
6067 | 0 | return(0); |
6068 | 0 | } |
6069 | 0 | return(1); |
6070 | 0 | case XML_XINCLUDE_START: |
6071 | 0 | case XML_XINCLUDE_END: |
6072 | 0 | return(1); |
6073 | 0 | case XML_CDATA_SECTION_NODE: |
6074 | 0 | case XML_ENTITY_REF_NODE: |
6075 | 0 | case XML_PI_NODE: |
6076 | 0 | case XML_COMMENT_NODE: |
6077 | 0 | return(1); |
6078 | 0 | case XML_ENTITY_NODE: |
6079 | 0 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
6080 | 0 | "Entity element not expected\n", NULL, NULL ,NULL); |
6081 | 0 | return(0); |
6082 | 0 | case XML_NOTATION_NODE: |
6083 | 0 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
6084 | 0 | "Notation element not expected\n", NULL, NULL ,NULL); |
6085 | 0 | return(0); |
6086 | 0 | case XML_DOCUMENT_NODE: |
6087 | 0 | case XML_DOCUMENT_TYPE_NODE: |
6088 | 0 | case XML_DOCUMENT_FRAG_NODE: |
6089 | 0 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
6090 | 0 | "Document element not expected\n", NULL, NULL ,NULL); |
6091 | 0 | return(0); |
6092 | 0 | case XML_HTML_DOCUMENT_NODE: |
6093 | 0 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
6094 | 0 | "HTML Document not expected\n", NULL, NULL ,NULL); |
6095 | 0 | return(0); |
6096 | 0 | case XML_ELEMENT_NODE: |
6097 | 0 | break; |
6098 | 0 | default: |
6099 | 0 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
6100 | 0 | "unknown element type\n", NULL, NULL ,NULL); |
6101 | 0 | return(0); |
6102 | 0 | } |
6103 | | |
6104 | | /* |
6105 | | * Fetch the declaration |
6106 | | */ |
6107 | 0 | elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
6108 | 0 | if (elemDecl == NULL) |
6109 | 0 | return(0); |
6110 | | |
6111 | | /* |
6112 | | * If vstateNr is not zero that means continuous validation is |
6113 | | * activated, do not try to check the content model at that level. |
6114 | | */ |
6115 | 0 | if (ctxt->vstateNr == 0) { |
6116 | | /* Check that the element content matches the definition */ |
6117 | 0 | switch (elemDecl->etype) { |
6118 | 0 | case XML_ELEMENT_TYPE_UNDEFINED: |
6119 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM, |
6120 | 0 | "No declaration for element %s\n", |
6121 | 0 | elem->name, NULL, NULL); |
6122 | 0 | return(0); |
6123 | 0 | case XML_ELEMENT_TYPE_EMPTY: |
6124 | 0 | if (elem->children != NULL) { |
6125 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY, |
6126 | 0 | "Element %s was declared EMPTY this one has content\n", |
6127 | 0 | elem->name, NULL, NULL); |
6128 | 0 | ret = 0; |
6129 | 0 | } |
6130 | 0 | break; |
6131 | 0 | case XML_ELEMENT_TYPE_ANY: |
6132 | | /* I don't think anything is required then */ |
6133 | 0 | break; |
6134 | 0 | case XML_ELEMENT_TYPE_MIXED: |
6135 | | |
6136 | | /* simple case of declared as #PCDATA */ |
6137 | 0 | if ((elemDecl->content != NULL) && |
6138 | 0 | (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) { |
6139 | 0 | ret = xmlValidateOneCdataElement(ctxt, doc, elem); |
6140 | 0 | if (!ret) { |
6141 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA, |
6142 | 0 | "Element %s was declared #PCDATA but contains non text nodes\n", |
6143 | 0 | elem->name, NULL, NULL); |
6144 | 0 | } |
6145 | 0 | break; |
6146 | 0 | } |
6147 | 0 | child = elem->children; |
6148 | | /* Hum, this start to get messy */ |
6149 | 0 | while (child != NULL) { |
6150 | 0 | if (child->type == XML_ELEMENT_NODE) { |
6151 | 0 | name = child->name; |
6152 | 0 | if ((child->ns != NULL) && (child->ns->prefix != NULL)) { |
6153 | 0 | xmlChar fn[50]; |
6154 | 0 | xmlChar *fullname; |
6155 | |
|
6156 | 0 | fullname = xmlBuildQName(child->name, child->ns->prefix, |
6157 | 0 | fn, 50); |
6158 | 0 | if (fullname == NULL) |
6159 | 0 | return(0); |
6160 | 0 | cont = elemDecl->content; |
6161 | 0 | while (cont != NULL) { |
6162 | 0 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
6163 | 0 | if (xmlStrEqual(cont->name, fullname)) |
6164 | 0 | break; |
6165 | 0 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
6166 | 0 | (cont->c1 != NULL) && |
6167 | 0 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
6168 | 0 | if (xmlStrEqual(cont->c1->name, fullname)) |
6169 | 0 | break; |
6170 | 0 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
6171 | 0 | (cont->c1 == NULL) || |
6172 | 0 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
6173 | 0 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
6174 | 0 | "Internal: MIXED struct corrupted\n", |
6175 | 0 | NULL); |
6176 | 0 | break; |
6177 | 0 | } |
6178 | 0 | cont = cont->c2; |
6179 | 0 | } |
6180 | 0 | if ((fullname != fn) && (fullname != child->name)) |
6181 | 0 | xmlFree(fullname); |
6182 | 0 | if (cont != NULL) |
6183 | 0 | goto child_ok; |
6184 | 0 | } |
6185 | 0 | cont = elemDecl->content; |
6186 | 0 | while (cont != NULL) { |
6187 | 0 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
6188 | 0 | if (xmlStrEqual(cont->name, name)) break; |
6189 | 0 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
6190 | 0 | (cont->c1 != NULL) && |
6191 | 0 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) { |
6192 | 0 | if (xmlStrEqual(cont->c1->name, name)) break; |
6193 | 0 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
6194 | 0 | (cont->c1 == NULL) || |
6195 | 0 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) { |
6196 | 0 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
6197 | 0 | "Internal: MIXED struct corrupted\n", |
6198 | 0 | NULL); |
6199 | 0 | break; |
6200 | 0 | } |
6201 | 0 | cont = cont->c2; |
6202 | 0 | } |
6203 | 0 | if (cont == NULL) { |
6204 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD, |
6205 | 0 | "Element %s is not declared in %s list of possible children\n", |
6206 | 0 | name, elem->name, NULL); |
6207 | 0 | ret = 0; |
6208 | 0 | } |
6209 | 0 | } |
6210 | 0 | child_ok: |
6211 | 0 | child = child->next; |
6212 | 0 | } |
6213 | 0 | break; |
6214 | 0 | case XML_ELEMENT_TYPE_ELEMENT: |
6215 | 0 | if ((doc->standalone == 1) && (extsubset == 1)) { |
6216 | | /* |
6217 | | * VC: Standalone Document Declaration |
6218 | | * - element types with element content, if white space |
6219 | | * occurs directly within any instance of those types. |
6220 | | */ |
6221 | 0 | child = elem->children; |
6222 | 0 | while (child != NULL) { |
6223 | 0 | if (child->type == XML_TEXT_NODE) { |
6224 | 0 | const xmlChar *content = child->content; |
6225 | |
|
6226 | 0 | while (IS_BLANK_CH(*content)) |
6227 | 0 | content++; |
6228 | 0 | if (*content == 0) { |
6229 | 0 | xmlErrValidNode(ctxt, elem, |
6230 | 0 | XML_DTD_STANDALONE_WHITE_SPACE, |
6231 | 0 | "standalone: %s declared in the external subset contains white spaces nodes\n", |
6232 | 0 | elem->name, NULL, NULL); |
6233 | 0 | ret = 0; |
6234 | 0 | break; |
6235 | 0 | } |
6236 | 0 | } |
6237 | 0 | child =child->next; |
6238 | 0 | } |
6239 | 0 | } |
6240 | 0 | child = elem->children; |
6241 | 0 | cont = elemDecl->content; |
6242 | 0 | tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem); |
6243 | 0 | if (tmp <= 0) |
6244 | 0 | ret = tmp; |
6245 | 0 | break; |
6246 | 0 | } |
6247 | 0 | } /* not continuous */ |
6248 | | |
6249 | | /* [ VC: Required Attribute ] */ |
6250 | 0 | attr = elemDecl->attributes; |
6251 | 0 | while (attr != NULL) { |
6252 | 0 | if (attr->def == XML_ATTRIBUTE_REQUIRED) { |
6253 | 0 | int qualified = -1; |
6254 | |
|
6255 | 0 | if ((attr->prefix == NULL) && |
6256 | 0 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
6257 | 0 | xmlNsPtr ns; |
6258 | |
|
6259 | 0 | ns = elem->nsDef; |
6260 | 0 | while (ns != NULL) { |
6261 | 0 | if (ns->prefix == NULL) |
6262 | 0 | goto found; |
6263 | 0 | ns = ns->next; |
6264 | 0 | } |
6265 | 0 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
6266 | 0 | xmlNsPtr ns; |
6267 | |
|
6268 | 0 | ns = elem->nsDef; |
6269 | 0 | while (ns != NULL) { |
6270 | 0 | if (xmlStrEqual(attr->name, ns->prefix)) |
6271 | 0 | goto found; |
6272 | 0 | ns = ns->next; |
6273 | 0 | } |
6274 | 0 | } else { |
6275 | 0 | xmlAttrPtr attrib; |
6276 | |
|
6277 | 0 | attrib = elem->properties; |
6278 | 0 | while (attrib != NULL) { |
6279 | 0 | if (xmlStrEqual(attrib->name, attr->name)) { |
6280 | 0 | if (attr->prefix != NULL) { |
6281 | 0 | xmlNsPtr nameSpace = attrib->ns; |
6282 | |
|
6283 | 0 | if (nameSpace == NULL) |
6284 | 0 | nameSpace = elem->ns; |
6285 | | /* |
6286 | | * qualified names handling is problematic, having a |
6287 | | * different prefix should be possible but DTDs don't |
6288 | | * allow to define the URI instead of the prefix :-( |
6289 | | */ |
6290 | 0 | if (nameSpace == NULL) { |
6291 | 0 | if (qualified < 0) |
6292 | 0 | qualified = 0; |
6293 | 0 | } else if (!xmlStrEqual(nameSpace->prefix, |
6294 | 0 | attr->prefix)) { |
6295 | 0 | if (qualified < 1) |
6296 | 0 | qualified = 1; |
6297 | 0 | } else |
6298 | 0 | goto found; |
6299 | 0 | } else { |
6300 | | /* |
6301 | | * We should allow applications to define namespaces |
6302 | | * for their application even if the DTD doesn't |
6303 | | * carry one, otherwise, basically we would always |
6304 | | * break. |
6305 | | */ |
6306 | 0 | goto found; |
6307 | 0 | } |
6308 | 0 | } |
6309 | 0 | attrib = attrib->next; |
6310 | 0 | } |
6311 | 0 | } |
6312 | 0 | if (qualified == -1) { |
6313 | 0 | if (attr->prefix == NULL) { |
6314 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
6315 | 0 | "Element %s does not carry attribute %s\n", |
6316 | 0 | elem->name, attr->name, NULL); |
6317 | 0 | ret = 0; |
6318 | 0 | } else { |
6319 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
6320 | 0 | "Element %s does not carry attribute %s:%s\n", |
6321 | 0 | elem->name, attr->prefix,attr->name); |
6322 | 0 | ret = 0; |
6323 | 0 | } |
6324 | 0 | } else if (qualified == 0) { |
6325 | 0 | xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX, |
6326 | 0 | "Element %s required attribute %s:%s has no prefix\n", |
6327 | 0 | elem->name, attr->prefix, attr->name); |
6328 | 0 | } else if (qualified == 1) { |
6329 | 0 | xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX, |
6330 | 0 | "Element %s required attribute %s:%s has different prefix\n", |
6331 | 0 | elem->name, attr->prefix, attr->name); |
6332 | 0 | } |
6333 | 0 | } else if (attr->def == XML_ATTRIBUTE_FIXED) { |
6334 | | /* |
6335 | | * Special tests checking #FIXED namespace declarations |
6336 | | * have the right value since this is not done as an |
6337 | | * attribute checking |
6338 | | */ |
6339 | 0 | if ((attr->prefix == NULL) && |
6340 | 0 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
6341 | 0 | xmlNsPtr ns; |
6342 | |
|
6343 | 0 | ns = elem->nsDef; |
6344 | 0 | while (ns != NULL) { |
6345 | 0 | if (ns->prefix == NULL) { |
6346 | 0 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
6347 | 0 | xmlErrValidNode(ctxt, elem, |
6348 | 0 | XML_DTD_ELEM_DEFAULT_NAMESPACE, |
6349 | 0 | "Element %s namespace name for default namespace does not match the DTD\n", |
6350 | 0 | elem->name, NULL, NULL); |
6351 | 0 | ret = 0; |
6352 | 0 | } |
6353 | 0 | goto found; |
6354 | 0 | } |
6355 | 0 | ns = ns->next; |
6356 | 0 | } |
6357 | 0 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
6358 | 0 | xmlNsPtr ns; |
6359 | |
|
6360 | 0 | ns = elem->nsDef; |
6361 | 0 | while (ns != NULL) { |
6362 | 0 | if (xmlStrEqual(attr->name, ns->prefix)) { |
6363 | 0 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
6364 | 0 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
6365 | 0 | "Element %s namespace name for %s does not match the DTD\n", |
6366 | 0 | elem->name, ns->prefix, NULL); |
6367 | 0 | ret = 0; |
6368 | 0 | } |
6369 | 0 | goto found; |
6370 | 0 | } |
6371 | 0 | ns = ns->next; |
6372 | 0 | } |
6373 | 0 | } |
6374 | 0 | } |
6375 | 0 | found: |
6376 | 0 | attr = attr->nexth; |
6377 | 0 | } |
6378 | 0 | return(ret); |
6379 | 0 | } |
6380 | | |
6381 | | /** |
6382 | | * xmlValidateRoot: |
6383 | | * @ctxt: the validation context |
6384 | | * @doc: a document instance |
6385 | | * |
6386 | | * Try to validate a the root element |
6387 | | * basically it does the following check as described by the |
6388 | | * XML-1.0 recommendation: |
6389 | | * - [ VC: Root Element Type ] |
6390 | | * it doesn't try to recurse or apply other check to the element |
6391 | | * |
6392 | | * returns 1 if valid or 0 otherwise |
6393 | | */ |
6394 | | |
6395 | | int |
6396 | 0 | xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
6397 | 0 | xmlNodePtr root; |
6398 | 0 | int ret; |
6399 | |
|
6400 | 0 | if (doc == NULL) return(0); |
6401 | | |
6402 | 0 | root = xmlDocGetRootElement(doc); |
6403 | 0 | if ((root == NULL) || (root->name == NULL)) { |
6404 | 0 | xmlErrValid(ctxt, XML_DTD_NO_ROOT, |
6405 | 0 | "no root element\n", NULL); |
6406 | 0 | return(0); |
6407 | 0 | } |
6408 | | |
6409 | | /* |
6410 | | * When doing post validation against a separate DTD, those may |
6411 | | * no internal subset has been generated |
6412 | | */ |
6413 | 0 | if ((doc->intSubset != NULL) && |
6414 | 0 | (doc->intSubset->name != NULL)) { |
6415 | | /* |
6416 | | * Check first the document root against the NQName |
6417 | | */ |
6418 | 0 | if (!xmlStrEqual(doc->intSubset->name, root->name)) { |
6419 | 0 | if ((root->ns != NULL) && (root->ns->prefix != NULL)) { |
6420 | 0 | xmlChar fn[50]; |
6421 | 0 | xmlChar *fullname; |
6422 | |
|
6423 | 0 | fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50); |
6424 | 0 | if (fullname == NULL) { |
6425 | 0 | xmlVErrMemory(ctxt, NULL); |
6426 | 0 | return(0); |
6427 | 0 | } |
6428 | 0 | ret = xmlStrEqual(doc->intSubset->name, fullname); |
6429 | 0 | if ((fullname != fn) && (fullname != root->name)) |
6430 | 0 | xmlFree(fullname); |
6431 | 0 | if (ret == 1) |
6432 | 0 | goto name_ok; |
6433 | 0 | } |
6434 | 0 | if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) && |
6435 | 0 | (xmlStrEqual(root->name, BAD_CAST "html"))) |
6436 | 0 | goto name_ok; |
6437 | 0 | xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME, |
6438 | 0 | "root and DTD name do not match '%s' and '%s'\n", |
6439 | 0 | root->name, doc->intSubset->name, NULL); |
6440 | 0 | return(0); |
6441 | 0 | } |
6442 | 0 | } |
6443 | 0 | name_ok: |
6444 | 0 | return(1); |
6445 | 0 | } |
6446 | | |
6447 | | |
6448 | | /** |
6449 | | * xmlValidateElement: |
6450 | | * @ctxt: the validation context |
6451 | | * @doc: a document instance |
6452 | | * @elem: an element instance |
6453 | | * |
6454 | | * Try to validate the subtree under an element |
6455 | | * |
6456 | | * returns 1 if valid or 0 otherwise |
6457 | | */ |
6458 | | |
6459 | | int |
6460 | 0 | xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) { |
6461 | 0 | xmlNodePtr child; |
6462 | 0 | xmlAttrPtr attr; |
6463 | 0 | xmlNsPtr ns; |
6464 | 0 | const xmlChar *value; |
6465 | 0 | int ret = 1; |
6466 | |
|
6467 | 0 | if (elem == NULL) return(0); |
6468 | | |
6469 | | /* |
6470 | | * XInclude elements were added after parsing in the infoset, |
6471 | | * they don't really mean anything validation wise. |
6472 | | */ |
6473 | 0 | if ((elem->type == XML_XINCLUDE_START) || |
6474 | 0 | (elem->type == XML_XINCLUDE_END) || |
6475 | 0 | (elem->type == XML_NAMESPACE_DECL)) |
6476 | 0 | return(1); |
6477 | | |
6478 | 0 | CHECK_DTD; |
6479 | | |
6480 | | /* |
6481 | | * Entities references have to be handled separately |
6482 | | */ |
6483 | 0 | if (elem->type == XML_ENTITY_REF_NODE) { |
6484 | 0 | return(1); |
6485 | 0 | } |
6486 | | |
6487 | 0 | ret &= xmlValidateOneElement(ctxt, doc, elem); |
6488 | 0 | if (elem->type == XML_ELEMENT_NODE) { |
6489 | 0 | attr = elem->properties; |
6490 | 0 | while (attr != NULL) { |
6491 | 0 | value = xmlNodeListGetString(doc, attr->children, 0); |
6492 | 0 | ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value); |
6493 | 0 | if (value != NULL) |
6494 | 0 | xmlFree((char *)value); |
6495 | 0 | attr= attr->next; |
6496 | 0 | } |
6497 | 0 | ns = elem->nsDef; |
6498 | 0 | while (ns != NULL) { |
6499 | 0 | if (elem->ns == NULL) |
6500 | 0 | ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL, |
6501 | 0 | ns, ns->href); |
6502 | 0 | else |
6503 | 0 | ret &= xmlValidateOneNamespace(ctxt, doc, elem, |
6504 | 0 | elem->ns->prefix, ns, ns->href); |
6505 | 0 | ns = ns->next; |
6506 | 0 | } |
6507 | 0 | } |
6508 | 0 | child = elem->children; |
6509 | 0 | while (child != NULL) { |
6510 | 0 | ret &= xmlValidateElement(ctxt, doc, child); |
6511 | 0 | child = child->next; |
6512 | 0 | } |
6513 | |
|
6514 | 0 | return(ret); |
6515 | 0 | } |
6516 | | |
6517 | | /** |
6518 | | * xmlValidateRef: |
6519 | | * @ref: A reference to be validated |
6520 | | * @ctxt: Validation context |
6521 | | * @name: Name of ID we are searching for |
6522 | | * |
6523 | | */ |
6524 | | static void |
6525 | | xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt, |
6526 | 0 | const xmlChar *name) { |
6527 | 0 | xmlAttrPtr id; |
6528 | 0 | xmlAttrPtr attr; |
6529 | |
|
6530 | 0 | if (ref == NULL) |
6531 | 0 | return; |
6532 | 0 | if ((ref->attr == NULL) && (ref->name == NULL)) |
6533 | 0 | return; |
6534 | 0 | attr = ref->attr; |
6535 | 0 | if (attr == NULL) { |
6536 | 0 | xmlChar *dup, *str = NULL, *cur, save; |
6537 | |
|
6538 | 0 | dup = xmlStrdup(name); |
6539 | 0 | if (dup == NULL) { |
6540 | 0 | ctxt->valid = 0; |
6541 | 0 | return; |
6542 | 0 | } |
6543 | 0 | cur = dup; |
6544 | 0 | while (*cur != 0) { |
6545 | 0 | str = cur; |
6546 | 0 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
6547 | 0 | save = *cur; |
6548 | 0 | *cur = 0; |
6549 | 0 | id = xmlGetID(ctxt->doc, str); |
6550 | 0 | if (id == NULL) { |
6551 | 0 | xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID, |
6552 | 0 | "attribute %s line %d references an unknown ID \"%s\"\n", |
6553 | 0 | ref->name, ref->lineno, str); |
6554 | 0 | ctxt->valid = 0; |
6555 | 0 | } |
6556 | 0 | if (save == 0) |
6557 | 0 | break; |
6558 | 0 | *cur = save; |
6559 | 0 | while (IS_BLANK_CH(*cur)) cur++; |
6560 | 0 | } |
6561 | 0 | xmlFree(dup); |
6562 | 0 | } else if (attr->atype == XML_ATTRIBUTE_IDREF) { |
6563 | 0 | id = xmlGetID(ctxt->doc, name); |
6564 | 0 | if (id == NULL) { |
6565 | 0 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
6566 | 0 | "IDREF attribute %s references an unknown ID \"%s\"\n", |
6567 | 0 | attr->name, name, NULL); |
6568 | 0 | ctxt->valid = 0; |
6569 | 0 | } |
6570 | 0 | } else if (attr->atype == XML_ATTRIBUTE_IDREFS) { |
6571 | 0 | xmlChar *dup, *str = NULL, *cur, save; |
6572 | |
|
6573 | 0 | dup = xmlStrdup(name); |
6574 | 0 | if (dup == NULL) { |
6575 | 0 | xmlVErrMemory(ctxt, "IDREFS split"); |
6576 | 0 | ctxt->valid = 0; |
6577 | 0 | return; |
6578 | 0 | } |
6579 | 0 | cur = dup; |
6580 | 0 | while (*cur != 0) { |
6581 | 0 | str = cur; |
6582 | 0 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
6583 | 0 | save = *cur; |
6584 | 0 | *cur = 0; |
6585 | 0 | id = xmlGetID(ctxt->doc, str); |
6586 | 0 | if (id == NULL) { |
6587 | 0 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
6588 | 0 | "IDREFS attribute %s references an unknown ID \"%s\"\n", |
6589 | 0 | attr->name, str, NULL); |
6590 | 0 | ctxt->valid = 0; |
6591 | 0 | } |
6592 | 0 | if (save == 0) |
6593 | 0 | break; |
6594 | 0 | *cur = save; |
6595 | 0 | while (IS_BLANK_CH(*cur)) cur++; |
6596 | 0 | } |
6597 | 0 | xmlFree(dup); |
6598 | 0 | } |
6599 | 0 | } |
6600 | | |
6601 | | /** |
6602 | | * xmlWalkValidateList: |
6603 | | * @data: Contents of current link |
6604 | | * @user: Value supplied by the user |
6605 | | * |
6606 | | * Returns 0 to abort the walk or 1 to continue |
6607 | | */ |
6608 | | static int |
6609 | | xmlWalkValidateList(const void *data, void *user) |
6610 | 0 | { |
6611 | 0 | xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user; |
6612 | 0 | xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name); |
6613 | 0 | return 1; |
6614 | 0 | } |
6615 | | |
6616 | | /** |
6617 | | * xmlValidateCheckRefCallback: |
6618 | | * @ref_list: List of references |
6619 | | * @ctxt: Validation context |
6620 | | * @name: Name of ID we are searching for |
6621 | | * |
6622 | | */ |
6623 | | static void |
6624 | 0 | xmlValidateCheckRefCallback(void *payload, void *data, const xmlChar *name) { |
6625 | 0 | xmlListPtr ref_list = (xmlListPtr) payload; |
6626 | 0 | xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data; |
6627 | 0 | xmlValidateMemo memo; |
6628 | |
|
6629 | 0 | if (ref_list == NULL) |
6630 | 0 | return; |
6631 | 0 | memo.ctxt = ctxt; |
6632 | 0 | memo.name = name; |
6633 | |
|
6634 | 0 | xmlListWalk(ref_list, xmlWalkValidateList, &memo); |
6635 | |
|
6636 | 0 | } |
6637 | | |
6638 | | /** |
6639 | | * xmlValidateDocumentFinal: |
6640 | | * @ctxt: the validation context |
6641 | | * @doc: a document instance |
6642 | | * |
6643 | | * Does the final step for the document validation once all the |
6644 | | * incremental validation steps have been completed |
6645 | | * |
6646 | | * basically it does the following checks described by the XML Rec |
6647 | | * |
6648 | | * Check all the IDREF/IDREFS attributes definition for validity |
6649 | | * |
6650 | | * returns 1 if valid or 0 otherwise |
6651 | | */ |
6652 | | |
6653 | | int |
6654 | 0 | xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
6655 | 0 | xmlRefTablePtr table; |
6656 | 0 | unsigned int save; |
6657 | |
|
6658 | 0 | if (ctxt == NULL) |
6659 | 0 | return(0); |
6660 | 0 | if (doc == NULL) { |
6661 | 0 | xmlErrValid(ctxt, XML_DTD_NO_DOC, |
6662 | 0 | "xmlValidateDocumentFinal: doc == NULL\n", NULL); |
6663 | 0 | return(0); |
6664 | 0 | } |
6665 | | |
6666 | | /* trick to get correct line id report */ |
6667 | 0 | save = ctxt->flags; |
6668 | 0 | ctxt->flags &= ~XML_VCTXT_USE_PCTXT; |
6669 | | |
6670 | | /* |
6671 | | * Check all the NOTATION/NOTATIONS attributes |
6672 | | */ |
6673 | | /* |
6674 | | * Check all the ENTITY/ENTITIES attributes definition for validity |
6675 | | */ |
6676 | | /* |
6677 | | * Check all the IDREF/IDREFS attributes definition for validity |
6678 | | */ |
6679 | 0 | table = (xmlRefTablePtr) doc->refs; |
6680 | 0 | ctxt->doc = doc; |
6681 | 0 | ctxt->valid = 1; |
6682 | 0 | xmlHashScan(table, xmlValidateCheckRefCallback, ctxt); |
6683 | |
|
6684 | 0 | ctxt->flags = save; |
6685 | 0 | return(ctxt->valid); |
6686 | 0 | } |
6687 | | |
6688 | | /** |
6689 | | * xmlValidateDtd: |
6690 | | * @ctxt: the validation context |
6691 | | * @doc: a document instance |
6692 | | * @dtd: a dtd instance |
6693 | | * |
6694 | | * Try to validate the document against the dtd instance |
6695 | | * |
6696 | | * Basically it does check all the definitions in the DtD. |
6697 | | * Note the the internal subset (if present) is de-coupled |
6698 | | * (i.e. not used), which could give problems if ID or IDREF |
6699 | | * is present. |
6700 | | * |
6701 | | * returns 1 if valid or 0 otherwise |
6702 | | */ |
6703 | | |
6704 | | int |
6705 | 0 | xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) { |
6706 | 0 | int ret; |
6707 | 0 | xmlDtdPtr oldExt, oldInt; |
6708 | 0 | xmlNodePtr root; |
6709 | |
|
6710 | 0 | if (dtd == NULL) return(0); |
6711 | 0 | if (doc == NULL) return(0); |
6712 | 0 | oldExt = doc->extSubset; |
6713 | 0 | oldInt = doc->intSubset; |
6714 | 0 | doc->extSubset = dtd; |
6715 | 0 | doc->intSubset = NULL; |
6716 | 0 | ret = xmlValidateRoot(ctxt, doc); |
6717 | 0 | if (ret == 0) { |
6718 | 0 | doc->extSubset = oldExt; |
6719 | 0 | doc->intSubset = oldInt; |
6720 | 0 | return(ret); |
6721 | 0 | } |
6722 | 0 | if (doc->ids != NULL) { |
6723 | 0 | xmlFreeIDTable(doc->ids); |
6724 | 0 | doc->ids = NULL; |
6725 | 0 | } |
6726 | 0 | if (doc->refs != NULL) { |
6727 | 0 | xmlFreeRefTable(doc->refs); |
6728 | 0 | doc->refs = NULL; |
6729 | 0 | } |
6730 | 0 | root = xmlDocGetRootElement(doc); |
6731 | 0 | ret = xmlValidateElement(ctxt, doc, root); |
6732 | 0 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
6733 | 0 | doc->extSubset = oldExt; |
6734 | 0 | doc->intSubset = oldInt; |
6735 | 0 | return(ret); |
6736 | 0 | } |
6737 | | |
6738 | | static void |
6739 | | xmlValidateNotationCallback(void *payload, void *data, |
6740 | 0 | const xmlChar *name ATTRIBUTE_UNUSED) { |
6741 | 0 | xmlEntityPtr cur = (xmlEntityPtr) payload; |
6742 | 0 | xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data; |
6743 | 0 | if (cur == NULL) |
6744 | 0 | return; |
6745 | 0 | if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
6746 | 0 | xmlChar *notation = cur->content; |
6747 | |
|
6748 | 0 | if (notation != NULL) { |
6749 | 0 | int ret; |
6750 | |
|
6751 | 0 | ret = xmlValidateNotationUse(ctxt, cur->doc, notation); |
6752 | 0 | if (ret != 1) { |
6753 | 0 | ctxt->valid = 0; |
6754 | 0 | } |
6755 | 0 | } |
6756 | 0 | } |
6757 | 0 | } |
6758 | | |
6759 | | static void |
6760 | | xmlValidateAttributeCallback(void *payload, void *data, |
6761 | 0 | const xmlChar *name ATTRIBUTE_UNUSED) { |
6762 | 0 | xmlAttributePtr cur = (xmlAttributePtr) payload; |
6763 | 0 | xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data; |
6764 | 0 | int ret; |
6765 | 0 | xmlDocPtr doc; |
6766 | 0 | xmlElementPtr elem = NULL; |
6767 | |
|
6768 | 0 | if (cur == NULL) |
6769 | 0 | return; |
6770 | 0 | switch (cur->atype) { |
6771 | 0 | case XML_ATTRIBUTE_CDATA: |
6772 | 0 | case XML_ATTRIBUTE_ID: |
6773 | 0 | case XML_ATTRIBUTE_IDREF : |
6774 | 0 | case XML_ATTRIBUTE_IDREFS: |
6775 | 0 | case XML_ATTRIBUTE_NMTOKEN: |
6776 | 0 | case XML_ATTRIBUTE_NMTOKENS: |
6777 | 0 | case XML_ATTRIBUTE_ENUMERATION: |
6778 | 0 | break; |
6779 | 0 | case XML_ATTRIBUTE_ENTITY: |
6780 | 0 | case XML_ATTRIBUTE_ENTITIES: |
6781 | 0 | case XML_ATTRIBUTE_NOTATION: |
6782 | 0 | if (cur->defaultValue != NULL) { |
6783 | |
|
6784 | 0 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name, |
6785 | 0 | cur->atype, cur->defaultValue); |
6786 | 0 | if ((ret == 0) && (ctxt->valid == 1)) |
6787 | 0 | ctxt->valid = 0; |
6788 | 0 | } |
6789 | 0 | if (cur->tree != NULL) { |
6790 | 0 | xmlEnumerationPtr tree = cur->tree; |
6791 | 0 | while (tree != NULL) { |
6792 | 0 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, |
6793 | 0 | cur->name, cur->atype, tree->name); |
6794 | 0 | if ((ret == 0) && (ctxt->valid == 1)) |
6795 | 0 | ctxt->valid = 0; |
6796 | 0 | tree = tree->next; |
6797 | 0 | } |
6798 | 0 | } |
6799 | 0 | } |
6800 | 0 | if (cur->atype == XML_ATTRIBUTE_NOTATION) { |
6801 | 0 | doc = cur->doc; |
6802 | 0 | if (cur->elem == NULL) { |
6803 | 0 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
6804 | 0 | "xmlValidateAttributeCallback(%s): internal error\n", |
6805 | 0 | (const char *) cur->name); |
6806 | 0 | return; |
6807 | 0 | } |
6808 | | |
6809 | 0 | if (doc != NULL) |
6810 | 0 | elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem); |
6811 | 0 | if ((elem == NULL) && (doc != NULL)) |
6812 | 0 | elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem); |
6813 | 0 | if ((elem == NULL) && (cur->parent != NULL) && |
6814 | 0 | (cur->parent->type == XML_DTD_NODE)) |
6815 | 0 | elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem); |
6816 | 0 | if (elem == NULL) { |
6817 | 0 | xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM, |
6818 | 0 | "attribute %s: could not find decl for element %s\n", |
6819 | 0 | cur->name, cur->elem, NULL); |
6820 | 0 | return; |
6821 | 0 | } |
6822 | 0 | if (elem->etype == XML_ELEMENT_TYPE_EMPTY) { |
6823 | 0 | xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION, |
6824 | 0 | "NOTATION attribute %s declared for EMPTY element %s\n", |
6825 | 0 | cur->name, cur->elem, NULL); |
6826 | 0 | ctxt->valid = 0; |
6827 | 0 | } |
6828 | 0 | } |
6829 | 0 | } |
6830 | | |
6831 | | /** |
6832 | | * xmlValidateDtdFinal: |
6833 | | * @ctxt: the validation context |
6834 | | * @doc: a document instance |
6835 | | * |
6836 | | * Does the final step for the dtds validation once all the |
6837 | | * subsets have been parsed |
6838 | | * |
6839 | | * basically it does the following checks described by the XML Rec |
6840 | | * - check that ENTITY and ENTITIES type attributes default or |
6841 | | * possible values matches one of the defined entities. |
6842 | | * - check that NOTATION type attributes default or |
6843 | | * possible values matches one of the defined notations. |
6844 | | * |
6845 | | * returns 1 if valid or 0 if invalid and -1 if not well-formed |
6846 | | */ |
6847 | | |
6848 | | int |
6849 | 0 | xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
6850 | 0 | xmlDtdPtr dtd; |
6851 | 0 | xmlAttributeTablePtr table; |
6852 | 0 | xmlEntitiesTablePtr entities; |
6853 | |
|
6854 | 0 | if ((doc == NULL) || (ctxt == NULL)) return(0); |
6855 | 0 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) |
6856 | 0 | return(0); |
6857 | 0 | ctxt->doc = doc; |
6858 | 0 | ctxt->valid = 1; |
6859 | 0 | dtd = doc->intSubset; |
6860 | 0 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
6861 | 0 | table = (xmlAttributeTablePtr) dtd->attributes; |
6862 | 0 | xmlHashScan(table, xmlValidateAttributeCallback, ctxt); |
6863 | 0 | } |
6864 | 0 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
6865 | 0 | entities = (xmlEntitiesTablePtr) dtd->entities; |
6866 | 0 | xmlHashScan(entities, xmlValidateNotationCallback, ctxt); |
6867 | 0 | } |
6868 | 0 | dtd = doc->extSubset; |
6869 | 0 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
6870 | 0 | table = (xmlAttributeTablePtr) dtd->attributes; |
6871 | 0 | xmlHashScan(table, xmlValidateAttributeCallback, ctxt); |
6872 | 0 | } |
6873 | 0 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
6874 | 0 | entities = (xmlEntitiesTablePtr) dtd->entities; |
6875 | 0 | xmlHashScan(entities, xmlValidateNotationCallback, ctxt); |
6876 | 0 | } |
6877 | 0 | return(ctxt->valid); |
6878 | 0 | } |
6879 | | |
6880 | | /** |
6881 | | * xmlValidateDocument: |
6882 | | * @ctxt: the validation context |
6883 | | * @doc: a document instance |
6884 | | * |
6885 | | * Try to validate the document instance |
6886 | | * |
6887 | | * basically it does the all the checks described by the XML Rec |
6888 | | * i.e. validates the internal and external subset (if present) |
6889 | | * and validate the document tree. |
6890 | | * |
6891 | | * returns 1 if valid or 0 otherwise |
6892 | | */ |
6893 | | |
6894 | | int |
6895 | 0 | xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
6896 | 0 | int ret; |
6897 | 0 | xmlNodePtr root; |
6898 | |
|
6899 | 0 | if (doc == NULL) |
6900 | 0 | return(0); |
6901 | 0 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
6902 | 0 | xmlErrValid(ctxt, XML_DTD_NO_DTD, |
6903 | 0 | "no DTD found!\n", NULL); |
6904 | 0 | return(0); |
6905 | 0 | } |
6906 | 0 | if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) || |
6907 | 0 | (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) { |
6908 | 0 | xmlChar *sysID; |
6909 | 0 | if (doc->intSubset->SystemID != NULL) { |
6910 | 0 | sysID = xmlBuildURI(doc->intSubset->SystemID, |
6911 | 0 | doc->URL); |
6912 | 0 | if (sysID == NULL) { |
6913 | 0 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
6914 | 0 | "Could not build URI for external subset \"%s\"\n", |
6915 | 0 | (const char *) doc->intSubset->SystemID); |
6916 | 0 | return 0; |
6917 | 0 | } |
6918 | 0 | } else |
6919 | 0 | sysID = NULL; |
6920 | 0 | doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, |
6921 | 0 | (const xmlChar *)sysID); |
6922 | 0 | if (sysID != NULL) |
6923 | 0 | xmlFree(sysID); |
6924 | 0 | if (doc->extSubset == NULL) { |
6925 | 0 | if (doc->intSubset->SystemID != NULL) { |
6926 | 0 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
6927 | 0 | "Could not load the external subset \"%s\"\n", |
6928 | 0 | (const char *) doc->intSubset->SystemID); |
6929 | 0 | } else { |
6930 | 0 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
6931 | 0 | "Could not load the external subset \"%s\"\n", |
6932 | 0 | (const char *) doc->intSubset->ExternalID); |
6933 | 0 | } |
6934 | 0 | return(0); |
6935 | 0 | } |
6936 | 0 | } |
6937 | | |
6938 | 0 | if (doc->ids != NULL) { |
6939 | 0 | xmlFreeIDTable(doc->ids); |
6940 | 0 | doc->ids = NULL; |
6941 | 0 | } |
6942 | 0 | if (doc->refs != NULL) { |
6943 | 0 | xmlFreeRefTable(doc->refs); |
6944 | 0 | doc->refs = NULL; |
6945 | 0 | } |
6946 | 0 | ret = xmlValidateDtdFinal(ctxt, doc); |
6947 | 0 | if (!xmlValidateRoot(ctxt, doc)) return(0); |
6948 | | |
6949 | 0 | root = xmlDocGetRootElement(doc); |
6950 | 0 | ret &= xmlValidateElement(ctxt, doc, root); |
6951 | 0 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
6952 | 0 | return(ret); |
6953 | 0 | } |
6954 | | |
6955 | | /************************************************************************ |
6956 | | * * |
6957 | | * Routines for dynamic validation editing * |
6958 | | * * |
6959 | | ************************************************************************/ |
6960 | | |
6961 | | /** |
6962 | | * xmlValidGetPotentialChildren: |
6963 | | * @ctree: an element content tree |
6964 | | * @names: an array to store the list of child names |
6965 | | * @len: a pointer to the number of element in the list |
6966 | | * @max: the size of the array |
6967 | | * |
6968 | | * Build/extend a list of potential children allowed by the content tree |
6969 | | * |
6970 | | * returns the number of element in the list, or -1 in case of error. |
6971 | | */ |
6972 | | |
6973 | | int |
6974 | | xmlValidGetPotentialChildren(xmlElementContent *ctree, |
6975 | | const xmlChar **names, |
6976 | 0 | int *len, int max) { |
6977 | 0 | int i; |
6978 | |
|
6979 | 0 | if ((ctree == NULL) || (names == NULL) || (len == NULL)) |
6980 | 0 | return(-1); |
6981 | 0 | if (*len >= max) return(*len); |
6982 | | |
6983 | 0 | switch (ctree->type) { |
6984 | 0 | case XML_ELEMENT_CONTENT_PCDATA: |
6985 | 0 | for (i = 0; i < *len;i++) |
6986 | 0 | if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len); |
6987 | 0 | names[(*len)++] = BAD_CAST "#PCDATA"; |
6988 | 0 | break; |
6989 | 0 | case XML_ELEMENT_CONTENT_ELEMENT: |
6990 | 0 | for (i = 0; i < *len;i++) |
6991 | 0 | if (xmlStrEqual(ctree->name, names[i])) return(*len); |
6992 | 0 | names[(*len)++] = ctree->name; |
6993 | 0 | break; |
6994 | 0 | case XML_ELEMENT_CONTENT_SEQ: |
6995 | 0 | xmlValidGetPotentialChildren(ctree->c1, names, len, max); |
6996 | 0 | xmlValidGetPotentialChildren(ctree->c2, names, len, max); |
6997 | 0 | break; |
6998 | 0 | case XML_ELEMENT_CONTENT_OR: |
6999 | 0 | xmlValidGetPotentialChildren(ctree->c1, names, len, max); |
7000 | 0 | xmlValidGetPotentialChildren(ctree->c2, names, len, max); |
7001 | 0 | break; |
7002 | 0 | } |
7003 | | |
7004 | 0 | return(*len); |
7005 | 0 | } |
7006 | | |
7007 | | /* |
7008 | | * Dummy function to suppress messages while we try out valid elements |
7009 | | */ |
7010 | | static void XMLCDECL xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED, |
7011 | 0 | const char *msg ATTRIBUTE_UNUSED, ...) { |
7012 | 0 | return; |
7013 | 0 | } |
7014 | | |
7015 | | /** |
7016 | | * xmlValidGetValidElements: |
7017 | | * @prev: an element to insert after |
7018 | | * @next: an element to insert next |
7019 | | * @names: an array to store the list of child names |
7020 | | * @max: the size of the array |
7021 | | * |
7022 | | * This function returns the list of authorized children to insert |
7023 | | * within an existing tree while respecting the validity constraints |
7024 | | * forced by the Dtd. The insertion point is defined using @prev and |
7025 | | * @next in the following ways: |
7026 | | * to insert before 'node': xmlValidGetValidElements(node->prev, node, ... |
7027 | | * to insert next 'node': xmlValidGetValidElements(node, node->next, ... |
7028 | | * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ... |
7029 | | * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs, |
7030 | | * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ... |
7031 | | * |
7032 | | * pointers to the element names are inserted at the beginning of the array |
7033 | | * and do not need to be freed. |
7034 | | * |
7035 | | * returns the number of element in the list, or -1 in case of error. If |
7036 | | * the function returns the value @max the caller is invited to grow the |
7037 | | * receiving array and retry. |
7038 | | */ |
7039 | | |
7040 | | int |
7041 | | xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names, |
7042 | 0 | int max) { |
7043 | 0 | xmlValidCtxt vctxt; |
7044 | 0 | int nb_valid_elements = 0; |
7045 | 0 | const xmlChar *elements[256]={0}; |
7046 | 0 | int nb_elements = 0, i; |
7047 | 0 | const xmlChar *name; |
7048 | |
|
7049 | 0 | xmlNode *ref_node; |
7050 | 0 | xmlNode *parent; |
7051 | 0 | xmlNode *test_node; |
7052 | |
|
7053 | 0 | xmlNode *prev_next; |
7054 | 0 | xmlNode *next_prev; |
7055 | 0 | xmlNode *parent_childs; |
7056 | 0 | xmlNode *parent_last; |
7057 | |
|
7058 | 0 | xmlElement *element_desc; |
7059 | |
|
7060 | 0 | if (prev == NULL && next == NULL) |
7061 | 0 | return(-1); |
7062 | | |
7063 | 0 | if (names == NULL) return(-1); |
7064 | 0 | if (max <= 0) return(-1); |
7065 | | |
7066 | 0 | memset(&vctxt, 0, sizeof (xmlValidCtxt)); |
7067 | 0 | vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */ |
7068 | |
|
7069 | 0 | nb_valid_elements = 0; |
7070 | 0 | ref_node = prev ? prev : next; |
7071 | 0 | parent = ref_node->parent; |
7072 | | |
7073 | | /* |
7074 | | * Retrieves the parent element declaration |
7075 | | */ |
7076 | 0 | element_desc = xmlGetDtdElementDesc(parent->doc->intSubset, |
7077 | 0 | parent->name); |
7078 | 0 | if ((element_desc == NULL) && (parent->doc->extSubset != NULL)) |
7079 | 0 | element_desc = xmlGetDtdElementDesc(parent->doc->extSubset, |
7080 | 0 | parent->name); |
7081 | 0 | if (element_desc == NULL) return(-1); |
7082 | | |
7083 | | /* |
7084 | | * Do a backup of the current tree structure |
7085 | | */ |
7086 | 0 | prev_next = prev ? prev->next : NULL; |
7087 | 0 | next_prev = next ? next->prev : NULL; |
7088 | 0 | parent_childs = parent->children; |
7089 | 0 | parent_last = parent->last; |
7090 | | |
7091 | | /* |
7092 | | * Creates a dummy node and insert it into the tree |
7093 | | */ |
7094 | 0 | test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL); |
7095 | 0 | if (test_node == NULL) |
7096 | 0 | return(-1); |
7097 | | |
7098 | 0 | test_node->parent = parent; |
7099 | 0 | test_node->prev = prev; |
7100 | 0 | test_node->next = next; |
7101 | 0 | name = test_node->name; |
7102 | |
|
7103 | 0 | if (prev) prev->next = test_node; |
7104 | 0 | else parent->children = test_node; |
7105 | |
|
7106 | 0 | if (next) next->prev = test_node; |
7107 | 0 | else parent->last = test_node; |
7108 | | |
7109 | | /* |
7110 | | * Insert each potential child node and check if the parent is |
7111 | | * still valid |
7112 | | */ |
7113 | 0 | nb_elements = xmlValidGetPotentialChildren(element_desc->content, |
7114 | 0 | elements, &nb_elements, 256); |
7115 | |
|
7116 | 0 | for (i = 0;i < nb_elements;i++) { |
7117 | 0 | test_node->name = elements[i]; |
7118 | 0 | if (xmlValidateOneElement(&vctxt, parent->doc, parent)) { |
7119 | 0 | int j; |
7120 | |
|
7121 | 0 | for (j = 0; j < nb_valid_elements;j++) |
7122 | 0 | if (xmlStrEqual(elements[i], names[j])) break; |
7123 | 0 | names[nb_valid_elements++] = elements[i]; |
7124 | 0 | if (nb_valid_elements >= max) break; |
7125 | 0 | } |
7126 | 0 | } |
7127 | | |
7128 | | /* |
7129 | | * Restore the tree structure |
7130 | | */ |
7131 | 0 | if (prev) prev->next = prev_next; |
7132 | 0 | if (next) next->prev = next_prev; |
7133 | 0 | parent->children = parent_childs; |
7134 | 0 | parent->last = parent_last; |
7135 | | |
7136 | | /* |
7137 | | * Free up the dummy node |
7138 | | */ |
7139 | 0 | test_node->name = name; |
7140 | 0 | xmlFreeNode(test_node); |
7141 | |
|
7142 | 0 | return(nb_valid_elements); |
7143 | 0 | } |
7144 | | #endif /* LIBXML_VALID_ENABLED */ |
7145 | | |