blob: 36f2bec56b93bda4653c6b8c0f1eb1760c11039f [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
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <string.h>
14
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18
19#include <libxml/xmlmemory.h>
20#include <libxml/hash.h>
21#include <libxml/valid.h>
22#include <libxml/parser.h>
23#include <libxml/parserInternals.h>
24#include <libxml/xmlerror.h>
25#include <libxml/list.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000026#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000027
Daniel Veillarde62d36c2001-05-15 08:53:16 +000028/* #define DEBUG_VALID_ALGO */
29
Owen Taylor3473f882001-02-23 17:55:21 +000030/*
31 * Generic function for accessing stacks in the Validity Context
32 */
33
34#define PUSH_AND_POP(scope, type, name) \
35scope int name##VPush(xmlValidCtxtPtr ctxt, type value) { \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000036 if (ctxt->name##Max <= 0) { \
37 ctxt->name##Max = 4; \
38 ctxt->name##Tab = (type *) xmlMalloc( \
39 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
40 if (ctxt->name##Tab == NULL) { \
41 xmlGenericError(xmlGenericErrorContext, \
42 "malloc failed !\n"); \
Daniel Veillarda9142e72001-06-19 11:07:54 +000043 ctxt->name##Max = 0; \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000044 return(0); \
45 } \
46 } \
Owen Taylor3473f882001-02-23 17:55:21 +000047 if (ctxt->name##Nr >= ctxt->name##Max) { \
48 ctxt->name##Max *= 2; \
49 ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \
50 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
51 if (ctxt->name##Tab == NULL) { \
52 xmlGenericError(xmlGenericErrorContext, \
53 "realloc failed !\n"); \
54 return(0); \
55 } \
56 } \
57 ctxt->name##Tab[ctxt->name##Nr] = value; \
58 ctxt->name = value; \
59 return(ctxt->name##Nr++); \
60} \
61scope type name##VPop(xmlValidCtxtPtr ctxt) { \
62 type ret; \
63 if (ctxt->name##Nr <= 0) return(0); \
64 ctxt->name##Nr--; \
65 if (ctxt->name##Nr > 0) \
66 ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \
67 else \
68 ctxt->name = NULL; \
69 ret = ctxt->name##Tab[ctxt->name##Nr]; \
70 ctxt->name##Tab[ctxt->name##Nr] = 0; \
71 return(ret); \
72} \
73
Daniel Veillarddab4cb32001-04-20 13:03:48 +000074/*
Daniel Veillardb44025c2001-10-11 22:55:55 +000075 * I use a home made algorithm less complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +000076 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +000077 * only restriction is on the deepness of the tree limited by the
78 * size of the occurs bitfield
79 *
80 * this is the content of a saved state for rollbacks
81 */
82
83#define ROLLBACK_OR 0
84#define ROLLBACK_PARENT 1
85
Daniel Veillardb44025c2001-10-11 22:55:55 +000086typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +000087 xmlElementContentPtr cont; /* pointer to the content model subtree */
88 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +000089 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +000090 unsigned char depth; /* current depth in the overall tree */
91 unsigned char state; /* ROLLBACK_XXX */
92} _xmlValidState;
93
94#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
95#define CONT ctxt->vstate->cont
96#define NODE ctxt->vstate->node
97#define DEPTH ctxt->vstate->depth
98#define OCCURS ctxt->vstate->occurs
99#define STATE ctxt->vstate->state
100
Daniel Veillard5344c602001-12-31 16:37:34 +0000101#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
102#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000103
Daniel Veillard5344c602001-12-31 16:37:34 +0000104#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
105#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000106
107static int
108vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
109 xmlNodePtr node, unsigned char depth, long occurs,
110 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000111 int i = ctxt->vstateNr - 1;
112
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000113 if (ctxt->vstateNr >= ctxt->vstateMax) {
114 ctxt->vstateMax *= 2;
115 ctxt->vstateTab = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
116 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
117 if (ctxt->vstateTab == NULL) {
118 xmlGenericError(xmlGenericErrorContext,
119 "realloc failed !n");
120 return(0);
121 }
Daniel Veillard06803992001-04-22 10:35:56 +0000122 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000123 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000124 /*
125 * Don't push on the stack a state already here
126 */
127 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
128 (ctxt->vstateTab[i].node == node) &&
129 (ctxt->vstateTab[i].depth == depth) &&
130 (ctxt->vstateTab[i].occurs == occurs) &&
131 (ctxt->vstateTab[i].state == state))
132 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000133 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
134 ctxt->vstateTab[ctxt->vstateNr].node = node;
135 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
136 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
137 ctxt->vstateTab[ctxt->vstateNr].state = state;
138 return(ctxt->vstateNr++);
139}
140
141static int
142vstateVPop(xmlValidCtxtPtr ctxt) {
143 if (ctxt->vstateNr <= 1) return(-1);
144 ctxt->vstateNr--;
145 ctxt->vstate = &ctxt->vstateTab[0];
146 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
147 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
148 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
149 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
150 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
151 return(ctxt->vstateNr);
152}
153
Owen Taylor3473f882001-02-23 17:55:21 +0000154PUSH_AND_POP(static, xmlNodePtr, node)
155
Owen Taylor3473f882001-02-23 17:55:21 +0000156#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000157static void
158xmlValidPrintNode(xmlNodePtr cur) {
159 if (cur == NULL) {
160 xmlGenericError(xmlGenericErrorContext, "null");
161 return;
162 }
163 switch (cur->type) {
164 case XML_ELEMENT_NODE:
165 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
166 break;
167 case XML_TEXT_NODE:
168 xmlGenericError(xmlGenericErrorContext, "text ");
169 break;
170 case XML_CDATA_SECTION_NODE:
171 xmlGenericError(xmlGenericErrorContext, "cdata ");
172 break;
173 case XML_ENTITY_REF_NODE:
174 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
175 break;
176 case XML_PI_NODE:
177 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
178 break;
179 case XML_COMMENT_NODE:
180 xmlGenericError(xmlGenericErrorContext, "comment ");
181 break;
182 case XML_ATTRIBUTE_NODE:
183 xmlGenericError(xmlGenericErrorContext, "?attr? ");
184 break;
185 case XML_ENTITY_NODE:
186 xmlGenericError(xmlGenericErrorContext, "?ent? ");
187 break;
188 case XML_DOCUMENT_NODE:
189 xmlGenericError(xmlGenericErrorContext, "?doc? ");
190 break;
191 case XML_DOCUMENT_TYPE_NODE:
192 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
193 break;
194 case XML_DOCUMENT_FRAG_NODE:
195 xmlGenericError(xmlGenericErrorContext, "?frag? ");
196 break;
197 case XML_NOTATION_NODE:
198 xmlGenericError(xmlGenericErrorContext, "?nota? ");
199 break;
200 case XML_HTML_DOCUMENT_NODE:
201 xmlGenericError(xmlGenericErrorContext, "?html? ");
202 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000203#ifdef LIBXML_DOCB_ENABLED
204 case XML_DOCB_DOCUMENT_NODE:
205 xmlGenericError(xmlGenericErrorContext, "?docb? ");
206 break;
207#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000208 case XML_DTD_NODE:
209 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
210 break;
211 case XML_ELEMENT_DECL:
212 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
213 break;
214 case XML_ATTRIBUTE_DECL:
215 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
216 break;
217 case XML_ENTITY_DECL:
218 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
219 break;
220 case XML_NAMESPACE_DECL:
221 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
222 break;
223 case XML_XINCLUDE_START:
224 xmlGenericError(xmlGenericErrorContext, "incstart ");
225 break;
226 case XML_XINCLUDE_END:
227 xmlGenericError(xmlGenericErrorContext, "incend ");
228 break;
229 }
230}
231
232static void
233xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000234 if (cur == NULL)
235 xmlGenericError(xmlGenericErrorContext, "null ");
236 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000237 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000238 cur = cur->next;
239 }
240}
241
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000242static void
243xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Owen Taylor3473f882001-02-23 17:55:21 +0000244 char expr[1000];
245
246 expr[0] = 0;
247 xmlGenericError(xmlGenericErrorContext, "valid: ");
248 xmlValidPrintNodeList(cur);
249 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000250 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000251 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
252}
253
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000254static void
255xmlValidDebugState(xmlValidStatePtr state) {
256 xmlGenericError(xmlGenericErrorContext, "(");
257 if (state->cont == NULL)
258 xmlGenericError(xmlGenericErrorContext, "null,");
259 else
260 switch (state->cont->type) {
261 case XML_ELEMENT_CONTENT_PCDATA:
262 xmlGenericError(xmlGenericErrorContext, "pcdata,");
263 break;
264 case XML_ELEMENT_CONTENT_ELEMENT:
265 xmlGenericError(xmlGenericErrorContext, "%s,",
266 state->cont->name);
267 break;
268 case XML_ELEMENT_CONTENT_SEQ:
269 xmlGenericError(xmlGenericErrorContext, "seq,");
270 break;
271 case XML_ELEMENT_CONTENT_OR:
272 xmlGenericError(xmlGenericErrorContext, "or,");
273 break;
274 }
275 xmlValidPrintNode(state->node);
276 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
277 state->depth, state->occurs, state->state);
278}
279
280static void
281xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
282 int i, j;
283
284 xmlGenericError(xmlGenericErrorContext, "state: ");
285 xmlValidDebugState(ctxt->vstate);
286 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
287 ctxt->vstateNr - 1);
288 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
289 xmlValidDebugState(&ctxt->vstateTab[j]);
290 xmlGenericError(xmlGenericErrorContext, "\n");
291}
292
293/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000294#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000295 *****/
296
297#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000298#define DEBUG_VALID_MSG(m) \
299 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
300
Owen Taylor3473f882001-02-23 17:55:21 +0000301#else
302#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000303#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000304#endif
305
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000306/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000307
308#define VERROR \
309 if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error
310
311#define VWARNING \
312 if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning
313
314#define CHECK_DTD \
315 if (doc == NULL) return(0); \
316 else if ((doc->intSubset == NULL) && \
317 (doc->extSubset == NULL)) return(0)
318
Daniel Veillarda10efa82001-04-18 13:09:01 +0000319static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
320 int create);
Owen Taylor3473f882001-02-23 17:55:21 +0000321xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
322
323/************************************************************************
324 * *
325 * QName handling helper *
326 * *
327 ************************************************************************/
328
329/**
330 * xmlSplitQName2:
331 * @name: an XML parser context
332 * @prefix: a xmlChar **
333 *
334 * parse an XML qualified name string
335 *
336 * [NS 5] QName ::= (Prefix ':')? LocalPart
337 *
338 * [NS 6] Prefix ::= NCName
339 *
340 * [NS 7] LocalPart ::= NCName
341 *
342 * Returns NULL if not a QName, otherwise the local part, and prefix
343 * is updated to get the Prefix if any.
344 */
345
346xmlChar *
347xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
348 int len = 0;
349 xmlChar *ret = NULL;
350
351 *prefix = NULL;
352
Daniel Veillardf4309d72001-10-02 09:28:58 +0000353#ifndef XML_XML_NAMESPACE
Owen Taylor3473f882001-02-23 17:55:21 +0000354 /* xml: prefix is not really a namespace */
355 if ((name[0] == 'x') && (name[1] == 'm') &&
356 (name[2] == 'l') && (name[3] == ':'))
357 return(NULL);
Daniel Veillardf4309d72001-10-02 09:28:58 +0000358#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000359
360 /* nasty but valid */
361 if (name[0] == ':')
362 return(NULL);
363
364 /*
365 * we are not trying to validate but just to cut, and yes it will
366 * work even if this is as set of UTF-8 encoded chars
367 */
368 while ((name[len] != 0) && (name[len] != ':'))
369 len++;
370
371 if (name[len] == 0)
372 return(NULL);
373
374 *prefix = xmlStrndup(name, len);
375 ret = xmlStrdup(&name[len + 1]);
376
377 return(ret);
378}
379
380/****************************************************************
381 * *
382 * Util functions for data allocation/deallocation *
383 * *
384 ****************************************************************/
385
386/**
387 * xmlNewElementContent:
388 * @name: the subelement name or NULL
389 * @type: the type of element content decl
390 *
391 * Allocate an element content structure.
392 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000393 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000394 */
395xmlElementContentPtr
396xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
397 xmlElementContentPtr ret;
398
399 switch(type) {
400 case XML_ELEMENT_CONTENT_ELEMENT:
401 if (name == NULL) {
402 xmlGenericError(xmlGenericErrorContext,
403 "xmlNewElementContent : name == NULL !\n");
404 }
405 break;
406 case XML_ELEMENT_CONTENT_PCDATA:
407 case XML_ELEMENT_CONTENT_SEQ:
408 case XML_ELEMENT_CONTENT_OR:
409 if (name != NULL) {
410 xmlGenericError(xmlGenericErrorContext,
411 "xmlNewElementContent : name != NULL !\n");
412 }
413 break;
414 default:
415 xmlGenericError(xmlGenericErrorContext,
416 "xmlNewElementContent: unknown type %d\n", type);
417 return(NULL);
418 }
419 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
420 if (ret == NULL) {
421 xmlGenericError(xmlGenericErrorContext,
422 "xmlNewElementContent : out of memory!\n");
423 return(NULL);
424 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000425 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000426 ret->type = type;
427 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000428 if (name != NULL) {
429 xmlChar *prefix = NULL;
430 ret->name = xmlSplitQName2(name, &prefix);
431 if (ret->name == NULL)
432 ret->name = xmlStrdup(name);
433 ret->prefix = prefix;
434 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000435 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000436 ret->prefix = NULL;
437 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000438 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000439 return(ret);
440}
441
442/**
443 * xmlCopyElementContent:
444 * @content: An element content pointer.
445 *
446 * Build a copy of an element content description.
447 *
448 * Returns the new xmlElementContentPtr or NULL in case of error.
449 */
450xmlElementContentPtr
451xmlCopyElementContent(xmlElementContentPtr cur) {
452 xmlElementContentPtr ret;
453
454 if (cur == NULL) return(NULL);
455 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
456 if (ret == NULL) {
457 xmlGenericError(xmlGenericErrorContext,
458 "xmlCopyElementContent : out of memory\n");
459 return(NULL);
460 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000461 if (cur->prefix != NULL)
462 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000463 ret->ocur = cur->ocur;
464 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000465 if (ret->c1 != NULL)
466 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000467 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000468 if (ret->c2 != NULL)
469 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000470 return(ret);
471}
472
473/**
474 * xmlFreeElementContent:
475 * @cur: the element content tree to free
476 *
477 * Free an element content structure. This is a recursive call !
478 */
479void
480xmlFreeElementContent(xmlElementContentPtr cur) {
481 if (cur == NULL) return;
482 switch (cur->type) {
483 case XML_ELEMENT_CONTENT_PCDATA:
484 case XML_ELEMENT_CONTENT_ELEMENT:
485 case XML_ELEMENT_CONTENT_SEQ:
486 case XML_ELEMENT_CONTENT_OR:
487 break;
488 default:
489 xmlGenericError(xmlGenericErrorContext,
490 "xmlFreeElementContent : type %d\n", cur->type);
491 return;
492 }
493 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
494 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
495 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000496 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000497 xmlFree(cur);
498}
499
500/**
501 * xmlDumpElementContent:
502 * @buf: An XML buffer
503 * @content: An element table
504 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
505 *
506 * This will dump the content of the element table as an XML DTD definition
507 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000508static void
Owen Taylor3473f882001-02-23 17:55:21 +0000509xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
510 if (content == NULL) return;
511
512 if (glob) xmlBufferWriteChar(buf, "(");
513 switch (content->type) {
514 case XML_ELEMENT_CONTENT_PCDATA:
515 xmlBufferWriteChar(buf, "#PCDATA");
516 break;
517 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000518 if (content->prefix != NULL) {
519 xmlBufferWriteCHAR(buf, content->prefix);
520 xmlBufferWriteChar(buf, ":");
521 }
Owen Taylor3473f882001-02-23 17:55:21 +0000522 xmlBufferWriteCHAR(buf, content->name);
523 break;
524 case XML_ELEMENT_CONTENT_SEQ:
525 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
526 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
527 xmlDumpElementContent(buf, content->c1, 1);
528 else
529 xmlDumpElementContent(buf, content->c1, 0);
530 xmlBufferWriteChar(buf, " , ");
531 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
532 xmlDumpElementContent(buf, content->c2, 1);
533 else
534 xmlDumpElementContent(buf, content->c2, 0);
535 break;
536 case XML_ELEMENT_CONTENT_OR:
537 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
538 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
539 xmlDumpElementContent(buf, content->c1, 1);
540 else
541 xmlDumpElementContent(buf, content->c1, 0);
542 xmlBufferWriteChar(buf, " | ");
543 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
544 xmlDumpElementContent(buf, content->c2, 1);
545 else
546 xmlDumpElementContent(buf, content->c2, 0);
547 break;
548 default:
549 xmlGenericError(xmlGenericErrorContext,
550 "xmlDumpElementContent: unknown type %d\n",
551 content->type);
552 }
553 if (glob)
554 xmlBufferWriteChar(buf, ")");
555 switch (content->ocur) {
556 case XML_ELEMENT_CONTENT_ONCE:
557 break;
558 case XML_ELEMENT_CONTENT_OPT:
559 xmlBufferWriteChar(buf, "?");
560 break;
561 case XML_ELEMENT_CONTENT_MULT:
562 xmlBufferWriteChar(buf, "*");
563 break;
564 case XML_ELEMENT_CONTENT_PLUS:
565 xmlBufferWriteChar(buf, "+");
566 break;
567 }
568}
569
570/**
571 * xmlSprintfElementContent:
572 * @buf: an output buffer
573 * @content: An element table
574 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
575 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000576 * Deprecated, unsafe, use xmlSnprintfElementContent
577 */
578void
579xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
580 xmlElementContentPtr content ATTRIBUTE_UNUSED,
581 int glob ATTRIBUTE_UNUSED) {
582}
583
584/**
585 * xmlSnprintfElementContent:
586 * @buf: an output buffer
587 * @size: the buffer size
588 * @content: An element table
589 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
590 *
Owen Taylor3473f882001-02-23 17:55:21 +0000591 * This will dump the content of the element content definition
592 * Intended just for the debug routine
593 */
594void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000595xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
596 int len;
597
Owen Taylor3473f882001-02-23 17:55:21 +0000598 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000599 len = strlen(buf);
600 if (size - len < 50) {
601 if ((size - len > 4) && (buf[len - 1] != '.'))
602 strcat(buf, " ...");
603 return;
604 }
Owen Taylor3473f882001-02-23 17:55:21 +0000605 if (glob) strcat(buf, "(");
606 switch (content->type) {
607 case XML_ELEMENT_CONTENT_PCDATA:
608 strcat(buf, "#PCDATA");
609 break;
610 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000611 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000612 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000613 strcat(buf, " ...");
614 return;
615 }
616 strcat(buf, (char *) content->prefix);
617 strcat(buf, ":");
618 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000619 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +0000620 strcat(buf, " ...");
621 return;
622 }
Owen Taylor3473f882001-02-23 17:55:21 +0000623 strcat(buf, (char *) content->name);
624 break;
625 case XML_ELEMENT_CONTENT_SEQ:
626 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
627 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000628 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000629 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000630 xmlSnprintfElementContent(buf, size, content->c1, 0);
631 len = strlen(buf);
632 if (size - len < 50) {
633 if ((size - len > 4) && (buf[len - 1] != '.'))
634 strcat(buf, " ...");
635 return;
636 }
Owen Taylor3473f882001-02-23 17:55:21 +0000637 strcat(buf, " , ");
638 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000639 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000640 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000641 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000642 break;
643 case XML_ELEMENT_CONTENT_OR:
644 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
645 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000646 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000647 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000648 xmlSnprintfElementContent(buf, size, content->c1, 0);
649 len = strlen(buf);
650 if (size - len < 50) {
651 if ((size - len > 4) && (buf[len - 1] != '.'))
652 strcat(buf, " ...");
653 return;
654 }
Owen Taylor3473f882001-02-23 17:55:21 +0000655 strcat(buf, " | ");
656 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000657 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000658 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000659 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000660 break;
661 }
662 if (glob)
663 strcat(buf, ")");
664 switch (content->ocur) {
665 case XML_ELEMENT_CONTENT_ONCE:
666 break;
667 case XML_ELEMENT_CONTENT_OPT:
668 strcat(buf, "?");
669 break;
670 case XML_ELEMENT_CONTENT_MULT:
671 strcat(buf, "*");
672 break;
673 case XML_ELEMENT_CONTENT_PLUS:
674 strcat(buf, "+");
675 break;
676 }
677}
678
679/****************************************************************
680 * *
681 * Registration of DTD declarations *
682 * *
683 ****************************************************************/
684
685/**
686 * xmlCreateElementTable:
687 *
688 * create and initialize an empty element hash table.
689 *
690 * Returns the xmlElementTablePtr just created or NULL in case of error.
691 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000692static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000693xmlCreateElementTable(void) {
694 return(xmlHashCreate(0));
695}
696
697/**
698 * xmlFreeElement:
699 * @elem: An element
700 *
701 * Deallocate the memory used by an element definition
702 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000703static void
Owen Taylor3473f882001-02-23 17:55:21 +0000704xmlFreeElement(xmlElementPtr elem) {
705 if (elem == NULL) return;
706 xmlUnlinkNode((xmlNodePtr) elem);
707 xmlFreeElementContent(elem->content);
708 if (elem->name != NULL)
709 xmlFree((xmlChar *) elem->name);
710 if (elem->prefix != NULL)
711 xmlFree((xmlChar *) elem->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000712 xmlFree(elem);
713}
714
715
716/**
717 * xmlAddElementDecl:
718 * @ctxt: the validation context
719 * @dtd: pointer to the DTD
720 * @name: the entity name
721 * @type: the element type
722 * @content: the element content tree or NULL
723 *
724 * Register a new element declaration
725 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000726 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000727 */
728xmlElementPtr
729xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
730 xmlElementTypeVal type,
731 xmlElementContentPtr content) {
732 xmlElementPtr ret;
733 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +0000734 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000735 xmlChar *ns, *uqname;
736
737 if (dtd == NULL) {
738 xmlGenericError(xmlGenericErrorContext,
739 "xmlAddElementDecl: dtd == NULL\n");
740 return(NULL);
741 }
742 if (name == NULL) {
743 xmlGenericError(xmlGenericErrorContext,
744 "xmlAddElementDecl: name == NULL\n");
745 return(NULL);
746 }
747 switch (type) {
748 case XML_ELEMENT_TYPE_EMPTY:
749 if (content != NULL) {
750 xmlGenericError(xmlGenericErrorContext,
751 "xmlAddElementDecl: content != NULL for EMPTY\n");
752 return(NULL);
753 }
754 break;
755 case XML_ELEMENT_TYPE_ANY:
756 if (content != NULL) {
757 xmlGenericError(xmlGenericErrorContext,
758 "xmlAddElementDecl: content != NULL for ANY\n");
759 return(NULL);
760 }
761 break;
762 case XML_ELEMENT_TYPE_MIXED:
763 if (content == NULL) {
764 xmlGenericError(xmlGenericErrorContext,
765 "xmlAddElementDecl: content == NULL for MIXED\n");
766 return(NULL);
767 }
768 break;
769 case XML_ELEMENT_TYPE_ELEMENT:
770 if (content == NULL) {
771 xmlGenericError(xmlGenericErrorContext,
772 "xmlAddElementDecl: content == NULL for ELEMENT\n");
773 return(NULL);
774 }
775 break;
776 default:
777 xmlGenericError(xmlGenericErrorContext,
778 "xmlAddElementDecl: unknown type %d\n", type);
779 return(NULL);
780 }
781
782 /*
783 * check if name is a QName
784 */
785 uqname = xmlSplitQName2(name, &ns);
786 if (uqname != NULL)
787 name = uqname;
788
789 /*
790 * Create the Element table if needed.
791 */
792 table = (xmlElementTablePtr) dtd->elements;
793 if (table == NULL) {
794 table = xmlCreateElementTable();
795 dtd->elements = (void *) table;
796 }
797 if (table == NULL) {
798 xmlGenericError(xmlGenericErrorContext,
799 "xmlAddElementDecl: Table creation failed!\n");
800 return(NULL);
801 }
802
Daniel Veillarda10efa82001-04-18 13:09:01 +0000803 /*
804 * lookup old attributes inserted on an undefined element in the
805 * internal subset.
806 */
807 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
808 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
809 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
810 oldAttributes = ret->attributes;
811 ret->attributes = NULL;
812 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
813 xmlFreeElement(ret);
814 }
Owen Taylor3473f882001-02-23 17:55:21 +0000815 }
Owen Taylor3473f882001-02-23 17:55:21 +0000816
817 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +0000818 * The element may already be present if one of its attribute
819 * was registered first
820 */
821 ret = xmlHashLookup2(table, name, ns);
822 if (ret != NULL) {
823 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
824 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000825 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000826 */
827 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
828 if (uqname != NULL)
829 xmlFree(uqname);
830 return(NULL);
831 }
832 } else {
833 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
834 if (ret == NULL) {
835 xmlGenericError(xmlGenericErrorContext,
836 "xmlAddElementDecl: out of memory\n");
837 return(NULL);
838 }
839 memset(ret, 0, sizeof(xmlElement));
840 ret->type = XML_ELEMENT_DECL;
841
842 /*
843 * fill the structure.
844 */
845 ret->name = xmlStrdup(name);
846 ret->prefix = ns;
847
848 /*
849 * Validity Check:
850 * Insertion must not fail
851 */
852 if (xmlHashAddEntry2(table, name, ns, ret)) {
853 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000854 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000855 */
856 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
857 xmlFreeElement(ret);
858 if (uqname != NULL)
859 xmlFree(uqname);
860 return(NULL);
861 }
862 }
863
864 /*
865 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +0000866 */
867 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +0000868 ret->content = xmlCopyElementContent(content);
Daniel Veillarda10efa82001-04-18 13:09:01 +0000869 ret->attributes = oldAttributes;
Owen Taylor3473f882001-02-23 17:55:21 +0000870
871 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000872 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000873 */
874 ret->parent = dtd;
875 ret->doc = dtd->doc;
876 if (dtd->last == NULL) {
877 dtd->children = dtd->last = (xmlNodePtr) ret;
878 } else {
879 dtd->last->next = (xmlNodePtr) ret;
880 ret->prev = dtd->last;
881 dtd->last = (xmlNodePtr) ret;
882 }
883 if (uqname != NULL)
884 xmlFree(uqname);
885 return(ret);
886}
887
888/**
889 * xmlFreeElementTable:
890 * @table: An element table
891 *
892 * Deallocate the memory used by an element hash table.
893 */
894void
895xmlFreeElementTable(xmlElementTablePtr table) {
896 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
897}
898
899/**
900 * xmlCopyElement:
901 * @elem: An element
902 *
903 * Build a copy of an element.
904 *
905 * Returns the new xmlElementPtr or NULL in case of error.
906 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000907static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000908xmlCopyElement(xmlElementPtr elem) {
909 xmlElementPtr cur;
910
911 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
912 if (cur == NULL) {
913 xmlGenericError(xmlGenericErrorContext,
914 "xmlCopyElement: out of memory !\n");
915 return(NULL);
916 }
917 memset(cur, 0, sizeof(xmlElement));
918 cur->type = XML_ELEMENT_DECL;
919 cur->etype = elem->etype;
920 if (elem->name != NULL)
921 cur->name = xmlStrdup(elem->name);
922 else
923 cur->name = NULL;
924 if (elem->prefix != NULL)
925 cur->prefix = xmlStrdup(elem->prefix);
926 else
927 cur->prefix = NULL;
928 cur->content = xmlCopyElementContent(elem->content);
929 /* TODO : rebuild the attribute list on the copy */
930 cur->attributes = NULL;
931 return(cur);
932}
933
934/**
935 * xmlCopyElementTable:
936 * @table: An element table
937 *
938 * Build a copy of an element table.
939 *
940 * Returns the new xmlElementTablePtr or NULL in case of error.
941 */
942xmlElementTablePtr
943xmlCopyElementTable(xmlElementTablePtr table) {
944 return((xmlElementTablePtr) xmlHashCopy(table,
945 (xmlHashCopier) xmlCopyElement));
946}
947
948/**
949 * xmlDumpElementDecl:
950 * @buf: the XML buffer output
951 * @elem: An element table
952 *
953 * This will dump the content of the element declaration as an XML
954 * DTD definition
955 */
956void
957xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
958 switch (elem->etype) {
959 case XML_ELEMENT_TYPE_EMPTY:
960 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000961 if (elem->prefix != NULL) {
962 xmlBufferWriteCHAR(buf, elem->prefix);
963 xmlBufferWriteChar(buf, ":");
964 }
Owen Taylor3473f882001-02-23 17:55:21 +0000965 xmlBufferWriteCHAR(buf, elem->name);
966 xmlBufferWriteChar(buf, " EMPTY>\n");
967 break;
968 case XML_ELEMENT_TYPE_ANY:
969 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000970 if (elem->prefix != NULL) {
971 xmlBufferWriteCHAR(buf, elem->prefix);
972 xmlBufferWriteChar(buf, ":");
973 }
Owen Taylor3473f882001-02-23 17:55:21 +0000974 xmlBufferWriteCHAR(buf, elem->name);
975 xmlBufferWriteChar(buf, " ANY>\n");
976 break;
977 case XML_ELEMENT_TYPE_MIXED:
978 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000979 if (elem->prefix != NULL) {
980 xmlBufferWriteCHAR(buf, elem->prefix);
981 xmlBufferWriteChar(buf, ":");
982 }
Owen Taylor3473f882001-02-23 17:55:21 +0000983 xmlBufferWriteCHAR(buf, elem->name);
984 xmlBufferWriteChar(buf, " ");
985 xmlDumpElementContent(buf, elem->content, 1);
986 xmlBufferWriteChar(buf, ">\n");
987 break;
988 case XML_ELEMENT_TYPE_ELEMENT:
989 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000990 if (elem->prefix != NULL) {
991 xmlBufferWriteCHAR(buf, elem->prefix);
992 xmlBufferWriteChar(buf, ":");
993 }
Owen Taylor3473f882001-02-23 17:55:21 +0000994 xmlBufferWriteCHAR(buf, elem->name);
995 xmlBufferWriteChar(buf, " ");
996 xmlDumpElementContent(buf, elem->content, 1);
997 xmlBufferWriteChar(buf, ">\n");
998 break;
999 default:
1000 xmlGenericError(xmlGenericErrorContext,
1001 "xmlDumpElementDecl: internal: unknown type %d\n",
1002 elem->etype);
1003 }
1004}
1005
1006/**
1007 * xmlDumpElementTable:
1008 * @buf: the XML buffer output
1009 * @table: An element table
1010 *
1011 * This will dump the content of the element table as an XML DTD definition
1012 */
1013void
1014xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1015 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1016}
1017
1018/**
1019 * xmlCreateEnumeration:
1020 * @name: the enumeration name or NULL
1021 *
1022 * create and initialize an enumeration attribute node.
1023 *
1024 * Returns the xmlEnumerationPtr just created or NULL in case
1025 * of error.
1026 */
1027xmlEnumerationPtr
1028xmlCreateEnumeration(xmlChar *name) {
1029 xmlEnumerationPtr ret;
1030
1031 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1032 if (ret == NULL) {
1033 xmlGenericError(xmlGenericErrorContext,
1034 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1035 (long)sizeof(xmlEnumeration));
1036 return(NULL);
1037 }
1038 memset(ret, 0, sizeof(xmlEnumeration));
1039
1040 if (name != NULL)
1041 ret->name = xmlStrdup(name);
1042 return(ret);
1043}
1044
1045/**
1046 * xmlFreeEnumeration:
1047 * @cur: the tree to free.
1048 *
1049 * free an enumeration attribute node (recursive).
1050 */
1051void
1052xmlFreeEnumeration(xmlEnumerationPtr cur) {
1053 if (cur == NULL) return;
1054
1055 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1056
1057 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001058 xmlFree(cur);
1059}
1060
1061/**
1062 * xmlCopyEnumeration:
1063 * @cur: the tree to copy.
1064 *
1065 * Copy an enumeration attribute node (recursive).
1066 *
1067 * Returns the xmlEnumerationPtr just created or NULL in case
1068 * of error.
1069 */
1070xmlEnumerationPtr
1071xmlCopyEnumeration(xmlEnumerationPtr cur) {
1072 xmlEnumerationPtr ret;
1073
1074 if (cur == NULL) return(NULL);
1075 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1076
1077 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1078 else ret->next = NULL;
1079
1080 return(ret);
1081}
1082
1083/**
1084 * xmlDumpEnumeration:
1085 * @buf: the XML buffer output
1086 * @enum: An enumeration
1087 *
1088 * This will dump the content of the enumeration
1089 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001090static void
Owen Taylor3473f882001-02-23 17:55:21 +00001091xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1092 if (cur == NULL) return;
1093
1094 xmlBufferWriteCHAR(buf, cur->name);
1095 if (cur->next == NULL)
1096 xmlBufferWriteChar(buf, ")");
1097 else {
1098 xmlBufferWriteChar(buf, " | ");
1099 xmlDumpEnumeration(buf, cur->next);
1100 }
1101}
1102
1103/**
1104 * xmlCreateAttributeTable:
1105 *
1106 * create and initialize an empty attribute hash table.
1107 *
1108 * Returns the xmlAttributeTablePtr just created or NULL in case
1109 * of error.
1110 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001111static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001112xmlCreateAttributeTable(void) {
1113 return(xmlHashCreate(0));
1114}
1115
1116/**
1117 * xmlScanAttributeDeclCallback:
1118 * @attr: the attribute decl
1119 * @list: the list to update
1120 *
1121 * Callback called by xmlScanAttributeDecl when a new attribute
1122 * has to be entered in the list.
1123 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001124static void
Owen Taylor3473f882001-02-23 17:55:21 +00001125xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001126 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001127 attr->nexth = *list;
1128 *list = attr;
1129}
1130
1131/**
1132 * xmlScanAttributeDecl:
1133 * @dtd: pointer to the DTD
1134 * @elem: the element name
1135 *
1136 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001137 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001138 *
1139 * Returns the pointer to the first attribute decl in the chain,
1140 * possibly NULL.
1141 */
1142xmlAttributePtr
1143xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1144 xmlAttributePtr ret = NULL;
1145 xmlAttributeTablePtr table;
1146
1147 if (dtd == NULL) {
1148 xmlGenericError(xmlGenericErrorContext,
1149 "xmlScanAttributeDecl: dtd == NULL\n");
1150 return(NULL);
1151 }
1152 if (elem == NULL) {
1153 xmlGenericError(xmlGenericErrorContext,
1154 "xmlScanAttributeDecl: elem == NULL\n");
1155 return(NULL);
1156 }
1157 table = (xmlAttributeTablePtr) dtd->attributes;
1158 if (table == NULL)
1159 return(NULL);
1160
1161 /* WRONG !!! */
1162 xmlHashScan3(table, NULL, NULL, elem,
1163 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1164 return(ret);
1165}
1166
1167/**
1168 * xmlScanIDAttributeDecl:
1169 * @ctxt: the validation context
1170 * @elem: the element name
1171 *
1172 * Verify that the element don't have too many ID attributes
1173 * declared.
1174 *
1175 * Returns the number of ID attributes found.
1176 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001177static int
Owen Taylor3473f882001-02-23 17:55:21 +00001178xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1179 xmlAttributePtr cur;
1180 int ret = 0;
1181
1182 if (elem == NULL) return(0);
1183 cur = elem->attributes;
1184 while (cur != NULL) {
1185 if (cur->atype == XML_ATTRIBUTE_ID) {
1186 ret ++;
1187 if (ret > 1)
1188 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001189 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001190 elem->name, cur->name);
1191 }
1192 cur = cur->nexth;
1193 }
1194 return(ret);
1195}
1196
1197/**
1198 * xmlFreeAttribute:
1199 * @elem: An attribute
1200 *
1201 * Deallocate the memory used by an attribute definition
1202 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001203static void
Owen Taylor3473f882001-02-23 17:55:21 +00001204xmlFreeAttribute(xmlAttributePtr attr) {
1205 if (attr == NULL) return;
1206 xmlUnlinkNode((xmlNodePtr) attr);
1207 if (attr->tree != NULL)
1208 xmlFreeEnumeration(attr->tree);
1209 if (attr->elem != NULL)
1210 xmlFree((xmlChar *) attr->elem);
1211 if (attr->name != NULL)
1212 xmlFree((xmlChar *) attr->name);
1213 if (attr->defaultValue != NULL)
1214 xmlFree((xmlChar *) attr->defaultValue);
1215 if (attr->prefix != NULL)
1216 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001217 xmlFree(attr);
1218}
1219
1220
1221/**
1222 * xmlAddAttributeDecl:
1223 * @ctxt: the validation context
1224 * @dtd: pointer to the DTD
1225 * @elem: the element name
1226 * @name: the attribute name
1227 * @ns: the attribute namespace prefix
1228 * @type: the attribute type
1229 * @def: the attribute default type
1230 * @defaultValue: the attribute default value
1231 * @tree: if it's an enumeration, the associated list
1232 *
1233 * Register a new attribute declaration
1234 * Note that @tree becomes the ownership of the DTD
1235 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001236 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001237 */
1238xmlAttributePtr
1239xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1240 const xmlChar *name, const xmlChar *ns,
1241 xmlAttributeType type, xmlAttributeDefault def,
1242 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1243 xmlAttributePtr ret;
1244 xmlAttributeTablePtr table;
1245 xmlElementPtr elemDef;
1246
1247 if (dtd == NULL) {
1248 xmlGenericError(xmlGenericErrorContext,
1249 "xmlAddAttributeDecl: dtd == NULL\n");
1250 xmlFreeEnumeration(tree);
1251 return(NULL);
1252 }
1253 if (name == NULL) {
1254 xmlGenericError(xmlGenericErrorContext,
1255 "xmlAddAttributeDecl: name == NULL\n");
1256 xmlFreeEnumeration(tree);
1257 return(NULL);
1258 }
1259 if (elem == NULL) {
1260 xmlGenericError(xmlGenericErrorContext,
1261 "xmlAddAttributeDecl: elem == NULL\n");
1262 xmlFreeEnumeration(tree);
1263 return(NULL);
1264 }
1265 /*
1266 * Check the type and possibly the default value.
1267 */
1268 switch (type) {
1269 case XML_ATTRIBUTE_CDATA:
1270 break;
1271 case XML_ATTRIBUTE_ID:
1272 break;
1273 case XML_ATTRIBUTE_IDREF:
1274 break;
1275 case XML_ATTRIBUTE_IDREFS:
1276 break;
1277 case XML_ATTRIBUTE_ENTITY:
1278 break;
1279 case XML_ATTRIBUTE_ENTITIES:
1280 break;
1281 case XML_ATTRIBUTE_NMTOKEN:
1282 break;
1283 case XML_ATTRIBUTE_NMTOKENS:
1284 break;
1285 case XML_ATTRIBUTE_ENUMERATION:
1286 break;
1287 case XML_ATTRIBUTE_NOTATION:
1288 break;
1289 default:
1290 xmlGenericError(xmlGenericErrorContext,
1291 "xmlAddAttributeDecl: unknown type %d\n", type);
1292 xmlFreeEnumeration(tree);
1293 return(NULL);
1294 }
1295 if ((defaultValue != NULL) &&
1296 (!xmlValidateAttributeValue(type, defaultValue))) {
1297 VERROR(ctxt->userData, "Attribute %s on %s: invalid default value\n",
1298 elem, name, defaultValue);
1299 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001300 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001301 }
1302
1303 /*
1304 * Create the Attribute table if needed.
1305 */
1306 table = (xmlAttributeTablePtr) dtd->attributes;
1307 if (table == NULL) {
1308 table = xmlCreateAttributeTable();
1309 dtd->attributes = (void *) table;
1310 }
1311 if (table == NULL) {
1312 xmlGenericError(xmlGenericErrorContext,
1313 "xmlAddAttributeDecl: Table creation failed!\n");
1314 return(NULL);
1315 }
1316
1317
1318 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1319 if (ret == NULL) {
1320 xmlGenericError(xmlGenericErrorContext,
1321 "xmlAddAttributeDecl: out of memory\n");
1322 return(NULL);
1323 }
1324 memset(ret, 0, sizeof(xmlAttribute));
1325 ret->type = XML_ATTRIBUTE_DECL;
1326
1327 /*
1328 * fill the structure.
1329 */
1330 ret->atype = type;
1331 ret->name = xmlStrdup(name);
1332 ret->prefix = xmlStrdup(ns);
1333 ret->elem = xmlStrdup(elem);
1334 ret->def = def;
1335 ret->tree = tree;
1336 if (defaultValue != NULL)
1337 ret->defaultValue = xmlStrdup(defaultValue);
1338
1339 /*
1340 * Validity Check:
1341 * Search the DTD for previous declarations of the ATTLIST
1342 */
1343 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1344 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001345 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001346 */
1347 VWARNING(ctxt->userData,
1348 "Attribute %s on %s: already defined\n",
1349 name, elem);
1350 xmlFreeAttribute(ret);
1351 return(NULL);
1352 }
1353
1354 /*
1355 * Validity Check:
1356 * Multiple ID per element
1357 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001358 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001359 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001360
Owen Taylor3473f882001-02-23 17:55:21 +00001361 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001362 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001363 VERROR(ctxt->userData,
1364 "Element %s has too may ID attributes defined : %s\n",
1365 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001366 ctxt->valid = 0;
1367 }
1368
Daniel Veillard48da9102001-08-07 01:10:10 +00001369 /*
1370 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001371 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001372 */
1373 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1374 ((ret->prefix != NULL &&
1375 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1376 ret->nexth = elemDef->attributes;
1377 elemDef->attributes = ret;
1378 } else {
1379 xmlAttributePtr tmp = elemDef->attributes;
1380
1381 while ((tmp != NULL) &&
1382 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1383 ((ret->prefix != NULL &&
1384 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1385 if (tmp->nexth == NULL)
1386 break;
1387 tmp = tmp->nexth;
1388 }
1389 if (tmp != NULL) {
1390 ret->nexth = tmp->nexth;
1391 tmp->nexth = ret;
1392 } else {
1393 ret->nexth = elemDef->attributes;
1394 elemDef->attributes = ret;
1395 }
1396 }
Owen Taylor3473f882001-02-23 17:55:21 +00001397 }
1398
1399 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001400 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001401 */
1402 ret->parent = dtd;
1403 ret->doc = dtd->doc;
1404 if (dtd->last == NULL) {
1405 dtd->children = dtd->last = (xmlNodePtr) ret;
1406 } else {
1407 dtd->last->next = (xmlNodePtr) ret;
1408 ret->prev = dtd->last;
1409 dtd->last = (xmlNodePtr) ret;
1410 }
1411 return(ret);
1412}
1413
1414/**
1415 * xmlFreeAttributeTable:
1416 * @table: An attribute table
1417 *
1418 * Deallocate the memory used by an entities hash table.
1419 */
1420void
1421xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1422 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1423}
1424
1425/**
1426 * xmlCopyAttribute:
1427 * @attr: An attribute
1428 *
1429 * Build a copy of an attribute.
1430 *
1431 * Returns the new xmlAttributePtr or NULL in case of error.
1432 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001433static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001434xmlCopyAttribute(xmlAttributePtr attr) {
1435 xmlAttributePtr cur;
1436
1437 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1438 if (cur == NULL) {
1439 xmlGenericError(xmlGenericErrorContext,
1440 "xmlCopyAttribute: out of memory !\n");
1441 return(NULL);
1442 }
1443 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001444 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001445 cur->atype = attr->atype;
1446 cur->def = attr->def;
1447 cur->tree = xmlCopyEnumeration(attr->tree);
1448 if (attr->elem != NULL)
1449 cur->elem = xmlStrdup(attr->elem);
1450 if (attr->name != NULL)
1451 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001452 if (attr->prefix != NULL)
1453 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001454 if (attr->defaultValue != NULL)
1455 cur->defaultValue = xmlStrdup(attr->defaultValue);
1456 return(cur);
1457}
1458
1459/**
1460 * xmlCopyAttributeTable:
1461 * @table: An attribute table
1462 *
1463 * Build a copy of an attribute table.
1464 *
1465 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1466 */
1467xmlAttributeTablePtr
1468xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1469 return((xmlAttributeTablePtr) xmlHashCopy(table,
1470 (xmlHashCopier) xmlCopyAttribute));
1471}
1472
1473/**
1474 * xmlDumpAttributeDecl:
1475 * @buf: the XML buffer output
1476 * @attr: An attribute declaration
1477 *
1478 * This will dump the content of the attribute declaration as an XML
1479 * DTD definition
1480 */
1481void
1482xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1483 xmlBufferWriteChar(buf, "<!ATTLIST ");
1484 xmlBufferWriteCHAR(buf, attr->elem);
1485 xmlBufferWriteChar(buf, " ");
1486 if (attr->prefix != NULL) {
1487 xmlBufferWriteCHAR(buf, attr->prefix);
1488 xmlBufferWriteChar(buf, ":");
1489 }
1490 xmlBufferWriteCHAR(buf, attr->name);
1491 switch (attr->atype) {
1492 case XML_ATTRIBUTE_CDATA:
1493 xmlBufferWriteChar(buf, " CDATA");
1494 break;
1495 case XML_ATTRIBUTE_ID:
1496 xmlBufferWriteChar(buf, " ID");
1497 break;
1498 case XML_ATTRIBUTE_IDREF:
1499 xmlBufferWriteChar(buf, " IDREF");
1500 break;
1501 case XML_ATTRIBUTE_IDREFS:
1502 xmlBufferWriteChar(buf, " IDREFS");
1503 break;
1504 case XML_ATTRIBUTE_ENTITY:
1505 xmlBufferWriteChar(buf, " ENTITY");
1506 break;
1507 case XML_ATTRIBUTE_ENTITIES:
1508 xmlBufferWriteChar(buf, " ENTITIES");
1509 break;
1510 case XML_ATTRIBUTE_NMTOKEN:
1511 xmlBufferWriteChar(buf, " NMTOKEN");
1512 break;
1513 case XML_ATTRIBUTE_NMTOKENS:
1514 xmlBufferWriteChar(buf, " NMTOKENS");
1515 break;
1516 case XML_ATTRIBUTE_ENUMERATION:
1517 xmlBufferWriteChar(buf, " (");
1518 xmlDumpEnumeration(buf, attr->tree);
1519 break;
1520 case XML_ATTRIBUTE_NOTATION:
1521 xmlBufferWriteChar(buf, " NOTATION (");
1522 xmlDumpEnumeration(buf, attr->tree);
1523 break;
1524 default:
1525 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001526 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001527 attr->atype);
1528 }
1529 switch (attr->def) {
1530 case XML_ATTRIBUTE_NONE:
1531 break;
1532 case XML_ATTRIBUTE_REQUIRED:
1533 xmlBufferWriteChar(buf, " #REQUIRED");
1534 break;
1535 case XML_ATTRIBUTE_IMPLIED:
1536 xmlBufferWriteChar(buf, " #IMPLIED");
1537 break;
1538 case XML_ATTRIBUTE_FIXED:
1539 xmlBufferWriteChar(buf, " #FIXED");
1540 break;
1541 default:
1542 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001543 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001544 attr->def);
1545 }
1546 if (attr->defaultValue != NULL) {
1547 xmlBufferWriteChar(buf, " ");
1548 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1549 }
1550 xmlBufferWriteChar(buf, ">\n");
1551}
1552
1553/**
1554 * xmlDumpAttributeTable:
1555 * @buf: the XML buffer output
1556 * @table: An attribute table
1557 *
1558 * This will dump the content of the attribute table as an XML DTD definition
1559 */
1560void
1561xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1562 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1563}
1564
1565/************************************************************************
1566 * *
1567 * NOTATIONs *
1568 * *
1569 ************************************************************************/
1570/**
1571 * xmlCreateNotationTable:
1572 *
1573 * create and initialize an empty notation hash table.
1574 *
1575 * Returns the xmlNotationTablePtr just created or NULL in case
1576 * of error.
1577 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001578static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001579xmlCreateNotationTable(void) {
1580 return(xmlHashCreate(0));
1581}
1582
1583/**
1584 * xmlFreeNotation:
1585 * @not: A notation
1586 *
1587 * Deallocate the memory used by an notation definition
1588 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001589static void
Owen Taylor3473f882001-02-23 17:55:21 +00001590xmlFreeNotation(xmlNotationPtr nota) {
1591 if (nota == NULL) return;
1592 if (nota->name != NULL)
1593 xmlFree((xmlChar *) nota->name);
1594 if (nota->PublicID != NULL)
1595 xmlFree((xmlChar *) nota->PublicID);
1596 if (nota->SystemID != NULL)
1597 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001598 xmlFree(nota);
1599}
1600
1601
1602/**
1603 * xmlAddNotationDecl:
1604 * @dtd: pointer to the DTD
1605 * @ctxt: the validation context
1606 * @name: the entity name
1607 * @PublicID: the public identifier or NULL
1608 * @SystemID: the system identifier or NULL
1609 *
1610 * Register a new notation declaration
1611 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001612 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001613 */
1614xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001615xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001616 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001617 const xmlChar *PublicID, const xmlChar *SystemID) {
1618 xmlNotationPtr ret;
1619 xmlNotationTablePtr table;
1620
1621 if (dtd == NULL) {
1622 xmlGenericError(xmlGenericErrorContext,
1623 "xmlAddNotationDecl: dtd == NULL\n");
1624 return(NULL);
1625 }
1626 if (name == NULL) {
1627 xmlGenericError(xmlGenericErrorContext,
1628 "xmlAddNotationDecl: name == NULL\n");
1629 return(NULL);
1630 }
1631 if ((PublicID == NULL) && (SystemID == NULL)) {
1632 xmlGenericError(xmlGenericErrorContext,
1633 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00001634 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001635 }
1636
1637 /*
1638 * Create the Notation table if needed.
1639 */
1640 table = (xmlNotationTablePtr) dtd->notations;
1641 if (table == NULL)
1642 dtd->notations = table = xmlCreateNotationTable();
1643 if (table == NULL) {
1644 xmlGenericError(xmlGenericErrorContext,
1645 "xmlAddNotationDecl: Table creation failed!\n");
1646 return(NULL);
1647 }
1648
1649 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1650 if (ret == NULL) {
1651 xmlGenericError(xmlGenericErrorContext,
1652 "xmlAddNotationDecl: out of memory\n");
1653 return(NULL);
1654 }
1655 memset(ret, 0, sizeof(xmlNotation));
1656
1657 /*
1658 * fill the structure.
1659 */
1660 ret->name = xmlStrdup(name);
1661 if (SystemID != NULL)
1662 ret->SystemID = xmlStrdup(SystemID);
1663 if (PublicID != NULL)
1664 ret->PublicID = xmlStrdup(PublicID);
1665
1666 /*
1667 * Validity Check:
1668 * Check the DTD for previous declarations of the ATTLIST
1669 */
1670 if (xmlHashAddEntry(table, name, ret)) {
1671 xmlGenericError(xmlGenericErrorContext,
1672 "xmlAddNotationDecl: %s already defined\n", name);
1673 xmlFreeNotation(ret);
1674 return(NULL);
1675 }
1676 return(ret);
1677}
1678
1679/**
1680 * xmlFreeNotationTable:
1681 * @table: An notation table
1682 *
1683 * Deallocate the memory used by an entities hash table.
1684 */
1685void
1686xmlFreeNotationTable(xmlNotationTablePtr table) {
1687 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
1688}
1689
1690/**
1691 * xmlCopyNotation:
1692 * @nota: A notation
1693 *
1694 * Build a copy of a notation.
1695 *
1696 * Returns the new xmlNotationPtr or NULL in case of error.
1697 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001698static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001699xmlCopyNotation(xmlNotationPtr nota) {
1700 xmlNotationPtr cur;
1701
1702 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1703 if (cur == NULL) {
1704 xmlGenericError(xmlGenericErrorContext,
1705 "xmlCopyNotation: out of memory !\n");
1706 return(NULL);
1707 }
1708 if (nota->name != NULL)
1709 cur->name = xmlStrdup(nota->name);
1710 else
1711 cur->name = NULL;
1712 if (nota->PublicID != NULL)
1713 cur->PublicID = xmlStrdup(nota->PublicID);
1714 else
1715 cur->PublicID = NULL;
1716 if (nota->SystemID != NULL)
1717 cur->SystemID = xmlStrdup(nota->SystemID);
1718 else
1719 cur->SystemID = NULL;
1720 return(cur);
1721}
1722
1723/**
1724 * xmlCopyNotationTable:
1725 * @table: A notation table
1726 *
1727 * Build a copy of a notation table.
1728 *
1729 * Returns the new xmlNotationTablePtr or NULL in case of error.
1730 */
1731xmlNotationTablePtr
1732xmlCopyNotationTable(xmlNotationTablePtr table) {
1733 return((xmlNotationTablePtr) xmlHashCopy(table,
1734 (xmlHashCopier) xmlCopyNotation));
1735}
1736
1737/**
1738 * xmlDumpNotationDecl:
1739 * @buf: the XML buffer output
1740 * @nota: A notation declaration
1741 *
1742 * This will dump the content the notation declaration as an XML DTD definition
1743 */
1744void
1745xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
1746 xmlBufferWriteChar(buf, "<!NOTATION ");
1747 xmlBufferWriteCHAR(buf, nota->name);
1748 if (nota->PublicID != NULL) {
1749 xmlBufferWriteChar(buf, " PUBLIC ");
1750 xmlBufferWriteQuotedString(buf, nota->PublicID);
1751 if (nota->SystemID != NULL) {
1752 xmlBufferWriteChar(buf, " ");
1753 xmlBufferWriteCHAR(buf, nota->SystemID);
1754 }
1755 } else {
1756 xmlBufferWriteChar(buf, " SYSTEM ");
1757 xmlBufferWriteCHAR(buf, nota->SystemID);
1758 }
1759 xmlBufferWriteChar(buf, " >\n");
1760}
1761
1762/**
1763 * xmlDumpNotationTable:
1764 * @buf: the XML buffer output
1765 * @table: A notation table
1766 *
1767 * This will dump the content of the notation table as an XML DTD definition
1768 */
1769void
1770xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
1771 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
1772}
1773
1774/************************************************************************
1775 * *
1776 * IDs *
1777 * *
1778 ************************************************************************/
1779/**
1780 * xmlCreateIDTable:
1781 *
1782 * create and initialize an empty id hash table.
1783 *
1784 * Returns the xmlIDTablePtr just created or NULL in case
1785 * of error.
1786 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001787static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001788xmlCreateIDTable(void) {
1789 return(xmlHashCreate(0));
1790}
1791
1792/**
1793 * xmlFreeID:
1794 * @not: A id
1795 *
1796 * Deallocate the memory used by an id definition
1797 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001798static void
Owen Taylor3473f882001-02-23 17:55:21 +00001799xmlFreeID(xmlIDPtr id) {
1800 if (id == NULL) return;
1801 if (id->value != NULL)
1802 xmlFree((xmlChar *) id->value);
Owen Taylor3473f882001-02-23 17:55:21 +00001803 xmlFree(id);
1804}
1805
1806/**
1807 * xmlAddID:
1808 * @ctxt: the validation context
1809 * @doc: pointer to the document
1810 * @value: the value name
1811 * @attr: the attribute holding the ID
1812 *
1813 * Register a new id declaration
1814 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001815 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001816 */
1817xmlIDPtr
1818xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
1819 xmlAttrPtr attr) {
1820 xmlIDPtr ret;
1821 xmlIDTablePtr table;
1822
1823 if (doc == NULL) {
1824 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001825 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001826 return(NULL);
1827 }
1828 if (value == NULL) {
1829 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001830 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001831 return(NULL);
1832 }
1833 if (attr == NULL) {
1834 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001835 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001836 return(NULL);
1837 }
1838
1839 /*
1840 * Create the ID table if needed.
1841 */
1842 table = (xmlIDTablePtr) doc->ids;
1843 if (table == NULL)
1844 doc->ids = table = xmlCreateIDTable();
1845 if (table == NULL) {
1846 xmlGenericError(xmlGenericErrorContext,
1847 "xmlAddID: Table creation failed!\n");
1848 return(NULL);
1849 }
1850
1851 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
1852 if (ret == NULL) {
1853 xmlGenericError(xmlGenericErrorContext,
1854 "xmlAddID: out of memory\n");
1855 return(NULL);
1856 }
1857
1858 /*
1859 * fill the structure.
1860 */
1861 ret->value = xmlStrdup(value);
1862 ret->attr = attr;
1863
1864 if (xmlHashAddEntry(table, value, ret) < 0) {
1865 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001866 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001867 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001868 if (ctxt != NULL)
1869 VERROR(ctxt->userData, "ID %s already defined\n", value);
Owen Taylor3473f882001-02-23 17:55:21 +00001870 xmlFreeID(ret);
1871 return(NULL);
1872 }
1873 return(ret);
1874}
1875
1876/**
1877 * xmlFreeIDTable:
1878 * @table: An id table
1879 *
1880 * Deallocate the memory used by an ID hash table.
1881 */
1882void
1883xmlFreeIDTable(xmlIDTablePtr table) {
1884 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
1885}
1886
1887/**
1888 * xmlIsID:
1889 * @doc: the document
1890 * @elem: the element carrying the attribute
1891 * @attr: the attribute
1892 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001893 * Determine whether an attribute is of type ID. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00001894 * then this is simple, otherwise we use an heuristic: name ID (upper
1895 * or lowercase).
1896 *
1897 * Returns 0 or 1 depending on the lookup result
1898 */
1899int
1900xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
1901 if (doc == NULL) return(0);
1902 if (attr == NULL) return(0);
1903 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
1904 return(0);
1905 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1906 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
1907 (xmlStrEqual(BAD_CAST "name", attr->name)))
1908 return(1);
1909 return(0);
1910 } else {
1911 xmlAttributePtr attrDecl;
1912
1913 if (elem == NULL) return(0);
1914 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
1915 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1916 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
1917 attr->name);
1918
1919 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
1920 return(1);
1921 }
1922 return(0);
1923}
1924
1925/**
1926 * xmlRemoveID
1927 * @doc: the document
1928 * @attr: the attribute
1929 *
1930 * Remove the given attribute from the ID table maintained internally.
1931 *
1932 * Returns -1 if the lookup failed and 0 otherwise
1933 */
1934int
1935xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
1936 xmlAttrPtr cur;
1937 xmlIDTablePtr table;
1938 xmlChar *ID;
1939
1940 if (doc == NULL) return(-1);
1941 if (attr == NULL) return(-1);
1942 table = (xmlIDTablePtr) doc->ids;
1943 if (table == NULL)
1944 return(-1);
1945
1946 if (attr == NULL)
1947 return(-1);
1948 ID = xmlNodeListGetString(doc, attr->children, 1);
1949 if (ID == NULL)
1950 return(-1);
1951 cur = xmlHashLookup(table, ID);
1952 if (cur != attr) {
1953 xmlFree(ID);
1954 return(-1);
1955 }
1956 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
1957 xmlFree(ID);
1958 return(0);
1959}
1960
1961/**
1962 * xmlGetID:
1963 * @doc: pointer to the document
1964 * @ID: the ID value
1965 *
1966 * Search the attribute declaring the given ID
1967 *
1968 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
1969 */
1970xmlAttrPtr
1971xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
1972 xmlIDTablePtr table;
1973 xmlIDPtr id;
1974
1975 if (doc == NULL) {
1976 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
1977 return(NULL);
1978 }
1979
1980 if (ID == NULL) {
1981 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
1982 return(NULL);
1983 }
1984
1985 table = (xmlIDTablePtr) doc->ids;
1986 if (table == NULL)
1987 return(NULL);
1988
1989 id = xmlHashLookup(table, ID);
1990 if (id == NULL)
1991 return(NULL);
1992 return(id->attr);
1993}
1994
1995/************************************************************************
1996 * *
1997 * Refs *
1998 * *
1999 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002000typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002001{
2002 xmlListPtr l;
2003 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002004} xmlRemoveMemo;
2005
2006typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2007
2008typedef struct xmlValidateMemo_t
2009{
2010 xmlValidCtxtPtr ctxt;
2011 const xmlChar *name;
2012} xmlValidateMemo;
2013
2014typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002015
2016/**
2017 * xmlCreateRefTable:
2018 *
2019 * create and initialize an empty ref hash table.
2020 *
2021 * Returns the xmlRefTablePtr just created or NULL in case
2022 * of error.
2023 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002024static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002025xmlCreateRefTable(void) {
2026 return(xmlHashCreate(0));
2027}
2028
2029/**
2030 * xmlFreeRef:
2031 * @lk: A list link
2032 *
2033 * Deallocate the memory used by a ref definition
2034 */
2035static void
2036xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002037 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2038 if (ref == NULL) return;
2039 if (ref->value != NULL)
2040 xmlFree((xmlChar *)ref->value);
2041 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002042}
2043
2044/**
2045 * xmlFreeRefList:
2046 * @list_ref: A list of references.
2047 *
2048 * Deallocate the memory used by a list of references
2049 */
2050static void
2051xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002052 if (list_ref == NULL) return;
2053 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002054}
2055
2056/**
2057 * xmlWalkRemoveRef:
2058 * @data: Contents of current link
2059 * @user: Value supplied by the user
2060 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002061 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002062 */
2063static int
2064xmlWalkRemoveRef(const void *data, const void *user)
2065{
Daniel Veillard37721922001-05-04 15:21:12 +00002066 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2067 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2068 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002069
Daniel Veillard37721922001-05-04 15:21:12 +00002070 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2071 xmlListRemoveFirst(ref_list, (void *)data);
2072 return 0;
2073 }
2074 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002075}
2076
2077/**
2078 * xmlAddRef:
2079 * @ctxt: the validation context
2080 * @doc: pointer to the document
2081 * @value: the value name
2082 * @attr: the attribute holding the Ref
2083 *
2084 * Register a new ref declaration
2085 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002086 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002087 */
2088xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002089xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002090 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002091 xmlRefPtr ret;
2092 xmlRefTablePtr table;
2093 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002094
Daniel Veillard37721922001-05-04 15:21:12 +00002095 if (doc == NULL) {
2096 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002097 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002098 return(NULL);
2099 }
2100 if (value == NULL) {
2101 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002102 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002103 return(NULL);
2104 }
2105 if (attr == NULL) {
2106 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002107 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002108 return(NULL);
2109 }
Owen Taylor3473f882001-02-23 17:55:21 +00002110
Daniel Veillard37721922001-05-04 15:21:12 +00002111 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002112 * Create the Ref table if needed.
2113 */
Daniel Veillard37721922001-05-04 15:21:12 +00002114 table = (xmlRefTablePtr) doc->refs;
2115 if (table == NULL)
2116 doc->refs = table = xmlCreateRefTable();
2117 if (table == NULL) {
2118 xmlGenericError(xmlGenericErrorContext,
2119 "xmlAddRef: Table creation failed!\n");
2120 return(NULL);
2121 }
Owen Taylor3473f882001-02-23 17:55:21 +00002122
Daniel Veillard37721922001-05-04 15:21:12 +00002123 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2124 if (ret == NULL) {
2125 xmlGenericError(xmlGenericErrorContext,
2126 "xmlAddRef: out of memory\n");
2127 return(NULL);
2128 }
Owen Taylor3473f882001-02-23 17:55:21 +00002129
Daniel Veillard37721922001-05-04 15:21:12 +00002130 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002131 * fill the structure.
2132 */
Daniel Veillard37721922001-05-04 15:21:12 +00002133 ret->value = xmlStrdup(value);
2134 ret->attr = attr;
Owen Taylor3473f882001-02-23 17:55:21 +00002135
Daniel Veillard37721922001-05-04 15:21:12 +00002136 /* To add a reference :-
2137 * References are maintained as a list of references,
2138 * Lookup the entry, if no entry create new nodelist
2139 * Add the owning node to the NodeList
2140 * Return the ref
2141 */
Owen Taylor3473f882001-02-23 17:55:21 +00002142
Daniel Veillard37721922001-05-04 15:21:12 +00002143 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2144 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2145 xmlGenericError(xmlGenericErrorContext,
2146 "xmlAddRef: Reference list creation failed!\n");
2147 return(NULL);
2148 }
2149 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2150 xmlListDelete(ref_list);
2151 xmlGenericError(xmlGenericErrorContext,
2152 "xmlAddRef: Reference list insertion failed!\n");
2153 return(NULL);
2154 }
2155 }
2156 xmlListInsert(ref_list, ret);
2157 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002158}
2159
2160/**
2161 * xmlFreeRefTable:
2162 * @table: An ref table
2163 *
2164 * Deallocate the memory used by an Ref hash table.
2165 */
2166void
2167xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002168 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002169}
2170
2171/**
2172 * xmlIsRef:
2173 * @doc: the document
2174 * @elem: the element carrying the attribute
2175 * @attr: the attribute
2176 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002177 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002178 * then this is simple, otherwise we use an heuristic: name Ref (upper
2179 * or lowercase).
2180 *
2181 * Returns 0 or 1 depending on the lookup result
2182 */
2183int
2184xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002185 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2186 return(0);
2187 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2188 /* TODO @@@ */
2189 return(0);
2190 } else {
2191 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002192
Daniel Veillard37721922001-05-04 15:21:12 +00002193 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2194 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2195 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2196 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002197
Daniel Veillard37721922001-05-04 15:21:12 +00002198 if ((attrDecl != NULL) &&
2199 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2200 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2201 return(1);
2202 }
2203 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002204}
2205
2206/**
2207 * xmlRemoveRef
2208 * @doc: the document
2209 * @attr: the attribute
2210 *
2211 * Remove the given attribute from the Ref table maintained internally.
2212 *
2213 * Returns -1 if the lookup failed and 0 otherwise
2214 */
2215int
2216xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002217 xmlListPtr ref_list;
2218 xmlRefTablePtr table;
2219 xmlChar *ID;
2220 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002221
Daniel Veillard37721922001-05-04 15:21:12 +00002222 if (doc == NULL) return(-1);
2223 if (attr == NULL) return(-1);
2224 table = (xmlRefTablePtr) doc->refs;
2225 if (table == NULL)
2226 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002227
Daniel Veillard37721922001-05-04 15:21:12 +00002228 if (attr == NULL)
2229 return(-1);
2230 ID = xmlNodeListGetString(doc, attr->children, 1);
2231 if (ID == NULL)
2232 return(-1);
2233 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002234
Daniel Veillard37721922001-05-04 15:21:12 +00002235 if(ref_list == NULL) {
2236 xmlFree(ID);
2237 return (-1);
2238 }
2239 /* At this point, ref_list refers to a list of references which
2240 * have the same key as the supplied attr. Our list of references
2241 * is ordered by reference address and we don't have that information
2242 * here to use when removing. We'll have to walk the list and
2243 * check for a matching attribute, when we find one stop the walk
2244 * and remove the entry.
2245 * The list is ordered by reference, so that means we don't have the
2246 * key. Passing the list and the reference to the walker means we
2247 * will have enough data to be able to remove the entry.
2248 */
2249 target.l = ref_list;
2250 target.ap = attr;
2251
2252 /* Remove the supplied attr from our list */
2253 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002254
Daniel Veillard37721922001-05-04 15:21:12 +00002255 /*If the list is empty then remove the list entry in the hash */
2256 if (xmlListEmpty(ref_list))
2257 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2258 xmlFreeRefList);
2259 xmlFree(ID);
2260 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002261}
2262
2263/**
2264 * xmlGetRefs:
2265 * @doc: pointer to the document
2266 * @ID: the ID value
2267 *
2268 * Find the set of references for the supplied ID.
2269 *
2270 * Returns NULL if not found, otherwise node set for the ID.
2271 */
2272xmlListPtr
2273xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002274 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002275
Daniel Veillard37721922001-05-04 15:21:12 +00002276 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002277 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002278 return(NULL);
2279 }
Owen Taylor3473f882001-02-23 17:55:21 +00002280
Daniel Veillard37721922001-05-04 15:21:12 +00002281 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002282 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002283 return(NULL);
2284 }
Owen Taylor3473f882001-02-23 17:55:21 +00002285
Daniel Veillard37721922001-05-04 15:21:12 +00002286 table = (xmlRefTablePtr) doc->refs;
2287 if (table == NULL)
2288 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002289
Daniel Veillard37721922001-05-04 15:21:12 +00002290 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002291}
2292
2293/************************************************************************
2294 * *
2295 * Routines for validity checking *
2296 * *
2297 ************************************************************************/
2298
2299/**
2300 * xmlGetDtdElementDesc:
2301 * @dtd: a pointer to the DtD to search
2302 * @name: the element name
2303 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002304 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002305 *
2306 * returns the xmlElementPtr if found or NULL
2307 */
2308
2309xmlElementPtr
2310xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2311 xmlElementTablePtr table;
2312 xmlElementPtr cur;
2313 xmlChar *uqname = NULL, *prefix = NULL;
2314
2315 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002316 if (dtd->elements == NULL)
2317 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002318 table = (xmlElementTablePtr) dtd->elements;
2319
2320 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002321 if (uqname != NULL)
2322 name = uqname;
2323 cur = xmlHashLookup2(table, name, prefix);
2324 if (prefix != NULL) xmlFree(prefix);
2325 if (uqname != NULL) xmlFree(uqname);
2326 return(cur);
2327}
2328/**
2329 * xmlGetDtdElementDesc2:
2330 * @dtd: a pointer to the DtD to search
2331 * @name: the element name
2332 * @create: create an empty description if not found
2333 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002334 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002335 *
2336 * returns the xmlElementPtr if found or NULL
2337 */
2338
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002339static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002340xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2341 xmlElementTablePtr table;
2342 xmlElementPtr cur;
2343 xmlChar *uqname = NULL, *prefix = NULL;
2344
2345 if (dtd == NULL) return(NULL);
2346 if (dtd->elements == NULL) {
2347 if (!create)
2348 return(NULL);
2349 /*
2350 * Create the Element table if needed.
2351 */
2352 table = (xmlElementTablePtr) dtd->elements;
2353 if (table == NULL) {
2354 table = xmlCreateElementTable();
2355 dtd->elements = (void *) table;
2356 }
2357 if (table == NULL) {
2358 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002359 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002360 return(NULL);
2361 }
2362 }
2363 table = (xmlElementTablePtr) dtd->elements;
2364
2365 uqname = xmlSplitQName2(name, &prefix);
2366 if (uqname != NULL)
2367 name = uqname;
2368 cur = xmlHashLookup2(table, name, prefix);
2369 if ((cur == NULL) && (create)) {
2370 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2371 if (cur == NULL) {
2372 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002373 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002374 return(NULL);
2375 }
2376 memset(cur, 0, sizeof(xmlElement));
2377 cur->type = XML_ELEMENT_DECL;
2378
2379 /*
2380 * fill the structure.
2381 */
2382 cur->name = xmlStrdup(name);
2383 cur->prefix = xmlStrdup(prefix);
2384 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2385
2386 xmlHashAddEntry2(table, name, prefix, cur);
2387 }
2388 if (prefix != NULL) xmlFree(prefix);
2389 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002390 return(cur);
2391}
2392
2393/**
2394 * xmlGetDtdQElementDesc:
2395 * @dtd: a pointer to the DtD to search
2396 * @name: the element name
2397 * @prefix: the element namespace prefix
2398 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002399 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002400 *
2401 * returns the xmlElementPtr if found or NULL
2402 */
2403
Daniel Veillard48da9102001-08-07 01:10:10 +00002404xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002405xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2406 const xmlChar *prefix) {
2407 xmlElementTablePtr table;
2408
2409 if (dtd == NULL) return(NULL);
2410 if (dtd->elements == NULL) return(NULL);
2411 table = (xmlElementTablePtr) dtd->elements;
2412
2413 return(xmlHashLookup2(table, name, prefix));
2414}
2415
2416/**
2417 * xmlGetDtdAttrDesc:
2418 * @dtd: a pointer to the DtD to search
2419 * @elem: the element name
2420 * @name: the attribute name
2421 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002422 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002423 * this element.
2424 *
2425 * returns the xmlAttributePtr if found or NULL
2426 */
2427
2428xmlAttributePtr
2429xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2430 xmlAttributeTablePtr table;
2431 xmlAttributePtr cur;
2432 xmlChar *uqname = NULL, *prefix = NULL;
2433
2434 if (dtd == NULL) return(NULL);
2435 if (dtd->attributes == NULL) return(NULL);
2436
2437 table = (xmlAttributeTablePtr) dtd->attributes;
2438 if (table == NULL)
2439 return(NULL);
2440
2441 uqname = xmlSplitQName2(name, &prefix);
2442
2443 if (uqname != NULL) {
2444 cur = xmlHashLookup3(table, uqname, prefix, elem);
2445 if (prefix != NULL) xmlFree(prefix);
2446 if (uqname != NULL) xmlFree(uqname);
2447 } else
2448 cur = xmlHashLookup3(table, name, NULL, elem);
2449 return(cur);
2450}
2451
2452/**
2453 * xmlGetDtdQAttrDesc:
2454 * @dtd: a pointer to the DtD to search
2455 * @elem: the element name
2456 * @name: the attribute name
2457 * @prefix: the attribute namespace prefix
2458 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002459 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002460 * this element.
2461 *
2462 * returns the xmlAttributePtr if found or NULL
2463 */
2464
Daniel Veillard48da9102001-08-07 01:10:10 +00002465xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002466xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2467 const xmlChar *prefix) {
2468 xmlAttributeTablePtr table;
2469
2470 if (dtd == NULL) return(NULL);
2471 if (dtd->attributes == NULL) return(NULL);
2472 table = (xmlAttributeTablePtr) dtd->attributes;
2473
2474 return(xmlHashLookup3(table, name, prefix, elem));
2475}
2476
2477/**
2478 * xmlGetDtdNotationDesc:
2479 * @dtd: a pointer to the DtD to search
2480 * @name: the notation name
2481 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002482 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002483 *
2484 * returns the xmlNotationPtr if found or NULL
2485 */
2486
2487xmlNotationPtr
2488xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2489 xmlNotationTablePtr table;
2490
2491 if (dtd == NULL) return(NULL);
2492 if (dtd->notations == NULL) return(NULL);
2493 table = (xmlNotationTablePtr) dtd->notations;
2494
2495 return(xmlHashLookup(table, name));
2496}
2497
2498/**
2499 * xmlValidateNotationUse:
2500 * @ctxt: the validation context
2501 * @doc: the document
2502 * @notationName: the notation name to check
2503 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002504 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002505 * - [ VC: Notation Declared ]
2506 *
2507 * returns 1 if valid or 0 otherwise
2508 */
2509
2510int
2511xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2512 const xmlChar *notationName) {
2513 xmlNotationPtr notaDecl;
2514 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2515
2516 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2517 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2518 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2519
2520 if (notaDecl == NULL) {
2521 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2522 notationName);
2523 return(0);
2524 }
2525 return(1);
2526}
2527
2528/**
2529 * xmlIsMixedElement
2530 * @doc: the document
2531 * @name: the element name
2532 *
2533 * Search in the DtDs whether an element accept Mixed content (or ANY)
2534 * basically if it is supposed to accept text childs
2535 *
2536 * returns 0 if no, 1 if yes, and -1 if no element description is available
2537 */
2538
2539int
2540xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2541 xmlElementPtr elemDecl;
2542
2543 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2544
2545 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2546 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2547 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2548 if (elemDecl == NULL) return(-1);
2549 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002550 case XML_ELEMENT_TYPE_UNDEFINED:
2551 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002552 case XML_ELEMENT_TYPE_ELEMENT:
2553 return(0);
2554 case XML_ELEMENT_TYPE_EMPTY:
2555 /*
2556 * return 1 for EMPTY since we want VC error to pop up
2557 * on <empty> </empty> for example
2558 */
2559 case XML_ELEMENT_TYPE_ANY:
2560 case XML_ELEMENT_TYPE_MIXED:
2561 return(1);
2562 }
2563 return(1);
2564}
2565
2566/**
2567 * xmlValidateNameValue:
2568 * @value: an Name value
2569 *
2570 * Validate that the given value match Name production
2571 *
2572 * returns 1 if valid or 0 otherwise
2573 */
2574
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002575static int
Owen Taylor3473f882001-02-23 17:55:21 +00002576xmlValidateNameValue(const xmlChar *value) {
2577 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002578 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002579
2580 if (value == NULL) return(0);
2581 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002582 val = xmlStringCurrentChar(NULL, cur, &len);
2583 cur += len;
2584 if (!IS_LETTER(val) && (val != '_') &&
2585 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002586 return(0);
2587 }
2588
Daniel Veillardd8224e02002-01-13 15:43:22 +00002589 val = xmlStringCurrentChar(NULL, cur, &len);
2590 cur += len;
2591 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2592 (val == '.') || (val == '-') ||
2593 (val == '_') || (val == ':') ||
2594 (IS_COMBINING(val)) ||
2595 (IS_EXTENDER(val))) {
2596 val = xmlStringCurrentChar(NULL, cur, &len);
2597 cur += len;
2598 }
Owen Taylor3473f882001-02-23 17:55:21 +00002599
Daniel Veillardd8224e02002-01-13 15:43:22 +00002600 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002601
2602 return(1);
2603}
2604
2605/**
2606 * xmlValidateNamesValue:
2607 * @value: an Names value
2608 *
2609 * Validate that the given value match Names production
2610 *
2611 * returns 1 if valid or 0 otherwise
2612 */
2613
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002614static int
Owen Taylor3473f882001-02-23 17:55:21 +00002615xmlValidateNamesValue(const xmlChar *value) {
2616 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002617 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002618
2619 if (value == NULL) return(0);
2620 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002621 val = xmlStringCurrentChar(NULL, cur, &len);
2622 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002623
Daniel Veillardd8224e02002-01-13 15:43:22 +00002624 if (!IS_LETTER(val) && (val != '_') &&
2625 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002626 return(0);
2627 }
2628
Daniel Veillardd8224e02002-01-13 15:43:22 +00002629 val = xmlStringCurrentChar(NULL, cur, &len);
2630 cur += len;
2631 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2632 (val == '.') || (val == '-') ||
2633 (val == '_') || (val == ':') ||
2634 (IS_COMBINING(val)) ||
2635 (IS_EXTENDER(val))) {
2636 val = xmlStringCurrentChar(NULL, cur, &len);
2637 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002638 }
2639
Daniel Veillardd8224e02002-01-13 15:43:22 +00002640 while (IS_BLANK(val)) {
2641 while (IS_BLANK(val)) {
2642 val = xmlStringCurrentChar(NULL, cur, &len);
2643 cur += len;
2644 }
2645
2646 if (!IS_LETTER(val) && (val != '_') &&
2647 (val != ':')) {
2648 return(0);
2649 }
2650 val = xmlStringCurrentChar(NULL, cur, &len);
2651 cur += len;
2652
2653 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2654 (val == '.') || (val == '-') ||
2655 (val == '_') || (val == ':') ||
2656 (IS_COMBINING(val)) ||
2657 (IS_EXTENDER(val))) {
2658 val = xmlStringCurrentChar(NULL, cur, &len);
2659 cur += len;
2660 }
2661 }
2662
2663 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002664
2665 return(1);
2666}
2667
2668/**
2669 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002670 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00002671 *
2672 * Validate that the given value match Nmtoken production
2673 *
2674 * [ VC: Name Token ]
2675 *
2676 * returns 1 if valid or 0 otherwise
2677 */
2678
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002679static int
Owen Taylor3473f882001-02-23 17:55:21 +00002680xmlValidateNmtokenValue(const xmlChar *value) {
2681 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002682 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002683
2684 if (value == NULL) return(0);
2685 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002686 val = xmlStringCurrentChar(NULL, cur, &len);
2687 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002688
Daniel Veillardd8224e02002-01-13 15:43:22 +00002689 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2690 (val != '.') && (val != '-') &&
2691 (val != '_') && (val != ':') &&
2692 (!IS_COMBINING(val)) &&
2693 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00002694 return(0);
2695
Daniel Veillardd8224e02002-01-13 15:43:22 +00002696 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2697 (val == '.') || (val == '-') ||
2698 (val == '_') || (val == ':') ||
2699 (IS_COMBINING(val)) ||
2700 (IS_EXTENDER(val))) {
2701 val = xmlStringCurrentChar(NULL, cur, &len);
2702 cur += len;
2703 }
Owen Taylor3473f882001-02-23 17:55:21 +00002704
Daniel Veillardd8224e02002-01-13 15:43:22 +00002705 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002706
2707 return(1);
2708}
2709
2710/**
2711 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002712 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00002713 *
2714 * Validate that the given value match Nmtokens production
2715 *
2716 * [ VC: Name Token ]
2717 *
2718 * returns 1 if valid or 0 otherwise
2719 */
2720
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002721static int
Owen Taylor3473f882001-02-23 17:55:21 +00002722xmlValidateNmtokensValue(const xmlChar *value) {
2723 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002724 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002725
2726 if (value == NULL) return(0);
2727 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002728 val = xmlStringCurrentChar(NULL, cur, &len);
2729 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002730
Daniel Veillardd8224e02002-01-13 15:43:22 +00002731 while (IS_BLANK(val)) {
2732 val = xmlStringCurrentChar(NULL, cur, &len);
2733 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002734 }
2735
Daniel Veillardd8224e02002-01-13 15:43:22 +00002736 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2737 (val != '.') && (val != '-') &&
2738 (val != '_') && (val != ':') &&
2739 (!IS_COMBINING(val)) &&
2740 (!IS_EXTENDER(val)))
2741 return(0);
2742
2743 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2744 (val == '.') || (val == '-') ||
2745 (val == '_') || (val == ':') ||
2746 (IS_COMBINING(val)) ||
2747 (IS_EXTENDER(val))) {
2748 val = xmlStringCurrentChar(NULL, cur, &len);
2749 cur += len;
2750 }
2751
2752 while (IS_BLANK(val)) {
2753 while (IS_BLANK(val)) {
2754 val = xmlStringCurrentChar(NULL, cur, &len);
2755 cur += len;
2756 }
2757 if (val == 0) return(1);
2758
2759 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2760 (val != '.') && (val != '-') &&
2761 (val != '_') && (val != ':') &&
2762 (!IS_COMBINING(val)) &&
2763 (!IS_EXTENDER(val)))
2764 return(0);
2765
2766 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2767 (val == '.') || (val == '-') ||
2768 (val == '_') || (val == ':') ||
2769 (IS_COMBINING(val)) ||
2770 (IS_EXTENDER(val))) {
2771 val = xmlStringCurrentChar(NULL, cur, &len);
2772 cur += len;
2773 }
2774 }
2775
2776 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002777
2778 return(1);
2779}
2780
2781/**
2782 * xmlValidateNotationDecl:
2783 * @ctxt: the validation context
2784 * @doc: a document instance
2785 * @nota: a notation definition
2786 *
2787 * Try to validate a single notation definition
2788 * basically it does the following checks as described by the
2789 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002790 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00002791 * But this function get called anyway ...
2792 *
2793 * returns 1 if valid or 0 otherwise
2794 */
2795
2796int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002797xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
2798 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002799 int ret = 1;
2800
2801 return(ret);
2802}
2803
2804/**
2805 * xmlValidateAttributeValue:
2806 * @type: an attribute type
2807 * @value: an attribute value
2808 *
2809 * Validate that the given attribute value match the proper production
2810 *
2811 * [ VC: ID ]
2812 * Values of type ID must match the Name production....
2813 *
2814 * [ VC: IDREF ]
2815 * Values of type IDREF must match the Name production, and values
2816 * of type IDREFS must match Names ...
2817 *
2818 * [ VC: Entity Name ]
2819 * Values of type ENTITY must match the Name production, values
2820 * of type ENTITIES must match Names ...
2821 *
2822 * [ VC: Name Token ]
2823 * Values of type NMTOKEN must match the Nmtoken production; values
2824 * of type NMTOKENS must match Nmtokens.
2825 *
2826 * returns 1 if valid or 0 otherwise
2827 */
2828
2829int
2830xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
2831 switch (type) {
2832 case XML_ATTRIBUTE_ENTITIES:
2833 case XML_ATTRIBUTE_IDREFS:
2834 return(xmlValidateNamesValue(value));
2835 case XML_ATTRIBUTE_ENTITY:
2836 case XML_ATTRIBUTE_IDREF:
2837 case XML_ATTRIBUTE_ID:
2838 case XML_ATTRIBUTE_NOTATION:
2839 return(xmlValidateNameValue(value));
2840 case XML_ATTRIBUTE_NMTOKENS:
2841 case XML_ATTRIBUTE_ENUMERATION:
2842 return(xmlValidateNmtokensValue(value));
2843 case XML_ATTRIBUTE_NMTOKEN:
2844 return(xmlValidateNmtokenValue(value));
2845 case XML_ATTRIBUTE_CDATA:
2846 break;
2847 }
2848 return(1);
2849}
2850
2851/**
2852 * xmlValidateAttributeValue2:
2853 * @ctxt: the validation context
2854 * @doc: the document
2855 * @name: the attribute name (used for error reporting only)
2856 * @type: the attribute type
2857 * @value: the attribute value
2858 *
2859 * Validate that the given attribute value match a given type.
2860 * This typically cannot be done before having finished parsing
2861 * the subsets.
2862 *
2863 * [ VC: IDREF ]
2864 * Values of type IDREF must match one of the declared IDs
2865 * Values of type IDREFS must match a sequence of the declared IDs
2866 * each Name must match the value of an ID attribute on some element
2867 * in the XML document; i.e. IDREF values must match the value of
2868 * some ID attribute
2869 *
2870 * [ VC: Entity Name ]
2871 * Values of type ENTITY must match one declared entity
2872 * Values of type ENTITIES must match a sequence of declared entities
2873 *
2874 * [ VC: Notation Attributes ]
2875 * all notation names in the declaration must be declared.
2876 *
2877 * returns 1 if valid or 0 otherwise
2878 */
2879
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002880static int
Owen Taylor3473f882001-02-23 17:55:21 +00002881xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2882 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
2883 int ret = 1;
2884 switch (type) {
2885 case XML_ATTRIBUTE_IDREFS:
2886 case XML_ATTRIBUTE_IDREF:
2887 case XML_ATTRIBUTE_ID:
2888 case XML_ATTRIBUTE_NMTOKENS:
2889 case XML_ATTRIBUTE_ENUMERATION:
2890 case XML_ATTRIBUTE_NMTOKEN:
2891 case XML_ATTRIBUTE_CDATA:
2892 break;
2893 case XML_ATTRIBUTE_ENTITY: {
2894 xmlEntityPtr ent;
2895
2896 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00002897 if ((ent == NULL) && (doc->standalone == 1)) {
2898 doc->standalone = 0;
2899 ent = xmlGetDocEntity(doc, value);
2900 if (ent != NULL) {
2901 VERROR(ctxt->userData,
2902"standalone problem: attribute %s reference entity \"%s\" in external subset\n",
2903 name, value);
2904 /* WAIT to get answer from the Core WG on this
2905 ret = 0;
2906 */
2907 }
2908 }
Owen Taylor3473f882001-02-23 17:55:21 +00002909 if (ent == NULL) {
2910 VERROR(ctxt->userData,
2911 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2912 name, value);
2913 ret = 0;
2914 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2915 VERROR(ctxt->userData,
2916 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2917 name, value);
2918 ret = 0;
2919 }
2920 break;
2921 }
2922 case XML_ATTRIBUTE_ENTITIES: {
2923 xmlChar *dup, *nam = NULL, *cur, save;
2924 xmlEntityPtr ent;
2925
2926 dup = xmlStrdup(value);
2927 if (dup == NULL)
2928 return(0);
2929 cur = dup;
2930 while (*cur != 0) {
2931 nam = cur;
2932 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2933 save = *cur;
2934 *cur = 0;
2935 ent = xmlGetDocEntity(doc, nam);
2936 if (ent == NULL) {
2937 VERROR(ctxt->userData,
2938 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
2939 name, nam);
2940 ret = 0;
2941 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2942 VERROR(ctxt->userData,
2943 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
2944 name, nam);
2945 ret = 0;
2946 }
2947 if (save == 0)
2948 break;
2949 *cur = save;
2950 while (IS_BLANK(*cur)) cur++;
2951 }
2952 xmlFree(dup);
2953 break;
2954 }
2955 case XML_ATTRIBUTE_NOTATION: {
2956 xmlNotationPtr nota;
2957
2958 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
2959 if ((nota == NULL) && (doc->extSubset != NULL))
2960 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
2961
2962 if (nota == NULL) {
2963 VERROR(ctxt->userData,
2964 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
2965 name, value);
2966 ret = 0;
2967 }
2968 break;
2969 }
2970 }
2971 return(ret);
2972}
2973
2974/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00002975 * xmlValidCtxtNormalizeAttributeValue:
2976 * @ctxt: the validation context
2977 * @doc: the document
2978 * @elem: the parent
2979 * @name: the attribute name
2980 * @value: the attribute value
2981 * @ctxt: the validation context or NULL
2982 *
2983 * Does the validation related extra step of the normalization of attribute
2984 * values:
2985 *
2986 * If the declared value is not CDATA, then the XML processor must further
2987 * process the normalized attribute value by discarding any leading and
2988 * trailing space (#x20) characters, and by replacing sequences of space
2989 * (#x20) characters by single space (#x20) character.
2990 *
2991 * Also check VC: Standalone Document Declaration in P32, and update
2992 * ctxt->valid accordingly
2993 *
2994 * returns a new normalized string if normalization is needed, NULL otherwise
2995 * the caller must free the returned value.
2996 */
2997
2998xmlChar *
2999xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3000 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3001 xmlChar *ret, *dst;
3002 const xmlChar *src;
3003 xmlAttributePtr attrDecl = NULL;
3004 int extsubset = 0;
3005
3006 if (doc == NULL) return(NULL);
3007 if (elem == NULL) return(NULL);
3008 if (name == NULL) return(NULL);
3009 if (value == NULL) return(NULL);
3010
3011 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3012 xmlChar qname[500];
3013 snprintf((char *) qname, sizeof(qname), "%s:%s",
3014 elem->ns->prefix, elem->name);
3015 qname[sizeof(qname) - 1] = 0;
3016 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3017 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3018 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3019 if (attrDecl != NULL)
3020 extsubset = 1;
3021 }
3022 }
3023 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3024 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3025 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3026 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3027 if (attrDecl != NULL)
3028 extsubset = 1;
3029 }
3030
3031 if (attrDecl == NULL)
3032 return(NULL);
3033 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3034 return(NULL);
3035
3036 ret = xmlStrdup(value);
3037 if (ret == NULL)
3038 return(NULL);
3039 src = value;
3040 dst = ret;
3041 while (*src == 0x20) src++;
3042 while (*src != 0) {
3043 if (*src == 0x20) {
3044 while (*src == 0x20) src++;
3045 if (*src != 0)
3046 *dst++ = 0x20;
3047 } else {
3048 *dst++ = *src++;
3049 }
3050 }
3051 *dst = 0;
3052 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3053 VERROR(ctxt->userData,
3054"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3055 name, elem->name);
3056 ctxt->valid = 0;
3057 }
3058 return(ret);
3059}
3060
3061/**
Owen Taylor3473f882001-02-23 17:55:21 +00003062 * xmlValidNormalizeAttributeValue:
3063 * @doc: the document
3064 * @elem: the parent
3065 * @name: the attribute name
3066 * @value: the attribute value
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003067 * @ctxt: the validation context or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00003068 *
3069 * Does the validation related extra step of the normalization of attribute
3070 * values:
3071 *
3072 * If the declared value is not CDATA, then the XML processor must further
3073 * process the normalized attribute value by discarding any leading and
3074 * trailing space (#x20) characters, and by replacing sequences of space
3075 * (#x20) characters by single space (#x20) character.
3076 *
3077 * returns a new normalized string if normalization is needed, NULL otherwise
3078 * the caller must free the returned value.
3079 */
3080
3081xmlChar *
3082xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3083 const xmlChar *name, const xmlChar *value) {
3084 xmlChar *ret, *dst;
3085 const xmlChar *src;
3086 xmlAttributePtr attrDecl = NULL;
3087
3088 if (doc == NULL) return(NULL);
3089 if (elem == NULL) return(NULL);
3090 if (name == NULL) return(NULL);
3091 if (value == NULL) return(NULL);
3092
3093 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3094 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003095 snprintf((char *) qname, sizeof(qname), "%s:%s",
3096 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003097 qname[sizeof(qname) - 1] = 0;
3098 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3099 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3100 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3101 }
3102 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3103 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3104 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3105
3106 if (attrDecl == NULL)
3107 return(NULL);
3108 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3109 return(NULL);
3110
3111 ret = xmlStrdup(value);
3112 if (ret == NULL)
3113 return(NULL);
3114 src = value;
3115 dst = ret;
3116 while (*src == 0x20) src++;
3117 while (*src != 0) {
3118 if (*src == 0x20) {
3119 while (*src == 0x20) src++;
3120 if (*src != 0)
3121 *dst++ = 0x20;
3122 } else {
3123 *dst++ = *src++;
3124 }
3125 }
3126 *dst = 0;
3127 return(ret);
3128}
3129
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003130static void
Owen Taylor3473f882001-02-23 17:55:21 +00003131xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003132 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003133 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3134}
3135
3136/**
3137 * xmlValidateAttributeDecl:
3138 * @ctxt: the validation context
3139 * @doc: a document instance
3140 * @attr: an attribute definition
3141 *
3142 * Try to validate a single attribute definition
3143 * basically it does the following checks as described by the
3144 * XML-1.0 recommendation:
3145 * - [ VC: Attribute Default Legal ]
3146 * - [ VC: Enumeration ]
3147 * - [ VC: ID Attribute Default ]
3148 *
3149 * The ID/IDREF uniqueness and matching are done separately
3150 *
3151 * returns 1 if valid or 0 otherwise
3152 */
3153
3154int
3155xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3156 xmlAttributePtr attr) {
3157 int ret = 1;
3158 int val;
3159 CHECK_DTD;
3160 if(attr == NULL) return(1);
3161
3162 /* Attribute Default Legal */
3163 /* Enumeration */
3164 if (attr->defaultValue != NULL) {
3165 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3166 if (val == 0) {
3167 VERROR(ctxt->userData,
3168 "Syntax of default value for attribute %s on %s is not valid\n",
3169 attr->name, attr->elem);
3170 }
3171 ret &= val;
3172 }
3173
3174 /* ID Attribute Default */
3175 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3176 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3177 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3178 VERROR(ctxt->userData,
3179 "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n",
3180 attr->name, attr->elem);
3181 ret = 0;
3182 }
3183
3184 /* One ID per Element Type */
3185 if (attr->atype == XML_ATTRIBUTE_ID) {
3186 int nbId;
3187
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003188 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003189 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3190 attr->elem);
3191 if (elem != NULL) {
3192 nbId = xmlScanIDAttributeDecl(NULL, elem);
3193 } else {
3194 xmlAttributeTablePtr table;
3195
3196 /*
3197 * The attribute may be declared in the internal subset and the
3198 * element in the external subset.
3199 */
3200 nbId = 0;
3201 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3202 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3203 xmlValidateAttributeIdCallback, &nbId);
3204 }
3205 if (nbId > 1) {
3206 VERROR(ctxt->userData,
3207 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3208 attr->elem, nbId, attr->name);
3209 } else if (doc->extSubset != NULL) {
3210 int extId = 0;
3211 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3212 if (elem != NULL) {
3213 extId = xmlScanIDAttributeDecl(NULL, elem);
3214 }
3215 if (extId > 1) {
3216 VERROR(ctxt->userData,
3217 "Element %s has %d ID attribute defined in the external subset : %s\n",
3218 attr->elem, extId, attr->name);
3219 } else if (extId + nbId > 1) {
3220 VERROR(ctxt->userData,
3221"Element %s has ID attributes defined in the internal and external subset : %s\n",
3222 attr->elem, attr->name);
3223 }
3224 }
3225 }
3226
3227 /* Validity Constraint: Enumeration */
3228 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3229 xmlEnumerationPtr tree = attr->tree;
3230 while (tree != NULL) {
3231 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3232 tree = tree->next;
3233 }
3234 if (tree == NULL) {
3235 VERROR(ctxt->userData,
3236"Default value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3237 attr->defaultValue, attr->name, attr->elem);
3238 ret = 0;
3239 }
3240 }
3241
3242 return(ret);
3243}
3244
3245/**
3246 * xmlValidateElementDecl:
3247 * @ctxt: the validation context
3248 * @doc: a document instance
3249 * @elem: an element definition
3250 *
3251 * Try to validate a single element definition
3252 * basically it does the following checks as described by the
3253 * XML-1.0 recommendation:
3254 * - [ VC: One ID per Element Type ]
3255 * - [ VC: No Duplicate Types ]
3256 * - [ VC: Unique Element Type Declaration ]
3257 *
3258 * returns 1 if valid or 0 otherwise
3259 */
3260
3261int
3262xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3263 xmlElementPtr elem) {
3264 int ret = 1;
3265 xmlElementPtr tst;
3266
3267 CHECK_DTD;
3268
3269 if (elem == NULL) return(1);
3270
3271 /* No Duplicate Types */
3272 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3273 xmlElementContentPtr cur, next;
3274 const xmlChar *name;
3275
3276 cur = elem->content;
3277 while (cur != NULL) {
3278 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3279 if (cur->c1 == NULL) break;
3280 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3281 name = cur->c1->name;
3282 next = cur->c2;
3283 while (next != NULL) {
3284 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3285 if (xmlStrEqual(next->name, name)) {
3286 VERROR(ctxt->userData,
3287 "Definition of %s has duplicate references of %s\n",
3288 elem->name, name);
3289 ret = 0;
3290 }
3291 break;
3292 }
3293 if (next->c1 == NULL) break;
3294 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3295 if (xmlStrEqual(next->c1->name, name)) {
3296 VERROR(ctxt->userData,
3297 "Definition of %s has duplicate references of %s\n",
3298 elem->name, name);
3299 ret = 0;
3300 }
3301 next = next->c2;
3302 }
3303 }
3304 cur = cur->c2;
3305 }
3306 }
3307
3308 /* VC: Unique Element Type Declaration */
3309 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003310 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003311 ((tst->prefix == elem->prefix) ||
3312 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003313 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003314 VERROR(ctxt->userData, "Redefinition of element %s\n",
3315 elem->name);
3316 ret = 0;
3317 }
3318 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003319 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003320 ((tst->prefix == elem->prefix) ||
3321 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003322 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003323 VERROR(ctxt->userData, "Redefinition of element %s\n",
3324 elem->name);
3325 ret = 0;
3326 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003327 /* One ID per Element Type
3328 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003329 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3330 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003331 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003332 return(ret);
3333}
3334
3335/**
3336 * xmlValidateOneAttribute:
3337 * @ctxt: the validation context
3338 * @doc: a document instance
3339 * @elem: an element instance
3340 * @attr: an attribute instance
3341 * @value: the attribute value (without entities processing)
3342 *
3343 * Try to validate a single attribute for an element
3344 * basically it does the following checks as described by the
3345 * XML-1.0 recommendation:
3346 * - [ VC: Attribute Value Type ]
3347 * - [ VC: Fixed Attribute Default ]
3348 * - [ VC: Entity Name ]
3349 * - [ VC: Name Token ]
3350 * - [ VC: ID ]
3351 * - [ VC: IDREF ]
3352 * - [ VC: Entity Name ]
3353 * - [ VC: Notation Attributes ]
3354 *
3355 * The ID/IDREF uniqueness and matching are done separately
3356 *
3357 * returns 1 if valid or 0 otherwise
3358 */
3359
3360int
3361xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3362 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3363 /* xmlElementPtr elemDecl; */
3364 xmlAttributePtr attrDecl = NULL;
3365 int val;
3366 int ret = 1;
3367
3368 CHECK_DTD;
3369 if ((elem == NULL) || (elem->name == NULL)) return(0);
3370 if ((attr == NULL) || (attr->name == NULL)) return(0);
3371
3372 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3373 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003374 snprintf((char *) qname, sizeof(qname), "%s:%s",
3375 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003376 qname[sizeof(qname) - 1] = 0;
3377 if (attr->ns != NULL) {
3378 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3379 attr->name, attr->ns->prefix);
3380 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3381 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3382 attr->name, attr->ns->prefix);
3383 } else {
3384 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3385 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3386 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3387 qname, attr->name);
3388 }
3389 }
3390 if (attrDecl == NULL) {
3391 if (attr->ns != NULL) {
3392 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3393 attr->name, attr->ns->prefix);
3394 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3395 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3396 attr->name, attr->ns->prefix);
3397 } else {
3398 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3399 elem->name, attr->name);
3400 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3401 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3402 elem->name, attr->name);
3403 }
3404 }
3405
3406
3407 /* Validity Constraint: Attribute Value Type */
3408 if (attrDecl == NULL) {
3409 VERROR(ctxt->userData,
3410 "No declaration for attribute %s on element %s\n",
3411 attr->name, elem->name);
3412 return(0);
3413 }
3414 attr->atype = attrDecl->atype;
3415
3416 val = xmlValidateAttributeValue(attrDecl->atype, value);
3417 if (val == 0) {
3418 VERROR(ctxt->userData,
3419 "Syntax of value for attribute %s on %s is not valid\n",
3420 attr->name, elem->name);
3421 ret = 0;
3422 }
3423
3424 /* Validity constraint: Fixed Attribute Default */
3425 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3426 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3427 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003428 "Value for attribute %s on %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003429 attr->name, elem->name, attrDecl->defaultValue);
3430 ret = 0;
3431 }
3432 }
3433
3434 /* Validity Constraint: ID uniqueness */
3435 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3436 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3437 ret = 0;
3438 }
3439
3440 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3441 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3442 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3443 ret = 0;
3444 }
3445
3446 /* Validity Constraint: Notation Attributes */
3447 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3448 xmlEnumerationPtr tree = attrDecl->tree;
3449 xmlNotationPtr nota;
3450
3451 /* First check that the given NOTATION was declared */
3452 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3453 if (nota == NULL)
3454 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3455
3456 if (nota == NULL) {
3457 VERROR(ctxt->userData,
3458 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3459 value, attr->name, elem->name);
3460 ret = 0;
3461 }
3462
3463 /* Second, verify that it's among the list */
3464 while (tree != NULL) {
3465 if (xmlStrEqual(tree->name, value)) break;
3466 tree = tree->next;
3467 }
3468 if (tree == NULL) {
3469 VERROR(ctxt->userData,
3470"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3471 value, attr->name, elem->name);
3472 ret = 0;
3473 }
3474 }
3475
3476 /* Validity Constraint: Enumeration */
3477 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3478 xmlEnumerationPtr tree = attrDecl->tree;
3479 while (tree != NULL) {
3480 if (xmlStrEqual(tree->name, value)) break;
3481 tree = tree->next;
3482 }
3483 if (tree == NULL) {
3484 VERROR(ctxt->userData,
3485 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3486 value, attr->name, elem->name);
3487 ret = 0;
3488 }
3489 }
3490
3491 /* Fixed Attribute Default */
3492 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3493 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3494 VERROR(ctxt->userData,
3495 "Value for attribute %s on %s must be \"%s\"\n",
3496 attr->name, elem->name, attrDecl->defaultValue);
3497 ret = 0;
3498 }
3499
3500 /* Extra check for the attribute value */
3501 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3502 attrDecl->atype, value);
3503
3504 return(ret);
3505}
3506
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003507/**
3508 * xmlValidateSkipIgnorable:
3509 * @ctxt: the validation context
3510 * @child: the child list
3511 *
3512 * Skip ignorable elements w.r.t. the validation process
3513 *
3514 * returns the first element to consider for validation of the content model
3515 */
3516
3517static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003518xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003519 while (child != NULL) {
3520 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003521 /* These things are ignored (skipped) during validation. */
3522 case XML_PI_NODE:
3523 case XML_COMMENT_NODE:
3524 case XML_XINCLUDE_START:
3525 case XML_XINCLUDE_END:
3526 child = child->next;
3527 break;
3528 case XML_TEXT_NODE:
3529 if (xmlIsBlankNode(child))
3530 child = child->next;
3531 else
3532 return(child);
3533 break;
3534 /* keep current node */
3535 default:
3536 return(child);
3537 }
3538 }
3539 return(child);
3540}
3541
3542/**
3543 * xmlValidateElementType:
3544 * @ctxt: the validation context
3545 *
3546 * Try to validate the content model of an element internal function
3547 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003548 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3549 * reference is found and -3 if the validation succeeded but
3550 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003551 */
3552
3553static int
3554xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003555 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003556 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003557
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003558 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003559 if ((NODE == NULL) && (CONT == NULL))
3560 return(1);
3561 if ((NODE == NULL) &&
3562 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3563 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3564 return(1);
3565 }
3566 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003567 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003568 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003569
3570 /*
3571 * We arrive here when more states need to be examined
3572 */
3573cont:
3574
3575 /*
3576 * We just recovered from a rollback generated by a possible
3577 * epsilon transition, go directly to the analysis phase
3578 */
3579 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003580 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003581 DEBUG_VALID_STATE(NODE, CONT)
3582 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003583 goto analyze;
3584 }
3585
3586 DEBUG_VALID_STATE(NODE, CONT)
3587 /*
3588 * we may have to save a backup state here. This is the equivalent
3589 * of handling epsilon transition in NFAs.
3590 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003591 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003592 ((CONT->parent == NULL) ||
3593 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003594 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003595 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00003596 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003597 DEBUG_VALID_MSG("saving parent branch");
3598 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3599 }
3600
3601
3602 /*
3603 * Check first if the content matches
3604 */
3605 switch (CONT->type) {
3606 case XML_ELEMENT_CONTENT_PCDATA:
3607 if (NODE == NULL) {
3608 DEBUG_VALID_MSG("pcdata failed no node");
3609 ret = 0;
3610 break;
3611 }
3612 if (NODE->type == XML_TEXT_NODE) {
3613 DEBUG_VALID_MSG("pcdata found, skip to next");
3614 /*
3615 * go to next element in the content model
3616 * skipping ignorable elems
3617 */
3618 do {
3619 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003620 NODE = xmlValidateSkipIgnorable(NODE);
3621 if ((NODE != NULL) &&
3622 (NODE->type == XML_ENTITY_REF_NODE))
3623 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003624 } while ((NODE != NULL) &&
3625 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003626 (NODE->type != XML_TEXT_NODE) &&
3627 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003628 ret = 1;
3629 break;
3630 } else {
3631 DEBUG_VALID_MSG("pcdata failed");
3632 ret = 0;
3633 break;
3634 }
3635 break;
3636 case XML_ELEMENT_CONTENT_ELEMENT:
3637 if (NODE == NULL) {
3638 DEBUG_VALID_MSG("element failed no node");
3639 ret = 0;
3640 break;
3641 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003642 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3643 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003644 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003645 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3646 ret = (CONT->prefix == NULL);
3647 } else if (CONT->prefix == NULL) {
3648 ret = 0;
3649 } else {
3650 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3651 }
3652 }
3653 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003654 DEBUG_VALID_MSG("element found, skip to next");
3655 /*
3656 * go to next element in the content model
3657 * skipping ignorable elems
3658 */
3659 do {
3660 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003661 NODE = xmlValidateSkipIgnorable(NODE);
3662 if ((NODE != NULL) &&
3663 (NODE->type == XML_ENTITY_REF_NODE))
3664 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003665 } while ((NODE != NULL) &&
3666 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003667 (NODE->type != XML_TEXT_NODE) &&
3668 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003669 } else {
3670 DEBUG_VALID_MSG("element failed");
3671 ret = 0;
3672 break;
3673 }
3674 break;
3675 case XML_ELEMENT_CONTENT_OR:
3676 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003677 * Small optimization.
3678 */
3679 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3680 if ((NODE == NULL) ||
3681 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3682 DEPTH++;
3683 CONT = CONT->c2;
3684 goto cont;
3685 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003686 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3687 ret = (CONT->c1->prefix == NULL);
3688 } else if (CONT->c1->prefix == NULL) {
3689 ret = 0;
3690 } else {
3691 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3692 }
3693 if (ret == 0) {
3694 DEPTH++;
3695 CONT = CONT->c2;
3696 goto cont;
3697 }
Daniel Veillard85349052001-04-20 13:48:21 +00003698 }
3699
3700 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003701 * save the second branch 'or' branch
3702 */
3703 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003704 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3705 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003706
3707 DEPTH++;
3708 CONT = CONT->c1;
3709 goto cont;
3710 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003711 /*
3712 * Small optimization.
3713 */
3714 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3715 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3716 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3717 if ((NODE == NULL) ||
3718 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3719 DEPTH++;
3720 CONT = CONT->c2;
3721 goto cont;
3722 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003723 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3724 ret = (CONT->c1->prefix == NULL);
3725 } else if (CONT->c1->prefix == NULL) {
3726 ret = 0;
3727 } else {
3728 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3729 }
3730 if (ret == 0) {
3731 DEPTH++;
3732 CONT = CONT->c2;
3733 goto cont;
3734 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003735 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003736 DEPTH++;
3737 CONT = CONT->c1;
3738 goto cont;
3739 }
3740
3741 /*
3742 * At this point handle going up in the tree
3743 */
3744 if (ret == -1) {
3745 DEBUG_VALID_MSG("error found returning");
3746 return(ret);
3747 }
3748analyze:
3749 while (CONT != NULL) {
3750 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003751 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003752 * this level.
3753 */
3754 if (ret == 0) {
3755 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003756 xmlNodePtr cur;
3757
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003758 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003759 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003760 DEBUG_VALID_MSG("Once branch failed, rollback");
3761 if (vstateVPop(ctxt) < 0 ) {
3762 DEBUG_VALID_MSG("exhaustion, failed");
3763 return(0);
3764 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003765 if (cur != ctxt->vstate->node)
3766 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003767 goto cont;
3768 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00003769 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003770 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003771 DEBUG_VALID_MSG("Plus branch failed, rollback");
3772 if (vstateVPop(ctxt) < 0 ) {
3773 DEBUG_VALID_MSG("exhaustion, failed");
3774 return(0);
3775 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003776 if (cur != ctxt->vstate->node)
3777 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003778 goto cont;
3779 }
3780 DEBUG_VALID_MSG("Plus branch found");
3781 ret = 1;
3782 break;
3783 case XML_ELEMENT_CONTENT_MULT:
3784#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00003785 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003786 DEBUG_VALID_MSG("Mult branch failed");
3787 } else {
3788 DEBUG_VALID_MSG("Mult branch found");
3789 }
3790#endif
3791 ret = 1;
3792 break;
3793 case XML_ELEMENT_CONTENT_OPT:
3794 DEBUG_VALID_MSG("Option branch failed");
3795 ret = 1;
3796 break;
3797 }
3798 } else {
3799 switch (CONT->ocur) {
3800 case XML_ELEMENT_CONTENT_OPT:
3801 DEBUG_VALID_MSG("Option branch succeeded");
3802 ret = 1;
3803 break;
3804 case XML_ELEMENT_CONTENT_ONCE:
3805 DEBUG_VALID_MSG("Once branch succeeded");
3806 ret = 1;
3807 break;
3808 case XML_ELEMENT_CONTENT_PLUS:
3809 if (STATE == ROLLBACK_PARENT) {
3810 DEBUG_VALID_MSG("Plus branch rollback");
3811 ret = 1;
3812 break;
3813 }
3814 if (NODE == NULL) {
3815 DEBUG_VALID_MSG("Plus branch exhausted");
3816 ret = 1;
3817 break;
3818 }
3819 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003820 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003821 goto cont;
3822 case XML_ELEMENT_CONTENT_MULT:
3823 if (STATE == ROLLBACK_PARENT) {
3824 DEBUG_VALID_MSG("Mult branch rollback");
3825 ret = 1;
3826 break;
3827 }
3828 if (NODE == NULL) {
3829 DEBUG_VALID_MSG("Mult branch exhausted");
3830 ret = 1;
3831 break;
3832 }
3833 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003834 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003835 goto cont;
3836 }
3837 }
3838 STATE = 0;
3839
3840 /*
3841 * Then act accordingly at the parent level
3842 */
Daniel Veillard5344c602001-12-31 16:37:34 +00003843 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003844 if (CONT->parent == NULL)
3845 break;
3846
3847 switch (CONT->parent->type) {
3848 case XML_ELEMENT_CONTENT_PCDATA:
3849 DEBUG_VALID_MSG("Error: parent pcdata");
3850 return(-1);
3851 case XML_ELEMENT_CONTENT_ELEMENT:
3852 DEBUG_VALID_MSG("Error: parent element");
3853 return(-1);
3854 case XML_ELEMENT_CONTENT_OR:
3855 if (ret == 1) {
3856 DEBUG_VALID_MSG("Or succeeded");
3857 CONT = CONT->parent;
3858 DEPTH--;
3859 } else {
3860 DEBUG_VALID_MSG("Or failed");
3861 CONT = CONT->parent;
3862 DEPTH--;
3863 }
3864 break;
3865 case XML_ELEMENT_CONTENT_SEQ:
3866 if (ret == 0) {
3867 DEBUG_VALID_MSG("Sequence failed");
3868 CONT = CONT->parent;
3869 DEPTH--;
3870 } else if (CONT == CONT->parent->c1) {
3871 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3872 CONT = CONT->parent->c2;
3873 goto cont;
3874 } else {
3875 DEBUG_VALID_MSG("Sequence succeeded");
3876 CONT = CONT->parent;
3877 DEPTH--;
3878 }
3879 }
3880 }
3881 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003882 xmlNodePtr cur;
3883
3884 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003885 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3886 if (vstateVPop(ctxt) < 0 ) {
3887 DEBUG_VALID_MSG("exhaustion, failed");
3888 return(0);
3889 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003890 if (cur != ctxt->vstate->node)
3891 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003892 goto cont;
3893 }
3894 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003895 xmlNodePtr cur;
3896
3897 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003898 DEBUG_VALID_MSG("Failure, rollback");
3899 if (vstateVPop(ctxt) < 0 ) {
3900 DEBUG_VALID_MSG("exhaustion, failed");
3901 return(0);
3902 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003903 if (cur != ctxt->vstate->node)
3904 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003905 goto cont;
3906 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003907 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003908}
3909
3910/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003911 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003912 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003913 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003914 * @content: An element
3915 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3916 *
3917 * This will dump the list of elements to the buffer
3918 * Intended just for the debug routine
3919 */
3920static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003921xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003922 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003923 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003924
3925 if (node == NULL) return;
3926 if (glob) strcat(buf, "(");
3927 cur = node;
3928 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003929 len = strlen(buf);
3930 if (size - len < 50) {
3931 if ((size - len > 4) && (buf[len - 1] != '.'))
3932 strcat(buf, " ...");
3933 return;
3934 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003935 switch (cur->type) {
3936 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003937 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00003938 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003939 if ((size - len > 4) && (buf[len - 1] != '.'))
3940 strcat(buf, " ...");
3941 return;
3942 }
3943 strcat(buf, (char *) cur->ns->prefix);
3944 strcat(buf, ":");
3945 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00003946 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003947 if ((size - len > 4) && (buf[len - 1] != '.'))
3948 strcat(buf, " ...");
3949 return;
3950 }
3951 strcat(buf, (char *) cur->name);
3952 if (cur->next != NULL)
3953 strcat(buf, " ");
3954 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003955 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003956 if (xmlIsBlankNode(cur))
3957 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003958 case XML_CDATA_SECTION_NODE:
3959 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003960 strcat(buf, "CDATA");
3961 if (cur->next != NULL)
3962 strcat(buf, " ");
3963 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003964 case XML_ATTRIBUTE_NODE:
3965 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003966#ifdef LIBXML_DOCB_ENABLED
3967 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003968#endif
3969 case XML_HTML_DOCUMENT_NODE:
3970 case XML_DOCUMENT_TYPE_NODE:
3971 case XML_DOCUMENT_FRAG_NODE:
3972 case XML_NOTATION_NODE:
3973 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003974 strcat(buf, "???");
3975 if (cur->next != NULL)
3976 strcat(buf, " ");
3977 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003978 case XML_ENTITY_NODE:
3979 case XML_PI_NODE:
3980 case XML_DTD_NODE:
3981 case XML_COMMENT_NODE:
3982 case XML_ELEMENT_DECL:
3983 case XML_ATTRIBUTE_DECL:
3984 case XML_ENTITY_DECL:
3985 case XML_XINCLUDE_START:
3986 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003987 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003988 }
3989 cur = cur->next;
3990 }
3991 if (glob) strcat(buf, ")");
3992}
3993
3994/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003995 * xmlValidateElementContent:
3996 * @ctxt: the validation context
3997 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003998 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003999 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004000 *
4001 * Try to validate the content model of an element
4002 *
4003 * returns 1 if valid or 0 if not and -1 in case of error
4004 */
4005
4006static int
4007xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004008 xmlElementPtr elemDecl, int warn) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004009 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004010 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004011 xmlElementContentPtr cont;
4012 const xmlChar *name;
4013
4014 if (elemDecl == NULL)
4015 return(-1);
4016 cont = elemDecl->content;
4017 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004018
4019 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004020 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004021 */
4022 ctxt->vstateMax = 8;
4023 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4024 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4025 if (ctxt->vstateTab == NULL) {
4026 xmlGenericError(xmlGenericErrorContext,
4027 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004028 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004029 }
4030 /*
4031 * The first entry in the stack is reserved to the current state
4032 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004033 ctxt->nodeMax = 0;
4034 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004035 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004036 ctxt->vstate = &ctxt->vstateTab[0];
4037 ctxt->vstateNr = 1;
4038 CONT = cont;
4039 NODE = child;
4040 DEPTH = 0;
4041 OCCURS = 0;
4042 STATE = 0;
4043 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004044 if ((ret == -3) && (warn)) {
4045 VWARNING(ctxt->userData,
4046 "Element %s content model is ambiguous\n", name);
4047 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004048 /*
4049 * An entities reference appeared at this level.
4050 * Buid a minimal representation of this node content
4051 * sufficient to run the validation process on it
4052 */
4053 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004054 cur = child;
4055 while (cur != NULL) {
4056 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004057 case XML_ENTITY_REF_NODE:
4058 /*
4059 * Push the current node to be able to roll back
4060 * and process within the entity
4061 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004062 if ((cur->children != NULL) &&
4063 (cur->children->children != NULL)) {
4064 nodeVPush(ctxt, cur);
4065 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004066 continue;
4067 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004068 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004069 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004070 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004071 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004072 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004073 case XML_CDATA_SECTION_NODE:
4074 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004075 case XML_ELEMENT_NODE:
4076 /*
4077 * Allocate a new node and minimally fills in
4078 * what's required
4079 */
4080 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4081 if (tmp == NULL) {
4082 xmlGenericError(xmlGenericErrorContext,
4083 "xmlValidateElementContent : malloc failed\n");
4084 xmlFreeNodeList(repl);
4085 ret = -1;
4086 goto done;
4087 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004088 tmp->type = cur->type;
4089 tmp->name = cur->name;
4090 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004091 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004092 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004093 if (repl == NULL)
4094 repl = last = tmp;
4095 else {
4096 last->next = tmp;
4097 last = tmp;
4098 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004099 if (cur->type == XML_CDATA_SECTION_NODE) {
4100 /*
4101 * E59 spaces in CDATA does not match the
4102 * nonterminal S
4103 */
4104 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4105 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004106 break;
4107 default:
4108 break;
4109 }
4110 /*
4111 * Switch to next element
4112 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004113 cur = cur->next;
4114 while (cur == NULL) {
4115 cur = nodeVPop(ctxt);
4116 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004117 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004118 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004119 }
4120 }
4121
4122 /*
4123 * Relaunch the validation
4124 */
4125 ctxt->vstate = &ctxt->vstateTab[0];
4126 ctxt->vstateNr = 1;
4127 CONT = cont;
4128 NODE = repl;
4129 DEPTH = 0;
4130 OCCURS = 0;
4131 STATE = 0;
4132 ret = xmlValidateElementType(ctxt);
4133 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004134 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004135 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4136 char expr[5000];
4137 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004138
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004139 expr[0] = 0;
4140 xmlSnprintfElementContent(expr, 5000, cont, 1);
4141 list[0] = 0;
4142 if (repl != NULL)
4143 xmlSnprintfElements(list, 5000, repl, 1);
4144 else
4145 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004146
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004147 if (name != NULL) {
4148 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004149 "Element %s content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004150 name, expr, list);
4151 } else {
4152 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004153 "Element content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004154 expr, list);
4155 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004156 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004157 if (name != NULL) {
4158 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004159 "Element %s content doesn't follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004160 name);
4161 } else {
4162 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004163 "Element content doesn't follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004164 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004165 }
4166 ret = 0;
4167 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004168 if (ret == -3)
4169 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004170
4171
4172done:
4173 /*
4174 * Deallocate the copy if done, and free up the validation stack
4175 */
4176 while (repl != NULL) {
4177 tmp = repl->next;
4178 xmlFree(repl);
4179 repl = tmp;
4180 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004181 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004182 if (ctxt->vstateTab != NULL) {
4183 xmlFree(ctxt->vstateTab);
4184 ctxt->vstateTab = NULL;
4185 }
4186 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004187 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004188 if (ctxt->nodeTab != NULL) {
4189 xmlFree(ctxt->nodeTab);
4190 ctxt->nodeTab = NULL;
4191 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004192 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004193
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004194}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004195
Owen Taylor3473f882001-02-23 17:55:21 +00004196/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004197 * xmlValidateCdataElement:
4198 * @ctxt: the validation context
4199 * @doc: a document instance
4200 * @elem: an element instance
4201 *
4202 * Check that an element follows #CDATA
4203 *
4204 * returns 1 if valid or 0 otherwise
4205 */
4206static int
4207xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4208 xmlNodePtr elem) {
4209 int ret = 1;
4210 xmlNodePtr cur, child;
4211
4212 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4213 return(0);
4214
4215 child = elem->children;
4216
4217 cur = child;
4218 while (cur != NULL) {
4219 switch (cur->type) {
4220 case XML_ENTITY_REF_NODE:
4221 /*
4222 * Push the current node to be able to roll back
4223 * and process within the entity
4224 */
4225 if ((cur->children != NULL) &&
4226 (cur->children->children != NULL)) {
4227 nodeVPush(ctxt, cur);
4228 cur = cur->children->children;
4229 continue;
4230 }
4231 break;
4232 case XML_COMMENT_NODE:
4233 case XML_PI_NODE:
4234 case XML_TEXT_NODE:
4235 case XML_CDATA_SECTION_NODE:
4236 break;
4237 default:
4238 ret = 0;
4239 goto done;
4240 }
4241 /*
4242 * Switch to next element
4243 */
4244 cur = cur->next;
4245 while (cur == NULL) {
4246 cur = nodeVPop(ctxt);
4247 if (cur == NULL)
4248 break;
4249 cur = cur->next;
4250 }
4251 }
4252done:
4253 ctxt->nodeMax = 0;
4254 ctxt->nodeNr = 0;
4255 if (ctxt->nodeTab != NULL) {
4256 xmlFree(ctxt->nodeTab);
4257 ctxt->nodeTab = NULL;
4258 }
4259 return(ret);
4260}
4261
4262/**
Owen Taylor3473f882001-02-23 17:55:21 +00004263 * xmlValidateOneElement:
4264 * @ctxt: the validation context
4265 * @doc: a document instance
4266 * @elem: an element instance
4267 *
4268 * Try to validate a single element and it's attributes,
4269 * basically it does the following checks as described by the
4270 * XML-1.0 recommendation:
4271 * - [ VC: Element Valid ]
4272 * - [ VC: Required Attribute ]
4273 * Then call xmlValidateOneAttribute() for each attribute present.
4274 *
4275 * The ID/IDREF checkings are done separately
4276 *
4277 * returns 1 if valid or 0 otherwise
4278 */
4279
4280int
4281xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4282 xmlNodePtr elem) {
4283 xmlElementPtr elemDecl = NULL;
4284 xmlElementContentPtr cont;
4285 xmlAttributePtr attr;
4286 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004287 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004288 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004289 const xmlChar *prefix = NULL;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004290 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004291
4292 CHECK_DTD;
4293
4294 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004295 switch (elem->type) {
4296 case XML_ATTRIBUTE_NODE:
4297 VERROR(ctxt->userData,
4298 "Attribute element not expected here\n");
4299 return(0);
4300 case XML_TEXT_NODE:
4301 if (elem->children != NULL) {
4302 VERROR(ctxt->userData, "Text element has childs !\n");
4303 return(0);
4304 }
4305 if (elem->properties != NULL) {
4306 VERROR(ctxt->userData, "Text element has attributes !\n");
4307 return(0);
4308 }
4309 if (elem->ns != NULL) {
4310 VERROR(ctxt->userData, "Text element has namespace !\n");
4311 return(0);
4312 }
4313 if (elem->nsDef != NULL) {
4314 VERROR(ctxt->userData,
4315 "Text element carries namespace definitions !\n");
4316 return(0);
4317 }
4318 if (elem->content == NULL) {
4319 VERROR(ctxt->userData,
4320 "Text element has no content !\n");
4321 return(0);
4322 }
4323 return(1);
4324 case XML_XINCLUDE_START:
4325 case XML_XINCLUDE_END:
4326 return(1);
4327 case XML_CDATA_SECTION_NODE:
4328 case XML_ENTITY_REF_NODE:
4329 case XML_PI_NODE:
4330 case XML_COMMENT_NODE:
4331 return(1);
4332 case XML_ENTITY_NODE:
4333 VERROR(ctxt->userData,
4334 "Entity element not expected here\n");
4335 return(0);
4336 case XML_NOTATION_NODE:
4337 VERROR(ctxt->userData,
4338 "Notation element not expected here\n");
4339 return(0);
4340 case XML_DOCUMENT_NODE:
4341 case XML_DOCUMENT_TYPE_NODE:
4342 case XML_DOCUMENT_FRAG_NODE:
4343 VERROR(ctxt->userData,
4344 "Document element not expected here\n");
4345 return(0);
4346 case XML_HTML_DOCUMENT_NODE:
4347 VERROR(ctxt->userData,
4348 "\n");
4349 return(0);
4350 case XML_ELEMENT_NODE:
4351 break;
4352 default:
4353 VERROR(ctxt->userData,
4354 "unknown element type %d\n", elem->type);
4355 return(0);
4356 }
4357 if (elem->name == NULL) return(0);
4358
4359 /*
4360 * Fetch the declaration for the qualified name
4361 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004362 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4363 prefix = elem->ns->prefix;
4364
4365 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004366 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004367 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004368 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004369 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004370 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004371 if (elemDecl != NULL)
4372 extsubset = 1;
4373 }
Owen Taylor3473f882001-02-23 17:55:21 +00004374 }
4375
4376 /*
4377 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004378 * This is "non-strict" validation should be done on the
4379 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004380 */
4381 if (elemDecl == NULL) {
4382 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004383 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004384 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004385 if (elemDecl != NULL)
4386 extsubset = 1;
4387 }
Owen Taylor3473f882001-02-23 17:55:21 +00004388 }
4389 if (elemDecl == NULL) {
4390 VERROR(ctxt->userData, "No declaration for element %s\n",
4391 elem->name);
4392 return(0);
4393 }
4394
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004395 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00004396 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004397 case XML_ELEMENT_TYPE_UNDEFINED:
4398 VERROR(ctxt->userData, "No declaration for element %s\n",
4399 elem->name);
4400 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004401 case XML_ELEMENT_TYPE_EMPTY:
4402 if (elem->children != NULL) {
4403 VERROR(ctxt->userData,
4404 "Element %s was declared EMPTY this one has content\n",
4405 elem->name);
4406 ret = 0;
4407 }
4408 break;
4409 case XML_ELEMENT_TYPE_ANY:
4410 /* I don't think anything is required then */
4411 break;
4412 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004413
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004414 /* simple case of declared as #PCDATA */
4415 if ((elemDecl->content != NULL) &&
4416 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4417 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4418 if (!ret) {
4419 VERROR(ctxt->userData,
4420 "Element %s was declared #PCDATA but contains non text nodes\n",
4421 elem->name);
4422 }
4423 break;
4424 }
Owen Taylor3473f882001-02-23 17:55:21 +00004425 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004426 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004427 while (child != NULL) {
4428 if (child->type == XML_ELEMENT_NODE) {
4429 name = child->name;
4430 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4431 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004432 snprintf((char *) qname, sizeof(qname), "%s:%s",
4433 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004434 qname[sizeof(qname) - 1] = 0;
4435 cont = elemDecl->content;
4436 while (cont != NULL) {
4437 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4438 if (xmlStrEqual(cont->name, qname)) break;
4439 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4440 (cont->c1 != NULL) &&
4441 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4442 if (xmlStrEqual(cont->c1->name, qname)) break;
4443 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4444 (cont->c1 == NULL) ||
4445 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4446 /* Internal error !!! */
4447 xmlGenericError(xmlGenericErrorContext,
4448 "Internal: MIXED struct bad\n");
4449 break;
4450 }
4451 cont = cont->c2;
4452 }
4453 if (cont != NULL)
4454 goto child_ok;
4455 }
4456 cont = elemDecl->content;
4457 while (cont != NULL) {
4458 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4459 if (xmlStrEqual(cont->name, name)) break;
4460 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4461 (cont->c1 != NULL) &&
4462 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4463 if (xmlStrEqual(cont->c1->name, name)) break;
4464 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4465 (cont->c1 == NULL) ||
4466 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4467 /* Internal error !!! */
4468 xmlGenericError(xmlGenericErrorContext,
4469 "Internal: MIXED struct bad\n");
4470 break;
4471 }
4472 cont = cont->c2;
4473 }
4474 if (cont == NULL) {
4475 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00004476 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004477 name, elem->name);
4478 ret = 0;
4479 }
4480 }
4481child_ok:
4482 child = child->next;
4483 }
4484 break;
4485 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004486 if ((doc->standalone == 1) && (extsubset == 1)) {
4487 /*
4488 * VC: Standalone Document Declaration
4489 * - element types with element content, if white space
4490 * occurs directly within any instance of those types.
4491 */
4492 child = elem->children;
4493 while (child != NULL) {
4494 if (child->type == XML_TEXT_NODE) {
4495 const xmlChar *content = child->content;
4496
4497 while (IS_BLANK(*content))
4498 content++;
4499 if (*content == 0) {
4500 VERROR(ctxt->userData,
4501"standalone: %s declared in the external subset contains white spaces nodes\n",
4502 elem->name);
4503 ret = 0;
4504 break;
4505 }
4506 }
4507 child =child->next;
4508 }
4509 }
Owen Taylor3473f882001-02-23 17:55:21 +00004510 child = elem->children;
4511 cont = elemDecl->content;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004512 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1);
4513 if (tmp <= 0)
4514 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004515 break;
4516 }
4517
4518 /* [ VC: Required Attribute ] */
4519 attr = elemDecl->attributes;
4520 while (attr != NULL) {
4521 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004522 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00004523
Daniel Veillarde4301c82002-02-13 13:32:35 +00004524 if ((attr->prefix == NULL) &&
4525 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4526 xmlNsPtr ns;
4527
4528 ns = elem->nsDef;
4529 while (ns != NULL) {
4530 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004531 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004532 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00004533 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004534 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4535 xmlNsPtr ns;
4536
4537 ns = elem->nsDef;
4538 while (ns != NULL) {
4539 if (xmlStrEqual(attr->name, ns->prefix))
4540 goto found;
4541 ns = ns->next;
4542 }
4543 } else {
4544 xmlAttrPtr attrib;
4545
4546 attrib = elem->properties;
4547 while (attrib != NULL) {
4548 if (xmlStrEqual(attrib->name, attr->name)) {
4549 if (attr->prefix != NULL) {
4550 xmlNsPtr nameSpace = attrib->ns;
4551
4552 if (nameSpace == NULL)
4553 nameSpace = elem->ns;
4554 /*
4555 * qualified names handling is problematic, having a
4556 * different prefix should be possible but DTDs don't
4557 * allow to define the URI instead of the prefix :-(
4558 */
4559 if (nameSpace == NULL) {
4560 if (qualified < 0)
4561 qualified = 0;
4562 } else if (!xmlStrEqual(nameSpace->prefix,
4563 attr->prefix)) {
4564 if (qualified < 1)
4565 qualified = 1;
4566 } else
4567 goto found;
4568 } else {
4569 /*
4570 * We should allow applications to define namespaces
4571 * for their application even if the DTD doesn't
4572 * carry one, otherwise, basically we would always
4573 * break.
4574 */
4575 goto found;
4576 }
4577 }
4578 attrib = attrib->next;
4579 }
Owen Taylor3473f882001-02-23 17:55:21 +00004580 }
4581 if (qualified == -1) {
4582 if (attr->prefix == NULL) {
4583 VERROR(ctxt->userData,
4584 "Element %s doesn't carry attribute %s\n",
4585 elem->name, attr->name);
4586 ret = 0;
4587 } else {
4588 VERROR(ctxt->userData,
4589 "Element %s doesn't carry attribute %s:%s\n",
4590 elem->name, attr->prefix,attr->name);
4591 ret = 0;
4592 }
4593 } else if (qualified == 0) {
4594 VWARNING(ctxt->userData,
4595 "Element %s required attribute %s:%s has no prefix\n",
4596 elem->name, attr->prefix,attr->name);
4597 } else if (qualified == 1) {
4598 VWARNING(ctxt->userData,
4599 "Element %s required attribute %s:%s has different prefix\n",
4600 elem->name, attr->prefix,attr->name);
4601 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004602 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
4603 /*
4604 * Special tests checking #FIXED namespace declarations
4605 * have the right value since this is not done as an
4606 * attribute checking
4607 */
4608 if ((attr->prefix == NULL) &&
4609 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4610 xmlNsPtr ns;
4611
4612 ns = elem->nsDef;
4613 while (ns != NULL) {
4614 if (ns->prefix == NULL) {
4615 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4616 VERROR(ctxt->userData,
4617 "Element %s namespace name for default namespace does not match the DTD\n",
4618 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00004619 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004620 }
4621 goto found;
4622 }
4623 ns = ns->next;
4624 }
4625 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4626 xmlNsPtr ns;
4627
4628 ns = elem->nsDef;
4629 while (ns != NULL) {
4630 if (xmlStrEqual(attr->name, ns->prefix)) {
4631 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4632 VERROR(ctxt->userData,
4633 "Element %s namespace name for %s doesn't match the DTD\n",
4634 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00004635 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004636 }
4637 goto found;
4638 }
4639 ns = ns->next;
4640 }
4641 }
Owen Taylor3473f882001-02-23 17:55:21 +00004642 }
4643found:
4644 attr = attr->nexth;
4645 }
4646 return(ret);
4647}
4648
4649/**
4650 * xmlValidateRoot:
4651 * @ctxt: the validation context
4652 * @doc: a document instance
4653 *
4654 * Try to validate a the root element
4655 * basically it does the following check as described by the
4656 * XML-1.0 recommendation:
4657 * - [ VC: Root Element Type ]
4658 * it doesn't try to recurse or apply other check to the element
4659 *
4660 * returns 1 if valid or 0 otherwise
4661 */
4662
4663int
4664xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4665 xmlNodePtr root;
4666 if (doc == NULL) return(0);
4667
4668 root = xmlDocGetRootElement(doc);
4669 if ((root == NULL) || (root->name == NULL)) {
4670 VERROR(ctxt->userData, "Not valid: no root element\n");
4671 return(0);
4672 }
4673
4674 /*
4675 * When doing post validation against a separate DTD, those may
4676 * no internal subset has been generated
4677 */
4678 if ((doc->intSubset != NULL) &&
4679 (doc->intSubset->name != NULL)) {
4680 /*
4681 * Check first the document root against the NQName
4682 */
4683 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4684 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4685 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004686 snprintf((char *) qname, sizeof(qname), "%s:%s",
4687 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004688 qname[sizeof(qname) - 1] = 0;
4689 if (xmlStrEqual(doc->intSubset->name, qname))
4690 goto name_ok;
4691 }
4692 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4693 (xmlStrEqual(root->name, BAD_CAST "html")))
4694 goto name_ok;
4695 VERROR(ctxt->userData,
4696 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4697 root->name, doc->intSubset->name);
4698 return(0);
4699
4700 }
4701 }
4702name_ok:
4703 return(1);
4704}
4705
4706
4707/**
4708 * xmlValidateElement:
4709 * @ctxt: the validation context
4710 * @doc: a document instance
4711 * @elem: an element instance
4712 *
4713 * Try to validate the subtree under an element
4714 *
4715 * returns 1 if valid or 0 otherwise
4716 */
4717
4718int
4719xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4720 xmlNodePtr child;
4721 xmlAttrPtr attr;
4722 xmlChar *value;
4723 int ret = 1;
4724
4725 if (elem == NULL) return(0);
4726
4727 /*
4728 * XInclude elements were added after parsing in the infoset,
4729 * they don't really mean anything validation wise.
4730 */
4731 if ((elem->type == XML_XINCLUDE_START) ||
4732 (elem->type == XML_XINCLUDE_END))
4733 return(1);
4734
4735 CHECK_DTD;
4736
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004737 /*
4738 * Entities references have to be handled separately
4739 */
4740 if (elem->type == XML_ENTITY_REF_NODE) {
4741 return(1);
4742 }
4743
Owen Taylor3473f882001-02-23 17:55:21 +00004744 ret &= xmlValidateOneElement(ctxt, doc, elem);
4745 attr = elem->properties;
4746 while(attr != NULL) {
4747 value = xmlNodeListGetString(doc, attr->children, 0);
4748 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4749 if (value != NULL)
4750 xmlFree(value);
4751 attr= attr->next;
4752 }
4753 child = elem->children;
4754 while (child != NULL) {
4755 ret &= xmlValidateElement(ctxt, doc, child);
4756 child = child->next;
4757 }
4758
4759 return(ret);
4760}
4761
Daniel Veillard8730c562001-02-26 10:49:57 +00004762/**
4763 * xmlValidateRef:
4764 * @ref: A reference to be validated
4765 * @ctxt: Validation context
4766 * @name: Name of ID we are searching for
4767 *
4768 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004769static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004770xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004771 const xmlChar *name) {
4772 xmlAttrPtr id;
4773 xmlAttrPtr attr;
4774
4775 if (ref == NULL)
4776 return;
4777 attr = ref->attr;
4778 if (attr == NULL)
4779 return;
4780 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4781 id = xmlGetID(ctxt->doc, name);
4782 if (id == NULL) {
4783 VERROR(ctxt->userData,
4784 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4785 attr->name, name);
4786 ctxt->valid = 0;
4787 }
4788 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4789 xmlChar *dup, *str = NULL, *cur, save;
4790
4791 dup = xmlStrdup(name);
4792 if (dup == NULL) {
4793 ctxt->valid = 0;
4794 return;
4795 }
4796 cur = dup;
4797 while (*cur != 0) {
4798 str = cur;
4799 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4800 save = *cur;
4801 *cur = 0;
4802 id = xmlGetID(ctxt->doc, str);
4803 if (id == NULL) {
4804 VERROR(ctxt->userData,
4805 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4806 attr->name, str);
4807 ctxt->valid = 0;
4808 }
4809 if (save == 0)
4810 break;
4811 *cur = save;
4812 while (IS_BLANK(*cur)) cur++;
4813 }
4814 xmlFree(dup);
4815 }
4816}
4817
4818/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004819 * xmlWalkValidateList:
4820 * @data: Contents of current link
4821 * @user: Value supplied by the user
4822 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004823 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00004824 */
4825static int
4826xmlWalkValidateList(const void *data, const void *user)
4827{
4828 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4829 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4830 return 1;
4831}
4832
4833/**
4834 * xmlValidateCheckRefCallback:
4835 * @ref_list: List of references
4836 * @ctxt: Validation context
4837 * @name: Name of ID we are searching for
4838 *
4839 */
4840static void
4841xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4842 const xmlChar *name) {
4843 xmlValidateMemo memo;
4844
4845 if (ref_list == NULL)
4846 return;
4847 memo.ctxt = ctxt;
4848 memo.name = name;
4849
4850 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4851
4852}
4853
4854/**
Owen Taylor3473f882001-02-23 17:55:21 +00004855 * xmlValidateDocumentFinal:
4856 * @ctxt: the validation context
4857 * @doc: a document instance
4858 *
4859 * Does the final step for the document validation once all the
4860 * incremental validation steps have been completed
4861 *
4862 * basically it does the following checks described by the XML Rec
4863 *
4864 *
4865 * returns 1 if valid or 0 otherwise
4866 */
4867
4868int
4869xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4870 xmlRefTablePtr table;
4871
4872 if (doc == NULL) {
4873 xmlGenericError(xmlGenericErrorContext,
4874 "xmlValidateDocumentFinal: doc == NULL\n");
4875 return(0);
4876 }
4877
4878 /*
4879 * Check all the NOTATION/NOTATIONS attributes
4880 */
4881 /*
4882 * Check all the ENTITY/ENTITIES attributes definition for validity
4883 */
4884 /*
4885 * Check all the IDREF/IDREFS attributes definition for validity
4886 */
4887 table = (xmlRefTablePtr) doc->refs;
4888 ctxt->doc = doc;
4889 ctxt->valid = 1;
4890 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4891 return(ctxt->valid);
4892}
4893
4894/**
4895 * xmlValidateDtd:
4896 * @ctxt: the validation context
4897 * @doc: a document instance
4898 * @dtd: a dtd instance
4899 *
4900 * Try to validate the document against the dtd instance
4901 *
4902 * basically it does check all the definitions in the DtD.
4903 *
4904 * returns 1 if valid or 0 otherwise
4905 */
4906
4907int
4908xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4909 int ret;
4910 xmlDtdPtr oldExt;
4911 xmlNodePtr root;
4912
4913 if (dtd == NULL) return(0);
4914 if (doc == NULL) return(0);
4915 oldExt = doc->extSubset;
4916 doc->extSubset = dtd;
4917 ret = xmlValidateRoot(ctxt, doc);
4918 if (ret == 0) {
4919 doc->extSubset = oldExt;
4920 return(ret);
4921 }
4922 if (doc->ids != NULL) {
4923 xmlFreeIDTable(doc->ids);
4924 doc->ids = NULL;
4925 }
4926 if (doc->refs != NULL) {
4927 xmlFreeRefTable(doc->refs);
4928 doc->refs = NULL;
4929 }
4930 root = xmlDocGetRootElement(doc);
4931 ret = xmlValidateElement(ctxt, doc, root);
4932 ret &= xmlValidateDocumentFinal(ctxt, doc);
4933 doc->extSubset = oldExt;
4934 return(ret);
4935}
4936
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004937static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004938xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
4939 const xmlChar *name ATTRIBUTE_UNUSED) {
4940 if (cur == NULL)
4941 return;
4942 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
4943 xmlChar *notation = cur->content;
4944
Daniel Veillard878eab02002-02-19 13:46:09 +00004945 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004946 int ret;
4947
4948 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
4949 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00004950 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004951 }
4952 }
4953 }
4954}
4955
4956static void
Owen Taylor3473f882001-02-23 17:55:21 +00004957xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004958 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004959 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00004960 xmlDocPtr doc;
4961 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004962
Owen Taylor3473f882001-02-23 17:55:21 +00004963 if (cur == NULL)
4964 return;
4965 switch (cur->atype) {
4966 case XML_ATTRIBUTE_CDATA:
4967 case XML_ATTRIBUTE_ID:
4968 case XML_ATTRIBUTE_IDREF :
4969 case XML_ATTRIBUTE_IDREFS:
4970 case XML_ATTRIBUTE_NMTOKEN:
4971 case XML_ATTRIBUTE_NMTOKENS:
4972 case XML_ATTRIBUTE_ENUMERATION:
4973 break;
4974 case XML_ATTRIBUTE_ENTITY:
4975 case XML_ATTRIBUTE_ENTITIES:
4976 case XML_ATTRIBUTE_NOTATION:
4977 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004978
4979 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
4980 cur->atype, cur->defaultValue);
4981 if ((ret == 0) && (ctxt->valid == 1))
4982 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004983 }
4984 if (cur->tree != NULL) {
4985 xmlEnumerationPtr tree = cur->tree;
4986 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004987 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00004988 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004989 if ((ret == 0) && (ctxt->valid == 1))
4990 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004991 tree = tree->next;
4992 }
4993 }
4994 }
Daniel Veillard878eab02002-02-19 13:46:09 +00004995 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
4996 doc = cur->doc;
4997 if ((doc == NULL) || (cur->elem == NULL)) {
4998 VERROR(ctxt->userData,
4999 "xmlValidateAttributeCallback(%s): internal error\n",
5000 cur->name);
5001 return;
5002 }
5003 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
5004 if (elem == NULL)
5005 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
5006 if (elem == NULL) {
5007 VERROR(ctxt->userData,
5008 "attribute %s: could not find decl for element %s\n",
5009 cur->name, cur->elem);
5010 return;
5011 }
5012 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
5013 VERROR(ctxt->userData,
5014 "NOTATION attribute %s declared on EMPTY element %s\n",
5015 cur->name, cur->elem);
5016 ctxt->valid = 0;
5017 }
5018 }
Owen Taylor3473f882001-02-23 17:55:21 +00005019}
5020
5021/**
5022 * xmlValidateDtdFinal:
5023 * @ctxt: the validation context
5024 * @doc: a document instance
5025 *
5026 * Does the final step for the dtds validation once all the
5027 * subsets have been parsed
5028 *
5029 * basically it does the following checks described by the XML Rec
5030 * - check that ENTITY and ENTITIES type attributes default or
5031 * possible values matches one of the defined entities.
5032 * - check that NOTATION type attributes default or
5033 * possible values matches one of the defined notations.
5034 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005035 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00005036 */
5037
5038int
5039xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00005040 xmlDtdPtr dtd;
5041 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005042 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00005043
5044 if (doc == NULL) return(0);
5045 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5046 return(0);
5047 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005048 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005049 dtd = doc->intSubset;
5050 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5051 table = (xmlAttributeTablePtr) dtd->attributes;
5052 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005053 }
5054 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005055 entities = (xmlEntitiesTablePtr) dtd->entities;
5056 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5057 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005058 }
5059 dtd = doc->extSubset;
5060 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5061 table = (xmlAttributeTablePtr) dtd->attributes;
5062 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005063 }
5064 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005065 entities = (xmlEntitiesTablePtr) dtd->entities;
5066 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5067 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005068 }
5069 return(ctxt->valid);
5070}
5071
5072/**
5073 * xmlValidateDocument:
5074 * @ctxt: the validation context
5075 * @doc: a document instance
5076 *
5077 * Try to validate the document instance
5078 *
5079 * basically it does the all the checks described by the XML Rec
5080 * i.e. validates the internal and external subset (if present)
5081 * and validate the document tree.
5082 *
5083 * returns 1 if valid or 0 otherwise
5084 */
5085
5086int
5087xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5088 int ret;
5089 xmlNodePtr root;
5090
5091 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5092 return(0);
5093 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
5094 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
5095 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
5096 doc->intSubset->SystemID);
5097 if (doc->extSubset == NULL) {
5098 if (doc->intSubset->SystemID != NULL) {
5099 VERROR(ctxt->userData,
5100 "Could not load the external subset \"%s\"\n",
5101 doc->intSubset->SystemID);
5102 } else {
5103 VERROR(ctxt->userData,
5104 "Could not load the external subset \"%s\"\n",
5105 doc->intSubset->ExternalID);
5106 }
5107 return(0);
5108 }
5109 }
5110
5111 if (doc->ids != NULL) {
5112 xmlFreeIDTable(doc->ids);
5113 doc->ids = NULL;
5114 }
5115 if (doc->refs != NULL) {
5116 xmlFreeRefTable(doc->refs);
5117 doc->refs = NULL;
5118 }
5119 ret = xmlValidateDtdFinal(ctxt, doc);
5120 if (!xmlValidateRoot(ctxt, doc)) return(0);
5121
5122 root = xmlDocGetRootElement(doc);
5123 ret &= xmlValidateElement(ctxt, doc, root);
5124 ret &= xmlValidateDocumentFinal(ctxt, doc);
5125 return(ret);
5126}
5127
5128
5129/************************************************************************
5130 * *
5131 * Routines for dynamic validation editing *
5132 * *
5133 ************************************************************************/
5134
5135/**
5136 * xmlValidGetPotentialChildren:
5137 * @ctree: an element content tree
5138 * @list: an array to store the list of child names
5139 * @len: a pointer to the number of element in the list
5140 * @max: the size of the array
5141 *
5142 * Build/extend a list of potential children allowed by the content tree
5143 *
5144 * returns the number of element in the list, or -1 in case of error.
5145 */
5146
5147int
5148xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
5149 int *len, int max) {
5150 int i;
5151
5152 if ((ctree == NULL) || (list == NULL) || (len == NULL))
5153 return(-1);
5154 if (*len >= max) return(*len);
5155
5156 switch (ctree->type) {
5157 case XML_ELEMENT_CONTENT_PCDATA:
5158 for (i = 0; i < *len;i++)
5159 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
5160 list[(*len)++] = BAD_CAST "#PCDATA";
5161 break;
5162 case XML_ELEMENT_CONTENT_ELEMENT:
5163 for (i = 0; i < *len;i++)
5164 if (xmlStrEqual(ctree->name, list[i])) return(*len);
5165 list[(*len)++] = ctree->name;
5166 break;
5167 case XML_ELEMENT_CONTENT_SEQ:
5168 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5169 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5170 break;
5171 case XML_ELEMENT_CONTENT_OR:
5172 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5173 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5174 break;
5175 }
5176
5177 return(*len);
5178}
5179
5180/**
5181 * xmlValidGetValidElements:
5182 * @prev: an element to insert after
5183 * @next: an element to insert next
5184 * @list: an array to store the list of child names
5185 * @max: the size of the array
5186 *
5187 * This function returns the list of authorized children to insert
5188 * within an existing tree while respecting the validity constraints
5189 * forced by the Dtd. The insertion point is defined using @prev and
5190 * @next in the following ways:
5191 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
5192 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
5193 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
5194 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
5195 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
5196 *
5197 * pointers to the element names are inserted at the beginning of the array
5198 * and do not need to be freed.
5199 *
5200 * returns the number of element in the list, or -1 in case of error. If
5201 * the function returns the value @max the caller is invited to grow the
5202 * receiving array and retry.
5203 */
5204
5205int
5206xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
5207 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005208 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00005209 int nb_valid_elements = 0;
5210 const xmlChar *elements[256];
5211 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005212 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005213
5214 xmlNode *ref_node;
5215 xmlNode *parent;
5216 xmlNode *test_node;
5217
5218 xmlNode *prev_next;
5219 xmlNode *next_prev;
5220 xmlNode *parent_childs;
5221 xmlNode *parent_last;
5222
5223 xmlElement *element_desc;
5224
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00005225 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005226
Owen Taylor3473f882001-02-23 17:55:21 +00005227 if (prev == NULL && next == NULL)
5228 return(-1);
5229
5230 if (list == NULL) return(-1);
5231 if (max <= 0) return(-1);
5232
5233 nb_valid_elements = 0;
5234 ref_node = prev ? prev : next;
5235 parent = ref_node->parent;
5236
5237 /*
5238 * Retrieves the parent element declaration
5239 */
5240 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
5241 parent->name);
5242 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
5243 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
5244 parent->name);
5245 if (element_desc == NULL) return(-1);
5246
5247 /*
5248 * Do a backup of the current tree structure
5249 */
5250 prev_next = prev ? prev->next : NULL;
5251 next_prev = next ? next->prev : NULL;
5252 parent_childs = parent->children;
5253 parent_last = parent->last;
5254
5255 /*
5256 * Creates a dummy node and insert it into the tree
5257 */
5258 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
5259 test_node->doc = ref_node->doc;
5260 test_node->parent = parent;
5261 test_node->prev = prev;
5262 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00005263 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00005264
5265 if (prev) prev->next = test_node;
5266 else parent->children = test_node;
5267
5268 if (next) next->prev = test_node;
5269 else parent->last = test_node;
5270
5271 /*
5272 * Insert each potential child node and check if the parent is
5273 * still valid
5274 */
5275 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
5276 elements, &nb_elements, 256);
5277
5278 for (i = 0;i < nb_elements;i++) {
5279 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005280 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005281 int j;
5282
5283 for (j = 0; j < nb_valid_elements;j++)
5284 if (xmlStrEqual(elements[i], list[j])) break;
5285 list[nb_valid_elements++] = elements[i];
5286 if (nb_valid_elements >= max) break;
5287 }
5288 }
5289
5290 /*
5291 * Restore the tree structure
5292 */
5293 if (prev) prev->next = prev_next;
5294 if (next) next->prev = next_prev;
5295 parent->children = parent_childs;
5296 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00005297
5298 /*
5299 * Free up the dummy node
5300 */
5301 test_node->name = name;
5302 xmlFreeNode(test_node);
5303
Owen Taylor3473f882001-02-23 17:55:21 +00005304 return(nb_valid_elements);
5305}