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