blob: a0e9c0f575ffb579a3d2de3a6df1bedea24bad25 [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) {
1274 if ((type == XML_ATTRIBUTE_ID) &&
1275 (xmlScanIDAttributeDecl(NULL, elemDef) != 0))
1276 VERROR(ctxt->userData,
1277 "Element %s has too may ID attributes defined : %s\n",
1278 elem, name);
1279 ret->nexth = elemDef->attributes;
1280 elemDef->attributes = ret;
1281 }
1282
1283 /*
1284 * Link it to the Dtd
1285 */
1286 ret->parent = dtd;
1287 ret->doc = dtd->doc;
1288 if (dtd->last == NULL) {
1289 dtd->children = dtd->last = (xmlNodePtr) ret;
1290 } else {
1291 dtd->last->next = (xmlNodePtr) ret;
1292 ret->prev = dtd->last;
1293 dtd->last = (xmlNodePtr) ret;
1294 }
1295 return(ret);
1296}
1297
1298/**
1299 * xmlFreeAttributeTable:
1300 * @table: An attribute table
1301 *
1302 * Deallocate the memory used by an entities hash table.
1303 */
1304void
1305xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1306 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1307}
1308
1309/**
1310 * xmlCopyAttribute:
1311 * @attr: An attribute
1312 *
1313 * Build a copy of an attribute.
1314 *
1315 * Returns the new xmlAttributePtr or NULL in case of error.
1316 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001317static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001318xmlCopyAttribute(xmlAttributePtr attr) {
1319 xmlAttributePtr cur;
1320
1321 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1322 if (cur == NULL) {
1323 xmlGenericError(xmlGenericErrorContext,
1324 "xmlCopyAttribute: out of memory !\n");
1325 return(NULL);
1326 }
1327 memset(cur, 0, sizeof(xmlAttribute));
1328 cur->atype = attr->atype;
1329 cur->def = attr->def;
1330 cur->tree = xmlCopyEnumeration(attr->tree);
1331 if (attr->elem != NULL)
1332 cur->elem = xmlStrdup(attr->elem);
1333 if (attr->name != NULL)
1334 cur->name = xmlStrdup(attr->name);
1335 if (attr->defaultValue != NULL)
1336 cur->defaultValue = xmlStrdup(attr->defaultValue);
1337 return(cur);
1338}
1339
1340/**
1341 * xmlCopyAttributeTable:
1342 * @table: An attribute table
1343 *
1344 * Build a copy of an attribute table.
1345 *
1346 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1347 */
1348xmlAttributeTablePtr
1349xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1350 return((xmlAttributeTablePtr) xmlHashCopy(table,
1351 (xmlHashCopier) xmlCopyAttribute));
1352}
1353
1354/**
1355 * xmlDumpAttributeDecl:
1356 * @buf: the XML buffer output
1357 * @attr: An attribute declaration
1358 *
1359 * This will dump the content of the attribute declaration as an XML
1360 * DTD definition
1361 */
1362void
1363xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1364 xmlBufferWriteChar(buf, "<!ATTLIST ");
1365 xmlBufferWriteCHAR(buf, attr->elem);
1366 xmlBufferWriteChar(buf, " ");
1367 if (attr->prefix != NULL) {
1368 xmlBufferWriteCHAR(buf, attr->prefix);
1369 xmlBufferWriteChar(buf, ":");
1370 }
1371 xmlBufferWriteCHAR(buf, attr->name);
1372 switch (attr->atype) {
1373 case XML_ATTRIBUTE_CDATA:
1374 xmlBufferWriteChar(buf, " CDATA");
1375 break;
1376 case XML_ATTRIBUTE_ID:
1377 xmlBufferWriteChar(buf, " ID");
1378 break;
1379 case XML_ATTRIBUTE_IDREF:
1380 xmlBufferWriteChar(buf, " IDREF");
1381 break;
1382 case XML_ATTRIBUTE_IDREFS:
1383 xmlBufferWriteChar(buf, " IDREFS");
1384 break;
1385 case XML_ATTRIBUTE_ENTITY:
1386 xmlBufferWriteChar(buf, " ENTITY");
1387 break;
1388 case XML_ATTRIBUTE_ENTITIES:
1389 xmlBufferWriteChar(buf, " ENTITIES");
1390 break;
1391 case XML_ATTRIBUTE_NMTOKEN:
1392 xmlBufferWriteChar(buf, " NMTOKEN");
1393 break;
1394 case XML_ATTRIBUTE_NMTOKENS:
1395 xmlBufferWriteChar(buf, " NMTOKENS");
1396 break;
1397 case XML_ATTRIBUTE_ENUMERATION:
1398 xmlBufferWriteChar(buf, " (");
1399 xmlDumpEnumeration(buf, attr->tree);
1400 break;
1401 case XML_ATTRIBUTE_NOTATION:
1402 xmlBufferWriteChar(buf, " NOTATION (");
1403 xmlDumpEnumeration(buf, attr->tree);
1404 break;
1405 default:
1406 xmlGenericError(xmlGenericErrorContext,
1407 "xmlDumpAttributeTable: internal: unknown type %d\n",
1408 attr->atype);
1409 }
1410 switch (attr->def) {
1411 case XML_ATTRIBUTE_NONE:
1412 break;
1413 case XML_ATTRIBUTE_REQUIRED:
1414 xmlBufferWriteChar(buf, " #REQUIRED");
1415 break;
1416 case XML_ATTRIBUTE_IMPLIED:
1417 xmlBufferWriteChar(buf, " #IMPLIED");
1418 break;
1419 case XML_ATTRIBUTE_FIXED:
1420 xmlBufferWriteChar(buf, " #FIXED");
1421 break;
1422 default:
1423 xmlGenericError(xmlGenericErrorContext,
1424 "xmlDumpAttributeTable: internal: unknown default %d\n",
1425 attr->def);
1426 }
1427 if (attr->defaultValue != NULL) {
1428 xmlBufferWriteChar(buf, " ");
1429 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1430 }
1431 xmlBufferWriteChar(buf, ">\n");
1432}
1433
1434/**
1435 * xmlDumpAttributeTable:
1436 * @buf: the XML buffer output
1437 * @table: An attribute table
1438 *
1439 * This will dump the content of the attribute table as an XML DTD definition
1440 */
1441void
1442xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1443 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1444}
1445
1446/************************************************************************
1447 * *
1448 * NOTATIONs *
1449 * *
1450 ************************************************************************/
1451/**
1452 * xmlCreateNotationTable:
1453 *
1454 * create and initialize an empty notation hash table.
1455 *
1456 * Returns the xmlNotationTablePtr just created or NULL in case
1457 * of error.
1458 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001459static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001460xmlCreateNotationTable(void) {
1461 return(xmlHashCreate(0));
1462}
1463
1464/**
1465 * xmlFreeNotation:
1466 * @not: A notation
1467 *
1468 * Deallocate the memory used by an notation definition
1469 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001470static void
Owen Taylor3473f882001-02-23 17:55:21 +00001471xmlFreeNotation(xmlNotationPtr nota) {
1472 if (nota == NULL) return;
1473 if (nota->name != NULL)
1474 xmlFree((xmlChar *) nota->name);
1475 if (nota->PublicID != NULL)
1476 xmlFree((xmlChar *) nota->PublicID);
1477 if (nota->SystemID != NULL)
1478 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001479 xmlFree(nota);
1480}
1481
1482
1483/**
1484 * xmlAddNotationDecl:
1485 * @dtd: pointer to the DTD
1486 * @ctxt: the validation context
1487 * @name: the entity name
1488 * @PublicID: the public identifier or NULL
1489 * @SystemID: the system identifier or NULL
1490 *
1491 * Register a new notation declaration
1492 *
1493 * Returns NULL if not, othervise the entity
1494 */
1495xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001496xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001497 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001498 const xmlChar *PublicID, const xmlChar *SystemID) {
1499 xmlNotationPtr ret;
1500 xmlNotationTablePtr table;
1501
1502 if (dtd == NULL) {
1503 xmlGenericError(xmlGenericErrorContext,
1504 "xmlAddNotationDecl: dtd == NULL\n");
1505 return(NULL);
1506 }
1507 if (name == NULL) {
1508 xmlGenericError(xmlGenericErrorContext,
1509 "xmlAddNotationDecl: name == NULL\n");
1510 return(NULL);
1511 }
1512 if ((PublicID == NULL) && (SystemID == NULL)) {
1513 xmlGenericError(xmlGenericErrorContext,
1514 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
1515 }
1516
1517 /*
1518 * Create the Notation table if needed.
1519 */
1520 table = (xmlNotationTablePtr) dtd->notations;
1521 if (table == NULL)
1522 dtd->notations = table = xmlCreateNotationTable();
1523 if (table == NULL) {
1524 xmlGenericError(xmlGenericErrorContext,
1525 "xmlAddNotationDecl: Table creation failed!\n");
1526 return(NULL);
1527 }
1528
1529 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1530 if (ret == NULL) {
1531 xmlGenericError(xmlGenericErrorContext,
1532 "xmlAddNotationDecl: out of memory\n");
1533 return(NULL);
1534 }
1535 memset(ret, 0, sizeof(xmlNotation));
1536
1537 /*
1538 * fill the structure.
1539 */
1540 ret->name = xmlStrdup(name);
1541 if (SystemID != NULL)
1542 ret->SystemID = xmlStrdup(SystemID);
1543 if (PublicID != NULL)
1544 ret->PublicID = xmlStrdup(PublicID);
1545
1546 /*
1547 * Validity Check:
1548 * Check the DTD for previous declarations of the ATTLIST
1549 */
1550 if (xmlHashAddEntry(table, name, ret)) {
1551 xmlGenericError(xmlGenericErrorContext,
1552 "xmlAddNotationDecl: %s already defined\n", name);
1553 xmlFreeNotation(ret);
1554 return(NULL);
1555 }
1556 return(ret);
1557}
1558
1559/**
1560 * xmlFreeNotationTable:
1561 * @table: An notation table
1562 *
1563 * Deallocate the memory used by an entities hash table.
1564 */
1565void
1566xmlFreeNotationTable(xmlNotationTablePtr table) {
1567 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
1568}
1569
1570/**
1571 * xmlCopyNotation:
1572 * @nota: A notation
1573 *
1574 * Build a copy of a notation.
1575 *
1576 * Returns the new xmlNotationPtr or NULL in case of error.
1577 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001578static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001579xmlCopyNotation(xmlNotationPtr nota) {
1580 xmlNotationPtr cur;
1581
1582 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1583 if (cur == NULL) {
1584 xmlGenericError(xmlGenericErrorContext,
1585 "xmlCopyNotation: out of memory !\n");
1586 return(NULL);
1587 }
1588 if (nota->name != NULL)
1589 cur->name = xmlStrdup(nota->name);
1590 else
1591 cur->name = NULL;
1592 if (nota->PublicID != NULL)
1593 cur->PublicID = xmlStrdup(nota->PublicID);
1594 else
1595 cur->PublicID = NULL;
1596 if (nota->SystemID != NULL)
1597 cur->SystemID = xmlStrdup(nota->SystemID);
1598 else
1599 cur->SystemID = NULL;
1600 return(cur);
1601}
1602
1603/**
1604 * xmlCopyNotationTable:
1605 * @table: A notation table
1606 *
1607 * Build a copy of a notation table.
1608 *
1609 * Returns the new xmlNotationTablePtr or NULL in case of error.
1610 */
1611xmlNotationTablePtr
1612xmlCopyNotationTable(xmlNotationTablePtr table) {
1613 return((xmlNotationTablePtr) xmlHashCopy(table,
1614 (xmlHashCopier) xmlCopyNotation));
1615}
1616
1617/**
1618 * xmlDumpNotationDecl:
1619 * @buf: the XML buffer output
1620 * @nota: A notation declaration
1621 *
1622 * This will dump the content the notation declaration as an XML DTD definition
1623 */
1624void
1625xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
1626 xmlBufferWriteChar(buf, "<!NOTATION ");
1627 xmlBufferWriteCHAR(buf, nota->name);
1628 if (nota->PublicID != NULL) {
1629 xmlBufferWriteChar(buf, " PUBLIC ");
1630 xmlBufferWriteQuotedString(buf, nota->PublicID);
1631 if (nota->SystemID != NULL) {
1632 xmlBufferWriteChar(buf, " ");
1633 xmlBufferWriteCHAR(buf, nota->SystemID);
1634 }
1635 } else {
1636 xmlBufferWriteChar(buf, " SYSTEM ");
1637 xmlBufferWriteCHAR(buf, nota->SystemID);
1638 }
1639 xmlBufferWriteChar(buf, " >\n");
1640}
1641
1642/**
1643 * xmlDumpNotationTable:
1644 * @buf: the XML buffer output
1645 * @table: A notation table
1646 *
1647 * This will dump the content of the notation table as an XML DTD definition
1648 */
1649void
1650xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
1651 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
1652}
1653
1654/************************************************************************
1655 * *
1656 * IDs *
1657 * *
1658 ************************************************************************/
1659/**
1660 * xmlCreateIDTable:
1661 *
1662 * create and initialize an empty id hash table.
1663 *
1664 * Returns the xmlIDTablePtr just created or NULL in case
1665 * of error.
1666 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001667static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001668xmlCreateIDTable(void) {
1669 return(xmlHashCreate(0));
1670}
1671
1672/**
1673 * xmlFreeID:
1674 * @not: A id
1675 *
1676 * Deallocate the memory used by an id definition
1677 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001678static void
Owen Taylor3473f882001-02-23 17:55:21 +00001679xmlFreeID(xmlIDPtr id) {
1680 if (id == NULL) return;
1681 if (id->value != NULL)
1682 xmlFree((xmlChar *) id->value);
Owen Taylor3473f882001-02-23 17:55:21 +00001683 xmlFree(id);
1684}
1685
1686/**
1687 * xmlAddID:
1688 * @ctxt: the validation context
1689 * @doc: pointer to the document
1690 * @value: the value name
1691 * @attr: the attribute holding the ID
1692 *
1693 * Register a new id declaration
1694 *
1695 * Returns NULL if not, othervise the new xmlIDPtr
1696 */
1697xmlIDPtr
1698xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
1699 xmlAttrPtr attr) {
1700 xmlIDPtr ret;
1701 xmlIDTablePtr table;
1702
1703 if (doc == NULL) {
1704 xmlGenericError(xmlGenericErrorContext,
1705 "xmlAddIDDecl: doc == NULL\n");
1706 return(NULL);
1707 }
1708 if (value == NULL) {
1709 xmlGenericError(xmlGenericErrorContext,
1710 "xmlAddIDDecl: value == NULL\n");
1711 return(NULL);
1712 }
1713 if (attr == NULL) {
1714 xmlGenericError(xmlGenericErrorContext,
1715 "xmlAddIDDecl: attr == NULL\n");
1716 return(NULL);
1717 }
1718
1719 /*
1720 * Create the ID table if needed.
1721 */
1722 table = (xmlIDTablePtr) doc->ids;
1723 if (table == NULL)
1724 doc->ids = table = xmlCreateIDTable();
1725 if (table == NULL) {
1726 xmlGenericError(xmlGenericErrorContext,
1727 "xmlAddID: Table creation failed!\n");
1728 return(NULL);
1729 }
1730
1731 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
1732 if (ret == NULL) {
1733 xmlGenericError(xmlGenericErrorContext,
1734 "xmlAddID: out of memory\n");
1735 return(NULL);
1736 }
1737
1738 /*
1739 * fill the structure.
1740 */
1741 ret->value = xmlStrdup(value);
1742 ret->attr = attr;
1743
1744 if (xmlHashAddEntry(table, value, ret) < 0) {
1745 /*
1746 * The id is already defined in this Dtd.
1747 */
1748 VERROR(ctxt->userData, "ID %s already defined\n", value);
1749 xmlFreeID(ret);
1750 return(NULL);
1751 }
1752 return(ret);
1753}
1754
1755/**
1756 * xmlFreeIDTable:
1757 * @table: An id table
1758 *
1759 * Deallocate the memory used by an ID hash table.
1760 */
1761void
1762xmlFreeIDTable(xmlIDTablePtr table) {
1763 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
1764}
1765
1766/**
1767 * xmlIsID:
1768 * @doc: the document
1769 * @elem: the element carrying the attribute
1770 * @attr: the attribute
1771 *
1772 * Determine whether an attribute is of type ID. In case we have Dtd(s)
1773 * then this is simple, otherwise we use an heuristic: name ID (upper
1774 * or lowercase).
1775 *
1776 * Returns 0 or 1 depending on the lookup result
1777 */
1778int
1779xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
1780 if (doc == NULL) return(0);
1781 if (attr == NULL) return(0);
1782 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
1783 return(0);
1784 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1785 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
1786 (xmlStrEqual(BAD_CAST "name", attr->name)))
1787 return(1);
1788 return(0);
1789 } else {
1790 xmlAttributePtr attrDecl;
1791
1792 if (elem == NULL) return(0);
1793 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
1794 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1795 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
1796 attr->name);
1797
1798 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
1799 return(1);
1800 }
1801 return(0);
1802}
1803
1804/**
1805 * xmlRemoveID
1806 * @doc: the document
1807 * @attr: the attribute
1808 *
1809 * Remove the given attribute from the ID table maintained internally.
1810 *
1811 * Returns -1 if the lookup failed and 0 otherwise
1812 */
1813int
1814xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
1815 xmlAttrPtr cur;
1816 xmlIDTablePtr table;
1817 xmlChar *ID;
1818
1819 if (doc == NULL) return(-1);
1820 if (attr == NULL) return(-1);
1821 table = (xmlIDTablePtr) doc->ids;
1822 if (table == NULL)
1823 return(-1);
1824
1825 if (attr == NULL)
1826 return(-1);
1827 ID = xmlNodeListGetString(doc, attr->children, 1);
1828 if (ID == NULL)
1829 return(-1);
1830 cur = xmlHashLookup(table, ID);
1831 if (cur != attr) {
1832 xmlFree(ID);
1833 return(-1);
1834 }
1835 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
1836 xmlFree(ID);
1837 return(0);
1838}
1839
1840/**
1841 * xmlGetID:
1842 * @doc: pointer to the document
1843 * @ID: the ID value
1844 *
1845 * Search the attribute declaring the given ID
1846 *
1847 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
1848 */
1849xmlAttrPtr
1850xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
1851 xmlIDTablePtr table;
1852 xmlIDPtr id;
1853
1854 if (doc == NULL) {
1855 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
1856 return(NULL);
1857 }
1858
1859 if (ID == NULL) {
1860 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
1861 return(NULL);
1862 }
1863
1864 table = (xmlIDTablePtr) doc->ids;
1865 if (table == NULL)
1866 return(NULL);
1867
1868 id = xmlHashLookup(table, ID);
1869 if (id == NULL)
1870 return(NULL);
1871 return(id->attr);
1872}
1873
1874/************************************************************************
1875 * *
1876 * Refs *
1877 * *
1878 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00001879typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00001880{
1881 xmlListPtr l;
1882 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00001883} xmlRemoveMemo;
1884
1885typedef xmlRemoveMemo *xmlRemoveMemoPtr;
1886
1887typedef struct xmlValidateMemo_t
1888{
1889 xmlValidCtxtPtr ctxt;
1890 const xmlChar *name;
1891} xmlValidateMemo;
1892
1893typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00001894
1895/**
1896 * xmlCreateRefTable:
1897 *
1898 * create and initialize an empty ref hash table.
1899 *
1900 * Returns the xmlRefTablePtr just created or NULL in case
1901 * of error.
1902 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001903static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001904xmlCreateRefTable(void) {
1905 return(xmlHashCreate(0));
1906}
1907
1908/**
1909 * xmlFreeRef:
1910 * @lk: A list link
1911 *
1912 * Deallocate the memory used by a ref definition
1913 */
1914static void
1915xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00001916 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
1917 if (ref == NULL) return;
1918 if (ref->value != NULL)
1919 xmlFree((xmlChar *)ref->value);
1920 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00001921}
1922
1923/**
1924 * xmlFreeRefList:
1925 * @list_ref: A list of references.
1926 *
1927 * Deallocate the memory used by a list of references
1928 */
1929static void
1930xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00001931 if (list_ref == NULL) return;
1932 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00001933}
1934
1935/**
1936 * xmlWalkRemoveRef:
1937 * @data: Contents of current link
1938 * @user: Value supplied by the user
1939 *
1940 * Return 0 to abort the walk or 1 to continue
1941 */
1942static int
1943xmlWalkRemoveRef(const void *data, const void *user)
1944{
Daniel Veillard37721922001-05-04 15:21:12 +00001945 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
1946 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
1947 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00001948
Daniel Veillard37721922001-05-04 15:21:12 +00001949 if (attr0 == attr1) { /* Matched: remove and terminate walk */
1950 xmlListRemoveFirst(ref_list, (void *)data);
1951 return 0;
1952 }
1953 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001954}
1955
1956/**
1957 * xmlAddRef:
1958 * @ctxt: the validation context
1959 * @doc: pointer to the document
1960 * @value: the value name
1961 * @attr: the attribute holding the Ref
1962 *
1963 * Register a new ref declaration
1964 *
1965 * Returns NULL if not, othervise the new xmlRefPtr
1966 */
1967xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001968xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00001969 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00001970 xmlRefPtr ret;
1971 xmlRefTablePtr table;
1972 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00001973
Daniel Veillard37721922001-05-04 15:21:12 +00001974 if (doc == NULL) {
1975 xmlGenericError(xmlGenericErrorContext,
1976 "xmlAddRefDecl: doc == NULL\n");
1977 return(NULL);
1978 }
1979 if (value == NULL) {
1980 xmlGenericError(xmlGenericErrorContext,
1981 "xmlAddRefDecl: value == NULL\n");
1982 return(NULL);
1983 }
1984 if (attr == NULL) {
1985 xmlGenericError(xmlGenericErrorContext,
1986 "xmlAddRefDecl: attr == NULL\n");
1987 return(NULL);
1988 }
Owen Taylor3473f882001-02-23 17:55:21 +00001989
Daniel Veillard37721922001-05-04 15:21:12 +00001990 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001991 * Create the Ref table if needed.
1992 */
Daniel Veillard37721922001-05-04 15:21:12 +00001993 table = (xmlRefTablePtr) doc->refs;
1994 if (table == NULL)
1995 doc->refs = table = xmlCreateRefTable();
1996 if (table == NULL) {
1997 xmlGenericError(xmlGenericErrorContext,
1998 "xmlAddRef: Table creation failed!\n");
1999 return(NULL);
2000 }
Owen Taylor3473f882001-02-23 17:55:21 +00002001
Daniel Veillard37721922001-05-04 15:21:12 +00002002 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2003 if (ret == NULL) {
2004 xmlGenericError(xmlGenericErrorContext,
2005 "xmlAddRef: out of memory\n");
2006 return(NULL);
2007 }
Owen Taylor3473f882001-02-23 17:55:21 +00002008
Daniel Veillard37721922001-05-04 15:21:12 +00002009 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002010 * fill the structure.
2011 */
Daniel Veillard37721922001-05-04 15:21:12 +00002012 ret->value = xmlStrdup(value);
2013 ret->attr = attr;
Owen Taylor3473f882001-02-23 17:55:21 +00002014
Daniel Veillard37721922001-05-04 15:21:12 +00002015 /* To add a reference :-
2016 * References are maintained as a list of references,
2017 * Lookup the entry, if no entry create new nodelist
2018 * Add the owning node to the NodeList
2019 * Return the ref
2020 */
Owen Taylor3473f882001-02-23 17:55:21 +00002021
Daniel Veillard37721922001-05-04 15:21:12 +00002022 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2023 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2024 xmlGenericError(xmlGenericErrorContext,
2025 "xmlAddRef: Reference list creation failed!\n");
2026 return(NULL);
2027 }
2028 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2029 xmlListDelete(ref_list);
2030 xmlGenericError(xmlGenericErrorContext,
2031 "xmlAddRef: Reference list insertion failed!\n");
2032 return(NULL);
2033 }
2034 }
2035 xmlListInsert(ref_list, ret);
2036 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002037}
2038
2039/**
2040 * xmlFreeRefTable:
2041 * @table: An ref table
2042 *
2043 * Deallocate the memory used by an Ref hash table.
2044 */
2045void
2046xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002047 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002048}
2049
2050/**
2051 * xmlIsRef:
2052 * @doc: the document
2053 * @elem: the element carrying the attribute
2054 * @attr: the attribute
2055 *
2056 * Determine whether an attribute is of type Ref. In case we have Dtd(s)
2057 * then this is simple, otherwise we use an heuristic: name Ref (upper
2058 * or lowercase).
2059 *
2060 * Returns 0 or 1 depending on the lookup result
2061 */
2062int
2063xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002064 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2065 return(0);
2066 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2067 /* TODO @@@ */
2068 return(0);
2069 } else {
2070 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002071
Daniel Veillard37721922001-05-04 15:21:12 +00002072 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2073 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2074 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2075 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002076
Daniel Veillard37721922001-05-04 15:21:12 +00002077 if ((attrDecl != NULL) &&
2078 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2079 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2080 return(1);
2081 }
2082 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002083}
2084
2085/**
2086 * xmlRemoveRef
2087 * @doc: the document
2088 * @attr: the attribute
2089 *
2090 * Remove the given attribute from the Ref table maintained internally.
2091 *
2092 * Returns -1 if the lookup failed and 0 otherwise
2093 */
2094int
2095xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002096 xmlListPtr ref_list;
2097 xmlRefTablePtr table;
2098 xmlChar *ID;
2099 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002100
Daniel Veillard37721922001-05-04 15:21:12 +00002101 if (doc == NULL) return(-1);
2102 if (attr == NULL) return(-1);
2103 table = (xmlRefTablePtr) doc->refs;
2104 if (table == NULL)
2105 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002106
Daniel Veillard37721922001-05-04 15:21:12 +00002107 if (attr == NULL)
2108 return(-1);
2109 ID = xmlNodeListGetString(doc, attr->children, 1);
2110 if (ID == NULL)
2111 return(-1);
2112 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002113
Daniel Veillard37721922001-05-04 15:21:12 +00002114 if(ref_list == NULL) {
2115 xmlFree(ID);
2116 return (-1);
2117 }
2118 /* At this point, ref_list refers to a list of references which
2119 * have the same key as the supplied attr. Our list of references
2120 * is ordered by reference address and we don't have that information
2121 * here to use when removing. We'll have to walk the list and
2122 * check for a matching attribute, when we find one stop the walk
2123 * and remove the entry.
2124 * The list is ordered by reference, so that means we don't have the
2125 * key. Passing the list and the reference to the walker means we
2126 * will have enough data to be able to remove the entry.
2127 */
2128 target.l = ref_list;
2129 target.ap = attr;
2130
2131 /* Remove the supplied attr from our list */
2132 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002133
Daniel Veillard37721922001-05-04 15:21:12 +00002134 /*If the list is empty then remove the list entry in the hash */
2135 if (xmlListEmpty(ref_list))
2136 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2137 xmlFreeRefList);
2138 xmlFree(ID);
2139 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002140}
2141
2142/**
2143 * xmlGetRefs:
2144 * @doc: pointer to the document
2145 * @ID: the ID value
2146 *
2147 * Find the set of references for the supplied ID.
2148 *
2149 * Returns NULL if not found, otherwise node set for the ID.
2150 */
2151xmlListPtr
2152xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002153 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002154
Daniel Veillard37721922001-05-04 15:21:12 +00002155 if (doc == NULL) {
2156 xmlGenericError(xmlGenericErrorContext, "xmlGetRef: doc == NULL\n");
2157 return(NULL);
2158 }
Owen Taylor3473f882001-02-23 17:55:21 +00002159
Daniel Veillard37721922001-05-04 15:21:12 +00002160 if (ID == NULL) {
2161 xmlGenericError(xmlGenericErrorContext, "xmlGetRef: ID == NULL\n");
2162 return(NULL);
2163 }
Owen Taylor3473f882001-02-23 17:55:21 +00002164
Daniel Veillard37721922001-05-04 15:21:12 +00002165 table = (xmlRefTablePtr) doc->refs;
2166 if (table == NULL)
2167 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002168
Daniel Veillard37721922001-05-04 15:21:12 +00002169 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002170}
2171
2172/************************************************************************
2173 * *
2174 * Routines for validity checking *
2175 * *
2176 ************************************************************************/
2177
2178/**
2179 * xmlGetDtdElementDesc:
2180 * @dtd: a pointer to the DtD to search
2181 * @name: the element name
2182 *
2183 * Search the Dtd for the description of this element
2184 *
2185 * returns the xmlElementPtr if found or NULL
2186 */
2187
2188xmlElementPtr
2189xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2190 xmlElementTablePtr table;
2191 xmlElementPtr cur;
2192 xmlChar *uqname = NULL, *prefix = NULL;
2193
2194 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002195 if (dtd->elements == NULL)
2196 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002197 table = (xmlElementTablePtr) dtd->elements;
2198
2199 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002200 if (uqname != NULL)
2201 name = uqname;
2202 cur = xmlHashLookup2(table, name, prefix);
2203 if (prefix != NULL) xmlFree(prefix);
2204 if (uqname != NULL) xmlFree(uqname);
2205 return(cur);
2206}
2207/**
2208 * xmlGetDtdElementDesc2:
2209 * @dtd: a pointer to the DtD to search
2210 * @name: the element name
2211 * @create: create an empty description if not found
2212 *
2213 * Search the Dtd for the description of this element
2214 *
2215 * returns the xmlElementPtr if found or NULL
2216 */
2217
2218xmlElementPtr
2219xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2220 xmlElementTablePtr table;
2221 xmlElementPtr cur;
2222 xmlChar *uqname = NULL, *prefix = NULL;
2223
2224 if (dtd == NULL) return(NULL);
2225 if (dtd->elements == NULL) {
2226 if (!create)
2227 return(NULL);
2228 /*
2229 * Create the Element table if needed.
2230 */
2231 table = (xmlElementTablePtr) dtd->elements;
2232 if (table == NULL) {
2233 table = xmlCreateElementTable();
2234 dtd->elements = (void *) table;
2235 }
2236 if (table == NULL) {
2237 xmlGenericError(xmlGenericErrorContext,
2238 "xmlGetDtdElementDesc: Table creation failed!\n");
2239 return(NULL);
2240 }
2241 }
2242 table = (xmlElementTablePtr) dtd->elements;
2243
2244 uqname = xmlSplitQName2(name, &prefix);
2245 if (uqname != NULL)
2246 name = uqname;
2247 cur = xmlHashLookup2(table, name, prefix);
2248 if ((cur == NULL) && (create)) {
2249 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2250 if (cur == NULL) {
2251 xmlGenericError(xmlGenericErrorContext,
2252 "xmlGetDtdElementDesc: out of memory\n");
2253 return(NULL);
2254 }
2255 memset(cur, 0, sizeof(xmlElement));
2256 cur->type = XML_ELEMENT_DECL;
2257
2258 /*
2259 * fill the structure.
2260 */
2261 cur->name = xmlStrdup(name);
2262 cur->prefix = xmlStrdup(prefix);
2263 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2264
2265 xmlHashAddEntry2(table, name, prefix, cur);
2266 }
2267 if (prefix != NULL) xmlFree(prefix);
2268 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002269 return(cur);
2270}
2271
2272/**
2273 * xmlGetDtdQElementDesc:
2274 * @dtd: a pointer to the DtD to search
2275 * @name: the element name
2276 * @prefix: the element namespace prefix
2277 *
2278 * Search the Dtd for the description of this element
2279 *
2280 * returns the xmlElementPtr if found or NULL
2281 */
2282
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002283static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002284xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2285 const xmlChar *prefix) {
2286 xmlElementTablePtr table;
2287
2288 if (dtd == NULL) return(NULL);
2289 if (dtd->elements == NULL) return(NULL);
2290 table = (xmlElementTablePtr) dtd->elements;
2291
2292 return(xmlHashLookup2(table, name, prefix));
2293}
2294
2295/**
2296 * xmlGetDtdAttrDesc:
2297 * @dtd: a pointer to the DtD to search
2298 * @elem: the element name
2299 * @name: the attribute name
2300 *
2301 * Search the Dtd for the description of this attribute on
2302 * this element.
2303 *
2304 * returns the xmlAttributePtr if found or NULL
2305 */
2306
2307xmlAttributePtr
2308xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2309 xmlAttributeTablePtr table;
2310 xmlAttributePtr cur;
2311 xmlChar *uqname = NULL, *prefix = NULL;
2312
2313 if (dtd == NULL) return(NULL);
2314 if (dtd->attributes == NULL) return(NULL);
2315
2316 table = (xmlAttributeTablePtr) dtd->attributes;
2317 if (table == NULL)
2318 return(NULL);
2319
2320 uqname = xmlSplitQName2(name, &prefix);
2321
2322 if (uqname != NULL) {
2323 cur = xmlHashLookup3(table, uqname, prefix, elem);
2324 if (prefix != NULL) xmlFree(prefix);
2325 if (uqname != NULL) xmlFree(uqname);
2326 } else
2327 cur = xmlHashLookup3(table, name, NULL, elem);
2328 return(cur);
2329}
2330
2331/**
2332 * xmlGetDtdQAttrDesc:
2333 * @dtd: a pointer to the DtD to search
2334 * @elem: the element name
2335 * @name: the attribute name
2336 * @prefix: the attribute namespace prefix
2337 *
2338 * Search the Dtd for the description of this qualified attribute on
2339 * this element.
2340 *
2341 * returns the xmlAttributePtr if found or NULL
2342 */
2343
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002344static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002345xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2346 const xmlChar *prefix) {
2347 xmlAttributeTablePtr table;
2348
2349 if (dtd == NULL) return(NULL);
2350 if (dtd->attributes == NULL) return(NULL);
2351 table = (xmlAttributeTablePtr) dtd->attributes;
2352
2353 return(xmlHashLookup3(table, name, prefix, elem));
2354}
2355
2356/**
2357 * xmlGetDtdNotationDesc:
2358 * @dtd: a pointer to the DtD to search
2359 * @name: the notation name
2360 *
2361 * Search the Dtd for the description of this notation
2362 *
2363 * returns the xmlNotationPtr if found or NULL
2364 */
2365
2366xmlNotationPtr
2367xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2368 xmlNotationTablePtr table;
2369
2370 if (dtd == NULL) return(NULL);
2371 if (dtd->notations == NULL) return(NULL);
2372 table = (xmlNotationTablePtr) dtd->notations;
2373
2374 return(xmlHashLookup(table, name));
2375}
2376
2377/**
2378 * xmlValidateNotationUse:
2379 * @ctxt: the validation context
2380 * @doc: the document
2381 * @notationName: the notation name to check
2382 *
2383 * Validate that the given mame match a notation declaration.
2384 * - [ VC: Notation Declared ]
2385 *
2386 * returns 1 if valid or 0 otherwise
2387 */
2388
2389int
2390xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2391 const xmlChar *notationName) {
2392 xmlNotationPtr notaDecl;
2393 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2394
2395 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2396 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2397 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2398
2399 if (notaDecl == NULL) {
2400 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2401 notationName);
2402 return(0);
2403 }
2404 return(1);
2405}
2406
2407/**
2408 * xmlIsMixedElement
2409 * @doc: the document
2410 * @name: the element name
2411 *
2412 * Search in the DtDs whether an element accept Mixed content (or ANY)
2413 * basically if it is supposed to accept text childs
2414 *
2415 * returns 0 if no, 1 if yes, and -1 if no element description is available
2416 */
2417
2418int
2419xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2420 xmlElementPtr elemDecl;
2421
2422 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2423
2424 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2425 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2426 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2427 if (elemDecl == NULL) return(-1);
2428 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002429 case XML_ELEMENT_TYPE_UNDEFINED:
2430 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002431 case XML_ELEMENT_TYPE_ELEMENT:
2432 return(0);
2433 case XML_ELEMENT_TYPE_EMPTY:
2434 /*
2435 * return 1 for EMPTY since we want VC error to pop up
2436 * on <empty> </empty> for example
2437 */
2438 case XML_ELEMENT_TYPE_ANY:
2439 case XML_ELEMENT_TYPE_MIXED:
2440 return(1);
2441 }
2442 return(1);
2443}
2444
2445/**
2446 * xmlValidateNameValue:
2447 * @value: an Name value
2448 *
2449 * Validate that the given value match Name production
2450 *
2451 * returns 1 if valid or 0 otherwise
2452 */
2453
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002454static int
Owen Taylor3473f882001-02-23 17:55:21 +00002455xmlValidateNameValue(const xmlChar *value) {
2456 const xmlChar *cur;
2457
2458 if (value == NULL) return(0);
2459 cur = value;
2460
2461 if (!IS_LETTER(*cur) && (*cur != '_') &&
2462 (*cur != ':')) {
2463 return(0);
2464 }
2465
2466 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2467 (*cur == '.') || (*cur == '-') ||
2468 (*cur == '_') || (*cur == ':') ||
2469 (IS_COMBINING(*cur)) ||
2470 (IS_EXTENDER(*cur)))
2471 cur++;
2472
2473 if (*cur != 0) return(0);
2474
2475 return(1);
2476}
2477
2478/**
2479 * xmlValidateNamesValue:
2480 * @value: an Names value
2481 *
2482 * Validate that the given value match Names production
2483 *
2484 * returns 1 if valid or 0 otherwise
2485 */
2486
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002487static int
Owen Taylor3473f882001-02-23 17:55:21 +00002488xmlValidateNamesValue(const xmlChar *value) {
2489 const xmlChar *cur;
2490
2491 if (value == NULL) return(0);
2492 cur = value;
2493
2494 if (!IS_LETTER(*cur) && (*cur != '_') &&
2495 (*cur != ':')) {
2496 return(0);
2497 }
2498
2499 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2500 (*cur == '.') || (*cur == '-') ||
2501 (*cur == '_') || (*cur == ':') ||
2502 (IS_COMBINING(*cur)) ||
2503 (IS_EXTENDER(*cur)))
2504 cur++;
2505
2506 while (IS_BLANK(*cur)) {
2507 while (IS_BLANK(*cur)) cur++;
2508
2509 if (!IS_LETTER(*cur) && (*cur != '_') &&
2510 (*cur != ':')) {
2511 return(0);
2512 }
2513
2514 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2515 (*cur == '.') || (*cur == '-') ||
2516 (*cur == '_') || (*cur == ':') ||
2517 (IS_COMBINING(*cur)) ||
2518 (IS_EXTENDER(*cur)))
2519 cur++;
2520 }
2521
2522 if (*cur != 0) return(0);
2523
2524 return(1);
2525}
2526
2527/**
2528 * xmlValidateNmtokenValue:
2529 * @value: an Mntoken value
2530 *
2531 * Validate that the given value match Nmtoken production
2532 *
2533 * [ VC: Name Token ]
2534 *
2535 * returns 1 if valid or 0 otherwise
2536 */
2537
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002538static int
Owen Taylor3473f882001-02-23 17:55:21 +00002539xmlValidateNmtokenValue(const xmlChar *value) {
2540 const xmlChar *cur;
2541
2542 if (value == NULL) return(0);
2543 cur = value;
2544
2545 if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) &&
2546 (*cur != '.') && (*cur != '-') &&
2547 (*cur != '_') && (*cur != ':') &&
2548 (!IS_COMBINING(*cur)) &&
2549 (!IS_EXTENDER(*cur)))
2550 return(0);
2551
2552 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2553 (*cur == '.') || (*cur == '-') ||
2554 (*cur == '_') || (*cur == ':') ||
2555 (IS_COMBINING(*cur)) ||
2556 (IS_EXTENDER(*cur)))
2557 cur++;
2558
2559 if (*cur != 0) return(0);
2560
2561 return(1);
2562}
2563
2564/**
2565 * xmlValidateNmtokensValue:
2566 * @value: an Mntokens value
2567 *
2568 * Validate that the given value match Nmtokens production
2569 *
2570 * [ VC: Name Token ]
2571 *
2572 * returns 1 if valid or 0 otherwise
2573 */
2574
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002575static int
Owen Taylor3473f882001-02-23 17:55:21 +00002576xmlValidateNmtokensValue(const xmlChar *value) {
2577 const xmlChar *cur;
2578
2579 if (value == NULL) return(0);
2580 cur = value;
2581
2582 while (IS_BLANK(*cur)) cur++;
2583 if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) &&
2584 (*cur != '.') && (*cur != '-') &&
2585 (*cur != '_') && (*cur != ':') &&
2586 (!IS_COMBINING(*cur)) &&
2587 (!IS_EXTENDER(*cur)))
2588 return(0);
2589
2590 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2591 (*cur == '.') || (*cur == '-') ||
2592 (*cur == '_') || (*cur == ':') ||
2593 (IS_COMBINING(*cur)) ||
2594 (IS_EXTENDER(*cur)))
2595 cur++;
2596
2597 while (IS_BLANK(*cur)) {
2598 while (IS_BLANK(*cur)) cur++;
2599 if (*cur == 0) return(1);
2600
2601 if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) &&
2602 (*cur != '.') && (*cur != '-') &&
2603 (*cur != '_') && (*cur != ':') &&
2604 (!IS_COMBINING(*cur)) &&
2605 (!IS_EXTENDER(*cur)))
2606 return(0);
2607
2608 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2609 (*cur == '.') || (*cur == '-') ||
2610 (*cur == '_') || (*cur == ':') ||
2611 (IS_COMBINING(*cur)) ||
2612 (IS_EXTENDER(*cur)))
2613 cur++;
2614 }
2615
2616 if (*cur != 0) return(0);
2617
2618 return(1);
2619}
2620
2621/**
2622 * xmlValidateNotationDecl:
2623 * @ctxt: the validation context
2624 * @doc: a document instance
2625 * @nota: a notation definition
2626 *
2627 * Try to validate a single notation definition
2628 * basically it does the following checks as described by the
2629 * XML-1.0 recommendation:
2630 * - it seems that no validity constraing exist on notation declarations
2631 * But this function get called anyway ...
2632 *
2633 * returns 1 if valid or 0 otherwise
2634 */
2635
2636int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002637xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
2638 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002639 int ret = 1;
2640
2641 return(ret);
2642}
2643
2644/**
2645 * xmlValidateAttributeValue:
2646 * @type: an attribute type
2647 * @value: an attribute value
2648 *
2649 * Validate that the given attribute value match the proper production
2650 *
2651 * [ VC: ID ]
2652 * Values of type ID must match the Name production....
2653 *
2654 * [ VC: IDREF ]
2655 * Values of type IDREF must match the Name production, and values
2656 * of type IDREFS must match Names ...
2657 *
2658 * [ VC: Entity Name ]
2659 * Values of type ENTITY must match the Name production, values
2660 * of type ENTITIES must match Names ...
2661 *
2662 * [ VC: Name Token ]
2663 * Values of type NMTOKEN must match the Nmtoken production; values
2664 * of type NMTOKENS must match Nmtokens.
2665 *
2666 * returns 1 if valid or 0 otherwise
2667 */
2668
2669int
2670xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
2671 switch (type) {
2672 case XML_ATTRIBUTE_ENTITIES:
2673 case XML_ATTRIBUTE_IDREFS:
2674 return(xmlValidateNamesValue(value));
2675 case XML_ATTRIBUTE_ENTITY:
2676 case XML_ATTRIBUTE_IDREF:
2677 case XML_ATTRIBUTE_ID:
2678 case XML_ATTRIBUTE_NOTATION:
2679 return(xmlValidateNameValue(value));
2680 case XML_ATTRIBUTE_NMTOKENS:
2681 case XML_ATTRIBUTE_ENUMERATION:
2682 return(xmlValidateNmtokensValue(value));
2683 case XML_ATTRIBUTE_NMTOKEN:
2684 return(xmlValidateNmtokenValue(value));
2685 case XML_ATTRIBUTE_CDATA:
2686 break;
2687 }
2688 return(1);
2689}
2690
2691/**
2692 * xmlValidateAttributeValue2:
2693 * @ctxt: the validation context
2694 * @doc: the document
2695 * @name: the attribute name (used for error reporting only)
2696 * @type: the attribute type
2697 * @value: the attribute value
2698 *
2699 * Validate that the given attribute value match a given type.
2700 * This typically cannot be done before having finished parsing
2701 * the subsets.
2702 *
2703 * [ VC: IDREF ]
2704 * Values of type IDREF must match one of the declared IDs
2705 * Values of type IDREFS must match a sequence of the declared IDs
2706 * each Name must match the value of an ID attribute on some element
2707 * in the XML document; i.e. IDREF values must match the value of
2708 * some ID attribute
2709 *
2710 * [ VC: Entity Name ]
2711 * Values of type ENTITY must match one declared entity
2712 * Values of type ENTITIES must match a sequence of declared entities
2713 *
2714 * [ VC: Notation Attributes ]
2715 * all notation names in the declaration must be declared.
2716 *
2717 * returns 1 if valid or 0 otherwise
2718 */
2719
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002720static int
Owen Taylor3473f882001-02-23 17:55:21 +00002721xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2722 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
2723 int ret = 1;
2724 switch (type) {
2725 case XML_ATTRIBUTE_IDREFS:
2726 case XML_ATTRIBUTE_IDREF:
2727 case XML_ATTRIBUTE_ID:
2728 case XML_ATTRIBUTE_NMTOKENS:
2729 case XML_ATTRIBUTE_ENUMERATION:
2730 case XML_ATTRIBUTE_NMTOKEN:
2731 case XML_ATTRIBUTE_CDATA:
2732 break;
2733 case XML_ATTRIBUTE_ENTITY: {
2734 xmlEntityPtr ent;
2735
2736 ent = xmlGetDocEntity(doc, value);
2737 if (ent == NULL) {
2738 VERROR(ctxt->userData,
2739 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2740 name, value);
2741 ret = 0;
2742 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2743 VERROR(ctxt->userData,
2744 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2745 name, value);
2746 ret = 0;
2747 }
2748 break;
2749 }
2750 case XML_ATTRIBUTE_ENTITIES: {
2751 xmlChar *dup, *nam = NULL, *cur, save;
2752 xmlEntityPtr ent;
2753
2754 dup = xmlStrdup(value);
2755 if (dup == NULL)
2756 return(0);
2757 cur = dup;
2758 while (*cur != 0) {
2759 nam = cur;
2760 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2761 save = *cur;
2762 *cur = 0;
2763 ent = xmlGetDocEntity(doc, nam);
2764 if (ent == NULL) {
2765 VERROR(ctxt->userData,
2766 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
2767 name, nam);
2768 ret = 0;
2769 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2770 VERROR(ctxt->userData,
2771 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
2772 name, nam);
2773 ret = 0;
2774 }
2775 if (save == 0)
2776 break;
2777 *cur = save;
2778 while (IS_BLANK(*cur)) cur++;
2779 }
2780 xmlFree(dup);
2781 break;
2782 }
2783 case XML_ATTRIBUTE_NOTATION: {
2784 xmlNotationPtr nota;
2785
2786 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
2787 if ((nota == NULL) && (doc->extSubset != NULL))
2788 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
2789
2790 if (nota == NULL) {
2791 VERROR(ctxt->userData,
2792 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
2793 name, value);
2794 ret = 0;
2795 }
2796 break;
2797 }
2798 }
2799 return(ret);
2800}
2801
2802/**
2803 * xmlValidNormalizeAttributeValue:
2804 * @doc: the document
2805 * @elem: the parent
2806 * @name: the attribute name
2807 * @value: the attribute value
2808 *
2809 * Does the validation related extra step of the normalization of attribute
2810 * values:
2811 *
2812 * If the declared value is not CDATA, then the XML processor must further
2813 * process the normalized attribute value by discarding any leading and
2814 * trailing space (#x20) characters, and by replacing sequences of space
2815 * (#x20) characters by single space (#x20) character.
2816 *
2817 * returns a new normalized string if normalization is needed, NULL otherwise
2818 * the caller must free the returned value.
2819 */
2820
2821xmlChar *
2822xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
2823 const xmlChar *name, const xmlChar *value) {
2824 xmlChar *ret, *dst;
2825 const xmlChar *src;
2826 xmlAttributePtr attrDecl = NULL;
2827
2828 if (doc == NULL) return(NULL);
2829 if (elem == NULL) return(NULL);
2830 if (name == NULL) return(NULL);
2831 if (value == NULL) return(NULL);
2832
2833 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
2834 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002835 snprintf((char *) qname, sizeof(qname), "%s:%s",
2836 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002837 qname[sizeof(qname) - 1] = 0;
2838 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
2839 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2840 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
2841 }
2842 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
2843 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2844 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
2845
2846 if (attrDecl == NULL)
2847 return(NULL);
2848 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
2849 return(NULL);
2850
2851 ret = xmlStrdup(value);
2852 if (ret == NULL)
2853 return(NULL);
2854 src = value;
2855 dst = ret;
2856 while (*src == 0x20) src++;
2857 while (*src != 0) {
2858 if (*src == 0x20) {
2859 while (*src == 0x20) src++;
2860 if (*src != 0)
2861 *dst++ = 0x20;
2862 } else {
2863 *dst++ = *src++;
2864 }
2865 }
2866 *dst = 0;
2867 return(ret);
2868}
2869
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002870static void
Owen Taylor3473f882001-02-23 17:55:21 +00002871xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002872 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002873 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
2874}
2875
2876/**
2877 * xmlValidateAttributeDecl:
2878 * @ctxt: the validation context
2879 * @doc: a document instance
2880 * @attr: an attribute definition
2881 *
2882 * Try to validate a single attribute definition
2883 * basically it does the following checks as described by the
2884 * XML-1.0 recommendation:
2885 * - [ VC: Attribute Default Legal ]
2886 * - [ VC: Enumeration ]
2887 * - [ VC: ID Attribute Default ]
2888 *
2889 * The ID/IDREF uniqueness and matching are done separately
2890 *
2891 * returns 1 if valid or 0 otherwise
2892 */
2893
2894int
2895xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2896 xmlAttributePtr attr) {
2897 int ret = 1;
2898 int val;
2899 CHECK_DTD;
2900 if(attr == NULL) return(1);
2901
2902 /* Attribute Default Legal */
2903 /* Enumeration */
2904 if (attr->defaultValue != NULL) {
2905 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
2906 if (val == 0) {
2907 VERROR(ctxt->userData,
2908 "Syntax of default value for attribute %s on %s is not valid\n",
2909 attr->name, attr->elem);
2910 }
2911 ret &= val;
2912 }
2913
2914 /* ID Attribute Default */
2915 if ((attr->atype == XML_ATTRIBUTE_ID)&&
2916 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
2917 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
2918 VERROR(ctxt->userData,
2919 "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n",
2920 attr->name, attr->elem);
2921 ret = 0;
2922 }
2923
2924 /* One ID per Element Type */
2925 if (attr->atype == XML_ATTRIBUTE_ID) {
2926 int nbId;
2927
2928 /* the trick is taht we parse DtD as their own internal subset */
2929 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
2930 attr->elem);
2931 if (elem != NULL) {
2932 nbId = xmlScanIDAttributeDecl(NULL, elem);
2933 } else {
2934 xmlAttributeTablePtr table;
2935
2936 /*
2937 * The attribute may be declared in the internal subset and the
2938 * element in the external subset.
2939 */
2940 nbId = 0;
2941 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
2942 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
2943 xmlValidateAttributeIdCallback, &nbId);
2944 }
2945 if (nbId > 1) {
2946 VERROR(ctxt->userData,
2947 "Element %s has %d ID attribute defined in the internal subset : %s\n",
2948 attr->elem, nbId, attr->name);
2949 } else if (doc->extSubset != NULL) {
2950 int extId = 0;
2951 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
2952 if (elem != NULL) {
2953 extId = xmlScanIDAttributeDecl(NULL, elem);
2954 }
2955 if (extId > 1) {
2956 VERROR(ctxt->userData,
2957 "Element %s has %d ID attribute defined in the external subset : %s\n",
2958 attr->elem, extId, attr->name);
2959 } else if (extId + nbId > 1) {
2960 VERROR(ctxt->userData,
2961"Element %s has ID attributes defined in the internal and external subset : %s\n",
2962 attr->elem, attr->name);
2963 }
2964 }
2965 }
2966
2967 /* Validity Constraint: Enumeration */
2968 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
2969 xmlEnumerationPtr tree = attr->tree;
2970 while (tree != NULL) {
2971 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
2972 tree = tree->next;
2973 }
2974 if (tree == NULL) {
2975 VERROR(ctxt->userData,
2976"Default value \"%s\" for attribute %s on %s is not among the enumerated set\n",
2977 attr->defaultValue, attr->name, attr->elem);
2978 ret = 0;
2979 }
2980 }
2981
2982 return(ret);
2983}
2984
2985/**
2986 * xmlValidateElementDecl:
2987 * @ctxt: the validation context
2988 * @doc: a document instance
2989 * @elem: an element definition
2990 *
2991 * Try to validate a single element definition
2992 * basically it does the following checks as described by the
2993 * XML-1.0 recommendation:
2994 * - [ VC: One ID per Element Type ]
2995 * - [ VC: No Duplicate Types ]
2996 * - [ VC: Unique Element Type Declaration ]
2997 *
2998 * returns 1 if valid or 0 otherwise
2999 */
3000
3001int
3002xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3003 xmlElementPtr elem) {
3004 int ret = 1;
3005 xmlElementPtr tst;
3006
3007 CHECK_DTD;
3008
3009 if (elem == NULL) return(1);
3010
3011 /* No Duplicate Types */
3012 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3013 xmlElementContentPtr cur, next;
3014 const xmlChar *name;
3015
3016 cur = elem->content;
3017 while (cur != NULL) {
3018 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3019 if (cur->c1 == NULL) break;
3020 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3021 name = cur->c1->name;
3022 next = cur->c2;
3023 while (next != NULL) {
3024 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3025 if (xmlStrEqual(next->name, name)) {
3026 VERROR(ctxt->userData,
3027 "Definition of %s has duplicate references of %s\n",
3028 elem->name, name);
3029 ret = 0;
3030 }
3031 break;
3032 }
3033 if (next->c1 == NULL) break;
3034 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3035 if (xmlStrEqual(next->c1->name, name)) {
3036 VERROR(ctxt->userData,
3037 "Definition of %s has duplicate references of %s\n",
3038 elem->name, name);
3039 ret = 0;
3040 }
3041 next = next->c2;
3042 }
3043 }
3044 cur = cur->c2;
3045 }
3046 }
3047
3048 /* VC: Unique Element Type Declaration */
3049 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003050 if ((tst != NULL ) && (tst != elem) &&
3051 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003052 VERROR(ctxt->userData, "Redefinition of element %s\n",
3053 elem->name);
3054 ret = 0;
3055 }
3056 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003057 if ((tst != NULL ) && (tst != elem) &&
3058 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003059 VERROR(ctxt->userData, "Redefinition of element %s\n",
3060 elem->name);
3061 ret = 0;
3062 }
3063
Daniel Veillarda10efa82001-04-18 13:09:01 +00003064 /* One ID per Element Type
3065 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003066 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3067 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003068 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003069 return(ret);
3070}
3071
3072/**
3073 * xmlValidateOneAttribute:
3074 * @ctxt: the validation context
3075 * @doc: a document instance
3076 * @elem: an element instance
3077 * @attr: an attribute instance
3078 * @value: the attribute value (without entities processing)
3079 *
3080 * Try to validate a single attribute for an element
3081 * basically it does the following checks as described by the
3082 * XML-1.0 recommendation:
3083 * - [ VC: Attribute Value Type ]
3084 * - [ VC: Fixed Attribute Default ]
3085 * - [ VC: Entity Name ]
3086 * - [ VC: Name Token ]
3087 * - [ VC: ID ]
3088 * - [ VC: IDREF ]
3089 * - [ VC: Entity Name ]
3090 * - [ VC: Notation Attributes ]
3091 *
3092 * The ID/IDREF uniqueness and matching are done separately
3093 *
3094 * returns 1 if valid or 0 otherwise
3095 */
3096
3097int
3098xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3099 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3100 /* xmlElementPtr elemDecl; */
3101 xmlAttributePtr attrDecl = NULL;
3102 int val;
3103 int ret = 1;
3104
3105 CHECK_DTD;
3106 if ((elem == NULL) || (elem->name == NULL)) return(0);
3107 if ((attr == NULL) || (attr->name == NULL)) return(0);
3108
3109 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3110 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003111 snprintf((char *) qname, sizeof(qname), "%s:%s",
3112 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003113 qname[sizeof(qname) - 1] = 0;
3114 if (attr->ns != NULL) {
3115 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3116 attr->name, attr->ns->prefix);
3117 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3118 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3119 attr->name, attr->ns->prefix);
3120 } else {
3121 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3122 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3123 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3124 qname, attr->name);
3125 }
3126 }
3127 if (attrDecl == NULL) {
3128 if (attr->ns != NULL) {
3129 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3130 attr->name, attr->ns->prefix);
3131 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3132 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3133 attr->name, attr->ns->prefix);
3134 } else {
3135 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3136 elem->name, attr->name);
3137 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3138 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3139 elem->name, attr->name);
3140 }
3141 }
3142
3143
3144 /* Validity Constraint: Attribute Value Type */
3145 if (attrDecl == NULL) {
3146 VERROR(ctxt->userData,
3147 "No declaration for attribute %s on element %s\n",
3148 attr->name, elem->name);
3149 return(0);
3150 }
3151 attr->atype = attrDecl->atype;
3152
3153 val = xmlValidateAttributeValue(attrDecl->atype, value);
3154 if (val == 0) {
3155 VERROR(ctxt->userData,
3156 "Syntax of value for attribute %s on %s is not valid\n",
3157 attr->name, elem->name);
3158 ret = 0;
3159 }
3160
3161 /* Validity constraint: Fixed Attribute Default */
3162 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3163 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3164 VERROR(ctxt->userData,
3165 "Value for attribute %s on %s is differnt from default \"%s\"\n",
3166 attr->name, elem->name, attrDecl->defaultValue);
3167 ret = 0;
3168 }
3169 }
3170
3171 /* Validity Constraint: ID uniqueness */
3172 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3173 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3174 ret = 0;
3175 }
3176
3177 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3178 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3179 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3180 ret = 0;
3181 }
3182
3183 /* Validity Constraint: Notation Attributes */
3184 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3185 xmlEnumerationPtr tree = attrDecl->tree;
3186 xmlNotationPtr nota;
3187
3188 /* First check that the given NOTATION was declared */
3189 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3190 if (nota == NULL)
3191 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3192
3193 if (nota == NULL) {
3194 VERROR(ctxt->userData,
3195 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3196 value, attr->name, elem->name);
3197 ret = 0;
3198 }
3199
3200 /* Second, verify that it's among the list */
3201 while (tree != NULL) {
3202 if (xmlStrEqual(tree->name, value)) break;
3203 tree = tree->next;
3204 }
3205 if (tree == NULL) {
3206 VERROR(ctxt->userData,
3207"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3208 value, attr->name, elem->name);
3209 ret = 0;
3210 }
3211 }
3212
3213 /* Validity Constraint: Enumeration */
3214 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3215 xmlEnumerationPtr tree = attrDecl->tree;
3216 while (tree != NULL) {
3217 if (xmlStrEqual(tree->name, value)) break;
3218 tree = tree->next;
3219 }
3220 if (tree == NULL) {
3221 VERROR(ctxt->userData,
3222 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3223 value, attr->name, elem->name);
3224 ret = 0;
3225 }
3226 }
3227
3228 /* Fixed Attribute Default */
3229 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3230 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3231 VERROR(ctxt->userData,
3232 "Value for attribute %s on %s must be \"%s\"\n",
3233 attr->name, elem->name, attrDecl->defaultValue);
3234 ret = 0;
3235 }
3236
3237 /* Extra check for the attribute value */
3238 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3239 attrDecl->atype, value);
3240
3241 return(ret);
3242}
3243
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003244/**
3245 * xmlValidateSkipIgnorable:
3246 * @ctxt: the validation context
3247 * @child: the child list
3248 *
3249 * Skip ignorable elements w.r.t. the validation process
3250 *
3251 * returns the first element to consider for validation of the content model
3252 */
3253
3254static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003255xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003256 while (child != NULL) {
3257 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003258 /* These things are ignored (skipped) during validation. */
3259 case XML_PI_NODE:
3260 case XML_COMMENT_NODE:
3261 case XML_XINCLUDE_START:
3262 case XML_XINCLUDE_END:
3263 child = child->next;
3264 break;
3265 case XML_TEXT_NODE:
3266 if (xmlIsBlankNode(child))
3267 child = child->next;
3268 else
3269 return(child);
3270 break;
3271 /* keep current node */
3272 default:
3273 return(child);
3274 }
3275 }
3276 return(child);
3277}
3278
3279/**
3280 * xmlValidateElementType:
3281 * @ctxt: the validation context
3282 *
3283 * Try to validate the content model of an element internal function
3284 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003285 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3286 * reference is found and -3 if the validation succeeded but
3287 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003288 */
3289
3290static int
3291xmlValidateElementType(xmlValidCtxtPtr ctxt) {
3292 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003293 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003294
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003295 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003296 if ((NODE == NULL) && (CONT == NULL))
3297 return(1);
3298 if ((NODE == NULL) &&
3299 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3300 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3301 return(1);
3302 }
3303 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003304 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003305 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003306
3307 /*
3308 * We arrive here when more states need to be examined
3309 */
3310cont:
3311
3312 /*
3313 * We just recovered from a rollback generated by a possible
3314 * epsilon transition, go directly to the analysis phase
3315 */
3316 if (STATE == ROLLBACK_PARENT) {
3317 DEBUG_VALID_MSG("restaured parent branch");
3318 DEBUG_VALID_STATE(NODE, CONT)
3319 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003320 goto analyze;
3321 }
3322
3323 DEBUG_VALID_STATE(NODE, CONT)
3324 /*
3325 * we may have to save a backup state here. This is the equivalent
3326 * of handling epsilon transition in NFAs.
3327 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003328 if ((CONT != NULL) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003329 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003330 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
3331 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003332 DEBUG_VALID_MSG("saving parent branch");
3333 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3334 }
3335
3336
3337 /*
3338 * Check first if the content matches
3339 */
3340 switch (CONT->type) {
3341 case XML_ELEMENT_CONTENT_PCDATA:
3342 if (NODE == NULL) {
3343 DEBUG_VALID_MSG("pcdata failed no node");
3344 ret = 0;
3345 break;
3346 }
3347 if (NODE->type == XML_TEXT_NODE) {
3348 DEBUG_VALID_MSG("pcdata found, skip to next");
3349 /*
3350 * go to next element in the content model
3351 * skipping ignorable elems
3352 */
3353 do {
3354 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003355 NODE = xmlValidateSkipIgnorable(NODE);
3356 if ((NODE != NULL) &&
3357 (NODE->type == XML_ENTITY_REF_NODE))
3358 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003359 } while ((NODE != NULL) &&
3360 ((NODE->type != XML_ELEMENT_NODE) &&
3361 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003362 ret = 1;
3363 break;
3364 } else {
3365 DEBUG_VALID_MSG("pcdata failed");
3366 ret = 0;
3367 break;
3368 }
3369 break;
3370 case XML_ELEMENT_CONTENT_ELEMENT:
3371 if (NODE == NULL) {
3372 DEBUG_VALID_MSG("element failed no node");
3373 ret = 0;
3374 break;
3375 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003376 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3377 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003378 if (ret == 1) {
3379 DEBUG_VALID_MSG("element found, skip to next");
3380 /*
3381 * go to next element in the content model
3382 * skipping ignorable elems
3383 */
3384 do {
3385 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003386 NODE = xmlValidateSkipIgnorable(NODE);
3387 if ((NODE != NULL) &&
3388 (NODE->type == XML_ENTITY_REF_NODE))
3389 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003390 } while ((NODE != NULL) &&
3391 ((NODE->type != XML_ELEMENT_NODE) &&
3392 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003393 } else {
3394 DEBUG_VALID_MSG("element failed");
3395 ret = 0;
3396 break;
3397 }
3398 break;
3399 case XML_ELEMENT_CONTENT_OR:
3400 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003401 * Small optimization.
3402 */
3403 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3404 if ((NODE == NULL) ||
3405 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3406 DEPTH++;
3407 CONT = CONT->c2;
3408 goto cont;
3409 }
3410 }
3411
3412 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003413 * save the second branch 'or' branch
3414 */
3415 DEBUG_VALID_MSG("saving 'or' branch");
3416 vstateVPush(ctxt, CONT->c2, NODE, DEPTH + 1, OCCURS, ROLLBACK_OR);
3417
3418 DEPTH++;
3419 CONT = CONT->c1;
3420 goto cont;
3421 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003422 /*
3423 * Small optimization.
3424 */
3425 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3426 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3427 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3428 if ((NODE == NULL) ||
3429 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3430 DEPTH++;
3431 CONT = CONT->c2;
3432 goto cont;
3433 }
3434 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003435 DEPTH++;
3436 CONT = CONT->c1;
3437 goto cont;
3438 }
3439
3440 /*
3441 * At this point handle going up in the tree
3442 */
3443 if (ret == -1) {
3444 DEBUG_VALID_MSG("error found returning");
3445 return(ret);
3446 }
3447analyze:
3448 while (CONT != NULL) {
3449 /*
3450 * First do the analysis depending on the occurence model at
3451 * this level.
3452 */
3453 if (ret == 0) {
3454 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003455 xmlNodePtr cur;
3456
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003457 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003458 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003459 DEBUG_VALID_MSG("Once branch failed, rollback");
3460 if (vstateVPop(ctxt) < 0 ) {
3461 DEBUG_VALID_MSG("exhaustion, failed");
3462 return(0);
3463 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003464 if (cur != ctxt->vstate->node)
3465 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003466 goto cont;
3467 case XML_ELEMENT_CONTENT_PLUS:
3468 if (OCCURENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003469 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003470 DEBUG_VALID_MSG("Plus branch failed, rollback");
3471 if (vstateVPop(ctxt) < 0 ) {
3472 DEBUG_VALID_MSG("exhaustion, failed");
3473 return(0);
3474 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003475 if (cur != ctxt->vstate->node)
3476 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003477 goto cont;
3478 }
3479 DEBUG_VALID_MSG("Plus branch found");
3480 ret = 1;
3481 break;
3482 case XML_ELEMENT_CONTENT_MULT:
3483#ifdef DEBUG_VALID_ALGO
3484 if (OCCURENCE == 0) {
3485 DEBUG_VALID_MSG("Mult branch failed");
3486 } else {
3487 DEBUG_VALID_MSG("Mult branch found");
3488 }
3489#endif
3490 ret = 1;
3491 break;
3492 case XML_ELEMENT_CONTENT_OPT:
3493 DEBUG_VALID_MSG("Option branch failed");
3494 ret = 1;
3495 break;
3496 }
3497 } else {
3498 switch (CONT->ocur) {
3499 case XML_ELEMENT_CONTENT_OPT:
3500 DEBUG_VALID_MSG("Option branch succeeded");
3501 ret = 1;
3502 break;
3503 case XML_ELEMENT_CONTENT_ONCE:
3504 DEBUG_VALID_MSG("Once branch succeeded");
3505 ret = 1;
3506 break;
3507 case XML_ELEMENT_CONTENT_PLUS:
3508 if (STATE == ROLLBACK_PARENT) {
3509 DEBUG_VALID_MSG("Plus branch rollback");
3510 ret = 1;
3511 break;
3512 }
3513 if (NODE == NULL) {
3514 DEBUG_VALID_MSG("Plus branch exhausted");
3515 ret = 1;
3516 break;
3517 }
3518 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
3519 SET_OCCURENCE;
3520 goto cont;
3521 case XML_ELEMENT_CONTENT_MULT:
3522 if (STATE == ROLLBACK_PARENT) {
3523 DEBUG_VALID_MSG("Mult branch rollback");
3524 ret = 1;
3525 break;
3526 }
3527 if (NODE == NULL) {
3528 DEBUG_VALID_MSG("Mult branch exhausted");
3529 ret = 1;
3530 break;
3531 }
3532 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
3533 SET_OCCURENCE;
3534 goto cont;
3535 }
3536 }
3537 STATE = 0;
3538
3539 /*
3540 * Then act accordingly at the parent level
3541 */
3542 RESET_OCCURENCE;
3543 if (CONT->parent == NULL)
3544 break;
3545
3546 switch (CONT->parent->type) {
3547 case XML_ELEMENT_CONTENT_PCDATA:
3548 DEBUG_VALID_MSG("Error: parent pcdata");
3549 return(-1);
3550 case XML_ELEMENT_CONTENT_ELEMENT:
3551 DEBUG_VALID_MSG("Error: parent element");
3552 return(-1);
3553 case XML_ELEMENT_CONTENT_OR:
3554 if (ret == 1) {
3555 DEBUG_VALID_MSG("Or succeeded");
3556 CONT = CONT->parent;
3557 DEPTH--;
3558 } else {
3559 DEBUG_VALID_MSG("Or failed");
3560 CONT = CONT->parent;
3561 DEPTH--;
3562 }
3563 break;
3564 case XML_ELEMENT_CONTENT_SEQ:
3565 if (ret == 0) {
3566 DEBUG_VALID_MSG("Sequence failed");
3567 CONT = CONT->parent;
3568 DEPTH--;
3569 } else if (CONT == CONT->parent->c1) {
3570 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3571 CONT = CONT->parent->c2;
3572 goto cont;
3573 } else {
3574 DEBUG_VALID_MSG("Sequence succeeded");
3575 CONT = CONT->parent;
3576 DEPTH--;
3577 }
3578 }
3579 }
3580 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003581 xmlNodePtr cur;
3582
3583 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003584 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3585 if (vstateVPop(ctxt) < 0 ) {
3586 DEBUG_VALID_MSG("exhaustion, failed");
3587 return(0);
3588 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003589 if (cur != ctxt->vstate->node)
3590 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003591 goto cont;
3592 }
3593 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003594 xmlNodePtr cur;
3595
3596 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003597 DEBUG_VALID_MSG("Failure, rollback");
3598 if (vstateVPop(ctxt) < 0 ) {
3599 DEBUG_VALID_MSG("exhaustion, failed");
3600 return(0);
3601 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003602 if (cur != ctxt->vstate->node)
3603 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003604 goto cont;
3605 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003606 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003607}
3608
3609/**
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003610 * xmlSprintfElements:
3611 * @buf: an output buffer
3612 * @content: An element
3613 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3614 *
3615 * This will dump the list of elements to the buffer
3616 * Intended just for the debug routine
3617 */
3618static void
3619xmlSprintfElements(char *buf, xmlNodePtr node, int glob) {
3620 xmlNodePtr cur;
3621
3622 if (node == NULL) return;
3623 if (glob) strcat(buf, "(");
3624 cur = node;
3625 while (cur != NULL) {
3626 switch (cur->type) {
3627 case XML_ELEMENT_NODE:
3628 strcat(buf, (char *) cur->name);
3629 if (cur->next != NULL)
3630 strcat(buf, " ");
3631 break;
3632 case XML_TEXT_NODE:
3633 if (xmlIsBlankNode(cur))
3634 break;
3635 case XML_CDATA_SECTION_NODE:
3636 case XML_ENTITY_REF_NODE:
3637 strcat(buf, "CDATA");
3638 if (cur->next != NULL)
3639 strcat(buf, " ");
3640 break;
3641 case XML_ATTRIBUTE_NODE:
3642 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003643#ifdef LIBXML_DOCB_ENABLED
3644 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003645#endif
3646 case XML_HTML_DOCUMENT_NODE:
3647 case XML_DOCUMENT_TYPE_NODE:
3648 case XML_DOCUMENT_FRAG_NODE:
3649 case XML_NOTATION_NODE:
3650 case XML_NAMESPACE_DECL:
3651 strcat(buf, "???");
3652 if (cur->next != NULL)
3653 strcat(buf, " ");
3654 break;
3655 case XML_ENTITY_NODE:
3656 case XML_PI_NODE:
3657 case XML_DTD_NODE:
3658 case XML_COMMENT_NODE:
3659 case XML_ELEMENT_DECL:
3660 case XML_ATTRIBUTE_DECL:
3661 case XML_ENTITY_DECL:
3662 case XML_XINCLUDE_START:
3663 case XML_XINCLUDE_END:
3664 break;
3665 }
3666 cur = cur->next;
3667 }
3668 if (glob) strcat(buf, ")");
3669}
3670
3671/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003672 * xmlValidateElementContent:
3673 * @ctxt: the validation context
3674 * @child: the child list
3675 * @cont: pointer to the content declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003676 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003677 *
3678 * Try to validate the content model of an element
3679 *
3680 * returns 1 if valid or 0 if not and -1 in case of error
3681 */
3682
3683static int
3684xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillard7533cc82001-04-24 15:52:00 +00003685 xmlElementContentPtr cont, int warn, const xmlChar *name) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003686 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003687 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003688
3689 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003690 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003691 */
3692 ctxt->vstateMax = 8;
3693 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
3694 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
3695 if (ctxt->vstateTab == NULL) {
3696 xmlGenericError(xmlGenericErrorContext,
3697 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003698 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003699 }
3700 /*
3701 * The first entry in the stack is reserved to the current state
3702 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00003703 ctxt->nodeMax = 0;
3704 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00003705 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003706 ctxt->vstate = &ctxt->vstateTab[0];
3707 ctxt->vstateNr = 1;
3708 CONT = cont;
3709 NODE = child;
3710 DEPTH = 0;
3711 OCCURS = 0;
3712 STATE = 0;
3713 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003714 if ((ret == -3) && (warn)) {
3715 VWARNING(ctxt->userData,
3716 "Element %s content model is ambiguous\n", name);
3717 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003718 /*
3719 * An entities reference appeared at this level.
3720 * Buid a minimal representation of this node content
3721 * sufficient to run the validation process on it
3722 */
3723 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003724 cur = child;
3725 while (cur != NULL) {
3726 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003727 case XML_ENTITY_REF_NODE:
3728 /*
3729 * Push the current node to be able to roll back
3730 * and process within the entity
3731 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003732 if ((cur->children != NULL) &&
3733 (cur->children->children != NULL)) {
3734 nodeVPush(ctxt, cur);
3735 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003736 continue;
3737 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00003738 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003739 case XML_TEXT_NODE:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003740 case XML_CDATA_SECTION_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003741 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003742 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003743 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003744 case XML_ELEMENT_NODE:
3745 /*
3746 * Allocate a new node and minimally fills in
3747 * what's required
3748 */
3749 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3750 if (tmp == NULL) {
3751 xmlGenericError(xmlGenericErrorContext,
3752 "xmlValidateElementContent : malloc failed\n");
3753 xmlFreeNodeList(repl);
3754 ret = -1;
3755 goto done;
3756 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003757 tmp->type = cur->type;
3758 tmp->name = cur->name;
3759 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003760 tmp->next = NULL;
3761 if (repl == NULL)
3762 repl = last = tmp;
3763 else {
3764 last->next = tmp;
3765 last = tmp;
3766 }
3767 break;
3768 default:
3769 break;
3770 }
3771 /*
3772 * Switch to next element
3773 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003774 cur = cur->next;
3775 while (cur == NULL) {
3776 cur = nodeVPop(ctxt);
3777 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003778 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003779 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003780 }
3781 }
3782
3783 /*
3784 * Relaunch the validation
3785 */
3786 ctxt->vstate = &ctxt->vstateTab[0];
3787 ctxt->vstateNr = 1;
3788 CONT = cont;
3789 NODE = repl;
3790 DEPTH = 0;
3791 OCCURS = 0;
3792 STATE = 0;
3793 ret = xmlValidateElementType(ctxt);
3794 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003795 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003796 char expr[5000];
3797 char list[5000];
3798
3799 expr[0] = 0;
3800 xmlSprintfElementContent(expr, cont, 1);
3801 list[0] = 0;
3802 if (repl != NULL)
3803 xmlSprintfElements(list, repl, 1);
3804 else
3805 xmlSprintfElements(list, child, 1);
3806
Daniel Veillard7533cc82001-04-24 15:52:00 +00003807 if (name != NULL) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003808 VERROR(ctxt->userData,
3809 "Element %s content doesn't follow the Dtd\nExpecting %s, got %s\n",
Daniel Veillard7533cc82001-04-24 15:52:00 +00003810 name, expr, list);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003811 } else {
3812 VERROR(ctxt->userData,
3813 "Element content doesn't follow the Dtd\nExpecting %s, got %s\n",
3814 expr, list);
3815 }
3816 ret = 0;
3817 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003818 if (ret == -3)
3819 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003820
3821
3822done:
3823 /*
3824 * Deallocate the copy if done, and free up the validation stack
3825 */
3826 while (repl != NULL) {
3827 tmp = repl->next;
3828 xmlFree(repl);
3829 repl = tmp;
3830 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003831 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003832 if (ctxt->vstateTab != NULL) {
3833 xmlFree(ctxt->vstateTab);
3834 ctxt->vstateTab = NULL;
3835 }
3836 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00003837 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003838 if (ctxt->nodeTab != NULL) {
3839 xmlFree(ctxt->nodeTab);
3840 ctxt->nodeTab = NULL;
3841 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003842 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003843
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003844}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003845
Owen Taylor3473f882001-02-23 17:55:21 +00003846/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003847 * xmlValidateCdataElement:
3848 * @ctxt: the validation context
3849 * @doc: a document instance
3850 * @elem: an element instance
3851 *
3852 * Check that an element follows #CDATA
3853 *
3854 * returns 1 if valid or 0 otherwise
3855 */
3856static int
3857xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3858 xmlNodePtr elem) {
3859 int ret = 1;
3860 xmlNodePtr cur, child;
3861
3862 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
3863 return(0);
3864
3865 child = elem->children;
3866
3867 cur = child;
3868 while (cur != NULL) {
3869 switch (cur->type) {
3870 case XML_ENTITY_REF_NODE:
3871 /*
3872 * Push the current node to be able to roll back
3873 * and process within the entity
3874 */
3875 if ((cur->children != NULL) &&
3876 (cur->children->children != NULL)) {
3877 nodeVPush(ctxt, cur);
3878 cur = cur->children->children;
3879 continue;
3880 }
3881 break;
3882 case XML_COMMENT_NODE:
3883 case XML_PI_NODE:
3884 case XML_TEXT_NODE:
3885 case XML_CDATA_SECTION_NODE:
3886 break;
3887 default:
3888 ret = 0;
3889 goto done;
3890 }
3891 /*
3892 * Switch to next element
3893 */
3894 cur = cur->next;
3895 while (cur == NULL) {
3896 cur = nodeVPop(ctxt);
3897 if (cur == NULL)
3898 break;
3899 cur = cur->next;
3900 }
3901 }
3902done:
3903 ctxt->nodeMax = 0;
3904 ctxt->nodeNr = 0;
3905 if (ctxt->nodeTab != NULL) {
3906 xmlFree(ctxt->nodeTab);
3907 ctxt->nodeTab = NULL;
3908 }
3909 return(ret);
3910}
3911
3912/**
Owen Taylor3473f882001-02-23 17:55:21 +00003913 * xmlValidateOneElement:
3914 * @ctxt: the validation context
3915 * @doc: a document instance
3916 * @elem: an element instance
3917 *
3918 * Try to validate a single element and it's attributes,
3919 * basically it does the following checks as described by the
3920 * XML-1.0 recommendation:
3921 * - [ VC: Element Valid ]
3922 * - [ VC: Required Attribute ]
3923 * Then call xmlValidateOneAttribute() for each attribute present.
3924 *
3925 * The ID/IDREF checkings are done separately
3926 *
3927 * returns 1 if valid or 0 otherwise
3928 */
3929
3930int
3931xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3932 xmlNodePtr elem) {
3933 xmlElementPtr elemDecl = NULL;
3934 xmlElementContentPtr cont;
3935 xmlAttributePtr attr;
3936 xmlNodePtr child;
3937 int ret = 1;
3938 const xmlChar *name;
3939
3940 CHECK_DTD;
3941
3942 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003943 switch (elem->type) {
3944 case XML_ATTRIBUTE_NODE:
3945 VERROR(ctxt->userData,
3946 "Attribute element not expected here\n");
3947 return(0);
3948 case XML_TEXT_NODE:
3949 if (elem->children != NULL) {
3950 VERROR(ctxt->userData, "Text element has childs !\n");
3951 return(0);
3952 }
3953 if (elem->properties != NULL) {
3954 VERROR(ctxt->userData, "Text element has attributes !\n");
3955 return(0);
3956 }
3957 if (elem->ns != NULL) {
3958 VERROR(ctxt->userData, "Text element has namespace !\n");
3959 return(0);
3960 }
3961 if (elem->nsDef != NULL) {
3962 VERROR(ctxt->userData,
3963 "Text element carries namespace definitions !\n");
3964 return(0);
3965 }
3966 if (elem->content == NULL) {
3967 VERROR(ctxt->userData,
3968 "Text element has no content !\n");
3969 return(0);
3970 }
3971 return(1);
3972 case XML_XINCLUDE_START:
3973 case XML_XINCLUDE_END:
3974 return(1);
3975 case XML_CDATA_SECTION_NODE:
3976 case XML_ENTITY_REF_NODE:
3977 case XML_PI_NODE:
3978 case XML_COMMENT_NODE:
3979 return(1);
3980 case XML_ENTITY_NODE:
3981 VERROR(ctxt->userData,
3982 "Entity element not expected here\n");
3983 return(0);
3984 case XML_NOTATION_NODE:
3985 VERROR(ctxt->userData,
3986 "Notation element not expected here\n");
3987 return(0);
3988 case XML_DOCUMENT_NODE:
3989 case XML_DOCUMENT_TYPE_NODE:
3990 case XML_DOCUMENT_FRAG_NODE:
3991 VERROR(ctxt->userData,
3992 "Document element not expected here\n");
3993 return(0);
3994 case XML_HTML_DOCUMENT_NODE:
3995 VERROR(ctxt->userData,
3996 "\n");
3997 return(0);
3998 case XML_ELEMENT_NODE:
3999 break;
4000 default:
4001 VERROR(ctxt->userData,
4002 "unknown element type %d\n", elem->type);
4003 return(0);
4004 }
4005 if (elem->name == NULL) return(0);
4006
4007 /*
4008 * Fetch the declaration for the qualified name
4009 */
4010 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
4011 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
4012 elem->name, elem->ns->prefix);
4013 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4014 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
4015 elem->name, elem->ns->prefix);
4016 }
4017
4018 /*
4019 * Fetch the declaration for the non qualified name
4020 */
4021 if (elemDecl == NULL) {
4022 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4023 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4024 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4025 }
4026 if (elemDecl == NULL) {
4027 VERROR(ctxt->userData, "No declaration for element %s\n",
4028 elem->name);
4029 return(0);
4030 }
4031
4032 /* Check taht the element content matches the definition */
4033 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004034 case XML_ELEMENT_TYPE_UNDEFINED:
4035 VERROR(ctxt->userData, "No declaration for element %s\n",
4036 elem->name);
4037 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004038 case XML_ELEMENT_TYPE_EMPTY:
4039 if (elem->children != NULL) {
4040 VERROR(ctxt->userData,
4041 "Element %s was declared EMPTY this one has content\n",
4042 elem->name);
4043 ret = 0;
4044 }
4045 break;
4046 case XML_ELEMENT_TYPE_ANY:
4047 /* I don't think anything is required then */
4048 break;
4049 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004050 /* simple case of declared as #PCDATA */
4051 if ((elemDecl->content != NULL) &&
4052 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4053 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4054 if (!ret) {
4055 VERROR(ctxt->userData,
4056 "Element %s was declared #PCDATA but contains non text nodes\n",
4057 elem->name);
4058 }
4059 break;
4060 }
Owen Taylor3473f882001-02-23 17:55:21 +00004061 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004062 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004063 while (child != NULL) {
4064 if (child->type == XML_ELEMENT_NODE) {
4065 name = child->name;
4066 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4067 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004068 snprintf((char *) qname, sizeof(qname), "%s:%s",
4069 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004070 qname[sizeof(qname) - 1] = 0;
4071 cont = elemDecl->content;
4072 while (cont != NULL) {
4073 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4074 if (xmlStrEqual(cont->name, qname)) break;
4075 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4076 (cont->c1 != NULL) &&
4077 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4078 if (xmlStrEqual(cont->c1->name, qname)) break;
4079 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4080 (cont->c1 == NULL) ||
4081 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4082 /* Internal error !!! */
4083 xmlGenericError(xmlGenericErrorContext,
4084 "Internal: MIXED struct bad\n");
4085 break;
4086 }
4087 cont = cont->c2;
4088 }
4089 if (cont != NULL)
4090 goto child_ok;
4091 }
4092 cont = elemDecl->content;
4093 while (cont != NULL) {
4094 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4095 if (xmlStrEqual(cont->name, name)) break;
4096 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4097 (cont->c1 != NULL) &&
4098 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4099 if (xmlStrEqual(cont->c1->name, name)) break;
4100 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4101 (cont->c1 == NULL) ||
4102 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4103 /* Internal error !!! */
4104 xmlGenericError(xmlGenericErrorContext,
4105 "Internal: MIXED struct bad\n");
4106 break;
4107 }
4108 cont = cont->c2;
4109 }
4110 if (cont == NULL) {
4111 VERROR(ctxt->userData,
4112 "Element %s is not declared in %s list of possible childs\n",
4113 name, elem->name);
4114 ret = 0;
4115 }
4116 }
4117child_ok:
4118 child = child->next;
4119 }
4120 break;
4121 case XML_ELEMENT_TYPE_ELEMENT:
4122 child = elem->children;
4123 cont = elemDecl->content;
Daniel Veillard7533cc82001-04-24 15:52:00 +00004124 ret = xmlValidateElementContent(ctxt, child, cont, 1, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004125 break;
4126 }
4127
4128 /* [ VC: Required Attribute ] */
4129 attr = elemDecl->attributes;
4130 while (attr != NULL) {
4131 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
4132 xmlAttrPtr attrib;
4133 int qualified = -1;
4134
4135 attrib = elem->properties;
4136 while (attrib != NULL) {
4137 if (xmlStrEqual(attrib->name, attr->name)) {
4138 if (attr->prefix != NULL) {
4139 xmlNsPtr nameSpace = attrib->ns;
4140
4141 if (nameSpace == NULL)
4142 nameSpace = elem->ns;
4143 /*
4144 * qualified names handling is problematic, having a
4145 * different prefix should be possible but DTDs don't
4146 * allow to define the URI instead of the prefix :-(
4147 */
4148 if (nameSpace == NULL) {
4149 if (qualified < 0)
4150 qualified = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004151 } else if (!xmlStrEqual(nameSpace->prefix,
4152 attr->prefix)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004153 if (qualified < 1)
4154 qualified = 1;
4155 } else
4156 goto found;
4157 } else {
4158 /*
4159 * We should allow applications to define namespaces
4160 * for their application even if the DTD doesn't
4161 * carry one, otherwise, basically we would always
4162 * break.
4163 */
4164 goto found;
4165 }
4166 }
4167 attrib = attrib->next;
4168 }
4169 if (qualified == -1) {
4170 if (attr->prefix == NULL) {
4171 VERROR(ctxt->userData,
4172 "Element %s doesn't carry attribute %s\n",
4173 elem->name, attr->name);
4174 ret = 0;
4175 } else {
4176 VERROR(ctxt->userData,
4177 "Element %s doesn't carry attribute %s:%s\n",
4178 elem->name, attr->prefix,attr->name);
4179 ret = 0;
4180 }
4181 } else if (qualified == 0) {
4182 VWARNING(ctxt->userData,
4183 "Element %s required attribute %s:%s has no prefix\n",
4184 elem->name, attr->prefix,attr->name);
4185 } else if (qualified == 1) {
4186 VWARNING(ctxt->userData,
4187 "Element %s required attribute %s:%s has different prefix\n",
4188 elem->name, attr->prefix,attr->name);
4189 }
4190 }
4191found:
4192 attr = attr->nexth;
4193 }
4194 return(ret);
4195}
4196
4197/**
4198 * xmlValidateRoot:
4199 * @ctxt: the validation context
4200 * @doc: a document instance
4201 *
4202 * Try to validate a the root element
4203 * basically it does the following check as described by the
4204 * XML-1.0 recommendation:
4205 * - [ VC: Root Element Type ]
4206 * it doesn't try to recurse or apply other check to the element
4207 *
4208 * returns 1 if valid or 0 otherwise
4209 */
4210
4211int
4212xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4213 xmlNodePtr root;
4214 if (doc == NULL) return(0);
4215
4216 root = xmlDocGetRootElement(doc);
4217 if ((root == NULL) || (root->name == NULL)) {
4218 VERROR(ctxt->userData, "Not valid: no root element\n");
4219 return(0);
4220 }
4221
4222 /*
4223 * When doing post validation against a separate DTD, those may
4224 * no internal subset has been generated
4225 */
4226 if ((doc->intSubset != NULL) &&
4227 (doc->intSubset->name != NULL)) {
4228 /*
4229 * Check first the document root against the NQName
4230 */
4231 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4232 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4233 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004234 snprintf((char *) qname, sizeof(qname), "%s:%s",
4235 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004236 qname[sizeof(qname) - 1] = 0;
4237 if (xmlStrEqual(doc->intSubset->name, qname))
4238 goto name_ok;
4239 }
4240 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4241 (xmlStrEqual(root->name, BAD_CAST "html")))
4242 goto name_ok;
4243 VERROR(ctxt->userData,
4244 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4245 root->name, doc->intSubset->name);
4246 return(0);
4247
4248 }
4249 }
4250name_ok:
4251 return(1);
4252}
4253
4254
4255/**
4256 * xmlValidateElement:
4257 * @ctxt: the validation context
4258 * @doc: a document instance
4259 * @elem: an element instance
4260 *
4261 * Try to validate the subtree under an element
4262 *
4263 * returns 1 if valid or 0 otherwise
4264 */
4265
4266int
4267xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4268 xmlNodePtr child;
4269 xmlAttrPtr attr;
4270 xmlChar *value;
4271 int ret = 1;
4272
4273 if (elem == NULL) return(0);
4274
4275 /*
4276 * XInclude elements were added after parsing in the infoset,
4277 * they don't really mean anything validation wise.
4278 */
4279 if ((elem->type == XML_XINCLUDE_START) ||
4280 (elem->type == XML_XINCLUDE_END))
4281 return(1);
4282
4283 CHECK_DTD;
4284
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004285 /*
4286 * Entities references have to be handled separately
4287 */
4288 if (elem->type == XML_ENTITY_REF_NODE) {
4289 return(1);
4290 }
4291
Owen Taylor3473f882001-02-23 17:55:21 +00004292 ret &= xmlValidateOneElement(ctxt, doc, elem);
4293 attr = elem->properties;
4294 while(attr != NULL) {
4295 value = xmlNodeListGetString(doc, attr->children, 0);
4296 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4297 if (value != NULL)
4298 xmlFree(value);
4299 attr= attr->next;
4300 }
4301 child = elem->children;
4302 while (child != NULL) {
4303 ret &= xmlValidateElement(ctxt, doc, child);
4304 child = child->next;
4305 }
4306
4307 return(ret);
4308}
4309
Daniel Veillard8730c562001-02-26 10:49:57 +00004310/**
4311 * xmlValidateRef:
4312 * @ref: A reference to be validated
4313 * @ctxt: Validation context
4314 * @name: Name of ID we are searching for
4315 *
4316 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004317static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004318xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004319 const xmlChar *name) {
4320 xmlAttrPtr id;
4321 xmlAttrPtr attr;
4322
4323 if (ref == NULL)
4324 return;
4325 attr = ref->attr;
4326 if (attr == NULL)
4327 return;
4328 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4329 id = xmlGetID(ctxt->doc, name);
4330 if (id == NULL) {
4331 VERROR(ctxt->userData,
4332 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4333 attr->name, name);
4334 ctxt->valid = 0;
4335 }
4336 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4337 xmlChar *dup, *str = NULL, *cur, save;
4338
4339 dup = xmlStrdup(name);
4340 if (dup == NULL) {
4341 ctxt->valid = 0;
4342 return;
4343 }
4344 cur = dup;
4345 while (*cur != 0) {
4346 str = cur;
4347 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4348 save = *cur;
4349 *cur = 0;
4350 id = xmlGetID(ctxt->doc, str);
4351 if (id == NULL) {
4352 VERROR(ctxt->userData,
4353 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4354 attr->name, str);
4355 ctxt->valid = 0;
4356 }
4357 if (save == 0)
4358 break;
4359 *cur = save;
4360 while (IS_BLANK(*cur)) cur++;
4361 }
4362 xmlFree(dup);
4363 }
4364}
4365
4366/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004367 * xmlWalkValidateList:
4368 * @data: Contents of current link
4369 * @user: Value supplied by the user
4370 *
4371 * Return 0 to abort the walk or 1 to continue
4372 */
4373static int
4374xmlWalkValidateList(const void *data, const void *user)
4375{
4376 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4377 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4378 return 1;
4379}
4380
4381/**
4382 * xmlValidateCheckRefCallback:
4383 * @ref_list: List of references
4384 * @ctxt: Validation context
4385 * @name: Name of ID we are searching for
4386 *
4387 */
4388static void
4389xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4390 const xmlChar *name) {
4391 xmlValidateMemo memo;
4392
4393 if (ref_list == NULL)
4394 return;
4395 memo.ctxt = ctxt;
4396 memo.name = name;
4397
4398 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4399
4400}
4401
4402/**
Owen Taylor3473f882001-02-23 17:55:21 +00004403 * xmlValidateDocumentFinal:
4404 * @ctxt: the validation context
4405 * @doc: a document instance
4406 *
4407 * Does the final step for the document validation once all the
4408 * incremental validation steps have been completed
4409 *
4410 * basically it does the following checks described by the XML Rec
4411 *
4412 *
4413 * returns 1 if valid or 0 otherwise
4414 */
4415
4416int
4417xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4418 xmlRefTablePtr table;
4419
4420 if (doc == NULL) {
4421 xmlGenericError(xmlGenericErrorContext,
4422 "xmlValidateDocumentFinal: doc == NULL\n");
4423 return(0);
4424 }
4425
4426 /*
4427 * Check all the NOTATION/NOTATIONS attributes
4428 */
4429 /*
4430 * Check all the ENTITY/ENTITIES attributes definition for validity
4431 */
4432 /*
4433 * Check all the IDREF/IDREFS attributes definition for validity
4434 */
4435 table = (xmlRefTablePtr) doc->refs;
4436 ctxt->doc = doc;
4437 ctxt->valid = 1;
4438 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4439 return(ctxt->valid);
4440}
4441
4442/**
4443 * xmlValidateDtd:
4444 * @ctxt: the validation context
4445 * @doc: a document instance
4446 * @dtd: a dtd instance
4447 *
4448 * Try to validate the document against the dtd instance
4449 *
4450 * basically it does check all the definitions in the DtD.
4451 *
4452 * returns 1 if valid or 0 otherwise
4453 */
4454
4455int
4456xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4457 int ret;
4458 xmlDtdPtr oldExt;
4459 xmlNodePtr root;
4460
4461 if (dtd == NULL) return(0);
4462 if (doc == NULL) return(0);
4463 oldExt = doc->extSubset;
4464 doc->extSubset = dtd;
4465 ret = xmlValidateRoot(ctxt, doc);
4466 if (ret == 0) {
4467 doc->extSubset = oldExt;
4468 return(ret);
4469 }
4470 if (doc->ids != NULL) {
4471 xmlFreeIDTable(doc->ids);
4472 doc->ids = NULL;
4473 }
4474 if (doc->refs != NULL) {
4475 xmlFreeRefTable(doc->refs);
4476 doc->refs = NULL;
4477 }
4478 root = xmlDocGetRootElement(doc);
4479 ret = xmlValidateElement(ctxt, doc, root);
4480 ret &= xmlValidateDocumentFinal(ctxt, doc);
4481 doc->extSubset = oldExt;
4482 return(ret);
4483}
4484
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004485static void
Owen Taylor3473f882001-02-23 17:55:21 +00004486xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004487 const xmlChar *name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004488 if (cur == NULL)
4489 return;
4490 switch (cur->atype) {
4491 case XML_ATTRIBUTE_CDATA:
4492 case XML_ATTRIBUTE_ID:
4493 case XML_ATTRIBUTE_IDREF :
4494 case XML_ATTRIBUTE_IDREFS:
4495 case XML_ATTRIBUTE_NMTOKEN:
4496 case XML_ATTRIBUTE_NMTOKENS:
4497 case XML_ATTRIBUTE_ENUMERATION:
4498 break;
4499 case XML_ATTRIBUTE_ENTITY:
4500 case XML_ATTRIBUTE_ENTITIES:
4501 case XML_ATTRIBUTE_NOTATION:
4502 if (cur->defaultValue != NULL) {
4503 ctxt->valid &= xmlValidateAttributeValue2(ctxt, ctxt->doc,
4504 cur->name, cur->atype, cur->defaultValue);
4505 }
4506 if (cur->tree != NULL) {
4507 xmlEnumerationPtr tree = cur->tree;
4508 while (tree != NULL) {
4509 ctxt->valid &= xmlValidateAttributeValue2(ctxt, ctxt->doc,
4510 cur->name, cur->atype, tree->name);
4511 tree = tree->next;
4512 }
4513 }
4514 }
4515}
4516
4517/**
4518 * xmlValidateDtdFinal:
4519 * @ctxt: the validation context
4520 * @doc: a document instance
4521 *
4522 * Does the final step for the dtds validation once all the
4523 * subsets have been parsed
4524 *
4525 * basically it does the following checks described by the XML Rec
4526 * - check that ENTITY and ENTITIES type attributes default or
4527 * possible values matches one of the defined entities.
4528 * - check that NOTATION type attributes default or
4529 * possible values matches one of the defined notations.
4530 *
4531 * returns 1 if valid or 0 otherwise
4532 */
4533
4534int
4535xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4536 int ret = 1;
4537 xmlDtdPtr dtd;
4538 xmlAttributeTablePtr table;
4539
4540 if (doc == NULL) return(0);
4541 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4542 return(0);
4543 ctxt->doc = doc;
4544 ctxt->valid = ret;
4545 dtd = doc->intSubset;
4546 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4547 table = (xmlAttributeTablePtr) dtd->attributes;
4548 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
4549 }
4550 dtd = doc->extSubset;
4551 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4552 table = (xmlAttributeTablePtr) dtd->attributes;
4553 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
4554 }
4555 return(ctxt->valid);
4556}
4557
4558/**
4559 * xmlValidateDocument:
4560 * @ctxt: the validation context
4561 * @doc: a document instance
4562 *
4563 * Try to validate the document instance
4564 *
4565 * basically it does the all the checks described by the XML Rec
4566 * i.e. validates the internal and external subset (if present)
4567 * and validate the document tree.
4568 *
4569 * returns 1 if valid or 0 otherwise
4570 */
4571
4572int
4573xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4574 int ret;
4575 xmlNodePtr root;
4576
4577 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4578 return(0);
4579 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
4580 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
4581 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
4582 doc->intSubset->SystemID);
4583 if (doc->extSubset == NULL) {
4584 if (doc->intSubset->SystemID != NULL) {
4585 VERROR(ctxt->userData,
4586 "Could not load the external subset \"%s\"\n",
4587 doc->intSubset->SystemID);
4588 } else {
4589 VERROR(ctxt->userData,
4590 "Could not load the external subset \"%s\"\n",
4591 doc->intSubset->ExternalID);
4592 }
4593 return(0);
4594 }
4595 }
4596
4597 if (doc->ids != NULL) {
4598 xmlFreeIDTable(doc->ids);
4599 doc->ids = NULL;
4600 }
4601 if (doc->refs != NULL) {
4602 xmlFreeRefTable(doc->refs);
4603 doc->refs = NULL;
4604 }
4605 ret = xmlValidateDtdFinal(ctxt, doc);
4606 if (!xmlValidateRoot(ctxt, doc)) return(0);
4607
4608 root = xmlDocGetRootElement(doc);
4609 ret &= xmlValidateElement(ctxt, doc, root);
4610 ret &= xmlValidateDocumentFinal(ctxt, doc);
4611 return(ret);
4612}
4613
4614
4615/************************************************************************
4616 * *
4617 * Routines for dynamic validation editing *
4618 * *
4619 ************************************************************************/
4620
4621/**
4622 * xmlValidGetPotentialChildren:
4623 * @ctree: an element content tree
4624 * @list: an array to store the list of child names
4625 * @len: a pointer to the number of element in the list
4626 * @max: the size of the array
4627 *
4628 * Build/extend a list of potential children allowed by the content tree
4629 *
4630 * returns the number of element in the list, or -1 in case of error.
4631 */
4632
4633int
4634xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
4635 int *len, int max) {
4636 int i;
4637
4638 if ((ctree == NULL) || (list == NULL) || (len == NULL))
4639 return(-1);
4640 if (*len >= max) return(*len);
4641
4642 switch (ctree->type) {
4643 case XML_ELEMENT_CONTENT_PCDATA:
4644 for (i = 0; i < *len;i++)
4645 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
4646 list[(*len)++] = BAD_CAST "#PCDATA";
4647 break;
4648 case XML_ELEMENT_CONTENT_ELEMENT:
4649 for (i = 0; i < *len;i++)
4650 if (xmlStrEqual(ctree->name, list[i])) return(*len);
4651 list[(*len)++] = ctree->name;
4652 break;
4653 case XML_ELEMENT_CONTENT_SEQ:
4654 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4655 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4656 break;
4657 case XML_ELEMENT_CONTENT_OR:
4658 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4659 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4660 break;
4661 }
4662
4663 return(*len);
4664}
4665
4666/**
4667 * xmlValidGetValidElements:
4668 * @prev: an element to insert after
4669 * @next: an element to insert next
4670 * @list: an array to store the list of child names
4671 * @max: the size of the array
4672 *
4673 * This function returns the list of authorized children to insert
4674 * within an existing tree while respecting the validity constraints
4675 * forced by the Dtd. The insertion point is defined using @prev and
4676 * @next in the following ways:
4677 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
4678 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
4679 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
4680 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
4681 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
4682 *
4683 * pointers to the element names are inserted at the beginning of the array
4684 * and do not need to be freed.
4685 *
4686 * returns the number of element in the list, or -1 in case of error. If
4687 * the function returns the value @max the caller is invited to grow the
4688 * receiving array and retry.
4689 */
4690
4691int
4692xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
4693 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004694 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004695 int nb_valid_elements = 0;
4696 const xmlChar *elements[256];
4697 int nb_elements = 0, i;
4698
4699 xmlNode *ref_node;
4700 xmlNode *parent;
4701 xmlNode *test_node;
4702
4703 xmlNode *prev_next;
4704 xmlNode *next_prev;
4705 xmlNode *parent_childs;
4706 xmlNode *parent_last;
4707
4708 xmlElement *element_desc;
4709
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004710 vctxt.userData = NULL;
4711 vctxt.error = NULL;
4712 vctxt.warning = NULL;
4713
Owen Taylor3473f882001-02-23 17:55:21 +00004714 if (prev == NULL && next == NULL)
4715 return(-1);
4716
4717 if (list == NULL) return(-1);
4718 if (max <= 0) return(-1);
4719
4720 nb_valid_elements = 0;
4721 ref_node = prev ? prev : next;
4722 parent = ref_node->parent;
4723
4724 /*
4725 * Retrieves the parent element declaration
4726 */
4727 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
4728 parent->name);
4729 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
4730 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
4731 parent->name);
4732 if (element_desc == NULL) return(-1);
4733
4734 /*
4735 * Do a backup of the current tree structure
4736 */
4737 prev_next = prev ? prev->next : NULL;
4738 next_prev = next ? next->prev : NULL;
4739 parent_childs = parent->children;
4740 parent_last = parent->last;
4741
4742 /*
4743 * Creates a dummy node and insert it into the tree
4744 */
4745 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
4746 test_node->doc = ref_node->doc;
4747 test_node->parent = parent;
4748 test_node->prev = prev;
4749 test_node->next = next;
4750
4751 if (prev) prev->next = test_node;
4752 else parent->children = test_node;
4753
4754 if (next) next->prev = test_node;
4755 else parent->last = test_node;
4756
4757 /*
4758 * Insert each potential child node and check if the parent is
4759 * still valid
4760 */
4761 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
4762 elements, &nb_elements, 256);
4763
4764 for (i = 0;i < nb_elements;i++) {
4765 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004766 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004767 int j;
4768
4769 for (j = 0; j < nb_valid_elements;j++)
4770 if (xmlStrEqual(elements[i], list[j])) break;
4771 list[nb_valid_elements++] = elements[i];
4772 if (nb_valid_elements >= max) break;
4773 }
4774 }
4775
4776 /*
4777 * Restore the tree structure
4778 */
4779 if (prev) prev->next = prev_next;
4780 if (next) next->prev = next_prev;
4781 parent->children = parent_childs;
4782 parent->last = parent_last;
4783
4784 return(nb_valid_elements);
4785}