blob: ad22408ba63ca545d385f958d4a85b9d047dc0bf [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>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000025#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000026
Daniel Veillarde62d36c2001-05-15 08:53:16 +000027/* #define DEBUG_VALID_ALGO */
28
Owen Taylor3473f882001-02-23 17:55:21 +000029/*
30 * Generic function for accessing stacks in the Validity Context
31 */
32
33#define PUSH_AND_POP(scope, type, name) \
34scope int name##VPush(xmlValidCtxtPtr ctxt, type value) { \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000035 if (ctxt->name##Max <= 0) { \
36 ctxt->name##Max = 4; \
37 ctxt->name##Tab = (type *) xmlMalloc( \
38 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
39 if (ctxt->name##Tab == NULL) { \
40 xmlGenericError(xmlGenericErrorContext, \
41 "malloc failed !\n"); \
Daniel Veillarda9142e72001-06-19 11:07:54 +000042 ctxt->name##Max = 0; \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000043 return(0); \
44 } \
45 } \
Owen Taylor3473f882001-02-23 17:55:21 +000046 if (ctxt->name##Nr >= ctxt->name##Max) { \
47 ctxt->name##Max *= 2; \
48 ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \
49 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
50 if (ctxt->name##Tab == NULL) { \
51 xmlGenericError(xmlGenericErrorContext, \
52 "realloc failed !\n"); \
53 return(0); \
54 } \
55 } \
56 ctxt->name##Tab[ctxt->name##Nr] = value; \
57 ctxt->name = value; \
58 return(ctxt->name##Nr++); \
59} \
60scope type name##VPop(xmlValidCtxtPtr ctxt) { \
61 type ret; \
62 if (ctxt->name##Nr <= 0) return(0); \
63 ctxt->name##Nr--; \
64 if (ctxt->name##Nr > 0) \
65 ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \
66 else \
67 ctxt->name = NULL; \
68 ret = ctxt->name##Tab[ctxt->name##Nr]; \
69 ctxt->name##Tab[ctxt->name##Nr] = 0; \
70 return(ret); \
71} \
72
Daniel Veillarddab4cb32001-04-20 13:03:48 +000073/*
Daniel Veillardb44025c2001-10-11 22:55:55 +000074 * I use a home made algorithm less complex and easier to
Daniel Veillarddab4cb32001-04-20 13:03:48 +000075 * debug/maintin than a generic NFA -> DFA state based algo. The
76 * only restriction is on the deepness of the tree limited by the
77 * size of the occurs bitfield
78 *
79 * this is the content of a saved state for rollbacks
80 */
81
82#define ROLLBACK_OR 0
83#define ROLLBACK_PARENT 1
84
Daniel Veillardb44025c2001-10-11 22:55:55 +000085typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +000086 xmlElementContentPtr cont; /* pointer to the content model subtree */
87 xmlNodePtr node; /* pointer to the current node in the list */
88 long occurs;/* bitfield for multiple occurences */
89 unsigned char depth; /* current depth in the overall tree */
90 unsigned char state; /* ROLLBACK_XXX */
91} _xmlValidState;
92
93#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
94#define CONT ctxt->vstate->cont
95#define NODE ctxt->vstate->node
96#define DEPTH ctxt->vstate->depth
97#define OCCURS ctxt->vstate->occurs
98#define STATE ctxt->vstate->state
99
100#define OCCURENCE (ctxt->vstate->occurs & (1 << DEPTH))
101#define PARENT_OCCURENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
102
103#define SET_OCCURENCE ctxt->vstate->occurs |= (1 << DEPTH)
104#define RESET_OCCURENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
105
106static int
107vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
108 xmlNodePtr node, unsigned char depth, long occurs,
109 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000110 int i = ctxt->vstateNr - 1;
111
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000112 if (ctxt->vstateNr >= ctxt->vstateMax) {
113 ctxt->vstateMax *= 2;
114 ctxt->vstateTab = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
115 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
116 if (ctxt->vstateTab == NULL) {
117 xmlGenericError(xmlGenericErrorContext,
118 "realloc failed !n");
119 return(0);
120 }
Daniel Veillard06803992001-04-22 10:35:56 +0000121 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000122 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000123 /*
124 * Don't push on the stack a state already here
125 */
126 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
127 (ctxt->vstateTab[i].node == node) &&
128 (ctxt->vstateTab[i].depth == depth) &&
129 (ctxt->vstateTab[i].occurs == occurs) &&
130 (ctxt->vstateTab[i].state == state))
131 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000132 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
133 ctxt->vstateTab[ctxt->vstateNr].node = node;
134 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
135 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
136 ctxt->vstateTab[ctxt->vstateNr].state = state;
137 return(ctxt->vstateNr++);
138}
139
140static int
141vstateVPop(xmlValidCtxtPtr ctxt) {
142 if (ctxt->vstateNr <= 1) return(-1);
143 ctxt->vstateNr--;
144 ctxt->vstate = &ctxt->vstateTab[0];
145 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
146 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
147 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
148 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
149 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
150 return(ctxt->vstateNr);
151}
152
Owen Taylor3473f882001-02-23 17:55:21 +0000153PUSH_AND_POP(static, xmlNodePtr, node)
154
Owen Taylor3473f882001-02-23 17:55:21 +0000155#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000156static void
157xmlValidPrintNode(xmlNodePtr cur) {
158 if (cur == NULL) {
159 xmlGenericError(xmlGenericErrorContext, "null");
160 return;
161 }
162 switch (cur->type) {
163 case XML_ELEMENT_NODE:
164 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
165 break;
166 case XML_TEXT_NODE:
167 xmlGenericError(xmlGenericErrorContext, "text ");
168 break;
169 case XML_CDATA_SECTION_NODE:
170 xmlGenericError(xmlGenericErrorContext, "cdata ");
171 break;
172 case XML_ENTITY_REF_NODE:
173 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
174 break;
175 case XML_PI_NODE:
176 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
177 break;
178 case XML_COMMENT_NODE:
179 xmlGenericError(xmlGenericErrorContext, "comment ");
180 break;
181 case XML_ATTRIBUTE_NODE:
182 xmlGenericError(xmlGenericErrorContext, "?attr? ");
183 break;
184 case XML_ENTITY_NODE:
185 xmlGenericError(xmlGenericErrorContext, "?ent? ");
186 break;
187 case XML_DOCUMENT_NODE:
188 xmlGenericError(xmlGenericErrorContext, "?doc? ");
189 break;
190 case XML_DOCUMENT_TYPE_NODE:
191 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
192 break;
193 case XML_DOCUMENT_FRAG_NODE:
194 xmlGenericError(xmlGenericErrorContext, "?frag? ");
195 break;
196 case XML_NOTATION_NODE:
197 xmlGenericError(xmlGenericErrorContext, "?nota? ");
198 break;
199 case XML_HTML_DOCUMENT_NODE:
200 xmlGenericError(xmlGenericErrorContext, "?html? ");
201 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000202#ifdef LIBXML_DOCB_ENABLED
203 case XML_DOCB_DOCUMENT_NODE:
204 xmlGenericError(xmlGenericErrorContext, "?docb? ");
205 break;
206#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000207 case XML_DTD_NODE:
208 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
209 break;
210 case XML_ELEMENT_DECL:
211 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
212 break;
213 case XML_ATTRIBUTE_DECL:
214 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
215 break;
216 case XML_ENTITY_DECL:
217 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
218 break;
219 case XML_NAMESPACE_DECL:
220 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
221 break;
222 case XML_XINCLUDE_START:
223 xmlGenericError(xmlGenericErrorContext, "incstart ");
224 break;
225 case XML_XINCLUDE_END:
226 xmlGenericError(xmlGenericErrorContext, "incend ");
227 break;
228 }
229}
230
231static void
232xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000233 if (cur == NULL)
234 xmlGenericError(xmlGenericErrorContext, "null ");
235 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000236 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000237 cur = cur->next;
238 }
239}
240
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000241static void
242xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Owen Taylor3473f882001-02-23 17:55:21 +0000243 char expr[1000];
244
245 expr[0] = 0;
246 xmlGenericError(xmlGenericErrorContext, "valid: ");
247 xmlValidPrintNodeList(cur);
248 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000249 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000250 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
251}
252
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000253static void
254xmlValidDebugState(xmlValidStatePtr state) {
255 xmlGenericError(xmlGenericErrorContext, "(");
256 if (state->cont == NULL)
257 xmlGenericError(xmlGenericErrorContext, "null,");
258 else
259 switch (state->cont->type) {
260 case XML_ELEMENT_CONTENT_PCDATA:
261 xmlGenericError(xmlGenericErrorContext, "pcdata,");
262 break;
263 case XML_ELEMENT_CONTENT_ELEMENT:
264 xmlGenericError(xmlGenericErrorContext, "%s,",
265 state->cont->name);
266 break;
267 case XML_ELEMENT_CONTENT_SEQ:
268 xmlGenericError(xmlGenericErrorContext, "seq,");
269 break;
270 case XML_ELEMENT_CONTENT_OR:
271 xmlGenericError(xmlGenericErrorContext, "or,");
272 break;
273 }
274 xmlValidPrintNode(state->node);
275 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
276 state->depth, state->occurs, state->state);
277}
278
279static void
280xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
281 int i, j;
282
283 xmlGenericError(xmlGenericErrorContext, "state: ");
284 xmlValidDebugState(ctxt->vstate);
285 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
286 ctxt->vstateNr - 1);
287 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
288 xmlValidDebugState(&ctxt->vstateTab[j]);
289 xmlGenericError(xmlGenericErrorContext, "\n");
290}
291
292/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000293#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000294 *****/
295
296#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000297#define DEBUG_VALID_MSG(m) \
298 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
299
Owen Taylor3473f882001-02-23 17:55:21 +0000300#else
301#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000302#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000303#endif
304
305/* TODO: use hash table for accesses to elem and attribute dedinitions */
306
307#define VERROR \
308 if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error
309
310#define VWARNING \
311 if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning
312
313#define CHECK_DTD \
314 if (doc == NULL) return(0); \
315 else if ((doc->intSubset == NULL) && \
316 (doc->extSubset == NULL)) return(0)
317
318xmlElementPtr xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name);
Daniel Veillarda10efa82001-04-18 13:09:01 +0000319static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
320 int create);
Owen Taylor3473f882001-02-23 17:55:21 +0000321xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
322
323/************************************************************************
324 * *
325 * QName handling helper *
326 * *
327 ************************************************************************/
328
329/**
330 * xmlSplitQName2:
331 * @name: an XML parser context
332 * @prefix: a xmlChar **
333 *
334 * parse an XML qualified name string
335 *
336 * [NS 5] QName ::= (Prefix ':')? LocalPart
337 *
338 * [NS 6] Prefix ::= NCName
339 *
340 * [NS 7] LocalPart ::= NCName
341 *
342 * Returns NULL if not a QName, otherwise the local part, and prefix
343 * is updated to get the Prefix if any.
344 */
345
346xmlChar *
347xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
348 int len = 0;
349 xmlChar *ret = NULL;
350
351 *prefix = NULL;
352
Daniel Veillardf4309d72001-10-02 09:28:58 +0000353#ifndef XML_XML_NAMESPACE
Owen Taylor3473f882001-02-23 17:55:21 +0000354 /* xml: prefix is not really a namespace */
355 if ((name[0] == 'x') && (name[1] == 'm') &&
356 (name[2] == 'l') && (name[3] == ':'))
357 return(NULL);
Daniel Veillardf4309d72001-10-02 09:28:58 +0000358#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000359
360 /* nasty but valid */
361 if (name[0] == ':')
362 return(NULL);
363
364 /*
365 * we are not trying to validate but just to cut, and yes it will
366 * work even if this is as set of UTF-8 encoded chars
367 */
368 while ((name[len] != 0) && (name[len] != ':'))
369 len++;
370
371 if (name[len] == 0)
372 return(NULL);
373
374 *prefix = xmlStrndup(name, len);
375 ret = xmlStrdup(&name[len + 1]);
376
377 return(ret);
378}
379
380/****************************************************************
381 * *
382 * Util functions for data allocation/deallocation *
383 * *
384 ****************************************************************/
385
386/**
387 * xmlNewElementContent:
388 * @name: the subelement name or NULL
389 * @type: the type of element content decl
390 *
391 * Allocate an element content structure.
392 *
393 * Returns NULL if not, othervise the new element content structure
394 */
395xmlElementContentPtr
396xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
397 xmlElementContentPtr ret;
398
399 switch(type) {
400 case XML_ELEMENT_CONTENT_ELEMENT:
401 if (name == NULL) {
402 xmlGenericError(xmlGenericErrorContext,
403 "xmlNewElementContent : name == NULL !\n");
404 }
405 break;
406 case XML_ELEMENT_CONTENT_PCDATA:
407 case XML_ELEMENT_CONTENT_SEQ:
408 case XML_ELEMENT_CONTENT_OR:
409 if (name != NULL) {
410 xmlGenericError(xmlGenericErrorContext,
411 "xmlNewElementContent : name != NULL !\n");
412 }
413 break;
414 default:
415 xmlGenericError(xmlGenericErrorContext,
416 "xmlNewElementContent: unknown type %d\n", type);
417 return(NULL);
418 }
419 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
420 if (ret == NULL) {
421 xmlGenericError(xmlGenericErrorContext,
422 "xmlNewElementContent : out of memory!\n");
423 return(NULL);
424 }
425 ret->type = type;
426 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000427 if (name != NULL) {
428 xmlChar *prefix = NULL;
429 ret->name = xmlSplitQName2(name, &prefix);
430 if (ret->name == NULL)
431 ret->name = xmlStrdup(name);
432 ret->prefix = prefix;
433 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000434 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000435 ret->prefix = NULL;
436 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000437 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000438 return(ret);
439}
440
441/**
442 * xmlCopyElementContent:
443 * @content: An element content pointer.
444 *
445 * Build a copy of an element content description.
446 *
447 * Returns the new xmlElementContentPtr or NULL in case of error.
448 */
449xmlElementContentPtr
450xmlCopyElementContent(xmlElementContentPtr cur) {
451 xmlElementContentPtr ret;
452
453 if (cur == NULL) return(NULL);
454 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
455 if (ret == NULL) {
456 xmlGenericError(xmlGenericErrorContext,
457 "xmlCopyElementContent : out of memory\n");
458 return(NULL);
459 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000460 if (cur->prefix != NULL)
461 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000462 ret->ocur = cur->ocur;
463 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000464 if (ret->c1 != NULL)
465 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000466 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000467 if (ret->c2 != NULL)
468 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000469 return(ret);
470}
471
472/**
473 * xmlFreeElementContent:
474 * @cur: the element content tree to free
475 *
476 * Free an element content structure. This is a recursive call !
477 */
478void
479xmlFreeElementContent(xmlElementContentPtr cur) {
480 if (cur == NULL) return;
481 switch (cur->type) {
482 case XML_ELEMENT_CONTENT_PCDATA:
483 case XML_ELEMENT_CONTENT_ELEMENT:
484 case XML_ELEMENT_CONTENT_SEQ:
485 case XML_ELEMENT_CONTENT_OR:
486 break;
487 default:
488 xmlGenericError(xmlGenericErrorContext,
489 "xmlFreeElementContent : type %d\n", cur->type);
490 return;
491 }
492 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
493 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
494 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000495 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000496 xmlFree(cur);
497}
498
499/**
500 * xmlDumpElementContent:
501 * @buf: An XML buffer
502 * @content: An element table
503 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
504 *
505 * This will dump the content of the element table as an XML DTD definition
506 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000507static void
Owen Taylor3473f882001-02-23 17:55:21 +0000508xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
509 if (content == NULL) return;
510
511 if (glob) xmlBufferWriteChar(buf, "(");
512 switch (content->type) {
513 case XML_ELEMENT_CONTENT_PCDATA:
514 xmlBufferWriteChar(buf, "#PCDATA");
515 break;
516 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000517 if (content->prefix != NULL) {
518 xmlBufferWriteCHAR(buf, content->prefix);
519 xmlBufferWriteChar(buf, ":");
520 }
Owen Taylor3473f882001-02-23 17:55:21 +0000521 xmlBufferWriteCHAR(buf, content->name);
522 break;
523 case XML_ELEMENT_CONTENT_SEQ:
524 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
525 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
526 xmlDumpElementContent(buf, content->c1, 1);
527 else
528 xmlDumpElementContent(buf, content->c1, 0);
529 xmlBufferWriteChar(buf, " , ");
530 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
531 xmlDumpElementContent(buf, content->c2, 1);
532 else
533 xmlDumpElementContent(buf, content->c2, 0);
534 break;
535 case XML_ELEMENT_CONTENT_OR:
536 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
537 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
538 xmlDumpElementContent(buf, content->c1, 1);
539 else
540 xmlDumpElementContent(buf, content->c1, 0);
541 xmlBufferWriteChar(buf, " | ");
542 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
543 xmlDumpElementContent(buf, content->c2, 1);
544 else
545 xmlDumpElementContent(buf, content->c2, 0);
546 break;
547 default:
548 xmlGenericError(xmlGenericErrorContext,
549 "xmlDumpElementContent: unknown type %d\n",
550 content->type);
551 }
552 if (glob)
553 xmlBufferWriteChar(buf, ")");
554 switch (content->ocur) {
555 case XML_ELEMENT_CONTENT_ONCE:
556 break;
557 case XML_ELEMENT_CONTENT_OPT:
558 xmlBufferWriteChar(buf, "?");
559 break;
560 case XML_ELEMENT_CONTENT_MULT:
561 xmlBufferWriteChar(buf, "*");
562 break;
563 case XML_ELEMENT_CONTENT_PLUS:
564 xmlBufferWriteChar(buf, "+");
565 break;
566 }
567}
568
569/**
570 * xmlSprintfElementContent:
571 * @buf: an output buffer
572 * @content: An element table
573 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
574 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000575 * Deprecated, unsafe, use xmlSnprintfElementContent
576 */
577void
578xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
579 xmlElementContentPtr content ATTRIBUTE_UNUSED,
580 int glob ATTRIBUTE_UNUSED) {
581}
582
583/**
584 * xmlSnprintfElementContent:
585 * @buf: an output buffer
586 * @size: the buffer size
587 * @content: An element table
588 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
589 *
Owen Taylor3473f882001-02-23 17:55:21 +0000590 * This will dump the content of the element content definition
591 * Intended just for the debug routine
592 */
593void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000594xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
595 int len;
596
Owen Taylor3473f882001-02-23 17:55:21 +0000597 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000598 len = strlen(buf);
599 if (size - len < 50) {
600 if ((size - len > 4) && (buf[len - 1] != '.'))
601 strcat(buf, " ...");
602 return;
603 }
Owen Taylor3473f882001-02-23 17:55:21 +0000604 if (glob) strcat(buf, "(");
605 switch (content->type) {
606 case XML_ELEMENT_CONTENT_PCDATA:
607 strcat(buf, "#PCDATA");
608 break;
609 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000610 if (content->prefix != NULL) {
611 if (size - len < xmlStrlen(content->prefix + 10)) {
612 strcat(buf, " ...");
613 return;
614 }
615 strcat(buf, (char *) content->prefix);
616 strcat(buf, ":");
617 }
Daniel Veillardd3d06722001-08-15 12:06:36 +0000618 if (size - len < xmlStrlen(content->name + 10)) {
619 strcat(buf, " ...");
620 return;
621 }
Owen Taylor3473f882001-02-23 17:55:21 +0000622 strcat(buf, (char *) content->name);
623 break;
624 case XML_ELEMENT_CONTENT_SEQ:
625 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
626 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000627 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000628 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000629 xmlSnprintfElementContent(buf, size, content->c1, 0);
630 len = strlen(buf);
631 if (size - len < 50) {
632 if ((size - len > 4) && (buf[len - 1] != '.'))
633 strcat(buf, " ...");
634 return;
635 }
Owen Taylor3473f882001-02-23 17:55:21 +0000636 strcat(buf, " , ");
637 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000638 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000639 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000640 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000641 break;
642 case XML_ELEMENT_CONTENT_OR:
643 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
644 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000645 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000646 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000647 xmlSnprintfElementContent(buf, size, content->c1, 0);
648 len = strlen(buf);
649 if (size - len < 50) {
650 if ((size - len > 4) && (buf[len - 1] != '.'))
651 strcat(buf, " ...");
652 return;
653 }
Owen Taylor3473f882001-02-23 17:55:21 +0000654 strcat(buf, " | ");
655 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000656 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000657 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000658 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000659 break;
660 }
661 if (glob)
662 strcat(buf, ")");
663 switch (content->ocur) {
664 case XML_ELEMENT_CONTENT_ONCE:
665 break;
666 case XML_ELEMENT_CONTENT_OPT:
667 strcat(buf, "?");
668 break;
669 case XML_ELEMENT_CONTENT_MULT:
670 strcat(buf, "*");
671 break;
672 case XML_ELEMENT_CONTENT_PLUS:
673 strcat(buf, "+");
674 break;
675 }
676}
677
678/****************************************************************
679 * *
680 * Registration of DTD declarations *
681 * *
682 ****************************************************************/
683
684/**
685 * xmlCreateElementTable:
686 *
687 * create and initialize an empty element hash table.
688 *
689 * Returns the xmlElementTablePtr just created or NULL in case of error.
690 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000691static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000692xmlCreateElementTable(void) {
693 return(xmlHashCreate(0));
694}
695
696/**
697 * xmlFreeElement:
698 * @elem: An element
699 *
700 * Deallocate the memory used by an element definition
701 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000702static void
Owen Taylor3473f882001-02-23 17:55:21 +0000703xmlFreeElement(xmlElementPtr elem) {
704 if (elem == NULL) return;
705 xmlUnlinkNode((xmlNodePtr) elem);
706 xmlFreeElementContent(elem->content);
707 if (elem->name != NULL)
708 xmlFree((xmlChar *) elem->name);
709 if (elem->prefix != NULL)
710 xmlFree((xmlChar *) elem->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000711 xmlFree(elem);
712}
713
714
715/**
716 * xmlAddElementDecl:
717 * @ctxt: the validation context
718 * @dtd: pointer to the DTD
719 * @name: the entity name
720 * @type: the element type
721 * @content: the element content tree or NULL
722 *
723 * Register a new element declaration
724 *
725 * Returns NULL if not, othervise the entity
726 */
727xmlElementPtr
728xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
729 xmlElementTypeVal type,
730 xmlElementContentPtr content) {
731 xmlElementPtr ret;
732 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +0000733 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000734 xmlChar *ns, *uqname;
735
736 if (dtd == NULL) {
737 xmlGenericError(xmlGenericErrorContext,
738 "xmlAddElementDecl: dtd == NULL\n");
739 return(NULL);
740 }
741 if (name == NULL) {
742 xmlGenericError(xmlGenericErrorContext,
743 "xmlAddElementDecl: name == NULL\n");
744 return(NULL);
745 }
746 switch (type) {
747 case XML_ELEMENT_TYPE_EMPTY:
748 if (content != NULL) {
749 xmlGenericError(xmlGenericErrorContext,
750 "xmlAddElementDecl: content != NULL for EMPTY\n");
751 return(NULL);
752 }
753 break;
754 case XML_ELEMENT_TYPE_ANY:
755 if (content != NULL) {
756 xmlGenericError(xmlGenericErrorContext,
757 "xmlAddElementDecl: content != NULL for ANY\n");
758 return(NULL);
759 }
760 break;
761 case XML_ELEMENT_TYPE_MIXED:
762 if (content == NULL) {
763 xmlGenericError(xmlGenericErrorContext,
764 "xmlAddElementDecl: content == NULL for MIXED\n");
765 return(NULL);
766 }
767 break;
768 case XML_ELEMENT_TYPE_ELEMENT:
769 if (content == NULL) {
770 xmlGenericError(xmlGenericErrorContext,
771 "xmlAddElementDecl: content == NULL for ELEMENT\n");
772 return(NULL);
773 }
774 break;
775 default:
776 xmlGenericError(xmlGenericErrorContext,
777 "xmlAddElementDecl: unknown type %d\n", type);
778 return(NULL);
779 }
780
781 /*
782 * check if name is a QName
783 */
784 uqname = xmlSplitQName2(name, &ns);
785 if (uqname != NULL)
786 name = uqname;
787
788 /*
789 * Create the Element table if needed.
790 */
791 table = (xmlElementTablePtr) dtd->elements;
792 if (table == NULL) {
793 table = xmlCreateElementTable();
794 dtd->elements = (void *) table;
795 }
796 if (table == NULL) {
797 xmlGenericError(xmlGenericErrorContext,
798 "xmlAddElementDecl: Table creation failed!\n");
799 return(NULL);
800 }
801
Daniel Veillarda10efa82001-04-18 13:09:01 +0000802 /*
803 * lookup old attributes inserted on an undefined element in the
804 * internal subset.
805 */
806 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
807 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
808 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
809 oldAttributes = ret->attributes;
810 ret->attributes = NULL;
811 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
812 xmlFreeElement(ret);
813 }
Owen Taylor3473f882001-02-23 17:55:21 +0000814 }
Owen Taylor3473f882001-02-23 17:55:21 +0000815
816 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +0000817 * The element may already be present if one of its attribute
818 * was registered first
819 */
820 ret = xmlHashLookup2(table, name, ns);
821 if (ret != NULL) {
822 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
823 /*
824 * The element is already defined in this Dtd.
825 */
826 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
827 if (uqname != NULL)
828 xmlFree(uqname);
829 return(NULL);
830 }
831 } else {
832 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
833 if (ret == NULL) {
834 xmlGenericError(xmlGenericErrorContext,
835 "xmlAddElementDecl: out of memory\n");
836 return(NULL);
837 }
838 memset(ret, 0, sizeof(xmlElement));
839 ret->type = XML_ELEMENT_DECL;
840
841 /*
842 * fill the structure.
843 */
844 ret->name = xmlStrdup(name);
845 ret->prefix = ns;
846
847 /*
848 * Validity Check:
849 * Insertion must not fail
850 */
851 if (xmlHashAddEntry2(table, name, ns, ret)) {
852 /*
853 * The element is already defined in this Dtd.
854 */
855 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
856 xmlFreeElement(ret);
857 if (uqname != NULL)
858 xmlFree(uqname);
859 return(NULL);
860 }
861 }
862
863 /*
864 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +0000865 */
866 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +0000867 ret->content = xmlCopyElementContent(content);
Daniel Veillarda10efa82001-04-18 13:09:01 +0000868 ret->attributes = oldAttributes;
Owen Taylor3473f882001-02-23 17:55:21 +0000869
870 /*
871 * Link it to the Dtd
872 */
873 ret->parent = dtd;
874 ret->doc = dtd->doc;
875 if (dtd->last == NULL) {
876 dtd->children = dtd->last = (xmlNodePtr) ret;
877 } else {
878 dtd->last->next = (xmlNodePtr) ret;
879 ret->prev = dtd->last;
880 dtd->last = (xmlNodePtr) ret;
881 }
882 if (uqname != NULL)
883 xmlFree(uqname);
884 return(ret);
885}
886
887/**
888 * xmlFreeElementTable:
889 * @table: An element table
890 *
891 * Deallocate the memory used by an element hash table.
892 */
893void
894xmlFreeElementTable(xmlElementTablePtr table) {
895 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
896}
897
898/**
899 * xmlCopyElement:
900 * @elem: An element
901 *
902 * Build a copy of an element.
903 *
904 * Returns the new xmlElementPtr or NULL in case of error.
905 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000906static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000907xmlCopyElement(xmlElementPtr elem) {
908 xmlElementPtr cur;
909
910 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
911 if (cur == NULL) {
912 xmlGenericError(xmlGenericErrorContext,
913 "xmlCopyElement: out of memory !\n");
914 return(NULL);
915 }
916 memset(cur, 0, sizeof(xmlElement));
917 cur->type = XML_ELEMENT_DECL;
918 cur->etype = elem->etype;
919 if (elem->name != NULL)
920 cur->name = xmlStrdup(elem->name);
921 else
922 cur->name = NULL;
923 if (elem->prefix != NULL)
924 cur->prefix = xmlStrdup(elem->prefix);
925 else
926 cur->prefix = NULL;
927 cur->content = xmlCopyElementContent(elem->content);
928 /* TODO : rebuild the attribute list on the copy */
929 cur->attributes = NULL;
930 return(cur);
931}
932
933/**
934 * xmlCopyElementTable:
935 * @table: An element table
936 *
937 * Build a copy of an element table.
938 *
939 * Returns the new xmlElementTablePtr or NULL in case of error.
940 */
941xmlElementTablePtr
942xmlCopyElementTable(xmlElementTablePtr table) {
943 return((xmlElementTablePtr) xmlHashCopy(table,
944 (xmlHashCopier) xmlCopyElement));
945}
946
947/**
948 * xmlDumpElementDecl:
949 * @buf: the XML buffer output
950 * @elem: An element table
951 *
952 * This will dump the content of the element declaration as an XML
953 * DTD definition
954 */
955void
956xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
957 switch (elem->etype) {
958 case XML_ELEMENT_TYPE_EMPTY:
959 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000960 if (elem->prefix != NULL) {
961 xmlBufferWriteCHAR(buf, elem->prefix);
962 xmlBufferWriteChar(buf, ":");
963 }
Owen Taylor3473f882001-02-23 17:55:21 +0000964 xmlBufferWriteCHAR(buf, elem->name);
965 xmlBufferWriteChar(buf, " EMPTY>\n");
966 break;
967 case XML_ELEMENT_TYPE_ANY:
968 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000969 if (elem->prefix != NULL) {
970 xmlBufferWriteCHAR(buf, elem->prefix);
971 xmlBufferWriteChar(buf, ":");
972 }
Owen Taylor3473f882001-02-23 17:55:21 +0000973 xmlBufferWriteCHAR(buf, elem->name);
974 xmlBufferWriteChar(buf, " ANY>\n");
975 break;
976 case XML_ELEMENT_TYPE_MIXED:
977 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000978 if (elem->prefix != NULL) {
979 xmlBufferWriteCHAR(buf, elem->prefix);
980 xmlBufferWriteChar(buf, ":");
981 }
Owen Taylor3473f882001-02-23 17:55:21 +0000982 xmlBufferWriteCHAR(buf, elem->name);
983 xmlBufferWriteChar(buf, " ");
984 xmlDumpElementContent(buf, elem->content, 1);
985 xmlBufferWriteChar(buf, ">\n");
986 break;
987 case XML_ELEMENT_TYPE_ELEMENT:
988 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000989 if (elem->prefix != NULL) {
990 xmlBufferWriteCHAR(buf, elem->prefix);
991 xmlBufferWriteChar(buf, ":");
992 }
Owen Taylor3473f882001-02-23 17:55:21 +0000993 xmlBufferWriteCHAR(buf, elem->name);
994 xmlBufferWriteChar(buf, " ");
995 xmlDumpElementContent(buf, elem->content, 1);
996 xmlBufferWriteChar(buf, ">\n");
997 break;
998 default:
999 xmlGenericError(xmlGenericErrorContext,
1000 "xmlDumpElementDecl: internal: unknown type %d\n",
1001 elem->etype);
1002 }
1003}
1004
1005/**
1006 * xmlDumpElementTable:
1007 * @buf: the XML buffer output
1008 * @table: An element table
1009 *
1010 * This will dump the content of the element table as an XML DTD definition
1011 */
1012void
1013xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1014 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1015}
1016
1017/**
1018 * xmlCreateEnumeration:
1019 * @name: the enumeration name or NULL
1020 *
1021 * create and initialize an enumeration attribute node.
1022 *
1023 * Returns the xmlEnumerationPtr just created or NULL in case
1024 * of error.
1025 */
1026xmlEnumerationPtr
1027xmlCreateEnumeration(xmlChar *name) {
1028 xmlEnumerationPtr ret;
1029
1030 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1031 if (ret == NULL) {
1032 xmlGenericError(xmlGenericErrorContext,
1033 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1034 (long)sizeof(xmlEnumeration));
1035 return(NULL);
1036 }
1037 memset(ret, 0, sizeof(xmlEnumeration));
1038
1039 if (name != NULL)
1040 ret->name = xmlStrdup(name);
1041 return(ret);
1042}
1043
1044/**
1045 * xmlFreeEnumeration:
1046 * @cur: the tree to free.
1047 *
1048 * free an enumeration attribute node (recursive).
1049 */
1050void
1051xmlFreeEnumeration(xmlEnumerationPtr cur) {
1052 if (cur == NULL) return;
1053
1054 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1055
1056 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001057 xmlFree(cur);
1058}
1059
1060/**
1061 * xmlCopyEnumeration:
1062 * @cur: the tree to copy.
1063 *
1064 * Copy an enumeration attribute node (recursive).
1065 *
1066 * Returns the xmlEnumerationPtr just created or NULL in case
1067 * of error.
1068 */
1069xmlEnumerationPtr
1070xmlCopyEnumeration(xmlEnumerationPtr cur) {
1071 xmlEnumerationPtr ret;
1072
1073 if (cur == NULL) return(NULL);
1074 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1075
1076 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1077 else ret->next = NULL;
1078
1079 return(ret);
1080}
1081
1082/**
1083 * xmlDumpEnumeration:
1084 * @buf: the XML buffer output
1085 * @enum: An enumeration
1086 *
1087 * This will dump the content of the enumeration
1088 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001089static void
Owen Taylor3473f882001-02-23 17:55:21 +00001090xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1091 if (cur == NULL) return;
1092
1093 xmlBufferWriteCHAR(buf, cur->name);
1094 if (cur->next == NULL)
1095 xmlBufferWriteChar(buf, ")");
1096 else {
1097 xmlBufferWriteChar(buf, " | ");
1098 xmlDumpEnumeration(buf, cur->next);
1099 }
1100}
1101
1102/**
1103 * xmlCreateAttributeTable:
1104 *
1105 * create and initialize an empty attribute hash table.
1106 *
1107 * Returns the xmlAttributeTablePtr just created or NULL in case
1108 * of error.
1109 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001110static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001111xmlCreateAttributeTable(void) {
1112 return(xmlHashCreate(0));
1113}
1114
1115/**
1116 * xmlScanAttributeDeclCallback:
1117 * @attr: the attribute decl
1118 * @list: the list to update
1119 *
1120 * Callback called by xmlScanAttributeDecl when a new attribute
1121 * has to be entered in the list.
1122 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001123static void
Owen Taylor3473f882001-02-23 17:55:21 +00001124xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001125 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001126 attr->nexth = *list;
1127 *list = attr;
1128}
1129
1130/**
1131 * xmlScanAttributeDecl:
1132 * @dtd: pointer to the DTD
1133 * @elem: the element name
1134 *
1135 * When inserting a new element scan the DtD for existing attributes
1136 * for taht element and initialize the Attribute chain
1137 *
1138 * Returns the pointer to the first attribute decl in the chain,
1139 * possibly NULL.
1140 */
1141xmlAttributePtr
1142xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1143 xmlAttributePtr ret = NULL;
1144 xmlAttributeTablePtr table;
1145
1146 if (dtd == NULL) {
1147 xmlGenericError(xmlGenericErrorContext,
1148 "xmlScanAttributeDecl: dtd == NULL\n");
1149 return(NULL);
1150 }
1151 if (elem == NULL) {
1152 xmlGenericError(xmlGenericErrorContext,
1153 "xmlScanAttributeDecl: elem == NULL\n");
1154 return(NULL);
1155 }
1156 table = (xmlAttributeTablePtr) dtd->attributes;
1157 if (table == NULL)
1158 return(NULL);
1159
1160 /* WRONG !!! */
1161 xmlHashScan3(table, NULL, NULL, elem,
1162 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1163 return(ret);
1164}
1165
1166/**
1167 * xmlScanIDAttributeDecl:
1168 * @ctxt: the validation context
1169 * @elem: the element name
1170 *
1171 * Verify that the element don't have too many ID attributes
1172 * declared.
1173 *
1174 * Returns the number of ID attributes found.
1175 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001176static int
Owen Taylor3473f882001-02-23 17:55:21 +00001177xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1178 xmlAttributePtr cur;
1179 int ret = 0;
1180
1181 if (elem == NULL) return(0);
1182 cur = elem->attributes;
1183 while (cur != NULL) {
1184 if (cur->atype == XML_ATTRIBUTE_ID) {
1185 ret ++;
1186 if (ret > 1)
1187 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001188 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001189 elem->name, cur->name);
1190 }
1191 cur = cur->nexth;
1192 }
1193 return(ret);
1194}
1195
1196/**
1197 * xmlFreeAttribute:
1198 * @elem: An attribute
1199 *
1200 * Deallocate the memory used by an attribute definition
1201 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001202static void
Owen Taylor3473f882001-02-23 17:55:21 +00001203xmlFreeAttribute(xmlAttributePtr attr) {
1204 if (attr == NULL) return;
1205 xmlUnlinkNode((xmlNodePtr) attr);
1206 if (attr->tree != NULL)
1207 xmlFreeEnumeration(attr->tree);
1208 if (attr->elem != NULL)
1209 xmlFree((xmlChar *) attr->elem);
1210 if (attr->name != NULL)
1211 xmlFree((xmlChar *) attr->name);
1212 if (attr->defaultValue != NULL)
1213 xmlFree((xmlChar *) attr->defaultValue);
1214 if (attr->prefix != NULL)
1215 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001216 xmlFree(attr);
1217}
1218
1219
1220/**
1221 * xmlAddAttributeDecl:
1222 * @ctxt: the validation context
1223 * @dtd: pointer to the DTD
1224 * @elem: the element name
1225 * @name: the attribute name
1226 * @ns: the attribute namespace prefix
1227 * @type: the attribute type
1228 * @def: the attribute default type
1229 * @defaultValue: the attribute default value
1230 * @tree: if it's an enumeration, the associated list
1231 *
1232 * Register a new attribute declaration
1233 * Note that @tree becomes the ownership of the DTD
1234 *
1235 * Returns NULL if not new, othervise the attribute decl
1236 */
1237xmlAttributePtr
1238xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1239 const xmlChar *name, const xmlChar *ns,
1240 xmlAttributeType type, xmlAttributeDefault def,
1241 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1242 xmlAttributePtr ret;
1243 xmlAttributeTablePtr table;
1244 xmlElementPtr elemDef;
1245
1246 if (dtd == NULL) {
1247 xmlGenericError(xmlGenericErrorContext,
1248 "xmlAddAttributeDecl: dtd == NULL\n");
1249 xmlFreeEnumeration(tree);
1250 return(NULL);
1251 }
1252 if (name == NULL) {
1253 xmlGenericError(xmlGenericErrorContext,
1254 "xmlAddAttributeDecl: name == NULL\n");
1255 xmlFreeEnumeration(tree);
1256 return(NULL);
1257 }
1258 if (elem == NULL) {
1259 xmlGenericError(xmlGenericErrorContext,
1260 "xmlAddAttributeDecl: elem == NULL\n");
1261 xmlFreeEnumeration(tree);
1262 return(NULL);
1263 }
1264 /*
1265 * Check the type and possibly the default value.
1266 */
1267 switch (type) {
1268 case XML_ATTRIBUTE_CDATA:
1269 break;
1270 case XML_ATTRIBUTE_ID:
1271 break;
1272 case XML_ATTRIBUTE_IDREF:
1273 break;
1274 case XML_ATTRIBUTE_IDREFS:
1275 break;
1276 case XML_ATTRIBUTE_ENTITY:
1277 break;
1278 case XML_ATTRIBUTE_ENTITIES:
1279 break;
1280 case XML_ATTRIBUTE_NMTOKEN:
1281 break;
1282 case XML_ATTRIBUTE_NMTOKENS:
1283 break;
1284 case XML_ATTRIBUTE_ENUMERATION:
1285 break;
1286 case XML_ATTRIBUTE_NOTATION:
1287 break;
1288 default:
1289 xmlGenericError(xmlGenericErrorContext,
1290 "xmlAddAttributeDecl: unknown type %d\n", type);
1291 xmlFreeEnumeration(tree);
1292 return(NULL);
1293 }
1294 if ((defaultValue != NULL) &&
1295 (!xmlValidateAttributeValue(type, defaultValue))) {
1296 VERROR(ctxt->userData, "Attribute %s on %s: invalid default value\n",
1297 elem, name, defaultValue);
1298 defaultValue = NULL;
1299 }
1300
1301 /*
1302 * Create the Attribute table if needed.
1303 */
1304 table = (xmlAttributeTablePtr) dtd->attributes;
1305 if (table == NULL) {
1306 table = xmlCreateAttributeTable();
1307 dtd->attributes = (void *) table;
1308 }
1309 if (table == NULL) {
1310 xmlGenericError(xmlGenericErrorContext,
1311 "xmlAddAttributeDecl: Table creation failed!\n");
1312 return(NULL);
1313 }
1314
1315
1316 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1317 if (ret == NULL) {
1318 xmlGenericError(xmlGenericErrorContext,
1319 "xmlAddAttributeDecl: out of memory\n");
1320 return(NULL);
1321 }
1322 memset(ret, 0, sizeof(xmlAttribute));
1323 ret->type = XML_ATTRIBUTE_DECL;
1324
1325 /*
1326 * fill the structure.
1327 */
1328 ret->atype = type;
1329 ret->name = xmlStrdup(name);
1330 ret->prefix = xmlStrdup(ns);
1331 ret->elem = xmlStrdup(elem);
1332 ret->def = def;
1333 ret->tree = tree;
1334 if (defaultValue != NULL)
1335 ret->defaultValue = xmlStrdup(defaultValue);
1336
1337 /*
1338 * Validity Check:
1339 * Search the DTD for previous declarations of the ATTLIST
1340 */
1341 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1342 /*
1343 * The attribute is already defined in this Dtd.
1344 */
1345 VWARNING(ctxt->userData,
1346 "Attribute %s on %s: already defined\n",
1347 name, elem);
1348 xmlFreeAttribute(ret);
1349 return(NULL);
1350 }
1351
1352 /*
1353 * Validity Check:
1354 * Multiple ID per element
1355 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001356 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001357 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001358
Owen Taylor3473f882001-02-23 17:55:21 +00001359 if ((type == XML_ATTRIBUTE_ID) &&
1360 (xmlScanIDAttributeDecl(NULL, elemDef) != 0))
1361 VERROR(ctxt->userData,
1362 "Element %s has too may ID attributes defined : %s\n",
1363 elem, name);
Daniel Veillard48da9102001-08-07 01:10:10 +00001364 /*
1365 * Insert namespace default def first they need to be
1366 * processed firt.
1367 */
1368 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1369 ((ret->prefix != NULL &&
1370 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1371 ret->nexth = elemDef->attributes;
1372 elemDef->attributes = ret;
1373 } else {
1374 xmlAttributePtr tmp = elemDef->attributes;
1375
1376 while ((tmp != NULL) &&
1377 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1378 ((ret->prefix != NULL &&
1379 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1380 if (tmp->nexth == NULL)
1381 break;
1382 tmp = tmp->nexth;
1383 }
1384 if (tmp != NULL) {
1385 ret->nexth = tmp->nexth;
1386 tmp->nexth = ret;
1387 } else {
1388 ret->nexth = elemDef->attributes;
1389 elemDef->attributes = ret;
1390 }
1391 }
Owen Taylor3473f882001-02-23 17:55:21 +00001392 }
1393
1394 /*
1395 * Link it to the Dtd
1396 */
1397 ret->parent = dtd;
1398 ret->doc = dtd->doc;
1399 if (dtd->last == NULL) {
1400 dtd->children = dtd->last = (xmlNodePtr) ret;
1401 } else {
1402 dtd->last->next = (xmlNodePtr) ret;
1403 ret->prev = dtd->last;
1404 dtd->last = (xmlNodePtr) ret;
1405 }
1406 return(ret);
1407}
1408
1409/**
1410 * xmlFreeAttributeTable:
1411 * @table: An attribute table
1412 *
1413 * Deallocate the memory used by an entities hash table.
1414 */
1415void
1416xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1417 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1418}
1419
1420/**
1421 * xmlCopyAttribute:
1422 * @attr: An attribute
1423 *
1424 * Build a copy of an attribute.
1425 *
1426 * Returns the new xmlAttributePtr or NULL in case of error.
1427 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001428static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001429xmlCopyAttribute(xmlAttributePtr attr) {
1430 xmlAttributePtr cur;
1431
1432 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1433 if (cur == NULL) {
1434 xmlGenericError(xmlGenericErrorContext,
1435 "xmlCopyAttribute: out of memory !\n");
1436 return(NULL);
1437 }
1438 memset(cur, 0, sizeof(xmlAttribute));
1439 cur->atype = attr->atype;
1440 cur->def = attr->def;
1441 cur->tree = xmlCopyEnumeration(attr->tree);
1442 if (attr->elem != NULL)
1443 cur->elem = xmlStrdup(attr->elem);
1444 if (attr->name != NULL)
1445 cur->name = xmlStrdup(attr->name);
1446 if (attr->defaultValue != NULL)
1447 cur->defaultValue = xmlStrdup(attr->defaultValue);
1448 return(cur);
1449}
1450
1451/**
1452 * xmlCopyAttributeTable:
1453 * @table: An attribute table
1454 *
1455 * Build a copy of an attribute table.
1456 *
1457 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1458 */
1459xmlAttributeTablePtr
1460xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1461 return((xmlAttributeTablePtr) xmlHashCopy(table,
1462 (xmlHashCopier) xmlCopyAttribute));
1463}
1464
1465/**
1466 * xmlDumpAttributeDecl:
1467 * @buf: the XML buffer output
1468 * @attr: An attribute declaration
1469 *
1470 * This will dump the content of the attribute declaration as an XML
1471 * DTD definition
1472 */
1473void
1474xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1475 xmlBufferWriteChar(buf, "<!ATTLIST ");
1476 xmlBufferWriteCHAR(buf, attr->elem);
1477 xmlBufferWriteChar(buf, " ");
1478 if (attr->prefix != NULL) {
1479 xmlBufferWriteCHAR(buf, attr->prefix);
1480 xmlBufferWriteChar(buf, ":");
1481 }
1482 xmlBufferWriteCHAR(buf, attr->name);
1483 switch (attr->atype) {
1484 case XML_ATTRIBUTE_CDATA:
1485 xmlBufferWriteChar(buf, " CDATA");
1486 break;
1487 case XML_ATTRIBUTE_ID:
1488 xmlBufferWriteChar(buf, " ID");
1489 break;
1490 case XML_ATTRIBUTE_IDREF:
1491 xmlBufferWriteChar(buf, " IDREF");
1492 break;
1493 case XML_ATTRIBUTE_IDREFS:
1494 xmlBufferWriteChar(buf, " IDREFS");
1495 break;
1496 case XML_ATTRIBUTE_ENTITY:
1497 xmlBufferWriteChar(buf, " ENTITY");
1498 break;
1499 case XML_ATTRIBUTE_ENTITIES:
1500 xmlBufferWriteChar(buf, " ENTITIES");
1501 break;
1502 case XML_ATTRIBUTE_NMTOKEN:
1503 xmlBufferWriteChar(buf, " NMTOKEN");
1504 break;
1505 case XML_ATTRIBUTE_NMTOKENS:
1506 xmlBufferWriteChar(buf, " NMTOKENS");
1507 break;
1508 case XML_ATTRIBUTE_ENUMERATION:
1509 xmlBufferWriteChar(buf, " (");
1510 xmlDumpEnumeration(buf, attr->tree);
1511 break;
1512 case XML_ATTRIBUTE_NOTATION:
1513 xmlBufferWriteChar(buf, " NOTATION (");
1514 xmlDumpEnumeration(buf, attr->tree);
1515 break;
1516 default:
1517 xmlGenericError(xmlGenericErrorContext,
1518 "xmlDumpAttributeTable: internal: unknown type %d\n",
1519 attr->atype);
1520 }
1521 switch (attr->def) {
1522 case XML_ATTRIBUTE_NONE:
1523 break;
1524 case XML_ATTRIBUTE_REQUIRED:
1525 xmlBufferWriteChar(buf, " #REQUIRED");
1526 break;
1527 case XML_ATTRIBUTE_IMPLIED:
1528 xmlBufferWriteChar(buf, " #IMPLIED");
1529 break;
1530 case XML_ATTRIBUTE_FIXED:
1531 xmlBufferWriteChar(buf, " #FIXED");
1532 break;
1533 default:
1534 xmlGenericError(xmlGenericErrorContext,
1535 "xmlDumpAttributeTable: internal: unknown default %d\n",
1536 attr->def);
1537 }
1538 if (attr->defaultValue != NULL) {
1539 xmlBufferWriteChar(buf, " ");
1540 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1541 }
1542 xmlBufferWriteChar(buf, ">\n");
1543}
1544
1545/**
1546 * xmlDumpAttributeTable:
1547 * @buf: the XML buffer output
1548 * @table: An attribute table
1549 *
1550 * This will dump the content of the attribute table as an XML DTD definition
1551 */
1552void
1553xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1554 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1555}
1556
1557/************************************************************************
1558 * *
1559 * NOTATIONs *
1560 * *
1561 ************************************************************************/
1562/**
1563 * xmlCreateNotationTable:
1564 *
1565 * create and initialize an empty notation hash table.
1566 *
1567 * Returns the xmlNotationTablePtr just created or NULL in case
1568 * of error.
1569 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001570static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001571xmlCreateNotationTable(void) {
1572 return(xmlHashCreate(0));
1573}
1574
1575/**
1576 * xmlFreeNotation:
1577 * @not: A notation
1578 *
1579 * Deallocate the memory used by an notation definition
1580 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001581static void
Owen Taylor3473f882001-02-23 17:55:21 +00001582xmlFreeNotation(xmlNotationPtr nota) {
1583 if (nota == NULL) return;
1584 if (nota->name != NULL)
1585 xmlFree((xmlChar *) nota->name);
1586 if (nota->PublicID != NULL)
1587 xmlFree((xmlChar *) nota->PublicID);
1588 if (nota->SystemID != NULL)
1589 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001590 xmlFree(nota);
1591}
1592
1593
1594/**
1595 * xmlAddNotationDecl:
1596 * @dtd: pointer to the DTD
1597 * @ctxt: the validation context
1598 * @name: the entity name
1599 * @PublicID: the public identifier or NULL
1600 * @SystemID: the system identifier or NULL
1601 *
1602 * Register a new notation declaration
1603 *
1604 * Returns NULL if not, othervise the entity
1605 */
1606xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001607xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001608 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001609 const xmlChar *PublicID, const xmlChar *SystemID) {
1610 xmlNotationPtr ret;
1611 xmlNotationTablePtr table;
1612
1613 if (dtd == NULL) {
1614 xmlGenericError(xmlGenericErrorContext,
1615 "xmlAddNotationDecl: dtd == NULL\n");
1616 return(NULL);
1617 }
1618 if (name == NULL) {
1619 xmlGenericError(xmlGenericErrorContext,
1620 "xmlAddNotationDecl: name == NULL\n");
1621 return(NULL);
1622 }
1623 if ((PublicID == NULL) && (SystemID == NULL)) {
1624 xmlGenericError(xmlGenericErrorContext,
1625 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
1626 }
1627
1628 /*
1629 * Create the Notation table if needed.
1630 */
1631 table = (xmlNotationTablePtr) dtd->notations;
1632 if (table == NULL)
1633 dtd->notations = table = xmlCreateNotationTable();
1634 if (table == NULL) {
1635 xmlGenericError(xmlGenericErrorContext,
1636 "xmlAddNotationDecl: Table creation failed!\n");
1637 return(NULL);
1638 }
1639
1640 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1641 if (ret == NULL) {
1642 xmlGenericError(xmlGenericErrorContext,
1643 "xmlAddNotationDecl: out of memory\n");
1644 return(NULL);
1645 }
1646 memset(ret, 0, sizeof(xmlNotation));
1647
1648 /*
1649 * fill the structure.
1650 */
1651 ret->name = xmlStrdup(name);
1652 if (SystemID != NULL)
1653 ret->SystemID = xmlStrdup(SystemID);
1654 if (PublicID != NULL)
1655 ret->PublicID = xmlStrdup(PublicID);
1656
1657 /*
1658 * Validity Check:
1659 * Check the DTD for previous declarations of the ATTLIST
1660 */
1661 if (xmlHashAddEntry(table, name, ret)) {
1662 xmlGenericError(xmlGenericErrorContext,
1663 "xmlAddNotationDecl: %s already defined\n", name);
1664 xmlFreeNotation(ret);
1665 return(NULL);
1666 }
1667 return(ret);
1668}
1669
1670/**
1671 * xmlFreeNotationTable:
1672 * @table: An notation table
1673 *
1674 * Deallocate the memory used by an entities hash table.
1675 */
1676void
1677xmlFreeNotationTable(xmlNotationTablePtr table) {
1678 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
1679}
1680
1681/**
1682 * xmlCopyNotation:
1683 * @nota: A notation
1684 *
1685 * Build a copy of a notation.
1686 *
1687 * Returns the new xmlNotationPtr or NULL in case of error.
1688 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001689static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001690xmlCopyNotation(xmlNotationPtr nota) {
1691 xmlNotationPtr cur;
1692
1693 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1694 if (cur == NULL) {
1695 xmlGenericError(xmlGenericErrorContext,
1696 "xmlCopyNotation: out of memory !\n");
1697 return(NULL);
1698 }
1699 if (nota->name != NULL)
1700 cur->name = xmlStrdup(nota->name);
1701 else
1702 cur->name = NULL;
1703 if (nota->PublicID != NULL)
1704 cur->PublicID = xmlStrdup(nota->PublicID);
1705 else
1706 cur->PublicID = NULL;
1707 if (nota->SystemID != NULL)
1708 cur->SystemID = xmlStrdup(nota->SystemID);
1709 else
1710 cur->SystemID = NULL;
1711 return(cur);
1712}
1713
1714/**
1715 * xmlCopyNotationTable:
1716 * @table: A notation table
1717 *
1718 * Build a copy of a notation table.
1719 *
1720 * Returns the new xmlNotationTablePtr or NULL in case of error.
1721 */
1722xmlNotationTablePtr
1723xmlCopyNotationTable(xmlNotationTablePtr table) {
1724 return((xmlNotationTablePtr) xmlHashCopy(table,
1725 (xmlHashCopier) xmlCopyNotation));
1726}
1727
1728/**
1729 * xmlDumpNotationDecl:
1730 * @buf: the XML buffer output
1731 * @nota: A notation declaration
1732 *
1733 * This will dump the content the notation declaration as an XML DTD definition
1734 */
1735void
1736xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
1737 xmlBufferWriteChar(buf, "<!NOTATION ");
1738 xmlBufferWriteCHAR(buf, nota->name);
1739 if (nota->PublicID != NULL) {
1740 xmlBufferWriteChar(buf, " PUBLIC ");
1741 xmlBufferWriteQuotedString(buf, nota->PublicID);
1742 if (nota->SystemID != NULL) {
1743 xmlBufferWriteChar(buf, " ");
1744 xmlBufferWriteCHAR(buf, nota->SystemID);
1745 }
1746 } else {
1747 xmlBufferWriteChar(buf, " SYSTEM ");
1748 xmlBufferWriteCHAR(buf, nota->SystemID);
1749 }
1750 xmlBufferWriteChar(buf, " >\n");
1751}
1752
1753/**
1754 * xmlDumpNotationTable:
1755 * @buf: the XML buffer output
1756 * @table: A notation table
1757 *
1758 * This will dump the content of the notation table as an XML DTD definition
1759 */
1760void
1761xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
1762 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
1763}
1764
1765/************************************************************************
1766 * *
1767 * IDs *
1768 * *
1769 ************************************************************************/
1770/**
1771 * xmlCreateIDTable:
1772 *
1773 * create and initialize an empty id hash table.
1774 *
1775 * Returns the xmlIDTablePtr just created or NULL in case
1776 * of error.
1777 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001778static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001779xmlCreateIDTable(void) {
1780 return(xmlHashCreate(0));
1781}
1782
1783/**
1784 * xmlFreeID:
1785 * @not: A id
1786 *
1787 * Deallocate the memory used by an id definition
1788 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001789static void
Owen Taylor3473f882001-02-23 17:55:21 +00001790xmlFreeID(xmlIDPtr id) {
1791 if (id == NULL) return;
1792 if (id->value != NULL)
1793 xmlFree((xmlChar *) id->value);
Owen Taylor3473f882001-02-23 17:55:21 +00001794 xmlFree(id);
1795}
1796
1797/**
1798 * xmlAddID:
1799 * @ctxt: the validation context
1800 * @doc: pointer to the document
1801 * @value: the value name
1802 * @attr: the attribute holding the ID
1803 *
1804 * Register a new id declaration
1805 *
1806 * Returns NULL if not, othervise the new xmlIDPtr
1807 */
1808xmlIDPtr
1809xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
1810 xmlAttrPtr attr) {
1811 xmlIDPtr ret;
1812 xmlIDTablePtr table;
1813
1814 if (doc == NULL) {
1815 xmlGenericError(xmlGenericErrorContext,
1816 "xmlAddIDDecl: doc == NULL\n");
1817 return(NULL);
1818 }
1819 if (value == NULL) {
1820 xmlGenericError(xmlGenericErrorContext,
1821 "xmlAddIDDecl: value == NULL\n");
1822 return(NULL);
1823 }
1824 if (attr == NULL) {
1825 xmlGenericError(xmlGenericErrorContext,
1826 "xmlAddIDDecl: attr == NULL\n");
1827 return(NULL);
1828 }
1829
1830 /*
1831 * Create the ID table if needed.
1832 */
1833 table = (xmlIDTablePtr) doc->ids;
1834 if (table == NULL)
1835 doc->ids = table = xmlCreateIDTable();
1836 if (table == NULL) {
1837 xmlGenericError(xmlGenericErrorContext,
1838 "xmlAddID: Table creation failed!\n");
1839 return(NULL);
1840 }
1841
1842 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
1843 if (ret == NULL) {
1844 xmlGenericError(xmlGenericErrorContext,
1845 "xmlAddID: out of memory\n");
1846 return(NULL);
1847 }
1848
1849 /*
1850 * fill the structure.
1851 */
1852 ret->value = xmlStrdup(value);
1853 ret->attr = attr;
1854
1855 if (xmlHashAddEntry(table, value, ret) < 0) {
1856 /*
1857 * The id is already defined in this Dtd.
1858 */
1859 VERROR(ctxt->userData, "ID %s already defined\n", value);
1860 xmlFreeID(ret);
1861 return(NULL);
1862 }
1863 return(ret);
1864}
1865
1866/**
1867 * xmlFreeIDTable:
1868 * @table: An id table
1869 *
1870 * Deallocate the memory used by an ID hash table.
1871 */
1872void
1873xmlFreeIDTable(xmlIDTablePtr table) {
1874 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
1875}
1876
1877/**
1878 * xmlIsID:
1879 * @doc: the document
1880 * @elem: the element carrying the attribute
1881 * @attr: the attribute
1882 *
1883 * Determine whether an attribute is of type ID. In case we have Dtd(s)
1884 * then this is simple, otherwise we use an heuristic: name ID (upper
1885 * or lowercase).
1886 *
1887 * Returns 0 or 1 depending on the lookup result
1888 */
1889int
1890xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
1891 if (doc == NULL) return(0);
1892 if (attr == NULL) return(0);
1893 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
1894 return(0);
1895 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1896 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
1897 (xmlStrEqual(BAD_CAST "name", attr->name)))
1898 return(1);
1899 return(0);
1900 } else {
1901 xmlAttributePtr attrDecl;
1902
1903 if (elem == NULL) return(0);
1904 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
1905 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1906 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
1907 attr->name);
1908
1909 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
1910 return(1);
1911 }
1912 return(0);
1913}
1914
1915/**
1916 * xmlRemoveID
1917 * @doc: the document
1918 * @attr: the attribute
1919 *
1920 * Remove the given attribute from the ID table maintained internally.
1921 *
1922 * Returns -1 if the lookup failed and 0 otherwise
1923 */
1924int
1925xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
1926 xmlAttrPtr cur;
1927 xmlIDTablePtr table;
1928 xmlChar *ID;
1929
1930 if (doc == NULL) return(-1);
1931 if (attr == NULL) return(-1);
1932 table = (xmlIDTablePtr) doc->ids;
1933 if (table == NULL)
1934 return(-1);
1935
1936 if (attr == NULL)
1937 return(-1);
1938 ID = xmlNodeListGetString(doc, attr->children, 1);
1939 if (ID == NULL)
1940 return(-1);
1941 cur = xmlHashLookup(table, ID);
1942 if (cur != attr) {
1943 xmlFree(ID);
1944 return(-1);
1945 }
1946 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
1947 xmlFree(ID);
1948 return(0);
1949}
1950
1951/**
1952 * xmlGetID:
1953 * @doc: pointer to the document
1954 * @ID: the ID value
1955 *
1956 * Search the attribute declaring the given ID
1957 *
1958 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
1959 */
1960xmlAttrPtr
1961xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
1962 xmlIDTablePtr table;
1963 xmlIDPtr id;
1964
1965 if (doc == NULL) {
1966 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
1967 return(NULL);
1968 }
1969
1970 if (ID == NULL) {
1971 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
1972 return(NULL);
1973 }
1974
1975 table = (xmlIDTablePtr) doc->ids;
1976 if (table == NULL)
1977 return(NULL);
1978
1979 id = xmlHashLookup(table, ID);
1980 if (id == NULL)
1981 return(NULL);
1982 return(id->attr);
1983}
1984
1985/************************************************************************
1986 * *
1987 * Refs *
1988 * *
1989 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00001990typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00001991{
1992 xmlListPtr l;
1993 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00001994} xmlRemoveMemo;
1995
1996typedef xmlRemoveMemo *xmlRemoveMemoPtr;
1997
1998typedef struct xmlValidateMemo_t
1999{
2000 xmlValidCtxtPtr ctxt;
2001 const xmlChar *name;
2002} xmlValidateMemo;
2003
2004typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002005
2006/**
2007 * xmlCreateRefTable:
2008 *
2009 * create and initialize an empty ref hash table.
2010 *
2011 * Returns the xmlRefTablePtr just created or NULL in case
2012 * of error.
2013 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002014static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002015xmlCreateRefTable(void) {
2016 return(xmlHashCreate(0));
2017}
2018
2019/**
2020 * xmlFreeRef:
2021 * @lk: A list link
2022 *
2023 * Deallocate the memory used by a ref definition
2024 */
2025static void
2026xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002027 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2028 if (ref == NULL) return;
2029 if (ref->value != NULL)
2030 xmlFree((xmlChar *)ref->value);
2031 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002032}
2033
2034/**
2035 * xmlFreeRefList:
2036 * @list_ref: A list of references.
2037 *
2038 * Deallocate the memory used by a list of references
2039 */
2040static void
2041xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002042 if (list_ref == NULL) return;
2043 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002044}
2045
2046/**
2047 * xmlWalkRemoveRef:
2048 * @data: Contents of current link
2049 * @user: Value supplied by the user
2050 *
2051 * Return 0 to abort the walk or 1 to continue
2052 */
2053static int
2054xmlWalkRemoveRef(const void *data, const void *user)
2055{
Daniel Veillard37721922001-05-04 15:21:12 +00002056 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2057 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2058 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002059
Daniel Veillard37721922001-05-04 15:21:12 +00002060 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2061 xmlListRemoveFirst(ref_list, (void *)data);
2062 return 0;
2063 }
2064 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002065}
2066
2067/**
2068 * xmlAddRef:
2069 * @ctxt: the validation context
2070 * @doc: pointer to the document
2071 * @value: the value name
2072 * @attr: the attribute holding the Ref
2073 *
2074 * Register a new ref declaration
2075 *
2076 * Returns NULL if not, othervise the new xmlRefPtr
2077 */
2078xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002079xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002080 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002081 xmlRefPtr ret;
2082 xmlRefTablePtr table;
2083 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002084
Daniel Veillard37721922001-05-04 15:21:12 +00002085 if (doc == NULL) {
2086 xmlGenericError(xmlGenericErrorContext,
2087 "xmlAddRefDecl: doc == NULL\n");
2088 return(NULL);
2089 }
2090 if (value == NULL) {
2091 xmlGenericError(xmlGenericErrorContext,
2092 "xmlAddRefDecl: value == NULL\n");
2093 return(NULL);
2094 }
2095 if (attr == NULL) {
2096 xmlGenericError(xmlGenericErrorContext,
2097 "xmlAddRefDecl: attr == NULL\n");
2098 return(NULL);
2099 }
Owen Taylor3473f882001-02-23 17:55:21 +00002100
Daniel Veillard37721922001-05-04 15:21:12 +00002101 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002102 * Create the Ref table if needed.
2103 */
Daniel Veillard37721922001-05-04 15:21:12 +00002104 table = (xmlRefTablePtr) doc->refs;
2105 if (table == NULL)
2106 doc->refs = table = xmlCreateRefTable();
2107 if (table == NULL) {
2108 xmlGenericError(xmlGenericErrorContext,
2109 "xmlAddRef: Table creation failed!\n");
2110 return(NULL);
2111 }
Owen Taylor3473f882001-02-23 17:55:21 +00002112
Daniel Veillard37721922001-05-04 15:21:12 +00002113 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2114 if (ret == NULL) {
2115 xmlGenericError(xmlGenericErrorContext,
2116 "xmlAddRef: out of memory\n");
2117 return(NULL);
2118 }
Owen Taylor3473f882001-02-23 17:55:21 +00002119
Daniel Veillard37721922001-05-04 15:21:12 +00002120 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002121 * fill the structure.
2122 */
Daniel Veillard37721922001-05-04 15:21:12 +00002123 ret->value = xmlStrdup(value);
2124 ret->attr = attr;
Owen Taylor3473f882001-02-23 17:55:21 +00002125
Daniel Veillard37721922001-05-04 15:21:12 +00002126 /* To add a reference :-
2127 * References are maintained as a list of references,
2128 * Lookup the entry, if no entry create new nodelist
2129 * Add the owning node to the NodeList
2130 * Return the ref
2131 */
Owen Taylor3473f882001-02-23 17:55:21 +00002132
Daniel Veillard37721922001-05-04 15:21:12 +00002133 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2134 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2135 xmlGenericError(xmlGenericErrorContext,
2136 "xmlAddRef: Reference list creation failed!\n");
2137 return(NULL);
2138 }
2139 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2140 xmlListDelete(ref_list);
2141 xmlGenericError(xmlGenericErrorContext,
2142 "xmlAddRef: Reference list insertion failed!\n");
2143 return(NULL);
2144 }
2145 }
2146 xmlListInsert(ref_list, ret);
2147 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002148}
2149
2150/**
2151 * xmlFreeRefTable:
2152 * @table: An ref table
2153 *
2154 * Deallocate the memory used by an Ref hash table.
2155 */
2156void
2157xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002158 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002159}
2160
2161/**
2162 * xmlIsRef:
2163 * @doc: the document
2164 * @elem: the element carrying the attribute
2165 * @attr: the attribute
2166 *
2167 * Determine whether an attribute is of type Ref. In case we have Dtd(s)
2168 * then this is simple, otherwise we use an heuristic: name Ref (upper
2169 * or lowercase).
2170 *
2171 * Returns 0 or 1 depending on the lookup result
2172 */
2173int
2174xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002175 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2176 return(0);
2177 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2178 /* TODO @@@ */
2179 return(0);
2180 } else {
2181 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002182
Daniel Veillard37721922001-05-04 15:21:12 +00002183 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2184 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2185 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2186 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002187
Daniel Veillard37721922001-05-04 15:21:12 +00002188 if ((attrDecl != NULL) &&
2189 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2190 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2191 return(1);
2192 }
2193 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002194}
2195
2196/**
2197 * xmlRemoveRef
2198 * @doc: the document
2199 * @attr: the attribute
2200 *
2201 * Remove the given attribute from the Ref table maintained internally.
2202 *
2203 * Returns -1 if the lookup failed and 0 otherwise
2204 */
2205int
2206xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002207 xmlListPtr ref_list;
2208 xmlRefTablePtr table;
2209 xmlChar *ID;
2210 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002211
Daniel Veillard37721922001-05-04 15:21:12 +00002212 if (doc == NULL) return(-1);
2213 if (attr == NULL) return(-1);
2214 table = (xmlRefTablePtr) doc->refs;
2215 if (table == NULL)
2216 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002217
Daniel Veillard37721922001-05-04 15:21:12 +00002218 if (attr == NULL)
2219 return(-1);
2220 ID = xmlNodeListGetString(doc, attr->children, 1);
2221 if (ID == NULL)
2222 return(-1);
2223 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002224
Daniel Veillard37721922001-05-04 15:21:12 +00002225 if(ref_list == NULL) {
2226 xmlFree(ID);
2227 return (-1);
2228 }
2229 /* At this point, ref_list refers to a list of references which
2230 * have the same key as the supplied attr. Our list of references
2231 * is ordered by reference address and we don't have that information
2232 * here to use when removing. We'll have to walk the list and
2233 * check for a matching attribute, when we find one stop the walk
2234 * and remove the entry.
2235 * The list is ordered by reference, so that means we don't have the
2236 * key. Passing the list and the reference to the walker means we
2237 * will have enough data to be able to remove the entry.
2238 */
2239 target.l = ref_list;
2240 target.ap = attr;
2241
2242 /* Remove the supplied attr from our list */
2243 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002244
Daniel Veillard37721922001-05-04 15:21:12 +00002245 /*If the list is empty then remove the list entry in the hash */
2246 if (xmlListEmpty(ref_list))
2247 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2248 xmlFreeRefList);
2249 xmlFree(ID);
2250 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002251}
2252
2253/**
2254 * xmlGetRefs:
2255 * @doc: pointer to the document
2256 * @ID: the ID value
2257 *
2258 * Find the set of references for the supplied ID.
2259 *
2260 * Returns NULL if not found, otherwise node set for the ID.
2261 */
2262xmlListPtr
2263xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002264 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002265
Daniel Veillard37721922001-05-04 15:21:12 +00002266 if (doc == NULL) {
2267 xmlGenericError(xmlGenericErrorContext, "xmlGetRef: doc == NULL\n");
2268 return(NULL);
2269 }
Owen Taylor3473f882001-02-23 17:55:21 +00002270
Daniel Veillard37721922001-05-04 15:21:12 +00002271 if (ID == NULL) {
2272 xmlGenericError(xmlGenericErrorContext, "xmlGetRef: ID == NULL\n");
2273 return(NULL);
2274 }
Owen Taylor3473f882001-02-23 17:55:21 +00002275
Daniel Veillard37721922001-05-04 15:21:12 +00002276 table = (xmlRefTablePtr) doc->refs;
2277 if (table == NULL)
2278 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002279
Daniel Veillard37721922001-05-04 15:21:12 +00002280 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002281}
2282
2283/************************************************************************
2284 * *
2285 * Routines for validity checking *
2286 * *
2287 ************************************************************************/
2288
2289/**
2290 * xmlGetDtdElementDesc:
2291 * @dtd: a pointer to the DtD to search
2292 * @name: the element name
2293 *
2294 * Search the Dtd for the description of this element
2295 *
2296 * returns the xmlElementPtr if found or NULL
2297 */
2298
2299xmlElementPtr
2300xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2301 xmlElementTablePtr table;
2302 xmlElementPtr cur;
2303 xmlChar *uqname = NULL, *prefix = NULL;
2304
2305 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002306 if (dtd->elements == NULL)
2307 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002308 table = (xmlElementTablePtr) dtd->elements;
2309
2310 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002311 if (uqname != NULL)
2312 name = uqname;
2313 cur = xmlHashLookup2(table, name, prefix);
2314 if (prefix != NULL) xmlFree(prefix);
2315 if (uqname != NULL) xmlFree(uqname);
2316 return(cur);
2317}
2318/**
2319 * xmlGetDtdElementDesc2:
2320 * @dtd: a pointer to the DtD to search
2321 * @name: the element name
2322 * @create: create an empty description if not found
2323 *
2324 * Search the Dtd for the description of this element
2325 *
2326 * returns the xmlElementPtr if found or NULL
2327 */
2328
2329xmlElementPtr
2330xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2331 xmlElementTablePtr table;
2332 xmlElementPtr cur;
2333 xmlChar *uqname = NULL, *prefix = NULL;
2334
2335 if (dtd == NULL) return(NULL);
2336 if (dtd->elements == NULL) {
2337 if (!create)
2338 return(NULL);
2339 /*
2340 * Create the Element table if needed.
2341 */
2342 table = (xmlElementTablePtr) dtd->elements;
2343 if (table == NULL) {
2344 table = xmlCreateElementTable();
2345 dtd->elements = (void *) table;
2346 }
2347 if (table == NULL) {
2348 xmlGenericError(xmlGenericErrorContext,
2349 "xmlGetDtdElementDesc: Table creation failed!\n");
2350 return(NULL);
2351 }
2352 }
2353 table = (xmlElementTablePtr) dtd->elements;
2354
2355 uqname = xmlSplitQName2(name, &prefix);
2356 if (uqname != NULL)
2357 name = uqname;
2358 cur = xmlHashLookup2(table, name, prefix);
2359 if ((cur == NULL) && (create)) {
2360 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2361 if (cur == NULL) {
2362 xmlGenericError(xmlGenericErrorContext,
2363 "xmlGetDtdElementDesc: out of memory\n");
2364 return(NULL);
2365 }
2366 memset(cur, 0, sizeof(xmlElement));
2367 cur->type = XML_ELEMENT_DECL;
2368
2369 /*
2370 * fill the structure.
2371 */
2372 cur->name = xmlStrdup(name);
2373 cur->prefix = xmlStrdup(prefix);
2374 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2375
2376 xmlHashAddEntry2(table, name, prefix, cur);
2377 }
2378 if (prefix != NULL) xmlFree(prefix);
2379 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002380 return(cur);
2381}
2382
2383/**
2384 * xmlGetDtdQElementDesc:
2385 * @dtd: a pointer to the DtD to search
2386 * @name: the element name
2387 * @prefix: the element namespace prefix
2388 *
2389 * Search the Dtd for the description of this element
2390 *
2391 * returns the xmlElementPtr if found or NULL
2392 */
2393
Daniel Veillard48da9102001-08-07 01:10:10 +00002394xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002395xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2396 const xmlChar *prefix) {
2397 xmlElementTablePtr table;
2398
2399 if (dtd == NULL) return(NULL);
2400 if (dtd->elements == NULL) return(NULL);
2401 table = (xmlElementTablePtr) dtd->elements;
2402
2403 return(xmlHashLookup2(table, name, prefix));
2404}
2405
2406/**
2407 * xmlGetDtdAttrDesc:
2408 * @dtd: a pointer to the DtD to search
2409 * @elem: the element name
2410 * @name: the attribute name
2411 *
2412 * Search the Dtd for the description of this attribute on
2413 * this element.
2414 *
2415 * returns the xmlAttributePtr if found or NULL
2416 */
2417
2418xmlAttributePtr
2419xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2420 xmlAttributeTablePtr table;
2421 xmlAttributePtr cur;
2422 xmlChar *uqname = NULL, *prefix = NULL;
2423
2424 if (dtd == NULL) return(NULL);
2425 if (dtd->attributes == NULL) return(NULL);
2426
2427 table = (xmlAttributeTablePtr) dtd->attributes;
2428 if (table == NULL)
2429 return(NULL);
2430
2431 uqname = xmlSplitQName2(name, &prefix);
2432
2433 if (uqname != NULL) {
2434 cur = xmlHashLookup3(table, uqname, prefix, elem);
2435 if (prefix != NULL) xmlFree(prefix);
2436 if (uqname != NULL) xmlFree(uqname);
2437 } else
2438 cur = xmlHashLookup3(table, name, NULL, elem);
2439 return(cur);
2440}
2441
2442/**
2443 * xmlGetDtdQAttrDesc:
2444 * @dtd: a pointer to the DtD to search
2445 * @elem: the element name
2446 * @name: the attribute name
2447 * @prefix: the attribute namespace prefix
2448 *
2449 * Search the Dtd for the description of this qualified attribute on
2450 * this element.
2451 *
2452 * returns the xmlAttributePtr if found or NULL
2453 */
2454
Daniel Veillard48da9102001-08-07 01:10:10 +00002455xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002456xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2457 const xmlChar *prefix) {
2458 xmlAttributeTablePtr table;
2459
2460 if (dtd == NULL) return(NULL);
2461 if (dtd->attributes == NULL) return(NULL);
2462 table = (xmlAttributeTablePtr) dtd->attributes;
2463
2464 return(xmlHashLookup3(table, name, prefix, elem));
2465}
2466
2467/**
2468 * xmlGetDtdNotationDesc:
2469 * @dtd: a pointer to the DtD to search
2470 * @name: the notation name
2471 *
2472 * Search the Dtd for the description of this notation
2473 *
2474 * returns the xmlNotationPtr if found or NULL
2475 */
2476
2477xmlNotationPtr
2478xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2479 xmlNotationTablePtr table;
2480
2481 if (dtd == NULL) return(NULL);
2482 if (dtd->notations == NULL) return(NULL);
2483 table = (xmlNotationTablePtr) dtd->notations;
2484
2485 return(xmlHashLookup(table, name));
2486}
2487
2488/**
2489 * xmlValidateNotationUse:
2490 * @ctxt: the validation context
2491 * @doc: the document
2492 * @notationName: the notation name to check
2493 *
2494 * Validate that the given mame match a notation declaration.
2495 * - [ VC: Notation Declared ]
2496 *
2497 * returns 1 if valid or 0 otherwise
2498 */
2499
2500int
2501xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2502 const xmlChar *notationName) {
2503 xmlNotationPtr notaDecl;
2504 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2505
2506 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2507 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2508 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2509
2510 if (notaDecl == NULL) {
2511 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2512 notationName);
2513 return(0);
2514 }
2515 return(1);
2516}
2517
2518/**
2519 * xmlIsMixedElement
2520 * @doc: the document
2521 * @name: the element name
2522 *
2523 * Search in the DtDs whether an element accept Mixed content (or ANY)
2524 * basically if it is supposed to accept text childs
2525 *
2526 * returns 0 if no, 1 if yes, and -1 if no element description is available
2527 */
2528
2529int
2530xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2531 xmlElementPtr elemDecl;
2532
2533 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2534
2535 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2536 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2537 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2538 if (elemDecl == NULL) return(-1);
2539 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002540 case XML_ELEMENT_TYPE_UNDEFINED:
2541 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002542 case XML_ELEMENT_TYPE_ELEMENT:
2543 return(0);
2544 case XML_ELEMENT_TYPE_EMPTY:
2545 /*
2546 * return 1 for EMPTY since we want VC error to pop up
2547 * on <empty> </empty> for example
2548 */
2549 case XML_ELEMENT_TYPE_ANY:
2550 case XML_ELEMENT_TYPE_MIXED:
2551 return(1);
2552 }
2553 return(1);
2554}
2555
2556/**
2557 * xmlValidateNameValue:
2558 * @value: an Name value
2559 *
2560 * Validate that the given value match Name production
2561 *
2562 * returns 1 if valid or 0 otherwise
2563 */
2564
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002565static int
Owen Taylor3473f882001-02-23 17:55:21 +00002566xmlValidateNameValue(const xmlChar *value) {
2567 const xmlChar *cur;
2568
2569 if (value == NULL) return(0);
2570 cur = value;
2571
2572 if (!IS_LETTER(*cur) && (*cur != '_') &&
2573 (*cur != ':')) {
2574 return(0);
2575 }
2576
2577 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2578 (*cur == '.') || (*cur == '-') ||
2579 (*cur == '_') || (*cur == ':') ||
2580 (IS_COMBINING(*cur)) ||
2581 (IS_EXTENDER(*cur)))
2582 cur++;
2583
2584 if (*cur != 0) return(0);
2585
2586 return(1);
2587}
2588
2589/**
2590 * xmlValidateNamesValue:
2591 * @value: an Names value
2592 *
2593 * Validate that the given value match Names production
2594 *
2595 * returns 1 if valid or 0 otherwise
2596 */
2597
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002598static int
Owen Taylor3473f882001-02-23 17:55:21 +00002599xmlValidateNamesValue(const xmlChar *value) {
2600 const xmlChar *cur;
2601
2602 if (value == NULL) return(0);
2603 cur = value;
2604
2605 if (!IS_LETTER(*cur) && (*cur != '_') &&
2606 (*cur != ':')) {
2607 return(0);
2608 }
2609
2610 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2611 (*cur == '.') || (*cur == '-') ||
2612 (*cur == '_') || (*cur == ':') ||
2613 (IS_COMBINING(*cur)) ||
2614 (IS_EXTENDER(*cur)))
2615 cur++;
2616
2617 while (IS_BLANK(*cur)) {
2618 while (IS_BLANK(*cur)) cur++;
2619
2620 if (!IS_LETTER(*cur) && (*cur != '_') &&
2621 (*cur != ':')) {
2622 return(0);
2623 }
2624
2625 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2626 (*cur == '.') || (*cur == '-') ||
2627 (*cur == '_') || (*cur == ':') ||
2628 (IS_COMBINING(*cur)) ||
2629 (IS_EXTENDER(*cur)))
2630 cur++;
2631 }
2632
2633 if (*cur != 0) return(0);
2634
2635 return(1);
2636}
2637
2638/**
2639 * xmlValidateNmtokenValue:
2640 * @value: an Mntoken value
2641 *
2642 * Validate that the given value match Nmtoken production
2643 *
2644 * [ VC: Name Token ]
2645 *
2646 * returns 1 if valid or 0 otherwise
2647 */
2648
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002649static int
Owen Taylor3473f882001-02-23 17:55:21 +00002650xmlValidateNmtokenValue(const xmlChar *value) {
2651 const xmlChar *cur;
2652
2653 if (value == NULL) return(0);
2654 cur = value;
2655
2656 if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) &&
2657 (*cur != '.') && (*cur != '-') &&
2658 (*cur != '_') && (*cur != ':') &&
2659 (!IS_COMBINING(*cur)) &&
2660 (!IS_EXTENDER(*cur)))
2661 return(0);
2662
2663 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2664 (*cur == '.') || (*cur == '-') ||
2665 (*cur == '_') || (*cur == ':') ||
2666 (IS_COMBINING(*cur)) ||
2667 (IS_EXTENDER(*cur)))
2668 cur++;
2669
2670 if (*cur != 0) return(0);
2671
2672 return(1);
2673}
2674
2675/**
2676 * xmlValidateNmtokensValue:
2677 * @value: an Mntokens value
2678 *
2679 * Validate that the given value match Nmtokens production
2680 *
2681 * [ VC: Name Token ]
2682 *
2683 * returns 1 if valid or 0 otherwise
2684 */
2685
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002686static int
Owen Taylor3473f882001-02-23 17:55:21 +00002687xmlValidateNmtokensValue(const xmlChar *value) {
2688 const xmlChar *cur;
2689
2690 if (value == NULL) return(0);
2691 cur = value;
2692
2693 while (IS_BLANK(*cur)) cur++;
2694 if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) &&
2695 (*cur != '.') && (*cur != '-') &&
2696 (*cur != '_') && (*cur != ':') &&
2697 (!IS_COMBINING(*cur)) &&
2698 (!IS_EXTENDER(*cur)))
2699 return(0);
2700
2701 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2702 (*cur == '.') || (*cur == '-') ||
2703 (*cur == '_') || (*cur == ':') ||
2704 (IS_COMBINING(*cur)) ||
2705 (IS_EXTENDER(*cur)))
2706 cur++;
2707
2708 while (IS_BLANK(*cur)) {
2709 while (IS_BLANK(*cur)) cur++;
2710 if (*cur == 0) return(1);
2711
2712 if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) &&
2713 (*cur != '.') && (*cur != '-') &&
2714 (*cur != '_') && (*cur != ':') &&
2715 (!IS_COMBINING(*cur)) &&
2716 (!IS_EXTENDER(*cur)))
2717 return(0);
2718
2719 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
2720 (*cur == '.') || (*cur == '-') ||
2721 (*cur == '_') || (*cur == ':') ||
2722 (IS_COMBINING(*cur)) ||
2723 (IS_EXTENDER(*cur)))
2724 cur++;
2725 }
2726
2727 if (*cur != 0) return(0);
2728
2729 return(1);
2730}
2731
2732/**
2733 * xmlValidateNotationDecl:
2734 * @ctxt: the validation context
2735 * @doc: a document instance
2736 * @nota: a notation definition
2737 *
2738 * Try to validate a single notation definition
2739 * basically it does the following checks as described by the
2740 * XML-1.0 recommendation:
2741 * - it seems that no validity constraing exist on notation declarations
2742 * But this function get called anyway ...
2743 *
2744 * returns 1 if valid or 0 otherwise
2745 */
2746
2747int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002748xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
2749 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002750 int ret = 1;
2751
2752 return(ret);
2753}
2754
2755/**
2756 * xmlValidateAttributeValue:
2757 * @type: an attribute type
2758 * @value: an attribute value
2759 *
2760 * Validate that the given attribute value match the proper production
2761 *
2762 * [ VC: ID ]
2763 * Values of type ID must match the Name production....
2764 *
2765 * [ VC: IDREF ]
2766 * Values of type IDREF must match the Name production, and values
2767 * of type IDREFS must match Names ...
2768 *
2769 * [ VC: Entity Name ]
2770 * Values of type ENTITY must match the Name production, values
2771 * of type ENTITIES must match Names ...
2772 *
2773 * [ VC: Name Token ]
2774 * Values of type NMTOKEN must match the Nmtoken production; values
2775 * of type NMTOKENS must match Nmtokens.
2776 *
2777 * returns 1 if valid or 0 otherwise
2778 */
2779
2780int
2781xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
2782 switch (type) {
2783 case XML_ATTRIBUTE_ENTITIES:
2784 case XML_ATTRIBUTE_IDREFS:
2785 return(xmlValidateNamesValue(value));
2786 case XML_ATTRIBUTE_ENTITY:
2787 case XML_ATTRIBUTE_IDREF:
2788 case XML_ATTRIBUTE_ID:
2789 case XML_ATTRIBUTE_NOTATION:
2790 return(xmlValidateNameValue(value));
2791 case XML_ATTRIBUTE_NMTOKENS:
2792 case XML_ATTRIBUTE_ENUMERATION:
2793 return(xmlValidateNmtokensValue(value));
2794 case XML_ATTRIBUTE_NMTOKEN:
2795 return(xmlValidateNmtokenValue(value));
2796 case XML_ATTRIBUTE_CDATA:
2797 break;
2798 }
2799 return(1);
2800}
2801
2802/**
2803 * xmlValidateAttributeValue2:
2804 * @ctxt: the validation context
2805 * @doc: the document
2806 * @name: the attribute name (used for error reporting only)
2807 * @type: the attribute type
2808 * @value: the attribute value
2809 *
2810 * Validate that the given attribute value match a given type.
2811 * This typically cannot be done before having finished parsing
2812 * the subsets.
2813 *
2814 * [ VC: IDREF ]
2815 * Values of type IDREF must match one of the declared IDs
2816 * Values of type IDREFS must match a sequence of the declared IDs
2817 * each Name must match the value of an ID attribute on some element
2818 * in the XML document; i.e. IDREF values must match the value of
2819 * some ID attribute
2820 *
2821 * [ VC: Entity Name ]
2822 * Values of type ENTITY must match one declared entity
2823 * Values of type ENTITIES must match a sequence of declared entities
2824 *
2825 * [ VC: Notation Attributes ]
2826 * all notation names in the declaration must be declared.
2827 *
2828 * returns 1 if valid or 0 otherwise
2829 */
2830
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002831static int
Owen Taylor3473f882001-02-23 17:55:21 +00002832xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2833 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
2834 int ret = 1;
2835 switch (type) {
2836 case XML_ATTRIBUTE_IDREFS:
2837 case XML_ATTRIBUTE_IDREF:
2838 case XML_ATTRIBUTE_ID:
2839 case XML_ATTRIBUTE_NMTOKENS:
2840 case XML_ATTRIBUTE_ENUMERATION:
2841 case XML_ATTRIBUTE_NMTOKEN:
2842 case XML_ATTRIBUTE_CDATA:
2843 break;
2844 case XML_ATTRIBUTE_ENTITY: {
2845 xmlEntityPtr ent;
2846
2847 ent = xmlGetDocEntity(doc, value);
2848 if (ent == NULL) {
2849 VERROR(ctxt->userData,
2850 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2851 name, value);
2852 ret = 0;
2853 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2854 VERROR(ctxt->userData,
2855 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2856 name, value);
2857 ret = 0;
2858 }
2859 break;
2860 }
2861 case XML_ATTRIBUTE_ENTITIES: {
2862 xmlChar *dup, *nam = NULL, *cur, save;
2863 xmlEntityPtr ent;
2864
2865 dup = xmlStrdup(value);
2866 if (dup == NULL)
2867 return(0);
2868 cur = dup;
2869 while (*cur != 0) {
2870 nam = cur;
2871 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2872 save = *cur;
2873 *cur = 0;
2874 ent = xmlGetDocEntity(doc, nam);
2875 if (ent == NULL) {
2876 VERROR(ctxt->userData,
2877 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
2878 name, nam);
2879 ret = 0;
2880 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2881 VERROR(ctxt->userData,
2882 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
2883 name, nam);
2884 ret = 0;
2885 }
2886 if (save == 0)
2887 break;
2888 *cur = save;
2889 while (IS_BLANK(*cur)) cur++;
2890 }
2891 xmlFree(dup);
2892 break;
2893 }
2894 case XML_ATTRIBUTE_NOTATION: {
2895 xmlNotationPtr nota;
2896
2897 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
2898 if ((nota == NULL) && (doc->extSubset != NULL))
2899 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
2900
2901 if (nota == NULL) {
2902 VERROR(ctxt->userData,
2903 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
2904 name, value);
2905 ret = 0;
2906 }
2907 break;
2908 }
2909 }
2910 return(ret);
2911}
2912
2913/**
2914 * xmlValidNormalizeAttributeValue:
2915 * @doc: the document
2916 * @elem: the parent
2917 * @name: the attribute name
2918 * @value: the attribute value
2919 *
2920 * Does the validation related extra step of the normalization of attribute
2921 * values:
2922 *
2923 * If the declared value is not CDATA, then the XML processor must further
2924 * process the normalized attribute value by discarding any leading and
2925 * trailing space (#x20) characters, and by replacing sequences of space
2926 * (#x20) characters by single space (#x20) character.
2927 *
2928 * returns a new normalized string if normalization is needed, NULL otherwise
2929 * the caller must free the returned value.
2930 */
2931
2932xmlChar *
2933xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
2934 const xmlChar *name, const xmlChar *value) {
2935 xmlChar *ret, *dst;
2936 const xmlChar *src;
2937 xmlAttributePtr attrDecl = NULL;
2938
2939 if (doc == NULL) return(NULL);
2940 if (elem == NULL) return(NULL);
2941 if (name == NULL) return(NULL);
2942 if (value == NULL) return(NULL);
2943
2944 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
2945 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002946 snprintf((char *) qname, sizeof(qname), "%s:%s",
2947 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002948 qname[sizeof(qname) - 1] = 0;
2949 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
2950 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2951 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
2952 }
2953 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
2954 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2955 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
2956
2957 if (attrDecl == NULL)
2958 return(NULL);
2959 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
2960 return(NULL);
2961
2962 ret = xmlStrdup(value);
2963 if (ret == NULL)
2964 return(NULL);
2965 src = value;
2966 dst = ret;
2967 while (*src == 0x20) src++;
2968 while (*src != 0) {
2969 if (*src == 0x20) {
2970 while (*src == 0x20) src++;
2971 if (*src != 0)
2972 *dst++ = 0x20;
2973 } else {
2974 *dst++ = *src++;
2975 }
2976 }
2977 *dst = 0;
2978 return(ret);
2979}
2980
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002981static void
Owen Taylor3473f882001-02-23 17:55:21 +00002982xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002983 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002984 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
2985}
2986
2987/**
2988 * xmlValidateAttributeDecl:
2989 * @ctxt: the validation context
2990 * @doc: a document instance
2991 * @attr: an attribute definition
2992 *
2993 * Try to validate a single attribute definition
2994 * basically it does the following checks as described by the
2995 * XML-1.0 recommendation:
2996 * - [ VC: Attribute Default Legal ]
2997 * - [ VC: Enumeration ]
2998 * - [ VC: ID Attribute Default ]
2999 *
3000 * The ID/IDREF uniqueness and matching are done separately
3001 *
3002 * returns 1 if valid or 0 otherwise
3003 */
3004
3005int
3006xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3007 xmlAttributePtr attr) {
3008 int ret = 1;
3009 int val;
3010 CHECK_DTD;
3011 if(attr == NULL) return(1);
3012
3013 /* Attribute Default Legal */
3014 /* Enumeration */
3015 if (attr->defaultValue != NULL) {
3016 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3017 if (val == 0) {
3018 VERROR(ctxt->userData,
3019 "Syntax of default value for attribute %s on %s is not valid\n",
3020 attr->name, attr->elem);
3021 }
3022 ret &= val;
3023 }
3024
3025 /* ID Attribute Default */
3026 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3027 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3028 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3029 VERROR(ctxt->userData,
3030 "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n",
3031 attr->name, attr->elem);
3032 ret = 0;
3033 }
3034
3035 /* One ID per Element Type */
3036 if (attr->atype == XML_ATTRIBUTE_ID) {
3037 int nbId;
3038
3039 /* the trick is taht we parse DtD as their own internal subset */
3040 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3041 attr->elem);
3042 if (elem != NULL) {
3043 nbId = xmlScanIDAttributeDecl(NULL, elem);
3044 } else {
3045 xmlAttributeTablePtr table;
3046
3047 /*
3048 * The attribute may be declared in the internal subset and the
3049 * element in the external subset.
3050 */
3051 nbId = 0;
3052 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3053 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3054 xmlValidateAttributeIdCallback, &nbId);
3055 }
3056 if (nbId > 1) {
3057 VERROR(ctxt->userData,
3058 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3059 attr->elem, nbId, attr->name);
3060 } else if (doc->extSubset != NULL) {
3061 int extId = 0;
3062 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3063 if (elem != NULL) {
3064 extId = xmlScanIDAttributeDecl(NULL, elem);
3065 }
3066 if (extId > 1) {
3067 VERROR(ctxt->userData,
3068 "Element %s has %d ID attribute defined in the external subset : %s\n",
3069 attr->elem, extId, attr->name);
3070 } else if (extId + nbId > 1) {
3071 VERROR(ctxt->userData,
3072"Element %s has ID attributes defined in the internal and external subset : %s\n",
3073 attr->elem, attr->name);
3074 }
3075 }
3076 }
3077
3078 /* Validity Constraint: Enumeration */
3079 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3080 xmlEnumerationPtr tree = attr->tree;
3081 while (tree != NULL) {
3082 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3083 tree = tree->next;
3084 }
3085 if (tree == NULL) {
3086 VERROR(ctxt->userData,
3087"Default value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3088 attr->defaultValue, attr->name, attr->elem);
3089 ret = 0;
3090 }
3091 }
3092
3093 return(ret);
3094}
3095
3096/**
3097 * xmlValidateElementDecl:
3098 * @ctxt: the validation context
3099 * @doc: a document instance
3100 * @elem: an element definition
3101 *
3102 * Try to validate a single element definition
3103 * basically it does the following checks as described by the
3104 * XML-1.0 recommendation:
3105 * - [ VC: One ID per Element Type ]
3106 * - [ VC: No Duplicate Types ]
3107 * - [ VC: Unique Element Type Declaration ]
3108 *
3109 * returns 1 if valid or 0 otherwise
3110 */
3111
3112int
3113xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3114 xmlElementPtr elem) {
3115 int ret = 1;
3116 xmlElementPtr tst;
3117
3118 CHECK_DTD;
3119
3120 if (elem == NULL) return(1);
3121
3122 /* No Duplicate Types */
3123 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3124 xmlElementContentPtr cur, next;
3125 const xmlChar *name;
3126
3127 cur = elem->content;
3128 while (cur != NULL) {
3129 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3130 if (cur->c1 == NULL) break;
3131 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3132 name = cur->c1->name;
3133 next = cur->c2;
3134 while (next != NULL) {
3135 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3136 if (xmlStrEqual(next->name, name)) {
3137 VERROR(ctxt->userData,
3138 "Definition of %s has duplicate references of %s\n",
3139 elem->name, name);
3140 ret = 0;
3141 }
3142 break;
3143 }
3144 if (next->c1 == NULL) break;
3145 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3146 if (xmlStrEqual(next->c1->name, name)) {
3147 VERROR(ctxt->userData,
3148 "Definition of %s has duplicate references of %s\n",
3149 elem->name, name);
3150 ret = 0;
3151 }
3152 next = next->c2;
3153 }
3154 }
3155 cur = cur->c2;
3156 }
3157 }
3158
3159 /* VC: Unique Element Type Declaration */
3160 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003161 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003162 ((tst->prefix == elem->prefix) ||
3163 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003164 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003165 VERROR(ctxt->userData, "Redefinition of element %s\n",
3166 elem->name);
3167 ret = 0;
3168 }
3169 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003170 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003171 ((tst->prefix == elem->prefix) ||
3172 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003173 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003174 VERROR(ctxt->userData, "Redefinition of element %s\n",
3175 elem->name);
3176 ret = 0;
3177 }
3178
Daniel Veillarda10efa82001-04-18 13:09:01 +00003179 /* One ID per Element Type
3180 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003181 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3182 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003183 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003184 return(ret);
3185}
3186
3187/**
3188 * xmlValidateOneAttribute:
3189 * @ctxt: the validation context
3190 * @doc: a document instance
3191 * @elem: an element instance
3192 * @attr: an attribute instance
3193 * @value: the attribute value (without entities processing)
3194 *
3195 * Try to validate a single attribute for an element
3196 * basically it does the following checks as described by the
3197 * XML-1.0 recommendation:
3198 * - [ VC: Attribute Value Type ]
3199 * - [ VC: Fixed Attribute Default ]
3200 * - [ VC: Entity Name ]
3201 * - [ VC: Name Token ]
3202 * - [ VC: ID ]
3203 * - [ VC: IDREF ]
3204 * - [ VC: Entity Name ]
3205 * - [ VC: Notation Attributes ]
3206 *
3207 * The ID/IDREF uniqueness and matching are done separately
3208 *
3209 * returns 1 if valid or 0 otherwise
3210 */
3211
3212int
3213xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3214 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3215 /* xmlElementPtr elemDecl; */
3216 xmlAttributePtr attrDecl = NULL;
3217 int val;
3218 int ret = 1;
3219
3220 CHECK_DTD;
3221 if ((elem == NULL) || (elem->name == NULL)) return(0);
3222 if ((attr == NULL) || (attr->name == NULL)) return(0);
3223
3224 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3225 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003226 snprintf((char *) qname, sizeof(qname), "%s:%s",
3227 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003228 qname[sizeof(qname) - 1] = 0;
3229 if (attr->ns != NULL) {
3230 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3231 attr->name, attr->ns->prefix);
3232 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3233 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3234 attr->name, attr->ns->prefix);
3235 } else {
3236 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3237 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3238 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3239 qname, attr->name);
3240 }
3241 }
3242 if (attrDecl == NULL) {
3243 if (attr->ns != NULL) {
3244 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3245 attr->name, attr->ns->prefix);
3246 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3247 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3248 attr->name, attr->ns->prefix);
3249 } else {
3250 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3251 elem->name, attr->name);
3252 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3253 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3254 elem->name, attr->name);
3255 }
3256 }
3257
3258
3259 /* Validity Constraint: Attribute Value Type */
3260 if (attrDecl == NULL) {
3261 VERROR(ctxt->userData,
3262 "No declaration for attribute %s on element %s\n",
3263 attr->name, elem->name);
3264 return(0);
3265 }
3266 attr->atype = attrDecl->atype;
3267
3268 val = xmlValidateAttributeValue(attrDecl->atype, value);
3269 if (val == 0) {
3270 VERROR(ctxt->userData,
3271 "Syntax of value for attribute %s on %s is not valid\n",
3272 attr->name, elem->name);
3273 ret = 0;
3274 }
3275
3276 /* Validity constraint: Fixed Attribute Default */
3277 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3278 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3279 VERROR(ctxt->userData,
3280 "Value for attribute %s on %s is differnt from default \"%s\"\n",
3281 attr->name, elem->name, attrDecl->defaultValue);
3282 ret = 0;
3283 }
3284 }
3285
3286 /* Validity Constraint: ID uniqueness */
3287 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3288 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3289 ret = 0;
3290 }
3291
3292 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3293 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3294 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3295 ret = 0;
3296 }
3297
3298 /* Validity Constraint: Notation Attributes */
3299 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3300 xmlEnumerationPtr tree = attrDecl->tree;
3301 xmlNotationPtr nota;
3302
3303 /* First check that the given NOTATION was declared */
3304 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3305 if (nota == NULL)
3306 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3307
3308 if (nota == NULL) {
3309 VERROR(ctxt->userData,
3310 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3311 value, attr->name, elem->name);
3312 ret = 0;
3313 }
3314
3315 /* Second, verify that it's among the list */
3316 while (tree != NULL) {
3317 if (xmlStrEqual(tree->name, value)) break;
3318 tree = tree->next;
3319 }
3320 if (tree == NULL) {
3321 VERROR(ctxt->userData,
3322"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3323 value, attr->name, elem->name);
3324 ret = 0;
3325 }
3326 }
3327
3328 /* Validity Constraint: Enumeration */
3329 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3330 xmlEnumerationPtr tree = attrDecl->tree;
3331 while (tree != NULL) {
3332 if (xmlStrEqual(tree->name, value)) break;
3333 tree = tree->next;
3334 }
3335 if (tree == NULL) {
3336 VERROR(ctxt->userData,
3337 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3338 value, attr->name, elem->name);
3339 ret = 0;
3340 }
3341 }
3342
3343 /* Fixed Attribute Default */
3344 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3345 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3346 VERROR(ctxt->userData,
3347 "Value for attribute %s on %s must be \"%s\"\n",
3348 attr->name, elem->name, attrDecl->defaultValue);
3349 ret = 0;
3350 }
3351
3352 /* Extra check for the attribute value */
3353 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3354 attrDecl->atype, value);
3355
3356 return(ret);
3357}
3358
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003359/**
3360 * xmlValidateSkipIgnorable:
3361 * @ctxt: the validation context
3362 * @child: the child list
3363 *
3364 * Skip ignorable elements w.r.t. the validation process
3365 *
3366 * returns the first element to consider for validation of the content model
3367 */
3368
3369static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003370xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003371 while (child != NULL) {
3372 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003373 /* These things are ignored (skipped) during validation. */
3374 case XML_PI_NODE:
3375 case XML_COMMENT_NODE:
3376 case XML_XINCLUDE_START:
3377 case XML_XINCLUDE_END:
3378 child = child->next;
3379 break;
3380 case XML_TEXT_NODE:
3381 if (xmlIsBlankNode(child))
3382 child = child->next;
3383 else
3384 return(child);
3385 break;
3386 /* keep current node */
3387 default:
3388 return(child);
3389 }
3390 }
3391 return(child);
3392}
3393
3394/**
3395 * xmlValidateElementType:
3396 * @ctxt: the validation context
3397 *
3398 * Try to validate the content model of an element internal function
3399 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003400 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3401 * reference is found and -3 if the validation succeeded but
3402 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003403 */
3404
3405static int
3406xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003407 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003408 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003409
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003410 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003411 if ((NODE == NULL) && (CONT == NULL))
3412 return(1);
3413 if ((NODE == NULL) &&
3414 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3415 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3416 return(1);
3417 }
3418 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003419 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003420 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003421
3422 /*
3423 * We arrive here when more states need to be examined
3424 */
3425cont:
3426
3427 /*
3428 * We just recovered from a rollback generated by a possible
3429 * epsilon transition, go directly to the analysis phase
3430 */
3431 if (STATE == ROLLBACK_PARENT) {
3432 DEBUG_VALID_MSG("restaured parent branch");
3433 DEBUG_VALID_STATE(NODE, CONT)
3434 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003435 goto analyze;
3436 }
3437
3438 DEBUG_VALID_STATE(NODE, CONT)
3439 /*
3440 * we may have to save a backup state here. This is the equivalent
3441 * of handling epsilon transition in NFAs.
3442 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003443 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003444 ((CONT->parent == NULL) ||
3445 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003446 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003447 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
3448 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003449 DEBUG_VALID_MSG("saving parent branch");
3450 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3451 }
3452
3453
3454 /*
3455 * Check first if the content matches
3456 */
3457 switch (CONT->type) {
3458 case XML_ELEMENT_CONTENT_PCDATA:
3459 if (NODE == NULL) {
3460 DEBUG_VALID_MSG("pcdata failed no node");
3461 ret = 0;
3462 break;
3463 }
3464 if (NODE->type == XML_TEXT_NODE) {
3465 DEBUG_VALID_MSG("pcdata found, skip to next");
3466 /*
3467 * go to next element in the content model
3468 * skipping ignorable elems
3469 */
3470 do {
3471 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003472 NODE = xmlValidateSkipIgnorable(NODE);
3473 if ((NODE != NULL) &&
3474 (NODE->type == XML_ENTITY_REF_NODE))
3475 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003476 } while ((NODE != NULL) &&
3477 ((NODE->type != XML_ELEMENT_NODE) &&
3478 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003479 ret = 1;
3480 break;
3481 } else {
3482 DEBUG_VALID_MSG("pcdata failed");
3483 ret = 0;
3484 break;
3485 }
3486 break;
3487 case XML_ELEMENT_CONTENT_ELEMENT:
3488 if (NODE == NULL) {
3489 DEBUG_VALID_MSG("element failed no node");
3490 ret = 0;
3491 break;
3492 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003493 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3494 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003495 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003496 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3497 ret = (CONT->prefix == NULL);
3498 } else if (CONT->prefix == NULL) {
3499 ret = 0;
3500 } else {
3501 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3502 }
3503 }
3504 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003505 DEBUG_VALID_MSG("element found, skip to next");
3506 /*
3507 * go to next element in the content model
3508 * skipping ignorable elems
3509 */
3510 do {
3511 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003512 NODE = xmlValidateSkipIgnorable(NODE);
3513 if ((NODE != NULL) &&
3514 (NODE->type == XML_ENTITY_REF_NODE))
3515 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003516 } while ((NODE != NULL) &&
3517 ((NODE->type != XML_ELEMENT_NODE) &&
3518 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003519 } else {
3520 DEBUG_VALID_MSG("element failed");
3521 ret = 0;
3522 break;
3523 }
3524 break;
3525 case XML_ELEMENT_CONTENT_OR:
3526 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003527 * Small optimization.
3528 */
3529 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3530 if ((NODE == NULL) ||
3531 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3532 DEPTH++;
3533 CONT = CONT->c2;
3534 goto cont;
3535 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003536 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3537 ret = (CONT->c1->prefix == NULL);
3538 } else if (CONT->c1->prefix == NULL) {
3539 ret = 0;
3540 } else {
3541 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3542 }
3543 if (ret == 0) {
3544 DEPTH++;
3545 CONT = CONT->c2;
3546 goto cont;
3547 }
Daniel Veillard85349052001-04-20 13:48:21 +00003548 }
3549
3550 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003551 * save the second branch 'or' branch
3552 */
3553 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003554 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3555 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003556
3557 DEPTH++;
3558 CONT = CONT->c1;
3559 goto cont;
3560 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003561 /*
3562 * Small optimization.
3563 */
3564 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3565 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3566 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3567 if ((NODE == NULL) ||
3568 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3569 DEPTH++;
3570 CONT = CONT->c2;
3571 goto cont;
3572 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003573 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3574 ret = (CONT->c1->prefix == NULL);
3575 } else if (CONT->c1->prefix == NULL) {
3576 ret = 0;
3577 } else {
3578 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3579 }
3580 if (ret == 0) {
3581 DEPTH++;
3582 CONT = CONT->c2;
3583 goto cont;
3584 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003585 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003586 DEPTH++;
3587 CONT = CONT->c1;
3588 goto cont;
3589 }
3590
3591 /*
3592 * At this point handle going up in the tree
3593 */
3594 if (ret == -1) {
3595 DEBUG_VALID_MSG("error found returning");
3596 return(ret);
3597 }
3598analyze:
3599 while (CONT != NULL) {
3600 /*
3601 * First do the analysis depending on the occurence model at
3602 * this level.
3603 */
3604 if (ret == 0) {
3605 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003606 xmlNodePtr cur;
3607
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003608 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003609 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003610 DEBUG_VALID_MSG("Once branch failed, rollback");
3611 if (vstateVPop(ctxt) < 0 ) {
3612 DEBUG_VALID_MSG("exhaustion, failed");
3613 return(0);
3614 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003615 if (cur != ctxt->vstate->node)
3616 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003617 goto cont;
3618 case XML_ELEMENT_CONTENT_PLUS:
3619 if (OCCURENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003620 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003621 DEBUG_VALID_MSG("Plus branch failed, rollback");
3622 if (vstateVPop(ctxt) < 0 ) {
3623 DEBUG_VALID_MSG("exhaustion, failed");
3624 return(0);
3625 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003626 if (cur != ctxt->vstate->node)
3627 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003628 goto cont;
3629 }
3630 DEBUG_VALID_MSG("Plus branch found");
3631 ret = 1;
3632 break;
3633 case XML_ELEMENT_CONTENT_MULT:
3634#ifdef DEBUG_VALID_ALGO
3635 if (OCCURENCE == 0) {
3636 DEBUG_VALID_MSG("Mult branch failed");
3637 } else {
3638 DEBUG_VALID_MSG("Mult branch found");
3639 }
3640#endif
3641 ret = 1;
3642 break;
3643 case XML_ELEMENT_CONTENT_OPT:
3644 DEBUG_VALID_MSG("Option branch failed");
3645 ret = 1;
3646 break;
3647 }
3648 } else {
3649 switch (CONT->ocur) {
3650 case XML_ELEMENT_CONTENT_OPT:
3651 DEBUG_VALID_MSG("Option branch succeeded");
3652 ret = 1;
3653 break;
3654 case XML_ELEMENT_CONTENT_ONCE:
3655 DEBUG_VALID_MSG("Once branch succeeded");
3656 ret = 1;
3657 break;
3658 case XML_ELEMENT_CONTENT_PLUS:
3659 if (STATE == ROLLBACK_PARENT) {
3660 DEBUG_VALID_MSG("Plus branch rollback");
3661 ret = 1;
3662 break;
3663 }
3664 if (NODE == NULL) {
3665 DEBUG_VALID_MSG("Plus branch exhausted");
3666 ret = 1;
3667 break;
3668 }
3669 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
3670 SET_OCCURENCE;
3671 goto cont;
3672 case XML_ELEMENT_CONTENT_MULT:
3673 if (STATE == ROLLBACK_PARENT) {
3674 DEBUG_VALID_MSG("Mult branch rollback");
3675 ret = 1;
3676 break;
3677 }
3678 if (NODE == NULL) {
3679 DEBUG_VALID_MSG("Mult branch exhausted");
3680 ret = 1;
3681 break;
3682 }
3683 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003684 /* SET_OCCURENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003685 goto cont;
3686 }
3687 }
3688 STATE = 0;
3689
3690 /*
3691 * Then act accordingly at the parent level
3692 */
3693 RESET_OCCURENCE;
3694 if (CONT->parent == NULL)
3695 break;
3696
3697 switch (CONT->parent->type) {
3698 case XML_ELEMENT_CONTENT_PCDATA:
3699 DEBUG_VALID_MSG("Error: parent pcdata");
3700 return(-1);
3701 case XML_ELEMENT_CONTENT_ELEMENT:
3702 DEBUG_VALID_MSG("Error: parent element");
3703 return(-1);
3704 case XML_ELEMENT_CONTENT_OR:
3705 if (ret == 1) {
3706 DEBUG_VALID_MSG("Or succeeded");
3707 CONT = CONT->parent;
3708 DEPTH--;
3709 } else {
3710 DEBUG_VALID_MSG("Or failed");
3711 CONT = CONT->parent;
3712 DEPTH--;
3713 }
3714 break;
3715 case XML_ELEMENT_CONTENT_SEQ:
3716 if (ret == 0) {
3717 DEBUG_VALID_MSG("Sequence failed");
3718 CONT = CONT->parent;
3719 DEPTH--;
3720 } else if (CONT == CONT->parent->c1) {
3721 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3722 CONT = CONT->parent->c2;
3723 goto cont;
3724 } else {
3725 DEBUG_VALID_MSG("Sequence succeeded");
3726 CONT = CONT->parent;
3727 DEPTH--;
3728 }
3729 }
3730 }
3731 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003732 xmlNodePtr cur;
3733
3734 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003735 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3736 if (vstateVPop(ctxt) < 0 ) {
3737 DEBUG_VALID_MSG("exhaustion, failed");
3738 return(0);
3739 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003740 if (cur != ctxt->vstate->node)
3741 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003742 goto cont;
3743 }
3744 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003745 xmlNodePtr cur;
3746
3747 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003748 DEBUG_VALID_MSG("Failure, rollback");
3749 if (vstateVPop(ctxt) < 0 ) {
3750 DEBUG_VALID_MSG("exhaustion, failed");
3751 return(0);
3752 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003753 if (cur != ctxt->vstate->node)
3754 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003755 goto cont;
3756 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003757 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003758}
3759
3760/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003761 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003762 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003763 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003764 * @content: An element
3765 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3766 *
3767 * This will dump the list of elements to the buffer
3768 * Intended just for the debug routine
3769 */
3770static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003771xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003772 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003773 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003774
3775 if (node == NULL) return;
3776 if (glob) strcat(buf, "(");
3777 cur = node;
3778 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003779 len = strlen(buf);
3780 if (size - len < 50) {
3781 if ((size - len > 4) && (buf[len - 1] != '.'))
3782 strcat(buf, " ...");
3783 return;
3784 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003785 switch (cur->type) {
3786 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003787 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
3788 if (size - len < xmlStrlen(cur->ns->prefix + 10)) {
3789 if ((size - len > 4) && (buf[len - 1] != '.'))
3790 strcat(buf, " ...");
3791 return;
3792 }
3793 strcat(buf, (char *) cur->ns->prefix);
3794 strcat(buf, ":");
3795 }
Daniel Veillardd3d06722001-08-15 12:06:36 +00003796 if (size - len < xmlStrlen(cur->name + 10)) {
3797 if ((size - len > 4) && (buf[len - 1] != '.'))
3798 strcat(buf, " ...");
3799 return;
3800 }
3801 strcat(buf, (char *) cur->name);
3802 if (cur->next != NULL)
3803 strcat(buf, " ");
3804 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003805 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003806 if (xmlIsBlankNode(cur))
3807 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003808 case XML_CDATA_SECTION_NODE:
3809 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003810 strcat(buf, "CDATA");
3811 if (cur->next != NULL)
3812 strcat(buf, " ");
3813 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003814 case XML_ATTRIBUTE_NODE:
3815 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003816#ifdef LIBXML_DOCB_ENABLED
3817 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003818#endif
3819 case XML_HTML_DOCUMENT_NODE:
3820 case XML_DOCUMENT_TYPE_NODE:
3821 case XML_DOCUMENT_FRAG_NODE:
3822 case XML_NOTATION_NODE:
3823 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003824 strcat(buf, "???");
3825 if (cur->next != NULL)
3826 strcat(buf, " ");
3827 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003828 case XML_ENTITY_NODE:
3829 case XML_PI_NODE:
3830 case XML_DTD_NODE:
3831 case XML_COMMENT_NODE:
3832 case XML_ELEMENT_DECL:
3833 case XML_ATTRIBUTE_DECL:
3834 case XML_ENTITY_DECL:
3835 case XML_XINCLUDE_START:
3836 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003837 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003838 }
3839 cur = cur->next;
3840 }
3841 if (glob) strcat(buf, ")");
3842}
3843
3844/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003845 * xmlValidateElementContent:
3846 * @ctxt: the validation context
3847 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003848 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003849 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003850 *
3851 * Try to validate the content model of an element
3852 *
3853 * returns 1 if valid or 0 if not and -1 in case of error
3854 */
3855
3856static int
3857xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003858 xmlElementPtr elemDecl, int warn) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003859 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003860 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003861 xmlElementContentPtr cont;
3862 const xmlChar *name;
3863
3864 if (elemDecl == NULL)
3865 return(-1);
3866 cont = elemDecl->content;
3867 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003868
3869 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003870 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003871 */
3872 ctxt->vstateMax = 8;
3873 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
3874 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
3875 if (ctxt->vstateTab == NULL) {
3876 xmlGenericError(xmlGenericErrorContext,
3877 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003878 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003879 }
3880 /*
3881 * The first entry in the stack is reserved to the current state
3882 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00003883 ctxt->nodeMax = 0;
3884 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00003885 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003886 ctxt->vstate = &ctxt->vstateTab[0];
3887 ctxt->vstateNr = 1;
3888 CONT = cont;
3889 NODE = child;
3890 DEPTH = 0;
3891 OCCURS = 0;
3892 STATE = 0;
3893 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003894 if ((ret == -3) && (warn)) {
3895 VWARNING(ctxt->userData,
3896 "Element %s content model is ambiguous\n", name);
3897 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003898 /*
3899 * An entities reference appeared at this level.
3900 * Buid a minimal representation of this node content
3901 * sufficient to run the validation process on it
3902 */
3903 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003904 cur = child;
3905 while (cur != NULL) {
3906 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003907 case XML_ENTITY_REF_NODE:
3908 /*
3909 * Push the current node to be able to roll back
3910 * and process within the entity
3911 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003912 if ((cur->children != NULL) &&
3913 (cur->children->children != NULL)) {
3914 nodeVPush(ctxt, cur);
3915 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003916 continue;
3917 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00003918 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003919 case XML_TEXT_NODE:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003920 case XML_CDATA_SECTION_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003921 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003922 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003923 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003924 case XML_ELEMENT_NODE:
3925 /*
3926 * Allocate a new node and minimally fills in
3927 * what's required
3928 */
3929 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3930 if (tmp == NULL) {
3931 xmlGenericError(xmlGenericErrorContext,
3932 "xmlValidateElementContent : malloc failed\n");
3933 xmlFreeNodeList(repl);
3934 ret = -1;
3935 goto done;
3936 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003937 tmp->type = cur->type;
3938 tmp->name = cur->name;
3939 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003940 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00003941 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003942 if (repl == NULL)
3943 repl = last = tmp;
3944 else {
3945 last->next = tmp;
3946 last = tmp;
3947 }
3948 break;
3949 default:
3950 break;
3951 }
3952 /*
3953 * Switch to next element
3954 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003955 cur = cur->next;
3956 while (cur == NULL) {
3957 cur = nodeVPop(ctxt);
3958 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003959 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003960 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003961 }
3962 }
3963
3964 /*
3965 * Relaunch the validation
3966 */
3967 ctxt->vstate = &ctxt->vstateTab[0];
3968 ctxt->vstateNr = 1;
3969 CONT = cont;
3970 NODE = repl;
3971 DEPTH = 0;
3972 OCCURS = 0;
3973 STATE = 0;
3974 ret = xmlValidateElementType(ctxt);
3975 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003976 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003977 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
3978 char expr[5000];
3979 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003980
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003981 expr[0] = 0;
3982 xmlSnprintfElementContent(expr, 5000, cont, 1);
3983 list[0] = 0;
3984 if (repl != NULL)
3985 xmlSnprintfElements(list, 5000, repl, 1);
3986 else
3987 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003988
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003989 if (name != NULL) {
3990 VERROR(ctxt->userData,
3991 "Element %s content doesn't follow the Dtd\nExpecting %s, got %s\n",
3992 name, expr, list);
3993 } else {
3994 VERROR(ctxt->userData,
3995 "Element content doesn't follow the Dtd\nExpecting %s, got %s\n",
3996 expr, list);
3997 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003998 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003999 if (name != NULL) {
4000 VERROR(ctxt->userData,
4001 "Element %s content doesn't follow the Dtd\n",
4002 name);
4003 } else {
4004 VERROR(ctxt->userData,
4005 "Element content doesn't follow the Dtd\n");
4006 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004007 }
4008 ret = 0;
4009 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004010 if (ret == -3)
4011 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004012
4013
4014done:
4015 /*
4016 * Deallocate the copy if done, and free up the validation stack
4017 */
4018 while (repl != NULL) {
4019 tmp = repl->next;
4020 xmlFree(repl);
4021 repl = tmp;
4022 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004023 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004024 if (ctxt->vstateTab != NULL) {
4025 xmlFree(ctxt->vstateTab);
4026 ctxt->vstateTab = NULL;
4027 }
4028 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004029 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004030 if (ctxt->nodeTab != NULL) {
4031 xmlFree(ctxt->nodeTab);
4032 ctxt->nodeTab = NULL;
4033 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004034 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004035
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004036}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004037
Owen Taylor3473f882001-02-23 17:55:21 +00004038/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004039 * xmlValidateCdataElement:
4040 * @ctxt: the validation context
4041 * @doc: a document instance
4042 * @elem: an element instance
4043 *
4044 * Check that an element follows #CDATA
4045 *
4046 * returns 1 if valid or 0 otherwise
4047 */
4048static int
4049xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4050 xmlNodePtr elem) {
4051 int ret = 1;
4052 xmlNodePtr cur, child;
4053
4054 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4055 return(0);
4056
4057 child = elem->children;
4058
4059 cur = child;
4060 while (cur != NULL) {
4061 switch (cur->type) {
4062 case XML_ENTITY_REF_NODE:
4063 /*
4064 * Push the current node to be able to roll back
4065 * and process within the entity
4066 */
4067 if ((cur->children != NULL) &&
4068 (cur->children->children != NULL)) {
4069 nodeVPush(ctxt, cur);
4070 cur = cur->children->children;
4071 continue;
4072 }
4073 break;
4074 case XML_COMMENT_NODE:
4075 case XML_PI_NODE:
4076 case XML_TEXT_NODE:
4077 case XML_CDATA_SECTION_NODE:
4078 break;
4079 default:
4080 ret = 0;
4081 goto done;
4082 }
4083 /*
4084 * Switch to next element
4085 */
4086 cur = cur->next;
4087 while (cur == NULL) {
4088 cur = nodeVPop(ctxt);
4089 if (cur == NULL)
4090 break;
4091 cur = cur->next;
4092 }
4093 }
4094done:
4095 ctxt->nodeMax = 0;
4096 ctxt->nodeNr = 0;
4097 if (ctxt->nodeTab != NULL) {
4098 xmlFree(ctxt->nodeTab);
4099 ctxt->nodeTab = NULL;
4100 }
4101 return(ret);
4102}
4103
4104/**
Owen Taylor3473f882001-02-23 17:55:21 +00004105 * xmlValidateOneElement:
4106 * @ctxt: the validation context
4107 * @doc: a document instance
4108 * @elem: an element instance
4109 *
4110 * Try to validate a single element and it's attributes,
4111 * basically it does the following checks as described by the
4112 * XML-1.0 recommendation:
4113 * - [ VC: Element Valid ]
4114 * - [ VC: Required Attribute ]
4115 * Then call xmlValidateOneAttribute() for each attribute present.
4116 *
4117 * The ID/IDREF checkings are done separately
4118 *
4119 * returns 1 if valid or 0 otherwise
4120 */
4121
4122int
4123xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4124 xmlNodePtr elem) {
4125 xmlElementPtr elemDecl = NULL;
4126 xmlElementContentPtr cont;
4127 xmlAttributePtr attr;
4128 xmlNodePtr child;
4129 int ret = 1;
4130 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004131 const xmlChar *prefix = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004132
4133 CHECK_DTD;
4134
4135 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004136 switch (elem->type) {
4137 case XML_ATTRIBUTE_NODE:
4138 VERROR(ctxt->userData,
4139 "Attribute element not expected here\n");
4140 return(0);
4141 case XML_TEXT_NODE:
4142 if (elem->children != NULL) {
4143 VERROR(ctxt->userData, "Text element has childs !\n");
4144 return(0);
4145 }
4146 if (elem->properties != NULL) {
4147 VERROR(ctxt->userData, "Text element has attributes !\n");
4148 return(0);
4149 }
4150 if (elem->ns != NULL) {
4151 VERROR(ctxt->userData, "Text element has namespace !\n");
4152 return(0);
4153 }
4154 if (elem->nsDef != NULL) {
4155 VERROR(ctxt->userData,
4156 "Text element carries namespace definitions !\n");
4157 return(0);
4158 }
4159 if (elem->content == NULL) {
4160 VERROR(ctxt->userData,
4161 "Text element has no content !\n");
4162 return(0);
4163 }
4164 return(1);
4165 case XML_XINCLUDE_START:
4166 case XML_XINCLUDE_END:
4167 return(1);
4168 case XML_CDATA_SECTION_NODE:
4169 case XML_ENTITY_REF_NODE:
4170 case XML_PI_NODE:
4171 case XML_COMMENT_NODE:
4172 return(1);
4173 case XML_ENTITY_NODE:
4174 VERROR(ctxt->userData,
4175 "Entity element not expected here\n");
4176 return(0);
4177 case XML_NOTATION_NODE:
4178 VERROR(ctxt->userData,
4179 "Notation element not expected here\n");
4180 return(0);
4181 case XML_DOCUMENT_NODE:
4182 case XML_DOCUMENT_TYPE_NODE:
4183 case XML_DOCUMENT_FRAG_NODE:
4184 VERROR(ctxt->userData,
4185 "Document element not expected here\n");
4186 return(0);
4187 case XML_HTML_DOCUMENT_NODE:
4188 VERROR(ctxt->userData,
4189 "\n");
4190 return(0);
4191 case XML_ELEMENT_NODE:
4192 break;
4193 default:
4194 VERROR(ctxt->userData,
4195 "unknown element type %d\n", elem->type);
4196 return(0);
4197 }
4198 if (elem->name == NULL) return(0);
4199
4200 /*
4201 * Fetch the declaration for the qualified name
4202 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004203 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4204 prefix = elem->ns->prefix;
4205
4206 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004207 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004208 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004209 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4210 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004211 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004212 }
4213
4214 /*
4215 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004216 * This is "non-strict" validation should be done on the
4217 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004218 */
4219 if (elemDecl == NULL) {
4220 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4221 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4222 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4223 }
4224 if (elemDecl == NULL) {
4225 VERROR(ctxt->userData, "No declaration for element %s\n",
4226 elem->name);
4227 return(0);
4228 }
4229
4230 /* Check taht the element content matches the definition */
4231 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004232 case XML_ELEMENT_TYPE_UNDEFINED:
4233 VERROR(ctxt->userData, "No declaration for element %s\n",
4234 elem->name);
4235 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004236 case XML_ELEMENT_TYPE_EMPTY:
4237 if (elem->children != NULL) {
4238 VERROR(ctxt->userData,
4239 "Element %s was declared EMPTY this one has content\n",
4240 elem->name);
4241 ret = 0;
4242 }
4243 break;
4244 case XML_ELEMENT_TYPE_ANY:
4245 /* I don't think anything is required then */
4246 break;
4247 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004248 /* simple case of declared as #PCDATA */
4249 if ((elemDecl->content != NULL) &&
4250 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4251 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4252 if (!ret) {
4253 VERROR(ctxt->userData,
4254 "Element %s was declared #PCDATA but contains non text nodes\n",
4255 elem->name);
4256 }
4257 break;
4258 }
Owen Taylor3473f882001-02-23 17:55:21 +00004259 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004260 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004261 while (child != NULL) {
4262 if (child->type == XML_ELEMENT_NODE) {
4263 name = child->name;
4264 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4265 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004266 snprintf((char *) qname, sizeof(qname), "%s:%s",
4267 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004268 qname[sizeof(qname) - 1] = 0;
4269 cont = elemDecl->content;
4270 while (cont != NULL) {
4271 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4272 if (xmlStrEqual(cont->name, qname)) break;
4273 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4274 (cont->c1 != NULL) &&
4275 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4276 if (xmlStrEqual(cont->c1->name, qname)) break;
4277 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4278 (cont->c1 == NULL) ||
4279 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4280 /* Internal error !!! */
4281 xmlGenericError(xmlGenericErrorContext,
4282 "Internal: MIXED struct bad\n");
4283 break;
4284 }
4285 cont = cont->c2;
4286 }
4287 if (cont != NULL)
4288 goto child_ok;
4289 }
4290 cont = elemDecl->content;
4291 while (cont != NULL) {
4292 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4293 if (xmlStrEqual(cont->name, name)) break;
4294 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4295 (cont->c1 != NULL) &&
4296 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4297 if (xmlStrEqual(cont->c1->name, name)) break;
4298 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4299 (cont->c1 == NULL) ||
4300 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4301 /* Internal error !!! */
4302 xmlGenericError(xmlGenericErrorContext,
4303 "Internal: MIXED struct bad\n");
4304 break;
4305 }
4306 cont = cont->c2;
4307 }
4308 if (cont == NULL) {
4309 VERROR(ctxt->userData,
4310 "Element %s is not declared in %s list of possible childs\n",
4311 name, elem->name);
4312 ret = 0;
4313 }
4314 }
4315child_ok:
4316 child = child->next;
4317 }
4318 break;
4319 case XML_ELEMENT_TYPE_ELEMENT:
4320 child = elem->children;
4321 cont = elemDecl->content;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004322 ret = xmlValidateElementContent(ctxt, child, elemDecl, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00004323 break;
4324 }
4325
4326 /* [ VC: Required Attribute ] */
4327 attr = elemDecl->attributes;
4328 while (attr != NULL) {
4329 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
4330 xmlAttrPtr attrib;
4331 int qualified = -1;
4332
4333 attrib = elem->properties;
4334 while (attrib != NULL) {
4335 if (xmlStrEqual(attrib->name, attr->name)) {
4336 if (attr->prefix != NULL) {
4337 xmlNsPtr nameSpace = attrib->ns;
4338
4339 if (nameSpace == NULL)
4340 nameSpace = elem->ns;
4341 /*
4342 * qualified names handling is problematic, having a
4343 * different prefix should be possible but DTDs don't
4344 * allow to define the URI instead of the prefix :-(
4345 */
4346 if (nameSpace == NULL) {
4347 if (qualified < 0)
4348 qualified = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004349 } else if (!xmlStrEqual(nameSpace->prefix,
4350 attr->prefix)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004351 if (qualified < 1)
4352 qualified = 1;
4353 } else
4354 goto found;
4355 } else {
4356 /*
4357 * We should allow applications to define namespaces
4358 * for their application even if the DTD doesn't
4359 * carry one, otherwise, basically we would always
4360 * break.
4361 */
4362 goto found;
4363 }
4364 }
4365 attrib = attrib->next;
4366 }
4367 if (qualified == -1) {
4368 if (attr->prefix == NULL) {
4369 VERROR(ctxt->userData,
4370 "Element %s doesn't carry attribute %s\n",
4371 elem->name, attr->name);
4372 ret = 0;
4373 } else {
4374 VERROR(ctxt->userData,
4375 "Element %s doesn't carry attribute %s:%s\n",
4376 elem->name, attr->prefix,attr->name);
4377 ret = 0;
4378 }
4379 } else if (qualified == 0) {
4380 VWARNING(ctxt->userData,
4381 "Element %s required attribute %s:%s has no prefix\n",
4382 elem->name, attr->prefix,attr->name);
4383 } else if (qualified == 1) {
4384 VWARNING(ctxt->userData,
4385 "Element %s required attribute %s:%s has different prefix\n",
4386 elem->name, attr->prefix,attr->name);
4387 }
4388 }
4389found:
4390 attr = attr->nexth;
4391 }
4392 return(ret);
4393}
4394
4395/**
4396 * xmlValidateRoot:
4397 * @ctxt: the validation context
4398 * @doc: a document instance
4399 *
4400 * Try to validate a the root element
4401 * basically it does the following check as described by the
4402 * XML-1.0 recommendation:
4403 * - [ VC: Root Element Type ]
4404 * it doesn't try to recurse or apply other check to the element
4405 *
4406 * returns 1 if valid or 0 otherwise
4407 */
4408
4409int
4410xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4411 xmlNodePtr root;
4412 if (doc == NULL) return(0);
4413
4414 root = xmlDocGetRootElement(doc);
4415 if ((root == NULL) || (root->name == NULL)) {
4416 VERROR(ctxt->userData, "Not valid: no root element\n");
4417 return(0);
4418 }
4419
4420 /*
4421 * When doing post validation against a separate DTD, those may
4422 * no internal subset has been generated
4423 */
4424 if ((doc->intSubset != NULL) &&
4425 (doc->intSubset->name != NULL)) {
4426 /*
4427 * Check first the document root against the NQName
4428 */
4429 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4430 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4431 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004432 snprintf((char *) qname, sizeof(qname), "%s:%s",
4433 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004434 qname[sizeof(qname) - 1] = 0;
4435 if (xmlStrEqual(doc->intSubset->name, qname))
4436 goto name_ok;
4437 }
4438 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4439 (xmlStrEqual(root->name, BAD_CAST "html")))
4440 goto name_ok;
4441 VERROR(ctxt->userData,
4442 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4443 root->name, doc->intSubset->name);
4444 return(0);
4445
4446 }
4447 }
4448name_ok:
4449 return(1);
4450}
4451
4452
4453/**
4454 * xmlValidateElement:
4455 * @ctxt: the validation context
4456 * @doc: a document instance
4457 * @elem: an element instance
4458 *
4459 * Try to validate the subtree under an element
4460 *
4461 * returns 1 if valid or 0 otherwise
4462 */
4463
4464int
4465xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4466 xmlNodePtr child;
4467 xmlAttrPtr attr;
4468 xmlChar *value;
4469 int ret = 1;
4470
4471 if (elem == NULL) return(0);
4472
4473 /*
4474 * XInclude elements were added after parsing in the infoset,
4475 * they don't really mean anything validation wise.
4476 */
4477 if ((elem->type == XML_XINCLUDE_START) ||
4478 (elem->type == XML_XINCLUDE_END))
4479 return(1);
4480
4481 CHECK_DTD;
4482
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004483 /*
4484 * Entities references have to be handled separately
4485 */
4486 if (elem->type == XML_ENTITY_REF_NODE) {
4487 return(1);
4488 }
4489
Owen Taylor3473f882001-02-23 17:55:21 +00004490 ret &= xmlValidateOneElement(ctxt, doc, elem);
4491 attr = elem->properties;
4492 while(attr != NULL) {
4493 value = xmlNodeListGetString(doc, attr->children, 0);
4494 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4495 if (value != NULL)
4496 xmlFree(value);
4497 attr= attr->next;
4498 }
4499 child = elem->children;
4500 while (child != NULL) {
4501 ret &= xmlValidateElement(ctxt, doc, child);
4502 child = child->next;
4503 }
4504
4505 return(ret);
4506}
4507
Daniel Veillard8730c562001-02-26 10:49:57 +00004508/**
4509 * xmlValidateRef:
4510 * @ref: A reference to be validated
4511 * @ctxt: Validation context
4512 * @name: Name of ID we are searching for
4513 *
4514 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004515static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004516xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004517 const xmlChar *name) {
4518 xmlAttrPtr id;
4519 xmlAttrPtr attr;
4520
4521 if (ref == NULL)
4522 return;
4523 attr = ref->attr;
4524 if (attr == NULL)
4525 return;
4526 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4527 id = xmlGetID(ctxt->doc, name);
4528 if (id == NULL) {
4529 VERROR(ctxt->userData,
4530 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4531 attr->name, name);
4532 ctxt->valid = 0;
4533 }
4534 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4535 xmlChar *dup, *str = NULL, *cur, save;
4536
4537 dup = xmlStrdup(name);
4538 if (dup == NULL) {
4539 ctxt->valid = 0;
4540 return;
4541 }
4542 cur = dup;
4543 while (*cur != 0) {
4544 str = cur;
4545 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4546 save = *cur;
4547 *cur = 0;
4548 id = xmlGetID(ctxt->doc, str);
4549 if (id == NULL) {
4550 VERROR(ctxt->userData,
4551 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4552 attr->name, str);
4553 ctxt->valid = 0;
4554 }
4555 if (save == 0)
4556 break;
4557 *cur = save;
4558 while (IS_BLANK(*cur)) cur++;
4559 }
4560 xmlFree(dup);
4561 }
4562}
4563
4564/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004565 * xmlWalkValidateList:
4566 * @data: Contents of current link
4567 * @user: Value supplied by the user
4568 *
4569 * Return 0 to abort the walk or 1 to continue
4570 */
4571static int
4572xmlWalkValidateList(const void *data, const void *user)
4573{
4574 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4575 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4576 return 1;
4577}
4578
4579/**
4580 * xmlValidateCheckRefCallback:
4581 * @ref_list: List of references
4582 * @ctxt: Validation context
4583 * @name: Name of ID we are searching for
4584 *
4585 */
4586static void
4587xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4588 const xmlChar *name) {
4589 xmlValidateMemo memo;
4590
4591 if (ref_list == NULL)
4592 return;
4593 memo.ctxt = ctxt;
4594 memo.name = name;
4595
4596 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4597
4598}
4599
4600/**
Owen Taylor3473f882001-02-23 17:55:21 +00004601 * xmlValidateDocumentFinal:
4602 * @ctxt: the validation context
4603 * @doc: a document instance
4604 *
4605 * Does the final step for the document validation once all the
4606 * incremental validation steps have been completed
4607 *
4608 * basically it does the following checks described by the XML Rec
4609 *
4610 *
4611 * returns 1 if valid or 0 otherwise
4612 */
4613
4614int
4615xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4616 xmlRefTablePtr table;
4617
4618 if (doc == NULL) {
4619 xmlGenericError(xmlGenericErrorContext,
4620 "xmlValidateDocumentFinal: doc == NULL\n");
4621 return(0);
4622 }
4623
4624 /*
4625 * Check all the NOTATION/NOTATIONS attributes
4626 */
4627 /*
4628 * Check all the ENTITY/ENTITIES attributes definition for validity
4629 */
4630 /*
4631 * Check all the IDREF/IDREFS attributes definition for validity
4632 */
4633 table = (xmlRefTablePtr) doc->refs;
4634 ctxt->doc = doc;
4635 ctxt->valid = 1;
4636 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4637 return(ctxt->valid);
4638}
4639
4640/**
4641 * xmlValidateDtd:
4642 * @ctxt: the validation context
4643 * @doc: a document instance
4644 * @dtd: a dtd instance
4645 *
4646 * Try to validate the document against the dtd instance
4647 *
4648 * basically it does check all the definitions in the DtD.
4649 *
4650 * returns 1 if valid or 0 otherwise
4651 */
4652
4653int
4654xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4655 int ret;
4656 xmlDtdPtr oldExt;
4657 xmlNodePtr root;
4658
4659 if (dtd == NULL) return(0);
4660 if (doc == NULL) return(0);
4661 oldExt = doc->extSubset;
4662 doc->extSubset = dtd;
4663 ret = xmlValidateRoot(ctxt, doc);
4664 if (ret == 0) {
4665 doc->extSubset = oldExt;
4666 return(ret);
4667 }
4668 if (doc->ids != NULL) {
4669 xmlFreeIDTable(doc->ids);
4670 doc->ids = NULL;
4671 }
4672 if (doc->refs != NULL) {
4673 xmlFreeRefTable(doc->refs);
4674 doc->refs = NULL;
4675 }
4676 root = xmlDocGetRootElement(doc);
4677 ret = xmlValidateElement(ctxt, doc, root);
4678 ret &= xmlValidateDocumentFinal(ctxt, doc);
4679 doc->extSubset = oldExt;
4680 return(ret);
4681}
4682
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004683static void
Owen Taylor3473f882001-02-23 17:55:21 +00004684xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004685 const xmlChar *name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004686 if (cur == NULL)
4687 return;
4688 switch (cur->atype) {
4689 case XML_ATTRIBUTE_CDATA:
4690 case XML_ATTRIBUTE_ID:
4691 case XML_ATTRIBUTE_IDREF :
4692 case XML_ATTRIBUTE_IDREFS:
4693 case XML_ATTRIBUTE_NMTOKEN:
4694 case XML_ATTRIBUTE_NMTOKENS:
4695 case XML_ATTRIBUTE_ENUMERATION:
4696 break;
4697 case XML_ATTRIBUTE_ENTITY:
4698 case XML_ATTRIBUTE_ENTITIES:
4699 case XML_ATTRIBUTE_NOTATION:
4700 if (cur->defaultValue != NULL) {
4701 ctxt->valid &= xmlValidateAttributeValue2(ctxt, ctxt->doc,
4702 cur->name, cur->atype, cur->defaultValue);
4703 }
4704 if (cur->tree != NULL) {
4705 xmlEnumerationPtr tree = cur->tree;
4706 while (tree != NULL) {
4707 ctxt->valid &= xmlValidateAttributeValue2(ctxt, ctxt->doc,
4708 cur->name, cur->atype, tree->name);
4709 tree = tree->next;
4710 }
4711 }
4712 }
4713}
4714
4715/**
4716 * xmlValidateDtdFinal:
4717 * @ctxt: the validation context
4718 * @doc: a document instance
4719 *
4720 * Does the final step for the dtds validation once all the
4721 * subsets have been parsed
4722 *
4723 * basically it does the following checks described by the XML Rec
4724 * - check that ENTITY and ENTITIES type attributes default or
4725 * possible values matches one of the defined entities.
4726 * - check that NOTATION type attributes default or
4727 * possible values matches one of the defined notations.
4728 *
4729 * returns 1 if valid or 0 otherwise
4730 */
4731
4732int
4733xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4734 int ret = 1;
4735 xmlDtdPtr dtd;
4736 xmlAttributeTablePtr table;
4737
4738 if (doc == NULL) return(0);
4739 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4740 return(0);
4741 ctxt->doc = doc;
4742 ctxt->valid = ret;
4743 dtd = doc->intSubset;
4744 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4745 table = (xmlAttributeTablePtr) dtd->attributes;
4746 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
4747 }
4748 dtd = doc->extSubset;
4749 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4750 table = (xmlAttributeTablePtr) dtd->attributes;
4751 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
4752 }
4753 return(ctxt->valid);
4754}
4755
4756/**
4757 * xmlValidateDocument:
4758 * @ctxt: the validation context
4759 * @doc: a document instance
4760 *
4761 * Try to validate the document instance
4762 *
4763 * basically it does the all the checks described by the XML Rec
4764 * i.e. validates the internal and external subset (if present)
4765 * and validate the document tree.
4766 *
4767 * returns 1 if valid or 0 otherwise
4768 */
4769
4770int
4771xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4772 int ret;
4773 xmlNodePtr root;
4774
4775 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4776 return(0);
4777 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
4778 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
4779 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
4780 doc->intSubset->SystemID);
4781 if (doc->extSubset == NULL) {
4782 if (doc->intSubset->SystemID != NULL) {
4783 VERROR(ctxt->userData,
4784 "Could not load the external subset \"%s\"\n",
4785 doc->intSubset->SystemID);
4786 } else {
4787 VERROR(ctxt->userData,
4788 "Could not load the external subset \"%s\"\n",
4789 doc->intSubset->ExternalID);
4790 }
4791 return(0);
4792 }
4793 }
4794
4795 if (doc->ids != NULL) {
4796 xmlFreeIDTable(doc->ids);
4797 doc->ids = NULL;
4798 }
4799 if (doc->refs != NULL) {
4800 xmlFreeRefTable(doc->refs);
4801 doc->refs = NULL;
4802 }
4803 ret = xmlValidateDtdFinal(ctxt, doc);
4804 if (!xmlValidateRoot(ctxt, doc)) return(0);
4805
4806 root = xmlDocGetRootElement(doc);
4807 ret &= xmlValidateElement(ctxt, doc, root);
4808 ret &= xmlValidateDocumentFinal(ctxt, doc);
4809 return(ret);
4810}
4811
4812
4813/************************************************************************
4814 * *
4815 * Routines for dynamic validation editing *
4816 * *
4817 ************************************************************************/
4818
4819/**
4820 * xmlValidGetPotentialChildren:
4821 * @ctree: an element content tree
4822 * @list: an array to store the list of child names
4823 * @len: a pointer to the number of element in the list
4824 * @max: the size of the array
4825 *
4826 * Build/extend a list of potential children allowed by the content tree
4827 *
4828 * returns the number of element in the list, or -1 in case of error.
4829 */
4830
4831int
4832xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
4833 int *len, int max) {
4834 int i;
4835
4836 if ((ctree == NULL) || (list == NULL) || (len == NULL))
4837 return(-1);
4838 if (*len >= max) return(*len);
4839
4840 switch (ctree->type) {
4841 case XML_ELEMENT_CONTENT_PCDATA:
4842 for (i = 0; i < *len;i++)
4843 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
4844 list[(*len)++] = BAD_CAST "#PCDATA";
4845 break;
4846 case XML_ELEMENT_CONTENT_ELEMENT:
4847 for (i = 0; i < *len;i++)
4848 if (xmlStrEqual(ctree->name, list[i])) return(*len);
4849 list[(*len)++] = ctree->name;
4850 break;
4851 case XML_ELEMENT_CONTENT_SEQ:
4852 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4853 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4854 break;
4855 case XML_ELEMENT_CONTENT_OR:
4856 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4857 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4858 break;
4859 }
4860
4861 return(*len);
4862}
4863
4864/**
4865 * xmlValidGetValidElements:
4866 * @prev: an element to insert after
4867 * @next: an element to insert next
4868 * @list: an array to store the list of child names
4869 * @max: the size of the array
4870 *
4871 * This function returns the list of authorized children to insert
4872 * within an existing tree while respecting the validity constraints
4873 * forced by the Dtd. The insertion point is defined using @prev and
4874 * @next in the following ways:
4875 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
4876 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
4877 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
4878 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
4879 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
4880 *
4881 * pointers to the element names are inserted at the beginning of the array
4882 * and do not need to be freed.
4883 *
4884 * returns the number of element in the list, or -1 in case of error. If
4885 * the function returns the value @max the caller is invited to grow the
4886 * receiving array and retry.
4887 */
4888
4889int
4890xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
4891 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004892 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004893 int nb_valid_elements = 0;
4894 const xmlChar *elements[256];
4895 int nb_elements = 0, i;
4896
4897 xmlNode *ref_node;
4898 xmlNode *parent;
4899 xmlNode *test_node;
4900
4901 xmlNode *prev_next;
4902 xmlNode *next_prev;
4903 xmlNode *parent_childs;
4904 xmlNode *parent_last;
4905
4906 xmlElement *element_desc;
4907
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004908 vctxt.userData = NULL;
4909 vctxt.error = NULL;
4910 vctxt.warning = NULL;
4911
Owen Taylor3473f882001-02-23 17:55:21 +00004912 if (prev == NULL && next == NULL)
4913 return(-1);
4914
4915 if (list == NULL) return(-1);
4916 if (max <= 0) return(-1);
4917
4918 nb_valid_elements = 0;
4919 ref_node = prev ? prev : next;
4920 parent = ref_node->parent;
4921
4922 /*
4923 * Retrieves the parent element declaration
4924 */
4925 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
4926 parent->name);
4927 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
4928 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
4929 parent->name);
4930 if (element_desc == NULL) return(-1);
4931
4932 /*
4933 * Do a backup of the current tree structure
4934 */
4935 prev_next = prev ? prev->next : NULL;
4936 next_prev = next ? next->prev : NULL;
4937 parent_childs = parent->children;
4938 parent_last = parent->last;
4939
4940 /*
4941 * Creates a dummy node and insert it into the tree
4942 */
4943 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
4944 test_node->doc = ref_node->doc;
4945 test_node->parent = parent;
4946 test_node->prev = prev;
4947 test_node->next = next;
4948
4949 if (prev) prev->next = test_node;
4950 else parent->children = test_node;
4951
4952 if (next) next->prev = test_node;
4953 else parent->last = test_node;
4954
4955 /*
4956 * Insert each potential child node and check if the parent is
4957 * still valid
4958 */
4959 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
4960 elements, &nb_elements, 256);
4961
4962 for (i = 0;i < nb_elements;i++) {
4963 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004964 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004965 int j;
4966
4967 for (j = 0; j < nb_valid_elements;j++)
4968 if (xmlStrEqual(elements[i], list[j])) break;
4969 list[nb_valid_elements++] = elements[i];
4970 if (nb_valid_elements >= max) break;
4971 }
4972 }
4973
4974 /*
4975 * Restore the tree structure
4976 */
4977 if (prev) prev->next = prev_next;
4978 if (next) next->prev = next_prev;
4979 parent->children = parent_childs;
4980 parent->last = parent_last;
4981
4982 return(nb_valid_elements);
4983}