blob: bb0bf8b853d08e5b83405ed72f46c7fe4f5f9a3f [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 Veillardcbaf3992001-12-31 16:16:02 +000075 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +000076 * 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 */
Daniel Veillardcbaf3992001-12-31 16:16:02 +000088 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +000089 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
Daniel Veillard5344c602001-12-31 16:37:34 +0000100#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
101#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000102
Daniel Veillard5344c602001-12-31 16:37:34 +0000103#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
104#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000105
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
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000305/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000306
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
Daniel Veillarda10efa82001-04-18 13:09:01 +0000318static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
319 int create);
Owen Taylor3473f882001-02-23 17:55:21 +0000320xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
321
322/************************************************************************
323 * *
324 * QName handling helper *
325 * *
326 ************************************************************************/
327
328/**
329 * xmlSplitQName2:
330 * @name: an XML parser context
331 * @prefix: a xmlChar **
332 *
333 * parse an XML qualified name string
334 *
335 * [NS 5] QName ::= (Prefix ':')? LocalPart
336 *
337 * [NS 6] Prefix ::= NCName
338 *
339 * [NS 7] LocalPart ::= NCName
340 *
341 * Returns NULL if not a QName, otherwise the local part, and prefix
342 * is updated to get the Prefix if any.
343 */
344
345xmlChar *
346xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
347 int len = 0;
348 xmlChar *ret = NULL;
349
350 *prefix = NULL;
351
Daniel Veillardf4309d72001-10-02 09:28:58 +0000352#ifndef XML_XML_NAMESPACE
Owen Taylor3473f882001-02-23 17:55:21 +0000353 /* xml: prefix is not really a namespace */
354 if ((name[0] == 'x') && (name[1] == 'm') &&
355 (name[2] == 'l') && (name[3] == ':'))
356 return(NULL);
Daniel Veillardf4309d72001-10-02 09:28:58 +0000357#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000358
359 /* nasty but valid */
360 if (name[0] == ':')
361 return(NULL);
362
363 /*
364 * we are not trying to validate but just to cut, and yes it will
365 * work even if this is as set of UTF-8 encoded chars
366 */
367 while ((name[len] != 0) && (name[len] != ':'))
368 len++;
369
370 if (name[len] == 0)
371 return(NULL);
372
373 *prefix = xmlStrndup(name, len);
374 ret = xmlStrdup(&name[len + 1]);
375
376 return(ret);
377}
378
379/****************************************************************
380 * *
381 * Util functions for data allocation/deallocation *
382 * *
383 ****************************************************************/
384
385/**
386 * xmlNewElementContent:
387 * @name: the subelement name or NULL
388 * @type: the type of element content decl
389 *
390 * Allocate an element content structure.
391 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000392 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000393 */
394xmlElementContentPtr
395xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
396 xmlElementContentPtr ret;
397
398 switch(type) {
399 case XML_ELEMENT_CONTENT_ELEMENT:
400 if (name == NULL) {
401 xmlGenericError(xmlGenericErrorContext,
402 "xmlNewElementContent : name == NULL !\n");
403 }
404 break;
405 case XML_ELEMENT_CONTENT_PCDATA:
406 case XML_ELEMENT_CONTENT_SEQ:
407 case XML_ELEMENT_CONTENT_OR:
408 if (name != NULL) {
409 xmlGenericError(xmlGenericErrorContext,
410 "xmlNewElementContent : name != NULL !\n");
411 }
412 break;
413 default:
414 xmlGenericError(xmlGenericErrorContext,
415 "xmlNewElementContent: unknown type %d\n", type);
416 return(NULL);
417 }
418 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
419 if (ret == NULL) {
420 xmlGenericError(xmlGenericErrorContext,
421 "xmlNewElementContent : out of memory!\n");
422 return(NULL);
423 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000424 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000425 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 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000725 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000726 */
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 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000824 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000825 */
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 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000853 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000854 */
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 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000871 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000872 */
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
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001136 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001137 *
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 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001235 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001236 */
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;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001299 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001300 }
1301
1302 /*
1303 * Create the Attribute table if needed.
1304 */
1305 table = (xmlAttributeTablePtr) dtd->attributes;
1306 if (table == NULL) {
1307 table = xmlCreateAttributeTable();
1308 dtd->attributes = (void *) table;
1309 }
1310 if (table == NULL) {
1311 xmlGenericError(xmlGenericErrorContext,
1312 "xmlAddAttributeDecl: Table creation failed!\n");
1313 return(NULL);
1314 }
1315
1316
1317 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1318 if (ret == NULL) {
1319 xmlGenericError(xmlGenericErrorContext,
1320 "xmlAddAttributeDecl: out of memory\n");
1321 return(NULL);
1322 }
1323 memset(ret, 0, sizeof(xmlAttribute));
1324 ret->type = XML_ATTRIBUTE_DECL;
1325
1326 /*
1327 * fill the structure.
1328 */
1329 ret->atype = type;
1330 ret->name = xmlStrdup(name);
1331 ret->prefix = xmlStrdup(ns);
1332 ret->elem = xmlStrdup(elem);
1333 ret->def = def;
1334 ret->tree = tree;
1335 if (defaultValue != NULL)
1336 ret->defaultValue = xmlStrdup(defaultValue);
1337
1338 /*
1339 * Validity Check:
1340 * Search the DTD for previous declarations of the ATTLIST
1341 */
1342 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1343 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001344 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001345 */
1346 VWARNING(ctxt->userData,
1347 "Attribute %s on %s: already defined\n",
1348 name, elem);
1349 xmlFreeAttribute(ret);
1350 return(NULL);
1351 }
1352
1353 /*
1354 * Validity Check:
1355 * Multiple ID per element
1356 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001357 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001358 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001359
Owen Taylor3473f882001-02-23 17:55:21 +00001360 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001361 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001362 VERROR(ctxt->userData,
1363 "Element %s has too may ID attributes defined : %s\n",
1364 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001365 ctxt->valid = 0;
1366 }
1367
Daniel Veillard48da9102001-08-07 01:10:10 +00001368 /*
1369 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001370 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001371 */
1372 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1373 ((ret->prefix != NULL &&
1374 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1375 ret->nexth = elemDef->attributes;
1376 elemDef->attributes = ret;
1377 } else {
1378 xmlAttributePtr tmp = elemDef->attributes;
1379
1380 while ((tmp != NULL) &&
1381 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1382 ((ret->prefix != NULL &&
1383 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1384 if (tmp->nexth == NULL)
1385 break;
1386 tmp = tmp->nexth;
1387 }
1388 if (tmp != NULL) {
1389 ret->nexth = tmp->nexth;
1390 tmp->nexth = ret;
1391 } else {
1392 ret->nexth = elemDef->attributes;
1393 elemDef->attributes = ret;
1394 }
1395 }
Owen Taylor3473f882001-02-23 17:55:21 +00001396 }
1397
1398 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001399 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001400 */
1401 ret->parent = dtd;
1402 ret->doc = dtd->doc;
1403 if (dtd->last == NULL) {
1404 dtd->children = dtd->last = (xmlNodePtr) ret;
1405 } else {
1406 dtd->last->next = (xmlNodePtr) ret;
1407 ret->prev = dtd->last;
1408 dtd->last = (xmlNodePtr) ret;
1409 }
1410 return(ret);
1411}
1412
1413/**
1414 * xmlFreeAttributeTable:
1415 * @table: An attribute table
1416 *
1417 * Deallocate the memory used by an entities hash table.
1418 */
1419void
1420xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1421 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1422}
1423
1424/**
1425 * xmlCopyAttribute:
1426 * @attr: An attribute
1427 *
1428 * Build a copy of an attribute.
1429 *
1430 * Returns the new xmlAttributePtr or NULL in case of error.
1431 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001432static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001433xmlCopyAttribute(xmlAttributePtr attr) {
1434 xmlAttributePtr cur;
1435
1436 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1437 if (cur == NULL) {
1438 xmlGenericError(xmlGenericErrorContext,
1439 "xmlCopyAttribute: out of memory !\n");
1440 return(NULL);
1441 }
1442 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001443 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001444 cur->atype = attr->atype;
1445 cur->def = attr->def;
1446 cur->tree = xmlCopyEnumeration(attr->tree);
1447 if (attr->elem != NULL)
1448 cur->elem = xmlStrdup(attr->elem);
1449 if (attr->name != NULL)
1450 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001451 if (attr->prefix != NULL)
1452 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001453 if (attr->defaultValue != NULL)
1454 cur->defaultValue = xmlStrdup(attr->defaultValue);
1455 return(cur);
1456}
1457
1458/**
1459 * xmlCopyAttributeTable:
1460 * @table: An attribute table
1461 *
1462 * Build a copy of an attribute table.
1463 *
1464 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1465 */
1466xmlAttributeTablePtr
1467xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1468 return((xmlAttributeTablePtr) xmlHashCopy(table,
1469 (xmlHashCopier) xmlCopyAttribute));
1470}
1471
1472/**
1473 * xmlDumpAttributeDecl:
1474 * @buf: the XML buffer output
1475 * @attr: An attribute declaration
1476 *
1477 * This will dump the content of the attribute declaration as an XML
1478 * DTD definition
1479 */
1480void
1481xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1482 xmlBufferWriteChar(buf, "<!ATTLIST ");
1483 xmlBufferWriteCHAR(buf, attr->elem);
1484 xmlBufferWriteChar(buf, " ");
1485 if (attr->prefix != NULL) {
1486 xmlBufferWriteCHAR(buf, attr->prefix);
1487 xmlBufferWriteChar(buf, ":");
1488 }
1489 xmlBufferWriteCHAR(buf, attr->name);
1490 switch (attr->atype) {
1491 case XML_ATTRIBUTE_CDATA:
1492 xmlBufferWriteChar(buf, " CDATA");
1493 break;
1494 case XML_ATTRIBUTE_ID:
1495 xmlBufferWriteChar(buf, " ID");
1496 break;
1497 case XML_ATTRIBUTE_IDREF:
1498 xmlBufferWriteChar(buf, " IDREF");
1499 break;
1500 case XML_ATTRIBUTE_IDREFS:
1501 xmlBufferWriteChar(buf, " IDREFS");
1502 break;
1503 case XML_ATTRIBUTE_ENTITY:
1504 xmlBufferWriteChar(buf, " ENTITY");
1505 break;
1506 case XML_ATTRIBUTE_ENTITIES:
1507 xmlBufferWriteChar(buf, " ENTITIES");
1508 break;
1509 case XML_ATTRIBUTE_NMTOKEN:
1510 xmlBufferWriteChar(buf, " NMTOKEN");
1511 break;
1512 case XML_ATTRIBUTE_NMTOKENS:
1513 xmlBufferWriteChar(buf, " NMTOKENS");
1514 break;
1515 case XML_ATTRIBUTE_ENUMERATION:
1516 xmlBufferWriteChar(buf, " (");
1517 xmlDumpEnumeration(buf, attr->tree);
1518 break;
1519 case XML_ATTRIBUTE_NOTATION:
1520 xmlBufferWriteChar(buf, " NOTATION (");
1521 xmlDumpEnumeration(buf, attr->tree);
1522 break;
1523 default:
1524 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001525 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001526 attr->atype);
1527 }
1528 switch (attr->def) {
1529 case XML_ATTRIBUTE_NONE:
1530 break;
1531 case XML_ATTRIBUTE_REQUIRED:
1532 xmlBufferWriteChar(buf, " #REQUIRED");
1533 break;
1534 case XML_ATTRIBUTE_IMPLIED:
1535 xmlBufferWriteChar(buf, " #IMPLIED");
1536 break;
1537 case XML_ATTRIBUTE_FIXED:
1538 xmlBufferWriteChar(buf, " #FIXED");
1539 break;
1540 default:
1541 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001542 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001543 attr->def);
1544 }
1545 if (attr->defaultValue != NULL) {
1546 xmlBufferWriteChar(buf, " ");
1547 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1548 }
1549 xmlBufferWriteChar(buf, ">\n");
1550}
1551
1552/**
1553 * xmlDumpAttributeTable:
1554 * @buf: the XML buffer output
1555 * @table: An attribute table
1556 *
1557 * This will dump the content of the attribute table as an XML DTD definition
1558 */
1559void
1560xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1561 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1562}
1563
1564/************************************************************************
1565 * *
1566 * NOTATIONs *
1567 * *
1568 ************************************************************************/
1569/**
1570 * xmlCreateNotationTable:
1571 *
1572 * create and initialize an empty notation hash table.
1573 *
1574 * Returns the xmlNotationTablePtr just created or NULL in case
1575 * of error.
1576 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001577static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001578xmlCreateNotationTable(void) {
1579 return(xmlHashCreate(0));
1580}
1581
1582/**
1583 * xmlFreeNotation:
1584 * @not: A notation
1585 *
1586 * Deallocate the memory used by an notation definition
1587 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001588static void
Owen Taylor3473f882001-02-23 17:55:21 +00001589xmlFreeNotation(xmlNotationPtr nota) {
1590 if (nota == NULL) return;
1591 if (nota->name != NULL)
1592 xmlFree((xmlChar *) nota->name);
1593 if (nota->PublicID != NULL)
1594 xmlFree((xmlChar *) nota->PublicID);
1595 if (nota->SystemID != NULL)
1596 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001597 xmlFree(nota);
1598}
1599
1600
1601/**
1602 * xmlAddNotationDecl:
1603 * @dtd: pointer to the DTD
1604 * @ctxt: the validation context
1605 * @name: the entity name
1606 * @PublicID: the public identifier or NULL
1607 * @SystemID: the system identifier or NULL
1608 *
1609 * Register a new notation declaration
1610 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001611 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001612 */
1613xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001614xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001615 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001616 const xmlChar *PublicID, const xmlChar *SystemID) {
1617 xmlNotationPtr ret;
1618 xmlNotationTablePtr table;
1619
1620 if (dtd == NULL) {
1621 xmlGenericError(xmlGenericErrorContext,
1622 "xmlAddNotationDecl: dtd == NULL\n");
1623 return(NULL);
1624 }
1625 if (name == NULL) {
1626 xmlGenericError(xmlGenericErrorContext,
1627 "xmlAddNotationDecl: name == NULL\n");
1628 return(NULL);
1629 }
1630 if ((PublicID == NULL) && (SystemID == NULL)) {
1631 xmlGenericError(xmlGenericErrorContext,
1632 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00001633 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001634 }
1635
1636 /*
1637 * Create the Notation table if needed.
1638 */
1639 table = (xmlNotationTablePtr) dtd->notations;
1640 if (table == NULL)
1641 dtd->notations = table = xmlCreateNotationTable();
1642 if (table == NULL) {
1643 xmlGenericError(xmlGenericErrorContext,
1644 "xmlAddNotationDecl: Table creation failed!\n");
1645 return(NULL);
1646 }
1647
1648 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1649 if (ret == NULL) {
1650 xmlGenericError(xmlGenericErrorContext,
1651 "xmlAddNotationDecl: out of memory\n");
1652 return(NULL);
1653 }
1654 memset(ret, 0, sizeof(xmlNotation));
1655
1656 /*
1657 * fill the structure.
1658 */
1659 ret->name = xmlStrdup(name);
1660 if (SystemID != NULL)
1661 ret->SystemID = xmlStrdup(SystemID);
1662 if (PublicID != NULL)
1663 ret->PublicID = xmlStrdup(PublicID);
1664
1665 /*
1666 * Validity Check:
1667 * Check the DTD for previous declarations of the ATTLIST
1668 */
1669 if (xmlHashAddEntry(table, name, ret)) {
1670 xmlGenericError(xmlGenericErrorContext,
1671 "xmlAddNotationDecl: %s already defined\n", name);
1672 xmlFreeNotation(ret);
1673 return(NULL);
1674 }
1675 return(ret);
1676}
1677
1678/**
1679 * xmlFreeNotationTable:
1680 * @table: An notation table
1681 *
1682 * Deallocate the memory used by an entities hash table.
1683 */
1684void
1685xmlFreeNotationTable(xmlNotationTablePtr table) {
1686 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
1687}
1688
1689/**
1690 * xmlCopyNotation:
1691 * @nota: A notation
1692 *
1693 * Build a copy of a notation.
1694 *
1695 * Returns the new xmlNotationPtr or NULL in case of error.
1696 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001697static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001698xmlCopyNotation(xmlNotationPtr nota) {
1699 xmlNotationPtr cur;
1700
1701 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1702 if (cur == NULL) {
1703 xmlGenericError(xmlGenericErrorContext,
1704 "xmlCopyNotation: out of memory !\n");
1705 return(NULL);
1706 }
1707 if (nota->name != NULL)
1708 cur->name = xmlStrdup(nota->name);
1709 else
1710 cur->name = NULL;
1711 if (nota->PublicID != NULL)
1712 cur->PublicID = xmlStrdup(nota->PublicID);
1713 else
1714 cur->PublicID = NULL;
1715 if (nota->SystemID != NULL)
1716 cur->SystemID = xmlStrdup(nota->SystemID);
1717 else
1718 cur->SystemID = NULL;
1719 return(cur);
1720}
1721
1722/**
1723 * xmlCopyNotationTable:
1724 * @table: A notation table
1725 *
1726 * Build a copy of a notation table.
1727 *
1728 * Returns the new xmlNotationTablePtr or NULL in case of error.
1729 */
1730xmlNotationTablePtr
1731xmlCopyNotationTable(xmlNotationTablePtr table) {
1732 return((xmlNotationTablePtr) xmlHashCopy(table,
1733 (xmlHashCopier) xmlCopyNotation));
1734}
1735
1736/**
1737 * xmlDumpNotationDecl:
1738 * @buf: the XML buffer output
1739 * @nota: A notation declaration
1740 *
1741 * This will dump the content the notation declaration as an XML DTD definition
1742 */
1743void
1744xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
1745 xmlBufferWriteChar(buf, "<!NOTATION ");
1746 xmlBufferWriteCHAR(buf, nota->name);
1747 if (nota->PublicID != NULL) {
1748 xmlBufferWriteChar(buf, " PUBLIC ");
1749 xmlBufferWriteQuotedString(buf, nota->PublicID);
1750 if (nota->SystemID != NULL) {
1751 xmlBufferWriteChar(buf, " ");
1752 xmlBufferWriteCHAR(buf, nota->SystemID);
1753 }
1754 } else {
1755 xmlBufferWriteChar(buf, " SYSTEM ");
1756 xmlBufferWriteCHAR(buf, nota->SystemID);
1757 }
1758 xmlBufferWriteChar(buf, " >\n");
1759}
1760
1761/**
1762 * xmlDumpNotationTable:
1763 * @buf: the XML buffer output
1764 * @table: A notation table
1765 *
1766 * This will dump the content of the notation table as an XML DTD definition
1767 */
1768void
1769xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
1770 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
1771}
1772
1773/************************************************************************
1774 * *
1775 * IDs *
1776 * *
1777 ************************************************************************/
1778/**
1779 * xmlCreateIDTable:
1780 *
1781 * create and initialize an empty id hash table.
1782 *
1783 * Returns the xmlIDTablePtr just created or NULL in case
1784 * of error.
1785 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001786static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001787xmlCreateIDTable(void) {
1788 return(xmlHashCreate(0));
1789}
1790
1791/**
1792 * xmlFreeID:
1793 * @not: A id
1794 *
1795 * Deallocate the memory used by an id definition
1796 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001797static void
Owen Taylor3473f882001-02-23 17:55:21 +00001798xmlFreeID(xmlIDPtr id) {
1799 if (id == NULL) return;
1800 if (id->value != NULL)
1801 xmlFree((xmlChar *) id->value);
Owen Taylor3473f882001-02-23 17:55:21 +00001802 xmlFree(id);
1803}
1804
1805/**
1806 * xmlAddID:
1807 * @ctxt: the validation context
1808 * @doc: pointer to the document
1809 * @value: the value name
1810 * @attr: the attribute holding the ID
1811 *
1812 * Register a new id declaration
1813 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001814 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001815 */
1816xmlIDPtr
1817xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
1818 xmlAttrPtr attr) {
1819 xmlIDPtr ret;
1820 xmlIDTablePtr table;
1821
1822 if (doc == NULL) {
1823 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001824 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001825 return(NULL);
1826 }
1827 if (value == NULL) {
1828 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001829 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001830 return(NULL);
1831 }
1832 if (attr == NULL) {
1833 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001834 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001835 return(NULL);
1836 }
1837
1838 /*
1839 * Create the ID table if needed.
1840 */
1841 table = (xmlIDTablePtr) doc->ids;
1842 if (table == NULL)
1843 doc->ids = table = xmlCreateIDTable();
1844 if (table == NULL) {
1845 xmlGenericError(xmlGenericErrorContext,
1846 "xmlAddID: Table creation failed!\n");
1847 return(NULL);
1848 }
1849
1850 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
1851 if (ret == NULL) {
1852 xmlGenericError(xmlGenericErrorContext,
1853 "xmlAddID: out of memory\n");
1854 return(NULL);
1855 }
1856
1857 /*
1858 * fill the structure.
1859 */
1860 ret->value = xmlStrdup(value);
1861 ret->attr = attr;
1862
1863 if (xmlHashAddEntry(table, value, ret) < 0) {
1864 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001865 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001866 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001867 if (ctxt != NULL)
1868 VERROR(ctxt->userData, "ID %s already defined\n", value);
Owen Taylor3473f882001-02-23 17:55:21 +00001869 xmlFreeID(ret);
1870 return(NULL);
1871 }
1872 return(ret);
1873}
1874
1875/**
1876 * xmlFreeIDTable:
1877 * @table: An id table
1878 *
1879 * Deallocate the memory used by an ID hash table.
1880 */
1881void
1882xmlFreeIDTable(xmlIDTablePtr table) {
1883 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
1884}
1885
1886/**
1887 * xmlIsID:
1888 * @doc: the document
1889 * @elem: the element carrying the attribute
1890 * @attr: the attribute
1891 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001892 * Determine whether an attribute is of type ID. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00001893 * then this is simple, otherwise we use an heuristic: name ID (upper
1894 * or lowercase).
1895 *
1896 * Returns 0 or 1 depending on the lookup result
1897 */
1898int
1899xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
1900 if (doc == NULL) return(0);
1901 if (attr == NULL) return(0);
1902 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
1903 return(0);
1904 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1905 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
1906 (xmlStrEqual(BAD_CAST "name", attr->name)))
1907 return(1);
1908 return(0);
1909 } else {
1910 xmlAttributePtr attrDecl;
1911
1912 if (elem == NULL) return(0);
1913 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
1914 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1915 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
1916 attr->name);
1917
1918 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
1919 return(1);
1920 }
1921 return(0);
1922}
1923
1924/**
1925 * xmlRemoveID
1926 * @doc: the document
1927 * @attr: the attribute
1928 *
1929 * Remove the given attribute from the ID table maintained internally.
1930 *
1931 * Returns -1 if the lookup failed and 0 otherwise
1932 */
1933int
1934xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
1935 xmlAttrPtr cur;
1936 xmlIDTablePtr table;
1937 xmlChar *ID;
1938
1939 if (doc == NULL) return(-1);
1940 if (attr == NULL) return(-1);
1941 table = (xmlIDTablePtr) doc->ids;
1942 if (table == NULL)
1943 return(-1);
1944
1945 if (attr == NULL)
1946 return(-1);
1947 ID = xmlNodeListGetString(doc, attr->children, 1);
1948 if (ID == NULL)
1949 return(-1);
1950 cur = xmlHashLookup(table, ID);
1951 if (cur != attr) {
1952 xmlFree(ID);
1953 return(-1);
1954 }
1955 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
1956 xmlFree(ID);
1957 return(0);
1958}
1959
1960/**
1961 * xmlGetID:
1962 * @doc: pointer to the document
1963 * @ID: the ID value
1964 *
1965 * Search the attribute declaring the given ID
1966 *
1967 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
1968 */
1969xmlAttrPtr
1970xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
1971 xmlIDTablePtr table;
1972 xmlIDPtr id;
1973
1974 if (doc == NULL) {
1975 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
1976 return(NULL);
1977 }
1978
1979 if (ID == NULL) {
1980 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
1981 return(NULL);
1982 }
1983
1984 table = (xmlIDTablePtr) doc->ids;
1985 if (table == NULL)
1986 return(NULL);
1987
1988 id = xmlHashLookup(table, ID);
1989 if (id == NULL)
1990 return(NULL);
1991 return(id->attr);
1992}
1993
1994/************************************************************************
1995 * *
1996 * Refs *
1997 * *
1998 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00001999typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002000{
2001 xmlListPtr l;
2002 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002003} xmlRemoveMemo;
2004
2005typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2006
2007typedef struct xmlValidateMemo_t
2008{
2009 xmlValidCtxtPtr ctxt;
2010 const xmlChar *name;
2011} xmlValidateMemo;
2012
2013typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002014
2015/**
2016 * xmlCreateRefTable:
2017 *
2018 * create and initialize an empty ref hash table.
2019 *
2020 * Returns the xmlRefTablePtr just created or NULL in case
2021 * of error.
2022 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002023static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002024xmlCreateRefTable(void) {
2025 return(xmlHashCreate(0));
2026}
2027
2028/**
2029 * xmlFreeRef:
2030 * @lk: A list link
2031 *
2032 * Deallocate the memory used by a ref definition
2033 */
2034static void
2035xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002036 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2037 if (ref == NULL) return;
2038 if (ref->value != NULL)
2039 xmlFree((xmlChar *)ref->value);
2040 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002041}
2042
2043/**
2044 * xmlFreeRefList:
2045 * @list_ref: A list of references.
2046 *
2047 * Deallocate the memory used by a list of references
2048 */
2049static void
2050xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002051 if (list_ref == NULL) return;
2052 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002053}
2054
2055/**
2056 * xmlWalkRemoveRef:
2057 * @data: Contents of current link
2058 * @user: Value supplied by the user
2059 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002060 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002061 */
2062static int
2063xmlWalkRemoveRef(const void *data, const void *user)
2064{
Daniel Veillard37721922001-05-04 15:21:12 +00002065 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2066 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2067 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002068
Daniel Veillard37721922001-05-04 15:21:12 +00002069 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2070 xmlListRemoveFirst(ref_list, (void *)data);
2071 return 0;
2072 }
2073 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002074}
2075
2076/**
2077 * xmlAddRef:
2078 * @ctxt: the validation context
2079 * @doc: pointer to the document
2080 * @value: the value name
2081 * @attr: the attribute holding the Ref
2082 *
2083 * Register a new ref declaration
2084 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002085 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002086 */
2087xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002088xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002089 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002090 xmlRefPtr ret;
2091 xmlRefTablePtr table;
2092 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002093
Daniel Veillard37721922001-05-04 15:21:12 +00002094 if (doc == NULL) {
2095 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002096 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002097 return(NULL);
2098 }
2099 if (value == NULL) {
2100 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002101 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002102 return(NULL);
2103 }
2104 if (attr == NULL) {
2105 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002106 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002107 return(NULL);
2108 }
Owen Taylor3473f882001-02-23 17:55:21 +00002109
Daniel Veillard37721922001-05-04 15:21:12 +00002110 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002111 * Create the Ref table if needed.
2112 */
Daniel Veillard37721922001-05-04 15:21:12 +00002113 table = (xmlRefTablePtr) doc->refs;
2114 if (table == NULL)
2115 doc->refs = table = xmlCreateRefTable();
2116 if (table == NULL) {
2117 xmlGenericError(xmlGenericErrorContext,
2118 "xmlAddRef: Table creation failed!\n");
2119 return(NULL);
2120 }
Owen Taylor3473f882001-02-23 17:55:21 +00002121
Daniel Veillard37721922001-05-04 15:21:12 +00002122 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2123 if (ret == NULL) {
2124 xmlGenericError(xmlGenericErrorContext,
2125 "xmlAddRef: out of memory\n");
2126 return(NULL);
2127 }
Owen Taylor3473f882001-02-23 17:55:21 +00002128
Daniel Veillard37721922001-05-04 15:21:12 +00002129 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002130 * fill the structure.
2131 */
Daniel Veillard37721922001-05-04 15:21:12 +00002132 ret->value = xmlStrdup(value);
2133 ret->attr = attr;
Owen Taylor3473f882001-02-23 17:55:21 +00002134
Daniel Veillard37721922001-05-04 15:21:12 +00002135 /* To add a reference :-
2136 * References are maintained as a list of references,
2137 * Lookup the entry, if no entry create new nodelist
2138 * Add the owning node to the NodeList
2139 * Return the ref
2140 */
Owen Taylor3473f882001-02-23 17:55:21 +00002141
Daniel Veillard37721922001-05-04 15:21:12 +00002142 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2143 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2144 xmlGenericError(xmlGenericErrorContext,
2145 "xmlAddRef: Reference list creation failed!\n");
2146 return(NULL);
2147 }
2148 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2149 xmlListDelete(ref_list);
2150 xmlGenericError(xmlGenericErrorContext,
2151 "xmlAddRef: Reference list insertion failed!\n");
2152 return(NULL);
2153 }
2154 }
2155 xmlListInsert(ref_list, ret);
2156 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002157}
2158
2159/**
2160 * xmlFreeRefTable:
2161 * @table: An ref table
2162 *
2163 * Deallocate the memory used by an Ref hash table.
2164 */
2165void
2166xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002167 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002168}
2169
2170/**
2171 * xmlIsRef:
2172 * @doc: the document
2173 * @elem: the element carrying the attribute
2174 * @attr: the attribute
2175 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002176 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002177 * then this is simple, otherwise we use an heuristic: name Ref (upper
2178 * or lowercase).
2179 *
2180 * Returns 0 or 1 depending on the lookup result
2181 */
2182int
2183xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002184 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2185 return(0);
2186 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2187 /* TODO @@@ */
2188 return(0);
2189 } else {
2190 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002191
Daniel Veillard37721922001-05-04 15:21:12 +00002192 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2193 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2194 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2195 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002196
Daniel Veillard37721922001-05-04 15:21:12 +00002197 if ((attrDecl != NULL) &&
2198 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2199 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2200 return(1);
2201 }
2202 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002203}
2204
2205/**
2206 * xmlRemoveRef
2207 * @doc: the document
2208 * @attr: the attribute
2209 *
2210 * Remove the given attribute from the Ref table maintained internally.
2211 *
2212 * Returns -1 if the lookup failed and 0 otherwise
2213 */
2214int
2215xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002216 xmlListPtr ref_list;
2217 xmlRefTablePtr table;
2218 xmlChar *ID;
2219 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002220
Daniel Veillard37721922001-05-04 15:21:12 +00002221 if (doc == NULL) return(-1);
2222 if (attr == NULL) return(-1);
2223 table = (xmlRefTablePtr) doc->refs;
2224 if (table == NULL)
2225 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002226
Daniel Veillard37721922001-05-04 15:21:12 +00002227 if (attr == NULL)
2228 return(-1);
2229 ID = xmlNodeListGetString(doc, attr->children, 1);
2230 if (ID == NULL)
2231 return(-1);
2232 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002233
Daniel Veillard37721922001-05-04 15:21:12 +00002234 if(ref_list == NULL) {
2235 xmlFree(ID);
2236 return (-1);
2237 }
2238 /* At this point, ref_list refers to a list of references which
2239 * have the same key as the supplied attr. Our list of references
2240 * is ordered by reference address and we don't have that information
2241 * here to use when removing. We'll have to walk the list and
2242 * check for a matching attribute, when we find one stop the walk
2243 * and remove the entry.
2244 * The list is ordered by reference, so that means we don't have the
2245 * key. Passing the list and the reference to the walker means we
2246 * will have enough data to be able to remove the entry.
2247 */
2248 target.l = ref_list;
2249 target.ap = attr;
2250
2251 /* Remove the supplied attr from our list */
2252 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002253
Daniel Veillard37721922001-05-04 15:21:12 +00002254 /*If the list is empty then remove the list entry in the hash */
2255 if (xmlListEmpty(ref_list))
2256 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2257 xmlFreeRefList);
2258 xmlFree(ID);
2259 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002260}
2261
2262/**
2263 * xmlGetRefs:
2264 * @doc: pointer to the document
2265 * @ID: the ID value
2266 *
2267 * Find the set of references for the supplied ID.
2268 *
2269 * Returns NULL if not found, otherwise node set for the ID.
2270 */
2271xmlListPtr
2272xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002273 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002274
Daniel Veillard37721922001-05-04 15:21:12 +00002275 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002276 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002277 return(NULL);
2278 }
Owen Taylor3473f882001-02-23 17:55:21 +00002279
Daniel Veillard37721922001-05-04 15:21:12 +00002280 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002281 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002282 return(NULL);
2283 }
Owen Taylor3473f882001-02-23 17:55:21 +00002284
Daniel Veillard37721922001-05-04 15:21:12 +00002285 table = (xmlRefTablePtr) doc->refs;
2286 if (table == NULL)
2287 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002288
Daniel Veillard37721922001-05-04 15:21:12 +00002289 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002290}
2291
2292/************************************************************************
2293 * *
2294 * Routines for validity checking *
2295 * *
2296 ************************************************************************/
2297
2298/**
2299 * xmlGetDtdElementDesc:
2300 * @dtd: a pointer to the DtD to search
2301 * @name: the element name
2302 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002303 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002304 *
2305 * returns the xmlElementPtr if found or NULL
2306 */
2307
2308xmlElementPtr
2309xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2310 xmlElementTablePtr table;
2311 xmlElementPtr cur;
2312 xmlChar *uqname = NULL, *prefix = NULL;
2313
2314 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002315 if (dtd->elements == NULL)
2316 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002317 table = (xmlElementTablePtr) dtd->elements;
2318
2319 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002320 if (uqname != NULL)
2321 name = uqname;
2322 cur = xmlHashLookup2(table, name, prefix);
2323 if (prefix != NULL) xmlFree(prefix);
2324 if (uqname != NULL) xmlFree(uqname);
2325 return(cur);
2326}
2327/**
2328 * xmlGetDtdElementDesc2:
2329 * @dtd: a pointer to the DtD to search
2330 * @name: the element name
2331 * @create: create an empty description if not found
2332 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002333 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002334 *
2335 * returns the xmlElementPtr if found or NULL
2336 */
2337
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002338static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002339xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2340 xmlElementTablePtr table;
2341 xmlElementPtr cur;
2342 xmlChar *uqname = NULL, *prefix = NULL;
2343
2344 if (dtd == NULL) return(NULL);
2345 if (dtd->elements == NULL) {
2346 if (!create)
2347 return(NULL);
2348 /*
2349 * Create the Element table if needed.
2350 */
2351 table = (xmlElementTablePtr) dtd->elements;
2352 if (table == NULL) {
2353 table = xmlCreateElementTable();
2354 dtd->elements = (void *) table;
2355 }
2356 if (table == NULL) {
2357 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002358 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002359 return(NULL);
2360 }
2361 }
2362 table = (xmlElementTablePtr) dtd->elements;
2363
2364 uqname = xmlSplitQName2(name, &prefix);
2365 if (uqname != NULL)
2366 name = uqname;
2367 cur = xmlHashLookup2(table, name, prefix);
2368 if ((cur == NULL) && (create)) {
2369 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2370 if (cur == NULL) {
2371 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002372 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002373 return(NULL);
2374 }
2375 memset(cur, 0, sizeof(xmlElement));
2376 cur->type = XML_ELEMENT_DECL;
2377
2378 /*
2379 * fill the structure.
2380 */
2381 cur->name = xmlStrdup(name);
2382 cur->prefix = xmlStrdup(prefix);
2383 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2384
2385 xmlHashAddEntry2(table, name, prefix, cur);
2386 }
2387 if (prefix != NULL) xmlFree(prefix);
2388 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002389 return(cur);
2390}
2391
2392/**
2393 * xmlGetDtdQElementDesc:
2394 * @dtd: a pointer to the DtD to search
2395 * @name: the element name
2396 * @prefix: the element namespace prefix
2397 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002398 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002399 *
2400 * returns the xmlElementPtr if found or NULL
2401 */
2402
Daniel Veillard48da9102001-08-07 01:10:10 +00002403xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002404xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2405 const xmlChar *prefix) {
2406 xmlElementTablePtr table;
2407
2408 if (dtd == NULL) return(NULL);
2409 if (dtd->elements == NULL) return(NULL);
2410 table = (xmlElementTablePtr) dtd->elements;
2411
2412 return(xmlHashLookup2(table, name, prefix));
2413}
2414
2415/**
2416 * xmlGetDtdAttrDesc:
2417 * @dtd: a pointer to the DtD to search
2418 * @elem: the element name
2419 * @name: the attribute name
2420 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002421 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002422 * this element.
2423 *
2424 * returns the xmlAttributePtr if found or NULL
2425 */
2426
2427xmlAttributePtr
2428xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2429 xmlAttributeTablePtr table;
2430 xmlAttributePtr cur;
2431 xmlChar *uqname = NULL, *prefix = NULL;
2432
2433 if (dtd == NULL) return(NULL);
2434 if (dtd->attributes == NULL) return(NULL);
2435
2436 table = (xmlAttributeTablePtr) dtd->attributes;
2437 if (table == NULL)
2438 return(NULL);
2439
2440 uqname = xmlSplitQName2(name, &prefix);
2441
2442 if (uqname != NULL) {
2443 cur = xmlHashLookup3(table, uqname, prefix, elem);
2444 if (prefix != NULL) xmlFree(prefix);
2445 if (uqname != NULL) xmlFree(uqname);
2446 } else
2447 cur = xmlHashLookup3(table, name, NULL, elem);
2448 return(cur);
2449}
2450
2451/**
2452 * xmlGetDtdQAttrDesc:
2453 * @dtd: a pointer to the DtD to search
2454 * @elem: the element name
2455 * @name: the attribute name
2456 * @prefix: the attribute namespace prefix
2457 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002458 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002459 * this element.
2460 *
2461 * returns the xmlAttributePtr if found or NULL
2462 */
2463
Daniel Veillard48da9102001-08-07 01:10:10 +00002464xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002465xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2466 const xmlChar *prefix) {
2467 xmlAttributeTablePtr table;
2468
2469 if (dtd == NULL) return(NULL);
2470 if (dtd->attributes == NULL) return(NULL);
2471 table = (xmlAttributeTablePtr) dtd->attributes;
2472
2473 return(xmlHashLookup3(table, name, prefix, elem));
2474}
2475
2476/**
2477 * xmlGetDtdNotationDesc:
2478 * @dtd: a pointer to the DtD to search
2479 * @name: the notation name
2480 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002481 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002482 *
2483 * returns the xmlNotationPtr if found or NULL
2484 */
2485
2486xmlNotationPtr
2487xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2488 xmlNotationTablePtr table;
2489
2490 if (dtd == NULL) return(NULL);
2491 if (dtd->notations == NULL) return(NULL);
2492 table = (xmlNotationTablePtr) dtd->notations;
2493
2494 return(xmlHashLookup(table, name));
2495}
2496
2497/**
2498 * xmlValidateNotationUse:
2499 * @ctxt: the validation context
2500 * @doc: the document
2501 * @notationName: the notation name to check
2502 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002503 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002504 * - [ VC: Notation Declared ]
2505 *
2506 * returns 1 if valid or 0 otherwise
2507 */
2508
2509int
2510xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2511 const xmlChar *notationName) {
2512 xmlNotationPtr notaDecl;
2513 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2514
2515 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2516 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2517 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2518
2519 if (notaDecl == NULL) {
2520 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2521 notationName);
2522 return(0);
2523 }
2524 return(1);
2525}
2526
2527/**
2528 * xmlIsMixedElement
2529 * @doc: the document
2530 * @name: the element name
2531 *
2532 * Search in the DtDs whether an element accept Mixed content (or ANY)
2533 * basically if it is supposed to accept text childs
2534 *
2535 * returns 0 if no, 1 if yes, and -1 if no element description is available
2536 */
2537
2538int
2539xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2540 xmlElementPtr elemDecl;
2541
2542 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2543
2544 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2545 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2546 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2547 if (elemDecl == NULL) return(-1);
2548 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002549 case XML_ELEMENT_TYPE_UNDEFINED:
2550 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002551 case XML_ELEMENT_TYPE_ELEMENT:
2552 return(0);
2553 case XML_ELEMENT_TYPE_EMPTY:
2554 /*
2555 * return 1 for EMPTY since we want VC error to pop up
2556 * on <empty> </empty> for example
2557 */
2558 case XML_ELEMENT_TYPE_ANY:
2559 case XML_ELEMENT_TYPE_MIXED:
2560 return(1);
2561 }
2562 return(1);
2563}
2564
2565/**
2566 * xmlValidateNameValue:
2567 * @value: an Name value
2568 *
2569 * Validate that the given value match Name production
2570 *
2571 * returns 1 if valid or 0 otherwise
2572 */
2573
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002574static int
Owen Taylor3473f882001-02-23 17:55:21 +00002575xmlValidateNameValue(const xmlChar *value) {
2576 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002577 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002578
2579 if (value == NULL) return(0);
2580 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002581 val = xmlStringCurrentChar(NULL, cur, &len);
2582 cur += len;
2583 if (!IS_LETTER(val) && (val != '_') &&
2584 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002585 return(0);
2586 }
2587
Daniel Veillardd8224e02002-01-13 15:43:22 +00002588 val = xmlStringCurrentChar(NULL, cur, &len);
2589 cur += len;
2590 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2591 (val == '.') || (val == '-') ||
2592 (val == '_') || (val == ':') ||
2593 (IS_COMBINING(val)) ||
2594 (IS_EXTENDER(val))) {
2595 val = xmlStringCurrentChar(NULL, cur, &len);
2596 cur += len;
2597 }
Owen Taylor3473f882001-02-23 17:55:21 +00002598
Daniel Veillardd8224e02002-01-13 15:43:22 +00002599 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002600
2601 return(1);
2602}
2603
2604/**
2605 * xmlValidateNamesValue:
2606 * @value: an Names value
2607 *
2608 * Validate that the given value match Names production
2609 *
2610 * returns 1 if valid or 0 otherwise
2611 */
2612
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002613static int
Owen Taylor3473f882001-02-23 17:55:21 +00002614xmlValidateNamesValue(const xmlChar *value) {
2615 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002616 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002617
2618 if (value == NULL) return(0);
2619 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002620 val = xmlStringCurrentChar(NULL, cur, &len);
2621 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002622
Daniel Veillardd8224e02002-01-13 15:43:22 +00002623 if (!IS_LETTER(val) && (val != '_') &&
2624 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002625 return(0);
2626 }
2627
Daniel Veillardd8224e02002-01-13 15:43:22 +00002628 val = xmlStringCurrentChar(NULL, cur, &len);
2629 cur += len;
2630 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2631 (val == '.') || (val == '-') ||
2632 (val == '_') || (val == ':') ||
2633 (IS_COMBINING(val)) ||
2634 (IS_EXTENDER(val))) {
2635 val = xmlStringCurrentChar(NULL, cur, &len);
2636 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002637 }
2638
Daniel Veillardd8224e02002-01-13 15:43:22 +00002639 while (IS_BLANK(val)) {
2640 while (IS_BLANK(val)) {
2641 val = xmlStringCurrentChar(NULL, cur, &len);
2642 cur += len;
2643 }
2644
2645 if (!IS_LETTER(val) && (val != '_') &&
2646 (val != ':')) {
2647 return(0);
2648 }
2649 val = xmlStringCurrentChar(NULL, cur, &len);
2650 cur += len;
2651
2652 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2653 (val == '.') || (val == '-') ||
2654 (val == '_') || (val == ':') ||
2655 (IS_COMBINING(val)) ||
2656 (IS_EXTENDER(val))) {
2657 val = xmlStringCurrentChar(NULL, cur, &len);
2658 cur += len;
2659 }
2660 }
2661
2662 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002663
2664 return(1);
2665}
2666
2667/**
2668 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002669 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00002670 *
2671 * Validate that the given value match Nmtoken production
2672 *
2673 * [ VC: Name Token ]
2674 *
2675 * returns 1 if valid or 0 otherwise
2676 */
2677
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002678static int
Owen Taylor3473f882001-02-23 17:55:21 +00002679xmlValidateNmtokenValue(const xmlChar *value) {
2680 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002681 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002682
2683 if (value == NULL) return(0);
2684 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002685 val = xmlStringCurrentChar(NULL, cur, &len);
2686 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002687
Daniel Veillardd8224e02002-01-13 15:43:22 +00002688 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2689 (val != '.') && (val != '-') &&
2690 (val != '_') && (val != ':') &&
2691 (!IS_COMBINING(val)) &&
2692 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00002693 return(0);
2694
Daniel Veillardd8224e02002-01-13 15:43:22 +00002695 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2696 (val == '.') || (val == '-') ||
2697 (val == '_') || (val == ':') ||
2698 (IS_COMBINING(val)) ||
2699 (IS_EXTENDER(val))) {
2700 val = xmlStringCurrentChar(NULL, cur, &len);
2701 cur += len;
2702 }
Owen Taylor3473f882001-02-23 17:55:21 +00002703
Daniel Veillardd8224e02002-01-13 15:43:22 +00002704 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002705
2706 return(1);
2707}
2708
2709/**
2710 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002711 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00002712 *
2713 * Validate that the given value match Nmtokens production
2714 *
2715 * [ VC: Name Token ]
2716 *
2717 * returns 1 if valid or 0 otherwise
2718 */
2719
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002720static int
Owen Taylor3473f882001-02-23 17:55:21 +00002721xmlValidateNmtokensValue(const xmlChar *value) {
2722 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002723 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002724
2725 if (value == NULL) return(0);
2726 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002727 val = xmlStringCurrentChar(NULL, cur, &len);
2728 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002729
Daniel Veillardd8224e02002-01-13 15:43:22 +00002730 while (IS_BLANK(val)) {
2731 val = xmlStringCurrentChar(NULL, cur, &len);
2732 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002733 }
2734
Daniel Veillardd8224e02002-01-13 15:43:22 +00002735 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2736 (val != '.') && (val != '-') &&
2737 (val != '_') && (val != ':') &&
2738 (!IS_COMBINING(val)) &&
2739 (!IS_EXTENDER(val)))
2740 return(0);
2741
2742 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2743 (val == '.') || (val == '-') ||
2744 (val == '_') || (val == ':') ||
2745 (IS_COMBINING(val)) ||
2746 (IS_EXTENDER(val))) {
2747 val = xmlStringCurrentChar(NULL, cur, &len);
2748 cur += len;
2749 }
2750
2751 while (IS_BLANK(val)) {
2752 while (IS_BLANK(val)) {
2753 val = xmlStringCurrentChar(NULL, cur, &len);
2754 cur += len;
2755 }
2756 if (val == 0) return(1);
2757
2758 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2759 (val != '.') && (val != '-') &&
2760 (val != '_') && (val != ':') &&
2761 (!IS_COMBINING(val)) &&
2762 (!IS_EXTENDER(val)))
2763 return(0);
2764
2765 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2766 (val == '.') || (val == '-') ||
2767 (val == '_') || (val == ':') ||
2768 (IS_COMBINING(val)) ||
2769 (IS_EXTENDER(val))) {
2770 val = xmlStringCurrentChar(NULL, cur, &len);
2771 cur += len;
2772 }
2773 }
2774
2775 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002776
2777 return(1);
2778}
2779
2780/**
2781 * xmlValidateNotationDecl:
2782 * @ctxt: the validation context
2783 * @doc: a document instance
2784 * @nota: a notation definition
2785 *
2786 * Try to validate a single notation definition
2787 * basically it does the following checks as described by the
2788 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002789 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00002790 * But this function get called anyway ...
2791 *
2792 * returns 1 if valid or 0 otherwise
2793 */
2794
2795int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002796xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
2797 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002798 int ret = 1;
2799
2800 return(ret);
2801}
2802
2803/**
2804 * xmlValidateAttributeValue:
2805 * @type: an attribute type
2806 * @value: an attribute value
2807 *
2808 * Validate that the given attribute value match the proper production
2809 *
2810 * [ VC: ID ]
2811 * Values of type ID must match the Name production....
2812 *
2813 * [ VC: IDREF ]
2814 * Values of type IDREF must match the Name production, and values
2815 * of type IDREFS must match Names ...
2816 *
2817 * [ VC: Entity Name ]
2818 * Values of type ENTITY must match the Name production, values
2819 * of type ENTITIES must match Names ...
2820 *
2821 * [ VC: Name Token ]
2822 * Values of type NMTOKEN must match the Nmtoken production; values
2823 * of type NMTOKENS must match Nmtokens.
2824 *
2825 * returns 1 if valid or 0 otherwise
2826 */
2827
2828int
2829xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
2830 switch (type) {
2831 case XML_ATTRIBUTE_ENTITIES:
2832 case XML_ATTRIBUTE_IDREFS:
2833 return(xmlValidateNamesValue(value));
2834 case XML_ATTRIBUTE_ENTITY:
2835 case XML_ATTRIBUTE_IDREF:
2836 case XML_ATTRIBUTE_ID:
2837 case XML_ATTRIBUTE_NOTATION:
2838 return(xmlValidateNameValue(value));
2839 case XML_ATTRIBUTE_NMTOKENS:
2840 case XML_ATTRIBUTE_ENUMERATION:
2841 return(xmlValidateNmtokensValue(value));
2842 case XML_ATTRIBUTE_NMTOKEN:
2843 return(xmlValidateNmtokenValue(value));
2844 case XML_ATTRIBUTE_CDATA:
2845 break;
2846 }
2847 return(1);
2848}
2849
2850/**
2851 * xmlValidateAttributeValue2:
2852 * @ctxt: the validation context
2853 * @doc: the document
2854 * @name: the attribute name (used for error reporting only)
2855 * @type: the attribute type
2856 * @value: the attribute value
2857 *
2858 * Validate that the given attribute value match a given type.
2859 * This typically cannot be done before having finished parsing
2860 * the subsets.
2861 *
2862 * [ VC: IDREF ]
2863 * Values of type IDREF must match one of the declared IDs
2864 * Values of type IDREFS must match a sequence of the declared IDs
2865 * each Name must match the value of an ID attribute on some element
2866 * in the XML document; i.e. IDREF values must match the value of
2867 * some ID attribute
2868 *
2869 * [ VC: Entity Name ]
2870 * Values of type ENTITY must match one declared entity
2871 * Values of type ENTITIES must match a sequence of declared entities
2872 *
2873 * [ VC: Notation Attributes ]
2874 * all notation names in the declaration must be declared.
2875 *
2876 * returns 1 if valid or 0 otherwise
2877 */
2878
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002879static int
Owen Taylor3473f882001-02-23 17:55:21 +00002880xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2881 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
2882 int ret = 1;
2883 switch (type) {
2884 case XML_ATTRIBUTE_IDREFS:
2885 case XML_ATTRIBUTE_IDREF:
2886 case XML_ATTRIBUTE_ID:
2887 case XML_ATTRIBUTE_NMTOKENS:
2888 case XML_ATTRIBUTE_ENUMERATION:
2889 case XML_ATTRIBUTE_NMTOKEN:
2890 case XML_ATTRIBUTE_CDATA:
2891 break;
2892 case XML_ATTRIBUTE_ENTITY: {
2893 xmlEntityPtr ent;
2894
2895 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00002896 if ((ent == NULL) && (doc->standalone == 1)) {
2897 doc->standalone = 0;
2898 ent = xmlGetDocEntity(doc, value);
2899 if (ent != NULL) {
2900 VERROR(ctxt->userData,
2901"standalone problem: attribute %s reference entity \"%s\" in external subset\n",
2902 name, value);
2903 /* WAIT to get answer from the Core WG on this
2904 ret = 0;
2905 */
2906 }
2907 }
Owen Taylor3473f882001-02-23 17:55:21 +00002908 if (ent == NULL) {
2909 VERROR(ctxt->userData,
2910 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2911 name, value);
2912 ret = 0;
2913 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2914 VERROR(ctxt->userData,
2915 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2916 name, value);
2917 ret = 0;
2918 }
2919 break;
2920 }
2921 case XML_ATTRIBUTE_ENTITIES: {
2922 xmlChar *dup, *nam = NULL, *cur, save;
2923 xmlEntityPtr ent;
2924
2925 dup = xmlStrdup(value);
2926 if (dup == NULL)
2927 return(0);
2928 cur = dup;
2929 while (*cur != 0) {
2930 nam = cur;
2931 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2932 save = *cur;
2933 *cur = 0;
2934 ent = xmlGetDocEntity(doc, nam);
2935 if (ent == NULL) {
2936 VERROR(ctxt->userData,
2937 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
2938 name, nam);
2939 ret = 0;
2940 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2941 VERROR(ctxt->userData,
2942 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
2943 name, nam);
2944 ret = 0;
2945 }
2946 if (save == 0)
2947 break;
2948 *cur = save;
2949 while (IS_BLANK(*cur)) cur++;
2950 }
2951 xmlFree(dup);
2952 break;
2953 }
2954 case XML_ATTRIBUTE_NOTATION: {
2955 xmlNotationPtr nota;
2956
2957 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
2958 if ((nota == NULL) && (doc->extSubset != NULL))
2959 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
2960
2961 if (nota == NULL) {
2962 VERROR(ctxt->userData,
2963 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
2964 name, value);
2965 ret = 0;
2966 }
2967 break;
2968 }
2969 }
2970 return(ret);
2971}
2972
2973/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00002974 * xmlValidCtxtNormalizeAttributeValue:
2975 * @ctxt: the validation context
2976 * @doc: the document
2977 * @elem: the parent
2978 * @name: the attribute name
2979 * @value: the attribute value
2980 * @ctxt: the validation context or NULL
2981 *
2982 * Does the validation related extra step of the normalization of attribute
2983 * values:
2984 *
2985 * If the declared value is not CDATA, then the XML processor must further
2986 * process the normalized attribute value by discarding any leading and
2987 * trailing space (#x20) characters, and by replacing sequences of space
2988 * (#x20) characters by single space (#x20) character.
2989 *
2990 * Also check VC: Standalone Document Declaration in P32, and update
2991 * ctxt->valid accordingly
2992 *
2993 * returns a new normalized string if normalization is needed, NULL otherwise
2994 * the caller must free the returned value.
2995 */
2996
2997xmlChar *
2998xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2999 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3000 xmlChar *ret, *dst;
3001 const xmlChar *src;
3002 xmlAttributePtr attrDecl = NULL;
3003 int extsubset = 0;
3004
3005 if (doc == NULL) return(NULL);
3006 if (elem == NULL) return(NULL);
3007 if (name == NULL) return(NULL);
3008 if (value == NULL) return(NULL);
3009
3010 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3011 xmlChar qname[500];
3012 snprintf((char *) qname, sizeof(qname), "%s:%s",
3013 elem->ns->prefix, elem->name);
3014 qname[sizeof(qname) - 1] = 0;
3015 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3016 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3017 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3018 if (attrDecl != NULL)
3019 extsubset = 1;
3020 }
3021 }
3022 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3023 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3024 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3025 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3026 if (attrDecl != NULL)
3027 extsubset = 1;
3028 }
3029
3030 if (attrDecl == NULL)
3031 return(NULL);
3032 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3033 return(NULL);
3034
3035 ret = xmlStrdup(value);
3036 if (ret == NULL)
3037 return(NULL);
3038 src = value;
3039 dst = ret;
3040 while (*src == 0x20) src++;
3041 while (*src != 0) {
3042 if (*src == 0x20) {
3043 while (*src == 0x20) src++;
3044 if (*src != 0)
3045 *dst++ = 0x20;
3046 } else {
3047 *dst++ = *src++;
3048 }
3049 }
3050 *dst = 0;
3051 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3052 VERROR(ctxt->userData,
3053"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3054 name, elem->name);
3055 ctxt->valid = 0;
3056 }
3057 return(ret);
3058}
3059
3060/**
Owen Taylor3473f882001-02-23 17:55:21 +00003061 * xmlValidNormalizeAttributeValue:
3062 * @doc: the document
3063 * @elem: the parent
3064 * @name: the attribute name
3065 * @value: the attribute value
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003066 * @ctxt: the validation context or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00003067 *
3068 * Does the validation related extra step of the normalization of attribute
3069 * values:
3070 *
3071 * If the declared value is not CDATA, then the XML processor must further
3072 * process the normalized attribute value by discarding any leading and
3073 * trailing space (#x20) characters, and by replacing sequences of space
3074 * (#x20) characters by single space (#x20) character.
3075 *
3076 * returns a new normalized string if normalization is needed, NULL otherwise
3077 * the caller must free the returned value.
3078 */
3079
3080xmlChar *
3081xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3082 const xmlChar *name, const xmlChar *value) {
3083 xmlChar *ret, *dst;
3084 const xmlChar *src;
3085 xmlAttributePtr attrDecl = NULL;
3086
3087 if (doc == NULL) return(NULL);
3088 if (elem == NULL) return(NULL);
3089 if (name == NULL) return(NULL);
3090 if (value == NULL) return(NULL);
3091
3092 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3093 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003094 snprintf((char *) qname, sizeof(qname), "%s:%s",
3095 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003096 qname[sizeof(qname) - 1] = 0;
3097 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3098 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3099 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3100 }
3101 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3102 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3103 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3104
3105 if (attrDecl == NULL)
3106 return(NULL);
3107 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3108 return(NULL);
3109
3110 ret = xmlStrdup(value);
3111 if (ret == NULL)
3112 return(NULL);
3113 src = value;
3114 dst = ret;
3115 while (*src == 0x20) src++;
3116 while (*src != 0) {
3117 if (*src == 0x20) {
3118 while (*src == 0x20) src++;
3119 if (*src != 0)
3120 *dst++ = 0x20;
3121 } else {
3122 *dst++ = *src++;
3123 }
3124 }
3125 *dst = 0;
3126 return(ret);
3127}
3128
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003129static void
Owen Taylor3473f882001-02-23 17:55:21 +00003130xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003131 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003132 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3133}
3134
3135/**
3136 * xmlValidateAttributeDecl:
3137 * @ctxt: the validation context
3138 * @doc: a document instance
3139 * @attr: an attribute definition
3140 *
3141 * Try to validate a single attribute definition
3142 * basically it does the following checks as described by the
3143 * XML-1.0 recommendation:
3144 * - [ VC: Attribute Default Legal ]
3145 * - [ VC: Enumeration ]
3146 * - [ VC: ID Attribute Default ]
3147 *
3148 * The ID/IDREF uniqueness and matching are done separately
3149 *
3150 * returns 1 if valid or 0 otherwise
3151 */
3152
3153int
3154xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3155 xmlAttributePtr attr) {
3156 int ret = 1;
3157 int val;
3158 CHECK_DTD;
3159 if(attr == NULL) return(1);
3160
3161 /* Attribute Default Legal */
3162 /* Enumeration */
3163 if (attr->defaultValue != NULL) {
3164 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3165 if (val == 0) {
3166 VERROR(ctxt->userData,
3167 "Syntax of default value for attribute %s on %s is not valid\n",
3168 attr->name, attr->elem);
3169 }
3170 ret &= val;
3171 }
3172
3173 /* ID Attribute Default */
3174 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3175 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3176 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3177 VERROR(ctxt->userData,
3178 "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n",
3179 attr->name, attr->elem);
3180 ret = 0;
3181 }
3182
3183 /* One ID per Element Type */
3184 if (attr->atype == XML_ATTRIBUTE_ID) {
3185 int nbId;
3186
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003187 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003188 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3189 attr->elem);
3190 if (elem != NULL) {
3191 nbId = xmlScanIDAttributeDecl(NULL, elem);
3192 } else {
3193 xmlAttributeTablePtr table;
3194
3195 /*
3196 * The attribute may be declared in the internal subset and the
3197 * element in the external subset.
3198 */
3199 nbId = 0;
3200 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3201 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3202 xmlValidateAttributeIdCallback, &nbId);
3203 }
3204 if (nbId > 1) {
3205 VERROR(ctxt->userData,
3206 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3207 attr->elem, nbId, attr->name);
3208 } else if (doc->extSubset != NULL) {
3209 int extId = 0;
3210 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3211 if (elem != NULL) {
3212 extId = xmlScanIDAttributeDecl(NULL, elem);
3213 }
3214 if (extId > 1) {
3215 VERROR(ctxt->userData,
3216 "Element %s has %d ID attribute defined in the external subset : %s\n",
3217 attr->elem, extId, attr->name);
3218 } else if (extId + nbId > 1) {
3219 VERROR(ctxt->userData,
3220"Element %s has ID attributes defined in the internal and external subset : %s\n",
3221 attr->elem, attr->name);
3222 }
3223 }
3224 }
3225
3226 /* Validity Constraint: Enumeration */
3227 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3228 xmlEnumerationPtr tree = attr->tree;
3229 while (tree != NULL) {
3230 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3231 tree = tree->next;
3232 }
3233 if (tree == NULL) {
3234 VERROR(ctxt->userData,
3235"Default value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3236 attr->defaultValue, attr->name, attr->elem);
3237 ret = 0;
3238 }
3239 }
3240
3241 return(ret);
3242}
3243
3244/**
3245 * xmlValidateElementDecl:
3246 * @ctxt: the validation context
3247 * @doc: a document instance
3248 * @elem: an element definition
3249 *
3250 * Try to validate a single element definition
3251 * basically it does the following checks as described by the
3252 * XML-1.0 recommendation:
3253 * - [ VC: One ID per Element Type ]
3254 * - [ VC: No Duplicate Types ]
3255 * - [ VC: Unique Element Type Declaration ]
3256 *
3257 * returns 1 if valid or 0 otherwise
3258 */
3259
3260int
3261xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3262 xmlElementPtr elem) {
3263 int ret = 1;
3264 xmlElementPtr tst;
3265
3266 CHECK_DTD;
3267
3268 if (elem == NULL) return(1);
3269
3270 /* No Duplicate Types */
3271 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3272 xmlElementContentPtr cur, next;
3273 const xmlChar *name;
3274
3275 cur = elem->content;
3276 while (cur != NULL) {
3277 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3278 if (cur->c1 == NULL) break;
3279 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3280 name = cur->c1->name;
3281 next = cur->c2;
3282 while (next != NULL) {
3283 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3284 if (xmlStrEqual(next->name, name)) {
3285 VERROR(ctxt->userData,
3286 "Definition of %s has duplicate references of %s\n",
3287 elem->name, name);
3288 ret = 0;
3289 }
3290 break;
3291 }
3292 if (next->c1 == NULL) break;
3293 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3294 if (xmlStrEqual(next->c1->name, name)) {
3295 VERROR(ctxt->userData,
3296 "Definition of %s has duplicate references of %s\n",
3297 elem->name, name);
3298 ret = 0;
3299 }
3300 next = next->c2;
3301 }
3302 }
3303 cur = cur->c2;
3304 }
3305 }
3306
3307 /* VC: Unique Element Type Declaration */
3308 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003309 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003310 ((tst->prefix == elem->prefix) ||
3311 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003312 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003313 VERROR(ctxt->userData, "Redefinition of element %s\n",
3314 elem->name);
3315 ret = 0;
3316 }
3317 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003318 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003319 ((tst->prefix == elem->prefix) ||
3320 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003321 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003322 VERROR(ctxt->userData, "Redefinition of element %s\n",
3323 elem->name);
3324 ret = 0;
3325 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003326 /* One ID per Element Type
3327 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003328 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3329 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003330 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003331 return(ret);
3332}
3333
3334/**
3335 * xmlValidateOneAttribute:
3336 * @ctxt: the validation context
3337 * @doc: a document instance
3338 * @elem: an element instance
3339 * @attr: an attribute instance
3340 * @value: the attribute value (without entities processing)
3341 *
3342 * Try to validate a single attribute for an element
3343 * basically it does the following checks as described by the
3344 * XML-1.0 recommendation:
3345 * - [ VC: Attribute Value Type ]
3346 * - [ VC: Fixed Attribute Default ]
3347 * - [ VC: Entity Name ]
3348 * - [ VC: Name Token ]
3349 * - [ VC: ID ]
3350 * - [ VC: IDREF ]
3351 * - [ VC: Entity Name ]
3352 * - [ VC: Notation Attributes ]
3353 *
3354 * The ID/IDREF uniqueness and matching are done separately
3355 *
3356 * returns 1 if valid or 0 otherwise
3357 */
3358
3359int
3360xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3361 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3362 /* xmlElementPtr elemDecl; */
3363 xmlAttributePtr attrDecl = NULL;
3364 int val;
3365 int ret = 1;
3366
3367 CHECK_DTD;
3368 if ((elem == NULL) || (elem->name == NULL)) return(0);
3369 if ((attr == NULL) || (attr->name == NULL)) return(0);
3370
3371 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3372 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003373 snprintf((char *) qname, sizeof(qname), "%s:%s",
3374 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003375 qname[sizeof(qname) - 1] = 0;
3376 if (attr->ns != NULL) {
3377 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3378 attr->name, attr->ns->prefix);
3379 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3380 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3381 attr->name, attr->ns->prefix);
3382 } else {
3383 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3384 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3385 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3386 qname, attr->name);
3387 }
3388 }
3389 if (attrDecl == NULL) {
3390 if (attr->ns != NULL) {
3391 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3392 attr->name, attr->ns->prefix);
3393 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3394 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3395 attr->name, attr->ns->prefix);
3396 } else {
3397 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3398 elem->name, attr->name);
3399 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3400 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3401 elem->name, attr->name);
3402 }
3403 }
3404
3405
3406 /* Validity Constraint: Attribute Value Type */
3407 if (attrDecl == NULL) {
3408 VERROR(ctxt->userData,
3409 "No declaration for attribute %s on element %s\n",
3410 attr->name, elem->name);
3411 return(0);
3412 }
3413 attr->atype = attrDecl->atype;
3414
3415 val = xmlValidateAttributeValue(attrDecl->atype, value);
3416 if (val == 0) {
3417 VERROR(ctxt->userData,
3418 "Syntax of value for attribute %s on %s is not valid\n",
3419 attr->name, elem->name);
3420 ret = 0;
3421 }
3422
3423 /* Validity constraint: Fixed Attribute Default */
3424 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3425 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3426 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003427 "Value for attribute %s on %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003428 attr->name, elem->name, attrDecl->defaultValue);
3429 ret = 0;
3430 }
3431 }
3432
3433 /* Validity Constraint: ID uniqueness */
3434 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3435 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3436 ret = 0;
3437 }
3438
3439 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3440 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3441 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3442 ret = 0;
3443 }
3444
3445 /* Validity Constraint: Notation Attributes */
3446 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3447 xmlEnumerationPtr tree = attrDecl->tree;
3448 xmlNotationPtr nota;
3449
3450 /* First check that the given NOTATION was declared */
3451 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3452 if (nota == NULL)
3453 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3454
3455 if (nota == NULL) {
3456 VERROR(ctxt->userData,
3457 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3458 value, attr->name, elem->name);
3459 ret = 0;
3460 }
3461
3462 /* Second, verify that it's among the list */
3463 while (tree != NULL) {
3464 if (xmlStrEqual(tree->name, value)) break;
3465 tree = tree->next;
3466 }
3467 if (tree == NULL) {
3468 VERROR(ctxt->userData,
3469"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3470 value, attr->name, elem->name);
3471 ret = 0;
3472 }
3473 }
3474
3475 /* Validity Constraint: Enumeration */
3476 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3477 xmlEnumerationPtr tree = attrDecl->tree;
3478 while (tree != NULL) {
3479 if (xmlStrEqual(tree->name, value)) break;
3480 tree = tree->next;
3481 }
3482 if (tree == NULL) {
3483 VERROR(ctxt->userData,
3484 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3485 value, attr->name, elem->name);
3486 ret = 0;
3487 }
3488 }
3489
3490 /* Fixed Attribute Default */
3491 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3492 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3493 VERROR(ctxt->userData,
3494 "Value for attribute %s on %s must be \"%s\"\n",
3495 attr->name, elem->name, attrDecl->defaultValue);
3496 ret = 0;
3497 }
3498
3499 /* Extra check for the attribute value */
3500 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3501 attrDecl->atype, value);
3502
3503 return(ret);
3504}
3505
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003506/**
3507 * xmlValidateSkipIgnorable:
3508 * @ctxt: the validation context
3509 * @child: the child list
3510 *
3511 * Skip ignorable elements w.r.t. the validation process
3512 *
3513 * returns the first element to consider for validation of the content model
3514 */
3515
3516static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003517xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003518 while (child != NULL) {
3519 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003520 /* These things are ignored (skipped) during validation. */
3521 case XML_PI_NODE:
3522 case XML_COMMENT_NODE:
3523 case XML_XINCLUDE_START:
3524 case XML_XINCLUDE_END:
3525 child = child->next;
3526 break;
3527 case XML_TEXT_NODE:
3528 if (xmlIsBlankNode(child))
3529 child = child->next;
3530 else
3531 return(child);
3532 break;
3533 /* keep current node */
3534 default:
3535 return(child);
3536 }
3537 }
3538 return(child);
3539}
3540
3541/**
3542 * xmlValidateElementType:
3543 * @ctxt: the validation context
3544 *
3545 * Try to validate the content model of an element internal function
3546 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003547 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3548 * reference is found and -3 if the validation succeeded but
3549 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003550 */
3551
3552static int
3553xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003554 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003555 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003556
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003557 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003558 if ((NODE == NULL) && (CONT == NULL))
3559 return(1);
3560 if ((NODE == NULL) &&
3561 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3562 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3563 return(1);
3564 }
3565 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003566 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003567 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003568
3569 /*
3570 * We arrive here when more states need to be examined
3571 */
3572cont:
3573
3574 /*
3575 * We just recovered from a rollback generated by a possible
3576 * epsilon transition, go directly to the analysis phase
3577 */
3578 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003579 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003580 DEBUG_VALID_STATE(NODE, CONT)
3581 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003582 goto analyze;
3583 }
3584
3585 DEBUG_VALID_STATE(NODE, CONT)
3586 /*
3587 * we may have to save a backup state here. This is the equivalent
3588 * of handling epsilon transition in NFAs.
3589 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003590 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003591 ((CONT->parent == NULL) ||
3592 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003593 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003594 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00003595 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003596 DEBUG_VALID_MSG("saving parent branch");
3597 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3598 }
3599
3600
3601 /*
3602 * Check first if the content matches
3603 */
3604 switch (CONT->type) {
3605 case XML_ELEMENT_CONTENT_PCDATA:
3606 if (NODE == NULL) {
3607 DEBUG_VALID_MSG("pcdata failed no node");
3608 ret = 0;
3609 break;
3610 }
3611 if (NODE->type == XML_TEXT_NODE) {
3612 DEBUG_VALID_MSG("pcdata found, skip to next");
3613 /*
3614 * go to next element in the content model
3615 * skipping ignorable elems
3616 */
3617 do {
3618 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003619 NODE = xmlValidateSkipIgnorable(NODE);
3620 if ((NODE != NULL) &&
3621 (NODE->type == XML_ENTITY_REF_NODE))
3622 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003623 } while ((NODE != NULL) &&
3624 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003625 (NODE->type != XML_TEXT_NODE) &&
3626 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003627 ret = 1;
3628 break;
3629 } else {
3630 DEBUG_VALID_MSG("pcdata failed");
3631 ret = 0;
3632 break;
3633 }
3634 break;
3635 case XML_ELEMENT_CONTENT_ELEMENT:
3636 if (NODE == NULL) {
3637 DEBUG_VALID_MSG("element failed no node");
3638 ret = 0;
3639 break;
3640 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003641 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3642 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003643 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003644 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3645 ret = (CONT->prefix == NULL);
3646 } else if (CONT->prefix == NULL) {
3647 ret = 0;
3648 } else {
3649 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3650 }
3651 }
3652 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003653 DEBUG_VALID_MSG("element found, skip to next");
3654 /*
3655 * go to next element in the content model
3656 * skipping ignorable elems
3657 */
3658 do {
3659 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003660 NODE = xmlValidateSkipIgnorable(NODE);
3661 if ((NODE != NULL) &&
3662 (NODE->type == XML_ENTITY_REF_NODE))
3663 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003664 } while ((NODE != NULL) &&
3665 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003666 (NODE->type != XML_TEXT_NODE) &&
3667 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003668 } else {
3669 DEBUG_VALID_MSG("element failed");
3670 ret = 0;
3671 break;
3672 }
3673 break;
3674 case XML_ELEMENT_CONTENT_OR:
3675 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003676 * Small optimization.
3677 */
3678 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3679 if ((NODE == NULL) ||
3680 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3681 DEPTH++;
3682 CONT = CONT->c2;
3683 goto cont;
3684 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003685 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3686 ret = (CONT->c1->prefix == NULL);
3687 } else if (CONT->c1->prefix == NULL) {
3688 ret = 0;
3689 } else {
3690 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3691 }
3692 if (ret == 0) {
3693 DEPTH++;
3694 CONT = CONT->c2;
3695 goto cont;
3696 }
Daniel Veillard85349052001-04-20 13:48:21 +00003697 }
3698
3699 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003700 * save the second branch 'or' branch
3701 */
3702 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003703 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3704 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003705
3706 DEPTH++;
3707 CONT = CONT->c1;
3708 goto cont;
3709 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003710 /*
3711 * Small optimization.
3712 */
3713 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3714 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3715 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3716 if ((NODE == NULL) ||
3717 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3718 DEPTH++;
3719 CONT = CONT->c2;
3720 goto cont;
3721 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003722 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3723 ret = (CONT->c1->prefix == NULL);
3724 } else if (CONT->c1->prefix == NULL) {
3725 ret = 0;
3726 } else {
3727 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3728 }
3729 if (ret == 0) {
3730 DEPTH++;
3731 CONT = CONT->c2;
3732 goto cont;
3733 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003734 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003735 DEPTH++;
3736 CONT = CONT->c1;
3737 goto cont;
3738 }
3739
3740 /*
3741 * At this point handle going up in the tree
3742 */
3743 if (ret == -1) {
3744 DEBUG_VALID_MSG("error found returning");
3745 return(ret);
3746 }
3747analyze:
3748 while (CONT != NULL) {
3749 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003750 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003751 * this level.
3752 */
3753 if (ret == 0) {
3754 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003755 xmlNodePtr cur;
3756
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003757 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003758 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003759 DEBUG_VALID_MSG("Once branch failed, rollback");
3760 if (vstateVPop(ctxt) < 0 ) {
3761 DEBUG_VALID_MSG("exhaustion, failed");
3762 return(0);
3763 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003764 if (cur != ctxt->vstate->node)
3765 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003766 goto cont;
3767 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00003768 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003769 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003770 DEBUG_VALID_MSG("Plus branch failed, rollback");
3771 if (vstateVPop(ctxt) < 0 ) {
3772 DEBUG_VALID_MSG("exhaustion, failed");
3773 return(0);
3774 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003775 if (cur != ctxt->vstate->node)
3776 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003777 goto cont;
3778 }
3779 DEBUG_VALID_MSG("Plus branch found");
3780 ret = 1;
3781 break;
3782 case XML_ELEMENT_CONTENT_MULT:
3783#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00003784 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003785 DEBUG_VALID_MSG("Mult branch failed");
3786 } else {
3787 DEBUG_VALID_MSG("Mult branch found");
3788 }
3789#endif
3790 ret = 1;
3791 break;
3792 case XML_ELEMENT_CONTENT_OPT:
3793 DEBUG_VALID_MSG("Option branch failed");
3794 ret = 1;
3795 break;
3796 }
3797 } else {
3798 switch (CONT->ocur) {
3799 case XML_ELEMENT_CONTENT_OPT:
3800 DEBUG_VALID_MSG("Option branch succeeded");
3801 ret = 1;
3802 break;
3803 case XML_ELEMENT_CONTENT_ONCE:
3804 DEBUG_VALID_MSG("Once branch succeeded");
3805 ret = 1;
3806 break;
3807 case XML_ELEMENT_CONTENT_PLUS:
3808 if (STATE == ROLLBACK_PARENT) {
3809 DEBUG_VALID_MSG("Plus branch rollback");
3810 ret = 1;
3811 break;
3812 }
3813 if (NODE == NULL) {
3814 DEBUG_VALID_MSG("Plus branch exhausted");
3815 ret = 1;
3816 break;
3817 }
3818 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003819 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003820 goto cont;
3821 case XML_ELEMENT_CONTENT_MULT:
3822 if (STATE == ROLLBACK_PARENT) {
3823 DEBUG_VALID_MSG("Mult branch rollback");
3824 ret = 1;
3825 break;
3826 }
3827 if (NODE == NULL) {
3828 DEBUG_VALID_MSG("Mult branch exhausted");
3829 ret = 1;
3830 break;
3831 }
3832 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003833 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003834 goto cont;
3835 }
3836 }
3837 STATE = 0;
3838
3839 /*
3840 * Then act accordingly at the parent level
3841 */
Daniel Veillard5344c602001-12-31 16:37:34 +00003842 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003843 if (CONT->parent == NULL)
3844 break;
3845
3846 switch (CONT->parent->type) {
3847 case XML_ELEMENT_CONTENT_PCDATA:
3848 DEBUG_VALID_MSG("Error: parent pcdata");
3849 return(-1);
3850 case XML_ELEMENT_CONTENT_ELEMENT:
3851 DEBUG_VALID_MSG("Error: parent element");
3852 return(-1);
3853 case XML_ELEMENT_CONTENT_OR:
3854 if (ret == 1) {
3855 DEBUG_VALID_MSG("Or succeeded");
3856 CONT = CONT->parent;
3857 DEPTH--;
3858 } else {
3859 DEBUG_VALID_MSG("Or failed");
3860 CONT = CONT->parent;
3861 DEPTH--;
3862 }
3863 break;
3864 case XML_ELEMENT_CONTENT_SEQ:
3865 if (ret == 0) {
3866 DEBUG_VALID_MSG("Sequence failed");
3867 CONT = CONT->parent;
3868 DEPTH--;
3869 } else if (CONT == CONT->parent->c1) {
3870 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3871 CONT = CONT->parent->c2;
3872 goto cont;
3873 } else {
3874 DEBUG_VALID_MSG("Sequence succeeded");
3875 CONT = CONT->parent;
3876 DEPTH--;
3877 }
3878 }
3879 }
3880 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003881 xmlNodePtr cur;
3882
3883 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003884 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3885 if (vstateVPop(ctxt) < 0 ) {
3886 DEBUG_VALID_MSG("exhaustion, failed");
3887 return(0);
3888 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003889 if (cur != ctxt->vstate->node)
3890 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003891 goto cont;
3892 }
3893 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003894 xmlNodePtr cur;
3895
3896 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003897 DEBUG_VALID_MSG("Failure, rollback");
3898 if (vstateVPop(ctxt) < 0 ) {
3899 DEBUG_VALID_MSG("exhaustion, failed");
3900 return(0);
3901 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003902 if (cur != ctxt->vstate->node)
3903 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003904 goto cont;
3905 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003906 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003907}
3908
3909/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003910 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003911 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003912 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003913 * @content: An element
3914 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3915 *
3916 * This will dump the list of elements to the buffer
3917 * Intended just for the debug routine
3918 */
3919static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003920xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003921 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003922 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003923
3924 if (node == NULL) return;
3925 if (glob) strcat(buf, "(");
3926 cur = node;
3927 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003928 len = strlen(buf);
3929 if (size - len < 50) {
3930 if ((size - len > 4) && (buf[len - 1] != '.'))
3931 strcat(buf, " ...");
3932 return;
3933 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003934 switch (cur->type) {
3935 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003936 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
3937 if (size - len < xmlStrlen(cur->ns->prefix + 10)) {
3938 if ((size - len > 4) && (buf[len - 1] != '.'))
3939 strcat(buf, " ...");
3940 return;
3941 }
3942 strcat(buf, (char *) cur->ns->prefix);
3943 strcat(buf, ":");
3944 }
Daniel Veillardd3d06722001-08-15 12:06:36 +00003945 if (size - len < xmlStrlen(cur->name + 10)) {
3946 if ((size - len > 4) && (buf[len - 1] != '.'))
3947 strcat(buf, " ...");
3948 return;
3949 }
3950 strcat(buf, (char *) cur->name);
3951 if (cur->next != NULL)
3952 strcat(buf, " ");
3953 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003954 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003955 if (xmlIsBlankNode(cur))
3956 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003957 case XML_CDATA_SECTION_NODE:
3958 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003959 strcat(buf, "CDATA");
3960 if (cur->next != NULL)
3961 strcat(buf, " ");
3962 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003963 case XML_ATTRIBUTE_NODE:
3964 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003965#ifdef LIBXML_DOCB_ENABLED
3966 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003967#endif
3968 case XML_HTML_DOCUMENT_NODE:
3969 case XML_DOCUMENT_TYPE_NODE:
3970 case XML_DOCUMENT_FRAG_NODE:
3971 case XML_NOTATION_NODE:
3972 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003973 strcat(buf, "???");
3974 if (cur->next != NULL)
3975 strcat(buf, " ");
3976 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003977 case XML_ENTITY_NODE:
3978 case XML_PI_NODE:
3979 case XML_DTD_NODE:
3980 case XML_COMMENT_NODE:
3981 case XML_ELEMENT_DECL:
3982 case XML_ATTRIBUTE_DECL:
3983 case XML_ENTITY_DECL:
3984 case XML_XINCLUDE_START:
3985 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003986 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003987 }
3988 cur = cur->next;
3989 }
3990 if (glob) strcat(buf, ")");
3991}
3992
3993/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003994 * xmlValidateElementContent:
3995 * @ctxt: the validation context
3996 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003997 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003998 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003999 *
4000 * Try to validate the content model of an element
4001 *
4002 * returns 1 if valid or 0 if not and -1 in case of error
4003 */
4004
4005static int
4006xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004007 xmlElementPtr elemDecl, int warn) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004008 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004009 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004010 xmlElementContentPtr cont;
4011 const xmlChar *name;
4012
4013 if (elemDecl == NULL)
4014 return(-1);
4015 cont = elemDecl->content;
4016 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004017
4018 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004019 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004020 */
4021 ctxt->vstateMax = 8;
4022 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4023 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4024 if (ctxt->vstateTab == NULL) {
4025 xmlGenericError(xmlGenericErrorContext,
4026 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004027 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004028 }
4029 /*
4030 * The first entry in the stack is reserved to the current state
4031 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004032 ctxt->nodeMax = 0;
4033 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004034 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004035 ctxt->vstate = &ctxt->vstateTab[0];
4036 ctxt->vstateNr = 1;
4037 CONT = cont;
4038 NODE = child;
4039 DEPTH = 0;
4040 OCCURS = 0;
4041 STATE = 0;
4042 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004043 if ((ret == -3) && (warn)) {
4044 VWARNING(ctxt->userData,
4045 "Element %s content model is ambiguous\n", name);
4046 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004047 /*
4048 * An entities reference appeared at this level.
4049 * Buid a minimal representation of this node content
4050 * sufficient to run the validation process on it
4051 */
4052 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004053 cur = child;
4054 while (cur != NULL) {
4055 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004056 case XML_ENTITY_REF_NODE:
4057 /*
4058 * Push the current node to be able to roll back
4059 * and process within the entity
4060 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004061 if ((cur->children != NULL) &&
4062 (cur->children->children != NULL)) {
4063 nodeVPush(ctxt, cur);
4064 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004065 continue;
4066 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004067 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004068 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004069 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004070 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004071 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004072 case XML_CDATA_SECTION_NODE:
4073 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004074 case XML_ELEMENT_NODE:
4075 /*
4076 * Allocate a new node and minimally fills in
4077 * what's required
4078 */
4079 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4080 if (tmp == NULL) {
4081 xmlGenericError(xmlGenericErrorContext,
4082 "xmlValidateElementContent : malloc failed\n");
4083 xmlFreeNodeList(repl);
4084 ret = -1;
4085 goto done;
4086 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004087 tmp->type = cur->type;
4088 tmp->name = cur->name;
4089 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004090 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004091 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004092 if (repl == NULL)
4093 repl = last = tmp;
4094 else {
4095 last->next = tmp;
4096 last = tmp;
4097 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004098 if (cur->type == XML_CDATA_SECTION_NODE) {
4099 /*
4100 * E59 spaces in CDATA does not match the
4101 * nonterminal S
4102 */
4103 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4104 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004105 break;
4106 default:
4107 break;
4108 }
4109 /*
4110 * Switch to next element
4111 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004112 cur = cur->next;
4113 while (cur == NULL) {
4114 cur = nodeVPop(ctxt);
4115 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004116 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004117 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004118 }
4119 }
4120
4121 /*
4122 * Relaunch the validation
4123 */
4124 ctxt->vstate = &ctxt->vstateTab[0];
4125 ctxt->vstateNr = 1;
4126 CONT = cont;
4127 NODE = repl;
4128 DEPTH = 0;
4129 OCCURS = 0;
4130 STATE = 0;
4131 ret = xmlValidateElementType(ctxt);
4132 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004133 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004134 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4135 char expr[5000];
4136 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004137
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004138 expr[0] = 0;
4139 xmlSnprintfElementContent(expr, 5000, cont, 1);
4140 list[0] = 0;
4141 if (repl != NULL)
4142 xmlSnprintfElements(list, 5000, repl, 1);
4143 else
4144 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004145
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004146 if (name != NULL) {
4147 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004148 "Element %s content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004149 name, expr, list);
4150 } else {
4151 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004152 "Element content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004153 expr, list);
4154 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004155 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004156 if (name != NULL) {
4157 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004158 "Element %s content doesn't follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004159 name);
4160 } else {
4161 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004162 "Element content doesn't follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004163 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004164 }
4165 ret = 0;
4166 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004167 if (ret == -3)
4168 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004169
4170
4171done:
4172 /*
4173 * Deallocate the copy if done, and free up the validation stack
4174 */
4175 while (repl != NULL) {
4176 tmp = repl->next;
4177 xmlFree(repl);
4178 repl = tmp;
4179 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004180 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004181 if (ctxt->vstateTab != NULL) {
4182 xmlFree(ctxt->vstateTab);
4183 ctxt->vstateTab = NULL;
4184 }
4185 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004186 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004187 if (ctxt->nodeTab != NULL) {
4188 xmlFree(ctxt->nodeTab);
4189 ctxt->nodeTab = NULL;
4190 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004191 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004192
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004193}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004194
Owen Taylor3473f882001-02-23 17:55:21 +00004195/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004196 * xmlValidateCdataElement:
4197 * @ctxt: the validation context
4198 * @doc: a document instance
4199 * @elem: an element instance
4200 *
4201 * Check that an element follows #CDATA
4202 *
4203 * returns 1 if valid or 0 otherwise
4204 */
4205static int
4206xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4207 xmlNodePtr elem) {
4208 int ret = 1;
4209 xmlNodePtr cur, child;
4210
4211 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4212 return(0);
4213
4214 child = elem->children;
4215
4216 cur = child;
4217 while (cur != NULL) {
4218 switch (cur->type) {
4219 case XML_ENTITY_REF_NODE:
4220 /*
4221 * Push the current node to be able to roll back
4222 * and process within the entity
4223 */
4224 if ((cur->children != NULL) &&
4225 (cur->children->children != NULL)) {
4226 nodeVPush(ctxt, cur);
4227 cur = cur->children->children;
4228 continue;
4229 }
4230 break;
4231 case XML_COMMENT_NODE:
4232 case XML_PI_NODE:
4233 case XML_TEXT_NODE:
4234 case XML_CDATA_SECTION_NODE:
4235 break;
4236 default:
4237 ret = 0;
4238 goto done;
4239 }
4240 /*
4241 * Switch to next element
4242 */
4243 cur = cur->next;
4244 while (cur == NULL) {
4245 cur = nodeVPop(ctxt);
4246 if (cur == NULL)
4247 break;
4248 cur = cur->next;
4249 }
4250 }
4251done:
4252 ctxt->nodeMax = 0;
4253 ctxt->nodeNr = 0;
4254 if (ctxt->nodeTab != NULL) {
4255 xmlFree(ctxt->nodeTab);
4256 ctxt->nodeTab = NULL;
4257 }
4258 return(ret);
4259}
4260
4261/**
Owen Taylor3473f882001-02-23 17:55:21 +00004262 * xmlValidateOneElement:
4263 * @ctxt: the validation context
4264 * @doc: a document instance
4265 * @elem: an element instance
4266 *
4267 * Try to validate a single element and it's attributes,
4268 * basically it does the following checks as described by the
4269 * XML-1.0 recommendation:
4270 * - [ VC: Element Valid ]
4271 * - [ VC: Required Attribute ]
4272 * Then call xmlValidateOneAttribute() for each attribute present.
4273 *
4274 * The ID/IDREF checkings are done separately
4275 *
4276 * returns 1 if valid or 0 otherwise
4277 */
4278
4279int
4280xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4281 xmlNodePtr elem) {
4282 xmlElementPtr elemDecl = NULL;
4283 xmlElementContentPtr cont;
4284 xmlAttributePtr attr;
4285 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004286 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004287 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004288 const xmlChar *prefix = NULL;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004289 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004290
4291 CHECK_DTD;
4292
4293 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004294 switch (elem->type) {
4295 case XML_ATTRIBUTE_NODE:
4296 VERROR(ctxt->userData,
4297 "Attribute element not expected here\n");
4298 return(0);
4299 case XML_TEXT_NODE:
4300 if (elem->children != NULL) {
4301 VERROR(ctxt->userData, "Text element has childs !\n");
4302 return(0);
4303 }
4304 if (elem->properties != NULL) {
4305 VERROR(ctxt->userData, "Text element has attributes !\n");
4306 return(0);
4307 }
4308 if (elem->ns != NULL) {
4309 VERROR(ctxt->userData, "Text element has namespace !\n");
4310 return(0);
4311 }
4312 if (elem->nsDef != NULL) {
4313 VERROR(ctxt->userData,
4314 "Text element carries namespace definitions !\n");
4315 return(0);
4316 }
4317 if (elem->content == NULL) {
4318 VERROR(ctxt->userData,
4319 "Text element has no content !\n");
4320 return(0);
4321 }
4322 return(1);
4323 case XML_XINCLUDE_START:
4324 case XML_XINCLUDE_END:
4325 return(1);
4326 case XML_CDATA_SECTION_NODE:
4327 case XML_ENTITY_REF_NODE:
4328 case XML_PI_NODE:
4329 case XML_COMMENT_NODE:
4330 return(1);
4331 case XML_ENTITY_NODE:
4332 VERROR(ctxt->userData,
4333 "Entity element not expected here\n");
4334 return(0);
4335 case XML_NOTATION_NODE:
4336 VERROR(ctxt->userData,
4337 "Notation element not expected here\n");
4338 return(0);
4339 case XML_DOCUMENT_NODE:
4340 case XML_DOCUMENT_TYPE_NODE:
4341 case XML_DOCUMENT_FRAG_NODE:
4342 VERROR(ctxt->userData,
4343 "Document element not expected here\n");
4344 return(0);
4345 case XML_HTML_DOCUMENT_NODE:
4346 VERROR(ctxt->userData,
4347 "\n");
4348 return(0);
4349 case XML_ELEMENT_NODE:
4350 break;
4351 default:
4352 VERROR(ctxt->userData,
4353 "unknown element type %d\n", elem->type);
4354 return(0);
4355 }
4356 if (elem->name == NULL) return(0);
4357
4358 /*
4359 * Fetch the declaration for the qualified name
4360 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004361 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4362 prefix = elem->ns->prefix;
4363
4364 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004365 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004366 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004367 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004368 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004369 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004370 if (elemDecl != NULL)
4371 extsubset = 1;
4372 }
Owen Taylor3473f882001-02-23 17:55:21 +00004373 }
4374
4375 /*
4376 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004377 * This is "non-strict" validation should be done on the
4378 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004379 */
4380 if (elemDecl == NULL) {
4381 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004382 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004383 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004384 if (elemDecl != NULL)
4385 extsubset = 1;
4386 }
Owen Taylor3473f882001-02-23 17:55:21 +00004387 }
4388 if (elemDecl == NULL) {
4389 VERROR(ctxt->userData, "No declaration for element %s\n",
4390 elem->name);
4391 return(0);
4392 }
4393
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004394 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00004395 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004396 case XML_ELEMENT_TYPE_UNDEFINED:
4397 VERROR(ctxt->userData, "No declaration for element %s\n",
4398 elem->name);
4399 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004400 case XML_ELEMENT_TYPE_EMPTY:
4401 if (elem->children != NULL) {
4402 VERROR(ctxt->userData,
4403 "Element %s was declared EMPTY this one has content\n",
4404 elem->name);
4405 ret = 0;
4406 }
4407 break;
4408 case XML_ELEMENT_TYPE_ANY:
4409 /* I don't think anything is required then */
4410 break;
4411 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004412
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004413 /* simple case of declared as #PCDATA */
4414 if ((elemDecl->content != NULL) &&
4415 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4416 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4417 if (!ret) {
4418 VERROR(ctxt->userData,
4419 "Element %s was declared #PCDATA but contains non text nodes\n",
4420 elem->name);
4421 }
4422 break;
4423 }
Owen Taylor3473f882001-02-23 17:55:21 +00004424 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004425 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004426 while (child != NULL) {
4427 if (child->type == XML_ELEMENT_NODE) {
4428 name = child->name;
4429 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4430 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004431 snprintf((char *) qname, sizeof(qname), "%s:%s",
4432 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004433 qname[sizeof(qname) - 1] = 0;
4434 cont = elemDecl->content;
4435 while (cont != NULL) {
4436 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4437 if (xmlStrEqual(cont->name, qname)) break;
4438 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4439 (cont->c1 != NULL) &&
4440 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4441 if (xmlStrEqual(cont->c1->name, qname)) break;
4442 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4443 (cont->c1 == NULL) ||
4444 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4445 /* Internal error !!! */
4446 xmlGenericError(xmlGenericErrorContext,
4447 "Internal: MIXED struct bad\n");
4448 break;
4449 }
4450 cont = cont->c2;
4451 }
4452 if (cont != NULL)
4453 goto child_ok;
4454 }
4455 cont = elemDecl->content;
4456 while (cont != NULL) {
4457 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4458 if (xmlStrEqual(cont->name, name)) break;
4459 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4460 (cont->c1 != NULL) &&
4461 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4462 if (xmlStrEqual(cont->c1->name, name)) break;
4463 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4464 (cont->c1 == NULL) ||
4465 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4466 /* Internal error !!! */
4467 xmlGenericError(xmlGenericErrorContext,
4468 "Internal: MIXED struct bad\n");
4469 break;
4470 }
4471 cont = cont->c2;
4472 }
4473 if (cont == NULL) {
4474 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00004475 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004476 name, elem->name);
4477 ret = 0;
4478 }
4479 }
4480child_ok:
4481 child = child->next;
4482 }
4483 break;
4484 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004485 if ((doc->standalone == 1) && (extsubset == 1)) {
4486 /*
4487 * VC: Standalone Document Declaration
4488 * - element types with element content, if white space
4489 * occurs directly within any instance of those types.
4490 */
4491 child = elem->children;
4492 while (child != NULL) {
4493 if (child->type == XML_TEXT_NODE) {
4494 const xmlChar *content = child->content;
4495
4496 while (IS_BLANK(*content))
4497 content++;
4498 if (*content == 0) {
4499 VERROR(ctxt->userData,
4500"standalone: %s declared in the external subset contains white spaces nodes\n",
4501 elem->name);
4502 ret = 0;
4503 break;
4504 }
4505 }
4506 child =child->next;
4507 }
4508 }
Owen Taylor3473f882001-02-23 17:55:21 +00004509 child = elem->children;
4510 cont = elemDecl->content;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004511 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1);
4512 if (tmp <= 0)
4513 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004514 break;
4515 }
4516
4517 /* [ VC: Required Attribute ] */
4518 attr = elemDecl->attributes;
4519 while (attr != NULL) {
4520 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004521 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00004522
Daniel Veillarde4301c82002-02-13 13:32:35 +00004523 if ((attr->prefix == NULL) &&
4524 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4525 xmlNsPtr ns;
4526
4527 ns = elem->nsDef;
4528 while (ns != NULL) {
4529 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004530 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004531 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00004532 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004533 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4534 xmlNsPtr ns;
4535
4536 ns = elem->nsDef;
4537 while (ns != NULL) {
4538 if (xmlStrEqual(attr->name, ns->prefix))
4539 goto found;
4540 ns = ns->next;
4541 }
4542 } else {
4543 xmlAttrPtr attrib;
4544
4545 attrib = elem->properties;
4546 while (attrib != NULL) {
4547 if (xmlStrEqual(attrib->name, attr->name)) {
4548 if (attr->prefix != NULL) {
4549 xmlNsPtr nameSpace = attrib->ns;
4550
4551 if (nameSpace == NULL)
4552 nameSpace = elem->ns;
4553 /*
4554 * qualified names handling is problematic, having a
4555 * different prefix should be possible but DTDs don't
4556 * allow to define the URI instead of the prefix :-(
4557 */
4558 if (nameSpace == NULL) {
4559 if (qualified < 0)
4560 qualified = 0;
4561 } else if (!xmlStrEqual(nameSpace->prefix,
4562 attr->prefix)) {
4563 if (qualified < 1)
4564 qualified = 1;
4565 } else
4566 goto found;
4567 } else {
4568 /*
4569 * We should allow applications to define namespaces
4570 * for their application even if the DTD doesn't
4571 * carry one, otherwise, basically we would always
4572 * break.
4573 */
4574 goto found;
4575 }
4576 }
4577 attrib = attrib->next;
4578 }
Owen Taylor3473f882001-02-23 17:55:21 +00004579 }
4580 if (qualified == -1) {
4581 if (attr->prefix == NULL) {
4582 VERROR(ctxt->userData,
4583 "Element %s doesn't carry attribute %s\n",
4584 elem->name, attr->name);
4585 ret = 0;
4586 } else {
4587 VERROR(ctxt->userData,
4588 "Element %s doesn't carry attribute %s:%s\n",
4589 elem->name, attr->prefix,attr->name);
4590 ret = 0;
4591 }
4592 } else if (qualified == 0) {
4593 VWARNING(ctxt->userData,
4594 "Element %s required attribute %s:%s has no prefix\n",
4595 elem->name, attr->prefix,attr->name);
4596 } else if (qualified == 1) {
4597 VWARNING(ctxt->userData,
4598 "Element %s required attribute %s:%s has different prefix\n",
4599 elem->name, attr->prefix,attr->name);
4600 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004601 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
4602 /*
4603 * Special tests checking #FIXED namespace declarations
4604 * have the right value since this is not done as an
4605 * attribute checking
4606 */
4607 if ((attr->prefix == NULL) &&
4608 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4609 xmlNsPtr ns;
4610
4611 ns = elem->nsDef;
4612 while (ns != NULL) {
4613 if (ns->prefix == NULL) {
4614 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4615 VERROR(ctxt->userData,
4616 "Element %s namespace name for default namespace does not match the DTD\n",
4617 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00004618 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004619 }
4620 goto found;
4621 }
4622 ns = ns->next;
4623 }
4624 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4625 xmlNsPtr ns;
4626
4627 ns = elem->nsDef;
4628 while (ns != NULL) {
4629 if (xmlStrEqual(attr->name, ns->prefix)) {
4630 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4631 VERROR(ctxt->userData,
4632 "Element %s namespace name for %s doesn't match the DTD\n",
4633 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00004634 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004635 }
4636 goto found;
4637 }
4638 ns = ns->next;
4639 }
4640 }
Owen Taylor3473f882001-02-23 17:55:21 +00004641 }
4642found:
4643 attr = attr->nexth;
4644 }
4645 return(ret);
4646}
4647
4648/**
4649 * xmlValidateRoot:
4650 * @ctxt: the validation context
4651 * @doc: a document instance
4652 *
4653 * Try to validate a the root element
4654 * basically it does the following check as described by the
4655 * XML-1.0 recommendation:
4656 * - [ VC: Root Element Type ]
4657 * it doesn't try to recurse or apply other check to the element
4658 *
4659 * returns 1 if valid or 0 otherwise
4660 */
4661
4662int
4663xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4664 xmlNodePtr root;
4665 if (doc == NULL) return(0);
4666
4667 root = xmlDocGetRootElement(doc);
4668 if ((root == NULL) || (root->name == NULL)) {
4669 VERROR(ctxt->userData, "Not valid: no root element\n");
4670 return(0);
4671 }
4672
4673 /*
4674 * When doing post validation against a separate DTD, those may
4675 * no internal subset has been generated
4676 */
4677 if ((doc->intSubset != NULL) &&
4678 (doc->intSubset->name != NULL)) {
4679 /*
4680 * Check first the document root against the NQName
4681 */
4682 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4683 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4684 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004685 snprintf((char *) qname, sizeof(qname), "%s:%s",
4686 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004687 qname[sizeof(qname) - 1] = 0;
4688 if (xmlStrEqual(doc->intSubset->name, qname))
4689 goto name_ok;
4690 }
4691 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4692 (xmlStrEqual(root->name, BAD_CAST "html")))
4693 goto name_ok;
4694 VERROR(ctxt->userData,
4695 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4696 root->name, doc->intSubset->name);
4697 return(0);
4698
4699 }
4700 }
4701name_ok:
4702 return(1);
4703}
4704
4705
4706/**
4707 * xmlValidateElement:
4708 * @ctxt: the validation context
4709 * @doc: a document instance
4710 * @elem: an element instance
4711 *
4712 * Try to validate the subtree under an element
4713 *
4714 * returns 1 if valid or 0 otherwise
4715 */
4716
4717int
4718xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4719 xmlNodePtr child;
4720 xmlAttrPtr attr;
4721 xmlChar *value;
4722 int ret = 1;
4723
4724 if (elem == NULL) return(0);
4725
4726 /*
4727 * XInclude elements were added after parsing in the infoset,
4728 * they don't really mean anything validation wise.
4729 */
4730 if ((elem->type == XML_XINCLUDE_START) ||
4731 (elem->type == XML_XINCLUDE_END))
4732 return(1);
4733
4734 CHECK_DTD;
4735
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004736 /*
4737 * Entities references have to be handled separately
4738 */
4739 if (elem->type == XML_ENTITY_REF_NODE) {
4740 return(1);
4741 }
4742
Owen Taylor3473f882001-02-23 17:55:21 +00004743 ret &= xmlValidateOneElement(ctxt, doc, elem);
4744 attr = elem->properties;
4745 while(attr != NULL) {
4746 value = xmlNodeListGetString(doc, attr->children, 0);
4747 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4748 if (value != NULL)
4749 xmlFree(value);
4750 attr= attr->next;
4751 }
4752 child = elem->children;
4753 while (child != NULL) {
4754 ret &= xmlValidateElement(ctxt, doc, child);
4755 child = child->next;
4756 }
4757
4758 return(ret);
4759}
4760
Daniel Veillard8730c562001-02-26 10:49:57 +00004761/**
4762 * xmlValidateRef:
4763 * @ref: A reference to be validated
4764 * @ctxt: Validation context
4765 * @name: Name of ID we are searching for
4766 *
4767 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004768static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004769xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004770 const xmlChar *name) {
4771 xmlAttrPtr id;
4772 xmlAttrPtr attr;
4773
4774 if (ref == NULL)
4775 return;
4776 attr = ref->attr;
4777 if (attr == NULL)
4778 return;
4779 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4780 id = xmlGetID(ctxt->doc, name);
4781 if (id == NULL) {
4782 VERROR(ctxt->userData,
4783 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4784 attr->name, name);
4785 ctxt->valid = 0;
4786 }
4787 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4788 xmlChar *dup, *str = NULL, *cur, save;
4789
4790 dup = xmlStrdup(name);
4791 if (dup == NULL) {
4792 ctxt->valid = 0;
4793 return;
4794 }
4795 cur = dup;
4796 while (*cur != 0) {
4797 str = cur;
4798 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4799 save = *cur;
4800 *cur = 0;
4801 id = xmlGetID(ctxt->doc, str);
4802 if (id == NULL) {
4803 VERROR(ctxt->userData,
4804 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4805 attr->name, str);
4806 ctxt->valid = 0;
4807 }
4808 if (save == 0)
4809 break;
4810 *cur = save;
4811 while (IS_BLANK(*cur)) cur++;
4812 }
4813 xmlFree(dup);
4814 }
4815}
4816
4817/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004818 * xmlWalkValidateList:
4819 * @data: Contents of current link
4820 * @user: Value supplied by the user
4821 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004822 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00004823 */
4824static int
4825xmlWalkValidateList(const void *data, const void *user)
4826{
4827 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4828 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4829 return 1;
4830}
4831
4832/**
4833 * xmlValidateCheckRefCallback:
4834 * @ref_list: List of references
4835 * @ctxt: Validation context
4836 * @name: Name of ID we are searching for
4837 *
4838 */
4839static void
4840xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4841 const xmlChar *name) {
4842 xmlValidateMemo memo;
4843
4844 if (ref_list == NULL)
4845 return;
4846 memo.ctxt = ctxt;
4847 memo.name = name;
4848
4849 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4850
4851}
4852
4853/**
Owen Taylor3473f882001-02-23 17:55:21 +00004854 * xmlValidateDocumentFinal:
4855 * @ctxt: the validation context
4856 * @doc: a document instance
4857 *
4858 * Does the final step for the document validation once all the
4859 * incremental validation steps have been completed
4860 *
4861 * basically it does the following checks described by the XML Rec
4862 *
4863 *
4864 * returns 1 if valid or 0 otherwise
4865 */
4866
4867int
4868xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4869 xmlRefTablePtr table;
4870
4871 if (doc == NULL) {
4872 xmlGenericError(xmlGenericErrorContext,
4873 "xmlValidateDocumentFinal: doc == NULL\n");
4874 return(0);
4875 }
4876
4877 /*
4878 * Check all the NOTATION/NOTATIONS attributes
4879 */
4880 /*
4881 * Check all the ENTITY/ENTITIES attributes definition for validity
4882 */
4883 /*
4884 * Check all the IDREF/IDREFS attributes definition for validity
4885 */
4886 table = (xmlRefTablePtr) doc->refs;
4887 ctxt->doc = doc;
4888 ctxt->valid = 1;
4889 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4890 return(ctxt->valid);
4891}
4892
4893/**
4894 * xmlValidateDtd:
4895 * @ctxt: the validation context
4896 * @doc: a document instance
4897 * @dtd: a dtd instance
4898 *
4899 * Try to validate the document against the dtd instance
4900 *
4901 * basically it does check all the definitions in the DtD.
4902 *
4903 * returns 1 if valid or 0 otherwise
4904 */
4905
4906int
4907xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4908 int ret;
4909 xmlDtdPtr oldExt;
4910 xmlNodePtr root;
4911
4912 if (dtd == NULL) return(0);
4913 if (doc == NULL) return(0);
4914 oldExt = doc->extSubset;
4915 doc->extSubset = dtd;
4916 ret = xmlValidateRoot(ctxt, doc);
4917 if (ret == 0) {
4918 doc->extSubset = oldExt;
4919 return(ret);
4920 }
4921 if (doc->ids != NULL) {
4922 xmlFreeIDTable(doc->ids);
4923 doc->ids = NULL;
4924 }
4925 if (doc->refs != NULL) {
4926 xmlFreeRefTable(doc->refs);
4927 doc->refs = NULL;
4928 }
4929 root = xmlDocGetRootElement(doc);
4930 ret = xmlValidateElement(ctxt, doc, root);
4931 ret &= xmlValidateDocumentFinal(ctxt, doc);
4932 doc->extSubset = oldExt;
4933 return(ret);
4934}
4935
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004936static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004937xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
4938 const xmlChar *name ATTRIBUTE_UNUSED) {
4939 if (cur == NULL)
4940 return;
4941 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
4942 xmlChar *notation = cur->content;
4943
Daniel Veillard878eab02002-02-19 13:46:09 +00004944 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004945 int ret;
4946
4947 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
4948 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00004949 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004950 }
4951 }
4952 }
4953}
4954
4955static void
Owen Taylor3473f882001-02-23 17:55:21 +00004956xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004957 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004958 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00004959 xmlDocPtr doc;
4960 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004961
Owen Taylor3473f882001-02-23 17:55:21 +00004962 if (cur == NULL)
4963 return;
4964 switch (cur->atype) {
4965 case XML_ATTRIBUTE_CDATA:
4966 case XML_ATTRIBUTE_ID:
4967 case XML_ATTRIBUTE_IDREF :
4968 case XML_ATTRIBUTE_IDREFS:
4969 case XML_ATTRIBUTE_NMTOKEN:
4970 case XML_ATTRIBUTE_NMTOKENS:
4971 case XML_ATTRIBUTE_ENUMERATION:
4972 break;
4973 case XML_ATTRIBUTE_ENTITY:
4974 case XML_ATTRIBUTE_ENTITIES:
4975 case XML_ATTRIBUTE_NOTATION:
4976 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004977
4978 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
4979 cur->atype, cur->defaultValue);
4980 if ((ret == 0) && (ctxt->valid == 1))
4981 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004982 }
4983 if (cur->tree != NULL) {
4984 xmlEnumerationPtr tree = cur->tree;
4985 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004986 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00004987 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004988 if ((ret == 0) && (ctxt->valid == 1))
4989 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004990 tree = tree->next;
4991 }
4992 }
4993 }
Daniel Veillard878eab02002-02-19 13:46:09 +00004994 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
4995 doc = cur->doc;
4996 if ((doc == NULL) || (cur->elem == NULL)) {
4997 VERROR(ctxt->userData,
4998 "xmlValidateAttributeCallback(%s): internal error\n",
4999 cur->name);
5000 return;
5001 }
5002 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
5003 if (elem == NULL)
5004 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
5005 if (elem == NULL) {
5006 VERROR(ctxt->userData,
5007 "attribute %s: could not find decl for element %s\n",
5008 cur->name, cur->elem);
5009 return;
5010 }
5011 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
5012 VERROR(ctxt->userData,
5013 "NOTATION attribute %s declared on EMPTY element %s\n",
5014 cur->name, cur->elem);
5015 ctxt->valid = 0;
5016 }
5017 }
Owen Taylor3473f882001-02-23 17:55:21 +00005018}
5019
5020/**
5021 * xmlValidateDtdFinal:
5022 * @ctxt: the validation context
5023 * @doc: a document instance
5024 *
5025 * Does the final step for the dtds validation once all the
5026 * subsets have been parsed
5027 *
5028 * basically it does the following checks described by the XML Rec
5029 * - check that ENTITY and ENTITIES type attributes default or
5030 * possible values matches one of the defined entities.
5031 * - check that NOTATION type attributes default or
5032 * possible values matches one of the defined notations.
5033 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005034 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00005035 */
5036
5037int
5038xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00005039 xmlDtdPtr dtd;
5040 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005041 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00005042
5043 if (doc == NULL) return(0);
5044 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5045 return(0);
5046 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005047 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005048 dtd = doc->intSubset;
5049 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5050 table = (xmlAttributeTablePtr) dtd->attributes;
5051 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005052 }
5053 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005054 entities = (xmlEntitiesTablePtr) dtd->entities;
5055 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5056 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005057 }
5058 dtd = doc->extSubset;
5059 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5060 table = (xmlAttributeTablePtr) dtd->attributes;
5061 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005062 }
5063 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005064 entities = (xmlEntitiesTablePtr) dtd->entities;
5065 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5066 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005067 }
5068 return(ctxt->valid);
5069}
5070
5071/**
5072 * xmlValidateDocument:
5073 * @ctxt: the validation context
5074 * @doc: a document instance
5075 *
5076 * Try to validate the document instance
5077 *
5078 * basically it does the all the checks described by the XML Rec
5079 * i.e. validates the internal and external subset (if present)
5080 * and validate the document tree.
5081 *
5082 * returns 1 if valid or 0 otherwise
5083 */
5084
5085int
5086xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5087 int ret;
5088 xmlNodePtr root;
5089
5090 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5091 return(0);
5092 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
5093 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
5094 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
5095 doc->intSubset->SystemID);
5096 if (doc->extSubset == NULL) {
5097 if (doc->intSubset->SystemID != NULL) {
5098 VERROR(ctxt->userData,
5099 "Could not load the external subset \"%s\"\n",
5100 doc->intSubset->SystemID);
5101 } else {
5102 VERROR(ctxt->userData,
5103 "Could not load the external subset \"%s\"\n",
5104 doc->intSubset->ExternalID);
5105 }
5106 return(0);
5107 }
5108 }
5109
5110 if (doc->ids != NULL) {
5111 xmlFreeIDTable(doc->ids);
5112 doc->ids = NULL;
5113 }
5114 if (doc->refs != NULL) {
5115 xmlFreeRefTable(doc->refs);
5116 doc->refs = NULL;
5117 }
5118 ret = xmlValidateDtdFinal(ctxt, doc);
5119 if (!xmlValidateRoot(ctxt, doc)) return(0);
5120
5121 root = xmlDocGetRootElement(doc);
5122 ret &= xmlValidateElement(ctxt, doc, root);
5123 ret &= xmlValidateDocumentFinal(ctxt, doc);
5124 return(ret);
5125}
5126
5127
5128/************************************************************************
5129 * *
5130 * Routines for dynamic validation editing *
5131 * *
5132 ************************************************************************/
5133
5134/**
5135 * xmlValidGetPotentialChildren:
5136 * @ctree: an element content tree
5137 * @list: an array to store the list of child names
5138 * @len: a pointer to the number of element in the list
5139 * @max: the size of the array
5140 *
5141 * Build/extend a list of potential children allowed by the content tree
5142 *
5143 * returns the number of element in the list, or -1 in case of error.
5144 */
5145
5146int
5147xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
5148 int *len, int max) {
5149 int i;
5150
5151 if ((ctree == NULL) || (list == NULL) || (len == NULL))
5152 return(-1);
5153 if (*len >= max) return(*len);
5154
5155 switch (ctree->type) {
5156 case XML_ELEMENT_CONTENT_PCDATA:
5157 for (i = 0; i < *len;i++)
5158 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
5159 list[(*len)++] = BAD_CAST "#PCDATA";
5160 break;
5161 case XML_ELEMENT_CONTENT_ELEMENT:
5162 for (i = 0; i < *len;i++)
5163 if (xmlStrEqual(ctree->name, list[i])) return(*len);
5164 list[(*len)++] = ctree->name;
5165 break;
5166 case XML_ELEMENT_CONTENT_SEQ:
5167 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5168 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5169 break;
5170 case XML_ELEMENT_CONTENT_OR:
5171 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5172 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5173 break;
5174 }
5175
5176 return(*len);
5177}
5178
5179/**
5180 * xmlValidGetValidElements:
5181 * @prev: an element to insert after
5182 * @next: an element to insert next
5183 * @list: an array to store the list of child names
5184 * @max: the size of the array
5185 *
5186 * This function returns the list of authorized children to insert
5187 * within an existing tree while respecting the validity constraints
5188 * forced by the Dtd. The insertion point is defined using @prev and
5189 * @next in the following ways:
5190 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
5191 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
5192 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
5193 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
5194 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
5195 *
5196 * pointers to the element names are inserted at the beginning of the array
5197 * and do not need to be freed.
5198 *
5199 * returns the number of element in the list, or -1 in case of error. If
5200 * the function returns the value @max the caller is invited to grow the
5201 * receiving array and retry.
5202 */
5203
5204int
5205xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
5206 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005207 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00005208 int nb_valid_elements = 0;
5209 const xmlChar *elements[256];
5210 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005211 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005212
5213 xmlNode *ref_node;
5214 xmlNode *parent;
5215 xmlNode *test_node;
5216
5217 xmlNode *prev_next;
5218 xmlNode *next_prev;
5219 xmlNode *parent_childs;
5220 xmlNode *parent_last;
5221
5222 xmlElement *element_desc;
5223
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005224 vctxt.userData = NULL;
5225 vctxt.error = NULL;
5226 vctxt.warning = NULL;
5227
Owen Taylor3473f882001-02-23 17:55:21 +00005228 if (prev == NULL && next == NULL)
5229 return(-1);
5230
5231 if (list == NULL) return(-1);
5232 if (max <= 0) return(-1);
5233
5234 nb_valid_elements = 0;
5235 ref_node = prev ? prev : next;
5236 parent = ref_node->parent;
5237
5238 /*
5239 * Retrieves the parent element declaration
5240 */
5241 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
5242 parent->name);
5243 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
5244 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
5245 parent->name);
5246 if (element_desc == NULL) return(-1);
5247
5248 /*
5249 * Do a backup of the current tree structure
5250 */
5251 prev_next = prev ? prev->next : NULL;
5252 next_prev = next ? next->prev : NULL;
5253 parent_childs = parent->children;
5254 parent_last = parent->last;
5255
5256 /*
5257 * Creates a dummy node and insert it into the tree
5258 */
5259 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
5260 test_node->doc = ref_node->doc;
5261 test_node->parent = parent;
5262 test_node->prev = prev;
5263 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00005264 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00005265
5266 if (prev) prev->next = test_node;
5267 else parent->children = test_node;
5268
5269 if (next) next->prev = test_node;
5270 else parent->last = test_node;
5271
5272 /*
5273 * Insert each potential child node and check if the parent is
5274 * still valid
5275 */
5276 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
5277 elements, &nb_elements, 256);
5278
5279 for (i = 0;i < nb_elements;i++) {
5280 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005281 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005282 int j;
5283
5284 for (j = 0; j < nb_valid_elements;j++)
5285 if (xmlStrEqual(elements[i], list[j])) break;
5286 list[nb_valid_elements++] = elements[i];
5287 if (nb_valid_elements >= max) break;
5288 }
5289 }
5290
5291 /*
5292 * Restore the tree structure
5293 */
5294 if (prev) prev->next = prev_next;
5295 if (next) next->prev = next_prev;
5296 parent->children = parent_childs;
5297 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00005298
5299 /*
5300 * Free up the dummy node
5301 */
5302 test_node->name = name;
5303 xmlFreeNode(test_node);
5304
Owen Taylor3473f882001-02-23 17:55:21 +00005305 return(nb_valid_elements);
5306}