blob: 29bfcfadea2a6d43c08978996e81fe84c4b75703 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
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 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Bjorn Reese70a9da52001-04-21 16:57:29 +000010#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000011
Owen Taylor3473f882001-02-23 17:55:21 +000012#include <string.h>
13
14#ifdef HAVE_STDLIB_H
15#include <stdlib.h>
16#endif
17
18#include <libxml/xmlmemory.h>
19#include <libxml/hash.h>
20#include <libxml/valid.h>
21#include <libxml/parser.h>
22#include <libxml/parserInternals.h>
23#include <libxml/xmlerror.h>
24#include <libxml/list.h>
25
Daniel Veillarde62d36c2001-05-15 08:53:16 +000026/* #define DEBUG_VALID_ALGO */
27
Owen Taylor3473f882001-02-23 17:55:21 +000028/*
29 * Generic function for accessing stacks in the Validity Context
30 */
31
32#define PUSH_AND_POP(scope, type, name) \
33scope int name##VPush(xmlValidCtxtPtr ctxt, type value) { \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000034 if (ctxt->name##Max <= 0) { \
35 ctxt->name##Max = 4; \
36 ctxt->name##Tab = (type *) xmlMalloc( \
37 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
38 if (ctxt->name##Tab == NULL) { \
39 xmlGenericError(xmlGenericErrorContext, \
40 "malloc failed !\n"); \
Daniel Veillarda9142e72001-06-19 11:07:54 +000041 ctxt->name##Max = 0; \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000042 return(0); \
43 } \
44 } \
Owen Taylor3473f882001-02-23 17:55:21 +000045 if (ctxt->name##Nr >= ctxt->name##Max) { \
46 ctxt->name##Max *= 2; \
47 ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \
48 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
49 if (ctxt->name##Tab == NULL) { \
50 xmlGenericError(xmlGenericErrorContext, \
51 "realloc failed !\n"); \
52 return(0); \
53 } \
54 } \
55 ctxt->name##Tab[ctxt->name##Nr] = value; \
56 ctxt->name = value; \
57 return(ctxt->name##Nr++); \
58} \
59scope type name##VPop(xmlValidCtxtPtr ctxt) { \
60 type ret; \
61 if (ctxt->name##Nr <= 0) return(0); \
62 ctxt->name##Nr--; \
63 if (ctxt->name##Nr > 0) \
64 ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \
65 else \
66 ctxt->name = NULL; \
67 ret = ctxt->name##Tab[ctxt->name##Nr]; \
68 ctxt->name##Tab[ctxt->name##Nr] = 0; \
69 return(ret); \
70} \
71
Daniel Veillarddab4cb32001-04-20 13:03:48 +000072/*
73 * I will use a home made algorithm less complex and easier to
74 * debug/maintin than a generic NFA -> DFA state based algo. The
75 * only restriction is on the deepness of the tree limited by the
76 * size of the occurs bitfield
77 *
78 * this is the content of a saved state for rollbacks
79 */
80
81#define ROLLBACK_OR 0
82#define ROLLBACK_PARENT 1
83
84struct _xmlValidState {
85 xmlElementContentPtr cont; /* pointer to the content model subtree */
86 xmlNodePtr node; /* pointer to the current node in the list */
87 long occurs;/* bitfield for multiple occurences */
88 unsigned char depth; /* current depth in the overall tree */
89 unsigned char state; /* ROLLBACK_XXX */
90} _xmlValidState;
91
92#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
93#define CONT ctxt->vstate->cont
94#define NODE ctxt->vstate->node
95#define DEPTH ctxt->vstate->depth
96#define OCCURS ctxt->vstate->occurs
97#define STATE ctxt->vstate->state
98
99#define OCCURENCE (ctxt->vstate->occurs & (1 << DEPTH))
100#define PARENT_OCCURENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
101
102#define SET_OCCURENCE ctxt->vstate->occurs |= (1 << DEPTH)
103#define RESET_OCCURENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
104
105static int
106vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
107 xmlNodePtr node, unsigned char depth, long occurs,
108 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000109 int i = ctxt->vstateNr - 1;
110
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000111 if (ctxt->vstateNr >= ctxt->vstateMax) {
112 ctxt->vstateMax *= 2;
113 ctxt->vstateTab = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
114 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
115 if (ctxt->vstateTab == NULL) {
116 xmlGenericError(xmlGenericErrorContext,
117 "realloc failed !n");
118 return(0);
119 }
Daniel Veillard06803992001-04-22 10:35:56 +0000120 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000121 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000122 /*
123 * Don't push on the stack a state already here
124 */
125 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
126 (ctxt->vstateTab[i].node == node) &&
127 (ctxt->vstateTab[i].depth == depth) &&
128 (ctxt->vstateTab[i].occurs == occurs) &&
129 (ctxt->vstateTab[i].state == state))
130 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000131 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
132 ctxt->vstateTab[ctxt->vstateNr].node = node;
133 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
134 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
135 ctxt->vstateTab[ctxt->vstateNr].state = state;
136 return(ctxt->vstateNr++);
137}
138
139static int
140vstateVPop(xmlValidCtxtPtr ctxt) {
141 if (ctxt->vstateNr <= 1) return(-1);
142 ctxt->vstateNr--;
143 ctxt->vstate = &ctxt->vstateTab[0];
144 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
145 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
146 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
147 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
148 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
149 return(ctxt->vstateNr);
150}
151
Owen Taylor3473f882001-02-23 17:55:21 +0000152PUSH_AND_POP(static, xmlNodePtr, node)
153
Owen Taylor3473f882001-02-23 17:55:21 +0000154#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000155static void
156xmlValidPrintNode(xmlNodePtr cur) {
157 if (cur == NULL) {
158 xmlGenericError(xmlGenericErrorContext, "null");
159 return;
160 }
161 switch (cur->type) {
162 case XML_ELEMENT_NODE:
163 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
164 break;
165 case XML_TEXT_NODE:
166 xmlGenericError(xmlGenericErrorContext, "text ");
167 break;
168 case XML_CDATA_SECTION_NODE:
169 xmlGenericError(xmlGenericErrorContext, "cdata ");
170 break;
171 case XML_ENTITY_REF_NODE:
172 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
173 break;
174 case XML_PI_NODE:
175 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
176 break;
177 case XML_COMMENT_NODE:
178 xmlGenericError(xmlGenericErrorContext, "comment ");
179 break;
180 case XML_ATTRIBUTE_NODE:
181 xmlGenericError(xmlGenericErrorContext, "?attr? ");
182 break;
183 case XML_ENTITY_NODE:
184 xmlGenericError(xmlGenericErrorContext, "?ent? ");
185 break;
186 case XML_DOCUMENT_NODE:
187 xmlGenericError(xmlGenericErrorContext, "?doc? ");
188 break;
189 case XML_DOCUMENT_TYPE_NODE:
190 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
191 break;
192 case XML_DOCUMENT_FRAG_NODE:
193 xmlGenericError(xmlGenericErrorContext, "?frag? ");
194 break;
195 case XML_NOTATION_NODE:
196 xmlGenericError(xmlGenericErrorContext, "?nota? ");
197 break;
198 case XML_HTML_DOCUMENT_NODE:
199 xmlGenericError(xmlGenericErrorContext, "?html? ");
200 break;
201 case XML_DTD_NODE:
202 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
203 break;
204 case XML_ELEMENT_DECL:
205 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
206 break;
207 case XML_ATTRIBUTE_DECL:
208 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
209 break;
210 case XML_ENTITY_DECL:
211 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
212 break;
213 case XML_NAMESPACE_DECL:
214 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
215 break;
216 case XML_XINCLUDE_START:
217 xmlGenericError(xmlGenericErrorContext, "incstart ");
218 break;
219 case XML_XINCLUDE_END:
220 xmlGenericError(xmlGenericErrorContext, "incend ");
221 break;
222 }
223}
224
225static void
226xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000227 if (cur == NULL)
228 xmlGenericError(xmlGenericErrorContext, "null ");
229 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000230 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000231 cur = cur->next;
232 }
233}
234
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000235static void
236xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Owen Taylor3473f882001-02-23 17:55:21 +0000237 char expr[1000];
238
239 expr[0] = 0;
240 xmlGenericError(xmlGenericErrorContext, "valid: ");
241 xmlValidPrintNodeList(cur);
242 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000243 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000244 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
245}
246
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000247static void
248xmlValidDebugState(xmlValidStatePtr state) {
249 xmlGenericError(xmlGenericErrorContext, "(");
250 if (state->cont == NULL)
251 xmlGenericError(xmlGenericErrorContext, "null,");
252 else
253 switch (state->cont->type) {
254 case XML_ELEMENT_CONTENT_PCDATA:
255 xmlGenericError(xmlGenericErrorContext, "pcdata,");
256 break;
257 case XML_ELEMENT_CONTENT_ELEMENT:
258 xmlGenericError(xmlGenericErrorContext, "%s,",
259 state->cont->name);
260 break;
261 case XML_ELEMENT_CONTENT_SEQ:
262 xmlGenericError(xmlGenericErrorContext, "seq,");
263 break;
264 case XML_ELEMENT_CONTENT_OR:
265 xmlGenericError(xmlGenericErrorContext, "or,");
266 break;
267 }
268 xmlValidPrintNode(state->node);
269 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
270 state->depth, state->occurs, state->state);
271}
272
273static void
274xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
275 int i, j;
276
277 xmlGenericError(xmlGenericErrorContext, "state: ");
278 xmlValidDebugState(ctxt->vstate);
279 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
280 ctxt->vstateNr - 1);
281 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
282 xmlValidDebugState(&ctxt->vstateTab[j]);
283 xmlGenericError(xmlGenericErrorContext, "\n");
284}
285
286/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000287#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000288 *****/
289
290#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000291#define DEBUG_VALID_MSG(m) \
292 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
293
Owen Taylor3473f882001-02-23 17:55:21 +0000294#else
295#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000296#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000297#endif
298
299/* TODO: use hash table for accesses to elem and attribute dedinitions */
300
301#define VERROR \
302 if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error
303
304#define VWARNING \
305 if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning
306
307#define CHECK_DTD \
308 if (doc == NULL) return(0); \
309 else if ((doc->intSubset == NULL) && \
310 (doc->extSubset == NULL)) return(0)
311
312xmlElementPtr xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name);
Daniel Veillarda10efa82001-04-18 13:09:01 +0000313static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
314 int create);
Owen Taylor3473f882001-02-23 17:55:21 +0000315xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
316
317/************************************************************************
318 * *
319 * QName handling helper *
320 * *
321 ************************************************************************/
322
323/**
324 * xmlSplitQName2:
325 * @name: an XML parser context
326 * @prefix: a xmlChar **
327 *
328 * parse an XML qualified name string
329 *
330 * [NS 5] QName ::= (Prefix ':')? LocalPart
331 *
332 * [NS 6] Prefix ::= NCName
333 *
334 * [NS 7] LocalPart ::= NCName
335 *
336 * Returns NULL if not a QName, otherwise the local part, and prefix
337 * is updated to get the Prefix if any.
338 */
339
340xmlChar *
341xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
342 int len = 0;
343 xmlChar *ret = NULL;
344
345 *prefix = NULL;
346
347 /* xml: prefix is not really a namespace */
348 if ((name[0] == 'x') && (name[1] == 'm') &&
349 (name[2] == 'l') && (name[3] == ':'))
350 return(NULL);
351
352 /* nasty but valid */
353 if (name[0] == ':')
354 return(NULL);
355
356 /*
357 * we are not trying to validate but just to cut, and yes it will
358 * work even if this is as set of UTF-8 encoded chars
359 */
360 while ((name[len] != 0) && (name[len] != ':'))
361 len++;
362
363 if (name[len] == 0)
364 return(NULL);
365
366 *prefix = xmlStrndup(name, len);
367 ret = xmlStrdup(&name[len + 1]);
368
369 return(ret);
370}
371
372/****************************************************************
373 * *
374 * Util functions for data allocation/deallocation *
375 * *
376 ****************************************************************/
377
378/**
379 * xmlNewElementContent:
380 * @name: the subelement name or NULL
381 * @type: the type of element content decl
382 *
383 * Allocate an element content structure.
384 *
385 * Returns NULL if not, othervise the new element content structure
386 */
387xmlElementContentPtr
388xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
389 xmlElementContentPtr ret;
390
391 switch(type) {
392 case XML_ELEMENT_CONTENT_ELEMENT:
393 if (name == NULL) {
394 xmlGenericError(xmlGenericErrorContext,
395 "xmlNewElementContent : name == NULL !\n");
396 }
397 break;
398 case XML_ELEMENT_CONTENT_PCDATA:
399 case XML_ELEMENT_CONTENT_SEQ:
400 case XML_ELEMENT_CONTENT_OR:
401 if (name != NULL) {
402 xmlGenericError(xmlGenericErrorContext,
403 "xmlNewElementContent : name != NULL !\n");
404 }
405 break;
406 default:
407 xmlGenericError(xmlGenericErrorContext,
408 "xmlNewElementContent: unknown type %d\n", type);
409 return(NULL);
410 }
411 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
412 if (ret == NULL) {
413 xmlGenericError(xmlGenericErrorContext,
414 "xmlNewElementContent : out of memory!\n");
415 return(NULL);
416 }
417 ret->type = type;
418 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
419 if (name != NULL)
420 ret->name = xmlStrdup(name);
421 else
422 ret->name = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000423 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000424 return(ret);
425}
426
427/**
428 * xmlCopyElementContent:
429 * @content: An element content pointer.
430 *
431 * Build a copy of an element content description.
432 *
433 * Returns the new xmlElementContentPtr or NULL in case of error.
434 */
435xmlElementContentPtr
436xmlCopyElementContent(xmlElementContentPtr cur) {
437 xmlElementContentPtr ret;
438
439 if (cur == NULL) return(NULL);
440 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
441 if (ret == NULL) {
442 xmlGenericError(xmlGenericErrorContext,
443 "xmlCopyElementContent : out of memory\n");
444 return(NULL);
445 }
446 ret->ocur = cur->ocur;
447 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000448 if (ret->c1 != NULL)
449 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000450 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000451 if (ret->c2 != NULL)
452 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000453 return(ret);
454}
455
456/**
457 * xmlFreeElementContent:
458 * @cur: the element content tree to free
459 *
460 * Free an element content structure. This is a recursive call !
461 */
462void
463xmlFreeElementContent(xmlElementContentPtr cur) {
464 if (cur == NULL) return;
465 switch (cur->type) {
466 case XML_ELEMENT_CONTENT_PCDATA:
467 case XML_ELEMENT_CONTENT_ELEMENT:
468 case XML_ELEMENT_CONTENT_SEQ:
469 case XML_ELEMENT_CONTENT_OR:
470 break;
471 default:
472 xmlGenericError(xmlGenericErrorContext,
473 "xmlFreeElementContent : type %d\n", cur->type);
474 return;
475 }
476 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
477 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
478 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +0000479 xmlFree(cur);
480}
481
482/**
483 * xmlDumpElementContent:
484 * @buf: An XML buffer
485 * @content: An element table
486 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
487 *
488 * This will dump the content of the element table as an XML DTD definition
489 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000490static void
Owen Taylor3473f882001-02-23 17:55:21 +0000491xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
492 if (content == NULL) return;
493
494 if (glob) xmlBufferWriteChar(buf, "(");
495 switch (content->type) {
496 case XML_ELEMENT_CONTENT_PCDATA:
497 xmlBufferWriteChar(buf, "#PCDATA");
498 break;
499 case XML_ELEMENT_CONTENT_ELEMENT:
500 xmlBufferWriteCHAR(buf, content->name);
501 break;
502 case XML_ELEMENT_CONTENT_SEQ:
503 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
504 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
505 xmlDumpElementContent(buf, content->c1, 1);
506 else
507 xmlDumpElementContent(buf, content->c1, 0);
508 xmlBufferWriteChar(buf, " , ");
509 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
510 xmlDumpElementContent(buf, content->c2, 1);
511 else
512 xmlDumpElementContent(buf, content->c2, 0);
513 break;
514 case XML_ELEMENT_CONTENT_OR:
515 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
516 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
517 xmlDumpElementContent(buf, content->c1, 1);
518 else
519 xmlDumpElementContent(buf, content->c1, 0);
520 xmlBufferWriteChar(buf, " | ");
521 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
522 xmlDumpElementContent(buf, content->c2, 1);
523 else
524 xmlDumpElementContent(buf, content->c2, 0);
525 break;
526 default:
527 xmlGenericError(xmlGenericErrorContext,
528 "xmlDumpElementContent: unknown type %d\n",
529 content->type);
530 }
531 if (glob)
532 xmlBufferWriteChar(buf, ")");
533 switch (content->ocur) {
534 case XML_ELEMENT_CONTENT_ONCE:
535 break;
536 case XML_ELEMENT_CONTENT_OPT:
537 xmlBufferWriteChar(buf, "?");
538 break;
539 case XML_ELEMENT_CONTENT_MULT:
540 xmlBufferWriteChar(buf, "*");
541 break;
542 case XML_ELEMENT_CONTENT_PLUS:
543 xmlBufferWriteChar(buf, "+");
544 break;
545 }
546}
547
548/**
549 * xmlSprintfElementContent:
550 * @buf: an output buffer
551 * @content: An element table
552 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
553 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000554 * Deprecated, unsafe, use xmlSnprintfElementContent
555 */
556void
557xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
558 xmlElementContentPtr content ATTRIBUTE_UNUSED,
559 int glob ATTRIBUTE_UNUSED) {
560}
561
562/**
563 * xmlSnprintfElementContent:
564 * @buf: an output buffer
565 * @size: the buffer size
566 * @content: An element table
567 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
568 *
Owen Taylor3473f882001-02-23 17:55:21 +0000569 * This will dump the content of the element content definition
570 * Intended just for the debug routine
571 */
572void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000573xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
574 int len;
575
Owen Taylor3473f882001-02-23 17:55:21 +0000576 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000577 len = strlen(buf);
578 if (size - len < 50) {
579 if ((size - len > 4) && (buf[len - 1] != '.'))
580 strcat(buf, " ...");
581 return;
582 }
Owen Taylor3473f882001-02-23 17:55:21 +0000583 if (glob) strcat(buf, "(");
584 switch (content->type) {
585 case XML_ELEMENT_CONTENT_PCDATA:
586 strcat(buf, "#PCDATA");
587 break;
588 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardd3d06722001-08-15 12:06:36 +0000589 if (size - len < xmlStrlen(content->name + 10)) {
590 strcat(buf, " ...");
591 return;
592 }
Owen Taylor3473f882001-02-23 17:55:21 +0000593 strcat(buf, (char *) content->name);
594 break;
595 case XML_ELEMENT_CONTENT_SEQ:
596 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
597 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000598 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000599 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000600 xmlSnprintfElementContent(buf, size, content->c1, 0);
601 len = strlen(buf);
602 if (size - len < 50) {
603 if ((size - len > 4) && (buf[len - 1] != '.'))
604 strcat(buf, " ...");
605 return;
606 }
Owen Taylor3473f882001-02-23 17:55:21 +0000607 strcat(buf, " , ");
608 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000609 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000610 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000611 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000612 break;
613 case XML_ELEMENT_CONTENT_OR:
614 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
615 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000616 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000617 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000618 xmlSnprintfElementContent(buf, size, content->c1, 0);
619 len = strlen(buf);
620 if (size - len < 50) {
621 if ((size - len > 4) && (buf[len - 1] != '.'))
622 strcat(buf, " ...");
623 return;
624 }
Owen Taylor3473f882001-02-23 17:55:21 +0000625 strcat(buf, " | ");
626 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000627 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000628 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000629 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000630 break;
631 }
632 if (glob)
633 strcat(buf, ")");
634 switch (content->ocur) {
635 case XML_ELEMENT_CONTENT_ONCE:
636 break;
637 case XML_ELEMENT_CONTENT_OPT:
638 strcat(buf, "?");
639 break;
640 case XML_ELEMENT_CONTENT_MULT:
641 strcat(buf, "*");
642 break;
643 case XML_ELEMENT_CONTENT_PLUS:
644 strcat(buf, "+");
645 break;
646 }
647}
648
649/****************************************************************
650 * *
651 * Registration of DTD declarations *
652 * *
653 ****************************************************************/
654
655/**
656 * xmlCreateElementTable:
657 *
658 * create and initialize an empty element hash table.
659 *
660 * Returns the xmlElementTablePtr just created or NULL in case of error.
661 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000662static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000663xmlCreateElementTable(void) {
664 return(xmlHashCreate(0));
665}
666
667/**
668 * xmlFreeElement:
669 * @elem: An element
670 *
671 * Deallocate the memory used by an element definition
672 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000673static void
Owen Taylor3473f882001-02-23 17:55:21 +0000674xmlFreeElement(xmlElementPtr elem) {
675 if (elem == NULL) return;
676 xmlUnlinkNode((xmlNodePtr) elem);
677 xmlFreeElementContent(elem->content);
678 if (elem->name != NULL)
679 xmlFree((xmlChar *) elem->name);
680 if (elem->prefix != NULL)
681 xmlFree((xmlChar *) elem->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000682 xmlFree(elem);
683}
684
685
686/**
687 * xmlAddElementDecl:
688 * @ctxt: the validation context
689 * @dtd: pointer to the DTD
690 * @name: the entity name
691 * @type: the element type
692 * @content: the element content tree or NULL
693 *
694 * Register a new element declaration
695 *
696 * Returns NULL if not, othervise the entity
697 */
698xmlElementPtr
699xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
700 xmlElementTypeVal type,
701 xmlElementContentPtr content) {
702 xmlElementPtr ret;
703 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +0000704 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000705 xmlChar *ns, *uqname;
706
707 if (dtd == NULL) {
708 xmlGenericError(xmlGenericErrorContext,
709 "xmlAddElementDecl: dtd == NULL\n");
710 return(NULL);
711 }
712 if (name == NULL) {
713 xmlGenericError(xmlGenericErrorContext,
714 "xmlAddElementDecl: name == NULL\n");
715 return(NULL);
716 }
717 switch (type) {
718 case XML_ELEMENT_TYPE_EMPTY:
719 if (content != NULL) {
720 xmlGenericError(xmlGenericErrorContext,
721 "xmlAddElementDecl: content != NULL for EMPTY\n");
722 return(NULL);
723 }
724 break;
725 case XML_ELEMENT_TYPE_ANY:
726 if (content != NULL) {
727 xmlGenericError(xmlGenericErrorContext,
728 "xmlAddElementDecl: content != NULL for ANY\n");
729 return(NULL);
730 }
731 break;
732 case XML_ELEMENT_TYPE_MIXED:
733 if (content == NULL) {
734 xmlGenericError(xmlGenericErrorContext,
735 "xmlAddElementDecl: content == NULL for MIXED\n");
736 return(NULL);
737 }
738 break;
739 case XML_ELEMENT_TYPE_ELEMENT:
740 if (content == NULL) {
741 xmlGenericError(xmlGenericErrorContext,
742 "xmlAddElementDecl: content == NULL for ELEMENT\n");
743 return(NULL);
744 }
745 break;
746 default:
747 xmlGenericError(xmlGenericErrorContext,
748 "xmlAddElementDecl: unknown type %d\n", type);
749 return(NULL);
750 }
751
752 /*
753 * check if name is a QName
754 */
755 uqname = xmlSplitQName2(name, &ns);
756 if (uqname != NULL)
757 name = uqname;
758
759 /*
760 * Create the Element table if needed.
761 */
762 table = (xmlElementTablePtr) dtd->elements;
763 if (table == NULL) {
764 table = xmlCreateElementTable();
765 dtd->elements = (void *) table;
766 }
767 if (table == NULL) {
768 xmlGenericError(xmlGenericErrorContext,
769 "xmlAddElementDecl: Table creation failed!\n");
770 return(NULL);
771 }
772
Daniel Veillarda10efa82001-04-18 13:09:01 +0000773 /*
774 * lookup old attributes inserted on an undefined element in the
775 * internal subset.
776 */
777 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
778 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
779 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
780 oldAttributes = ret->attributes;
781 ret->attributes = NULL;
782 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
783 xmlFreeElement(ret);
784 }
Owen Taylor3473f882001-02-23 17:55:21 +0000785 }
Owen Taylor3473f882001-02-23 17:55:21 +0000786
787 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +0000788 * The element may already be present if one of its attribute
789 * was registered first
790 */
791 ret = xmlHashLookup2(table, name, ns);
792 if (ret != NULL) {
793 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
794 /*
795 * The element is already defined in this Dtd.
796 */
797 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
798 if (uqname != NULL)
799 xmlFree(uqname);
800 return(NULL);
801 }
802 } else {
803 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
804 if (ret == NULL) {
805 xmlGenericError(xmlGenericErrorContext,
806 "xmlAddElementDecl: out of memory\n");
807 return(NULL);
808 }
809 memset(ret, 0, sizeof(xmlElement));
810 ret->type = XML_ELEMENT_DECL;
811
812 /*
813 * fill the structure.
814 */
815 ret->name = xmlStrdup(name);
816 ret->prefix = ns;
817
818 /*
819 * Validity Check:
820 * Insertion must not fail
821 */
822 if (xmlHashAddEntry2(table, name, ns, ret)) {
823 /*
824 * The element is already defined in this Dtd.
825 */
826 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
827 xmlFreeElement(ret);
828 if (uqname != NULL)
829 xmlFree(uqname);
830 return(NULL);
831 }
832 }
833
834 /*
835 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +0000836 */
837 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +0000838 ret->content = xmlCopyElementContent(content);
Daniel Veillarda10efa82001-04-18 13:09:01 +0000839 ret->attributes = oldAttributes;
Owen Taylor3473f882001-02-23 17:55:21 +0000840
841 /*
842 * Link it to the Dtd
843 */
844 ret->parent = dtd;
845 ret->doc = dtd->doc;
846 if (dtd->last == NULL) {
847 dtd->children = dtd->last = (xmlNodePtr) ret;
848 } else {
849 dtd->last->next = (xmlNodePtr) ret;
850 ret->prev = dtd->last;
851 dtd->last = (xmlNodePtr) ret;
852 }
853 if (uqname != NULL)
854 xmlFree(uqname);
855 return(ret);
856}
857
858/**
859 * xmlFreeElementTable:
860 * @table: An element table
861 *
862 * Deallocate the memory used by an element hash table.
863 */
864void
865xmlFreeElementTable(xmlElementTablePtr table) {
866 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
867}
868
869/**
870 * xmlCopyElement:
871 * @elem: An element
872 *
873 * Build a copy of an element.
874 *
875 * Returns the new xmlElementPtr or NULL in case of error.
876 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000877static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000878xmlCopyElement(xmlElementPtr elem) {
879 xmlElementPtr cur;
880
881 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
882 if (cur == NULL) {
883 xmlGenericError(xmlGenericErrorContext,
884 "xmlCopyElement: out of memory !\n");
885 return(NULL);
886 }
887 memset(cur, 0, sizeof(xmlElement));
888 cur->type = XML_ELEMENT_DECL;
889 cur->etype = elem->etype;
890 if (elem->name != NULL)
891 cur->name = xmlStrdup(elem->name);
892 else
893 cur->name = NULL;
894 if (elem->prefix != NULL)
895 cur->prefix = xmlStrdup(elem->prefix);
896 else
897 cur->prefix = NULL;
898 cur->content = xmlCopyElementContent(elem->content);
899 /* TODO : rebuild the attribute list on the copy */
900 cur->attributes = NULL;
901 return(cur);
902}
903
904/**
905 * xmlCopyElementTable:
906 * @table: An element table
907 *
908 * Build a copy of an element table.
909 *
910 * Returns the new xmlElementTablePtr or NULL in case of error.
911 */
912xmlElementTablePtr
913xmlCopyElementTable(xmlElementTablePtr table) {
914 return((xmlElementTablePtr) xmlHashCopy(table,
915 (xmlHashCopier) xmlCopyElement));
916}
917
918/**
919 * xmlDumpElementDecl:
920 * @buf: the XML buffer output
921 * @elem: An element table
922 *
923 * This will dump the content of the element declaration as an XML
924 * DTD definition
925 */
926void
927xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
928 switch (elem->etype) {
929 case XML_ELEMENT_TYPE_EMPTY:
930 xmlBufferWriteChar(buf, "<!ELEMENT ");
931 xmlBufferWriteCHAR(buf, elem->name);
932 xmlBufferWriteChar(buf, " EMPTY>\n");
933 break;
934 case XML_ELEMENT_TYPE_ANY:
935 xmlBufferWriteChar(buf, "<!ELEMENT ");
936 xmlBufferWriteCHAR(buf, elem->name);
937 xmlBufferWriteChar(buf, " ANY>\n");
938 break;
939 case XML_ELEMENT_TYPE_MIXED:
940 xmlBufferWriteChar(buf, "<!ELEMENT ");
941 xmlBufferWriteCHAR(buf, elem->name);
942 xmlBufferWriteChar(buf, " ");
943 xmlDumpElementContent(buf, elem->content, 1);
944 xmlBufferWriteChar(buf, ">\n");
945 break;
946 case XML_ELEMENT_TYPE_ELEMENT:
947 xmlBufferWriteChar(buf, "<!ELEMENT ");
948 xmlBufferWriteCHAR(buf, elem->name);
949 xmlBufferWriteChar(buf, " ");
950 xmlDumpElementContent(buf, elem->content, 1);
951 xmlBufferWriteChar(buf, ">\n");
952 break;
953 default:
954 xmlGenericError(xmlGenericErrorContext,
955 "xmlDumpElementDecl: internal: unknown type %d\n",
956 elem->etype);
957 }
958}
959
960/**
961 * xmlDumpElementTable:
962 * @buf: the XML buffer output
963 * @table: An element table
964 *
965 * This will dump the content of the element table as an XML DTD definition
966 */
967void
968xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
969 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
970}
971
972/**
973 * xmlCreateEnumeration:
974 * @name: the enumeration name or NULL
975 *
976 * create and initialize an enumeration attribute node.
977 *
978 * Returns the xmlEnumerationPtr just created or NULL in case
979 * of error.
980 */
981xmlEnumerationPtr
982xmlCreateEnumeration(xmlChar *name) {
983 xmlEnumerationPtr ret;
984
985 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
986 if (ret == NULL) {
987 xmlGenericError(xmlGenericErrorContext,
988 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
989 (long)sizeof(xmlEnumeration));
990 return(NULL);
991 }
992 memset(ret, 0, sizeof(xmlEnumeration));
993
994 if (name != NULL)
995 ret->name = xmlStrdup(name);
996 return(ret);
997}
998
999/**
1000 * xmlFreeEnumeration:
1001 * @cur: the tree to free.
1002 *
1003 * free an enumeration attribute node (recursive).
1004 */
1005void
1006xmlFreeEnumeration(xmlEnumerationPtr cur) {
1007 if (cur == NULL) return;
1008
1009 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1010
1011 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001012 xmlFree(cur);
1013}
1014
1015/**
1016 * xmlCopyEnumeration:
1017 * @cur: the tree to copy.
1018 *
1019 * Copy an enumeration attribute node (recursive).
1020 *
1021 * Returns the xmlEnumerationPtr just created or NULL in case
1022 * of error.
1023 */
1024xmlEnumerationPtr
1025xmlCopyEnumeration(xmlEnumerationPtr cur) {
1026 xmlEnumerationPtr ret;
1027
1028 if (cur == NULL) return(NULL);
1029 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1030
1031 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1032 else ret->next = NULL;
1033
1034 return(ret);
1035}
1036
1037/**
1038 * xmlDumpEnumeration:
1039 * @buf: the XML buffer output
1040 * @enum: An enumeration
1041 *
1042 * This will dump the content of the enumeration
1043 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001044static void
Owen Taylor3473f882001-02-23 17:55:21 +00001045xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1046 if (cur == NULL) return;
1047
1048 xmlBufferWriteCHAR(buf, cur->name);
1049 if (cur->next == NULL)
1050 xmlBufferWriteChar(buf, ")");
1051 else {
1052 xmlBufferWriteChar(buf, " | ");
1053 xmlDumpEnumeration(buf, cur->next);
1054 }
1055}
1056
1057/**
1058 * xmlCreateAttributeTable:
1059 *
1060 * create and initialize an empty attribute hash table.
1061 *
1062 * Returns the xmlAttributeTablePtr just created or NULL in case
1063 * of error.
1064 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001065static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001066xmlCreateAttributeTable(void) {
1067 return(xmlHashCreate(0));
1068}
1069
1070/**
1071 * xmlScanAttributeDeclCallback:
1072 * @attr: the attribute decl
1073 * @list: the list to update
1074 *
1075 * Callback called by xmlScanAttributeDecl when a new attribute
1076 * has to be entered in the list.
1077 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001078static void
Owen Taylor3473f882001-02-23 17:55:21 +00001079xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001080 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001081 attr->nexth = *list;
1082 *list = attr;
1083}
1084
1085/**
1086 * xmlScanAttributeDecl:
1087 * @dtd: pointer to the DTD
1088 * @elem: the element name
1089 *
1090 * When inserting a new element scan the DtD for existing attributes
1091 * for taht element and initialize the Attribute chain
1092 *
1093 * Returns the pointer to the first attribute decl in the chain,
1094 * possibly NULL.
1095 */
1096xmlAttributePtr
1097xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1098 xmlAttributePtr ret = NULL;
1099 xmlAttributeTablePtr table;
1100
1101 if (dtd == NULL) {
1102 xmlGenericError(xmlGenericErrorContext,
1103 "xmlScanAttributeDecl: dtd == NULL\n");
1104 return(NULL);
1105 }
1106 if (elem == NULL) {
1107 xmlGenericError(xmlGenericErrorContext,
1108 "xmlScanAttributeDecl: elem == NULL\n");
1109 return(NULL);
1110 }
1111 table = (xmlAttributeTablePtr) dtd->attributes;
1112 if (table == NULL)
1113 return(NULL);
1114
1115 /* WRONG !!! */
1116 xmlHashScan3(table, NULL, NULL, elem,
1117 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1118 return(ret);
1119}
1120
1121/**
1122 * xmlScanIDAttributeDecl:
1123 * @ctxt: the validation context
1124 * @elem: the element name
1125 *
1126 * Verify that the element don't have too many ID attributes
1127 * declared.
1128 *
1129 * Returns the number of ID attributes found.
1130 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001131static int
Owen Taylor3473f882001-02-23 17:55:21 +00001132xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1133 xmlAttributePtr cur;
1134 int ret = 0;
1135
1136 if (elem == NULL) return(0);
1137 cur = elem->attributes;
1138 while (cur != NULL) {
1139 if (cur->atype == XML_ATTRIBUTE_ID) {
1140 ret ++;
1141 if (ret > 1)
1142 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001143 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001144 elem->name, cur->name);
1145 }
1146 cur = cur->nexth;
1147 }
1148 return(ret);
1149}
1150
1151/**
1152 * xmlFreeAttribute:
1153 * @elem: An attribute
1154 *
1155 * Deallocate the memory used by an attribute definition
1156 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001157static void
Owen Taylor3473f882001-02-23 17:55:21 +00001158xmlFreeAttribute(xmlAttributePtr attr) {
1159 if (attr == NULL) return;
1160 xmlUnlinkNode((xmlNodePtr) attr);
1161 if (attr->tree != NULL)
1162 xmlFreeEnumeration(attr->tree);
1163 if (attr->elem != NULL)
1164 xmlFree((xmlChar *) attr->elem);
1165 if (attr->name != NULL)
1166 xmlFree((xmlChar *) attr->name);
1167 if (attr->defaultValue != NULL)
1168 xmlFree((xmlChar *) attr->defaultValue);
1169 if (attr->prefix != NULL)
1170 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001171 xmlFree(attr);
1172}
1173
1174
1175/**
1176 * xmlAddAttributeDecl:
1177 * @ctxt: the validation context
1178 * @dtd: pointer to the DTD
1179 * @elem: the element name
1180 * @name: the attribute name
1181 * @ns: the attribute namespace prefix
1182 * @type: the attribute type
1183 * @def: the attribute default type
1184 * @defaultValue: the attribute default value
1185 * @tree: if it's an enumeration, the associated list
1186 *
1187 * Register a new attribute declaration
1188 * Note that @tree becomes the ownership of the DTD
1189 *
1190 * Returns NULL if not new, othervise the attribute decl
1191 */
1192xmlAttributePtr
1193xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1194 const xmlChar *name, const xmlChar *ns,
1195 xmlAttributeType type, xmlAttributeDefault def,
1196 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1197 xmlAttributePtr ret;
1198 xmlAttributeTablePtr table;
1199 xmlElementPtr elemDef;
1200
1201 if (dtd == NULL) {
1202 xmlGenericError(xmlGenericErrorContext,
1203 "xmlAddAttributeDecl: dtd == NULL\n");
1204 xmlFreeEnumeration(tree);
1205 return(NULL);
1206 }
1207 if (name == NULL) {
1208 xmlGenericError(xmlGenericErrorContext,
1209 "xmlAddAttributeDecl: name == NULL\n");
1210 xmlFreeEnumeration(tree);
1211 return(NULL);
1212 }
1213 if (elem == NULL) {
1214 xmlGenericError(xmlGenericErrorContext,
1215 "xmlAddAttributeDecl: elem == NULL\n");
1216 xmlFreeEnumeration(tree);
1217 return(NULL);
1218 }
1219 /*
1220 * Check the type and possibly the default value.
1221 */
1222 switch (type) {
1223 case XML_ATTRIBUTE_CDATA:
1224 break;
1225 case XML_ATTRIBUTE_ID:
1226 break;
1227 case XML_ATTRIBUTE_IDREF:
1228 break;
1229 case XML_ATTRIBUTE_IDREFS:
1230 break;
1231 case XML_ATTRIBUTE_ENTITY:
1232 break;
1233 case XML_ATTRIBUTE_ENTITIES:
1234 break;
1235 case XML_ATTRIBUTE_NMTOKEN:
1236 break;
1237 case XML_ATTRIBUTE_NMTOKENS:
1238 break;
1239 case XML_ATTRIBUTE_ENUMERATION:
1240 break;
1241 case XML_ATTRIBUTE_NOTATION:
1242 break;
1243 default:
1244 xmlGenericError(xmlGenericErrorContext,
1245 "xmlAddAttributeDecl: unknown type %d\n", type);
1246 xmlFreeEnumeration(tree);
1247 return(NULL);
1248 }
1249 if ((defaultValue != NULL) &&
1250 (!xmlValidateAttributeValue(type, defaultValue))) {
1251 VERROR(ctxt->userData, "Attribute %s on %s: invalid default value\n",
1252 elem, name, defaultValue);
1253 defaultValue = NULL;
1254 }
1255
1256 /*
1257 * Create the Attribute table if needed.
1258 */
1259 table = (xmlAttributeTablePtr) dtd->attributes;
1260 if (table == NULL) {
1261 table = xmlCreateAttributeTable();
1262 dtd->attributes = (void *) table;
1263 }
1264 if (table == NULL) {
1265 xmlGenericError(xmlGenericErrorContext,
1266 "xmlAddAttributeDecl: Table creation failed!\n");
1267 return(NULL);
1268 }
1269
1270
1271 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1272 if (ret == NULL) {
1273 xmlGenericError(xmlGenericErrorContext,
1274 "xmlAddAttributeDecl: out of memory\n");
1275 return(NULL);
1276 }
1277 memset(ret, 0, sizeof(xmlAttribute));
1278 ret->type = XML_ATTRIBUTE_DECL;
1279
1280 /*
1281 * fill the structure.
1282 */
1283 ret->atype = type;
1284 ret->name = xmlStrdup(name);
1285 ret->prefix = xmlStrdup(ns);
1286 ret->elem = xmlStrdup(elem);
1287 ret->def = def;
1288 ret->tree = tree;
1289 if (defaultValue != NULL)
1290 ret->defaultValue = xmlStrdup(defaultValue);
1291
1292 /*
1293 * Validity Check:
1294 * Search the DTD for previous declarations of the ATTLIST
1295 */
1296 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1297 /*
1298 * The attribute is already defined in this Dtd.
1299 */
1300 VWARNING(ctxt->userData,
1301 "Attribute %s on %s: already defined\n",
1302 name, elem);
1303 xmlFreeAttribute(ret);
1304 return(NULL);
1305 }
1306
1307 /*
1308 * Validity Check:
1309 * Multiple ID per element
1310 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001311 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001312 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001313
Owen Taylor3473f882001-02-23 17:55:21 +00001314 if ((type == XML_ATTRIBUTE_ID) &&
1315 (xmlScanIDAttributeDecl(NULL, elemDef) != 0))
1316 VERROR(ctxt->userData,
1317 "Element %s has too may ID attributes defined : %s\n",
1318 elem, name);
Daniel Veillard48da9102001-08-07 01:10:10 +00001319 /*
1320 * Insert namespace default def first they need to be
1321 * processed firt.
1322 */
1323 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1324 ((ret->prefix != NULL &&
1325 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1326 ret->nexth = elemDef->attributes;
1327 elemDef->attributes = ret;
1328 } else {
1329 xmlAttributePtr tmp = elemDef->attributes;
1330
1331 while ((tmp != NULL) &&
1332 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1333 ((ret->prefix != NULL &&
1334 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1335 if (tmp->nexth == NULL)
1336 break;
1337 tmp = tmp->nexth;
1338 }
1339 if (tmp != NULL) {
1340 ret->nexth = tmp->nexth;
1341 tmp->nexth = ret;
1342 } else {
1343 ret->nexth = elemDef->attributes;
1344 elemDef->attributes = ret;
1345 }
1346 }
Owen Taylor3473f882001-02-23 17:55:21 +00001347 }
1348
1349 /*
1350 * Link it to the Dtd
1351 */
1352 ret->parent = dtd;
1353 ret->doc = dtd->doc;
1354 if (dtd->last == NULL) {
1355 dtd->children = dtd->last = (xmlNodePtr) ret;
1356 } else {
1357 dtd->last->next = (xmlNodePtr) ret;
1358 ret->prev = dtd->last;
1359 dtd->last = (xmlNodePtr) ret;
1360 }
1361 return(ret);
1362}
1363
1364/**
1365 * xmlFreeAttributeTable:
1366 * @table: An attribute table
1367 *
1368 * Deallocate the memory used by an entities hash table.
1369 */
1370void
1371xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1372 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1373}
1374
1375/**
1376 * xmlCopyAttribute:
1377 * @attr: An attribute
1378 *
1379 * Build a copy of an attribute.
1380 *
1381 * Returns the new xmlAttributePtr or NULL in case of error.
1382 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001383static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001384xmlCopyAttribute(xmlAttributePtr attr) {
1385 xmlAttributePtr cur;
1386
1387 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1388 if (cur == NULL) {
1389 xmlGenericError(xmlGenericErrorContext,
1390 "xmlCopyAttribute: out of memory !\n");
1391 return(NULL);
1392 }
1393 memset(cur, 0, sizeof(xmlAttribute));
1394 cur->atype = attr->atype;
1395 cur->def = attr->def;
1396 cur->tree = xmlCopyEnumeration(attr->tree);
1397 if (attr->elem != NULL)
1398 cur->elem = xmlStrdup(attr->elem);
1399 if (attr->name != NULL)
1400 cur->name = xmlStrdup(attr->name);
1401 if (attr->defaultValue != NULL)
1402 cur->defaultValue = xmlStrdup(attr->defaultValue);
1403 return(cur);
1404}
1405
1406/**
1407 * xmlCopyAttributeTable:
1408 * @table: An attribute table
1409 *
1410 * Build a copy of an attribute table.
1411 *
1412 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1413 */
1414xmlAttributeTablePtr
1415xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1416 return((xmlAttributeTablePtr) xmlHashCopy(table,
1417 (xmlHashCopier) xmlCopyAttribute));
1418}
1419
1420/**
1421 * xmlDumpAttributeDecl:
1422 * @buf: the XML buffer output
1423 * @attr: An attribute declaration
1424 *
1425 * This will dump the content of the attribute declaration as an XML
1426 * DTD definition
1427 */
1428void
1429xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1430 xmlBufferWriteChar(buf, "<!ATTLIST ");
1431 xmlBufferWriteCHAR(buf, attr->elem);
1432 xmlBufferWriteChar(buf, " ");
1433 if (attr->prefix != NULL) {
1434 xmlBufferWriteCHAR(buf, attr->prefix);
1435 xmlBufferWriteChar(buf, ":");
1436 }
1437 xmlBufferWriteCHAR(buf, attr->name);
1438 switch (attr->atype) {
1439 case XML_ATTRIBUTE_CDATA:
1440 xmlBufferWriteChar(buf, " CDATA");
1441 break;
1442 case XML_ATTRIBUTE_ID:
1443 xmlBufferWriteChar(buf, " ID");
1444 break;
1445 case XML_ATTRIBUTE_IDREF:
1446 xmlBufferWriteChar(buf, " IDREF");
1447 break;
1448 case XML_ATTRIBUTE_IDREFS:
1449 xmlBufferWriteChar(buf, " IDREFS");
1450 break;
1451 case XML_ATTRIBUTE_ENTITY:
1452 xmlBufferWriteChar(buf, " ENTITY");
1453 break;
1454 case XML_ATTRIBUTE_ENTITIES:
1455 xmlBufferWriteChar(buf, " ENTITIES");
1456 break;
1457 case XML_ATTRIBUTE_NMTOKEN:
1458 xmlBufferWriteChar(buf, " NMTOKEN");
1459 break;
1460 case XML_ATTRIBUTE_NMTOKENS:
1461 xmlBufferWriteChar(buf, " NMTOKENS");
1462 break;
1463 case XML_ATTRIBUTE_ENUMERATION:
1464 xmlBufferWriteChar(buf, " (");
1465 xmlDumpEnumeration(buf, attr->tree);
1466 break;
1467 case XML_ATTRIBUTE_NOTATION:
1468 xmlBufferWriteChar(buf, " NOTATION (");
1469 xmlDumpEnumeration(buf, attr->tree);
1470 break;
1471 default:
1472 xmlGenericError(xmlGenericErrorContext,
1473 "xmlDumpAttributeTable: internal: unknown type %d\n",
1474 attr->atype);
1475 }
1476 switch (attr->def) {
1477 case XML_ATTRIBUTE_NONE:
1478 break;
1479 case XML_ATTRIBUTE_REQUIRED:
1480 xmlBufferWriteChar(buf, " #REQUIRED");
1481 break;
1482 case XML_ATTRIBUTE_IMPLIED:
1483 xmlBufferWriteChar(buf, " #IMPLIED");
1484 break;
1485 case XML_ATTRIBUTE_FIXED:
1486 xmlBufferWriteChar(buf, " #FIXED");
1487 break;
1488 default:
1489 xmlGenericError(xmlGenericErrorContext,
1490 "xmlDumpAttributeTable: internal: unknown default %d\n",
1491 attr->def);
1492 }
1493 if (attr->defaultValue != NULL) {
1494 xmlBufferWriteChar(buf, " ");
1495 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1496 }
1497 xmlBufferWriteChar(buf, ">\n");
1498}
1499
1500/**
1501 * xmlDumpAttributeTable:
1502 * @buf: the XML buffer output
1503 * @table: An attribute table
1504 *
1505 * This will dump the content of the attribute table as an XML DTD definition
1506 */
1507void
1508xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1509 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1510}
1511
1512/************************************************************************
1513 * *
1514 * NOTATIONs *
1515 * *
1516 ************************************************************************/
1517/**
1518 * xmlCreateNotationTable:
1519 *
1520 * create and initialize an empty notation hash table.
1521 *
1522 * Returns the xmlNotationTablePtr just created or NULL in case
1523 * of error.
1524 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001525static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001526xmlCreateNotationTable(void) {
1527 return(xmlHashCreate(0));
1528}
1529
1530/**
1531 * xmlFreeNotation:
1532 * @not: A notation
1533 *
1534 * Deallocate the memory used by an notation definition
1535 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001536static void
Owen Taylor3473f882001-02-23 17:55:21 +00001537xmlFreeNotation(xmlNotationPtr nota) {
1538 if (nota == NULL) return;
1539 if (nota->name != NULL)
1540 xmlFree((xmlChar *) nota->name);
1541 if (nota->PublicID != NULL)
1542 xmlFree((xmlChar *) nota->PublicID);
1543 if (nota->SystemID != NULL)
1544 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001545 xmlFree(nota);
1546}
1547
1548
1549/**
1550 * xmlAddNotationDecl:
1551 * @dtd: pointer to the DTD
1552 * @ctxt: the validation context
1553 * @name: the entity name
1554 * @PublicID: the public identifier or NULL
1555 * @SystemID: the system identifier or NULL
1556 *
1557 * Register a new notation declaration
1558 *
1559 * Returns NULL if not, othervise the entity
1560 */
1561xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001562xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001563 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001564 const xmlChar *PublicID, const xmlChar *SystemID) {
1565 xmlNotationPtr ret;
1566 xmlNotationTablePtr table;
1567
1568 if (dtd == NULL) {
1569 xmlGenericError(xmlGenericErrorContext,
1570 "xmlAddNotationDecl: dtd == NULL\n");
1571 return(NULL);
1572 }
1573 if (name == NULL) {
1574 xmlGenericError(xmlGenericErrorContext,
1575 "xmlAddNotationDecl: name == NULL\n");
1576 return(NULL);
1577 }
1578 if ((PublicID == NULL) && (SystemID == NULL)) {
1579 xmlGenericError(xmlGenericErrorContext,
1580 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
1581 }
1582
1583 /*
1584 * Create the Notation table if needed.
1585 */
1586 table = (xmlNotationTablePtr) dtd->notations;
1587 if (table == NULL)
1588 dtd->notations = table = xmlCreateNotationTable();
1589 if (table == NULL) {
1590 xmlGenericError(xmlGenericErrorContext,
1591 "xmlAddNotationDecl: Table creation failed!\n");
1592 return(NULL);
1593 }
1594
1595 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1596 if (ret == NULL) {
1597 xmlGenericError(xmlGenericErrorContext,
1598 "xmlAddNotationDecl: out of memory\n");
1599 return(NULL);
1600 }
1601 memset(ret, 0, sizeof(xmlNotation));
1602
1603 /*
1604 * fill the structure.
1605 */
1606 ret->name = xmlStrdup(name);
1607 if (SystemID != NULL)
1608 ret->SystemID = xmlStrdup(SystemID);
1609 if (PublicID != NULL)
1610 ret->PublicID = xmlStrdup(PublicID);
1611
1612 /*
1613 * Validity Check:
1614 * Check the DTD for previous declarations of the ATTLIST
1615 */
1616 if (xmlHashAddEntry(table, name, ret)) {
1617 xmlGenericError(xmlGenericErrorContext,
1618 "xmlAddNotationDecl: %s already defined\n", name);
1619 xmlFreeNotation(ret);
1620 return(NULL);
1621 }
1622 return(ret);
1623}
1624
1625/**
1626 * xmlFreeNotationTable:
1627 * @table: An notation table
1628 *
1629 * Deallocate the memory used by an entities hash table.
1630 */
1631void
1632xmlFreeNotationTable(xmlNotationTablePtr table) {
1633 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
1634}
1635
1636/**
1637 * xmlCopyNotation:
1638 * @nota: A notation
1639 *
1640 * Build a copy of a notation.
1641 *
1642 * Returns the new xmlNotationPtr or NULL in case of error.
1643 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001644static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001645xmlCopyNotation(xmlNotationPtr nota) {
1646 xmlNotationPtr cur;
1647
1648 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1649 if (cur == NULL) {
1650 xmlGenericError(xmlGenericErrorContext,
1651 "xmlCopyNotation: out of memory !\n");
1652 return(NULL);
1653 }
1654 if (nota->name != NULL)
1655 cur->name = xmlStrdup(nota->name);
1656 else
1657 cur->name = NULL;
1658 if (nota->PublicID != NULL)
1659 cur->PublicID = xmlStrdup(nota->PublicID);
1660 else
1661 cur->PublicID = NULL;
1662 if (nota->SystemID != NULL)
1663 cur->SystemID = xmlStrdup(nota->SystemID);
1664 else
1665 cur->SystemID = NULL;
1666 return(cur);
1667}
1668
1669/**
1670 * xmlCopyNotationTable:
1671 * @table: A notation table
1672 *
1673 * Build a copy of a notation table.
1674 *
1675 * Returns the new xmlNotationTablePtr or NULL in case of error.
1676 */
1677xmlNotationTablePtr
1678xmlCopyNotationTable(xmlNotationTablePtr table) {
1679 return((xmlNotationTablePtr) xmlHashCopy(table,
1680 (xmlHashCopier) xmlCopyNotation));
1681}
1682
1683/**
1684 * xmlDumpNotationDecl:
1685 * @buf: the XML buffer output
1686 * @nota: A notation declaration
1687 *
1688 * This will dump the content the notation declaration as an XML DTD definition
1689 */
1690void
1691xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
1692 xmlBufferWriteChar(buf, "<!NOTATION ");
1693 xmlBufferWriteCHAR(buf, nota->name);
1694 if (nota->PublicID != NULL) {
1695 xmlBufferWriteChar(buf, " PUBLIC ");
1696 xmlBufferWriteQuotedString(buf, nota->PublicID);
1697 if (nota->SystemID != NULL) {
1698 xmlBufferWriteChar(buf, " ");
1699 xmlBufferWriteCHAR(buf, nota->SystemID);
1700 }
1701 } else {
1702 xmlBufferWriteChar(buf, " SYSTEM ");
1703 xmlBufferWriteCHAR(buf, nota->SystemID);
1704 }
1705 xmlBufferWriteChar(buf, " >\n");
1706}
1707
1708/**
1709 * xmlDumpNotationTable:
1710 * @buf: the XML buffer output
1711 * @table: A notation table
1712 *
1713 * This will dump the content of the notation table as an XML DTD definition
1714 */
1715void
1716xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
1717 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
1718}
1719
1720/************************************************************************
1721 * *
1722 * IDs *
1723 * *
1724 ************************************************************************/
1725/**
1726 * xmlCreateIDTable:
1727 *
1728 * create and initialize an empty id hash table.
1729 *
1730 * Returns the xmlIDTablePtr just created or NULL in case
1731 * of error.
1732 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001733static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001734xmlCreateIDTable(void) {
1735 return(xmlHashCreate(0));
1736}
1737
1738/**
1739 * xmlFreeID:
1740 * @not: A id
1741 *
1742 * Deallocate the memory used by an id definition
1743 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001744static void
Owen Taylor3473f882001-02-23 17:55:21 +00001745xmlFreeID(xmlIDPtr id) {
1746 if (id == NULL) return;
1747 if (id->value != NULL)
1748 xmlFree((xmlChar *) id->value);
Owen Taylor3473f882001-02-23 17:55:21 +00001749 xmlFree(id);
1750}
1751
1752/**
1753 * xmlAddID:
1754 * @ctxt: the validation context
1755 * @doc: pointer to the document
1756 * @value: the value name
1757 * @attr: the attribute holding the ID
1758 *
1759 * Register a new id declaration
1760 *
1761 * Returns NULL if not, othervise the new xmlIDPtr
1762 */
1763xmlIDPtr
1764xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
1765 xmlAttrPtr attr) {
1766 xmlIDPtr ret;
1767 xmlIDTablePtr table;
1768
1769 if (doc == NULL) {
1770 xmlGenericError(xmlGenericErrorContext,
1771 "xmlAddIDDecl: doc == NULL\n");
1772 return(NULL);
1773 }
1774 if (value == NULL) {
1775 xmlGenericError(xmlGenericErrorContext,
1776 "xmlAddIDDecl: value == NULL\n");
1777 return(NULL);
1778 }
1779 if (attr == NULL) {
1780 xmlGenericError(xmlGenericErrorContext,
1781 "xmlAddIDDecl: attr == NULL\n");
1782 return(NULL);
1783 }
1784
1785 /*
1786 * Create the ID table if needed.
1787 */
1788 table = (xmlIDTablePtr) doc->ids;
1789 if (table == NULL)
1790 doc->ids = table = xmlCreateIDTable();
1791 if (table == NULL) {
1792 xmlGenericError(xmlGenericErrorContext,
1793 "xmlAddID: Table creation failed!\n");
1794 return(NULL);
1795 }
1796
1797 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
1798 if (ret == NULL) {
1799 xmlGenericError(xmlGenericErrorContext,
1800 "xmlAddID: out of memory\n");
1801 return(NULL);
1802 }
1803
1804 /*
1805 * fill the structure.
1806 */
1807 ret->value = xmlStrdup(value);
1808 ret->attr = attr;
1809
1810 if (xmlHashAddEntry(table, value, ret) < 0) {
1811 /*
1812 * The id is already defined in this Dtd.
1813 */
1814 VERROR(ctxt->userData, "ID %s already defined\n", value);
1815 xmlFreeID(ret);
1816 return(NULL);
1817 }
1818 return(ret);
1819}
1820
1821/**
1822 * xmlFreeIDTable:
1823 * @table: An id table
1824 *
1825 * Deallocate the memory used by an ID hash table.
1826 */
1827void
1828xmlFreeIDTable(xmlIDTablePtr table) {
1829 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
1830}
1831
1832/**
1833 * xmlIsID:
1834 * @doc: the document
1835 * @elem: the element carrying the attribute
1836 * @attr: the attribute
1837 *
1838 * Determine whether an attribute is of type ID. In case we have Dtd(s)
1839 * then this is simple, otherwise we use an heuristic: name ID (upper
1840 * or lowercase).
1841 *
1842 * Returns 0 or 1 depending on the lookup result
1843 */
1844int
1845xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
1846 if (doc == NULL) return(0);
1847 if (attr == NULL) return(0);
1848 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
1849 return(0);
1850 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1851 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
1852 (xmlStrEqual(BAD_CAST "name", attr->name)))
1853 return(1);
1854 return(0);
1855 } else {
1856 xmlAttributePtr attrDecl;
1857
1858 if (elem == NULL) return(0);
1859 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
1860 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1861 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
1862 attr->name);
1863
1864 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
1865 return(1);
1866 }
1867 return(0);
1868}
1869
1870/**
1871 * xmlRemoveID
1872 * @doc: the document
1873 * @attr: the attribute
1874 *
1875 * Remove the given attribute from the ID table maintained internally.
1876 *
1877 * Returns -1 if the lookup failed and 0 otherwise
1878 */
1879int
1880xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
1881 xmlAttrPtr cur;
1882 xmlIDTablePtr table;
1883 xmlChar *ID;
1884
1885 if (doc == NULL) return(-1);
1886 if (attr == NULL) return(-1);
1887 table = (xmlIDTablePtr) doc->ids;
1888 if (table == NULL)
1889 return(-1);
1890
1891 if (attr == NULL)
1892 return(-1);
1893 ID = xmlNodeListGetString(doc, attr->children, 1);
1894 if (ID == NULL)
1895 return(-1);
1896 cur = xmlHashLookup(table, ID);
1897 if (cur != attr) {
1898 xmlFree(ID);
1899 return(-1);
1900 }
1901 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
1902 xmlFree(ID);
1903 return(0);
1904}
1905
1906/**
1907 * xmlGetID:
1908 * @doc: pointer to the document
1909 * @ID: the ID value
1910 *
1911 * Search the attribute declaring the given ID
1912 *
1913 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
1914 */
1915xmlAttrPtr
1916xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
1917 xmlIDTablePtr table;
1918 xmlIDPtr id;
1919
1920 if (doc == NULL) {
1921 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
1922 return(NULL);
1923 }
1924
1925 if (ID == NULL) {
1926 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
1927 return(NULL);
1928 }
1929
1930 table = (xmlIDTablePtr) doc->ids;
1931 if (table == NULL)
1932 return(NULL);
1933
1934 id = xmlHashLookup(table, ID);
1935 if (id == NULL)
1936 return(NULL);
1937 return(id->attr);
1938}
1939
1940/************************************************************************
1941 * *
1942 * Refs *
1943 * *
1944 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00001945typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00001946{
1947 xmlListPtr l;
1948 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00001949} xmlRemoveMemo;
1950
1951typedef xmlRemoveMemo *xmlRemoveMemoPtr;
1952
1953typedef struct xmlValidateMemo_t
1954{
1955 xmlValidCtxtPtr ctxt;
1956 const xmlChar *name;
1957} xmlValidateMemo;
1958
1959typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00001960
1961/**
1962 * xmlCreateRefTable:
1963 *
1964 * create and initialize an empty ref hash table.
1965 *
1966 * Returns the xmlRefTablePtr just created or NULL in case
1967 * of error.
1968 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001969static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001970xmlCreateRefTable(void) {
1971 return(xmlHashCreate(0));
1972}
1973
1974/**
1975 * xmlFreeRef:
1976 * @lk: A list link
1977 *
1978 * Deallocate the memory used by a ref definition
1979 */
1980static void
1981xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00001982 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
1983 if (ref == NULL) return;
1984 if (ref->value != NULL)
1985 xmlFree((xmlChar *)ref->value);
1986 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00001987}
1988
1989/**
1990 * xmlFreeRefList:
1991 * @list_ref: A list of references.
1992 *
1993 * Deallocate the memory used by a list of references
1994 */
1995static void
1996xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00001997 if (list_ref == NULL) return;
1998 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00001999}
2000
2001/**
2002 * xmlWalkRemoveRef:
2003 * @data: Contents of current link
2004 * @user: Value supplied by the user
2005 *
2006 * Return 0 to abort the walk or 1 to continue
2007 */
2008static int
2009xmlWalkRemoveRef(const void *data, const void *user)
2010{
Daniel Veillard37721922001-05-04 15:21:12 +00002011 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2012 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2013 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002014
Daniel Veillard37721922001-05-04 15:21:12 +00002015 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2016 xmlListRemoveFirst(ref_list, (void *)data);
2017 return 0;
2018 }
2019 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002020}
2021
2022/**
2023 * xmlAddRef:
2024 * @ctxt: the validation context
2025 * @doc: pointer to the document
2026 * @value: the value name
2027 * @attr: the attribute holding the Ref
2028 *
2029 * Register a new ref declaration
2030 *
2031 * Returns NULL if not, othervise the new xmlRefPtr
2032 */
2033xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002034xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002035 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002036 xmlRefPtr ret;
2037 xmlRefTablePtr table;
2038 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002039
Daniel Veillard37721922001-05-04 15:21:12 +00002040 if (doc == NULL) {
2041 xmlGenericError(xmlGenericErrorContext,
2042 "xmlAddRefDecl: doc == NULL\n");
2043 return(NULL);
2044 }
2045 if (value == NULL) {
2046 xmlGenericError(xmlGenericErrorContext,
2047 "xmlAddRefDecl: value == NULL\n");
2048 return(NULL);
2049 }
2050 if (attr == NULL) {
2051 xmlGenericError(xmlGenericErrorContext,
2052 "xmlAddRefDecl: attr == NULL\n");
2053 return(NULL);
2054 }
Owen Taylor3473f882001-02-23 17:55:21 +00002055
Daniel Veillard37721922001-05-04 15:21:12 +00002056 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002057 * Create the Ref table if needed.
2058 */
Daniel Veillard37721922001-05-04 15:21:12 +00002059 table = (xmlRefTablePtr) doc->refs;
2060 if (table == NULL)
2061 doc->refs = table = xmlCreateRefTable();
2062 if (table == NULL) {
2063 xmlGenericError(xmlGenericErrorContext,
2064 "xmlAddRef: Table creation failed!\n");
2065 return(NULL);
2066 }
Owen Taylor3473f882001-02-23 17:55:21 +00002067
Daniel Veillard37721922001-05-04 15:21:12 +00002068 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2069 if (ret == NULL) {
2070 xmlGenericError(xmlGenericErrorContext,
2071 "xmlAddRef: out of memory\n");
2072 return(NULL);
2073 }
Owen Taylor3473f882001-02-23 17:55:21 +00002074
Daniel Veillard37721922001-05-04 15:21:12 +00002075 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002076 * fill the structure.
2077 */
Daniel Veillard37721922001-05-04 15:21:12 +00002078 ret->value = xmlStrdup(value);
2079 ret->attr = attr;
Owen Taylor3473f882001-02-23 17:55:21 +00002080
Daniel Veillard37721922001-05-04 15:21:12 +00002081 /* To add a reference :-
2082 * References are maintained as a list of references,
2083 * Lookup the entry, if no entry create new nodelist
2084 * Add the owning node to the NodeList
2085 * Return the ref
2086 */
Owen Taylor3473f882001-02-23 17:55:21 +00002087
Daniel Veillard37721922001-05-04 15:21:12 +00002088 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2089 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2090 xmlGenericError(xmlGenericErrorContext,
2091 "xmlAddRef: Reference list creation failed!\n");
2092 return(NULL);
2093 }
2094 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2095 xmlListDelete(ref_list);
2096 xmlGenericError(xmlGenericErrorContext,
2097 "xmlAddRef: Reference list insertion failed!\n");
2098 return(NULL);
2099 }
2100 }
2101 xmlListInsert(ref_list, ret);
2102 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002103}
2104
2105/**
2106 * xmlFreeRefTable:
2107 * @table: An ref table
2108 *
2109 * Deallocate the memory used by an Ref hash table.
2110 */
2111void
2112xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002113 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002114}
2115
2116/**
2117 * xmlIsRef:
2118 * @doc: the document
2119 * @elem: the element carrying the attribute
2120 * @attr: the attribute
2121 *
2122 * Determine whether an attribute is of type Ref. In case we have Dtd(s)
2123 * then this is simple, otherwise we use an heuristic: name Ref (upper
2124 * or lowercase).
2125 *
2126 * Returns 0 or 1 depending on the lookup result
2127 */
2128int
2129xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002130 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2131 return(0);
2132 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2133 /* TODO @@@ */
2134 return(0);
2135 } else {
2136 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002137
Daniel Veillard37721922001-05-04 15:21:12 +00002138 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2139 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2140 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2141 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002142
Daniel Veillard37721922001-05-04 15:21:12 +00002143 if ((attrDecl != NULL) &&
2144 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2145 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2146 return(1);
2147 }
2148 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002149}
2150
2151/**
2152 * xmlRemoveRef
2153 * @doc: the document
2154 * @attr: the attribute
2155 *
2156 * Remove the given attribute from the Ref table maintained internally.
2157 *
2158 * Returns -1 if the lookup failed and 0 otherwise
2159 */
2160int
2161xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002162 xmlListPtr ref_list;
2163 xmlRefTablePtr table;
2164 xmlChar *ID;
2165 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002166
Daniel Veillard37721922001-05-04 15:21:12 +00002167 if (doc == NULL) return(-1);
2168 if (attr == NULL) return(-1);
2169 table = (xmlRefTablePtr) doc->refs;
2170 if (table == NULL)
2171 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002172
Daniel Veillard37721922001-05-04 15:21:12 +00002173 if (attr == NULL)
2174 return(-1);
2175 ID = xmlNodeListGetString(doc, attr->children, 1);
2176 if (ID == NULL)
2177 return(-1);
2178 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002179
Daniel Veillard37721922001-05-04 15:21:12 +00002180 if(ref_list == NULL) {
2181 xmlFree(ID);
2182 return (-1);
2183 }
2184 /* At this point, ref_list refers to a list of references which
2185 * have the same key as the supplied attr. Our list of references
2186 * is ordered by reference address and we don't have that information
2187 * here to use when removing. We'll have to walk the list and
2188 * check for a matching attribute, when we find one stop the walk
2189 * and remove the entry.
2190 * The list is ordered by reference, so that means we don't have the
2191 * key. Passing the list and the reference to the walker means we
2192 * will have enough data to be able to remove the entry.
2193 */
2194 target.l = ref_list;
2195 target.ap = attr;
2196
2197 /* Remove the supplied attr from our list */
2198 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002199
Daniel Veillard37721922001-05-04 15:21:12 +00002200 /*If the list is empty then remove the list entry in the hash */
2201 if (xmlListEmpty(ref_list))
2202 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2203 xmlFreeRefList);
2204 xmlFree(ID);
2205 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002206}
2207
2208/**
2209 * xmlGetRefs:
2210 * @doc: pointer to the document
2211 * @ID: the ID value
2212 *
2213 * Find the set of references for the supplied ID.
2214 *
2215 * Returns NULL if not found, otherwise node set for the ID.
2216 */
2217xmlListPtr
2218xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002219 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002220
Daniel Veillard37721922001-05-04 15:21:12 +00002221 if (doc == NULL) {
2222 xmlGenericError(xmlGenericErrorContext, "xmlGetRef: doc == NULL\n");
2223 return(NULL);
2224 }
Owen Taylor3473f882001-02-23 17:55:21 +00002225
Daniel Veillard37721922001-05-04 15:21:12 +00002226 if (ID == NULL) {
2227 xmlGenericError(xmlGenericErrorContext, "xmlGetRef: ID == NULL\n");
2228 return(NULL);
2229 }
Owen Taylor3473f882001-02-23 17:55:21 +00002230
Daniel Veillard37721922001-05-04 15:21:12 +00002231 table = (xmlRefTablePtr) doc->refs;
2232 if (table == NULL)
2233 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002234
Daniel Veillard37721922001-05-04 15:21:12 +00002235 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002236}
2237
2238/************************************************************************
2239 * *
2240 * Routines for validity checking *
2241 * *
2242 ************************************************************************/
2243
2244/**
2245 * xmlGetDtdElementDesc:
2246 * @dtd: a pointer to the DtD to search
2247 * @name: the element name
2248 *
2249 * Search the Dtd for the description of this element
2250 *
2251 * returns the xmlElementPtr if found or NULL
2252 */
2253
2254xmlElementPtr
2255xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2256 xmlElementTablePtr table;
2257 xmlElementPtr cur;
2258 xmlChar *uqname = NULL, *prefix = NULL;
2259
2260 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002261 if (dtd->elements == NULL)
2262 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002263 table = (xmlElementTablePtr) dtd->elements;
2264
2265 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002266 if (uqname != NULL)
2267 name = uqname;
2268 cur = xmlHashLookup2(table, name, prefix);
2269 if (prefix != NULL) xmlFree(prefix);
2270 if (uqname != NULL) xmlFree(uqname);
2271 return(cur);
2272}
2273/**
2274 * xmlGetDtdElementDesc2:
2275 * @dtd: a pointer to the DtD to search
2276 * @name: the element name
2277 * @create: create an empty description if not found
2278 *
2279 * Search the Dtd for the description of this element
2280 *
2281 * returns the xmlElementPtr if found or NULL
2282 */
2283
2284xmlElementPtr
2285xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2286 xmlElementTablePtr table;
2287 xmlElementPtr cur;
2288 xmlChar *uqname = NULL, *prefix = NULL;
2289
2290 if (dtd == NULL) return(NULL);
2291 if (dtd->elements == NULL) {
2292 if (!create)
2293 return(NULL);
2294 /*
2295 * Create the Element table if needed.
2296 */
2297 table = (xmlElementTablePtr) dtd->elements;
2298 if (table == NULL) {
2299 table = xmlCreateElementTable();
2300 dtd->elements = (void *) table;
2301 }
2302 if (table == NULL) {
2303 xmlGenericError(xmlGenericErrorContext,
2304 "xmlGetDtdElementDesc: Table creation failed!\n");
2305 return(NULL);
2306 }
2307 }
2308 table = (xmlElementTablePtr) dtd->elements;
2309
2310 uqname = xmlSplitQName2(name, &prefix);
2311 if (uqname != NULL)
2312 name = uqname;
2313 cur = xmlHashLookup2(table, name, prefix);
2314 if ((cur == NULL) && (create)) {
2315 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2316 if (cur == NULL) {
2317 xmlGenericError(xmlGenericErrorContext,
2318 "xmlGetDtdElementDesc: out of memory\n");
2319 return(NULL);
2320 }
2321 memset(cur, 0, sizeof(xmlElement));
2322 cur->type = XML_ELEMENT_DECL;
2323
2324 /*
2325 * fill the structure.
2326 */
2327 cur->name = xmlStrdup(name);
2328 cur->prefix = xmlStrdup(prefix);
2329 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2330
2331 xmlHashAddEntry2(table, name, prefix, cur);
2332 }
2333 if (prefix != NULL) xmlFree(prefix);
2334 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002335 return(cur);
2336}
2337
2338/**
2339 * xmlGetDtdQElementDesc:
2340 * @dtd: a pointer to the DtD to search
2341 * @name: the element name
2342 * @prefix: the element namespace prefix
2343 *
2344 * Search the Dtd for the description of this element
2345 *
2346 * returns the xmlElementPtr if found or NULL
2347 */
2348
Daniel Veillard48da9102001-08-07 01:10:10 +00002349xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002350xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2351 const xmlChar *prefix) {
2352 xmlElementTablePtr table;
2353
2354 if (dtd == NULL) return(NULL);
2355 if (dtd->elements == NULL) return(NULL);
2356 table = (xmlElementTablePtr) dtd->elements;
2357
2358 return(xmlHashLookup2(table, name, prefix));
2359}
2360
2361/**
2362 * xmlGetDtdAttrDesc:
2363 * @dtd: a pointer to the DtD to search
2364 * @elem: the element name
2365 * @name: the attribute name
2366 *
2367 * Search the Dtd for the description of this attribute on
2368 * this element.
2369 *
2370 * returns the xmlAttributePtr if found or NULL
2371 */
2372
2373xmlAttributePtr
2374xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2375 xmlAttributeTablePtr table;
2376 xmlAttributePtr cur;
2377 xmlChar *uqname = NULL, *prefix = NULL;
2378
2379 if (dtd == NULL) return(NULL);
2380 if (dtd->attributes == NULL) return(NULL);
2381
2382 table = (xmlAttributeTablePtr) dtd->attributes;
2383 if (table == NULL)
2384 return(NULL);
2385
2386 uqname = xmlSplitQName2(name, &prefix);
2387
2388 if (uqname != NULL) {
2389 cur = xmlHashLookup3(table, uqname, prefix, elem);
2390 if (prefix != NULL) xmlFree(prefix);
2391 if (uqname != NULL) xmlFree(uqname);
2392 } else
2393 cur = xmlHashLookup3(table, name, NULL, elem);
2394 return(cur);
2395}
2396
2397/**
2398 * xmlGetDtdQAttrDesc:
2399 * @dtd: a pointer to the DtD to search
2400 * @elem: the element name
2401 * @name: the attribute name
2402 * @prefix: the attribute namespace prefix
2403 *
2404 * Search the Dtd for the description of this qualified attribute on
2405 * this element.
2406 *
2407 * returns the xmlAttributePtr if found or NULL
2408 */
2409
Daniel Veillard48da9102001-08-07 01:10:10 +00002410xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002411xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2412 const xmlChar *prefix) {
2413 xmlAttributeTablePtr table;
2414
2415 if (dtd == NULL) return(NULL);
2416 if (dtd->attributes == NULL) return(NULL);
2417 table = (xmlAttributeTablePtr) dtd->attributes;
2418
2419 return(xmlHashLookup3(table, name, prefix, elem));
2420}
2421
2422/**
2423 * xmlGetDtdNotationDesc:
2424 * @dtd: a pointer to the DtD to search
2425 * @name: the notation name
2426 *
2427 * Search the Dtd for the description of this notation
2428 *
2429 * returns the xmlNotationPtr if found or NULL
2430 */
2431
2432xmlNotationPtr
2433xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2434 xmlNotationTablePtr table;
2435
2436 if (dtd == NULL) return(NULL);
2437 if (dtd->notations == NULL) return(NULL);
2438 table = (xmlNotationTablePtr) dtd->notations;
2439
2440 return(xmlHashLookup(table, name));
2441}
2442
2443/**
2444 * xmlValidateNotationUse:
2445 * @ctxt: the validation context
2446 * @doc: the document
2447 * @notationName: the notation name to check
2448 *
2449 * Validate that the given mame match a notation declaration.
2450 * - [ VC: Notation Declared ]
2451 *
2452 * returns 1 if valid or 0 otherwise
2453 */
2454
2455int
2456xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2457 const xmlChar *notationName) {
2458 xmlNotationPtr notaDecl;
2459 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2460
2461 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2462 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2463 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2464
2465 if (notaDecl == NULL) {
2466 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2467 notationName);
2468 return(0);
2469 }
2470 return(1);
2471}
2472
2473/**
2474 * xmlIsMixedElement
2475 * @doc: the document
2476 * @name: the element name
2477 *
2478 * Search in the DtDs whether an element accept Mixed content (or ANY)
2479 * basically if it is supposed to accept text childs
2480 *
2481 * returns 0 if no, 1 if yes, and -1 if no element description is available
2482 */
2483
2484int
2485xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2486 xmlElementPtr elemDecl;
2487
2488 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2489
2490 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2491 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2492 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2493 if (elemDecl == NULL) return(-1);
2494 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002495 case XML_ELEMENT_TYPE_UNDEFINED:
2496 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002497 case XML_ELEMENT_TYPE_ELEMENT:
2498 return(0);
2499 case XML_ELEMENT_TYPE_EMPTY:
2500 /*
2501 * return 1 for EMPTY since we want VC error to pop up
2502 * on <empty> </empty> for example
2503 */
2504 case XML_ELEMENT_TYPE_ANY:
2505 case XML_ELEMENT_TYPE_MIXED:
2506 return(1);
2507 }
2508 return(1);
2509}
2510
2511/**
2512 * xmlValidateNameValue:
2513 * @value: an Name value
2514 *
2515 * Validate that the given value match Name production
2516 *
2517 * returns 1 if valid or 0 otherwise
2518 */
2519
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002520static int
Owen Taylor3473f882001-02-23 17:55:21 +00002521xmlValidateNameValue(const xmlChar *value) {
2522 const xmlChar *cur;
2523
2524 if (value == NULL) return(0);
2525 cur = value;
2526
2527 if (!IS_LETTER(*cur) && (*cur != '_') &&
2528 (*cur != ':')) {
2529 return(0);
2530 }
2531
2532 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2533 (*cur == '.') || (*cur == '-') ||
2534 (*cur == '_') || (*cur == ':') ||
2535 (IS_COMBINING(*cur)) ||
2536 (IS_EXTENDER(*cur)))
2537 cur++;
2538
2539 if (*cur != 0) return(0);
2540
2541 return(1);
2542}
2543
2544/**
2545 * xmlValidateNamesValue:
2546 * @value: an Names value
2547 *
2548 * Validate that the given value match Names production
2549 *
2550 * returns 1 if valid or 0 otherwise
2551 */
2552
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002553static int
Owen Taylor3473f882001-02-23 17:55:21 +00002554xmlValidateNamesValue(const xmlChar *value) {
2555 const xmlChar *cur;
2556
2557 if (value == NULL) return(0);
2558 cur = value;
2559
2560 if (!IS_LETTER(*cur) && (*cur != '_') &&
2561 (*cur != ':')) {
2562 return(0);
2563 }
2564
2565 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2566 (*cur == '.') || (*cur == '-') ||
2567 (*cur == '_') || (*cur == ':') ||
2568 (IS_COMBINING(*cur)) ||
2569 (IS_EXTENDER(*cur)))
2570 cur++;
2571
2572 while (IS_BLANK(*cur)) {
2573 while (IS_BLANK(*cur)) cur++;
2574
2575 if (!IS_LETTER(*cur) && (*cur != '_') &&
2576 (*cur != ':')) {
2577 return(0);
2578 }
2579
2580 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2581 (*cur == '.') || (*cur == '-') ||
2582 (*cur == '_') || (*cur == ':') ||
2583 (IS_COMBINING(*cur)) ||
2584 (IS_EXTENDER(*cur)))
2585 cur++;
2586 }
2587
2588 if (*cur != 0) return(0);
2589
2590 return(1);
2591}
2592
2593/**
2594 * xmlValidateNmtokenValue:
2595 * @value: an Mntoken value
2596 *
2597 * Validate that the given value match Nmtoken production
2598 *
2599 * [ VC: Name Token ]
2600 *
2601 * returns 1 if valid or 0 otherwise
2602 */
2603
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002604static int
Owen Taylor3473f882001-02-23 17:55:21 +00002605xmlValidateNmtokenValue(const xmlChar *value) {
2606 const xmlChar *cur;
2607
2608 if (value == NULL) return(0);
2609 cur = value;
2610
2611 if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) &&
2612 (*cur != '.') && (*cur != '-') &&
2613 (*cur != '_') && (*cur != ':') &&
2614 (!IS_COMBINING(*cur)) &&
2615 (!IS_EXTENDER(*cur)))
2616 return(0);
2617
2618 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2619 (*cur == '.') || (*cur == '-') ||
2620 (*cur == '_') || (*cur == ':') ||
2621 (IS_COMBINING(*cur)) ||
2622 (IS_EXTENDER(*cur)))
2623 cur++;
2624
2625 if (*cur != 0) return(0);
2626
2627 return(1);
2628}
2629
2630/**
2631 * xmlValidateNmtokensValue:
2632 * @value: an Mntokens value
2633 *
2634 * Validate that the given value match Nmtokens production
2635 *
2636 * [ VC: Name Token ]
2637 *
2638 * returns 1 if valid or 0 otherwise
2639 */
2640
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002641static int
Owen Taylor3473f882001-02-23 17:55:21 +00002642xmlValidateNmtokensValue(const xmlChar *value) {
2643 const xmlChar *cur;
2644
2645 if (value == NULL) return(0);
2646 cur = value;
2647
2648 while (IS_BLANK(*cur)) cur++;
2649 if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) &&
2650 (*cur != '.') && (*cur != '-') &&
2651 (*cur != '_') && (*cur != ':') &&
2652 (!IS_COMBINING(*cur)) &&
2653 (!IS_EXTENDER(*cur)))
2654 return(0);
2655
2656 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2657 (*cur == '.') || (*cur == '-') ||
2658 (*cur == '_') || (*cur == ':') ||
2659 (IS_COMBINING(*cur)) ||
2660 (IS_EXTENDER(*cur)))
2661 cur++;
2662
2663 while (IS_BLANK(*cur)) {
2664 while (IS_BLANK(*cur)) cur++;
2665 if (*cur == 0) return(1);
2666
2667 if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) &&
2668 (*cur != '.') && (*cur != '-') &&
2669 (*cur != '_') && (*cur != ':') &&
2670 (!IS_COMBINING(*cur)) &&
2671 (!IS_EXTENDER(*cur)))
2672 return(0);
2673
2674 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2675 (*cur == '.') || (*cur == '-') ||
2676 (*cur == '_') || (*cur == ':') ||
2677 (IS_COMBINING(*cur)) ||
2678 (IS_EXTENDER(*cur)))
2679 cur++;
2680 }
2681
2682 if (*cur != 0) return(0);
2683
2684 return(1);
2685}
2686
2687/**
2688 * xmlValidateNotationDecl:
2689 * @ctxt: the validation context
2690 * @doc: a document instance
2691 * @nota: a notation definition
2692 *
2693 * Try to validate a single notation definition
2694 * basically it does the following checks as described by the
2695 * XML-1.0 recommendation:
2696 * - it seems that no validity constraing exist on notation declarations
2697 * But this function get called anyway ...
2698 *
2699 * returns 1 if valid or 0 otherwise
2700 */
2701
2702int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002703xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
2704 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002705 int ret = 1;
2706
2707 return(ret);
2708}
2709
2710/**
2711 * xmlValidateAttributeValue:
2712 * @type: an attribute type
2713 * @value: an attribute value
2714 *
2715 * Validate that the given attribute value match the proper production
2716 *
2717 * [ VC: ID ]
2718 * Values of type ID must match the Name production....
2719 *
2720 * [ VC: IDREF ]
2721 * Values of type IDREF must match the Name production, and values
2722 * of type IDREFS must match Names ...
2723 *
2724 * [ VC: Entity Name ]
2725 * Values of type ENTITY must match the Name production, values
2726 * of type ENTITIES must match Names ...
2727 *
2728 * [ VC: Name Token ]
2729 * Values of type NMTOKEN must match the Nmtoken production; values
2730 * of type NMTOKENS must match Nmtokens.
2731 *
2732 * returns 1 if valid or 0 otherwise
2733 */
2734
2735int
2736xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
2737 switch (type) {
2738 case XML_ATTRIBUTE_ENTITIES:
2739 case XML_ATTRIBUTE_IDREFS:
2740 return(xmlValidateNamesValue(value));
2741 case XML_ATTRIBUTE_ENTITY:
2742 case XML_ATTRIBUTE_IDREF:
2743 case XML_ATTRIBUTE_ID:
2744 case XML_ATTRIBUTE_NOTATION:
2745 return(xmlValidateNameValue(value));
2746 case XML_ATTRIBUTE_NMTOKENS:
2747 case XML_ATTRIBUTE_ENUMERATION:
2748 return(xmlValidateNmtokensValue(value));
2749 case XML_ATTRIBUTE_NMTOKEN:
2750 return(xmlValidateNmtokenValue(value));
2751 case XML_ATTRIBUTE_CDATA:
2752 break;
2753 }
2754 return(1);
2755}
2756
2757/**
2758 * xmlValidateAttributeValue2:
2759 * @ctxt: the validation context
2760 * @doc: the document
2761 * @name: the attribute name (used for error reporting only)
2762 * @type: the attribute type
2763 * @value: the attribute value
2764 *
2765 * Validate that the given attribute value match a given type.
2766 * This typically cannot be done before having finished parsing
2767 * the subsets.
2768 *
2769 * [ VC: IDREF ]
2770 * Values of type IDREF must match one of the declared IDs
2771 * Values of type IDREFS must match a sequence of the declared IDs
2772 * each Name must match the value of an ID attribute on some element
2773 * in the XML document; i.e. IDREF values must match the value of
2774 * some ID attribute
2775 *
2776 * [ VC: Entity Name ]
2777 * Values of type ENTITY must match one declared entity
2778 * Values of type ENTITIES must match a sequence of declared entities
2779 *
2780 * [ VC: Notation Attributes ]
2781 * all notation names in the declaration must be declared.
2782 *
2783 * returns 1 if valid or 0 otherwise
2784 */
2785
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002786static int
Owen Taylor3473f882001-02-23 17:55:21 +00002787xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2788 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
2789 int ret = 1;
2790 switch (type) {
2791 case XML_ATTRIBUTE_IDREFS:
2792 case XML_ATTRIBUTE_IDREF:
2793 case XML_ATTRIBUTE_ID:
2794 case XML_ATTRIBUTE_NMTOKENS:
2795 case XML_ATTRIBUTE_ENUMERATION:
2796 case XML_ATTRIBUTE_NMTOKEN:
2797 case XML_ATTRIBUTE_CDATA:
2798 break;
2799 case XML_ATTRIBUTE_ENTITY: {
2800 xmlEntityPtr ent;
2801
2802 ent = xmlGetDocEntity(doc, value);
2803 if (ent == NULL) {
2804 VERROR(ctxt->userData,
2805 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2806 name, value);
2807 ret = 0;
2808 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2809 VERROR(ctxt->userData,
2810 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2811 name, value);
2812 ret = 0;
2813 }
2814 break;
2815 }
2816 case XML_ATTRIBUTE_ENTITIES: {
2817 xmlChar *dup, *nam = NULL, *cur, save;
2818 xmlEntityPtr ent;
2819
2820 dup = xmlStrdup(value);
2821 if (dup == NULL)
2822 return(0);
2823 cur = dup;
2824 while (*cur != 0) {
2825 nam = cur;
2826 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2827 save = *cur;
2828 *cur = 0;
2829 ent = xmlGetDocEntity(doc, nam);
2830 if (ent == NULL) {
2831 VERROR(ctxt->userData,
2832 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
2833 name, nam);
2834 ret = 0;
2835 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2836 VERROR(ctxt->userData,
2837 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
2838 name, nam);
2839 ret = 0;
2840 }
2841 if (save == 0)
2842 break;
2843 *cur = save;
2844 while (IS_BLANK(*cur)) cur++;
2845 }
2846 xmlFree(dup);
2847 break;
2848 }
2849 case XML_ATTRIBUTE_NOTATION: {
2850 xmlNotationPtr nota;
2851
2852 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
2853 if ((nota == NULL) && (doc->extSubset != NULL))
2854 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
2855
2856 if (nota == NULL) {
2857 VERROR(ctxt->userData,
2858 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
2859 name, value);
2860 ret = 0;
2861 }
2862 break;
2863 }
2864 }
2865 return(ret);
2866}
2867
2868/**
2869 * xmlValidNormalizeAttributeValue:
2870 * @doc: the document
2871 * @elem: the parent
2872 * @name: the attribute name
2873 * @value: the attribute value
2874 *
2875 * Does the validation related extra step of the normalization of attribute
2876 * values:
2877 *
2878 * If the declared value is not CDATA, then the XML processor must further
2879 * process the normalized attribute value by discarding any leading and
2880 * trailing space (#x20) characters, and by replacing sequences of space
2881 * (#x20) characters by single space (#x20) character.
2882 *
2883 * returns a new normalized string if normalization is needed, NULL otherwise
2884 * the caller must free the returned value.
2885 */
2886
2887xmlChar *
2888xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
2889 const xmlChar *name, const xmlChar *value) {
2890 xmlChar *ret, *dst;
2891 const xmlChar *src;
2892 xmlAttributePtr attrDecl = NULL;
2893
2894 if (doc == NULL) return(NULL);
2895 if (elem == NULL) return(NULL);
2896 if (name == NULL) return(NULL);
2897 if (value == NULL) return(NULL);
2898
2899 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
2900 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002901 snprintf((char *) qname, sizeof(qname), "%s:%s",
2902 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002903 qname[sizeof(qname) - 1] = 0;
2904 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
2905 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2906 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
2907 }
2908 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
2909 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2910 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
2911
2912 if (attrDecl == NULL)
2913 return(NULL);
2914 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
2915 return(NULL);
2916
2917 ret = xmlStrdup(value);
2918 if (ret == NULL)
2919 return(NULL);
2920 src = value;
2921 dst = ret;
2922 while (*src == 0x20) src++;
2923 while (*src != 0) {
2924 if (*src == 0x20) {
2925 while (*src == 0x20) src++;
2926 if (*src != 0)
2927 *dst++ = 0x20;
2928 } else {
2929 *dst++ = *src++;
2930 }
2931 }
2932 *dst = 0;
2933 return(ret);
2934}
2935
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002936static void
Owen Taylor3473f882001-02-23 17:55:21 +00002937xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002938 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002939 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
2940}
2941
2942/**
2943 * xmlValidateAttributeDecl:
2944 * @ctxt: the validation context
2945 * @doc: a document instance
2946 * @attr: an attribute definition
2947 *
2948 * Try to validate a single attribute definition
2949 * basically it does the following checks as described by the
2950 * XML-1.0 recommendation:
2951 * - [ VC: Attribute Default Legal ]
2952 * - [ VC: Enumeration ]
2953 * - [ VC: ID Attribute Default ]
2954 *
2955 * The ID/IDREF uniqueness and matching are done separately
2956 *
2957 * returns 1 if valid or 0 otherwise
2958 */
2959
2960int
2961xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2962 xmlAttributePtr attr) {
2963 int ret = 1;
2964 int val;
2965 CHECK_DTD;
2966 if(attr == NULL) return(1);
2967
2968 /* Attribute Default Legal */
2969 /* Enumeration */
2970 if (attr->defaultValue != NULL) {
2971 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
2972 if (val == 0) {
2973 VERROR(ctxt->userData,
2974 "Syntax of default value for attribute %s on %s is not valid\n",
2975 attr->name, attr->elem);
2976 }
2977 ret &= val;
2978 }
2979
2980 /* ID Attribute Default */
2981 if ((attr->atype == XML_ATTRIBUTE_ID)&&
2982 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
2983 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
2984 VERROR(ctxt->userData,
2985 "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n",
2986 attr->name, attr->elem);
2987 ret = 0;
2988 }
2989
2990 /* One ID per Element Type */
2991 if (attr->atype == XML_ATTRIBUTE_ID) {
2992 int nbId;
2993
2994 /* the trick is taht we parse DtD as their own internal subset */
2995 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
2996 attr->elem);
2997 if (elem != NULL) {
2998 nbId = xmlScanIDAttributeDecl(NULL, elem);
2999 } else {
3000 xmlAttributeTablePtr table;
3001
3002 /*
3003 * The attribute may be declared in the internal subset and the
3004 * element in the external subset.
3005 */
3006 nbId = 0;
3007 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3008 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3009 xmlValidateAttributeIdCallback, &nbId);
3010 }
3011 if (nbId > 1) {
3012 VERROR(ctxt->userData,
3013 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3014 attr->elem, nbId, attr->name);
3015 } else if (doc->extSubset != NULL) {
3016 int extId = 0;
3017 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3018 if (elem != NULL) {
3019 extId = xmlScanIDAttributeDecl(NULL, elem);
3020 }
3021 if (extId > 1) {
3022 VERROR(ctxt->userData,
3023 "Element %s has %d ID attribute defined in the external subset : %s\n",
3024 attr->elem, extId, attr->name);
3025 } else if (extId + nbId > 1) {
3026 VERROR(ctxt->userData,
3027"Element %s has ID attributes defined in the internal and external subset : %s\n",
3028 attr->elem, attr->name);
3029 }
3030 }
3031 }
3032
3033 /* Validity Constraint: Enumeration */
3034 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3035 xmlEnumerationPtr tree = attr->tree;
3036 while (tree != NULL) {
3037 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3038 tree = tree->next;
3039 }
3040 if (tree == NULL) {
3041 VERROR(ctxt->userData,
3042"Default value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3043 attr->defaultValue, attr->name, attr->elem);
3044 ret = 0;
3045 }
3046 }
3047
3048 return(ret);
3049}
3050
3051/**
3052 * xmlValidateElementDecl:
3053 * @ctxt: the validation context
3054 * @doc: a document instance
3055 * @elem: an element definition
3056 *
3057 * Try to validate a single element definition
3058 * basically it does the following checks as described by the
3059 * XML-1.0 recommendation:
3060 * - [ VC: One ID per Element Type ]
3061 * - [ VC: No Duplicate Types ]
3062 * - [ VC: Unique Element Type Declaration ]
3063 *
3064 * returns 1 if valid or 0 otherwise
3065 */
3066
3067int
3068xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3069 xmlElementPtr elem) {
3070 int ret = 1;
3071 xmlElementPtr tst;
3072
3073 CHECK_DTD;
3074
3075 if (elem == NULL) return(1);
3076
3077 /* No Duplicate Types */
3078 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3079 xmlElementContentPtr cur, next;
3080 const xmlChar *name;
3081
3082 cur = elem->content;
3083 while (cur != NULL) {
3084 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3085 if (cur->c1 == NULL) break;
3086 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3087 name = cur->c1->name;
3088 next = cur->c2;
3089 while (next != NULL) {
3090 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3091 if (xmlStrEqual(next->name, name)) {
3092 VERROR(ctxt->userData,
3093 "Definition of %s has duplicate references of %s\n",
3094 elem->name, name);
3095 ret = 0;
3096 }
3097 break;
3098 }
3099 if (next->c1 == NULL) break;
3100 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3101 if (xmlStrEqual(next->c1->name, name)) {
3102 VERROR(ctxt->userData,
3103 "Definition of %s has duplicate references of %s\n",
3104 elem->name, name);
3105 ret = 0;
3106 }
3107 next = next->c2;
3108 }
3109 }
3110 cur = cur->c2;
3111 }
3112 }
3113
3114 /* VC: Unique Element Type Declaration */
3115 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003116 if ((tst != NULL ) && (tst != elem) &&
3117 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003118 VERROR(ctxt->userData, "Redefinition of element %s\n",
3119 elem->name);
3120 ret = 0;
3121 }
3122 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003123 if ((tst != NULL ) && (tst != elem) &&
3124 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003125 VERROR(ctxt->userData, "Redefinition of element %s\n",
3126 elem->name);
3127 ret = 0;
3128 }
3129
Daniel Veillarda10efa82001-04-18 13:09:01 +00003130 /* One ID per Element Type
3131 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003132 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3133 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003134 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003135 return(ret);
3136}
3137
3138/**
3139 * xmlValidateOneAttribute:
3140 * @ctxt: the validation context
3141 * @doc: a document instance
3142 * @elem: an element instance
3143 * @attr: an attribute instance
3144 * @value: the attribute value (without entities processing)
3145 *
3146 * Try to validate a single attribute for an element
3147 * basically it does the following checks as described by the
3148 * XML-1.0 recommendation:
3149 * - [ VC: Attribute Value Type ]
3150 * - [ VC: Fixed Attribute Default ]
3151 * - [ VC: Entity Name ]
3152 * - [ VC: Name Token ]
3153 * - [ VC: ID ]
3154 * - [ VC: IDREF ]
3155 * - [ VC: Entity Name ]
3156 * - [ VC: Notation Attributes ]
3157 *
3158 * The ID/IDREF uniqueness and matching are done separately
3159 *
3160 * returns 1 if valid or 0 otherwise
3161 */
3162
3163int
3164xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3165 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3166 /* xmlElementPtr elemDecl; */
3167 xmlAttributePtr attrDecl = NULL;
3168 int val;
3169 int ret = 1;
3170
3171 CHECK_DTD;
3172 if ((elem == NULL) || (elem->name == NULL)) return(0);
3173 if ((attr == NULL) || (attr->name == NULL)) return(0);
3174
3175 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3176 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003177 snprintf((char *) qname, sizeof(qname), "%s:%s",
3178 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003179 qname[sizeof(qname) - 1] = 0;
3180 if (attr->ns != NULL) {
3181 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3182 attr->name, attr->ns->prefix);
3183 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3184 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3185 attr->name, attr->ns->prefix);
3186 } else {
3187 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3188 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3189 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3190 qname, attr->name);
3191 }
3192 }
3193 if (attrDecl == NULL) {
3194 if (attr->ns != NULL) {
3195 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3196 attr->name, attr->ns->prefix);
3197 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3198 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3199 attr->name, attr->ns->prefix);
3200 } else {
3201 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3202 elem->name, attr->name);
3203 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3204 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3205 elem->name, attr->name);
3206 }
3207 }
3208
3209
3210 /* Validity Constraint: Attribute Value Type */
3211 if (attrDecl == NULL) {
3212 VERROR(ctxt->userData,
3213 "No declaration for attribute %s on element %s\n",
3214 attr->name, elem->name);
3215 return(0);
3216 }
3217 attr->atype = attrDecl->atype;
3218
3219 val = xmlValidateAttributeValue(attrDecl->atype, value);
3220 if (val == 0) {
3221 VERROR(ctxt->userData,
3222 "Syntax of value for attribute %s on %s is not valid\n",
3223 attr->name, elem->name);
3224 ret = 0;
3225 }
3226
3227 /* Validity constraint: Fixed Attribute Default */
3228 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3229 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3230 VERROR(ctxt->userData,
3231 "Value for attribute %s on %s is differnt from default \"%s\"\n",
3232 attr->name, elem->name, attrDecl->defaultValue);
3233 ret = 0;
3234 }
3235 }
3236
3237 /* Validity Constraint: ID uniqueness */
3238 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3239 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3240 ret = 0;
3241 }
3242
3243 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3244 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3245 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3246 ret = 0;
3247 }
3248
3249 /* Validity Constraint: Notation Attributes */
3250 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3251 xmlEnumerationPtr tree = attrDecl->tree;
3252 xmlNotationPtr nota;
3253
3254 /* First check that the given NOTATION was declared */
3255 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3256 if (nota == NULL)
3257 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3258
3259 if (nota == NULL) {
3260 VERROR(ctxt->userData,
3261 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3262 value, attr->name, elem->name);
3263 ret = 0;
3264 }
3265
3266 /* Second, verify that it's among the list */
3267 while (tree != NULL) {
3268 if (xmlStrEqual(tree->name, value)) break;
3269 tree = tree->next;
3270 }
3271 if (tree == NULL) {
3272 VERROR(ctxt->userData,
3273"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3274 value, attr->name, elem->name);
3275 ret = 0;
3276 }
3277 }
3278
3279 /* Validity Constraint: Enumeration */
3280 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3281 xmlEnumerationPtr tree = attrDecl->tree;
3282 while (tree != NULL) {
3283 if (xmlStrEqual(tree->name, value)) break;
3284 tree = tree->next;
3285 }
3286 if (tree == NULL) {
3287 VERROR(ctxt->userData,
3288 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3289 value, attr->name, elem->name);
3290 ret = 0;
3291 }
3292 }
3293
3294 /* Fixed Attribute Default */
3295 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3296 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3297 VERROR(ctxt->userData,
3298 "Value for attribute %s on %s must be \"%s\"\n",
3299 attr->name, elem->name, attrDecl->defaultValue);
3300 ret = 0;
3301 }
3302
3303 /* Extra check for the attribute value */
3304 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3305 attrDecl->atype, value);
3306
3307 return(ret);
3308}
3309
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003310/**
3311 * xmlValidateSkipIgnorable:
3312 * @ctxt: the validation context
3313 * @child: the child list
3314 *
3315 * Skip ignorable elements w.r.t. the validation process
3316 *
3317 * returns the first element to consider for validation of the content model
3318 */
3319
3320static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003321xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003322 while (child != NULL) {
3323 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003324 /* These things are ignored (skipped) during validation. */
3325 case XML_PI_NODE:
3326 case XML_COMMENT_NODE:
3327 case XML_XINCLUDE_START:
3328 case XML_XINCLUDE_END:
3329 child = child->next;
3330 break;
3331 case XML_TEXT_NODE:
3332 if (xmlIsBlankNode(child))
3333 child = child->next;
3334 else
3335 return(child);
3336 break;
3337 /* keep current node */
3338 default:
3339 return(child);
3340 }
3341 }
3342 return(child);
3343}
3344
3345/**
3346 * xmlValidateElementType:
3347 * @ctxt: the validation context
3348 *
3349 * Try to validate the content model of an element internal function
3350 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003351 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3352 * reference is found and -3 if the validation succeeded but
3353 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003354 */
3355
3356static int
3357xmlValidateElementType(xmlValidCtxtPtr ctxt) {
3358 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003359 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003360
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003361 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003362 if ((NODE == NULL) && (CONT == NULL))
3363 return(1);
3364 if ((NODE == NULL) &&
3365 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3366 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3367 return(1);
3368 }
3369 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003370 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003371 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003372
3373 /*
3374 * We arrive here when more states need to be examined
3375 */
3376cont:
3377
3378 /*
3379 * We just recovered from a rollback generated by a possible
3380 * epsilon transition, go directly to the analysis phase
3381 */
3382 if (STATE == ROLLBACK_PARENT) {
3383 DEBUG_VALID_MSG("restaured parent branch");
3384 DEBUG_VALID_STATE(NODE, CONT)
3385 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003386 goto analyze;
3387 }
3388
3389 DEBUG_VALID_STATE(NODE, CONT)
3390 /*
3391 * we may have to save a backup state here. This is the equivalent
3392 * of handling epsilon transition in NFAs.
3393 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003394 if ((CONT != NULL) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003395 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003396 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
3397 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003398 DEBUG_VALID_MSG("saving parent branch");
3399 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3400 }
3401
3402
3403 /*
3404 * Check first if the content matches
3405 */
3406 switch (CONT->type) {
3407 case XML_ELEMENT_CONTENT_PCDATA:
3408 if (NODE == NULL) {
3409 DEBUG_VALID_MSG("pcdata failed no node");
3410 ret = 0;
3411 break;
3412 }
3413 if (NODE->type == XML_TEXT_NODE) {
3414 DEBUG_VALID_MSG("pcdata found, skip to next");
3415 /*
3416 * go to next element in the content model
3417 * skipping ignorable elems
3418 */
3419 do {
3420 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003421 NODE = xmlValidateSkipIgnorable(NODE);
3422 if ((NODE != NULL) &&
3423 (NODE->type == XML_ENTITY_REF_NODE))
3424 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003425 } while ((NODE != NULL) &&
3426 ((NODE->type != XML_ELEMENT_NODE) &&
3427 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003428 ret = 1;
3429 break;
3430 } else {
3431 DEBUG_VALID_MSG("pcdata failed");
3432 ret = 0;
3433 break;
3434 }
3435 break;
3436 case XML_ELEMENT_CONTENT_ELEMENT:
3437 if (NODE == NULL) {
3438 DEBUG_VALID_MSG("element failed no node");
3439 ret = 0;
3440 break;
3441 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003442 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3443 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003444 if (ret == 1) {
3445 DEBUG_VALID_MSG("element found, skip to next");
3446 /*
3447 * go to next element in the content model
3448 * skipping ignorable elems
3449 */
3450 do {
3451 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003452 NODE = xmlValidateSkipIgnorable(NODE);
3453 if ((NODE != NULL) &&
3454 (NODE->type == XML_ENTITY_REF_NODE))
3455 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003456 } while ((NODE != NULL) &&
3457 ((NODE->type != XML_ELEMENT_NODE) &&
3458 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003459 } else {
3460 DEBUG_VALID_MSG("element failed");
3461 ret = 0;
3462 break;
3463 }
3464 break;
3465 case XML_ELEMENT_CONTENT_OR:
3466 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003467 * Small optimization.
3468 */
3469 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3470 if ((NODE == NULL) ||
3471 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3472 DEPTH++;
3473 CONT = CONT->c2;
3474 goto cont;
3475 }
3476 }
3477
3478 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003479 * save the second branch 'or' branch
3480 */
3481 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003482 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3483 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003484
3485 DEPTH++;
3486 CONT = CONT->c1;
3487 goto cont;
3488 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003489 /*
3490 * Small optimization.
3491 */
3492 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3493 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3494 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3495 if ((NODE == NULL) ||
3496 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3497 DEPTH++;
3498 CONT = CONT->c2;
3499 goto cont;
3500 }
3501 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003502 DEPTH++;
3503 CONT = CONT->c1;
3504 goto cont;
3505 }
3506
3507 /*
3508 * At this point handle going up in the tree
3509 */
3510 if (ret == -1) {
3511 DEBUG_VALID_MSG("error found returning");
3512 return(ret);
3513 }
3514analyze:
3515 while (CONT != NULL) {
3516 /*
3517 * First do the analysis depending on the occurence model at
3518 * this level.
3519 */
3520 if (ret == 0) {
3521 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003522 xmlNodePtr cur;
3523
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003524 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003525 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003526 DEBUG_VALID_MSG("Once branch failed, rollback");
3527 if (vstateVPop(ctxt) < 0 ) {
3528 DEBUG_VALID_MSG("exhaustion, failed");
3529 return(0);
3530 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003531 if (cur != ctxt->vstate->node)
3532 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003533 goto cont;
3534 case XML_ELEMENT_CONTENT_PLUS:
3535 if (OCCURENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003536 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003537 DEBUG_VALID_MSG("Plus branch failed, rollback");
3538 if (vstateVPop(ctxt) < 0 ) {
3539 DEBUG_VALID_MSG("exhaustion, failed");
3540 return(0);
3541 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003542 if (cur != ctxt->vstate->node)
3543 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003544 goto cont;
3545 }
3546 DEBUG_VALID_MSG("Plus branch found");
3547 ret = 1;
3548 break;
3549 case XML_ELEMENT_CONTENT_MULT:
3550#ifdef DEBUG_VALID_ALGO
3551 if (OCCURENCE == 0) {
3552 DEBUG_VALID_MSG("Mult branch failed");
3553 } else {
3554 DEBUG_VALID_MSG("Mult branch found");
3555 }
3556#endif
3557 ret = 1;
3558 break;
3559 case XML_ELEMENT_CONTENT_OPT:
3560 DEBUG_VALID_MSG("Option branch failed");
3561 ret = 1;
3562 break;
3563 }
3564 } else {
3565 switch (CONT->ocur) {
3566 case XML_ELEMENT_CONTENT_OPT:
3567 DEBUG_VALID_MSG("Option branch succeeded");
3568 ret = 1;
3569 break;
3570 case XML_ELEMENT_CONTENT_ONCE:
3571 DEBUG_VALID_MSG("Once branch succeeded");
3572 ret = 1;
3573 break;
3574 case XML_ELEMENT_CONTENT_PLUS:
3575 if (STATE == ROLLBACK_PARENT) {
3576 DEBUG_VALID_MSG("Plus branch rollback");
3577 ret = 1;
3578 break;
3579 }
3580 if (NODE == NULL) {
3581 DEBUG_VALID_MSG("Plus branch exhausted");
3582 ret = 1;
3583 break;
3584 }
3585 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
3586 SET_OCCURENCE;
3587 goto cont;
3588 case XML_ELEMENT_CONTENT_MULT:
3589 if (STATE == ROLLBACK_PARENT) {
3590 DEBUG_VALID_MSG("Mult branch rollback");
3591 ret = 1;
3592 break;
3593 }
3594 if (NODE == NULL) {
3595 DEBUG_VALID_MSG("Mult branch exhausted");
3596 ret = 1;
3597 break;
3598 }
3599 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
3600 SET_OCCURENCE;
3601 goto cont;
3602 }
3603 }
3604 STATE = 0;
3605
3606 /*
3607 * Then act accordingly at the parent level
3608 */
3609 RESET_OCCURENCE;
3610 if (CONT->parent == NULL)
3611 break;
3612
3613 switch (CONT->parent->type) {
3614 case XML_ELEMENT_CONTENT_PCDATA:
3615 DEBUG_VALID_MSG("Error: parent pcdata");
3616 return(-1);
3617 case XML_ELEMENT_CONTENT_ELEMENT:
3618 DEBUG_VALID_MSG("Error: parent element");
3619 return(-1);
3620 case XML_ELEMENT_CONTENT_OR:
3621 if (ret == 1) {
3622 DEBUG_VALID_MSG("Or succeeded");
3623 CONT = CONT->parent;
3624 DEPTH--;
3625 } else {
3626 DEBUG_VALID_MSG("Or failed");
3627 CONT = CONT->parent;
3628 DEPTH--;
3629 }
3630 break;
3631 case XML_ELEMENT_CONTENT_SEQ:
3632 if (ret == 0) {
3633 DEBUG_VALID_MSG("Sequence failed");
3634 CONT = CONT->parent;
3635 DEPTH--;
3636 } else if (CONT == CONT->parent->c1) {
3637 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3638 CONT = CONT->parent->c2;
3639 goto cont;
3640 } else {
3641 DEBUG_VALID_MSG("Sequence succeeded");
3642 CONT = CONT->parent;
3643 DEPTH--;
3644 }
3645 }
3646 }
3647 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003648 xmlNodePtr cur;
3649
3650 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003651 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3652 if (vstateVPop(ctxt) < 0 ) {
3653 DEBUG_VALID_MSG("exhaustion, failed");
3654 return(0);
3655 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003656 if (cur != ctxt->vstate->node)
3657 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003658 goto cont;
3659 }
3660 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003661 xmlNodePtr cur;
3662
3663 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003664 DEBUG_VALID_MSG("Failure, rollback");
3665 if (vstateVPop(ctxt) < 0 ) {
3666 DEBUG_VALID_MSG("exhaustion, failed");
3667 return(0);
3668 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003669 if (cur != ctxt->vstate->node)
3670 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003671 goto cont;
3672 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003673 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003674}
3675
3676/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003677 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003678 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003679 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003680 * @content: An element
3681 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3682 *
3683 * This will dump the list of elements to the buffer
3684 * Intended just for the debug routine
3685 */
3686static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003687xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003688 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003689 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003690
3691 if (node == NULL) return;
3692 if (glob) strcat(buf, "(");
3693 cur = node;
3694 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003695 len = strlen(buf);
3696 if (size - len < 50) {
3697 if ((size - len > 4) && (buf[len - 1] != '.'))
3698 strcat(buf, " ...");
3699 return;
3700 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003701 switch (cur->type) {
3702 case XML_ELEMENT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003703 if (size - len < xmlStrlen(cur->name + 10)) {
3704 if ((size - len > 4) && (buf[len - 1] != '.'))
3705 strcat(buf, " ...");
3706 return;
3707 }
3708 strcat(buf, (char *) cur->name);
3709 if (cur->next != NULL)
3710 strcat(buf, " ");
3711 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003712 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003713 if (xmlIsBlankNode(cur))
3714 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003715 case XML_CDATA_SECTION_NODE:
3716 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003717 strcat(buf, "CDATA");
3718 if (cur->next != NULL)
3719 strcat(buf, " ");
3720 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003721 case XML_ATTRIBUTE_NODE:
3722 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003723#ifdef LIBXML_DOCB_ENABLED
3724 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003725#endif
3726 case XML_HTML_DOCUMENT_NODE:
3727 case XML_DOCUMENT_TYPE_NODE:
3728 case XML_DOCUMENT_FRAG_NODE:
3729 case XML_NOTATION_NODE:
3730 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003731 strcat(buf, "???");
3732 if (cur->next != NULL)
3733 strcat(buf, " ");
3734 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003735 case XML_ENTITY_NODE:
3736 case XML_PI_NODE:
3737 case XML_DTD_NODE:
3738 case XML_COMMENT_NODE:
3739 case XML_ELEMENT_DECL:
3740 case XML_ATTRIBUTE_DECL:
3741 case XML_ENTITY_DECL:
3742 case XML_XINCLUDE_START:
3743 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003744 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003745 }
3746 cur = cur->next;
3747 }
3748 if (glob) strcat(buf, ")");
3749}
3750
3751/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003752 * xmlValidateElementContent:
3753 * @ctxt: the validation context
3754 * @child: the child list
3755 * @cont: pointer to the content declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003756 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003757 *
3758 * Try to validate the content model of an element
3759 *
3760 * returns 1 if valid or 0 if not and -1 in case of error
3761 */
3762
3763static int
3764xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillard7533cc82001-04-24 15:52:00 +00003765 xmlElementContentPtr cont, int warn, const xmlChar *name) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003766 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003767 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003768
3769 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003770 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003771 */
3772 ctxt->vstateMax = 8;
3773 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
3774 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
3775 if (ctxt->vstateTab == NULL) {
3776 xmlGenericError(xmlGenericErrorContext,
3777 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003778 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003779 }
3780 /*
3781 * The first entry in the stack is reserved to the current state
3782 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00003783 ctxt->nodeMax = 0;
3784 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00003785 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003786 ctxt->vstate = &ctxt->vstateTab[0];
3787 ctxt->vstateNr = 1;
3788 CONT = cont;
3789 NODE = child;
3790 DEPTH = 0;
3791 OCCURS = 0;
3792 STATE = 0;
3793 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003794 if ((ret == -3) && (warn)) {
3795 VWARNING(ctxt->userData,
3796 "Element %s content model is ambiguous\n", name);
3797 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003798 /*
3799 * An entities reference appeared at this level.
3800 * Buid a minimal representation of this node content
3801 * sufficient to run the validation process on it
3802 */
3803 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003804 cur = child;
3805 while (cur != NULL) {
3806 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003807 case XML_ENTITY_REF_NODE:
3808 /*
3809 * Push the current node to be able to roll back
3810 * and process within the entity
3811 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003812 if ((cur->children != NULL) &&
3813 (cur->children->children != NULL)) {
3814 nodeVPush(ctxt, cur);
3815 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003816 continue;
3817 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00003818 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003819 case XML_TEXT_NODE:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003820 case XML_CDATA_SECTION_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003821 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003822 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003823 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003824 case XML_ELEMENT_NODE:
3825 /*
3826 * Allocate a new node and minimally fills in
3827 * what's required
3828 */
3829 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3830 if (tmp == NULL) {
3831 xmlGenericError(xmlGenericErrorContext,
3832 "xmlValidateElementContent : malloc failed\n");
3833 xmlFreeNodeList(repl);
3834 ret = -1;
3835 goto done;
3836 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003837 tmp->type = cur->type;
3838 tmp->name = cur->name;
3839 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003840 tmp->next = NULL;
3841 if (repl == NULL)
3842 repl = last = tmp;
3843 else {
3844 last->next = tmp;
3845 last = tmp;
3846 }
3847 break;
3848 default:
3849 break;
3850 }
3851 /*
3852 * Switch to next element
3853 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003854 cur = cur->next;
3855 while (cur == NULL) {
3856 cur = nodeVPop(ctxt);
3857 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003858 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003859 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003860 }
3861 }
3862
3863 /*
3864 * Relaunch the validation
3865 */
3866 ctxt->vstate = &ctxt->vstateTab[0];
3867 ctxt->vstateNr = 1;
3868 CONT = cont;
3869 NODE = repl;
3870 DEPTH = 0;
3871 OCCURS = 0;
3872 STATE = 0;
3873 ret = xmlValidateElementType(ctxt);
3874 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003875 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003876 char expr[5000];
3877 char list[5000];
3878
3879 expr[0] = 0;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003880 xmlSnprintfElementContent(expr, 5000, cont, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003881 list[0] = 0;
3882 if (repl != NULL)
Daniel Veillardd3d06722001-08-15 12:06:36 +00003883 xmlSnprintfElements(list, 5000, repl, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003884 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00003885 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003886
Daniel Veillard7533cc82001-04-24 15:52:00 +00003887 if (name != NULL) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003888 VERROR(ctxt->userData,
3889 "Element %s content doesn't follow the Dtd\nExpecting %s, got %s\n",
Daniel Veillard7533cc82001-04-24 15:52:00 +00003890 name, expr, list);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003891 } else {
3892 VERROR(ctxt->userData,
3893 "Element content doesn't follow the Dtd\nExpecting %s, got %s\n",
3894 expr, list);
3895 }
3896 ret = 0;
3897 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003898 if (ret == -3)
3899 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003900
3901
3902done:
3903 /*
3904 * Deallocate the copy if done, and free up the validation stack
3905 */
3906 while (repl != NULL) {
3907 tmp = repl->next;
3908 xmlFree(repl);
3909 repl = tmp;
3910 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003911 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003912 if (ctxt->vstateTab != NULL) {
3913 xmlFree(ctxt->vstateTab);
3914 ctxt->vstateTab = NULL;
3915 }
3916 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00003917 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003918 if (ctxt->nodeTab != NULL) {
3919 xmlFree(ctxt->nodeTab);
3920 ctxt->nodeTab = NULL;
3921 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003922 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003923
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003924}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003925
Owen Taylor3473f882001-02-23 17:55:21 +00003926/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003927 * xmlValidateCdataElement:
3928 * @ctxt: the validation context
3929 * @doc: a document instance
3930 * @elem: an element instance
3931 *
3932 * Check that an element follows #CDATA
3933 *
3934 * returns 1 if valid or 0 otherwise
3935 */
3936static int
3937xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3938 xmlNodePtr elem) {
3939 int ret = 1;
3940 xmlNodePtr cur, child;
3941
3942 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
3943 return(0);
3944
3945 child = elem->children;
3946
3947 cur = child;
3948 while (cur != NULL) {
3949 switch (cur->type) {
3950 case XML_ENTITY_REF_NODE:
3951 /*
3952 * Push the current node to be able to roll back
3953 * and process within the entity
3954 */
3955 if ((cur->children != NULL) &&
3956 (cur->children->children != NULL)) {
3957 nodeVPush(ctxt, cur);
3958 cur = cur->children->children;
3959 continue;
3960 }
3961 break;
3962 case XML_COMMENT_NODE:
3963 case XML_PI_NODE:
3964 case XML_TEXT_NODE:
3965 case XML_CDATA_SECTION_NODE:
3966 break;
3967 default:
3968 ret = 0;
3969 goto done;
3970 }
3971 /*
3972 * Switch to next element
3973 */
3974 cur = cur->next;
3975 while (cur == NULL) {
3976 cur = nodeVPop(ctxt);
3977 if (cur == NULL)
3978 break;
3979 cur = cur->next;
3980 }
3981 }
3982done:
3983 ctxt->nodeMax = 0;
3984 ctxt->nodeNr = 0;
3985 if (ctxt->nodeTab != NULL) {
3986 xmlFree(ctxt->nodeTab);
3987 ctxt->nodeTab = NULL;
3988 }
3989 return(ret);
3990}
3991
3992/**
Owen Taylor3473f882001-02-23 17:55:21 +00003993 * xmlValidateOneElement:
3994 * @ctxt: the validation context
3995 * @doc: a document instance
3996 * @elem: an element instance
3997 *
3998 * Try to validate a single element and it's attributes,
3999 * basically it does the following checks as described by the
4000 * XML-1.0 recommendation:
4001 * - [ VC: Element Valid ]
4002 * - [ VC: Required Attribute ]
4003 * Then call xmlValidateOneAttribute() for each attribute present.
4004 *
4005 * The ID/IDREF checkings are done separately
4006 *
4007 * returns 1 if valid or 0 otherwise
4008 */
4009
4010int
4011xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4012 xmlNodePtr elem) {
4013 xmlElementPtr elemDecl = NULL;
4014 xmlElementContentPtr cont;
4015 xmlAttributePtr attr;
4016 xmlNodePtr child;
4017 int ret = 1;
4018 const xmlChar *name;
4019
4020 CHECK_DTD;
4021
4022 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004023 switch (elem->type) {
4024 case XML_ATTRIBUTE_NODE:
4025 VERROR(ctxt->userData,
4026 "Attribute element not expected here\n");
4027 return(0);
4028 case XML_TEXT_NODE:
4029 if (elem->children != NULL) {
4030 VERROR(ctxt->userData, "Text element has childs !\n");
4031 return(0);
4032 }
4033 if (elem->properties != NULL) {
4034 VERROR(ctxt->userData, "Text element has attributes !\n");
4035 return(0);
4036 }
4037 if (elem->ns != NULL) {
4038 VERROR(ctxt->userData, "Text element has namespace !\n");
4039 return(0);
4040 }
4041 if (elem->nsDef != NULL) {
4042 VERROR(ctxt->userData,
4043 "Text element carries namespace definitions !\n");
4044 return(0);
4045 }
4046 if (elem->content == NULL) {
4047 VERROR(ctxt->userData,
4048 "Text element has no content !\n");
4049 return(0);
4050 }
4051 return(1);
4052 case XML_XINCLUDE_START:
4053 case XML_XINCLUDE_END:
4054 return(1);
4055 case XML_CDATA_SECTION_NODE:
4056 case XML_ENTITY_REF_NODE:
4057 case XML_PI_NODE:
4058 case XML_COMMENT_NODE:
4059 return(1);
4060 case XML_ENTITY_NODE:
4061 VERROR(ctxt->userData,
4062 "Entity element not expected here\n");
4063 return(0);
4064 case XML_NOTATION_NODE:
4065 VERROR(ctxt->userData,
4066 "Notation element not expected here\n");
4067 return(0);
4068 case XML_DOCUMENT_NODE:
4069 case XML_DOCUMENT_TYPE_NODE:
4070 case XML_DOCUMENT_FRAG_NODE:
4071 VERROR(ctxt->userData,
4072 "Document element not expected here\n");
4073 return(0);
4074 case XML_HTML_DOCUMENT_NODE:
4075 VERROR(ctxt->userData,
4076 "\n");
4077 return(0);
4078 case XML_ELEMENT_NODE:
4079 break;
4080 default:
4081 VERROR(ctxt->userData,
4082 "unknown element type %d\n", elem->type);
4083 return(0);
4084 }
4085 if (elem->name == NULL) return(0);
4086
4087 /*
4088 * Fetch the declaration for the qualified name
4089 */
4090 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
4091 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
4092 elem->name, elem->ns->prefix);
4093 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4094 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
4095 elem->name, elem->ns->prefix);
4096 }
4097
4098 /*
4099 * Fetch the declaration for the non qualified name
4100 */
4101 if (elemDecl == NULL) {
4102 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4103 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4104 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4105 }
4106 if (elemDecl == NULL) {
4107 VERROR(ctxt->userData, "No declaration for element %s\n",
4108 elem->name);
4109 return(0);
4110 }
4111
4112 /* Check taht the element content matches the definition */
4113 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004114 case XML_ELEMENT_TYPE_UNDEFINED:
4115 VERROR(ctxt->userData, "No declaration for element %s\n",
4116 elem->name);
4117 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004118 case XML_ELEMENT_TYPE_EMPTY:
4119 if (elem->children != NULL) {
4120 VERROR(ctxt->userData,
4121 "Element %s was declared EMPTY this one has content\n",
4122 elem->name);
4123 ret = 0;
4124 }
4125 break;
4126 case XML_ELEMENT_TYPE_ANY:
4127 /* I don't think anything is required then */
4128 break;
4129 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004130 /* simple case of declared as #PCDATA */
4131 if ((elemDecl->content != NULL) &&
4132 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4133 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4134 if (!ret) {
4135 VERROR(ctxt->userData,
4136 "Element %s was declared #PCDATA but contains non text nodes\n",
4137 elem->name);
4138 }
4139 break;
4140 }
Owen Taylor3473f882001-02-23 17:55:21 +00004141 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004142 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004143 while (child != NULL) {
4144 if (child->type == XML_ELEMENT_NODE) {
4145 name = child->name;
4146 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4147 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004148 snprintf((char *) qname, sizeof(qname), "%s:%s",
4149 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004150 qname[sizeof(qname) - 1] = 0;
4151 cont = elemDecl->content;
4152 while (cont != NULL) {
4153 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4154 if (xmlStrEqual(cont->name, qname)) break;
4155 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4156 (cont->c1 != NULL) &&
4157 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4158 if (xmlStrEqual(cont->c1->name, qname)) break;
4159 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4160 (cont->c1 == NULL) ||
4161 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4162 /* Internal error !!! */
4163 xmlGenericError(xmlGenericErrorContext,
4164 "Internal: MIXED struct bad\n");
4165 break;
4166 }
4167 cont = cont->c2;
4168 }
4169 if (cont != NULL)
4170 goto child_ok;
4171 }
4172 cont = elemDecl->content;
4173 while (cont != NULL) {
4174 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4175 if (xmlStrEqual(cont->name, name)) break;
4176 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4177 (cont->c1 != NULL) &&
4178 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4179 if (xmlStrEqual(cont->c1->name, name)) break;
4180 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4181 (cont->c1 == NULL) ||
4182 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4183 /* Internal error !!! */
4184 xmlGenericError(xmlGenericErrorContext,
4185 "Internal: MIXED struct bad\n");
4186 break;
4187 }
4188 cont = cont->c2;
4189 }
4190 if (cont == NULL) {
4191 VERROR(ctxt->userData,
4192 "Element %s is not declared in %s list of possible childs\n",
4193 name, elem->name);
4194 ret = 0;
4195 }
4196 }
4197child_ok:
4198 child = child->next;
4199 }
4200 break;
4201 case XML_ELEMENT_TYPE_ELEMENT:
4202 child = elem->children;
4203 cont = elemDecl->content;
Daniel Veillard7533cc82001-04-24 15:52:00 +00004204 ret = xmlValidateElementContent(ctxt, child, cont, 1, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004205 break;
4206 }
4207
4208 /* [ VC: Required Attribute ] */
4209 attr = elemDecl->attributes;
4210 while (attr != NULL) {
4211 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
4212 xmlAttrPtr attrib;
4213 int qualified = -1;
4214
4215 attrib = elem->properties;
4216 while (attrib != NULL) {
4217 if (xmlStrEqual(attrib->name, attr->name)) {
4218 if (attr->prefix != NULL) {
4219 xmlNsPtr nameSpace = attrib->ns;
4220
4221 if (nameSpace == NULL)
4222 nameSpace = elem->ns;
4223 /*
4224 * qualified names handling is problematic, having a
4225 * different prefix should be possible but DTDs don't
4226 * allow to define the URI instead of the prefix :-(
4227 */
4228 if (nameSpace == NULL) {
4229 if (qualified < 0)
4230 qualified = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004231 } else if (!xmlStrEqual(nameSpace->prefix,
4232 attr->prefix)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004233 if (qualified < 1)
4234 qualified = 1;
4235 } else
4236 goto found;
4237 } else {
4238 /*
4239 * We should allow applications to define namespaces
4240 * for their application even if the DTD doesn't
4241 * carry one, otherwise, basically we would always
4242 * break.
4243 */
4244 goto found;
4245 }
4246 }
4247 attrib = attrib->next;
4248 }
4249 if (qualified == -1) {
4250 if (attr->prefix == NULL) {
4251 VERROR(ctxt->userData,
4252 "Element %s doesn't carry attribute %s\n",
4253 elem->name, attr->name);
4254 ret = 0;
4255 } else {
4256 VERROR(ctxt->userData,
4257 "Element %s doesn't carry attribute %s:%s\n",
4258 elem->name, attr->prefix,attr->name);
4259 ret = 0;
4260 }
4261 } else if (qualified == 0) {
4262 VWARNING(ctxt->userData,
4263 "Element %s required attribute %s:%s has no prefix\n",
4264 elem->name, attr->prefix,attr->name);
4265 } else if (qualified == 1) {
4266 VWARNING(ctxt->userData,
4267 "Element %s required attribute %s:%s has different prefix\n",
4268 elem->name, attr->prefix,attr->name);
4269 }
4270 }
4271found:
4272 attr = attr->nexth;
4273 }
4274 return(ret);
4275}
4276
4277/**
4278 * xmlValidateRoot:
4279 * @ctxt: the validation context
4280 * @doc: a document instance
4281 *
4282 * Try to validate a the root element
4283 * basically it does the following check as described by the
4284 * XML-1.0 recommendation:
4285 * - [ VC: Root Element Type ]
4286 * it doesn't try to recurse or apply other check to the element
4287 *
4288 * returns 1 if valid or 0 otherwise
4289 */
4290
4291int
4292xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4293 xmlNodePtr root;
4294 if (doc == NULL) return(0);
4295
4296 root = xmlDocGetRootElement(doc);
4297 if ((root == NULL) || (root->name == NULL)) {
4298 VERROR(ctxt->userData, "Not valid: no root element\n");
4299 return(0);
4300 }
4301
4302 /*
4303 * When doing post validation against a separate DTD, those may
4304 * no internal subset has been generated
4305 */
4306 if ((doc->intSubset != NULL) &&
4307 (doc->intSubset->name != NULL)) {
4308 /*
4309 * Check first the document root against the NQName
4310 */
4311 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4312 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4313 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004314 snprintf((char *) qname, sizeof(qname), "%s:%s",
4315 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004316 qname[sizeof(qname) - 1] = 0;
4317 if (xmlStrEqual(doc->intSubset->name, qname))
4318 goto name_ok;
4319 }
4320 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4321 (xmlStrEqual(root->name, BAD_CAST "html")))
4322 goto name_ok;
4323 VERROR(ctxt->userData,
4324 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4325 root->name, doc->intSubset->name);
4326 return(0);
4327
4328 }
4329 }
4330name_ok:
4331 return(1);
4332}
4333
4334
4335/**
4336 * xmlValidateElement:
4337 * @ctxt: the validation context
4338 * @doc: a document instance
4339 * @elem: an element instance
4340 *
4341 * Try to validate the subtree under an element
4342 *
4343 * returns 1 if valid or 0 otherwise
4344 */
4345
4346int
4347xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4348 xmlNodePtr child;
4349 xmlAttrPtr attr;
4350 xmlChar *value;
4351 int ret = 1;
4352
4353 if (elem == NULL) return(0);
4354
4355 /*
4356 * XInclude elements were added after parsing in the infoset,
4357 * they don't really mean anything validation wise.
4358 */
4359 if ((elem->type == XML_XINCLUDE_START) ||
4360 (elem->type == XML_XINCLUDE_END))
4361 return(1);
4362
4363 CHECK_DTD;
4364
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004365 /*
4366 * Entities references have to be handled separately
4367 */
4368 if (elem->type == XML_ENTITY_REF_NODE) {
4369 return(1);
4370 }
4371
Owen Taylor3473f882001-02-23 17:55:21 +00004372 ret &= xmlValidateOneElement(ctxt, doc, elem);
4373 attr = elem->properties;
4374 while(attr != NULL) {
4375 value = xmlNodeListGetString(doc, attr->children, 0);
4376 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4377 if (value != NULL)
4378 xmlFree(value);
4379 attr= attr->next;
4380 }
4381 child = elem->children;
4382 while (child != NULL) {
4383 ret &= xmlValidateElement(ctxt, doc, child);
4384 child = child->next;
4385 }
4386
4387 return(ret);
4388}
4389
Daniel Veillard8730c562001-02-26 10:49:57 +00004390/**
4391 * xmlValidateRef:
4392 * @ref: A reference to be validated
4393 * @ctxt: Validation context
4394 * @name: Name of ID we are searching for
4395 *
4396 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004397static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004398xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004399 const xmlChar *name) {
4400 xmlAttrPtr id;
4401 xmlAttrPtr attr;
4402
4403 if (ref == NULL)
4404 return;
4405 attr = ref->attr;
4406 if (attr == NULL)
4407 return;
4408 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4409 id = xmlGetID(ctxt->doc, name);
4410 if (id == NULL) {
4411 VERROR(ctxt->userData,
4412 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4413 attr->name, name);
4414 ctxt->valid = 0;
4415 }
4416 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4417 xmlChar *dup, *str = NULL, *cur, save;
4418
4419 dup = xmlStrdup(name);
4420 if (dup == NULL) {
4421 ctxt->valid = 0;
4422 return;
4423 }
4424 cur = dup;
4425 while (*cur != 0) {
4426 str = cur;
4427 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4428 save = *cur;
4429 *cur = 0;
4430 id = xmlGetID(ctxt->doc, str);
4431 if (id == NULL) {
4432 VERROR(ctxt->userData,
4433 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4434 attr->name, str);
4435 ctxt->valid = 0;
4436 }
4437 if (save == 0)
4438 break;
4439 *cur = save;
4440 while (IS_BLANK(*cur)) cur++;
4441 }
4442 xmlFree(dup);
4443 }
4444}
4445
4446/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004447 * xmlWalkValidateList:
4448 * @data: Contents of current link
4449 * @user: Value supplied by the user
4450 *
4451 * Return 0 to abort the walk or 1 to continue
4452 */
4453static int
4454xmlWalkValidateList(const void *data, const void *user)
4455{
4456 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4457 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4458 return 1;
4459}
4460
4461/**
4462 * xmlValidateCheckRefCallback:
4463 * @ref_list: List of references
4464 * @ctxt: Validation context
4465 * @name: Name of ID we are searching for
4466 *
4467 */
4468static void
4469xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4470 const xmlChar *name) {
4471 xmlValidateMemo memo;
4472
4473 if (ref_list == NULL)
4474 return;
4475 memo.ctxt = ctxt;
4476 memo.name = name;
4477
4478 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4479
4480}
4481
4482/**
Owen Taylor3473f882001-02-23 17:55:21 +00004483 * xmlValidateDocumentFinal:
4484 * @ctxt: the validation context
4485 * @doc: a document instance
4486 *
4487 * Does the final step for the document validation once all the
4488 * incremental validation steps have been completed
4489 *
4490 * basically it does the following checks described by the XML Rec
4491 *
4492 *
4493 * returns 1 if valid or 0 otherwise
4494 */
4495
4496int
4497xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4498 xmlRefTablePtr table;
4499
4500 if (doc == NULL) {
4501 xmlGenericError(xmlGenericErrorContext,
4502 "xmlValidateDocumentFinal: doc == NULL\n");
4503 return(0);
4504 }
4505
4506 /*
4507 * Check all the NOTATION/NOTATIONS attributes
4508 */
4509 /*
4510 * Check all the ENTITY/ENTITIES attributes definition for validity
4511 */
4512 /*
4513 * Check all the IDREF/IDREFS attributes definition for validity
4514 */
4515 table = (xmlRefTablePtr) doc->refs;
4516 ctxt->doc = doc;
4517 ctxt->valid = 1;
4518 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4519 return(ctxt->valid);
4520}
4521
4522/**
4523 * xmlValidateDtd:
4524 * @ctxt: the validation context
4525 * @doc: a document instance
4526 * @dtd: a dtd instance
4527 *
4528 * Try to validate the document against the dtd instance
4529 *
4530 * basically it does check all the definitions in the DtD.
4531 *
4532 * returns 1 if valid or 0 otherwise
4533 */
4534
4535int
4536xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4537 int ret;
4538 xmlDtdPtr oldExt;
4539 xmlNodePtr root;
4540
4541 if (dtd == NULL) return(0);
4542 if (doc == NULL) return(0);
4543 oldExt = doc->extSubset;
4544 doc->extSubset = dtd;
4545 ret = xmlValidateRoot(ctxt, doc);
4546 if (ret == 0) {
4547 doc->extSubset = oldExt;
4548 return(ret);
4549 }
4550 if (doc->ids != NULL) {
4551 xmlFreeIDTable(doc->ids);
4552 doc->ids = NULL;
4553 }
4554 if (doc->refs != NULL) {
4555 xmlFreeRefTable(doc->refs);
4556 doc->refs = NULL;
4557 }
4558 root = xmlDocGetRootElement(doc);
4559 ret = xmlValidateElement(ctxt, doc, root);
4560 ret &= xmlValidateDocumentFinal(ctxt, doc);
4561 doc->extSubset = oldExt;
4562 return(ret);
4563}
4564
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004565static void
Owen Taylor3473f882001-02-23 17:55:21 +00004566xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004567 const xmlChar *name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004568 if (cur == NULL)
4569 return;
4570 switch (cur->atype) {
4571 case XML_ATTRIBUTE_CDATA:
4572 case XML_ATTRIBUTE_ID:
4573 case XML_ATTRIBUTE_IDREF :
4574 case XML_ATTRIBUTE_IDREFS:
4575 case XML_ATTRIBUTE_NMTOKEN:
4576 case XML_ATTRIBUTE_NMTOKENS:
4577 case XML_ATTRIBUTE_ENUMERATION:
4578 break;
4579 case XML_ATTRIBUTE_ENTITY:
4580 case XML_ATTRIBUTE_ENTITIES:
4581 case XML_ATTRIBUTE_NOTATION:
4582 if (cur->defaultValue != NULL) {
4583 ctxt->valid &= xmlValidateAttributeValue2(ctxt, ctxt->doc,
4584 cur->name, cur->atype, cur->defaultValue);
4585 }
4586 if (cur->tree != NULL) {
4587 xmlEnumerationPtr tree = cur->tree;
4588 while (tree != NULL) {
4589 ctxt->valid &= xmlValidateAttributeValue2(ctxt, ctxt->doc,
4590 cur->name, cur->atype, tree->name);
4591 tree = tree->next;
4592 }
4593 }
4594 }
4595}
4596
4597/**
4598 * xmlValidateDtdFinal:
4599 * @ctxt: the validation context
4600 * @doc: a document instance
4601 *
4602 * Does the final step for the dtds validation once all the
4603 * subsets have been parsed
4604 *
4605 * basically it does the following checks described by the XML Rec
4606 * - check that ENTITY and ENTITIES type attributes default or
4607 * possible values matches one of the defined entities.
4608 * - check that NOTATION type attributes default or
4609 * possible values matches one of the defined notations.
4610 *
4611 * returns 1 if valid or 0 otherwise
4612 */
4613
4614int
4615xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4616 int ret = 1;
4617 xmlDtdPtr dtd;
4618 xmlAttributeTablePtr table;
4619
4620 if (doc == NULL) return(0);
4621 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4622 return(0);
4623 ctxt->doc = doc;
4624 ctxt->valid = ret;
4625 dtd = doc->intSubset;
4626 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4627 table = (xmlAttributeTablePtr) dtd->attributes;
4628 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
4629 }
4630 dtd = doc->extSubset;
4631 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4632 table = (xmlAttributeTablePtr) dtd->attributes;
4633 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
4634 }
4635 return(ctxt->valid);
4636}
4637
4638/**
4639 * xmlValidateDocument:
4640 * @ctxt: the validation context
4641 * @doc: a document instance
4642 *
4643 * Try to validate the document instance
4644 *
4645 * basically it does the all the checks described by the XML Rec
4646 * i.e. validates the internal and external subset (if present)
4647 * and validate the document tree.
4648 *
4649 * returns 1 if valid or 0 otherwise
4650 */
4651
4652int
4653xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4654 int ret;
4655 xmlNodePtr root;
4656
4657 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4658 return(0);
4659 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
4660 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
4661 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
4662 doc->intSubset->SystemID);
4663 if (doc->extSubset == NULL) {
4664 if (doc->intSubset->SystemID != NULL) {
4665 VERROR(ctxt->userData,
4666 "Could not load the external subset \"%s\"\n",
4667 doc->intSubset->SystemID);
4668 } else {
4669 VERROR(ctxt->userData,
4670 "Could not load the external subset \"%s\"\n",
4671 doc->intSubset->ExternalID);
4672 }
4673 return(0);
4674 }
4675 }
4676
4677 if (doc->ids != NULL) {
4678 xmlFreeIDTable(doc->ids);
4679 doc->ids = NULL;
4680 }
4681 if (doc->refs != NULL) {
4682 xmlFreeRefTable(doc->refs);
4683 doc->refs = NULL;
4684 }
4685 ret = xmlValidateDtdFinal(ctxt, doc);
4686 if (!xmlValidateRoot(ctxt, doc)) return(0);
4687
4688 root = xmlDocGetRootElement(doc);
4689 ret &= xmlValidateElement(ctxt, doc, root);
4690 ret &= xmlValidateDocumentFinal(ctxt, doc);
4691 return(ret);
4692}
4693
4694
4695/************************************************************************
4696 * *
4697 * Routines for dynamic validation editing *
4698 * *
4699 ************************************************************************/
4700
4701/**
4702 * xmlValidGetPotentialChildren:
4703 * @ctree: an element content tree
4704 * @list: an array to store the list of child names
4705 * @len: a pointer to the number of element in the list
4706 * @max: the size of the array
4707 *
4708 * Build/extend a list of potential children allowed by the content tree
4709 *
4710 * returns the number of element in the list, or -1 in case of error.
4711 */
4712
4713int
4714xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
4715 int *len, int max) {
4716 int i;
4717
4718 if ((ctree == NULL) || (list == NULL) || (len == NULL))
4719 return(-1);
4720 if (*len >= max) return(*len);
4721
4722 switch (ctree->type) {
4723 case XML_ELEMENT_CONTENT_PCDATA:
4724 for (i = 0; i < *len;i++)
4725 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
4726 list[(*len)++] = BAD_CAST "#PCDATA";
4727 break;
4728 case XML_ELEMENT_CONTENT_ELEMENT:
4729 for (i = 0; i < *len;i++)
4730 if (xmlStrEqual(ctree->name, list[i])) return(*len);
4731 list[(*len)++] = ctree->name;
4732 break;
4733 case XML_ELEMENT_CONTENT_SEQ:
4734 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4735 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4736 break;
4737 case XML_ELEMENT_CONTENT_OR:
4738 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4739 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4740 break;
4741 }
4742
4743 return(*len);
4744}
4745
4746/**
4747 * xmlValidGetValidElements:
4748 * @prev: an element to insert after
4749 * @next: an element to insert next
4750 * @list: an array to store the list of child names
4751 * @max: the size of the array
4752 *
4753 * This function returns the list of authorized children to insert
4754 * within an existing tree while respecting the validity constraints
4755 * forced by the Dtd. The insertion point is defined using @prev and
4756 * @next in the following ways:
4757 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
4758 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
4759 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
4760 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
4761 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
4762 *
4763 * pointers to the element names are inserted at the beginning of the array
4764 * and do not need to be freed.
4765 *
4766 * returns the number of element in the list, or -1 in case of error. If
4767 * the function returns the value @max the caller is invited to grow the
4768 * receiving array and retry.
4769 */
4770
4771int
4772xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
4773 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004774 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004775 int nb_valid_elements = 0;
4776 const xmlChar *elements[256];
4777 int nb_elements = 0, i;
4778
4779 xmlNode *ref_node;
4780 xmlNode *parent;
4781 xmlNode *test_node;
4782
4783 xmlNode *prev_next;
4784 xmlNode *next_prev;
4785 xmlNode *parent_childs;
4786 xmlNode *parent_last;
4787
4788 xmlElement *element_desc;
4789
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004790 vctxt.userData = NULL;
4791 vctxt.error = NULL;
4792 vctxt.warning = NULL;
4793
Owen Taylor3473f882001-02-23 17:55:21 +00004794 if (prev == NULL && next == NULL)
4795 return(-1);
4796
4797 if (list == NULL) return(-1);
4798 if (max <= 0) return(-1);
4799
4800 nb_valid_elements = 0;
4801 ref_node = prev ? prev : next;
4802 parent = ref_node->parent;
4803
4804 /*
4805 * Retrieves the parent element declaration
4806 */
4807 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
4808 parent->name);
4809 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
4810 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
4811 parent->name);
4812 if (element_desc == NULL) return(-1);
4813
4814 /*
4815 * Do a backup of the current tree structure
4816 */
4817 prev_next = prev ? prev->next : NULL;
4818 next_prev = next ? next->prev : NULL;
4819 parent_childs = parent->children;
4820 parent_last = parent->last;
4821
4822 /*
4823 * Creates a dummy node and insert it into the tree
4824 */
4825 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
4826 test_node->doc = ref_node->doc;
4827 test_node->parent = parent;
4828 test_node->prev = prev;
4829 test_node->next = next;
4830
4831 if (prev) prev->next = test_node;
4832 else parent->children = test_node;
4833
4834 if (next) next->prev = test_node;
4835 else parent->last = test_node;
4836
4837 /*
4838 * Insert each potential child node and check if the parent is
4839 * still valid
4840 */
4841 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
4842 elements, &nb_elements, 256);
4843
4844 for (i = 0;i < nb_elements;i++) {
4845 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004846 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004847 int j;
4848
4849 for (j = 0; j < nb_valid_elements;j++)
4850 if (xmlStrEqual(elements[i], list[j])) break;
4851 list[nb_valid_elements++] = elements[i];
4852 if (nb_valid_elements >= max) break;
4853 }
4854 }
4855
4856 /*
4857 * Restore the tree structure
4858 */
4859 if (prev) prev->next = prev_next;
4860 if (next) next->prev = next_prev;
4861 parent->children = parent_childs;
4862 parent->last = parent_last;
4863
4864 return(nb_valid_elements);
4865}