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