blob: 1f1845d11082a7fafb607cac49d53cccd47e45a4 [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 Veillard56a4cb82001-03-24 17:00:36 +00002588static int
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 Veillard56a4cb82001-03-24 17:00:36 +00002627static int
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 Veillard56a4cb82001-03-24 17:00:36 +00002692static int
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 Veillard56a4cb82001-03-24 17:00:36 +00002734static int
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
Daniel Veillardd85f4f42002-03-25 10:48:46 +00003385 if (xmlStrEqual(attr->name, "lang")) {
3386 printf("hello\n");
3387 }
Owen Taylor3473f882001-02-23 17:55:21 +00003388 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3389 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003390 snprintf((char *) qname, sizeof(qname), "%s:%s",
3391 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003392 qname[sizeof(qname) - 1] = 0;
3393 if (attr->ns != NULL) {
3394 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3395 attr->name, attr->ns->prefix);
3396 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3397 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3398 attr->name, attr->ns->prefix);
3399 } else {
3400 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3401 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3402 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3403 qname, attr->name);
3404 }
3405 }
3406 if (attrDecl == NULL) {
3407 if (attr->ns != NULL) {
3408 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3409 attr->name, attr->ns->prefix);
3410 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3411 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3412 attr->name, attr->ns->prefix);
3413 } else {
3414 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3415 elem->name, attr->name);
3416 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3417 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3418 elem->name, attr->name);
3419 }
3420 }
3421
3422
3423 /* Validity Constraint: Attribute Value Type */
3424 if (attrDecl == NULL) {
3425 VERROR(ctxt->userData,
3426 "No declaration for attribute %s on element %s\n",
3427 attr->name, elem->name);
3428 return(0);
3429 }
3430 attr->atype = attrDecl->atype;
3431
3432 val = xmlValidateAttributeValue(attrDecl->atype, value);
3433 if (val == 0) {
3434 VERROR(ctxt->userData,
3435 "Syntax of value for attribute %s on %s is not valid\n",
3436 attr->name, elem->name);
3437 ret = 0;
3438 }
3439
3440 /* Validity constraint: Fixed Attribute Default */
3441 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3442 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3443 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003444 "Value for attribute %s on %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003445 attr->name, elem->name, attrDecl->defaultValue);
3446 ret = 0;
3447 }
3448 }
3449
3450 /* Validity Constraint: ID uniqueness */
3451 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3452 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3453 ret = 0;
3454 }
3455
3456 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3457 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3458 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3459 ret = 0;
3460 }
3461
3462 /* Validity Constraint: Notation Attributes */
3463 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3464 xmlEnumerationPtr tree = attrDecl->tree;
3465 xmlNotationPtr nota;
3466
3467 /* First check that the given NOTATION was declared */
3468 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3469 if (nota == NULL)
3470 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3471
3472 if (nota == NULL) {
3473 VERROR(ctxt->userData,
3474 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3475 value, attr->name, elem->name);
3476 ret = 0;
3477 }
3478
3479 /* Second, verify that it's among the list */
3480 while (tree != NULL) {
3481 if (xmlStrEqual(tree->name, value)) break;
3482 tree = tree->next;
3483 }
3484 if (tree == NULL) {
3485 VERROR(ctxt->userData,
3486"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3487 value, attr->name, elem->name);
3488 ret = 0;
3489 }
3490 }
3491
3492 /* Validity Constraint: Enumeration */
3493 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3494 xmlEnumerationPtr tree = attrDecl->tree;
3495 while (tree != NULL) {
3496 if (xmlStrEqual(tree->name, value)) break;
3497 tree = tree->next;
3498 }
3499 if (tree == NULL) {
3500 VERROR(ctxt->userData,
3501 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3502 value, attr->name, elem->name);
3503 ret = 0;
3504 }
3505 }
3506
3507 /* Fixed Attribute Default */
3508 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3509 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3510 VERROR(ctxt->userData,
3511 "Value for attribute %s on %s must be \"%s\"\n",
3512 attr->name, elem->name, attrDecl->defaultValue);
3513 ret = 0;
3514 }
3515
3516 /* Extra check for the attribute value */
3517 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3518 attrDecl->atype, value);
3519
3520 return(ret);
3521}
3522
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003523/**
3524 * xmlValidateSkipIgnorable:
3525 * @ctxt: the validation context
3526 * @child: the child list
3527 *
3528 * Skip ignorable elements w.r.t. the validation process
3529 *
3530 * returns the first element to consider for validation of the content model
3531 */
3532
3533static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003534xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003535 while (child != NULL) {
3536 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003537 /* These things are ignored (skipped) during validation. */
3538 case XML_PI_NODE:
3539 case XML_COMMENT_NODE:
3540 case XML_XINCLUDE_START:
3541 case XML_XINCLUDE_END:
3542 child = child->next;
3543 break;
3544 case XML_TEXT_NODE:
3545 if (xmlIsBlankNode(child))
3546 child = child->next;
3547 else
3548 return(child);
3549 break;
3550 /* keep current node */
3551 default:
3552 return(child);
3553 }
3554 }
3555 return(child);
3556}
3557
3558/**
3559 * xmlValidateElementType:
3560 * @ctxt: the validation context
3561 *
3562 * Try to validate the content model of an element internal function
3563 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003564 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3565 * reference is found and -3 if the validation succeeded but
3566 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003567 */
3568
3569static int
3570xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003571 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003572 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003573
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003574 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003575 if ((NODE == NULL) && (CONT == NULL))
3576 return(1);
3577 if ((NODE == NULL) &&
3578 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3579 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3580 return(1);
3581 }
3582 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003583 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003584 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003585
3586 /*
3587 * We arrive here when more states need to be examined
3588 */
3589cont:
3590
3591 /*
3592 * We just recovered from a rollback generated by a possible
3593 * epsilon transition, go directly to the analysis phase
3594 */
3595 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003596 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003597 DEBUG_VALID_STATE(NODE, CONT)
3598 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003599 goto analyze;
3600 }
3601
3602 DEBUG_VALID_STATE(NODE, CONT)
3603 /*
3604 * we may have to save a backup state here. This is the equivalent
3605 * of handling epsilon transition in NFAs.
3606 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003607 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003608 ((CONT->parent == NULL) ||
3609 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003610 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003611 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00003612 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003613 DEBUG_VALID_MSG("saving parent branch");
3614 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3615 }
3616
3617
3618 /*
3619 * Check first if the content matches
3620 */
3621 switch (CONT->type) {
3622 case XML_ELEMENT_CONTENT_PCDATA:
3623 if (NODE == NULL) {
3624 DEBUG_VALID_MSG("pcdata failed no node");
3625 ret = 0;
3626 break;
3627 }
3628 if (NODE->type == XML_TEXT_NODE) {
3629 DEBUG_VALID_MSG("pcdata found, skip to next");
3630 /*
3631 * go to next element in the content model
3632 * skipping ignorable elems
3633 */
3634 do {
3635 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003636 NODE = xmlValidateSkipIgnorable(NODE);
3637 if ((NODE != NULL) &&
3638 (NODE->type == XML_ENTITY_REF_NODE))
3639 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003640 } while ((NODE != NULL) &&
3641 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003642 (NODE->type != XML_TEXT_NODE) &&
3643 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003644 ret = 1;
3645 break;
3646 } else {
3647 DEBUG_VALID_MSG("pcdata failed");
3648 ret = 0;
3649 break;
3650 }
3651 break;
3652 case XML_ELEMENT_CONTENT_ELEMENT:
3653 if (NODE == NULL) {
3654 DEBUG_VALID_MSG("element failed no node");
3655 ret = 0;
3656 break;
3657 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003658 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3659 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003660 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003661 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3662 ret = (CONT->prefix == NULL);
3663 } else if (CONT->prefix == NULL) {
3664 ret = 0;
3665 } else {
3666 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3667 }
3668 }
3669 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003670 DEBUG_VALID_MSG("element found, skip to next");
3671 /*
3672 * go to next element in the content model
3673 * skipping ignorable elems
3674 */
3675 do {
3676 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003677 NODE = xmlValidateSkipIgnorable(NODE);
3678 if ((NODE != NULL) &&
3679 (NODE->type == XML_ENTITY_REF_NODE))
3680 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003681 } while ((NODE != NULL) &&
3682 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003683 (NODE->type != XML_TEXT_NODE) &&
3684 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003685 } else {
3686 DEBUG_VALID_MSG("element failed");
3687 ret = 0;
3688 break;
3689 }
3690 break;
3691 case XML_ELEMENT_CONTENT_OR:
3692 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003693 * Small optimization.
3694 */
3695 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3696 if ((NODE == NULL) ||
3697 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3698 DEPTH++;
3699 CONT = CONT->c2;
3700 goto cont;
3701 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003702 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3703 ret = (CONT->c1->prefix == NULL);
3704 } else if (CONT->c1->prefix == NULL) {
3705 ret = 0;
3706 } else {
3707 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3708 }
3709 if (ret == 0) {
3710 DEPTH++;
3711 CONT = CONT->c2;
3712 goto cont;
3713 }
Daniel Veillard85349052001-04-20 13:48:21 +00003714 }
3715
3716 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003717 * save the second branch 'or' branch
3718 */
3719 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003720 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3721 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003722
3723 DEPTH++;
3724 CONT = CONT->c1;
3725 goto cont;
3726 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003727 /*
3728 * Small optimization.
3729 */
3730 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3731 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3732 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3733 if ((NODE == NULL) ||
3734 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3735 DEPTH++;
3736 CONT = CONT->c2;
3737 goto cont;
3738 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003739 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3740 ret = (CONT->c1->prefix == NULL);
3741 } else if (CONT->c1->prefix == NULL) {
3742 ret = 0;
3743 } else {
3744 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3745 }
3746 if (ret == 0) {
3747 DEPTH++;
3748 CONT = CONT->c2;
3749 goto cont;
3750 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003751 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003752 DEPTH++;
3753 CONT = CONT->c1;
3754 goto cont;
3755 }
3756
3757 /*
3758 * At this point handle going up in the tree
3759 */
3760 if (ret == -1) {
3761 DEBUG_VALID_MSG("error found returning");
3762 return(ret);
3763 }
3764analyze:
3765 while (CONT != NULL) {
3766 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003767 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003768 * this level.
3769 */
3770 if (ret == 0) {
3771 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003772 xmlNodePtr cur;
3773
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003774 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003775 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003776 DEBUG_VALID_MSG("Once branch failed, rollback");
3777 if (vstateVPop(ctxt) < 0 ) {
3778 DEBUG_VALID_MSG("exhaustion, failed");
3779 return(0);
3780 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003781 if (cur != ctxt->vstate->node)
3782 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003783 goto cont;
3784 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00003785 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003786 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003787 DEBUG_VALID_MSG("Plus branch failed, rollback");
3788 if (vstateVPop(ctxt) < 0 ) {
3789 DEBUG_VALID_MSG("exhaustion, failed");
3790 return(0);
3791 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003792 if (cur != ctxt->vstate->node)
3793 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003794 goto cont;
3795 }
3796 DEBUG_VALID_MSG("Plus branch found");
3797 ret = 1;
3798 break;
3799 case XML_ELEMENT_CONTENT_MULT:
3800#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00003801 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003802 DEBUG_VALID_MSG("Mult branch failed");
3803 } else {
3804 DEBUG_VALID_MSG("Mult branch found");
3805 }
3806#endif
3807 ret = 1;
3808 break;
3809 case XML_ELEMENT_CONTENT_OPT:
3810 DEBUG_VALID_MSG("Option branch failed");
3811 ret = 1;
3812 break;
3813 }
3814 } else {
3815 switch (CONT->ocur) {
3816 case XML_ELEMENT_CONTENT_OPT:
3817 DEBUG_VALID_MSG("Option branch succeeded");
3818 ret = 1;
3819 break;
3820 case XML_ELEMENT_CONTENT_ONCE:
3821 DEBUG_VALID_MSG("Once branch succeeded");
3822 ret = 1;
3823 break;
3824 case XML_ELEMENT_CONTENT_PLUS:
3825 if (STATE == ROLLBACK_PARENT) {
3826 DEBUG_VALID_MSG("Plus branch rollback");
3827 ret = 1;
3828 break;
3829 }
3830 if (NODE == NULL) {
3831 DEBUG_VALID_MSG("Plus branch exhausted");
3832 ret = 1;
3833 break;
3834 }
3835 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003836 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003837 goto cont;
3838 case XML_ELEMENT_CONTENT_MULT:
3839 if (STATE == ROLLBACK_PARENT) {
3840 DEBUG_VALID_MSG("Mult branch rollback");
3841 ret = 1;
3842 break;
3843 }
3844 if (NODE == NULL) {
3845 DEBUG_VALID_MSG("Mult branch exhausted");
3846 ret = 1;
3847 break;
3848 }
3849 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003850 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003851 goto cont;
3852 }
3853 }
3854 STATE = 0;
3855
3856 /*
3857 * Then act accordingly at the parent level
3858 */
Daniel Veillard5344c602001-12-31 16:37:34 +00003859 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003860 if (CONT->parent == NULL)
3861 break;
3862
3863 switch (CONT->parent->type) {
3864 case XML_ELEMENT_CONTENT_PCDATA:
3865 DEBUG_VALID_MSG("Error: parent pcdata");
3866 return(-1);
3867 case XML_ELEMENT_CONTENT_ELEMENT:
3868 DEBUG_VALID_MSG("Error: parent element");
3869 return(-1);
3870 case XML_ELEMENT_CONTENT_OR:
3871 if (ret == 1) {
3872 DEBUG_VALID_MSG("Or succeeded");
3873 CONT = CONT->parent;
3874 DEPTH--;
3875 } else {
3876 DEBUG_VALID_MSG("Or failed");
3877 CONT = CONT->parent;
3878 DEPTH--;
3879 }
3880 break;
3881 case XML_ELEMENT_CONTENT_SEQ:
3882 if (ret == 0) {
3883 DEBUG_VALID_MSG("Sequence failed");
3884 CONT = CONT->parent;
3885 DEPTH--;
3886 } else if (CONT == CONT->parent->c1) {
3887 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3888 CONT = CONT->parent->c2;
3889 goto cont;
3890 } else {
3891 DEBUG_VALID_MSG("Sequence succeeded");
3892 CONT = CONT->parent;
3893 DEPTH--;
3894 }
3895 }
3896 }
3897 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003898 xmlNodePtr cur;
3899
3900 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003901 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3902 if (vstateVPop(ctxt) < 0 ) {
3903 DEBUG_VALID_MSG("exhaustion, failed");
3904 return(0);
3905 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003906 if (cur != ctxt->vstate->node)
3907 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003908 goto cont;
3909 }
3910 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003911 xmlNodePtr cur;
3912
3913 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003914 DEBUG_VALID_MSG("Failure, rollback");
3915 if (vstateVPop(ctxt) < 0 ) {
3916 DEBUG_VALID_MSG("exhaustion, failed");
3917 return(0);
3918 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003919 if (cur != ctxt->vstate->node)
3920 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003921 goto cont;
3922 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003923 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003924}
3925
3926/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003927 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003928 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003929 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003930 * @content: An element
3931 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3932 *
3933 * This will dump the list of elements to the buffer
3934 * Intended just for the debug routine
3935 */
3936static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003937xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003938 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003939 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003940
3941 if (node == NULL) return;
3942 if (glob) strcat(buf, "(");
3943 cur = node;
3944 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003945 len = strlen(buf);
3946 if (size - len < 50) {
3947 if ((size - len > 4) && (buf[len - 1] != '.'))
3948 strcat(buf, " ...");
3949 return;
3950 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003951 switch (cur->type) {
3952 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003953 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00003954 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003955 if ((size - len > 4) && (buf[len - 1] != '.'))
3956 strcat(buf, " ...");
3957 return;
3958 }
3959 strcat(buf, (char *) cur->ns->prefix);
3960 strcat(buf, ":");
3961 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00003962 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003963 if ((size - len > 4) && (buf[len - 1] != '.'))
3964 strcat(buf, " ...");
3965 return;
3966 }
3967 strcat(buf, (char *) cur->name);
3968 if (cur->next != NULL)
3969 strcat(buf, " ");
3970 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003971 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003972 if (xmlIsBlankNode(cur))
3973 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003974 case XML_CDATA_SECTION_NODE:
3975 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003976 strcat(buf, "CDATA");
3977 if (cur->next != NULL)
3978 strcat(buf, " ");
3979 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003980 case XML_ATTRIBUTE_NODE:
3981 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003982#ifdef LIBXML_DOCB_ENABLED
3983 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003984#endif
3985 case XML_HTML_DOCUMENT_NODE:
3986 case XML_DOCUMENT_TYPE_NODE:
3987 case XML_DOCUMENT_FRAG_NODE:
3988 case XML_NOTATION_NODE:
3989 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003990 strcat(buf, "???");
3991 if (cur->next != NULL)
3992 strcat(buf, " ");
3993 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003994 case XML_ENTITY_NODE:
3995 case XML_PI_NODE:
3996 case XML_DTD_NODE:
3997 case XML_COMMENT_NODE:
3998 case XML_ELEMENT_DECL:
3999 case XML_ATTRIBUTE_DECL:
4000 case XML_ENTITY_DECL:
4001 case XML_XINCLUDE_START:
4002 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004003 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004004 }
4005 cur = cur->next;
4006 }
4007 if (glob) strcat(buf, ")");
4008}
4009
4010/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004011 * xmlValidateElementContent:
4012 * @ctxt: the validation context
4013 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004014 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004015 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004016 *
4017 * Try to validate the content model of an element
4018 *
4019 * returns 1 if valid or 0 if not and -1 in case of error
4020 */
4021
4022static int
4023xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004024 xmlElementPtr elemDecl, int warn) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004025 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004026 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004027 xmlElementContentPtr cont;
4028 const xmlChar *name;
4029
4030 if (elemDecl == NULL)
4031 return(-1);
4032 cont = elemDecl->content;
4033 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004034
4035 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004036 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004037 */
4038 ctxt->vstateMax = 8;
4039 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4040 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4041 if (ctxt->vstateTab == NULL) {
4042 xmlGenericError(xmlGenericErrorContext,
4043 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004044 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004045 }
4046 /*
4047 * The first entry in the stack is reserved to the current state
4048 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004049 ctxt->nodeMax = 0;
4050 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004051 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004052 ctxt->vstate = &ctxt->vstateTab[0];
4053 ctxt->vstateNr = 1;
4054 CONT = cont;
4055 NODE = child;
4056 DEPTH = 0;
4057 OCCURS = 0;
4058 STATE = 0;
4059 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004060 if ((ret == -3) && (warn)) {
4061 VWARNING(ctxt->userData,
4062 "Element %s content model is ambiguous\n", name);
4063 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004064 /*
4065 * An entities reference appeared at this level.
4066 * Buid a minimal representation of this node content
4067 * sufficient to run the validation process on it
4068 */
4069 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004070 cur = child;
4071 while (cur != NULL) {
4072 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004073 case XML_ENTITY_REF_NODE:
4074 /*
4075 * Push the current node to be able to roll back
4076 * and process within the entity
4077 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004078 if ((cur->children != NULL) &&
4079 (cur->children->children != NULL)) {
4080 nodeVPush(ctxt, cur);
4081 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004082 continue;
4083 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004084 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004085 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004086 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004087 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004088 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004089 case XML_CDATA_SECTION_NODE:
4090 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004091 case XML_ELEMENT_NODE:
4092 /*
4093 * Allocate a new node and minimally fills in
4094 * what's required
4095 */
4096 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4097 if (tmp == NULL) {
4098 xmlGenericError(xmlGenericErrorContext,
4099 "xmlValidateElementContent : malloc failed\n");
4100 xmlFreeNodeList(repl);
4101 ret = -1;
4102 goto done;
4103 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004104 tmp->type = cur->type;
4105 tmp->name = cur->name;
4106 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004107 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004108 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004109 if (repl == NULL)
4110 repl = last = tmp;
4111 else {
4112 last->next = tmp;
4113 last = tmp;
4114 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004115 if (cur->type == XML_CDATA_SECTION_NODE) {
4116 /*
4117 * E59 spaces in CDATA does not match the
4118 * nonterminal S
4119 */
4120 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4121 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004122 break;
4123 default:
4124 break;
4125 }
4126 /*
4127 * Switch to next element
4128 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004129 cur = cur->next;
4130 while (cur == NULL) {
4131 cur = nodeVPop(ctxt);
4132 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004133 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004134 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004135 }
4136 }
4137
4138 /*
4139 * Relaunch the validation
4140 */
4141 ctxt->vstate = &ctxt->vstateTab[0];
4142 ctxt->vstateNr = 1;
4143 CONT = cont;
4144 NODE = repl;
4145 DEPTH = 0;
4146 OCCURS = 0;
4147 STATE = 0;
4148 ret = xmlValidateElementType(ctxt);
4149 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004150 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004151 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4152 char expr[5000];
4153 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004154
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004155 expr[0] = 0;
4156 xmlSnprintfElementContent(expr, 5000, cont, 1);
4157 list[0] = 0;
4158 if (repl != NULL)
4159 xmlSnprintfElements(list, 5000, repl, 1);
4160 else
4161 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004162
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004163 if (name != NULL) {
4164 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004165 "Element %s content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004166 name, expr, list);
4167 } else {
4168 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004169 "Element content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004170 expr, list);
4171 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004172 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004173 if (name != NULL) {
4174 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004175 "Element %s content doesn't follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004176 name);
4177 } else {
4178 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004179 "Element content doesn't follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004180 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004181 }
4182 ret = 0;
4183 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004184 if (ret == -3)
4185 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004186
4187
4188done:
4189 /*
4190 * Deallocate the copy if done, and free up the validation stack
4191 */
4192 while (repl != NULL) {
4193 tmp = repl->next;
4194 xmlFree(repl);
4195 repl = tmp;
4196 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004197 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004198 if (ctxt->vstateTab != NULL) {
4199 xmlFree(ctxt->vstateTab);
4200 ctxt->vstateTab = NULL;
4201 }
4202 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004203 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004204 if (ctxt->nodeTab != NULL) {
4205 xmlFree(ctxt->nodeTab);
4206 ctxt->nodeTab = NULL;
4207 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004208 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004209
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004210}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004211
Owen Taylor3473f882001-02-23 17:55:21 +00004212/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004213 * xmlValidateCdataElement:
4214 * @ctxt: the validation context
4215 * @doc: a document instance
4216 * @elem: an element instance
4217 *
4218 * Check that an element follows #CDATA
4219 *
4220 * returns 1 if valid or 0 otherwise
4221 */
4222static int
4223xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4224 xmlNodePtr elem) {
4225 int ret = 1;
4226 xmlNodePtr cur, child;
4227
4228 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4229 return(0);
4230
4231 child = elem->children;
4232
4233 cur = child;
4234 while (cur != NULL) {
4235 switch (cur->type) {
4236 case XML_ENTITY_REF_NODE:
4237 /*
4238 * Push the current node to be able to roll back
4239 * and process within the entity
4240 */
4241 if ((cur->children != NULL) &&
4242 (cur->children->children != NULL)) {
4243 nodeVPush(ctxt, cur);
4244 cur = cur->children->children;
4245 continue;
4246 }
4247 break;
4248 case XML_COMMENT_NODE:
4249 case XML_PI_NODE:
4250 case XML_TEXT_NODE:
4251 case XML_CDATA_SECTION_NODE:
4252 break;
4253 default:
4254 ret = 0;
4255 goto done;
4256 }
4257 /*
4258 * Switch to next element
4259 */
4260 cur = cur->next;
4261 while (cur == NULL) {
4262 cur = nodeVPop(ctxt);
4263 if (cur == NULL)
4264 break;
4265 cur = cur->next;
4266 }
4267 }
4268done:
4269 ctxt->nodeMax = 0;
4270 ctxt->nodeNr = 0;
4271 if (ctxt->nodeTab != NULL) {
4272 xmlFree(ctxt->nodeTab);
4273 ctxt->nodeTab = NULL;
4274 }
4275 return(ret);
4276}
4277
4278/**
Owen Taylor3473f882001-02-23 17:55:21 +00004279 * xmlValidateOneElement:
4280 * @ctxt: the validation context
4281 * @doc: a document instance
4282 * @elem: an element instance
4283 *
4284 * Try to validate a single element and it's attributes,
4285 * basically it does the following checks as described by the
4286 * XML-1.0 recommendation:
4287 * - [ VC: Element Valid ]
4288 * - [ VC: Required Attribute ]
4289 * Then call xmlValidateOneAttribute() for each attribute present.
4290 *
4291 * The ID/IDREF checkings are done separately
4292 *
4293 * returns 1 if valid or 0 otherwise
4294 */
4295
4296int
4297xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4298 xmlNodePtr elem) {
4299 xmlElementPtr elemDecl = NULL;
4300 xmlElementContentPtr cont;
4301 xmlAttributePtr attr;
4302 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004303 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004304 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004305 const xmlChar *prefix = NULL;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004306 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004307
4308 CHECK_DTD;
4309
4310 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004311 switch (elem->type) {
4312 case XML_ATTRIBUTE_NODE:
4313 VERROR(ctxt->userData,
4314 "Attribute element not expected here\n");
4315 return(0);
4316 case XML_TEXT_NODE:
4317 if (elem->children != NULL) {
4318 VERROR(ctxt->userData, "Text element has childs !\n");
4319 return(0);
4320 }
4321 if (elem->properties != NULL) {
4322 VERROR(ctxt->userData, "Text element has attributes !\n");
4323 return(0);
4324 }
4325 if (elem->ns != NULL) {
4326 VERROR(ctxt->userData, "Text element has namespace !\n");
4327 return(0);
4328 }
4329 if (elem->nsDef != NULL) {
4330 VERROR(ctxt->userData,
4331 "Text element carries namespace definitions !\n");
4332 return(0);
4333 }
4334 if (elem->content == NULL) {
4335 VERROR(ctxt->userData,
4336 "Text element has no content !\n");
4337 return(0);
4338 }
4339 return(1);
4340 case XML_XINCLUDE_START:
4341 case XML_XINCLUDE_END:
4342 return(1);
4343 case XML_CDATA_SECTION_NODE:
4344 case XML_ENTITY_REF_NODE:
4345 case XML_PI_NODE:
4346 case XML_COMMENT_NODE:
4347 return(1);
4348 case XML_ENTITY_NODE:
4349 VERROR(ctxt->userData,
4350 "Entity element not expected here\n");
4351 return(0);
4352 case XML_NOTATION_NODE:
4353 VERROR(ctxt->userData,
4354 "Notation element not expected here\n");
4355 return(0);
4356 case XML_DOCUMENT_NODE:
4357 case XML_DOCUMENT_TYPE_NODE:
4358 case XML_DOCUMENT_FRAG_NODE:
4359 VERROR(ctxt->userData,
4360 "Document element not expected here\n");
4361 return(0);
4362 case XML_HTML_DOCUMENT_NODE:
4363 VERROR(ctxt->userData,
4364 "\n");
4365 return(0);
4366 case XML_ELEMENT_NODE:
4367 break;
4368 default:
4369 VERROR(ctxt->userData,
4370 "unknown element type %d\n", elem->type);
4371 return(0);
4372 }
4373 if (elem->name == NULL) return(0);
4374
4375 /*
4376 * Fetch the declaration for the qualified name
4377 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004378 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4379 prefix = elem->ns->prefix;
4380
4381 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004382 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004383 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004384 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004385 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004386 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004387 if (elemDecl != NULL)
4388 extsubset = 1;
4389 }
Owen Taylor3473f882001-02-23 17:55:21 +00004390 }
4391
4392 /*
4393 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004394 * This is "non-strict" validation should be done on the
4395 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004396 */
4397 if (elemDecl == NULL) {
4398 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004399 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004400 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004401 if (elemDecl != NULL)
4402 extsubset = 1;
4403 }
Owen Taylor3473f882001-02-23 17:55:21 +00004404 }
4405 if (elemDecl == NULL) {
4406 VERROR(ctxt->userData, "No declaration for element %s\n",
4407 elem->name);
4408 return(0);
4409 }
4410
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004411 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00004412 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004413 case XML_ELEMENT_TYPE_UNDEFINED:
4414 VERROR(ctxt->userData, "No declaration for element %s\n",
4415 elem->name);
4416 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004417 case XML_ELEMENT_TYPE_EMPTY:
4418 if (elem->children != NULL) {
4419 VERROR(ctxt->userData,
4420 "Element %s was declared EMPTY this one has content\n",
4421 elem->name);
4422 ret = 0;
4423 }
4424 break;
4425 case XML_ELEMENT_TYPE_ANY:
4426 /* I don't think anything is required then */
4427 break;
4428 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004429
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004430 /* simple case of declared as #PCDATA */
4431 if ((elemDecl->content != NULL) &&
4432 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4433 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4434 if (!ret) {
4435 VERROR(ctxt->userData,
4436 "Element %s was declared #PCDATA but contains non text nodes\n",
4437 elem->name);
4438 }
4439 break;
4440 }
Owen Taylor3473f882001-02-23 17:55:21 +00004441 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004442 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004443 while (child != NULL) {
4444 if (child->type == XML_ELEMENT_NODE) {
4445 name = child->name;
4446 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4447 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004448 snprintf((char *) qname, sizeof(qname), "%s:%s",
4449 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004450 qname[sizeof(qname) - 1] = 0;
4451 cont = elemDecl->content;
4452 while (cont != NULL) {
4453 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4454 if (xmlStrEqual(cont->name, qname)) break;
4455 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4456 (cont->c1 != NULL) &&
4457 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4458 if (xmlStrEqual(cont->c1->name, qname)) break;
4459 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4460 (cont->c1 == NULL) ||
4461 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4462 /* Internal error !!! */
4463 xmlGenericError(xmlGenericErrorContext,
4464 "Internal: MIXED struct bad\n");
4465 break;
4466 }
4467 cont = cont->c2;
4468 }
4469 if (cont != NULL)
4470 goto child_ok;
4471 }
4472 cont = elemDecl->content;
4473 while (cont != NULL) {
4474 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4475 if (xmlStrEqual(cont->name, name)) break;
4476 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4477 (cont->c1 != NULL) &&
4478 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4479 if (xmlStrEqual(cont->c1->name, name)) break;
4480 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4481 (cont->c1 == NULL) ||
4482 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4483 /* Internal error !!! */
4484 xmlGenericError(xmlGenericErrorContext,
4485 "Internal: MIXED struct bad\n");
4486 break;
4487 }
4488 cont = cont->c2;
4489 }
4490 if (cont == NULL) {
4491 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00004492 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004493 name, elem->name);
4494 ret = 0;
4495 }
4496 }
4497child_ok:
4498 child = child->next;
4499 }
4500 break;
4501 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004502 if ((doc->standalone == 1) && (extsubset == 1)) {
4503 /*
4504 * VC: Standalone Document Declaration
4505 * - element types with element content, if white space
4506 * occurs directly within any instance of those types.
4507 */
4508 child = elem->children;
4509 while (child != NULL) {
4510 if (child->type == XML_TEXT_NODE) {
4511 const xmlChar *content = child->content;
4512
4513 while (IS_BLANK(*content))
4514 content++;
4515 if (*content == 0) {
4516 VERROR(ctxt->userData,
4517"standalone: %s declared in the external subset contains white spaces nodes\n",
4518 elem->name);
4519 ret = 0;
4520 break;
4521 }
4522 }
4523 child =child->next;
4524 }
4525 }
Owen Taylor3473f882001-02-23 17:55:21 +00004526 child = elem->children;
4527 cont = elemDecl->content;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004528 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1);
4529 if (tmp <= 0)
4530 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004531 break;
4532 }
4533
4534 /* [ VC: Required Attribute ] */
4535 attr = elemDecl->attributes;
4536 while (attr != NULL) {
4537 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004538 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00004539
Daniel Veillarde4301c82002-02-13 13:32:35 +00004540 if ((attr->prefix == NULL) &&
4541 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4542 xmlNsPtr ns;
4543
4544 ns = elem->nsDef;
4545 while (ns != NULL) {
4546 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004547 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004548 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00004549 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004550 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4551 xmlNsPtr ns;
4552
4553 ns = elem->nsDef;
4554 while (ns != NULL) {
4555 if (xmlStrEqual(attr->name, ns->prefix))
4556 goto found;
4557 ns = ns->next;
4558 }
4559 } else {
4560 xmlAttrPtr attrib;
4561
4562 attrib = elem->properties;
4563 while (attrib != NULL) {
4564 if (xmlStrEqual(attrib->name, attr->name)) {
4565 if (attr->prefix != NULL) {
4566 xmlNsPtr nameSpace = attrib->ns;
4567
4568 if (nameSpace == NULL)
4569 nameSpace = elem->ns;
4570 /*
4571 * qualified names handling is problematic, having a
4572 * different prefix should be possible but DTDs don't
4573 * allow to define the URI instead of the prefix :-(
4574 */
4575 if (nameSpace == NULL) {
4576 if (qualified < 0)
4577 qualified = 0;
4578 } else if (!xmlStrEqual(nameSpace->prefix,
4579 attr->prefix)) {
4580 if (qualified < 1)
4581 qualified = 1;
4582 } else
4583 goto found;
4584 } else {
4585 /*
4586 * We should allow applications to define namespaces
4587 * for their application even if the DTD doesn't
4588 * carry one, otherwise, basically we would always
4589 * break.
4590 */
4591 goto found;
4592 }
4593 }
4594 attrib = attrib->next;
4595 }
Owen Taylor3473f882001-02-23 17:55:21 +00004596 }
4597 if (qualified == -1) {
4598 if (attr->prefix == NULL) {
4599 VERROR(ctxt->userData,
4600 "Element %s doesn't carry attribute %s\n",
4601 elem->name, attr->name);
4602 ret = 0;
4603 } else {
4604 VERROR(ctxt->userData,
4605 "Element %s doesn't carry attribute %s:%s\n",
4606 elem->name, attr->prefix,attr->name);
4607 ret = 0;
4608 }
4609 } else if (qualified == 0) {
4610 VWARNING(ctxt->userData,
4611 "Element %s required attribute %s:%s has no prefix\n",
4612 elem->name, attr->prefix,attr->name);
4613 } else if (qualified == 1) {
4614 VWARNING(ctxt->userData,
4615 "Element %s required attribute %s:%s has different prefix\n",
4616 elem->name, attr->prefix,attr->name);
4617 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004618 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
4619 /*
4620 * Special tests checking #FIXED namespace declarations
4621 * have the right value since this is not done as an
4622 * attribute checking
4623 */
4624 if ((attr->prefix == NULL) &&
4625 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4626 xmlNsPtr ns;
4627
4628 ns = elem->nsDef;
4629 while (ns != NULL) {
4630 if (ns->prefix == NULL) {
4631 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4632 VERROR(ctxt->userData,
4633 "Element %s namespace name for default namespace does not match the DTD\n",
4634 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00004635 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004636 }
4637 goto found;
4638 }
4639 ns = ns->next;
4640 }
4641 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4642 xmlNsPtr ns;
4643
4644 ns = elem->nsDef;
4645 while (ns != NULL) {
4646 if (xmlStrEqual(attr->name, ns->prefix)) {
4647 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4648 VERROR(ctxt->userData,
4649 "Element %s namespace name for %s doesn't match the DTD\n",
4650 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00004651 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004652 }
4653 goto found;
4654 }
4655 ns = ns->next;
4656 }
4657 }
Owen Taylor3473f882001-02-23 17:55:21 +00004658 }
4659found:
4660 attr = attr->nexth;
4661 }
4662 return(ret);
4663}
4664
4665/**
4666 * xmlValidateRoot:
4667 * @ctxt: the validation context
4668 * @doc: a document instance
4669 *
4670 * Try to validate a the root element
4671 * basically it does the following check as described by the
4672 * XML-1.0 recommendation:
4673 * - [ VC: Root Element Type ]
4674 * it doesn't try to recurse or apply other check to the element
4675 *
4676 * returns 1 if valid or 0 otherwise
4677 */
4678
4679int
4680xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4681 xmlNodePtr root;
4682 if (doc == NULL) return(0);
4683
4684 root = xmlDocGetRootElement(doc);
4685 if ((root == NULL) || (root->name == NULL)) {
4686 VERROR(ctxt->userData, "Not valid: no root element\n");
4687 return(0);
4688 }
4689
4690 /*
4691 * When doing post validation against a separate DTD, those may
4692 * no internal subset has been generated
4693 */
4694 if ((doc->intSubset != NULL) &&
4695 (doc->intSubset->name != NULL)) {
4696 /*
4697 * Check first the document root against the NQName
4698 */
4699 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4700 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4701 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004702 snprintf((char *) qname, sizeof(qname), "%s:%s",
4703 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004704 qname[sizeof(qname) - 1] = 0;
4705 if (xmlStrEqual(doc->intSubset->name, qname))
4706 goto name_ok;
4707 }
4708 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4709 (xmlStrEqual(root->name, BAD_CAST "html")))
4710 goto name_ok;
4711 VERROR(ctxt->userData,
4712 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4713 root->name, doc->intSubset->name);
4714 return(0);
4715
4716 }
4717 }
4718name_ok:
4719 return(1);
4720}
4721
4722
4723/**
4724 * xmlValidateElement:
4725 * @ctxt: the validation context
4726 * @doc: a document instance
4727 * @elem: an element instance
4728 *
4729 * Try to validate the subtree under an element
4730 *
4731 * returns 1 if valid or 0 otherwise
4732 */
4733
4734int
4735xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4736 xmlNodePtr child;
4737 xmlAttrPtr attr;
4738 xmlChar *value;
4739 int ret = 1;
4740
4741 if (elem == NULL) return(0);
4742
4743 /*
4744 * XInclude elements were added after parsing in the infoset,
4745 * they don't really mean anything validation wise.
4746 */
4747 if ((elem->type == XML_XINCLUDE_START) ||
4748 (elem->type == XML_XINCLUDE_END))
4749 return(1);
4750
4751 CHECK_DTD;
4752
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004753 /*
4754 * Entities references have to be handled separately
4755 */
4756 if (elem->type == XML_ENTITY_REF_NODE) {
4757 return(1);
4758 }
4759
Owen Taylor3473f882001-02-23 17:55:21 +00004760 ret &= xmlValidateOneElement(ctxt, doc, elem);
4761 attr = elem->properties;
4762 while(attr != NULL) {
4763 value = xmlNodeListGetString(doc, attr->children, 0);
4764 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4765 if (value != NULL)
4766 xmlFree(value);
4767 attr= attr->next;
4768 }
4769 child = elem->children;
4770 while (child != NULL) {
4771 ret &= xmlValidateElement(ctxt, doc, child);
4772 child = child->next;
4773 }
4774
4775 return(ret);
4776}
4777
Daniel Veillard8730c562001-02-26 10:49:57 +00004778/**
4779 * xmlValidateRef:
4780 * @ref: A reference to be validated
4781 * @ctxt: Validation context
4782 * @name: Name of ID we are searching for
4783 *
4784 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004785static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004786xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004787 const xmlChar *name) {
4788 xmlAttrPtr id;
4789 xmlAttrPtr attr;
4790
4791 if (ref == NULL)
4792 return;
4793 attr = ref->attr;
4794 if (attr == NULL)
4795 return;
4796 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4797 id = xmlGetID(ctxt->doc, name);
4798 if (id == NULL) {
4799 VERROR(ctxt->userData,
4800 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4801 attr->name, name);
4802 ctxt->valid = 0;
4803 }
4804 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4805 xmlChar *dup, *str = NULL, *cur, save;
4806
4807 dup = xmlStrdup(name);
4808 if (dup == NULL) {
4809 ctxt->valid = 0;
4810 return;
4811 }
4812 cur = dup;
4813 while (*cur != 0) {
4814 str = cur;
4815 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4816 save = *cur;
4817 *cur = 0;
4818 id = xmlGetID(ctxt->doc, str);
4819 if (id == NULL) {
4820 VERROR(ctxt->userData,
4821 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4822 attr->name, str);
4823 ctxt->valid = 0;
4824 }
4825 if (save == 0)
4826 break;
4827 *cur = save;
4828 while (IS_BLANK(*cur)) cur++;
4829 }
4830 xmlFree(dup);
4831 }
4832}
4833
4834/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004835 * xmlWalkValidateList:
4836 * @data: Contents of current link
4837 * @user: Value supplied by the user
4838 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004839 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00004840 */
4841static int
4842xmlWalkValidateList(const void *data, const void *user)
4843{
4844 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4845 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4846 return 1;
4847}
4848
4849/**
4850 * xmlValidateCheckRefCallback:
4851 * @ref_list: List of references
4852 * @ctxt: Validation context
4853 * @name: Name of ID we are searching for
4854 *
4855 */
4856static void
4857xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4858 const xmlChar *name) {
4859 xmlValidateMemo memo;
4860
4861 if (ref_list == NULL)
4862 return;
4863 memo.ctxt = ctxt;
4864 memo.name = name;
4865
4866 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4867
4868}
4869
4870/**
Owen Taylor3473f882001-02-23 17:55:21 +00004871 * xmlValidateDocumentFinal:
4872 * @ctxt: the validation context
4873 * @doc: a document instance
4874 *
4875 * Does the final step for the document validation once all the
4876 * incremental validation steps have been completed
4877 *
4878 * basically it does the following checks described by the XML Rec
4879 *
4880 *
4881 * returns 1 if valid or 0 otherwise
4882 */
4883
4884int
4885xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4886 xmlRefTablePtr table;
4887
4888 if (doc == NULL) {
4889 xmlGenericError(xmlGenericErrorContext,
4890 "xmlValidateDocumentFinal: doc == NULL\n");
4891 return(0);
4892 }
4893
4894 /*
4895 * Check all the NOTATION/NOTATIONS attributes
4896 */
4897 /*
4898 * Check all the ENTITY/ENTITIES attributes definition for validity
4899 */
4900 /*
4901 * Check all the IDREF/IDREFS attributes definition for validity
4902 */
4903 table = (xmlRefTablePtr) doc->refs;
4904 ctxt->doc = doc;
4905 ctxt->valid = 1;
4906 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4907 return(ctxt->valid);
4908}
4909
4910/**
4911 * xmlValidateDtd:
4912 * @ctxt: the validation context
4913 * @doc: a document instance
4914 * @dtd: a dtd instance
4915 *
4916 * Try to validate the document against the dtd instance
4917 *
4918 * basically it does check all the definitions in the DtD.
4919 *
4920 * returns 1 if valid or 0 otherwise
4921 */
4922
4923int
4924xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4925 int ret;
4926 xmlDtdPtr oldExt;
4927 xmlNodePtr root;
4928
4929 if (dtd == NULL) return(0);
4930 if (doc == NULL) return(0);
4931 oldExt = doc->extSubset;
4932 doc->extSubset = dtd;
4933 ret = xmlValidateRoot(ctxt, doc);
4934 if (ret == 0) {
4935 doc->extSubset = oldExt;
4936 return(ret);
4937 }
4938 if (doc->ids != NULL) {
4939 xmlFreeIDTable(doc->ids);
4940 doc->ids = NULL;
4941 }
4942 if (doc->refs != NULL) {
4943 xmlFreeRefTable(doc->refs);
4944 doc->refs = NULL;
4945 }
4946 root = xmlDocGetRootElement(doc);
4947 ret = xmlValidateElement(ctxt, doc, root);
4948 ret &= xmlValidateDocumentFinal(ctxt, doc);
4949 doc->extSubset = oldExt;
4950 return(ret);
4951}
4952
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004953static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004954xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
4955 const xmlChar *name ATTRIBUTE_UNUSED) {
4956 if (cur == NULL)
4957 return;
4958 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
4959 xmlChar *notation = cur->content;
4960
Daniel Veillard878eab02002-02-19 13:46:09 +00004961 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004962 int ret;
4963
4964 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
4965 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00004966 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004967 }
4968 }
4969 }
4970}
4971
4972static void
Owen Taylor3473f882001-02-23 17:55:21 +00004973xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004974 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004975 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00004976 xmlDocPtr doc;
4977 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004978
Owen Taylor3473f882001-02-23 17:55:21 +00004979 if (cur == NULL)
4980 return;
4981 switch (cur->atype) {
4982 case XML_ATTRIBUTE_CDATA:
4983 case XML_ATTRIBUTE_ID:
4984 case XML_ATTRIBUTE_IDREF :
4985 case XML_ATTRIBUTE_IDREFS:
4986 case XML_ATTRIBUTE_NMTOKEN:
4987 case XML_ATTRIBUTE_NMTOKENS:
4988 case XML_ATTRIBUTE_ENUMERATION:
4989 break;
4990 case XML_ATTRIBUTE_ENTITY:
4991 case XML_ATTRIBUTE_ENTITIES:
4992 case XML_ATTRIBUTE_NOTATION:
4993 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004994
4995 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
4996 cur->atype, cur->defaultValue);
4997 if ((ret == 0) && (ctxt->valid == 1))
4998 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004999 }
5000 if (cur->tree != NULL) {
5001 xmlEnumerationPtr tree = cur->tree;
5002 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005003 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00005004 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005005 if ((ret == 0) && (ctxt->valid == 1))
5006 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005007 tree = tree->next;
5008 }
5009 }
5010 }
Daniel Veillard878eab02002-02-19 13:46:09 +00005011 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
5012 doc = cur->doc;
5013 if ((doc == NULL) || (cur->elem == NULL)) {
5014 VERROR(ctxt->userData,
5015 "xmlValidateAttributeCallback(%s): internal error\n",
5016 cur->name);
5017 return;
5018 }
5019 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
5020 if (elem == NULL)
5021 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
5022 if (elem == NULL) {
5023 VERROR(ctxt->userData,
5024 "attribute %s: could not find decl for element %s\n",
5025 cur->name, cur->elem);
5026 return;
5027 }
5028 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
5029 VERROR(ctxt->userData,
5030 "NOTATION attribute %s declared on EMPTY element %s\n",
5031 cur->name, cur->elem);
5032 ctxt->valid = 0;
5033 }
5034 }
Owen Taylor3473f882001-02-23 17:55:21 +00005035}
5036
5037/**
5038 * xmlValidateDtdFinal:
5039 * @ctxt: the validation context
5040 * @doc: a document instance
5041 *
5042 * Does the final step for the dtds validation once all the
5043 * subsets have been parsed
5044 *
5045 * basically it does the following checks described by the XML Rec
5046 * - check that ENTITY and ENTITIES type attributes default or
5047 * possible values matches one of the defined entities.
5048 * - check that NOTATION type attributes default or
5049 * possible values matches one of the defined notations.
5050 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005051 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00005052 */
5053
5054int
5055xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00005056 xmlDtdPtr dtd;
5057 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005058 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00005059
5060 if (doc == NULL) return(0);
5061 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5062 return(0);
5063 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005064 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005065 dtd = doc->intSubset;
5066 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5067 table = (xmlAttributeTablePtr) dtd->attributes;
5068 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005069 }
5070 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005071 entities = (xmlEntitiesTablePtr) dtd->entities;
5072 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5073 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005074 }
5075 dtd = doc->extSubset;
5076 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5077 table = (xmlAttributeTablePtr) dtd->attributes;
5078 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005079 }
5080 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005081 entities = (xmlEntitiesTablePtr) dtd->entities;
5082 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5083 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005084 }
5085 return(ctxt->valid);
5086}
5087
5088/**
5089 * xmlValidateDocument:
5090 * @ctxt: the validation context
5091 * @doc: a document instance
5092 *
5093 * Try to validate the document instance
5094 *
5095 * basically it does the all the checks described by the XML Rec
5096 * i.e. validates the internal and external subset (if present)
5097 * and validate the document tree.
5098 *
5099 * returns 1 if valid or 0 otherwise
5100 */
5101
5102int
5103xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5104 int ret;
5105 xmlNodePtr root;
5106
5107 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5108 return(0);
5109 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
5110 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
5111 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
5112 doc->intSubset->SystemID);
5113 if (doc->extSubset == NULL) {
5114 if (doc->intSubset->SystemID != NULL) {
5115 VERROR(ctxt->userData,
5116 "Could not load the external subset \"%s\"\n",
5117 doc->intSubset->SystemID);
5118 } else {
5119 VERROR(ctxt->userData,
5120 "Could not load the external subset \"%s\"\n",
5121 doc->intSubset->ExternalID);
5122 }
5123 return(0);
5124 }
5125 }
5126
5127 if (doc->ids != NULL) {
5128 xmlFreeIDTable(doc->ids);
5129 doc->ids = NULL;
5130 }
5131 if (doc->refs != NULL) {
5132 xmlFreeRefTable(doc->refs);
5133 doc->refs = NULL;
5134 }
5135 ret = xmlValidateDtdFinal(ctxt, doc);
5136 if (!xmlValidateRoot(ctxt, doc)) return(0);
5137
5138 root = xmlDocGetRootElement(doc);
5139 ret &= xmlValidateElement(ctxt, doc, root);
5140 ret &= xmlValidateDocumentFinal(ctxt, doc);
5141 return(ret);
5142}
5143
5144
5145/************************************************************************
5146 * *
5147 * Routines for dynamic validation editing *
5148 * *
5149 ************************************************************************/
5150
5151/**
5152 * xmlValidGetPotentialChildren:
5153 * @ctree: an element content tree
5154 * @list: an array to store the list of child names
5155 * @len: a pointer to the number of element in the list
5156 * @max: the size of the array
5157 *
5158 * Build/extend a list of potential children allowed by the content tree
5159 *
5160 * returns the number of element in the list, or -1 in case of error.
5161 */
5162
5163int
5164xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
5165 int *len, int max) {
5166 int i;
5167
5168 if ((ctree == NULL) || (list == NULL) || (len == NULL))
5169 return(-1);
5170 if (*len >= max) return(*len);
5171
5172 switch (ctree->type) {
5173 case XML_ELEMENT_CONTENT_PCDATA:
5174 for (i = 0; i < *len;i++)
5175 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
5176 list[(*len)++] = BAD_CAST "#PCDATA";
5177 break;
5178 case XML_ELEMENT_CONTENT_ELEMENT:
5179 for (i = 0; i < *len;i++)
5180 if (xmlStrEqual(ctree->name, list[i])) return(*len);
5181 list[(*len)++] = ctree->name;
5182 break;
5183 case XML_ELEMENT_CONTENT_SEQ:
5184 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5185 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5186 break;
5187 case XML_ELEMENT_CONTENT_OR:
5188 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5189 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5190 break;
5191 }
5192
5193 return(*len);
5194}
5195
5196/**
5197 * xmlValidGetValidElements:
5198 * @prev: an element to insert after
5199 * @next: an element to insert next
5200 * @list: an array to store the list of child names
5201 * @max: the size of the array
5202 *
5203 * This function returns the list of authorized children to insert
5204 * within an existing tree while respecting the validity constraints
5205 * forced by the Dtd. The insertion point is defined using @prev and
5206 * @next in the following ways:
5207 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
5208 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
5209 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
5210 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
5211 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
5212 *
5213 * pointers to the element names are inserted at the beginning of the array
5214 * and do not need to be freed.
5215 *
5216 * returns the number of element in the list, or -1 in case of error. If
5217 * the function returns the value @max the caller is invited to grow the
5218 * receiving array and retry.
5219 */
5220
5221int
5222xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
5223 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005224 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00005225 int nb_valid_elements = 0;
5226 const xmlChar *elements[256];
5227 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005228 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005229
5230 xmlNode *ref_node;
5231 xmlNode *parent;
5232 xmlNode *test_node;
5233
5234 xmlNode *prev_next;
5235 xmlNode *next_prev;
5236 xmlNode *parent_childs;
5237 xmlNode *parent_last;
5238
5239 xmlElement *element_desc;
5240
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00005241 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005242
Owen Taylor3473f882001-02-23 17:55:21 +00005243 if (prev == NULL && next == NULL)
5244 return(-1);
5245
5246 if (list == NULL) return(-1);
5247 if (max <= 0) return(-1);
5248
5249 nb_valid_elements = 0;
5250 ref_node = prev ? prev : next;
5251 parent = ref_node->parent;
5252
5253 /*
5254 * Retrieves the parent element declaration
5255 */
5256 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
5257 parent->name);
5258 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
5259 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
5260 parent->name);
5261 if (element_desc == NULL) return(-1);
5262
5263 /*
5264 * Do a backup of the current tree structure
5265 */
5266 prev_next = prev ? prev->next : NULL;
5267 next_prev = next ? next->prev : NULL;
5268 parent_childs = parent->children;
5269 parent_last = parent->last;
5270
5271 /*
5272 * Creates a dummy node and insert it into the tree
5273 */
5274 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
5275 test_node->doc = ref_node->doc;
5276 test_node->parent = parent;
5277 test_node->prev = prev;
5278 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00005279 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00005280
5281 if (prev) prev->next = test_node;
5282 else parent->children = test_node;
5283
5284 if (next) next->prev = test_node;
5285 else parent->last = test_node;
5286
5287 /*
5288 * Insert each potential child node and check if the parent is
5289 * still valid
5290 */
5291 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
5292 elements, &nb_elements, 256);
5293
5294 for (i = 0;i < nb_elements;i++) {
5295 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005296 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005297 int j;
5298
5299 for (j = 0; j < nb_valid_elements;j++)
5300 if (xmlStrEqual(elements[i], list[j])) break;
5301 list[nb_valid_elements++] = elements[i];
5302 if (nb_valid_elements >= max) break;
5303 }
5304 }
5305
5306 /*
5307 * Restore the tree structure
5308 */
5309 if (prev) prev->next = prev_next;
5310 if (next) next->prev = next_prev;
5311 parent->children = parent_childs;
5312 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00005313
5314 /*
5315 * Free up the dummy node
5316 */
5317 test_node->name = name;
5318 xmlFreeNode(test_node);
5319
Owen Taylor3473f882001-02-23 17:55:21 +00005320 return(nb_valid_elements);
5321}