blob: 15514d81123fc46236430ab436c54ce5355a5a92 [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>
Daniel Veillard29b17482004-08-16 00:39:03 +000021#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000022#include <libxml/valid.h>
23#include <libxml/parser.h>
24#include <libxml/parserInternals.h>
25#include <libxml/xmlerror.h>
26#include <libxml/list.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000027#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000028
Daniel Veillard4432df22003-09-28 18:58:27 +000029static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
30 int create);
Daniel Veillarde62d36c2001-05-15 08:53:16 +000031/* #define DEBUG_VALID_ALGO */
Daniel Veillard5acfd6b2002-09-18 16:29:02 +000032/* #define DEBUG_REGEXP_ALGO */
Daniel Veillarde62d36c2001-05-15 08:53:16 +000033
Daniel Veillarda646cfd2002-09-17 21:50:03 +000034#define TODO \
35 xmlGenericError(xmlGenericErrorContext, \
36 "Unimplemented block at %s:%d\n", \
37 __FILE__, __LINE__);
Owen Taylor3473f882001-02-23 17:55:21 +000038
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000039/************************************************************************
40 * *
41 * Error handling routines *
42 * *
43 ************************************************************************/
44
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000045/**
Daniel Veillardce9457f2003-10-05 21:33:18 +000046 * xmlVErrMemory:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000047 * @ctxt: an XML validation parser context
48 * @extra: extra informations
49 *
50 * Handle an out of memory error
51 */
52static void
Daniel Veillardce9457f2003-10-05 21:33:18 +000053xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000054{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000055 xmlGenericErrorFunc channel = NULL;
56 xmlParserCtxtPtr pctxt = NULL;
57 void *data = NULL;
58
59 if (ctxt != NULL) {
60 channel = ctxt->error;
61 data = ctxt->userData;
62 pctxt = ctxt->userData;
63 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000064 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +000065 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000066 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000067 XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
68 "Memory allocation failed : %s\n", extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000069 else
Daniel Veillard73000572003-10-11 11:26:42 +000070 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000071 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000072 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
73 "Memory allocation failed\n");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000074}
75
76/**
77 * xmlErrValid:
78 * @ctxt: an XML validation parser context
Daniel Veillardbb5abab2003-10-03 22:21:51 +000079 * @error: the error number
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000080 * @extra: extra informations
81 *
82 * Handle a validation error
83 */
84static void
Daniel Veillardf88d8cf2003-12-08 10:25:02 +000085xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000086 const char *msg, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000087{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000088 xmlGenericErrorFunc channel = NULL;
89 xmlParserCtxtPtr pctxt = NULL;
90 void *data = NULL;
91
92 if (ctxt != NULL) {
93 channel = ctxt->error;
94 data = ctxt->userData;
95 pctxt = ctxt->userData;
96 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000097 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +000098 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000099 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000100 XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
101 msg, extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000102 else
Daniel Veillard73000572003-10-11 11:26:42 +0000103 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +0000104 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000105 XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
106 msg);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000107}
108
Daniel Veillardf54cd532004-02-25 11:52:31 +0000109#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
110/**
111 * xmlErrValidNode:
112 * @ctxt: an XML validation parser context
113 * @node: the node raising the error
114 * @error: the error number
115 * @str1: extra informations
116 * @str2: extra informations
117 * @str3: extra informations
118 *
119 * Handle a validation error, provide contextual informations
120 */
121static void
122xmlErrValidNode(xmlValidCtxtPtr ctxt,
123 xmlNodePtr node, xmlParserErrors error,
124 const char *msg, const xmlChar * str1,
125 const xmlChar * str2, const xmlChar * str3)
126{
127 xmlStructuredErrorFunc schannel = NULL;
128 xmlGenericErrorFunc channel = NULL;
129 xmlParserCtxtPtr pctxt = NULL;
130 void *data = NULL;
131
132 if (ctxt != NULL) {
133 channel = ctxt->error;
134 data = ctxt->userData;
135 pctxt = ctxt->userData;
136 }
137 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
138 XML_ERR_ERROR, NULL, 0,
139 (const char *) str1,
140 (const char *) str1,
141 (const char *) str3, 0, 0, msg, str1, str2, str3);
142}
143#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
144
Daniel Veillardd61e8fb2003-10-19 21:59:17 +0000145#ifdef LIBXML_VALID_ENABLED
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000146/**
147 * xmlErrValidNodeNr:
148 * @ctxt: an XML validation parser context
149 * @node: the node raising the error
150 * @error: the error number
151 * @str1: extra informations
152 * @int2: extra informations
153 * @str3: extra informations
154 *
155 * Handle a validation error, provide contextual informations
156 */
157static void
Daniel Veillard72b9e292003-10-28 15:44:17 +0000158xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000159 xmlNodePtr node, xmlParserErrors error,
160 const char *msg, const xmlChar * str1,
161 int int2, const xmlChar * str3)
162{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000163 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000164 xmlGenericErrorFunc channel = NULL;
165 xmlParserCtxtPtr pctxt = NULL;
166 void *data = NULL;
167
168 if (ctxt != NULL) {
169 channel = ctxt->error;
170 data = ctxt->userData;
171 pctxt = ctxt->userData;
172 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000173 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000174 XML_ERR_ERROR, NULL, 0,
175 (const char *) str1,
176 (const char *) str3,
177 NULL, int2, 0, msg, str1, int2, str3);
178}
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000179
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000180/**
181 * xmlErrValidWarning:
182 * @ctxt: an XML validation parser context
183 * @node: the node raising the error
184 * @error: the error number
William M. Brackedb65a72004-02-06 07:36:04 +0000185 * @str1: extra information
186 * @str2: extra information
187 * @str3: extra information
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000188 *
William M. Brackedb65a72004-02-06 07:36:04 +0000189 * Handle a validation error, provide contextual information
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000190 */
191static void
William M. Brackedb65a72004-02-06 07:36:04 +0000192xmlErrValidWarning(xmlValidCtxtPtr ctxt,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000193 xmlNodePtr node, xmlParserErrors error,
194 const char *msg, const xmlChar * str1,
195 const xmlChar * str2, const xmlChar * str3)
196{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000197 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000198 xmlGenericErrorFunc channel = NULL;
199 xmlParserCtxtPtr pctxt = NULL;
200 void *data = NULL;
201
202 if (ctxt != NULL) {
203 channel = ctxt->error;
204 data = ctxt->userData;
205 pctxt = ctxt->userData;
206 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000207 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000208 XML_ERR_WARNING, NULL, 0,
209 (const char *) str1,
210 (const char *) str1,
211 (const char *) str3, 0, 0, msg, str1, str2, str3);
212}
213
214
Daniel Veillardea7751d2002-12-20 00:16:24 +0000215
216#ifdef LIBXML_REGEXP_ENABLED
217/*
218 * If regexp are enabled we can do continuous validation without the
219 * need of a tree to validate the content model. this is done in each
220 * callbacks.
221 * Each xmlValidState represent the validation state associated to the
222 * set of nodes currently open from the document root to the current element.
223 */
224
225
226typedef struct _xmlValidState {
227 xmlElementPtr elemDecl; /* pointer to the content model */
228 xmlNodePtr node; /* pointer to the current node */
229 xmlRegExecCtxtPtr exec; /* regexp runtime */
230} _xmlValidState;
231
232
233static int
234vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000235 if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000236 ctxt->vstateMax = 10;
237 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
238 sizeof(ctxt->vstateTab[0]));
239 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000240 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000241 return(-1);
242 }
243 }
244
245 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000246 xmlValidState *tmp;
247
248 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
249 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
250 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000251 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000252 return(-1);
253 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000254 ctxt->vstateMax *= 2;
255 ctxt->vstateTab = tmp;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000256 }
257 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
258 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
259 ctxt->vstateTab[ctxt->vstateNr].node = node;
260 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
261 if (elemDecl->contModel == NULL)
262 xmlValidBuildContentModel(ctxt, elemDecl);
263 if (elemDecl->contModel != NULL) {
264 ctxt->vstateTab[ctxt->vstateNr].exec =
265 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
266 } else {
267 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000268 xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
269 XML_ERR_INTERNAL_ERROR,
270 "Failed to build content model regexp for %s\n",
271 node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000272 }
273 }
274 return(ctxt->vstateNr++);
275}
276
277static int
278vstateVPop(xmlValidCtxtPtr ctxt) {
279 xmlElementPtr elemDecl;
280
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000281 if (ctxt->vstateNr < 1) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000282 ctxt->vstateNr--;
283 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
284 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
285 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
286 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
287 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
288 }
289 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
290 if (ctxt->vstateNr >= 1)
291 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
292 else
293 ctxt->vstate = NULL;
294 return(ctxt->vstateNr);
295}
296
297#else /* not LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000298/*
Daniel Veillard1c732d22002-11-30 11:22:59 +0000299 * If regexp are not enabled, it uses a home made algorithm less
300 * complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000301 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000302 * only restriction is on the deepness of the tree limited by the
303 * size of the occurs bitfield
304 *
305 * this is the content of a saved state for rollbacks
306 */
307
308#define ROLLBACK_OR 0
309#define ROLLBACK_PARENT 1
310
Daniel Veillardb44025c2001-10-11 22:55:55 +0000311typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000312 xmlElementContentPtr cont; /* pointer to the content model subtree */
313 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000314 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000315 unsigned char depth; /* current depth in the overall tree */
316 unsigned char state; /* ROLLBACK_XXX */
317} _xmlValidState;
318
Daniel Veillardfc57b412002-04-29 15:50:14 +0000319#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000320#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
321#define CONT ctxt->vstate->cont
322#define NODE ctxt->vstate->node
323#define DEPTH ctxt->vstate->depth
324#define OCCURS ctxt->vstate->occurs
325#define STATE ctxt->vstate->state
326
Daniel Veillard5344c602001-12-31 16:37:34 +0000327#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
328#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000329
Daniel Veillard5344c602001-12-31 16:37:34 +0000330#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
331#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000332
333static int
334vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
335 xmlNodePtr node, unsigned char depth, long occurs,
336 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000337 int i = ctxt->vstateNr - 1;
338
Daniel Veillard940492d2002-04-15 10:15:25 +0000339 if (ctxt->vstateNr > MAX_RECURSE) {
340 return(-1);
341 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000342 if (ctxt->vstateTab == NULL) {
343 ctxt->vstateMax = 8;
344 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
345 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
346 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000347 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000348 return(-1);
349 }
350 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000351 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000352 xmlValidState *tmp;
353
354 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
355 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
356 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000357 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard940492d2002-04-15 10:15:25 +0000358 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000359 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000360 ctxt->vstateMax *= 2;
361 ctxt->vstateTab = tmp;
Daniel Veillard06803992001-04-22 10:35:56 +0000362 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000363 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000364 /*
365 * Don't push on the stack a state already here
366 */
367 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
368 (ctxt->vstateTab[i].node == node) &&
369 (ctxt->vstateTab[i].depth == depth) &&
370 (ctxt->vstateTab[i].occurs == occurs) &&
371 (ctxt->vstateTab[i].state == state))
372 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000373 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
374 ctxt->vstateTab[ctxt->vstateNr].node = node;
375 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
376 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
377 ctxt->vstateTab[ctxt->vstateNr].state = state;
378 return(ctxt->vstateNr++);
379}
380
381static int
382vstateVPop(xmlValidCtxtPtr ctxt) {
383 if (ctxt->vstateNr <= 1) return(-1);
384 ctxt->vstateNr--;
385 ctxt->vstate = &ctxt->vstateTab[0];
386 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
387 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
388 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
389 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
390 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
391 return(ctxt->vstateNr);
392}
393
Daniel Veillard118aed72002-09-24 14:13:13 +0000394#endif /* LIBXML_REGEXP_ENABLED */
395
Daniel Veillard1c732d22002-11-30 11:22:59 +0000396static int
397nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
398{
399 if (ctxt->nodeMax <= 0) {
400 ctxt->nodeMax = 4;
401 ctxt->nodeTab =
402 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
403 sizeof(ctxt->nodeTab[0]));
404 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000405 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000406 ctxt->nodeMax = 0;
407 return (0);
408 }
409 }
410 if (ctxt->nodeNr >= ctxt->nodeMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000411 xmlNodePtr *tmp;
412 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
413 ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
414 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000415 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000416 return (0);
417 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000418 ctxt->nodeMax *= 2;
419 ctxt->nodeTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000420 }
421 ctxt->nodeTab[ctxt->nodeNr] = value;
422 ctxt->node = value;
423 return (ctxt->nodeNr++);
424}
425static xmlNodePtr
426nodeVPop(xmlValidCtxtPtr ctxt)
427{
428 xmlNodePtr ret;
429
430 if (ctxt->nodeNr <= 0)
431 return (0);
432 ctxt->nodeNr--;
433 if (ctxt->nodeNr > 0)
434 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
435 else
436 ctxt->node = NULL;
437 ret = ctxt->nodeTab[ctxt->nodeNr];
438 ctxt->nodeTab[ctxt->nodeNr] = 0;
439 return (ret);
440}
Owen Taylor3473f882001-02-23 17:55:21 +0000441
Owen Taylor3473f882001-02-23 17:55:21 +0000442#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000443static void
444xmlValidPrintNode(xmlNodePtr cur) {
445 if (cur == NULL) {
446 xmlGenericError(xmlGenericErrorContext, "null");
447 return;
448 }
449 switch (cur->type) {
450 case XML_ELEMENT_NODE:
451 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
452 break;
453 case XML_TEXT_NODE:
454 xmlGenericError(xmlGenericErrorContext, "text ");
455 break;
456 case XML_CDATA_SECTION_NODE:
457 xmlGenericError(xmlGenericErrorContext, "cdata ");
458 break;
459 case XML_ENTITY_REF_NODE:
460 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
461 break;
462 case XML_PI_NODE:
463 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
464 break;
465 case XML_COMMENT_NODE:
466 xmlGenericError(xmlGenericErrorContext, "comment ");
467 break;
468 case XML_ATTRIBUTE_NODE:
469 xmlGenericError(xmlGenericErrorContext, "?attr? ");
470 break;
471 case XML_ENTITY_NODE:
472 xmlGenericError(xmlGenericErrorContext, "?ent? ");
473 break;
474 case XML_DOCUMENT_NODE:
475 xmlGenericError(xmlGenericErrorContext, "?doc? ");
476 break;
477 case XML_DOCUMENT_TYPE_NODE:
478 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
479 break;
480 case XML_DOCUMENT_FRAG_NODE:
481 xmlGenericError(xmlGenericErrorContext, "?frag? ");
482 break;
483 case XML_NOTATION_NODE:
484 xmlGenericError(xmlGenericErrorContext, "?nota? ");
485 break;
486 case XML_HTML_DOCUMENT_NODE:
487 xmlGenericError(xmlGenericErrorContext, "?html? ");
488 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000489#ifdef LIBXML_DOCB_ENABLED
490 case XML_DOCB_DOCUMENT_NODE:
491 xmlGenericError(xmlGenericErrorContext, "?docb? ");
492 break;
493#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000494 case XML_DTD_NODE:
495 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
496 break;
497 case XML_ELEMENT_DECL:
498 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
499 break;
500 case XML_ATTRIBUTE_DECL:
501 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
502 break;
503 case XML_ENTITY_DECL:
504 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
505 break;
506 case XML_NAMESPACE_DECL:
507 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
508 break;
509 case XML_XINCLUDE_START:
510 xmlGenericError(xmlGenericErrorContext, "incstart ");
511 break;
512 case XML_XINCLUDE_END:
513 xmlGenericError(xmlGenericErrorContext, "incend ");
514 break;
515 }
516}
517
518static void
519xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000520 if (cur == NULL)
521 xmlGenericError(xmlGenericErrorContext, "null ");
522 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000523 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000524 cur = cur->next;
525 }
526}
527
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000528static void
529xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Daniel Veillard83391282003-03-06 21:37:30 +0000530 char expr[5000];
Owen Taylor3473f882001-02-23 17:55:21 +0000531
532 expr[0] = 0;
533 xmlGenericError(xmlGenericErrorContext, "valid: ");
534 xmlValidPrintNodeList(cur);
535 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000536 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000537 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
538}
539
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000540static void
541xmlValidDebugState(xmlValidStatePtr state) {
542 xmlGenericError(xmlGenericErrorContext, "(");
543 if (state->cont == NULL)
544 xmlGenericError(xmlGenericErrorContext, "null,");
545 else
546 switch (state->cont->type) {
547 case XML_ELEMENT_CONTENT_PCDATA:
548 xmlGenericError(xmlGenericErrorContext, "pcdata,");
549 break;
550 case XML_ELEMENT_CONTENT_ELEMENT:
551 xmlGenericError(xmlGenericErrorContext, "%s,",
552 state->cont->name);
553 break;
554 case XML_ELEMENT_CONTENT_SEQ:
555 xmlGenericError(xmlGenericErrorContext, "seq,");
556 break;
557 case XML_ELEMENT_CONTENT_OR:
558 xmlGenericError(xmlGenericErrorContext, "or,");
559 break;
560 }
561 xmlValidPrintNode(state->node);
562 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
563 state->depth, state->occurs, state->state);
564}
565
566static void
567xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
568 int i, j;
569
570 xmlGenericError(xmlGenericErrorContext, "state: ");
571 xmlValidDebugState(ctxt->vstate);
572 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
573 ctxt->vstateNr - 1);
574 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
575 xmlValidDebugState(&ctxt->vstateTab[j]);
576 xmlGenericError(xmlGenericErrorContext, "\n");
577}
578
579/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000580#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000581 *****/
582
583#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000584#define DEBUG_VALID_MSG(m) \
585 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
586
Owen Taylor3473f882001-02-23 17:55:21 +0000587#else
588#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000589#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000590#endif
591
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000592/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000593
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000594
Owen Taylor3473f882001-02-23 17:55:21 +0000595#define CHECK_DTD \
596 if (doc == NULL) return(0); \
597 else if ((doc->intSubset == NULL) && \
598 (doc->extSubset == NULL)) return(0)
599
Owen Taylor3473f882001-02-23 17:55:21 +0000600xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
601
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000602#ifdef LIBXML_REGEXP_ENABLED
603
604/************************************************************************
605 * *
606 * Content model validation based on the regexps *
607 * *
608 ************************************************************************/
609
610/**
611 * xmlValidBuildAContentModel:
612 * @content: the content model
613 * @ctxt: the schema parser context
614 * @name: the element name whose content is being built
615 *
616 * Generate the automata sequence needed for that type
617 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000618 * Returns 1 if successful or 0 in case of error.
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000619 */
620static int
621xmlValidBuildAContentModel(xmlElementContentPtr content,
622 xmlValidCtxtPtr ctxt,
623 const xmlChar *name) {
624 if (content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000625 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
626 "Found NULL content 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 }
630 switch (content->type) {
631 case XML_ELEMENT_CONTENT_PCDATA:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000632 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
633 "Found PCDATA in content model of %s\n",
634 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000635 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000636 break;
637 case XML_ELEMENT_CONTENT_ELEMENT: {
638 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillardc00cda82003-04-07 10:22:39 +0000639 xmlChar fn[50];
640 xmlChar *fullname;
641
642 fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
643 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000644 xmlVErrMemory(ctxt, "Building content model");
Daniel Veillardc00cda82003-04-07 10:22:39 +0000645 return(0);
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000646 }
647
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000648 switch (content->ocur) {
649 case XML_ELEMENT_CONTENT_ONCE:
650 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000651 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000652 break;
653 case XML_ELEMENT_CONTENT_OPT:
654 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000655 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000656 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
657 break;
658 case XML_ELEMENT_CONTENT_PLUS:
659 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000660 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000661 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000662 ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000663 break;
664 case XML_ELEMENT_CONTENT_MULT:
William M. Brack8b0cbb02004-04-17 13:31:06 +0000665 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
666 ctxt->state, NULL);
667 xmlAutomataNewTransition(ctxt->am,
668 ctxt->state, ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000669 break;
670 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000671 if ((fullname != fn) && (fullname != content->name))
672 xmlFree(fullname);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000673 break;
674 }
675 case XML_ELEMENT_CONTENT_SEQ: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000676 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000677 xmlElementContentOccur ocur;
678
679 /*
680 * Simply iterate over the content
681 */
682 oldstate = ctxt->state;
683 ocur = content->ocur;
Daniel Veillardf4be0182003-02-24 19:54:33 +0000684 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
685 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
686 oldstate = ctxt->state;
687 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000688 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000689 xmlValidBuildAContentModel(content->c1, ctxt, name);
690 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000691 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
692 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
693 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000694 oldend = ctxt->state;
695 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000696 switch (ocur) {
697 case XML_ELEMENT_CONTENT_ONCE:
698 break;
699 case XML_ELEMENT_CONTENT_OPT:
700 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
701 break;
702 case XML_ELEMENT_CONTENT_MULT:
703 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000704 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000705 break;
706 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000707 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000708 break;
709 }
710 break;
711 }
712 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000713 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000714 xmlElementContentOccur ocur;
715
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000716 ocur = content->ocur;
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000717 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
718 (ocur == XML_ELEMENT_CONTENT_MULT)) {
719 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
720 ctxt->state, NULL);
721 }
722 oldstate = ctxt->state;
723 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000724
725 /*
726 * iterate over the subtypes and remerge the end with an
727 * epsilon transition
728 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000729 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000730 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000731 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000732 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000733 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000734 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
735 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000736 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000737 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000738 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
739 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000740 switch (ocur) {
741 case XML_ELEMENT_CONTENT_ONCE:
742 break;
743 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000744 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000745 break;
746 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000747 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
748 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000749 break;
750 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000751 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000752 break;
753 }
754 break;
755 }
756 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000757 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
758 "ContentModel broken for element %s\n",
759 (const char *) name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000760 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000761 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000762 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000763}
764/**
765 * xmlValidBuildContentModel:
766 * @ctxt: a validation context
767 * @elem: an element declaration node
768 *
769 * (Re)Build the automata associated to the content model of this
770 * element
771 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000772 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000773 */
774int
775xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000776
777 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000778 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000779 if (elem->type != XML_ELEMENT_DECL)
780 return(0);
781 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
782 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000783 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000784 if (elem->contModel != NULL) {
785 if (!xmlRegexpIsDeterminist(elem->contModel)) {
786 ctxt->valid = 0;
787 return(0);
788 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000789 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000790 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000791
792 ctxt->am = xmlNewAutomata();
793 if (ctxt->am == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000794 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
795 XML_ERR_INTERNAL_ERROR,
796 "Cannot create automata for element %s\n",
797 elem->name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000798 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000799 }
William M. Brack78637da2003-07-31 14:47:38 +0000800 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000801 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
802 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000803 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000804 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000805 char expr[5000];
806 expr[0] = 0;
807 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000808 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
809 XML_DTD_CONTENT_NOT_DETERMINIST,
810 "Content model of %s is not determinist: %s\n",
811 elem->name, BAD_CAST expr, NULL);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000812#ifdef DEBUG_REGEXP_ALGO
813 xmlRegexpPrint(stderr, elem->contModel);
814#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000815 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000816 ctxt->state = NULL;
817 xmlFreeAutomata(ctxt->am);
818 ctxt->am = NULL;
819 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000820 }
821 ctxt->state = NULL;
822 xmlFreeAutomata(ctxt->am);
823 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000824 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000825}
826
827#endif /* LIBXML_REGEXP_ENABLED */
828
Owen Taylor3473f882001-02-23 17:55:21 +0000829/****************************************************************
830 * *
831 * Util functions for data allocation/deallocation *
832 * *
833 ****************************************************************/
834
835/**
Daniel Veillarda37aab82003-06-09 09:10:36 +0000836 * xmlNewValidCtxt:
837 *
838 * Allocate a validation context structure.
839 *
840 * Returns NULL if not, otherwise the new validation context structure
841 */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000842xmlValidCtxtPtr xmlNewValidCtxt(void) {
Daniel Veillarda37aab82003-06-09 09:10:36 +0000843 xmlValidCtxtPtr ret;
844
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000845 if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000846 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda37aab82003-06-09 09:10:36 +0000847 return (NULL);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000848 }
Daniel Veillarda37aab82003-06-09 09:10:36 +0000849
850 (void) memset(ret, 0, sizeof (xmlValidCtxt));
851
852 return (ret);
853}
854
855/**
856 * xmlFreeValidCtxt:
857 * @cur: the validation context to free
858 *
859 * Free a validation context structure.
860 */
861void
862xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
863 xmlFree(cur);
864}
865
Daniel Veillard4432df22003-09-28 18:58:27 +0000866#endif /* LIBXML_VALID_ENABLED */
867
Daniel Veillarda37aab82003-06-09 09:10:36 +0000868/**
Owen Taylor3473f882001-02-23 17:55:21 +0000869 * xmlNewElementContent:
870 * @name: the subelement name or NULL
871 * @type: the type of element content decl
872 *
873 * Allocate an element content structure.
874 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000875 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000876 */
877xmlElementContentPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000878xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
Owen Taylor3473f882001-02-23 17:55:21 +0000879 xmlElementContentPtr ret;
880
881 switch(type) {
882 case XML_ELEMENT_CONTENT_ELEMENT:
883 if (name == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000884 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
885 "xmlNewElementContent : name == NULL !\n",
886 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000887 }
888 break;
889 case XML_ELEMENT_CONTENT_PCDATA:
890 case XML_ELEMENT_CONTENT_SEQ:
891 case XML_ELEMENT_CONTENT_OR:
892 if (name != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000893 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
894 "xmlNewElementContent : name != NULL !\n",
895 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000896 }
897 break;
898 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000899 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
900 "Internal: ELEMENT content corrupted invalid type\n",
901 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000902 return(NULL);
903 }
904 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
905 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000906 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000907 return(NULL);
908 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000909 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000910 ret->type = type;
911 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000912 if (name != NULL) {
913 xmlChar *prefix = NULL;
914 ret->name = xmlSplitQName2(name, &prefix);
915 if (ret->name == NULL)
916 ret->name = xmlStrdup(name);
917 ret->prefix = prefix;
918 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000919 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000920 ret->prefix = NULL;
921 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000922 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000923 return(ret);
924}
925
926/**
927 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000928 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +0000929 *
930 * Build a copy of an element content description.
931 *
932 * Returns the new xmlElementContentPtr or NULL in case of error.
933 */
934xmlElementContentPtr
935xmlCopyElementContent(xmlElementContentPtr cur) {
936 xmlElementContentPtr ret;
937
938 if (cur == NULL) return(NULL);
939 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
940 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000941 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000942 return(NULL);
943 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000944 if (cur->prefix != NULL)
945 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000946 ret->ocur = cur->ocur;
947 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000948 if (ret->c1 != NULL)
949 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000950 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000951 if (ret->c2 != NULL)
952 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000953 return(ret);
954}
955
956/**
957 * xmlFreeElementContent:
958 * @cur: the element content tree to free
959 *
960 * Free an element content structure. This is a recursive call !
961 */
962void
963xmlFreeElementContent(xmlElementContentPtr cur) {
964 if (cur == NULL) return;
965 switch (cur->type) {
966 case XML_ELEMENT_CONTENT_PCDATA:
967 case XML_ELEMENT_CONTENT_ELEMENT:
968 case XML_ELEMENT_CONTENT_SEQ:
969 case XML_ELEMENT_CONTENT_OR:
970 break;
971 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000972 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
973 "Internal: ELEMENT content corrupted invalid type\n",
974 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000975 return;
976 }
977 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
978 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
979 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000980 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000981 xmlFree(cur);
982}
983
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000984#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000985/**
986 * xmlDumpElementContent:
987 * @buf: An XML buffer
988 * @content: An element table
989 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
990 *
991 * This will dump the content of the element table as an XML DTD definition
992 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000993static void
Owen Taylor3473f882001-02-23 17:55:21 +0000994xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
995 if (content == NULL) return;
996
997 if (glob) xmlBufferWriteChar(buf, "(");
998 switch (content->type) {
999 case XML_ELEMENT_CONTENT_PCDATA:
1000 xmlBufferWriteChar(buf, "#PCDATA");
1001 break;
1002 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001003 if (content->prefix != NULL) {
1004 xmlBufferWriteCHAR(buf, content->prefix);
1005 xmlBufferWriteChar(buf, ":");
1006 }
Owen Taylor3473f882001-02-23 17:55:21 +00001007 xmlBufferWriteCHAR(buf, content->name);
1008 break;
1009 case XML_ELEMENT_CONTENT_SEQ:
1010 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1011 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1012 xmlDumpElementContent(buf, content->c1, 1);
1013 else
1014 xmlDumpElementContent(buf, content->c1, 0);
1015 xmlBufferWriteChar(buf, " , ");
William M. Brack4119d1c2004-06-24 02:24:44 +00001016 if ((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1017 ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) &&
1018 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
Owen Taylor3473f882001-02-23 17:55:21 +00001019 xmlDumpElementContent(buf, content->c2, 1);
1020 else
1021 xmlDumpElementContent(buf, content->c2, 0);
1022 break;
1023 case XML_ELEMENT_CONTENT_OR:
1024 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1025 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1026 xmlDumpElementContent(buf, content->c1, 1);
1027 else
1028 xmlDumpElementContent(buf, content->c1, 0);
1029 xmlBufferWriteChar(buf, " | ");
William M. Brack4119d1c2004-06-24 02:24:44 +00001030 if ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1031 ((content->c2->type == XML_ELEMENT_CONTENT_OR) &&
1032 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
Owen Taylor3473f882001-02-23 17:55:21 +00001033 xmlDumpElementContent(buf, content->c2, 1);
1034 else
1035 xmlDumpElementContent(buf, content->c2, 0);
1036 break;
1037 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001038 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1039 "Internal: ELEMENT content corrupted invalid type\n",
1040 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001041 }
1042 if (glob)
1043 xmlBufferWriteChar(buf, ")");
1044 switch (content->ocur) {
1045 case XML_ELEMENT_CONTENT_ONCE:
1046 break;
1047 case XML_ELEMENT_CONTENT_OPT:
1048 xmlBufferWriteChar(buf, "?");
1049 break;
1050 case XML_ELEMENT_CONTENT_MULT:
1051 xmlBufferWriteChar(buf, "*");
1052 break;
1053 case XML_ELEMENT_CONTENT_PLUS:
1054 xmlBufferWriteChar(buf, "+");
1055 break;
1056 }
1057}
1058
1059/**
1060 * xmlSprintfElementContent:
1061 * @buf: an output buffer
1062 * @content: An element table
1063 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1064 *
Daniel Veillardd3d06722001-08-15 12:06:36 +00001065 * Deprecated, unsafe, use xmlSnprintfElementContent
1066 */
1067void
1068xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
1069 xmlElementContentPtr content ATTRIBUTE_UNUSED,
1070 int glob ATTRIBUTE_UNUSED) {
1071}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001072#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardd3d06722001-08-15 12:06:36 +00001073
1074/**
1075 * xmlSnprintfElementContent:
1076 * @buf: an output buffer
1077 * @size: the buffer size
1078 * @content: An element table
1079 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1080 *
Owen Taylor3473f882001-02-23 17:55:21 +00001081 * This will dump the content of the element content definition
1082 * Intended just for the debug routine
1083 */
1084void
Daniel Veillardd3d06722001-08-15 12:06:36 +00001085xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
1086 int len;
1087
Owen Taylor3473f882001-02-23 17:55:21 +00001088 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +00001089 len = strlen(buf);
1090 if (size - len < 50) {
1091 if ((size - len > 4) && (buf[len - 1] != '.'))
1092 strcat(buf, " ...");
1093 return;
1094 }
Owen Taylor3473f882001-02-23 17:55:21 +00001095 if (glob) strcat(buf, "(");
1096 switch (content->type) {
1097 case XML_ELEMENT_CONTENT_PCDATA:
1098 strcat(buf, "#PCDATA");
1099 break;
1100 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001101 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001102 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001103 strcat(buf, " ...");
1104 return;
1105 }
1106 strcat(buf, (char *) content->prefix);
1107 strcat(buf, ":");
1108 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001109 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001110 strcat(buf, " ...");
1111 return;
1112 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001113 if (content->name != NULL)
1114 strcat(buf, (char *) content->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001115 break;
1116 case XML_ELEMENT_CONTENT_SEQ:
1117 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1118 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001119 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001120 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001121 xmlSnprintfElementContent(buf, size, content->c1, 0);
1122 len = strlen(buf);
1123 if (size - len < 50) {
1124 if ((size - len > 4) && (buf[len - 1] != '.'))
1125 strcat(buf, " ...");
1126 return;
1127 }
Owen Taylor3473f882001-02-23 17:55:21 +00001128 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001129 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1130 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1131 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001132 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001133 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001134 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001135 break;
1136 case XML_ELEMENT_CONTENT_OR:
1137 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1138 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001139 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001140 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001141 xmlSnprintfElementContent(buf, size, content->c1, 0);
1142 len = strlen(buf);
1143 if (size - len < 50) {
1144 if ((size - len > 4) && (buf[len - 1] != '.'))
1145 strcat(buf, " ...");
1146 return;
1147 }
Owen Taylor3473f882001-02-23 17:55:21 +00001148 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001149 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1150 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1151 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001152 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001153 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001154 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001155 break;
1156 }
1157 if (glob)
1158 strcat(buf, ")");
1159 switch (content->ocur) {
1160 case XML_ELEMENT_CONTENT_ONCE:
1161 break;
1162 case XML_ELEMENT_CONTENT_OPT:
1163 strcat(buf, "?");
1164 break;
1165 case XML_ELEMENT_CONTENT_MULT:
1166 strcat(buf, "*");
1167 break;
1168 case XML_ELEMENT_CONTENT_PLUS:
1169 strcat(buf, "+");
1170 break;
1171 }
1172}
1173
1174/****************************************************************
1175 * *
1176 * Registration of DTD declarations *
1177 * *
1178 ****************************************************************/
1179
1180/**
1181 * xmlCreateElementTable:
1182 *
1183 * create and initialize an empty element hash table.
1184 *
1185 * Returns the xmlElementTablePtr just created or NULL in case of error.
1186 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001187static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001188xmlCreateElementTable(void) {
1189 return(xmlHashCreate(0));
1190}
1191
1192/**
1193 * xmlFreeElement:
1194 * @elem: An element
1195 *
1196 * Deallocate the memory used by an element definition
1197 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001198static void
Owen Taylor3473f882001-02-23 17:55:21 +00001199xmlFreeElement(xmlElementPtr elem) {
1200 if (elem == NULL) return;
1201 xmlUnlinkNode((xmlNodePtr) elem);
1202 xmlFreeElementContent(elem->content);
1203 if (elem->name != NULL)
1204 xmlFree((xmlChar *) elem->name);
1205 if (elem->prefix != NULL)
1206 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001207#ifdef LIBXML_REGEXP_ENABLED
1208 if (elem->contModel != NULL)
1209 xmlRegFreeRegexp(elem->contModel);
1210#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001211 xmlFree(elem);
1212}
1213
1214
1215/**
1216 * xmlAddElementDecl:
1217 * @ctxt: the validation context
1218 * @dtd: pointer to the DTD
1219 * @name: the entity name
1220 * @type: the element type
1221 * @content: the element content tree or NULL
1222 *
1223 * Register a new element declaration
1224 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001225 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001226 */
1227xmlElementPtr
William M. Brackedb65a72004-02-06 07:36:04 +00001228xmlAddElementDecl(xmlValidCtxtPtr ctxt,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001229 xmlDtdPtr dtd, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001230 xmlElementTypeVal type,
1231 xmlElementContentPtr content) {
1232 xmlElementPtr ret;
1233 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001234 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001235 xmlChar *ns, *uqname;
1236
1237 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001238 return(NULL);
1239 }
1240 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001241 return(NULL);
1242 }
1243 switch (type) {
1244 case XML_ELEMENT_TYPE_EMPTY:
1245 if (content != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001246 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1247 "xmlAddElementDecl: content != NULL for EMPTY\n",
1248 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001249 return(NULL);
1250 }
1251 break;
1252 case XML_ELEMENT_TYPE_ANY:
1253 if (content != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001254 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1255 "xmlAddElementDecl: content != NULL for ANY\n",
1256 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001257 return(NULL);
1258 }
1259 break;
1260 case XML_ELEMENT_TYPE_MIXED:
1261 if (content == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001262 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1263 "xmlAddElementDecl: content == NULL for MIXED\n",
1264 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001265 return(NULL);
1266 }
1267 break;
1268 case XML_ELEMENT_TYPE_ELEMENT:
1269 if (content == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001270 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1271 "xmlAddElementDecl: content == NULL for ELEMENT\n",
1272 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001273 return(NULL);
1274 }
1275 break;
1276 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001277 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1278 "Internal: ELEMENT decl corrupted invalid type\n",
1279 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001280 return(NULL);
1281 }
1282
1283 /*
1284 * check if name is a QName
1285 */
1286 uqname = xmlSplitQName2(name, &ns);
1287 if (uqname != NULL)
1288 name = uqname;
1289
1290 /*
1291 * Create the Element table if needed.
1292 */
1293 table = (xmlElementTablePtr) dtd->elements;
1294 if (table == NULL) {
1295 table = xmlCreateElementTable();
1296 dtd->elements = (void *) table;
1297 }
1298 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001299 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001300 "xmlAddElementDecl: Table creation failed!\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001301 if (uqname != NULL)
1302 xmlFree(uqname);
1303 if (ns != NULL)
1304 xmlFree(ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001305 return(NULL);
1306 }
1307
Daniel Veillarda10efa82001-04-18 13:09:01 +00001308 /*
1309 * lookup old attributes inserted on an undefined element in the
1310 * internal subset.
1311 */
1312 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1313 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1314 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1315 oldAttributes = ret->attributes;
1316 ret->attributes = NULL;
1317 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1318 xmlFreeElement(ret);
1319 }
Owen Taylor3473f882001-02-23 17:55:21 +00001320 }
Owen Taylor3473f882001-02-23 17:55:21 +00001321
1322 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001323 * The element may already be present if one of its attribute
1324 * was registered first
1325 */
1326 ret = xmlHashLookup2(table, name, ns);
1327 if (ret != NULL) {
1328 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001329#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001330 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001331 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001332 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001333 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1334 "Redefinition of element %s\n",
1335 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001336#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001337 if (uqname != NULL)
1338 xmlFree(uqname);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001339 if (ns != NULL)
1340 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001341 return(NULL);
1342 }
1343 } else {
1344 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1345 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001346 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001347 if (uqname != NULL)
1348 xmlFree(uqname);
1349 if (ns != NULL)
1350 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001351 return(NULL);
1352 }
1353 memset(ret, 0, sizeof(xmlElement));
1354 ret->type = XML_ELEMENT_DECL;
1355
1356 /*
1357 * fill the structure.
1358 */
1359 ret->name = xmlStrdup(name);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001360 if (ret->name == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001361 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001362 if (uqname != NULL)
1363 xmlFree(uqname);
1364 if (ns != NULL)
1365 xmlFree(ns);
1366 xmlFree(ret);
1367 return(NULL);
1368 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001369 ret->prefix = ns;
1370
1371 /*
1372 * Validity Check:
1373 * Insertion must not fail
1374 */
1375 if (xmlHashAddEntry2(table, name, ns, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001376#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001377 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001378 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001379 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001380 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1381 "Redefinition of element %s\n",
1382 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001383#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001384 xmlFreeElement(ret);
1385 if (uqname != NULL)
1386 xmlFree(uqname);
1387 return(NULL);
1388 }
William M. Brack4e52f2f2003-09-14 18:07:39 +00001389 /*
1390 * For new element, may have attributes from earlier
1391 * definition in internal subset
1392 */
1393 ret->attributes = oldAttributes;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001394 }
1395
1396 /*
1397 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001398 */
1399 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +00001400 ret->content = xmlCopyElementContent(content);
Owen Taylor3473f882001-02-23 17:55:21 +00001401
1402 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001403 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001404 */
1405 ret->parent = dtd;
1406 ret->doc = dtd->doc;
1407 if (dtd->last == NULL) {
1408 dtd->children = dtd->last = (xmlNodePtr) ret;
1409 } else {
1410 dtd->last->next = (xmlNodePtr) ret;
1411 ret->prev = dtd->last;
1412 dtd->last = (xmlNodePtr) ret;
1413 }
1414 if (uqname != NULL)
1415 xmlFree(uqname);
1416 return(ret);
1417}
1418
1419/**
1420 * xmlFreeElementTable:
1421 * @table: An element table
1422 *
1423 * Deallocate the memory used by an element hash table.
1424 */
1425void
1426xmlFreeElementTable(xmlElementTablePtr table) {
1427 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1428}
1429
Daniel Veillard652327a2003-09-29 18:02:38 +00001430#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001431/**
1432 * xmlCopyElement:
1433 * @elem: An element
1434 *
1435 * Build a copy of an element.
1436 *
1437 * Returns the new xmlElementPtr or NULL in case of error.
1438 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001439static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001440xmlCopyElement(xmlElementPtr elem) {
1441 xmlElementPtr cur;
1442
1443 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1444 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001445 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001446 return(NULL);
1447 }
1448 memset(cur, 0, sizeof(xmlElement));
1449 cur->type = XML_ELEMENT_DECL;
1450 cur->etype = elem->etype;
1451 if (elem->name != NULL)
1452 cur->name = xmlStrdup(elem->name);
1453 else
1454 cur->name = NULL;
1455 if (elem->prefix != NULL)
1456 cur->prefix = xmlStrdup(elem->prefix);
1457 else
1458 cur->prefix = NULL;
1459 cur->content = xmlCopyElementContent(elem->content);
1460 /* TODO : rebuild the attribute list on the copy */
1461 cur->attributes = NULL;
1462 return(cur);
1463}
1464
1465/**
1466 * xmlCopyElementTable:
1467 * @table: An element table
1468 *
1469 * Build a copy of an element table.
1470 *
1471 * Returns the new xmlElementTablePtr or NULL in case of error.
1472 */
1473xmlElementTablePtr
1474xmlCopyElementTable(xmlElementTablePtr table) {
1475 return((xmlElementTablePtr) xmlHashCopy(table,
1476 (xmlHashCopier) xmlCopyElement));
1477}
Daniel Veillard652327a2003-09-29 18:02:38 +00001478#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001479
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001480#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001481/**
1482 * xmlDumpElementDecl:
1483 * @buf: the XML buffer output
1484 * @elem: An element table
1485 *
1486 * This will dump the content of the element declaration as an XML
1487 * DTD definition
1488 */
1489void
1490xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1491 switch (elem->etype) {
1492 case XML_ELEMENT_TYPE_EMPTY:
1493 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001494 if (elem->prefix != NULL) {
1495 xmlBufferWriteCHAR(buf, elem->prefix);
1496 xmlBufferWriteChar(buf, ":");
1497 }
Owen Taylor3473f882001-02-23 17:55:21 +00001498 xmlBufferWriteCHAR(buf, elem->name);
1499 xmlBufferWriteChar(buf, " EMPTY>\n");
1500 break;
1501 case XML_ELEMENT_TYPE_ANY:
1502 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001503 if (elem->prefix != NULL) {
1504 xmlBufferWriteCHAR(buf, elem->prefix);
1505 xmlBufferWriteChar(buf, ":");
1506 }
Owen Taylor3473f882001-02-23 17:55:21 +00001507 xmlBufferWriteCHAR(buf, elem->name);
1508 xmlBufferWriteChar(buf, " ANY>\n");
1509 break;
1510 case XML_ELEMENT_TYPE_MIXED:
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 case XML_ELEMENT_TYPE_ELEMENT:
1522 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001523 if (elem->prefix != NULL) {
1524 xmlBufferWriteCHAR(buf, elem->prefix);
1525 xmlBufferWriteChar(buf, ":");
1526 }
Owen Taylor3473f882001-02-23 17:55:21 +00001527 xmlBufferWriteCHAR(buf, elem->name);
1528 xmlBufferWriteChar(buf, " ");
1529 xmlDumpElementContent(buf, elem->content, 1);
1530 xmlBufferWriteChar(buf, ">\n");
1531 break;
1532 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001533 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1534 "Internal: ELEMENT struct corrupted invalid type\n",
1535 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001536 }
1537}
1538
1539/**
William M. Brack9e660592003-10-20 14:56:06 +00001540 * xmlDumpElementDeclScan:
1541 * @elem: An element table
1542 * @buf: the XML buffer output
1543 *
1544 * This routine is used by the hash scan function. It just reverses
1545 * the arguments.
1546 */
1547static void
1548xmlDumpElementDeclScan(xmlElementPtr elem, xmlBufferPtr buf) {
1549 xmlDumpElementDecl(buf, elem);
1550}
1551
1552/**
Owen Taylor3473f882001-02-23 17:55:21 +00001553 * xmlDumpElementTable:
1554 * @buf: the XML buffer output
1555 * @table: An element table
1556 *
1557 * This will dump the content of the element table as an XML DTD definition
1558 */
1559void
1560xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +00001561 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001562}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001563#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001564
1565/**
1566 * xmlCreateEnumeration:
1567 * @name: the enumeration name or NULL
1568 *
1569 * create and initialize an enumeration attribute node.
1570 *
1571 * Returns the xmlEnumerationPtr just created or NULL in case
1572 * of error.
1573 */
1574xmlEnumerationPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001575xmlCreateEnumeration(const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00001576 xmlEnumerationPtr ret;
1577
1578 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1579 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001580 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001581 return(NULL);
1582 }
1583 memset(ret, 0, sizeof(xmlEnumeration));
1584
1585 if (name != NULL)
1586 ret->name = xmlStrdup(name);
1587 return(ret);
1588}
1589
1590/**
1591 * xmlFreeEnumeration:
1592 * @cur: the tree to free.
1593 *
1594 * free an enumeration attribute node (recursive).
1595 */
1596void
1597xmlFreeEnumeration(xmlEnumerationPtr cur) {
1598 if (cur == NULL) return;
1599
1600 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1601
1602 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001603 xmlFree(cur);
1604}
1605
Daniel Veillard652327a2003-09-29 18:02:38 +00001606#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001607/**
1608 * xmlCopyEnumeration:
1609 * @cur: the tree to copy.
1610 *
1611 * Copy an enumeration attribute node (recursive).
1612 *
1613 * Returns the xmlEnumerationPtr just created or NULL in case
1614 * of error.
1615 */
1616xmlEnumerationPtr
1617xmlCopyEnumeration(xmlEnumerationPtr cur) {
1618 xmlEnumerationPtr ret;
1619
1620 if (cur == NULL) return(NULL);
1621 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1622
1623 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1624 else ret->next = NULL;
1625
1626 return(ret);
1627}
Daniel Veillard652327a2003-09-29 18:02:38 +00001628#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001629
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001630#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001631/**
1632 * xmlDumpEnumeration:
1633 * @buf: the XML buffer output
1634 * @enum: An enumeration
1635 *
1636 * This will dump the content of the enumeration
1637 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001638static void
Owen Taylor3473f882001-02-23 17:55:21 +00001639xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1640 if (cur == NULL) return;
1641
1642 xmlBufferWriteCHAR(buf, cur->name);
1643 if (cur->next == NULL)
1644 xmlBufferWriteChar(buf, ")");
1645 else {
1646 xmlBufferWriteChar(buf, " | ");
1647 xmlDumpEnumeration(buf, cur->next);
1648 }
1649}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001650#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001651
1652/**
1653 * xmlCreateAttributeTable:
1654 *
1655 * create and initialize an empty attribute hash table.
1656 *
1657 * Returns the xmlAttributeTablePtr just created or NULL in case
1658 * of error.
1659 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001660static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001661xmlCreateAttributeTable(void) {
1662 return(xmlHashCreate(0));
1663}
1664
Daniel Veillard4432df22003-09-28 18:58:27 +00001665#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001666/**
1667 * xmlScanAttributeDeclCallback:
1668 * @attr: the attribute decl
1669 * @list: the list to update
1670 *
1671 * Callback called by xmlScanAttributeDecl when a new attribute
1672 * has to be entered in the list.
1673 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001674static void
Owen Taylor3473f882001-02-23 17:55:21 +00001675xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001676 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001677 attr->nexth = *list;
1678 *list = attr;
1679}
1680
1681/**
1682 * xmlScanAttributeDecl:
1683 * @dtd: pointer to the DTD
1684 * @elem: the element name
1685 *
1686 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001687 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001688 *
1689 * Returns the pointer to the first attribute decl in the chain,
1690 * possibly NULL.
1691 */
1692xmlAttributePtr
1693xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1694 xmlAttributePtr ret = NULL;
1695 xmlAttributeTablePtr table;
1696
1697 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001698 return(NULL);
1699 }
1700 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001701 return(NULL);
1702 }
1703 table = (xmlAttributeTablePtr) dtd->attributes;
1704 if (table == NULL)
1705 return(NULL);
1706
1707 /* WRONG !!! */
1708 xmlHashScan3(table, NULL, NULL, elem,
1709 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1710 return(ret);
1711}
1712
1713/**
1714 * xmlScanIDAttributeDecl:
1715 * @ctxt: the validation context
1716 * @elem: the element name
1717 *
1718 * Verify that the element don't have too many ID attributes
1719 * declared.
1720 *
1721 * Returns the number of ID attributes found.
1722 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001723static int
Owen Taylor3473f882001-02-23 17:55:21 +00001724xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1725 xmlAttributePtr cur;
1726 int ret = 0;
1727
1728 if (elem == NULL) return(0);
1729 cur = elem->attributes;
1730 while (cur != NULL) {
1731 if (cur->atype == XML_ATTRIBUTE_ID) {
1732 ret ++;
1733 if (ret > 1)
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001734 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001735 "Element %s has too many ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001736 elem->name, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001737 }
1738 cur = cur->nexth;
1739 }
1740 return(ret);
1741}
Daniel Veillard4432df22003-09-28 18:58:27 +00001742#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001743
1744/**
1745 * xmlFreeAttribute:
1746 * @elem: An attribute
1747 *
1748 * Deallocate the memory used by an attribute definition
1749 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001750static void
Owen Taylor3473f882001-02-23 17:55:21 +00001751xmlFreeAttribute(xmlAttributePtr attr) {
1752 if (attr == NULL) return;
1753 xmlUnlinkNode((xmlNodePtr) attr);
1754 if (attr->tree != NULL)
1755 xmlFreeEnumeration(attr->tree);
1756 if (attr->elem != NULL)
1757 xmlFree((xmlChar *) attr->elem);
1758 if (attr->name != NULL)
1759 xmlFree((xmlChar *) attr->name);
1760 if (attr->defaultValue != NULL)
1761 xmlFree((xmlChar *) attr->defaultValue);
1762 if (attr->prefix != NULL)
1763 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001764 xmlFree(attr);
1765}
1766
1767
1768/**
1769 * xmlAddAttributeDecl:
1770 * @ctxt: the validation context
1771 * @dtd: pointer to the DTD
1772 * @elem: the element name
1773 * @name: the attribute name
1774 * @ns: the attribute namespace prefix
1775 * @type: the attribute type
1776 * @def: the attribute default type
1777 * @defaultValue: the attribute default value
1778 * @tree: if it's an enumeration, the associated list
1779 *
1780 * Register a new attribute declaration
1781 * Note that @tree becomes the ownership of the DTD
1782 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001783 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001784 */
1785xmlAttributePtr
William M. Brackedb65a72004-02-06 07:36:04 +00001786xmlAddAttributeDecl(xmlValidCtxtPtr ctxt,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001787 xmlDtdPtr dtd, const xmlChar *elem,
Owen Taylor3473f882001-02-23 17:55:21 +00001788 const xmlChar *name, const xmlChar *ns,
1789 xmlAttributeType type, xmlAttributeDefault def,
1790 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1791 xmlAttributePtr ret;
1792 xmlAttributeTablePtr table;
1793 xmlElementPtr elemDef;
1794
1795 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001796 xmlFreeEnumeration(tree);
1797 return(NULL);
1798 }
1799 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001800 xmlFreeEnumeration(tree);
1801 return(NULL);
1802 }
1803 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001804 xmlFreeEnumeration(tree);
1805 return(NULL);
1806 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001807
Daniel Veillard4432df22003-09-28 18:58:27 +00001808#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001809 /*
1810 * Check the type and possibly the default value.
1811 */
1812 switch (type) {
1813 case XML_ATTRIBUTE_CDATA:
1814 break;
1815 case XML_ATTRIBUTE_ID:
1816 break;
1817 case XML_ATTRIBUTE_IDREF:
1818 break;
1819 case XML_ATTRIBUTE_IDREFS:
1820 break;
1821 case XML_ATTRIBUTE_ENTITY:
1822 break;
1823 case XML_ATTRIBUTE_ENTITIES:
1824 break;
1825 case XML_ATTRIBUTE_NMTOKEN:
1826 break;
1827 case XML_ATTRIBUTE_NMTOKENS:
1828 break;
1829 case XML_ATTRIBUTE_ENUMERATION:
1830 break;
1831 case XML_ATTRIBUTE_NOTATION:
1832 break;
1833 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001834 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1835 "Internal: ATTRIBUTE struct corrupted invalid type\n",
1836 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001837 xmlFreeEnumeration(tree);
1838 return(NULL);
1839 }
1840 if ((defaultValue != NULL) &&
1841 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001842 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
1843 "Attribute %s of %s: invalid default value\n",
1844 elem, name, defaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001845 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001846 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001847 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001848#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001849
1850 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001851 * Check first that an attribute defined in the external subset wasn't
1852 * already defined in the internal subset
1853 */
1854 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1855 (dtd->doc->intSubset != NULL) &&
1856 (dtd->doc->intSubset->attributes != NULL)) {
1857 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1858 if (ret != NULL)
1859 return(NULL);
1860 }
1861
1862 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001863 * Create the Attribute table if needed.
1864 */
1865 table = (xmlAttributeTablePtr) dtd->attributes;
1866 if (table == NULL) {
1867 table = xmlCreateAttributeTable();
1868 dtd->attributes = (void *) table;
1869 }
1870 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001871 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001872 "xmlAddAttributeDecl: Table creation failed!\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001873 return(NULL);
1874 }
1875
1876
1877 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1878 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001879 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001880 return(NULL);
1881 }
1882 memset(ret, 0, sizeof(xmlAttribute));
1883 ret->type = XML_ATTRIBUTE_DECL;
1884
1885 /*
1886 * fill the structure.
1887 */
1888 ret->atype = type;
1889 ret->name = xmlStrdup(name);
1890 ret->prefix = xmlStrdup(ns);
1891 ret->elem = xmlStrdup(elem);
1892 ret->def = def;
1893 ret->tree = tree;
1894 if (defaultValue != NULL)
1895 ret->defaultValue = xmlStrdup(defaultValue);
1896
1897 /*
1898 * Validity Check:
1899 * Search the DTD for previous declarations of the ATTLIST
1900 */
1901 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001902#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001903 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001904 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001905 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001906 xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00001907 "Attribute %s of element %s: already defined\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001908 name, elem, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001909#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001910 xmlFreeAttribute(ret);
1911 return(NULL);
1912 }
1913
1914 /*
1915 * Validity Check:
1916 * Multiple ID per element
1917 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001918 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001919 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001920
Daniel Veillard4432df22003-09-28 18:58:27 +00001921#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001922 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001923 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001924 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
Owen Taylor3473f882001-02-23 17:55:21 +00001925 "Element %s has too may ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001926 elem, name, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00001927 ctxt->valid = 0;
1928 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001929#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardc7612992002-02-17 22:47:37 +00001930
Daniel Veillard48da9102001-08-07 01:10:10 +00001931 /*
1932 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001933 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001934 */
1935 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1936 ((ret->prefix != NULL &&
1937 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1938 ret->nexth = elemDef->attributes;
1939 elemDef->attributes = ret;
1940 } else {
1941 xmlAttributePtr tmp = elemDef->attributes;
1942
1943 while ((tmp != NULL) &&
1944 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1945 ((ret->prefix != NULL &&
1946 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1947 if (tmp->nexth == NULL)
1948 break;
1949 tmp = tmp->nexth;
1950 }
1951 if (tmp != NULL) {
1952 ret->nexth = tmp->nexth;
1953 tmp->nexth = ret;
1954 } else {
1955 ret->nexth = elemDef->attributes;
1956 elemDef->attributes = ret;
1957 }
1958 }
Owen Taylor3473f882001-02-23 17:55:21 +00001959 }
1960
1961 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001962 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001963 */
1964 ret->parent = dtd;
1965 ret->doc = dtd->doc;
1966 if (dtd->last == NULL) {
1967 dtd->children = dtd->last = (xmlNodePtr) ret;
1968 } else {
1969 dtd->last->next = (xmlNodePtr) ret;
1970 ret->prev = dtd->last;
1971 dtd->last = (xmlNodePtr) ret;
1972 }
1973 return(ret);
1974}
1975
1976/**
1977 * xmlFreeAttributeTable:
1978 * @table: An attribute table
1979 *
1980 * Deallocate the memory used by an entities hash table.
1981 */
1982void
1983xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1984 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1985}
1986
Daniel Veillard652327a2003-09-29 18:02:38 +00001987#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001988/**
1989 * xmlCopyAttribute:
1990 * @attr: An attribute
1991 *
1992 * Build a copy of an attribute.
1993 *
1994 * Returns the new xmlAttributePtr or NULL in case of error.
1995 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001996static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001997xmlCopyAttribute(xmlAttributePtr attr) {
1998 xmlAttributePtr cur;
1999
2000 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2001 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002002 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002003 return(NULL);
2004 }
2005 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00002006 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00002007 cur->atype = attr->atype;
2008 cur->def = attr->def;
2009 cur->tree = xmlCopyEnumeration(attr->tree);
2010 if (attr->elem != NULL)
2011 cur->elem = xmlStrdup(attr->elem);
2012 if (attr->name != NULL)
2013 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00002014 if (attr->prefix != NULL)
2015 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002016 if (attr->defaultValue != NULL)
2017 cur->defaultValue = xmlStrdup(attr->defaultValue);
2018 return(cur);
2019}
2020
2021/**
2022 * xmlCopyAttributeTable:
2023 * @table: An attribute table
2024 *
2025 * Build a copy of an attribute table.
2026 *
2027 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2028 */
2029xmlAttributeTablePtr
2030xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2031 return((xmlAttributeTablePtr) xmlHashCopy(table,
2032 (xmlHashCopier) xmlCopyAttribute));
2033}
Daniel Veillard652327a2003-09-29 18:02:38 +00002034#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002035
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002036#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002037/**
2038 * xmlDumpAttributeDecl:
2039 * @buf: the XML buffer output
2040 * @attr: An attribute declaration
2041 *
2042 * This will dump the content of the attribute declaration as an XML
2043 * DTD definition
2044 */
2045void
2046xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
2047 xmlBufferWriteChar(buf, "<!ATTLIST ");
2048 xmlBufferWriteCHAR(buf, attr->elem);
2049 xmlBufferWriteChar(buf, " ");
2050 if (attr->prefix != NULL) {
2051 xmlBufferWriteCHAR(buf, attr->prefix);
2052 xmlBufferWriteChar(buf, ":");
2053 }
2054 xmlBufferWriteCHAR(buf, attr->name);
2055 switch (attr->atype) {
2056 case XML_ATTRIBUTE_CDATA:
2057 xmlBufferWriteChar(buf, " CDATA");
2058 break;
2059 case XML_ATTRIBUTE_ID:
2060 xmlBufferWriteChar(buf, " ID");
2061 break;
2062 case XML_ATTRIBUTE_IDREF:
2063 xmlBufferWriteChar(buf, " IDREF");
2064 break;
2065 case XML_ATTRIBUTE_IDREFS:
2066 xmlBufferWriteChar(buf, " IDREFS");
2067 break;
2068 case XML_ATTRIBUTE_ENTITY:
2069 xmlBufferWriteChar(buf, " ENTITY");
2070 break;
2071 case XML_ATTRIBUTE_ENTITIES:
2072 xmlBufferWriteChar(buf, " ENTITIES");
2073 break;
2074 case XML_ATTRIBUTE_NMTOKEN:
2075 xmlBufferWriteChar(buf, " NMTOKEN");
2076 break;
2077 case XML_ATTRIBUTE_NMTOKENS:
2078 xmlBufferWriteChar(buf, " NMTOKENS");
2079 break;
2080 case XML_ATTRIBUTE_ENUMERATION:
2081 xmlBufferWriteChar(buf, " (");
2082 xmlDumpEnumeration(buf, attr->tree);
2083 break;
2084 case XML_ATTRIBUTE_NOTATION:
2085 xmlBufferWriteChar(buf, " NOTATION (");
2086 xmlDumpEnumeration(buf, attr->tree);
2087 break;
2088 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002089 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2090 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2091 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002092 }
2093 switch (attr->def) {
2094 case XML_ATTRIBUTE_NONE:
2095 break;
2096 case XML_ATTRIBUTE_REQUIRED:
2097 xmlBufferWriteChar(buf, " #REQUIRED");
2098 break;
2099 case XML_ATTRIBUTE_IMPLIED:
2100 xmlBufferWriteChar(buf, " #IMPLIED");
2101 break;
2102 case XML_ATTRIBUTE_FIXED:
2103 xmlBufferWriteChar(buf, " #FIXED");
2104 break;
2105 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002106 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2107 "Internal: ATTRIBUTE struct corrupted invalid def\n",
2108 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002109 }
2110 if (attr->defaultValue != NULL) {
2111 xmlBufferWriteChar(buf, " ");
2112 xmlBufferWriteQuotedString(buf, attr->defaultValue);
2113 }
2114 xmlBufferWriteChar(buf, ">\n");
2115}
2116
2117/**
William M. Brack9e660592003-10-20 14:56:06 +00002118 * xmlDumpAttributeDeclScan:
2119 * @attr: An attribute declaration
2120 * @buf: the XML buffer output
2121 *
2122 * This is used with the hash scan function - just reverses arguments
2123 */
2124static void
2125xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) {
2126 xmlDumpAttributeDecl(buf, attr);
2127}
2128
2129/**
Owen Taylor3473f882001-02-23 17:55:21 +00002130 * xmlDumpAttributeTable:
2131 * @buf: the XML buffer output
2132 * @table: An attribute table
2133 *
2134 * This will dump the content of the attribute table as an XML DTD definition
2135 */
2136void
2137xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +00002138 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002139}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002140#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002141
2142/************************************************************************
2143 * *
2144 * NOTATIONs *
2145 * *
2146 ************************************************************************/
2147/**
2148 * xmlCreateNotationTable:
2149 *
2150 * create and initialize an empty notation hash table.
2151 *
2152 * Returns the xmlNotationTablePtr just created or NULL in case
2153 * of error.
2154 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002155static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002156xmlCreateNotationTable(void) {
2157 return(xmlHashCreate(0));
2158}
2159
2160/**
2161 * xmlFreeNotation:
2162 * @not: A notation
2163 *
2164 * Deallocate the memory used by an notation definition
2165 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002166static void
Owen Taylor3473f882001-02-23 17:55:21 +00002167xmlFreeNotation(xmlNotationPtr nota) {
2168 if (nota == NULL) return;
2169 if (nota->name != NULL)
2170 xmlFree((xmlChar *) nota->name);
2171 if (nota->PublicID != NULL)
2172 xmlFree((xmlChar *) nota->PublicID);
2173 if (nota->SystemID != NULL)
2174 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002175 xmlFree(nota);
2176}
2177
2178
2179/**
2180 * xmlAddNotationDecl:
2181 * @dtd: pointer to the DTD
2182 * @ctxt: the validation context
2183 * @name: the entity name
2184 * @PublicID: the public identifier or NULL
2185 * @SystemID: the system identifier or NULL
2186 *
2187 * Register a new notation declaration
2188 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002189 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002190 */
2191xmlNotationPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002192xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002193 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002194 const xmlChar *PublicID, const xmlChar *SystemID) {
2195 xmlNotationPtr ret;
2196 xmlNotationTablePtr table;
2197
2198 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002199 return(NULL);
2200 }
2201 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002202 return(NULL);
2203 }
2204 if ((PublicID == NULL) && (SystemID == NULL)) {
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002205 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002206 }
2207
2208 /*
2209 * Create the Notation table if needed.
2210 */
2211 table = (xmlNotationTablePtr) dtd->notations;
2212 if (table == NULL)
2213 dtd->notations = table = xmlCreateNotationTable();
2214 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002215 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002216 "xmlAddNotationDecl: Table creation failed!\n");
2217 return(NULL);
2218 }
2219
2220 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2221 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002222 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002223 return(NULL);
2224 }
2225 memset(ret, 0, sizeof(xmlNotation));
2226
2227 /*
2228 * fill the structure.
2229 */
2230 ret->name = xmlStrdup(name);
2231 if (SystemID != NULL)
2232 ret->SystemID = xmlStrdup(SystemID);
2233 if (PublicID != NULL)
2234 ret->PublicID = xmlStrdup(PublicID);
2235
2236 /*
2237 * Validity Check:
2238 * Check the DTD for previous declarations of the ATTLIST
2239 */
2240 if (xmlHashAddEntry(table, name, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002241#ifdef LIBXML_VALID_ENABLED
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002242 xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
2243 "xmlAddNotationDecl: %s already defined\n",
2244 (const char *) name);
Daniel Veillard4432df22003-09-28 18:58:27 +00002245#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002246 xmlFreeNotation(ret);
2247 return(NULL);
2248 }
2249 return(ret);
2250}
2251
2252/**
2253 * xmlFreeNotationTable:
2254 * @table: An notation table
2255 *
2256 * Deallocate the memory used by an entities hash table.
2257 */
2258void
2259xmlFreeNotationTable(xmlNotationTablePtr table) {
2260 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2261}
2262
Daniel Veillard652327a2003-09-29 18:02:38 +00002263#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002264/**
2265 * xmlCopyNotation:
2266 * @nota: A notation
2267 *
2268 * Build a copy of a notation.
2269 *
2270 * Returns the new xmlNotationPtr or NULL in case of error.
2271 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002272static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002273xmlCopyNotation(xmlNotationPtr nota) {
2274 xmlNotationPtr cur;
2275
2276 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2277 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002278 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002279 return(NULL);
2280 }
2281 if (nota->name != NULL)
2282 cur->name = xmlStrdup(nota->name);
2283 else
2284 cur->name = NULL;
2285 if (nota->PublicID != NULL)
2286 cur->PublicID = xmlStrdup(nota->PublicID);
2287 else
2288 cur->PublicID = NULL;
2289 if (nota->SystemID != NULL)
2290 cur->SystemID = xmlStrdup(nota->SystemID);
2291 else
2292 cur->SystemID = NULL;
2293 return(cur);
2294}
2295
2296/**
2297 * xmlCopyNotationTable:
2298 * @table: A notation table
2299 *
2300 * Build a copy of a notation table.
2301 *
2302 * Returns the new xmlNotationTablePtr or NULL in case of error.
2303 */
2304xmlNotationTablePtr
2305xmlCopyNotationTable(xmlNotationTablePtr table) {
2306 return((xmlNotationTablePtr) xmlHashCopy(table,
2307 (xmlHashCopier) xmlCopyNotation));
2308}
Daniel Veillard652327a2003-09-29 18:02:38 +00002309#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002310
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002311#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002312/**
2313 * xmlDumpNotationDecl:
2314 * @buf: the XML buffer output
2315 * @nota: A notation declaration
2316 *
2317 * This will dump the content the notation declaration as an XML DTD definition
2318 */
2319void
2320xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2321 xmlBufferWriteChar(buf, "<!NOTATION ");
2322 xmlBufferWriteCHAR(buf, nota->name);
2323 if (nota->PublicID != NULL) {
2324 xmlBufferWriteChar(buf, " PUBLIC ");
2325 xmlBufferWriteQuotedString(buf, nota->PublicID);
2326 if (nota->SystemID != NULL) {
2327 xmlBufferWriteChar(buf, " ");
2328 xmlBufferWriteCHAR(buf, nota->SystemID);
2329 }
2330 } else {
2331 xmlBufferWriteChar(buf, " SYSTEM ");
2332 xmlBufferWriteCHAR(buf, nota->SystemID);
2333 }
2334 xmlBufferWriteChar(buf, " >\n");
2335}
2336
2337/**
William M. Brack9e660592003-10-20 14:56:06 +00002338 * xmlDumpNotationDeclScan:
2339 * @nota: A notation declaration
2340 * @buf: the XML buffer output
2341 *
2342 * This is called with the hash scan function, and just reverses args
2343 */
2344static void
2345xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) {
2346 xmlDumpNotationDecl(buf, nota);
2347}
2348
2349/**
Owen Taylor3473f882001-02-23 17:55:21 +00002350 * xmlDumpNotationTable:
2351 * @buf: the XML buffer output
2352 * @table: A notation table
2353 *
2354 * This will dump the content of the notation table as an XML DTD definition
2355 */
2356void
2357xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +00002358 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002359}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002360#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002361
2362/************************************************************************
2363 * *
2364 * IDs *
2365 * *
2366 ************************************************************************/
2367/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002368 * DICT_FREE:
2369 * @str: a string
2370 *
2371 * Free a string if it is not owned by the "dict" dictionnary in the
2372 * current scope
2373 */
2374#define DICT_FREE(str) \
2375 if ((str) && ((!dict) || \
2376 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
2377 xmlFree((char *)(str));
2378
2379/**
Owen Taylor3473f882001-02-23 17:55:21 +00002380 * xmlCreateIDTable:
2381 *
2382 * create and initialize an empty id hash table.
2383 *
2384 * Returns the xmlIDTablePtr just created or NULL in case
2385 * of error.
2386 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002387static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002388xmlCreateIDTable(void) {
2389 return(xmlHashCreate(0));
2390}
2391
2392/**
2393 * xmlFreeID:
2394 * @not: A id
2395 *
2396 * Deallocate the memory used by an id definition
2397 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002398static void
Owen Taylor3473f882001-02-23 17:55:21 +00002399xmlFreeID(xmlIDPtr id) {
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002400 xmlDictPtr dict = NULL;
2401
Owen Taylor3473f882001-02-23 17:55:21 +00002402 if (id == NULL) return;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002403
2404 if (id->doc != NULL)
2405 dict = id->doc->dict;
2406
Owen Taylor3473f882001-02-23 17:55:21 +00002407 if (id->value != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002408 DICT_FREE(id->value)
Daniel Veillardea7751d2002-12-20 00:16:24 +00002409 if (id->name != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002410 DICT_FREE(id->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002411 xmlFree(id);
2412}
2413
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002414
Owen Taylor3473f882001-02-23 17:55:21 +00002415/**
2416 * xmlAddID:
2417 * @ctxt: the validation context
2418 * @doc: pointer to the document
2419 * @value: the value name
2420 * @attr: the attribute holding the ID
2421 *
2422 * Register a new id declaration
2423 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002424 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002425 */
2426xmlIDPtr
2427xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2428 xmlAttrPtr attr) {
2429 xmlIDPtr ret;
2430 xmlIDTablePtr table;
2431
2432 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002433 return(NULL);
2434 }
2435 if (value == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002436 return(NULL);
2437 }
2438 if (attr == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002439 return(NULL);
2440 }
2441
2442 /*
2443 * Create the ID table if needed.
2444 */
2445 table = (xmlIDTablePtr) doc->ids;
2446 if (table == NULL)
2447 doc->ids = table = xmlCreateIDTable();
2448 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002449 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002450 "xmlAddID: Table creation failed!\n");
2451 return(NULL);
2452 }
2453
2454 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2455 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002456 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002457 return(NULL);
2458 }
2459
2460 /*
2461 * fill the structure.
2462 */
2463 ret->value = xmlStrdup(value);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002464 ret->doc = doc;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002465 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2466 /*
2467 * Operating in streaming mode, attr is gonna disapear
2468 */
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002469 if (doc->dict != NULL)
2470 ret->name = xmlDictLookup(doc->dict, attr->name, -1);
2471 else
2472 ret->name = xmlStrdup(attr->name);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002473 ret->attr = NULL;
2474 } else {
2475 ret->attr = attr;
2476 ret->name = NULL;
2477 }
2478 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002479
2480 if (xmlHashAddEntry(table, value, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002481#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002482 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002483 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002484 */
Daniel Veillardd3669b22004-02-25 12:34:55 +00002485 if ((ctxt != NULL) && (ctxt->error != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002486 xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2487 "ID %s already defined\n",
2488 value, NULL, NULL);
Daniel Veillard76575762002-09-05 14:21:15 +00002489 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002490#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002491 xmlFreeID(ret);
2492 return(NULL);
2493 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002494 if (attr != NULL)
2495 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002496 return(ret);
2497}
2498
2499/**
2500 * xmlFreeIDTable:
2501 * @table: An id table
2502 *
2503 * Deallocate the memory used by an ID hash table.
2504 */
2505void
2506xmlFreeIDTable(xmlIDTablePtr table) {
2507 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2508}
2509
2510/**
2511 * xmlIsID:
2512 * @doc: the document
2513 * @elem: the element carrying the attribute
2514 * @attr: the attribute
2515 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002516 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002517 * then this is done if DTD loading has been requested. In the case
2518 * of HTML documents parsed with the HTML parser, then ID detection is
2519 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002520 *
2521 * Returns 0 or 1 depending on the lookup result
2522 */
2523int
2524xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2525 if (doc == NULL) return(0);
2526 if (attr == NULL) return(0);
2527 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2528 return(0);
2529 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillard9ba8e382003-10-28 21:31:45 +00002530 if (((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2531 (xmlStrEqual(BAD_CAST "name", attr->name))) &&
2532 ((elem != NULL) && (!xmlStrEqual(elem->name, BAD_CAST "input"))))
Owen Taylor3473f882001-02-23 17:55:21 +00002533 return(1);
2534 return(0);
2535 } else {
2536 xmlAttributePtr attrDecl;
2537
2538 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002539 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00002540 xmlChar fn[50];
Daniel Veillard37f961d2002-07-06 17:53:56 +00002541 xmlChar *fullname;
Daniel Veillardc00cda82003-04-07 10:22:39 +00002542
2543 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002544 if (fullname == NULL)
2545 return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002546 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2547 attr->name);
2548 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2549 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2550 attr->name);
Daniel Veillardc00cda82003-04-07 10:22:39 +00002551 if ((fullname != fn) && (fullname != elem->name))
2552 xmlFree(fullname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002553 } else {
2554 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2555 attr->name);
2556 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2557 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2558 attr->name);
2559 }
Owen Taylor3473f882001-02-23 17:55:21 +00002560
2561 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2562 return(1);
2563 }
2564 return(0);
2565}
2566
2567/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002568 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002569 * @doc: the document
2570 * @attr: the attribute
2571 *
2572 * Remove the given attribute from the ID table maintained internally.
2573 *
2574 * Returns -1 if the lookup failed and 0 otherwise
2575 */
2576int
2577xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002578 xmlIDTablePtr table;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002579 xmlIDPtr id;
Owen Taylor3473f882001-02-23 17:55:21 +00002580 xmlChar *ID;
2581
2582 if (doc == NULL) return(-1);
2583 if (attr == NULL) return(-1);
2584 table = (xmlIDTablePtr) doc->ids;
2585 if (table == NULL)
2586 return(-1);
2587
2588 if (attr == NULL)
2589 return(-1);
2590 ID = xmlNodeListGetString(doc, attr->children, 1);
2591 if (ID == NULL)
2592 return(-1);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002593 id = xmlHashLookup(table, ID);
2594 if (id == NULL || id->attr != attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002595 xmlFree(ID);
2596 return(-1);
2597 }
2598 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
2599 xmlFree(ID);
2600 return(0);
2601}
2602
2603/**
2604 * xmlGetID:
2605 * @doc: pointer to the document
2606 * @ID: the ID value
2607 *
2608 * Search the attribute declaring the given ID
2609 *
2610 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2611 */
2612xmlAttrPtr
2613xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2614 xmlIDTablePtr table;
2615 xmlIDPtr id;
2616
2617 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002618 return(NULL);
2619 }
2620
2621 if (ID == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002622 return(NULL);
2623 }
2624
2625 table = (xmlIDTablePtr) doc->ids;
2626 if (table == NULL)
2627 return(NULL);
2628
2629 id = xmlHashLookup(table, ID);
2630 if (id == NULL)
2631 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002632 if (id->attr == NULL) {
2633 /*
2634 * We are operating on a stream, return a well known reference
2635 * since the attribute node doesn't exist anymore
2636 */
2637 return((xmlAttrPtr) doc);
2638 }
Owen Taylor3473f882001-02-23 17:55:21 +00002639 return(id->attr);
2640}
2641
2642/************************************************************************
2643 * *
2644 * Refs *
2645 * *
2646 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002647typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002648{
2649 xmlListPtr l;
2650 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002651} xmlRemoveMemo;
2652
2653typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2654
2655typedef struct xmlValidateMemo_t
2656{
2657 xmlValidCtxtPtr ctxt;
2658 const xmlChar *name;
2659} xmlValidateMemo;
2660
2661typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002662
2663/**
2664 * xmlCreateRefTable:
2665 *
2666 * create and initialize an empty ref hash table.
2667 *
2668 * Returns the xmlRefTablePtr just created or NULL in case
2669 * of error.
2670 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002671static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002672xmlCreateRefTable(void) {
2673 return(xmlHashCreate(0));
2674}
2675
2676/**
2677 * xmlFreeRef:
2678 * @lk: A list link
2679 *
2680 * Deallocate the memory used by a ref definition
2681 */
2682static void
2683xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002684 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2685 if (ref == NULL) return;
2686 if (ref->value != NULL)
2687 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002688 if (ref->name != NULL)
2689 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002690 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002691}
2692
2693/**
2694 * xmlFreeRefList:
2695 * @list_ref: A list of references.
2696 *
2697 * Deallocate the memory used by a list of references
2698 */
2699static void
2700xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002701 if (list_ref == NULL) return;
2702 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002703}
2704
2705/**
2706 * xmlWalkRemoveRef:
2707 * @data: Contents of current link
2708 * @user: Value supplied by the user
2709 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002710 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002711 */
2712static int
2713xmlWalkRemoveRef(const void *data, const void *user)
2714{
Daniel Veillard37721922001-05-04 15:21:12 +00002715 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2716 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2717 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002718
Daniel Veillard37721922001-05-04 15:21:12 +00002719 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2720 xmlListRemoveFirst(ref_list, (void *)data);
2721 return 0;
2722 }
2723 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002724}
2725
2726/**
Daniel Veillard770075b2004-02-25 10:44:30 +00002727 * xmlDummyCompare
2728 * @data0: Value supplied by the user
2729 * @data1: Value supplied by the user
2730 *
2731 * Do nothing, return 0. Used to create unordered lists.
2732 */
2733static int
Daniel Veillardf54cd532004-02-25 11:52:31 +00002734xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2735 const void *data1 ATTRIBUTE_UNUSED)
Daniel Veillard770075b2004-02-25 10:44:30 +00002736{
2737 return (0);
2738}
2739
2740/**
Owen Taylor3473f882001-02-23 17:55:21 +00002741 * xmlAddRef:
2742 * @ctxt: the validation context
2743 * @doc: pointer to the document
2744 * @value: the value name
2745 * @attr: the attribute holding the Ref
2746 *
2747 * Register a new ref declaration
2748 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002749 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002750 */
2751xmlRefPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002752xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002753 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002754 xmlRefPtr ret;
2755 xmlRefTablePtr table;
2756 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002757
Daniel Veillard37721922001-05-04 15:21:12 +00002758 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002759 return(NULL);
2760 }
2761 if (value == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002762 return(NULL);
2763 }
2764 if (attr == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002765 return(NULL);
2766 }
Owen Taylor3473f882001-02-23 17:55:21 +00002767
Daniel Veillard37721922001-05-04 15:21:12 +00002768 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002769 * Create the Ref table if needed.
2770 */
Daniel Veillard37721922001-05-04 15:21:12 +00002771 table = (xmlRefTablePtr) doc->refs;
2772 if (table == NULL)
2773 doc->refs = table = xmlCreateRefTable();
2774 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002775 xmlVErrMemory(ctxt,
Daniel Veillard37721922001-05-04 15:21:12 +00002776 "xmlAddRef: Table creation failed!\n");
2777 return(NULL);
2778 }
Owen Taylor3473f882001-02-23 17:55:21 +00002779
Daniel Veillard37721922001-05-04 15:21:12 +00002780 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2781 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002782 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard37721922001-05-04 15:21:12 +00002783 return(NULL);
2784 }
Owen Taylor3473f882001-02-23 17:55:21 +00002785
Daniel Veillard37721922001-05-04 15:21:12 +00002786 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002787 * fill the structure.
2788 */
Daniel Veillard37721922001-05-04 15:21:12 +00002789 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002790 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2791 /*
2792 * Operating in streaming mode, attr is gonna disapear
2793 */
2794 ret->name = xmlStrdup(attr->name);
2795 ret->attr = NULL;
2796 } else {
2797 ret->name = NULL;
2798 ret->attr = attr;
2799 }
2800 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002801
Daniel Veillard37721922001-05-04 15:21:12 +00002802 /* To add a reference :-
2803 * References are maintained as a list of references,
2804 * Lookup the entry, if no entry create new nodelist
2805 * Add the owning node to the NodeList
2806 * Return the ref
2807 */
Owen Taylor3473f882001-02-23 17:55:21 +00002808
Daniel Veillard37721922001-05-04 15:21:12 +00002809 if (NULL == (ref_list = xmlHashLookup(table, value))) {
Daniel Veillard770075b2004-02-25 10:44:30 +00002810 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002811 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2812 "xmlAddRef: Reference list creation failed!\n",
2813 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002814 return(NULL);
2815 }
2816 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2817 xmlListDelete(ref_list);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002818 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2819 "xmlAddRef: Reference list insertion failed!\n",
2820 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002821 return(NULL);
2822 }
2823 }
Daniel Veillard965983a2004-02-17 16:30:24 +00002824/* xmlListInsert(ref_list, ret); */
2825 xmlListAppend(ref_list, ret);
Daniel Veillard37721922001-05-04 15:21:12 +00002826 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002827}
2828
2829/**
2830 * xmlFreeRefTable:
2831 * @table: An ref table
2832 *
2833 * Deallocate the memory used by an Ref hash table.
2834 */
2835void
2836xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002837 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002838}
2839
2840/**
2841 * xmlIsRef:
2842 * @doc: the document
2843 * @elem: the element carrying the attribute
2844 * @attr: the attribute
2845 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002846 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002847 * then this is simple, otherwise we use an heuristic: name Ref (upper
2848 * or lowercase).
2849 *
2850 * Returns 0 or 1 depending on the lookup result
2851 */
2852int
2853xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002854 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2855 return(0);
2856 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2857 /* TODO @@@ */
2858 return(0);
2859 } else {
2860 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002861
Daniel Veillard37721922001-05-04 15:21:12 +00002862 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2863 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2864 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2865 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002866
Daniel Veillard37721922001-05-04 15:21:12 +00002867 if ((attrDecl != NULL) &&
2868 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2869 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2870 return(1);
2871 }
2872 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002873}
2874
2875/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002876 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00002877 * @doc: the document
2878 * @attr: the attribute
2879 *
2880 * Remove the given attribute from the Ref table maintained internally.
2881 *
2882 * Returns -1 if the lookup failed and 0 otherwise
2883 */
2884int
2885xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002886 xmlListPtr ref_list;
2887 xmlRefTablePtr table;
2888 xmlChar *ID;
2889 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002890
Daniel Veillard37721922001-05-04 15:21:12 +00002891 if (doc == NULL) return(-1);
2892 if (attr == NULL) return(-1);
2893 table = (xmlRefTablePtr) doc->refs;
2894 if (table == NULL)
2895 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002896
Daniel Veillard37721922001-05-04 15:21:12 +00002897 if (attr == NULL)
2898 return(-1);
2899 ID = xmlNodeListGetString(doc, attr->children, 1);
2900 if (ID == NULL)
2901 return(-1);
2902 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002903
Daniel Veillard37721922001-05-04 15:21:12 +00002904 if(ref_list == NULL) {
2905 xmlFree(ID);
2906 return (-1);
2907 }
2908 /* At this point, ref_list refers to a list of references which
2909 * have the same key as the supplied attr. Our list of references
2910 * is ordered by reference address and we don't have that information
2911 * here to use when removing. We'll have to walk the list and
2912 * check for a matching attribute, when we find one stop the walk
2913 * and remove the entry.
2914 * The list is ordered by reference, so that means we don't have the
2915 * key. Passing the list and the reference to the walker means we
2916 * will have enough data to be able to remove the entry.
2917 */
2918 target.l = ref_list;
2919 target.ap = attr;
2920
2921 /* Remove the supplied attr from our list */
2922 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002923
Daniel Veillard37721922001-05-04 15:21:12 +00002924 /*If the list is empty then remove the list entry in the hash */
2925 if (xmlListEmpty(ref_list))
2926 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2927 xmlFreeRefList);
2928 xmlFree(ID);
2929 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002930}
2931
2932/**
2933 * xmlGetRefs:
2934 * @doc: pointer to the document
2935 * @ID: the ID value
2936 *
2937 * Find the set of references for the supplied ID.
2938 *
2939 * Returns NULL if not found, otherwise node set for the ID.
2940 */
2941xmlListPtr
2942xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002943 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002944
Daniel Veillard37721922001-05-04 15:21:12 +00002945 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002946 return(NULL);
2947 }
Owen Taylor3473f882001-02-23 17:55:21 +00002948
Daniel Veillard37721922001-05-04 15:21:12 +00002949 if (ID == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002950 return(NULL);
2951 }
Owen Taylor3473f882001-02-23 17:55:21 +00002952
Daniel Veillard37721922001-05-04 15:21:12 +00002953 table = (xmlRefTablePtr) doc->refs;
2954 if (table == NULL)
2955 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002956
Daniel Veillard37721922001-05-04 15:21:12 +00002957 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002958}
2959
2960/************************************************************************
2961 * *
2962 * Routines for validity checking *
2963 * *
2964 ************************************************************************/
2965
2966/**
2967 * xmlGetDtdElementDesc:
2968 * @dtd: a pointer to the DtD to search
2969 * @name: the element name
2970 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002971 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002972 *
2973 * returns the xmlElementPtr if found or NULL
2974 */
2975
2976xmlElementPtr
2977xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2978 xmlElementTablePtr table;
2979 xmlElementPtr cur;
2980 xmlChar *uqname = NULL, *prefix = NULL;
2981
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002982 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002983 if (dtd->elements == NULL)
2984 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002985 table = (xmlElementTablePtr) dtd->elements;
2986
2987 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002988 if (uqname != NULL)
2989 name = uqname;
2990 cur = xmlHashLookup2(table, name, prefix);
2991 if (prefix != NULL) xmlFree(prefix);
2992 if (uqname != NULL) xmlFree(uqname);
2993 return(cur);
2994}
2995/**
2996 * xmlGetDtdElementDesc2:
2997 * @dtd: a pointer to the DtD to search
2998 * @name: the element name
2999 * @create: create an empty description if not found
3000 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003001 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00003002 *
3003 * returns the xmlElementPtr if found or NULL
3004 */
3005
Daniel Veillard86fd5a72001-12-13 14:55:21 +00003006static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00003007xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
3008 xmlElementTablePtr table;
3009 xmlElementPtr cur;
3010 xmlChar *uqname = NULL, *prefix = NULL;
3011
3012 if (dtd == NULL) return(NULL);
3013 if (dtd->elements == NULL) {
3014 if (!create)
3015 return(NULL);
3016 /*
3017 * Create the Element table if needed.
3018 */
3019 table = (xmlElementTablePtr) dtd->elements;
3020 if (table == NULL) {
3021 table = xmlCreateElementTable();
3022 dtd->elements = (void *) table;
3023 }
3024 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003025 xmlVErrMemory(NULL, "element table allocation failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003026 return(NULL);
3027 }
3028 }
3029 table = (xmlElementTablePtr) dtd->elements;
3030
3031 uqname = xmlSplitQName2(name, &prefix);
3032 if (uqname != NULL)
3033 name = uqname;
3034 cur = xmlHashLookup2(table, name, prefix);
3035 if ((cur == NULL) && (create)) {
3036 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
3037 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003038 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003039 return(NULL);
3040 }
3041 memset(cur, 0, sizeof(xmlElement));
3042 cur->type = XML_ELEMENT_DECL;
3043
3044 /*
3045 * fill the structure.
3046 */
3047 cur->name = xmlStrdup(name);
3048 cur->prefix = xmlStrdup(prefix);
3049 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3050
3051 xmlHashAddEntry2(table, name, prefix, cur);
3052 }
3053 if (prefix != NULL) xmlFree(prefix);
3054 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00003055 return(cur);
3056}
3057
3058/**
3059 * xmlGetDtdQElementDesc:
3060 * @dtd: a pointer to the DtD to search
3061 * @name: the element name
3062 * @prefix: the element namespace prefix
3063 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003064 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003065 *
3066 * returns the xmlElementPtr if found or NULL
3067 */
3068
Daniel Veillard48da9102001-08-07 01:10:10 +00003069xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00003070xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3071 const xmlChar *prefix) {
3072 xmlElementTablePtr table;
3073
3074 if (dtd == NULL) return(NULL);
3075 if (dtd->elements == NULL) return(NULL);
3076 table = (xmlElementTablePtr) dtd->elements;
3077
3078 return(xmlHashLookup2(table, name, prefix));
3079}
3080
3081/**
3082 * xmlGetDtdAttrDesc:
3083 * @dtd: a pointer to the DtD to search
3084 * @elem: the element name
3085 * @name: the attribute name
3086 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003087 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003088 * this element.
3089 *
3090 * returns the xmlAttributePtr if found or NULL
3091 */
3092
3093xmlAttributePtr
3094xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3095 xmlAttributeTablePtr table;
3096 xmlAttributePtr cur;
3097 xmlChar *uqname = NULL, *prefix = NULL;
3098
3099 if (dtd == NULL) return(NULL);
3100 if (dtd->attributes == NULL) return(NULL);
3101
3102 table = (xmlAttributeTablePtr) dtd->attributes;
3103 if (table == NULL)
3104 return(NULL);
3105
3106 uqname = xmlSplitQName2(name, &prefix);
3107
3108 if (uqname != NULL) {
3109 cur = xmlHashLookup3(table, uqname, prefix, elem);
3110 if (prefix != NULL) xmlFree(prefix);
3111 if (uqname != NULL) xmlFree(uqname);
3112 } else
3113 cur = xmlHashLookup3(table, name, NULL, elem);
3114 return(cur);
3115}
3116
3117/**
3118 * xmlGetDtdQAttrDesc:
3119 * @dtd: a pointer to the DtD to search
3120 * @elem: the element name
3121 * @name: the attribute name
3122 * @prefix: the attribute namespace prefix
3123 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003124 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003125 * this element.
3126 *
3127 * returns the xmlAttributePtr if found or NULL
3128 */
3129
Daniel Veillard48da9102001-08-07 01:10:10 +00003130xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00003131xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3132 const xmlChar *prefix) {
3133 xmlAttributeTablePtr table;
3134
3135 if (dtd == NULL) return(NULL);
3136 if (dtd->attributes == NULL) return(NULL);
3137 table = (xmlAttributeTablePtr) dtd->attributes;
3138
3139 return(xmlHashLookup3(table, name, prefix, elem));
3140}
3141
3142/**
3143 * xmlGetDtdNotationDesc:
3144 * @dtd: a pointer to the DtD to search
3145 * @name: the notation name
3146 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003147 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00003148 *
3149 * returns the xmlNotationPtr if found or NULL
3150 */
3151
3152xmlNotationPtr
3153xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3154 xmlNotationTablePtr table;
3155
3156 if (dtd == NULL) return(NULL);
3157 if (dtd->notations == NULL) return(NULL);
3158 table = (xmlNotationTablePtr) dtd->notations;
3159
3160 return(xmlHashLookup(table, name));
3161}
3162
Daniel Veillardf54cd532004-02-25 11:52:31 +00003163#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003164/**
3165 * xmlValidateNotationUse:
3166 * @ctxt: the validation context
3167 * @doc: the document
3168 * @notationName: the notation name to check
3169 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003170 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00003171 * - [ VC: Notation Declared ]
3172 *
3173 * returns 1 if valid or 0 otherwise
3174 */
3175
3176int
3177xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3178 const xmlChar *notationName) {
3179 xmlNotationPtr notaDecl;
3180 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3181
3182 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3183 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3184 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3185
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003186 if ((notaDecl == NULL) && (ctxt != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003187 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3188 "NOTATION %s is not declared\n",
3189 notationName, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003190 return(0);
3191 }
3192 return(1);
3193}
Daniel Veillardf54cd532004-02-25 11:52:31 +00003194#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003195
3196/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003197 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00003198 * @doc: the document
3199 * @name: the element name
3200 *
3201 * Search in the DtDs whether an element accept Mixed content (or ANY)
3202 * basically if it is supposed to accept text childs
3203 *
3204 * returns 0 if no, 1 if yes, and -1 if no element description is available
3205 */
3206
3207int
3208xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3209 xmlElementPtr elemDecl;
3210
3211 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3212
3213 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3214 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3215 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3216 if (elemDecl == NULL) return(-1);
3217 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00003218 case XML_ELEMENT_TYPE_UNDEFINED:
3219 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003220 case XML_ELEMENT_TYPE_ELEMENT:
3221 return(0);
3222 case XML_ELEMENT_TYPE_EMPTY:
3223 /*
3224 * return 1 for EMPTY since we want VC error to pop up
3225 * on <empty> </empty> for example
3226 */
3227 case XML_ELEMENT_TYPE_ANY:
3228 case XML_ELEMENT_TYPE_MIXED:
3229 return(1);
3230 }
3231 return(1);
3232}
3233
Daniel Veillard4432df22003-09-28 18:58:27 +00003234#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003235/**
3236 * xmlValidateNameValue:
3237 * @value: an Name value
3238 *
3239 * Validate that the given value match Name production
3240 *
3241 * returns 1 if valid or 0 otherwise
3242 */
3243
Daniel Veillard9b731d72002-04-14 12:56:08 +00003244int
Owen Taylor3473f882001-02-23 17:55:21 +00003245xmlValidateNameValue(const xmlChar *value) {
3246 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003247 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003248
3249 if (value == NULL) return(0);
3250 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003251 val = xmlStringCurrentChar(NULL, cur, &len);
3252 cur += len;
3253 if (!IS_LETTER(val) && (val != '_') &&
3254 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003255 return(0);
3256 }
3257
Daniel Veillardd8224e02002-01-13 15:43:22 +00003258 val = xmlStringCurrentChar(NULL, cur, &len);
3259 cur += len;
3260 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3261 (val == '.') || (val == '-') ||
3262 (val == '_') || (val == ':') ||
3263 (IS_COMBINING(val)) ||
3264 (IS_EXTENDER(val))) {
3265 val = xmlStringCurrentChar(NULL, cur, &len);
3266 cur += len;
3267 }
Owen Taylor3473f882001-02-23 17:55:21 +00003268
Daniel Veillardd8224e02002-01-13 15:43:22 +00003269 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003270
3271 return(1);
3272}
3273
3274/**
3275 * xmlValidateNamesValue:
3276 * @value: an Names value
3277 *
3278 * Validate that the given value match Names production
3279 *
3280 * returns 1 if valid or 0 otherwise
3281 */
3282
Daniel Veillard9b731d72002-04-14 12:56:08 +00003283int
Owen Taylor3473f882001-02-23 17:55:21 +00003284xmlValidateNamesValue(const xmlChar *value) {
3285 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003286 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003287
3288 if (value == NULL) return(0);
3289 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003290 val = xmlStringCurrentChar(NULL, cur, &len);
3291 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003292
Daniel Veillardd8224e02002-01-13 15:43:22 +00003293 if (!IS_LETTER(val) && (val != '_') &&
3294 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003295 return(0);
3296 }
3297
Daniel Veillardd8224e02002-01-13 15:43:22 +00003298 val = xmlStringCurrentChar(NULL, cur, &len);
3299 cur += len;
3300 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3301 (val == '.') || (val == '-') ||
3302 (val == '_') || (val == ':') ||
3303 (IS_COMBINING(val)) ||
3304 (IS_EXTENDER(val))) {
3305 val = xmlStringCurrentChar(NULL, cur, &len);
3306 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003307 }
3308
Daniel Veillardd8224e02002-01-13 15:43:22 +00003309 while (IS_BLANK(val)) {
3310 while (IS_BLANK(val)) {
3311 val = xmlStringCurrentChar(NULL, cur, &len);
3312 cur += len;
3313 }
3314
3315 if (!IS_LETTER(val) && (val != '_') &&
3316 (val != ':')) {
3317 return(0);
3318 }
3319 val = xmlStringCurrentChar(NULL, cur, &len);
3320 cur += len;
3321
3322 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3323 (val == '.') || (val == '-') ||
3324 (val == '_') || (val == ':') ||
3325 (IS_COMBINING(val)) ||
3326 (IS_EXTENDER(val))) {
3327 val = xmlStringCurrentChar(NULL, cur, &len);
3328 cur += len;
3329 }
3330 }
3331
3332 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003333
3334 return(1);
3335}
3336
3337/**
3338 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003339 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003340 *
3341 * Validate that the given value match Nmtoken production
3342 *
3343 * [ VC: Name Token ]
3344 *
3345 * returns 1 if valid or 0 otherwise
3346 */
3347
Daniel Veillard9b731d72002-04-14 12:56:08 +00003348int
Owen Taylor3473f882001-02-23 17:55:21 +00003349xmlValidateNmtokenValue(const xmlChar *value) {
3350 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003351 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003352
3353 if (value == NULL) return(0);
3354 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003355 val = xmlStringCurrentChar(NULL, cur, &len);
3356 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003357
Daniel Veillardd8224e02002-01-13 15:43:22 +00003358 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3359 (val != '.') && (val != '-') &&
3360 (val != '_') && (val != ':') &&
3361 (!IS_COMBINING(val)) &&
3362 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003363 return(0);
3364
Daniel Veillardd8224e02002-01-13 15:43:22 +00003365 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3366 (val == '.') || (val == '-') ||
3367 (val == '_') || (val == ':') ||
3368 (IS_COMBINING(val)) ||
3369 (IS_EXTENDER(val))) {
3370 val = xmlStringCurrentChar(NULL, cur, &len);
3371 cur += len;
3372 }
Owen Taylor3473f882001-02-23 17:55:21 +00003373
Daniel Veillardd8224e02002-01-13 15:43:22 +00003374 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003375
3376 return(1);
3377}
3378
3379/**
3380 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003381 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003382 *
3383 * Validate that the given value match Nmtokens production
3384 *
3385 * [ VC: Name Token ]
3386 *
3387 * returns 1 if valid or 0 otherwise
3388 */
3389
Daniel Veillard9b731d72002-04-14 12:56:08 +00003390int
Owen Taylor3473f882001-02-23 17:55:21 +00003391xmlValidateNmtokensValue(const xmlChar *value) {
3392 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003393 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003394
3395 if (value == NULL) return(0);
3396 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003397 val = xmlStringCurrentChar(NULL, cur, &len);
3398 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003399
Daniel Veillardd8224e02002-01-13 15:43:22 +00003400 while (IS_BLANK(val)) {
3401 val = xmlStringCurrentChar(NULL, cur, &len);
3402 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003403 }
3404
Daniel Veillardd8224e02002-01-13 15:43:22 +00003405 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3406 (val != '.') && (val != '-') &&
3407 (val != '_') && (val != ':') &&
3408 (!IS_COMBINING(val)) &&
3409 (!IS_EXTENDER(val)))
3410 return(0);
3411
3412 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3413 (val == '.') || (val == '-') ||
3414 (val == '_') || (val == ':') ||
3415 (IS_COMBINING(val)) ||
3416 (IS_EXTENDER(val))) {
3417 val = xmlStringCurrentChar(NULL, cur, &len);
3418 cur += len;
3419 }
3420
3421 while (IS_BLANK(val)) {
3422 while (IS_BLANK(val)) {
3423 val = xmlStringCurrentChar(NULL, cur, &len);
3424 cur += len;
3425 }
3426 if (val == 0) return(1);
3427
3428 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3429 (val != '.') && (val != '-') &&
3430 (val != '_') && (val != ':') &&
3431 (!IS_COMBINING(val)) &&
3432 (!IS_EXTENDER(val)))
3433 return(0);
3434
3435 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3436 (val == '.') || (val == '-') ||
3437 (val == '_') || (val == ':') ||
3438 (IS_COMBINING(val)) ||
3439 (IS_EXTENDER(val))) {
3440 val = xmlStringCurrentChar(NULL, cur, &len);
3441 cur += len;
3442 }
3443 }
3444
3445 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003446
3447 return(1);
3448}
3449
3450/**
3451 * xmlValidateNotationDecl:
3452 * @ctxt: the validation context
3453 * @doc: a document instance
3454 * @nota: a notation definition
3455 *
3456 * Try to validate a single notation definition
3457 * basically it does the following checks as described by the
3458 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003459 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003460 * But this function get called anyway ...
3461 *
3462 * returns 1 if valid or 0 otherwise
3463 */
3464
3465int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003466xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3467 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003468 int ret = 1;
3469
3470 return(ret);
3471}
3472
3473/**
3474 * xmlValidateAttributeValue:
3475 * @type: an attribute type
3476 * @value: an attribute value
3477 *
3478 * Validate that the given attribute value match the proper production
3479 *
3480 * [ VC: ID ]
3481 * Values of type ID must match the Name production....
3482 *
3483 * [ VC: IDREF ]
3484 * Values of type IDREF must match the Name production, and values
3485 * of type IDREFS must match Names ...
3486 *
3487 * [ VC: Entity Name ]
3488 * Values of type ENTITY must match the Name production, values
3489 * of type ENTITIES must match Names ...
3490 *
3491 * [ VC: Name Token ]
3492 * Values of type NMTOKEN must match the Nmtoken production; values
3493 * of type NMTOKENS must match Nmtokens.
3494 *
3495 * returns 1 if valid or 0 otherwise
3496 */
3497
3498int
3499xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3500 switch (type) {
3501 case XML_ATTRIBUTE_ENTITIES:
3502 case XML_ATTRIBUTE_IDREFS:
3503 return(xmlValidateNamesValue(value));
3504 case XML_ATTRIBUTE_ENTITY:
3505 case XML_ATTRIBUTE_IDREF:
3506 case XML_ATTRIBUTE_ID:
3507 case XML_ATTRIBUTE_NOTATION:
3508 return(xmlValidateNameValue(value));
3509 case XML_ATTRIBUTE_NMTOKENS:
3510 case XML_ATTRIBUTE_ENUMERATION:
3511 return(xmlValidateNmtokensValue(value));
3512 case XML_ATTRIBUTE_NMTOKEN:
3513 return(xmlValidateNmtokenValue(value));
3514 case XML_ATTRIBUTE_CDATA:
3515 break;
3516 }
3517 return(1);
3518}
3519
3520/**
3521 * xmlValidateAttributeValue2:
3522 * @ctxt: the validation context
3523 * @doc: the document
3524 * @name: the attribute name (used for error reporting only)
3525 * @type: the attribute type
3526 * @value: the attribute value
3527 *
3528 * Validate that the given attribute value match a given type.
3529 * This typically cannot be done before having finished parsing
3530 * the subsets.
3531 *
3532 * [ VC: IDREF ]
3533 * Values of type IDREF must match one of the declared IDs
3534 * Values of type IDREFS must match a sequence of the declared IDs
3535 * each Name must match the value of an ID attribute on some element
3536 * in the XML document; i.e. IDREF values must match the value of
3537 * some ID attribute
3538 *
3539 * [ VC: Entity Name ]
3540 * Values of type ENTITY must match one declared entity
3541 * Values of type ENTITIES must match a sequence of declared entities
3542 *
3543 * [ VC: Notation Attributes ]
3544 * all notation names in the declaration must be declared.
3545 *
3546 * returns 1 if valid or 0 otherwise
3547 */
3548
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003549static int
Owen Taylor3473f882001-02-23 17:55:21 +00003550xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3551 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3552 int ret = 1;
3553 switch (type) {
3554 case XML_ATTRIBUTE_IDREFS:
3555 case XML_ATTRIBUTE_IDREF:
3556 case XML_ATTRIBUTE_ID:
3557 case XML_ATTRIBUTE_NMTOKENS:
3558 case XML_ATTRIBUTE_ENUMERATION:
3559 case XML_ATTRIBUTE_NMTOKEN:
3560 case XML_ATTRIBUTE_CDATA:
3561 break;
3562 case XML_ATTRIBUTE_ENTITY: {
3563 xmlEntityPtr ent;
3564
3565 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003566 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003567 if ((ent == NULL) && (doc->standalone == 1)) {
3568 doc->standalone = 0;
3569 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003570 }
Owen Taylor3473f882001-02-23 17:55:21 +00003571 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003572 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3573 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003574 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003575 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003576 ret = 0;
3577 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003578 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3579 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003580 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003581 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003582 ret = 0;
3583 }
3584 break;
3585 }
3586 case XML_ATTRIBUTE_ENTITIES: {
3587 xmlChar *dup, *nam = NULL, *cur, save;
3588 xmlEntityPtr ent;
3589
3590 dup = xmlStrdup(value);
3591 if (dup == NULL)
3592 return(0);
3593 cur = dup;
3594 while (*cur != 0) {
3595 nam = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00003596 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003597 save = *cur;
3598 *cur = 0;
3599 ent = xmlGetDocEntity(doc, nam);
3600 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003601 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3602 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003603 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003604 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003605 ret = 0;
3606 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003607 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3608 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003609 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003610 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003611 ret = 0;
3612 }
3613 if (save == 0)
3614 break;
3615 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00003616 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003617 }
3618 xmlFree(dup);
3619 break;
3620 }
3621 case XML_ATTRIBUTE_NOTATION: {
3622 xmlNotationPtr nota;
3623
3624 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3625 if ((nota == NULL) && (doc->extSubset != NULL))
3626 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3627
3628 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003629 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3630 XML_DTD_UNKNOWN_NOTATION,
Owen Taylor3473f882001-02-23 17:55:21 +00003631 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003632 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003633 ret = 0;
3634 }
3635 break;
3636 }
3637 }
3638 return(ret);
3639}
3640
3641/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003642 * xmlValidCtxtNormalizeAttributeValue:
3643 * @ctxt: the validation context
3644 * @doc: the document
3645 * @elem: the parent
3646 * @name: the attribute name
3647 * @value: the attribute value
3648 * @ctxt: the validation context or NULL
3649 *
3650 * Does the validation related extra step of the normalization of attribute
3651 * values:
3652 *
3653 * If the declared value is not CDATA, then the XML processor must further
3654 * process the normalized attribute value by discarding any leading and
3655 * trailing space (#x20) characters, and by replacing sequences of space
3656 * (#x20) characters by single space (#x20) character.
3657 *
3658 * Also check VC: Standalone Document Declaration in P32, and update
3659 * ctxt->valid accordingly
3660 *
3661 * returns a new normalized string if normalization is needed, NULL otherwise
3662 * the caller must free the returned value.
3663 */
3664
3665xmlChar *
3666xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3667 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3668 xmlChar *ret, *dst;
3669 const xmlChar *src;
3670 xmlAttributePtr attrDecl = NULL;
3671 int extsubset = 0;
3672
3673 if (doc == NULL) return(NULL);
3674 if (elem == NULL) return(NULL);
3675 if (name == NULL) return(NULL);
3676 if (value == NULL) return(NULL);
3677
3678 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003679 xmlChar fn[50];
3680 xmlChar *fullname;
3681
3682 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3683 if (fullname == NULL)
3684 return(0);
3685 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003686 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003687 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003688 if (attrDecl != NULL)
3689 extsubset = 1;
3690 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003691 if ((fullname != fn) && (fullname != elem->name))
3692 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003693 }
3694 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3695 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3696 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3697 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3698 if (attrDecl != NULL)
3699 extsubset = 1;
3700 }
3701
3702 if (attrDecl == NULL)
3703 return(NULL);
3704 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3705 return(NULL);
3706
3707 ret = xmlStrdup(value);
3708 if (ret == NULL)
3709 return(NULL);
3710 src = value;
3711 dst = ret;
3712 while (*src == 0x20) src++;
3713 while (*src != 0) {
3714 if (*src == 0x20) {
3715 while (*src == 0x20) src++;
3716 if (*src != 0)
3717 *dst++ = 0x20;
3718 } else {
3719 *dst++ = *src++;
3720 }
3721 }
3722 *dst = 0;
3723 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003724 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003725"standalone: %s on %s value had to be normalized based on external subset declaration\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003726 name, elem->name, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003727 ctxt->valid = 0;
3728 }
3729 return(ret);
3730}
3731
3732/**
Owen Taylor3473f882001-02-23 17:55:21 +00003733 * xmlValidNormalizeAttributeValue:
3734 * @doc: the document
3735 * @elem: the parent
3736 * @name: the attribute name
3737 * @value: the attribute value
3738 *
3739 * Does the validation related extra step of the normalization of attribute
3740 * values:
3741 *
3742 * If the declared value is not CDATA, then the XML processor must further
3743 * process the normalized attribute value by discarding any leading and
3744 * trailing space (#x20) characters, and by replacing sequences of space
3745 * (#x20) characters by single space (#x20) character.
3746 *
Daniel Veillard652327a2003-09-29 18:02:38 +00003747 * Returns a new normalized string if normalization is needed, NULL otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00003748 * the caller must free the returned value.
3749 */
3750
3751xmlChar *
3752xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3753 const xmlChar *name, const xmlChar *value) {
3754 xmlChar *ret, *dst;
3755 const xmlChar *src;
3756 xmlAttributePtr attrDecl = NULL;
3757
3758 if (doc == NULL) return(NULL);
3759 if (elem == NULL) return(NULL);
3760 if (name == NULL) return(NULL);
3761 if (value == NULL) return(NULL);
3762
3763 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003764 xmlChar fn[50];
3765 xmlChar *fullname;
3766
3767 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3768 if (fullname == NULL)
3769 return(0);
3770 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003771 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003772 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3773 if ((fullname != fn) && (fullname != elem->name))
3774 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003775 }
3776 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3777 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3778 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3779
3780 if (attrDecl == NULL)
3781 return(NULL);
3782 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3783 return(NULL);
3784
3785 ret = xmlStrdup(value);
3786 if (ret == NULL)
3787 return(NULL);
3788 src = value;
3789 dst = ret;
3790 while (*src == 0x20) src++;
3791 while (*src != 0) {
3792 if (*src == 0x20) {
3793 while (*src == 0x20) src++;
3794 if (*src != 0)
3795 *dst++ = 0x20;
3796 } else {
3797 *dst++ = *src++;
3798 }
3799 }
3800 *dst = 0;
3801 return(ret);
3802}
3803
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003804static void
Owen Taylor3473f882001-02-23 17:55:21 +00003805xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003806 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003807 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3808}
3809
3810/**
3811 * xmlValidateAttributeDecl:
3812 * @ctxt: the validation context
3813 * @doc: a document instance
3814 * @attr: an attribute definition
3815 *
3816 * Try to validate a single attribute definition
3817 * basically it does the following checks as described by the
3818 * XML-1.0 recommendation:
3819 * - [ VC: Attribute Default Legal ]
3820 * - [ VC: Enumeration ]
3821 * - [ VC: ID Attribute Default ]
3822 *
3823 * The ID/IDREF uniqueness and matching are done separately
3824 *
3825 * returns 1 if valid or 0 otherwise
3826 */
3827
3828int
3829xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3830 xmlAttributePtr attr) {
3831 int ret = 1;
3832 int val;
3833 CHECK_DTD;
3834 if(attr == NULL) return(1);
3835
3836 /* Attribute Default Legal */
3837 /* Enumeration */
3838 if (attr->defaultValue != NULL) {
3839 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3840 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003841 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003842 "Syntax of default value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003843 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003844 }
3845 ret &= val;
3846 }
3847
3848 /* ID Attribute Default */
3849 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3850 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3851 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003852 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003853 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003854 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003855 ret = 0;
3856 }
3857
3858 /* One ID per Element Type */
3859 if (attr->atype == XML_ATTRIBUTE_ID) {
3860 int nbId;
3861
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003862 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003863 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3864 attr->elem);
3865 if (elem != NULL) {
3866 nbId = xmlScanIDAttributeDecl(NULL, elem);
3867 } else {
3868 xmlAttributeTablePtr table;
3869
3870 /*
3871 * The attribute may be declared in the internal subset and the
3872 * element in the external subset.
3873 */
3874 nbId = 0;
3875 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3876 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3877 xmlValidateAttributeIdCallback, &nbId);
3878 }
3879 if (nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003880
3881 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00003882 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3883 attr->elem, nbId, attr->name);
3884 } else if (doc->extSubset != NULL) {
3885 int extId = 0;
3886 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3887 if (elem != NULL) {
3888 extId = xmlScanIDAttributeDecl(NULL, elem);
3889 }
3890 if (extId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003891 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00003892 "Element %s has %d ID attribute defined in the external subset : %s\n",
3893 attr->elem, extId, attr->name);
3894 } else if (extId + nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003895 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00003896"Element %s has ID attributes defined in the internal and external subset : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003897 attr->elem, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003898 }
3899 }
3900 }
3901
3902 /* Validity Constraint: Enumeration */
3903 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3904 xmlEnumerationPtr tree = attr->tree;
3905 while (tree != NULL) {
3906 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3907 tree = tree->next;
3908 }
3909 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003910 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003911"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003912 attr->defaultValue, attr->name, attr->elem);
3913 ret = 0;
3914 }
3915 }
3916
3917 return(ret);
3918}
3919
3920/**
3921 * xmlValidateElementDecl:
3922 * @ctxt: the validation context
3923 * @doc: a document instance
3924 * @elem: an element definition
3925 *
3926 * Try to validate a single element definition
3927 * basically it does the following checks as described by the
3928 * XML-1.0 recommendation:
3929 * - [ VC: One ID per Element Type ]
3930 * - [ VC: No Duplicate Types ]
3931 * - [ VC: Unique Element Type Declaration ]
3932 *
3933 * returns 1 if valid or 0 otherwise
3934 */
3935
3936int
3937xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3938 xmlElementPtr elem) {
3939 int ret = 1;
3940 xmlElementPtr tst;
3941
3942 CHECK_DTD;
3943
3944 if (elem == NULL) return(1);
3945
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003946#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00003947#ifdef LIBXML_REGEXP_ENABLED
3948 /* Build the regexp associated to the content model */
3949 ret = xmlValidBuildContentModel(ctxt, elem);
3950#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003951#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00003952
Owen Taylor3473f882001-02-23 17:55:21 +00003953 /* No Duplicate Types */
3954 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3955 xmlElementContentPtr cur, next;
3956 const xmlChar *name;
3957
3958 cur = elem->content;
3959 while (cur != NULL) {
3960 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3961 if (cur->c1 == NULL) break;
3962 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3963 name = cur->c1->name;
3964 next = cur->c2;
3965 while (next != NULL) {
3966 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00003967 if ((xmlStrEqual(next->name, name)) &&
3968 (xmlStrEqual(next->prefix, cur->prefix))) {
3969 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003970 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00003971 "Definition of %s has duplicate references of %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003972 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00003973 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003974 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00003975 "Definition of %s has duplicate references of %s:%s\n",
3976 elem->name, cur->prefix, name);
3977 }
Owen Taylor3473f882001-02-23 17:55:21 +00003978 ret = 0;
3979 }
3980 break;
3981 }
3982 if (next->c1 == NULL) break;
3983 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00003984 if ((xmlStrEqual(next->c1->name, name)) &&
3985 (xmlStrEqual(next->c1->prefix, cur->prefix))) {
3986 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003987 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00003988 "Definition of %s has duplicate references to %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003989 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00003990 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003991 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00003992 "Definition of %s has duplicate references to %s:%s\n",
3993 elem->name, cur->prefix, name);
3994 }
Owen Taylor3473f882001-02-23 17:55:21 +00003995 ret = 0;
3996 }
3997 next = next->c2;
3998 }
3999 }
4000 cur = cur->c2;
4001 }
4002 }
4003
4004 /* VC: Unique Element Type Declaration */
4005 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004006 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004007 ((tst->prefix == elem->prefix) ||
4008 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004009 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004010 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4011 "Redefinition of element %s\n",
4012 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004013 ret = 0;
4014 }
4015 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004016 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004017 ((tst->prefix == elem->prefix) ||
4018 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004019 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004020 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4021 "Redefinition of element %s\n",
4022 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004023 ret = 0;
4024 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00004025 /* One ID per Element Type
4026 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00004027 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4028 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00004029 } */
Owen Taylor3473f882001-02-23 17:55:21 +00004030 return(ret);
4031}
4032
4033/**
4034 * xmlValidateOneAttribute:
4035 * @ctxt: the validation context
4036 * @doc: a document instance
4037 * @elem: an element instance
4038 * @attr: an attribute instance
4039 * @value: the attribute value (without entities processing)
4040 *
4041 * Try to validate a single attribute for an element
4042 * basically it does the following checks as described by the
4043 * XML-1.0 recommendation:
4044 * - [ VC: Attribute Value Type ]
4045 * - [ VC: Fixed Attribute Default ]
4046 * - [ VC: Entity Name ]
4047 * - [ VC: Name Token ]
4048 * - [ VC: ID ]
4049 * - [ VC: IDREF ]
4050 * - [ VC: Entity Name ]
4051 * - [ VC: Notation Attributes ]
4052 *
4053 * The ID/IDREF uniqueness and matching are done separately
4054 *
4055 * returns 1 if valid or 0 otherwise
4056 */
4057
4058int
4059xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillard07cb8222003-09-10 10:51:05 +00004060 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4061{
Owen Taylor3473f882001-02-23 17:55:21 +00004062 xmlAttributePtr attrDecl = NULL;
4063 int val;
4064 int ret = 1;
4065
4066 CHECK_DTD;
4067 if ((elem == NULL) || (elem->name == NULL)) return(0);
4068 if ((attr == NULL) || (attr->name == NULL)) return(0);
4069
4070 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004071 xmlChar fn[50];
4072 xmlChar *fullname;
4073
4074 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4075 if (fullname == NULL)
4076 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004077 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004078 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004079 attr->name, attr->ns->prefix);
4080 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004081 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004082 attr->name, attr->ns->prefix);
4083 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004084 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004085 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4086 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00004087 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004088 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004089 if ((fullname != fn) && (fullname != elem->name))
4090 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00004091 }
4092 if (attrDecl == NULL) {
4093 if (attr->ns != NULL) {
4094 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4095 attr->name, attr->ns->prefix);
4096 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4097 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4098 attr->name, attr->ns->prefix);
4099 } else {
4100 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4101 elem->name, attr->name);
4102 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4103 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4104 elem->name, attr->name);
4105 }
4106 }
4107
4108
4109 /* Validity Constraint: Attribute Value Type */
4110 if (attrDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004111 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004112 "No declaration for attribute %s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004113 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004114 return(0);
4115 }
4116 attr->atype = attrDecl->atype;
4117
4118 val = xmlValidateAttributeValue(attrDecl->atype, value);
4119 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004120 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004121 "Syntax of value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004122 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004123 ret = 0;
4124 }
4125
4126 /* Validity constraint: Fixed Attribute Default */
4127 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4128 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004129 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004130 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004131 attr->name, elem->name, attrDecl->defaultValue);
4132 ret = 0;
4133 }
4134 }
4135
4136 /* Validity Constraint: ID uniqueness */
4137 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4138 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4139 ret = 0;
4140 }
4141
4142 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4143 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4144 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4145 ret = 0;
4146 }
4147
4148 /* Validity Constraint: Notation Attributes */
4149 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4150 xmlEnumerationPtr tree = attrDecl->tree;
4151 xmlNotationPtr nota;
4152
4153 /* First check that the given NOTATION was declared */
4154 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4155 if (nota == NULL)
4156 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4157
4158 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004159 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004160 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004161 value, attr->name, elem->name);
4162 ret = 0;
4163 }
4164
4165 /* Second, verify that it's among the list */
4166 while (tree != NULL) {
4167 if (xmlStrEqual(tree->name, value)) break;
4168 tree = tree->next;
4169 }
4170 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004171 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004172"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004173 value, attr->name, elem->name);
4174 ret = 0;
4175 }
4176 }
4177
4178 /* Validity Constraint: Enumeration */
4179 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4180 xmlEnumerationPtr tree = attrDecl->tree;
4181 while (tree != NULL) {
4182 if (xmlStrEqual(tree->name, value)) break;
4183 tree = tree->next;
4184 }
4185 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004186 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004187 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004188 value, attr->name, elem->name);
4189 ret = 0;
4190 }
4191 }
4192
4193 /* Fixed Attribute Default */
4194 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4195 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004196 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004197 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004198 attr->name, elem->name, attrDecl->defaultValue);
4199 ret = 0;
4200 }
4201
4202 /* Extra check for the attribute value */
4203 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4204 attrDecl->atype, value);
4205
4206 return(ret);
4207}
4208
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004209/**
4210 * xmlValidateOneNamespace:
4211 * @ctxt: the validation context
4212 * @doc: a document instance
4213 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004214 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004215 * @ns: an namespace declaration instance
4216 * @value: the attribute value (without entities processing)
4217 *
4218 * Try to validate a single namespace declaration for an element
4219 * basically it does the following checks as described by the
4220 * XML-1.0 recommendation:
4221 * - [ VC: Attribute Value Type ]
4222 * - [ VC: Fixed Attribute Default ]
4223 * - [ VC: Entity Name ]
4224 * - [ VC: Name Token ]
4225 * - [ VC: ID ]
4226 * - [ VC: IDREF ]
4227 * - [ VC: Entity Name ]
4228 * - [ VC: Notation Attributes ]
4229 *
4230 * The ID/IDREF uniqueness and matching are done separately
4231 *
4232 * returns 1 if valid or 0 otherwise
4233 */
4234
4235int
4236xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4237xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4238 /* xmlElementPtr elemDecl; */
4239 xmlAttributePtr attrDecl = NULL;
4240 int val;
4241 int ret = 1;
4242
4243 CHECK_DTD;
4244 if ((elem == NULL) || (elem->name == NULL)) return(0);
4245 if ((ns == NULL) || (ns->href == NULL)) return(0);
4246
4247 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004248 xmlChar fn[50];
4249 xmlChar *fullname;
4250
4251 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4252 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004253 xmlVErrMemory(ctxt, "Validating namespace");
Daniel Veillardc00cda82003-04-07 10:22:39 +00004254 return(0);
4255 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004256 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004257 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004258 ns->prefix, BAD_CAST "xmlns");
4259 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004260 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004261 ns->prefix, BAD_CAST "xmlns");
4262 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004263 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004264 BAD_CAST "xmlns");
4265 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004266 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004267 BAD_CAST "xmlns");
4268 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004269 if ((fullname != fn) && (fullname != elem->name))
4270 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004271 }
4272 if (attrDecl == NULL) {
4273 if (ns->prefix != NULL) {
4274 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4275 ns->prefix, BAD_CAST "xmlns");
4276 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4277 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4278 ns->prefix, BAD_CAST "xmlns");
4279 } else {
4280 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4281 elem->name, BAD_CAST "xmlns");
4282 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4283 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4284 elem->name, BAD_CAST "xmlns");
4285 }
4286 }
4287
4288
4289 /* Validity Constraint: Attribute Value Type */
4290 if (attrDecl == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004291 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004292 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004293 "No declaration for attribute xmlns:%s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004294 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004295 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004296 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004297 "No declaration for attribute xmlns of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004298 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004299 }
4300 return(0);
4301 }
4302
4303 val = xmlValidateAttributeValue(attrDecl->atype, value);
4304 if (val == 0) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004305 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004306 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004307 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004308 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004309 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004310 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004311 "Syntax of value for attribute xmlns of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004312 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004313 }
4314 ret = 0;
4315 }
4316
4317 /* Validity constraint: Fixed Attribute Default */
4318 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4319 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004320 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004321 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004322 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4323 ns->prefix, elem->name, attrDecl->defaultValue);
4324 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004325 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004326 "Value for attribute xmlns of %s is different from default \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004327 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004328 }
4329 ret = 0;
4330 }
4331 }
4332
4333 /* Validity Constraint: ID uniqueness */
4334 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4335 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4336 ret = 0;
4337 }
4338
4339 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4340 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4341 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4342 ret = 0;
4343 }
4344
4345 /* Validity Constraint: Notation Attributes */
4346 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4347 xmlEnumerationPtr tree = attrDecl->tree;
4348 xmlNotationPtr nota;
4349
4350 /* First check that the given NOTATION was declared */
4351 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4352 if (nota == NULL)
4353 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4354
4355 if (nota == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004356 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004357 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004358 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4359 value, ns->prefix, elem->name);
4360 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004361 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004362 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004363 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004364 }
4365 ret = 0;
4366 }
4367
4368 /* Second, verify that it's among the list */
4369 while (tree != NULL) {
4370 if (xmlStrEqual(tree->name, value)) break;
4371 tree = tree->next;
4372 }
4373 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004374 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004375 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004376"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4377 value, ns->prefix, elem->name);
4378 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004379 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004380"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004381 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004382 }
4383 ret = 0;
4384 }
4385 }
4386
4387 /* Validity Constraint: Enumeration */
4388 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4389 xmlEnumerationPtr tree = attrDecl->tree;
4390 while (tree != NULL) {
4391 if (xmlStrEqual(tree->name, value)) break;
4392 tree = tree->next;
4393 }
4394 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004395 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004396 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004397"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4398 value, ns->prefix, elem->name);
4399 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004400 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004401"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004402 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004403 }
4404 ret = 0;
4405 }
4406 }
4407
4408 /* Fixed Attribute Default */
4409 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4410 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004411 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004412 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004413 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4414 ns->prefix, elem->name, attrDecl->defaultValue);
4415 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004416 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004417 "Value for attribute xmlns of %s must be \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004418 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004419 }
4420 ret = 0;
4421 }
4422
4423 /* Extra check for the attribute value */
4424 if (ns->prefix != NULL) {
4425 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4426 attrDecl->atype, value);
4427 } else {
4428 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4429 attrDecl->atype, value);
4430 }
4431
4432 return(ret);
4433}
4434
Daniel Veillard118aed72002-09-24 14:13:13 +00004435#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004436/**
4437 * xmlValidateSkipIgnorable:
4438 * @ctxt: the validation context
4439 * @child: the child list
4440 *
4441 * Skip ignorable elements w.r.t. the validation process
4442 *
4443 * returns the first element to consider for validation of the content model
4444 */
4445
4446static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004447xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004448 while (child != NULL) {
4449 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004450 /* These things are ignored (skipped) during validation. */
4451 case XML_PI_NODE:
4452 case XML_COMMENT_NODE:
4453 case XML_XINCLUDE_START:
4454 case XML_XINCLUDE_END:
4455 child = child->next;
4456 break;
4457 case XML_TEXT_NODE:
4458 if (xmlIsBlankNode(child))
4459 child = child->next;
4460 else
4461 return(child);
4462 break;
4463 /* keep current node */
4464 default:
4465 return(child);
4466 }
4467 }
4468 return(child);
4469}
4470
4471/**
4472 * xmlValidateElementType:
4473 * @ctxt: the validation context
4474 *
4475 * Try to validate the content model of an element internal function
4476 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004477 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4478 * reference is found and -3 if the validation succeeded but
4479 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004480 */
4481
4482static int
4483xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004484 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004485 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004486
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004487 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004488 if ((NODE == NULL) && (CONT == NULL))
4489 return(1);
4490 if ((NODE == NULL) &&
4491 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4492 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4493 return(1);
4494 }
4495 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004496 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004497 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004498
4499 /*
4500 * We arrive here when more states need to be examined
4501 */
4502cont:
4503
4504 /*
4505 * We just recovered from a rollback generated by a possible
4506 * epsilon transition, go directly to the analysis phase
4507 */
4508 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004509 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004510 DEBUG_VALID_STATE(NODE, CONT)
4511 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004512 goto analyze;
4513 }
4514
4515 DEBUG_VALID_STATE(NODE, CONT)
4516 /*
4517 * we may have to save a backup state here. This is the equivalent
4518 * of handling epsilon transition in NFAs.
4519 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004520 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004521 ((CONT->parent == NULL) ||
4522 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004523 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004524 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004525 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004526 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004527 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4528 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004529 }
4530
4531
4532 /*
4533 * Check first if the content matches
4534 */
4535 switch (CONT->type) {
4536 case XML_ELEMENT_CONTENT_PCDATA:
4537 if (NODE == NULL) {
4538 DEBUG_VALID_MSG("pcdata failed no node");
4539 ret = 0;
4540 break;
4541 }
4542 if (NODE->type == XML_TEXT_NODE) {
4543 DEBUG_VALID_MSG("pcdata found, skip to next");
4544 /*
4545 * go to next element in the content model
4546 * skipping ignorable elems
4547 */
4548 do {
4549 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004550 NODE = xmlValidateSkipIgnorable(NODE);
4551 if ((NODE != NULL) &&
4552 (NODE->type == XML_ENTITY_REF_NODE))
4553 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004554 } while ((NODE != NULL) &&
4555 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004556 (NODE->type != XML_TEXT_NODE) &&
4557 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004558 ret = 1;
4559 break;
4560 } else {
4561 DEBUG_VALID_MSG("pcdata failed");
4562 ret = 0;
4563 break;
4564 }
4565 break;
4566 case XML_ELEMENT_CONTENT_ELEMENT:
4567 if (NODE == NULL) {
4568 DEBUG_VALID_MSG("element failed no node");
4569 ret = 0;
4570 break;
4571 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004572 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4573 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004574 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004575 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4576 ret = (CONT->prefix == NULL);
4577 } else if (CONT->prefix == NULL) {
4578 ret = 0;
4579 } else {
4580 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4581 }
4582 }
4583 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004584 DEBUG_VALID_MSG("element found, skip to next");
4585 /*
4586 * go to next element in the content model
4587 * skipping ignorable elems
4588 */
4589 do {
4590 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004591 NODE = xmlValidateSkipIgnorable(NODE);
4592 if ((NODE != NULL) &&
4593 (NODE->type == XML_ENTITY_REF_NODE))
4594 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004595 } while ((NODE != NULL) &&
4596 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004597 (NODE->type != XML_TEXT_NODE) &&
4598 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004599 } else {
4600 DEBUG_VALID_MSG("element failed");
4601 ret = 0;
4602 break;
4603 }
4604 break;
4605 case XML_ELEMENT_CONTENT_OR:
4606 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004607 * Small optimization.
4608 */
4609 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4610 if ((NODE == NULL) ||
4611 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4612 DEPTH++;
4613 CONT = CONT->c2;
4614 goto cont;
4615 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004616 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4617 ret = (CONT->c1->prefix == NULL);
4618 } else if (CONT->c1->prefix == NULL) {
4619 ret = 0;
4620 } else {
4621 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4622 }
4623 if (ret == 0) {
4624 DEPTH++;
4625 CONT = CONT->c2;
4626 goto cont;
4627 }
Daniel Veillard85349052001-04-20 13:48:21 +00004628 }
4629
4630 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004631 * save the second branch 'or' branch
4632 */
4633 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004634 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4635 OCCURS, ROLLBACK_OR) < 0)
4636 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004637 DEPTH++;
4638 CONT = CONT->c1;
4639 goto cont;
4640 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004641 /*
4642 * Small optimization.
4643 */
4644 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4645 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4646 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4647 if ((NODE == NULL) ||
4648 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4649 DEPTH++;
4650 CONT = CONT->c2;
4651 goto cont;
4652 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004653 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4654 ret = (CONT->c1->prefix == NULL);
4655 } else if (CONT->c1->prefix == NULL) {
4656 ret = 0;
4657 } else {
4658 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4659 }
4660 if (ret == 0) {
4661 DEPTH++;
4662 CONT = CONT->c2;
4663 goto cont;
4664 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004665 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004666 DEPTH++;
4667 CONT = CONT->c1;
4668 goto cont;
4669 }
4670
4671 /*
4672 * At this point handle going up in the tree
4673 */
4674 if (ret == -1) {
4675 DEBUG_VALID_MSG("error found returning");
4676 return(ret);
4677 }
4678analyze:
4679 while (CONT != NULL) {
4680 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004681 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004682 * this level.
4683 */
4684 if (ret == 0) {
4685 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004686 xmlNodePtr cur;
4687
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004688 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004689 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004690 DEBUG_VALID_MSG("Once branch failed, rollback");
4691 if (vstateVPop(ctxt) < 0 ) {
4692 DEBUG_VALID_MSG("exhaustion, failed");
4693 return(0);
4694 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004695 if (cur != ctxt->vstate->node)
4696 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004697 goto cont;
4698 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004699 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004700 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004701 DEBUG_VALID_MSG("Plus branch failed, rollback");
4702 if (vstateVPop(ctxt) < 0 ) {
4703 DEBUG_VALID_MSG("exhaustion, failed");
4704 return(0);
4705 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004706 if (cur != ctxt->vstate->node)
4707 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004708 goto cont;
4709 }
4710 DEBUG_VALID_MSG("Plus branch found");
4711 ret = 1;
4712 break;
4713 case XML_ELEMENT_CONTENT_MULT:
4714#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004715 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004716 DEBUG_VALID_MSG("Mult branch failed");
4717 } else {
4718 DEBUG_VALID_MSG("Mult branch found");
4719 }
4720#endif
4721 ret = 1;
4722 break;
4723 case XML_ELEMENT_CONTENT_OPT:
4724 DEBUG_VALID_MSG("Option branch failed");
4725 ret = 1;
4726 break;
4727 }
4728 } else {
4729 switch (CONT->ocur) {
4730 case XML_ELEMENT_CONTENT_OPT:
4731 DEBUG_VALID_MSG("Option branch succeeded");
4732 ret = 1;
4733 break;
4734 case XML_ELEMENT_CONTENT_ONCE:
4735 DEBUG_VALID_MSG("Once branch succeeded");
4736 ret = 1;
4737 break;
4738 case XML_ELEMENT_CONTENT_PLUS:
4739 if (STATE == ROLLBACK_PARENT) {
4740 DEBUG_VALID_MSG("Plus branch rollback");
4741 ret = 1;
4742 break;
4743 }
4744 if (NODE == NULL) {
4745 DEBUG_VALID_MSG("Plus branch exhausted");
4746 ret = 1;
4747 break;
4748 }
4749 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004750 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004751 goto cont;
4752 case XML_ELEMENT_CONTENT_MULT:
4753 if (STATE == ROLLBACK_PARENT) {
4754 DEBUG_VALID_MSG("Mult branch rollback");
4755 ret = 1;
4756 break;
4757 }
4758 if (NODE == NULL) {
4759 DEBUG_VALID_MSG("Mult branch exhausted");
4760 ret = 1;
4761 break;
4762 }
4763 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004764 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004765 goto cont;
4766 }
4767 }
4768 STATE = 0;
4769
4770 /*
4771 * Then act accordingly at the parent level
4772 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004773 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004774 if (CONT->parent == NULL)
4775 break;
4776
4777 switch (CONT->parent->type) {
4778 case XML_ELEMENT_CONTENT_PCDATA:
4779 DEBUG_VALID_MSG("Error: parent pcdata");
4780 return(-1);
4781 case XML_ELEMENT_CONTENT_ELEMENT:
4782 DEBUG_VALID_MSG("Error: parent element");
4783 return(-1);
4784 case XML_ELEMENT_CONTENT_OR:
4785 if (ret == 1) {
4786 DEBUG_VALID_MSG("Or succeeded");
4787 CONT = CONT->parent;
4788 DEPTH--;
4789 } else {
4790 DEBUG_VALID_MSG("Or failed");
4791 CONT = CONT->parent;
4792 DEPTH--;
4793 }
4794 break;
4795 case XML_ELEMENT_CONTENT_SEQ:
4796 if (ret == 0) {
4797 DEBUG_VALID_MSG("Sequence failed");
4798 CONT = CONT->parent;
4799 DEPTH--;
4800 } else if (CONT == CONT->parent->c1) {
4801 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4802 CONT = CONT->parent->c2;
4803 goto cont;
4804 } else {
4805 DEBUG_VALID_MSG("Sequence succeeded");
4806 CONT = CONT->parent;
4807 DEPTH--;
4808 }
4809 }
4810 }
4811 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004812 xmlNodePtr cur;
4813
4814 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004815 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4816 if (vstateVPop(ctxt) < 0 ) {
4817 DEBUG_VALID_MSG("exhaustion, failed");
4818 return(0);
4819 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004820 if (cur != ctxt->vstate->node)
4821 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004822 goto cont;
4823 }
4824 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004825 xmlNodePtr cur;
4826
4827 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004828 DEBUG_VALID_MSG("Failure, rollback");
4829 if (vstateVPop(ctxt) < 0 ) {
4830 DEBUG_VALID_MSG("exhaustion, failed");
4831 return(0);
4832 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004833 if (cur != ctxt->vstate->node)
4834 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004835 goto cont;
4836 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004837 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004838}
Daniel Veillard23e73572002-09-19 19:56:43 +00004839#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004840
4841/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00004842 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004843 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00004844 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004845 * @content: An element
4846 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
4847 *
4848 * This will dump the list of elements to the buffer
4849 * Intended just for the debug routine
4850 */
4851static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00004852xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004853 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00004854 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004855
4856 if (node == NULL) return;
4857 if (glob) strcat(buf, "(");
4858 cur = node;
4859 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004860 len = strlen(buf);
4861 if (size - len < 50) {
4862 if ((size - len > 4) && (buf[len - 1] != '.'))
4863 strcat(buf, " ...");
4864 return;
4865 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004866 switch (cur->type) {
4867 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004868 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004869 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004870 if ((size - len > 4) && (buf[len - 1] != '.'))
4871 strcat(buf, " ...");
4872 return;
4873 }
4874 strcat(buf, (char *) cur->ns->prefix);
4875 strcat(buf, ":");
4876 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004877 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004878 if ((size - len > 4) && (buf[len - 1] != '.'))
4879 strcat(buf, " ...");
4880 return;
4881 }
4882 strcat(buf, (char *) cur->name);
4883 if (cur->next != NULL)
4884 strcat(buf, " ");
4885 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004886 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004887 if (xmlIsBlankNode(cur))
4888 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004889 case XML_CDATA_SECTION_NODE:
4890 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004891 strcat(buf, "CDATA");
4892 if (cur->next != NULL)
4893 strcat(buf, " ");
4894 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004895 case XML_ATTRIBUTE_NODE:
4896 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004897#ifdef LIBXML_DOCB_ENABLED
4898 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004899#endif
4900 case XML_HTML_DOCUMENT_NODE:
4901 case XML_DOCUMENT_TYPE_NODE:
4902 case XML_DOCUMENT_FRAG_NODE:
4903 case XML_NOTATION_NODE:
4904 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004905 strcat(buf, "???");
4906 if (cur->next != NULL)
4907 strcat(buf, " ");
4908 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004909 case XML_ENTITY_NODE:
4910 case XML_PI_NODE:
4911 case XML_DTD_NODE:
4912 case XML_COMMENT_NODE:
4913 case XML_ELEMENT_DECL:
4914 case XML_ATTRIBUTE_DECL:
4915 case XML_ENTITY_DECL:
4916 case XML_XINCLUDE_START:
4917 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004918 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004919 }
4920 cur = cur->next;
4921 }
4922 if (glob) strcat(buf, ")");
4923}
4924
4925/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004926 * xmlValidateElementContent:
4927 * @ctxt: the validation context
4928 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004929 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004930 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004931 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004932 *
4933 * Try to validate the content model of an element
4934 *
4935 * returns 1 if valid or 0 if not and -1 in case of error
4936 */
4937
4938static int
4939xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004940 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004941 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00004942#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00004943 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00004944#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00004945 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004946 xmlElementContentPtr cont;
4947 const xmlChar *name;
4948
4949 if (elemDecl == NULL)
4950 return(-1);
4951 cont = elemDecl->content;
4952 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004953
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004954#ifdef LIBXML_REGEXP_ENABLED
4955 /* Build the regexp associated to the content model */
4956 if (elemDecl->contModel == NULL)
4957 ret = xmlValidBuildContentModel(ctxt, elemDecl);
4958 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004959 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004960 } else {
4961 xmlRegExecCtxtPtr exec;
4962
Daniel Veillardec498e12003-02-05 11:01:50 +00004963 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
4964 return(-1);
4965 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004966 ctxt->nodeMax = 0;
4967 ctxt->nodeNr = 0;
4968 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004969 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
4970 if (exec != NULL) {
4971 cur = child;
4972 while (cur != NULL) {
4973 switch (cur->type) {
4974 case XML_ENTITY_REF_NODE:
4975 /*
4976 * Push the current node to be able to roll back
4977 * and process within the entity
4978 */
4979 if ((cur->children != NULL) &&
4980 (cur->children->children != NULL)) {
4981 nodeVPush(ctxt, cur);
4982 cur = cur->children->children;
4983 continue;
4984 }
4985 break;
4986 case XML_TEXT_NODE:
4987 if (xmlIsBlankNode(cur))
4988 break;
4989 ret = 0;
4990 goto fail;
4991 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00004992 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004993 ret = 0;
4994 goto fail;
4995 case XML_ELEMENT_NODE:
4996 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004997 xmlChar fn[50];
4998 xmlChar *fullname;
4999
5000 fullname = xmlBuildQName(cur->name,
5001 cur->ns->prefix, fn, 50);
5002 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005003 ret = -1;
5004 goto fail;
5005 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005006 ret = xmlRegExecPushString(exec, fullname, NULL);
5007 if ((fullname != fn) && (fullname != cur->name))
5008 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005009 } else {
5010 ret = xmlRegExecPushString(exec, cur->name, NULL);
5011 }
5012 break;
5013 default:
5014 break;
5015 }
5016 /*
5017 * Switch to next element
5018 */
5019 cur = cur->next;
5020 while (cur == NULL) {
5021 cur = nodeVPop(ctxt);
5022 if (cur == NULL)
5023 break;
5024 cur = cur->next;
5025 }
5026 }
5027 ret = xmlRegExecPushString(exec, NULL, NULL);
5028fail:
5029 xmlRegFreeExecCtxt(exec);
5030 }
5031 }
5032#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005033 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005034 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005035 */
5036 ctxt->vstateMax = 8;
5037 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5038 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5039 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005040 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005041 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005042 }
5043 /*
5044 * The first entry in the stack is reserved to the current state
5045 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00005046 ctxt->nodeMax = 0;
5047 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00005048 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005049 ctxt->vstate = &ctxt->vstateTab[0];
5050 ctxt->vstateNr = 1;
5051 CONT = cont;
5052 NODE = child;
5053 DEPTH = 0;
5054 OCCURS = 0;
5055 STATE = 0;
5056 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005057 if ((ret == -3) && (warn)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005058 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
5059 "Content model for Element %s is ambiguous\n",
Daniel Veillard0cc72772003-10-13 14:00:21 +00005060 name, NULL, NULL);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005061 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005062 /*
5063 * An entities reference appeared at this level.
5064 * Buid a minimal representation of this node content
5065 * sufficient to run the validation process on it
5066 */
5067 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005068 cur = child;
5069 while (cur != NULL) {
5070 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005071 case XML_ENTITY_REF_NODE:
5072 /*
5073 * Push the current node to be able to roll back
5074 * and process within the entity
5075 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005076 if ((cur->children != NULL) &&
5077 (cur->children->children != NULL)) {
5078 nodeVPush(ctxt, cur);
5079 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005080 continue;
5081 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00005082 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005083 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005084 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005085 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005086 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005087 case XML_CDATA_SECTION_NODE:
5088 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005089 case XML_ELEMENT_NODE:
5090 /*
5091 * Allocate a new node and minimally fills in
5092 * what's required
5093 */
5094 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5095 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005096 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005097 xmlFreeNodeList(repl);
5098 ret = -1;
5099 goto done;
5100 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005101 tmp->type = cur->type;
5102 tmp->name = cur->name;
5103 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005104 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00005105 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005106 if (repl == NULL)
5107 repl = last = tmp;
5108 else {
5109 last->next = tmp;
5110 last = tmp;
5111 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005112 if (cur->type == XML_CDATA_SECTION_NODE) {
5113 /*
5114 * E59 spaces in CDATA does not match the
5115 * nonterminal S
5116 */
5117 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5118 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005119 break;
5120 default:
5121 break;
5122 }
5123 /*
5124 * Switch to next element
5125 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005126 cur = cur->next;
5127 while (cur == NULL) {
5128 cur = nodeVPop(ctxt);
5129 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005130 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005131 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005132 }
5133 }
5134
5135 /*
5136 * Relaunch the validation
5137 */
5138 ctxt->vstate = &ctxt->vstateTab[0];
5139 ctxt->vstateNr = 1;
5140 CONT = cont;
5141 NODE = repl;
5142 DEPTH = 0;
5143 OCCURS = 0;
5144 STATE = 0;
5145 ret = xmlValidateElementType(ctxt);
5146 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005147#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005148 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005149 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
5150 char expr[5000];
5151 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005152
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005153 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005154 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005155 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00005156#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005157 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005158 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005159 else
Daniel Veillard01992e02002-10-09 10:20:30 +00005160#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005161 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005162
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005163 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005164 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5165 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5166 name, BAD_CAST expr, BAD_CAST list);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005167 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005168 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5169 "Element content does not follow the DTD, expecting %s, got %s\n",
5170 BAD_CAST expr, BAD_CAST list, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005171 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005172 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005173 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005174 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005175 "Element %s content does not follow the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005176 name, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005177 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005178 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5179 "Element content does not follow the DTD\n",
5180 NULL, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005181 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005182 }
5183 ret = 0;
5184 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005185 if (ret == -3)
5186 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005187
Daniel Veillard23e73572002-09-19 19:56:43 +00005188#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005189done:
5190 /*
5191 * Deallocate the copy if done, and free up the validation stack
5192 */
5193 while (repl != NULL) {
5194 tmp = repl->next;
5195 xmlFree(repl);
5196 repl = tmp;
5197 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005198 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005199 if (ctxt->vstateTab != NULL) {
5200 xmlFree(ctxt->vstateTab);
5201 ctxt->vstateTab = NULL;
5202 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005203#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005204 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00005205 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005206 if (ctxt->nodeTab != NULL) {
5207 xmlFree(ctxt->nodeTab);
5208 ctxt->nodeTab = NULL;
5209 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005210 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005211
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005212}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005213
Owen Taylor3473f882001-02-23 17:55:21 +00005214/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005215 * xmlValidateCdataElement:
5216 * @ctxt: the validation context
5217 * @doc: a document instance
5218 * @elem: an element instance
5219 *
5220 * Check that an element follows #CDATA
5221 *
5222 * returns 1 if valid or 0 otherwise
5223 */
5224static int
5225xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5226 xmlNodePtr elem) {
5227 int ret = 1;
5228 xmlNodePtr cur, child;
5229
Daniel Veillardceb09b92002-10-04 11:46:37 +00005230 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005231 return(0);
5232
5233 child = elem->children;
5234
5235 cur = child;
5236 while (cur != NULL) {
5237 switch (cur->type) {
5238 case XML_ENTITY_REF_NODE:
5239 /*
5240 * Push the current node to be able to roll back
5241 * and process within the entity
5242 */
5243 if ((cur->children != NULL) &&
5244 (cur->children->children != NULL)) {
5245 nodeVPush(ctxt, cur);
5246 cur = cur->children->children;
5247 continue;
5248 }
5249 break;
5250 case XML_COMMENT_NODE:
5251 case XML_PI_NODE:
5252 case XML_TEXT_NODE:
5253 case XML_CDATA_SECTION_NODE:
5254 break;
5255 default:
5256 ret = 0;
5257 goto done;
5258 }
5259 /*
5260 * Switch to next element
5261 */
5262 cur = cur->next;
5263 while (cur == NULL) {
5264 cur = nodeVPop(ctxt);
5265 if (cur == NULL)
5266 break;
5267 cur = cur->next;
5268 }
5269 }
5270done:
5271 ctxt->nodeMax = 0;
5272 ctxt->nodeNr = 0;
5273 if (ctxt->nodeTab != NULL) {
5274 xmlFree(ctxt->nodeTab);
5275 ctxt->nodeTab = NULL;
5276 }
5277 return(ret);
5278}
5279
5280/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005281 * xmlValidateCheckMixed:
5282 * @ctxt: the validation context
5283 * @cont: the mixed content model
5284 * @qname: the qualified name as appearing in the serialization
5285 *
5286 * Check if the given node is part of the content model.
5287 *
5288 * Returns 1 if yes, 0 if no, -1 in case of error
5289 */
5290static int
William M. Brackedb65a72004-02-06 07:36:04 +00005291xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005292 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005293 const xmlChar *name;
5294 int plen;
5295 name = xmlSplitQName3(qname, &plen);
5296
5297 if (name == NULL) {
5298 while (cont != NULL) {
5299 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5300 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5301 return(1);
5302 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5303 (cont->c1 != NULL) &&
5304 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5305 if ((cont->c1->prefix == NULL) &&
5306 (xmlStrEqual(cont->c1->name, qname)))
5307 return(1);
5308 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5309 (cont->c1 == NULL) ||
5310 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005311 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5312 "Internal: MIXED struct corrupted\n",
5313 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005314 break;
5315 }
5316 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005317 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005318 } else {
5319 while (cont != NULL) {
5320 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5321 if ((cont->prefix != NULL) &&
5322 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5323 (xmlStrEqual(cont->name, name)))
5324 return(1);
5325 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5326 (cont->c1 != NULL) &&
5327 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5328 if ((cont->c1->prefix != NULL) &&
5329 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5330 (xmlStrEqual(cont->c1->name, name)))
5331 return(1);
5332 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5333 (cont->c1 == NULL) ||
5334 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005335 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5336 "Internal: MIXED struct corrupted\n",
5337 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005338 break;
5339 }
5340 cont = cont->c2;
5341 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005342 }
5343 return(0);
5344}
5345
5346/**
5347 * xmlValidGetElemDecl:
5348 * @ctxt: the validation context
5349 * @doc: a document instance
5350 * @elem: an element instance
5351 * @extsubset: pointer, (out) indicate if the declaration was found
5352 * in the external subset.
5353 *
5354 * Finds a declaration associated to an element in the document.
5355 *
5356 * returns the pointer to the declaration or NULL if not found.
5357 */
5358static xmlElementPtr
5359xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5360 xmlNodePtr elem, int *extsubset) {
5361 xmlElementPtr elemDecl = NULL;
5362 const xmlChar *prefix = NULL;
5363
5364 if ((elem == NULL) || (elem->name == NULL)) return(NULL);
5365 if (extsubset != NULL)
5366 *extsubset = 0;
5367
5368 /*
5369 * Fetch the declaration for the qualified name
5370 */
5371 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5372 prefix = elem->ns->prefix;
5373
5374 if (prefix != NULL) {
5375 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5376 elem->name, prefix);
5377 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5378 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5379 elem->name, prefix);
5380 if ((elemDecl != NULL) && (extsubset != NULL))
5381 *extsubset = 1;
5382 }
5383 }
5384
5385 /*
5386 * Fetch the declaration for the non qualified name
5387 * This is "non-strict" validation should be done on the
5388 * full QName but in that case being flexible makes sense.
5389 */
5390 if (elemDecl == NULL) {
5391 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5392 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5393 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5394 if ((elemDecl != NULL) && (extsubset != NULL))
5395 *extsubset = 1;
5396 }
5397 }
5398 if (elemDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005399 xmlErrValidNode(ctxt, elem,
5400 XML_DTD_UNKNOWN_ELEM,
5401 "No declaration for element %s\n",
5402 elem->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005403 }
5404 return(elemDecl);
5405}
5406
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005407#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005408/**
5409 * xmlValidatePushElement:
5410 * @ctxt: the validation context
5411 * @doc: a document instance
5412 * @elem: an element instance
5413 * @qname: the qualified name as appearing in the serialization
5414 *
5415 * Push a new element start on the validation stack.
5416 *
5417 * returns 1 if no validation problem was found or 0 otherwise
5418 */
5419int
5420xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5421 xmlNodePtr elem, const xmlChar *qname) {
5422 int ret = 1;
5423 xmlElementPtr eDecl;
5424 int extsubset = 0;
5425
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005426/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005427 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5428 xmlValidStatePtr state = ctxt->vstate;
5429 xmlElementPtr elemDecl;
5430
5431 /*
5432 * Check the new element agaisnt the content model of the new elem.
5433 */
5434 if (state->elemDecl != NULL) {
5435 elemDecl = state->elemDecl;
5436
5437 switch(elemDecl->etype) {
5438 case XML_ELEMENT_TYPE_UNDEFINED:
5439 ret = 0;
5440 break;
5441 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005442 xmlErrValidNode(ctxt, state->node,
5443 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005444 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005445 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005446 ret = 0;
5447 break;
5448 case XML_ELEMENT_TYPE_ANY:
5449 /* I don't think anything is required then */
5450 break;
5451 case XML_ELEMENT_TYPE_MIXED:
5452 /* simple case of declared as #PCDATA */
5453 if ((elemDecl->content != NULL) &&
5454 (elemDecl->content->type ==
5455 XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005456 xmlErrValidNode(ctxt, state->node,
5457 XML_DTD_NOT_PCDATA,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005458 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005459 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005460 ret = 0;
5461 } else {
5462 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5463 qname);
5464 if (ret != 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005465 xmlErrValidNode(ctxt, state->node,
5466 XML_DTD_INVALID_CHILD,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005467 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005468 qname, state->node->name, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005469 }
5470 }
5471 break;
5472 case XML_ELEMENT_TYPE_ELEMENT:
5473 /*
5474 * TODO:
5475 * VC: Standalone Document Declaration
5476 * - element types with element content, if white space
5477 * occurs directly within any instance of those types.
5478 */
5479 if (state->exec != NULL) {
5480 ret = xmlRegExecPushString(state->exec, qname, NULL);
5481 if (ret < 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005482 xmlErrValidNode(ctxt, state->node,
5483 XML_DTD_CONTENT_MODEL,
5484 "Element %s content does not follow the DTD, Misplaced %s\n",
5485 state->node->name, qname, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005486 ret = 0;
5487 } else {
5488 ret = 1;
5489 }
5490 }
5491 break;
5492 }
5493 }
5494 }
5495 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5496 vstateVPush(ctxt, eDecl, elem);
5497 return(ret);
5498}
5499
5500/**
5501 * xmlValidatePushCData:
5502 * @ctxt: the validation context
5503 * @data: some character data read
5504 * @len: the lenght of the data
5505 *
5506 * check the CData parsed for validation in the current stack
5507 *
5508 * returns 1 if no validation problem was found or 0 otherwise
5509 */
5510int
5511xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5512 int ret = 1;
5513
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005514/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005515 if (len <= 0)
5516 return(ret);
5517 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5518 xmlValidStatePtr state = ctxt->vstate;
5519 xmlElementPtr elemDecl;
5520
5521 /*
5522 * Check the new element agaisnt the content model of the new elem.
5523 */
5524 if (state->elemDecl != NULL) {
5525 elemDecl = state->elemDecl;
5526
5527 switch(elemDecl->etype) {
5528 case XML_ELEMENT_TYPE_UNDEFINED:
5529 ret = 0;
5530 break;
5531 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005532 xmlErrValidNode(ctxt, state->node,
5533 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005534 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005535 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005536 ret = 0;
5537 break;
5538 case XML_ELEMENT_TYPE_ANY:
5539 break;
5540 case XML_ELEMENT_TYPE_MIXED:
5541 break;
5542 case XML_ELEMENT_TYPE_ELEMENT:
5543 if (len > 0) {
5544 int i;
5545
5546 for (i = 0;i < len;i++) {
William M. Brack76e95df2003-10-18 16:20:14 +00005547 if (!IS_BLANK_CH(data[i])) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005548 xmlErrValidNode(ctxt, state->node,
5549 XML_DTD_CONTENT_MODEL,
5550 "Element %s content does not follow the DTD, Text not allowed\n",
5551 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005552 ret = 0;
5553 goto done;
5554 }
5555 }
5556 /*
5557 * TODO:
5558 * VC: Standalone Document Declaration
5559 * element types with element content, if white space
5560 * occurs directly within any instance of those types.
5561 */
5562 }
5563 break;
5564 }
5565 }
5566 }
5567done:
5568 return(ret);
5569}
5570
5571/**
5572 * xmlValidatePopElement:
5573 * @ctxt: the validation context
5574 * @doc: a document instance
5575 * @elem: an element instance
5576 * @qname: the qualified name as appearing in the serialization
5577 *
5578 * Pop the element end from the validation stack.
5579 *
5580 * returns 1 if no validation problem was found or 0 otherwise
5581 */
5582int
5583xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005584 xmlNodePtr elem ATTRIBUTE_UNUSED,
5585 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005586 int ret = 1;
5587
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005588/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005589 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5590 xmlValidStatePtr state = ctxt->vstate;
5591 xmlElementPtr elemDecl;
5592
5593 /*
5594 * Check the new element agaisnt the content model of the new elem.
5595 */
5596 if (state->elemDecl != NULL) {
5597 elemDecl = state->elemDecl;
5598
5599 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5600 if (state->exec != NULL) {
5601 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5602 if (ret == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005603 xmlErrValidNode(ctxt, state->node,
5604 XML_DTD_CONTENT_MODEL,
5605 "Element %s content does not follow the DTD, Expecting more child\n",
5606 state->node->name, NULL,NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005607 } else {
5608 /*
5609 * previous validation errors should not generate
5610 * a new one here
5611 */
5612 ret = 1;
5613 }
5614 }
5615 }
5616 }
5617 vstateVPop(ctxt);
5618 }
5619 return(ret);
5620}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005621#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005622
5623/**
Owen Taylor3473f882001-02-23 17:55:21 +00005624 * xmlValidateOneElement:
5625 * @ctxt: the validation context
5626 * @doc: a document instance
5627 * @elem: an element instance
5628 *
5629 * Try to validate a single element and it's attributes,
5630 * basically it does the following checks as described by the
5631 * XML-1.0 recommendation:
5632 * - [ VC: Element Valid ]
5633 * - [ VC: Required Attribute ]
5634 * Then call xmlValidateOneAttribute() for each attribute present.
5635 *
5636 * The ID/IDREF checkings are done separately
5637 *
5638 * returns 1 if valid or 0 otherwise
5639 */
5640
5641int
5642xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5643 xmlNodePtr elem) {
5644 xmlElementPtr elemDecl = NULL;
5645 xmlElementContentPtr cont;
5646 xmlAttributePtr attr;
5647 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005648 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005649 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005650 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005651
5652 CHECK_DTD;
5653
5654 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005655 switch (elem->type) {
5656 case XML_ATTRIBUTE_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005657 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5658 "Attribute element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005659 return(0);
5660 case XML_TEXT_NODE:
5661 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005662 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5663 "Text element has children !\n",
5664 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005665 return(0);
5666 }
5667 if (elem->properties != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005668 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5669 "Text element has attribute !\n",
5670 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005671 return(0);
5672 }
5673 if (elem->ns != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005674 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5675 "Text element has namespace !\n",
5676 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005677 return(0);
5678 }
5679 if (elem->nsDef != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005680 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5681 "Text element has namespace !\n",
5682 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005683 return(0);
5684 }
5685 if (elem->content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005686 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5687 "Text element has no content !\n",
5688 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005689 return(0);
5690 }
5691 return(1);
5692 case XML_XINCLUDE_START:
5693 case XML_XINCLUDE_END:
5694 return(1);
5695 case XML_CDATA_SECTION_NODE:
5696 case XML_ENTITY_REF_NODE:
5697 case XML_PI_NODE:
5698 case XML_COMMENT_NODE:
5699 return(1);
5700 case XML_ENTITY_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005701 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5702 "Entity element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005703 return(0);
5704 case XML_NOTATION_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005705 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5706 "Notation element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005707 return(0);
5708 case XML_DOCUMENT_NODE:
5709 case XML_DOCUMENT_TYPE_NODE:
5710 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005711 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5712 "Document element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005713 return(0);
5714 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005715 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5716 "HTML Document not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005717 return(0);
5718 case XML_ELEMENT_NODE:
5719 break;
5720 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005721 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5722 "unknown element type\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005723 return(0);
5724 }
Owen Taylor3473f882001-02-23 17:55:21 +00005725
5726 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005727 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005728 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005729 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5730 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005731 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005732
Daniel Veillardea7751d2002-12-20 00:16:24 +00005733 /*
5734 * If vstateNr is not zero that means continuous validation is
5735 * activated, do not try to check the content model at that level.
5736 */
5737 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005738 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005739 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005740 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005741 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
5742 "No declaration for element %s\n",
5743 elem->name, NULL, NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005744 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005745 case XML_ELEMENT_TYPE_EMPTY:
5746 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005747 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
Owen Taylor3473f882001-02-23 17:55:21 +00005748 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005749 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005750 ret = 0;
5751 }
5752 break;
5753 case XML_ELEMENT_TYPE_ANY:
5754 /* I don't think anything is required then */
5755 break;
5756 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005757
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005758 /* simple case of declared as #PCDATA */
5759 if ((elemDecl->content != NULL) &&
5760 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5761 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5762 if (!ret) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005763 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005764 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005765 elem->name, NULL, NULL);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005766 }
5767 break;
5768 }
Owen Taylor3473f882001-02-23 17:55:21 +00005769 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005770 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005771 while (child != NULL) {
5772 if (child->type == XML_ELEMENT_NODE) {
5773 name = child->name;
5774 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005775 xmlChar fn[50];
5776 xmlChar *fullname;
5777
5778 fullname = xmlBuildQName(child->name, child->ns->prefix,
5779 fn, 50);
5780 if (fullname == NULL)
5781 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005782 cont = elemDecl->content;
5783 while (cont != NULL) {
5784 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005785 if (xmlStrEqual(cont->name, fullname))
5786 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005787 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5788 (cont->c1 != NULL) &&
5789 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005790 if (xmlStrEqual(cont->c1->name, fullname))
5791 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005792 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5793 (cont->c1 == NULL) ||
5794 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005795 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5796 "Internal: MIXED struct corrupted\n",
5797 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005798 break;
5799 }
5800 cont = cont->c2;
5801 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005802 if ((fullname != fn) && (fullname != child->name))
5803 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005804 if (cont != NULL)
5805 goto child_ok;
5806 }
5807 cont = elemDecl->content;
5808 while (cont != NULL) {
5809 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5810 if (xmlStrEqual(cont->name, name)) break;
5811 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5812 (cont->c1 != NULL) &&
5813 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5814 if (xmlStrEqual(cont->c1->name, name)) break;
5815 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5816 (cont->c1 == NULL) ||
5817 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005818 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5819 "Internal: MIXED struct corrupted\n",
5820 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005821 break;
5822 }
5823 cont = cont->c2;
5824 }
5825 if (cont == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005826 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005827 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005828 name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005829 ret = 0;
5830 }
5831 }
5832child_ok:
5833 child = child->next;
5834 }
5835 break;
5836 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005837 if ((doc->standalone == 1) && (extsubset == 1)) {
5838 /*
5839 * VC: Standalone Document Declaration
5840 * - element types with element content, if white space
5841 * occurs directly within any instance of those types.
5842 */
5843 child = elem->children;
5844 while (child != NULL) {
5845 if (child->type == XML_TEXT_NODE) {
5846 const xmlChar *content = child->content;
5847
William M. Brack76e95df2003-10-18 16:20:14 +00005848 while (IS_BLANK_CH(*content))
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005849 content++;
5850 if (*content == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005851 xmlErrValidNode(ctxt, elem,
5852 XML_DTD_STANDALONE_WHITE_SPACE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005853"standalone: %s declared in the external subset contains white spaces nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005854 elem->name, NULL, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005855 ret = 0;
5856 break;
5857 }
5858 }
5859 child =child->next;
5860 }
5861 }
Owen Taylor3473f882001-02-23 17:55:21 +00005862 child = elem->children;
5863 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005864 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005865 if (tmp <= 0)
5866 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005867 break;
5868 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005869 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00005870
5871 /* [ VC: Required Attribute ] */
5872 attr = elemDecl->attributes;
5873 while (attr != NULL) {
5874 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005875 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00005876
Daniel Veillarde4301c82002-02-13 13:32:35 +00005877 if ((attr->prefix == NULL) &&
5878 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5879 xmlNsPtr ns;
5880
5881 ns = elem->nsDef;
5882 while (ns != NULL) {
5883 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005884 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005885 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00005886 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005887 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5888 xmlNsPtr ns;
5889
5890 ns = elem->nsDef;
5891 while (ns != NULL) {
5892 if (xmlStrEqual(attr->name, ns->prefix))
5893 goto found;
5894 ns = ns->next;
5895 }
5896 } else {
5897 xmlAttrPtr attrib;
5898
5899 attrib = elem->properties;
5900 while (attrib != NULL) {
5901 if (xmlStrEqual(attrib->name, attr->name)) {
5902 if (attr->prefix != NULL) {
5903 xmlNsPtr nameSpace = attrib->ns;
5904
5905 if (nameSpace == NULL)
5906 nameSpace = elem->ns;
5907 /*
5908 * qualified names handling is problematic, having a
5909 * different prefix should be possible but DTDs don't
5910 * allow to define the URI instead of the prefix :-(
5911 */
5912 if (nameSpace == NULL) {
5913 if (qualified < 0)
5914 qualified = 0;
5915 } else if (!xmlStrEqual(nameSpace->prefix,
5916 attr->prefix)) {
5917 if (qualified < 1)
5918 qualified = 1;
5919 } else
5920 goto found;
5921 } else {
5922 /*
5923 * We should allow applications to define namespaces
5924 * for their application even if the DTD doesn't
5925 * carry one, otherwise, basically we would always
5926 * break.
5927 */
5928 goto found;
5929 }
5930 }
5931 attrib = attrib->next;
5932 }
Owen Taylor3473f882001-02-23 17:55:21 +00005933 }
5934 if (qualified == -1) {
5935 if (attr->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005936 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005937 "Element %s does not carry attribute %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005938 elem->name, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005939 ret = 0;
5940 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005941 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005942 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005943 elem->name, attr->prefix,attr->name);
5944 ret = 0;
5945 }
5946 } else if (qualified == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005947 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00005948 "Element %s required attribute %s:%s has no prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005949 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005950 } else if (qualified == 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005951 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00005952 "Element %s required attribute %s:%s has different prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005953 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005954 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005955 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
5956 /*
5957 * Special tests checking #FIXED namespace declarations
5958 * have the right value since this is not done as an
5959 * attribute checking
5960 */
5961 if ((attr->prefix == NULL) &&
5962 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5963 xmlNsPtr ns;
5964
5965 ns = elem->nsDef;
5966 while (ns != NULL) {
5967 if (ns->prefix == NULL) {
5968 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005969 xmlErrValidNode(ctxt, elem,
5970 XML_DTD_ELEM_DEFAULT_NAMESPACE,
Daniel Veillarde4301c82002-02-13 13:32:35 +00005971 "Element %s namespace name for default namespace does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005972 elem->name, NULL, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00005973 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005974 }
5975 goto found;
5976 }
5977 ns = ns->next;
5978 }
5979 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5980 xmlNsPtr ns;
5981
5982 ns = elem->nsDef;
5983 while (ns != NULL) {
5984 if (xmlStrEqual(attr->name, ns->prefix)) {
5985 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005986 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005987 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005988 elem->name, ns->prefix, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00005989 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005990 }
5991 goto found;
5992 }
5993 ns = ns->next;
5994 }
5995 }
Owen Taylor3473f882001-02-23 17:55:21 +00005996 }
5997found:
5998 attr = attr->nexth;
5999 }
6000 return(ret);
6001}
6002
6003/**
6004 * xmlValidateRoot:
6005 * @ctxt: the validation context
6006 * @doc: a document instance
6007 *
6008 * Try to validate a the root element
6009 * basically it does the following check as described by the
6010 * XML-1.0 recommendation:
6011 * - [ VC: Root Element Type ]
6012 * it doesn't try to recurse or apply other check to the element
6013 *
6014 * returns 1 if valid or 0 otherwise
6015 */
6016
6017int
6018xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6019 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00006020 int ret;
6021
Owen Taylor3473f882001-02-23 17:55:21 +00006022 if (doc == NULL) return(0);
6023
6024 root = xmlDocGetRootElement(doc);
6025 if ((root == NULL) || (root->name == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006026 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
6027 "no root element\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006028 return(0);
6029 }
6030
6031 /*
6032 * When doing post validation against a separate DTD, those may
6033 * no internal subset has been generated
6034 */
6035 if ((doc->intSubset != NULL) &&
6036 (doc->intSubset->name != NULL)) {
6037 /*
6038 * Check first the document root against the NQName
6039 */
6040 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
6041 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006042 xmlChar fn[50];
6043 xmlChar *fullname;
6044
6045 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6046 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006047 xmlVErrMemory(ctxt, NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00006048 return(0);
6049 }
6050 ret = xmlStrEqual(doc->intSubset->name, fullname);
6051 if ((fullname != fn) && (fullname != root->name))
6052 xmlFree(fullname);
6053 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00006054 goto name_ok;
6055 }
6056 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6057 (xmlStrEqual(root->name, BAD_CAST "html")))
6058 goto name_ok;
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006059 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6060 "root and DTD name do not match '%s' and '%s'\n",
6061 root->name, doc->intSubset->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006062 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006063 }
6064 }
6065name_ok:
6066 return(1);
6067}
6068
6069
6070/**
6071 * xmlValidateElement:
6072 * @ctxt: the validation context
6073 * @doc: a document instance
6074 * @elem: an element instance
6075 *
6076 * Try to validate the subtree under an element
6077 *
6078 * returns 1 if valid or 0 otherwise
6079 */
6080
6081int
6082xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
6083 xmlNodePtr child;
6084 xmlAttrPtr attr;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006085 xmlNsPtr ns;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006086 const xmlChar *value;
Owen Taylor3473f882001-02-23 17:55:21 +00006087 int ret = 1;
6088
6089 if (elem == NULL) return(0);
6090
6091 /*
6092 * XInclude elements were added after parsing in the infoset,
6093 * they don't really mean anything validation wise.
6094 */
6095 if ((elem->type == XML_XINCLUDE_START) ||
6096 (elem->type == XML_XINCLUDE_END))
6097 return(1);
6098
6099 CHECK_DTD;
6100
Daniel Veillard10ea86c2001-06-20 13:55:33 +00006101 /*
6102 * Entities references have to be handled separately
6103 */
6104 if (elem->type == XML_ENTITY_REF_NODE) {
6105 return(1);
6106 }
6107
Owen Taylor3473f882001-02-23 17:55:21 +00006108 ret &= xmlValidateOneElement(ctxt, doc, elem);
6109 attr = elem->properties;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006110 while (attr != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006111 value = xmlNodeListGetString(doc, attr->children, 0);
6112 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6113 if (value != NULL)
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006114 xmlFree((char *)value);
Owen Taylor3473f882001-02-23 17:55:21 +00006115 attr= attr->next;
6116 }
Daniel Veillarde133dd82003-10-30 10:42:20 +00006117 ns = elem->nsDef;
6118 while (ns != NULL) {
Daniel Veillard1f5c9892003-12-29 17:09:55 +00006119 if (elem->ns == NULL)
6120 ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
6121 ns, ns->href);
6122 else
6123 ret &= xmlValidateOneNamespace(ctxt, doc, elem, elem->ns->prefix,
6124 ns, ns->href);
Daniel Veillarde133dd82003-10-30 10:42:20 +00006125 ns = ns->next;
6126 }
Owen Taylor3473f882001-02-23 17:55:21 +00006127 child = elem->children;
6128 while (child != NULL) {
6129 ret &= xmlValidateElement(ctxt, doc, child);
6130 child = child->next;
6131 }
6132
6133 return(ret);
6134}
6135
Daniel Veillard8730c562001-02-26 10:49:57 +00006136/**
6137 * xmlValidateRef:
6138 * @ref: A reference to be validated
6139 * @ctxt: Validation context
6140 * @name: Name of ID we are searching for
6141 *
6142 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006143static void
Daniel Veillard8730c562001-02-26 10:49:57 +00006144xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006145 const xmlChar *name) {
6146 xmlAttrPtr id;
6147 xmlAttrPtr attr;
6148
6149 if (ref == NULL)
6150 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006151 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006152 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006153 attr = ref->attr;
6154 if (attr == NULL) {
6155 xmlChar *dup, *str = NULL, *cur, save;
6156
6157 dup = xmlStrdup(name);
6158 if (dup == NULL) {
6159 ctxt->valid = 0;
6160 return;
6161 }
6162 cur = dup;
6163 while (*cur != 0) {
6164 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006165 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006166 save = *cur;
6167 *cur = 0;
6168 id = xmlGetID(ctxt->doc, str);
6169 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006170 xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006171 "attribute %s line %d references an unknown ID \"%s\"\n",
6172 ref->name, ref->lineno, str);
6173 ctxt->valid = 0;
6174 }
6175 if (save == 0)
6176 break;
6177 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006178 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006179 }
6180 xmlFree(dup);
6181 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00006182 id = xmlGetID(ctxt->doc, name);
6183 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006184 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006185 "IDREF attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006186 attr->name, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006187 ctxt->valid = 0;
6188 }
6189 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6190 xmlChar *dup, *str = NULL, *cur, save;
6191
6192 dup = xmlStrdup(name);
6193 if (dup == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006194 xmlVErrMemory(ctxt, "IDREFS split");
Owen Taylor3473f882001-02-23 17:55:21 +00006195 ctxt->valid = 0;
6196 return;
6197 }
6198 cur = dup;
6199 while (*cur != 0) {
6200 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006201 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006202 save = *cur;
6203 *cur = 0;
6204 id = xmlGetID(ctxt->doc, str);
6205 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006206 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006207 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006208 attr->name, str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006209 ctxt->valid = 0;
6210 }
6211 if (save == 0)
6212 break;
6213 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006214 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006215 }
6216 xmlFree(dup);
6217 }
6218}
6219
6220/**
Daniel Veillard8730c562001-02-26 10:49:57 +00006221 * xmlWalkValidateList:
6222 * @data: Contents of current link
6223 * @user: Value supplied by the user
6224 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006225 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00006226 */
6227static int
6228xmlWalkValidateList(const void *data, const void *user)
6229{
6230 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6231 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6232 return 1;
6233}
6234
6235/**
6236 * xmlValidateCheckRefCallback:
6237 * @ref_list: List of references
6238 * @ctxt: Validation context
6239 * @name: Name of ID we are searching for
6240 *
6241 */
6242static void
6243xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6244 const xmlChar *name) {
6245 xmlValidateMemo memo;
6246
6247 if (ref_list == NULL)
6248 return;
6249 memo.ctxt = ctxt;
6250 memo.name = name;
6251
6252 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6253
6254}
6255
6256/**
Owen Taylor3473f882001-02-23 17:55:21 +00006257 * xmlValidateDocumentFinal:
6258 * @ctxt: the validation context
6259 * @doc: a document instance
6260 *
6261 * Does the final step for the document validation once all the
6262 * incremental validation steps have been completed
6263 *
6264 * basically it does the following checks described by the XML Rec
6265 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006266 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00006267 *
6268 * returns 1 if valid or 0 otherwise
6269 */
6270
6271int
6272xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6273 xmlRefTablePtr table;
6274
6275 if (doc == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006276 xmlErrValid(ctxt, XML_DTD_NO_DOC,
6277 "xmlValidateDocumentFinal: doc == NULL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006278 return(0);
6279 }
6280
6281 /*
6282 * Check all the NOTATION/NOTATIONS attributes
6283 */
6284 /*
6285 * Check all the ENTITY/ENTITIES attributes definition for validity
6286 */
6287 /*
6288 * Check all the IDREF/IDREFS attributes definition for validity
6289 */
6290 table = (xmlRefTablePtr) doc->refs;
6291 ctxt->doc = doc;
6292 ctxt->valid = 1;
6293 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6294 return(ctxt->valid);
6295}
6296
6297/**
6298 * xmlValidateDtd:
6299 * @ctxt: the validation context
6300 * @doc: a document instance
6301 * @dtd: a dtd instance
6302 *
6303 * Try to validate the document against the dtd instance
6304 *
6305 * basically it does check all the definitions in the DtD.
6306 *
6307 * returns 1 if valid or 0 otherwise
6308 */
6309
6310int
6311xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6312 int ret;
6313 xmlDtdPtr oldExt;
6314 xmlNodePtr root;
6315
6316 if (dtd == NULL) return(0);
6317 if (doc == NULL) return(0);
6318 oldExt = doc->extSubset;
6319 doc->extSubset = dtd;
6320 ret = xmlValidateRoot(ctxt, doc);
6321 if (ret == 0) {
6322 doc->extSubset = oldExt;
6323 return(ret);
6324 }
6325 if (doc->ids != NULL) {
6326 xmlFreeIDTable(doc->ids);
6327 doc->ids = NULL;
6328 }
6329 if (doc->refs != NULL) {
6330 xmlFreeRefTable(doc->refs);
6331 doc->refs = NULL;
6332 }
6333 root = xmlDocGetRootElement(doc);
6334 ret = xmlValidateElement(ctxt, doc, root);
6335 ret &= xmlValidateDocumentFinal(ctxt, doc);
6336 doc->extSubset = oldExt;
6337 return(ret);
6338}
6339
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006340static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006341xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6342 const xmlChar *name ATTRIBUTE_UNUSED) {
6343 if (cur == NULL)
6344 return;
6345 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6346 xmlChar *notation = cur->content;
6347
Daniel Veillard878eab02002-02-19 13:46:09 +00006348 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006349 int ret;
6350
6351 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6352 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006353 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006354 }
6355 }
6356 }
6357}
6358
6359static void
Owen Taylor3473f882001-02-23 17:55:21 +00006360xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006361 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006362 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006363 xmlDocPtr doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006364 xmlElementPtr elem = NULL;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006365
Owen Taylor3473f882001-02-23 17:55:21 +00006366 if (cur == NULL)
6367 return;
6368 switch (cur->atype) {
6369 case XML_ATTRIBUTE_CDATA:
6370 case XML_ATTRIBUTE_ID:
6371 case XML_ATTRIBUTE_IDREF :
6372 case XML_ATTRIBUTE_IDREFS:
6373 case XML_ATTRIBUTE_NMTOKEN:
6374 case XML_ATTRIBUTE_NMTOKENS:
6375 case XML_ATTRIBUTE_ENUMERATION:
6376 break;
6377 case XML_ATTRIBUTE_ENTITY:
6378 case XML_ATTRIBUTE_ENTITIES:
6379 case XML_ATTRIBUTE_NOTATION:
6380 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006381
6382 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6383 cur->atype, cur->defaultValue);
6384 if ((ret == 0) && (ctxt->valid == 1))
6385 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006386 }
6387 if (cur->tree != NULL) {
6388 xmlEnumerationPtr tree = cur->tree;
6389 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006390 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006391 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006392 if ((ret == 0) && (ctxt->valid == 1))
6393 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006394 tree = tree->next;
6395 }
6396 }
6397 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006398 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6399 doc = cur->doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006400 if (cur->elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006401 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard878eab02002-02-19 13:46:09 +00006402 "xmlValidateAttributeCallback(%s): internal error\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006403 (const char *) cur->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00006404 return;
6405 }
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006406
6407 if (doc != NULL)
6408 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6409 if ((elem == NULL) && (doc != NULL))
Daniel Veillard878eab02002-02-19 13:46:09 +00006410 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006411 if ((elem == NULL) && (cur->parent != NULL) &&
6412 (cur->parent->type == XML_DTD_NODE))
6413 elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00006414 if (elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006415 xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
Daniel Veillard878eab02002-02-19 13:46:09 +00006416 "attribute %s: could not find decl for element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006417 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006418 return;
6419 }
6420 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006421 xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006422 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006423 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006424 ctxt->valid = 0;
6425 }
6426 }
Owen Taylor3473f882001-02-23 17:55:21 +00006427}
6428
6429/**
6430 * xmlValidateDtdFinal:
6431 * @ctxt: the validation context
6432 * @doc: a document instance
6433 *
6434 * Does the final step for the dtds validation once all the
6435 * subsets have been parsed
6436 *
6437 * basically it does the following checks described by the XML Rec
6438 * - check that ENTITY and ENTITIES type attributes default or
6439 * possible values matches one of the defined entities.
6440 * - check that NOTATION type attributes default or
6441 * possible values matches one of the defined notations.
6442 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006443 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006444 */
6445
6446int
6447xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006448 xmlDtdPtr dtd;
6449 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006450 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006451
6452 if (doc == NULL) return(0);
6453 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6454 return(0);
6455 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006456 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006457 dtd = doc->intSubset;
6458 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6459 table = (xmlAttributeTablePtr) dtd->attributes;
6460 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006461 }
6462 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006463 entities = (xmlEntitiesTablePtr) dtd->entities;
6464 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6465 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006466 }
6467 dtd = doc->extSubset;
6468 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6469 table = (xmlAttributeTablePtr) dtd->attributes;
6470 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006471 }
6472 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006473 entities = (xmlEntitiesTablePtr) dtd->entities;
6474 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6475 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006476 }
6477 return(ctxt->valid);
6478}
6479
6480/**
6481 * xmlValidateDocument:
6482 * @ctxt: the validation context
6483 * @doc: a document instance
6484 *
6485 * Try to validate the document instance
6486 *
6487 * basically it does the all the checks described by the XML Rec
6488 * i.e. validates the internal and external subset (if present)
6489 * and validate the document tree.
6490 *
6491 * returns 1 if valid or 0 otherwise
6492 */
6493
6494int
6495xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6496 int ret;
6497 xmlNodePtr root;
6498
Daniel Veillard2fd85422002-10-16 14:32:41 +00006499 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006500 xmlErrValid(ctxt, XML_DTD_NO_DTD,
6501 "no DTD found!\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006502 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006503 }
Owen Taylor3473f882001-02-23 17:55:21 +00006504 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6505 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
William M. Brack8c22f9f2004-08-06 16:23:27 +00006506 xmlChar *sysID;
6507 if (doc->intSubset->SystemID != NULL) {
William M. Brackbebe7302004-08-05 06:46:47 +00006508 sysID = xmlBuildURI(doc->intSubset->SystemID,
6509 doc->URL);
William M. Brack8c22f9f2004-08-06 16:23:27 +00006510 if (sysID == NULL) {
6511 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6512 "Could not build URI for external subset \"%s\"\n",
6513 (const char *) doc->intSubset->SystemID);
6514 return 0;
6515 }
6516 } else
William M. Brackbebe7302004-08-05 06:46:47 +00006517 sysID = NULL;
William M. Brack8c22f9f2004-08-06 16:23:27 +00006518 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
William M. Brackbebe7302004-08-05 06:46:47 +00006519 (const xmlChar *)sysID);
William M. Brackbebe7302004-08-05 06:46:47 +00006520 if (sysID != NULL)
6521 xmlFree(sysID);
Owen Taylor3473f882001-02-23 17:55:21 +00006522 if (doc->extSubset == NULL) {
6523 if (doc->intSubset->SystemID != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006524 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006525 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006526 (const char *) doc->intSubset->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00006527 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006528 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006529 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006530 (const char *) doc->intSubset->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00006531 }
6532 return(0);
6533 }
6534 }
6535
6536 if (doc->ids != NULL) {
6537 xmlFreeIDTable(doc->ids);
6538 doc->ids = NULL;
6539 }
6540 if (doc->refs != NULL) {
6541 xmlFreeRefTable(doc->refs);
6542 doc->refs = NULL;
6543 }
6544 ret = xmlValidateDtdFinal(ctxt, doc);
6545 if (!xmlValidateRoot(ctxt, doc)) return(0);
6546
6547 root = xmlDocGetRootElement(doc);
6548 ret &= xmlValidateElement(ctxt, doc, root);
6549 ret &= xmlValidateDocumentFinal(ctxt, doc);
6550 return(ret);
6551}
6552
Owen Taylor3473f882001-02-23 17:55:21 +00006553/************************************************************************
6554 * *
6555 * Routines for dynamic validation editing *
6556 * *
6557 ************************************************************************/
6558
6559/**
6560 * xmlValidGetPotentialChildren:
6561 * @ctree: an element content tree
6562 * @list: an array to store the list of child names
6563 * @len: a pointer to the number of element in the list
6564 * @max: the size of the array
6565 *
6566 * Build/extend a list of potential children allowed by the content tree
6567 *
6568 * returns the number of element in the list, or -1 in case of error.
6569 */
6570
6571int
6572xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6573 int *len, int max) {
6574 int i;
6575
6576 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6577 return(-1);
6578 if (*len >= max) return(*len);
6579
6580 switch (ctree->type) {
6581 case XML_ELEMENT_CONTENT_PCDATA:
6582 for (i = 0; i < *len;i++)
6583 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6584 list[(*len)++] = BAD_CAST "#PCDATA";
6585 break;
6586 case XML_ELEMENT_CONTENT_ELEMENT:
6587 for (i = 0; i < *len;i++)
6588 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6589 list[(*len)++] = ctree->name;
6590 break;
6591 case XML_ELEMENT_CONTENT_SEQ:
6592 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6593 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6594 break;
6595 case XML_ELEMENT_CONTENT_OR:
6596 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6597 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6598 break;
6599 }
6600
6601 return(*len);
6602}
6603
William M. Brack9333cc22004-06-24 08:33:40 +00006604/*
6605 * Dummy function to suppress messages while we try out valid elements
6606 */
6607static void xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
6608 const char *msg ATTRIBUTE_UNUSED, ...) {
6609 return;
6610}
6611
Owen Taylor3473f882001-02-23 17:55:21 +00006612/**
6613 * xmlValidGetValidElements:
6614 * @prev: an element to insert after
6615 * @next: an element to insert next
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006616 * @names: an array to store the list of child names
Owen Taylor3473f882001-02-23 17:55:21 +00006617 * @max: the size of the array
6618 *
6619 * This function returns the list of authorized children to insert
6620 * within an existing tree while respecting the validity constraints
6621 * forced by the Dtd. The insertion point is defined using @prev and
6622 * @next in the following ways:
6623 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6624 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6625 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6626 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6627 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6628 *
6629 * pointers to the element names are inserted at the beginning of the array
6630 * and do not need to be freed.
6631 *
6632 * returns the number of element in the list, or -1 in case of error. If
6633 * the function returns the value @max the caller is invited to grow the
6634 * receiving array and retry.
6635 */
6636
6637int
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006638xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
Owen Taylor3473f882001-02-23 17:55:21 +00006639 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006640 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006641 int nb_valid_elements = 0;
6642 const xmlChar *elements[256];
6643 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006644 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006645
6646 xmlNode *ref_node;
6647 xmlNode *parent;
6648 xmlNode *test_node;
6649
6650 xmlNode *prev_next;
6651 xmlNode *next_prev;
6652 xmlNode *parent_childs;
6653 xmlNode *parent_last;
6654
6655 xmlElement *element_desc;
6656
6657 if (prev == NULL && next == NULL)
6658 return(-1);
6659
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006660 if (names == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006661 if (max <= 0) return(-1);
6662
William M. Brack9333cc22004-06-24 08:33:40 +00006663 memset(&vctxt, 0, sizeof (xmlValidCtxt));
6664 vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
6665
Owen Taylor3473f882001-02-23 17:55:21 +00006666 nb_valid_elements = 0;
6667 ref_node = prev ? prev : next;
6668 parent = ref_node->parent;
6669
6670 /*
6671 * Retrieves the parent element declaration
6672 */
6673 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6674 parent->name);
6675 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6676 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6677 parent->name);
6678 if (element_desc == NULL) return(-1);
6679
6680 /*
6681 * Do a backup of the current tree structure
6682 */
6683 prev_next = prev ? prev->next : NULL;
6684 next_prev = next ? next->prev : NULL;
6685 parent_childs = parent->children;
6686 parent_last = parent->last;
6687
6688 /*
6689 * Creates a dummy node and insert it into the tree
6690 */
6691 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
6692 test_node->doc = ref_node->doc;
6693 test_node->parent = parent;
6694 test_node->prev = prev;
6695 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006696 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006697
6698 if (prev) prev->next = test_node;
6699 else parent->children = test_node;
6700
6701 if (next) next->prev = test_node;
6702 else parent->last = test_node;
6703
6704 /*
6705 * Insert each potential child node and check if the parent is
6706 * still valid
6707 */
6708 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6709 elements, &nb_elements, 256);
6710
6711 for (i = 0;i < nb_elements;i++) {
6712 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006713 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006714 int j;
6715
6716 for (j = 0; j < nb_valid_elements;j++)
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006717 if (xmlStrEqual(elements[i], names[j])) break;
6718 names[nb_valid_elements++] = elements[i];
Owen Taylor3473f882001-02-23 17:55:21 +00006719 if (nb_valid_elements >= max) break;
6720 }
6721 }
6722
6723 /*
6724 * Restore the tree structure
6725 */
6726 if (prev) prev->next = prev_next;
6727 if (next) next->prev = next_prev;
6728 parent->children = parent_childs;
6729 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006730
6731 /*
6732 * Free up the dummy node
6733 */
6734 test_node->name = name;
6735 xmlFreeNode(test_node);
6736
Owen Taylor3473f882001-02-23 17:55:21 +00006737 return(nb_valid_elements);
6738}
Daniel Veillard4432df22003-09-28 18:58:27 +00006739#endif /* LIBXML_VALID_ENABLED */
6740