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