blob: 34f93df2139d5db194fd4209d5e19b362e8ecd40 [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 Veillard4432df22003-09-28 18:58:27 +000028static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
29 int create);
30#ifdef LIBXML_VALID_ENABLED
31
Daniel Veillarde62d36c2001-05-15 08:53:16 +000032/* #define DEBUG_VALID_ALGO */
Daniel Veillard5acfd6b2002-09-18 16:29:02 +000033/* #define DEBUG_REGEXP_ALGO */
Daniel Veillarde62d36c2001-05-15 08:53:16 +000034
Daniel Veillarda646cfd2002-09-17 21:50:03 +000035#define TODO \
36 xmlGenericError(xmlGenericErrorContext, \
37 "Unimplemented block at %s:%d\n", \
38 __FILE__, __LINE__);
Owen Taylor3473f882001-02-23 17:55:21 +000039
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000040/************************************************************************
41 * *
42 * Error handling routines *
43 * *
44 ************************************************************************/
45
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000046/**
Daniel Veillardce9457f2003-10-05 21:33:18 +000047 * xmlVErrMemory:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000048 * @ctxt: an XML validation parser context
49 * @extra: extra informations
50 *
51 * Handle an out of memory error
52 */
53static void
Daniel Veillardce9457f2003-10-05 21:33:18 +000054xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000055{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000056 xmlGenericErrorFunc channel = NULL;
57 xmlParserCtxtPtr pctxt = NULL;
58 void *data = NULL;
59
60 if (ctxt != NULL) {
61 channel = ctxt->error;
62 data = ctxt->userData;
63 pctxt = ctxt->userData;
64 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000065 if (extra)
Daniel Veillardbb5abab2003-10-03 22:21:51 +000066 __xmlRaiseError(channel, data,
67 pctxt, NULL, XML_FROM_DTD, XML_ERR_NO_MEMORY,
68 XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
69 "Memory allocation failed : %s\n", extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000070 else
Daniel Veillardbb5abab2003-10-03 22:21:51 +000071 __xmlRaiseError(channel, data,
72 pctxt, NULL, XML_FROM_DTD, XML_ERR_NO_MEMORY,
73 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
74 "Memory allocation failed\n");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000075}
76
77/**
78 * xmlErrValid:
79 * @ctxt: an XML validation parser context
Daniel Veillardbb5abab2003-10-03 22:21:51 +000080 * @error: the error number
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000081 * @extra: extra informations
82 *
83 * Handle a validation error
84 */
85static void
86xmlErrValid(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000087 const char *msg, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000088{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000089 xmlGenericErrorFunc channel = NULL;
90 xmlParserCtxtPtr pctxt = NULL;
91 void *data = NULL;
92
93 if (ctxt != NULL) {
94 channel = ctxt->error;
95 data = ctxt->userData;
96 pctxt = ctxt->userData;
97 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000098 if (extra)
Daniel Veillardbb5abab2003-10-03 22:21:51 +000099 __xmlRaiseError(channel, data,
100 pctxt, NULL, XML_FROM_DTD, error,
101 XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
102 msg, extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000103 else
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000104 __xmlRaiseError(channel, data,
105 pctxt, NULL, XML_FROM_DTD, error,
106 XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
107 msg);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000108}
109
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000110/**
111 * xmlErrValidNodeNr:
112 * @ctxt: an XML validation parser context
113 * @node: the node raising the error
114 * @error: the error number
115 * @str1: extra informations
116 * @int2: extra informations
117 * @str3: extra informations
118 *
119 * Handle a validation error, provide contextual informations
120 */
121static void
122xmlErrValidNodeNr(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
123 xmlNodePtr node, xmlParserErrors error,
124 const char *msg, const xmlChar * str1,
125 int int2, const xmlChar * str3)
126{
127 xmlGenericErrorFunc channel = NULL;
128 xmlParserCtxtPtr pctxt = NULL;
129 void *data = NULL;
130
131 if (ctxt != NULL) {
132 channel = ctxt->error;
133 data = ctxt->userData;
134 pctxt = ctxt->userData;
135 }
136 __xmlRaiseError(channel, data, pctxt, node, XML_FROM_DTD, error,
137 XML_ERR_ERROR, NULL, 0,
138 (const char *) str1,
139 (const char *) str3,
140 NULL, int2, 0, msg, str1, int2, str3);
141}
142/**
143 * xmlErrValidNode:
144 * @ctxt: an XML validation parser context
145 * @node: the node raising the error
146 * @error: the error number
147 * @str1: extra informations
148 * @str2: extra informations
149 * @str3: extra informations
150 *
151 * Handle a validation error, provide contextual informations
152 */
153static void
154xmlErrValidNode(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
155 xmlNodePtr node, xmlParserErrors error,
156 const char *msg, const xmlChar * str1,
157 const xmlChar * str2, const xmlChar * str3)
158{
159 xmlGenericErrorFunc channel = NULL;
160 xmlParserCtxtPtr pctxt = NULL;
161 void *data = NULL;
162
163 if (ctxt != NULL) {
164 channel = ctxt->error;
165 data = ctxt->userData;
166 pctxt = ctxt->userData;
167 }
168 __xmlRaiseError(channel, data, pctxt, node, XML_FROM_DTD, error,
169 XML_ERR_ERROR, NULL, 0,
170 (const char *) str1,
171 (const char *) str1,
172 (const char *) str3, 0, 0, msg, str1, str2, str3);
173}
174/**
175 * xmlErrValidWarning:
176 * @ctxt: an XML validation parser context
177 * @node: the node raising the error
178 * @error: the error number
179 * @str1: extra informations
180 * @str2: extra informations
181 * @str3: extra informations
182 *
183 * Handle a validation error, provide contextual informations
184 */
185static void
186xmlErrValidWarning(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
187 xmlNodePtr node, xmlParserErrors error,
188 const char *msg, const xmlChar * str1,
189 const xmlChar * str2, const xmlChar * str3)
190{
191 xmlGenericErrorFunc channel = NULL;
192 xmlParserCtxtPtr pctxt = NULL;
193 void *data = NULL;
194
195 if (ctxt != NULL) {
196 channel = ctxt->error;
197 data = ctxt->userData;
198 pctxt = ctxt->userData;
199 }
200 __xmlRaiseError(channel, data, pctxt, node, XML_FROM_DTD, error,
201 XML_ERR_WARNING, NULL, 0,
202 (const char *) str1,
203 (const char *) str1,
204 (const char *) str3, 0, 0, msg, str1, str2, str3);
205}
206
207
Daniel Veillardea7751d2002-12-20 00:16:24 +0000208
209#ifdef LIBXML_REGEXP_ENABLED
210/*
211 * If regexp are enabled we can do continuous validation without the
212 * need of a tree to validate the content model. this is done in each
213 * callbacks.
214 * Each xmlValidState represent the validation state associated to the
215 * set of nodes currently open from the document root to the current element.
216 */
217
218
219typedef struct _xmlValidState {
220 xmlElementPtr elemDecl; /* pointer to the content model */
221 xmlNodePtr node; /* pointer to the current node */
222 xmlRegExecCtxtPtr exec; /* regexp runtime */
223} _xmlValidState;
224
225
226static int
227vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000228 if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000229 ctxt->vstateMax = 10;
230 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
231 sizeof(ctxt->vstateTab[0]));
232 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000233 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000234 return(-1);
235 }
236 }
237
238 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000239 xmlValidState *tmp;
240
241 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
242 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
243 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000244 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000245 return(-1);
246 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000247 ctxt->vstateMax *= 2;
248 ctxt->vstateTab = tmp;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000249 }
250 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
251 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
252 ctxt->vstateTab[ctxt->vstateNr].node = node;
253 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
254 if (elemDecl->contModel == NULL)
255 xmlValidBuildContentModel(ctxt, elemDecl);
256 if (elemDecl->contModel != NULL) {
257 ctxt->vstateTab[ctxt->vstateNr].exec =
258 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
259 } else {
260 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000261 xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
262 XML_ERR_INTERNAL_ERROR,
263 "Failed to build content model regexp for %s\n",
264 node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000265 }
266 }
267 return(ctxt->vstateNr++);
268}
269
270static int
271vstateVPop(xmlValidCtxtPtr ctxt) {
272 xmlElementPtr elemDecl;
273
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000274 if (ctxt->vstateNr < 1) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000275 ctxt->vstateNr--;
276 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
277 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
278 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
279 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
280 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
281 }
282 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
283 if (ctxt->vstateNr >= 1)
284 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
285 else
286 ctxt->vstate = NULL;
287 return(ctxt->vstateNr);
288}
289
290#else /* not LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000291/*
Daniel Veillard1c732d22002-11-30 11:22:59 +0000292 * If regexp are not enabled, it uses a home made algorithm less
293 * complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000294 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000295 * only restriction is on the deepness of the tree limited by the
296 * size of the occurs bitfield
297 *
298 * this is the content of a saved state for rollbacks
299 */
300
301#define ROLLBACK_OR 0
302#define ROLLBACK_PARENT 1
303
Daniel Veillardb44025c2001-10-11 22:55:55 +0000304typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000305 xmlElementContentPtr cont; /* pointer to the content model subtree */
306 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000307 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000308 unsigned char depth; /* current depth in the overall tree */
309 unsigned char state; /* ROLLBACK_XXX */
310} _xmlValidState;
311
Daniel Veillardfc57b412002-04-29 15:50:14 +0000312#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000313#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
314#define CONT ctxt->vstate->cont
315#define NODE ctxt->vstate->node
316#define DEPTH ctxt->vstate->depth
317#define OCCURS ctxt->vstate->occurs
318#define STATE ctxt->vstate->state
319
Daniel Veillard5344c602001-12-31 16:37:34 +0000320#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
321#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000322
Daniel Veillard5344c602001-12-31 16:37:34 +0000323#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
324#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000325
326static int
327vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
328 xmlNodePtr node, unsigned char depth, long occurs,
329 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000330 int i = ctxt->vstateNr - 1;
331
Daniel Veillard940492d2002-04-15 10:15:25 +0000332 if (ctxt->vstateNr > MAX_RECURSE) {
333 return(-1);
334 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000335 if (ctxt->vstateTab == NULL) {
336 ctxt->vstateMax = 8;
337 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
338 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
339 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000340 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000341 return(-1);
342 }
343 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000344 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000345 xmlValidState *tmp;
346
347 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
348 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
349 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000350 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard940492d2002-04-15 10:15:25 +0000351 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000352 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000353 ctxt->vstateMax *= 2;
354 ctxt->vstateTab = tmp;
Daniel Veillard06803992001-04-22 10:35:56 +0000355 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000356 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000357 /*
358 * Don't push on the stack a state already here
359 */
360 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
361 (ctxt->vstateTab[i].node == node) &&
362 (ctxt->vstateTab[i].depth == depth) &&
363 (ctxt->vstateTab[i].occurs == occurs) &&
364 (ctxt->vstateTab[i].state == state))
365 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000366 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
367 ctxt->vstateTab[ctxt->vstateNr].node = node;
368 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
369 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
370 ctxt->vstateTab[ctxt->vstateNr].state = state;
371 return(ctxt->vstateNr++);
372}
373
374static int
375vstateVPop(xmlValidCtxtPtr ctxt) {
376 if (ctxt->vstateNr <= 1) return(-1);
377 ctxt->vstateNr--;
378 ctxt->vstate = &ctxt->vstateTab[0];
379 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
380 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
381 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
382 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
383 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
384 return(ctxt->vstateNr);
385}
386
Daniel Veillard118aed72002-09-24 14:13:13 +0000387#endif /* LIBXML_REGEXP_ENABLED */
388
Daniel Veillard1c732d22002-11-30 11:22:59 +0000389static int
390nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
391{
392 if (ctxt->nodeMax <= 0) {
393 ctxt->nodeMax = 4;
394 ctxt->nodeTab =
395 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
396 sizeof(ctxt->nodeTab[0]));
397 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000398 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000399 ctxt->nodeMax = 0;
400 return (0);
401 }
402 }
403 if (ctxt->nodeNr >= ctxt->nodeMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000404 xmlNodePtr *tmp;
405 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
406 ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
407 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000408 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000409 return (0);
410 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000411 ctxt->nodeMax *= 2;
412 ctxt->nodeTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000413 }
414 ctxt->nodeTab[ctxt->nodeNr] = value;
415 ctxt->node = value;
416 return (ctxt->nodeNr++);
417}
418static xmlNodePtr
419nodeVPop(xmlValidCtxtPtr ctxt)
420{
421 xmlNodePtr ret;
422
423 if (ctxt->nodeNr <= 0)
424 return (0);
425 ctxt->nodeNr--;
426 if (ctxt->nodeNr > 0)
427 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
428 else
429 ctxt->node = NULL;
430 ret = ctxt->nodeTab[ctxt->nodeNr];
431 ctxt->nodeTab[ctxt->nodeNr] = 0;
432 return (ret);
433}
Owen Taylor3473f882001-02-23 17:55:21 +0000434
Owen Taylor3473f882001-02-23 17:55:21 +0000435#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000436static void
437xmlValidPrintNode(xmlNodePtr cur) {
438 if (cur == NULL) {
439 xmlGenericError(xmlGenericErrorContext, "null");
440 return;
441 }
442 switch (cur->type) {
443 case XML_ELEMENT_NODE:
444 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
445 break;
446 case XML_TEXT_NODE:
447 xmlGenericError(xmlGenericErrorContext, "text ");
448 break;
449 case XML_CDATA_SECTION_NODE:
450 xmlGenericError(xmlGenericErrorContext, "cdata ");
451 break;
452 case XML_ENTITY_REF_NODE:
453 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
454 break;
455 case XML_PI_NODE:
456 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
457 break;
458 case XML_COMMENT_NODE:
459 xmlGenericError(xmlGenericErrorContext, "comment ");
460 break;
461 case XML_ATTRIBUTE_NODE:
462 xmlGenericError(xmlGenericErrorContext, "?attr? ");
463 break;
464 case XML_ENTITY_NODE:
465 xmlGenericError(xmlGenericErrorContext, "?ent? ");
466 break;
467 case XML_DOCUMENT_NODE:
468 xmlGenericError(xmlGenericErrorContext, "?doc? ");
469 break;
470 case XML_DOCUMENT_TYPE_NODE:
471 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
472 break;
473 case XML_DOCUMENT_FRAG_NODE:
474 xmlGenericError(xmlGenericErrorContext, "?frag? ");
475 break;
476 case XML_NOTATION_NODE:
477 xmlGenericError(xmlGenericErrorContext, "?nota? ");
478 break;
479 case XML_HTML_DOCUMENT_NODE:
480 xmlGenericError(xmlGenericErrorContext, "?html? ");
481 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000482#ifdef LIBXML_DOCB_ENABLED
483 case XML_DOCB_DOCUMENT_NODE:
484 xmlGenericError(xmlGenericErrorContext, "?docb? ");
485 break;
486#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000487 case XML_DTD_NODE:
488 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
489 break;
490 case XML_ELEMENT_DECL:
491 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
492 break;
493 case XML_ATTRIBUTE_DECL:
494 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
495 break;
496 case XML_ENTITY_DECL:
497 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
498 break;
499 case XML_NAMESPACE_DECL:
500 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
501 break;
502 case XML_XINCLUDE_START:
503 xmlGenericError(xmlGenericErrorContext, "incstart ");
504 break;
505 case XML_XINCLUDE_END:
506 xmlGenericError(xmlGenericErrorContext, "incend ");
507 break;
508 }
509}
510
511static void
512xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000513 if (cur == NULL)
514 xmlGenericError(xmlGenericErrorContext, "null ");
515 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000516 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000517 cur = cur->next;
518 }
519}
520
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000521static void
522xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Daniel Veillard83391282003-03-06 21:37:30 +0000523 char expr[5000];
Owen Taylor3473f882001-02-23 17:55:21 +0000524
525 expr[0] = 0;
526 xmlGenericError(xmlGenericErrorContext, "valid: ");
527 xmlValidPrintNodeList(cur);
528 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000529 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000530 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
531}
532
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000533static void
534xmlValidDebugState(xmlValidStatePtr state) {
535 xmlGenericError(xmlGenericErrorContext, "(");
536 if (state->cont == NULL)
537 xmlGenericError(xmlGenericErrorContext, "null,");
538 else
539 switch (state->cont->type) {
540 case XML_ELEMENT_CONTENT_PCDATA:
541 xmlGenericError(xmlGenericErrorContext, "pcdata,");
542 break;
543 case XML_ELEMENT_CONTENT_ELEMENT:
544 xmlGenericError(xmlGenericErrorContext, "%s,",
545 state->cont->name);
546 break;
547 case XML_ELEMENT_CONTENT_SEQ:
548 xmlGenericError(xmlGenericErrorContext, "seq,");
549 break;
550 case XML_ELEMENT_CONTENT_OR:
551 xmlGenericError(xmlGenericErrorContext, "or,");
552 break;
553 }
554 xmlValidPrintNode(state->node);
555 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
556 state->depth, state->occurs, state->state);
557}
558
559static void
560xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
561 int i, j;
562
563 xmlGenericError(xmlGenericErrorContext, "state: ");
564 xmlValidDebugState(ctxt->vstate);
565 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
566 ctxt->vstateNr - 1);
567 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
568 xmlValidDebugState(&ctxt->vstateTab[j]);
569 xmlGenericError(xmlGenericErrorContext, "\n");
570}
571
572/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000573#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000574 *****/
575
576#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000577#define DEBUG_VALID_MSG(m) \
578 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
579
Owen Taylor3473f882001-02-23 17:55:21 +0000580#else
581#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000582#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000583#endif
584
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000585/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000586
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000587
Owen Taylor3473f882001-02-23 17:55:21 +0000588#define CHECK_DTD \
589 if (doc == NULL) return(0); \
590 else if ((doc->intSubset == NULL) && \
591 (doc->extSubset == NULL)) return(0)
592
Owen Taylor3473f882001-02-23 17:55:21 +0000593xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
594
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000595#ifdef LIBXML_REGEXP_ENABLED
596
597/************************************************************************
598 * *
599 * Content model validation based on the regexps *
600 * *
601 ************************************************************************/
602
603/**
604 * xmlValidBuildAContentModel:
605 * @content: the content model
606 * @ctxt: the schema parser context
607 * @name: the element name whose content is being built
608 *
609 * Generate the automata sequence needed for that type
610 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000611 * Returns 1 if successful or 0 in case of error.
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000612 */
613static int
614xmlValidBuildAContentModel(xmlElementContentPtr content,
615 xmlValidCtxtPtr ctxt,
616 const xmlChar *name) {
617 if (content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000618 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
619 "Found NULL content in content model of %s\n",
620 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000621 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000622 }
623 switch (content->type) {
624 case XML_ELEMENT_CONTENT_PCDATA:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000625 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
626 "Found PCDATA in content model of %s\n",
627 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000628 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000629 break;
630 case XML_ELEMENT_CONTENT_ELEMENT: {
631 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillardc00cda82003-04-07 10:22:39 +0000632 xmlChar fn[50];
633 xmlChar *fullname;
634
635 fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
636 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000637 xmlVErrMemory(ctxt, "Building content model");
Daniel Veillardc00cda82003-04-07 10:22:39 +0000638 return(0);
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000639 }
640
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000641 switch (content->ocur) {
642 case XML_ELEMENT_CONTENT_ONCE:
643 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000644 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000645 break;
646 case XML_ELEMENT_CONTENT_OPT:
647 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000648 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000649 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
650 break;
651 case XML_ELEMENT_CONTENT_PLUS:
652 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000653 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000654 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000655 ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000656 break;
657 case XML_ELEMENT_CONTENT_MULT:
658 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000659 ctxt->state, fullname, NULL);
Daniel Veillard57e79b32003-02-04 15:33:12 +0000660 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
661 NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000662 break;
663 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000664 if ((fullname != fn) && (fullname != content->name))
665 xmlFree(fullname);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000666 break;
667 }
668 case XML_ELEMENT_CONTENT_SEQ: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000669 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000670 xmlElementContentOccur ocur;
671
672 /*
673 * Simply iterate over the content
674 */
675 oldstate = ctxt->state;
676 ocur = content->ocur;
Daniel Veillardf4be0182003-02-24 19:54:33 +0000677 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
678 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
679 oldstate = ctxt->state;
680 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000681 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000682 xmlValidBuildAContentModel(content->c1, ctxt, name);
683 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000684 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
685 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
686 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000687 oldend = ctxt->state;
688 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000689 switch (ocur) {
690 case XML_ELEMENT_CONTENT_ONCE:
691 break;
692 case XML_ELEMENT_CONTENT_OPT:
693 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
694 break;
695 case XML_ELEMENT_CONTENT_MULT:
696 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000697 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000698 break;
699 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000700 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000701 break;
702 }
703 break;
704 }
705 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000706 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000707 xmlElementContentOccur ocur;
708
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000709 ocur = content->ocur;
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000710 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
711 (ocur == XML_ELEMENT_CONTENT_MULT)) {
712 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
713 ctxt->state, NULL);
714 }
715 oldstate = ctxt->state;
716 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000717
718 /*
719 * iterate over the subtypes and remerge the end with an
720 * epsilon transition
721 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000722 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000723 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000724 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000725 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000726 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000727 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
728 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000729 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000730 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000731 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
732 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000733 switch (ocur) {
734 case XML_ELEMENT_CONTENT_ONCE:
735 break;
736 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000737 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000738 break;
739 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000740 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
741 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000742 break;
743 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000744 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000745 break;
746 }
747 break;
748 }
749 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000750 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
751 "ContentModel broken for element %s\n",
752 (const char *) name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000753 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000754 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000755 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000756}
757/**
758 * xmlValidBuildContentModel:
759 * @ctxt: a validation context
760 * @elem: an element declaration node
761 *
762 * (Re)Build the automata associated to the content model of this
763 * element
764 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000765 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000766 */
767int
768xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000769
770 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000771 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000772 if (elem->type != XML_ELEMENT_DECL)
773 return(0);
774 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
775 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000776 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000777 if (elem->contModel != NULL) {
778 if (!xmlRegexpIsDeterminist(elem->contModel)) {
779 ctxt->valid = 0;
780 return(0);
781 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000782 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000783 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000784
785 ctxt->am = xmlNewAutomata();
786 if (ctxt->am == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000787 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
788 XML_ERR_INTERNAL_ERROR,
789 "Cannot create automata for element %s\n",
790 elem->name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000791 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000792 }
William M. Brack78637da2003-07-31 14:47:38 +0000793 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000794 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
795 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000796 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000797 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000798 char expr[5000];
799 expr[0] = 0;
800 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000801 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
802 XML_DTD_CONTENT_NOT_DETERMINIST,
803 "Content model of %s is not determinist: %s\n",
804 elem->name, BAD_CAST expr, NULL);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000805#ifdef DEBUG_REGEXP_ALGO
806 xmlRegexpPrint(stderr, elem->contModel);
807#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000808 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000809 ctxt->state = NULL;
810 xmlFreeAutomata(ctxt->am);
811 ctxt->am = NULL;
812 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000813 }
814 ctxt->state = NULL;
815 xmlFreeAutomata(ctxt->am);
816 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000817 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000818}
819
820#endif /* LIBXML_REGEXP_ENABLED */
821
Owen Taylor3473f882001-02-23 17:55:21 +0000822/****************************************************************
823 * *
824 * Util functions for data allocation/deallocation *
825 * *
826 ****************************************************************/
827
828/**
Daniel Veillarda37aab82003-06-09 09:10:36 +0000829 * xmlNewValidCtxt:
830 *
831 * Allocate a validation context structure.
832 *
833 * Returns NULL if not, otherwise the new validation context structure
834 */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000835xmlValidCtxtPtr xmlNewValidCtxt(void) {
Daniel Veillarda37aab82003-06-09 09:10:36 +0000836 xmlValidCtxtPtr ret;
837
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000838 if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000839 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda37aab82003-06-09 09:10:36 +0000840 return (NULL);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000841 }
Daniel Veillarda37aab82003-06-09 09:10:36 +0000842
843 (void) memset(ret, 0, sizeof (xmlValidCtxt));
844
845 return (ret);
846}
847
848/**
849 * xmlFreeValidCtxt:
850 * @cur: the validation context to free
851 *
852 * Free a validation context structure.
853 */
854void
855xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
856 xmlFree(cur);
857}
858
Daniel Veillard4432df22003-09-28 18:58:27 +0000859#endif /* LIBXML_VALID_ENABLED */
860
Daniel Veillarda37aab82003-06-09 09:10:36 +0000861/**
Owen Taylor3473f882001-02-23 17:55:21 +0000862 * xmlNewElementContent:
863 * @name: the subelement name or NULL
864 * @type: the type of element content decl
865 *
866 * Allocate an element content structure.
867 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000868 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000869 */
870xmlElementContentPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000871xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
Owen Taylor3473f882001-02-23 17:55:21 +0000872 xmlElementContentPtr ret;
873
874 switch(type) {
875 case XML_ELEMENT_CONTENT_ELEMENT:
876 if (name == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000877 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
878 "xmlNewElementContent : name == NULL !\n",
879 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000880 }
881 break;
882 case XML_ELEMENT_CONTENT_PCDATA:
883 case XML_ELEMENT_CONTENT_SEQ:
884 case XML_ELEMENT_CONTENT_OR:
885 if (name != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000886 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
887 "xmlNewElementContent : name != NULL !\n",
888 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000889 }
890 break;
891 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000892 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
893 "Internal: ELEMENT content corrupted invalid type\n",
894 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000895 return(NULL);
896 }
897 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
898 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000899 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000900 return(NULL);
901 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000902 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000903 ret->type = type;
904 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000905 if (name != NULL) {
906 xmlChar *prefix = NULL;
907 ret->name = xmlSplitQName2(name, &prefix);
908 if (ret->name == NULL)
909 ret->name = xmlStrdup(name);
910 ret->prefix = prefix;
911 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000912 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000913 ret->prefix = NULL;
914 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000915 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000916 return(ret);
917}
918
919/**
920 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000921 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +0000922 *
923 * Build a copy of an element content description.
924 *
925 * Returns the new xmlElementContentPtr or NULL in case of error.
926 */
927xmlElementContentPtr
928xmlCopyElementContent(xmlElementContentPtr cur) {
929 xmlElementContentPtr ret;
930
931 if (cur == NULL) return(NULL);
932 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
933 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000934 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000935 return(NULL);
936 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000937 if (cur->prefix != NULL)
938 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000939 ret->ocur = cur->ocur;
940 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000941 if (ret->c1 != NULL)
942 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000943 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000944 if (ret->c2 != NULL)
945 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000946 return(ret);
947}
948
949/**
950 * xmlFreeElementContent:
951 * @cur: the element content tree to free
952 *
953 * Free an element content structure. This is a recursive call !
954 */
955void
956xmlFreeElementContent(xmlElementContentPtr cur) {
957 if (cur == NULL) return;
958 switch (cur->type) {
959 case XML_ELEMENT_CONTENT_PCDATA:
960 case XML_ELEMENT_CONTENT_ELEMENT:
961 case XML_ELEMENT_CONTENT_SEQ:
962 case XML_ELEMENT_CONTENT_OR:
963 break;
964 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000965 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
966 "Internal: ELEMENT content corrupted invalid type\n",
967 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000968 return;
969 }
970 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
971 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
972 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000973 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000974 xmlFree(cur);
975}
976
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000977#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000978/**
979 * xmlDumpElementContent:
980 * @buf: An XML buffer
981 * @content: An element table
982 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
983 *
984 * This will dump the content of the element table as an XML DTD definition
985 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000986static void
Owen Taylor3473f882001-02-23 17:55:21 +0000987xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
988 if (content == NULL) return;
989
990 if (glob) xmlBufferWriteChar(buf, "(");
991 switch (content->type) {
992 case XML_ELEMENT_CONTENT_PCDATA:
993 xmlBufferWriteChar(buf, "#PCDATA");
994 break;
995 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000996 if (content->prefix != NULL) {
997 xmlBufferWriteCHAR(buf, content->prefix);
998 xmlBufferWriteChar(buf, ":");
999 }
Owen Taylor3473f882001-02-23 17:55:21 +00001000 xmlBufferWriteCHAR(buf, content->name);
1001 break;
1002 case XML_ELEMENT_CONTENT_SEQ:
1003 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1004 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1005 xmlDumpElementContent(buf, content->c1, 1);
1006 else
1007 xmlDumpElementContent(buf, content->c1, 0);
1008 xmlBufferWriteChar(buf, " , ");
1009 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
1010 xmlDumpElementContent(buf, content->c2, 1);
1011 else
1012 xmlDumpElementContent(buf, content->c2, 0);
1013 break;
1014 case XML_ELEMENT_CONTENT_OR:
1015 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1016 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1017 xmlDumpElementContent(buf, content->c1, 1);
1018 else
1019 xmlDumpElementContent(buf, content->c1, 0);
1020 xmlBufferWriteChar(buf, " | ");
1021 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
1022 xmlDumpElementContent(buf, content->c2, 1);
1023 else
1024 xmlDumpElementContent(buf, content->c2, 0);
1025 break;
1026 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001027 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1028 "Internal: ELEMENT content corrupted invalid type\n",
1029 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001030 }
1031 if (glob)
1032 xmlBufferWriteChar(buf, ")");
1033 switch (content->ocur) {
1034 case XML_ELEMENT_CONTENT_ONCE:
1035 break;
1036 case XML_ELEMENT_CONTENT_OPT:
1037 xmlBufferWriteChar(buf, "?");
1038 break;
1039 case XML_ELEMENT_CONTENT_MULT:
1040 xmlBufferWriteChar(buf, "*");
1041 break;
1042 case XML_ELEMENT_CONTENT_PLUS:
1043 xmlBufferWriteChar(buf, "+");
1044 break;
1045 }
1046}
1047
1048/**
1049 * xmlSprintfElementContent:
1050 * @buf: an output buffer
1051 * @content: An element table
1052 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1053 *
Daniel Veillardd3d06722001-08-15 12:06:36 +00001054 * Deprecated, unsafe, use xmlSnprintfElementContent
1055 */
1056void
1057xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
1058 xmlElementContentPtr content ATTRIBUTE_UNUSED,
1059 int glob ATTRIBUTE_UNUSED) {
1060}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001061#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardd3d06722001-08-15 12:06:36 +00001062
1063/**
1064 * xmlSnprintfElementContent:
1065 * @buf: an output buffer
1066 * @size: the buffer size
1067 * @content: An element table
1068 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1069 *
Owen Taylor3473f882001-02-23 17:55:21 +00001070 * This will dump the content of the element content definition
1071 * Intended just for the debug routine
1072 */
1073void
Daniel Veillardd3d06722001-08-15 12:06:36 +00001074xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
1075 int len;
1076
Owen Taylor3473f882001-02-23 17:55:21 +00001077 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +00001078 len = strlen(buf);
1079 if (size - len < 50) {
1080 if ((size - len > 4) && (buf[len - 1] != '.'))
1081 strcat(buf, " ...");
1082 return;
1083 }
Owen Taylor3473f882001-02-23 17:55:21 +00001084 if (glob) strcat(buf, "(");
1085 switch (content->type) {
1086 case XML_ELEMENT_CONTENT_PCDATA:
1087 strcat(buf, "#PCDATA");
1088 break;
1089 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001090 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001091 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001092 strcat(buf, " ...");
1093 return;
1094 }
1095 strcat(buf, (char *) content->prefix);
1096 strcat(buf, ":");
1097 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001098 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001099 strcat(buf, " ...");
1100 return;
1101 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001102 if (content->name != NULL)
1103 strcat(buf, (char *) content->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001104 break;
1105 case XML_ELEMENT_CONTENT_SEQ:
1106 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1107 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001108 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001109 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001110 xmlSnprintfElementContent(buf, size, content->c1, 0);
1111 len = strlen(buf);
1112 if (size - len < 50) {
1113 if ((size - len > 4) && (buf[len - 1] != '.'))
1114 strcat(buf, " ...");
1115 return;
1116 }
Owen Taylor3473f882001-02-23 17:55:21 +00001117 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001118 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1119 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1120 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001121 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001122 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001123 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001124 break;
1125 case XML_ELEMENT_CONTENT_OR:
1126 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1127 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001128 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001129 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001130 xmlSnprintfElementContent(buf, size, content->c1, 0);
1131 len = strlen(buf);
1132 if (size - len < 50) {
1133 if ((size - len > 4) && (buf[len - 1] != '.'))
1134 strcat(buf, " ...");
1135 return;
1136 }
Owen Taylor3473f882001-02-23 17:55:21 +00001137 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001138 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1139 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1140 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001141 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001142 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001143 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001144 break;
1145 }
1146 if (glob)
1147 strcat(buf, ")");
1148 switch (content->ocur) {
1149 case XML_ELEMENT_CONTENT_ONCE:
1150 break;
1151 case XML_ELEMENT_CONTENT_OPT:
1152 strcat(buf, "?");
1153 break;
1154 case XML_ELEMENT_CONTENT_MULT:
1155 strcat(buf, "*");
1156 break;
1157 case XML_ELEMENT_CONTENT_PLUS:
1158 strcat(buf, "+");
1159 break;
1160 }
1161}
1162
1163/****************************************************************
1164 * *
1165 * Registration of DTD declarations *
1166 * *
1167 ****************************************************************/
1168
1169/**
1170 * xmlCreateElementTable:
1171 *
1172 * create and initialize an empty element hash table.
1173 *
1174 * Returns the xmlElementTablePtr just created or NULL in case of error.
1175 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001176static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001177xmlCreateElementTable(void) {
1178 return(xmlHashCreate(0));
1179}
1180
1181/**
1182 * xmlFreeElement:
1183 * @elem: An element
1184 *
1185 * Deallocate the memory used by an element definition
1186 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001187static void
Owen Taylor3473f882001-02-23 17:55:21 +00001188xmlFreeElement(xmlElementPtr elem) {
1189 if (elem == NULL) return;
1190 xmlUnlinkNode((xmlNodePtr) elem);
1191 xmlFreeElementContent(elem->content);
1192 if (elem->name != NULL)
1193 xmlFree((xmlChar *) elem->name);
1194 if (elem->prefix != NULL)
1195 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001196#ifdef LIBXML_REGEXP_ENABLED
1197 if (elem->contModel != NULL)
1198 xmlRegFreeRegexp(elem->contModel);
1199#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001200 xmlFree(elem);
1201}
1202
1203
1204/**
1205 * xmlAddElementDecl:
1206 * @ctxt: the validation context
1207 * @dtd: pointer to the DTD
1208 * @name: the entity name
1209 * @type: the element type
1210 * @content: the element content tree or NULL
1211 *
1212 * Register a new element declaration
1213 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001214 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001215 */
1216xmlElementPtr
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001217xmlAddElementDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1218 xmlDtdPtr dtd, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001219 xmlElementTypeVal type,
1220 xmlElementContentPtr content) {
1221 xmlElementPtr ret;
1222 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001223 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001224 xmlChar *ns, *uqname;
1225
1226 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001227 return(NULL);
1228 }
1229 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001230 return(NULL);
1231 }
1232 switch (type) {
1233 case XML_ELEMENT_TYPE_EMPTY:
1234 if (content != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001235 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1236 "xmlAddElementDecl: content != NULL for EMPTY\n",
1237 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001238 return(NULL);
1239 }
1240 break;
1241 case XML_ELEMENT_TYPE_ANY:
1242 if (content != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001243 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1244 "xmlAddElementDecl: content != NULL for ANY\n",
1245 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001246 return(NULL);
1247 }
1248 break;
1249 case XML_ELEMENT_TYPE_MIXED:
1250 if (content == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001251 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1252 "xmlAddElementDecl: content == NULL for MIXED\n",
1253 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001254 return(NULL);
1255 }
1256 break;
1257 case XML_ELEMENT_TYPE_ELEMENT:
1258 if (content == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001259 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1260 "xmlAddElementDecl: content == NULL for ELEMENT\n",
1261 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001262 return(NULL);
1263 }
1264 break;
1265 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001266 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1267 "Internal: ELEMENT decl corrupted invalid type\n",
1268 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001269 return(NULL);
1270 }
1271
1272 /*
1273 * check if name is a QName
1274 */
1275 uqname = xmlSplitQName2(name, &ns);
1276 if (uqname != NULL)
1277 name = uqname;
1278
1279 /*
1280 * Create the Element table if needed.
1281 */
1282 table = (xmlElementTablePtr) dtd->elements;
1283 if (table == NULL) {
1284 table = xmlCreateElementTable();
1285 dtd->elements = (void *) table;
1286 }
1287 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001288 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001289 "xmlAddElementDecl: Table creation failed!\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001290 if (uqname != NULL)
1291 xmlFree(uqname);
1292 if (ns != NULL)
1293 xmlFree(ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001294 return(NULL);
1295 }
1296
Daniel Veillarda10efa82001-04-18 13:09:01 +00001297 /*
1298 * lookup old attributes inserted on an undefined element in the
1299 * internal subset.
1300 */
1301 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1302 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1303 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1304 oldAttributes = ret->attributes;
1305 ret->attributes = NULL;
1306 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1307 xmlFreeElement(ret);
1308 }
Owen Taylor3473f882001-02-23 17:55:21 +00001309 }
Owen Taylor3473f882001-02-23 17:55:21 +00001310
1311 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001312 * The element may already be present if one of its attribute
1313 * was registered first
1314 */
1315 ret = xmlHashLookup2(table, name, ns);
1316 if (ret != NULL) {
1317 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001318#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001319 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001320 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001321 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001322 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1323 "Redefinition of element %s\n",
1324 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001325#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001326 if (uqname != NULL)
1327 xmlFree(uqname);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001328 if (ns != NULL)
1329 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001330 return(NULL);
1331 }
1332 } else {
1333 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1334 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001335 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001336 if (uqname != NULL)
1337 xmlFree(uqname);
1338 if (ns != NULL)
1339 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001340 return(NULL);
1341 }
1342 memset(ret, 0, sizeof(xmlElement));
1343 ret->type = XML_ELEMENT_DECL;
1344
1345 /*
1346 * fill the structure.
1347 */
1348 ret->name = xmlStrdup(name);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001349 if (ret->name == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001350 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001351 if (uqname != NULL)
1352 xmlFree(uqname);
1353 if (ns != NULL)
1354 xmlFree(ns);
1355 xmlFree(ret);
1356 return(NULL);
1357 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001358 ret->prefix = ns;
1359
1360 /*
1361 * Validity Check:
1362 * Insertion must not fail
1363 */
1364 if (xmlHashAddEntry2(table, name, ns, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001365#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001366 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001367 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001368 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001369 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1370 "Redefinition of element %s\n",
1371 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001372#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001373 xmlFreeElement(ret);
1374 if (uqname != NULL)
1375 xmlFree(uqname);
1376 return(NULL);
1377 }
William M. Brack4e52f2f2003-09-14 18:07:39 +00001378 /*
1379 * For new element, may have attributes from earlier
1380 * definition in internal subset
1381 */
1382 ret->attributes = oldAttributes;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001383 }
1384
1385 /*
1386 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001387 */
1388 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +00001389 ret->content = xmlCopyElementContent(content);
Owen Taylor3473f882001-02-23 17:55:21 +00001390
1391 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001392 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001393 */
1394 ret->parent = dtd;
1395 ret->doc = dtd->doc;
1396 if (dtd->last == NULL) {
1397 dtd->children = dtd->last = (xmlNodePtr) ret;
1398 } else {
1399 dtd->last->next = (xmlNodePtr) ret;
1400 ret->prev = dtd->last;
1401 dtd->last = (xmlNodePtr) ret;
1402 }
1403 if (uqname != NULL)
1404 xmlFree(uqname);
1405 return(ret);
1406}
1407
1408/**
1409 * xmlFreeElementTable:
1410 * @table: An element table
1411 *
1412 * Deallocate the memory used by an element hash table.
1413 */
1414void
1415xmlFreeElementTable(xmlElementTablePtr table) {
1416 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1417}
1418
Daniel Veillard652327a2003-09-29 18:02:38 +00001419#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001420/**
1421 * xmlCopyElement:
1422 * @elem: An element
1423 *
1424 * Build a copy of an element.
1425 *
1426 * Returns the new xmlElementPtr or NULL in case of error.
1427 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001428static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001429xmlCopyElement(xmlElementPtr elem) {
1430 xmlElementPtr cur;
1431
1432 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1433 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001434 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001435 return(NULL);
1436 }
1437 memset(cur, 0, sizeof(xmlElement));
1438 cur->type = XML_ELEMENT_DECL;
1439 cur->etype = elem->etype;
1440 if (elem->name != NULL)
1441 cur->name = xmlStrdup(elem->name);
1442 else
1443 cur->name = NULL;
1444 if (elem->prefix != NULL)
1445 cur->prefix = xmlStrdup(elem->prefix);
1446 else
1447 cur->prefix = NULL;
1448 cur->content = xmlCopyElementContent(elem->content);
1449 /* TODO : rebuild the attribute list on the copy */
1450 cur->attributes = NULL;
1451 return(cur);
1452}
1453
1454/**
1455 * xmlCopyElementTable:
1456 * @table: An element table
1457 *
1458 * Build a copy of an element table.
1459 *
1460 * Returns the new xmlElementTablePtr or NULL in case of error.
1461 */
1462xmlElementTablePtr
1463xmlCopyElementTable(xmlElementTablePtr table) {
1464 return((xmlElementTablePtr) xmlHashCopy(table,
1465 (xmlHashCopier) xmlCopyElement));
1466}
Daniel Veillard652327a2003-09-29 18:02:38 +00001467#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001468
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001469#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001470/**
1471 * xmlDumpElementDecl:
1472 * @buf: the XML buffer output
1473 * @elem: An element table
1474 *
1475 * This will dump the content of the element declaration as an XML
1476 * DTD definition
1477 */
1478void
1479xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1480 switch (elem->etype) {
1481 case XML_ELEMENT_TYPE_EMPTY:
1482 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001483 if (elem->prefix != NULL) {
1484 xmlBufferWriteCHAR(buf, elem->prefix);
1485 xmlBufferWriteChar(buf, ":");
1486 }
Owen Taylor3473f882001-02-23 17:55:21 +00001487 xmlBufferWriteCHAR(buf, elem->name);
1488 xmlBufferWriteChar(buf, " EMPTY>\n");
1489 break;
1490 case XML_ELEMENT_TYPE_ANY:
1491 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001492 if (elem->prefix != NULL) {
1493 xmlBufferWriteCHAR(buf, elem->prefix);
1494 xmlBufferWriteChar(buf, ":");
1495 }
Owen Taylor3473f882001-02-23 17:55:21 +00001496 xmlBufferWriteCHAR(buf, elem->name);
1497 xmlBufferWriteChar(buf, " ANY>\n");
1498 break;
1499 case XML_ELEMENT_TYPE_MIXED:
1500 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001501 if (elem->prefix != NULL) {
1502 xmlBufferWriteCHAR(buf, elem->prefix);
1503 xmlBufferWriteChar(buf, ":");
1504 }
Owen Taylor3473f882001-02-23 17:55:21 +00001505 xmlBufferWriteCHAR(buf, elem->name);
1506 xmlBufferWriteChar(buf, " ");
1507 xmlDumpElementContent(buf, elem->content, 1);
1508 xmlBufferWriteChar(buf, ">\n");
1509 break;
1510 case XML_ELEMENT_TYPE_ELEMENT:
1511 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001512 if (elem->prefix != NULL) {
1513 xmlBufferWriteCHAR(buf, elem->prefix);
1514 xmlBufferWriteChar(buf, ":");
1515 }
Owen Taylor3473f882001-02-23 17:55:21 +00001516 xmlBufferWriteCHAR(buf, elem->name);
1517 xmlBufferWriteChar(buf, " ");
1518 xmlDumpElementContent(buf, elem->content, 1);
1519 xmlBufferWriteChar(buf, ">\n");
1520 break;
1521 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001522 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1523 "Internal: ELEMENT struct corrupted invalid type\n",
1524 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001525 }
1526}
1527
1528/**
1529 * xmlDumpElementTable:
1530 * @buf: the XML buffer output
1531 * @table: An element table
1532 *
1533 * This will dump the content of the element table as an XML DTD definition
1534 */
1535void
1536xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1537 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1538}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001539#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001540
1541/**
1542 * xmlCreateEnumeration:
1543 * @name: the enumeration name or NULL
1544 *
1545 * create and initialize an enumeration attribute node.
1546 *
1547 * Returns the xmlEnumerationPtr just created or NULL in case
1548 * of error.
1549 */
1550xmlEnumerationPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001551xmlCreateEnumeration(const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00001552 xmlEnumerationPtr ret;
1553
1554 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1555 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001556 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001557 return(NULL);
1558 }
1559 memset(ret, 0, sizeof(xmlEnumeration));
1560
1561 if (name != NULL)
1562 ret->name = xmlStrdup(name);
1563 return(ret);
1564}
1565
1566/**
1567 * xmlFreeEnumeration:
1568 * @cur: the tree to free.
1569 *
1570 * free an enumeration attribute node (recursive).
1571 */
1572void
1573xmlFreeEnumeration(xmlEnumerationPtr cur) {
1574 if (cur == NULL) return;
1575
1576 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1577
1578 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001579 xmlFree(cur);
1580}
1581
Daniel Veillard652327a2003-09-29 18:02:38 +00001582#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001583/**
1584 * xmlCopyEnumeration:
1585 * @cur: the tree to copy.
1586 *
1587 * Copy an enumeration attribute node (recursive).
1588 *
1589 * Returns the xmlEnumerationPtr just created or NULL in case
1590 * of error.
1591 */
1592xmlEnumerationPtr
1593xmlCopyEnumeration(xmlEnumerationPtr cur) {
1594 xmlEnumerationPtr ret;
1595
1596 if (cur == NULL) return(NULL);
1597 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1598
1599 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1600 else ret->next = NULL;
1601
1602 return(ret);
1603}
Daniel Veillard652327a2003-09-29 18:02:38 +00001604#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001605
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001606#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001607/**
1608 * xmlDumpEnumeration:
1609 * @buf: the XML buffer output
1610 * @enum: An enumeration
1611 *
1612 * This will dump the content of the enumeration
1613 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001614static void
Owen Taylor3473f882001-02-23 17:55:21 +00001615xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1616 if (cur == NULL) return;
1617
1618 xmlBufferWriteCHAR(buf, cur->name);
1619 if (cur->next == NULL)
1620 xmlBufferWriteChar(buf, ")");
1621 else {
1622 xmlBufferWriteChar(buf, " | ");
1623 xmlDumpEnumeration(buf, cur->next);
1624 }
1625}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001626#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001627
1628/**
1629 * xmlCreateAttributeTable:
1630 *
1631 * create and initialize an empty attribute hash table.
1632 *
1633 * Returns the xmlAttributeTablePtr just created or NULL in case
1634 * of error.
1635 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001636static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001637xmlCreateAttributeTable(void) {
1638 return(xmlHashCreate(0));
1639}
1640
Daniel Veillard4432df22003-09-28 18:58:27 +00001641#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001642/**
1643 * xmlScanAttributeDeclCallback:
1644 * @attr: the attribute decl
1645 * @list: the list to update
1646 *
1647 * Callback called by xmlScanAttributeDecl when a new attribute
1648 * has to be entered in the list.
1649 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001650static void
Owen Taylor3473f882001-02-23 17:55:21 +00001651xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001652 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001653 attr->nexth = *list;
1654 *list = attr;
1655}
1656
1657/**
1658 * xmlScanAttributeDecl:
1659 * @dtd: pointer to the DTD
1660 * @elem: the element name
1661 *
1662 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001663 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001664 *
1665 * Returns the pointer to the first attribute decl in the chain,
1666 * possibly NULL.
1667 */
1668xmlAttributePtr
1669xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1670 xmlAttributePtr ret = NULL;
1671 xmlAttributeTablePtr table;
1672
1673 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001674 return(NULL);
1675 }
1676 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001677 return(NULL);
1678 }
1679 table = (xmlAttributeTablePtr) dtd->attributes;
1680 if (table == NULL)
1681 return(NULL);
1682
1683 /* WRONG !!! */
1684 xmlHashScan3(table, NULL, NULL, elem,
1685 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1686 return(ret);
1687}
1688
1689/**
1690 * xmlScanIDAttributeDecl:
1691 * @ctxt: the validation context
1692 * @elem: the element name
1693 *
1694 * Verify that the element don't have too many ID attributes
1695 * declared.
1696 *
1697 * Returns the number of ID attributes found.
1698 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001699static int
Owen Taylor3473f882001-02-23 17:55:21 +00001700xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1701 xmlAttributePtr cur;
1702 int ret = 0;
1703
1704 if (elem == NULL) return(0);
1705 cur = elem->attributes;
1706 while (cur != NULL) {
1707 if (cur->atype == XML_ATTRIBUTE_ID) {
1708 ret ++;
1709 if (ret > 1)
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001710 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001711 "Element %s has too many ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001712 elem->name, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001713 }
1714 cur = cur->nexth;
1715 }
1716 return(ret);
1717}
Daniel Veillard4432df22003-09-28 18:58:27 +00001718#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001719
1720/**
1721 * xmlFreeAttribute:
1722 * @elem: An attribute
1723 *
1724 * Deallocate the memory used by an attribute definition
1725 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001726static void
Owen Taylor3473f882001-02-23 17:55:21 +00001727xmlFreeAttribute(xmlAttributePtr attr) {
1728 if (attr == NULL) return;
1729 xmlUnlinkNode((xmlNodePtr) attr);
1730 if (attr->tree != NULL)
1731 xmlFreeEnumeration(attr->tree);
1732 if (attr->elem != NULL)
1733 xmlFree((xmlChar *) attr->elem);
1734 if (attr->name != NULL)
1735 xmlFree((xmlChar *) attr->name);
1736 if (attr->defaultValue != NULL)
1737 xmlFree((xmlChar *) attr->defaultValue);
1738 if (attr->prefix != NULL)
1739 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001740 xmlFree(attr);
1741}
1742
1743
1744/**
1745 * xmlAddAttributeDecl:
1746 * @ctxt: the validation context
1747 * @dtd: pointer to the DTD
1748 * @elem: the element name
1749 * @name: the attribute name
1750 * @ns: the attribute namespace prefix
1751 * @type: the attribute type
1752 * @def: the attribute default type
1753 * @defaultValue: the attribute default value
1754 * @tree: if it's an enumeration, the associated list
1755 *
1756 * Register a new attribute declaration
1757 * Note that @tree becomes the ownership of the DTD
1758 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001759 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001760 */
1761xmlAttributePtr
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001762xmlAddAttributeDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1763 xmlDtdPtr dtd, const xmlChar *elem,
Owen Taylor3473f882001-02-23 17:55:21 +00001764 const xmlChar *name, const xmlChar *ns,
1765 xmlAttributeType type, xmlAttributeDefault def,
1766 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1767 xmlAttributePtr ret;
1768 xmlAttributeTablePtr table;
1769 xmlElementPtr elemDef;
1770
1771 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001772 xmlFreeEnumeration(tree);
1773 return(NULL);
1774 }
1775 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001776 xmlFreeEnumeration(tree);
1777 return(NULL);
1778 }
1779 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001780 xmlFreeEnumeration(tree);
1781 return(NULL);
1782 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001783
Daniel Veillard4432df22003-09-28 18:58:27 +00001784#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001785 /*
1786 * Check the type and possibly the default value.
1787 */
1788 switch (type) {
1789 case XML_ATTRIBUTE_CDATA:
1790 break;
1791 case XML_ATTRIBUTE_ID:
1792 break;
1793 case XML_ATTRIBUTE_IDREF:
1794 break;
1795 case XML_ATTRIBUTE_IDREFS:
1796 break;
1797 case XML_ATTRIBUTE_ENTITY:
1798 break;
1799 case XML_ATTRIBUTE_ENTITIES:
1800 break;
1801 case XML_ATTRIBUTE_NMTOKEN:
1802 break;
1803 case XML_ATTRIBUTE_NMTOKENS:
1804 break;
1805 case XML_ATTRIBUTE_ENUMERATION:
1806 break;
1807 case XML_ATTRIBUTE_NOTATION:
1808 break;
1809 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001810 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1811 "Internal: ATTRIBUTE struct corrupted invalid type\n",
1812 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001813 xmlFreeEnumeration(tree);
1814 return(NULL);
1815 }
1816 if ((defaultValue != NULL) &&
1817 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001818 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
1819 "Attribute %s of %s: invalid default value\n",
1820 elem, name, defaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001821 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001822 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001823 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001824#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001825
1826 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001827 * Check first that an attribute defined in the external subset wasn't
1828 * already defined in the internal subset
1829 */
1830 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1831 (dtd->doc->intSubset != NULL) &&
1832 (dtd->doc->intSubset->attributes != NULL)) {
1833 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1834 if (ret != NULL)
1835 return(NULL);
1836 }
1837
1838 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001839 * Create the Attribute table if needed.
1840 */
1841 table = (xmlAttributeTablePtr) dtd->attributes;
1842 if (table == NULL) {
1843 table = xmlCreateAttributeTable();
1844 dtd->attributes = (void *) table;
1845 }
1846 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001847 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001848 "xmlAddAttributeDecl: Table creation failed!\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001849 return(NULL);
1850 }
1851
1852
1853 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1854 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001855 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001856 return(NULL);
1857 }
1858 memset(ret, 0, sizeof(xmlAttribute));
1859 ret->type = XML_ATTRIBUTE_DECL;
1860
1861 /*
1862 * fill the structure.
1863 */
1864 ret->atype = type;
1865 ret->name = xmlStrdup(name);
1866 ret->prefix = xmlStrdup(ns);
1867 ret->elem = xmlStrdup(elem);
1868 ret->def = def;
1869 ret->tree = tree;
1870 if (defaultValue != NULL)
1871 ret->defaultValue = xmlStrdup(defaultValue);
1872
1873 /*
1874 * Validity Check:
1875 * Search the DTD for previous declarations of the ATTLIST
1876 */
1877 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001878#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001879 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001880 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001881 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001882 xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00001883 "Attribute %s of element %s: already defined\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001884 name, elem, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001885#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001886 xmlFreeAttribute(ret);
1887 return(NULL);
1888 }
1889
1890 /*
1891 * Validity Check:
1892 * Multiple ID per element
1893 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001894 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001895 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001896
Daniel Veillard4432df22003-09-28 18:58:27 +00001897#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001898 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001899 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001900 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
Owen Taylor3473f882001-02-23 17:55:21 +00001901 "Element %s has too may ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001902 elem, name, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00001903 ctxt->valid = 0;
1904 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001905#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardc7612992002-02-17 22:47:37 +00001906
Daniel Veillard48da9102001-08-07 01:10:10 +00001907 /*
1908 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001909 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001910 */
1911 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1912 ((ret->prefix != NULL &&
1913 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1914 ret->nexth = elemDef->attributes;
1915 elemDef->attributes = ret;
1916 } else {
1917 xmlAttributePtr tmp = elemDef->attributes;
1918
1919 while ((tmp != NULL) &&
1920 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1921 ((ret->prefix != NULL &&
1922 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1923 if (tmp->nexth == NULL)
1924 break;
1925 tmp = tmp->nexth;
1926 }
1927 if (tmp != NULL) {
1928 ret->nexth = tmp->nexth;
1929 tmp->nexth = ret;
1930 } else {
1931 ret->nexth = elemDef->attributes;
1932 elemDef->attributes = ret;
1933 }
1934 }
Owen Taylor3473f882001-02-23 17:55:21 +00001935 }
1936
1937 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001938 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001939 */
1940 ret->parent = dtd;
1941 ret->doc = dtd->doc;
1942 if (dtd->last == NULL) {
1943 dtd->children = dtd->last = (xmlNodePtr) ret;
1944 } else {
1945 dtd->last->next = (xmlNodePtr) ret;
1946 ret->prev = dtd->last;
1947 dtd->last = (xmlNodePtr) ret;
1948 }
1949 return(ret);
1950}
1951
1952/**
1953 * xmlFreeAttributeTable:
1954 * @table: An attribute table
1955 *
1956 * Deallocate the memory used by an entities hash table.
1957 */
1958void
1959xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1960 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1961}
1962
Daniel Veillard652327a2003-09-29 18:02:38 +00001963#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001964/**
1965 * xmlCopyAttribute:
1966 * @attr: An attribute
1967 *
1968 * Build a copy of an attribute.
1969 *
1970 * Returns the new xmlAttributePtr or NULL in case of error.
1971 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001972static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001973xmlCopyAttribute(xmlAttributePtr attr) {
1974 xmlAttributePtr cur;
1975
1976 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1977 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001978 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001979 return(NULL);
1980 }
1981 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001982 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001983 cur->atype = attr->atype;
1984 cur->def = attr->def;
1985 cur->tree = xmlCopyEnumeration(attr->tree);
1986 if (attr->elem != NULL)
1987 cur->elem = xmlStrdup(attr->elem);
1988 if (attr->name != NULL)
1989 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001990 if (attr->prefix != NULL)
1991 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001992 if (attr->defaultValue != NULL)
1993 cur->defaultValue = xmlStrdup(attr->defaultValue);
1994 return(cur);
1995}
1996
1997/**
1998 * xmlCopyAttributeTable:
1999 * @table: An attribute table
2000 *
2001 * Build a copy of an attribute table.
2002 *
2003 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2004 */
2005xmlAttributeTablePtr
2006xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2007 return((xmlAttributeTablePtr) xmlHashCopy(table,
2008 (xmlHashCopier) xmlCopyAttribute));
2009}
Daniel Veillard652327a2003-09-29 18:02:38 +00002010#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002011
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002012#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002013/**
2014 * xmlDumpAttributeDecl:
2015 * @buf: the XML buffer output
2016 * @attr: An attribute declaration
2017 *
2018 * This will dump the content of the attribute declaration as an XML
2019 * DTD definition
2020 */
2021void
2022xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
2023 xmlBufferWriteChar(buf, "<!ATTLIST ");
2024 xmlBufferWriteCHAR(buf, attr->elem);
2025 xmlBufferWriteChar(buf, " ");
2026 if (attr->prefix != NULL) {
2027 xmlBufferWriteCHAR(buf, attr->prefix);
2028 xmlBufferWriteChar(buf, ":");
2029 }
2030 xmlBufferWriteCHAR(buf, attr->name);
2031 switch (attr->atype) {
2032 case XML_ATTRIBUTE_CDATA:
2033 xmlBufferWriteChar(buf, " CDATA");
2034 break;
2035 case XML_ATTRIBUTE_ID:
2036 xmlBufferWriteChar(buf, " ID");
2037 break;
2038 case XML_ATTRIBUTE_IDREF:
2039 xmlBufferWriteChar(buf, " IDREF");
2040 break;
2041 case XML_ATTRIBUTE_IDREFS:
2042 xmlBufferWriteChar(buf, " IDREFS");
2043 break;
2044 case XML_ATTRIBUTE_ENTITY:
2045 xmlBufferWriteChar(buf, " ENTITY");
2046 break;
2047 case XML_ATTRIBUTE_ENTITIES:
2048 xmlBufferWriteChar(buf, " ENTITIES");
2049 break;
2050 case XML_ATTRIBUTE_NMTOKEN:
2051 xmlBufferWriteChar(buf, " NMTOKEN");
2052 break;
2053 case XML_ATTRIBUTE_NMTOKENS:
2054 xmlBufferWriteChar(buf, " NMTOKENS");
2055 break;
2056 case XML_ATTRIBUTE_ENUMERATION:
2057 xmlBufferWriteChar(buf, " (");
2058 xmlDumpEnumeration(buf, attr->tree);
2059 break;
2060 case XML_ATTRIBUTE_NOTATION:
2061 xmlBufferWriteChar(buf, " NOTATION (");
2062 xmlDumpEnumeration(buf, attr->tree);
2063 break;
2064 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002065 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2066 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2067 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002068 }
2069 switch (attr->def) {
2070 case XML_ATTRIBUTE_NONE:
2071 break;
2072 case XML_ATTRIBUTE_REQUIRED:
2073 xmlBufferWriteChar(buf, " #REQUIRED");
2074 break;
2075 case XML_ATTRIBUTE_IMPLIED:
2076 xmlBufferWriteChar(buf, " #IMPLIED");
2077 break;
2078 case XML_ATTRIBUTE_FIXED:
2079 xmlBufferWriteChar(buf, " #FIXED");
2080 break;
2081 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002082 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2083 "Internal: ATTRIBUTE struct corrupted invalid def\n",
2084 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002085 }
2086 if (attr->defaultValue != NULL) {
2087 xmlBufferWriteChar(buf, " ");
2088 xmlBufferWriteQuotedString(buf, attr->defaultValue);
2089 }
2090 xmlBufferWriteChar(buf, ">\n");
2091}
2092
2093/**
2094 * xmlDumpAttributeTable:
2095 * @buf: the XML buffer output
2096 * @table: An attribute table
2097 *
2098 * This will dump the content of the attribute table as an XML DTD definition
2099 */
2100void
2101xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
2102 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
2103}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002104#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002105
2106/************************************************************************
2107 * *
2108 * NOTATIONs *
2109 * *
2110 ************************************************************************/
2111/**
2112 * xmlCreateNotationTable:
2113 *
2114 * create and initialize an empty notation hash table.
2115 *
2116 * Returns the xmlNotationTablePtr just created or NULL in case
2117 * of error.
2118 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002119static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002120xmlCreateNotationTable(void) {
2121 return(xmlHashCreate(0));
2122}
2123
2124/**
2125 * xmlFreeNotation:
2126 * @not: A notation
2127 *
2128 * Deallocate the memory used by an notation definition
2129 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002130static void
Owen Taylor3473f882001-02-23 17:55:21 +00002131xmlFreeNotation(xmlNotationPtr nota) {
2132 if (nota == NULL) return;
2133 if (nota->name != NULL)
2134 xmlFree((xmlChar *) nota->name);
2135 if (nota->PublicID != NULL)
2136 xmlFree((xmlChar *) nota->PublicID);
2137 if (nota->SystemID != NULL)
2138 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002139 xmlFree(nota);
2140}
2141
2142
2143/**
2144 * xmlAddNotationDecl:
2145 * @dtd: pointer to the DTD
2146 * @ctxt: the validation context
2147 * @name: the entity name
2148 * @PublicID: the public identifier or NULL
2149 * @SystemID: the system identifier or NULL
2150 *
2151 * Register a new notation declaration
2152 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002153 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002154 */
2155xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002156xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002157 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002158 const xmlChar *PublicID, const xmlChar *SystemID) {
2159 xmlNotationPtr ret;
2160 xmlNotationTablePtr table;
2161
2162 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002163 return(NULL);
2164 }
2165 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002166 return(NULL);
2167 }
2168 if ((PublicID == NULL) && (SystemID == NULL)) {
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002169 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002170 }
2171
2172 /*
2173 * Create the Notation table if needed.
2174 */
2175 table = (xmlNotationTablePtr) dtd->notations;
2176 if (table == NULL)
2177 dtd->notations = table = xmlCreateNotationTable();
2178 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002179 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002180 "xmlAddNotationDecl: Table creation failed!\n");
2181 return(NULL);
2182 }
2183
2184 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2185 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002186 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002187 return(NULL);
2188 }
2189 memset(ret, 0, sizeof(xmlNotation));
2190
2191 /*
2192 * fill the structure.
2193 */
2194 ret->name = xmlStrdup(name);
2195 if (SystemID != NULL)
2196 ret->SystemID = xmlStrdup(SystemID);
2197 if (PublicID != NULL)
2198 ret->PublicID = xmlStrdup(PublicID);
2199
2200 /*
2201 * Validity Check:
2202 * Check the DTD for previous declarations of the ATTLIST
2203 */
2204 if (xmlHashAddEntry(table, name, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002205#ifdef LIBXML_VALID_ENABLED
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002206 xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
2207 "xmlAddNotationDecl: %s already defined\n",
2208 (const char *) name);
Daniel Veillard4432df22003-09-28 18:58:27 +00002209#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002210 xmlFreeNotation(ret);
2211 return(NULL);
2212 }
2213 return(ret);
2214}
2215
2216/**
2217 * xmlFreeNotationTable:
2218 * @table: An notation table
2219 *
2220 * Deallocate the memory used by an entities hash table.
2221 */
2222void
2223xmlFreeNotationTable(xmlNotationTablePtr table) {
2224 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2225}
2226
Daniel Veillard652327a2003-09-29 18:02:38 +00002227#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002228/**
2229 * xmlCopyNotation:
2230 * @nota: A notation
2231 *
2232 * Build a copy of a notation.
2233 *
2234 * Returns the new xmlNotationPtr or NULL in case of error.
2235 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002236static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002237xmlCopyNotation(xmlNotationPtr nota) {
2238 xmlNotationPtr cur;
2239
2240 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2241 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002242 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002243 return(NULL);
2244 }
2245 if (nota->name != NULL)
2246 cur->name = xmlStrdup(nota->name);
2247 else
2248 cur->name = NULL;
2249 if (nota->PublicID != NULL)
2250 cur->PublicID = xmlStrdup(nota->PublicID);
2251 else
2252 cur->PublicID = NULL;
2253 if (nota->SystemID != NULL)
2254 cur->SystemID = xmlStrdup(nota->SystemID);
2255 else
2256 cur->SystemID = NULL;
2257 return(cur);
2258}
2259
2260/**
2261 * xmlCopyNotationTable:
2262 * @table: A notation table
2263 *
2264 * Build a copy of a notation table.
2265 *
2266 * Returns the new xmlNotationTablePtr or NULL in case of error.
2267 */
2268xmlNotationTablePtr
2269xmlCopyNotationTable(xmlNotationTablePtr table) {
2270 return((xmlNotationTablePtr) xmlHashCopy(table,
2271 (xmlHashCopier) xmlCopyNotation));
2272}
Daniel Veillard652327a2003-09-29 18:02:38 +00002273#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002274
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002275#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002276/**
2277 * xmlDumpNotationDecl:
2278 * @buf: the XML buffer output
2279 * @nota: A notation declaration
2280 *
2281 * This will dump the content the notation declaration as an XML DTD definition
2282 */
2283void
2284xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2285 xmlBufferWriteChar(buf, "<!NOTATION ");
2286 xmlBufferWriteCHAR(buf, nota->name);
2287 if (nota->PublicID != NULL) {
2288 xmlBufferWriteChar(buf, " PUBLIC ");
2289 xmlBufferWriteQuotedString(buf, nota->PublicID);
2290 if (nota->SystemID != NULL) {
2291 xmlBufferWriteChar(buf, " ");
2292 xmlBufferWriteCHAR(buf, nota->SystemID);
2293 }
2294 } else {
2295 xmlBufferWriteChar(buf, " SYSTEM ");
2296 xmlBufferWriteCHAR(buf, nota->SystemID);
2297 }
2298 xmlBufferWriteChar(buf, " >\n");
2299}
2300
2301/**
2302 * xmlDumpNotationTable:
2303 * @buf: the XML buffer output
2304 * @table: A notation table
2305 *
2306 * This will dump the content of the notation table as an XML DTD definition
2307 */
2308void
2309xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2310 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
2311}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002312#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002313
2314/************************************************************************
2315 * *
2316 * IDs *
2317 * *
2318 ************************************************************************/
2319/**
2320 * xmlCreateIDTable:
2321 *
2322 * create and initialize an empty id hash table.
2323 *
2324 * Returns the xmlIDTablePtr just created or NULL in case
2325 * of error.
2326 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002327static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002328xmlCreateIDTable(void) {
2329 return(xmlHashCreate(0));
2330}
2331
2332/**
2333 * xmlFreeID:
2334 * @not: A id
2335 *
2336 * Deallocate the memory used by an id definition
2337 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002338static void
Owen Taylor3473f882001-02-23 17:55:21 +00002339xmlFreeID(xmlIDPtr id) {
2340 if (id == NULL) return;
2341 if (id->value != NULL)
2342 xmlFree((xmlChar *) id->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002343 if (id->name != NULL)
2344 xmlFree((xmlChar *) id->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002345 xmlFree(id);
2346}
2347
2348/**
2349 * xmlAddID:
2350 * @ctxt: the validation context
2351 * @doc: pointer to the document
2352 * @value: the value name
2353 * @attr: the attribute holding the ID
2354 *
2355 * Register a new id declaration
2356 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002357 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002358 */
2359xmlIDPtr
2360xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2361 xmlAttrPtr attr) {
2362 xmlIDPtr ret;
2363 xmlIDTablePtr table;
2364
2365 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002366 return(NULL);
2367 }
2368 if (value == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002369 return(NULL);
2370 }
2371 if (attr == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002372 return(NULL);
2373 }
2374
2375 /*
2376 * Create the ID table if needed.
2377 */
2378 table = (xmlIDTablePtr) doc->ids;
2379 if (table == NULL)
2380 doc->ids = table = xmlCreateIDTable();
2381 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002382 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002383 "xmlAddID: Table creation failed!\n");
2384 return(NULL);
2385 }
2386
2387 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2388 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002389 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002390 return(NULL);
2391 }
2392
2393 /*
2394 * fill the structure.
2395 */
2396 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002397 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2398 /*
2399 * Operating in streaming mode, attr is gonna disapear
2400 */
2401 ret->name = xmlStrdup(attr->name);
2402 ret->attr = NULL;
2403 } else {
2404 ret->attr = attr;
2405 ret->name = NULL;
2406 }
2407 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002408
2409 if (xmlHashAddEntry(table, value, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002410#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002411 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002412 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002413 */
Daniel Veillard76575762002-09-05 14:21:15 +00002414 if (ctxt != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002415 xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2416 "ID %s already defined\n",
2417 value, NULL, NULL);
Daniel Veillard76575762002-09-05 14:21:15 +00002418 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002419#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002420 xmlFreeID(ret);
2421 return(NULL);
2422 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002423 if (attr != NULL)
2424 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002425 return(ret);
2426}
2427
2428/**
2429 * xmlFreeIDTable:
2430 * @table: An id table
2431 *
2432 * Deallocate the memory used by an ID hash table.
2433 */
2434void
2435xmlFreeIDTable(xmlIDTablePtr table) {
2436 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2437}
2438
2439/**
2440 * xmlIsID:
2441 * @doc: the document
2442 * @elem: the element carrying the attribute
2443 * @attr: the attribute
2444 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002445 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002446 * then this is done if DTD loading has been requested. In the case
2447 * of HTML documents parsed with the HTML parser, then ID detection is
2448 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002449 *
2450 * Returns 0 or 1 depending on the lookup result
2451 */
2452int
2453xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2454 if (doc == NULL) return(0);
2455 if (attr == NULL) return(0);
2456 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2457 return(0);
2458 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2459 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2460 (xmlStrEqual(BAD_CAST "name", attr->name)))
2461 return(1);
2462 return(0);
2463 } else {
2464 xmlAttributePtr attrDecl;
2465
2466 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002467 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00002468 xmlChar fn[50];
Daniel Veillard37f961d2002-07-06 17:53:56 +00002469 xmlChar *fullname;
Daniel Veillardc00cda82003-04-07 10:22:39 +00002470
2471 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002472 if (fullname == NULL)
2473 return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002474 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2475 attr->name);
2476 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2477 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2478 attr->name);
Daniel Veillardc00cda82003-04-07 10:22:39 +00002479 if ((fullname != fn) && (fullname != elem->name))
2480 xmlFree(fullname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002481 } else {
2482 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2483 attr->name);
2484 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2485 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2486 attr->name);
2487 }
Owen Taylor3473f882001-02-23 17:55:21 +00002488
2489 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2490 return(1);
2491 }
2492 return(0);
2493}
2494
2495/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002496 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002497 * @doc: the document
2498 * @attr: the attribute
2499 *
2500 * Remove the given attribute from the ID table maintained internally.
2501 *
2502 * Returns -1 if the lookup failed and 0 otherwise
2503 */
2504int
2505xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2506 xmlAttrPtr cur;
2507 xmlIDTablePtr table;
2508 xmlChar *ID;
2509
2510 if (doc == NULL) return(-1);
2511 if (attr == NULL) return(-1);
2512 table = (xmlIDTablePtr) doc->ids;
2513 if (table == NULL)
2514 return(-1);
2515
2516 if (attr == NULL)
2517 return(-1);
2518 ID = xmlNodeListGetString(doc, attr->children, 1);
2519 if (ID == NULL)
2520 return(-1);
2521 cur = xmlHashLookup(table, ID);
2522 if (cur != attr) {
2523 xmlFree(ID);
2524 return(-1);
2525 }
2526 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
2527 xmlFree(ID);
2528 return(0);
2529}
2530
2531/**
2532 * xmlGetID:
2533 * @doc: pointer to the document
2534 * @ID: the ID value
2535 *
2536 * Search the attribute declaring the given ID
2537 *
2538 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2539 */
2540xmlAttrPtr
2541xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2542 xmlIDTablePtr table;
2543 xmlIDPtr id;
2544
2545 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002546 return(NULL);
2547 }
2548
2549 if (ID == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002550 return(NULL);
2551 }
2552
2553 table = (xmlIDTablePtr) doc->ids;
2554 if (table == NULL)
2555 return(NULL);
2556
2557 id = xmlHashLookup(table, ID);
2558 if (id == NULL)
2559 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002560 if (id->attr == NULL) {
2561 /*
2562 * We are operating on a stream, return a well known reference
2563 * since the attribute node doesn't exist anymore
2564 */
2565 return((xmlAttrPtr) doc);
2566 }
Owen Taylor3473f882001-02-23 17:55:21 +00002567 return(id->attr);
2568}
2569
2570/************************************************************************
2571 * *
2572 * Refs *
2573 * *
2574 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002575typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002576{
2577 xmlListPtr l;
2578 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002579} xmlRemoveMemo;
2580
2581typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2582
2583typedef struct xmlValidateMemo_t
2584{
2585 xmlValidCtxtPtr ctxt;
2586 const xmlChar *name;
2587} xmlValidateMemo;
2588
2589typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002590
2591/**
2592 * xmlCreateRefTable:
2593 *
2594 * create and initialize an empty ref hash table.
2595 *
2596 * Returns the xmlRefTablePtr just created or NULL in case
2597 * of error.
2598 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002599static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002600xmlCreateRefTable(void) {
2601 return(xmlHashCreate(0));
2602}
2603
2604/**
2605 * xmlFreeRef:
2606 * @lk: A list link
2607 *
2608 * Deallocate the memory used by a ref definition
2609 */
2610static void
2611xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002612 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2613 if (ref == NULL) return;
2614 if (ref->value != NULL)
2615 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002616 if (ref->name != NULL)
2617 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002618 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002619}
2620
2621/**
2622 * xmlFreeRefList:
2623 * @list_ref: A list of references.
2624 *
2625 * Deallocate the memory used by a list of references
2626 */
2627static void
2628xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002629 if (list_ref == NULL) return;
2630 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002631}
2632
2633/**
2634 * xmlWalkRemoveRef:
2635 * @data: Contents of current link
2636 * @user: Value supplied by the user
2637 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002638 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002639 */
2640static int
2641xmlWalkRemoveRef(const void *data, const void *user)
2642{
Daniel Veillard37721922001-05-04 15:21:12 +00002643 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2644 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2645 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002646
Daniel Veillard37721922001-05-04 15:21:12 +00002647 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2648 xmlListRemoveFirst(ref_list, (void *)data);
2649 return 0;
2650 }
2651 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002652}
2653
2654/**
2655 * xmlAddRef:
2656 * @ctxt: the validation context
2657 * @doc: pointer to the document
2658 * @value: the value name
2659 * @attr: the attribute holding the Ref
2660 *
2661 * Register a new ref declaration
2662 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002663 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002664 */
2665xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002666xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002667 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002668 xmlRefPtr ret;
2669 xmlRefTablePtr table;
2670 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002671
Daniel Veillard37721922001-05-04 15:21:12 +00002672 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002673 return(NULL);
2674 }
2675 if (value == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002676 return(NULL);
2677 }
2678 if (attr == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002679 return(NULL);
2680 }
Owen Taylor3473f882001-02-23 17:55:21 +00002681
Daniel Veillard37721922001-05-04 15:21:12 +00002682 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002683 * Create the Ref table if needed.
2684 */
Daniel Veillard37721922001-05-04 15:21:12 +00002685 table = (xmlRefTablePtr) doc->refs;
2686 if (table == NULL)
2687 doc->refs = table = xmlCreateRefTable();
2688 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002689 xmlVErrMemory(ctxt,
Daniel Veillard37721922001-05-04 15:21:12 +00002690 "xmlAddRef: Table creation failed!\n");
2691 return(NULL);
2692 }
Owen Taylor3473f882001-02-23 17:55:21 +00002693
Daniel Veillard37721922001-05-04 15:21:12 +00002694 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2695 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002696 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard37721922001-05-04 15:21:12 +00002697 return(NULL);
2698 }
Owen Taylor3473f882001-02-23 17:55:21 +00002699
Daniel Veillard37721922001-05-04 15:21:12 +00002700 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002701 * fill the structure.
2702 */
Daniel Veillard37721922001-05-04 15:21:12 +00002703 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002704 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2705 /*
2706 * Operating in streaming mode, attr is gonna disapear
2707 */
2708 ret->name = xmlStrdup(attr->name);
2709 ret->attr = NULL;
2710 } else {
2711 ret->name = NULL;
2712 ret->attr = attr;
2713 }
2714 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002715
Daniel Veillard37721922001-05-04 15:21:12 +00002716 /* To add a reference :-
2717 * References are maintained as a list of references,
2718 * Lookup the entry, if no entry create new nodelist
2719 * Add the owning node to the NodeList
2720 * Return the ref
2721 */
Owen Taylor3473f882001-02-23 17:55:21 +00002722
Daniel Veillard37721922001-05-04 15:21:12 +00002723 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2724 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002725 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2726 "xmlAddRef: Reference list creation failed!\n",
2727 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002728 return(NULL);
2729 }
2730 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2731 xmlListDelete(ref_list);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002732 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2733 "xmlAddRef: Reference list insertion failed!\n",
2734 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002735 return(NULL);
2736 }
2737 }
2738 xmlListInsert(ref_list, ret);
2739 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002740}
2741
2742/**
2743 * xmlFreeRefTable:
2744 * @table: An ref table
2745 *
2746 * Deallocate the memory used by an Ref hash table.
2747 */
2748void
2749xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002750 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002751}
2752
2753/**
2754 * xmlIsRef:
2755 * @doc: the document
2756 * @elem: the element carrying the attribute
2757 * @attr: the attribute
2758 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002759 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002760 * then this is simple, otherwise we use an heuristic: name Ref (upper
2761 * or lowercase).
2762 *
2763 * Returns 0 or 1 depending on the lookup result
2764 */
2765int
2766xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002767 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2768 return(0);
2769 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2770 /* TODO @@@ */
2771 return(0);
2772 } else {
2773 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002774
Daniel Veillard37721922001-05-04 15:21:12 +00002775 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2776 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2777 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2778 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002779
Daniel Veillard37721922001-05-04 15:21:12 +00002780 if ((attrDecl != NULL) &&
2781 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2782 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2783 return(1);
2784 }
2785 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002786}
2787
2788/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002789 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00002790 * @doc: the document
2791 * @attr: the attribute
2792 *
2793 * Remove the given attribute from the Ref table maintained internally.
2794 *
2795 * Returns -1 if the lookup failed and 0 otherwise
2796 */
2797int
2798xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002799 xmlListPtr ref_list;
2800 xmlRefTablePtr table;
2801 xmlChar *ID;
2802 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002803
Daniel Veillard37721922001-05-04 15:21:12 +00002804 if (doc == NULL) return(-1);
2805 if (attr == NULL) return(-1);
2806 table = (xmlRefTablePtr) doc->refs;
2807 if (table == NULL)
2808 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002809
Daniel Veillard37721922001-05-04 15:21:12 +00002810 if (attr == NULL)
2811 return(-1);
2812 ID = xmlNodeListGetString(doc, attr->children, 1);
2813 if (ID == NULL)
2814 return(-1);
2815 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002816
Daniel Veillard37721922001-05-04 15:21:12 +00002817 if(ref_list == NULL) {
2818 xmlFree(ID);
2819 return (-1);
2820 }
2821 /* At this point, ref_list refers to a list of references which
2822 * have the same key as the supplied attr. Our list of references
2823 * is ordered by reference address and we don't have that information
2824 * here to use when removing. We'll have to walk the list and
2825 * check for a matching attribute, when we find one stop the walk
2826 * and remove the entry.
2827 * The list is ordered by reference, so that means we don't have the
2828 * key. Passing the list and the reference to the walker means we
2829 * will have enough data to be able to remove the entry.
2830 */
2831 target.l = ref_list;
2832 target.ap = attr;
2833
2834 /* Remove the supplied attr from our list */
2835 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002836
Daniel Veillard37721922001-05-04 15:21:12 +00002837 /*If the list is empty then remove the list entry in the hash */
2838 if (xmlListEmpty(ref_list))
2839 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2840 xmlFreeRefList);
2841 xmlFree(ID);
2842 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002843}
2844
2845/**
2846 * xmlGetRefs:
2847 * @doc: pointer to the document
2848 * @ID: the ID value
2849 *
2850 * Find the set of references for the supplied ID.
2851 *
2852 * Returns NULL if not found, otherwise node set for the ID.
2853 */
2854xmlListPtr
2855xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002856 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002857
Daniel Veillard37721922001-05-04 15:21:12 +00002858 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002859 return(NULL);
2860 }
Owen Taylor3473f882001-02-23 17:55:21 +00002861
Daniel Veillard37721922001-05-04 15:21:12 +00002862 if (ID == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002863 return(NULL);
2864 }
Owen Taylor3473f882001-02-23 17:55:21 +00002865
Daniel Veillard37721922001-05-04 15:21:12 +00002866 table = (xmlRefTablePtr) doc->refs;
2867 if (table == NULL)
2868 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002869
Daniel Veillard37721922001-05-04 15:21:12 +00002870 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002871}
2872
2873/************************************************************************
2874 * *
2875 * Routines for validity checking *
2876 * *
2877 ************************************************************************/
2878
2879/**
2880 * xmlGetDtdElementDesc:
2881 * @dtd: a pointer to the DtD to search
2882 * @name: the element name
2883 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002884 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002885 *
2886 * returns the xmlElementPtr if found or NULL
2887 */
2888
2889xmlElementPtr
2890xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2891 xmlElementTablePtr table;
2892 xmlElementPtr cur;
2893 xmlChar *uqname = NULL, *prefix = NULL;
2894
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002895 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002896 if (dtd->elements == NULL)
2897 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002898 table = (xmlElementTablePtr) dtd->elements;
2899
2900 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002901 if (uqname != NULL)
2902 name = uqname;
2903 cur = xmlHashLookup2(table, name, prefix);
2904 if (prefix != NULL) xmlFree(prefix);
2905 if (uqname != NULL) xmlFree(uqname);
2906 return(cur);
2907}
2908/**
2909 * xmlGetDtdElementDesc2:
2910 * @dtd: a pointer to the DtD to search
2911 * @name: the element name
2912 * @create: create an empty description if not found
2913 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002914 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002915 *
2916 * returns the xmlElementPtr if found or NULL
2917 */
2918
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002919static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002920xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2921 xmlElementTablePtr table;
2922 xmlElementPtr cur;
2923 xmlChar *uqname = NULL, *prefix = NULL;
2924
2925 if (dtd == NULL) return(NULL);
2926 if (dtd->elements == NULL) {
2927 if (!create)
2928 return(NULL);
2929 /*
2930 * Create the Element table if needed.
2931 */
2932 table = (xmlElementTablePtr) dtd->elements;
2933 if (table == NULL) {
2934 table = xmlCreateElementTable();
2935 dtd->elements = (void *) table;
2936 }
2937 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002938 xmlVErrMemory(NULL, "element table allocation failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002939 return(NULL);
2940 }
2941 }
2942 table = (xmlElementTablePtr) dtd->elements;
2943
2944 uqname = xmlSplitQName2(name, &prefix);
2945 if (uqname != NULL)
2946 name = uqname;
2947 cur = xmlHashLookup2(table, name, prefix);
2948 if ((cur == NULL) && (create)) {
2949 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2950 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002951 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002952 return(NULL);
2953 }
2954 memset(cur, 0, sizeof(xmlElement));
2955 cur->type = XML_ELEMENT_DECL;
2956
2957 /*
2958 * fill the structure.
2959 */
2960 cur->name = xmlStrdup(name);
2961 cur->prefix = xmlStrdup(prefix);
2962 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2963
2964 xmlHashAddEntry2(table, name, prefix, cur);
2965 }
2966 if (prefix != NULL) xmlFree(prefix);
2967 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002968 return(cur);
2969}
2970
2971/**
2972 * xmlGetDtdQElementDesc:
2973 * @dtd: a pointer to the DtD to search
2974 * @name: the element name
2975 * @prefix: the element namespace prefix
2976 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002977 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002978 *
2979 * returns the xmlElementPtr if found or NULL
2980 */
2981
Daniel Veillard48da9102001-08-07 01:10:10 +00002982xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002983xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2984 const xmlChar *prefix) {
2985 xmlElementTablePtr table;
2986
2987 if (dtd == NULL) return(NULL);
2988 if (dtd->elements == NULL) return(NULL);
2989 table = (xmlElementTablePtr) dtd->elements;
2990
2991 return(xmlHashLookup2(table, name, prefix));
2992}
2993
2994/**
2995 * xmlGetDtdAttrDesc:
2996 * @dtd: a pointer to the DtD to search
2997 * @elem: the element name
2998 * @name: the attribute name
2999 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003000 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003001 * this element.
3002 *
3003 * returns the xmlAttributePtr if found or NULL
3004 */
3005
3006xmlAttributePtr
3007xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3008 xmlAttributeTablePtr table;
3009 xmlAttributePtr cur;
3010 xmlChar *uqname = NULL, *prefix = NULL;
3011
3012 if (dtd == NULL) return(NULL);
3013 if (dtd->attributes == NULL) return(NULL);
3014
3015 table = (xmlAttributeTablePtr) dtd->attributes;
3016 if (table == NULL)
3017 return(NULL);
3018
3019 uqname = xmlSplitQName2(name, &prefix);
3020
3021 if (uqname != NULL) {
3022 cur = xmlHashLookup3(table, uqname, prefix, elem);
3023 if (prefix != NULL) xmlFree(prefix);
3024 if (uqname != NULL) xmlFree(uqname);
3025 } else
3026 cur = xmlHashLookup3(table, name, NULL, elem);
3027 return(cur);
3028}
3029
3030/**
3031 * xmlGetDtdQAttrDesc:
3032 * @dtd: a pointer to the DtD to search
3033 * @elem: the element name
3034 * @name: the attribute name
3035 * @prefix: the attribute namespace prefix
3036 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003037 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003038 * this element.
3039 *
3040 * returns the xmlAttributePtr if found or NULL
3041 */
3042
Daniel Veillard48da9102001-08-07 01:10:10 +00003043xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00003044xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3045 const xmlChar *prefix) {
3046 xmlAttributeTablePtr table;
3047
3048 if (dtd == NULL) return(NULL);
3049 if (dtd->attributes == NULL) return(NULL);
3050 table = (xmlAttributeTablePtr) dtd->attributes;
3051
3052 return(xmlHashLookup3(table, name, prefix, elem));
3053}
3054
3055/**
3056 * xmlGetDtdNotationDesc:
3057 * @dtd: a pointer to the DtD to search
3058 * @name: the notation name
3059 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003060 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00003061 *
3062 * returns the xmlNotationPtr if found or NULL
3063 */
3064
3065xmlNotationPtr
3066xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3067 xmlNotationTablePtr table;
3068
3069 if (dtd == NULL) return(NULL);
3070 if (dtd->notations == NULL) return(NULL);
3071 table = (xmlNotationTablePtr) dtd->notations;
3072
3073 return(xmlHashLookup(table, name));
3074}
3075
Daniel Veillard4432df22003-09-28 18:58:27 +00003076#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003077/**
3078 * xmlValidateNotationUse:
3079 * @ctxt: the validation context
3080 * @doc: the document
3081 * @notationName: the notation name to check
3082 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003083 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00003084 * - [ VC: Notation Declared ]
3085 *
3086 * returns 1 if valid or 0 otherwise
3087 */
3088
3089int
3090xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3091 const xmlChar *notationName) {
3092 xmlNotationPtr notaDecl;
3093 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3094
3095 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3096 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3097 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3098
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003099 if ((notaDecl == NULL) && (ctxt != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003100 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3101 "NOTATION %s is not declared\n",
3102 notationName, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003103 return(0);
3104 }
3105 return(1);
3106}
Daniel Veillard4432df22003-09-28 18:58:27 +00003107#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003108
3109/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003110 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00003111 * @doc: the document
3112 * @name: the element name
3113 *
3114 * Search in the DtDs whether an element accept Mixed content (or ANY)
3115 * basically if it is supposed to accept text childs
3116 *
3117 * returns 0 if no, 1 if yes, and -1 if no element description is available
3118 */
3119
3120int
3121xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3122 xmlElementPtr elemDecl;
3123
3124 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3125
3126 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3127 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3128 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3129 if (elemDecl == NULL) return(-1);
3130 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00003131 case XML_ELEMENT_TYPE_UNDEFINED:
3132 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003133 case XML_ELEMENT_TYPE_ELEMENT:
3134 return(0);
3135 case XML_ELEMENT_TYPE_EMPTY:
3136 /*
3137 * return 1 for EMPTY since we want VC error to pop up
3138 * on <empty> </empty> for example
3139 */
3140 case XML_ELEMENT_TYPE_ANY:
3141 case XML_ELEMENT_TYPE_MIXED:
3142 return(1);
3143 }
3144 return(1);
3145}
3146
Daniel Veillard4432df22003-09-28 18:58:27 +00003147#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003148/**
3149 * xmlValidateNameValue:
3150 * @value: an Name value
3151 *
3152 * Validate that the given value match Name production
3153 *
3154 * returns 1 if valid or 0 otherwise
3155 */
3156
Daniel Veillard9b731d72002-04-14 12:56:08 +00003157int
Owen Taylor3473f882001-02-23 17:55:21 +00003158xmlValidateNameValue(const xmlChar *value) {
3159 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003160 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003161
3162 if (value == NULL) return(0);
3163 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003164 val = xmlStringCurrentChar(NULL, cur, &len);
3165 cur += len;
3166 if (!IS_LETTER(val) && (val != '_') &&
3167 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003168 return(0);
3169 }
3170
Daniel Veillardd8224e02002-01-13 15:43:22 +00003171 val = xmlStringCurrentChar(NULL, cur, &len);
3172 cur += len;
3173 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3174 (val == '.') || (val == '-') ||
3175 (val == '_') || (val == ':') ||
3176 (IS_COMBINING(val)) ||
3177 (IS_EXTENDER(val))) {
3178 val = xmlStringCurrentChar(NULL, cur, &len);
3179 cur += len;
3180 }
Owen Taylor3473f882001-02-23 17:55:21 +00003181
Daniel Veillardd8224e02002-01-13 15:43:22 +00003182 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003183
3184 return(1);
3185}
3186
3187/**
3188 * xmlValidateNamesValue:
3189 * @value: an Names value
3190 *
3191 * Validate that the given value match Names production
3192 *
3193 * returns 1 if valid or 0 otherwise
3194 */
3195
Daniel Veillard9b731d72002-04-14 12:56:08 +00003196int
Owen Taylor3473f882001-02-23 17:55:21 +00003197xmlValidateNamesValue(const xmlChar *value) {
3198 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003199 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003200
3201 if (value == NULL) return(0);
3202 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003203 val = xmlStringCurrentChar(NULL, cur, &len);
3204 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003205
Daniel Veillardd8224e02002-01-13 15:43:22 +00003206 if (!IS_LETTER(val) && (val != '_') &&
3207 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003208 return(0);
3209 }
3210
Daniel Veillardd8224e02002-01-13 15:43:22 +00003211 val = xmlStringCurrentChar(NULL, cur, &len);
3212 cur += len;
3213 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3214 (val == '.') || (val == '-') ||
3215 (val == '_') || (val == ':') ||
3216 (IS_COMBINING(val)) ||
3217 (IS_EXTENDER(val))) {
3218 val = xmlStringCurrentChar(NULL, cur, &len);
3219 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003220 }
3221
Daniel Veillardd8224e02002-01-13 15:43:22 +00003222 while (IS_BLANK(val)) {
3223 while (IS_BLANK(val)) {
3224 val = xmlStringCurrentChar(NULL, cur, &len);
3225 cur += len;
3226 }
3227
3228 if (!IS_LETTER(val) && (val != '_') &&
3229 (val != ':')) {
3230 return(0);
3231 }
3232 val = xmlStringCurrentChar(NULL, cur, &len);
3233 cur += len;
3234
3235 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3236 (val == '.') || (val == '-') ||
3237 (val == '_') || (val == ':') ||
3238 (IS_COMBINING(val)) ||
3239 (IS_EXTENDER(val))) {
3240 val = xmlStringCurrentChar(NULL, cur, &len);
3241 cur += len;
3242 }
3243 }
3244
3245 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003246
3247 return(1);
3248}
3249
3250/**
3251 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003252 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003253 *
3254 * Validate that the given value match Nmtoken production
3255 *
3256 * [ VC: Name Token ]
3257 *
3258 * returns 1 if valid or 0 otherwise
3259 */
3260
Daniel Veillard9b731d72002-04-14 12:56:08 +00003261int
Owen Taylor3473f882001-02-23 17:55:21 +00003262xmlValidateNmtokenValue(const xmlChar *value) {
3263 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003264 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003265
3266 if (value == NULL) return(0);
3267 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003268 val = xmlStringCurrentChar(NULL, cur, &len);
3269 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003270
Daniel Veillardd8224e02002-01-13 15:43:22 +00003271 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3272 (val != '.') && (val != '-') &&
3273 (val != '_') && (val != ':') &&
3274 (!IS_COMBINING(val)) &&
3275 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003276 return(0);
3277
Daniel Veillardd8224e02002-01-13 15:43:22 +00003278 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3279 (val == '.') || (val == '-') ||
3280 (val == '_') || (val == ':') ||
3281 (IS_COMBINING(val)) ||
3282 (IS_EXTENDER(val))) {
3283 val = xmlStringCurrentChar(NULL, cur, &len);
3284 cur += len;
3285 }
Owen Taylor3473f882001-02-23 17:55:21 +00003286
Daniel Veillardd8224e02002-01-13 15:43:22 +00003287 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003288
3289 return(1);
3290}
3291
3292/**
3293 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003294 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003295 *
3296 * Validate that the given value match Nmtokens production
3297 *
3298 * [ VC: Name Token ]
3299 *
3300 * returns 1 if valid or 0 otherwise
3301 */
3302
Daniel Veillard9b731d72002-04-14 12:56:08 +00003303int
Owen Taylor3473f882001-02-23 17:55:21 +00003304xmlValidateNmtokensValue(const xmlChar *value) {
3305 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003306 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003307
3308 if (value == NULL) return(0);
3309 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003310 val = xmlStringCurrentChar(NULL, cur, &len);
3311 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003312
Daniel Veillardd8224e02002-01-13 15:43:22 +00003313 while (IS_BLANK(val)) {
3314 val = xmlStringCurrentChar(NULL, cur, &len);
3315 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003316 }
3317
Daniel Veillardd8224e02002-01-13 15:43:22 +00003318 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3319 (val != '.') && (val != '-') &&
3320 (val != '_') && (val != ':') &&
3321 (!IS_COMBINING(val)) &&
3322 (!IS_EXTENDER(val)))
3323 return(0);
3324
3325 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3326 (val == '.') || (val == '-') ||
3327 (val == '_') || (val == ':') ||
3328 (IS_COMBINING(val)) ||
3329 (IS_EXTENDER(val))) {
3330 val = xmlStringCurrentChar(NULL, cur, &len);
3331 cur += len;
3332 }
3333
3334 while (IS_BLANK(val)) {
3335 while (IS_BLANK(val)) {
3336 val = xmlStringCurrentChar(NULL, cur, &len);
3337 cur += len;
3338 }
3339 if (val == 0) return(1);
3340
3341 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3342 (val != '.') && (val != '-') &&
3343 (val != '_') && (val != ':') &&
3344 (!IS_COMBINING(val)) &&
3345 (!IS_EXTENDER(val)))
3346 return(0);
3347
3348 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3349 (val == '.') || (val == '-') ||
3350 (val == '_') || (val == ':') ||
3351 (IS_COMBINING(val)) ||
3352 (IS_EXTENDER(val))) {
3353 val = xmlStringCurrentChar(NULL, cur, &len);
3354 cur += len;
3355 }
3356 }
3357
3358 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003359
3360 return(1);
3361}
3362
3363/**
3364 * xmlValidateNotationDecl:
3365 * @ctxt: the validation context
3366 * @doc: a document instance
3367 * @nota: a notation definition
3368 *
3369 * Try to validate a single notation definition
3370 * basically it does the following checks as described by the
3371 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003372 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003373 * But this function get called anyway ...
3374 *
3375 * returns 1 if valid or 0 otherwise
3376 */
3377
3378int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003379xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3380 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003381 int ret = 1;
3382
3383 return(ret);
3384}
3385
3386/**
3387 * xmlValidateAttributeValue:
3388 * @type: an attribute type
3389 * @value: an attribute value
3390 *
3391 * Validate that the given attribute value match the proper production
3392 *
3393 * [ VC: ID ]
3394 * Values of type ID must match the Name production....
3395 *
3396 * [ VC: IDREF ]
3397 * Values of type IDREF must match the Name production, and values
3398 * of type IDREFS must match Names ...
3399 *
3400 * [ VC: Entity Name ]
3401 * Values of type ENTITY must match the Name production, values
3402 * of type ENTITIES must match Names ...
3403 *
3404 * [ VC: Name Token ]
3405 * Values of type NMTOKEN must match the Nmtoken production; values
3406 * of type NMTOKENS must match Nmtokens.
3407 *
3408 * returns 1 if valid or 0 otherwise
3409 */
3410
3411int
3412xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3413 switch (type) {
3414 case XML_ATTRIBUTE_ENTITIES:
3415 case XML_ATTRIBUTE_IDREFS:
3416 return(xmlValidateNamesValue(value));
3417 case XML_ATTRIBUTE_ENTITY:
3418 case XML_ATTRIBUTE_IDREF:
3419 case XML_ATTRIBUTE_ID:
3420 case XML_ATTRIBUTE_NOTATION:
3421 return(xmlValidateNameValue(value));
3422 case XML_ATTRIBUTE_NMTOKENS:
3423 case XML_ATTRIBUTE_ENUMERATION:
3424 return(xmlValidateNmtokensValue(value));
3425 case XML_ATTRIBUTE_NMTOKEN:
3426 return(xmlValidateNmtokenValue(value));
3427 case XML_ATTRIBUTE_CDATA:
3428 break;
3429 }
3430 return(1);
3431}
3432
3433/**
3434 * xmlValidateAttributeValue2:
3435 * @ctxt: the validation context
3436 * @doc: the document
3437 * @name: the attribute name (used for error reporting only)
3438 * @type: the attribute type
3439 * @value: the attribute value
3440 *
3441 * Validate that the given attribute value match a given type.
3442 * This typically cannot be done before having finished parsing
3443 * the subsets.
3444 *
3445 * [ VC: IDREF ]
3446 * Values of type IDREF must match one of the declared IDs
3447 * Values of type IDREFS must match a sequence of the declared IDs
3448 * each Name must match the value of an ID attribute on some element
3449 * in the XML document; i.e. IDREF values must match the value of
3450 * some ID attribute
3451 *
3452 * [ VC: Entity Name ]
3453 * Values of type ENTITY must match one declared entity
3454 * Values of type ENTITIES must match a sequence of declared entities
3455 *
3456 * [ VC: Notation Attributes ]
3457 * all notation names in the declaration must be declared.
3458 *
3459 * returns 1 if valid or 0 otherwise
3460 */
3461
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003462static int
Owen Taylor3473f882001-02-23 17:55:21 +00003463xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3464 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3465 int ret = 1;
3466 switch (type) {
3467 case XML_ATTRIBUTE_IDREFS:
3468 case XML_ATTRIBUTE_IDREF:
3469 case XML_ATTRIBUTE_ID:
3470 case XML_ATTRIBUTE_NMTOKENS:
3471 case XML_ATTRIBUTE_ENUMERATION:
3472 case XML_ATTRIBUTE_NMTOKEN:
3473 case XML_ATTRIBUTE_CDATA:
3474 break;
3475 case XML_ATTRIBUTE_ENTITY: {
3476 xmlEntityPtr ent;
3477
3478 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003479 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003480 if ((ent == NULL) && (doc->standalone == 1)) {
3481 doc->standalone = 0;
3482 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003483 }
Owen Taylor3473f882001-02-23 17:55:21 +00003484 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003485 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3486 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003487 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003488 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003489 ret = 0;
3490 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003491 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3492 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003493 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003494 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003495 ret = 0;
3496 }
3497 break;
3498 }
3499 case XML_ATTRIBUTE_ENTITIES: {
3500 xmlChar *dup, *nam = NULL, *cur, save;
3501 xmlEntityPtr ent;
3502
3503 dup = xmlStrdup(value);
3504 if (dup == NULL)
3505 return(0);
3506 cur = dup;
3507 while (*cur != 0) {
3508 nam = cur;
3509 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
3510 save = *cur;
3511 *cur = 0;
3512 ent = xmlGetDocEntity(doc, nam);
3513 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003514 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3515 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003516 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003517 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003518 ret = 0;
3519 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003520 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3521 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003522 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003523 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003524 ret = 0;
3525 }
3526 if (save == 0)
3527 break;
3528 *cur = save;
3529 while (IS_BLANK(*cur)) cur++;
3530 }
3531 xmlFree(dup);
3532 break;
3533 }
3534 case XML_ATTRIBUTE_NOTATION: {
3535 xmlNotationPtr nota;
3536
3537 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3538 if ((nota == NULL) && (doc->extSubset != NULL))
3539 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3540
3541 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003542 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3543 XML_DTD_UNKNOWN_NOTATION,
Owen Taylor3473f882001-02-23 17:55:21 +00003544 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003545 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003546 ret = 0;
3547 }
3548 break;
3549 }
3550 }
3551 return(ret);
3552}
3553
3554/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003555 * xmlValidCtxtNormalizeAttributeValue:
3556 * @ctxt: the validation context
3557 * @doc: the document
3558 * @elem: the parent
3559 * @name: the attribute name
3560 * @value: the attribute value
3561 * @ctxt: the validation context or NULL
3562 *
3563 * Does the validation related extra step of the normalization of attribute
3564 * values:
3565 *
3566 * If the declared value is not CDATA, then the XML processor must further
3567 * process the normalized attribute value by discarding any leading and
3568 * trailing space (#x20) characters, and by replacing sequences of space
3569 * (#x20) characters by single space (#x20) character.
3570 *
3571 * Also check VC: Standalone Document Declaration in P32, and update
3572 * ctxt->valid accordingly
3573 *
3574 * returns a new normalized string if normalization is needed, NULL otherwise
3575 * the caller must free the returned value.
3576 */
3577
3578xmlChar *
3579xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3580 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3581 xmlChar *ret, *dst;
3582 const xmlChar *src;
3583 xmlAttributePtr attrDecl = NULL;
3584 int extsubset = 0;
3585
3586 if (doc == NULL) return(NULL);
3587 if (elem == NULL) return(NULL);
3588 if (name == NULL) return(NULL);
3589 if (value == NULL) return(NULL);
3590
3591 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003592 xmlChar fn[50];
3593 xmlChar *fullname;
3594
3595 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3596 if (fullname == NULL)
3597 return(0);
3598 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003599 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003600 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003601 if (attrDecl != NULL)
3602 extsubset = 1;
3603 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003604 if ((fullname != fn) && (fullname != elem->name))
3605 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003606 }
3607 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3608 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3609 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3610 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3611 if (attrDecl != NULL)
3612 extsubset = 1;
3613 }
3614
3615 if (attrDecl == NULL)
3616 return(NULL);
3617 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3618 return(NULL);
3619
3620 ret = xmlStrdup(value);
3621 if (ret == NULL)
3622 return(NULL);
3623 src = value;
3624 dst = ret;
3625 while (*src == 0x20) src++;
3626 while (*src != 0) {
3627 if (*src == 0x20) {
3628 while (*src == 0x20) src++;
3629 if (*src != 0)
3630 *dst++ = 0x20;
3631 } else {
3632 *dst++ = *src++;
3633 }
3634 }
3635 *dst = 0;
3636 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003637 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003638"standalone: %s on %s value had to be normalized based on external subset declaration\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003639 name, elem->name, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003640 ctxt->valid = 0;
3641 }
3642 return(ret);
3643}
3644
3645/**
Owen Taylor3473f882001-02-23 17:55:21 +00003646 * xmlValidNormalizeAttributeValue:
3647 * @doc: the document
3648 * @elem: the parent
3649 * @name: the attribute name
3650 * @value: the attribute value
3651 *
3652 * Does the validation related extra step of the normalization of attribute
3653 * values:
3654 *
3655 * If the declared value is not CDATA, then the XML processor must further
3656 * process the normalized attribute value by discarding any leading and
3657 * trailing space (#x20) characters, and by replacing sequences of space
3658 * (#x20) characters by single space (#x20) character.
3659 *
Daniel Veillard652327a2003-09-29 18:02:38 +00003660 * Returns a new normalized string if normalization is needed, NULL otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00003661 * the caller must free the returned value.
3662 */
3663
3664xmlChar *
3665xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3666 const xmlChar *name, const xmlChar *value) {
3667 xmlChar *ret, *dst;
3668 const xmlChar *src;
3669 xmlAttributePtr attrDecl = NULL;
3670
3671 if (doc == NULL) return(NULL);
3672 if (elem == NULL) return(NULL);
3673 if (name == NULL) return(NULL);
3674 if (value == NULL) return(NULL);
3675
3676 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003677 xmlChar fn[50];
3678 xmlChar *fullname;
3679
3680 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3681 if (fullname == NULL)
3682 return(0);
3683 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003684 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003685 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3686 if ((fullname != fn) && (fullname != elem->name))
3687 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003688 }
3689 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3690 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3691 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3692
3693 if (attrDecl == NULL)
3694 return(NULL);
3695 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3696 return(NULL);
3697
3698 ret = xmlStrdup(value);
3699 if (ret == NULL)
3700 return(NULL);
3701 src = value;
3702 dst = ret;
3703 while (*src == 0x20) src++;
3704 while (*src != 0) {
3705 if (*src == 0x20) {
3706 while (*src == 0x20) src++;
3707 if (*src != 0)
3708 *dst++ = 0x20;
3709 } else {
3710 *dst++ = *src++;
3711 }
3712 }
3713 *dst = 0;
3714 return(ret);
3715}
3716
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003717static void
Owen Taylor3473f882001-02-23 17:55:21 +00003718xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003719 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003720 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3721}
3722
3723/**
3724 * xmlValidateAttributeDecl:
3725 * @ctxt: the validation context
3726 * @doc: a document instance
3727 * @attr: an attribute definition
3728 *
3729 * Try to validate a single attribute definition
3730 * basically it does the following checks as described by the
3731 * XML-1.0 recommendation:
3732 * - [ VC: Attribute Default Legal ]
3733 * - [ VC: Enumeration ]
3734 * - [ VC: ID Attribute Default ]
3735 *
3736 * The ID/IDREF uniqueness and matching are done separately
3737 *
3738 * returns 1 if valid or 0 otherwise
3739 */
3740
3741int
3742xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3743 xmlAttributePtr attr) {
3744 int ret = 1;
3745 int val;
3746 CHECK_DTD;
3747 if(attr == NULL) return(1);
3748
3749 /* Attribute Default Legal */
3750 /* Enumeration */
3751 if (attr->defaultValue != NULL) {
3752 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3753 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003754 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003755 "Syntax of default value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003756 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003757 }
3758 ret &= val;
3759 }
3760
3761 /* ID Attribute Default */
3762 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3763 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3764 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003765 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003766 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003767 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003768 ret = 0;
3769 }
3770
3771 /* One ID per Element Type */
3772 if (attr->atype == XML_ATTRIBUTE_ID) {
3773 int nbId;
3774
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003775 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003776 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3777 attr->elem);
3778 if (elem != NULL) {
3779 nbId = xmlScanIDAttributeDecl(NULL, elem);
3780 } else {
3781 xmlAttributeTablePtr table;
3782
3783 /*
3784 * The attribute may be declared in the internal subset and the
3785 * element in the external subset.
3786 */
3787 nbId = 0;
3788 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3789 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3790 xmlValidateAttributeIdCallback, &nbId);
3791 }
3792 if (nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003793
3794 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00003795 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3796 attr->elem, nbId, attr->name);
3797 } else if (doc->extSubset != NULL) {
3798 int extId = 0;
3799 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3800 if (elem != NULL) {
3801 extId = xmlScanIDAttributeDecl(NULL, elem);
3802 }
3803 if (extId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003804 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00003805 "Element %s has %d ID attribute defined in the external subset : %s\n",
3806 attr->elem, extId, attr->name);
3807 } else if (extId + nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003808 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00003809"Element %s has ID attributes defined in the internal and external subset : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003810 attr->elem, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003811 }
3812 }
3813 }
3814
3815 /* Validity Constraint: Enumeration */
3816 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3817 xmlEnumerationPtr tree = attr->tree;
3818 while (tree != NULL) {
3819 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3820 tree = tree->next;
3821 }
3822 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003823 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003824"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003825 attr->defaultValue, attr->name, attr->elem);
3826 ret = 0;
3827 }
3828 }
3829
3830 return(ret);
3831}
3832
3833/**
3834 * xmlValidateElementDecl:
3835 * @ctxt: the validation context
3836 * @doc: a document instance
3837 * @elem: an element definition
3838 *
3839 * Try to validate a single element definition
3840 * basically it does the following checks as described by the
3841 * XML-1.0 recommendation:
3842 * - [ VC: One ID per Element Type ]
3843 * - [ VC: No Duplicate Types ]
3844 * - [ VC: Unique Element Type Declaration ]
3845 *
3846 * returns 1 if valid or 0 otherwise
3847 */
3848
3849int
3850xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3851 xmlElementPtr elem) {
3852 int ret = 1;
3853 xmlElementPtr tst;
3854
3855 CHECK_DTD;
3856
3857 if (elem == NULL) return(1);
3858
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003859#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00003860#ifdef LIBXML_REGEXP_ENABLED
3861 /* Build the regexp associated to the content model */
3862 ret = xmlValidBuildContentModel(ctxt, elem);
3863#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003864#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00003865
Owen Taylor3473f882001-02-23 17:55:21 +00003866 /* No Duplicate Types */
3867 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3868 xmlElementContentPtr cur, next;
3869 const xmlChar *name;
3870
3871 cur = elem->content;
3872 while (cur != NULL) {
3873 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3874 if (cur->c1 == NULL) break;
3875 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3876 name = cur->c1->name;
3877 next = cur->c2;
3878 while (next != NULL) {
3879 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00003880 if ((xmlStrEqual(next->name, name)) &&
3881 (xmlStrEqual(next->prefix, cur->prefix))) {
3882 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003883 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00003884 "Definition of %s has duplicate references of %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003885 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00003886 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003887 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00003888 "Definition of %s has duplicate references of %s:%s\n",
3889 elem->name, cur->prefix, name);
3890 }
Owen Taylor3473f882001-02-23 17:55:21 +00003891 ret = 0;
3892 }
3893 break;
3894 }
3895 if (next->c1 == NULL) break;
3896 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00003897 if ((xmlStrEqual(next->c1->name, name)) &&
3898 (xmlStrEqual(next->c1->prefix, cur->prefix))) {
3899 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003900 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00003901 "Definition of %s has duplicate references to %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003902 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00003903 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003904 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00003905 "Definition of %s has duplicate references to %s:%s\n",
3906 elem->name, cur->prefix, name);
3907 }
Owen Taylor3473f882001-02-23 17:55:21 +00003908 ret = 0;
3909 }
3910 next = next->c2;
3911 }
3912 }
3913 cur = cur->c2;
3914 }
3915 }
3916
3917 /* VC: Unique Element Type Declaration */
3918 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003919 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003920 ((tst->prefix == elem->prefix) ||
3921 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003922 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003923 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
3924 "Redefinition of element %s\n",
3925 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003926 ret = 0;
3927 }
3928 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003929 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003930 ((tst->prefix == elem->prefix) ||
3931 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003932 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003933 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
3934 "Redefinition of element %s\n",
3935 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003936 ret = 0;
3937 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003938 /* One ID per Element Type
3939 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003940 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3941 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003942 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003943 return(ret);
3944}
3945
3946/**
3947 * xmlValidateOneAttribute:
3948 * @ctxt: the validation context
3949 * @doc: a document instance
3950 * @elem: an element instance
3951 * @attr: an attribute instance
3952 * @value: the attribute value (without entities processing)
3953 *
3954 * Try to validate a single attribute for an element
3955 * basically it does the following checks as described by the
3956 * XML-1.0 recommendation:
3957 * - [ VC: Attribute Value Type ]
3958 * - [ VC: Fixed Attribute Default ]
3959 * - [ VC: Entity Name ]
3960 * - [ VC: Name Token ]
3961 * - [ VC: ID ]
3962 * - [ VC: IDREF ]
3963 * - [ VC: Entity Name ]
3964 * - [ VC: Notation Attributes ]
3965 *
3966 * The ID/IDREF uniqueness and matching are done separately
3967 *
3968 * returns 1 if valid or 0 otherwise
3969 */
3970
3971int
3972xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillard07cb8222003-09-10 10:51:05 +00003973 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
3974{
Owen Taylor3473f882001-02-23 17:55:21 +00003975 xmlAttributePtr attrDecl = NULL;
3976 int val;
3977 int ret = 1;
3978
3979 CHECK_DTD;
3980 if ((elem == NULL) || (elem->name == NULL)) return(0);
3981 if ((attr == NULL) || (attr->name == NULL)) return(0);
3982
3983 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003984 xmlChar fn[50];
3985 xmlChar *fullname;
3986
3987 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3988 if (fullname == NULL)
3989 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003990 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003991 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00003992 attr->name, attr->ns->prefix);
3993 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003994 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00003995 attr->name, attr->ns->prefix);
3996 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003997 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003998 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3999 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00004000 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004001 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004002 if ((fullname != fn) && (fullname != elem->name))
4003 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00004004 }
4005 if (attrDecl == NULL) {
4006 if (attr->ns != NULL) {
4007 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4008 attr->name, attr->ns->prefix);
4009 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4010 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4011 attr->name, attr->ns->prefix);
4012 } else {
4013 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4014 elem->name, attr->name);
4015 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4016 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4017 elem->name, attr->name);
4018 }
4019 }
4020
4021
4022 /* Validity Constraint: Attribute Value Type */
4023 if (attrDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004024 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004025 "No declaration for attribute %s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004026 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004027 return(0);
4028 }
4029 attr->atype = attrDecl->atype;
4030
4031 val = xmlValidateAttributeValue(attrDecl->atype, value);
4032 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004033 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004034 "Syntax of value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004035 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004036 ret = 0;
4037 }
4038
4039 /* Validity constraint: Fixed Attribute Default */
4040 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4041 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004042 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004043 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004044 attr->name, elem->name, attrDecl->defaultValue);
4045 ret = 0;
4046 }
4047 }
4048
4049 /* Validity Constraint: ID uniqueness */
4050 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4051 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4052 ret = 0;
4053 }
4054
4055 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4056 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4057 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4058 ret = 0;
4059 }
4060
4061 /* Validity Constraint: Notation Attributes */
4062 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4063 xmlEnumerationPtr tree = attrDecl->tree;
4064 xmlNotationPtr nota;
4065
4066 /* First check that the given NOTATION was declared */
4067 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4068 if (nota == NULL)
4069 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4070
4071 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004072 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004073 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004074 value, attr->name, elem->name);
4075 ret = 0;
4076 }
4077
4078 /* Second, verify that it's among the list */
4079 while (tree != NULL) {
4080 if (xmlStrEqual(tree->name, value)) break;
4081 tree = tree->next;
4082 }
4083 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004084 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004085"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004086 value, attr->name, elem->name);
4087 ret = 0;
4088 }
4089 }
4090
4091 /* Validity Constraint: Enumeration */
4092 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4093 xmlEnumerationPtr tree = attrDecl->tree;
4094 while (tree != NULL) {
4095 if (xmlStrEqual(tree->name, value)) break;
4096 tree = tree->next;
4097 }
4098 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004099 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004100 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004101 value, attr->name, elem->name);
4102 ret = 0;
4103 }
4104 }
4105
4106 /* Fixed Attribute Default */
4107 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4108 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004109 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004110 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004111 attr->name, elem->name, attrDecl->defaultValue);
4112 ret = 0;
4113 }
4114
4115 /* Extra check for the attribute value */
4116 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4117 attrDecl->atype, value);
4118
4119 return(ret);
4120}
4121
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004122/**
4123 * xmlValidateOneNamespace:
4124 * @ctxt: the validation context
4125 * @doc: a document instance
4126 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004127 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004128 * @ns: an namespace declaration instance
4129 * @value: the attribute value (without entities processing)
4130 *
4131 * Try to validate a single namespace declaration for an element
4132 * basically it does the following checks as described by the
4133 * XML-1.0 recommendation:
4134 * - [ VC: Attribute Value Type ]
4135 * - [ VC: Fixed Attribute Default ]
4136 * - [ VC: Entity Name ]
4137 * - [ VC: Name Token ]
4138 * - [ VC: ID ]
4139 * - [ VC: IDREF ]
4140 * - [ VC: Entity Name ]
4141 * - [ VC: Notation Attributes ]
4142 *
4143 * The ID/IDREF uniqueness and matching are done separately
4144 *
4145 * returns 1 if valid or 0 otherwise
4146 */
4147
4148int
4149xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4150xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4151 /* xmlElementPtr elemDecl; */
4152 xmlAttributePtr attrDecl = NULL;
4153 int val;
4154 int ret = 1;
4155
4156 CHECK_DTD;
4157 if ((elem == NULL) || (elem->name == NULL)) return(0);
4158 if ((ns == NULL) || (ns->href == NULL)) return(0);
4159
4160 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004161 xmlChar fn[50];
4162 xmlChar *fullname;
4163
4164 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4165 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004166 xmlVErrMemory(ctxt, "Validating namespace");
Daniel Veillardc00cda82003-04-07 10:22:39 +00004167 return(0);
4168 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004169 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004170 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004171 ns->prefix, BAD_CAST "xmlns");
4172 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004173 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004174 ns->prefix, BAD_CAST "xmlns");
4175 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004176 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004177 BAD_CAST "xmlns");
4178 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004179 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004180 BAD_CAST "xmlns");
4181 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004182 if ((fullname != fn) && (fullname != elem->name))
4183 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004184 }
4185 if (attrDecl == NULL) {
4186 if (ns->prefix != NULL) {
4187 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4188 ns->prefix, BAD_CAST "xmlns");
4189 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4190 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4191 ns->prefix, BAD_CAST "xmlns");
4192 } else {
4193 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4194 elem->name, BAD_CAST "xmlns");
4195 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4196 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4197 elem->name, BAD_CAST "xmlns");
4198 }
4199 }
4200
4201
4202 /* Validity Constraint: Attribute Value Type */
4203 if (attrDecl == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004204 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004205 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004206 "No declaration for attribute xmlns:%s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004207 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004208 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004209 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004210 "No declaration for attribute xmlns of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004211 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004212 }
4213 return(0);
4214 }
4215
4216 val = xmlValidateAttributeValue(attrDecl->atype, value);
4217 if (val == 0) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004218 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004219 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004220 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004221 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004222 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004223 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004224 "Syntax of value for attribute xmlns of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004225 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004226 }
4227 ret = 0;
4228 }
4229
4230 /* Validity constraint: Fixed Attribute Default */
4231 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4232 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004233 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004234 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004235 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4236 ns->prefix, elem->name, attrDecl->defaultValue);
4237 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004238 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004239 "Value for attribute xmlns of %s is different from default \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004240 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004241 }
4242 ret = 0;
4243 }
4244 }
4245
4246 /* Validity Constraint: ID uniqueness */
4247 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4248 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4249 ret = 0;
4250 }
4251
4252 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4253 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4254 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4255 ret = 0;
4256 }
4257
4258 /* Validity Constraint: Notation Attributes */
4259 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4260 xmlEnumerationPtr tree = attrDecl->tree;
4261 xmlNotationPtr nota;
4262
4263 /* First check that the given NOTATION was declared */
4264 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4265 if (nota == NULL)
4266 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4267
4268 if (nota == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004269 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004270 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004271 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4272 value, ns->prefix, elem->name);
4273 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004274 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004275 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004276 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004277 }
4278 ret = 0;
4279 }
4280
4281 /* Second, verify that it's among the list */
4282 while (tree != NULL) {
4283 if (xmlStrEqual(tree->name, value)) break;
4284 tree = tree->next;
4285 }
4286 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004287 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004288 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004289"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4290 value, ns->prefix, elem->name);
4291 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004292 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004293"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004294 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004295 }
4296 ret = 0;
4297 }
4298 }
4299
4300 /* Validity Constraint: Enumeration */
4301 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4302 xmlEnumerationPtr tree = attrDecl->tree;
4303 while (tree != NULL) {
4304 if (xmlStrEqual(tree->name, value)) break;
4305 tree = tree->next;
4306 }
4307 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004308 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004309 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004310"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4311 value, ns->prefix, elem->name);
4312 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004313 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004314"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004315 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004316 }
4317 ret = 0;
4318 }
4319 }
4320
4321 /* Fixed Attribute Default */
4322 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4323 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004324 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004325 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004326 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4327 ns->prefix, elem->name, attrDecl->defaultValue);
4328 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004329 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004330 "Value for attribute xmlns of %s must be \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004331 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004332 }
4333 ret = 0;
4334 }
4335
4336 /* Extra check for the attribute value */
4337 if (ns->prefix != NULL) {
4338 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4339 attrDecl->atype, value);
4340 } else {
4341 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4342 attrDecl->atype, value);
4343 }
4344
4345 return(ret);
4346}
4347
Daniel Veillard118aed72002-09-24 14:13:13 +00004348#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004349/**
4350 * xmlValidateSkipIgnorable:
4351 * @ctxt: the validation context
4352 * @child: the child list
4353 *
4354 * Skip ignorable elements w.r.t. the validation process
4355 *
4356 * returns the first element to consider for validation of the content model
4357 */
4358
4359static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004360xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004361 while (child != NULL) {
4362 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004363 /* These things are ignored (skipped) during validation. */
4364 case XML_PI_NODE:
4365 case XML_COMMENT_NODE:
4366 case XML_XINCLUDE_START:
4367 case XML_XINCLUDE_END:
4368 child = child->next;
4369 break;
4370 case XML_TEXT_NODE:
4371 if (xmlIsBlankNode(child))
4372 child = child->next;
4373 else
4374 return(child);
4375 break;
4376 /* keep current node */
4377 default:
4378 return(child);
4379 }
4380 }
4381 return(child);
4382}
4383
4384/**
4385 * xmlValidateElementType:
4386 * @ctxt: the validation context
4387 *
4388 * Try to validate the content model of an element internal function
4389 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004390 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4391 * reference is found and -3 if the validation succeeded but
4392 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004393 */
4394
4395static int
4396xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004397 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004398 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004399
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004400 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004401 if ((NODE == NULL) && (CONT == NULL))
4402 return(1);
4403 if ((NODE == NULL) &&
4404 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4405 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4406 return(1);
4407 }
4408 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004409 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004410 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004411
4412 /*
4413 * We arrive here when more states need to be examined
4414 */
4415cont:
4416
4417 /*
4418 * We just recovered from a rollback generated by a possible
4419 * epsilon transition, go directly to the analysis phase
4420 */
4421 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004422 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004423 DEBUG_VALID_STATE(NODE, CONT)
4424 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004425 goto analyze;
4426 }
4427
4428 DEBUG_VALID_STATE(NODE, CONT)
4429 /*
4430 * we may have to save a backup state here. This is the equivalent
4431 * of handling epsilon transition in NFAs.
4432 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004433 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004434 ((CONT->parent == NULL) ||
4435 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004436 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004437 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004438 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004439 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004440 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4441 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004442 }
4443
4444
4445 /*
4446 * Check first if the content matches
4447 */
4448 switch (CONT->type) {
4449 case XML_ELEMENT_CONTENT_PCDATA:
4450 if (NODE == NULL) {
4451 DEBUG_VALID_MSG("pcdata failed no node");
4452 ret = 0;
4453 break;
4454 }
4455 if (NODE->type == XML_TEXT_NODE) {
4456 DEBUG_VALID_MSG("pcdata found, skip to next");
4457 /*
4458 * go to next element in the content model
4459 * skipping ignorable elems
4460 */
4461 do {
4462 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004463 NODE = xmlValidateSkipIgnorable(NODE);
4464 if ((NODE != NULL) &&
4465 (NODE->type == XML_ENTITY_REF_NODE))
4466 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004467 } while ((NODE != NULL) &&
4468 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004469 (NODE->type != XML_TEXT_NODE) &&
4470 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004471 ret = 1;
4472 break;
4473 } else {
4474 DEBUG_VALID_MSG("pcdata failed");
4475 ret = 0;
4476 break;
4477 }
4478 break;
4479 case XML_ELEMENT_CONTENT_ELEMENT:
4480 if (NODE == NULL) {
4481 DEBUG_VALID_MSG("element failed no node");
4482 ret = 0;
4483 break;
4484 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004485 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4486 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004487 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004488 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4489 ret = (CONT->prefix == NULL);
4490 } else if (CONT->prefix == NULL) {
4491 ret = 0;
4492 } else {
4493 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4494 }
4495 }
4496 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004497 DEBUG_VALID_MSG("element found, skip to next");
4498 /*
4499 * go to next element in the content model
4500 * skipping ignorable elems
4501 */
4502 do {
4503 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004504 NODE = xmlValidateSkipIgnorable(NODE);
4505 if ((NODE != NULL) &&
4506 (NODE->type == XML_ENTITY_REF_NODE))
4507 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004508 } while ((NODE != NULL) &&
4509 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004510 (NODE->type != XML_TEXT_NODE) &&
4511 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004512 } else {
4513 DEBUG_VALID_MSG("element failed");
4514 ret = 0;
4515 break;
4516 }
4517 break;
4518 case XML_ELEMENT_CONTENT_OR:
4519 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004520 * Small optimization.
4521 */
4522 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4523 if ((NODE == NULL) ||
4524 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4525 DEPTH++;
4526 CONT = CONT->c2;
4527 goto cont;
4528 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004529 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4530 ret = (CONT->c1->prefix == NULL);
4531 } else if (CONT->c1->prefix == NULL) {
4532 ret = 0;
4533 } else {
4534 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4535 }
4536 if (ret == 0) {
4537 DEPTH++;
4538 CONT = CONT->c2;
4539 goto cont;
4540 }
Daniel Veillard85349052001-04-20 13:48:21 +00004541 }
4542
4543 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004544 * save the second branch 'or' branch
4545 */
4546 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004547 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4548 OCCURS, ROLLBACK_OR) < 0)
4549 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004550 DEPTH++;
4551 CONT = CONT->c1;
4552 goto cont;
4553 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004554 /*
4555 * Small optimization.
4556 */
4557 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4558 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4559 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4560 if ((NODE == NULL) ||
4561 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4562 DEPTH++;
4563 CONT = CONT->c2;
4564 goto cont;
4565 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004566 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4567 ret = (CONT->c1->prefix == NULL);
4568 } else if (CONT->c1->prefix == NULL) {
4569 ret = 0;
4570 } else {
4571 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4572 }
4573 if (ret == 0) {
4574 DEPTH++;
4575 CONT = CONT->c2;
4576 goto cont;
4577 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004578 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004579 DEPTH++;
4580 CONT = CONT->c1;
4581 goto cont;
4582 }
4583
4584 /*
4585 * At this point handle going up in the tree
4586 */
4587 if (ret == -1) {
4588 DEBUG_VALID_MSG("error found returning");
4589 return(ret);
4590 }
4591analyze:
4592 while (CONT != NULL) {
4593 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004594 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004595 * this level.
4596 */
4597 if (ret == 0) {
4598 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004599 xmlNodePtr cur;
4600
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004601 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004602 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004603 DEBUG_VALID_MSG("Once branch failed, rollback");
4604 if (vstateVPop(ctxt) < 0 ) {
4605 DEBUG_VALID_MSG("exhaustion, failed");
4606 return(0);
4607 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004608 if (cur != ctxt->vstate->node)
4609 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004610 goto cont;
4611 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004612 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004613 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004614 DEBUG_VALID_MSG("Plus branch failed, rollback");
4615 if (vstateVPop(ctxt) < 0 ) {
4616 DEBUG_VALID_MSG("exhaustion, failed");
4617 return(0);
4618 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004619 if (cur != ctxt->vstate->node)
4620 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004621 goto cont;
4622 }
4623 DEBUG_VALID_MSG("Plus branch found");
4624 ret = 1;
4625 break;
4626 case XML_ELEMENT_CONTENT_MULT:
4627#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004628 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004629 DEBUG_VALID_MSG("Mult branch failed");
4630 } else {
4631 DEBUG_VALID_MSG("Mult branch found");
4632 }
4633#endif
4634 ret = 1;
4635 break;
4636 case XML_ELEMENT_CONTENT_OPT:
4637 DEBUG_VALID_MSG("Option branch failed");
4638 ret = 1;
4639 break;
4640 }
4641 } else {
4642 switch (CONT->ocur) {
4643 case XML_ELEMENT_CONTENT_OPT:
4644 DEBUG_VALID_MSG("Option branch succeeded");
4645 ret = 1;
4646 break;
4647 case XML_ELEMENT_CONTENT_ONCE:
4648 DEBUG_VALID_MSG("Once branch succeeded");
4649 ret = 1;
4650 break;
4651 case XML_ELEMENT_CONTENT_PLUS:
4652 if (STATE == ROLLBACK_PARENT) {
4653 DEBUG_VALID_MSG("Plus branch rollback");
4654 ret = 1;
4655 break;
4656 }
4657 if (NODE == NULL) {
4658 DEBUG_VALID_MSG("Plus branch exhausted");
4659 ret = 1;
4660 break;
4661 }
4662 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004663 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004664 goto cont;
4665 case XML_ELEMENT_CONTENT_MULT:
4666 if (STATE == ROLLBACK_PARENT) {
4667 DEBUG_VALID_MSG("Mult branch rollback");
4668 ret = 1;
4669 break;
4670 }
4671 if (NODE == NULL) {
4672 DEBUG_VALID_MSG("Mult branch exhausted");
4673 ret = 1;
4674 break;
4675 }
4676 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004677 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004678 goto cont;
4679 }
4680 }
4681 STATE = 0;
4682
4683 /*
4684 * Then act accordingly at the parent level
4685 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004686 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004687 if (CONT->parent == NULL)
4688 break;
4689
4690 switch (CONT->parent->type) {
4691 case XML_ELEMENT_CONTENT_PCDATA:
4692 DEBUG_VALID_MSG("Error: parent pcdata");
4693 return(-1);
4694 case XML_ELEMENT_CONTENT_ELEMENT:
4695 DEBUG_VALID_MSG("Error: parent element");
4696 return(-1);
4697 case XML_ELEMENT_CONTENT_OR:
4698 if (ret == 1) {
4699 DEBUG_VALID_MSG("Or succeeded");
4700 CONT = CONT->parent;
4701 DEPTH--;
4702 } else {
4703 DEBUG_VALID_MSG("Or failed");
4704 CONT = CONT->parent;
4705 DEPTH--;
4706 }
4707 break;
4708 case XML_ELEMENT_CONTENT_SEQ:
4709 if (ret == 0) {
4710 DEBUG_VALID_MSG("Sequence failed");
4711 CONT = CONT->parent;
4712 DEPTH--;
4713 } else if (CONT == CONT->parent->c1) {
4714 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4715 CONT = CONT->parent->c2;
4716 goto cont;
4717 } else {
4718 DEBUG_VALID_MSG("Sequence succeeded");
4719 CONT = CONT->parent;
4720 DEPTH--;
4721 }
4722 }
4723 }
4724 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004725 xmlNodePtr cur;
4726
4727 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004728 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4729 if (vstateVPop(ctxt) < 0 ) {
4730 DEBUG_VALID_MSG("exhaustion, failed");
4731 return(0);
4732 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004733 if (cur != ctxt->vstate->node)
4734 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004735 goto cont;
4736 }
4737 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004738 xmlNodePtr cur;
4739
4740 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004741 DEBUG_VALID_MSG("Failure, rollback");
4742 if (vstateVPop(ctxt) < 0 ) {
4743 DEBUG_VALID_MSG("exhaustion, failed");
4744 return(0);
4745 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004746 if (cur != ctxt->vstate->node)
4747 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004748 goto cont;
4749 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004750 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004751}
Daniel Veillard23e73572002-09-19 19:56:43 +00004752#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004753
4754/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00004755 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004756 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00004757 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004758 * @content: An element
4759 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
4760 *
4761 * This will dump the list of elements to the buffer
4762 * Intended just for the debug routine
4763 */
4764static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00004765xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004766 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00004767 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004768
4769 if (node == NULL) return;
4770 if (glob) strcat(buf, "(");
4771 cur = node;
4772 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004773 len = strlen(buf);
4774 if (size - len < 50) {
4775 if ((size - len > 4) && (buf[len - 1] != '.'))
4776 strcat(buf, " ...");
4777 return;
4778 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004779 switch (cur->type) {
4780 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004781 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004782 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004783 if ((size - len > 4) && (buf[len - 1] != '.'))
4784 strcat(buf, " ...");
4785 return;
4786 }
4787 strcat(buf, (char *) cur->ns->prefix);
4788 strcat(buf, ":");
4789 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004790 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004791 if ((size - len > 4) && (buf[len - 1] != '.'))
4792 strcat(buf, " ...");
4793 return;
4794 }
4795 strcat(buf, (char *) cur->name);
4796 if (cur->next != NULL)
4797 strcat(buf, " ");
4798 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004799 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004800 if (xmlIsBlankNode(cur))
4801 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004802 case XML_CDATA_SECTION_NODE:
4803 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004804 strcat(buf, "CDATA");
4805 if (cur->next != NULL)
4806 strcat(buf, " ");
4807 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004808 case XML_ATTRIBUTE_NODE:
4809 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004810#ifdef LIBXML_DOCB_ENABLED
4811 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004812#endif
4813 case XML_HTML_DOCUMENT_NODE:
4814 case XML_DOCUMENT_TYPE_NODE:
4815 case XML_DOCUMENT_FRAG_NODE:
4816 case XML_NOTATION_NODE:
4817 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004818 strcat(buf, "???");
4819 if (cur->next != NULL)
4820 strcat(buf, " ");
4821 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004822 case XML_ENTITY_NODE:
4823 case XML_PI_NODE:
4824 case XML_DTD_NODE:
4825 case XML_COMMENT_NODE:
4826 case XML_ELEMENT_DECL:
4827 case XML_ATTRIBUTE_DECL:
4828 case XML_ENTITY_DECL:
4829 case XML_XINCLUDE_START:
4830 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004831 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004832 }
4833 cur = cur->next;
4834 }
4835 if (glob) strcat(buf, ")");
4836}
4837
4838/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004839 * xmlValidateElementContent:
4840 * @ctxt: the validation context
4841 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004842 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004843 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004844 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004845 *
4846 * Try to validate the content model of an element
4847 *
4848 * returns 1 if valid or 0 if not and -1 in case of error
4849 */
4850
4851static int
4852xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004853 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004854 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00004855#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00004856 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00004857#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00004858 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004859 xmlElementContentPtr cont;
4860 const xmlChar *name;
4861
4862 if (elemDecl == NULL)
4863 return(-1);
4864 cont = elemDecl->content;
4865 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004866
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004867#ifdef LIBXML_REGEXP_ENABLED
4868 /* Build the regexp associated to the content model */
4869 if (elemDecl->contModel == NULL)
4870 ret = xmlValidBuildContentModel(ctxt, elemDecl);
4871 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004872 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004873 } else {
4874 xmlRegExecCtxtPtr exec;
4875
Daniel Veillardec498e12003-02-05 11:01:50 +00004876 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
4877 return(-1);
4878 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004879 ctxt->nodeMax = 0;
4880 ctxt->nodeNr = 0;
4881 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004882 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
4883 if (exec != NULL) {
4884 cur = child;
4885 while (cur != NULL) {
4886 switch (cur->type) {
4887 case XML_ENTITY_REF_NODE:
4888 /*
4889 * Push the current node to be able to roll back
4890 * and process within the entity
4891 */
4892 if ((cur->children != NULL) &&
4893 (cur->children->children != NULL)) {
4894 nodeVPush(ctxt, cur);
4895 cur = cur->children->children;
4896 continue;
4897 }
4898 break;
4899 case XML_TEXT_NODE:
4900 if (xmlIsBlankNode(cur))
4901 break;
4902 ret = 0;
4903 goto fail;
4904 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00004905 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004906 ret = 0;
4907 goto fail;
4908 case XML_ELEMENT_NODE:
4909 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004910 xmlChar fn[50];
4911 xmlChar *fullname;
4912
4913 fullname = xmlBuildQName(cur->name,
4914 cur->ns->prefix, fn, 50);
4915 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004916 ret = -1;
4917 goto fail;
4918 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004919 ret = xmlRegExecPushString(exec, fullname, NULL);
4920 if ((fullname != fn) && (fullname != cur->name))
4921 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004922 } else {
4923 ret = xmlRegExecPushString(exec, cur->name, NULL);
4924 }
4925 break;
4926 default:
4927 break;
4928 }
4929 /*
4930 * Switch to next element
4931 */
4932 cur = cur->next;
4933 while (cur == NULL) {
4934 cur = nodeVPop(ctxt);
4935 if (cur == NULL)
4936 break;
4937 cur = cur->next;
4938 }
4939 }
4940 ret = xmlRegExecPushString(exec, NULL, NULL);
4941fail:
4942 xmlRegFreeExecCtxt(exec);
4943 }
4944 }
4945#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004946 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004947 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004948 */
4949 ctxt->vstateMax = 8;
4950 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4951 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4952 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004953 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004954 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004955 }
4956 /*
4957 * The first entry in the stack is reserved to the current state
4958 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004959 ctxt->nodeMax = 0;
4960 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004961 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004962 ctxt->vstate = &ctxt->vstateTab[0];
4963 ctxt->vstateNr = 1;
4964 CONT = cont;
4965 NODE = child;
4966 DEPTH = 0;
4967 OCCURS = 0;
4968 STATE = 0;
4969 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004970 if ((ret == -3) && (warn)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004971 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
4972 "Content model for Element %s is ambiguous\n",
4973 name);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004974 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004975 /*
4976 * An entities reference appeared at this level.
4977 * Buid a minimal representation of this node content
4978 * sufficient to run the validation process on it
4979 */
4980 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004981 cur = child;
4982 while (cur != NULL) {
4983 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004984 case XML_ENTITY_REF_NODE:
4985 /*
4986 * Push the current node to be able to roll back
4987 * and process within the entity
4988 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004989 if ((cur->children != NULL) &&
4990 (cur->children->children != NULL)) {
4991 nodeVPush(ctxt, cur);
4992 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004993 continue;
4994 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004995 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004996 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004997 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004998 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004999 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005000 case XML_CDATA_SECTION_NODE:
5001 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005002 case XML_ELEMENT_NODE:
5003 /*
5004 * Allocate a new node and minimally fills in
5005 * what's required
5006 */
5007 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5008 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005009 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005010 xmlFreeNodeList(repl);
5011 ret = -1;
5012 goto done;
5013 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005014 tmp->type = cur->type;
5015 tmp->name = cur->name;
5016 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005017 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00005018 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005019 if (repl == NULL)
5020 repl = last = tmp;
5021 else {
5022 last->next = tmp;
5023 last = tmp;
5024 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005025 if (cur->type == XML_CDATA_SECTION_NODE) {
5026 /*
5027 * E59 spaces in CDATA does not match the
5028 * nonterminal S
5029 */
5030 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5031 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005032 break;
5033 default:
5034 break;
5035 }
5036 /*
5037 * Switch to next element
5038 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005039 cur = cur->next;
5040 while (cur == NULL) {
5041 cur = nodeVPop(ctxt);
5042 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005043 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005044 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005045 }
5046 }
5047
5048 /*
5049 * Relaunch the validation
5050 */
5051 ctxt->vstate = &ctxt->vstateTab[0];
5052 ctxt->vstateNr = 1;
5053 CONT = cont;
5054 NODE = repl;
5055 DEPTH = 0;
5056 OCCURS = 0;
5057 STATE = 0;
5058 ret = xmlValidateElementType(ctxt);
5059 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005060#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005061 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005062 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
5063 char expr[5000];
5064 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005065
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005066 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005067 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005068 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00005069#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005070 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005071 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005072 else
Daniel Veillard01992e02002-10-09 10:20:30 +00005073#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005074 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005075
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005076 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005077 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5078 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5079 name, BAD_CAST expr, BAD_CAST list);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005080 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005081 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5082 "Element content does not follow the DTD, expecting %s, got %s\n",
5083 BAD_CAST expr, BAD_CAST list, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005084 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005085 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005086 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005087 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005088 "Element %s content does not follow the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005089 name, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005090 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005091 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5092 "Element content does not follow the DTD\n",
5093 NULL, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005094 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005095 }
5096 ret = 0;
5097 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005098 if (ret == -3)
5099 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005100
Daniel Veillard23e73572002-09-19 19:56:43 +00005101#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005102done:
5103 /*
5104 * Deallocate the copy if done, and free up the validation stack
5105 */
5106 while (repl != NULL) {
5107 tmp = repl->next;
5108 xmlFree(repl);
5109 repl = tmp;
5110 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005111 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005112 if (ctxt->vstateTab != NULL) {
5113 xmlFree(ctxt->vstateTab);
5114 ctxt->vstateTab = NULL;
5115 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005116#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005117 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00005118 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005119 if (ctxt->nodeTab != NULL) {
5120 xmlFree(ctxt->nodeTab);
5121 ctxt->nodeTab = NULL;
5122 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005123 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005124
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005125}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005126
Owen Taylor3473f882001-02-23 17:55:21 +00005127/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005128 * xmlValidateCdataElement:
5129 * @ctxt: the validation context
5130 * @doc: a document instance
5131 * @elem: an element instance
5132 *
5133 * Check that an element follows #CDATA
5134 *
5135 * returns 1 if valid or 0 otherwise
5136 */
5137static int
5138xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5139 xmlNodePtr elem) {
5140 int ret = 1;
5141 xmlNodePtr cur, child;
5142
Daniel Veillardceb09b92002-10-04 11:46:37 +00005143 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005144 return(0);
5145
5146 child = elem->children;
5147
5148 cur = child;
5149 while (cur != NULL) {
5150 switch (cur->type) {
5151 case XML_ENTITY_REF_NODE:
5152 /*
5153 * Push the current node to be able to roll back
5154 * and process within the entity
5155 */
5156 if ((cur->children != NULL) &&
5157 (cur->children->children != NULL)) {
5158 nodeVPush(ctxt, cur);
5159 cur = cur->children->children;
5160 continue;
5161 }
5162 break;
5163 case XML_COMMENT_NODE:
5164 case XML_PI_NODE:
5165 case XML_TEXT_NODE:
5166 case XML_CDATA_SECTION_NODE:
5167 break;
5168 default:
5169 ret = 0;
5170 goto done;
5171 }
5172 /*
5173 * Switch to next element
5174 */
5175 cur = cur->next;
5176 while (cur == NULL) {
5177 cur = nodeVPop(ctxt);
5178 if (cur == NULL)
5179 break;
5180 cur = cur->next;
5181 }
5182 }
5183done:
5184 ctxt->nodeMax = 0;
5185 ctxt->nodeNr = 0;
5186 if (ctxt->nodeTab != NULL) {
5187 xmlFree(ctxt->nodeTab);
5188 ctxt->nodeTab = NULL;
5189 }
5190 return(ret);
5191}
5192
5193/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005194 * xmlValidateCheckMixed:
5195 * @ctxt: the validation context
5196 * @cont: the mixed content model
5197 * @qname: the qualified name as appearing in the serialization
5198 *
5199 * Check if the given node is part of the content model.
5200 *
5201 * Returns 1 if yes, 0 if no, -1 in case of error
5202 */
5203static int
5204xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
5205 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005206 const xmlChar *name;
5207 int plen;
5208 name = xmlSplitQName3(qname, &plen);
5209
5210 if (name == NULL) {
5211 while (cont != NULL) {
5212 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5213 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5214 return(1);
5215 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5216 (cont->c1 != NULL) &&
5217 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5218 if ((cont->c1->prefix == NULL) &&
5219 (xmlStrEqual(cont->c1->name, qname)))
5220 return(1);
5221 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5222 (cont->c1 == NULL) ||
5223 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005224 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5225 "Internal: MIXED struct corrupted\n",
5226 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005227 break;
5228 }
5229 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005230 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005231 } else {
5232 while (cont != NULL) {
5233 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5234 if ((cont->prefix != NULL) &&
5235 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5236 (xmlStrEqual(cont->name, name)))
5237 return(1);
5238 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5239 (cont->c1 != NULL) &&
5240 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5241 if ((cont->c1->prefix != NULL) &&
5242 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5243 (xmlStrEqual(cont->c1->name, name)))
5244 return(1);
5245 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5246 (cont->c1 == NULL) ||
5247 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005248 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5249 "Internal: MIXED struct corrupted\n",
5250 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005251 break;
5252 }
5253 cont = cont->c2;
5254 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005255 }
5256 return(0);
5257}
5258
5259/**
5260 * xmlValidGetElemDecl:
5261 * @ctxt: the validation context
5262 * @doc: a document instance
5263 * @elem: an element instance
5264 * @extsubset: pointer, (out) indicate if the declaration was found
5265 * in the external subset.
5266 *
5267 * Finds a declaration associated to an element in the document.
5268 *
5269 * returns the pointer to the declaration or NULL if not found.
5270 */
5271static xmlElementPtr
5272xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5273 xmlNodePtr elem, int *extsubset) {
5274 xmlElementPtr elemDecl = NULL;
5275 const xmlChar *prefix = NULL;
5276
5277 if ((elem == NULL) || (elem->name == NULL)) return(NULL);
5278 if (extsubset != NULL)
5279 *extsubset = 0;
5280
5281 /*
5282 * Fetch the declaration for the qualified name
5283 */
5284 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5285 prefix = elem->ns->prefix;
5286
5287 if (prefix != NULL) {
5288 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5289 elem->name, prefix);
5290 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5291 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5292 elem->name, prefix);
5293 if ((elemDecl != NULL) && (extsubset != NULL))
5294 *extsubset = 1;
5295 }
5296 }
5297
5298 /*
5299 * Fetch the declaration for the non qualified name
5300 * This is "non-strict" validation should be done on the
5301 * full QName but in that case being flexible makes sense.
5302 */
5303 if (elemDecl == NULL) {
5304 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5305 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5306 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5307 if ((elemDecl != NULL) && (extsubset != NULL))
5308 *extsubset = 1;
5309 }
5310 }
5311 if (elemDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005312 xmlErrValidNode(ctxt, elem,
5313 XML_DTD_UNKNOWN_ELEM,
5314 "No declaration for element %s\n",
5315 elem->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005316 }
5317 return(elemDecl);
5318}
5319
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005320#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005321/**
5322 * xmlValidatePushElement:
5323 * @ctxt: the validation context
5324 * @doc: a document instance
5325 * @elem: an element instance
5326 * @qname: the qualified name as appearing in the serialization
5327 *
5328 * Push a new element start on the validation stack.
5329 *
5330 * returns 1 if no validation problem was found or 0 otherwise
5331 */
5332int
5333xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5334 xmlNodePtr elem, const xmlChar *qname) {
5335 int ret = 1;
5336 xmlElementPtr eDecl;
5337 int extsubset = 0;
5338
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005339/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005340 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5341 xmlValidStatePtr state = ctxt->vstate;
5342 xmlElementPtr elemDecl;
5343
5344 /*
5345 * Check the new element agaisnt the content model of the new elem.
5346 */
5347 if (state->elemDecl != NULL) {
5348 elemDecl = state->elemDecl;
5349
5350 switch(elemDecl->etype) {
5351 case XML_ELEMENT_TYPE_UNDEFINED:
5352 ret = 0;
5353 break;
5354 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005355 xmlErrValidNode(ctxt, state->node,
5356 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005357 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005358 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005359 ret = 0;
5360 break;
5361 case XML_ELEMENT_TYPE_ANY:
5362 /* I don't think anything is required then */
5363 break;
5364 case XML_ELEMENT_TYPE_MIXED:
5365 /* simple case of declared as #PCDATA */
5366 if ((elemDecl->content != NULL) &&
5367 (elemDecl->content->type ==
5368 XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005369 xmlErrValidNode(ctxt, state->node,
5370 XML_DTD_NOT_PCDATA,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005371 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005372 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005373 ret = 0;
5374 } else {
5375 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5376 qname);
5377 if (ret != 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005378 xmlErrValidNode(ctxt, state->node,
5379 XML_DTD_INVALID_CHILD,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005380 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005381 qname, state->node->name, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005382 }
5383 }
5384 break;
5385 case XML_ELEMENT_TYPE_ELEMENT:
5386 /*
5387 * TODO:
5388 * VC: Standalone Document Declaration
5389 * - element types with element content, if white space
5390 * occurs directly within any instance of those types.
5391 */
5392 if (state->exec != NULL) {
5393 ret = xmlRegExecPushString(state->exec, qname, NULL);
5394 if (ret < 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005395 xmlErrValidNode(ctxt, state->node,
5396 XML_DTD_CONTENT_MODEL,
5397 "Element %s content does not follow the DTD, Misplaced %s\n",
5398 state->node->name, qname, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005399 ret = 0;
5400 } else {
5401 ret = 1;
5402 }
5403 }
5404 break;
5405 }
5406 }
5407 }
5408 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5409 vstateVPush(ctxt, eDecl, elem);
5410 return(ret);
5411}
5412
5413/**
5414 * xmlValidatePushCData:
5415 * @ctxt: the validation context
5416 * @data: some character data read
5417 * @len: the lenght of the data
5418 *
5419 * check the CData parsed for validation in the current stack
5420 *
5421 * returns 1 if no validation problem was found or 0 otherwise
5422 */
5423int
5424xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5425 int ret = 1;
5426
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005427/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005428 if (len <= 0)
5429 return(ret);
5430 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5431 xmlValidStatePtr state = ctxt->vstate;
5432 xmlElementPtr elemDecl;
5433
5434 /*
5435 * Check the new element agaisnt the content model of the new elem.
5436 */
5437 if (state->elemDecl != NULL) {
5438 elemDecl = state->elemDecl;
5439
5440 switch(elemDecl->etype) {
5441 case XML_ELEMENT_TYPE_UNDEFINED:
5442 ret = 0;
5443 break;
5444 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005445 xmlErrValidNode(ctxt, state->node,
5446 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005447 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005448 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005449 ret = 0;
5450 break;
5451 case XML_ELEMENT_TYPE_ANY:
5452 break;
5453 case XML_ELEMENT_TYPE_MIXED:
5454 break;
5455 case XML_ELEMENT_TYPE_ELEMENT:
5456 if (len > 0) {
5457 int i;
5458
5459 for (i = 0;i < len;i++) {
5460 if (!IS_BLANK(data[i])) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005461 xmlErrValidNode(ctxt, state->node,
5462 XML_DTD_CONTENT_MODEL,
5463 "Element %s content does not follow the DTD, Text not allowed\n",
5464 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005465 ret = 0;
5466 goto done;
5467 }
5468 }
5469 /*
5470 * TODO:
5471 * VC: Standalone Document Declaration
5472 * element types with element content, if white space
5473 * occurs directly within any instance of those types.
5474 */
5475 }
5476 break;
5477 }
5478 }
5479 }
5480done:
5481 return(ret);
5482}
5483
5484/**
5485 * xmlValidatePopElement:
5486 * @ctxt: the validation context
5487 * @doc: a document instance
5488 * @elem: an element instance
5489 * @qname: the qualified name as appearing in the serialization
5490 *
5491 * Pop the element end from the validation stack.
5492 *
5493 * returns 1 if no validation problem was found or 0 otherwise
5494 */
5495int
5496xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005497 xmlNodePtr elem ATTRIBUTE_UNUSED,
5498 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005499 int ret = 1;
5500
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005501/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005502 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5503 xmlValidStatePtr state = ctxt->vstate;
5504 xmlElementPtr elemDecl;
5505
5506 /*
5507 * Check the new element agaisnt the content model of the new elem.
5508 */
5509 if (state->elemDecl != NULL) {
5510 elemDecl = state->elemDecl;
5511
5512 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5513 if (state->exec != NULL) {
5514 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5515 if (ret == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005516 xmlErrValidNode(ctxt, state->node,
5517 XML_DTD_CONTENT_MODEL,
5518 "Element %s content does not follow the DTD, Expecting more child\n",
5519 state->node->name, NULL,NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005520 } else {
5521 /*
5522 * previous validation errors should not generate
5523 * a new one here
5524 */
5525 ret = 1;
5526 }
5527 }
5528 }
5529 }
5530 vstateVPop(ctxt);
5531 }
5532 return(ret);
5533}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005534#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005535
5536/**
Owen Taylor3473f882001-02-23 17:55:21 +00005537 * xmlValidateOneElement:
5538 * @ctxt: the validation context
5539 * @doc: a document instance
5540 * @elem: an element instance
5541 *
5542 * Try to validate a single element and it's attributes,
5543 * basically it does the following checks as described by the
5544 * XML-1.0 recommendation:
5545 * - [ VC: Element Valid ]
5546 * - [ VC: Required Attribute ]
5547 * Then call xmlValidateOneAttribute() for each attribute present.
5548 *
5549 * The ID/IDREF checkings are done separately
5550 *
5551 * returns 1 if valid or 0 otherwise
5552 */
5553
5554int
5555xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5556 xmlNodePtr elem) {
5557 xmlElementPtr elemDecl = NULL;
5558 xmlElementContentPtr cont;
5559 xmlAttributePtr attr;
5560 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005561 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005562 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005563 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005564
5565 CHECK_DTD;
5566
5567 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005568 switch (elem->type) {
5569 case XML_ATTRIBUTE_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005570 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5571 "Attribute element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005572 return(0);
5573 case XML_TEXT_NODE:
5574 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005575 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5576 "Text element has children !\n",
5577 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005578 return(0);
5579 }
5580 if (elem->properties != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005581 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5582 "Text element has attribute !\n",
5583 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005584 return(0);
5585 }
5586 if (elem->ns != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005587 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5588 "Text element has namespace !\n",
5589 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005590 return(0);
5591 }
5592 if (elem->nsDef != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005593 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5594 "Text element has namespace !\n",
5595 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005596 return(0);
5597 }
5598 if (elem->content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005599 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5600 "Text element has no content !\n",
5601 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005602 return(0);
5603 }
5604 return(1);
5605 case XML_XINCLUDE_START:
5606 case XML_XINCLUDE_END:
5607 return(1);
5608 case XML_CDATA_SECTION_NODE:
5609 case XML_ENTITY_REF_NODE:
5610 case XML_PI_NODE:
5611 case XML_COMMENT_NODE:
5612 return(1);
5613 case XML_ENTITY_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005614 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5615 "Entity element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005616 return(0);
5617 case XML_NOTATION_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005618 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5619 "Notation element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005620 return(0);
5621 case XML_DOCUMENT_NODE:
5622 case XML_DOCUMENT_TYPE_NODE:
5623 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005624 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5625 "Document element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005626 return(0);
5627 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005628 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5629 "HTML Document not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005630 return(0);
5631 case XML_ELEMENT_NODE:
5632 break;
5633 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005634 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5635 "unknown element type\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005636 return(0);
5637 }
Owen Taylor3473f882001-02-23 17:55:21 +00005638
5639 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005640 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005641 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005642 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5643 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005644 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005645
Daniel Veillardea7751d2002-12-20 00:16:24 +00005646 /*
5647 * If vstateNr is not zero that means continuous validation is
5648 * activated, do not try to check the content model at that level.
5649 */
5650 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005651 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005652 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005653 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005654 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
5655 "No declaration for element %s\n",
5656 elem->name, NULL, NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005657 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005658 case XML_ELEMENT_TYPE_EMPTY:
5659 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005660 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
Owen Taylor3473f882001-02-23 17:55:21 +00005661 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005662 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005663 ret = 0;
5664 }
5665 break;
5666 case XML_ELEMENT_TYPE_ANY:
5667 /* I don't think anything is required then */
5668 break;
5669 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005670
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005671 /* simple case of declared as #PCDATA */
5672 if ((elemDecl->content != NULL) &&
5673 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5674 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5675 if (!ret) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005676 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005677 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005678 elem->name, NULL, NULL);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005679 }
5680 break;
5681 }
Owen Taylor3473f882001-02-23 17:55:21 +00005682 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005683 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005684 while (child != NULL) {
5685 if (child->type == XML_ELEMENT_NODE) {
5686 name = child->name;
5687 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005688 xmlChar fn[50];
5689 xmlChar *fullname;
5690
5691 fullname = xmlBuildQName(child->name, child->ns->prefix,
5692 fn, 50);
5693 if (fullname == NULL)
5694 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005695 cont = elemDecl->content;
5696 while (cont != NULL) {
5697 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005698 if (xmlStrEqual(cont->name, fullname))
5699 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005700 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5701 (cont->c1 != NULL) &&
5702 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005703 if (xmlStrEqual(cont->c1->name, fullname))
5704 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005705 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5706 (cont->c1 == NULL) ||
5707 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005708 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5709 "Internal: MIXED struct corrupted\n",
5710 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005711 break;
5712 }
5713 cont = cont->c2;
5714 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005715 if ((fullname != fn) && (fullname != child->name))
5716 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005717 if (cont != NULL)
5718 goto child_ok;
5719 }
5720 cont = elemDecl->content;
5721 while (cont != NULL) {
5722 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5723 if (xmlStrEqual(cont->name, name)) break;
5724 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5725 (cont->c1 != NULL) &&
5726 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5727 if (xmlStrEqual(cont->c1->name, name)) break;
5728 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5729 (cont->c1 == NULL) ||
5730 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005731 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5732 "Internal: MIXED struct corrupted\n",
5733 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005734 break;
5735 }
5736 cont = cont->c2;
5737 }
5738 if (cont == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005739 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005740 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005741 name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005742 ret = 0;
5743 }
5744 }
5745child_ok:
5746 child = child->next;
5747 }
5748 break;
5749 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005750 if ((doc->standalone == 1) && (extsubset == 1)) {
5751 /*
5752 * VC: Standalone Document Declaration
5753 * - element types with element content, if white space
5754 * occurs directly within any instance of those types.
5755 */
5756 child = elem->children;
5757 while (child != NULL) {
5758 if (child->type == XML_TEXT_NODE) {
5759 const xmlChar *content = child->content;
5760
5761 while (IS_BLANK(*content))
5762 content++;
5763 if (*content == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005764 xmlErrValidNode(ctxt, elem,
5765 XML_DTD_STANDALONE_WHITE_SPACE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005766"standalone: %s declared in the external subset contains white spaces nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005767 elem->name, NULL, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005768 ret = 0;
5769 break;
5770 }
5771 }
5772 child =child->next;
5773 }
5774 }
Owen Taylor3473f882001-02-23 17:55:21 +00005775 child = elem->children;
5776 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005777 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005778 if (tmp <= 0)
5779 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005780 break;
5781 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005782 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00005783
5784 /* [ VC: Required Attribute ] */
5785 attr = elemDecl->attributes;
5786 while (attr != NULL) {
5787 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005788 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00005789
Daniel Veillarde4301c82002-02-13 13:32:35 +00005790 if ((attr->prefix == NULL) &&
5791 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5792 xmlNsPtr ns;
5793
5794 ns = elem->nsDef;
5795 while (ns != NULL) {
5796 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005797 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005798 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00005799 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005800 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5801 xmlNsPtr ns;
5802
5803 ns = elem->nsDef;
5804 while (ns != NULL) {
5805 if (xmlStrEqual(attr->name, ns->prefix))
5806 goto found;
5807 ns = ns->next;
5808 }
5809 } else {
5810 xmlAttrPtr attrib;
5811
5812 attrib = elem->properties;
5813 while (attrib != NULL) {
5814 if (xmlStrEqual(attrib->name, attr->name)) {
5815 if (attr->prefix != NULL) {
5816 xmlNsPtr nameSpace = attrib->ns;
5817
5818 if (nameSpace == NULL)
5819 nameSpace = elem->ns;
5820 /*
5821 * qualified names handling is problematic, having a
5822 * different prefix should be possible but DTDs don't
5823 * allow to define the URI instead of the prefix :-(
5824 */
5825 if (nameSpace == NULL) {
5826 if (qualified < 0)
5827 qualified = 0;
5828 } else if (!xmlStrEqual(nameSpace->prefix,
5829 attr->prefix)) {
5830 if (qualified < 1)
5831 qualified = 1;
5832 } else
5833 goto found;
5834 } else {
5835 /*
5836 * We should allow applications to define namespaces
5837 * for their application even if the DTD doesn't
5838 * carry one, otherwise, basically we would always
5839 * break.
5840 */
5841 goto found;
5842 }
5843 }
5844 attrib = attrib->next;
5845 }
Owen Taylor3473f882001-02-23 17:55:21 +00005846 }
5847 if (qualified == -1) {
5848 if (attr->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005849 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005850 "Element %s does not carry attribute %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005851 elem->name, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005852 ret = 0;
5853 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005854 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005855 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005856 elem->name, attr->prefix,attr->name);
5857 ret = 0;
5858 }
5859 } else if (qualified == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005860 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00005861 "Element %s required attribute %s:%s has no prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005862 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005863 } else if (qualified == 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005864 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00005865 "Element %s required attribute %s:%s has different prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005866 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005867 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005868 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
5869 /*
5870 * Special tests checking #FIXED namespace declarations
5871 * have the right value since this is not done as an
5872 * attribute checking
5873 */
5874 if ((attr->prefix == NULL) &&
5875 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5876 xmlNsPtr ns;
5877
5878 ns = elem->nsDef;
5879 while (ns != NULL) {
5880 if (ns->prefix == NULL) {
5881 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005882 xmlErrValidNode(ctxt, elem,
5883 XML_DTD_ELEM_DEFAULT_NAMESPACE,
Daniel Veillarde4301c82002-02-13 13:32:35 +00005884 "Element %s namespace name for default namespace does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005885 elem->name, NULL, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00005886 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005887 }
5888 goto found;
5889 }
5890 ns = ns->next;
5891 }
5892 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5893 xmlNsPtr ns;
5894
5895 ns = elem->nsDef;
5896 while (ns != NULL) {
5897 if (xmlStrEqual(attr->name, ns->prefix)) {
5898 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005899 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005900 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005901 elem->name, ns->prefix, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00005902 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005903 }
5904 goto found;
5905 }
5906 ns = ns->next;
5907 }
5908 }
Owen Taylor3473f882001-02-23 17:55:21 +00005909 }
5910found:
5911 attr = attr->nexth;
5912 }
5913 return(ret);
5914}
5915
5916/**
5917 * xmlValidateRoot:
5918 * @ctxt: the validation context
5919 * @doc: a document instance
5920 *
5921 * Try to validate a the root element
5922 * basically it does the following check as described by the
5923 * XML-1.0 recommendation:
5924 * - [ VC: Root Element Type ]
5925 * it doesn't try to recurse or apply other check to the element
5926 *
5927 * returns 1 if valid or 0 otherwise
5928 */
5929
5930int
5931xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5932 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00005933 int ret;
5934
Owen Taylor3473f882001-02-23 17:55:21 +00005935 if (doc == NULL) return(0);
5936
5937 root = xmlDocGetRootElement(doc);
5938 if ((root == NULL) || (root->name == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005939 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
5940 "no root element\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005941 return(0);
5942 }
5943
5944 /*
5945 * When doing post validation against a separate DTD, those may
5946 * no internal subset has been generated
5947 */
5948 if ((doc->intSubset != NULL) &&
5949 (doc->intSubset->name != NULL)) {
5950 /*
5951 * Check first the document root against the NQName
5952 */
5953 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
5954 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005955 xmlChar fn[50];
5956 xmlChar *fullname;
5957
5958 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
5959 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005960 xmlVErrMemory(ctxt, NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00005961 return(0);
5962 }
5963 ret = xmlStrEqual(doc->intSubset->name, fullname);
5964 if ((fullname != fn) && (fullname != root->name))
5965 xmlFree(fullname);
5966 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00005967 goto name_ok;
5968 }
5969 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
5970 (xmlStrEqual(root->name, BAD_CAST "html")))
5971 goto name_ok;
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005972 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
5973 "root and DTD name do not match '%s' and '%s'\n",
5974 root->name, doc->intSubset->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005975 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005976 }
5977 }
5978name_ok:
5979 return(1);
5980}
5981
5982
5983/**
5984 * xmlValidateElement:
5985 * @ctxt: the validation context
5986 * @doc: a document instance
5987 * @elem: an element instance
5988 *
5989 * Try to validate the subtree under an element
5990 *
5991 * returns 1 if valid or 0 otherwise
5992 */
5993
5994int
5995xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
5996 xmlNodePtr child;
5997 xmlAttrPtr attr;
5998 xmlChar *value;
5999 int ret = 1;
6000
6001 if (elem == NULL) return(0);
6002
6003 /*
6004 * XInclude elements were added after parsing in the infoset,
6005 * they don't really mean anything validation wise.
6006 */
6007 if ((elem->type == XML_XINCLUDE_START) ||
6008 (elem->type == XML_XINCLUDE_END))
6009 return(1);
6010
6011 CHECK_DTD;
6012
Daniel Veillard10ea86c2001-06-20 13:55:33 +00006013 /*
6014 * Entities references have to be handled separately
6015 */
6016 if (elem->type == XML_ENTITY_REF_NODE) {
6017 return(1);
6018 }
6019
Owen Taylor3473f882001-02-23 17:55:21 +00006020 ret &= xmlValidateOneElement(ctxt, doc, elem);
6021 attr = elem->properties;
6022 while(attr != NULL) {
6023 value = xmlNodeListGetString(doc, attr->children, 0);
6024 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6025 if (value != NULL)
6026 xmlFree(value);
6027 attr= attr->next;
6028 }
6029 child = elem->children;
6030 while (child != NULL) {
6031 ret &= xmlValidateElement(ctxt, doc, child);
6032 child = child->next;
6033 }
6034
6035 return(ret);
6036}
6037
Daniel Veillard8730c562001-02-26 10:49:57 +00006038/**
6039 * xmlValidateRef:
6040 * @ref: A reference to be validated
6041 * @ctxt: Validation context
6042 * @name: Name of ID we are searching for
6043 *
6044 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006045static void
Daniel Veillard8730c562001-02-26 10:49:57 +00006046xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006047 const xmlChar *name) {
6048 xmlAttrPtr id;
6049 xmlAttrPtr attr;
6050
6051 if (ref == NULL)
6052 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006053 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006054 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006055 attr = ref->attr;
6056 if (attr == NULL) {
6057 xmlChar *dup, *str = NULL, *cur, save;
6058
6059 dup = xmlStrdup(name);
6060 if (dup == NULL) {
6061 ctxt->valid = 0;
6062 return;
6063 }
6064 cur = dup;
6065 while (*cur != 0) {
6066 str = cur;
6067 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
6068 save = *cur;
6069 *cur = 0;
6070 id = xmlGetID(ctxt->doc, str);
6071 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006072 xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006073 "attribute %s line %d references an unknown ID \"%s\"\n",
6074 ref->name, ref->lineno, str);
6075 ctxt->valid = 0;
6076 }
6077 if (save == 0)
6078 break;
6079 *cur = save;
6080 while (IS_BLANK(*cur)) cur++;
6081 }
6082 xmlFree(dup);
6083 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00006084 id = xmlGetID(ctxt->doc, name);
6085 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006086 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006087 "IDREF attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006088 attr->name, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006089 ctxt->valid = 0;
6090 }
6091 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6092 xmlChar *dup, *str = NULL, *cur, save;
6093
6094 dup = xmlStrdup(name);
6095 if (dup == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006096 xmlVErrMemory(ctxt, "IDREFS split");
Owen Taylor3473f882001-02-23 17:55:21 +00006097 ctxt->valid = 0;
6098 return;
6099 }
6100 cur = dup;
6101 while (*cur != 0) {
6102 str = cur;
6103 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
6104 save = *cur;
6105 *cur = 0;
6106 id = xmlGetID(ctxt->doc, str);
6107 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006108 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006109 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006110 attr->name, str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006111 ctxt->valid = 0;
6112 }
6113 if (save == 0)
6114 break;
6115 *cur = save;
6116 while (IS_BLANK(*cur)) cur++;
6117 }
6118 xmlFree(dup);
6119 }
6120}
6121
6122/**
Daniel Veillard8730c562001-02-26 10:49:57 +00006123 * xmlWalkValidateList:
6124 * @data: Contents of current link
6125 * @user: Value supplied by the user
6126 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006127 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00006128 */
6129static int
6130xmlWalkValidateList(const void *data, const void *user)
6131{
6132 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6133 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6134 return 1;
6135}
6136
6137/**
6138 * xmlValidateCheckRefCallback:
6139 * @ref_list: List of references
6140 * @ctxt: Validation context
6141 * @name: Name of ID we are searching for
6142 *
6143 */
6144static void
6145xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6146 const xmlChar *name) {
6147 xmlValidateMemo memo;
6148
6149 if (ref_list == NULL)
6150 return;
6151 memo.ctxt = ctxt;
6152 memo.name = name;
6153
6154 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6155
6156}
6157
6158/**
Owen Taylor3473f882001-02-23 17:55:21 +00006159 * xmlValidateDocumentFinal:
6160 * @ctxt: the validation context
6161 * @doc: a document instance
6162 *
6163 * Does the final step for the document validation once all the
6164 * incremental validation steps have been completed
6165 *
6166 * basically it does the following checks described by the XML Rec
6167 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006168 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00006169 *
6170 * returns 1 if valid or 0 otherwise
6171 */
6172
6173int
6174xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6175 xmlRefTablePtr table;
6176
6177 if (doc == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006178 xmlErrValid(ctxt, XML_DTD_NO_DOC,
6179 "xmlValidateDocumentFinal: doc == NULL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006180 return(0);
6181 }
6182
6183 /*
6184 * Check all the NOTATION/NOTATIONS attributes
6185 */
6186 /*
6187 * Check all the ENTITY/ENTITIES attributes definition for validity
6188 */
6189 /*
6190 * Check all the IDREF/IDREFS attributes definition for validity
6191 */
6192 table = (xmlRefTablePtr) doc->refs;
6193 ctxt->doc = doc;
6194 ctxt->valid = 1;
6195 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6196 return(ctxt->valid);
6197}
6198
6199/**
6200 * xmlValidateDtd:
6201 * @ctxt: the validation context
6202 * @doc: a document instance
6203 * @dtd: a dtd instance
6204 *
6205 * Try to validate the document against the dtd instance
6206 *
6207 * basically it does check all the definitions in the DtD.
6208 *
6209 * returns 1 if valid or 0 otherwise
6210 */
6211
6212int
6213xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6214 int ret;
6215 xmlDtdPtr oldExt;
6216 xmlNodePtr root;
6217
6218 if (dtd == NULL) return(0);
6219 if (doc == NULL) return(0);
6220 oldExt = doc->extSubset;
6221 doc->extSubset = dtd;
6222 ret = xmlValidateRoot(ctxt, doc);
6223 if (ret == 0) {
6224 doc->extSubset = oldExt;
6225 return(ret);
6226 }
6227 if (doc->ids != NULL) {
6228 xmlFreeIDTable(doc->ids);
6229 doc->ids = NULL;
6230 }
6231 if (doc->refs != NULL) {
6232 xmlFreeRefTable(doc->refs);
6233 doc->refs = NULL;
6234 }
6235 root = xmlDocGetRootElement(doc);
6236 ret = xmlValidateElement(ctxt, doc, root);
6237 ret &= xmlValidateDocumentFinal(ctxt, doc);
6238 doc->extSubset = oldExt;
6239 return(ret);
6240}
6241
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006242static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006243xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6244 const xmlChar *name ATTRIBUTE_UNUSED) {
6245 if (cur == NULL)
6246 return;
6247 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6248 xmlChar *notation = cur->content;
6249
Daniel Veillard878eab02002-02-19 13:46:09 +00006250 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006251 int ret;
6252
6253 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6254 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006255 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006256 }
6257 }
6258 }
6259}
6260
6261static void
Owen Taylor3473f882001-02-23 17:55:21 +00006262xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006263 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006264 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006265 xmlDocPtr doc;
6266 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006267
Owen Taylor3473f882001-02-23 17:55:21 +00006268 if (cur == NULL)
6269 return;
6270 switch (cur->atype) {
6271 case XML_ATTRIBUTE_CDATA:
6272 case XML_ATTRIBUTE_ID:
6273 case XML_ATTRIBUTE_IDREF :
6274 case XML_ATTRIBUTE_IDREFS:
6275 case XML_ATTRIBUTE_NMTOKEN:
6276 case XML_ATTRIBUTE_NMTOKENS:
6277 case XML_ATTRIBUTE_ENUMERATION:
6278 break;
6279 case XML_ATTRIBUTE_ENTITY:
6280 case XML_ATTRIBUTE_ENTITIES:
6281 case XML_ATTRIBUTE_NOTATION:
6282 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006283
6284 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6285 cur->atype, cur->defaultValue);
6286 if ((ret == 0) && (ctxt->valid == 1))
6287 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006288 }
6289 if (cur->tree != NULL) {
6290 xmlEnumerationPtr tree = cur->tree;
6291 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006292 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006293 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006294 if ((ret == 0) && (ctxt->valid == 1))
6295 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006296 tree = tree->next;
6297 }
6298 }
6299 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006300 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6301 doc = cur->doc;
6302 if ((doc == NULL) || (cur->elem == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006303 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard878eab02002-02-19 13:46:09 +00006304 "xmlValidateAttributeCallback(%s): internal error\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006305 (const char *) cur->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00006306 return;
6307 }
6308 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6309 if (elem == NULL)
6310 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
6311 if (elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006312 xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
Daniel Veillard878eab02002-02-19 13:46:09 +00006313 "attribute %s: could not find decl for element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006314 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006315 return;
6316 }
6317 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006318 xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006319 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006320 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006321 ctxt->valid = 0;
6322 }
6323 }
Owen Taylor3473f882001-02-23 17:55:21 +00006324}
6325
6326/**
6327 * xmlValidateDtdFinal:
6328 * @ctxt: the validation context
6329 * @doc: a document instance
6330 *
6331 * Does the final step for the dtds validation once all the
6332 * subsets have been parsed
6333 *
6334 * basically it does the following checks described by the XML Rec
6335 * - check that ENTITY and ENTITIES type attributes default or
6336 * possible values matches one of the defined entities.
6337 * - check that NOTATION type attributes default or
6338 * possible values matches one of the defined notations.
6339 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006340 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006341 */
6342
6343int
6344xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006345 xmlDtdPtr dtd;
6346 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006347 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006348
6349 if (doc == NULL) return(0);
6350 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6351 return(0);
6352 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006353 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006354 dtd = doc->intSubset;
6355 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6356 table = (xmlAttributeTablePtr) dtd->attributes;
6357 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006358 }
6359 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006360 entities = (xmlEntitiesTablePtr) dtd->entities;
6361 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6362 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006363 }
6364 dtd = doc->extSubset;
6365 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6366 table = (xmlAttributeTablePtr) dtd->attributes;
6367 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006368 }
6369 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006370 entities = (xmlEntitiesTablePtr) dtd->entities;
6371 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6372 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006373 }
6374 return(ctxt->valid);
6375}
6376
6377/**
6378 * xmlValidateDocument:
6379 * @ctxt: the validation context
6380 * @doc: a document instance
6381 *
6382 * Try to validate the document instance
6383 *
6384 * basically it does the all the checks described by the XML Rec
6385 * i.e. validates the internal and external subset (if present)
6386 * and validate the document tree.
6387 *
6388 * returns 1 if valid or 0 otherwise
6389 */
6390
6391int
6392xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6393 int ret;
6394 xmlNodePtr root;
6395
Daniel Veillard2fd85422002-10-16 14:32:41 +00006396 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006397 xmlErrValid(ctxt, XML_DTD_NO_DTD,
6398 "no DTD found!\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006399 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006400 }
Owen Taylor3473f882001-02-23 17:55:21 +00006401 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6402 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6403 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6404 doc->intSubset->SystemID);
6405 if (doc->extSubset == NULL) {
6406 if (doc->intSubset->SystemID != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006407 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006408 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006409 (const char *) doc->intSubset->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00006410 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006411 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006412 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006413 (const char *) doc->intSubset->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00006414 }
6415 return(0);
6416 }
6417 }
6418
6419 if (doc->ids != NULL) {
6420 xmlFreeIDTable(doc->ids);
6421 doc->ids = NULL;
6422 }
6423 if (doc->refs != NULL) {
6424 xmlFreeRefTable(doc->refs);
6425 doc->refs = NULL;
6426 }
6427 ret = xmlValidateDtdFinal(ctxt, doc);
6428 if (!xmlValidateRoot(ctxt, doc)) return(0);
6429
6430 root = xmlDocGetRootElement(doc);
6431 ret &= xmlValidateElement(ctxt, doc, root);
6432 ret &= xmlValidateDocumentFinal(ctxt, doc);
6433 return(ret);
6434}
6435
Owen Taylor3473f882001-02-23 17:55:21 +00006436/************************************************************************
6437 * *
6438 * Routines for dynamic validation editing *
6439 * *
6440 ************************************************************************/
6441
6442/**
6443 * xmlValidGetPotentialChildren:
6444 * @ctree: an element content tree
6445 * @list: an array to store the list of child names
6446 * @len: a pointer to the number of element in the list
6447 * @max: the size of the array
6448 *
6449 * Build/extend a list of potential children allowed by the content tree
6450 *
6451 * returns the number of element in the list, or -1 in case of error.
6452 */
6453
6454int
6455xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6456 int *len, int max) {
6457 int i;
6458
6459 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6460 return(-1);
6461 if (*len >= max) return(*len);
6462
6463 switch (ctree->type) {
6464 case XML_ELEMENT_CONTENT_PCDATA:
6465 for (i = 0; i < *len;i++)
6466 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6467 list[(*len)++] = BAD_CAST "#PCDATA";
6468 break;
6469 case XML_ELEMENT_CONTENT_ELEMENT:
6470 for (i = 0; i < *len;i++)
6471 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6472 list[(*len)++] = ctree->name;
6473 break;
6474 case XML_ELEMENT_CONTENT_SEQ:
6475 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6476 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6477 break;
6478 case XML_ELEMENT_CONTENT_OR:
6479 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6480 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6481 break;
6482 }
6483
6484 return(*len);
6485}
6486
6487/**
6488 * xmlValidGetValidElements:
6489 * @prev: an element to insert after
6490 * @next: an element to insert next
6491 * @list: an array to store the list of child names
6492 * @max: the size of the array
6493 *
6494 * This function returns the list of authorized children to insert
6495 * within an existing tree while respecting the validity constraints
6496 * forced by the Dtd. The insertion point is defined using @prev and
6497 * @next in the following ways:
6498 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6499 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6500 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6501 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6502 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6503 *
6504 * pointers to the element names are inserted at the beginning of the array
6505 * and do not need to be freed.
6506 *
6507 * returns the number of element in the list, or -1 in case of error. If
6508 * the function returns the value @max the caller is invited to grow the
6509 * receiving array and retry.
6510 */
6511
6512int
6513xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
6514 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006515 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006516 int nb_valid_elements = 0;
6517 const xmlChar *elements[256];
6518 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006519 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006520
6521 xmlNode *ref_node;
6522 xmlNode *parent;
6523 xmlNode *test_node;
6524
6525 xmlNode *prev_next;
6526 xmlNode *next_prev;
6527 xmlNode *parent_childs;
6528 xmlNode *parent_last;
6529
6530 xmlElement *element_desc;
6531
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00006532 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006533
Owen Taylor3473f882001-02-23 17:55:21 +00006534 if (prev == NULL && next == NULL)
6535 return(-1);
6536
6537 if (list == NULL) return(-1);
6538 if (max <= 0) return(-1);
6539
6540 nb_valid_elements = 0;
6541 ref_node = prev ? prev : next;
6542 parent = ref_node->parent;
6543
6544 /*
6545 * Retrieves the parent element declaration
6546 */
6547 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6548 parent->name);
6549 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6550 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6551 parent->name);
6552 if (element_desc == NULL) return(-1);
6553
6554 /*
6555 * Do a backup of the current tree structure
6556 */
6557 prev_next = prev ? prev->next : NULL;
6558 next_prev = next ? next->prev : NULL;
6559 parent_childs = parent->children;
6560 parent_last = parent->last;
6561
6562 /*
6563 * Creates a dummy node and insert it into the tree
6564 */
6565 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
6566 test_node->doc = ref_node->doc;
6567 test_node->parent = parent;
6568 test_node->prev = prev;
6569 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006570 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006571
6572 if (prev) prev->next = test_node;
6573 else parent->children = test_node;
6574
6575 if (next) next->prev = test_node;
6576 else parent->last = test_node;
6577
6578 /*
6579 * Insert each potential child node and check if the parent is
6580 * still valid
6581 */
6582 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6583 elements, &nb_elements, 256);
6584
6585 for (i = 0;i < nb_elements;i++) {
6586 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006587 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006588 int j;
6589
6590 for (j = 0; j < nb_valid_elements;j++)
6591 if (xmlStrEqual(elements[i], list[j])) break;
6592 list[nb_valid_elements++] = elements[i];
6593 if (nb_valid_elements >= max) break;
6594 }
6595 }
6596
6597 /*
6598 * Restore the tree structure
6599 */
6600 if (prev) prev->next = prev_next;
6601 if (next) next->prev = next_prev;
6602 parent->children = parent_childs;
6603 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006604
6605 /*
6606 * Free up the dummy node
6607 */
6608 test_node->name = name;
6609 xmlFreeNode(test_node);
6610
Owen Taylor3473f882001-02-23 17:55:21 +00006611 return(nb_valid_elements);
6612}
Daniel Veillard4432df22003-09-28 18:58:27 +00006613#endif /* LIBXML_VALID_ENABLED */
6614