blob: 6b524b2db04d52953b7f86aab08461bf03c7c733 [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 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001265
Owen Taylor3473f882001-02-23 17:55:21 +00001266 /*
1267 * Check the type and possibly the default value.
1268 */
1269 switch (type) {
1270 case XML_ATTRIBUTE_CDATA:
1271 break;
1272 case XML_ATTRIBUTE_ID:
1273 break;
1274 case XML_ATTRIBUTE_IDREF:
1275 break;
1276 case XML_ATTRIBUTE_IDREFS:
1277 break;
1278 case XML_ATTRIBUTE_ENTITY:
1279 break;
1280 case XML_ATTRIBUTE_ENTITIES:
1281 break;
1282 case XML_ATTRIBUTE_NMTOKEN:
1283 break;
1284 case XML_ATTRIBUTE_NMTOKENS:
1285 break;
1286 case XML_ATTRIBUTE_ENUMERATION:
1287 break;
1288 case XML_ATTRIBUTE_NOTATION:
1289 break;
1290 default:
1291 xmlGenericError(xmlGenericErrorContext,
1292 "xmlAddAttributeDecl: unknown type %d\n", type);
1293 xmlFreeEnumeration(tree);
1294 return(NULL);
1295 }
1296 if ((defaultValue != NULL) &&
1297 (!xmlValidateAttributeValue(type, defaultValue))) {
1298 VERROR(ctxt->userData, "Attribute %s on %s: invalid default value\n",
1299 elem, name, defaultValue);
1300 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001301 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001302 }
1303
1304 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001305 * Check first that an attribute defined in the external subset wasn't
1306 * already defined in the internal subset
1307 */
1308 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1309 (dtd->doc->intSubset != NULL) &&
1310 (dtd->doc->intSubset->attributes != NULL)) {
1311 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1312 if (ret != NULL)
1313 return(NULL);
1314 }
1315
1316 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001317 * Create the Attribute table if needed.
1318 */
1319 table = (xmlAttributeTablePtr) dtd->attributes;
1320 if (table == NULL) {
1321 table = xmlCreateAttributeTable();
1322 dtd->attributes = (void *) table;
1323 }
1324 if (table == NULL) {
1325 xmlGenericError(xmlGenericErrorContext,
1326 "xmlAddAttributeDecl: Table creation failed!\n");
1327 return(NULL);
1328 }
1329
1330
1331 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1332 if (ret == NULL) {
1333 xmlGenericError(xmlGenericErrorContext,
1334 "xmlAddAttributeDecl: out of memory\n");
1335 return(NULL);
1336 }
1337 memset(ret, 0, sizeof(xmlAttribute));
1338 ret->type = XML_ATTRIBUTE_DECL;
1339
1340 /*
1341 * fill the structure.
1342 */
1343 ret->atype = type;
1344 ret->name = xmlStrdup(name);
1345 ret->prefix = xmlStrdup(ns);
1346 ret->elem = xmlStrdup(elem);
1347 ret->def = def;
1348 ret->tree = tree;
1349 if (defaultValue != NULL)
1350 ret->defaultValue = xmlStrdup(defaultValue);
1351
1352 /*
1353 * Validity Check:
1354 * Search the DTD for previous declarations of the ATTLIST
1355 */
1356 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1357 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001358 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001359 */
1360 VWARNING(ctxt->userData,
1361 "Attribute %s on %s: already defined\n",
1362 name, elem);
1363 xmlFreeAttribute(ret);
1364 return(NULL);
1365 }
1366
1367 /*
1368 * Validity Check:
1369 * Multiple ID per element
1370 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001371 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001372 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001373
Owen Taylor3473f882001-02-23 17:55:21 +00001374 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001375 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001376 VERROR(ctxt->userData,
1377 "Element %s has too may ID attributes defined : %s\n",
1378 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001379 ctxt->valid = 0;
1380 }
1381
Daniel Veillard48da9102001-08-07 01:10:10 +00001382 /*
1383 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001384 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001385 */
1386 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1387 ((ret->prefix != NULL &&
1388 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1389 ret->nexth = elemDef->attributes;
1390 elemDef->attributes = ret;
1391 } else {
1392 xmlAttributePtr tmp = elemDef->attributes;
1393
1394 while ((tmp != NULL) &&
1395 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1396 ((ret->prefix != NULL &&
1397 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1398 if (tmp->nexth == NULL)
1399 break;
1400 tmp = tmp->nexth;
1401 }
1402 if (tmp != NULL) {
1403 ret->nexth = tmp->nexth;
1404 tmp->nexth = ret;
1405 } else {
1406 ret->nexth = elemDef->attributes;
1407 elemDef->attributes = ret;
1408 }
1409 }
Owen Taylor3473f882001-02-23 17:55:21 +00001410 }
1411
1412 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001413 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001414 */
1415 ret->parent = dtd;
1416 ret->doc = dtd->doc;
1417 if (dtd->last == NULL) {
1418 dtd->children = dtd->last = (xmlNodePtr) ret;
1419 } else {
1420 dtd->last->next = (xmlNodePtr) ret;
1421 ret->prev = dtd->last;
1422 dtd->last = (xmlNodePtr) ret;
1423 }
1424 return(ret);
1425}
1426
1427/**
1428 * xmlFreeAttributeTable:
1429 * @table: An attribute table
1430 *
1431 * Deallocate the memory used by an entities hash table.
1432 */
1433void
1434xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1435 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1436}
1437
1438/**
1439 * xmlCopyAttribute:
1440 * @attr: An attribute
1441 *
1442 * Build a copy of an attribute.
1443 *
1444 * Returns the new xmlAttributePtr or NULL in case of error.
1445 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001446static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001447xmlCopyAttribute(xmlAttributePtr attr) {
1448 xmlAttributePtr cur;
1449
1450 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1451 if (cur == NULL) {
1452 xmlGenericError(xmlGenericErrorContext,
1453 "xmlCopyAttribute: out of memory !\n");
1454 return(NULL);
1455 }
1456 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001457 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001458 cur->atype = attr->atype;
1459 cur->def = attr->def;
1460 cur->tree = xmlCopyEnumeration(attr->tree);
1461 if (attr->elem != NULL)
1462 cur->elem = xmlStrdup(attr->elem);
1463 if (attr->name != NULL)
1464 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001465 if (attr->prefix != NULL)
1466 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001467 if (attr->defaultValue != NULL)
1468 cur->defaultValue = xmlStrdup(attr->defaultValue);
1469 return(cur);
1470}
1471
1472/**
1473 * xmlCopyAttributeTable:
1474 * @table: An attribute table
1475 *
1476 * Build a copy of an attribute table.
1477 *
1478 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1479 */
1480xmlAttributeTablePtr
1481xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1482 return((xmlAttributeTablePtr) xmlHashCopy(table,
1483 (xmlHashCopier) xmlCopyAttribute));
1484}
1485
1486/**
1487 * xmlDumpAttributeDecl:
1488 * @buf: the XML buffer output
1489 * @attr: An attribute declaration
1490 *
1491 * This will dump the content of the attribute declaration as an XML
1492 * DTD definition
1493 */
1494void
1495xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1496 xmlBufferWriteChar(buf, "<!ATTLIST ");
1497 xmlBufferWriteCHAR(buf, attr->elem);
1498 xmlBufferWriteChar(buf, " ");
1499 if (attr->prefix != NULL) {
1500 xmlBufferWriteCHAR(buf, attr->prefix);
1501 xmlBufferWriteChar(buf, ":");
1502 }
1503 xmlBufferWriteCHAR(buf, attr->name);
1504 switch (attr->atype) {
1505 case XML_ATTRIBUTE_CDATA:
1506 xmlBufferWriteChar(buf, " CDATA");
1507 break;
1508 case XML_ATTRIBUTE_ID:
1509 xmlBufferWriteChar(buf, " ID");
1510 break;
1511 case XML_ATTRIBUTE_IDREF:
1512 xmlBufferWriteChar(buf, " IDREF");
1513 break;
1514 case XML_ATTRIBUTE_IDREFS:
1515 xmlBufferWriteChar(buf, " IDREFS");
1516 break;
1517 case XML_ATTRIBUTE_ENTITY:
1518 xmlBufferWriteChar(buf, " ENTITY");
1519 break;
1520 case XML_ATTRIBUTE_ENTITIES:
1521 xmlBufferWriteChar(buf, " ENTITIES");
1522 break;
1523 case XML_ATTRIBUTE_NMTOKEN:
1524 xmlBufferWriteChar(buf, " NMTOKEN");
1525 break;
1526 case XML_ATTRIBUTE_NMTOKENS:
1527 xmlBufferWriteChar(buf, " NMTOKENS");
1528 break;
1529 case XML_ATTRIBUTE_ENUMERATION:
1530 xmlBufferWriteChar(buf, " (");
1531 xmlDumpEnumeration(buf, attr->tree);
1532 break;
1533 case XML_ATTRIBUTE_NOTATION:
1534 xmlBufferWriteChar(buf, " NOTATION (");
1535 xmlDumpEnumeration(buf, attr->tree);
1536 break;
1537 default:
1538 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001539 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001540 attr->atype);
1541 }
1542 switch (attr->def) {
1543 case XML_ATTRIBUTE_NONE:
1544 break;
1545 case XML_ATTRIBUTE_REQUIRED:
1546 xmlBufferWriteChar(buf, " #REQUIRED");
1547 break;
1548 case XML_ATTRIBUTE_IMPLIED:
1549 xmlBufferWriteChar(buf, " #IMPLIED");
1550 break;
1551 case XML_ATTRIBUTE_FIXED:
1552 xmlBufferWriteChar(buf, " #FIXED");
1553 break;
1554 default:
1555 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001556 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001557 attr->def);
1558 }
1559 if (attr->defaultValue != NULL) {
1560 xmlBufferWriteChar(buf, " ");
1561 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1562 }
1563 xmlBufferWriteChar(buf, ">\n");
1564}
1565
1566/**
1567 * xmlDumpAttributeTable:
1568 * @buf: the XML buffer output
1569 * @table: An attribute table
1570 *
1571 * This will dump the content of the attribute table as an XML DTD definition
1572 */
1573void
1574xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1575 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1576}
1577
1578/************************************************************************
1579 * *
1580 * NOTATIONs *
1581 * *
1582 ************************************************************************/
1583/**
1584 * xmlCreateNotationTable:
1585 *
1586 * create and initialize an empty notation hash table.
1587 *
1588 * Returns the xmlNotationTablePtr just created or NULL in case
1589 * of error.
1590 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001591static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001592xmlCreateNotationTable(void) {
1593 return(xmlHashCreate(0));
1594}
1595
1596/**
1597 * xmlFreeNotation:
1598 * @not: A notation
1599 *
1600 * Deallocate the memory used by an notation definition
1601 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001602static void
Owen Taylor3473f882001-02-23 17:55:21 +00001603xmlFreeNotation(xmlNotationPtr nota) {
1604 if (nota == NULL) return;
1605 if (nota->name != NULL)
1606 xmlFree((xmlChar *) nota->name);
1607 if (nota->PublicID != NULL)
1608 xmlFree((xmlChar *) nota->PublicID);
1609 if (nota->SystemID != NULL)
1610 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001611 xmlFree(nota);
1612}
1613
1614
1615/**
1616 * xmlAddNotationDecl:
1617 * @dtd: pointer to the DTD
1618 * @ctxt: the validation context
1619 * @name: the entity name
1620 * @PublicID: the public identifier or NULL
1621 * @SystemID: the system identifier or NULL
1622 *
1623 * Register a new notation declaration
1624 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001625 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001626 */
1627xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001628xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001629 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001630 const xmlChar *PublicID, const xmlChar *SystemID) {
1631 xmlNotationPtr ret;
1632 xmlNotationTablePtr table;
1633
1634 if (dtd == NULL) {
1635 xmlGenericError(xmlGenericErrorContext,
1636 "xmlAddNotationDecl: dtd == NULL\n");
1637 return(NULL);
1638 }
1639 if (name == NULL) {
1640 xmlGenericError(xmlGenericErrorContext,
1641 "xmlAddNotationDecl: name == NULL\n");
1642 return(NULL);
1643 }
1644 if ((PublicID == NULL) && (SystemID == NULL)) {
1645 xmlGenericError(xmlGenericErrorContext,
1646 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00001647 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001648 }
1649
1650 /*
1651 * Create the Notation table if needed.
1652 */
1653 table = (xmlNotationTablePtr) dtd->notations;
1654 if (table == NULL)
1655 dtd->notations = table = xmlCreateNotationTable();
1656 if (table == NULL) {
1657 xmlGenericError(xmlGenericErrorContext,
1658 "xmlAddNotationDecl: Table creation failed!\n");
1659 return(NULL);
1660 }
1661
1662 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1663 if (ret == NULL) {
1664 xmlGenericError(xmlGenericErrorContext,
1665 "xmlAddNotationDecl: out of memory\n");
1666 return(NULL);
1667 }
1668 memset(ret, 0, sizeof(xmlNotation));
1669
1670 /*
1671 * fill the structure.
1672 */
1673 ret->name = xmlStrdup(name);
1674 if (SystemID != NULL)
1675 ret->SystemID = xmlStrdup(SystemID);
1676 if (PublicID != NULL)
1677 ret->PublicID = xmlStrdup(PublicID);
1678
1679 /*
1680 * Validity Check:
1681 * Check the DTD for previous declarations of the ATTLIST
1682 */
1683 if (xmlHashAddEntry(table, name, ret)) {
1684 xmlGenericError(xmlGenericErrorContext,
1685 "xmlAddNotationDecl: %s already defined\n", name);
1686 xmlFreeNotation(ret);
1687 return(NULL);
1688 }
1689 return(ret);
1690}
1691
1692/**
1693 * xmlFreeNotationTable:
1694 * @table: An notation table
1695 *
1696 * Deallocate the memory used by an entities hash table.
1697 */
1698void
1699xmlFreeNotationTable(xmlNotationTablePtr table) {
1700 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
1701}
1702
1703/**
1704 * xmlCopyNotation:
1705 * @nota: A notation
1706 *
1707 * Build a copy of a notation.
1708 *
1709 * Returns the new xmlNotationPtr or NULL in case of error.
1710 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001711static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001712xmlCopyNotation(xmlNotationPtr nota) {
1713 xmlNotationPtr cur;
1714
1715 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1716 if (cur == NULL) {
1717 xmlGenericError(xmlGenericErrorContext,
1718 "xmlCopyNotation: out of memory !\n");
1719 return(NULL);
1720 }
1721 if (nota->name != NULL)
1722 cur->name = xmlStrdup(nota->name);
1723 else
1724 cur->name = NULL;
1725 if (nota->PublicID != NULL)
1726 cur->PublicID = xmlStrdup(nota->PublicID);
1727 else
1728 cur->PublicID = NULL;
1729 if (nota->SystemID != NULL)
1730 cur->SystemID = xmlStrdup(nota->SystemID);
1731 else
1732 cur->SystemID = NULL;
1733 return(cur);
1734}
1735
1736/**
1737 * xmlCopyNotationTable:
1738 * @table: A notation table
1739 *
1740 * Build a copy of a notation table.
1741 *
1742 * Returns the new xmlNotationTablePtr or NULL in case of error.
1743 */
1744xmlNotationTablePtr
1745xmlCopyNotationTable(xmlNotationTablePtr table) {
1746 return((xmlNotationTablePtr) xmlHashCopy(table,
1747 (xmlHashCopier) xmlCopyNotation));
1748}
1749
1750/**
1751 * xmlDumpNotationDecl:
1752 * @buf: the XML buffer output
1753 * @nota: A notation declaration
1754 *
1755 * This will dump the content the notation declaration as an XML DTD definition
1756 */
1757void
1758xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
1759 xmlBufferWriteChar(buf, "<!NOTATION ");
1760 xmlBufferWriteCHAR(buf, nota->name);
1761 if (nota->PublicID != NULL) {
1762 xmlBufferWriteChar(buf, " PUBLIC ");
1763 xmlBufferWriteQuotedString(buf, nota->PublicID);
1764 if (nota->SystemID != NULL) {
1765 xmlBufferWriteChar(buf, " ");
1766 xmlBufferWriteCHAR(buf, nota->SystemID);
1767 }
1768 } else {
1769 xmlBufferWriteChar(buf, " SYSTEM ");
1770 xmlBufferWriteCHAR(buf, nota->SystemID);
1771 }
1772 xmlBufferWriteChar(buf, " >\n");
1773}
1774
1775/**
1776 * xmlDumpNotationTable:
1777 * @buf: the XML buffer output
1778 * @table: A notation table
1779 *
1780 * This will dump the content of the notation table as an XML DTD definition
1781 */
1782void
1783xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
1784 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
1785}
1786
1787/************************************************************************
1788 * *
1789 * IDs *
1790 * *
1791 ************************************************************************/
1792/**
1793 * xmlCreateIDTable:
1794 *
1795 * create and initialize an empty id hash table.
1796 *
1797 * Returns the xmlIDTablePtr just created or NULL in case
1798 * of error.
1799 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001800static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001801xmlCreateIDTable(void) {
1802 return(xmlHashCreate(0));
1803}
1804
1805/**
1806 * xmlFreeID:
1807 * @not: A id
1808 *
1809 * Deallocate the memory used by an id definition
1810 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001811static void
Owen Taylor3473f882001-02-23 17:55:21 +00001812xmlFreeID(xmlIDPtr id) {
1813 if (id == NULL) return;
1814 if (id->value != NULL)
1815 xmlFree((xmlChar *) id->value);
Owen Taylor3473f882001-02-23 17:55:21 +00001816 xmlFree(id);
1817}
1818
1819/**
1820 * xmlAddID:
1821 * @ctxt: the validation context
1822 * @doc: pointer to the document
1823 * @value: the value name
1824 * @attr: the attribute holding the ID
1825 *
1826 * Register a new id declaration
1827 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001828 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001829 */
1830xmlIDPtr
1831xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
1832 xmlAttrPtr attr) {
1833 xmlIDPtr ret;
1834 xmlIDTablePtr table;
1835
1836 if (doc == NULL) {
1837 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001838 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001839 return(NULL);
1840 }
1841 if (value == NULL) {
1842 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001843 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001844 return(NULL);
1845 }
1846 if (attr == NULL) {
1847 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001848 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001849 return(NULL);
1850 }
1851
1852 /*
1853 * Create the ID table if needed.
1854 */
1855 table = (xmlIDTablePtr) doc->ids;
1856 if (table == NULL)
1857 doc->ids = table = xmlCreateIDTable();
1858 if (table == NULL) {
1859 xmlGenericError(xmlGenericErrorContext,
1860 "xmlAddID: Table creation failed!\n");
1861 return(NULL);
1862 }
1863
1864 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
1865 if (ret == NULL) {
1866 xmlGenericError(xmlGenericErrorContext,
1867 "xmlAddID: out of memory\n");
1868 return(NULL);
1869 }
1870
1871 /*
1872 * fill the structure.
1873 */
1874 ret->value = xmlStrdup(value);
1875 ret->attr = attr;
1876
1877 if (xmlHashAddEntry(table, value, ret) < 0) {
1878 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001879 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001880 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001881 if (ctxt != NULL)
1882 VERROR(ctxt->userData, "ID %s already defined\n", value);
Owen Taylor3473f882001-02-23 17:55:21 +00001883 xmlFreeID(ret);
1884 return(NULL);
1885 }
1886 return(ret);
1887}
1888
1889/**
1890 * xmlFreeIDTable:
1891 * @table: An id table
1892 *
1893 * Deallocate the memory used by an ID hash table.
1894 */
1895void
1896xmlFreeIDTable(xmlIDTablePtr table) {
1897 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
1898}
1899
1900/**
1901 * xmlIsID:
1902 * @doc: the document
1903 * @elem: the element carrying the attribute
1904 * @attr: the attribute
1905 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001906 * Determine whether an attribute is of type ID. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00001907 * then this is simple, otherwise we use an heuristic: name ID (upper
1908 * or lowercase).
1909 *
1910 * Returns 0 or 1 depending on the lookup result
1911 */
1912int
1913xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
1914 if (doc == NULL) return(0);
1915 if (attr == NULL) return(0);
1916 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
1917 return(0);
1918 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1919 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
1920 (xmlStrEqual(BAD_CAST "name", attr->name)))
1921 return(1);
1922 return(0);
1923 } else {
1924 xmlAttributePtr attrDecl;
1925
1926 if (elem == NULL) return(0);
1927 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
1928 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1929 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
1930 attr->name);
1931
1932 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
1933 return(1);
1934 }
1935 return(0);
1936}
1937
1938/**
1939 * xmlRemoveID
1940 * @doc: the document
1941 * @attr: the attribute
1942 *
1943 * Remove the given attribute from the ID table maintained internally.
1944 *
1945 * Returns -1 if the lookup failed and 0 otherwise
1946 */
1947int
1948xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
1949 xmlAttrPtr cur;
1950 xmlIDTablePtr table;
1951 xmlChar *ID;
1952
1953 if (doc == NULL) return(-1);
1954 if (attr == NULL) return(-1);
1955 table = (xmlIDTablePtr) doc->ids;
1956 if (table == NULL)
1957 return(-1);
1958
1959 if (attr == NULL)
1960 return(-1);
1961 ID = xmlNodeListGetString(doc, attr->children, 1);
1962 if (ID == NULL)
1963 return(-1);
1964 cur = xmlHashLookup(table, ID);
1965 if (cur != attr) {
1966 xmlFree(ID);
1967 return(-1);
1968 }
1969 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
1970 xmlFree(ID);
1971 return(0);
1972}
1973
1974/**
1975 * xmlGetID:
1976 * @doc: pointer to the document
1977 * @ID: the ID value
1978 *
1979 * Search the attribute declaring the given ID
1980 *
1981 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
1982 */
1983xmlAttrPtr
1984xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
1985 xmlIDTablePtr table;
1986 xmlIDPtr id;
1987
1988 if (doc == NULL) {
1989 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
1990 return(NULL);
1991 }
1992
1993 if (ID == NULL) {
1994 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
1995 return(NULL);
1996 }
1997
1998 table = (xmlIDTablePtr) doc->ids;
1999 if (table == NULL)
2000 return(NULL);
2001
2002 id = xmlHashLookup(table, ID);
2003 if (id == NULL)
2004 return(NULL);
2005 return(id->attr);
2006}
2007
2008/************************************************************************
2009 * *
2010 * Refs *
2011 * *
2012 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002013typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002014{
2015 xmlListPtr l;
2016 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002017} xmlRemoveMemo;
2018
2019typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2020
2021typedef struct xmlValidateMemo_t
2022{
2023 xmlValidCtxtPtr ctxt;
2024 const xmlChar *name;
2025} xmlValidateMemo;
2026
2027typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002028
2029/**
2030 * xmlCreateRefTable:
2031 *
2032 * create and initialize an empty ref hash table.
2033 *
2034 * Returns the xmlRefTablePtr just created or NULL in case
2035 * of error.
2036 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002037static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002038xmlCreateRefTable(void) {
2039 return(xmlHashCreate(0));
2040}
2041
2042/**
2043 * xmlFreeRef:
2044 * @lk: A list link
2045 *
2046 * Deallocate the memory used by a ref definition
2047 */
2048static void
2049xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002050 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2051 if (ref == NULL) return;
2052 if (ref->value != NULL)
2053 xmlFree((xmlChar *)ref->value);
2054 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002055}
2056
2057/**
2058 * xmlFreeRefList:
2059 * @list_ref: A list of references.
2060 *
2061 * Deallocate the memory used by a list of references
2062 */
2063static void
2064xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002065 if (list_ref == NULL) return;
2066 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002067}
2068
2069/**
2070 * xmlWalkRemoveRef:
2071 * @data: Contents of current link
2072 * @user: Value supplied by the user
2073 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002074 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002075 */
2076static int
2077xmlWalkRemoveRef(const void *data, const void *user)
2078{
Daniel Veillard37721922001-05-04 15:21:12 +00002079 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2080 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2081 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002082
Daniel Veillard37721922001-05-04 15:21:12 +00002083 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2084 xmlListRemoveFirst(ref_list, (void *)data);
2085 return 0;
2086 }
2087 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002088}
2089
2090/**
2091 * xmlAddRef:
2092 * @ctxt: the validation context
2093 * @doc: pointer to the document
2094 * @value: the value name
2095 * @attr: the attribute holding the Ref
2096 *
2097 * Register a new ref declaration
2098 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002099 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002100 */
2101xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002102xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002103 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002104 xmlRefPtr ret;
2105 xmlRefTablePtr table;
2106 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002107
Daniel Veillard37721922001-05-04 15:21:12 +00002108 if (doc == NULL) {
2109 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002110 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002111 return(NULL);
2112 }
2113 if (value == NULL) {
2114 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002115 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002116 return(NULL);
2117 }
2118 if (attr == NULL) {
2119 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002120 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002121 return(NULL);
2122 }
Owen Taylor3473f882001-02-23 17:55:21 +00002123
Daniel Veillard37721922001-05-04 15:21:12 +00002124 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002125 * Create the Ref table if needed.
2126 */
Daniel Veillard37721922001-05-04 15:21:12 +00002127 table = (xmlRefTablePtr) doc->refs;
2128 if (table == NULL)
2129 doc->refs = table = xmlCreateRefTable();
2130 if (table == NULL) {
2131 xmlGenericError(xmlGenericErrorContext,
2132 "xmlAddRef: Table creation failed!\n");
2133 return(NULL);
2134 }
Owen Taylor3473f882001-02-23 17:55:21 +00002135
Daniel Veillard37721922001-05-04 15:21:12 +00002136 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2137 if (ret == NULL) {
2138 xmlGenericError(xmlGenericErrorContext,
2139 "xmlAddRef: out of memory\n");
2140 return(NULL);
2141 }
Owen Taylor3473f882001-02-23 17:55:21 +00002142
Daniel Veillard37721922001-05-04 15:21:12 +00002143 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002144 * fill the structure.
2145 */
Daniel Veillard37721922001-05-04 15:21:12 +00002146 ret->value = xmlStrdup(value);
2147 ret->attr = attr;
Owen Taylor3473f882001-02-23 17:55:21 +00002148
Daniel Veillard37721922001-05-04 15:21:12 +00002149 /* To add a reference :-
2150 * References are maintained as a list of references,
2151 * Lookup the entry, if no entry create new nodelist
2152 * Add the owning node to the NodeList
2153 * Return the ref
2154 */
Owen Taylor3473f882001-02-23 17:55:21 +00002155
Daniel Veillard37721922001-05-04 15:21:12 +00002156 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2157 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2158 xmlGenericError(xmlGenericErrorContext,
2159 "xmlAddRef: Reference list creation failed!\n");
2160 return(NULL);
2161 }
2162 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2163 xmlListDelete(ref_list);
2164 xmlGenericError(xmlGenericErrorContext,
2165 "xmlAddRef: Reference list insertion failed!\n");
2166 return(NULL);
2167 }
2168 }
2169 xmlListInsert(ref_list, ret);
2170 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002171}
2172
2173/**
2174 * xmlFreeRefTable:
2175 * @table: An ref table
2176 *
2177 * Deallocate the memory used by an Ref hash table.
2178 */
2179void
2180xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002181 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002182}
2183
2184/**
2185 * xmlIsRef:
2186 * @doc: the document
2187 * @elem: the element carrying the attribute
2188 * @attr: the attribute
2189 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002190 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002191 * then this is simple, otherwise we use an heuristic: name Ref (upper
2192 * or lowercase).
2193 *
2194 * Returns 0 or 1 depending on the lookup result
2195 */
2196int
2197xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002198 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2199 return(0);
2200 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2201 /* TODO @@@ */
2202 return(0);
2203 } else {
2204 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002205
Daniel Veillard37721922001-05-04 15:21:12 +00002206 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2207 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2208 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2209 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002210
Daniel Veillard37721922001-05-04 15:21:12 +00002211 if ((attrDecl != NULL) &&
2212 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2213 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2214 return(1);
2215 }
2216 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002217}
2218
2219/**
2220 * xmlRemoveRef
2221 * @doc: the document
2222 * @attr: the attribute
2223 *
2224 * Remove the given attribute from the Ref table maintained internally.
2225 *
2226 * Returns -1 if the lookup failed and 0 otherwise
2227 */
2228int
2229xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002230 xmlListPtr ref_list;
2231 xmlRefTablePtr table;
2232 xmlChar *ID;
2233 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002234
Daniel Veillard37721922001-05-04 15:21:12 +00002235 if (doc == NULL) return(-1);
2236 if (attr == NULL) return(-1);
2237 table = (xmlRefTablePtr) doc->refs;
2238 if (table == NULL)
2239 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002240
Daniel Veillard37721922001-05-04 15:21:12 +00002241 if (attr == NULL)
2242 return(-1);
2243 ID = xmlNodeListGetString(doc, attr->children, 1);
2244 if (ID == NULL)
2245 return(-1);
2246 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002247
Daniel Veillard37721922001-05-04 15:21:12 +00002248 if(ref_list == NULL) {
2249 xmlFree(ID);
2250 return (-1);
2251 }
2252 /* At this point, ref_list refers to a list of references which
2253 * have the same key as the supplied attr. Our list of references
2254 * is ordered by reference address and we don't have that information
2255 * here to use when removing. We'll have to walk the list and
2256 * check for a matching attribute, when we find one stop the walk
2257 * and remove the entry.
2258 * The list is ordered by reference, so that means we don't have the
2259 * key. Passing the list and the reference to the walker means we
2260 * will have enough data to be able to remove the entry.
2261 */
2262 target.l = ref_list;
2263 target.ap = attr;
2264
2265 /* Remove the supplied attr from our list */
2266 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002267
Daniel Veillard37721922001-05-04 15:21:12 +00002268 /*If the list is empty then remove the list entry in the hash */
2269 if (xmlListEmpty(ref_list))
2270 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2271 xmlFreeRefList);
2272 xmlFree(ID);
2273 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002274}
2275
2276/**
2277 * xmlGetRefs:
2278 * @doc: pointer to the document
2279 * @ID: the ID value
2280 *
2281 * Find the set of references for the supplied ID.
2282 *
2283 * Returns NULL if not found, otherwise node set for the ID.
2284 */
2285xmlListPtr
2286xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002287 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002288
Daniel Veillard37721922001-05-04 15:21:12 +00002289 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002290 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002291 return(NULL);
2292 }
Owen Taylor3473f882001-02-23 17:55:21 +00002293
Daniel Veillard37721922001-05-04 15:21:12 +00002294 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002295 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002296 return(NULL);
2297 }
Owen Taylor3473f882001-02-23 17:55:21 +00002298
Daniel Veillard37721922001-05-04 15:21:12 +00002299 table = (xmlRefTablePtr) doc->refs;
2300 if (table == NULL)
2301 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002302
Daniel Veillard37721922001-05-04 15:21:12 +00002303 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002304}
2305
2306/************************************************************************
2307 * *
2308 * Routines for validity checking *
2309 * *
2310 ************************************************************************/
2311
2312/**
2313 * xmlGetDtdElementDesc:
2314 * @dtd: a pointer to the DtD to search
2315 * @name: the element name
2316 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002317 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002318 *
2319 * returns the xmlElementPtr if found or NULL
2320 */
2321
2322xmlElementPtr
2323xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2324 xmlElementTablePtr table;
2325 xmlElementPtr cur;
2326 xmlChar *uqname = NULL, *prefix = NULL;
2327
2328 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002329 if (dtd->elements == NULL)
2330 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002331 table = (xmlElementTablePtr) dtd->elements;
2332
2333 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002334 if (uqname != NULL)
2335 name = uqname;
2336 cur = xmlHashLookup2(table, name, prefix);
2337 if (prefix != NULL) xmlFree(prefix);
2338 if (uqname != NULL) xmlFree(uqname);
2339 return(cur);
2340}
2341/**
2342 * xmlGetDtdElementDesc2:
2343 * @dtd: a pointer to the DtD to search
2344 * @name: the element name
2345 * @create: create an empty description if not found
2346 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002347 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002348 *
2349 * returns the xmlElementPtr if found or NULL
2350 */
2351
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002352static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002353xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2354 xmlElementTablePtr table;
2355 xmlElementPtr cur;
2356 xmlChar *uqname = NULL, *prefix = NULL;
2357
2358 if (dtd == NULL) return(NULL);
2359 if (dtd->elements == NULL) {
2360 if (!create)
2361 return(NULL);
2362 /*
2363 * Create the Element table if needed.
2364 */
2365 table = (xmlElementTablePtr) dtd->elements;
2366 if (table == NULL) {
2367 table = xmlCreateElementTable();
2368 dtd->elements = (void *) table;
2369 }
2370 if (table == NULL) {
2371 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002372 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002373 return(NULL);
2374 }
2375 }
2376 table = (xmlElementTablePtr) dtd->elements;
2377
2378 uqname = xmlSplitQName2(name, &prefix);
2379 if (uqname != NULL)
2380 name = uqname;
2381 cur = xmlHashLookup2(table, name, prefix);
2382 if ((cur == NULL) && (create)) {
2383 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2384 if (cur == NULL) {
2385 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002386 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002387 return(NULL);
2388 }
2389 memset(cur, 0, sizeof(xmlElement));
2390 cur->type = XML_ELEMENT_DECL;
2391
2392 /*
2393 * fill the structure.
2394 */
2395 cur->name = xmlStrdup(name);
2396 cur->prefix = xmlStrdup(prefix);
2397 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2398
2399 xmlHashAddEntry2(table, name, prefix, cur);
2400 }
2401 if (prefix != NULL) xmlFree(prefix);
2402 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002403 return(cur);
2404}
2405
2406/**
2407 * xmlGetDtdQElementDesc:
2408 * @dtd: a pointer to the DtD to search
2409 * @name: the element name
2410 * @prefix: the element namespace prefix
2411 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002412 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002413 *
2414 * returns the xmlElementPtr if found or NULL
2415 */
2416
Daniel Veillard48da9102001-08-07 01:10:10 +00002417xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002418xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2419 const xmlChar *prefix) {
2420 xmlElementTablePtr table;
2421
2422 if (dtd == NULL) return(NULL);
2423 if (dtd->elements == NULL) return(NULL);
2424 table = (xmlElementTablePtr) dtd->elements;
2425
2426 return(xmlHashLookup2(table, name, prefix));
2427}
2428
2429/**
2430 * xmlGetDtdAttrDesc:
2431 * @dtd: a pointer to the DtD to search
2432 * @elem: the element name
2433 * @name: the attribute name
2434 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002435 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002436 * this element.
2437 *
2438 * returns the xmlAttributePtr if found or NULL
2439 */
2440
2441xmlAttributePtr
2442xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2443 xmlAttributeTablePtr table;
2444 xmlAttributePtr cur;
2445 xmlChar *uqname = NULL, *prefix = NULL;
2446
2447 if (dtd == NULL) return(NULL);
2448 if (dtd->attributes == NULL) return(NULL);
2449
2450 table = (xmlAttributeTablePtr) dtd->attributes;
2451 if (table == NULL)
2452 return(NULL);
2453
2454 uqname = xmlSplitQName2(name, &prefix);
2455
2456 if (uqname != NULL) {
2457 cur = xmlHashLookup3(table, uqname, prefix, elem);
2458 if (prefix != NULL) xmlFree(prefix);
2459 if (uqname != NULL) xmlFree(uqname);
2460 } else
2461 cur = xmlHashLookup3(table, name, NULL, elem);
2462 return(cur);
2463}
2464
2465/**
2466 * xmlGetDtdQAttrDesc:
2467 * @dtd: a pointer to the DtD to search
2468 * @elem: the element name
2469 * @name: the attribute name
2470 * @prefix: the attribute namespace prefix
2471 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002472 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002473 * this element.
2474 *
2475 * returns the xmlAttributePtr if found or NULL
2476 */
2477
Daniel Veillard48da9102001-08-07 01:10:10 +00002478xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002479xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2480 const xmlChar *prefix) {
2481 xmlAttributeTablePtr table;
2482
2483 if (dtd == NULL) return(NULL);
2484 if (dtd->attributes == NULL) return(NULL);
2485 table = (xmlAttributeTablePtr) dtd->attributes;
2486
2487 return(xmlHashLookup3(table, name, prefix, elem));
2488}
2489
2490/**
2491 * xmlGetDtdNotationDesc:
2492 * @dtd: a pointer to the DtD to search
2493 * @name: the notation name
2494 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002495 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002496 *
2497 * returns the xmlNotationPtr if found or NULL
2498 */
2499
2500xmlNotationPtr
2501xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2502 xmlNotationTablePtr table;
2503
2504 if (dtd == NULL) return(NULL);
2505 if (dtd->notations == NULL) return(NULL);
2506 table = (xmlNotationTablePtr) dtd->notations;
2507
2508 return(xmlHashLookup(table, name));
2509}
2510
2511/**
2512 * xmlValidateNotationUse:
2513 * @ctxt: the validation context
2514 * @doc: the document
2515 * @notationName: the notation name to check
2516 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002517 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002518 * - [ VC: Notation Declared ]
2519 *
2520 * returns 1 if valid or 0 otherwise
2521 */
2522
2523int
2524xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2525 const xmlChar *notationName) {
2526 xmlNotationPtr notaDecl;
2527 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2528
2529 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2530 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2531 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2532
2533 if (notaDecl == NULL) {
2534 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2535 notationName);
2536 return(0);
2537 }
2538 return(1);
2539}
2540
2541/**
2542 * xmlIsMixedElement
2543 * @doc: the document
2544 * @name: the element name
2545 *
2546 * Search in the DtDs whether an element accept Mixed content (or ANY)
2547 * basically if it is supposed to accept text childs
2548 *
2549 * returns 0 if no, 1 if yes, and -1 if no element description is available
2550 */
2551
2552int
2553xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2554 xmlElementPtr elemDecl;
2555
2556 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2557
2558 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2559 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2560 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2561 if (elemDecl == NULL) return(-1);
2562 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002563 case XML_ELEMENT_TYPE_UNDEFINED:
2564 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002565 case XML_ELEMENT_TYPE_ELEMENT:
2566 return(0);
2567 case XML_ELEMENT_TYPE_EMPTY:
2568 /*
2569 * return 1 for EMPTY since we want VC error to pop up
2570 * on <empty> </empty> for example
2571 */
2572 case XML_ELEMENT_TYPE_ANY:
2573 case XML_ELEMENT_TYPE_MIXED:
2574 return(1);
2575 }
2576 return(1);
2577}
2578
2579/**
2580 * xmlValidateNameValue:
2581 * @value: an Name value
2582 *
2583 * Validate that the given value match Name production
2584 *
2585 * returns 1 if valid or 0 otherwise
2586 */
2587
Daniel Veillard9b731d72002-04-14 12:56:08 +00002588int
Owen Taylor3473f882001-02-23 17:55:21 +00002589xmlValidateNameValue(const xmlChar *value) {
2590 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002591 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002592
2593 if (value == NULL) return(0);
2594 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002595 val = xmlStringCurrentChar(NULL, cur, &len);
2596 cur += len;
2597 if (!IS_LETTER(val) && (val != '_') &&
2598 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002599 return(0);
2600 }
2601
Daniel Veillardd8224e02002-01-13 15:43:22 +00002602 val = xmlStringCurrentChar(NULL, cur, &len);
2603 cur += len;
2604 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2605 (val == '.') || (val == '-') ||
2606 (val == '_') || (val == ':') ||
2607 (IS_COMBINING(val)) ||
2608 (IS_EXTENDER(val))) {
2609 val = xmlStringCurrentChar(NULL, cur, &len);
2610 cur += len;
2611 }
Owen Taylor3473f882001-02-23 17:55:21 +00002612
Daniel Veillardd8224e02002-01-13 15:43:22 +00002613 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002614
2615 return(1);
2616}
2617
2618/**
2619 * xmlValidateNamesValue:
2620 * @value: an Names value
2621 *
2622 * Validate that the given value match Names production
2623 *
2624 * returns 1 if valid or 0 otherwise
2625 */
2626
Daniel Veillard9b731d72002-04-14 12:56:08 +00002627int
Owen Taylor3473f882001-02-23 17:55:21 +00002628xmlValidateNamesValue(const xmlChar *value) {
2629 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002630 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002631
2632 if (value == NULL) return(0);
2633 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002634 val = xmlStringCurrentChar(NULL, cur, &len);
2635 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002636
Daniel Veillardd8224e02002-01-13 15:43:22 +00002637 if (!IS_LETTER(val) && (val != '_') &&
2638 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002639 return(0);
2640 }
2641
Daniel Veillardd8224e02002-01-13 15:43:22 +00002642 val = xmlStringCurrentChar(NULL, cur, &len);
2643 cur += len;
2644 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2645 (val == '.') || (val == '-') ||
2646 (val == '_') || (val == ':') ||
2647 (IS_COMBINING(val)) ||
2648 (IS_EXTENDER(val))) {
2649 val = xmlStringCurrentChar(NULL, cur, &len);
2650 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002651 }
2652
Daniel Veillardd8224e02002-01-13 15:43:22 +00002653 while (IS_BLANK(val)) {
2654 while (IS_BLANK(val)) {
2655 val = xmlStringCurrentChar(NULL, cur, &len);
2656 cur += len;
2657 }
2658
2659 if (!IS_LETTER(val) && (val != '_') &&
2660 (val != ':')) {
2661 return(0);
2662 }
2663 val = xmlStringCurrentChar(NULL, cur, &len);
2664 cur += len;
2665
2666 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2667 (val == '.') || (val == '-') ||
2668 (val == '_') || (val == ':') ||
2669 (IS_COMBINING(val)) ||
2670 (IS_EXTENDER(val))) {
2671 val = xmlStringCurrentChar(NULL, cur, &len);
2672 cur += len;
2673 }
2674 }
2675
2676 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002677
2678 return(1);
2679}
2680
2681/**
2682 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002683 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00002684 *
2685 * Validate that the given value match Nmtoken production
2686 *
2687 * [ VC: Name Token ]
2688 *
2689 * returns 1 if valid or 0 otherwise
2690 */
2691
Daniel Veillard9b731d72002-04-14 12:56:08 +00002692int
Owen Taylor3473f882001-02-23 17:55:21 +00002693xmlValidateNmtokenValue(const xmlChar *value) {
2694 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002695 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002696
2697 if (value == NULL) return(0);
2698 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002699 val = xmlStringCurrentChar(NULL, cur, &len);
2700 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002701
Daniel Veillardd8224e02002-01-13 15:43:22 +00002702 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2703 (val != '.') && (val != '-') &&
2704 (val != '_') && (val != ':') &&
2705 (!IS_COMBINING(val)) &&
2706 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00002707 return(0);
2708
Daniel Veillardd8224e02002-01-13 15:43:22 +00002709 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2710 (val == '.') || (val == '-') ||
2711 (val == '_') || (val == ':') ||
2712 (IS_COMBINING(val)) ||
2713 (IS_EXTENDER(val))) {
2714 val = xmlStringCurrentChar(NULL, cur, &len);
2715 cur += len;
2716 }
Owen Taylor3473f882001-02-23 17:55:21 +00002717
Daniel Veillardd8224e02002-01-13 15:43:22 +00002718 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002719
2720 return(1);
2721}
2722
2723/**
2724 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002725 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00002726 *
2727 * Validate that the given value match Nmtokens production
2728 *
2729 * [ VC: Name Token ]
2730 *
2731 * returns 1 if valid or 0 otherwise
2732 */
2733
Daniel Veillard9b731d72002-04-14 12:56:08 +00002734int
Owen Taylor3473f882001-02-23 17:55:21 +00002735xmlValidateNmtokensValue(const xmlChar *value) {
2736 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002737 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002738
2739 if (value == NULL) return(0);
2740 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002741 val = xmlStringCurrentChar(NULL, cur, &len);
2742 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002743
Daniel Veillardd8224e02002-01-13 15:43:22 +00002744 while (IS_BLANK(val)) {
2745 val = xmlStringCurrentChar(NULL, cur, &len);
2746 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002747 }
2748
Daniel Veillardd8224e02002-01-13 15:43:22 +00002749 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2750 (val != '.') && (val != '-') &&
2751 (val != '_') && (val != ':') &&
2752 (!IS_COMBINING(val)) &&
2753 (!IS_EXTENDER(val)))
2754 return(0);
2755
2756 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2757 (val == '.') || (val == '-') ||
2758 (val == '_') || (val == ':') ||
2759 (IS_COMBINING(val)) ||
2760 (IS_EXTENDER(val))) {
2761 val = xmlStringCurrentChar(NULL, cur, &len);
2762 cur += len;
2763 }
2764
2765 while (IS_BLANK(val)) {
2766 while (IS_BLANK(val)) {
2767 val = xmlStringCurrentChar(NULL, cur, &len);
2768 cur += len;
2769 }
2770 if (val == 0) return(1);
2771
2772 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2773 (val != '.') && (val != '-') &&
2774 (val != '_') && (val != ':') &&
2775 (!IS_COMBINING(val)) &&
2776 (!IS_EXTENDER(val)))
2777 return(0);
2778
2779 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2780 (val == '.') || (val == '-') ||
2781 (val == '_') || (val == ':') ||
2782 (IS_COMBINING(val)) ||
2783 (IS_EXTENDER(val))) {
2784 val = xmlStringCurrentChar(NULL, cur, &len);
2785 cur += len;
2786 }
2787 }
2788
2789 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002790
2791 return(1);
2792}
2793
2794/**
2795 * xmlValidateNotationDecl:
2796 * @ctxt: the validation context
2797 * @doc: a document instance
2798 * @nota: a notation definition
2799 *
2800 * Try to validate a single notation definition
2801 * basically it does the following checks as described by the
2802 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002803 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00002804 * But this function get called anyway ...
2805 *
2806 * returns 1 if valid or 0 otherwise
2807 */
2808
2809int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002810xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
2811 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002812 int ret = 1;
2813
2814 return(ret);
2815}
2816
2817/**
2818 * xmlValidateAttributeValue:
2819 * @type: an attribute type
2820 * @value: an attribute value
2821 *
2822 * Validate that the given attribute value match the proper production
2823 *
2824 * [ VC: ID ]
2825 * Values of type ID must match the Name production....
2826 *
2827 * [ VC: IDREF ]
2828 * Values of type IDREF must match the Name production, and values
2829 * of type IDREFS must match Names ...
2830 *
2831 * [ VC: Entity Name ]
2832 * Values of type ENTITY must match the Name production, values
2833 * of type ENTITIES must match Names ...
2834 *
2835 * [ VC: Name Token ]
2836 * Values of type NMTOKEN must match the Nmtoken production; values
2837 * of type NMTOKENS must match Nmtokens.
2838 *
2839 * returns 1 if valid or 0 otherwise
2840 */
2841
2842int
2843xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
2844 switch (type) {
2845 case XML_ATTRIBUTE_ENTITIES:
2846 case XML_ATTRIBUTE_IDREFS:
2847 return(xmlValidateNamesValue(value));
2848 case XML_ATTRIBUTE_ENTITY:
2849 case XML_ATTRIBUTE_IDREF:
2850 case XML_ATTRIBUTE_ID:
2851 case XML_ATTRIBUTE_NOTATION:
2852 return(xmlValidateNameValue(value));
2853 case XML_ATTRIBUTE_NMTOKENS:
2854 case XML_ATTRIBUTE_ENUMERATION:
2855 return(xmlValidateNmtokensValue(value));
2856 case XML_ATTRIBUTE_NMTOKEN:
2857 return(xmlValidateNmtokenValue(value));
2858 case XML_ATTRIBUTE_CDATA:
2859 break;
2860 }
2861 return(1);
2862}
2863
2864/**
2865 * xmlValidateAttributeValue2:
2866 * @ctxt: the validation context
2867 * @doc: the document
2868 * @name: the attribute name (used for error reporting only)
2869 * @type: the attribute type
2870 * @value: the attribute value
2871 *
2872 * Validate that the given attribute value match a given type.
2873 * This typically cannot be done before having finished parsing
2874 * the subsets.
2875 *
2876 * [ VC: IDREF ]
2877 * Values of type IDREF must match one of the declared IDs
2878 * Values of type IDREFS must match a sequence of the declared IDs
2879 * each Name must match the value of an ID attribute on some element
2880 * in the XML document; i.e. IDREF values must match the value of
2881 * some ID attribute
2882 *
2883 * [ VC: Entity Name ]
2884 * Values of type ENTITY must match one declared entity
2885 * Values of type ENTITIES must match a sequence of declared entities
2886 *
2887 * [ VC: Notation Attributes ]
2888 * all notation names in the declaration must be declared.
2889 *
2890 * returns 1 if valid or 0 otherwise
2891 */
2892
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002893static int
Owen Taylor3473f882001-02-23 17:55:21 +00002894xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2895 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
2896 int ret = 1;
2897 switch (type) {
2898 case XML_ATTRIBUTE_IDREFS:
2899 case XML_ATTRIBUTE_IDREF:
2900 case XML_ATTRIBUTE_ID:
2901 case XML_ATTRIBUTE_NMTOKENS:
2902 case XML_ATTRIBUTE_ENUMERATION:
2903 case XML_ATTRIBUTE_NMTOKEN:
2904 case XML_ATTRIBUTE_CDATA:
2905 break;
2906 case XML_ATTRIBUTE_ENTITY: {
2907 xmlEntityPtr ent;
2908
2909 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00002910 if ((ent == NULL) && (doc->standalone == 1)) {
2911 doc->standalone = 0;
2912 ent = xmlGetDocEntity(doc, value);
2913 if (ent != NULL) {
2914 VERROR(ctxt->userData,
2915"standalone problem: attribute %s reference entity \"%s\" in external subset\n",
2916 name, value);
2917 /* WAIT to get answer from the Core WG on this
2918 ret = 0;
2919 */
2920 }
2921 }
Owen Taylor3473f882001-02-23 17:55:21 +00002922 if (ent == NULL) {
2923 VERROR(ctxt->userData,
2924 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2925 name, value);
2926 ret = 0;
2927 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2928 VERROR(ctxt->userData,
2929 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2930 name, value);
2931 ret = 0;
2932 }
2933 break;
2934 }
2935 case XML_ATTRIBUTE_ENTITIES: {
2936 xmlChar *dup, *nam = NULL, *cur, save;
2937 xmlEntityPtr ent;
2938
2939 dup = xmlStrdup(value);
2940 if (dup == NULL)
2941 return(0);
2942 cur = dup;
2943 while (*cur != 0) {
2944 nam = cur;
2945 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2946 save = *cur;
2947 *cur = 0;
2948 ent = xmlGetDocEntity(doc, nam);
2949 if (ent == NULL) {
2950 VERROR(ctxt->userData,
2951 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
2952 name, nam);
2953 ret = 0;
2954 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2955 VERROR(ctxt->userData,
2956 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
2957 name, nam);
2958 ret = 0;
2959 }
2960 if (save == 0)
2961 break;
2962 *cur = save;
2963 while (IS_BLANK(*cur)) cur++;
2964 }
2965 xmlFree(dup);
2966 break;
2967 }
2968 case XML_ATTRIBUTE_NOTATION: {
2969 xmlNotationPtr nota;
2970
2971 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
2972 if ((nota == NULL) && (doc->extSubset != NULL))
2973 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
2974
2975 if (nota == NULL) {
2976 VERROR(ctxt->userData,
2977 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
2978 name, value);
2979 ret = 0;
2980 }
2981 break;
2982 }
2983 }
2984 return(ret);
2985}
2986
2987/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00002988 * xmlValidCtxtNormalizeAttributeValue:
2989 * @ctxt: the validation context
2990 * @doc: the document
2991 * @elem: the parent
2992 * @name: the attribute name
2993 * @value: the attribute value
2994 * @ctxt: the validation context or NULL
2995 *
2996 * Does the validation related extra step of the normalization of attribute
2997 * values:
2998 *
2999 * If the declared value is not CDATA, then the XML processor must further
3000 * process the normalized attribute value by discarding any leading and
3001 * trailing space (#x20) characters, and by replacing sequences of space
3002 * (#x20) characters by single space (#x20) character.
3003 *
3004 * Also check VC: Standalone Document Declaration in P32, and update
3005 * ctxt->valid accordingly
3006 *
3007 * returns a new normalized string if normalization is needed, NULL otherwise
3008 * the caller must free the returned value.
3009 */
3010
3011xmlChar *
3012xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3013 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3014 xmlChar *ret, *dst;
3015 const xmlChar *src;
3016 xmlAttributePtr attrDecl = NULL;
3017 int extsubset = 0;
3018
3019 if (doc == NULL) return(NULL);
3020 if (elem == NULL) return(NULL);
3021 if (name == NULL) return(NULL);
3022 if (value == NULL) return(NULL);
3023
3024 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3025 xmlChar qname[500];
3026 snprintf((char *) qname, sizeof(qname), "%s:%s",
3027 elem->ns->prefix, elem->name);
3028 qname[sizeof(qname) - 1] = 0;
3029 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3030 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3031 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3032 if (attrDecl != NULL)
3033 extsubset = 1;
3034 }
3035 }
3036 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3037 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3038 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3039 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3040 if (attrDecl != NULL)
3041 extsubset = 1;
3042 }
3043
3044 if (attrDecl == NULL)
3045 return(NULL);
3046 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3047 return(NULL);
3048
3049 ret = xmlStrdup(value);
3050 if (ret == NULL)
3051 return(NULL);
3052 src = value;
3053 dst = ret;
3054 while (*src == 0x20) src++;
3055 while (*src != 0) {
3056 if (*src == 0x20) {
3057 while (*src == 0x20) src++;
3058 if (*src != 0)
3059 *dst++ = 0x20;
3060 } else {
3061 *dst++ = *src++;
3062 }
3063 }
3064 *dst = 0;
3065 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3066 VERROR(ctxt->userData,
3067"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3068 name, elem->name);
3069 ctxt->valid = 0;
3070 }
3071 return(ret);
3072}
3073
3074/**
Owen Taylor3473f882001-02-23 17:55:21 +00003075 * xmlValidNormalizeAttributeValue:
3076 * @doc: the document
3077 * @elem: the parent
3078 * @name: the attribute name
3079 * @value: the attribute value
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003080 * @ctxt: the validation context or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00003081 *
3082 * Does the validation related extra step of the normalization of attribute
3083 * values:
3084 *
3085 * If the declared value is not CDATA, then the XML processor must further
3086 * process the normalized attribute value by discarding any leading and
3087 * trailing space (#x20) characters, and by replacing sequences of space
3088 * (#x20) characters by single space (#x20) character.
3089 *
3090 * returns a new normalized string if normalization is needed, NULL otherwise
3091 * the caller must free the returned value.
3092 */
3093
3094xmlChar *
3095xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3096 const xmlChar *name, const xmlChar *value) {
3097 xmlChar *ret, *dst;
3098 const xmlChar *src;
3099 xmlAttributePtr attrDecl = NULL;
3100
3101 if (doc == NULL) return(NULL);
3102 if (elem == NULL) return(NULL);
3103 if (name == NULL) return(NULL);
3104 if (value == NULL) return(NULL);
3105
3106 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3107 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003108 snprintf((char *) qname, sizeof(qname), "%s:%s",
3109 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003110 qname[sizeof(qname) - 1] = 0;
3111 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3112 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3113 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3114 }
3115 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3116 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3117 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3118
3119 if (attrDecl == NULL)
3120 return(NULL);
3121 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3122 return(NULL);
3123
3124 ret = xmlStrdup(value);
3125 if (ret == NULL)
3126 return(NULL);
3127 src = value;
3128 dst = ret;
3129 while (*src == 0x20) src++;
3130 while (*src != 0) {
3131 if (*src == 0x20) {
3132 while (*src == 0x20) src++;
3133 if (*src != 0)
3134 *dst++ = 0x20;
3135 } else {
3136 *dst++ = *src++;
3137 }
3138 }
3139 *dst = 0;
3140 return(ret);
3141}
3142
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003143static void
Owen Taylor3473f882001-02-23 17:55:21 +00003144xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003145 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003146 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3147}
3148
3149/**
3150 * xmlValidateAttributeDecl:
3151 * @ctxt: the validation context
3152 * @doc: a document instance
3153 * @attr: an attribute definition
3154 *
3155 * Try to validate a single attribute definition
3156 * basically it does the following checks as described by the
3157 * XML-1.0 recommendation:
3158 * - [ VC: Attribute Default Legal ]
3159 * - [ VC: Enumeration ]
3160 * - [ VC: ID Attribute Default ]
3161 *
3162 * The ID/IDREF uniqueness and matching are done separately
3163 *
3164 * returns 1 if valid or 0 otherwise
3165 */
3166
3167int
3168xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3169 xmlAttributePtr attr) {
3170 int ret = 1;
3171 int val;
3172 CHECK_DTD;
3173 if(attr == NULL) return(1);
3174
3175 /* Attribute Default Legal */
3176 /* Enumeration */
3177 if (attr->defaultValue != NULL) {
3178 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3179 if (val == 0) {
3180 VERROR(ctxt->userData,
3181 "Syntax of default value for attribute %s on %s is not valid\n",
3182 attr->name, attr->elem);
3183 }
3184 ret &= val;
3185 }
3186
3187 /* ID Attribute Default */
3188 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3189 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3190 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3191 VERROR(ctxt->userData,
3192 "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n",
3193 attr->name, attr->elem);
3194 ret = 0;
3195 }
3196
3197 /* One ID per Element Type */
3198 if (attr->atype == XML_ATTRIBUTE_ID) {
3199 int nbId;
3200
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003201 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003202 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3203 attr->elem);
3204 if (elem != NULL) {
3205 nbId = xmlScanIDAttributeDecl(NULL, elem);
3206 } else {
3207 xmlAttributeTablePtr table;
3208
3209 /*
3210 * The attribute may be declared in the internal subset and the
3211 * element in the external subset.
3212 */
3213 nbId = 0;
3214 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3215 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3216 xmlValidateAttributeIdCallback, &nbId);
3217 }
3218 if (nbId > 1) {
3219 VERROR(ctxt->userData,
3220 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3221 attr->elem, nbId, attr->name);
3222 } else if (doc->extSubset != NULL) {
3223 int extId = 0;
3224 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3225 if (elem != NULL) {
3226 extId = xmlScanIDAttributeDecl(NULL, elem);
3227 }
3228 if (extId > 1) {
3229 VERROR(ctxt->userData,
3230 "Element %s has %d ID attribute defined in the external subset : %s\n",
3231 attr->elem, extId, attr->name);
3232 } else if (extId + nbId > 1) {
3233 VERROR(ctxt->userData,
3234"Element %s has ID attributes defined in the internal and external subset : %s\n",
3235 attr->elem, attr->name);
3236 }
3237 }
3238 }
3239
3240 /* Validity Constraint: Enumeration */
3241 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3242 xmlEnumerationPtr tree = attr->tree;
3243 while (tree != NULL) {
3244 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3245 tree = tree->next;
3246 }
3247 if (tree == NULL) {
3248 VERROR(ctxt->userData,
3249"Default value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3250 attr->defaultValue, attr->name, attr->elem);
3251 ret = 0;
3252 }
3253 }
3254
3255 return(ret);
3256}
3257
3258/**
3259 * xmlValidateElementDecl:
3260 * @ctxt: the validation context
3261 * @doc: a document instance
3262 * @elem: an element definition
3263 *
3264 * Try to validate a single element definition
3265 * basically it does the following checks as described by the
3266 * XML-1.0 recommendation:
3267 * - [ VC: One ID per Element Type ]
3268 * - [ VC: No Duplicate Types ]
3269 * - [ VC: Unique Element Type Declaration ]
3270 *
3271 * returns 1 if valid or 0 otherwise
3272 */
3273
3274int
3275xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3276 xmlElementPtr elem) {
3277 int ret = 1;
3278 xmlElementPtr tst;
3279
3280 CHECK_DTD;
3281
3282 if (elem == NULL) return(1);
3283
3284 /* No Duplicate Types */
3285 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3286 xmlElementContentPtr cur, next;
3287 const xmlChar *name;
3288
3289 cur = elem->content;
3290 while (cur != NULL) {
3291 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3292 if (cur->c1 == NULL) break;
3293 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3294 name = cur->c1->name;
3295 next = cur->c2;
3296 while (next != NULL) {
3297 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3298 if (xmlStrEqual(next->name, name)) {
3299 VERROR(ctxt->userData,
3300 "Definition of %s has duplicate references of %s\n",
3301 elem->name, name);
3302 ret = 0;
3303 }
3304 break;
3305 }
3306 if (next->c1 == NULL) break;
3307 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3308 if (xmlStrEqual(next->c1->name, name)) {
3309 VERROR(ctxt->userData,
3310 "Definition of %s has duplicate references of %s\n",
3311 elem->name, name);
3312 ret = 0;
3313 }
3314 next = next->c2;
3315 }
3316 }
3317 cur = cur->c2;
3318 }
3319 }
3320
3321 /* VC: Unique Element Type Declaration */
3322 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003323 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003324 ((tst->prefix == elem->prefix) ||
3325 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003326 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003327 VERROR(ctxt->userData, "Redefinition of element %s\n",
3328 elem->name);
3329 ret = 0;
3330 }
3331 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003332 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003333 ((tst->prefix == elem->prefix) ||
3334 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003335 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003336 VERROR(ctxt->userData, "Redefinition of element %s\n",
3337 elem->name);
3338 ret = 0;
3339 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003340 /* One ID per Element Type
3341 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003342 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3343 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003344 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003345 return(ret);
3346}
3347
3348/**
3349 * xmlValidateOneAttribute:
3350 * @ctxt: the validation context
3351 * @doc: a document instance
3352 * @elem: an element instance
3353 * @attr: an attribute instance
3354 * @value: the attribute value (without entities processing)
3355 *
3356 * Try to validate a single attribute for an element
3357 * basically it does the following checks as described by the
3358 * XML-1.0 recommendation:
3359 * - [ VC: Attribute Value Type ]
3360 * - [ VC: Fixed Attribute Default ]
3361 * - [ VC: Entity Name ]
3362 * - [ VC: Name Token ]
3363 * - [ VC: ID ]
3364 * - [ VC: IDREF ]
3365 * - [ VC: Entity Name ]
3366 * - [ VC: Notation Attributes ]
3367 *
3368 * The ID/IDREF uniqueness and matching are done separately
3369 *
3370 * returns 1 if valid or 0 otherwise
3371 */
3372
3373int
3374xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3375 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3376 /* xmlElementPtr elemDecl; */
3377 xmlAttributePtr attrDecl = NULL;
3378 int val;
3379 int ret = 1;
3380
3381 CHECK_DTD;
3382 if ((elem == NULL) || (elem->name == NULL)) return(0);
3383 if ((attr == NULL) || (attr->name == NULL)) return(0);
3384
3385 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3386 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003387 snprintf((char *) qname, sizeof(qname), "%s:%s",
3388 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003389 qname[sizeof(qname) - 1] = 0;
3390 if (attr->ns != NULL) {
3391 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3392 attr->name, attr->ns->prefix);
3393 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3394 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3395 attr->name, attr->ns->prefix);
3396 } else {
3397 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3398 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3399 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3400 qname, attr->name);
3401 }
3402 }
3403 if (attrDecl == NULL) {
3404 if (attr->ns != NULL) {
3405 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3406 attr->name, attr->ns->prefix);
3407 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3408 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3409 attr->name, attr->ns->prefix);
3410 } else {
3411 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3412 elem->name, attr->name);
3413 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3414 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3415 elem->name, attr->name);
3416 }
3417 }
3418
3419
3420 /* Validity Constraint: Attribute Value Type */
3421 if (attrDecl == NULL) {
3422 VERROR(ctxt->userData,
3423 "No declaration for attribute %s on element %s\n",
3424 attr->name, elem->name);
3425 return(0);
3426 }
3427 attr->atype = attrDecl->atype;
3428
3429 val = xmlValidateAttributeValue(attrDecl->atype, value);
3430 if (val == 0) {
3431 VERROR(ctxt->userData,
3432 "Syntax of value for attribute %s on %s is not valid\n",
3433 attr->name, elem->name);
3434 ret = 0;
3435 }
3436
3437 /* Validity constraint: Fixed Attribute Default */
3438 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3439 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3440 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003441 "Value for attribute %s on %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003442 attr->name, elem->name, attrDecl->defaultValue);
3443 ret = 0;
3444 }
3445 }
3446
3447 /* Validity Constraint: ID uniqueness */
3448 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3449 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3450 ret = 0;
3451 }
3452
3453 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3454 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3455 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3456 ret = 0;
3457 }
3458
3459 /* Validity Constraint: Notation Attributes */
3460 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3461 xmlEnumerationPtr tree = attrDecl->tree;
3462 xmlNotationPtr nota;
3463
3464 /* First check that the given NOTATION was declared */
3465 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3466 if (nota == NULL)
3467 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3468
3469 if (nota == NULL) {
3470 VERROR(ctxt->userData,
3471 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3472 value, attr->name, elem->name);
3473 ret = 0;
3474 }
3475
3476 /* Second, verify that it's among the list */
3477 while (tree != NULL) {
3478 if (xmlStrEqual(tree->name, value)) break;
3479 tree = tree->next;
3480 }
3481 if (tree == NULL) {
3482 VERROR(ctxt->userData,
3483"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3484 value, attr->name, elem->name);
3485 ret = 0;
3486 }
3487 }
3488
3489 /* Validity Constraint: Enumeration */
3490 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3491 xmlEnumerationPtr tree = attrDecl->tree;
3492 while (tree != NULL) {
3493 if (xmlStrEqual(tree->name, value)) break;
3494 tree = tree->next;
3495 }
3496 if (tree == NULL) {
3497 VERROR(ctxt->userData,
3498 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3499 value, attr->name, elem->name);
3500 ret = 0;
3501 }
3502 }
3503
3504 /* Fixed Attribute Default */
3505 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3506 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3507 VERROR(ctxt->userData,
3508 "Value for attribute %s on %s must be \"%s\"\n",
3509 attr->name, elem->name, attrDecl->defaultValue);
3510 ret = 0;
3511 }
3512
3513 /* Extra check for the attribute value */
3514 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3515 attrDecl->atype, value);
3516
3517 return(ret);
3518}
3519
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003520/**
3521 * xmlValidateSkipIgnorable:
3522 * @ctxt: the validation context
3523 * @child: the child list
3524 *
3525 * Skip ignorable elements w.r.t. the validation process
3526 *
3527 * returns the first element to consider for validation of the content model
3528 */
3529
3530static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003531xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003532 while (child != NULL) {
3533 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003534 /* These things are ignored (skipped) during validation. */
3535 case XML_PI_NODE:
3536 case XML_COMMENT_NODE:
3537 case XML_XINCLUDE_START:
3538 case XML_XINCLUDE_END:
3539 child = child->next;
3540 break;
3541 case XML_TEXT_NODE:
3542 if (xmlIsBlankNode(child))
3543 child = child->next;
3544 else
3545 return(child);
3546 break;
3547 /* keep current node */
3548 default:
3549 return(child);
3550 }
3551 }
3552 return(child);
3553}
3554
3555/**
3556 * xmlValidateElementType:
3557 * @ctxt: the validation context
3558 *
3559 * Try to validate the content model of an element internal function
3560 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003561 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3562 * reference is found and -3 if the validation succeeded but
3563 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003564 */
3565
3566static int
3567xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003568 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003569 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003570
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003571 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003572 if ((NODE == NULL) && (CONT == NULL))
3573 return(1);
3574 if ((NODE == NULL) &&
3575 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3576 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3577 return(1);
3578 }
3579 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003580 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003581 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003582
3583 /*
3584 * We arrive here when more states need to be examined
3585 */
3586cont:
3587
3588 /*
3589 * We just recovered from a rollback generated by a possible
3590 * epsilon transition, go directly to the analysis phase
3591 */
3592 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003593 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003594 DEBUG_VALID_STATE(NODE, CONT)
3595 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003596 goto analyze;
3597 }
3598
3599 DEBUG_VALID_STATE(NODE, CONT)
3600 /*
3601 * we may have to save a backup state here. This is the equivalent
3602 * of handling epsilon transition in NFAs.
3603 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003604 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003605 ((CONT->parent == NULL) ||
3606 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003607 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003608 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00003609 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003610 DEBUG_VALID_MSG("saving parent branch");
3611 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3612 }
3613
3614
3615 /*
3616 * Check first if the content matches
3617 */
3618 switch (CONT->type) {
3619 case XML_ELEMENT_CONTENT_PCDATA:
3620 if (NODE == NULL) {
3621 DEBUG_VALID_MSG("pcdata failed no node");
3622 ret = 0;
3623 break;
3624 }
3625 if (NODE->type == XML_TEXT_NODE) {
3626 DEBUG_VALID_MSG("pcdata found, skip to next");
3627 /*
3628 * go to next element in the content model
3629 * skipping ignorable elems
3630 */
3631 do {
3632 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003633 NODE = xmlValidateSkipIgnorable(NODE);
3634 if ((NODE != NULL) &&
3635 (NODE->type == XML_ENTITY_REF_NODE))
3636 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003637 } while ((NODE != NULL) &&
3638 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003639 (NODE->type != XML_TEXT_NODE) &&
3640 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003641 ret = 1;
3642 break;
3643 } else {
3644 DEBUG_VALID_MSG("pcdata failed");
3645 ret = 0;
3646 break;
3647 }
3648 break;
3649 case XML_ELEMENT_CONTENT_ELEMENT:
3650 if (NODE == NULL) {
3651 DEBUG_VALID_MSG("element failed no node");
3652 ret = 0;
3653 break;
3654 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003655 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3656 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003657 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003658 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3659 ret = (CONT->prefix == NULL);
3660 } else if (CONT->prefix == NULL) {
3661 ret = 0;
3662 } else {
3663 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3664 }
3665 }
3666 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003667 DEBUG_VALID_MSG("element found, skip to next");
3668 /*
3669 * go to next element in the content model
3670 * skipping ignorable elems
3671 */
3672 do {
3673 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003674 NODE = xmlValidateSkipIgnorable(NODE);
3675 if ((NODE != NULL) &&
3676 (NODE->type == XML_ENTITY_REF_NODE))
3677 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003678 } while ((NODE != NULL) &&
3679 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003680 (NODE->type != XML_TEXT_NODE) &&
3681 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003682 } else {
3683 DEBUG_VALID_MSG("element failed");
3684 ret = 0;
3685 break;
3686 }
3687 break;
3688 case XML_ELEMENT_CONTENT_OR:
3689 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003690 * Small optimization.
3691 */
3692 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3693 if ((NODE == NULL) ||
3694 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3695 DEPTH++;
3696 CONT = CONT->c2;
3697 goto cont;
3698 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003699 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3700 ret = (CONT->c1->prefix == NULL);
3701 } else if (CONT->c1->prefix == NULL) {
3702 ret = 0;
3703 } else {
3704 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3705 }
3706 if (ret == 0) {
3707 DEPTH++;
3708 CONT = CONT->c2;
3709 goto cont;
3710 }
Daniel Veillard85349052001-04-20 13:48:21 +00003711 }
3712
3713 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003714 * save the second branch 'or' branch
3715 */
3716 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003717 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3718 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003719
3720 DEPTH++;
3721 CONT = CONT->c1;
3722 goto cont;
3723 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003724 /*
3725 * Small optimization.
3726 */
3727 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3728 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3729 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3730 if ((NODE == NULL) ||
3731 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3732 DEPTH++;
3733 CONT = CONT->c2;
3734 goto cont;
3735 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003736 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3737 ret = (CONT->c1->prefix == NULL);
3738 } else if (CONT->c1->prefix == NULL) {
3739 ret = 0;
3740 } else {
3741 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3742 }
3743 if (ret == 0) {
3744 DEPTH++;
3745 CONT = CONT->c2;
3746 goto cont;
3747 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003748 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003749 DEPTH++;
3750 CONT = CONT->c1;
3751 goto cont;
3752 }
3753
3754 /*
3755 * At this point handle going up in the tree
3756 */
3757 if (ret == -1) {
3758 DEBUG_VALID_MSG("error found returning");
3759 return(ret);
3760 }
3761analyze:
3762 while (CONT != NULL) {
3763 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003764 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003765 * this level.
3766 */
3767 if (ret == 0) {
3768 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003769 xmlNodePtr cur;
3770
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003771 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003772 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003773 DEBUG_VALID_MSG("Once branch failed, rollback");
3774 if (vstateVPop(ctxt) < 0 ) {
3775 DEBUG_VALID_MSG("exhaustion, failed");
3776 return(0);
3777 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003778 if (cur != ctxt->vstate->node)
3779 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003780 goto cont;
3781 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00003782 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003783 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003784 DEBUG_VALID_MSG("Plus branch failed, rollback");
3785 if (vstateVPop(ctxt) < 0 ) {
3786 DEBUG_VALID_MSG("exhaustion, failed");
3787 return(0);
3788 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003789 if (cur != ctxt->vstate->node)
3790 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003791 goto cont;
3792 }
3793 DEBUG_VALID_MSG("Plus branch found");
3794 ret = 1;
3795 break;
3796 case XML_ELEMENT_CONTENT_MULT:
3797#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00003798 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003799 DEBUG_VALID_MSG("Mult branch failed");
3800 } else {
3801 DEBUG_VALID_MSG("Mult branch found");
3802 }
3803#endif
3804 ret = 1;
3805 break;
3806 case XML_ELEMENT_CONTENT_OPT:
3807 DEBUG_VALID_MSG("Option branch failed");
3808 ret = 1;
3809 break;
3810 }
3811 } else {
3812 switch (CONT->ocur) {
3813 case XML_ELEMENT_CONTENT_OPT:
3814 DEBUG_VALID_MSG("Option branch succeeded");
3815 ret = 1;
3816 break;
3817 case XML_ELEMENT_CONTENT_ONCE:
3818 DEBUG_VALID_MSG("Once branch succeeded");
3819 ret = 1;
3820 break;
3821 case XML_ELEMENT_CONTENT_PLUS:
3822 if (STATE == ROLLBACK_PARENT) {
3823 DEBUG_VALID_MSG("Plus branch rollback");
3824 ret = 1;
3825 break;
3826 }
3827 if (NODE == NULL) {
3828 DEBUG_VALID_MSG("Plus branch exhausted");
3829 ret = 1;
3830 break;
3831 }
3832 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003833 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003834 goto cont;
3835 case XML_ELEMENT_CONTENT_MULT:
3836 if (STATE == ROLLBACK_PARENT) {
3837 DEBUG_VALID_MSG("Mult branch rollback");
3838 ret = 1;
3839 break;
3840 }
3841 if (NODE == NULL) {
3842 DEBUG_VALID_MSG("Mult branch exhausted");
3843 ret = 1;
3844 break;
3845 }
3846 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003847 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003848 goto cont;
3849 }
3850 }
3851 STATE = 0;
3852
3853 /*
3854 * Then act accordingly at the parent level
3855 */
Daniel Veillard5344c602001-12-31 16:37:34 +00003856 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003857 if (CONT->parent == NULL)
3858 break;
3859
3860 switch (CONT->parent->type) {
3861 case XML_ELEMENT_CONTENT_PCDATA:
3862 DEBUG_VALID_MSG("Error: parent pcdata");
3863 return(-1);
3864 case XML_ELEMENT_CONTENT_ELEMENT:
3865 DEBUG_VALID_MSG("Error: parent element");
3866 return(-1);
3867 case XML_ELEMENT_CONTENT_OR:
3868 if (ret == 1) {
3869 DEBUG_VALID_MSG("Or succeeded");
3870 CONT = CONT->parent;
3871 DEPTH--;
3872 } else {
3873 DEBUG_VALID_MSG("Or failed");
3874 CONT = CONT->parent;
3875 DEPTH--;
3876 }
3877 break;
3878 case XML_ELEMENT_CONTENT_SEQ:
3879 if (ret == 0) {
3880 DEBUG_VALID_MSG("Sequence failed");
3881 CONT = CONT->parent;
3882 DEPTH--;
3883 } else if (CONT == CONT->parent->c1) {
3884 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3885 CONT = CONT->parent->c2;
3886 goto cont;
3887 } else {
3888 DEBUG_VALID_MSG("Sequence succeeded");
3889 CONT = CONT->parent;
3890 DEPTH--;
3891 }
3892 }
3893 }
3894 if (NODE != NULL) {
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("Failed, remaining input, 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 }
3907 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003908 xmlNodePtr cur;
3909
3910 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003911 DEBUG_VALID_MSG("Failure, rollback");
3912 if (vstateVPop(ctxt) < 0 ) {
3913 DEBUG_VALID_MSG("exhaustion, failed");
3914 return(0);
3915 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003916 if (cur != ctxt->vstate->node)
3917 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003918 goto cont;
3919 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003920 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003921}
3922
3923/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003924 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003925 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003926 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003927 * @content: An element
3928 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3929 *
3930 * This will dump the list of elements to the buffer
3931 * Intended just for the debug routine
3932 */
3933static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003934xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003935 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003936 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003937
3938 if (node == NULL) return;
3939 if (glob) strcat(buf, "(");
3940 cur = node;
3941 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003942 len = strlen(buf);
3943 if (size - len < 50) {
3944 if ((size - len > 4) && (buf[len - 1] != '.'))
3945 strcat(buf, " ...");
3946 return;
3947 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003948 switch (cur->type) {
3949 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003950 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00003951 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003952 if ((size - len > 4) && (buf[len - 1] != '.'))
3953 strcat(buf, " ...");
3954 return;
3955 }
3956 strcat(buf, (char *) cur->ns->prefix);
3957 strcat(buf, ":");
3958 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00003959 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003960 if ((size - len > 4) && (buf[len - 1] != '.'))
3961 strcat(buf, " ...");
3962 return;
3963 }
3964 strcat(buf, (char *) cur->name);
3965 if (cur->next != NULL)
3966 strcat(buf, " ");
3967 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003968 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003969 if (xmlIsBlankNode(cur))
3970 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003971 case XML_CDATA_SECTION_NODE:
3972 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003973 strcat(buf, "CDATA");
3974 if (cur->next != NULL)
3975 strcat(buf, " ");
3976 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003977 case XML_ATTRIBUTE_NODE:
3978 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003979#ifdef LIBXML_DOCB_ENABLED
3980 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003981#endif
3982 case XML_HTML_DOCUMENT_NODE:
3983 case XML_DOCUMENT_TYPE_NODE:
3984 case XML_DOCUMENT_FRAG_NODE:
3985 case XML_NOTATION_NODE:
3986 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003987 strcat(buf, "???");
3988 if (cur->next != NULL)
3989 strcat(buf, " ");
3990 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003991 case XML_ENTITY_NODE:
3992 case XML_PI_NODE:
3993 case XML_DTD_NODE:
3994 case XML_COMMENT_NODE:
3995 case XML_ELEMENT_DECL:
3996 case XML_ATTRIBUTE_DECL:
3997 case XML_ENTITY_DECL:
3998 case XML_XINCLUDE_START:
3999 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004000 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004001 }
4002 cur = cur->next;
4003 }
4004 if (glob) strcat(buf, ")");
4005}
4006
4007/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004008 * xmlValidateElementContent:
4009 * @ctxt: the validation context
4010 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004011 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004012 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004013 *
4014 * Try to validate the content model of an element
4015 *
4016 * returns 1 if valid or 0 if not and -1 in case of error
4017 */
4018
4019static int
4020xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004021 xmlElementPtr elemDecl, int warn) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004022 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004023 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004024 xmlElementContentPtr cont;
4025 const xmlChar *name;
4026
4027 if (elemDecl == NULL)
4028 return(-1);
4029 cont = elemDecl->content;
4030 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004031
4032 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004033 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004034 */
4035 ctxt->vstateMax = 8;
4036 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4037 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4038 if (ctxt->vstateTab == NULL) {
4039 xmlGenericError(xmlGenericErrorContext,
4040 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004041 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004042 }
4043 /*
4044 * The first entry in the stack is reserved to the current state
4045 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004046 ctxt->nodeMax = 0;
4047 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004048 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004049 ctxt->vstate = &ctxt->vstateTab[0];
4050 ctxt->vstateNr = 1;
4051 CONT = cont;
4052 NODE = child;
4053 DEPTH = 0;
4054 OCCURS = 0;
4055 STATE = 0;
4056 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004057 if ((ret == -3) && (warn)) {
4058 VWARNING(ctxt->userData,
4059 "Element %s content model is ambiguous\n", name);
4060 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004061 /*
4062 * An entities reference appeared at this level.
4063 * Buid a minimal representation of this node content
4064 * sufficient to run the validation process on it
4065 */
4066 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004067 cur = child;
4068 while (cur != NULL) {
4069 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004070 case XML_ENTITY_REF_NODE:
4071 /*
4072 * Push the current node to be able to roll back
4073 * and process within the entity
4074 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004075 if ((cur->children != NULL) &&
4076 (cur->children->children != NULL)) {
4077 nodeVPush(ctxt, cur);
4078 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004079 continue;
4080 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004081 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004082 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004083 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004084 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004085 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004086 case XML_CDATA_SECTION_NODE:
4087 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004088 case XML_ELEMENT_NODE:
4089 /*
4090 * Allocate a new node and minimally fills in
4091 * what's required
4092 */
4093 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4094 if (tmp == NULL) {
4095 xmlGenericError(xmlGenericErrorContext,
4096 "xmlValidateElementContent : malloc failed\n");
4097 xmlFreeNodeList(repl);
4098 ret = -1;
4099 goto done;
4100 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004101 tmp->type = cur->type;
4102 tmp->name = cur->name;
4103 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004104 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004105 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004106 if (repl == NULL)
4107 repl = last = tmp;
4108 else {
4109 last->next = tmp;
4110 last = tmp;
4111 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004112 if (cur->type == XML_CDATA_SECTION_NODE) {
4113 /*
4114 * E59 spaces in CDATA does not match the
4115 * nonterminal S
4116 */
4117 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4118 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004119 break;
4120 default:
4121 break;
4122 }
4123 /*
4124 * Switch to next element
4125 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004126 cur = cur->next;
4127 while (cur == NULL) {
4128 cur = nodeVPop(ctxt);
4129 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004130 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004131 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004132 }
4133 }
4134
4135 /*
4136 * Relaunch the validation
4137 */
4138 ctxt->vstate = &ctxt->vstateTab[0];
4139 ctxt->vstateNr = 1;
4140 CONT = cont;
4141 NODE = repl;
4142 DEPTH = 0;
4143 OCCURS = 0;
4144 STATE = 0;
4145 ret = xmlValidateElementType(ctxt);
4146 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004147 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004148 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4149 char expr[5000];
4150 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004151
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004152 expr[0] = 0;
4153 xmlSnprintfElementContent(expr, 5000, cont, 1);
4154 list[0] = 0;
4155 if (repl != NULL)
4156 xmlSnprintfElements(list, 5000, repl, 1);
4157 else
4158 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004159
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004160 if (name != NULL) {
4161 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004162 "Element %s content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004163 name, expr, list);
4164 } else {
4165 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004166 "Element content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004167 expr, list);
4168 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004169 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004170 if (name != NULL) {
4171 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004172 "Element %s content doesn't follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004173 name);
4174 } else {
4175 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004176 "Element content doesn't follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004177 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004178 }
4179 ret = 0;
4180 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004181 if (ret == -3)
4182 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004183
4184
4185done:
4186 /*
4187 * Deallocate the copy if done, and free up the validation stack
4188 */
4189 while (repl != NULL) {
4190 tmp = repl->next;
4191 xmlFree(repl);
4192 repl = tmp;
4193 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004194 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004195 if (ctxt->vstateTab != NULL) {
4196 xmlFree(ctxt->vstateTab);
4197 ctxt->vstateTab = NULL;
4198 }
4199 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004200 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004201 if (ctxt->nodeTab != NULL) {
4202 xmlFree(ctxt->nodeTab);
4203 ctxt->nodeTab = NULL;
4204 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004205 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004206
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004207}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004208
Owen Taylor3473f882001-02-23 17:55:21 +00004209/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004210 * xmlValidateCdataElement:
4211 * @ctxt: the validation context
4212 * @doc: a document instance
4213 * @elem: an element instance
4214 *
4215 * Check that an element follows #CDATA
4216 *
4217 * returns 1 if valid or 0 otherwise
4218 */
4219static int
4220xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4221 xmlNodePtr elem) {
4222 int ret = 1;
4223 xmlNodePtr cur, child;
4224
4225 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4226 return(0);
4227
4228 child = elem->children;
4229
4230 cur = child;
4231 while (cur != NULL) {
4232 switch (cur->type) {
4233 case XML_ENTITY_REF_NODE:
4234 /*
4235 * Push the current node to be able to roll back
4236 * and process within the entity
4237 */
4238 if ((cur->children != NULL) &&
4239 (cur->children->children != NULL)) {
4240 nodeVPush(ctxt, cur);
4241 cur = cur->children->children;
4242 continue;
4243 }
4244 break;
4245 case XML_COMMENT_NODE:
4246 case XML_PI_NODE:
4247 case XML_TEXT_NODE:
4248 case XML_CDATA_SECTION_NODE:
4249 break;
4250 default:
4251 ret = 0;
4252 goto done;
4253 }
4254 /*
4255 * Switch to next element
4256 */
4257 cur = cur->next;
4258 while (cur == NULL) {
4259 cur = nodeVPop(ctxt);
4260 if (cur == NULL)
4261 break;
4262 cur = cur->next;
4263 }
4264 }
4265done:
4266 ctxt->nodeMax = 0;
4267 ctxt->nodeNr = 0;
4268 if (ctxt->nodeTab != NULL) {
4269 xmlFree(ctxt->nodeTab);
4270 ctxt->nodeTab = NULL;
4271 }
4272 return(ret);
4273}
4274
4275/**
Owen Taylor3473f882001-02-23 17:55:21 +00004276 * xmlValidateOneElement:
4277 * @ctxt: the validation context
4278 * @doc: a document instance
4279 * @elem: an element instance
4280 *
4281 * Try to validate a single element and it's attributes,
4282 * basically it does the following checks as described by the
4283 * XML-1.0 recommendation:
4284 * - [ VC: Element Valid ]
4285 * - [ VC: Required Attribute ]
4286 * Then call xmlValidateOneAttribute() for each attribute present.
4287 *
4288 * The ID/IDREF checkings are done separately
4289 *
4290 * returns 1 if valid or 0 otherwise
4291 */
4292
4293int
4294xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4295 xmlNodePtr elem) {
4296 xmlElementPtr elemDecl = NULL;
4297 xmlElementContentPtr cont;
4298 xmlAttributePtr attr;
4299 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004300 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004301 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004302 const xmlChar *prefix = NULL;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004303 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004304
4305 CHECK_DTD;
4306
4307 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004308 switch (elem->type) {
4309 case XML_ATTRIBUTE_NODE:
4310 VERROR(ctxt->userData,
4311 "Attribute element not expected here\n");
4312 return(0);
4313 case XML_TEXT_NODE:
4314 if (elem->children != NULL) {
4315 VERROR(ctxt->userData, "Text element has childs !\n");
4316 return(0);
4317 }
4318 if (elem->properties != NULL) {
4319 VERROR(ctxt->userData, "Text element has attributes !\n");
4320 return(0);
4321 }
4322 if (elem->ns != NULL) {
4323 VERROR(ctxt->userData, "Text element has namespace !\n");
4324 return(0);
4325 }
4326 if (elem->nsDef != NULL) {
4327 VERROR(ctxt->userData,
4328 "Text element carries namespace definitions !\n");
4329 return(0);
4330 }
4331 if (elem->content == NULL) {
4332 VERROR(ctxt->userData,
4333 "Text element has no content !\n");
4334 return(0);
4335 }
4336 return(1);
4337 case XML_XINCLUDE_START:
4338 case XML_XINCLUDE_END:
4339 return(1);
4340 case XML_CDATA_SECTION_NODE:
4341 case XML_ENTITY_REF_NODE:
4342 case XML_PI_NODE:
4343 case XML_COMMENT_NODE:
4344 return(1);
4345 case XML_ENTITY_NODE:
4346 VERROR(ctxt->userData,
4347 "Entity element not expected here\n");
4348 return(0);
4349 case XML_NOTATION_NODE:
4350 VERROR(ctxt->userData,
4351 "Notation element not expected here\n");
4352 return(0);
4353 case XML_DOCUMENT_NODE:
4354 case XML_DOCUMENT_TYPE_NODE:
4355 case XML_DOCUMENT_FRAG_NODE:
4356 VERROR(ctxt->userData,
4357 "Document element not expected here\n");
4358 return(0);
4359 case XML_HTML_DOCUMENT_NODE:
4360 VERROR(ctxt->userData,
4361 "\n");
4362 return(0);
4363 case XML_ELEMENT_NODE:
4364 break;
4365 default:
4366 VERROR(ctxt->userData,
4367 "unknown element type %d\n", elem->type);
4368 return(0);
4369 }
4370 if (elem->name == NULL) return(0);
4371
4372 /*
4373 * Fetch the declaration for the qualified name
4374 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004375 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4376 prefix = elem->ns->prefix;
4377
4378 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004379 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004380 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004381 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004382 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004383 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004384 if (elemDecl != NULL)
4385 extsubset = 1;
4386 }
Owen Taylor3473f882001-02-23 17:55:21 +00004387 }
4388
4389 /*
4390 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004391 * This is "non-strict" validation should be done on the
4392 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004393 */
4394 if (elemDecl == NULL) {
4395 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004396 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004397 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004398 if (elemDecl != NULL)
4399 extsubset = 1;
4400 }
Owen Taylor3473f882001-02-23 17:55:21 +00004401 }
4402 if (elemDecl == NULL) {
4403 VERROR(ctxt->userData, "No declaration for element %s\n",
4404 elem->name);
4405 return(0);
4406 }
4407
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004408 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00004409 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004410 case XML_ELEMENT_TYPE_UNDEFINED:
4411 VERROR(ctxt->userData, "No declaration for element %s\n",
4412 elem->name);
4413 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004414 case XML_ELEMENT_TYPE_EMPTY:
4415 if (elem->children != NULL) {
4416 VERROR(ctxt->userData,
4417 "Element %s was declared EMPTY this one has content\n",
4418 elem->name);
4419 ret = 0;
4420 }
4421 break;
4422 case XML_ELEMENT_TYPE_ANY:
4423 /* I don't think anything is required then */
4424 break;
4425 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004426
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004427 /* simple case of declared as #PCDATA */
4428 if ((elemDecl->content != NULL) &&
4429 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4430 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4431 if (!ret) {
4432 VERROR(ctxt->userData,
4433 "Element %s was declared #PCDATA but contains non text nodes\n",
4434 elem->name);
4435 }
4436 break;
4437 }
Owen Taylor3473f882001-02-23 17:55:21 +00004438 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004439 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004440 while (child != NULL) {
4441 if (child->type == XML_ELEMENT_NODE) {
4442 name = child->name;
4443 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4444 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004445 snprintf((char *) qname, sizeof(qname), "%s:%s",
4446 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004447 qname[sizeof(qname) - 1] = 0;
4448 cont = elemDecl->content;
4449 while (cont != NULL) {
4450 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4451 if (xmlStrEqual(cont->name, qname)) break;
4452 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4453 (cont->c1 != NULL) &&
4454 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4455 if (xmlStrEqual(cont->c1->name, qname)) break;
4456 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4457 (cont->c1 == NULL) ||
4458 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4459 /* Internal error !!! */
4460 xmlGenericError(xmlGenericErrorContext,
4461 "Internal: MIXED struct bad\n");
4462 break;
4463 }
4464 cont = cont->c2;
4465 }
4466 if (cont != NULL)
4467 goto child_ok;
4468 }
4469 cont = elemDecl->content;
4470 while (cont != NULL) {
4471 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4472 if (xmlStrEqual(cont->name, name)) break;
4473 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4474 (cont->c1 != NULL) &&
4475 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4476 if (xmlStrEqual(cont->c1->name, name)) break;
4477 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4478 (cont->c1 == NULL) ||
4479 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4480 /* Internal error !!! */
4481 xmlGenericError(xmlGenericErrorContext,
4482 "Internal: MIXED struct bad\n");
4483 break;
4484 }
4485 cont = cont->c2;
4486 }
4487 if (cont == NULL) {
4488 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00004489 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004490 name, elem->name);
4491 ret = 0;
4492 }
4493 }
4494child_ok:
4495 child = child->next;
4496 }
4497 break;
4498 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004499 if ((doc->standalone == 1) && (extsubset == 1)) {
4500 /*
4501 * VC: Standalone Document Declaration
4502 * - element types with element content, if white space
4503 * occurs directly within any instance of those types.
4504 */
4505 child = elem->children;
4506 while (child != NULL) {
4507 if (child->type == XML_TEXT_NODE) {
4508 const xmlChar *content = child->content;
4509
4510 while (IS_BLANK(*content))
4511 content++;
4512 if (*content == 0) {
4513 VERROR(ctxt->userData,
4514"standalone: %s declared in the external subset contains white spaces nodes\n",
4515 elem->name);
4516 ret = 0;
4517 break;
4518 }
4519 }
4520 child =child->next;
4521 }
4522 }
Owen Taylor3473f882001-02-23 17:55:21 +00004523 child = elem->children;
4524 cont = elemDecl->content;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004525 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1);
4526 if (tmp <= 0)
4527 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004528 break;
4529 }
4530
4531 /* [ VC: Required Attribute ] */
4532 attr = elemDecl->attributes;
4533 while (attr != NULL) {
4534 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004535 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00004536
Daniel Veillarde4301c82002-02-13 13:32:35 +00004537 if ((attr->prefix == NULL) &&
4538 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4539 xmlNsPtr ns;
4540
4541 ns = elem->nsDef;
4542 while (ns != NULL) {
4543 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004544 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004545 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00004546 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004547 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4548 xmlNsPtr ns;
4549
4550 ns = elem->nsDef;
4551 while (ns != NULL) {
4552 if (xmlStrEqual(attr->name, ns->prefix))
4553 goto found;
4554 ns = ns->next;
4555 }
4556 } else {
4557 xmlAttrPtr attrib;
4558
4559 attrib = elem->properties;
4560 while (attrib != NULL) {
4561 if (xmlStrEqual(attrib->name, attr->name)) {
4562 if (attr->prefix != NULL) {
4563 xmlNsPtr nameSpace = attrib->ns;
4564
4565 if (nameSpace == NULL)
4566 nameSpace = elem->ns;
4567 /*
4568 * qualified names handling is problematic, having a
4569 * different prefix should be possible but DTDs don't
4570 * allow to define the URI instead of the prefix :-(
4571 */
4572 if (nameSpace == NULL) {
4573 if (qualified < 0)
4574 qualified = 0;
4575 } else if (!xmlStrEqual(nameSpace->prefix,
4576 attr->prefix)) {
4577 if (qualified < 1)
4578 qualified = 1;
4579 } else
4580 goto found;
4581 } else {
4582 /*
4583 * We should allow applications to define namespaces
4584 * for their application even if the DTD doesn't
4585 * carry one, otherwise, basically we would always
4586 * break.
4587 */
4588 goto found;
4589 }
4590 }
4591 attrib = attrib->next;
4592 }
Owen Taylor3473f882001-02-23 17:55:21 +00004593 }
4594 if (qualified == -1) {
4595 if (attr->prefix == NULL) {
4596 VERROR(ctxt->userData,
4597 "Element %s doesn't carry attribute %s\n",
4598 elem->name, attr->name);
4599 ret = 0;
4600 } else {
4601 VERROR(ctxt->userData,
4602 "Element %s doesn't carry attribute %s:%s\n",
4603 elem->name, attr->prefix,attr->name);
4604 ret = 0;
4605 }
4606 } else if (qualified == 0) {
4607 VWARNING(ctxt->userData,
4608 "Element %s required attribute %s:%s has no prefix\n",
4609 elem->name, attr->prefix,attr->name);
4610 } else if (qualified == 1) {
4611 VWARNING(ctxt->userData,
4612 "Element %s required attribute %s:%s has different prefix\n",
4613 elem->name, attr->prefix,attr->name);
4614 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004615 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
4616 /*
4617 * Special tests checking #FIXED namespace declarations
4618 * have the right value since this is not done as an
4619 * attribute checking
4620 */
4621 if ((attr->prefix == NULL) &&
4622 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4623 xmlNsPtr ns;
4624
4625 ns = elem->nsDef;
4626 while (ns != NULL) {
4627 if (ns->prefix == NULL) {
4628 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4629 VERROR(ctxt->userData,
4630 "Element %s namespace name for default namespace does not match the DTD\n",
4631 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00004632 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004633 }
4634 goto found;
4635 }
4636 ns = ns->next;
4637 }
4638 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4639 xmlNsPtr ns;
4640
4641 ns = elem->nsDef;
4642 while (ns != NULL) {
4643 if (xmlStrEqual(attr->name, ns->prefix)) {
4644 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4645 VERROR(ctxt->userData,
4646 "Element %s namespace name for %s doesn't match the DTD\n",
4647 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00004648 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004649 }
4650 goto found;
4651 }
4652 ns = ns->next;
4653 }
4654 }
Owen Taylor3473f882001-02-23 17:55:21 +00004655 }
4656found:
4657 attr = attr->nexth;
4658 }
4659 return(ret);
4660}
4661
4662/**
4663 * xmlValidateRoot:
4664 * @ctxt: the validation context
4665 * @doc: a document instance
4666 *
4667 * Try to validate a the root element
4668 * basically it does the following check as described by the
4669 * XML-1.0 recommendation:
4670 * - [ VC: Root Element Type ]
4671 * it doesn't try to recurse or apply other check to the element
4672 *
4673 * returns 1 if valid or 0 otherwise
4674 */
4675
4676int
4677xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4678 xmlNodePtr root;
4679 if (doc == NULL) return(0);
4680
4681 root = xmlDocGetRootElement(doc);
4682 if ((root == NULL) || (root->name == NULL)) {
4683 VERROR(ctxt->userData, "Not valid: no root element\n");
4684 return(0);
4685 }
4686
4687 /*
4688 * When doing post validation against a separate DTD, those may
4689 * no internal subset has been generated
4690 */
4691 if ((doc->intSubset != NULL) &&
4692 (doc->intSubset->name != NULL)) {
4693 /*
4694 * Check first the document root against the NQName
4695 */
4696 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4697 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4698 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004699 snprintf((char *) qname, sizeof(qname), "%s:%s",
4700 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004701 qname[sizeof(qname) - 1] = 0;
4702 if (xmlStrEqual(doc->intSubset->name, qname))
4703 goto name_ok;
4704 }
4705 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4706 (xmlStrEqual(root->name, BAD_CAST "html")))
4707 goto name_ok;
4708 VERROR(ctxt->userData,
4709 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4710 root->name, doc->intSubset->name);
4711 return(0);
4712
4713 }
4714 }
4715name_ok:
4716 return(1);
4717}
4718
4719
4720/**
4721 * xmlValidateElement:
4722 * @ctxt: the validation context
4723 * @doc: a document instance
4724 * @elem: an element instance
4725 *
4726 * Try to validate the subtree under an element
4727 *
4728 * returns 1 if valid or 0 otherwise
4729 */
4730
4731int
4732xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4733 xmlNodePtr child;
4734 xmlAttrPtr attr;
4735 xmlChar *value;
4736 int ret = 1;
4737
4738 if (elem == NULL) return(0);
4739
4740 /*
4741 * XInclude elements were added after parsing in the infoset,
4742 * they don't really mean anything validation wise.
4743 */
4744 if ((elem->type == XML_XINCLUDE_START) ||
4745 (elem->type == XML_XINCLUDE_END))
4746 return(1);
4747
4748 CHECK_DTD;
4749
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004750 /*
4751 * Entities references have to be handled separately
4752 */
4753 if (elem->type == XML_ENTITY_REF_NODE) {
4754 return(1);
4755 }
4756
Owen Taylor3473f882001-02-23 17:55:21 +00004757 ret &= xmlValidateOneElement(ctxt, doc, elem);
4758 attr = elem->properties;
4759 while(attr != NULL) {
4760 value = xmlNodeListGetString(doc, attr->children, 0);
4761 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4762 if (value != NULL)
4763 xmlFree(value);
4764 attr= attr->next;
4765 }
4766 child = elem->children;
4767 while (child != NULL) {
4768 ret &= xmlValidateElement(ctxt, doc, child);
4769 child = child->next;
4770 }
4771
4772 return(ret);
4773}
4774
Daniel Veillard8730c562001-02-26 10:49:57 +00004775/**
4776 * xmlValidateRef:
4777 * @ref: A reference to be validated
4778 * @ctxt: Validation context
4779 * @name: Name of ID we are searching for
4780 *
4781 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004782static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004783xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004784 const xmlChar *name) {
4785 xmlAttrPtr id;
4786 xmlAttrPtr attr;
4787
4788 if (ref == NULL)
4789 return;
4790 attr = ref->attr;
4791 if (attr == NULL)
4792 return;
4793 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4794 id = xmlGetID(ctxt->doc, name);
4795 if (id == NULL) {
4796 VERROR(ctxt->userData,
4797 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4798 attr->name, name);
4799 ctxt->valid = 0;
4800 }
4801 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4802 xmlChar *dup, *str = NULL, *cur, save;
4803
4804 dup = xmlStrdup(name);
4805 if (dup == NULL) {
4806 ctxt->valid = 0;
4807 return;
4808 }
4809 cur = dup;
4810 while (*cur != 0) {
4811 str = cur;
4812 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4813 save = *cur;
4814 *cur = 0;
4815 id = xmlGetID(ctxt->doc, str);
4816 if (id == NULL) {
4817 VERROR(ctxt->userData,
4818 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4819 attr->name, str);
4820 ctxt->valid = 0;
4821 }
4822 if (save == 0)
4823 break;
4824 *cur = save;
4825 while (IS_BLANK(*cur)) cur++;
4826 }
4827 xmlFree(dup);
4828 }
4829}
4830
4831/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004832 * xmlWalkValidateList:
4833 * @data: Contents of current link
4834 * @user: Value supplied by the user
4835 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004836 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00004837 */
4838static int
4839xmlWalkValidateList(const void *data, const void *user)
4840{
4841 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4842 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4843 return 1;
4844}
4845
4846/**
4847 * xmlValidateCheckRefCallback:
4848 * @ref_list: List of references
4849 * @ctxt: Validation context
4850 * @name: Name of ID we are searching for
4851 *
4852 */
4853static void
4854xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4855 const xmlChar *name) {
4856 xmlValidateMemo memo;
4857
4858 if (ref_list == NULL)
4859 return;
4860 memo.ctxt = ctxt;
4861 memo.name = name;
4862
4863 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4864
4865}
4866
4867/**
Owen Taylor3473f882001-02-23 17:55:21 +00004868 * xmlValidateDocumentFinal:
4869 * @ctxt: the validation context
4870 * @doc: a document instance
4871 *
4872 * Does the final step for the document validation once all the
4873 * incremental validation steps have been completed
4874 *
4875 * basically it does the following checks described by the XML Rec
4876 *
4877 *
4878 * returns 1 if valid or 0 otherwise
4879 */
4880
4881int
4882xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4883 xmlRefTablePtr table;
4884
4885 if (doc == NULL) {
4886 xmlGenericError(xmlGenericErrorContext,
4887 "xmlValidateDocumentFinal: doc == NULL\n");
4888 return(0);
4889 }
4890
4891 /*
4892 * Check all the NOTATION/NOTATIONS attributes
4893 */
4894 /*
4895 * Check all the ENTITY/ENTITIES attributes definition for validity
4896 */
4897 /*
4898 * Check all the IDREF/IDREFS attributes definition for validity
4899 */
4900 table = (xmlRefTablePtr) doc->refs;
4901 ctxt->doc = doc;
4902 ctxt->valid = 1;
4903 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4904 return(ctxt->valid);
4905}
4906
4907/**
4908 * xmlValidateDtd:
4909 * @ctxt: the validation context
4910 * @doc: a document instance
4911 * @dtd: a dtd instance
4912 *
4913 * Try to validate the document against the dtd instance
4914 *
4915 * basically it does check all the definitions in the DtD.
4916 *
4917 * returns 1 if valid or 0 otherwise
4918 */
4919
4920int
4921xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4922 int ret;
4923 xmlDtdPtr oldExt;
4924 xmlNodePtr root;
4925
4926 if (dtd == NULL) return(0);
4927 if (doc == NULL) return(0);
4928 oldExt = doc->extSubset;
4929 doc->extSubset = dtd;
4930 ret = xmlValidateRoot(ctxt, doc);
4931 if (ret == 0) {
4932 doc->extSubset = oldExt;
4933 return(ret);
4934 }
4935 if (doc->ids != NULL) {
4936 xmlFreeIDTable(doc->ids);
4937 doc->ids = NULL;
4938 }
4939 if (doc->refs != NULL) {
4940 xmlFreeRefTable(doc->refs);
4941 doc->refs = NULL;
4942 }
4943 root = xmlDocGetRootElement(doc);
4944 ret = xmlValidateElement(ctxt, doc, root);
4945 ret &= xmlValidateDocumentFinal(ctxt, doc);
4946 doc->extSubset = oldExt;
4947 return(ret);
4948}
4949
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004950static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004951xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
4952 const xmlChar *name ATTRIBUTE_UNUSED) {
4953 if (cur == NULL)
4954 return;
4955 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
4956 xmlChar *notation = cur->content;
4957
Daniel Veillard878eab02002-02-19 13:46:09 +00004958 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004959 int ret;
4960
4961 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
4962 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00004963 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004964 }
4965 }
4966 }
4967}
4968
4969static void
Owen Taylor3473f882001-02-23 17:55:21 +00004970xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004971 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004972 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00004973 xmlDocPtr doc;
4974 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004975
Owen Taylor3473f882001-02-23 17:55:21 +00004976 if (cur == NULL)
4977 return;
4978 switch (cur->atype) {
4979 case XML_ATTRIBUTE_CDATA:
4980 case XML_ATTRIBUTE_ID:
4981 case XML_ATTRIBUTE_IDREF :
4982 case XML_ATTRIBUTE_IDREFS:
4983 case XML_ATTRIBUTE_NMTOKEN:
4984 case XML_ATTRIBUTE_NMTOKENS:
4985 case XML_ATTRIBUTE_ENUMERATION:
4986 break;
4987 case XML_ATTRIBUTE_ENTITY:
4988 case XML_ATTRIBUTE_ENTITIES:
4989 case XML_ATTRIBUTE_NOTATION:
4990 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004991
4992 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
4993 cur->atype, cur->defaultValue);
4994 if ((ret == 0) && (ctxt->valid == 1))
4995 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004996 }
4997 if (cur->tree != NULL) {
4998 xmlEnumerationPtr tree = cur->tree;
4999 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005000 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00005001 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005002 if ((ret == 0) && (ctxt->valid == 1))
5003 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005004 tree = tree->next;
5005 }
5006 }
5007 }
Daniel Veillard878eab02002-02-19 13:46:09 +00005008 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
5009 doc = cur->doc;
5010 if ((doc == NULL) || (cur->elem == NULL)) {
5011 VERROR(ctxt->userData,
5012 "xmlValidateAttributeCallback(%s): internal error\n",
5013 cur->name);
5014 return;
5015 }
5016 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
5017 if (elem == NULL)
5018 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
5019 if (elem == NULL) {
5020 VERROR(ctxt->userData,
5021 "attribute %s: could not find decl for element %s\n",
5022 cur->name, cur->elem);
5023 return;
5024 }
5025 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
5026 VERROR(ctxt->userData,
5027 "NOTATION attribute %s declared on EMPTY element %s\n",
5028 cur->name, cur->elem);
5029 ctxt->valid = 0;
5030 }
5031 }
Owen Taylor3473f882001-02-23 17:55:21 +00005032}
5033
5034/**
5035 * xmlValidateDtdFinal:
5036 * @ctxt: the validation context
5037 * @doc: a document instance
5038 *
5039 * Does the final step for the dtds validation once all the
5040 * subsets have been parsed
5041 *
5042 * basically it does the following checks described by the XML Rec
5043 * - check that ENTITY and ENTITIES type attributes default or
5044 * possible values matches one of the defined entities.
5045 * - check that NOTATION type attributes default or
5046 * possible values matches one of the defined notations.
5047 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005048 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00005049 */
5050
5051int
5052xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00005053 xmlDtdPtr dtd;
5054 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005055 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00005056
5057 if (doc == NULL) return(0);
5058 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5059 return(0);
5060 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005061 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005062 dtd = doc->intSubset;
5063 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5064 table = (xmlAttributeTablePtr) dtd->attributes;
5065 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005066 }
5067 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005068 entities = (xmlEntitiesTablePtr) dtd->entities;
5069 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5070 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005071 }
5072 dtd = doc->extSubset;
5073 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5074 table = (xmlAttributeTablePtr) dtd->attributes;
5075 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005076 }
5077 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005078 entities = (xmlEntitiesTablePtr) dtd->entities;
5079 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5080 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005081 }
5082 return(ctxt->valid);
5083}
5084
5085/**
5086 * xmlValidateDocument:
5087 * @ctxt: the validation context
5088 * @doc: a document instance
5089 *
5090 * Try to validate the document instance
5091 *
5092 * basically it does the all the checks described by the XML Rec
5093 * i.e. validates the internal and external subset (if present)
5094 * and validate the document tree.
5095 *
5096 * returns 1 if valid or 0 otherwise
5097 */
5098
5099int
5100xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5101 int ret;
5102 xmlNodePtr root;
5103
5104 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5105 return(0);
5106 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
5107 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
5108 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
5109 doc->intSubset->SystemID);
5110 if (doc->extSubset == NULL) {
5111 if (doc->intSubset->SystemID != NULL) {
5112 VERROR(ctxt->userData,
5113 "Could not load the external subset \"%s\"\n",
5114 doc->intSubset->SystemID);
5115 } else {
5116 VERROR(ctxt->userData,
5117 "Could not load the external subset \"%s\"\n",
5118 doc->intSubset->ExternalID);
5119 }
5120 return(0);
5121 }
5122 }
5123
5124 if (doc->ids != NULL) {
5125 xmlFreeIDTable(doc->ids);
5126 doc->ids = NULL;
5127 }
5128 if (doc->refs != NULL) {
5129 xmlFreeRefTable(doc->refs);
5130 doc->refs = NULL;
5131 }
5132 ret = xmlValidateDtdFinal(ctxt, doc);
5133 if (!xmlValidateRoot(ctxt, doc)) return(0);
5134
5135 root = xmlDocGetRootElement(doc);
5136 ret &= xmlValidateElement(ctxt, doc, root);
5137 ret &= xmlValidateDocumentFinal(ctxt, doc);
5138 return(ret);
5139}
5140
5141
5142/************************************************************************
5143 * *
5144 * Routines for dynamic validation editing *
5145 * *
5146 ************************************************************************/
5147
5148/**
5149 * xmlValidGetPotentialChildren:
5150 * @ctree: an element content tree
5151 * @list: an array to store the list of child names
5152 * @len: a pointer to the number of element in the list
5153 * @max: the size of the array
5154 *
5155 * Build/extend a list of potential children allowed by the content tree
5156 *
5157 * returns the number of element in the list, or -1 in case of error.
5158 */
5159
5160int
5161xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
5162 int *len, int max) {
5163 int i;
5164
5165 if ((ctree == NULL) || (list == NULL) || (len == NULL))
5166 return(-1);
5167 if (*len >= max) return(*len);
5168
5169 switch (ctree->type) {
5170 case XML_ELEMENT_CONTENT_PCDATA:
5171 for (i = 0; i < *len;i++)
5172 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
5173 list[(*len)++] = BAD_CAST "#PCDATA";
5174 break;
5175 case XML_ELEMENT_CONTENT_ELEMENT:
5176 for (i = 0; i < *len;i++)
5177 if (xmlStrEqual(ctree->name, list[i])) return(*len);
5178 list[(*len)++] = ctree->name;
5179 break;
5180 case XML_ELEMENT_CONTENT_SEQ:
5181 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5182 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5183 break;
5184 case XML_ELEMENT_CONTENT_OR:
5185 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5186 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5187 break;
5188 }
5189
5190 return(*len);
5191}
5192
5193/**
5194 * xmlValidGetValidElements:
5195 * @prev: an element to insert after
5196 * @next: an element to insert next
5197 * @list: an array to store the list of child names
5198 * @max: the size of the array
5199 *
5200 * This function returns the list of authorized children to insert
5201 * within an existing tree while respecting the validity constraints
5202 * forced by the Dtd. The insertion point is defined using @prev and
5203 * @next in the following ways:
5204 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
5205 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
5206 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
5207 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
5208 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
5209 *
5210 * pointers to the element names are inserted at the beginning of the array
5211 * and do not need to be freed.
5212 *
5213 * returns the number of element in the list, or -1 in case of error. If
5214 * the function returns the value @max the caller is invited to grow the
5215 * receiving array and retry.
5216 */
5217
5218int
5219xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
5220 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005221 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00005222 int nb_valid_elements = 0;
5223 const xmlChar *elements[256];
5224 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005225 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005226
5227 xmlNode *ref_node;
5228 xmlNode *parent;
5229 xmlNode *test_node;
5230
5231 xmlNode *prev_next;
5232 xmlNode *next_prev;
5233 xmlNode *parent_childs;
5234 xmlNode *parent_last;
5235
5236 xmlElement *element_desc;
5237
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00005238 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005239
Owen Taylor3473f882001-02-23 17:55:21 +00005240 if (prev == NULL && next == NULL)
5241 return(-1);
5242
5243 if (list == NULL) return(-1);
5244 if (max <= 0) return(-1);
5245
5246 nb_valid_elements = 0;
5247 ref_node = prev ? prev : next;
5248 parent = ref_node->parent;
5249
5250 /*
5251 * Retrieves the parent element declaration
5252 */
5253 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
5254 parent->name);
5255 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
5256 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
5257 parent->name);
5258 if (element_desc == NULL) return(-1);
5259
5260 /*
5261 * Do a backup of the current tree structure
5262 */
5263 prev_next = prev ? prev->next : NULL;
5264 next_prev = next ? next->prev : NULL;
5265 parent_childs = parent->children;
5266 parent_last = parent->last;
5267
5268 /*
5269 * Creates a dummy node and insert it into the tree
5270 */
5271 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
5272 test_node->doc = ref_node->doc;
5273 test_node->parent = parent;
5274 test_node->prev = prev;
5275 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00005276 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00005277
5278 if (prev) prev->next = test_node;
5279 else parent->children = test_node;
5280
5281 if (next) next->prev = test_node;
5282 else parent->last = test_node;
5283
5284 /*
5285 * Insert each potential child node and check if the parent is
5286 * still valid
5287 */
5288 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
5289 elements, &nb_elements, 256);
5290
5291 for (i = 0;i < nb_elements;i++) {
5292 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005293 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005294 int j;
5295
5296 for (j = 0; j < nb_valid_elements;j++)
5297 if (xmlStrEqual(elements[i], list[j])) break;
5298 list[nb_valid_elements++] = elements[i];
5299 if (nb_valid_elements >= max) break;
5300 }
5301 }
5302
5303 /*
5304 * Restore the tree structure
5305 */
5306 if (prev) prev->next = prev_next;
5307 if (next) next->prev = next_prev;
5308 parent->children = parent_childs;
5309 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00005310
5311 /*
5312 * Free up the dummy node
5313 */
5314 test_node->name = name;
5315 xmlFreeNode(test_node);
5316
Owen Taylor3473f882001-02-23 17:55:21 +00005317 return(nb_valid_elements);
5318}