blob: 98fc40b30ed5a8435c7579a7813d264cbb003496 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * valid.c : part of the code use to do the DTD handling and the validity
3 * checking
4 *
5 * See Copyright for the status of this software.
6 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <string.h>
14
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18
19#include <libxml/xmlmemory.h>
20#include <libxml/hash.h>
21#include <libxml/valid.h>
22#include <libxml/parser.h>
23#include <libxml/parserInternals.h>
24#include <libxml/xmlerror.h>
25#include <libxml/list.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000026#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000027
Daniel Veillard4432df22003-09-28 18:58:27 +000028static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
29 int create);
Daniel Veillarde62d36c2001-05-15 08:53:16 +000030/* #define DEBUG_VALID_ALGO */
Daniel Veillard5acfd6b2002-09-18 16:29:02 +000031/* #define DEBUG_REGEXP_ALGO */
Daniel Veillarde62d36c2001-05-15 08:53:16 +000032
Daniel Veillarda646cfd2002-09-17 21:50:03 +000033#define TODO \
34 xmlGenericError(xmlGenericErrorContext, \
35 "Unimplemented block at %s:%d\n", \
36 __FILE__, __LINE__);
Owen Taylor3473f882001-02-23 17:55:21 +000037
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000038/************************************************************************
39 * *
40 * Error handling routines *
41 * *
42 ************************************************************************/
43
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000044/**
Daniel Veillardce9457f2003-10-05 21:33:18 +000045 * xmlVErrMemory:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000046 * @ctxt: an XML validation parser context
47 * @extra: extra informations
48 *
49 * Handle an out of memory error
50 */
51static void
Daniel Veillardce9457f2003-10-05 21:33:18 +000052xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000053{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000054 xmlGenericErrorFunc channel = NULL;
55 xmlParserCtxtPtr pctxt = NULL;
56 void *data = NULL;
57
58 if (ctxt != NULL) {
59 channel = ctxt->error;
60 data = ctxt->userData;
61 pctxt = ctxt->userData;
62 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000063 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +000064 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000065 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000066 XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
67 "Memory allocation failed : %s\n", extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000068 else
Daniel Veillard73000572003-10-11 11:26:42 +000069 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000070 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000071 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
72 "Memory allocation failed\n");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000073}
74
75/**
76 * xmlErrValid:
77 * @ctxt: an XML validation parser context
Daniel Veillardbb5abab2003-10-03 22:21:51 +000078 * @error: the error number
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000079 * @extra: extra informations
80 *
81 * Handle a validation error
82 */
83static void
84xmlErrValid(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000085 const char *msg, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000086{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000087 xmlGenericErrorFunc channel = NULL;
88 xmlParserCtxtPtr pctxt = NULL;
89 void *data = NULL;
90
91 if (ctxt != NULL) {
92 channel = ctxt->error;
93 data = ctxt->userData;
94 pctxt = ctxt->userData;
95 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000096 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +000097 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000098 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000099 XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
100 msg, extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000101 else
Daniel Veillard73000572003-10-11 11:26:42 +0000102 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +0000103 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000104 XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
105 msg);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000106}
107
Daniel Veillardd61e8fb2003-10-19 21:59:17 +0000108#ifdef LIBXML_VALID_ENABLED
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000109/**
110 * xmlErrValidNodeNr:
111 * @ctxt: an XML validation parser context
112 * @node: the node raising the error
113 * @error: the error number
114 * @str1: extra informations
115 * @int2: extra informations
116 * @str3: extra informations
117 *
118 * Handle a validation error, provide contextual informations
119 */
120static void
Daniel Veillard72b9e292003-10-28 15:44:17 +0000121xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000122 xmlNodePtr node, xmlParserErrors error,
123 const char *msg, const xmlChar * str1,
124 int int2, const xmlChar * str3)
125{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000126 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000127 xmlGenericErrorFunc channel = NULL;
128 xmlParserCtxtPtr pctxt = NULL;
129 void *data = NULL;
130
131 if (ctxt != NULL) {
132 channel = ctxt->error;
133 data = ctxt->userData;
134 pctxt = ctxt->userData;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000135 pctxt = ctxt->userData;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000136 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000137 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000138 XML_ERR_ERROR, NULL, 0,
139 (const char *) str1,
140 (const char *) str3,
141 NULL, int2, 0, msg, str1, int2, str3);
142}
143/**
144 * xmlErrValidNode:
145 * @ctxt: an XML validation parser context
146 * @node: the node raising the error
147 * @error: the error number
148 * @str1: extra informations
149 * @str2: extra informations
150 * @str3: extra informations
151 *
152 * Handle a validation error, provide contextual informations
153 */
154static void
155xmlErrValidNode(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
156 xmlNodePtr node, xmlParserErrors error,
157 const char *msg, const xmlChar * str1,
158 const xmlChar * str2, const xmlChar * str3)
159{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000160 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000161 xmlGenericErrorFunc channel = NULL;
162 xmlParserCtxtPtr pctxt = NULL;
163 void *data = NULL;
164
165 if (ctxt != NULL) {
166 channel = ctxt->error;
167 data = ctxt->userData;
168 pctxt = ctxt->userData;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000169 pctxt = ctxt->userData;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000170 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000171 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000172 XML_ERR_ERROR, NULL, 0,
173 (const char *) str1,
174 (const char *) str1,
175 (const char *) str3, 0, 0, msg, str1, str2, str3);
176}
177/**
178 * xmlErrValidWarning:
179 * @ctxt: an XML validation parser context
180 * @node: the node raising the error
181 * @error: the error number
182 * @str1: extra informations
183 * @str2: extra informations
184 * @str3: extra informations
185 *
186 * Handle a validation error, provide contextual informations
187 */
188static void
189xmlErrValidWarning(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
190 xmlNodePtr node, xmlParserErrors error,
191 const char *msg, const xmlChar * str1,
192 const xmlChar * str2, const xmlChar * str3)
193{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000194 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000195 xmlGenericErrorFunc channel = NULL;
196 xmlParserCtxtPtr pctxt = NULL;
197 void *data = NULL;
198
199 if (ctxt != NULL) {
200 channel = ctxt->error;
201 data = ctxt->userData;
202 pctxt = ctxt->userData;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000203 pctxt = ctxt->userData;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000204 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000205 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000206 XML_ERR_WARNING, NULL, 0,
207 (const char *) str1,
208 (const char *) str1,
209 (const char *) str3, 0, 0, msg, str1, str2, str3);
210}
211
212
Daniel Veillardea7751d2002-12-20 00:16:24 +0000213
214#ifdef LIBXML_REGEXP_ENABLED
215/*
216 * If regexp are enabled we can do continuous validation without the
217 * need of a tree to validate the content model. this is done in each
218 * callbacks.
219 * Each xmlValidState represent the validation state associated to the
220 * set of nodes currently open from the document root to the current element.
221 */
222
223
224typedef struct _xmlValidState {
225 xmlElementPtr elemDecl; /* pointer to the content model */
226 xmlNodePtr node; /* pointer to the current node */
227 xmlRegExecCtxtPtr exec; /* regexp runtime */
228} _xmlValidState;
229
230
231static int
232vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000233 if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000234 ctxt->vstateMax = 10;
235 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
236 sizeof(ctxt->vstateTab[0]));
237 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000238 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000239 return(-1);
240 }
241 }
242
243 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000244 xmlValidState *tmp;
245
246 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
247 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
248 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000249 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000250 return(-1);
251 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000252 ctxt->vstateMax *= 2;
253 ctxt->vstateTab = tmp;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000254 }
255 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
256 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
257 ctxt->vstateTab[ctxt->vstateNr].node = node;
258 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
259 if (elemDecl->contModel == NULL)
260 xmlValidBuildContentModel(ctxt, elemDecl);
261 if (elemDecl->contModel != NULL) {
262 ctxt->vstateTab[ctxt->vstateNr].exec =
263 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
264 } else {
265 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000266 xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
267 XML_ERR_INTERNAL_ERROR,
268 "Failed to build content model regexp for %s\n",
269 node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000270 }
271 }
272 return(ctxt->vstateNr++);
273}
274
275static int
276vstateVPop(xmlValidCtxtPtr ctxt) {
277 xmlElementPtr elemDecl;
278
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000279 if (ctxt->vstateNr < 1) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000280 ctxt->vstateNr--;
281 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
282 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
283 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
284 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
285 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
286 }
287 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
288 if (ctxt->vstateNr >= 1)
289 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
290 else
291 ctxt->vstate = NULL;
292 return(ctxt->vstateNr);
293}
294
295#else /* not LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000296/*
Daniel Veillard1c732d22002-11-30 11:22:59 +0000297 * If regexp are not enabled, it uses a home made algorithm less
298 * complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000299 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000300 * only restriction is on the deepness of the tree limited by the
301 * size of the occurs bitfield
302 *
303 * this is the content of a saved state for rollbacks
304 */
305
306#define ROLLBACK_OR 0
307#define ROLLBACK_PARENT 1
308
Daniel Veillardb44025c2001-10-11 22:55:55 +0000309typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000310 xmlElementContentPtr cont; /* pointer to the content model subtree */
311 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000312 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000313 unsigned char depth; /* current depth in the overall tree */
314 unsigned char state; /* ROLLBACK_XXX */
315} _xmlValidState;
316
Daniel Veillardfc57b412002-04-29 15:50:14 +0000317#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000318#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
319#define CONT ctxt->vstate->cont
320#define NODE ctxt->vstate->node
321#define DEPTH ctxt->vstate->depth
322#define OCCURS ctxt->vstate->occurs
323#define STATE ctxt->vstate->state
324
Daniel Veillard5344c602001-12-31 16:37:34 +0000325#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
326#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000327
Daniel Veillard5344c602001-12-31 16:37:34 +0000328#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
329#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000330
331static int
332vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
333 xmlNodePtr node, unsigned char depth, long occurs,
334 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000335 int i = ctxt->vstateNr - 1;
336
Daniel Veillard940492d2002-04-15 10:15:25 +0000337 if (ctxt->vstateNr > MAX_RECURSE) {
338 return(-1);
339 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000340 if (ctxt->vstateTab == NULL) {
341 ctxt->vstateMax = 8;
342 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
343 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
344 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000345 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000346 return(-1);
347 }
348 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000349 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000350 xmlValidState *tmp;
351
352 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
353 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
354 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000355 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard940492d2002-04-15 10:15:25 +0000356 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000357 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000358 ctxt->vstateMax *= 2;
359 ctxt->vstateTab = tmp;
Daniel Veillard06803992001-04-22 10:35:56 +0000360 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000361 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000362 /*
363 * Don't push on the stack a state already here
364 */
365 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
366 (ctxt->vstateTab[i].node == node) &&
367 (ctxt->vstateTab[i].depth == depth) &&
368 (ctxt->vstateTab[i].occurs == occurs) &&
369 (ctxt->vstateTab[i].state == state))
370 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000371 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
372 ctxt->vstateTab[ctxt->vstateNr].node = node;
373 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
374 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
375 ctxt->vstateTab[ctxt->vstateNr].state = state;
376 return(ctxt->vstateNr++);
377}
378
379static int
380vstateVPop(xmlValidCtxtPtr ctxt) {
381 if (ctxt->vstateNr <= 1) return(-1);
382 ctxt->vstateNr--;
383 ctxt->vstate = &ctxt->vstateTab[0];
384 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
385 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
386 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
387 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
388 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
389 return(ctxt->vstateNr);
390}
391
Daniel Veillard118aed72002-09-24 14:13:13 +0000392#endif /* LIBXML_REGEXP_ENABLED */
393
Daniel Veillard1c732d22002-11-30 11:22:59 +0000394static int
395nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
396{
397 if (ctxt->nodeMax <= 0) {
398 ctxt->nodeMax = 4;
399 ctxt->nodeTab =
400 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
401 sizeof(ctxt->nodeTab[0]));
402 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000403 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000404 ctxt->nodeMax = 0;
405 return (0);
406 }
407 }
408 if (ctxt->nodeNr >= ctxt->nodeMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000409 xmlNodePtr *tmp;
410 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
411 ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
412 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000413 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000414 return (0);
415 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000416 ctxt->nodeMax *= 2;
417 ctxt->nodeTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000418 }
419 ctxt->nodeTab[ctxt->nodeNr] = value;
420 ctxt->node = value;
421 return (ctxt->nodeNr++);
422}
423static xmlNodePtr
424nodeVPop(xmlValidCtxtPtr ctxt)
425{
426 xmlNodePtr ret;
427
428 if (ctxt->nodeNr <= 0)
429 return (0);
430 ctxt->nodeNr--;
431 if (ctxt->nodeNr > 0)
432 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
433 else
434 ctxt->node = NULL;
435 ret = ctxt->nodeTab[ctxt->nodeNr];
436 ctxt->nodeTab[ctxt->nodeNr] = 0;
437 return (ret);
438}
Owen Taylor3473f882001-02-23 17:55:21 +0000439
Owen Taylor3473f882001-02-23 17:55:21 +0000440#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000441static void
442xmlValidPrintNode(xmlNodePtr cur) {
443 if (cur == NULL) {
444 xmlGenericError(xmlGenericErrorContext, "null");
445 return;
446 }
447 switch (cur->type) {
448 case XML_ELEMENT_NODE:
449 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
450 break;
451 case XML_TEXT_NODE:
452 xmlGenericError(xmlGenericErrorContext, "text ");
453 break;
454 case XML_CDATA_SECTION_NODE:
455 xmlGenericError(xmlGenericErrorContext, "cdata ");
456 break;
457 case XML_ENTITY_REF_NODE:
458 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
459 break;
460 case XML_PI_NODE:
461 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
462 break;
463 case XML_COMMENT_NODE:
464 xmlGenericError(xmlGenericErrorContext, "comment ");
465 break;
466 case XML_ATTRIBUTE_NODE:
467 xmlGenericError(xmlGenericErrorContext, "?attr? ");
468 break;
469 case XML_ENTITY_NODE:
470 xmlGenericError(xmlGenericErrorContext, "?ent? ");
471 break;
472 case XML_DOCUMENT_NODE:
473 xmlGenericError(xmlGenericErrorContext, "?doc? ");
474 break;
475 case XML_DOCUMENT_TYPE_NODE:
476 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
477 break;
478 case XML_DOCUMENT_FRAG_NODE:
479 xmlGenericError(xmlGenericErrorContext, "?frag? ");
480 break;
481 case XML_NOTATION_NODE:
482 xmlGenericError(xmlGenericErrorContext, "?nota? ");
483 break;
484 case XML_HTML_DOCUMENT_NODE:
485 xmlGenericError(xmlGenericErrorContext, "?html? ");
486 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000487#ifdef LIBXML_DOCB_ENABLED
488 case XML_DOCB_DOCUMENT_NODE:
489 xmlGenericError(xmlGenericErrorContext, "?docb? ");
490 break;
491#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000492 case XML_DTD_NODE:
493 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
494 break;
495 case XML_ELEMENT_DECL:
496 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
497 break;
498 case XML_ATTRIBUTE_DECL:
499 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
500 break;
501 case XML_ENTITY_DECL:
502 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
503 break;
504 case XML_NAMESPACE_DECL:
505 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
506 break;
507 case XML_XINCLUDE_START:
508 xmlGenericError(xmlGenericErrorContext, "incstart ");
509 break;
510 case XML_XINCLUDE_END:
511 xmlGenericError(xmlGenericErrorContext, "incend ");
512 break;
513 }
514}
515
516static void
517xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000518 if (cur == NULL)
519 xmlGenericError(xmlGenericErrorContext, "null ");
520 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000521 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000522 cur = cur->next;
523 }
524}
525
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000526static void
527xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Daniel Veillard83391282003-03-06 21:37:30 +0000528 char expr[5000];
Owen Taylor3473f882001-02-23 17:55:21 +0000529
530 expr[0] = 0;
531 xmlGenericError(xmlGenericErrorContext, "valid: ");
532 xmlValidPrintNodeList(cur);
533 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000534 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000535 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
536}
537
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000538static void
539xmlValidDebugState(xmlValidStatePtr state) {
540 xmlGenericError(xmlGenericErrorContext, "(");
541 if (state->cont == NULL)
542 xmlGenericError(xmlGenericErrorContext, "null,");
543 else
544 switch (state->cont->type) {
545 case XML_ELEMENT_CONTENT_PCDATA:
546 xmlGenericError(xmlGenericErrorContext, "pcdata,");
547 break;
548 case XML_ELEMENT_CONTENT_ELEMENT:
549 xmlGenericError(xmlGenericErrorContext, "%s,",
550 state->cont->name);
551 break;
552 case XML_ELEMENT_CONTENT_SEQ:
553 xmlGenericError(xmlGenericErrorContext, "seq,");
554 break;
555 case XML_ELEMENT_CONTENT_OR:
556 xmlGenericError(xmlGenericErrorContext, "or,");
557 break;
558 }
559 xmlValidPrintNode(state->node);
560 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
561 state->depth, state->occurs, state->state);
562}
563
564static void
565xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
566 int i, j;
567
568 xmlGenericError(xmlGenericErrorContext, "state: ");
569 xmlValidDebugState(ctxt->vstate);
570 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
571 ctxt->vstateNr - 1);
572 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
573 xmlValidDebugState(&ctxt->vstateTab[j]);
574 xmlGenericError(xmlGenericErrorContext, "\n");
575}
576
577/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000578#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000579 *****/
580
581#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000582#define DEBUG_VALID_MSG(m) \
583 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
584
Owen Taylor3473f882001-02-23 17:55:21 +0000585#else
586#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000587#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000588#endif
589
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000590/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000591
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000592
Owen Taylor3473f882001-02-23 17:55:21 +0000593#define CHECK_DTD \
594 if (doc == NULL) return(0); \
595 else if ((doc->intSubset == NULL) && \
596 (doc->extSubset == NULL)) return(0)
597
Owen Taylor3473f882001-02-23 17:55:21 +0000598xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
599
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000600#ifdef LIBXML_REGEXP_ENABLED
601
602/************************************************************************
603 * *
604 * Content model validation based on the regexps *
605 * *
606 ************************************************************************/
607
608/**
609 * xmlValidBuildAContentModel:
610 * @content: the content model
611 * @ctxt: the schema parser context
612 * @name: the element name whose content is being built
613 *
614 * Generate the automata sequence needed for that type
615 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000616 * Returns 1 if successful or 0 in case of error.
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000617 */
618static int
619xmlValidBuildAContentModel(xmlElementContentPtr content,
620 xmlValidCtxtPtr ctxt,
621 const xmlChar *name) {
622 if (content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000623 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
624 "Found NULL content in content model of %s\n",
625 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000626 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000627 }
628 switch (content->type) {
629 case XML_ELEMENT_CONTENT_PCDATA:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000630 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
631 "Found PCDATA in content model of %s\n",
632 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000633 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000634 break;
635 case XML_ELEMENT_CONTENT_ELEMENT: {
636 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillardc00cda82003-04-07 10:22:39 +0000637 xmlChar fn[50];
638 xmlChar *fullname;
639
640 fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
641 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000642 xmlVErrMemory(ctxt, "Building content model");
Daniel Veillardc00cda82003-04-07 10:22:39 +0000643 return(0);
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000644 }
645
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000646 switch (content->ocur) {
647 case XML_ELEMENT_CONTENT_ONCE:
648 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000649 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000650 break;
651 case XML_ELEMENT_CONTENT_OPT:
652 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000653 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000654 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
655 break;
656 case XML_ELEMENT_CONTENT_PLUS:
657 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000658 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000659 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000660 ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000661 break;
662 case XML_ELEMENT_CONTENT_MULT:
663 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000664 ctxt->state, fullname, NULL);
Daniel Veillard57e79b32003-02-04 15:33:12 +0000665 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
666 NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000667 break;
668 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000669 if ((fullname != fn) && (fullname != content->name))
670 xmlFree(fullname);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000671 break;
672 }
673 case XML_ELEMENT_CONTENT_SEQ: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000674 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000675 xmlElementContentOccur ocur;
676
677 /*
678 * Simply iterate over the content
679 */
680 oldstate = ctxt->state;
681 ocur = content->ocur;
Daniel Veillardf4be0182003-02-24 19:54:33 +0000682 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
683 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
684 oldstate = ctxt->state;
685 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000686 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000687 xmlValidBuildAContentModel(content->c1, ctxt, name);
688 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000689 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
690 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
691 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000692 oldend = ctxt->state;
693 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000694 switch (ocur) {
695 case XML_ELEMENT_CONTENT_ONCE:
696 break;
697 case XML_ELEMENT_CONTENT_OPT:
698 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
699 break;
700 case XML_ELEMENT_CONTENT_MULT:
701 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000702 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000703 break;
704 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000705 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000706 break;
707 }
708 break;
709 }
710 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000711 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000712 xmlElementContentOccur ocur;
713
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000714 ocur = content->ocur;
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000715 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
716 (ocur == XML_ELEMENT_CONTENT_MULT)) {
717 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
718 ctxt->state, NULL);
719 }
720 oldstate = ctxt->state;
721 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000722
723 /*
724 * iterate over the subtypes and remerge the end with an
725 * epsilon transition
726 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000727 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000728 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000729 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000730 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000731 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000732 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
733 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000734 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000735 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000736 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
737 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000738 switch (ocur) {
739 case XML_ELEMENT_CONTENT_ONCE:
740 break;
741 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000742 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000743 break;
744 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000745 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
746 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000747 break;
748 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000749 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000750 break;
751 }
752 break;
753 }
754 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000755 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
756 "ContentModel broken for element %s\n",
757 (const char *) name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000758 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000759 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000760 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000761}
762/**
763 * xmlValidBuildContentModel:
764 * @ctxt: a validation context
765 * @elem: an element declaration node
766 *
767 * (Re)Build the automata associated to the content model of this
768 * element
769 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000770 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000771 */
772int
773xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000774
775 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000776 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000777 if (elem->type != XML_ELEMENT_DECL)
778 return(0);
779 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
780 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000781 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000782 if (elem->contModel != NULL) {
783 if (!xmlRegexpIsDeterminist(elem->contModel)) {
784 ctxt->valid = 0;
785 return(0);
786 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000787 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000788 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000789
790 ctxt->am = xmlNewAutomata();
791 if (ctxt->am == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000792 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
793 XML_ERR_INTERNAL_ERROR,
794 "Cannot create automata for element %s\n",
795 elem->name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000796 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000797 }
William M. Brack78637da2003-07-31 14:47:38 +0000798 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000799 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
800 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000801 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000802 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000803 char expr[5000];
804 expr[0] = 0;
805 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000806 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
807 XML_DTD_CONTENT_NOT_DETERMINIST,
808 "Content model of %s is not determinist: %s\n",
809 elem->name, BAD_CAST expr, NULL);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000810#ifdef DEBUG_REGEXP_ALGO
811 xmlRegexpPrint(stderr, elem->contModel);
812#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000813 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000814 ctxt->state = NULL;
815 xmlFreeAutomata(ctxt->am);
816 ctxt->am = NULL;
817 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000818 }
819 ctxt->state = NULL;
820 xmlFreeAutomata(ctxt->am);
821 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000822 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000823}
824
825#endif /* LIBXML_REGEXP_ENABLED */
826
Owen Taylor3473f882001-02-23 17:55:21 +0000827/****************************************************************
828 * *
829 * Util functions for data allocation/deallocation *
830 * *
831 ****************************************************************/
832
833/**
Daniel Veillarda37aab82003-06-09 09:10:36 +0000834 * xmlNewValidCtxt:
835 *
836 * Allocate a validation context structure.
837 *
838 * Returns NULL if not, otherwise the new validation context structure
839 */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000840xmlValidCtxtPtr xmlNewValidCtxt(void) {
Daniel Veillarda37aab82003-06-09 09:10:36 +0000841 xmlValidCtxtPtr ret;
842
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000843 if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000844 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda37aab82003-06-09 09:10:36 +0000845 return (NULL);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000846 }
Daniel Veillarda37aab82003-06-09 09:10:36 +0000847
848 (void) memset(ret, 0, sizeof (xmlValidCtxt));
849
850 return (ret);
851}
852
853/**
854 * xmlFreeValidCtxt:
855 * @cur: the validation context to free
856 *
857 * Free a validation context structure.
858 */
859void
860xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
861 xmlFree(cur);
862}
863
Daniel Veillard4432df22003-09-28 18:58:27 +0000864#endif /* LIBXML_VALID_ENABLED */
865
Daniel Veillarda37aab82003-06-09 09:10:36 +0000866/**
Owen Taylor3473f882001-02-23 17:55:21 +0000867 * xmlNewElementContent:
868 * @name: the subelement name or NULL
869 * @type: the type of element content decl
870 *
871 * Allocate an element content structure.
872 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000873 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000874 */
875xmlElementContentPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000876xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
Owen Taylor3473f882001-02-23 17:55:21 +0000877 xmlElementContentPtr ret;
878
879 switch(type) {
880 case XML_ELEMENT_CONTENT_ELEMENT:
881 if (name == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000882 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
883 "xmlNewElementContent : name == NULL !\n",
884 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000885 }
886 break;
887 case XML_ELEMENT_CONTENT_PCDATA:
888 case XML_ELEMENT_CONTENT_SEQ:
889 case XML_ELEMENT_CONTENT_OR:
890 if (name != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000891 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
892 "xmlNewElementContent : name != NULL !\n",
893 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000894 }
895 break;
896 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000897 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
898 "Internal: ELEMENT content corrupted invalid type\n",
899 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000900 return(NULL);
901 }
902 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
903 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000904 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000905 return(NULL);
906 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000907 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000908 ret->type = type;
909 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000910 if (name != NULL) {
911 xmlChar *prefix = NULL;
912 ret->name = xmlSplitQName2(name, &prefix);
913 if (ret->name == NULL)
914 ret->name = xmlStrdup(name);
915 ret->prefix = prefix;
916 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000917 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000918 ret->prefix = NULL;
919 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000920 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000921 return(ret);
922}
923
924/**
925 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000926 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +0000927 *
928 * Build a copy of an element content description.
929 *
930 * Returns the new xmlElementContentPtr or NULL in case of error.
931 */
932xmlElementContentPtr
933xmlCopyElementContent(xmlElementContentPtr cur) {
934 xmlElementContentPtr ret;
935
936 if (cur == NULL) return(NULL);
937 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
938 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000939 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000940 return(NULL);
941 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000942 if (cur->prefix != NULL)
943 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000944 ret->ocur = cur->ocur;
945 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000946 if (ret->c1 != NULL)
947 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000948 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000949 if (ret->c2 != NULL)
950 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000951 return(ret);
952}
953
954/**
955 * xmlFreeElementContent:
956 * @cur: the element content tree to free
957 *
958 * Free an element content structure. This is a recursive call !
959 */
960void
961xmlFreeElementContent(xmlElementContentPtr cur) {
962 if (cur == NULL) return;
963 switch (cur->type) {
964 case XML_ELEMENT_CONTENT_PCDATA:
965 case XML_ELEMENT_CONTENT_ELEMENT:
966 case XML_ELEMENT_CONTENT_SEQ:
967 case XML_ELEMENT_CONTENT_OR:
968 break;
969 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000970 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
971 "Internal: ELEMENT content corrupted invalid type\n",
972 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000973 return;
974 }
975 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
976 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
977 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000978 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000979 xmlFree(cur);
980}
981
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000982#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000983/**
984 * xmlDumpElementContent:
985 * @buf: An XML buffer
986 * @content: An element table
987 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
988 *
989 * This will dump the content of the element table as an XML DTD definition
990 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000991static void
Owen Taylor3473f882001-02-23 17:55:21 +0000992xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
993 if (content == NULL) return;
994
995 if (glob) xmlBufferWriteChar(buf, "(");
996 switch (content->type) {
997 case XML_ELEMENT_CONTENT_PCDATA:
998 xmlBufferWriteChar(buf, "#PCDATA");
999 break;
1000 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001001 if (content->prefix != NULL) {
1002 xmlBufferWriteCHAR(buf, content->prefix);
1003 xmlBufferWriteChar(buf, ":");
1004 }
Owen Taylor3473f882001-02-23 17:55:21 +00001005 xmlBufferWriteCHAR(buf, content->name);
1006 break;
1007 case XML_ELEMENT_CONTENT_SEQ:
1008 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1009 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1010 xmlDumpElementContent(buf, content->c1, 1);
1011 else
1012 xmlDumpElementContent(buf, content->c1, 0);
1013 xmlBufferWriteChar(buf, " , ");
1014 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
1015 xmlDumpElementContent(buf, content->c2, 1);
1016 else
1017 xmlDumpElementContent(buf, content->c2, 0);
1018 break;
1019 case XML_ELEMENT_CONTENT_OR:
1020 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1021 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1022 xmlDumpElementContent(buf, content->c1, 1);
1023 else
1024 xmlDumpElementContent(buf, content->c1, 0);
1025 xmlBufferWriteChar(buf, " | ");
1026 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
1027 xmlDumpElementContent(buf, content->c2, 1);
1028 else
1029 xmlDumpElementContent(buf, content->c2, 0);
1030 break;
1031 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001032 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1033 "Internal: ELEMENT content corrupted invalid type\n",
1034 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001035 }
1036 if (glob)
1037 xmlBufferWriteChar(buf, ")");
1038 switch (content->ocur) {
1039 case XML_ELEMENT_CONTENT_ONCE:
1040 break;
1041 case XML_ELEMENT_CONTENT_OPT:
1042 xmlBufferWriteChar(buf, "?");
1043 break;
1044 case XML_ELEMENT_CONTENT_MULT:
1045 xmlBufferWriteChar(buf, "*");
1046 break;
1047 case XML_ELEMENT_CONTENT_PLUS:
1048 xmlBufferWriteChar(buf, "+");
1049 break;
1050 }
1051}
1052
1053/**
1054 * xmlSprintfElementContent:
1055 * @buf: an output buffer
1056 * @content: An element table
1057 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1058 *
Daniel Veillardd3d06722001-08-15 12:06:36 +00001059 * Deprecated, unsafe, use xmlSnprintfElementContent
1060 */
1061void
1062xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
1063 xmlElementContentPtr content ATTRIBUTE_UNUSED,
1064 int glob ATTRIBUTE_UNUSED) {
1065}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001066#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardd3d06722001-08-15 12:06:36 +00001067
1068/**
1069 * xmlSnprintfElementContent:
1070 * @buf: an output buffer
1071 * @size: the buffer size
1072 * @content: An element table
1073 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1074 *
Owen Taylor3473f882001-02-23 17:55:21 +00001075 * This will dump the content of the element content definition
1076 * Intended just for the debug routine
1077 */
1078void
Daniel Veillardd3d06722001-08-15 12:06:36 +00001079xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
1080 int len;
1081
Owen Taylor3473f882001-02-23 17:55:21 +00001082 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +00001083 len = strlen(buf);
1084 if (size - len < 50) {
1085 if ((size - len > 4) && (buf[len - 1] != '.'))
1086 strcat(buf, " ...");
1087 return;
1088 }
Owen Taylor3473f882001-02-23 17:55:21 +00001089 if (glob) strcat(buf, "(");
1090 switch (content->type) {
1091 case XML_ELEMENT_CONTENT_PCDATA:
1092 strcat(buf, "#PCDATA");
1093 break;
1094 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001095 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001096 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001097 strcat(buf, " ...");
1098 return;
1099 }
1100 strcat(buf, (char *) content->prefix);
1101 strcat(buf, ":");
1102 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001103 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001104 strcat(buf, " ...");
1105 return;
1106 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001107 if (content->name != NULL)
1108 strcat(buf, (char *) content->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001109 break;
1110 case XML_ELEMENT_CONTENT_SEQ:
1111 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1112 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001113 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001114 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001115 xmlSnprintfElementContent(buf, size, content->c1, 0);
1116 len = strlen(buf);
1117 if (size - len < 50) {
1118 if ((size - len > 4) && (buf[len - 1] != '.'))
1119 strcat(buf, " ...");
1120 return;
1121 }
Owen Taylor3473f882001-02-23 17:55:21 +00001122 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001123 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1124 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1125 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001126 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001127 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001128 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001129 break;
1130 case XML_ELEMENT_CONTENT_OR:
1131 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1132 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001133 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001134 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001135 xmlSnprintfElementContent(buf, size, content->c1, 0);
1136 len = strlen(buf);
1137 if (size - len < 50) {
1138 if ((size - len > 4) && (buf[len - 1] != '.'))
1139 strcat(buf, " ...");
1140 return;
1141 }
Owen Taylor3473f882001-02-23 17:55:21 +00001142 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001143 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1144 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1145 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001146 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001147 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001148 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001149 break;
1150 }
1151 if (glob)
1152 strcat(buf, ")");
1153 switch (content->ocur) {
1154 case XML_ELEMENT_CONTENT_ONCE:
1155 break;
1156 case XML_ELEMENT_CONTENT_OPT:
1157 strcat(buf, "?");
1158 break;
1159 case XML_ELEMENT_CONTENT_MULT:
1160 strcat(buf, "*");
1161 break;
1162 case XML_ELEMENT_CONTENT_PLUS:
1163 strcat(buf, "+");
1164 break;
1165 }
1166}
1167
1168/****************************************************************
1169 * *
1170 * Registration of DTD declarations *
1171 * *
1172 ****************************************************************/
1173
1174/**
1175 * xmlCreateElementTable:
1176 *
1177 * create and initialize an empty element hash table.
1178 *
1179 * Returns the xmlElementTablePtr just created or NULL in case of error.
1180 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001181static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001182xmlCreateElementTable(void) {
1183 return(xmlHashCreate(0));
1184}
1185
1186/**
1187 * xmlFreeElement:
1188 * @elem: An element
1189 *
1190 * Deallocate the memory used by an element definition
1191 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001192static void
Owen Taylor3473f882001-02-23 17:55:21 +00001193xmlFreeElement(xmlElementPtr elem) {
1194 if (elem == NULL) return;
1195 xmlUnlinkNode((xmlNodePtr) elem);
1196 xmlFreeElementContent(elem->content);
1197 if (elem->name != NULL)
1198 xmlFree((xmlChar *) elem->name);
1199 if (elem->prefix != NULL)
1200 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001201#ifdef LIBXML_REGEXP_ENABLED
1202 if (elem->contModel != NULL)
1203 xmlRegFreeRegexp(elem->contModel);
1204#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001205 xmlFree(elem);
1206}
1207
1208
1209/**
1210 * xmlAddElementDecl:
1211 * @ctxt: the validation context
1212 * @dtd: pointer to the DTD
1213 * @name: the entity name
1214 * @type: the element type
1215 * @content: the element content tree or NULL
1216 *
1217 * Register a new element declaration
1218 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001219 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001220 */
1221xmlElementPtr
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001222xmlAddElementDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1223 xmlDtdPtr dtd, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001224 xmlElementTypeVal type,
1225 xmlElementContentPtr content) {
1226 xmlElementPtr ret;
1227 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001228 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001229 xmlChar *ns, *uqname;
1230
1231 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001232 return(NULL);
1233 }
1234 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001235 return(NULL);
1236 }
1237 switch (type) {
1238 case XML_ELEMENT_TYPE_EMPTY:
1239 if (content != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001240 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1241 "xmlAddElementDecl: content != NULL for EMPTY\n",
1242 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001243 return(NULL);
1244 }
1245 break;
1246 case XML_ELEMENT_TYPE_ANY:
1247 if (content != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001248 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1249 "xmlAddElementDecl: content != NULL for ANY\n",
1250 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001251 return(NULL);
1252 }
1253 break;
1254 case XML_ELEMENT_TYPE_MIXED:
1255 if (content == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001256 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1257 "xmlAddElementDecl: content == NULL for MIXED\n",
1258 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001259 return(NULL);
1260 }
1261 break;
1262 case XML_ELEMENT_TYPE_ELEMENT:
1263 if (content == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001264 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1265 "xmlAddElementDecl: content == NULL for ELEMENT\n",
1266 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001267 return(NULL);
1268 }
1269 break;
1270 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001271 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1272 "Internal: ELEMENT decl corrupted invalid type\n",
1273 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001274 return(NULL);
1275 }
1276
1277 /*
1278 * check if name is a QName
1279 */
1280 uqname = xmlSplitQName2(name, &ns);
1281 if (uqname != NULL)
1282 name = uqname;
1283
1284 /*
1285 * Create the Element table if needed.
1286 */
1287 table = (xmlElementTablePtr) dtd->elements;
1288 if (table == NULL) {
1289 table = xmlCreateElementTable();
1290 dtd->elements = (void *) table;
1291 }
1292 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001293 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001294 "xmlAddElementDecl: Table creation failed!\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001295 if (uqname != NULL)
1296 xmlFree(uqname);
1297 if (ns != NULL)
1298 xmlFree(ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001299 return(NULL);
1300 }
1301
Daniel Veillarda10efa82001-04-18 13:09:01 +00001302 /*
1303 * lookup old attributes inserted on an undefined element in the
1304 * internal subset.
1305 */
1306 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1307 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1308 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1309 oldAttributes = ret->attributes;
1310 ret->attributes = NULL;
1311 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1312 xmlFreeElement(ret);
1313 }
Owen Taylor3473f882001-02-23 17:55:21 +00001314 }
Owen Taylor3473f882001-02-23 17:55:21 +00001315
1316 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001317 * The element may already be present if one of its attribute
1318 * was registered first
1319 */
1320 ret = xmlHashLookup2(table, name, ns);
1321 if (ret != NULL) {
1322 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001323#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001324 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001325 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001326 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001327 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1328 "Redefinition of element %s\n",
1329 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001330#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001331 if (uqname != NULL)
1332 xmlFree(uqname);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001333 if (ns != NULL)
1334 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001335 return(NULL);
1336 }
1337 } else {
1338 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1339 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001340 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001341 if (uqname != NULL)
1342 xmlFree(uqname);
1343 if (ns != NULL)
1344 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001345 return(NULL);
1346 }
1347 memset(ret, 0, sizeof(xmlElement));
1348 ret->type = XML_ELEMENT_DECL;
1349
1350 /*
1351 * fill the structure.
1352 */
1353 ret->name = xmlStrdup(name);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001354 if (ret->name == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001355 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001356 if (uqname != NULL)
1357 xmlFree(uqname);
1358 if (ns != NULL)
1359 xmlFree(ns);
1360 xmlFree(ret);
1361 return(NULL);
1362 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001363 ret->prefix = ns;
1364
1365 /*
1366 * Validity Check:
1367 * Insertion must not fail
1368 */
1369 if (xmlHashAddEntry2(table, name, ns, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001370#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001371 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001372 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001373 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001374 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1375 "Redefinition of element %s\n",
1376 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001377#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001378 xmlFreeElement(ret);
1379 if (uqname != NULL)
1380 xmlFree(uqname);
1381 return(NULL);
1382 }
William M. Brack4e52f2f2003-09-14 18:07:39 +00001383 /*
1384 * For new element, may have attributes from earlier
1385 * definition in internal subset
1386 */
1387 ret->attributes = oldAttributes;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001388 }
1389
1390 /*
1391 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001392 */
1393 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +00001394 ret->content = xmlCopyElementContent(content);
Owen Taylor3473f882001-02-23 17:55:21 +00001395
1396 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001397 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001398 */
1399 ret->parent = dtd;
1400 ret->doc = dtd->doc;
1401 if (dtd->last == NULL) {
1402 dtd->children = dtd->last = (xmlNodePtr) ret;
1403 } else {
1404 dtd->last->next = (xmlNodePtr) ret;
1405 ret->prev = dtd->last;
1406 dtd->last = (xmlNodePtr) ret;
1407 }
1408 if (uqname != NULL)
1409 xmlFree(uqname);
1410 return(ret);
1411}
1412
1413/**
1414 * xmlFreeElementTable:
1415 * @table: An element table
1416 *
1417 * Deallocate the memory used by an element hash table.
1418 */
1419void
1420xmlFreeElementTable(xmlElementTablePtr table) {
1421 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1422}
1423
Daniel Veillard652327a2003-09-29 18:02:38 +00001424#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001425/**
1426 * xmlCopyElement:
1427 * @elem: An element
1428 *
1429 * Build a copy of an element.
1430 *
1431 * Returns the new xmlElementPtr or NULL in case of error.
1432 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001433static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001434xmlCopyElement(xmlElementPtr elem) {
1435 xmlElementPtr cur;
1436
1437 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1438 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001439 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001440 return(NULL);
1441 }
1442 memset(cur, 0, sizeof(xmlElement));
1443 cur->type = XML_ELEMENT_DECL;
1444 cur->etype = elem->etype;
1445 if (elem->name != NULL)
1446 cur->name = xmlStrdup(elem->name);
1447 else
1448 cur->name = NULL;
1449 if (elem->prefix != NULL)
1450 cur->prefix = xmlStrdup(elem->prefix);
1451 else
1452 cur->prefix = NULL;
1453 cur->content = xmlCopyElementContent(elem->content);
1454 /* TODO : rebuild the attribute list on the copy */
1455 cur->attributes = NULL;
1456 return(cur);
1457}
1458
1459/**
1460 * xmlCopyElementTable:
1461 * @table: An element table
1462 *
1463 * Build a copy of an element table.
1464 *
1465 * Returns the new xmlElementTablePtr or NULL in case of error.
1466 */
1467xmlElementTablePtr
1468xmlCopyElementTable(xmlElementTablePtr table) {
1469 return((xmlElementTablePtr) xmlHashCopy(table,
1470 (xmlHashCopier) xmlCopyElement));
1471}
Daniel Veillard652327a2003-09-29 18:02:38 +00001472#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001473
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001474#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001475/**
1476 * xmlDumpElementDecl:
1477 * @buf: the XML buffer output
1478 * @elem: An element table
1479 *
1480 * This will dump the content of the element declaration as an XML
1481 * DTD definition
1482 */
1483void
1484xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1485 switch (elem->etype) {
1486 case XML_ELEMENT_TYPE_EMPTY:
1487 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001488 if (elem->prefix != NULL) {
1489 xmlBufferWriteCHAR(buf, elem->prefix);
1490 xmlBufferWriteChar(buf, ":");
1491 }
Owen Taylor3473f882001-02-23 17:55:21 +00001492 xmlBufferWriteCHAR(buf, elem->name);
1493 xmlBufferWriteChar(buf, " EMPTY>\n");
1494 break;
1495 case XML_ELEMENT_TYPE_ANY:
1496 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001497 if (elem->prefix != NULL) {
1498 xmlBufferWriteCHAR(buf, elem->prefix);
1499 xmlBufferWriteChar(buf, ":");
1500 }
Owen Taylor3473f882001-02-23 17:55:21 +00001501 xmlBufferWriteCHAR(buf, elem->name);
1502 xmlBufferWriteChar(buf, " ANY>\n");
1503 break;
1504 case XML_ELEMENT_TYPE_MIXED:
1505 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001506 if (elem->prefix != NULL) {
1507 xmlBufferWriteCHAR(buf, elem->prefix);
1508 xmlBufferWriteChar(buf, ":");
1509 }
Owen Taylor3473f882001-02-23 17:55:21 +00001510 xmlBufferWriteCHAR(buf, elem->name);
1511 xmlBufferWriteChar(buf, " ");
1512 xmlDumpElementContent(buf, elem->content, 1);
1513 xmlBufferWriteChar(buf, ">\n");
1514 break;
1515 case XML_ELEMENT_TYPE_ELEMENT:
1516 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001517 if (elem->prefix != NULL) {
1518 xmlBufferWriteCHAR(buf, elem->prefix);
1519 xmlBufferWriteChar(buf, ":");
1520 }
Owen Taylor3473f882001-02-23 17:55:21 +00001521 xmlBufferWriteCHAR(buf, elem->name);
1522 xmlBufferWriteChar(buf, " ");
1523 xmlDumpElementContent(buf, elem->content, 1);
1524 xmlBufferWriteChar(buf, ">\n");
1525 break;
1526 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001527 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1528 "Internal: ELEMENT struct corrupted invalid type\n",
1529 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001530 }
1531}
1532
1533/**
William M. Brack9e660592003-10-20 14:56:06 +00001534 * xmlDumpElementDeclScan:
1535 * @elem: An element table
1536 * @buf: the XML buffer output
1537 *
1538 * This routine is used by the hash scan function. It just reverses
1539 * the arguments.
1540 */
1541static void
1542xmlDumpElementDeclScan(xmlElementPtr elem, xmlBufferPtr buf) {
1543 xmlDumpElementDecl(buf, elem);
1544}
1545
1546/**
Owen Taylor3473f882001-02-23 17:55:21 +00001547 * xmlDumpElementTable:
1548 * @buf: the XML buffer output
1549 * @table: An element table
1550 *
1551 * This will dump the content of the element table as an XML DTD definition
1552 */
1553void
1554xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +00001555 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001556}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001557#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001558
1559/**
1560 * xmlCreateEnumeration:
1561 * @name: the enumeration name or NULL
1562 *
1563 * create and initialize an enumeration attribute node.
1564 *
1565 * Returns the xmlEnumerationPtr just created or NULL in case
1566 * of error.
1567 */
1568xmlEnumerationPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001569xmlCreateEnumeration(const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00001570 xmlEnumerationPtr ret;
1571
1572 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1573 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001574 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001575 return(NULL);
1576 }
1577 memset(ret, 0, sizeof(xmlEnumeration));
1578
1579 if (name != NULL)
1580 ret->name = xmlStrdup(name);
1581 return(ret);
1582}
1583
1584/**
1585 * xmlFreeEnumeration:
1586 * @cur: the tree to free.
1587 *
1588 * free an enumeration attribute node (recursive).
1589 */
1590void
1591xmlFreeEnumeration(xmlEnumerationPtr cur) {
1592 if (cur == NULL) return;
1593
1594 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1595
1596 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001597 xmlFree(cur);
1598}
1599
Daniel Veillard652327a2003-09-29 18:02:38 +00001600#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001601/**
1602 * xmlCopyEnumeration:
1603 * @cur: the tree to copy.
1604 *
1605 * Copy an enumeration attribute node (recursive).
1606 *
1607 * Returns the xmlEnumerationPtr just created or NULL in case
1608 * of error.
1609 */
1610xmlEnumerationPtr
1611xmlCopyEnumeration(xmlEnumerationPtr cur) {
1612 xmlEnumerationPtr ret;
1613
1614 if (cur == NULL) return(NULL);
1615 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1616
1617 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1618 else ret->next = NULL;
1619
1620 return(ret);
1621}
Daniel Veillard652327a2003-09-29 18:02:38 +00001622#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001623
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001624#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001625/**
1626 * xmlDumpEnumeration:
1627 * @buf: the XML buffer output
1628 * @enum: An enumeration
1629 *
1630 * This will dump the content of the enumeration
1631 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001632static void
Owen Taylor3473f882001-02-23 17:55:21 +00001633xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1634 if (cur == NULL) return;
1635
1636 xmlBufferWriteCHAR(buf, cur->name);
1637 if (cur->next == NULL)
1638 xmlBufferWriteChar(buf, ")");
1639 else {
1640 xmlBufferWriteChar(buf, " | ");
1641 xmlDumpEnumeration(buf, cur->next);
1642 }
1643}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001644#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001645
1646/**
1647 * xmlCreateAttributeTable:
1648 *
1649 * create and initialize an empty attribute hash table.
1650 *
1651 * Returns the xmlAttributeTablePtr just created or NULL in case
1652 * of error.
1653 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001654static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001655xmlCreateAttributeTable(void) {
1656 return(xmlHashCreate(0));
1657}
1658
Daniel Veillard4432df22003-09-28 18:58:27 +00001659#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001660/**
1661 * xmlScanAttributeDeclCallback:
1662 * @attr: the attribute decl
1663 * @list: the list to update
1664 *
1665 * Callback called by xmlScanAttributeDecl when a new attribute
1666 * has to be entered in the list.
1667 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001668static void
Owen Taylor3473f882001-02-23 17:55:21 +00001669xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001670 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001671 attr->nexth = *list;
1672 *list = attr;
1673}
1674
1675/**
1676 * xmlScanAttributeDecl:
1677 * @dtd: pointer to the DTD
1678 * @elem: the element name
1679 *
1680 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001681 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001682 *
1683 * Returns the pointer to the first attribute decl in the chain,
1684 * possibly NULL.
1685 */
1686xmlAttributePtr
1687xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1688 xmlAttributePtr ret = NULL;
1689 xmlAttributeTablePtr table;
1690
1691 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001692 return(NULL);
1693 }
1694 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001695 return(NULL);
1696 }
1697 table = (xmlAttributeTablePtr) dtd->attributes;
1698 if (table == NULL)
1699 return(NULL);
1700
1701 /* WRONG !!! */
1702 xmlHashScan3(table, NULL, NULL, elem,
1703 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1704 return(ret);
1705}
1706
1707/**
1708 * xmlScanIDAttributeDecl:
1709 * @ctxt: the validation context
1710 * @elem: the element name
1711 *
1712 * Verify that the element don't have too many ID attributes
1713 * declared.
1714 *
1715 * Returns the number of ID attributes found.
1716 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001717static int
Owen Taylor3473f882001-02-23 17:55:21 +00001718xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1719 xmlAttributePtr cur;
1720 int ret = 0;
1721
1722 if (elem == NULL) return(0);
1723 cur = elem->attributes;
1724 while (cur != NULL) {
1725 if (cur->atype == XML_ATTRIBUTE_ID) {
1726 ret ++;
1727 if (ret > 1)
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001728 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001729 "Element %s has too many ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001730 elem->name, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001731 }
1732 cur = cur->nexth;
1733 }
1734 return(ret);
1735}
Daniel Veillard4432df22003-09-28 18:58:27 +00001736#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001737
1738/**
1739 * xmlFreeAttribute:
1740 * @elem: An attribute
1741 *
1742 * Deallocate the memory used by an attribute definition
1743 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001744static void
Owen Taylor3473f882001-02-23 17:55:21 +00001745xmlFreeAttribute(xmlAttributePtr attr) {
1746 if (attr == NULL) return;
1747 xmlUnlinkNode((xmlNodePtr) attr);
1748 if (attr->tree != NULL)
1749 xmlFreeEnumeration(attr->tree);
1750 if (attr->elem != NULL)
1751 xmlFree((xmlChar *) attr->elem);
1752 if (attr->name != NULL)
1753 xmlFree((xmlChar *) attr->name);
1754 if (attr->defaultValue != NULL)
1755 xmlFree((xmlChar *) attr->defaultValue);
1756 if (attr->prefix != NULL)
1757 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001758 xmlFree(attr);
1759}
1760
1761
1762/**
1763 * xmlAddAttributeDecl:
1764 * @ctxt: the validation context
1765 * @dtd: pointer to the DTD
1766 * @elem: the element name
1767 * @name: the attribute name
1768 * @ns: the attribute namespace prefix
1769 * @type: the attribute type
1770 * @def: the attribute default type
1771 * @defaultValue: the attribute default value
1772 * @tree: if it's an enumeration, the associated list
1773 *
1774 * Register a new attribute declaration
1775 * Note that @tree becomes the ownership of the DTD
1776 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001777 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001778 */
1779xmlAttributePtr
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001780xmlAddAttributeDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1781 xmlDtdPtr dtd, const xmlChar *elem,
Owen Taylor3473f882001-02-23 17:55:21 +00001782 const xmlChar *name, const xmlChar *ns,
1783 xmlAttributeType type, xmlAttributeDefault def,
1784 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1785 xmlAttributePtr ret;
1786 xmlAttributeTablePtr table;
1787 xmlElementPtr elemDef;
1788
1789 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001790 xmlFreeEnumeration(tree);
1791 return(NULL);
1792 }
1793 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001794 xmlFreeEnumeration(tree);
1795 return(NULL);
1796 }
1797 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001798 xmlFreeEnumeration(tree);
1799 return(NULL);
1800 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001801
Daniel Veillard4432df22003-09-28 18:58:27 +00001802#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001803 /*
1804 * Check the type and possibly the default value.
1805 */
1806 switch (type) {
1807 case XML_ATTRIBUTE_CDATA:
1808 break;
1809 case XML_ATTRIBUTE_ID:
1810 break;
1811 case XML_ATTRIBUTE_IDREF:
1812 break;
1813 case XML_ATTRIBUTE_IDREFS:
1814 break;
1815 case XML_ATTRIBUTE_ENTITY:
1816 break;
1817 case XML_ATTRIBUTE_ENTITIES:
1818 break;
1819 case XML_ATTRIBUTE_NMTOKEN:
1820 break;
1821 case XML_ATTRIBUTE_NMTOKENS:
1822 break;
1823 case XML_ATTRIBUTE_ENUMERATION:
1824 break;
1825 case XML_ATTRIBUTE_NOTATION:
1826 break;
1827 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001828 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1829 "Internal: ATTRIBUTE struct corrupted invalid type\n",
1830 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001831 xmlFreeEnumeration(tree);
1832 return(NULL);
1833 }
1834 if ((defaultValue != NULL) &&
1835 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001836 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
1837 "Attribute %s of %s: invalid default value\n",
1838 elem, name, defaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001839 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001840 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001841 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001842#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001843
1844 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001845 * Check first that an attribute defined in the external subset wasn't
1846 * already defined in the internal subset
1847 */
1848 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1849 (dtd->doc->intSubset != NULL) &&
1850 (dtd->doc->intSubset->attributes != NULL)) {
1851 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1852 if (ret != NULL)
1853 return(NULL);
1854 }
1855
1856 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001857 * Create the Attribute table if needed.
1858 */
1859 table = (xmlAttributeTablePtr) dtd->attributes;
1860 if (table == NULL) {
1861 table = xmlCreateAttributeTable();
1862 dtd->attributes = (void *) table;
1863 }
1864 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001865 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001866 "xmlAddAttributeDecl: Table creation failed!\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001867 return(NULL);
1868 }
1869
1870
1871 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1872 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001873 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001874 return(NULL);
1875 }
1876 memset(ret, 0, sizeof(xmlAttribute));
1877 ret->type = XML_ATTRIBUTE_DECL;
1878
1879 /*
1880 * fill the structure.
1881 */
1882 ret->atype = type;
1883 ret->name = xmlStrdup(name);
1884 ret->prefix = xmlStrdup(ns);
1885 ret->elem = xmlStrdup(elem);
1886 ret->def = def;
1887 ret->tree = tree;
1888 if (defaultValue != NULL)
1889 ret->defaultValue = xmlStrdup(defaultValue);
1890
1891 /*
1892 * Validity Check:
1893 * Search the DTD for previous declarations of the ATTLIST
1894 */
1895 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001896#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001897 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001898 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001899 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001900 xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00001901 "Attribute %s of element %s: already defined\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001902 name, elem, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001903#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001904 xmlFreeAttribute(ret);
1905 return(NULL);
1906 }
1907
1908 /*
1909 * Validity Check:
1910 * Multiple ID per element
1911 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001912 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001913 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001914
Daniel Veillard4432df22003-09-28 18:58:27 +00001915#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001916 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001917 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001918 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
Owen Taylor3473f882001-02-23 17:55:21 +00001919 "Element %s has too may ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001920 elem, name, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00001921 ctxt->valid = 0;
1922 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001923#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardc7612992002-02-17 22:47:37 +00001924
Daniel Veillard48da9102001-08-07 01:10:10 +00001925 /*
1926 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001927 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001928 */
1929 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1930 ((ret->prefix != NULL &&
1931 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1932 ret->nexth = elemDef->attributes;
1933 elemDef->attributes = ret;
1934 } else {
1935 xmlAttributePtr tmp = elemDef->attributes;
1936
1937 while ((tmp != NULL) &&
1938 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1939 ((ret->prefix != NULL &&
1940 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1941 if (tmp->nexth == NULL)
1942 break;
1943 tmp = tmp->nexth;
1944 }
1945 if (tmp != NULL) {
1946 ret->nexth = tmp->nexth;
1947 tmp->nexth = ret;
1948 } else {
1949 ret->nexth = elemDef->attributes;
1950 elemDef->attributes = ret;
1951 }
1952 }
Owen Taylor3473f882001-02-23 17:55:21 +00001953 }
1954
1955 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001956 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001957 */
1958 ret->parent = dtd;
1959 ret->doc = dtd->doc;
1960 if (dtd->last == NULL) {
1961 dtd->children = dtd->last = (xmlNodePtr) ret;
1962 } else {
1963 dtd->last->next = (xmlNodePtr) ret;
1964 ret->prev = dtd->last;
1965 dtd->last = (xmlNodePtr) ret;
1966 }
1967 return(ret);
1968}
1969
1970/**
1971 * xmlFreeAttributeTable:
1972 * @table: An attribute table
1973 *
1974 * Deallocate the memory used by an entities hash table.
1975 */
1976void
1977xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1978 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1979}
1980
Daniel Veillard652327a2003-09-29 18:02:38 +00001981#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001982/**
1983 * xmlCopyAttribute:
1984 * @attr: An attribute
1985 *
1986 * Build a copy of an attribute.
1987 *
1988 * Returns the new xmlAttributePtr or NULL in case of error.
1989 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001990static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001991xmlCopyAttribute(xmlAttributePtr attr) {
1992 xmlAttributePtr cur;
1993
1994 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1995 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001996 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001997 return(NULL);
1998 }
1999 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00002000 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00002001 cur->atype = attr->atype;
2002 cur->def = attr->def;
2003 cur->tree = xmlCopyEnumeration(attr->tree);
2004 if (attr->elem != NULL)
2005 cur->elem = xmlStrdup(attr->elem);
2006 if (attr->name != NULL)
2007 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00002008 if (attr->prefix != NULL)
2009 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002010 if (attr->defaultValue != NULL)
2011 cur->defaultValue = xmlStrdup(attr->defaultValue);
2012 return(cur);
2013}
2014
2015/**
2016 * xmlCopyAttributeTable:
2017 * @table: An attribute table
2018 *
2019 * Build a copy of an attribute table.
2020 *
2021 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2022 */
2023xmlAttributeTablePtr
2024xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2025 return((xmlAttributeTablePtr) xmlHashCopy(table,
2026 (xmlHashCopier) xmlCopyAttribute));
2027}
Daniel Veillard652327a2003-09-29 18:02:38 +00002028#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002029
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002030#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002031/**
2032 * xmlDumpAttributeDecl:
2033 * @buf: the XML buffer output
2034 * @attr: An attribute declaration
2035 *
2036 * This will dump the content of the attribute declaration as an XML
2037 * DTD definition
2038 */
2039void
2040xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
2041 xmlBufferWriteChar(buf, "<!ATTLIST ");
2042 xmlBufferWriteCHAR(buf, attr->elem);
2043 xmlBufferWriteChar(buf, " ");
2044 if (attr->prefix != NULL) {
2045 xmlBufferWriteCHAR(buf, attr->prefix);
2046 xmlBufferWriteChar(buf, ":");
2047 }
2048 xmlBufferWriteCHAR(buf, attr->name);
2049 switch (attr->atype) {
2050 case XML_ATTRIBUTE_CDATA:
2051 xmlBufferWriteChar(buf, " CDATA");
2052 break;
2053 case XML_ATTRIBUTE_ID:
2054 xmlBufferWriteChar(buf, " ID");
2055 break;
2056 case XML_ATTRIBUTE_IDREF:
2057 xmlBufferWriteChar(buf, " IDREF");
2058 break;
2059 case XML_ATTRIBUTE_IDREFS:
2060 xmlBufferWriteChar(buf, " IDREFS");
2061 break;
2062 case XML_ATTRIBUTE_ENTITY:
2063 xmlBufferWriteChar(buf, " ENTITY");
2064 break;
2065 case XML_ATTRIBUTE_ENTITIES:
2066 xmlBufferWriteChar(buf, " ENTITIES");
2067 break;
2068 case XML_ATTRIBUTE_NMTOKEN:
2069 xmlBufferWriteChar(buf, " NMTOKEN");
2070 break;
2071 case XML_ATTRIBUTE_NMTOKENS:
2072 xmlBufferWriteChar(buf, " NMTOKENS");
2073 break;
2074 case XML_ATTRIBUTE_ENUMERATION:
2075 xmlBufferWriteChar(buf, " (");
2076 xmlDumpEnumeration(buf, attr->tree);
2077 break;
2078 case XML_ATTRIBUTE_NOTATION:
2079 xmlBufferWriteChar(buf, " NOTATION (");
2080 xmlDumpEnumeration(buf, attr->tree);
2081 break;
2082 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002083 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2084 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2085 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002086 }
2087 switch (attr->def) {
2088 case XML_ATTRIBUTE_NONE:
2089 break;
2090 case XML_ATTRIBUTE_REQUIRED:
2091 xmlBufferWriteChar(buf, " #REQUIRED");
2092 break;
2093 case XML_ATTRIBUTE_IMPLIED:
2094 xmlBufferWriteChar(buf, " #IMPLIED");
2095 break;
2096 case XML_ATTRIBUTE_FIXED:
2097 xmlBufferWriteChar(buf, " #FIXED");
2098 break;
2099 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002100 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2101 "Internal: ATTRIBUTE struct corrupted invalid def\n",
2102 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002103 }
2104 if (attr->defaultValue != NULL) {
2105 xmlBufferWriteChar(buf, " ");
2106 xmlBufferWriteQuotedString(buf, attr->defaultValue);
2107 }
2108 xmlBufferWriteChar(buf, ">\n");
2109}
2110
2111/**
William M. Brack9e660592003-10-20 14:56:06 +00002112 * xmlDumpAttributeDeclScan:
2113 * @attr: An attribute declaration
2114 * @buf: the XML buffer output
2115 *
2116 * This is used with the hash scan function - just reverses arguments
2117 */
2118static void
2119xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) {
2120 xmlDumpAttributeDecl(buf, attr);
2121}
2122
2123/**
Owen Taylor3473f882001-02-23 17:55:21 +00002124 * xmlDumpAttributeTable:
2125 * @buf: the XML buffer output
2126 * @table: An attribute table
2127 *
2128 * This will dump the content of the attribute table as an XML DTD definition
2129 */
2130void
2131xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +00002132 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002133}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002134#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002135
2136/************************************************************************
2137 * *
2138 * NOTATIONs *
2139 * *
2140 ************************************************************************/
2141/**
2142 * xmlCreateNotationTable:
2143 *
2144 * create and initialize an empty notation hash table.
2145 *
2146 * Returns the xmlNotationTablePtr just created or NULL in case
2147 * of error.
2148 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002149static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002150xmlCreateNotationTable(void) {
2151 return(xmlHashCreate(0));
2152}
2153
2154/**
2155 * xmlFreeNotation:
2156 * @not: A notation
2157 *
2158 * Deallocate the memory used by an notation definition
2159 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002160static void
Owen Taylor3473f882001-02-23 17:55:21 +00002161xmlFreeNotation(xmlNotationPtr nota) {
2162 if (nota == NULL) return;
2163 if (nota->name != NULL)
2164 xmlFree((xmlChar *) nota->name);
2165 if (nota->PublicID != NULL)
2166 xmlFree((xmlChar *) nota->PublicID);
2167 if (nota->SystemID != NULL)
2168 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002169 xmlFree(nota);
2170}
2171
2172
2173/**
2174 * xmlAddNotationDecl:
2175 * @dtd: pointer to the DTD
2176 * @ctxt: the validation context
2177 * @name: the entity name
2178 * @PublicID: the public identifier or NULL
2179 * @SystemID: the system identifier or NULL
2180 *
2181 * Register a new notation declaration
2182 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002183 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002184 */
2185xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002186xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002187 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002188 const xmlChar *PublicID, const xmlChar *SystemID) {
2189 xmlNotationPtr ret;
2190 xmlNotationTablePtr table;
2191
2192 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002193 return(NULL);
2194 }
2195 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002196 return(NULL);
2197 }
2198 if ((PublicID == NULL) && (SystemID == NULL)) {
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002199 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002200 }
2201
2202 /*
2203 * Create the Notation table if needed.
2204 */
2205 table = (xmlNotationTablePtr) dtd->notations;
2206 if (table == NULL)
2207 dtd->notations = table = xmlCreateNotationTable();
2208 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002209 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002210 "xmlAddNotationDecl: Table creation failed!\n");
2211 return(NULL);
2212 }
2213
2214 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2215 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002216 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002217 return(NULL);
2218 }
2219 memset(ret, 0, sizeof(xmlNotation));
2220
2221 /*
2222 * fill the structure.
2223 */
2224 ret->name = xmlStrdup(name);
2225 if (SystemID != NULL)
2226 ret->SystemID = xmlStrdup(SystemID);
2227 if (PublicID != NULL)
2228 ret->PublicID = xmlStrdup(PublicID);
2229
2230 /*
2231 * Validity Check:
2232 * Check the DTD for previous declarations of the ATTLIST
2233 */
2234 if (xmlHashAddEntry(table, name, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002235#ifdef LIBXML_VALID_ENABLED
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002236 xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
2237 "xmlAddNotationDecl: %s already defined\n",
2238 (const char *) name);
Daniel Veillard4432df22003-09-28 18:58:27 +00002239#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002240 xmlFreeNotation(ret);
2241 return(NULL);
2242 }
2243 return(ret);
2244}
2245
2246/**
2247 * xmlFreeNotationTable:
2248 * @table: An notation table
2249 *
2250 * Deallocate the memory used by an entities hash table.
2251 */
2252void
2253xmlFreeNotationTable(xmlNotationTablePtr table) {
2254 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2255}
2256
Daniel Veillard652327a2003-09-29 18:02:38 +00002257#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002258/**
2259 * xmlCopyNotation:
2260 * @nota: A notation
2261 *
2262 * Build a copy of a notation.
2263 *
2264 * Returns the new xmlNotationPtr or NULL in case of error.
2265 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002266static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002267xmlCopyNotation(xmlNotationPtr nota) {
2268 xmlNotationPtr cur;
2269
2270 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2271 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002272 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002273 return(NULL);
2274 }
2275 if (nota->name != NULL)
2276 cur->name = xmlStrdup(nota->name);
2277 else
2278 cur->name = NULL;
2279 if (nota->PublicID != NULL)
2280 cur->PublicID = xmlStrdup(nota->PublicID);
2281 else
2282 cur->PublicID = NULL;
2283 if (nota->SystemID != NULL)
2284 cur->SystemID = xmlStrdup(nota->SystemID);
2285 else
2286 cur->SystemID = NULL;
2287 return(cur);
2288}
2289
2290/**
2291 * xmlCopyNotationTable:
2292 * @table: A notation table
2293 *
2294 * Build a copy of a notation table.
2295 *
2296 * Returns the new xmlNotationTablePtr or NULL in case of error.
2297 */
2298xmlNotationTablePtr
2299xmlCopyNotationTable(xmlNotationTablePtr table) {
2300 return((xmlNotationTablePtr) xmlHashCopy(table,
2301 (xmlHashCopier) xmlCopyNotation));
2302}
Daniel Veillard652327a2003-09-29 18:02:38 +00002303#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002304
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002305#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002306/**
2307 * xmlDumpNotationDecl:
2308 * @buf: the XML buffer output
2309 * @nota: A notation declaration
2310 *
2311 * This will dump the content the notation declaration as an XML DTD definition
2312 */
2313void
2314xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2315 xmlBufferWriteChar(buf, "<!NOTATION ");
2316 xmlBufferWriteCHAR(buf, nota->name);
2317 if (nota->PublicID != NULL) {
2318 xmlBufferWriteChar(buf, " PUBLIC ");
2319 xmlBufferWriteQuotedString(buf, nota->PublicID);
2320 if (nota->SystemID != NULL) {
2321 xmlBufferWriteChar(buf, " ");
2322 xmlBufferWriteCHAR(buf, nota->SystemID);
2323 }
2324 } else {
2325 xmlBufferWriteChar(buf, " SYSTEM ");
2326 xmlBufferWriteCHAR(buf, nota->SystemID);
2327 }
2328 xmlBufferWriteChar(buf, " >\n");
2329}
2330
2331/**
William M. Brack9e660592003-10-20 14:56:06 +00002332 * xmlDumpNotationDeclScan:
2333 * @nota: A notation declaration
2334 * @buf: the XML buffer output
2335 *
2336 * This is called with the hash scan function, and just reverses args
2337 */
2338static void
2339xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) {
2340 xmlDumpNotationDecl(buf, nota);
2341}
2342
2343/**
Owen Taylor3473f882001-02-23 17:55:21 +00002344 * xmlDumpNotationTable:
2345 * @buf: the XML buffer output
2346 * @table: A notation table
2347 *
2348 * This will dump the content of the notation table as an XML DTD definition
2349 */
2350void
2351xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
William M. Brack9e660592003-10-20 14:56:06 +00002352 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002353}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002354#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002355
2356/************************************************************************
2357 * *
2358 * IDs *
2359 * *
2360 ************************************************************************/
2361/**
2362 * xmlCreateIDTable:
2363 *
2364 * create and initialize an empty id hash table.
2365 *
2366 * Returns the xmlIDTablePtr just created or NULL in case
2367 * of error.
2368 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002369static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002370xmlCreateIDTable(void) {
2371 return(xmlHashCreate(0));
2372}
2373
2374/**
2375 * xmlFreeID:
2376 * @not: A id
2377 *
2378 * Deallocate the memory used by an id definition
2379 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002380static void
Owen Taylor3473f882001-02-23 17:55:21 +00002381xmlFreeID(xmlIDPtr id) {
2382 if (id == NULL) return;
2383 if (id->value != NULL)
2384 xmlFree((xmlChar *) id->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002385 if (id->name != NULL)
2386 xmlFree((xmlChar *) id->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002387 xmlFree(id);
2388}
2389
2390/**
2391 * xmlAddID:
2392 * @ctxt: the validation context
2393 * @doc: pointer to the document
2394 * @value: the value name
2395 * @attr: the attribute holding the ID
2396 *
2397 * Register a new id declaration
2398 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002399 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002400 */
2401xmlIDPtr
2402xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2403 xmlAttrPtr attr) {
2404 xmlIDPtr ret;
2405 xmlIDTablePtr table;
2406
2407 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002408 return(NULL);
2409 }
2410 if (value == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002411 return(NULL);
2412 }
2413 if (attr == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002414 return(NULL);
2415 }
2416
2417 /*
2418 * Create the ID table if needed.
2419 */
2420 table = (xmlIDTablePtr) doc->ids;
2421 if (table == NULL)
2422 doc->ids = table = xmlCreateIDTable();
2423 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002424 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002425 "xmlAddID: Table creation failed!\n");
2426 return(NULL);
2427 }
2428
2429 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2430 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002431 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002432 return(NULL);
2433 }
2434
2435 /*
2436 * fill the structure.
2437 */
2438 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002439 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2440 /*
2441 * Operating in streaming mode, attr is gonna disapear
2442 */
2443 ret->name = xmlStrdup(attr->name);
2444 ret->attr = NULL;
2445 } else {
2446 ret->attr = attr;
2447 ret->name = NULL;
2448 }
2449 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002450
2451 if (xmlHashAddEntry(table, value, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002452#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002453 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002454 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002455 */
Daniel Veillard76575762002-09-05 14:21:15 +00002456 if (ctxt != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002457 xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2458 "ID %s already defined\n",
2459 value, NULL, NULL);
Daniel Veillard76575762002-09-05 14:21:15 +00002460 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002461#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002462 xmlFreeID(ret);
2463 return(NULL);
2464 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002465 if (attr != NULL)
2466 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002467 return(ret);
2468}
2469
2470/**
2471 * xmlFreeIDTable:
2472 * @table: An id table
2473 *
2474 * Deallocate the memory used by an ID hash table.
2475 */
2476void
2477xmlFreeIDTable(xmlIDTablePtr table) {
2478 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2479}
2480
2481/**
2482 * xmlIsID:
2483 * @doc: the document
2484 * @elem: the element carrying the attribute
2485 * @attr: the attribute
2486 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002487 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002488 * then this is done if DTD loading has been requested. In the case
2489 * of HTML documents parsed with the HTML parser, then ID detection is
2490 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002491 *
2492 * Returns 0 or 1 depending on the lookup result
2493 */
2494int
2495xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2496 if (doc == NULL) return(0);
2497 if (attr == NULL) return(0);
2498 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2499 return(0);
2500 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillard9ba8e382003-10-28 21:31:45 +00002501 if (((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2502 (xmlStrEqual(BAD_CAST "name", attr->name))) &&
2503 ((elem != NULL) && (!xmlStrEqual(elem->name, BAD_CAST "input"))))
Owen Taylor3473f882001-02-23 17:55:21 +00002504 return(1);
2505 return(0);
2506 } else {
2507 xmlAttributePtr attrDecl;
2508
2509 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002510 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00002511 xmlChar fn[50];
Daniel Veillard37f961d2002-07-06 17:53:56 +00002512 xmlChar *fullname;
Daniel Veillardc00cda82003-04-07 10:22:39 +00002513
2514 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002515 if (fullname == NULL)
2516 return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002517 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2518 attr->name);
2519 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2520 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2521 attr->name);
Daniel Veillardc00cda82003-04-07 10:22:39 +00002522 if ((fullname != fn) && (fullname != elem->name))
2523 xmlFree(fullname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002524 } else {
2525 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2526 attr->name);
2527 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2528 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2529 attr->name);
2530 }
Owen Taylor3473f882001-02-23 17:55:21 +00002531
2532 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2533 return(1);
2534 }
2535 return(0);
2536}
2537
2538/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002539 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002540 * @doc: the document
2541 * @attr: the attribute
2542 *
2543 * Remove the given attribute from the ID table maintained internally.
2544 *
2545 * Returns -1 if the lookup failed and 0 otherwise
2546 */
2547int
2548xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2549 xmlAttrPtr cur;
2550 xmlIDTablePtr table;
2551 xmlChar *ID;
2552
2553 if (doc == NULL) return(-1);
2554 if (attr == NULL) return(-1);
2555 table = (xmlIDTablePtr) doc->ids;
2556 if (table == NULL)
2557 return(-1);
2558
2559 if (attr == NULL)
2560 return(-1);
2561 ID = xmlNodeListGetString(doc, attr->children, 1);
2562 if (ID == NULL)
2563 return(-1);
2564 cur = xmlHashLookup(table, ID);
2565 if (cur != attr) {
2566 xmlFree(ID);
2567 return(-1);
2568 }
2569 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
2570 xmlFree(ID);
2571 return(0);
2572}
2573
2574/**
2575 * xmlGetID:
2576 * @doc: pointer to the document
2577 * @ID: the ID value
2578 *
2579 * Search the attribute declaring the given ID
2580 *
2581 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2582 */
2583xmlAttrPtr
2584xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2585 xmlIDTablePtr table;
2586 xmlIDPtr id;
2587
2588 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002589 return(NULL);
2590 }
2591
2592 if (ID == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002593 return(NULL);
2594 }
2595
2596 table = (xmlIDTablePtr) doc->ids;
2597 if (table == NULL)
2598 return(NULL);
2599
2600 id = xmlHashLookup(table, ID);
2601 if (id == NULL)
2602 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002603 if (id->attr == NULL) {
2604 /*
2605 * We are operating on a stream, return a well known reference
2606 * since the attribute node doesn't exist anymore
2607 */
2608 return((xmlAttrPtr) doc);
2609 }
Owen Taylor3473f882001-02-23 17:55:21 +00002610 return(id->attr);
2611}
2612
2613/************************************************************************
2614 * *
2615 * Refs *
2616 * *
2617 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002618typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002619{
2620 xmlListPtr l;
2621 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002622} xmlRemoveMemo;
2623
2624typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2625
2626typedef struct xmlValidateMemo_t
2627{
2628 xmlValidCtxtPtr ctxt;
2629 const xmlChar *name;
2630} xmlValidateMemo;
2631
2632typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002633
2634/**
2635 * xmlCreateRefTable:
2636 *
2637 * create and initialize an empty ref hash table.
2638 *
2639 * Returns the xmlRefTablePtr just created or NULL in case
2640 * of error.
2641 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002642static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002643xmlCreateRefTable(void) {
2644 return(xmlHashCreate(0));
2645}
2646
2647/**
2648 * xmlFreeRef:
2649 * @lk: A list link
2650 *
2651 * Deallocate the memory used by a ref definition
2652 */
2653static void
2654xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002655 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2656 if (ref == NULL) return;
2657 if (ref->value != NULL)
2658 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002659 if (ref->name != NULL)
2660 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002661 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002662}
2663
2664/**
2665 * xmlFreeRefList:
2666 * @list_ref: A list of references.
2667 *
2668 * Deallocate the memory used by a list of references
2669 */
2670static void
2671xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002672 if (list_ref == NULL) return;
2673 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002674}
2675
2676/**
2677 * xmlWalkRemoveRef:
2678 * @data: Contents of current link
2679 * @user: Value supplied by the user
2680 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002681 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002682 */
2683static int
2684xmlWalkRemoveRef(const void *data, const void *user)
2685{
Daniel Veillard37721922001-05-04 15:21:12 +00002686 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2687 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2688 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002689
Daniel Veillard37721922001-05-04 15:21:12 +00002690 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2691 xmlListRemoveFirst(ref_list, (void *)data);
2692 return 0;
2693 }
2694 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002695}
2696
2697/**
2698 * xmlAddRef:
2699 * @ctxt: the validation context
2700 * @doc: pointer to the document
2701 * @value: the value name
2702 * @attr: the attribute holding the Ref
2703 *
2704 * Register a new ref declaration
2705 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002706 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002707 */
2708xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002709xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002710 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002711 xmlRefPtr ret;
2712 xmlRefTablePtr table;
2713 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002714
Daniel Veillard37721922001-05-04 15:21:12 +00002715 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002716 return(NULL);
2717 }
2718 if (value == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002719 return(NULL);
2720 }
2721 if (attr == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002722 return(NULL);
2723 }
Owen Taylor3473f882001-02-23 17:55:21 +00002724
Daniel Veillard37721922001-05-04 15:21:12 +00002725 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002726 * Create the Ref table if needed.
2727 */
Daniel Veillard37721922001-05-04 15:21:12 +00002728 table = (xmlRefTablePtr) doc->refs;
2729 if (table == NULL)
2730 doc->refs = table = xmlCreateRefTable();
2731 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002732 xmlVErrMemory(ctxt,
Daniel Veillard37721922001-05-04 15:21:12 +00002733 "xmlAddRef: Table creation failed!\n");
2734 return(NULL);
2735 }
Owen Taylor3473f882001-02-23 17:55:21 +00002736
Daniel Veillard37721922001-05-04 15:21:12 +00002737 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2738 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002739 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard37721922001-05-04 15:21:12 +00002740 return(NULL);
2741 }
Owen Taylor3473f882001-02-23 17:55:21 +00002742
Daniel Veillard37721922001-05-04 15:21:12 +00002743 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002744 * fill the structure.
2745 */
Daniel Veillard37721922001-05-04 15:21:12 +00002746 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002747 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2748 /*
2749 * Operating in streaming mode, attr is gonna disapear
2750 */
2751 ret->name = xmlStrdup(attr->name);
2752 ret->attr = NULL;
2753 } else {
2754 ret->name = NULL;
2755 ret->attr = attr;
2756 }
2757 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002758
Daniel Veillard37721922001-05-04 15:21:12 +00002759 /* To add a reference :-
2760 * References are maintained as a list of references,
2761 * Lookup the entry, if no entry create new nodelist
2762 * Add the owning node to the NodeList
2763 * Return the ref
2764 */
Owen Taylor3473f882001-02-23 17:55:21 +00002765
Daniel Veillard37721922001-05-04 15:21:12 +00002766 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2767 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002768 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2769 "xmlAddRef: Reference list creation failed!\n",
2770 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002771 return(NULL);
2772 }
2773 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2774 xmlListDelete(ref_list);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002775 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2776 "xmlAddRef: Reference list insertion failed!\n",
2777 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002778 return(NULL);
2779 }
2780 }
2781 xmlListInsert(ref_list, ret);
2782 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002783}
2784
2785/**
2786 * xmlFreeRefTable:
2787 * @table: An ref table
2788 *
2789 * Deallocate the memory used by an Ref hash table.
2790 */
2791void
2792xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002793 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002794}
2795
2796/**
2797 * xmlIsRef:
2798 * @doc: the document
2799 * @elem: the element carrying the attribute
2800 * @attr: the attribute
2801 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002802 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002803 * then this is simple, otherwise we use an heuristic: name Ref (upper
2804 * or lowercase).
2805 *
2806 * Returns 0 or 1 depending on the lookup result
2807 */
2808int
2809xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002810 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2811 return(0);
2812 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2813 /* TODO @@@ */
2814 return(0);
2815 } else {
2816 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002817
Daniel Veillard37721922001-05-04 15:21:12 +00002818 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2819 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2820 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2821 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002822
Daniel Veillard37721922001-05-04 15:21:12 +00002823 if ((attrDecl != NULL) &&
2824 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2825 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2826 return(1);
2827 }
2828 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002829}
2830
2831/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002832 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00002833 * @doc: the document
2834 * @attr: the attribute
2835 *
2836 * Remove the given attribute from the Ref table maintained internally.
2837 *
2838 * Returns -1 if the lookup failed and 0 otherwise
2839 */
2840int
2841xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002842 xmlListPtr ref_list;
2843 xmlRefTablePtr table;
2844 xmlChar *ID;
2845 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002846
Daniel Veillard37721922001-05-04 15:21:12 +00002847 if (doc == NULL) return(-1);
2848 if (attr == NULL) return(-1);
2849 table = (xmlRefTablePtr) doc->refs;
2850 if (table == NULL)
2851 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002852
Daniel Veillard37721922001-05-04 15:21:12 +00002853 if (attr == NULL)
2854 return(-1);
2855 ID = xmlNodeListGetString(doc, attr->children, 1);
2856 if (ID == NULL)
2857 return(-1);
2858 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002859
Daniel Veillard37721922001-05-04 15:21:12 +00002860 if(ref_list == NULL) {
2861 xmlFree(ID);
2862 return (-1);
2863 }
2864 /* At this point, ref_list refers to a list of references which
2865 * have the same key as the supplied attr. Our list of references
2866 * is ordered by reference address and we don't have that information
2867 * here to use when removing. We'll have to walk the list and
2868 * check for a matching attribute, when we find one stop the walk
2869 * and remove the entry.
2870 * The list is ordered by reference, so that means we don't have the
2871 * key. Passing the list and the reference to the walker means we
2872 * will have enough data to be able to remove the entry.
2873 */
2874 target.l = ref_list;
2875 target.ap = attr;
2876
2877 /* Remove the supplied attr from our list */
2878 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002879
Daniel Veillard37721922001-05-04 15:21:12 +00002880 /*If the list is empty then remove the list entry in the hash */
2881 if (xmlListEmpty(ref_list))
2882 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2883 xmlFreeRefList);
2884 xmlFree(ID);
2885 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002886}
2887
2888/**
2889 * xmlGetRefs:
2890 * @doc: pointer to the document
2891 * @ID: the ID value
2892 *
2893 * Find the set of references for the supplied ID.
2894 *
2895 * Returns NULL if not found, otherwise node set for the ID.
2896 */
2897xmlListPtr
2898xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002899 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002900
Daniel Veillard37721922001-05-04 15:21:12 +00002901 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002902 return(NULL);
2903 }
Owen Taylor3473f882001-02-23 17:55:21 +00002904
Daniel Veillard37721922001-05-04 15:21:12 +00002905 if (ID == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002906 return(NULL);
2907 }
Owen Taylor3473f882001-02-23 17:55:21 +00002908
Daniel Veillard37721922001-05-04 15:21:12 +00002909 table = (xmlRefTablePtr) doc->refs;
2910 if (table == NULL)
2911 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002912
Daniel Veillard37721922001-05-04 15:21:12 +00002913 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002914}
2915
2916/************************************************************************
2917 * *
2918 * Routines for validity checking *
2919 * *
2920 ************************************************************************/
2921
2922/**
2923 * xmlGetDtdElementDesc:
2924 * @dtd: a pointer to the DtD to search
2925 * @name: the element name
2926 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002927 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002928 *
2929 * returns the xmlElementPtr if found or NULL
2930 */
2931
2932xmlElementPtr
2933xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2934 xmlElementTablePtr table;
2935 xmlElementPtr cur;
2936 xmlChar *uqname = NULL, *prefix = NULL;
2937
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002938 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002939 if (dtd->elements == NULL)
2940 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002941 table = (xmlElementTablePtr) dtd->elements;
2942
2943 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002944 if (uqname != NULL)
2945 name = uqname;
2946 cur = xmlHashLookup2(table, name, prefix);
2947 if (prefix != NULL) xmlFree(prefix);
2948 if (uqname != NULL) xmlFree(uqname);
2949 return(cur);
2950}
2951/**
2952 * xmlGetDtdElementDesc2:
2953 * @dtd: a pointer to the DtD to search
2954 * @name: the element name
2955 * @create: create an empty description if not found
2956 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002957 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002958 *
2959 * returns the xmlElementPtr if found or NULL
2960 */
2961
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002962static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002963xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2964 xmlElementTablePtr table;
2965 xmlElementPtr cur;
2966 xmlChar *uqname = NULL, *prefix = NULL;
2967
2968 if (dtd == NULL) return(NULL);
2969 if (dtd->elements == NULL) {
2970 if (!create)
2971 return(NULL);
2972 /*
2973 * Create the Element table if needed.
2974 */
2975 table = (xmlElementTablePtr) dtd->elements;
2976 if (table == NULL) {
2977 table = xmlCreateElementTable();
2978 dtd->elements = (void *) table;
2979 }
2980 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002981 xmlVErrMemory(NULL, "element table allocation failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002982 return(NULL);
2983 }
2984 }
2985 table = (xmlElementTablePtr) dtd->elements;
2986
2987 uqname = xmlSplitQName2(name, &prefix);
2988 if (uqname != NULL)
2989 name = uqname;
2990 cur = xmlHashLookup2(table, name, prefix);
2991 if ((cur == NULL) && (create)) {
2992 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2993 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002994 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002995 return(NULL);
2996 }
2997 memset(cur, 0, sizeof(xmlElement));
2998 cur->type = XML_ELEMENT_DECL;
2999
3000 /*
3001 * fill the structure.
3002 */
3003 cur->name = xmlStrdup(name);
3004 cur->prefix = xmlStrdup(prefix);
3005 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3006
3007 xmlHashAddEntry2(table, name, prefix, cur);
3008 }
3009 if (prefix != NULL) xmlFree(prefix);
3010 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00003011 return(cur);
3012}
3013
3014/**
3015 * xmlGetDtdQElementDesc:
3016 * @dtd: a pointer to the DtD to search
3017 * @name: the element name
3018 * @prefix: the element namespace prefix
3019 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003020 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003021 *
3022 * returns the xmlElementPtr if found or NULL
3023 */
3024
Daniel Veillard48da9102001-08-07 01:10:10 +00003025xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00003026xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3027 const xmlChar *prefix) {
3028 xmlElementTablePtr table;
3029
3030 if (dtd == NULL) return(NULL);
3031 if (dtd->elements == NULL) return(NULL);
3032 table = (xmlElementTablePtr) dtd->elements;
3033
3034 return(xmlHashLookup2(table, name, prefix));
3035}
3036
3037/**
3038 * xmlGetDtdAttrDesc:
3039 * @dtd: a pointer to the DtD to search
3040 * @elem: the element name
3041 * @name: the attribute name
3042 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003043 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003044 * this element.
3045 *
3046 * returns the xmlAttributePtr if found or NULL
3047 */
3048
3049xmlAttributePtr
3050xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3051 xmlAttributeTablePtr table;
3052 xmlAttributePtr cur;
3053 xmlChar *uqname = NULL, *prefix = NULL;
3054
3055 if (dtd == NULL) return(NULL);
3056 if (dtd->attributes == NULL) return(NULL);
3057
3058 table = (xmlAttributeTablePtr) dtd->attributes;
3059 if (table == NULL)
3060 return(NULL);
3061
3062 uqname = xmlSplitQName2(name, &prefix);
3063
3064 if (uqname != NULL) {
3065 cur = xmlHashLookup3(table, uqname, prefix, elem);
3066 if (prefix != NULL) xmlFree(prefix);
3067 if (uqname != NULL) xmlFree(uqname);
3068 } else
3069 cur = xmlHashLookup3(table, name, NULL, elem);
3070 return(cur);
3071}
3072
3073/**
3074 * xmlGetDtdQAttrDesc:
3075 * @dtd: a pointer to the DtD to search
3076 * @elem: the element name
3077 * @name: the attribute name
3078 * @prefix: the attribute namespace prefix
3079 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003080 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003081 * this element.
3082 *
3083 * returns the xmlAttributePtr if found or NULL
3084 */
3085
Daniel Veillard48da9102001-08-07 01:10:10 +00003086xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00003087xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3088 const xmlChar *prefix) {
3089 xmlAttributeTablePtr table;
3090
3091 if (dtd == NULL) return(NULL);
3092 if (dtd->attributes == NULL) return(NULL);
3093 table = (xmlAttributeTablePtr) dtd->attributes;
3094
3095 return(xmlHashLookup3(table, name, prefix, elem));
3096}
3097
3098/**
3099 * xmlGetDtdNotationDesc:
3100 * @dtd: a pointer to the DtD to search
3101 * @name: the notation name
3102 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003103 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00003104 *
3105 * returns the xmlNotationPtr if found or NULL
3106 */
3107
3108xmlNotationPtr
3109xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3110 xmlNotationTablePtr table;
3111
3112 if (dtd == NULL) return(NULL);
3113 if (dtd->notations == NULL) return(NULL);
3114 table = (xmlNotationTablePtr) dtd->notations;
3115
3116 return(xmlHashLookup(table, name));
3117}
3118
Daniel Veillard4432df22003-09-28 18:58:27 +00003119#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003120/**
3121 * xmlValidateNotationUse:
3122 * @ctxt: the validation context
3123 * @doc: the document
3124 * @notationName: the notation name to check
3125 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003126 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00003127 * - [ VC: Notation Declared ]
3128 *
3129 * returns 1 if valid or 0 otherwise
3130 */
3131
3132int
3133xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3134 const xmlChar *notationName) {
3135 xmlNotationPtr notaDecl;
3136 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3137
3138 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3139 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3140 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3141
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003142 if ((notaDecl == NULL) && (ctxt != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003143 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3144 "NOTATION %s is not declared\n",
3145 notationName, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003146 return(0);
3147 }
3148 return(1);
3149}
Daniel Veillard4432df22003-09-28 18:58:27 +00003150#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003151
3152/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003153 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00003154 * @doc: the document
3155 * @name: the element name
3156 *
3157 * Search in the DtDs whether an element accept Mixed content (or ANY)
3158 * basically if it is supposed to accept text childs
3159 *
3160 * returns 0 if no, 1 if yes, and -1 if no element description is available
3161 */
3162
3163int
3164xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3165 xmlElementPtr elemDecl;
3166
3167 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3168
3169 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3170 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3171 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3172 if (elemDecl == NULL) return(-1);
3173 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00003174 case XML_ELEMENT_TYPE_UNDEFINED:
3175 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003176 case XML_ELEMENT_TYPE_ELEMENT:
3177 return(0);
3178 case XML_ELEMENT_TYPE_EMPTY:
3179 /*
3180 * return 1 for EMPTY since we want VC error to pop up
3181 * on <empty> </empty> for example
3182 */
3183 case XML_ELEMENT_TYPE_ANY:
3184 case XML_ELEMENT_TYPE_MIXED:
3185 return(1);
3186 }
3187 return(1);
3188}
3189
Daniel Veillard4432df22003-09-28 18:58:27 +00003190#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003191/**
3192 * xmlValidateNameValue:
3193 * @value: an Name value
3194 *
3195 * Validate that the given value match Name production
3196 *
3197 * returns 1 if valid or 0 otherwise
3198 */
3199
Daniel Veillard9b731d72002-04-14 12:56:08 +00003200int
Owen Taylor3473f882001-02-23 17:55:21 +00003201xmlValidateNameValue(const xmlChar *value) {
3202 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003203 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003204
3205 if (value == NULL) return(0);
3206 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003207 val = xmlStringCurrentChar(NULL, cur, &len);
3208 cur += len;
3209 if (!IS_LETTER(val) && (val != '_') &&
3210 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003211 return(0);
3212 }
3213
Daniel Veillardd8224e02002-01-13 15:43:22 +00003214 val = xmlStringCurrentChar(NULL, cur, &len);
3215 cur += len;
3216 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3217 (val == '.') || (val == '-') ||
3218 (val == '_') || (val == ':') ||
3219 (IS_COMBINING(val)) ||
3220 (IS_EXTENDER(val))) {
3221 val = xmlStringCurrentChar(NULL, cur, &len);
3222 cur += len;
3223 }
Owen Taylor3473f882001-02-23 17:55:21 +00003224
Daniel Veillardd8224e02002-01-13 15:43:22 +00003225 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003226
3227 return(1);
3228}
3229
3230/**
3231 * xmlValidateNamesValue:
3232 * @value: an Names value
3233 *
3234 * Validate that the given value match Names production
3235 *
3236 * returns 1 if valid or 0 otherwise
3237 */
3238
Daniel Veillard9b731d72002-04-14 12:56:08 +00003239int
Owen Taylor3473f882001-02-23 17:55:21 +00003240xmlValidateNamesValue(const xmlChar *value) {
3241 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003242 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003243
3244 if (value == NULL) return(0);
3245 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003246 val = xmlStringCurrentChar(NULL, cur, &len);
3247 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003248
Daniel Veillardd8224e02002-01-13 15:43:22 +00003249 if (!IS_LETTER(val) && (val != '_') &&
3250 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003251 return(0);
3252 }
3253
Daniel Veillardd8224e02002-01-13 15:43:22 +00003254 val = xmlStringCurrentChar(NULL, cur, &len);
3255 cur += len;
3256 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3257 (val == '.') || (val == '-') ||
3258 (val == '_') || (val == ':') ||
3259 (IS_COMBINING(val)) ||
3260 (IS_EXTENDER(val))) {
3261 val = xmlStringCurrentChar(NULL, cur, &len);
3262 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003263 }
3264
Daniel Veillardd8224e02002-01-13 15:43:22 +00003265 while (IS_BLANK(val)) {
3266 while (IS_BLANK(val)) {
3267 val = xmlStringCurrentChar(NULL, cur, &len);
3268 cur += len;
3269 }
3270
3271 if (!IS_LETTER(val) && (val != '_') &&
3272 (val != ':')) {
3273 return(0);
3274 }
3275 val = xmlStringCurrentChar(NULL, cur, &len);
3276 cur += len;
3277
3278 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3279 (val == '.') || (val == '-') ||
3280 (val == '_') || (val == ':') ||
3281 (IS_COMBINING(val)) ||
3282 (IS_EXTENDER(val))) {
3283 val = xmlStringCurrentChar(NULL, cur, &len);
3284 cur += len;
3285 }
3286 }
3287
3288 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003289
3290 return(1);
3291}
3292
3293/**
3294 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003295 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003296 *
3297 * Validate that the given value match Nmtoken production
3298 *
3299 * [ VC: Name Token ]
3300 *
3301 * returns 1 if valid or 0 otherwise
3302 */
3303
Daniel Veillard9b731d72002-04-14 12:56:08 +00003304int
Owen Taylor3473f882001-02-23 17:55:21 +00003305xmlValidateNmtokenValue(const xmlChar *value) {
3306 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003307 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003308
3309 if (value == NULL) return(0);
3310 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003311 val = xmlStringCurrentChar(NULL, cur, &len);
3312 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003313
Daniel Veillardd8224e02002-01-13 15:43:22 +00003314 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3315 (val != '.') && (val != '-') &&
3316 (val != '_') && (val != ':') &&
3317 (!IS_COMBINING(val)) &&
3318 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003319 return(0);
3320
Daniel Veillardd8224e02002-01-13 15:43:22 +00003321 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3322 (val == '.') || (val == '-') ||
3323 (val == '_') || (val == ':') ||
3324 (IS_COMBINING(val)) ||
3325 (IS_EXTENDER(val))) {
3326 val = xmlStringCurrentChar(NULL, cur, &len);
3327 cur += len;
3328 }
Owen Taylor3473f882001-02-23 17:55:21 +00003329
Daniel Veillardd8224e02002-01-13 15:43:22 +00003330 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003331
3332 return(1);
3333}
3334
3335/**
3336 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003337 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003338 *
3339 * Validate that the given value match Nmtokens production
3340 *
3341 * [ VC: Name Token ]
3342 *
3343 * returns 1 if valid or 0 otherwise
3344 */
3345
Daniel Veillard9b731d72002-04-14 12:56:08 +00003346int
Owen Taylor3473f882001-02-23 17:55:21 +00003347xmlValidateNmtokensValue(const xmlChar *value) {
3348 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003349 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003350
3351 if (value == NULL) return(0);
3352 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003353 val = xmlStringCurrentChar(NULL, cur, &len);
3354 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003355
Daniel Veillardd8224e02002-01-13 15:43:22 +00003356 while (IS_BLANK(val)) {
3357 val = xmlStringCurrentChar(NULL, cur, &len);
3358 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003359 }
3360
Daniel Veillardd8224e02002-01-13 15:43:22 +00003361 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3362 (val != '.') && (val != '-') &&
3363 (val != '_') && (val != ':') &&
3364 (!IS_COMBINING(val)) &&
3365 (!IS_EXTENDER(val)))
3366 return(0);
3367
3368 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3369 (val == '.') || (val == '-') ||
3370 (val == '_') || (val == ':') ||
3371 (IS_COMBINING(val)) ||
3372 (IS_EXTENDER(val))) {
3373 val = xmlStringCurrentChar(NULL, cur, &len);
3374 cur += len;
3375 }
3376
3377 while (IS_BLANK(val)) {
3378 while (IS_BLANK(val)) {
3379 val = xmlStringCurrentChar(NULL, cur, &len);
3380 cur += len;
3381 }
3382 if (val == 0) return(1);
3383
3384 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3385 (val != '.') && (val != '-') &&
3386 (val != '_') && (val != ':') &&
3387 (!IS_COMBINING(val)) &&
3388 (!IS_EXTENDER(val)))
3389 return(0);
3390
3391 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3392 (val == '.') || (val == '-') ||
3393 (val == '_') || (val == ':') ||
3394 (IS_COMBINING(val)) ||
3395 (IS_EXTENDER(val))) {
3396 val = xmlStringCurrentChar(NULL, cur, &len);
3397 cur += len;
3398 }
3399 }
3400
3401 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003402
3403 return(1);
3404}
3405
3406/**
3407 * xmlValidateNotationDecl:
3408 * @ctxt: the validation context
3409 * @doc: a document instance
3410 * @nota: a notation definition
3411 *
3412 * Try to validate a single notation definition
3413 * basically it does the following checks as described by the
3414 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003415 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003416 * But this function get called anyway ...
3417 *
3418 * returns 1 if valid or 0 otherwise
3419 */
3420
3421int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003422xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3423 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003424 int ret = 1;
3425
3426 return(ret);
3427}
3428
3429/**
3430 * xmlValidateAttributeValue:
3431 * @type: an attribute type
3432 * @value: an attribute value
3433 *
3434 * Validate that the given attribute value match the proper production
3435 *
3436 * [ VC: ID ]
3437 * Values of type ID must match the Name production....
3438 *
3439 * [ VC: IDREF ]
3440 * Values of type IDREF must match the Name production, and values
3441 * of type IDREFS must match Names ...
3442 *
3443 * [ VC: Entity Name ]
3444 * Values of type ENTITY must match the Name production, values
3445 * of type ENTITIES must match Names ...
3446 *
3447 * [ VC: Name Token ]
3448 * Values of type NMTOKEN must match the Nmtoken production; values
3449 * of type NMTOKENS must match Nmtokens.
3450 *
3451 * returns 1 if valid or 0 otherwise
3452 */
3453
3454int
3455xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3456 switch (type) {
3457 case XML_ATTRIBUTE_ENTITIES:
3458 case XML_ATTRIBUTE_IDREFS:
3459 return(xmlValidateNamesValue(value));
3460 case XML_ATTRIBUTE_ENTITY:
3461 case XML_ATTRIBUTE_IDREF:
3462 case XML_ATTRIBUTE_ID:
3463 case XML_ATTRIBUTE_NOTATION:
3464 return(xmlValidateNameValue(value));
3465 case XML_ATTRIBUTE_NMTOKENS:
3466 case XML_ATTRIBUTE_ENUMERATION:
3467 return(xmlValidateNmtokensValue(value));
3468 case XML_ATTRIBUTE_NMTOKEN:
3469 return(xmlValidateNmtokenValue(value));
3470 case XML_ATTRIBUTE_CDATA:
3471 break;
3472 }
3473 return(1);
3474}
3475
3476/**
3477 * xmlValidateAttributeValue2:
3478 * @ctxt: the validation context
3479 * @doc: the document
3480 * @name: the attribute name (used for error reporting only)
3481 * @type: the attribute type
3482 * @value: the attribute value
3483 *
3484 * Validate that the given attribute value match a given type.
3485 * This typically cannot be done before having finished parsing
3486 * the subsets.
3487 *
3488 * [ VC: IDREF ]
3489 * Values of type IDREF must match one of the declared IDs
3490 * Values of type IDREFS must match a sequence of the declared IDs
3491 * each Name must match the value of an ID attribute on some element
3492 * in the XML document; i.e. IDREF values must match the value of
3493 * some ID attribute
3494 *
3495 * [ VC: Entity Name ]
3496 * Values of type ENTITY must match one declared entity
3497 * Values of type ENTITIES must match a sequence of declared entities
3498 *
3499 * [ VC: Notation Attributes ]
3500 * all notation names in the declaration must be declared.
3501 *
3502 * returns 1 if valid or 0 otherwise
3503 */
3504
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003505static int
Owen Taylor3473f882001-02-23 17:55:21 +00003506xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3507 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3508 int ret = 1;
3509 switch (type) {
3510 case XML_ATTRIBUTE_IDREFS:
3511 case XML_ATTRIBUTE_IDREF:
3512 case XML_ATTRIBUTE_ID:
3513 case XML_ATTRIBUTE_NMTOKENS:
3514 case XML_ATTRIBUTE_ENUMERATION:
3515 case XML_ATTRIBUTE_NMTOKEN:
3516 case XML_ATTRIBUTE_CDATA:
3517 break;
3518 case XML_ATTRIBUTE_ENTITY: {
3519 xmlEntityPtr ent;
3520
3521 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003522 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003523 if ((ent == NULL) && (doc->standalone == 1)) {
3524 doc->standalone = 0;
3525 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003526 }
Owen Taylor3473f882001-02-23 17:55:21 +00003527 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003528 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3529 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003530 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003531 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003532 ret = 0;
3533 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003534 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3535 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003536 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003537 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003538 ret = 0;
3539 }
3540 break;
3541 }
3542 case XML_ATTRIBUTE_ENTITIES: {
3543 xmlChar *dup, *nam = NULL, *cur, save;
3544 xmlEntityPtr ent;
3545
3546 dup = xmlStrdup(value);
3547 if (dup == NULL)
3548 return(0);
3549 cur = dup;
3550 while (*cur != 0) {
3551 nam = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00003552 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003553 save = *cur;
3554 *cur = 0;
3555 ent = xmlGetDocEntity(doc, nam);
3556 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003557 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3558 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003559 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003560 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003561 ret = 0;
3562 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003563 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3564 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003565 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003566 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003567 ret = 0;
3568 }
3569 if (save == 0)
3570 break;
3571 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00003572 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003573 }
3574 xmlFree(dup);
3575 break;
3576 }
3577 case XML_ATTRIBUTE_NOTATION: {
3578 xmlNotationPtr nota;
3579
3580 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3581 if ((nota == NULL) && (doc->extSubset != NULL))
3582 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3583
3584 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003585 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3586 XML_DTD_UNKNOWN_NOTATION,
Owen Taylor3473f882001-02-23 17:55:21 +00003587 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003588 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003589 ret = 0;
3590 }
3591 break;
3592 }
3593 }
3594 return(ret);
3595}
3596
3597/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003598 * xmlValidCtxtNormalizeAttributeValue:
3599 * @ctxt: the validation context
3600 * @doc: the document
3601 * @elem: the parent
3602 * @name: the attribute name
3603 * @value: the attribute value
3604 * @ctxt: the validation context or NULL
3605 *
3606 * Does the validation related extra step of the normalization of attribute
3607 * values:
3608 *
3609 * If the declared value is not CDATA, then the XML processor must further
3610 * process the normalized attribute value by discarding any leading and
3611 * trailing space (#x20) characters, and by replacing sequences of space
3612 * (#x20) characters by single space (#x20) character.
3613 *
3614 * Also check VC: Standalone Document Declaration in P32, and update
3615 * ctxt->valid accordingly
3616 *
3617 * returns a new normalized string if normalization is needed, NULL otherwise
3618 * the caller must free the returned value.
3619 */
3620
3621xmlChar *
3622xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3623 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3624 xmlChar *ret, *dst;
3625 const xmlChar *src;
3626 xmlAttributePtr attrDecl = NULL;
3627 int extsubset = 0;
3628
3629 if (doc == NULL) return(NULL);
3630 if (elem == NULL) return(NULL);
3631 if (name == NULL) return(NULL);
3632 if (value == NULL) return(NULL);
3633
3634 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003635 xmlChar fn[50];
3636 xmlChar *fullname;
3637
3638 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3639 if (fullname == NULL)
3640 return(0);
3641 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003642 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003643 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003644 if (attrDecl != NULL)
3645 extsubset = 1;
3646 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003647 if ((fullname != fn) && (fullname != elem->name))
3648 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003649 }
3650 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3651 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3652 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3653 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3654 if (attrDecl != NULL)
3655 extsubset = 1;
3656 }
3657
3658 if (attrDecl == NULL)
3659 return(NULL);
3660 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3661 return(NULL);
3662
3663 ret = xmlStrdup(value);
3664 if (ret == NULL)
3665 return(NULL);
3666 src = value;
3667 dst = ret;
3668 while (*src == 0x20) src++;
3669 while (*src != 0) {
3670 if (*src == 0x20) {
3671 while (*src == 0x20) src++;
3672 if (*src != 0)
3673 *dst++ = 0x20;
3674 } else {
3675 *dst++ = *src++;
3676 }
3677 }
3678 *dst = 0;
3679 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003680 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003681"standalone: %s on %s value had to be normalized based on external subset declaration\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003682 name, elem->name, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003683 ctxt->valid = 0;
3684 }
3685 return(ret);
3686}
3687
3688/**
Owen Taylor3473f882001-02-23 17:55:21 +00003689 * xmlValidNormalizeAttributeValue:
3690 * @doc: the document
3691 * @elem: the parent
3692 * @name: the attribute name
3693 * @value: the attribute value
3694 *
3695 * Does the validation related extra step of the normalization of attribute
3696 * values:
3697 *
3698 * If the declared value is not CDATA, then the XML processor must further
3699 * process the normalized attribute value by discarding any leading and
3700 * trailing space (#x20) characters, and by replacing sequences of space
3701 * (#x20) characters by single space (#x20) character.
3702 *
Daniel Veillard652327a2003-09-29 18:02:38 +00003703 * Returns a new normalized string if normalization is needed, NULL otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00003704 * the caller must free the returned value.
3705 */
3706
3707xmlChar *
3708xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3709 const xmlChar *name, const xmlChar *value) {
3710 xmlChar *ret, *dst;
3711 const xmlChar *src;
3712 xmlAttributePtr attrDecl = NULL;
3713
3714 if (doc == NULL) return(NULL);
3715 if (elem == NULL) return(NULL);
3716 if (name == NULL) return(NULL);
3717 if (value == NULL) return(NULL);
3718
3719 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003720 xmlChar fn[50];
3721 xmlChar *fullname;
3722
3723 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3724 if (fullname == NULL)
3725 return(0);
3726 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003727 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003728 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3729 if ((fullname != fn) && (fullname != elem->name))
3730 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003731 }
3732 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3733 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3734 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3735
3736 if (attrDecl == NULL)
3737 return(NULL);
3738 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3739 return(NULL);
3740
3741 ret = xmlStrdup(value);
3742 if (ret == NULL)
3743 return(NULL);
3744 src = value;
3745 dst = ret;
3746 while (*src == 0x20) src++;
3747 while (*src != 0) {
3748 if (*src == 0x20) {
3749 while (*src == 0x20) src++;
3750 if (*src != 0)
3751 *dst++ = 0x20;
3752 } else {
3753 *dst++ = *src++;
3754 }
3755 }
3756 *dst = 0;
3757 return(ret);
3758}
3759
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003760static void
Owen Taylor3473f882001-02-23 17:55:21 +00003761xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003762 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003763 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3764}
3765
3766/**
3767 * xmlValidateAttributeDecl:
3768 * @ctxt: the validation context
3769 * @doc: a document instance
3770 * @attr: an attribute definition
3771 *
3772 * Try to validate a single attribute definition
3773 * basically it does the following checks as described by the
3774 * XML-1.0 recommendation:
3775 * - [ VC: Attribute Default Legal ]
3776 * - [ VC: Enumeration ]
3777 * - [ VC: ID Attribute Default ]
3778 *
3779 * The ID/IDREF uniqueness and matching are done separately
3780 *
3781 * returns 1 if valid or 0 otherwise
3782 */
3783
3784int
3785xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3786 xmlAttributePtr attr) {
3787 int ret = 1;
3788 int val;
3789 CHECK_DTD;
3790 if(attr == NULL) return(1);
3791
3792 /* Attribute Default Legal */
3793 /* Enumeration */
3794 if (attr->defaultValue != NULL) {
3795 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3796 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003797 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003798 "Syntax of default value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003799 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003800 }
3801 ret &= val;
3802 }
3803
3804 /* ID Attribute Default */
3805 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3806 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3807 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003808 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003809 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003810 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003811 ret = 0;
3812 }
3813
3814 /* One ID per Element Type */
3815 if (attr->atype == XML_ATTRIBUTE_ID) {
3816 int nbId;
3817
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003818 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003819 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3820 attr->elem);
3821 if (elem != NULL) {
3822 nbId = xmlScanIDAttributeDecl(NULL, elem);
3823 } else {
3824 xmlAttributeTablePtr table;
3825
3826 /*
3827 * The attribute may be declared in the internal subset and the
3828 * element in the external subset.
3829 */
3830 nbId = 0;
3831 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3832 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3833 xmlValidateAttributeIdCallback, &nbId);
3834 }
3835 if (nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003836
3837 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00003838 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3839 attr->elem, nbId, attr->name);
3840 } else if (doc->extSubset != NULL) {
3841 int extId = 0;
3842 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3843 if (elem != NULL) {
3844 extId = xmlScanIDAttributeDecl(NULL, elem);
3845 }
3846 if (extId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003847 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00003848 "Element %s has %d ID attribute defined in the external subset : %s\n",
3849 attr->elem, extId, attr->name);
3850 } else if (extId + nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003851 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00003852"Element %s has ID attributes defined in the internal and external subset : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003853 attr->elem, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003854 }
3855 }
3856 }
3857
3858 /* Validity Constraint: Enumeration */
3859 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3860 xmlEnumerationPtr tree = attr->tree;
3861 while (tree != NULL) {
3862 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3863 tree = tree->next;
3864 }
3865 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003866 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003867"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003868 attr->defaultValue, attr->name, attr->elem);
3869 ret = 0;
3870 }
3871 }
3872
3873 return(ret);
3874}
3875
3876/**
3877 * xmlValidateElementDecl:
3878 * @ctxt: the validation context
3879 * @doc: a document instance
3880 * @elem: an element definition
3881 *
3882 * Try to validate a single element definition
3883 * basically it does the following checks as described by the
3884 * XML-1.0 recommendation:
3885 * - [ VC: One ID per Element Type ]
3886 * - [ VC: No Duplicate Types ]
3887 * - [ VC: Unique Element Type Declaration ]
3888 *
3889 * returns 1 if valid or 0 otherwise
3890 */
3891
3892int
3893xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3894 xmlElementPtr elem) {
3895 int ret = 1;
3896 xmlElementPtr tst;
3897
3898 CHECK_DTD;
3899
3900 if (elem == NULL) return(1);
3901
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003902#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00003903#ifdef LIBXML_REGEXP_ENABLED
3904 /* Build the regexp associated to the content model */
3905 ret = xmlValidBuildContentModel(ctxt, elem);
3906#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003907#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00003908
Owen Taylor3473f882001-02-23 17:55:21 +00003909 /* No Duplicate Types */
3910 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3911 xmlElementContentPtr cur, next;
3912 const xmlChar *name;
3913
3914 cur = elem->content;
3915 while (cur != NULL) {
3916 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3917 if (cur->c1 == NULL) break;
3918 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3919 name = cur->c1->name;
3920 next = cur->c2;
3921 while (next != NULL) {
3922 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00003923 if ((xmlStrEqual(next->name, name)) &&
3924 (xmlStrEqual(next->prefix, cur->prefix))) {
3925 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003926 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00003927 "Definition of %s has duplicate references of %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003928 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00003929 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003930 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00003931 "Definition of %s has duplicate references of %s:%s\n",
3932 elem->name, cur->prefix, name);
3933 }
Owen Taylor3473f882001-02-23 17:55:21 +00003934 ret = 0;
3935 }
3936 break;
3937 }
3938 if (next->c1 == NULL) break;
3939 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00003940 if ((xmlStrEqual(next->c1->name, name)) &&
3941 (xmlStrEqual(next->c1->prefix, cur->prefix))) {
3942 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003943 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00003944 "Definition of %s has duplicate references to %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003945 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00003946 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003947 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00003948 "Definition of %s has duplicate references to %s:%s\n",
3949 elem->name, cur->prefix, name);
3950 }
Owen Taylor3473f882001-02-23 17:55:21 +00003951 ret = 0;
3952 }
3953 next = next->c2;
3954 }
3955 }
3956 cur = cur->c2;
3957 }
3958 }
3959
3960 /* VC: Unique Element Type Declaration */
3961 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003962 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003963 ((tst->prefix == elem->prefix) ||
3964 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003965 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003966 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
3967 "Redefinition of element %s\n",
3968 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003969 ret = 0;
3970 }
3971 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003972 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003973 ((tst->prefix == elem->prefix) ||
3974 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003975 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003976 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
3977 "Redefinition of element %s\n",
3978 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003979 ret = 0;
3980 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003981 /* One ID per Element Type
3982 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003983 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3984 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003985 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003986 return(ret);
3987}
3988
3989/**
3990 * xmlValidateOneAttribute:
3991 * @ctxt: the validation context
3992 * @doc: a document instance
3993 * @elem: an element instance
3994 * @attr: an attribute instance
3995 * @value: the attribute value (without entities processing)
3996 *
3997 * Try to validate a single attribute for an element
3998 * basically it does the following checks as described by the
3999 * XML-1.0 recommendation:
4000 * - [ VC: Attribute Value Type ]
4001 * - [ VC: Fixed Attribute Default ]
4002 * - [ VC: Entity Name ]
4003 * - [ VC: Name Token ]
4004 * - [ VC: ID ]
4005 * - [ VC: IDREF ]
4006 * - [ VC: Entity Name ]
4007 * - [ VC: Notation Attributes ]
4008 *
4009 * The ID/IDREF uniqueness and matching are done separately
4010 *
4011 * returns 1 if valid or 0 otherwise
4012 */
4013
4014int
4015xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillard07cb8222003-09-10 10:51:05 +00004016 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4017{
Owen Taylor3473f882001-02-23 17:55:21 +00004018 xmlAttributePtr attrDecl = NULL;
4019 int val;
4020 int ret = 1;
4021
4022 CHECK_DTD;
4023 if ((elem == NULL) || (elem->name == NULL)) return(0);
4024 if ((attr == NULL) || (attr->name == NULL)) return(0);
4025
4026 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004027 xmlChar fn[50];
4028 xmlChar *fullname;
4029
4030 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4031 if (fullname == NULL)
4032 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004033 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004034 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004035 attr->name, attr->ns->prefix);
4036 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004037 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004038 attr->name, attr->ns->prefix);
4039 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004040 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004041 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4042 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00004043 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004044 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004045 if ((fullname != fn) && (fullname != elem->name))
4046 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00004047 }
4048 if (attrDecl == NULL) {
4049 if (attr->ns != NULL) {
4050 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4051 attr->name, attr->ns->prefix);
4052 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4053 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4054 attr->name, attr->ns->prefix);
4055 } else {
4056 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4057 elem->name, attr->name);
4058 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4059 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4060 elem->name, attr->name);
4061 }
4062 }
4063
4064
4065 /* Validity Constraint: Attribute Value Type */
4066 if (attrDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004067 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004068 "No declaration for attribute %s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004069 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004070 return(0);
4071 }
4072 attr->atype = attrDecl->atype;
4073
4074 val = xmlValidateAttributeValue(attrDecl->atype, value);
4075 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004076 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004077 "Syntax of value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004078 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004079 ret = 0;
4080 }
4081
4082 /* Validity constraint: Fixed Attribute Default */
4083 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4084 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004085 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004086 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004087 attr->name, elem->name, attrDecl->defaultValue);
4088 ret = 0;
4089 }
4090 }
4091
4092 /* Validity Constraint: ID uniqueness */
4093 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4094 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4095 ret = 0;
4096 }
4097
4098 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4099 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4100 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4101 ret = 0;
4102 }
4103
4104 /* Validity Constraint: Notation Attributes */
4105 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4106 xmlEnumerationPtr tree = attrDecl->tree;
4107 xmlNotationPtr nota;
4108
4109 /* First check that the given NOTATION was declared */
4110 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4111 if (nota == NULL)
4112 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4113
4114 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004115 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004116 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004117 value, attr->name, elem->name);
4118 ret = 0;
4119 }
4120
4121 /* Second, verify that it's among the list */
4122 while (tree != NULL) {
4123 if (xmlStrEqual(tree->name, value)) break;
4124 tree = tree->next;
4125 }
4126 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004127 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004128"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004129 value, attr->name, elem->name);
4130 ret = 0;
4131 }
4132 }
4133
4134 /* Validity Constraint: Enumeration */
4135 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4136 xmlEnumerationPtr tree = attrDecl->tree;
4137 while (tree != NULL) {
4138 if (xmlStrEqual(tree->name, value)) break;
4139 tree = tree->next;
4140 }
4141 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004142 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004143 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004144 value, attr->name, elem->name);
4145 ret = 0;
4146 }
4147 }
4148
4149 /* Fixed Attribute Default */
4150 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4151 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004152 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004153 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004154 attr->name, elem->name, attrDecl->defaultValue);
4155 ret = 0;
4156 }
4157
4158 /* Extra check for the attribute value */
4159 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4160 attrDecl->atype, value);
4161
4162 return(ret);
4163}
4164
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004165/**
4166 * xmlValidateOneNamespace:
4167 * @ctxt: the validation context
4168 * @doc: a document instance
4169 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004170 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004171 * @ns: an namespace declaration instance
4172 * @value: the attribute value (without entities processing)
4173 *
4174 * Try to validate a single namespace declaration for an element
4175 * basically it does the following checks as described by the
4176 * XML-1.0 recommendation:
4177 * - [ VC: Attribute Value Type ]
4178 * - [ VC: Fixed Attribute Default ]
4179 * - [ VC: Entity Name ]
4180 * - [ VC: Name Token ]
4181 * - [ VC: ID ]
4182 * - [ VC: IDREF ]
4183 * - [ VC: Entity Name ]
4184 * - [ VC: Notation Attributes ]
4185 *
4186 * The ID/IDREF uniqueness and matching are done separately
4187 *
4188 * returns 1 if valid or 0 otherwise
4189 */
4190
4191int
4192xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4193xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4194 /* xmlElementPtr elemDecl; */
4195 xmlAttributePtr attrDecl = NULL;
4196 int val;
4197 int ret = 1;
4198
4199 CHECK_DTD;
4200 if ((elem == NULL) || (elem->name == NULL)) return(0);
4201 if ((ns == NULL) || (ns->href == NULL)) return(0);
4202
4203 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004204 xmlChar fn[50];
4205 xmlChar *fullname;
4206
4207 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4208 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004209 xmlVErrMemory(ctxt, "Validating namespace");
Daniel Veillardc00cda82003-04-07 10:22:39 +00004210 return(0);
4211 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004212 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004213 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004214 ns->prefix, BAD_CAST "xmlns");
4215 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004216 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004217 ns->prefix, BAD_CAST "xmlns");
4218 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004219 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004220 BAD_CAST "xmlns");
4221 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004222 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004223 BAD_CAST "xmlns");
4224 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004225 if ((fullname != fn) && (fullname != elem->name))
4226 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004227 }
4228 if (attrDecl == NULL) {
4229 if (ns->prefix != NULL) {
4230 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4231 ns->prefix, BAD_CAST "xmlns");
4232 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4233 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4234 ns->prefix, BAD_CAST "xmlns");
4235 } else {
4236 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4237 elem->name, BAD_CAST "xmlns");
4238 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4239 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4240 elem->name, BAD_CAST "xmlns");
4241 }
4242 }
4243
4244
4245 /* Validity Constraint: Attribute Value Type */
4246 if (attrDecl == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004247 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004248 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004249 "No declaration for attribute xmlns:%s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004250 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004251 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004252 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004253 "No declaration for attribute xmlns of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004254 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004255 }
4256 return(0);
4257 }
4258
4259 val = xmlValidateAttributeValue(attrDecl->atype, value);
4260 if (val == 0) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004261 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004262 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004263 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004264 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004265 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004266 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004267 "Syntax of value for attribute xmlns of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004268 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004269 }
4270 ret = 0;
4271 }
4272
4273 /* Validity constraint: Fixed Attribute Default */
4274 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4275 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004276 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004277 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004278 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4279 ns->prefix, elem->name, attrDecl->defaultValue);
4280 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004281 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004282 "Value for attribute xmlns of %s is different from default \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004283 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004284 }
4285 ret = 0;
4286 }
4287 }
4288
4289 /* Validity Constraint: ID uniqueness */
4290 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4291 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4292 ret = 0;
4293 }
4294
4295 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4296 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4297 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4298 ret = 0;
4299 }
4300
4301 /* Validity Constraint: Notation Attributes */
4302 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4303 xmlEnumerationPtr tree = attrDecl->tree;
4304 xmlNotationPtr nota;
4305
4306 /* First check that the given NOTATION was declared */
4307 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4308 if (nota == NULL)
4309 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4310
4311 if (nota == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004312 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004313 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004314 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4315 value, ns->prefix, elem->name);
4316 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004317 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004318 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004319 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004320 }
4321 ret = 0;
4322 }
4323
4324 /* Second, verify that it's among the list */
4325 while (tree != NULL) {
4326 if (xmlStrEqual(tree->name, value)) break;
4327 tree = tree->next;
4328 }
4329 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004330 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004331 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004332"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4333 value, ns->prefix, elem->name);
4334 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004335 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004336"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004337 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004338 }
4339 ret = 0;
4340 }
4341 }
4342
4343 /* Validity Constraint: Enumeration */
4344 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4345 xmlEnumerationPtr tree = attrDecl->tree;
4346 while (tree != NULL) {
4347 if (xmlStrEqual(tree->name, value)) break;
4348 tree = tree->next;
4349 }
4350 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004351 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004352 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004353"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4354 value, ns->prefix, elem->name);
4355 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004356 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004357"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004358 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004359 }
4360 ret = 0;
4361 }
4362 }
4363
4364 /* Fixed Attribute Default */
4365 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4366 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004367 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004368 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004369 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4370 ns->prefix, elem->name, attrDecl->defaultValue);
4371 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004372 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004373 "Value for attribute xmlns of %s must be \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004374 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004375 }
4376 ret = 0;
4377 }
4378
4379 /* Extra check for the attribute value */
4380 if (ns->prefix != NULL) {
4381 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4382 attrDecl->atype, value);
4383 } else {
4384 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4385 attrDecl->atype, value);
4386 }
4387
4388 return(ret);
4389}
4390
Daniel Veillard118aed72002-09-24 14:13:13 +00004391#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004392/**
4393 * xmlValidateSkipIgnorable:
4394 * @ctxt: the validation context
4395 * @child: the child list
4396 *
4397 * Skip ignorable elements w.r.t. the validation process
4398 *
4399 * returns the first element to consider for validation of the content model
4400 */
4401
4402static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004403xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004404 while (child != NULL) {
4405 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004406 /* These things are ignored (skipped) during validation. */
4407 case XML_PI_NODE:
4408 case XML_COMMENT_NODE:
4409 case XML_XINCLUDE_START:
4410 case XML_XINCLUDE_END:
4411 child = child->next;
4412 break;
4413 case XML_TEXT_NODE:
4414 if (xmlIsBlankNode(child))
4415 child = child->next;
4416 else
4417 return(child);
4418 break;
4419 /* keep current node */
4420 default:
4421 return(child);
4422 }
4423 }
4424 return(child);
4425}
4426
4427/**
4428 * xmlValidateElementType:
4429 * @ctxt: the validation context
4430 *
4431 * Try to validate the content model of an element internal function
4432 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004433 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4434 * reference is found and -3 if the validation succeeded but
4435 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004436 */
4437
4438static int
4439xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004440 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004441 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004442
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004443 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004444 if ((NODE == NULL) && (CONT == NULL))
4445 return(1);
4446 if ((NODE == NULL) &&
4447 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4448 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4449 return(1);
4450 }
4451 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004452 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004453 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004454
4455 /*
4456 * We arrive here when more states need to be examined
4457 */
4458cont:
4459
4460 /*
4461 * We just recovered from a rollback generated by a possible
4462 * epsilon transition, go directly to the analysis phase
4463 */
4464 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004465 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004466 DEBUG_VALID_STATE(NODE, CONT)
4467 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004468 goto analyze;
4469 }
4470
4471 DEBUG_VALID_STATE(NODE, CONT)
4472 /*
4473 * we may have to save a backup state here. This is the equivalent
4474 * of handling epsilon transition in NFAs.
4475 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004476 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004477 ((CONT->parent == NULL) ||
4478 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004479 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004480 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004481 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004482 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004483 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4484 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004485 }
4486
4487
4488 /*
4489 * Check first if the content matches
4490 */
4491 switch (CONT->type) {
4492 case XML_ELEMENT_CONTENT_PCDATA:
4493 if (NODE == NULL) {
4494 DEBUG_VALID_MSG("pcdata failed no node");
4495 ret = 0;
4496 break;
4497 }
4498 if (NODE->type == XML_TEXT_NODE) {
4499 DEBUG_VALID_MSG("pcdata found, skip to next");
4500 /*
4501 * go to next element in the content model
4502 * skipping ignorable elems
4503 */
4504 do {
4505 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004506 NODE = xmlValidateSkipIgnorable(NODE);
4507 if ((NODE != NULL) &&
4508 (NODE->type == XML_ENTITY_REF_NODE))
4509 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004510 } while ((NODE != NULL) &&
4511 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004512 (NODE->type != XML_TEXT_NODE) &&
4513 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004514 ret = 1;
4515 break;
4516 } else {
4517 DEBUG_VALID_MSG("pcdata failed");
4518 ret = 0;
4519 break;
4520 }
4521 break;
4522 case XML_ELEMENT_CONTENT_ELEMENT:
4523 if (NODE == NULL) {
4524 DEBUG_VALID_MSG("element failed no node");
4525 ret = 0;
4526 break;
4527 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004528 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4529 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004530 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004531 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4532 ret = (CONT->prefix == NULL);
4533 } else if (CONT->prefix == NULL) {
4534 ret = 0;
4535 } else {
4536 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4537 }
4538 }
4539 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004540 DEBUG_VALID_MSG("element found, skip to next");
4541 /*
4542 * go to next element in the content model
4543 * skipping ignorable elems
4544 */
4545 do {
4546 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004547 NODE = xmlValidateSkipIgnorable(NODE);
4548 if ((NODE != NULL) &&
4549 (NODE->type == XML_ENTITY_REF_NODE))
4550 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004551 } while ((NODE != NULL) &&
4552 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004553 (NODE->type != XML_TEXT_NODE) &&
4554 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004555 } else {
4556 DEBUG_VALID_MSG("element failed");
4557 ret = 0;
4558 break;
4559 }
4560 break;
4561 case XML_ELEMENT_CONTENT_OR:
4562 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004563 * Small optimization.
4564 */
4565 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4566 if ((NODE == NULL) ||
4567 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4568 DEPTH++;
4569 CONT = CONT->c2;
4570 goto cont;
4571 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004572 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4573 ret = (CONT->c1->prefix == NULL);
4574 } else if (CONT->c1->prefix == NULL) {
4575 ret = 0;
4576 } else {
4577 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4578 }
4579 if (ret == 0) {
4580 DEPTH++;
4581 CONT = CONT->c2;
4582 goto cont;
4583 }
Daniel Veillard85349052001-04-20 13:48:21 +00004584 }
4585
4586 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004587 * save the second branch 'or' branch
4588 */
4589 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004590 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4591 OCCURS, ROLLBACK_OR) < 0)
4592 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004593 DEPTH++;
4594 CONT = CONT->c1;
4595 goto cont;
4596 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004597 /*
4598 * Small optimization.
4599 */
4600 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4601 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4602 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4603 if ((NODE == NULL) ||
4604 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4605 DEPTH++;
4606 CONT = CONT->c2;
4607 goto cont;
4608 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004609 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4610 ret = (CONT->c1->prefix == NULL);
4611 } else if (CONT->c1->prefix == NULL) {
4612 ret = 0;
4613 } else {
4614 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4615 }
4616 if (ret == 0) {
4617 DEPTH++;
4618 CONT = CONT->c2;
4619 goto cont;
4620 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004621 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004622 DEPTH++;
4623 CONT = CONT->c1;
4624 goto cont;
4625 }
4626
4627 /*
4628 * At this point handle going up in the tree
4629 */
4630 if (ret == -1) {
4631 DEBUG_VALID_MSG("error found returning");
4632 return(ret);
4633 }
4634analyze:
4635 while (CONT != NULL) {
4636 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004637 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004638 * this level.
4639 */
4640 if (ret == 0) {
4641 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004642 xmlNodePtr cur;
4643
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004644 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004645 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004646 DEBUG_VALID_MSG("Once branch failed, rollback");
4647 if (vstateVPop(ctxt) < 0 ) {
4648 DEBUG_VALID_MSG("exhaustion, failed");
4649 return(0);
4650 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004651 if (cur != ctxt->vstate->node)
4652 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004653 goto cont;
4654 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004655 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004656 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004657 DEBUG_VALID_MSG("Plus branch failed, rollback");
4658 if (vstateVPop(ctxt) < 0 ) {
4659 DEBUG_VALID_MSG("exhaustion, failed");
4660 return(0);
4661 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004662 if (cur != ctxt->vstate->node)
4663 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004664 goto cont;
4665 }
4666 DEBUG_VALID_MSG("Plus branch found");
4667 ret = 1;
4668 break;
4669 case XML_ELEMENT_CONTENT_MULT:
4670#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004671 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004672 DEBUG_VALID_MSG("Mult branch failed");
4673 } else {
4674 DEBUG_VALID_MSG("Mult branch found");
4675 }
4676#endif
4677 ret = 1;
4678 break;
4679 case XML_ELEMENT_CONTENT_OPT:
4680 DEBUG_VALID_MSG("Option branch failed");
4681 ret = 1;
4682 break;
4683 }
4684 } else {
4685 switch (CONT->ocur) {
4686 case XML_ELEMENT_CONTENT_OPT:
4687 DEBUG_VALID_MSG("Option branch succeeded");
4688 ret = 1;
4689 break;
4690 case XML_ELEMENT_CONTENT_ONCE:
4691 DEBUG_VALID_MSG("Once branch succeeded");
4692 ret = 1;
4693 break;
4694 case XML_ELEMENT_CONTENT_PLUS:
4695 if (STATE == ROLLBACK_PARENT) {
4696 DEBUG_VALID_MSG("Plus branch rollback");
4697 ret = 1;
4698 break;
4699 }
4700 if (NODE == NULL) {
4701 DEBUG_VALID_MSG("Plus branch exhausted");
4702 ret = 1;
4703 break;
4704 }
4705 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004706 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004707 goto cont;
4708 case XML_ELEMENT_CONTENT_MULT:
4709 if (STATE == ROLLBACK_PARENT) {
4710 DEBUG_VALID_MSG("Mult branch rollback");
4711 ret = 1;
4712 break;
4713 }
4714 if (NODE == NULL) {
4715 DEBUG_VALID_MSG("Mult branch exhausted");
4716 ret = 1;
4717 break;
4718 }
4719 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004720 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004721 goto cont;
4722 }
4723 }
4724 STATE = 0;
4725
4726 /*
4727 * Then act accordingly at the parent level
4728 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004729 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004730 if (CONT->parent == NULL)
4731 break;
4732
4733 switch (CONT->parent->type) {
4734 case XML_ELEMENT_CONTENT_PCDATA:
4735 DEBUG_VALID_MSG("Error: parent pcdata");
4736 return(-1);
4737 case XML_ELEMENT_CONTENT_ELEMENT:
4738 DEBUG_VALID_MSG("Error: parent element");
4739 return(-1);
4740 case XML_ELEMENT_CONTENT_OR:
4741 if (ret == 1) {
4742 DEBUG_VALID_MSG("Or succeeded");
4743 CONT = CONT->parent;
4744 DEPTH--;
4745 } else {
4746 DEBUG_VALID_MSG("Or failed");
4747 CONT = CONT->parent;
4748 DEPTH--;
4749 }
4750 break;
4751 case XML_ELEMENT_CONTENT_SEQ:
4752 if (ret == 0) {
4753 DEBUG_VALID_MSG("Sequence failed");
4754 CONT = CONT->parent;
4755 DEPTH--;
4756 } else if (CONT == CONT->parent->c1) {
4757 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4758 CONT = CONT->parent->c2;
4759 goto cont;
4760 } else {
4761 DEBUG_VALID_MSG("Sequence succeeded");
4762 CONT = CONT->parent;
4763 DEPTH--;
4764 }
4765 }
4766 }
4767 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004768 xmlNodePtr cur;
4769
4770 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004771 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4772 if (vstateVPop(ctxt) < 0 ) {
4773 DEBUG_VALID_MSG("exhaustion, failed");
4774 return(0);
4775 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004776 if (cur != ctxt->vstate->node)
4777 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004778 goto cont;
4779 }
4780 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004781 xmlNodePtr cur;
4782
4783 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004784 DEBUG_VALID_MSG("Failure, rollback");
4785 if (vstateVPop(ctxt) < 0 ) {
4786 DEBUG_VALID_MSG("exhaustion, failed");
4787 return(0);
4788 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004789 if (cur != ctxt->vstate->node)
4790 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004791 goto cont;
4792 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004793 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004794}
Daniel Veillard23e73572002-09-19 19:56:43 +00004795#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004796
4797/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00004798 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004799 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00004800 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004801 * @content: An element
4802 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
4803 *
4804 * This will dump the list of elements to the buffer
4805 * Intended just for the debug routine
4806 */
4807static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00004808xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004809 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00004810 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004811
4812 if (node == NULL) return;
4813 if (glob) strcat(buf, "(");
4814 cur = node;
4815 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004816 len = strlen(buf);
4817 if (size - len < 50) {
4818 if ((size - len > 4) && (buf[len - 1] != '.'))
4819 strcat(buf, " ...");
4820 return;
4821 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004822 switch (cur->type) {
4823 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004824 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004825 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004826 if ((size - len > 4) && (buf[len - 1] != '.'))
4827 strcat(buf, " ...");
4828 return;
4829 }
4830 strcat(buf, (char *) cur->ns->prefix);
4831 strcat(buf, ":");
4832 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004833 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004834 if ((size - len > 4) && (buf[len - 1] != '.'))
4835 strcat(buf, " ...");
4836 return;
4837 }
4838 strcat(buf, (char *) cur->name);
4839 if (cur->next != NULL)
4840 strcat(buf, " ");
4841 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004842 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004843 if (xmlIsBlankNode(cur))
4844 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004845 case XML_CDATA_SECTION_NODE:
4846 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004847 strcat(buf, "CDATA");
4848 if (cur->next != NULL)
4849 strcat(buf, " ");
4850 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004851 case XML_ATTRIBUTE_NODE:
4852 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004853#ifdef LIBXML_DOCB_ENABLED
4854 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004855#endif
4856 case XML_HTML_DOCUMENT_NODE:
4857 case XML_DOCUMENT_TYPE_NODE:
4858 case XML_DOCUMENT_FRAG_NODE:
4859 case XML_NOTATION_NODE:
4860 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004861 strcat(buf, "???");
4862 if (cur->next != NULL)
4863 strcat(buf, " ");
4864 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004865 case XML_ENTITY_NODE:
4866 case XML_PI_NODE:
4867 case XML_DTD_NODE:
4868 case XML_COMMENT_NODE:
4869 case XML_ELEMENT_DECL:
4870 case XML_ATTRIBUTE_DECL:
4871 case XML_ENTITY_DECL:
4872 case XML_XINCLUDE_START:
4873 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004874 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004875 }
4876 cur = cur->next;
4877 }
4878 if (glob) strcat(buf, ")");
4879}
4880
4881/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004882 * xmlValidateElementContent:
4883 * @ctxt: the validation context
4884 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004885 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004886 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004887 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004888 *
4889 * Try to validate the content model of an element
4890 *
4891 * returns 1 if valid or 0 if not and -1 in case of error
4892 */
4893
4894static int
4895xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004896 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004897 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00004898#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00004899 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00004900#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00004901 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004902 xmlElementContentPtr cont;
4903 const xmlChar *name;
4904
4905 if (elemDecl == NULL)
4906 return(-1);
4907 cont = elemDecl->content;
4908 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004909
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004910#ifdef LIBXML_REGEXP_ENABLED
4911 /* Build the regexp associated to the content model */
4912 if (elemDecl->contModel == NULL)
4913 ret = xmlValidBuildContentModel(ctxt, elemDecl);
4914 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004915 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004916 } else {
4917 xmlRegExecCtxtPtr exec;
4918
Daniel Veillardec498e12003-02-05 11:01:50 +00004919 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
4920 return(-1);
4921 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004922 ctxt->nodeMax = 0;
4923 ctxt->nodeNr = 0;
4924 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004925 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
4926 if (exec != NULL) {
4927 cur = child;
4928 while (cur != NULL) {
4929 switch (cur->type) {
4930 case XML_ENTITY_REF_NODE:
4931 /*
4932 * Push the current node to be able to roll back
4933 * and process within the entity
4934 */
4935 if ((cur->children != NULL) &&
4936 (cur->children->children != NULL)) {
4937 nodeVPush(ctxt, cur);
4938 cur = cur->children->children;
4939 continue;
4940 }
4941 break;
4942 case XML_TEXT_NODE:
4943 if (xmlIsBlankNode(cur))
4944 break;
4945 ret = 0;
4946 goto fail;
4947 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00004948 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004949 ret = 0;
4950 goto fail;
4951 case XML_ELEMENT_NODE:
4952 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004953 xmlChar fn[50];
4954 xmlChar *fullname;
4955
4956 fullname = xmlBuildQName(cur->name,
4957 cur->ns->prefix, fn, 50);
4958 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004959 ret = -1;
4960 goto fail;
4961 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004962 ret = xmlRegExecPushString(exec, fullname, NULL);
4963 if ((fullname != fn) && (fullname != cur->name))
4964 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004965 } else {
4966 ret = xmlRegExecPushString(exec, cur->name, NULL);
4967 }
4968 break;
4969 default:
4970 break;
4971 }
4972 /*
4973 * Switch to next element
4974 */
4975 cur = cur->next;
4976 while (cur == NULL) {
4977 cur = nodeVPop(ctxt);
4978 if (cur == NULL)
4979 break;
4980 cur = cur->next;
4981 }
4982 }
4983 ret = xmlRegExecPushString(exec, NULL, NULL);
4984fail:
4985 xmlRegFreeExecCtxt(exec);
4986 }
4987 }
4988#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004989 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004990 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004991 */
4992 ctxt->vstateMax = 8;
4993 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4994 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4995 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004996 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004997 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004998 }
4999 /*
5000 * The first entry in the stack is reserved to the current state
5001 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00005002 ctxt->nodeMax = 0;
5003 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00005004 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005005 ctxt->vstate = &ctxt->vstateTab[0];
5006 ctxt->vstateNr = 1;
5007 CONT = cont;
5008 NODE = child;
5009 DEPTH = 0;
5010 OCCURS = 0;
5011 STATE = 0;
5012 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005013 if ((ret == -3) && (warn)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005014 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
5015 "Content model for Element %s is ambiguous\n",
Daniel Veillard0cc72772003-10-13 14:00:21 +00005016 name, NULL, NULL);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005017 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005018 /*
5019 * An entities reference appeared at this level.
5020 * Buid a minimal representation of this node content
5021 * sufficient to run the validation process on it
5022 */
5023 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005024 cur = child;
5025 while (cur != NULL) {
5026 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005027 case XML_ENTITY_REF_NODE:
5028 /*
5029 * Push the current node to be able to roll back
5030 * and process within the entity
5031 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005032 if ((cur->children != NULL) &&
5033 (cur->children->children != NULL)) {
5034 nodeVPush(ctxt, cur);
5035 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005036 continue;
5037 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00005038 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005039 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005040 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005041 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005042 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005043 case XML_CDATA_SECTION_NODE:
5044 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005045 case XML_ELEMENT_NODE:
5046 /*
5047 * Allocate a new node and minimally fills in
5048 * what's required
5049 */
5050 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5051 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005052 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005053 xmlFreeNodeList(repl);
5054 ret = -1;
5055 goto done;
5056 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005057 tmp->type = cur->type;
5058 tmp->name = cur->name;
5059 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005060 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00005061 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005062 if (repl == NULL)
5063 repl = last = tmp;
5064 else {
5065 last->next = tmp;
5066 last = tmp;
5067 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005068 if (cur->type == XML_CDATA_SECTION_NODE) {
5069 /*
5070 * E59 spaces in CDATA does not match the
5071 * nonterminal S
5072 */
5073 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5074 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005075 break;
5076 default:
5077 break;
5078 }
5079 /*
5080 * Switch to next element
5081 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005082 cur = cur->next;
5083 while (cur == NULL) {
5084 cur = nodeVPop(ctxt);
5085 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005086 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005087 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005088 }
5089 }
5090
5091 /*
5092 * Relaunch the validation
5093 */
5094 ctxt->vstate = &ctxt->vstateTab[0];
5095 ctxt->vstateNr = 1;
5096 CONT = cont;
5097 NODE = repl;
5098 DEPTH = 0;
5099 OCCURS = 0;
5100 STATE = 0;
5101 ret = xmlValidateElementType(ctxt);
5102 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005103#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005104 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005105 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
5106 char expr[5000];
5107 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005108
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005109 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005110 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005111 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00005112#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005113 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005114 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005115 else
Daniel Veillard01992e02002-10-09 10:20:30 +00005116#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005117 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005118
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005119 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005120 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5121 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5122 name, BAD_CAST expr, BAD_CAST list);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005123 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005124 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5125 "Element content does not follow the DTD, expecting %s, got %s\n",
5126 BAD_CAST expr, BAD_CAST list, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005127 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005128 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005129 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005130 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005131 "Element %s content does not follow the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005132 name, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005133 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005134 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5135 "Element content does not follow the DTD\n",
5136 NULL, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005137 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005138 }
5139 ret = 0;
5140 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005141 if (ret == -3)
5142 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005143
Daniel Veillard23e73572002-09-19 19:56:43 +00005144#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005145done:
5146 /*
5147 * Deallocate the copy if done, and free up the validation stack
5148 */
5149 while (repl != NULL) {
5150 tmp = repl->next;
5151 xmlFree(repl);
5152 repl = tmp;
5153 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005154 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005155 if (ctxt->vstateTab != NULL) {
5156 xmlFree(ctxt->vstateTab);
5157 ctxt->vstateTab = NULL;
5158 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005159#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005160 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00005161 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005162 if (ctxt->nodeTab != NULL) {
5163 xmlFree(ctxt->nodeTab);
5164 ctxt->nodeTab = NULL;
5165 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005166 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005167
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005168}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005169
Owen Taylor3473f882001-02-23 17:55:21 +00005170/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005171 * xmlValidateCdataElement:
5172 * @ctxt: the validation context
5173 * @doc: a document instance
5174 * @elem: an element instance
5175 *
5176 * Check that an element follows #CDATA
5177 *
5178 * returns 1 if valid or 0 otherwise
5179 */
5180static int
5181xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5182 xmlNodePtr elem) {
5183 int ret = 1;
5184 xmlNodePtr cur, child;
5185
Daniel Veillardceb09b92002-10-04 11:46:37 +00005186 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005187 return(0);
5188
5189 child = elem->children;
5190
5191 cur = child;
5192 while (cur != NULL) {
5193 switch (cur->type) {
5194 case XML_ENTITY_REF_NODE:
5195 /*
5196 * Push the current node to be able to roll back
5197 * and process within the entity
5198 */
5199 if ((cur->children != NULL) &&
5200 (cur->children->children != NULL)) {
5201 nodeVPush(ctxt, cur);
5202 cur = cur->children->children;
5203 continue;
5204 }
5205 break;
5206 case XML_COMMENT_NODE:
5207 case XML_PI_NODE:
5208 case XML_TEXT_NODE:
5209 case XML_CDATA_SECTION_NODE:
5210 break;
5211 default:
5212 ret = 0;
5213 goto done;
5214 }
5215 /*
5216 * Switch to next element
5217 */
5218 cur = cur->next;
5219 while (cur == NULL) {
5220 cur = nodeVPop(ctxt);
5221 if (cur == NULL)
5222 break;
5223 cur = cur->next;
5224 }
5225 }
5226done:
5227 ctxt->nodeMax = 0;
5228 ctxt->nodeNr = 0;
5229 if (ctxt->nodeTab != NULL) {
5230 xmlFree(ctxt->nodeTab);
5231 ctxt->nodeTab = NULL;
5232 }
5233 return(ret);
5234}
5235
5236/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005237 * xmlValidateCheckMixed:
5238 * @ctxt: the validation context
5239 * @cont: the mixed content model
5240 * @qname: the qualified name as appearing in the serialization
5241 *
5242 * Check if the given node is part of the content model.
5243 *
5244 * Returns 1 if yes, 0 if no, -1 in case of error
5245 */
5246static int
5247xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
5248 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005249 const xmlChar *name;
5250 int plen;
5251 name = xmlSplitQName3(qname, &plen);
5252
5253 if (name == NULL) {
5254 while (cont != NULL) {
5255 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5256 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5257 return(1);
5258 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5259 (cont->c1 != NULL) &&
5260 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5261 if ((cont->c1->prefix == NULL) &&
5262 (xmlStrEqual(cont->c1->name, qname)))
5263 return(1);
5264 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5265 (cont->c1 == NULL) ||
5266 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005267 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5268 "Internal: MIXED struct corrupted\n",
5269 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005270 break;
5271 }
5272 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005273 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005274 } else {
5275 while (cont != NULL) {
5276 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5277 if ((cont->prefix != NULL) &&
5278 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5279 (xmlStrEqual(cont->name, name)))
5280 return(1);
5281 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5282 (cont->c1 != NULL) &&
5283 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5284 if ((cont->c1->prefix != NULL) &&
5285 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5286 (xmlStrEqual(cont->c1->name, name)))
5287 return(1);
5288 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5289 (cont->c1 == NULL) ||
5290 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005291 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5292 "Internal: MIXED struct corrupted\n",
5293 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005294 break;
5295 }
5296 cont = cont->c2;
5297 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005298 }
5299 return(0);
5300}
5301
5302/**
5303 * xmlValidGetElemDecl:
5304 * @ctxt: the validation context
5305 * @doc: a document instance
5306 * @elem: an element instance
5307 * @extsubset: pointer, (out) indicate if the declaration was found
5308 * in the external subset.
5309 *
5310 * Finds a declaration associated to an element in the document.
5311 *
5312 * returns the pointer to the declaration or NULL if not found.
5313 */
5314static xmlElementPtr
5315xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5316 xmlNodePtr elem, int *extsubset) {
5317 xmlElementPtr elemDecl = NULL;
5318 const xmlChar *prefix = NULL;
5319
5320 if ((elem == NULL) || (elem->name == NULL)) return(NULL);
5321 if (extsubset != NULL)
5322 *extsubset = 0;
5323
5324 /*
5325 * Fetch the declaration for the qualified name
5326 */
5327 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5328 prefix = elem->ns->prefix;
5329
5330 if (prefix != NULL) {
5331 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5332 elem->name, prefix);
5333 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5334 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5335 elem->name, prefix);
5336 if ((elemDecl != NULL) && (extsubset != NULL))
5337 *extsubset = 1;
5338 }
5339 }
5340
5341 /*
5342 * Fetch the declaration for the non qualified name
5343 * This is "non-strict" validation should be done on the
5344 * full QName but in that case being flexible makes sense.
5345 */
5346 if (elemDecl == NULL) {
5347 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5348 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5349 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5350 if ((elemDecl != NULL) && (extsubset != NULL))
5351 *extsubset = 1;
5352 }
5353 }
5354 if (elemDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005355 xmlErrValidNode(ctxt, elem,
5356 XML_DTD_UNKNOWN_ELEM,
5357 "No declaration for element %s\n",
5358 elem->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005359 }
5360 return(elemDecl);
5361}
5362
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005363#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005364/**
5365 * xmlValidatePushElement:
5366 * @ctxt: the validation context
5367 * @doc: a document instance
5368 * @elem: an element instance
5369 * @qname: the qualified name as appearing in the serialization
5370 *
5371 * Push a new element start on the validation stack.
5372 *
5373 * returns 1 if no validation problem was found or 0 otherwise
5374 */
5375int
5376xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5377 xmlNodePtr elem, const xmlChar *qname) {
5378 int ret = 1;
5379 xmlElementPtr eDecl;
5380 int extsubset = 0;
5381
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005382/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005383 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5384 xmlValidStatePtr state = ctxt->vstate;
5385 xmlElementPtr elemDecl;
5386
5387 /*
5388 * Check the new element agaisnt the content model of the new elem.
5389 */
5390 if (state->elemDecl != NULL) {
5391 elemDecl = state->elemDecl;
5392
5393 switch(elemDecl->etype) {
5394 case XML_ELEMENT_TYPE_UNDEFINED:
5395 ret = 0;
5396 break;
5397 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005398 xmlErrValidNode(ctxt, state->node,
5399 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005400 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005401 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005402 ret = 0;
5403 break;
5404 case XML_ELEMENT_TYPE_ANY:
5405 /* I don't think anything is required then */
5406 break;
5407 case XML_ELEMENT_TYPE_MIXED:
5408 /* simple case of declared as #PCDATA */
5409 if ((elemDecl->content != NULL) &&
5410 (elemDecl->content->type ==
5411 XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005412 xmlErrValidNode(ctxt, state->node,
5413 XML_DTD_NOT_PCDATA,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005414 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005415 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005416 ret = 0;
5417 } else {
5418 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5419 qname);
5420 if (ret != 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005421 xmlErrValidNode(ctxt, state->node,
5422 XML_DTD_INVALID_CHILD,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005423 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005424 qname, state->node->name, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005425 }
5426 }
5427 break;
5428 case XML_ELEMENT_TYPE_ELEMENT:
5429 /*
5430 * TODO:
5431 * VC: Standalone Document Declaration
5432 * - element types with element content, if white space
5433 * occurs directly within any instance of those types.
5434 */
5435 if (state->exec != NULL) {
5436 ret = xmlRegExecPushString(state->exec, qname, NULL);
5437 if (ret < 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005438 xmlErrValidNode(ctxt, state->node,
5439 XML_DTD_CONTENT_MODEL,
5440 "Element %s content does not follow the DTD, Misplaced %s\n",
5441 state->node->name, qname, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005442 ret = 0;
5443 } else {
5444 ret = 1;
5445 }
5446 }
5447 break;
5448 }
5449 }
5450 }
5451 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5452 vstateVPush(ctxt, eDecl, elem);
5453 return(ret);
5454}
5455
5456/**
5457 * xmlValidatePushCData:
5458 * @ctxt: the validation context
5459 * @data: some character data read
5460 * @len: the lenght of the data
5461 *
5462 * check the CData parsed for validation in the current stack
5463 *
5464 * returns 1 if no validation problem was found or 0 otherwise
5465 */
5466int
5467xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5468 int ret = 1;
5469
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005470/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005471 if (len <= 0)
5472 return(ret);
5473 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5474 xmlValidStatePtr state = ctxt->vstate;
5475 xmlElementPtr elemDecl;
5476
5477 /*
5478 * Check the new element agaisnt the content model of the new elem.
5479 */
5480 if (state->elemDecl != NULL) {
5481 elemDecl = state->elemDecl;
5482
5483 switch(elemDecl->etype) {
5484 case XML_ELEMENT_TYPE_UNDEFINED:
5485 ret = 0;
5486 break;
5487 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005488 xmlErrValidNode(ctxt, state->node,
5489 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005490 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005491 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005492 ret = 0;
5493 break;
5494 case XML_ELEMENT_TYPE_ANY:
5495 break;
5496 case XML_ELEMENT_TYPE_MIXED:
5497 break;
5498 case XML_ELEMENT_TYPE_ELEMENT:
5499 if (len > 0) {
5500 int i;
5501
5502 for (i = 0;i < len;i++) {
William M. Brack76e95df2003-10-18 16:20:14 +00005503 if (!IS_BLANK_CH(data[i])) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005504 xmlErrValidNode(ctxt, state->node,
5505 XML_DTD_CONTENT_MODEL,
5506 "Element %s content does not follow the DTD, Text not allowed\n",
5507 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005508 ret = 0;
5509 goto done;
5510 }
5511 }
5512 /*
5513 * TODO:
5514 * VC: Standalone Document Declaration
5515 * element types with element content, if white space
5516 * occurs directly within any instance of those types.
5517 */
5518 }
5519 break;
5520 }
5521 }
5522 }
5523done:
5524 return(ret);
5525}
5526
5527/**
5528 * xmlValidatePopElement:
5529 * @ctxt: the validation context
5530 * @doc: a document instance
5531 * @elem: an element instance
5532 * @qname: the qualified name as appearing in the serialization
5533 *
5534 * Pop the element end from the validation stack.
5535 *
5536 * returns 1 if no validation problem was found or 0 otherwise
5537 */
5538int
5539xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005540 xmlNodePtr elem ATTRIBUTE_UNUSED,
5541 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005542 int ret = 1;
5543
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005544/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005545 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5546 xmlValidStatePtr state = ctxt->vstate;
5547 xmlElementPtr elemDecl;
5548
5549 /*
5550 * Check the new element agaisnt the content model of the new elem.
5551 */
5552 if (state->elemDecl != NULL) {
5553 elemDecl = state->elemDecl;
5554
5555 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5556 if (state->exec != NULL) {
5557 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5558 if (ret == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005559 xmlErrValidNode(ctxt, state->node,
5560 XML_DTD_CONTENT_MODEL,
5561 "Element %s content does not follow the DTD, Expecting more child\n",
5562 state->node->name, NULL,NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005563 } else {
5564 /*
5565 * previous validation errors should not generate
5566 * a new one here
5567 */
5568 ret = 1;
5569 }
5570 }
5571 }
5572 }
5573 vstateVPop(ctxt);
5574 }
5575 return(ret);
5576}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005577#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005578
5579/**
Owen Taylor3473f882001-02-23 17:55:21 +00005580 * xmlValidateOneElement:
5581 * @ctxt: the validation context
5582 * @doc: a document instance
5583 * @elem: an element instance
5584 *
5585 * Try to validate a single element and it's attributes,
5586 * basically it does the following checks as described by the
5587 * XML-1.0 recommendation:
5588 * - [ VC: Element Valid ]
5589 * - [ VC: Required Attribute ]
5590 * Then call xmlValidateOneAttribute() for each attribute present.
5591 *
5592 * The ID/IDREF checkings are done separately
5593 *
5594 * returns 1 if valid or 0 otherwise
5595 */
5596
5597int
5598xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5599 xmlNodePtr elem) {
5600 xmlElementPtr elemDecl = NULL;
5601 xmlElementContentPtr cont;
5602 xmlAttributePtr attr;
5603 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005604 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005605 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005606 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005607
5608 CHECK_DTD;
5609
5610 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005611 switch (elem->type) {
5612 case XML_ATTRIBUTE_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005613 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5614 "Attribute element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005615 return(0);
5616 case XML_TEXT_NODE:
5617 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005618 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5619 "Text element has children !\n",
5620 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005621 return(0);
5622 }
5623 if (elem->properties != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005624 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5625 "Text element has attribute !\n",
5626 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005627 return(0);
5628 }
5629 if (elem->ns != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005630 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5631 "Text element has namespace !\n",
5632 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005633 return(0);
5634 }
5635 if (elem->nsDef != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005636 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5637 "Text element has namespace !\n",
5638 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005639 return(0);
5640 }
5641 if (elem->content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005642 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5643 "Text element has no content !\n",
5644 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005645 return(0);
5646 }
5647 return(1);
5648 case XML_XINCLUDE_START:
5649 case XML_XINCLUDE_END:
5650 return(1);
5651 case XML_CDATA_SECTION_NODE:
5652 case XML_ENTITY_REF_NODE:
5653 case XML_PI_NODE:
5654 case XML_COMMENT_NODE:
5655 return(1);
5656 case XML_ENTITY_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005657 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5658 "Entity element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005659 return(0);
5660 case XML_NOTATION_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005661 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5662 "Notation element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005663 return(0);
5664 case XML_DOCUMENT_NODE:
5665 case XML_DOCUMENT_TYPE_NODE:
5666 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005667 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5668 "Document element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005669 return(0);
5670 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005671 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5672 "HTML Document not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005673 return(0);
5674 case XML_ELEMENT_NODE:
5675 break;
5676 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005677 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5678 "unknown element type\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005679 return(0);
5680 }
Owen Taylor3473f882001-02-23 17:55:21 +00005681
5682 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005683 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005684 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005685 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5686 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005687 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005688
Daniel Veillardea7751d2002-12-20 00:16:24 +00005689 /*
5690 * If vstateNr is not zero that means continuous validation is
5691 * activated, do not try to check the content model at that level.
5692 */
5693 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005694 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005695 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005696 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005697 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
5698 "No declaration for element %s\n",
5699 elem->name, NULL, NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005700 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005701 case XML_ELEMENT_TYPE_EMPTY:
5702 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005703 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
Owen Taylor3473f882001-02-23 17:55:21 +00005704 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005705 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005706 ret = 0;
5707 }
5708 break;
5709 case XML_ELEMENT_TYPE_ANY:
5710 /* I don't think anything is required then */
5711 break;
5712 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005713
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005714 /* simple case of declared as #PCDATA */
5715 if ((elemDecl->content != NULL) &&
5716 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5717 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5718 if (!ret) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005719 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005720 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005721 elem->name, NULL, NULL);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005722 }
5723 break;
5724 }
Owen Taylor3473f882001-02-23 17:55:21 +00005725 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005726 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005727 while (child != NULL) {
5728 if (child->type == XML_ELEMENT_NODE) {
5729 name = child->name;
5730 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005731 xmlChar fn[50];
5732 xmlChar *fullname;
5733
5734 fullname = xmlBuildQName(child->name, child->ns->prefix,
5735 fn, 50);
5736 if (fullname == NULL)
5737 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005738 cont = elemDecl->content;
5739 while (cont != NULL) {
5740 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005741 if (xmlStrEqual(cont->name, fullname))
5742 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005743 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5744 (cont->c1 != NULL) &&
5745 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005746 if (xmlStrEqual(cont->c1->name, fullname))
5747 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005748 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5749 (cont->c1 == NULL) ||
5750 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005751 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5752 "Internal: MIXED struct corrupted\n",
5753 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005754 break;
5755 }
5756 cont = cont->c2;
5757 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005758 if ((fullname != fn) && (fullname != child->name))
5759 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005760 if (cont != NULL)
5761 goto child_ok;
5762 }
5763 cont = elemDecl->content;
5764 while (cont != NULL) {
5765 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5766 if (xmlStrEqual(cont->name, name)) break;
5767 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5768 (cont->c1 != NULL) &&
5769 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5770 if (xmlStrEqual(cont->c1->name, name)) break;
5771 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5772 (cont->c1 == NULL) ||
5773 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005774 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5775 "Internal: MIXED struct corrupted\n",
5776 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005777 break;
5778 }
5779 cont = cont->c2;
5780 }
5781 if (cont == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005782 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005783 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005784 name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005785 ret = 0;
5786 }
5787 }
5788child_ok:
5789 child = child->next;
5790 }
5791 break;
5792 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005793 if ((doc->standalone == 1) && (extsubset == 1)) {
5794 /*
5795 * VC: Standalone Document Declaration
5796 * - element types with element content, if white space
5797 * occurs directly within any instance of those types.
5798 */
5799 child = elem->children;
5800 while (child != NULL) {
5801 if (child->type == XML_TEXT_NODE) {
5802 const xmlChar *content = child->content;
5803
William M. Brack76e95df2003-10-18 16:20:14 +00005804 while (IS_BLANK_CH(*content))
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005805 content++;
5806 if (*content == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005807 xmlErrValidNode(ctxt, elem,
5808 XML_DTD_STANDALONE_WHITE_SPACE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005809"standalone: %s declared in the external subset contains white spaces nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005810 elem->name, NULL, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005811 ret = 0;
5812 break;
5813 }
5814 }
5815 child =child->next;
5816 }
5817 }
Owen Taylor3473f882001-02-23 17:55:21 +00005818 child = elem->children;
5819 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005820 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005821 if (tmp <= 0)
5822 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005823 break;
5824 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005825 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00005826
5827 /* [ VC: Required Attribute ] */
5828 attr = elemDecl->attributes;
5829 while (attr != NULL) {
5830 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005831 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00005832
Daniel Veillarde4301c82002-02-13 13:32:35 +00005833 if ((attr->prefix == NULL) &&
5834 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5835 xmlNsPtr ns;
5836
5837 ns = elem->nsDef;
5838 while (ns != NULL) {
5839 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005840 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005841 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00005842 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005843 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5844 xmlNsPtr ns;
5845
5846 ns = elem->nsDef;
5847 while (ns != NULL) {
5848 if (xmlStrEqual(attr->name, ns->prefix))
5849 goto found;
5850 ns = ns->next;
5851 }
5852 } else {
5853 xmlAttrPtr attrib;
5854
5855 attrib = elem->properties;
5856 while (attrib != NULL) {
5857 if (xmlStrEqual(attrib->name, attr->name)) {
5858 if (attr->prefix != NULL) {
5859 xmlNsPtr nameSpace = attrib->ns;
5860
5861 if (nameSpace == NULL)
5862 nameSpace = elem->ns;
5863 /*
5864 * qualified names handling is problematic, having a
5865 * different prefix should be possible but DTDs don't
5866 * allow to define the URI instead of the prefix :-(
5867 */
5868 if (nameSpace == NULL) {
5869 if (qualified < 0)
5870 qualified = 0;
5871 } else if (!xmlStrEqual(nameSpace->prefix,
5872 attr->prefix)) {
5873 if (qualified < 1)
5874 qualified = 1;
5875 } else
5876 goto found;
5877 } else {
5878 /*
5879 * We should allow applications to define namespaces
5880 * for their application even if the DTD doesn't
5881 * carry one, otherwise, basically we would always
5882 * break.
5883 */
5884 goto found;
5885 }
5886 }
5887 attrib = attrib->next;
5888 }
Owen Taylor3473f882001-02-23 17:55:21 +00005889 }
5890 if (qualified == -1) {
5891 if (attr->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005892 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005893 "Element %s does not carry attribute %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005894 elem->name, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005895 ret = 0;
5896 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005897 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005898 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005899 elem->name, attr->prefix,attr->name);
5900 ret = 0;
5901 }
5902 } else if (qualified == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005903 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00005904 "Element %s required attribute %s:%s has no prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005905 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005906 } else if (qualified == 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005907 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00005908 "Element %s required attribute %s:%s has different prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005909 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005910 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005911 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
5912 /*
5913 * Special tests checking #FIXED namespace declarations
5914 * have the right value since this is not done as an
5915 * attribute checking
5916 */
5917 if ((attr->prefix == NULL) &&
5918 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5919 xmlNsPtr ns;
5920
5921 ns = elem->nsDef;
5922 while (ns != NULL) {
5923 if (ns->prefix == NULL) {
5924 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005925 xmlErrValidNode(ctxt, elem,
5926 XML_DTD_ELEM_DEFAULT_NAMESPACE,
Daniel Veillarde4301c82002-02-13 13:32:35 +00005927 "Element %s namespace name for default namespace does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005928 elem->name, NULL, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00005929 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005930 }
5931 goto found;
5932 }
5933 ns = ns->next;
5934 }
5935 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5936 xmlNsPtr ns;
5937
5938 ns = elem->nsDef;
5939 while (ns != NULL) {
5940 if (xmlStrEqual(attr->name, ns->prefix)) {
5941 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005942 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005943 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005944 elem->name, ns->prefix, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00005945 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005946 }
5947 goto found;
5948 }
5949 ns = ns->next;
5950 }
5951 }
Owen Taylor3473f882001-02-23 17:55:21 +00005952 }
5953found:
5954 attr = attr->nexth;
5955 }
5956 return(ret);
5957}
5958
5959/**
5960 * xmlValidateRoot:
5961 * @ctxt: the validation context
5962 * @doc: a document instance
5963 *
5964 * Try to validate a the root element
5965 * basically it does the following check as described by the
5966 * XML-1.0 recommendation:
5967 * - [ VC: Root Element Type ]
5968 * it doesn't try to recurse or apply other check to the element
5969 *
5970 * returns 1 if valid or 0 otherwise
5971 */
5972
5973int
5974xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5975 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00005976 int ret;
5977
Owen Taylor3473f882001-02-23 17:55:21 +00005978 if (doc == NULL) return(0);
5979
5980 root = xmlDocGetRootElement(doc);
5981 if ((root == NULL) || (root->name == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005982 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
5983 "no root element\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005984 return(0);
5985 }
5986
5987 /*
5988 * When doing post validation against a separate DTD, those may
5989 * no internal subset has been generated
5990 */
5991 if ((doc->intSubset != NULL) &&
5992 (doc->intSubset->name != NULL)) {
5993 /*
5994 * Check first the document root against the NQName
5995 */
5996 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
5997 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005998 xmlChar fn[50];
5999 xmlChar *fullname;
6000
6001 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6002 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006003 xmlVErrMemory(ctxt, NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00006004 return(0);
6005 }
6006 ret = xmlStrEqual(doc->intSubset->name, fullname);
6007 if ((fullname != fn) && (fullname != root->name))
6008 xmlFree(fullname);
6009 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00006010 goto name_ok;
6011 }
6012 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6013 (xmlStrEqual(root->name, BAD_CAST "html")))
6014 goto name_ok;
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006015 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6016 "root and DTD name do not match '%s' and '%s'\n",
6017 root->name, doc->intSubset->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006018 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006019 }
6020 }
6021name_ok:
6022 return(1);
6023}
6024
6025
6026/**
6027 * xmlValidateElement:
6028 * @ctxt: the validation context
6029 * @doc: a document instance
6030 * @elem: an element instance
6031 *
6032 * Try to validate the subtree under an element
6033 *
6034 * returns 1 if valid or 0 otherwise
6035 */
6036
6037int
6038xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
6039 xmlNodePtr child;
6040 xmlAttrPtr attr;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006041 xmlNsPtr ns;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006042 const xmlChar *value;
Owen Taylor3473f882001-02-23 17:55:21 +00006043 int ret = 1;
6044
6045 if (elem == NULL) return(0);
6046
6047 /*
6048 * XInclude elements were added after parsing in the infoset,
6049 * they don't really mean anything validation wise.
6050 */
6051 if ((elem->type == XML_XINCLUDE_START) ||
6052 (elem->type == XML_XINCLUDE_END))
6053 return(1);
6054
6055 CHECK_DTD;
6056
Daniel Veillard10ea86c2001-06-20 13:55:33 +00006057 /*
6058 * Entities references have to be handled separately
6059 */
6060 if (elem->type == XML_ENTITY_REF_NODE) {
6061 return(1);
6062 }
6063
Owen Taylor3473f882001-02-23 17:55:21 +00006064 ret &= xmlValidateOneElement(ctxt, doc, elem);
6065 attr = elem->properties;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006066 while (attr != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006067 value = xmlNodeListGetString(doc, attr->children, 0);
6068 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6069 if (value != NULL)
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006070 xmlFree((char *)value);
Owen Taylor3473f882001-02-23 17:55:21 +00006071 attr= attr->next;
6072 }
Daniel Veillarde133dd82003-10-30 10:42:20 +00006073 ns = elem->nsDef;
6074 while (ns != NULL) {
6075 value = ns->href;
6076 ret &= xmlValidateOneNamespace(ctxt, doc, elem, ns->prefix,
6077 ns, ns->href);
6078 ns = ns->next;
6079 }
Owen Taylor3473f882001-02-23 17:55:21 +00006080 child = elem->children;
6081 while (child != NULL) {
6082 ret &= xmlValidateElement(ctxt, doc, child);
6083 child = child->next;
6084 }
6085
6086 return(ret);
6087}
6088
Daniel Veillard8730c562001-02-26 10:49:57 +00006089/**
6090 * xmlValidateRef:
6091 * @ref: A reference to be validated
6092 * @ctxt: Validation context
6093 * @name: Name of ID we are searching for
6094 *
6095 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006096static void
Daniel Veillard8730c562001-02-26 10:49:57 +00006097xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006098 const xmlChar *name) {
6099 xmlAttrPtr id;
6100 xmlAttrPtr attr;
6101
6102 if (ref == NULL)
6103 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006104 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006105 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006106 attr = ref->attr;
6107 if (attr == NULL) {
6108 xmlChar *dup, *str = NULL, *cur, save;
6109
6110 dup = xmlStrdup(name);
6111 if (dup == NULL) {
6112 ctxt->valid = 0;
6113 return;
6114 }
6115 cur = dup;
6116 while (*cur != 0) {
6117 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006118 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006119 save = *cur;
6120 *cur = 0;
6121 id = xmlGetID(ctxt->doc, str);
6122 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006123 xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006124 "attribute %s line %d references an unknown ID \"%s\"\n",
6125 ref->name, ref->lineno, str);
6126 ctxt->valid = 0;
6127 }
6128 if (save == 0)
6129 break;
6130 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006131 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006132 }
6133 xmlFree(dup);
6134 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00006135 id = xmlGetID(ctxt->doc, name);
6136 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006137 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006138 "IDREF attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006139 attr->name, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006140 ctxt->valid = 0;
6141 }
6142 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6143 xmlChar *dup, *str = NULL, *cur, save;
6144
6145 dup = xmlStrdup(name);
6146 if (dup == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006147 xmlVErrMemory(ctxt, "IDREFS split");
Owen Taylor3473f882001-02-23 17:55:21 +00006148 ctxt->valid = 0;
6149 return;
6150 }
6151 cur = dup;
6152 while (*cur != 0) {
6153 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006154 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006155 save = *cur;
6156 *cur = 0;
6157 id = xmlGetID(ctxt->doc, str);
6158 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006159 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006160 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006161 attr->name, str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006162 ctxt->valid = 0;
6163 }
6164 if (save == 0)
6165 break;
6166 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006167 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006168 }
6169 xmlFree(dup);
6170 }
6171}
6172
6173/**
Daniel Veillard8730c562001-02-26 10:49:57 +00006174 * xmlWalkValidateList:
6175 * @data: Contents of current link
6176 * @user: Value supplied by the user
6177 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006178 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00006179 */
6180static int
6181xmlWalkValidateList(const void *data, const void *user)
6182{
6183 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6184 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6185 return 1;
6186}
6187
6188/**
6189 * xmlValidateCheckRefCallback:
6190 * @ref_list: List of references
6191 * @ctxt: Validation context
6192 * @name: Name of ID we are searching for
6193 *
6194 */
6195static void
6196xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6197 const xmlChar *name) {
6198 xmlValidateMemo memo;
6199
6200 if (ref_list == NULL)
6201 return;
6202 memo.ctxt = ctxt;
6203 memo.name = name;
6204
6205 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6206
6207}
6208
6209/**
Owen Taylor3473f882001-02-23 17:55:21 +00006210 * xmlValidateDocumentFinal:
6211 * @ctxt: the validation context
6212 * @doc: a document instance
6213 *
6214 * Does the final step for the document validation once all the
6215 * incremental validation steps have been completed
6216 *
6217 * basically it does the following checks described by the XML Rec
6218 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006219 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00006220 *
6221 * returns 1 if valid or 0 otherwise
6222 */
6223
6224int
6225xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6226 xmlRefTablePtr table;
6227
6228 if (doc == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006229 xmlErrValid(ctxt, XML_DTD_NO_DOC,
6230 "xmlValidateDocumentFinal: doc == NULL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006231 return(0);
6232 }
6233
6234 /*
6235 * Check all the NOTATION/NOTATIONS attributes
6236 */
6237 /*
6238 * Check all the ENTITY/ENTITIES attributes definition for validity
6239 */
6240 /*
6241 * Check all the IDREF/IDREFS attributes definition for validity
6242 */
6243 table = (xmlRefTablePtr) doc->refs;
6244 ctxt->doc = doc;
6245 ctxt->valid = 1;
6246 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6247 return(ctxt->valid);
6248}
6249
6250/**
6251 * xmlValidateDtd:
6252 * @ctxt: the validation context
6253 * @doc: a document instance
6254 * @dtd: a dtd instance
6255 *
6256 * Try to validate the document against the dtd instance
6257 *
6258 * basically it does check all the definitions in the DtD.
6259 *
6260 * returns 1 if valid or 0 otherwise
6261 */
6262
6263int
6264xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6265 int ret;
6266 xmlDtdPtr oldExt;
6267 xmlNodePtr root;
6268
6269 if (dtd == NULL) return(0);
6270 if (doc == NULL) return(0);
6271 oldExt = doc->extSubset;
6272 doc->extSubset = dtd;
6273 ret = xmlValidateRoot(ctxt, doc);
6274 if (ret == 0) {
6275 doc->extSubset = oldExt;
6276 return(ret);
6277 }
6278 if (doc->ids != NULL) {
6279 xmlFreeIDTable(doc->ids);
6280 doc->ids = NULL;
6281 }
6282 if (doc->refs != NULL) {
6283 xmlFreeRefTable(doc->refs);
6284 doc->refs = NULL;
6285 }
6286 root = xmlDocGetRootElement(doc);
6287 ret = xmlValidateElement(ctxt, doc, root);
6288 ret &= xmlValidateDocumentFinal(ctxt, doc);
6289 doc->extSubset = oldExt;
6290 return(ret);
6291}
6292
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006293static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006294xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6295 const xmlChar *name ATTRIBUTE_UNUSED) {
6296 if (cur == NULL)
6297 return;
6298 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6299 xmlChar *notation = cur->content;
6300
Daniel Veillard878eab02002-02-19 13:46:09 +00006301 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006302 int ret;
6303
6304 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6305 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006306 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006307 }
6308 }
6309 }
6310}
6311
6312static void
Owen Taylor3473f882001-02-23 17:55:21 +00006313xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006314 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006315 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006316 xmlDocPtr doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006317 xmlElementPtr elem = NULL;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006318
Owen Taylor3473f882001-02-23 17:55:21 +00006319 if (cur == NULL)
6320 return;
6321 switch (cur->atype) {
6322 case XML_ATTRIBUTE_CDATA:
6323 case XML_ATTRIBUTE_ID:
6324 case XML_ATTRIBUTE_IDREF :
6325 case XML_ATTRIBUTE_IDREFS:
6326 case XML_ATTRIBUTE_NMTOKEN:
6327 case XML_ATTRIBUTE_NMTOKENS:
6328 case XML_ATTRIBUTE_ENUMERATION:
6329 break;
6330 case XML_ATTRIBUTE_ENTITY:
6331 case XML_ATTRIBUTE_ENTITIES:
6332 case XML_ATTRIBUTE_NOTATION:
6333 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006334
6335 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6336 cur->atype, cur->defaultValue);
6337 if ((ret == 0) && (ctxt->valid == 1))
6338 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006339 }
6340 if (cur->tree != NULL) {
6341 xmlEnumerationPtr tree = cur->tree;
6342 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006343 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006344 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006345 if ((ret == 0) && (ctxt->valid == 1))
6346 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006347 tree = tree->next;
6348 }
6349 }
6350 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006351 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6352 doc = cur->doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006353 if (cur->elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006354 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard878eab02002-02-19 13:46:09 +00006355 "xmlValidateAttributeCallback(%s): internal error\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006356 (const char *) cur->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00006357 return;
6358 }
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006359
6360 if (doc != NULL)
6361 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6362 if ((elem == NULL) && (doc != NULL))
Daniel Veillard878eab02002-02-19 13:46:09 +00006363 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006364 if ((elem == NULL) && (cur->parent != NULL) &&
6365 (cur->parent->type == XML_DTD_NODE))
6366 elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00006367 if (elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006368 xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
Daniel Veillard878eab02002-02-19 13:46:09 +00006369 "attribute %s: could not find decl for element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006370 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006371 return;
6372 }
6373 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006374 xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006375 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006376 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006377 ctxt->valid = 0;
6378 }
6379 }
Owen Taylor3473f882001-02-23 17:55:21 +00006380}
6381
6382/**
6383 * xmlValidateDtdFinal:
6384 * @ctxt: the validation context
6385 * @doc: a document instance
6386 *
6387 * Does the final step for the dtds validation once all the
6388 * subsets have been parsed
6389 *
6390 * basically it does the following checks described by the XML Rec
6391 * - check that ENTITY and ENTITIES type attributes default or
6392 * possible values matches one of the defined entities.
6393 * - check that NOTATION type attributes default or
6394 * possible values matches one of the defined notations.
6395 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006396 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006397 */
6398
6399int
6400xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006401 xmlDtdPtr dtd;
6402 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006403 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006404
6405 if (doc == NULL) return(0);
6406 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6407 return(0);
6408 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006409 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006410 dtd = doc->intSubset;
6411 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6412 table = (xmlAttributeTablePtr) dtd->attributes;
6413 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006414 }
6415 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006416 entities = (xmlEntitiesTablePtr) dtd->entities;
6417 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6418 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006419 }
6420 dtd = doc->extSubset;
6421 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6422 table = (xmlAttributeTablePtr) dtd->attributes;
6423 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006424 }
6425 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006426 entities = (xmlEntitiesTablePtr) dtd->entities;
6427 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6428 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006429 }
6430 return(ctxt->valid);
6431}
6432
6433/**
6434 * xmlValidateDocument:
6435 * @ctxt: the validation context
6436 * @doc: a document instance
6437 *
6438 * Try to validate the document instance
6439 *
6440 * basically it does the all the checks described by the XML Rec
6441 * i.e. validates the internal and external subset (if present)
6442 * and validate the document tree.
6443 *
6444 * returns 1 if valid or 0 otherwise
6445 */
6446
6447int
6448xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6449 int ret;
6450 xmlNodePtr root;
6451
Daniel Veillard2fd85422002-10-16 14:32:41 +00006452 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006453 xmlErrValid(ctxt, XML_DTD_NO_DTD,
6454 "no DTD found!\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006455 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006456 }
Owen Taylor3473f882001-02-23 17:55:21 +00006457 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6458 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6459 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6460 doc->intSubset->SystemID);
6461 if (doc->extSubset == NULL) {
6462 if (doc->intSubset->SystemID != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006463 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006464 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006465 (const char *) doc->intSubset->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00006466 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006467 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006468 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006469 (const char *) doc->intSubset->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00006470 }
6471 return(0);
6472 }
6473 }
6474
6475 if (doc->ids != NULL) {
6476 xmlFreeIDTable(doc->ids);
6477 doc->ids = NULL;
6478 }
6479 if (doc->refs != NULL) {
6480 xmlFreeRefTable(doc->refs);
6481 doc->refs = NULL;
6482 }
6483 ret = xmlValidateDtdFinal(ctxt, doc);
6484 if (!xmlValidateRoot(ctxt, doc)) return(0);
6485
6486 root = xmlDocGetRootElement(doc);
6487 ret &= xmlValidateElement(ctxt, doc, root);
6488 ret &= xmlValidateDocumentFinal(ctxt, doc);
6489 return(ret);
6490}
6491
Owen Taylor3473f882001-02-23 17:55:21 +00006492/************************************************************************
6493 * *
6494 * Routines for dynamic validation editing *
6495 * *
6496 ************************************************************************/
6497
6498/**
6499 * xmlValidGetPotentialChildren:
6500 * @ctree: an element content tree
6501 * @list: an array to store the list of child names
6502 * @len: a pointer to the number of element in the list
6503 * @max: the size of the array
6504 *
6505 * Build/extend a list of potential children allowed by the content tree
6506 *
6507 * returns the number of element in the list, or -1 in case of error.
6508 */
6509
6510int
6511xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6512 int *len, int max) {
6513 int i;
6514
6515 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6516 return(-1);
6517 if (*len >= max) return(*len);
6518
6519 switch (ctree->type) {
6520 case XML_ELEMENT_CONTENT_PCDATA:
6521 for (i = 0; i < *len;i++)
6522 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6523 list[(*len)++] = BAD_CAST "#PCDATA";
6524 break;
6525 case XML_ELEMENT_CONTENT_ELEMENT:
6526 for (i = 0; i < *len;i++)
6527 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6528 list[(*len)++] = ctree->name;
6529 break;
6530 case XML_ELEMENT_CONTENT_SEQ:
6531 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6532 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6533 break;
6534 case XML_ELEMENT_CONTENT_OR:
6535 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6536 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6537 break;
6538 }
6539
6540 return(*len);
6541}
6542
6543/**
6544 * xmlValidGetValidElements:
6545 * @prev: an element to insert after
6546 * @next: an element to insert next
6547 * @list: an array to store the list of child names
6548 * @max: the size of the array
6549 *
6550 * This function returns the list of authorized children to insert
6551 * within an existing tree while respecting the validity constraints
6552 * forced by the Dtd. The insertion point is defined using @prev and
6553 * @next in the following ways:
6554 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6555 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6556 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6557 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6558 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6559 *
6560 * pointers to the element names are inserted at the beginning of the array
6561 * and do not need to be freed.
6562 *
6563 * returns the number of element in the list, or -1 in case of error. If
6564 * the function returns the value @max the caller is invited to grow the
6565 * receiving array and retry.
6566 */
6567
6568int
6569xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
6570 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006571 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006572 int nb_valid_elements = 0;
6573 const xmlChar *elements[256];
6574 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006575 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006576
6577 xmlNode *ref_node;
6578 xmlNode *parent;
6579 xmlNode *test_node;
6580
6581 xmlNode *prev_next;
6582 xmlNode *next_prev;
6583 xmlNode *parent_childs;
6584 xmlNode *parent_last;
6585
6586 xmlElement *element_desc;
6587
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00006588 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006589
Owen Taylor3473f882001-02-23 17:55:21 +00006590 if (prev == NULL && next == NULL)
6591 return(-1);
6592
6593 if (list == NULL) return(-1);
6594 if (max <= 0) return(-1);
6595
6596 nb_valid_elements = 0;
6597 ref_node = prev ? prev : next;
6598 parent = ref_node->parent;
6599
6600 /*
6601 * Retrieves the parent element declaration
6602 */
6603 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6604 parent->name);
6605 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6606 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6607 parent->name);
6608 if (element_desc == NULL) return(-1);
6609
6610 /*
6611 * Do a backup of the current tree structure
6612 */
6613 prev_next = prev ? prev->next : NULL;
6614 next_prev = next ? next->prev : NULL;
6615 parent_childs = parent->children;
6616 parent_last = parent->last;
6617
6618 /*
6619 * Creates a dummy node and insert it into the tree
6620 */
6621 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
6622 test_node->doc = ref_node->doc;
6623 test_node->parent = parent;
6624 test_node->prev = prev;
6625 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006626 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006627
6628 if (prev) prev->next = test_node;
6629 else parent->children = test_node;
6630
6631 if (next) next->prev = test_node;
6632 else parent->last = test_node;
6633
6634 /*
6635 * Insert each potential child node and check if the parent is
6636 * still valid
6637 */
6638 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6639 elements, &nb_elements, 256);
6640
6641 for (i = 0;i < nb_elements;i++) {
6642 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006643 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006644 int j;
6645
6646 for (j = 0; j < nb_valid_elements;j++)
6647 if (xmlStrEqual(elements[i], list[j])) break;
6648 list[nb_valid_elements++] = elements[i];
6649 if (nb_valid_elements >= max) break;
6650 }
6651 }
6652
6653 /*
6654 * Restore the tree structure
6655 */
6656 if (prev) prev->next = prev_next;
6657 if (next) next->prev = next_prev;
6658 parent->children = parent_childs;
6659 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006660
6661 /*
6662 * Free up the dummy node
6663 */
6664 test_node->name = name;
6665 xmlFreeNode(test_node);
6666
Owen Taylor3473f882001-02-23 17:55:21 +00006667 return(nb_valid_elements);
6668}
Daniel Veillard4432df22003-09-28 18:58:27 +00006669#endif /* LIBXML_VALID_ENABLED */
6670