blob: 96e3de6ecc5a21217ec0cb162adb10c68ce02d6d [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * valid.c : part of the code use to do the DTD handling and the validity
3 * checking
4 *
5 * See Copyright for the status of this software.
6 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <string.h>
14
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18
19#include <libxml/xmlmemory.h>
20#include <libxml/hash.h>
Daniel Veillard29b17482004-08-16 00:39:03 +000021#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000022#include <libxml/valid.h>
23#include <libxml/parser.h>
24#include <libxml/parserInternals.h>
25#include <libxml/xmlerror.h>
26#include <libxml/list.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000027#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000028
Daniel Veillard4432df22003-09-28 18:58:27 +000029static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
30 int create);
Daniel Veillarde62d36c2001-05-15 08:53:16 +000031/* #define DEBUG_VALID_ALGO */
Daniel Veillard5acfd6b2002-09-18 16:29:02 +000032/* #define DEBUG_REGEXP_ALGO */
Daniel Veillarde62d36c2001-05-15 08:53:16 +000033
Daniel Veillarda646cfd2002-09-17 21:50:03 +000034#define TODO \
35 xmlGenericError(xmlGenericErrorContext, \
36 "Unimplemented block at %s:%d\n", \
37 __FILE__, __LINE__);
Owen Taylor3473f882001-02-23 17:55:21 +000038
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000039/************************************************************************
40 * *
41 * Error handling routines *
42 * *
43 ************************************************************************/
44
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000045/**
Daniel Veillardce9457f2003-10-05 21:33:18 +000046 * xmlVErrMemory:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000047 * @ctxt: an XML validation parser context
48 * @extra: extra informations
49 *
50 * Handle an out of memory error
51 */
52static void
Daniel Veillardce9457f2003-10-05 21:33:18 +000053xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000054{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000055 xmlGenericErrorFunc channel = NULL;
56 xmlParserCtxtPtr pctxt = NULL;
57 void *data = NULL;
58
59 if (ctxt != NULL) {
60 channel = ctxt->error;
61 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +000062 /* Use the special values to detect if it is part of a parsing
63 context */
64 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
65 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
66 pctxt = ctxt->userData;
67 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +000068 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000069 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +000070 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000071 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000072 XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
73 "Memory allocation failed : %s\n", extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000074 else
Daniel Veillard73000572003-10-11 11:26:42 +000075 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000076 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000077 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
78 "Memory allocation failed\n");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000079}
80
81/**
82 * xmlErrValid:
83 * @ctxt: an XML validation parser context
Daniel Veillardbb5abab2003-10-03 22:21:51 +000084 * @error: the error number
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000085 * @extra: extra informations
86 *
87 * Handle a validation error
88 */
89static void
Daniel Veillardf88d8cf2003-12-08 10:25:02 +000090xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000091 const char *msg, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000092{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000093 xmlGenericErrorFunc channel = NULL;
94 xmlParserCtxtPtr pctxt = NULL;
95 void *data = NULL;
96
97 if (ctxt != NULL) {
98 channel = ctxt->error;
99 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000100 /* Use the special values to detect if it is part of a parsing
101 context */
102 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
103 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
104 pctxt = ctxt->userData;
105 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000106 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000107 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +0000108 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +0000109 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000110 XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
111 msg, extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000112 else
Daniel Veillard73000572003-10-11 11:26:42 +0000113 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +0000114 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000115 XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
116 msg);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000117}
118
Daniel Veillardf54cd532004-02-25 11:52:31 +0000119#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
120/**
121 * xmlErrValidNode:
122 * @ctxt: an XML validation parser context
123 * @node: the node raising the error
124 * @error: the error number
125 * @str1: extra informations
126 * @str2: extra informations
127 * @str3: extra informations
128 *
129 * Handle a validation error, provide contextual informations
130 */
131static void
132xmlErrValidNode(xmlValidCtxtPtr ctxt,
133 xmlNodePtr node, xmlParserErrors error,
134 const char *msg, const xmlChar * str1,
135 const xmlChar * str2, const xmlChar * str3)
136{
137 xmlStructuredErrorFunc schannel = NULL;
138 xmlGenericErrorFunc channel = NULL;
139 xmlParserCtxtPtr pctxt = NULL;
140 void *data = NULL;
141
142 if (ctxt != NULL) {
143 channel = ctxt->error;
144 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000145 /* Use the special values to detect if it is part of a parsing
146 context */
147 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
148 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
149 pctxt = ctxt->userData;
150 }
Daniel Veillardf54cd532004-02-25 11:52:31 +0000151 }
152 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
153 XML_ERR_ERROR, NULL, 0,
154 (const char *) str1,
155 (const char *) str1,
156 (const char *) str3, 0, 0, msg, str1, str2, str3);
157}
158#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
159
Daniel Veillardd61e8fb2003-10-19 21:59:17 +0000160#ifdef LIBXML_VALID_ENABLED
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000161/**
162 * xmlErrValidNodeNr:
163 * @ctxt: an XML validation parser context
164 * @node: the node raising the error
165 * @error: the error number
166 * @str1: extra informations
167 * @int2: extra informations
168 * @str3: extra informations
169 *
170 * Handle a validation error, provide contextual informations
171 */
172static void
Daniel Veillard72b9e292003-10-28 15:44:17 +0000173xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000174 xmlNodePtr node, xmlParserErrors error,
175 const char *msg, const xmlChar * str1,
176 int int2, const xmlChar * str3)
177{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000178 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000179 xmlGenericErrorFunc channel = NULL;
180 xmlParserCtxtPtr pctxt = NULL;
181 void *data = NULL;
182
183 if (ctxt != NULL) {
184 channel = ctxt->error;
185 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000186 /* Use the special values to detect if it is part of a parsing
187 context */
188 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
189 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
190 pctxt = ctxt->userData;
191 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000192 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000193 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000194 XML_ERR_ERROR, NULL, 0,
195 (const char *) str1,
196 (const char *) str3,
197 NULL, int2, 0, msg, str1, int2, str3);
198}
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000199
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000200/**
201 * xmlErrValidWarning:
202 * @ctxt: an XML validation parser context
203 * @node: the node raising the error
204 * @error: the error number
William M. Brackedb65a72004-02-06 07:36:04 +0000205 * @str1: extra information
206 * @str2: extra information
207 * @str3: extra information
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000208 *
William M. Brackedb65a72004-02-06 07:36:04 +0000209 * Handle a validation error, provide contextual information
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000210 */
211static void
William M. Brackedb65a72004-02-06 07:36:04 +0000212xmlErrValidWarning(xmlValidCtxtPtr ctxt,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000213 xmlNodePtr node, xmlParserErrors error,
214 const char *msg, const xmlChar * str1,
215 const xmlChar * str2, const xmlChar * str3)
216{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000217 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000218 xmlGenericErrorFunc channel = NULL;
219 xmlParserCtxtPtr pctxt = NULL;
220 void *data = NULL;
221
222 if (ctxt != NULL) {
William M. Bracke4d526f2004-12-18 00:01:21 +0000223 channel = ctxt->warning;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000224 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000225 /* Use the special values to detect if it is part of a parsing
226 context */
227 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
228 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
229 pctxt = ctxt->userData;
230 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000231 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000232 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000233 XML_ERR_WARNING, NULL, 0,
234 (const char *) str1,
235 (const char *) str1,
236 (const char *) str3, 0, 0, msg, str1, str2, str3);
237}
238
239
Daniel Veillardea7751d2002-12-20 00:16:24 +0000240
241#ifdef LIBXML_REGEXP_ENABLED
242/*
243 * If regexp are enabled we can do continuous validation without the
244 * need of a tree to validate the content model. this is done in each
245 * callbacks.
246 * Each xmlValidState represent the validation state associated to the
247 * set of nodes currently open from the document root to the current element.
248 */
249
250
251typedef struct _xmlValidState {
252 xmlElementPtr elemDecl; /* pointer to the content model */
253 xmlNodePtr node; /* pointer to the current node */
254 xmlRegExecCtxtPtr exec; /* regexp runtime */
255} _xmlValidState;
256
257
258static int
259vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000260 if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000261 ctxt->vstateMax = 10;
262 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
263 sizeof(ctxt->vstateTab[0]));
264 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000265 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000266 return(-1);
267 }
268 }
269
270 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000271 xmlValidState *tmp;
272
273 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
274 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
275 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000276 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000277 return(-1);
278 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000279 ctxt->vstateMax *= 2;
280 ctxt->vstateTab = tmp;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000281 }
282 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
283 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
284 ctxt->vstateTab[ctxt->vstateNr].node = node;
285 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
286 if (elemDecl->contModel == NULL)
287 xmlValidBuildContentModel(ctxt, elemDecl);
288 if (elemDecl->contModel != NULL) {
289 ctxt->vstateTab[ctxt->vstateNr].exec =
290 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
291 } else {
292 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000293 xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
294 XML_ERR_INTERNAL_ERROR,
295 "Failed to build content model regexp for %s\n",
296 node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000297 }
298 }
299 return(ctxt->vstateNr++);
300}
301
302static int
303vstateVPop(xmlValidCtxtPtr ctxt) {
304 xmlElementPtr elemDecl;
305
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000306 if (ctxt->vstateNr < 1) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000307 ctxt->vstateNr--;
308 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
309 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
310 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
311 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
312 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
313 }
314 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
315 if (ctxt->vstateNr >= 1)
316 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
317 else
318 ctxt->vstate = NULL;
319 return(ctxt->vstateNr);
320}
321
322#else /* not LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000323/*
Daniel Veillard1c732d22002-11-30 11:22:59 +0000324 * If regexp are not enabled, it uses a home made algorithm less
325 * complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000326 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000327 * only restriction is on the deepness of the tree limited by the
328 * size of the occurs bitfield
329 *
330 * this is the content of a saved state for rollbacks
331 */
332
333#define ROLLBACK_OR 0
334#define ROLLBACK_PARENT 1
335
Daniel Veillardb44025c2001-10-11 22:55:55 +0000336typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000337 xmlElementContentPtr cont; /* pointer to the content model subtree */
338 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000339 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000340 unsigned char depth; /* current depth in the overall tree */
341 unsigned char state; /* ROLLBACK_XXX */
342} _xmlValidState;
343
Daniel Veillardfc57b412002-04-29 15:50:14 +0000344#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000345#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
346#define CONT ctxt->vstate->cont
347#define NODE ctxt->vstate->node
348#define DEPTH ctxt->vstate->depth
349#define OCCURS ctxt->vstate->occurs
350#define STATE ctxt->vstate->state
351
Daniel Veillard5344c602001-12-31 16:37:34 +0000352#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
353#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000354
Daniel Veillard5344c602001-12-31 16:37:34 +0000355#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
356#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000357
358static int
359vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
360 xmlNodePtr node, unsigned char depth, long occurs,
361 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000362 int i = ctxt->vstateNr - 1;
363
Daniel Veillard940492d2002-04-15 10:15:25 +0000364 if (ctxt->vstateNr > MAX_RECURSE) {
365 return(-1);
366 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000367 if (ctxt->vstateTab == NULL) {
368 ctxt->vstateMax = 8;
369 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
370 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
371 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000372 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000373 return(-1);
374 }
375 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000376 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000377 xmlValidState *tmp;
378
379 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
380 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
381 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000382 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard940492d2002-04-15 10:15:25 +0000383 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000384 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000385 ctxt->vstateMax *= 2;
386 ctxt->vstateTab = tmp;
Daniel Veillard06803992001-04-22 10:35:56 +0000387 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000388 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000389 /*
390 * Don't push on the stack a state already here
391 */
392 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
393 (ctxt->vstateTab[i].node == node) &&
394 (ctxt->vstateTab[i].depth == depth) &&
395 (ctxt->vstateTab[i].occurs == occurs) &&
396 (ctxt->vstateTab[i].state == state))
397 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000398 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
399 ctxt->vstateTab[ctxt->vstateNr].node = node;
400 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
401 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
402 ctxt->vstateTab[ctxt->vstateNr].state = state;
403 return(ctxt->vstateNr++);
404}
405
406static int
407vstateVPop(xmlValidCtxtPtr ctxt) {
408 if (ctxt->vstateNr <= 1) return(-1);
409 ctxt->vstateNr--;
410 ctxt->vstate = &ctxt->vstateTab[0];
411 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
412 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
413 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
414 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
415 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
416 return(ctxt->vstateNr);
417}
418
Daniel Veillard118aed72002-09-24 14:13:13 +0000419#endif /* LIBXML_REGEXP_ENABLED */
420
Daniel Veillard1c732d22002-11-30 11:22:59 +0000421static int
422nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
423{
424 if (ctxt->nodeMax <= 0) {
425 ctxt->nodeMax = 4;
426 ctxt->nodeTab =
427 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
428 sizeof(ctxt->nodeTab[0]));
429 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000430 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000431 ctxt->nodeMax = 0;
432 return (0);
433 }
434 }
435 if (ctxt->nodeNr >= ctxt->nodeMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000436 xmlNodePtr *tmp;
437 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
438 ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
439 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000440 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000441 return (0);
442 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000443 ctxt->nodeMax *= 2;
444 ctxt->nodeTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000445 }
446 ctxt->nodeTab[ctxt->nodeNr] = value;
447 ctxt->node = value;
448 return (ctxt->nodeNr++);
449}
450static xmlNodePtr
451nodeVPop(xmlValidCtxtPtr ctxt)
452{
453 xmlNodePtr ret;
454
455 if (ctxt->nodeNr <= 0)
456 return (0);
457 ctxt->nodeNr--;
458 if (ctxt->nodeNr > 0)
459 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
460 else
461 ctxt->node = NULL;
462 ret = ctxt->nodeTab[ctxt->nodeNr];
463 ctxt->nodeTab[ctxt->nodeNr] = 0;
464 return (ret);
465}
Owen Taylor3473f882001-02-23 17:55:21 +0000466
Owen Taylor3473f882001-02-23 17:55:21 +0000467#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000468static void
469xmlValidPrintNode(xmlNodePtr cur) {
470 if (cur == NULL) {
471 xmlGenericError(xmlGenericErrorContext, "null");
472 return;
473 }
474 switch (cur->type) {
475 case XML_ELEMENT_NODE:
476 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
477 break;
478 case XML_TEXT_NODE:
479 xmlGenericError(xmlGenericErrorContext, "text ");
480 break;
481 case XML_CDATA_SECTION_NODE:
482 xmlGenericError(xmlGenericErrorContext, "cdata ");
483 break;
484 case XML_ENTITY_REF_NODE:
485 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
486 break;
487 case XML_PI_NODE:
488 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
489 break;
490 case XML_COMMENT_NODE:
491 xmlGenericError(xmlGenericErrorContext, "comment ");
492 break;
493 case XML_ATTRIBUTE_NODE:
494 xmlGenericError(xmlGenericErrorContext, "?attr? ");
495 break;
496 case XML_ENTITY_NODE:
497 xmlGenericError(xmlGenericErrorContext, "?ent? ");
498 break;
499 case XML_DOCUMENT_NODE:
500 xmlGenericError(xmlGenericErrorContext, "?doc? ");
501 break;
502 case XML_DOCUMENT_TYPE_NODE:
503 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
504 break;
505 case XML_DOCUMENT_FRAG_NODE:
506 xmlGenericError(xmlGenericErrorContext, "?frag? ");
507 break;
508 case XML_NOTATION_NODE:
509 xmlGenericError(xmlGenericErrorContext, "?nota? ");
510 break;
511 case XML_HTML_DOCUMENT_NODE:
512 xmlGenericError(xmlGenericErrorContext, "?html? ");
513 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000514#ifdef LIBXML_DOCB_ENABLED
515 case XML_DOCB_DOCUMENT_NODE:
516 xmlGenericError(xmlGenericErrorContext, "?docb? ");
517 break;
518#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000519 case XML_DTD_NODE:
520 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
521 break;
522 case XML_ELEMENT_DECL:
523 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
524 break;
525 case XML_ATTRIBUTE_DECL:
526 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
527 break;
528 case XML_ENTITY_DECL:
529 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
530 break;
531 case XML_NAMESPACE_DECL:
532 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
533 break;
534 case XML_XINCLUDE_START:
535 xmlGenericError(xmlGenericErrorContext, "incstart ");
536 break;
537 case XML_XINCLUDE_END:
538 xmlGenericError(xmlGenericErrorContext, "incend ");
539 break;
540 }
541}
542
543static void
544xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000545 if (cur == NULL)
546 xmlGenericError(xmlGenericErrorContext, "null ");
547 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000548 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000549 cur = cur->next;
550 }
551}
552
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000553static void
554xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Daniel Veillard83391282003-03-06 21:37:30 +0000555 char expr[5000];
Owen Taylor3473f882001-02-23 17:55:21 +0000556
557 expr[0] = 0;
558 xmlGenericError(xmlGenericErrorContext, "valid: ");
559 xmlValidPrintNodeList(cur);
560 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000561 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000562 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
563}
564
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000565static void
566xmlValidDebugState(xmlValidStatePtr state) {
567 xmlGenericError(xmlGenericErrorContext, "(");
568 if (state->cont == NULL)
569 xmlGenericError(xmlGenericErrorContext, "null,");
570 else
571 switch (state->cont->type) {
572 case XML_ELEMENT_CONTENT_PCDATA:
573 xmlGenericError(xmlGenericErrorContext, "pcdata,");
574 break;
575 case XML_ELEMENT_CONTENT_ELEMENT:
576 xmlGenericError(xmlGenericErrorContext, "%s,",
577 state->cont->name);
578 break;
579 case XML_ELEMENT_CONTENT_SEQ:
580 xmlGenericError(xmlGenericErrorContext, "seq,");
581 break;
582 case XML_ELEMENT_CONTENT_OR:
583 xmlGenericError(xmlGenericErrorContext, "or,");
584 break;
585 }
586 xmlValidPrintNode(state->node);
587 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
588 state->depth, state->occurs, state->state);
589}
590
591static void
592xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
593 int i, j;
594
595 xmlGenericError(xmlGenericErrorContext, "state: ");
596 xmlValidDebugState(ctxt->vstate);
597 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
598 ctxt->vstateNr - 1);
599 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
600 xmlValidDebugState(&ctxt->vstateTab[j]);
601 xmlGenericError(xmlGenericErrorContext, "\n");
602}
603
604/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000605#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000606 *****/
607
608#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000609#define DEBUG_VALID_MSG(m) \
610 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
611
Owen Taylor3473f882001-02-23 17:55:21 +0000612#else
613#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000614#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000615#endif
616
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000617/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000618
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000619
Owen Taylor3473f882001-02-23 17:55:21 +0000620#define CHECK_DTD \
621 if (doc == NULL) return(0); \
622 else if ((doc->intSubset == NULL) && \
623 (doc->extSubset == NULL)) return(0)
624
Owen Taylor3473f882001-02-23 17:55:21 +0000625xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
626
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000627#ifdef LIBXML_REGEXP_ENABLED
628
629/************************************************************************
630 * *
631 * Content model validation based on the regexps *
632 * *
633 ************************************************************************/
634
635/**
636 * xmlValidBuildAContentModel:
637 * @content: the content model
638 * @ctxt: the schema parser context
639 * @name: the element name whose content is being built
640 *
641 * Generate the automata sequence needed for that type
642 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000643 * Returns 1 if successful or 0 in case of error.
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000644 */
645static int
646xmlValidBuildAContentModel(xmlElementContentPtr content,
647 xmlValidCtxtPtr ctxt,
648 const xmlChar *name) {
649 if (content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000650 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
651 "Found NULL content in content model of %s\n",
652 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000653 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000654 }
655 switch (content->type) {
656 case XML_ELEMENT_CONTENT_PCDATA:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000657 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
658 "Found PCDATA in content model of %s\n",
659 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000660 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000661 break;
662 case XML_ELEMENT_CONTENT_ELEMENT: {
663 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillardc00cda82003-04-07 10:22:39 +0000664 xmlChar fn[50];
665 xmlChar *fullname;
666
667 fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
668 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000669 xmlVErrMemory(ctxt, "Building content model");
Daniel Veillardc00cda82003-04-07 10:22:39 +0000670 return(0);
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000671 }
672
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000673 switch (content->ocur) {
674 case XML_ELEMENT_CONTENT_ONCE:
675 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000676 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000677 break;
678 case XML_ELEMENT_CONTENT_OPT:
679 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000680 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000681 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
682 break;
683 case XML_ELEMENT_CONTENT_PLUS:
684 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000685 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000686 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000687 ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000688 break;
689 case XML_ELEMENT_CONTENT_MULT:
William M. Brack8b0cbb02004-04-17 13:31:06 +0000690 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
691 ctxt->state, NULL);
692 xmlAutomataNewTransition(ctxt->am,
693 ctxt->state, ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000694 break;
695 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000696 if ((fullname != fn) && (fullname != content->name))
697 xmlFree(fullname);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000698 break;
699 }
700 case XML_ELEMENT_CONTENT_SEQ: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000701 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000702 xmlElementContentOccur ocur;
703
704 /*
705 * Simply iterate over the content
706 */
707 oldstate = ctxt->state;
708 ocur = content->ocur;
Daniel Veillardf4be0182003-02-24 19:54:33 +0000709 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
710 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
711 oldstate = ctxt->state;
712 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000713 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000714 xmlValidBuildAContentModel(content->c1, ctxt, name);
715 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000716 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
717 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
718 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000719 oldend = ctxt->state;
720 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000721 switch (ocur) {
722 case XML_ELEMENT_CONTENT_ONCE:
723 break;
724 case XML_ELEMENT_CONTENT_OPT:
725 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
726 break;
727 case XML_ELEMENT_CONTENT_MULT:
728 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000729 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000730 break;
731 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000732 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000733 break;
734 }
735 break;
736 }
737 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000738 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000739 xmlElementContentOccur ocur;
740
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000741 ocur = content->ocur;
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000742 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
743 (ocur == XML_ELEMENT_CONTENT_MULT)) {
744 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
745 ctxt->state, NULL);
746 }
747 oldstate = ctxt->state;
748 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000749
750 /*
751 * iterate over the subtypes and remerge the end with an
752 * epsilon transition
753 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000754 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000755 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000756 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000757 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000758 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000759 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
760 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000761 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000762 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000763 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
764 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000765 switch (ocur) {
766 case XML_ELEMENT_CONTENT_ONCE:
767 break;
768 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000769 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000770 break;
771 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000772 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
773 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000774 break;
775 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000776 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000777 break;
778 }
779 break;
780 }
781 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000782 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
783 "ContentModel broken for element %s\n",
784 (const char *) name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000785 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000786 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000787 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000788}
789/**
790 * xmlValidBuildContentModel:
791 * @ctxt: a validation context
792 * @elem: an element declaration node
793 *
794 * (Re)Build the automata associated to the content model of this
795 * element
796 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000797 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000798 */
799int
800xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000801
802 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000803 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000804 if (elem->type != XML_ELEMENT_DECL)
805 return(0);
806 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
807 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000808 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000809 if (elem->contModel != NULL) {
810 if (!xmlRegexpIsDeterminist(elem->contModel)) {
811 ctxt->valid = 0;
812 return(0);
813 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000814 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000815 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000816
817 ctxt->am = xmlNewAutomata();
818 if (ctxt->am == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000819 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
820 XML_ERR_INTERNAL_ERROR,
821 "Cannot create automata for element %s\n",
822 elem->name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000823 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000824 }
William M. Brack78637da2003-07-31 14:47:38 +0000825 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000826 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
827 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000828 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000829 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000830 char expr[5000];
831 expr[0] = 0;
832 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000833 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
834 XML_DTD_CONTENT_NOT_DETERMINIST,
835 "Content model of %s is not determinist: %s\n",
836 elem->name, BAD_CAST expr, NULL);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000837#ifdef DEBUG_REGEXP_ALGO
838 xmlRegexpPrint(stderr, elem->contModel);
839#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000840 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000841 ctxt->state = NULL;
842 xmlFreeAutomata(ctxt->am);
843 ctxt->am = NULL;
844 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000845 }
846 ctxt->state = NULL;
847 xmlFreeAutomata(ctxt->am);
848 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000849 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000850}
851
852#endif /* LIBXML_REGEXP_ENABLED */
853
Owen Taylor3473f882001-02-23 17:55:21 +0000854/****************************************************************
855 * *
856 * Util functions for data allocation/deallocation *
857 * *
858 ****************************************************************/
859
860/**
Daniel Veillarda37aab82003-06-09 09:10:36 +0000861 * xmlNewValidCtxt:
862 *
863 * Allocate a validation context structure.
864 *
865 * Returns NULL if not, otherwise the new validation context structure
866 */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000867xmlValidCtxtPtr xmlNewValidCtxt(void) {
Daniel Veillarda37aab82003-06-09 09:10:36 +0000868 xmlValidCtxtPtr ret;
869
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000870 if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000871 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda37aab82003-06-09 09:10:36 +0000872 return (NULL);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000873 }
Daniel Veillarda37aab82003-06-09 09:10:36 +0000874
875 (void) memset(ret, 0, sizeof (xmlValidCtxt));
876
877 return (ret);
878}
879
880/**
881 * xmlFreeValidCtxt:
882 * @cur: the validation context to free
883 *
884 * Free a validation context structure.
885 */
886void
887xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
Daniel Veillardc0be74b2004-11-03 19:16:55 +0000888 if (cur->vstateTab != NULL)
889 xmlFree(cur->vstateTab);
890 if (cur->nodeTab != NULL)
891 xmlFree(cur->nodeTab);
Daniel Veillarda37aab82003-06-09 09:10:36 +0000892 xmlFree(cur);
893}
894
Daniel Veillard4432df22003-09-28 18:58:27 +0000895#endif /* LIBXML_VALID_ENABLED */
896
Daniel Veillarda37aab82003-06-09 09:10:36 +0000897/**
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000898 * xmlNewDocElementContent:
899 * @doc: the document
Owen Taylor3473f882001-02-23 17:55:21 +0000900 * @name: the subelement name or NULL
901 * @type: the type of element content decl
902 *
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000903 * Allocate an element content structure for the document.
Owen Taylor3473f882001-02-23 17:55:21 +0000904 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000905 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000906 */
907xmlElementContentPtr
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000908xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name,
909 xmlElementContentType type) {
Owen Taylor3473f882001-02-23 17:55:21 +0000910 xmlElementContentPtr ret;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000911 xmlDictPtr dict = NULL;
912
913 if (doc != NULL)
914 dict = doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +0000915
916 switch(type) {
917 case XML_ELEMENT_CONTENT_ELEMENT:
918 if (name == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000919 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
920 "xmlNewElementContent : name == NULL !\n",
921 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000922 }
923 break;
924 case XML_ELEMENT_CONTENT_PCDATA:
925 case XML_ELEMENT_CONTENT_SEQ:
926 case XML_ELEMENT_CONTENT_OR:
927 if (name != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000928 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
929 "xmlNewElementContent : name != NULL !\n",
930 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000931 }
932 break;
933 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000934 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
935 "Internal: ELEMENT content corrupted invalid type\n",
936 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000937 return(NULL);
938 }
939 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
940 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000941 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000942 return(NULL);
943 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000944 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000945 ret->type = type;
946 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000947 if (name != NULL) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000948 int l;
949 const xmlChar *tmp;
950
951 tmp = xmlSplitQName3(name, &l);
952 if (tmp == NULL) {
953 if (dict == NULL)
954 ret->name = xmlStrdup(name);
955 else
956 ret->name = xmlDictLookup(dict, name, -1);
957 } else {
958 if (dict == NULL) {
959 ret->prefix = xmlStrndup(name, l);
960 ret->name = xmlStrdup(tmp);
961 } else {
962 ret->prefix = xmlDictLookup(dict, name, l);
963 ret->name = xmlDictLookup(dict, tmp, -1);
964 }
965 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000966 }
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000967 return(ret);
968}
969
970/**
971 * xmlNewElementContent:
972 * @name: the subelement name or NULL
973 * @type: the type of element content decl
974 *
975 * Allocate an element content structure.
976 * Deprecated in favor of xmlNewDocElementContent
977 *
978 * Returns NULL if not, otherwise the new element content structure
979 */
980xmlElementContentPtr
981xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
982 return(xmlNewDocElementContent(NULL, name, type));
983}
984
985/**
986 * xmlCopyDocElementContent:
987 * @doc: the document owning the element declaration
988 * @cur: An element content pointer.
989 *
990 * Build a copy of an element content description.
991 *
992 * Returns the new xmlElementContentPtr or NULL in case of error.
993 */
994xmlElementContentPtr
995xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
996 xmlElementContentPtr ret = NULL, prev = NULL, tmp;
997 xmlDictPtr dict = NULL;
998
999 if (cur == NULL) return(NULL);
1000
1001 if (doc != NULL)
1002 dict = doc->dict;
1003
1004 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
1005 if (ret == NULL) {
1006 xmlVErrMemory(NULL, "malloc failed");
1007 return(NULL);
1008 }
1009 memset(ret, 0, sizeof(xmlElementContent));
1010 ret->type = cur->type;
1011 ret->ocur = cur->ocur;
1012 if (cur->name != NULL) {
1013 if (dict)
1014 ret->name = xmlDictLookup(dict, cur->name, -1);
1015 else
1016 ret->name = xmlStrdup(cur->name);
1017 }
1018
1019 if (cur->prefix != NULL) {
1020 if (dict)
1021 ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
1022 else
1023 ret->prefix = xmlStrdup(cur->prefix);
1024 }
1025 if (cur->c1 != NULL)
1026 ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
1027 if (ret->c1 != NULL)
1028 ret->c1->parent = ret;
1029 if (cur->c2 != NULL) {
1030 prev = ret;
1031 cur = cur->c2;
1032 while (cur != NULL) {
1033 tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
1034 if (tmp == NULL) {
1035 xmlVErrMemory(NULL, "malloc failed");
1036 return(ret);
1037 }
1038 memset(tmp, 0, sizeof(xmlElementContent));
1039 tmp->type = cur->type;
1040 tmp->ocur = cur->ocur;
1041 prev->c2 = tmp;
1042 if (cur->name != NULL) {
1043 if (dict)
1044 tmp->name = xmlDictLookup(dict, cur->name, -1);
1045 else
1046 tmp->name = xmlStrdup(cur->name);
1047 }
1048
1049 if (cur->prefix != NULL) {
1050 if (dict)
1051 tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
1052 else
1053 tmp->prefix = xmlStrdup(cur->prefix);
1054 }
1055 if (cur->c1 != NULL)
1056 tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
1057 if (tmp->c1 != NULL)
1058 tmp->c1->parent = ret;
1059 prev = tmp;
1060 cur = cur->c2;
1061 }
1062 }
Owen Taylor3473f882001-02-23 17:55:21 +00001063 return(ret);
1064}
1065
1066/**
1067 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +00001068 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +00001069 *
1070 * Build a copy of an element content description.
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001071 * Deprecated, use xmlCopyDocElementContent instead
Owen Taylor3473f882001-02-23 17:55:21 +00001072 *
1073 * Returns the new xmlElementContentPtr or NULL in case of error.
1074 */
1075xmlElementContentPtr
1076xmlCopyElementContent(xmlElementContentPtr cur) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001077 return(xmlCopyDocElementContent(NULL, cur));
1078}
Owen Taylor3473f882001-02-23 17:55:21 +00001079
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001080/**
1081 * xmlFreeDocElementContent:
1082 * @doc: the document owning the element declaration
1083 * @cur: the element content tree to free
1084 *
1085 * Free an element content structure. The whole subtree is removed.
1086 */
1087void
1088xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
1089 xmlElementContentPtr next;
1090 xmlDictPtr dict = NULL;
1091
1092 if (doc != NULL)
1093 dict = doc->dict;
1094
1095 while (cur != NULL) {
1096 next = cur->c2;
1097 switch (cur->type) {
1098 case XML_ELEMENT_CONTENT_PCDATA:
1099 case XML_ELEMENT_CONTENT_ELEMENT:
1100 case XML_ELEMENT_CONTENT_SEQ:
1101 case XML_ELEMENT_CONTENT_OR:
1102 break;
1103 default:
1104 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1105 "Internal: ELEMENT content corrupted invalid type\n",
1106 NULL);
1107 return;
1108 }
1109 if (cur->c1 != NULL) xmlFreeDocElementContent(doc, cur->c1);
1110 if (dict) {
1111 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
1112 xmlFree((xmlChar *) cur->name);
1113 if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
1114 xmlFree((xmlChar *) cur->prefix);
1115 } else {
1116 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
1117 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
1118 }
1119 xmlFree(cur);
1120 cur = next;
Owen Taylor3473f882001-02-23 17:55:21 +00001121 }
Owen Taylor3473f882001-02-23 17:55:21 +00001122}
1123
1124/**
1125 * xmlFreeElementContent:
1126 * @cur: the element content tree to free
1127 *
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001128 * Free an element content structure. The whole subtree is removed.
1129 * Deprecated, use xmlFreeDocElementContent instead
Owen Taylor3473f882001-02-23 17:55:21 +00001130 */
1131void
1132xmlFreeElementContent(xmlElementContentPtr cur) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001133 xmlFreeDocElementContent(NULL, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001134}
1135
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001136#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001137/**
1138 * xmlDumpElementContent:
1139 * @buf: An XML buffer
1140 * @content: An element table
1141 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1142 *
1143 * This will dump the content of the element table as an XML DTD definition
1144 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001145static void
Owen Taylor3473f882001-02-23 17:55:21 +00001146xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
1147 if (content == NULL) return;
1148
1149 if (glob) xmlBufferWriteChar(buf, "(");
1150 switch (content->type) {
1151 case XML_ELEMENT_CONTENT_PCDATA:
1152 xmlBufferWriteChar(buf, "#PCDATA");
1153 break;
1154 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001155 if (content->prefix != NULL) {
1156 xmlBufferWriteCHAR(buf, content->prefix);
1157 xmlBufferWriteChar(buf, ":");
1158 }
Owen Taylor3473f882001-02-23 17:55:21 +00001159 xmlBufferWriteCHAR(buf, content->name);
1160 break;
1161 case XML_ELEMENT_CONTENT_SEQ:
1162 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1163 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1164 xmlDumpElementContent(buf, content->c1, 1);
1165 else
1166 xmlDumpElementContent(buf, content->c1, 0);
1167 xmlBufferWriteChar(buf, " , ");
William M. Brack4119d1c2004-06-24 02:24:44 +00001168 if ((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1169 ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) &&
1170 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
Owen Taylor3473f882001-02-23 17:55:21 +00001171 xmlDumpElementContent(buf, content->c2, 1);
1172 else
1173 xmlDumpElementContent(buf, content->c2, 0);
1174 break;
1175 case XML_ELEMENT_CONTENT_OR:
1176 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1177 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1178 xmlDumpElementContent(buf, content->c1, 1);
1179 else
1180 xmlDumpElementContent(buf, content->c1, 0);
1181 xmlBufferWriteChar(buf, " | ");
William M. Brack4119d1c2004-06-24 02:24:44 +00001182 if ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1183 ((content->c2->type == XML_ELEMENT_CONTENT_OR) &&
1184 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
Owen Taylor3473f882001-02-23 17:55:21 +00001185 xmlDumpElementContent(buf, content->c2, 1);
1186 else
1187 xmlDumpElementContent(buf, content->c2, 0);
1188 break;
1189 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001190 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1191 "Internal: ELEMENT content corrupted invalid type\n",
1192 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001193 }
1194 if (glob)
1195 xmlBufferWriteChar(buf, ")");
1196 switch (content->ocur) {
1197 case XML_ELEMENT_CONTENT_ONCE:
1198 break;
1199 case XML_ELEMENT_CONTENT_OPT:
1200 xmlBufferWriteChar(buf, "?");
1201 break;
1202 case XML_ELEMENT_CONTENT_MULT:
1203 xmlBufferWriteChar(buf, "*");
1204 break;
1205 case XML_ELEMENT_CONTENT_PLUS:
1206 xmlBufferWriteChar(buf, "+");
1207 break;
1208 }
1209}
1210
1211/**
1212 * xmlSprintfElementContent:
1213 * @buf: an output buffer
1214 * @content: An element table
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001215 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001216 *
Daniel Veillardd3d06722001-08-15 12:06:36 +00001217 * Deprecated, unsafe, use xmlSnprintfElementContent
1218 */
1219void
1220xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
1221 xmlElementContentPtr content ATTRIBUTE_UNUSED,
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001222 int englob ATTRIBUTE_UNUSED) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001223}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001224#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardd3d06722001-08-15 12:06:36 +00001225
1226/**
1227 * xmlSnprintfElementContent:
1228 * @buf: an output buffer
1229 * @size: the buffer size
1230 * @content: An element table
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001231 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
Daniel Veillardd3d06722001-08-15 12:06:36 +00001232 *
Owen Taylor3473f882001-02-23 17:55:21 +00001233 * This will dump the content of the element content definition
1234 * Intended just for the debug routine
1235 */
1236void
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001237xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001238 int len;
1239
Owen Taylor3473f882001-02-23 17:55:21 +00001240 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +00001241 len = strlen(buf);
1242 if (size - len < 50) {
1243 if ((size - len > 4) && (buf[len - 1] != '.'))
1244 strcat(buf, " ...");
1245 return;
1246 }
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001247 if (englob) strcat(buf, "(");
Owen Taylor3473f882001-02-23 17:55:21 +00001248 switch (content->type) {
1249 case XML_ELEMENT_CONTENT_PCDATA:
1250 strcat(buf, "#PCDATA");
1251 break;
1252 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001253 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001254 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001255 strcat(buf, " ...");
1256 return;
1257 }
1258 strcat(buf, (char *) content->prefix);
1259 strcat(buf, ":");
1260 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001261 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001262 strcat(buf, " ...");
1263 return;
1264 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001265 if (content->name != NULL)
1266 strcat(buf, (char *) content->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001267 break;
1268 case XML_ELEMENT_CONTENT_SEQ:
1269 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1270 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001271 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001272 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001273 xmlSnprintfElementContent(buf, size, content->c1, 0);
1274 len = strlen(buf);
1275 if (size - len < 50) {
1276 if ((size - len > 4) && (buf[len - 1] != '.'))
1277 strcat(buf, " ...");
1278 return;
1279 }
Owen Taylor3473f882001-02-23 17:55:21 +00001280 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001281 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1282 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1283 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001284 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001285 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001286 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001287 break;
1288 case XML_ELEMENT_CONTENT_OR:
1289 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1290 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001291 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001292 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001293 xmlSnprintfElementContent(buf, size, content->c1, 0);
1294 len = strlen(buf);
1295 if (size - len < 50) {
1296 if ((size - len > 4) && (buf[len - 1] != '.'))
1297 strcat(buf, " ...");
1298 return;
1299 }
Owen Taylor3473f882001-02-23 17:55:21 +00001300 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001301 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1302 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1303 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001304 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001305 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001306 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001307 break;
1308 }
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001309 if (englob)
Owen Taylor3473f882001-02-23 17:55:21 +00001310 strcat(buf, ")");
1311 switch (content->ocur) {
1312 case XML_ELEMENT_CONTENT_ONCE:
1313 break;
1314 case XML_ELEMENT_CONTENT_OPT:
1315 strcat(buf, "?");
1316 break;
1317 case XML_ELEMENT_CONTENT_MULT:
1318 strcat(buf, "*");
1319 break;
1320 case XML_ELEMENT_CONTENT_PLUS:
1321 strcat(buf, "+");
1322 break;
1323 }
1324}
1325
1326/****************************************************************
1327 * *
1328 * Registration of DTD declarations *
1329 * *
1330 ****************************************************************/
1331
1332/**
Owen Taylor3473f882001-02-23 17:55:21 +00001333 * xmlFreeElement:
1334 * @elem: An element
1335 *
1336 * Deallocate the memory used by an element definition
1337 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001338static void
Owen Taylor3473f882001-02-23 17:55:21 +00001339xmlFreeElement(xmlElementPtr elem) {
1340 if (elem == NULL) return;
1341 xmlUnlinkNode((xmlNodePtr) elem);
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001342 xmlFreeDocElementContent(elem->doc, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001343 if (elem->name != NULL)
1344 xmlFree((xmlChar *) elem->name);
1345 if (elem->prefix != NULL)
1346 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001347#ifdef LIBXML_REGEXP_ENABLED
1348 if (elem->contModel != NULL)
1349 xmlRegFreeRegexp(elem->contModel);
1350#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001351 xmlFree(elem);
1352}
1353
1354
1355/**
1356 * xmlAddElementDecl:
1357 * @ctxt: the validation context
1358 * @dtd: pointer to the DTD
1359 * @name: the entity name
1360 * @type: the element type
1361 * @content: the element content tree or NULL
1362 *
1363 * Register a new element declaration
1364 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001365 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001366 */
1367xmlElementPtr
William M. Brackedb65a72004-02-06 07:36:04 +00001368xmlAddElementDecl(xmlValidCtxtPtr ctxt,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001369 xmlDtdPtr dtd, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001370 xmlElementTypeVal type,
1371 xmlElementContentPtr content) {
1372 xmlElementPtr ret;
1373 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001374 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001375 xmlChar *ns, *uqname;
1376
1377 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001378 return(NULL);
1379 }
1380 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001381 return(NULL);
1382 }
Daniel Veillard316a5c32005-01-23 22:56:39 +00001383
Owen Taylor3473f882001-02-23 17:55:21 +00001384 switch (type) {
1385 case XML_ELEMENT_TYPE_EMPTY:
1386 if (content != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001387 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1388 "xmlAddElementDecl: content != NULL for EMPTY\n",
1389 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001390 return(NULL);
1391 }
1392 break;
1393 case XML_ELEMENT_TYPE_ANY:
1394 if (content != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001395 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1396 "xmlAddElementDecl: content != NULL for ANY\n",
1397 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001398 return(NULL);
1399 }
1400 break;
1401 case XML_ELEMENT_TYPE_MIXED:
1402 if (content == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001403 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1404 "xmlAddElementDecl: content == NULL for MIXED\n",
1405 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001406 return(NULL);
1407 }
1408 break;
1409 case XML_ELEMENT_TYPE_ELEMENT:
1410 if (content == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001411 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1412 "xmlAddElementDecl: content == NULL for ELEMENT\n",
1413 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001414 return(NULL);
1415 }
1416 break;
1417 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001418 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1419 "Internal: ELEMENT decl corrupted invalid type\n",
1420 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001421 return(NULL);
1422 }
1423
1424 /*
1425 * check if name is a QName
1426 */
1427 uqname = xmlSplitQName2(name, &ns);
1428 if (uqname != NULL)
1429 name = uqname;
1430
1431 /*
1432 * Create the Element table if needed.
1433 */
1434 table = (xmlElementTablePtr) dtd->elements;
1435 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00001436 xmlDictPtr dict = NULL;
1437
1438 if (dtd->doc != NULL)
1439 dict = dtd->doc->dict;
1440 table = xmlHashCreateDict(0, dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001441 dtd->elements = (void *) table;
1442 }
1443 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001444 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001445 "xmlAddElementDecl: Table creation failed!\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001446 if (uqname != NULL)
1447 xmlFree(uqname);
1448 if (ns != NULL)
1449 xmlFree(ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001450 return(NULL);
1451 }
1452
Daniel Veillarda10efa82001-04-18 13:09:01 +00001453 /*
1454 * lookup old attributes inserted on an undefined element in the
1455 * internal subset.
1456 */
1457 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1458 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1459 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1460 oldAttributes = ret->attributes;
1461 ret->attributes = NULL;
1462 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1463 xmlFreeElement(ret);
1464 }
Owen Taylor3473f882001-02-23 17:55:21 +00001465 }
Owen Taylor3473f882001-02-23 17:55:21 +00001466
1467 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001468 * The element may already be present if one of its attribute
1469 * was registered first
1470 */
1471 ret = xmlHashLookup2(table, name, ns);
1472 if (ret != NULL) {
1473 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001474#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001475 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001476 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001477 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001478 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1479 "Redefinition of element %s\n",
1480 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001481#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001482 if (uqname != NULL)
1483 xmlFree(uqname);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001484 if (ns != NULL)
1485 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001486 return(NULL);
1487 }
William M. Brackd6e347e2005-04-15 01:34:41 +00001488 if (ns != NULL) {
1489 xmlFree(ns);
1490 ns = NULL;
1491 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001492 } else {
1493 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1494 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001495 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001496 if (uqname != NULL)
1497 xmlFree(uqname);
1498 if (ns != NULL)
1499 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001500 return(NULL);
1501 }
1502 memset(ret, 0, sizeof(xmlElement));
1503 ret->type = XML_ELEMENT_DECL;
1504
1505 /*
1506 * fill the structure.
1507 */
1508 ret->name = xmlStrdup(name);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001509 if (ret->name == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001510 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001511 if (uqname != NULL)
1512 xmlFree(uqname);
1513 if (ns != NULL)
1514 xmlFree(ns);
1515 xmlFree(ret);
1516 return(NULL);
1517 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001518 ret->prefix = ns;
1519
1520 /*
1521 * Validity Check:
1522 * Insertion must not fail
1523 */
1524 if (xmlHashAddEntry2(table, name, ns, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001525#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001526 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001527 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001528 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001529 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1530 "Redefinition of element %s\n",
1531 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001532#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001533 xmlFreeElement(ret);
1534 if (uqname != NULL)
1535 xmlFree(uqname);
1536 return(NULL);
1537 }
William M. Brack4e52f2f2003-09-14 18:07:39 +00001538 /*
1539 * For new element, may have attributes from earlier
1540 * definition in internal subset
1541 */
1542 ret->attributes = oldAttributes;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001543 }
1544
1545 /*
1546 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001547 */
1548 ret->etype = type;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001549 /*
1550 * Avoid a stupid copy when called by the parser
1551 * and flag it by setting a special parent value
1552 * so the parser doesn't unallocate it.
1553 */
Daniel Veillardc394f732005-01-26 00:04:52 +00001554 if ((ctxt != NULL) &&
1555 ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
1556 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1))) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001557 ret->content = content;
1558 if (content != NULL)
1559 content->parent = (xmlElementContentPtr) 1;
1560 } else {
1561 ret->content = xmlCopyDocElementContent(dtd->doc, content);
1562 }
Owen Taylor3473f882001-02-23 17:55:21 +00001563
1564 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001565 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001566 */
1567 ret->parent = dtd;
1568 ret->doc = dtd->doc;
1569 if (dtd->last == NULL) {
1570 dtd->children = dtd->last = (xmlNodePtr) ret;
1571 } else {
1572 dtd->last->next = (xmlNodePtr) ret;
1573 ret->prev = dtd->last;
1574 dtd->last = (xmlNodePtr) ret;
1575 }
1576 if (uqname != NULL)
1577 xmlFree(uqname);
1578 return(ret);
1579}
1580
1581/**
1582 * xmlFreeElementTable:
1583 * @table: An element table
1584 *
1585 * Deallocate the memory used by an element hash table.
1586 */
1587void
1588xmlFreeElementTable(xmlElementTablePtr table) {
1589 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1590}
1591
Daniel Veillard652327a2003-09-29 18:02:38 +00001592#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001593/**
1594 * xmlCopyElement:
1595 * @elem: An element
1596 *
1597 * Build a copy of an element.
1598 *
1599 * Returns the new xmlElementPtr or NULL in case of error.
1600 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001601static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001602xmlCopyElement(xmlElementPtr elem) {
1603 xmlElementPtr cur;
1604
1605 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1606 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001607 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001608 return(NULL);
1609 }
1610 memset(cur, 0, sizeof(xmlElement));
1611 cur->type = XML_ELEMENT_DECL;
1612 cur->etype = elem->etype;
1613 if (elem->name != NULL)
1614 cur->name = xmlStrdup(elem->name);
1615 else
1616 cur->name = NULL;
1617 if (elem->prefix != NULL)
1618 cur->prefix = xmlStrdup(elem->prefix);
1619 else
1620 cur->prefix = NULL;
1621 cur->content = xmlCopyElementContent(elem->content);
1622 /* TODO : rebuild the attribute list on the copy */
1623 cur->attributes = NULL;
1624 return(cur);
1625}
1626
1627/**
1628 * xmlCopyElementTable:
1629 * @table: An element table
1630 *
1631 * Build a copy of an element table.
1632 *
1633 * Returns the new xmlElementTablePtr or NULL in case of error.
1634 */
1635xmlElementTablePtr
1636xmlCopyElementTable(xmlElementTablePtr table) {
1637 return((xmlElementTablePtr) xmlHashCopy(table,
1638 (xmlHashCopier) xmlCopyElement));
1639}
Daniel Veillard652327a2003-09-29 18:02:38 +00001640#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001641
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001642#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001643/**
1644 * xmlDumpElementDecl:
1645 * @buf: the XML buffer output
1646 * @elem: An element table
1647 *
1648 * This will dump the content of the element declaration as an XML
1649 * DTD definition
1650 */
1651void
1652xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00001653 if ((buf == NULL) || (elem == NULL))
1654 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001655 switch (elem->etype) {
1656 case XML_ELEMENT_TYPE_EMPTY:
1657 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001658 if (elem->prefix != NULL) {
1659 xmlBufferWriteCHAR(buf, elem->prefix);
1660 xmlBufferWriteChar(buf, ":");
1661 }
Owen Taylor3473f882001-02-23 17:55:21 +00001662 xmlBufferWriteCHAR(buf, elem->name);
1663 xmlBufferWriteChar(buf, " EMPTY>\n");
1664 break;
1665 case XML_ELEMENT_TYPE_ANY:
1666 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001667 if (elem->prefix != NULL) {
1668 xmlBufferWriteCHAR(buf, elem->prefix);
1669 xmlBufferWriteChar(buf, ":");
1670 }
Owen Taylor3473f882001-02-23 17:55:21 +00001671 xmlBufferWriteCHAR(buf, elem->name);
1672 xmlBufferWriteChar(buf, " ANY>\n");
1673 break;
1674 case XML_ELEMENT_TYPE_MIXED:
1675 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001676 if (elem->prefix != NULL) {
1677 xmlBufferWriteCHAR(buf, elem->prefix);
1678 xmlBufferWriteChar(buf, ":");
1679 }
Owen Taylor3473f882001-02-23 17:55:21 +00001680 xmlBufferWriteCHAR(buf, elem->name);
1681 xmlBufferWriteChar(buf, " ");
1682 xmlDumpElementContent(buf, elem->content, 1);
1683 xmlBufferWriteChar(buf, ">\n");
1684 break;
1685 case XML_ELEMENT_TYPE_ELEMENT:
1686 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001687 if (elem->prefix != NULL) {
1688 xmlBufferWriteCHAR(buf, elem->prefix);
1689 xmlBufferWriteChar(buf, ":");
1690 }
Owen Taylor3473f882001-02-23 17:55:21 +00001691 xmlBufferWriteCHAR(buf, elem->name);
1692 xmlBufferWriteChar(buf, " ");
1693 xmlDumpElementContent(buf, elem->content, 1);
1694 xmlBufferWriteChar(buf, ">\n");
1695 break;
1696 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001697 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1698 "Internal: ELEMENT struct corrupted invalid type\n",
1699 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001700 }
1701}
1702
1703/**
William M. Brack9e660592003-10-20 14:56:06 +00001704 * xmlDumpElementDeclScan:
1705 * @elem: An element table
1706 * @buf: the XML buffer output
1707 *
1708 * This routine is used by the hash scan function. It just reverses
1709 * the arguments.
1710 */
1711static void
1712xmlDumpElementDeclScan(xmlElementPtr elem, xmlBufferPtr buf) {
1713 xmlDumpElementDecl(buf, elem);
1714}
1715
1716/**
Owen Taylor3473f882001-02-23 17:55:21 +00001717 * xmlDumpElementTable:
1718 * @buf: the XML buffer output
1719 * @table: An element table
1720 *
1721 * This will dump the content of the element table as an XML DTD definition
1722 */
1723void
1724xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00001725 if ((buf == NULL) || (table == NULL))
1726 return;
William M. Brack9e660592003-10-20 14:56:06 +00001727 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001728}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001729#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001730
1731/**
1732 * xmlCreateEnumeration:
1733 * @name: the enumeration name or NULL
1734 *
1735 * create and initialize an enumeration attribute node.
1736 *
1737 * Returns the xmlEnumerationPtr just created or NULL in case
1738 * of error.
1739 */
1740xmlEnumerationPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001741xmlCreateEnumeration(const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00001742 xmlEnumerationPtr ret;
1743
1744 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1745 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001746 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001747 return(NULL);
1748 }
1749 memset(ret, 0, sizeof(xmlEnumeration));
1750
1751 if (name != NULL)
1752 ret->name = xmlStrdup(name);
1753 return(ret);
1754}
1755
1756/**
1757 * xmlFreeEnumeration:
1758 * @cur: the tree to free.
1759 *
1760 * free an enumeration attribute node (recursive).
1761 */
1762void
1763xmlFreeEnumeration(xmlEnumerationPtr cur) {
1764 if (cur == NULL) return;
1765
1766 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1767
1768 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001769 xmlFree(cur);
1770}
1771
Daniel Veillard652327a2003-09-29 18:02:38 +00001772#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001773/**
1774 * xmlCopyEnumeration:
1775 * @cur: the tree to copy.
1776 *
1777 * Copy an enumeration attribute node (recursive).
1778 *
1779 * Returns the xmlEnumerationPtr just created or NULL in case
1780 * of error.
1781 */
1782xmlEnumerationPtr
1783xmlCopyEnumeration(xmlEnumerationPtr cur) {
1784 xmlEnumerationPtr ret;
1785
1786 if (cur == NULL) return(NULL);
1787 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1788
1789 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1790 else ret->next = NULL;
1791
1792 return(ret);
1793}
Daniel Veillard652327a2003-09-29 18:02:38 +00001794#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001795
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001796#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001797/**
1798 * xmlDumpEnumeration:
1799 * @buf: the XML buffer output
1800 * @enum: An enumeration
1801 *
1802 * This will dump the content of the enumeration
1803 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001804static void
Owen Taylor3473f882001-02-23 17:55:21 +00001805xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00001806 if ((buf == NULL) || (cur == NULL))
1807 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001808
1809 xmlBufferWriteCHAR(buf, cur->name);
1810 if (cur->next == NULL)
1811 xmlBufferWriteChar(buf, ")");
1812 else {
1813 xmlBufferWriteChar(buf, " | ");
1814 xmlDumpEnumeration(buf, cur->next);
1815 }
1816}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001817#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001818
Daniel Veillard4432df22003-09-28 18:58:27 +00001819#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001820/**
1821 * xmlScanAttributeDeclCallback:
1822 * @attr: the attribute decl
1823 * @list: the list to update
1824 *
1825 * Callback called by xmlScanAttributeDecl when a new attribute
1826 * has to be entered in the list.
1827 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001828static void
Owen Taylor3473f882001-02-23 17:55:21 +00001829xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001830 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001831 attr->nexth = *list;
1832 *list = attr;
1833}
1834
1835/**
1836 * xmlScanAttributeDecl:
1837 * @dtd: pointer to the DTD
1838 * @elem: the element name
1839 *
1840 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001841 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001842 *
1843 * Returns the pointer to the first attribute decl in the chain,
1844 * possibly NULL.
1845 */
1846xmlAttributePtr
1847xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1848 xmlAttributePtr ret = NULL;
1849 xmlAttributeTablePtr table;
1850
1851 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001852 return(NULL);
1853 }
1854 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001855 return(NULL);
1856 }
1857 table = (xmlAttributeTablePtr) dtd->attributes;
1858 if (table == NULL)
1859 return(NULL);
1860
1861 /* WRONG !!! */
1862 xmlHashScan3(table, NULL, NULL, elem,
1863 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1864 return(ret);
1865}
1866
1867/**
1868 * xmlScanIDAttributeDecl:
1869 * @ctxt: the validation context
1870 * @elem: the element name
Daniel Veillarddbee0f12005-06-27 13:42:57 +00001871 * @err: whether to raise errors here
Owen Taylor3473f882001-02-23 17:55:21 +00001872 *
1873 * Verify that the element don't have too many ID attributes
1874 * declared.
1875 *
1876 * Returns the number of ID attributes found.
1877 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001878static int
Daniel Veillarddbee0f12005-06-27 13:42:57 +00001879xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) {
Owen Taylor3473f882001-02-23 17:55:21 +00001880 xmlAttributePtr cur;
1881 int ret = 0;
1882
1883 if (elem == NULL) return(0);
1884 cur = elem->attributes;
1885 while (cur != NULL) {
1886 if (cur->atype == XML_ATTRIBUTE_ID) {
1887 ret ++;
Daniel Veillarddbee0f12005-06-27 13:42:57 +00001888 if ((ret > 1) && (err))
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001889 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001890 "Element %s has too many ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001891 elem->name, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001892 }
1893 cur = cur->nexth;
1894 }
1895 return(ret);
1896}
Daniel Veillard4432df22003-09-28 18:58:27 +00001897#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001898
1899/**
1900 * xmlFreeAttribute:
1901 * @elem: An attribute
1902 *
1903 * Deallocate the memory used by an attribute definition
1904 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001905static void
Owen Taylor3473f882001-02-23 17:55:21 +00001906xmlFreeAttribute(xmlAttributePtr attr) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001907 xmlDictPtr dict;
1908
Owen Taylor3473f882001-02-23 17:55:21 +00001909 if (attr == NULL) return;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001910 if (attr->doc != NULL)
1911 dict = attr->doc->dict;
1912 else
1913 dict = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001914 xmlUnlinkNode((xmlNodePtr) attr);
1915 if (attr->tree != NULL)
1916 xmlFreeEnumeration(attr->tree);
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001917 if (dict) {
1918 if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
1919 xmlFree((xmlChar *) attr->elem);
1920 if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
1921 xmlFree((xmlChar *) attr->name);
1922 if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
1923 xmlFree((xmlChar *) attr->prefix);
1924 if ((attr->defaultValue != NULL) &&
1925 (!xmlDictOwns(dict, attr->defaultValue)))
1926 xmlFree((xmlChar *) attr->defaultValue);
1927 } else {
1928 if (attr->elem != NULL)
1929 xmlFree((xmlChar *) attr->elem);
1930 if (attr->name != NULL)
1931 xmlFree((xmlChar *) attr->name);
1932 if (attr->defaultValue != NULL)
1933 xmlFree((xmlChar *) attr->defaultValue);
1934 if (attr->prefix != NULL)
1935 xmlFree((xmlChar *) attr->prefix);
1936 }
Owen Taylor3473f882001-02-23 17:55:21 +00001937 xmlFree(attr);
1938}
1939
1940
1941/**
1942 * xmlAddAttributeDecl:
1943 * @ctxt: the validation context
1944 * @dtd: pointer to the DTD
1945 * @elem: the element name
1946 * @name: the attribute name
1947 * @ns: the attribute namespace prefix
1948 * @type: the attribute type
1949 * @def: the attribute default type
1950 * @defaultValue: the attribute default value
1951 * @tree: if it's an enumeration, the associated list
1952 *
1953 * Register a new attribute declaration
1954 * Note that @tree becomes the ownership of the DTD
1955 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001956 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001957 */
1958xmlAttributePtr
William M. Brackedb65a72004-02-06 07:36:04 +00001959xmlAddAttributeDecl(xmlValidCtxtPtr ctxt,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001960 xmlDtdPtr dtd, const xmlChar *elem,
Owen Taylor3473f882001-02-23 17:55:21 +00001961 const xmlChar *name, const xmlChar *ns,
1962 xmlAttributeType type, xmlAttributeDefault def,
1963 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1964 xmlAttributePtr ret;
1965 xmlAttributeTablePtr table;
1966 xmlElementPtr elemDef;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001967 xmlDictPtr dict = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001968
1969 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001970 xmlFreeEnumeration(tree);
1971 return(NULL);
1972 }
1973 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001974 xmlFreeEnumeration(tree);
1975 return(NULL);
1976 }
1977 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001978 xmlFreeEnumeration(tree);
1979 return(NULL);
1980 }
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001981 if (dtd->doc != NULL)
1982 dict = dtd->doc->dict;
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001983
Daniel Veillard4432df22003-09-28 18:58:27 +00001984#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001985 /*
1986 * Check the type and possibly the default value.
1987 */
1988 switch (type) {
1989 case XML_ATTRIBUTE_CDATA:
1990 break;
1991 case XML_ATTRIBUTE_ID:
1992 break;
1993 case XML_ATTRIBUTE_IDREF:
1994 break;
1995 case XML_ATTRIBUTE_IDREFS:
1996 break;
1997 case XML_ATTRIBUTE_ENTITY:
1998 break;
1999 case XML_ATTRIBUTE_ENTITIES:
2000 break;
2001 case XML_ATTRIBUTE_NMTOKEN:
2002 break;
2003 case XML_ATTRIBUTE_NMTOKENS:
2004 break;
2005 case XML_ATTRIBUTE_ENUMERATION:
2006 break;
2007 case XML_ATTRIBUTE_NOTATION:
2008 break;
2009 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002010 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
2011 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2012 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002013 xmlFreeEnumeration(tree);
2014 return(NULL);
2015 }
2016 if ((defaultValue != NULL) &&
2017 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002018 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
2019 "Attribute %s of %s: invalid default value\n",
2020 elem, name, defaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00002021 defaultValue = NULL;
Daniel Veillard42595322004-11-08 10:52:06 +00002022 if (ctxt != NULL)
2023 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002024 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002025#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002026
2027 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00002028 * Check first that an attribute defined in the external subset wasn't
2029 * already defined in the internal subset
2030 */
2031 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
2032 (dtd->doc->intSubset != NULL) &&
2033 (dtd->doc->intSubset->attributes != NULL)) {
2034 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
2035 if (ret != NULL)
2036 return(NULL);
2037 }
2038
2039 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002040 * Create the Attribute table if needed.
2041 */
2042 table = (xmlAttributeTablePtr) dtd->attributes;
2043 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00002044 table = xmlHashCreateDict(0, dict);
Owen Taylor3473f882001-02-23 17:55:21 +00002045 dtd->attributes = (void *) table;
2046 }
2047 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002048 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002049 "xmlAddAttributeDecl: Table creation failed!\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002050 return(NULL);
2051 }
2052
2053
2054 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2055 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002056 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002057 return(NULL);
2058 }
2059 memset(ret, 0, sizeof(xmlAttribute));
2060 ret->type = XML_ATTRIBUTE_DECL;
2061
2062 /*
2063 * fill the structure.
2064 */
2065 ret->atype = type;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002066 if (dict) {
2067 ret->name = xmlDictLookup(dict, name, -1);
2068 ret->prefix = xmlDictLookup(dict, ns, -1);
2069 ret->elem = xmlDictLookup(dict, elem, -1);
2070 } else {
2071 ret->name = xmlStrdup(name);
2072 ret->prefix = xmlStrdup(ns);
2073 ret->elem = xmlStrdup(elem);
2074 }
Owen Taylor3473f882001-02-23 17:55:21 +00002075 ret->def = def;
2076 ret->tree = tree;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002077 if (defaultValue != NULL) {
2078 if (dict)
2079 ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
2080 else
2081 ret->defaultValue = xmlStrdup(defaultValue);
2082 }
Owen Taylor3473f882001-02-23 17:55:21 +00002083
2084 /*
2085 * Validity Check:
2086 * Search the DTD for previous declarations of the ATTLIST
2087 */
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002088 if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002089#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002090 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002091 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002092 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002093 xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00002094 "Attribute %s of element %s: already defined\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002095 name, elem, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00002096#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002097 xmlFreeAttribute(ret);
2098 return(NULL);
2099 }
2100
2101 /*
2102 * Validity Check:
2103 * Multiple ID per element
2104 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00002105 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00002106 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00002107
Daniel Veillard4432df22003-09-28 18:58:27 +00002108#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002109 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillarddbee0f12005-06-27 13:42:57 +00002110 (xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002111 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
Owen Taylor3473f882001-02-23 17:55:21 +00002112 "Element %s has too may ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002113 elem, name, NULL);
Daniel Veillard42595322004-11-08 10:52:06 +00002114 if (ctxt != NULL)
2115 ctxt->valid = 0;
Daniel Veillardc7612992002-02-17 22:47:37 +00002116 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002117#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardc7612992002-02-17 22:47:37 +00002118
Daniel Veillard48da9102001-08-07 01:10:10 +00002119 /*
2120 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002121 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00002122 */
2123 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
2124 ((ret->prefix != NULL &&
2125 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
2126 ret->nexth = elemDef->attributes;
2127 elemDef->attributes = ret;
2128 } else {
2129 xmlAttributePtr tmp = elemDef->attributes;
2130
2131 while ((tmp != NULL) &&
2132 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
2133 ((ret->prefix != NULL &&
2134 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
2135 if (tmp->nexth == NULL)
2136 break;
2137 tmp = tmp->nexth;
2138 }
2139 if (tmp != NULL) {
2140 ret->nexth = tmp->nexth;
2141 tmp->nexth = ret;
2142 } else {
2143 ret->nexth = elemDef->attributes;
2144 elemDef->attributes = ret;
2145 }
2146 }
Owen Taylor3473f882001-02-23 17:55:21 +00002147 }
2148
2149 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002150 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00002151 */
2152 ret->parent = dtd;
2153 ret->doc = dtd->doc;
2154 if (dtd->last == NULL) {
2155 dtd->children = dtd->last = (xmlNodePtr) ret;
2156 } else {
2157 dtd->last->next = (xmlNodePtr) ret;
2158 ret->prev = dtd->last;
2159 dtd->last = (xmlNodePtr) ret;
2160 }
2161 return(ret);
2162}
2163
2164/**
2165 * xmlFreeAttributeTable:
2166 * @table: An attribute table
2167 *
2168 * Deallocate the memory used by an entities hash table.
2169 */
2170void
2171xmlFreeAttributeTable(xmlAttributeTablePtr table) {
2172 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
2173}
2174
Daniel Veillard652327a2003-09-29 18:02:38 +00002175#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002176/**
2177 * xmlCopyAttribute:
2178 * @attr: An attribute
2179 *
2180 * Build a copy of an attribute.
2181 *
2182 * Returns the new xmlAttributePtr or NULL in case of error.
2183 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002184static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002185xmlCopyAttribute(xmlAttributePtr attr) {
2186 xmlAttributePtr cur;
2187
2188 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2189 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002190 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002191 return(NULL);
2192 }
2193 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00002194 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00002195 cur->atype = attr->atype;
2196 cur->def = attr->def;
2197 cur->tree = xmlCopyEnumeration(attr->tree);
2198 if (attr->elem != NULL)
2199 cur->elem = xmlStrdup(attr->elem);
2200 if (attr->name != NULL)
2201 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00002202 if (attr->prefix != NULL)
2203 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002204 if (attr->defaultValue != NULL)
2205 cur->defaultValue = xmlStrdup(attr->defaultValue);
2206 return(cur);
2207}
2208
2209/**
2210 * xmlCopyAttributeTable:
2211 * @table: An attribute table
2212 *
2213 * Build a copy of an attribute table.
2214 *
2215 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2216 */
2217xmlAttributeTablePtr
2218xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2219 return((xmlAttributeTablePtr) xmlHashCopy(table,
2220 (xmlHashCopier) xmlCopyAttribute));
2221}
Daniel Veillard652327a2003-09-29 18:02:38 +00002222#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002223
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002224#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002225/**
2226 * xmlDumpAttributeDecl:
2227 * @buf: the XML buffer output
2228 * @attr: An attribute declaration
2229 *
2230 * This will dump the content of the attribute declaration as an XML
2231 * DTD definition
2232 */
2233void
2234xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002235 if ((buf == NULL) || (attr == NULL))
2236 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002237 xmlBufferWriteChar(buf, "<!ATTLIST ");
2238 xmlBufferWriteCHAR(buf, attr->elem);
2239 xmlBufferWriteChar(buf, " ");
2240 if (attr->prefix != NULL) {
2241 xmlBufferWriteCHAR(buf, attr->prefix);
2242 xmlBufferWriteChar(buf, ":");
2243 }
2244 xmlBufferWriteCHAR(buf, attr->name);
2245 switch (attr->atype) {
2246 case XML_ATTRIBUTE_CDATA:
2247 xmlBufferWriteChar(buf, " CDATA");
2248 break;
2249 case XML_ATTRIBUTE_ID:
2250 xmlBufferWriteChar(buf, " ID");
2251 break;
2252 case XML_ATTRIBUTE_IDREF:
2253 xmlBufferWriteChar(buf, " IDREF");
2254 break;
2255 case XML_ATTRIBUTE_IDREFS:
2256 xmlBufferWriteChar(buf, " IDREFS");
2257 break;
2258 case XML_ATTRIBUTE_ENTITY:
2259 xmlBufferWriteChar(buf, " ENTITY");
2260 break;
2261 case XML_ATTRIBUTE_ENTITIES:
2262 xmlBufferWriteChar(buf, " ENTITIES");
2263 break;
2264 case XML_ATTRIBUTE_NMTOKEN:
2265 xmlBufferWriteChar(buf, " NMTOKEN");
2266 break;
2267 case XML_ATTRIBUTE_NMTOKENS:
2268 xmlBufferWriteChar(buf, " NMTOKENS");
2269 break;
2270 case XML_ATTRIBUTE_ENUMERATION:
2271 xmlBufferWriteChar(buf, " (");
2272 xmlDumpEnumeration(buf, attr->tree);
2273 break;
2274 case XML_ATTRIBUTE_NOTATION:
2275 xmlBufferWriteChar(buf, " NOTATION (");
2276 xmlDumpEnumeration(buf, attr->tree);
2277 break;
2278 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002279 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2280 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2281 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002282 }
2283 switch (attr->def) {
2284 case XML_ATTRIBUTE_NONE:
2285 break;
2286 case XML_ATTRIBUTE_REQUIRED:
2287 xmlBufferWriteChar(buf, " #REQUIRED");
2288 break;
2289 case XML_ATTRIBUTE_IMPLIED:
2290 xmlBufferWriteChar(buf, " #IMPLIED");
2291 break;
2292 case XML_ATTRIBUTE_FIXED:
2293 xmlBufferWriteChar(buf, " #FIXED");
2294 break;
2295 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002296 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2297 "Internal: ATTRIBUTE struct corrupted invalid def\n",
2298 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002299 }
2300 if (attr->defaultValue != NULL) {
2301 xmlBufferWriteChar(buf, " ");
2302 xmlBufferWriteQuotedString(buf, attr->defaultValue);
2303 }
2304 xmlBufferWriteChar(buf, ">\n");
2305}
2306
2307/**
William M. Brack9e660592003-10-20 14:56:06 +00002308 * xmlDumpAttributeDeclScan:
2309 * @attr: An attribute declaration
2310 * @buf: the XML buffer output
2311 *
2312 * This is used with the hash scan function - just reverses arguments
2313 */
2314static void
2315xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) {
2316 xmlDumpAttributeDecl(buf, attr);
2317}
2318
2319/**
Owen Taylor3473f882001-02-23 17:55:21 +00002320 * xmlDumpAttributeTable:
2321 * @buf: the XML buffer output
2322 * @table: An attribute table
2323 *
2324 * This will dump the content of the attribute table as an XML DTD definition
2325 */
2326void
2327xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002328 if ((buf == NULL) || (table == NULL))
2329 return;
William M. Brack9e660592003-10-20 14:56:06 +00002330 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002331}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002332#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002333
2334/************************************************************************
2335 * *
2336 * NOTATIONs *
2337 * *
2338 ************************************************************************/
2339/**
Owen Taylor3473f882001-02-23 17:55:21 +00002340 * xmlFreeNotation:
2341 * @not: A notation
2342 *
2343 * Deallocate the memory used by an notation definition
2344 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002345static void
Owen Taylor3473f882001-02-23 17:55:21 +00002346xmlFreeNotation(xmlNotationPtr nota) {
2347 if (nota == NULL) return;
2348 if (nota->name != NULL)
2349 xmlFree((xmlChar *) nota->name);
2350 if (nota->PublicID != NULL)
2351 xmlFree((xmlChar *) nota->PublicID);
2352 if (nota->SystemID != NULL)
2353 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002354 xmlFree(nota);
2355}
2356
2357
2358/**
2359 * xmlAddNotationDecl:
2360 * @dtd: pointer to the DTD
2361 * @ctxt: the validation context
2362 * @name: the entity name
2363 * @PublicID: the public identifier or NULL
2364 * @SystemID: the system identifier or NULL
2365 *
2366 * Register a new notation declaration
2367 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002368 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002369 */
2370xmlNotationPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002371xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002372 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002373 const xmlChar *PublicID, const xmlChar *SystemID) {
2374 xmlNotationPtr ret;
2375 xmlNotationTablePtr table;
2376
2377 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002378 return(NULL);
2379 }
2380 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002381 return(NULL);
2382 }
2383 if ((PublicID == NULL) && (SystemID == NULL)) {
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002384 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002385 }
2386
2387 /*
2388 * Create the Notation table if needed.
2389 */
2390 table = (xmlNotationTablePtr) dtd->notations;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002391 if (table == NULL) {
2392 xmlDictPtr dict = NULL;
2393 if (dtd->doc != NULL)
2394 dict = dtd->doc->dict;
2395
2396 dtd->notations = table = xmlHashCreateDict(0, dict);
2397 }
Owen Taylor3473f882001-02-23 17:55:21 +00002398 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002399 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002400 "xmlAddNotationDecl: Table creation failed!\n");
2401 return(NULL);
2402 }
2403
2404 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2405 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002406 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002407 return(NULL);
2408 }
2409 memset(ret, 0, sizeof(xmlNotation));
2410
2411 /*
2412 * fill the structure.
2413 */
2414 ret->name = xmlStrdup(name);
2415 if (SystemID != NULL)
2416 ret->SystemID = xmlStrdup(SystemID);
2417 if (PublicID != NULL)
2418 ret->PublicID = xmlStrdup(PublicID);
2419
2420 /*
2421 * Validity Check:
2422 * Check the DTD for previous declarations of the ATTLIST
2423 */
2424 if (xmlHashAddEntry(table, name, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002425#ifdef LIBXML_VALID_ENABLED
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002426 xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
2427 "xmlAddNotationDecl: %s already defined\n",
2428 (const char *) name);
Daniel Veillard4432df22003-09-28 18:58:27 +00002429#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002430 xmlFreeNotation(ret);
2431 return(NULL);
2432 }
2433 return(ret);
2434}
2435
2436/**
2437 * xmlFreeNotationTable:
2438 * @table: An notation table
2439 *
2440 * Deallocate the memory used by an entities hash table.
2441 */
2442void
2443xmlFreeNotationTable(xmlNotationTablePtr table) {
2444 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2445}
2446
Daniel Veillard652327a2003-09-29 18:02:38 +00002447#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002448/**
2449 * xmlCopyNotation:
2450 * @nota: A notation
2451 *
2452 * Build a copy of a notation.
2453 *
2454 * Returns the new xmlNotationPtr or NULL in case of error.
2455 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002456static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002457xmlCopyNotation(xmlNotationPtr nota) {
2458 xmlNotationPtr cur;
2459
2460 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2461 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002462 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002463 return(NULL);
2464 }
2465 if (nota->name != NULL)
2466 cur->name = xmlStrdup(nota->name);
2467 else
2468 cur->name = NULL;
2469 if (nota->PublicID != NULL)
2470 cur->PublicID = xmlStrdup(nota->PublicID);
2471 else
2472 cur->PublicID = NULL;
2473 if (nota->SystemID != NULL)
2474 cur->SystemID = xmlStrdup(nota->SystemID);
2475 else
2476 cur->SystemID = NULL;
2477 return(cur);
2478}
2479
2480/**
2481 * xmlCopyNotationTable:
2482 * @table: A notation table
2483 *
2484 * Build a copy of a notation table.
2485 *
2486 * Returns the new xmlNotationTablePtr or NULL in case of error.
2487 */
2488xmlNotationTablePtr
2489xmlCopyNotationTable(xmlNotationTablePtr table) {
2490 return((xmlNotationTablePtr) xmlHashCopy(table,
2491 (xmlHashCopier) xmlCopyNotation));
2492}
Daniel Veillard652327a2003-09-29 18:02:38 +00002493#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002494
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002495#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002496/**
2497 * xmlDumpNotationDecl:
2498 * @buf: the XML buffer output
2499 * @nota: A notation declaration
2500 *
2501 * This will dump the content the notation declaration as an XML DTD definition
2502 */
2503void
2504xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002505 if ((buf == NULL) || (nota == NULL))
2506 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002507 xmlBufferWriteChar(buf, "<!NOTATION ");
2508 xmlBufferWriteCHAR(buf, nota->name);
2509 if (nota->PublicID != NULL) {
2510 xmlBufferWriteChar(buf, " PUBLIC ");
2511 xmlBufferWriteQuotedString(buf, nota->PublicID);
2512 if (nota->SystemID != NULL) {
2513 xmlBufferWriteChar(buf, " ");
Daniel Veillard41c4a752004-09-08 20:55:38 +00002514 xmlBufferWriteQuotedString(buf, nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002515 }
2516 } else {
2517 xmlBufferWriteChar(buf, " SYSTEM ");
Daniel Veillard41c4a752004-09-08 20:55:38 +00002518 xmlBufferWriteQuotedString(buf, nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002519 }
2520 xmlBufferWriteChar(buf, " >\n");
2521}
2522
2523/**
William M. Brack9e660592003-10-20 14:56:06 +00002524 * xmlDumpNotationDeclScan:
2525 * @nota: A notation declaration
2526 * @buf: the XML buffer output
2527 *
2528 * This is called with the hash scan function, and just reverses args
2529 */
2530static void
2531xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) {
2532 xmlDumpNotationDecl(buf, nota);
2533}
2534
2535/**
Owen Taylor3473f882001-02-23 17:55:21 +00002536 * xmlDumpNotationTable:
2537 * @buf: the XML buffer output
2538 * @table: A notation table
2539 *
2540 * This will dump the content of the notation table as an XML DTD definition
2541 */
2542void
2543xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002544 if ((buf == NULL) || (table == NULL))
2545 return;
William M. Brack9e660592003-10-20 14:56:06 +00002546 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002547}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002548#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002549
2550/************************************************************************
2551 * *
2552 * IDs *
2553 * *
2554 ************************************************************************/
2555/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002556 * DICT_FREE:
2557 * @str: a string
2558 *
2559 * Free a string if it is not owned by the "dict" dictionnary in the
2560 * current scope
2561 */
2562#define DICT_FREE(str) \
2563 if ((str) && ((!dict) || \
2564 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
2565 xmlFree((char *)(str));
2566
2567/**
Owen Taylor3473f882001-02-23 17:55:21 +00002568 * xmlFreeID:
2569 * @not: A id
2570 *
2571 * Deallocate the memory used by an id definition
2572 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002573static void
Owen Taylor3473f882001-02-23 17:55:21 +00002574xmlFreeID(xmlIDPtr id) {
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002575 xmlDictPtr dict = NULL;
2576
Owen Taylor3473f882001-02-23 17:55:21 +00002577 if (id == NULL) return;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002578
2579 if (id->doc != NULL)
2580 dict = id->doc->dict;
2581
Owen Taylor3473f882001-02-23 17:55:21 +00002582 if (id->value != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002583 DICT_FREE(id->value)
Daniel Veillardea7751d2002-12-20 00:16:24 +00002584 if (id->name != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002585 DICT_FREE(id->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002586 xmlFree(id);
2587}
2588
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002589
Owen Taylor3473f882001-02-23 17:55:21 +00002590/**
2591 * xmlAddID:
2592 * @ctxt: the validation context
2593 * @doc: pointer to the document
2594 * @value: the value name
2595 * @attr: the attribute holding the ID
2596 *
2597 * Register a new id declaration
2598 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002599 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002600 */
2601xmlIDPtr
2602xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2603 xmlAttrPtr attr) {
2604 xmlIDPtr ret;
2605 xmlIDTablePtr table;
2606
2607 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002608 return(NULL);
2609 }
2610 if (value == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002611 return(NULL);
2612 }
2613 if (attr == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002614 return(NULL);
2615 }
2616
2617 /*
2618 * Create the ID table if needed.
2619 */
2620 table = (xmlIDTablePtr) doc->ids;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002621 if (table == NULL) {
2622 doc->ids = table = xmlHashCreateDict(0, doc->dict);
2623 }
Owen Taylor3473f882001-02-23 17:55:21 +00002624 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002625 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002626 "xmlAddID: Table creation failed!\n");
2627 return(NULL);
2628 }
2629
2630 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2631 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002632 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002633 return(NULL);
2634 }
2635
2636 /*
2637 * fill the structure.
2638 */
2639 ret->value = xmlStrdup(value);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002640 ret->doc = doc;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002641 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2642 /*
2643 * Operating in streaming mode, attr is gonna disapear
2644 */
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002645 if (doc->dict != NULL)
2646 ret->name = xmlDictLookup(doc->dict, attr->name, -1);
2647 else
2648 ret->name = xmlStrdup(attr->name);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002649 ret->attr = NULL;
2650 } else {
2651 ret->attr = attr;
2652 ret->name = NULL;
2653 }
2654 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002655
2656 if (xmlHashAddEntry(table, value, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002657#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002658 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002659 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002660 */
Daniel Veillardd3669b22004-02-25 12:34:55 +00002661 if ((ctxt != NULL) && (ctxt->error != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002662 xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2663 "ID %s already defined\n",
2664 value, NULL, NULL);
Daniel Veillard76575762002-09-05 14:21:15 +00002665 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002666#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002667 xmlFreeID(ret);
2668 return(NULL);
2669 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002670 if (attr != NULL)
2671 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002672 return(ret);
2673}
2674
2675/**
2676 * xmlFreeIDTable:
2677 * @table: An id table
2678 *
2679 * Deallocate the memory used by an ID hash table.
2680 */
2681void
2682xmlFreeIDTable(xmlIDTablePtr table) {
2683 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2684}
2685
2686/**
2687 * xmlIsID:
2688 * @doc: the document
2689 * @elem: the element carrying the attribute
2690 * @attr: the attribute
2691 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002692 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002693 * then this is done if DTD loading has been requested. In the case
2694 * of HTML documents parsed with the HTML parser, then ID detection is
2695 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002696 *
2697 * Returns 0 or 1 depending on the lookup result
2698 */
2699int
2700xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2701 if (doc == NULL) return(0);
2702 if (attr == NULL) return(0);
2703 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2704 return(0);
2705 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillard9ba8e382003-10-28 21:31:45 +00002706 if (((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2707 (xmlStrEqual(BAD_CAST "name", attr->name))) &&
2708 ((elem != NULL) && (!xmlStrEqual(elem->name, BAD_CAST "input"))))
Owen Taylor3473f882001-02-23 17:55:21 +00002709 return(1);
2710 return(0);
2711 } else {
2712 xmlAttributePtr attrDecl;
2713
2714 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002715 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00002716 xmlChar fn[50];
Daniel Veillard37f961d2002-07-06 17:53:56 +00002717 xmlChar *fullname;
Daniel Veillardc00cda82003-04-07 10:22:39 +00002718
2719 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002720 if (fullname == NULL)
2721 return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002722 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2723 attr->name);
2724 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2725 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2726 attr->name);
Daniel Veillardc00cda82003-04-07 10:22:39 +00002727 if ((fullname != fn) && (fullname != elem->name))
2728 xmlFree(fullname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002729 } else {
2730 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2731 attr->name);
2732 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2733 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2734 attr->name);
2735 }
Owen Taylor3473f882001-02-23 17:55:21 +00002736
2737 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2738 return(1);
2739 }
2740 return(0);
2741}
2742
2743/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002744 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002745 * @doc: the document
2746 * @attr: the attribute
2747 *
2748 * Remove the given attribute from the ID table maintained internally.
2749 *
2750 * Returns -1 if the lookup failed and 0 otherwise
2751 */
2752int
2753xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002754 xmlIDTablePtr table;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002755 xmlIDPtr id;
Owen Taylor3473f882001-02-23 17:55:21 +00002756 xmlChar *ID;
2757
2758 if (doc == NULL) return(-1);
2759 if (attr == NULL) return(-1);
2760 table = (xmlIDTablePtr) doc->ids;
2761 if (table == NULL)
2762 return(-1);
2763
2764 if (attr == NULL)
2765 return(-1);
2766 ID = xmlNodeListGetString(doc, attr->children, 1);
2767 if (ID == NULL)
2768 return(-1);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002769 id = xmlHashLookup(table, ID);
2770 if (id == NULL || id->attr != attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002771 xmlFree(ID);
2772 return(-1);
2773 }
Daniel Veillard91b955c2004-12-10 10:26:42 +00002774 xmlHashRemoveEntry(table, ID, (xmlHashDeallocator) xmlFreeID);
Owen Taylor3473f882001-02-23 17:55:21 +00002775 xmlFree(ID);
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002776 attr->atype = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002777 return(0);
2778}
2779
2780/**
2781 * xmlGetID:
2782 * @doc: pointer to the document
2783 * @ID: the ID value
2784 *
2785 * Search the attribute declaring the given ID
2786 *
2787 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2788 */
2789xmlAttrPtr
2790xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2791 xmlIDTablePtr table;
2792 xmlIDPtr id;
2793
2794 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002795 return(NULL);
2796 }
2797
2798 if (ID == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002799 return(NULL);
2800 }
2801
2802 table = (xmlIDTablePtr) doc->ids;
2803 if (table == NULL)
2804 return(NULL);
2805
2806 id = xmlHashLookup(table, ID);
2807 if (id == NULL)
2808 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002809 if (id->attr == NULL) {
2810 /*
2811 * We are operating on a stream, return a well known reference
2812 * since the attribute node doesn't exist anymore
2813 */
2814 return((xmlAttrPtr) doc);
2815 }
Owen Taylor3473f882001-02-23 17:55:21 +00002816 return(id->attr);
2817}
2818
2819/************************************************************************
2820 * *
2821 * Refs *
2822 * *
2823 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002824typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002825{
2826 xmlListPtr l;
2827 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002828} xmlRemoveMemo;
2829
2830typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2831
2832typedef struct xmlValidateMemo_t
2833{
2834 xmlValidCtxtPtr ctxt;
2835 const xmlChar *name;
2836} xmlValidateMemo;
2837
2838typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002839
2840/**
Owen Taylor3473f882001-02-23 17:55:21 +00002841 * xmlFreeRef:
2842 * @lk: A list link
2843 *
2844 * Deallocate the memory used by a ref definition
2845 */
2846static void
2847xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002848 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2849 if (ref == NULL) return;
2850 if (ref->value != NULL)
2851 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002852 if (ref->name != NULL)
2853 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002854 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002855}
2856
2857/**
2858 * xmlFreeRefList:
2859 * @list_ref: A list of references.
2860 *
2861 * Deallocate the memory used by a list of references
2862 */
2863static void
2864xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002865 if (list_ref == NULL) return;
2866 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002867}
2868
2869/**
2870 * xmlWalkRemoveRef:
2871 * @data: Contents of current link
2872 * @user: Value supplied by the user
2873 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002874 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002875 */
2876static int
2877xmlWalkRemoveRef(const void *data, const void *user)
2878{
Daniel Veillard37721922001-05-04 15:21:12 +00002879 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2880 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2881 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002882
Daniel Veillard37721922001-05-04 15:21:12 +00002883 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2884 xmlListRemoveFirst(ref_list, (void *)data);
2885 return 0;
2886 }
2887 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002888}
2889
2890/**
Daniel Veillard770075b2004-02-25 10:44:30 +00002891 * xmlDummyCompare
2892 * @data0: Value supplied by the user
2893 * @data1: Value supplied by the user
2894 *
2895 * Do nothing, return 0. Used to create unordered lists.
2896 */
2897static int
Daniel Veillardf54cd532004-02-25 11:52:31 +00002898xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2899 const void *data1 ATTRIBUTE_UNUSED)
Daniel Veillard770075b2004-02-25 10:44:30 +00002900{
2901 return (0);
2902}
2903
2904/**
Owen Taylor3473f882001-02-23 17:55:21 +00002905 * xmlAddRef:
2906 * @ctxt: the validation context
2907 * @doc: pointer to the document
2908 * @value: the value name
2909 * @attr: the attribute holding the Ref
2910 *
2911 * Register a new ref declaration
2912 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002913 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002914 */
2915xmlRefPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002916xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002917 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002918 xmlRefPtr ret;
2919 xmlRefTablePtr table;
2920 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002921
Daniel Veillard37721922001-05-04 15:21:12 +00002922 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002923 return(NULL);
2924 }
2925 if (value == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002926 return(NULL);
2927 }
2928 if (attr == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002929 return(NULL);
2930 }
Owen Taylor3473f882001-02-23 17:55:21 +00002931
Daniel Veillard37721922001-05-04 15:21:12 +00002932 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002933 * Create the Ref table if needed.
2934 */
Daniel Veillard37721922001-05-04 15:21:12 +00002935 table = (xmlRefTablePtr) doc->refs;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002936 if (table == NULL) {
2937 doc->refs = table = xmlHashCreateDict(0, doc->dict);
2938 }
Daniel Veillard37721922001-05-04 15:21:12 +00002939 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002940 xmlVErrMemory(ctxt,
Daniel Veillard37721922001-05-04 15:21:12 +00002941 "xmlAddRef: Table creation failed!\n");
2942 return(NULL);
2943 }
Owen Taylor3473f882001-02-23 17:55:21 +00002944
Daniel Veillard37721922001-05-04 15:21:12 +00002945 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2946 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002947 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard37721922001-05-04 15:21:12 +00002948 return(NULL);
2949 }
Owen Taylor3473f882001-02-23 17:55:21 +00002950
Daniel Veillard37721922001-05-04 15:21:12 +00002951 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002952 * fill the structure.
2953 */
Daniel Veillard37721922001-05-04 15:21:12 +00002954 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002955 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2956 /*
2957 * Operating in streaming mode, attr is gonna disapear
2958 */
2959 ret->name = xmlStrdup(attr->name);
2960 ret->attr = NULL;
2961 } else {
2962 ret->name = NULL;
2963 ret->attr = attr;
2964 }
2965 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002966
Daniel Veillard37721922001-05-04 15:21:12 +00002967 /* To add a reference :-
2968 * References are maintained as a list of references,
2969 * Lookup the entry, if no entry create new nodelist
2970 * Add the owning node to the NodeList
2971 * Return the ref
2972 */
Owen Taylor3473f882001-02-23 17:55:21 +00002973
Daniel Veillard37721922001-05-04 15:21:12 +00002974 if (NULL == (ref_list = xmlHashLookup(table, value))) {
Daniel Veillard770075b2004-02-25 10:44:30 +00002975 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002976 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2977 "xmlAddRef: Reference list creation failed!\n",
2978 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002979 return(NULL);
2980 }
2981 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2982 xmlListDelete(ref_list);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002983 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2984 "xmlAddRef: Reference list insertion failed!\n",
2985 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002986 return(NULL);
2987 }
2988 }
Daniel Veillard965983a2004-02-17 16:30:24 +00002989/* xmlListInsert(ref_list, ret); */
2990 xmlListAppend(ref_list, ret);
Daniel Veillard37721922001-05-04 15:21:12 +00002991 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002992}
2993
2994/**
2995 * xmlFreeRefTable:
2996 * @table: An ref table
2997 *
2998 * Deallocate the memory used by an Ref hash table.
2999 */
3000void
3001xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00003002 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00003003}
3004
3005/**
3006 * xmlIsRef:
3007 * @doc: the document
3008 * @elem: the element carrying the attribute
3009 * @attr: the attribute
3010 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003011 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00003012 * then this is simple, otherwise we use an heuristic: name Ref (upper
3013 * or lowercase).
3014 *
3015 * Returns 0 or 1 depending on the lookup result
3016 */
3017int
3018xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003019 if (attr == NULL)
3020 return(0);
3021 if (doc == NULL) {
3022 doc = attr->doc;
3023 if (doc == NULL) return(0);
3024 }
3025
Daniel Veillard37721922001-05-04 15:21:12 +00003026 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
3027 return(0);
3028 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
3029 /* TODO @@@ */
3030 return(0);
3031 } else {
3032 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00003033
Daniel Veillardce244ad2004-11-05 10:03:46 +00003034 if (elem == NULL) return(0);
Daniel Veillard37721922001-05-04 15:21:12 +00003035 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
3036 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3037 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3038 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003039
Daniel Veillard37721922001-05-04 15:21:12 +00003040 if ((attrDecl != NULL) &&
3041 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
3042 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
3043 return(1);
3044 }
3045 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003046}
3047
3048/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003049 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00003050 * @doc: the document
3051 * @attr: the attribute
3052 *
3053 * Remove the given attribute from the Ref table maintained internally.
3054 *
3055 * Returns -1 if the lookup failed and 0 otherwise
3056 */
3057int
3058xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00003059 xmlListPtr ref_list;
3060 xmlRefTablePtr table;
3061 xmlChar *ID;
3062 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00003063
Daniel Veillard37721922001-05-04 15:21:12 +00003064 if (doc == NULL) return(-1);
3065 if (attr == NULL) return(-1);
3066 table = (xmlRefTablePtr) doc->refs;
3067 if (table == NULL)
3068 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003069
Daniel Veillard37721922001-05-04 15:21:12 +00003070 if (attr == NULL)
3071 return(-1);
3072 ID = xmlNodeListGetString(doc, attr->children, 1);
3073 if (ID == NULL)
3074 return(-1);
3075 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00003076
Daniel Veillard37721922001-05-04 15:21:12 +00003077 if(ref_list == NULL) {
3078 xmlFree(ID);
3079 return (-1);
3080 }
3081 /* At this point, ref_list refers to a list of references which
3082 * have the same key as the supplied attr. Our list of references
3083 * is ordered by reference address and we don't have that information
3084 * here to use when removing. We'll have to walk the list and
3085 * check for a matching attribute, when we find one stop the walk
3086 * and remove the entry.
3087 * The list is ordered by reference, so that means we don't have the
3088 * key. Passing the list and the reference to the walker means we
3089 * will have enough data to be able to remove the entry.
3090 */
3091 target.l = ref_list;
3092 target.ap = attr;
3093
3094 /* Remove the supplied attr from our list */
3095 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00003096
Daniel Veillard37721922001-05-04 15:21:12 +00003097 /*If the list is empty then remove the list entry in the hash */
3098 if (xmlListEmpty(ref_list))
3099 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
3100 xmlFreeRefList);
3101 xmlFree(ID);
3102 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003103}
3104
3105/**
3106 * xmlGetRefs:
3107 * @doc: pointer to the document
3108 * @ID: the ID value
3109 *
3110 * Find the set of references for the supplied ID.
3111 *
3112 * Returns NULL if not found, otherwise node set for the ID.
3113 */
3114xmlListPtr
3115xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00003116 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00003117
Daniel Veillard37721922001-05-04 15:21:12 +00003118 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00003119 return(NULL);
3120 }
Owen Taylor3473f882001-02-23 17:55:21 +00003121
Daniel Veillard37721922001-05-04 15:21:12 +00003122 if (ID == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00003123 return(NULL);
3124 }
Owen Taylor3473f882001-02-23 17:55:21 +00003125
Daniel Veillard37721922001-05-04 15:21:12 +00003126 table = (xmlRefTablePtr) doc->refs;
3127 if (table == NULL)
3128 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003129
Daniel Veillard37721922001-05-04 15:21:12 +00003130 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00003131}
3132
3133/************************************************************************
3134 * *
3135 * Routines for validity checking *
3136 * *
3137 ************************************************************************/
3138
3139/**
3140 * xmlGetDtdElementDesc:
3141 * @dtd: a pointer to the DtD to search
3142 * @name: the element name
3143 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003144 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003145 *
3146 * returns the xmlElementPtr if found or NULL
3147 */
3148
3149xmlElementPtr
3150xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
3151 xmlElementTablePtr table;
3152 xmlElementPtr cur;
3153 xmlChar *uqname = NULL, *prefix = NULL;
3154
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003155 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003156 if (dtd->elements == NULL)
3157 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003158 table = (xmlElementTablePtr) dtd->elements;
3159
3160 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003161 if (uqname != NULL)
3162 name = uqname;
3163 cur = xmlHashLookup2(table, name, prefix);
3164 if (prefix != NULL) xmlFree(prefix);
3165 if (uqname != NULL) xmlFree(uqname);
3166 return(cur);
3167}
3168/**
3169 * xmlGetDtdElementDesc2:
3170 * @dtd: a pointer to the DtD to search
3171 * @name: the element name
3172 * @create: create an empty description if not found
3173 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003174 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00003175 *
3176 * returns the xmlElementPtr if found or NULL
3177 */
3178
Daniel Veillard86fd5a72001-12-13 14:55:21 +00003179static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00003180xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
3181 xmlElementTablePtr table;
3182 xmlElementPtr cur;
3183 xmlChar *uqname = NULL, *prefix = NULL;
3184
3185 if (dtd == NULL) return(NULL);
3186 if (dtd->elements == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003187 xmlDictPtr dict = NULL;
3188
3189 if (dtd->doc != NULL)
3190 dict = dtd->doc->dict;
3191
Daniel Veillarda10efa82001-04-18 13:09:01 +00003192 if (!create)
3193 return(NULL);
3194 /*
3195 * Create the Element table if needed.
3196 */
3197 table = (xmlElementTablePtr) dtd->elements;
3198 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003199 table = xmlHashCreateDict(0, dict);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003200 dtd->elements = (void *) table;
3201 }
3202 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003203 xmlVErrMemory(NULL, "element table allocation failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003204 return(NULL);
3205 }
3206 }
3207 table = (xmlElementTablePtr) dtd->elements;
3208
3209 uqname = xmlSplitQName2(name, &prefix);
3210 if (uqname != NULL)
3211 name = uqname;
3212 cur = xmlHashLookup2(table, name, prefix);
3213 if ((cur == NULL) && (create)) {
3214 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
3215 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003216 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003217 return(NULL);
3218 }
3219 memset(cur, 0, sizeof(xmlElement));
3220 cur->type = XML_ELEMENT_DECL;
3221
3222 /*
3223 * fill the structure.
3224 */
3225 cur->name = xmlStrdup(name);
3226 cur->prefix = xmlStrdup(prefix);
3227 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3228
3229 xmlHashAddEntry2(table, name, prefix, cur);
3230 }
3231 if (prefix != NULL) xmlFree(prefix);
3232 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00003233 return(cur);
3234}
3235
3236/**
3237 * xmlGetDtdQElementDesc:
3238 * @dtd: a pointer to the DtD to search
3239 * @name: the element name
3240 * @prefix: the element namespace prefix
3241 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003242 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003243 *
3244 * returns the xmlElementPtr if found or NULL
3245 */
3246
Daniel Veillard48da9102001-08-07 01:10:10 +00003247xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00003248xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3249 const xmlChar *prefix) {
3250 xmlElementTablePtr table;
3251
3252 if (dtd == NULL) return(NULL);
3253 if (dtd->elements == NULL) return(NULL);
3254 table = (xmlElementTablePtr) dtd->elements;
3255
3256 return(xmlHashLookup2(table, name, prefix));
3257}
3258
3259/**
3260 * xmlGetDtdAttrDesc:
3261 * @dtd: a pointer to the DtD to search
3262 * @elem: the element name
3263 * @name: the attribute name
3264 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003265 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003266 * this element.
3267 *
3268 * returns the xmlAttributePtr if found or NULL
3269 */
3270
3271xmlAttributePtr
3272xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3273 xmlAttributeTablePtr table;
3274 xmlAttributePtr cur;
3275 xmlChar *uqname = NULL, *prefix = NULL;
3276
3277 if (dtd == NULL) return(NULL);
3278 if (dtd->attributes == NULL) return(NULL);
3279
3280 table = (xmlAttributeTablePtr) dtd->attributes;
3281 if (table == NULL)
3282 return(NULL);
3283
3284 uqname = xmlSplitQName2(name, &prefix);
3285
3286 if (uqname != NULL) {
3287 cur = xmlHashLookup3(table, uqname, prefix, elem);
3288 if (prefix != NULL) xmlFree(prefix);
3289 if (uqname != NULL) xmlFree(uqname);
3290 } else
3291 cur = xmlHashLookup3(table, name, NULL, elem);
3292 return(cur);
3293}
3294
3295/**
3296 * xmlGetDtdQAttrDesc:
3297 * @dtd: a pointer to the DtD to search
3298 * @elem: the element name
3299 * @name: the attribute name
3300 * @prefix: the attribute namespace prefix
3301 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003302 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003303 * this element.
3304 *
3305 * returns the xmlAttributePtr if found or NULL
3306 */
3307
Daniel Veillard48da9102001-08-07 01:10:10 +00003308xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00003309xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3310 const xmlChar *prefix) {
3311 xmlAttributeTablePtr table;
3312
3313 if (dtd == NULL) return(NULL);
3314 if (dtd->attributes == NULL) return(NULL);
3315 table = (xmlAttributeTablePtr) dtd->attributes;
3316
3317 return(xmlHashLookup3(table, name, prefix, elem));
3318}
3319
3320/**
3321 * xmlGetDtdNotationDesc:
3322 * @dtd: a pointer to the DtD to search
3323 * @name: the notation name
3324 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003325 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00003326 *
3327 * returns the xmlNotationPtr if found or NULL
3328 */
3329
3330xmlNotationPtr
3331xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3332 xmlNotationTablePtr table;
3333
3334 if (dtd == NULL) return(NULL);
3335 if (dtd->notations == NULL) return(NULL);
3336 table = (xmlNotationTablePtr) dtd->notations;
3337
3338 return(xmlHashLookup(table, name));
3339}
3340
Daniel Veillardf54cd532004-02-25 11:52:31 +00003341#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003342/**
3343 * xmlValidateNotationUse:
3344 * @ctxt: the validation context
3345 * @doc: the document
3346 * @notationName: the notation name to check
3347 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003348 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00003349 * - [ VC: Notation Declared ]
3350 *
3351 * returns 1 if valid or 0 otherwise
3352 */
3353
3354int
3355xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3356 const xmlChar *notationName) {
3357 xmlNotationPtr notaDecl;
3358 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3359
3360 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3361 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3362 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3363
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003364 if ((notaDecl == NULL) && (ctxt != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003365 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3366 "NOTATION %s is not declared\n",
3367 notationName, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003368 return(0);
3369 }
3370 return(1);
3371}
Daniel Veillardf54cd532004-02-25 11:52:31 +00003372#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003373
3374/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003375 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00003376 * @doc: the document
3377 * @name: the element name
3378 *
3379 * Search in the DtDs whether an element accept Mixed content (or ANY)
3380 * basically if it is supposed to accept text childs
3381 *
3382 * returns 0 if no, 1 if yes, and -1 if no element description is available
3383 */
3384
3385int
3386xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3387 xmlElementPtr elemDecl;
3388
3389 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3390
3391 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3392 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3393 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3394 if (elemDecl == NULL) return(-1);
3395 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00003396 case XML_ELEMENT_TYPE_UNDEFINED:
3397 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003398 case XML_ELEMENT_TYPE_ELEMENT:
3399 return(0);
3400 case XML_ELEMENT_TYPE_EMPTY:
3401 /*
3402 * return 1 for EMPTY since we want VC error to pop up
3403 * on <empty> </empty> for example
3404 */
3405 case XML_ELEMENT_TYPE_ANY:
3406 case XML_ELEMENT_TYPE_MIXED:
3407 return(1);
3408 }
3409 return(1);
3410}
3411
Daniel Veillard4432df22003-09-28 18:58:27 +00003412#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003413/**
3414 * xmlValidateNameValue:
3415 * @value: an Name value
3416 *
3417 * Validate that the given value match Name production
3418 *
3419 * returns 1 if valid or 0 otherwise
3420 */
3421
Daniel Veillard9b731d72002-04-14 12:56:08 +00003422int
Owen Taylor3473f882001-02-23 17:55:21 +00003423xmlValidateNameValue(const xmlChar *value) {
3424 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003425 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003426
3427 if (value == NULL) return(0);
3428 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003429 val = xmlStringCurrentChar(NULL, cur, &len);
3430 cur += len;
3431 if (!IS_LETTER(val) && (val != '_') &&
3432 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003433 return(0);
3434 }
3435
Daniel Veillardd8224e02002-01-13 15:43:22 +00003436 val = xmlStringCurrentChar(NULL, cur, &len);
3437 cur += len;
3438 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3439 (val == '.') || (val == '-') ||
3440 (val == '_') || (val == ':') ||
3441 (IS_COMBINING(val)) ||
3442 (IS_EXTENDER(val))) {
3443 val = xmlStringCurrentChar(NULL, cur, &len);
3444 cur += len;
3445 }
Owen Taylor3473f882001-02-23 17:55:21 +00003446
Daniel Veillardd8224e02002-01-13 15:43:22 +00003447 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003448
3449 return(1);
3450}
3451
3452/**
3453 * xmlValidateNamesValue:
3454 * @value: an Names value
3455 *
3456 * Validate that the given value match Names production
3457 *
3458 * returns 1 if valid or 0 otherwise
3459 */
3460
Daniel Veillard9b731d72002-04-14 12:56:08 +00003461int
Owen Taylor3473f882001-02-23 17:55:21 +00003462xmlValidateNamesValue(const xmlChar *value) {
3463 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003464 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003465
3466 if (value == NULL) return(0);
3467 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003468 val = xmlStringCurrentChar(NULL, cur, &len);
3469 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003470
Daniel Veillardd8224e02002-01-13 15:43:22 +00003471 if (!IS_LETTER(val) && (val != '_') &&
3472 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003473 return(0);
3474 }
3475
Daniel Veillardd8224e02002-01-13 15:43:22 +00003476 val = xmlStringCurrentChar(NULL, cur, &len);
3477 cur += len;
3478 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3479 (val == '.') || (val == '-') ||
3480 (val == '_') || (val == ':') ||
3481 (IS_COMBINING(val)) ||
3482 (IS_EXTENDER(val))) {
3483 val = xmlStringCurrentChar(NULL, cur, &len);
3484 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003485 }
3486
Daniel Veillard807b4de2004-09-26 14:42:56 +00003487 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3488 while (val == 0x20) {
3489 while (val == 0x20) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003490 val = xmlStringCurrentChar(NULL, cur, &len);
3491 cur += len;
3492 }
3493
3494 if (!IS_LETTER(val) && (val != '_') &&
3495 (val != ':')) {
3496 return(0);
3497 }
3498 val = xmlStringCurrentChar(NULL, cur, &len);
3499 cur += len;
3500
3501 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3502 (val == '.') || (val == '-') ||
3503 (val == '_') || (val == ':') ||
3504 (IS_COMBINING(val)) ||
3505 (IS_EXTENDER(val))) {
3506 val = xmlStringCurrentChar(NULL, cur, &len);
3507 cur += len;
3508 }
3509 }
3510
3511 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003512
3513 return(1);
3514}
3515
3516/**
3517 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003518 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003519 *
3520 * Validate that the given value match Nmtoken production
3521 *
3522 * [ VC: Name Token ]
3523 *
3524 * returns 1 if valid or 0 otherwise
3525 */
3526
Daniel Veillard9b731d72002-04-14 12:56:08 +00003527int
Owen Taylor3473f882001-02-23 17:55:21 +00003528xmlValidateNmtokenValue(const xmlChar *value) {
3529 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003530 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003531
3532 if (value == NULL) return(0);
3533 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003534 val = xmlStringCurrentChar(NULL, cur, &len);
3535 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003536
Daniel Veillardd8224e02002-01-13 15:43:22 +00003537 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3538 (val != '.') && (val != '-') &&
3539 (val != '_') && (val != ':') &&
3540 (!IS_COMBINING(val)) &&
3541 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003542 return(0);
3543
Daniel Veillardd8224e02002-01-13 15:43:22 +00003544 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3545 (val == '.') || (val == '-') ||
3546 (val == '_') || (val == ':') ||
3547 (IS_COMBINING(val)) ||
3548 (IS_EXTENDER(val))) {
3549 val = xmlStringCurrentChar(NULL, cur, &len);
3550 cur += len;
3551 }
Owen Taylor3473f882001-02-23 17:55:21 +00003552
Daniel Veillardd8224e02002-01-13 15:43:22 +00003553 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003554
3555 return(1);
3556}
3557
3558/**
3559 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003560 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003561 *
3562 * Validate that the given value match Nmtokens production
3563 *
3564 * [ VC: Name Token ]
3565 *
3566 * returns 1 if valid or 0 otherwise
3567 */
3568
Daniel Veillard9b731d72002-04-14 12:56:08 +00003569int
Owen Taylor3473f882001-02-23 17:55:21 +00003570xmlValidateNmtokensValue(const xmlChar *value) {
3571 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003572 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003573
3574 if (value == NULL) return(0);
3575 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003576 val = xmlStringCurrentChar(NULL, cur, &len);
3577 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003578
Daniel Veillardd8224e02002-01-13 15:43:22 +00003579 while (IS_BLANK(val)) {
3580 val = xmlStringCurrentChar(NULL, cur, &len);
3581 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003582 }
3583
Daniel Veillardd8224e02002-01-13 15:43:22 +00003584 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3585 (val != '.') && (val != '-') &&
3586 (val != '_') && (val != ':') &&
3587 (!IS_COMBINING(val)) &&
3588 (!IS_EXTENDER(val)))
3589 return(0);
3590
3591 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3592 (val == '.') || (val == '-') ||
3593 (val == '_') || (val == ':') ||
3594 (IS_COMBINING(val)) ||
3595 (IS_EXTENDER(val))) {
3596 val = xmlStringCurrentChar(NULL, cur, &len);
3597 cur += len;
3598 }
3599
Daniel Veillard807b4de2004-09-26 14:42:56 +00003600 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3601 while (val == 0x20) {
3602 while (val == 0x20) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003603 val = xmlStringCurrentChar(NULL, cur, &len);
3604 cur += len;
3605 }
3606 if (val == 0) return(1);
3607
3608 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3609 (val != '.') && (val != '-') &&
3610 (val != '_') && (val != ':') &&
3611 (!IS_COMBINING(val)) &&
3612 (!IS_EXTENDER(val)))
3613 return(0);
3614
3615 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3616 (val == '.') || (val == '-') ||
3617 (val == '_') || (val == ':') ||
3618 (IS_COMBINING(val)) ||
3619 (IS_EXTENDER(val))) {
3620 val = xmlStringCurrentChar(NULL, cur, &len);
3621 cur += len;
3622 }
3623 }
3624
3625 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003626
3627 return(1);
3628}
3629
3630/**
3631 * xmlValidateNotationDecl:
3632 * @ctxt: the validation context
3633 * @doc: a document instance
3634 * @nota: a notation definition
3635 *
3636 * Try to validate a single notation definition
3637 * basically it does the following checks as described by the
3638 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003639 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003640 * But this function get called anyway ...
3641 *
3642 * returns 1 if valid or 0 otherwise
3643 */
3644
3645int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003646xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3647 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003648 int ret = 1;
3649
3650 return(ret);
3651}
3652
3653/**
3654 * xmlValidateAttributeValue:
3655 * @type: an attribute type
3656 * @value: an attribute value
3657 *
3658 * Validate that the given attribute value match the proper production
3659 *
3660 * [ VC: ID ]
3661 * Values of type ID must match the Name production....
3662 *
3663 * [ VC: IDREF ]
3664 * Values of type IDREF must match the Name production, and values
3665 * of type IDREFS must match Names ...
3666 *
3667 * [ VC: Entity Name ]
3668 * Values of type ENTITY must match the Name production, values
3669 * of type ENTITIES must match Names ...
3670 *
3671 * [ VC: Name Token ]
3672 * Values of type NMTOKEN must match the Nmtoken production; values
3673 * of type NMTOKENS must match Nmtokens.
3674 *
3675 * returns 1 if valid or 0 otherwise
3676 */
3677
3678int
3679xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3680 switch (type) {
3681 case XML_ATTRIBUTE_ENTITIES:
3682 case XML_ATTRIBUTE_IDREFS:
3683 return(xmlValidateNamesValue(value));
3684 case XML_ATTRIBUTE_ENTITY:
3685 case XML_ATTRIBUTE_IDREF:
3686 case XML_ATTRIBUTE_ID:
3687 case XML_ATTRIBUTE_NOTATION:
3688 return(xmlValidateNameValue(value));
3689 case XML_ATTRIBUTE_NMTOKENS:
3690 case XML_ATTRIBUTE_ENUMERATION:
3691 return(xmlValidateNmtokensValue(value));
3692 case XML_ATTRIBUTE_NMTOKEN:
3693 return(xmlValidateNmtokenValue(value));
3694 case XML_ATTRIBUTE_CDATA:
3695 break;
3696 }
3697 return(1);
3698}
3699
3700/**
3701 * xmlValidateAttributeValue2:
3702 * @ctxt: the validation context
3703 * @doc: the document
3704 * @name: the attribute name (used for error reporting only)
3705 * @type: the attribute type
3706 * @value: the attribute value
3707 *
3708 * Validate that the given attribute value match a given type.
3709 * This typically cannot be done before having finished parsing
3710 * the subsets.
3711 *
3712 * [ VC: IDREF ]
3713 * Values of type IDREF must match one of the declared IDs
3714 * Values of type IDREFS must match a sequence of the declared IDs
3715 * each Name must match the value of an ID attribute on some element
3716 * in the XML document; i.e. IDREF values must match the value of
3717 * some ID attribute
3718 *
3719 * [ VC: Entity Name ]
3720 * Values of type ENTITY must match one declared entity
3721 * Values of type ENTITIES must match a sequence of declared entities
3722 *
3723 * [ VC: Notation Attributes ]
3724 * all notation names in the declaration must be declared.
3725 *
3726 * returns 1 if valid or 0 otherwise
3727 */
3728
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003729static int
Owen Taylor3473f882001-02-23 17:55:21 +00003730xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3731 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3732 int ret = 1;
3733 switch (type) {
3734 case XML_ATTRIBUTE_IDREFS:
3735 case XML_ATTRIBUTE_IDREF:
3736 case XML_ATTRIBUTE_ID:
3737 case XML_ATTRIBUTE_NMTOKENS:
3738 case XML_ATTRIBUTE_ENUMERATION:
3739 case XML_ATTRIBUTE_NMTOKEN:
3740 case XML_ATTRIBUTE_CDATA:
3741 break;
3742 case XML_ATTRIBUTE_ENTITY: {
3743 xmlEntityPtr ent;
3744
3745 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003746 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003747 if ((ent == NULL) && (doc->standalone == 1)) {
3748 doc->standalone = 0;
3749 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003750 }
Owen Taylor3473f882001-02-23 17:55:21 +00003751 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003752 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3753 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003754 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003755 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003756 ret = 0;
3757 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003758 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3759 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003760 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003761 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003762 ret = 0;
3763 }
3764 break;
3765 }
3766 case XML_ATTRIBUTE_ENTITIES: {
3767 xmlChar *dup, *nam = NULL, *cur, save;
3768 xmlEntityPtr ent;
3769
3770 dup = xmlStrdup(value);
3771 if (dup == NULL)
3772 return(0);
3773 cur = dup;
3774 while (*cur != 0) {
3775 nam = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00003776 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003777 save = *cur;
3778 *cur = 0;
3779 ent = xmlGetDocEntity(doc, nam);
3780 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003781 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3782 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003783 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003784 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003785 ret = 0;
3786 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003787 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3788 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003789 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003790 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003791 ret = 0;
3792 }
3793 if (save == 0)
3794 break;
3795 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00003796 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003797 }
3798 xmlFree(dup);
3799 break;
3800 }
3801 case XML_ATTRIBUTE_NOTATION: {
3802 xmlNotationPtr nota;
3803
3804 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3805 if ((nota == NULL) && (doc->extSubset != NULL))
3806 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3807
3808 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003809 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3810 XML_DTD_UNKNOWN_NOTATION,
Owen Taylor3473f882001-02-23 17:55:21 +00003811 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003812 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003813 ret = 0;
3814 }
3815 break;
3816 }
3817 }
3818 return(ret);
3819}
3820
3821/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003822 * xmlValidCtxtNormalizeAttributeValue:
3823 * @ctxt: the validation context
3824 * @doc: the document
3825 * @elem: the parent
3826 * @name: the attribute name
3827 * @value: the attribute value
3828 * @ctxt: the validation context or NULL
3829 *
3830 * Does the validation related extra step of the normalization of attribute
3831 * values:
3832 *
3833 * If the declared value is not CDATA, then the XML processor must further
3834 * process the normalized attribute value by discarding any leading and
3835 * trailing space (#x20) characters, and by replacing sequences of space
3836 * (#x20) characters by single space (#x20) character.
3837 *
3838 * Also check VC: Standalone Document Declaration in P32, and update
3839 * ctxt->valid accordingly
3840 *
3841 * returns a new normalized string if normalization is needed, NULL otherwise
3842 * the caller must free the returned value.
3843 */
3844
3845xmlChar *
3846xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3847 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3848 xmlChar *ret, *dst;
3849 const xmlChar *src;
3850 xmlAttributePtr attrDecl = NULL;
3851 int extsubset = 0;
3852
3853 if (doc == NULL) return(NULL);
3854 if (elem == NULL) return(NULL);
3855 if (name == NULL) return(NULL);
3856 if (value == NULL) return(NULL);
3857
3858 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003859 xmlChar fn[50];
3860 xmlChar *fullname;
3861
3862 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3863 if (fullname == NULL)
3864 return(0);
3865 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003866 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003867 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003868 if (attrDecl != NULL)
3869 extsubset = 1;
3870 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003871 if ((fullname != fn) && (fullname != elem->name))
3872 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003873 }
3874 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3875 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3876 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3877 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3878 if (attrDecl != NULL)
3879 extsubset = 1;
3880 }
3881
3882 if (attrDecl == NULL)
3883 return(NULL);
3884 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3885 return(NULL);
3886
3887 ret = xmlStrdup(value);
3888 if (ret == NULL)
3889 return(NULL);
3890 src = value;
3891 dst = ret;
3892 while (*src == 0x20) src++;
3893 while (*src != 0) {
3894 if (*src == 0x20) {
3895 while (*src == 0x20) src++;
3896 if (*src != 0)
3897 *dst++ = 0x20;
3898 } else {
3899 *dst++ = *src++;
3900 }
3901 }
3902 *dst = 0;
3903 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003904 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003905"standalone: %s on %s value had to be normalized based on external subset declaration\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003906 name, elem->name, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003907 ctxt->valid = 0;
3908 }
3909 return(ret);
3910}
3911
3912/**
Owen Taylor3473f882001-02-23 17:55:21 +00003913 * xmlValidNormalizeAttributeValue:
3914 * @doc: the document
3915 * @elem: the parent
3916 * @name: the attribute name
3917 * @value: the attribute value
3918 *
3919 * Does the validation related extra step of the normalization of attribute
3920 * values:
3921 *
3922 * If the declared value is not CDATA, then the XML processor must further
3923 * process the normalized attribute value by discarding any leading and
3924 * trailing space (#x20) characters, and by replacing sequences of space
3925 * (#x20) characters by single space (#x20) character.
3926 *
Daniel Veillard652327a2003-09-29 18:02:38 +00003927 * Returns a new normalized string if normalization is needed, NULL otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00003928 * the caller must free the returned value.
3929 */
3930
3931xmlChar *
3932xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3933 const xmlChar *name, const xmlChar *value) {
3934 xmlChar *ret, *dst;
3935 const xmlChar *src;
3936 xmlAttributePtr attrDecl = NULL;
3937
3938 if (doc == NULL) return(NULL);
3939 if (elem == NULL) return(NULL);
3940 if (name == NULL) return(NULL);
3941 if (value == NULL) return(NULL);
3942
3943 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003944 xmlChar fn[50];
3945 xmlChar *fullname;
3946
3947 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3948 if (fullname == NULL)
3949 return(0);
3950 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003951 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003952 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3953 if ((fullname != fn) && (fullname != elem->name))
3954 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003955 }
3956 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3957 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3958 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3959
3960 if (attrDecl == NULL)
3961 return(NULL);
3962 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3963 return(NULL);
3964
3965 ret = xmlStrdup(value);
3966 if (ret == NULL)
3967 return(NULL);
3968 src = value;
3969 dst = ret;
3970 while (*src == 0x20) src++;
3971 while (*src != 0) {
3972 if (*src == 0x20) {
3973 while (*src == 0x20) src++;
3974 if (*src != 0)
3975 *dst++ = 0x20;
3976 } else {
3977 *dst++ = *src++;
3978 }
3979 }
3980 *dst = 0;
3981 return(ret);
3982}
3983
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003984static void
Owen Taylor3473f882001-02-23 17:55:21 +00003985xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003986 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003987 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3988}
3989
3990/**
3991 * xmlValidateAttributeDecl:
3992 * @ctxt: the validation context
3993 * @doc: a document instance
3994 * @attr: an attribute definition
3995 *
3996 * Try to validate a single attribute definition
3997 * basically it does the following checks as described by the
3998 * XML-1.0 recommendation:
3999 * - [ VC: Attribute Default Legal ]
4000 * - [ VC: Enumeration ]
4001 * - [ VC: ID Attribute Default ]
4002 *
4003 * The ID/IDREF uniqueness and matching are done separately
4004 *
4005 * returns 1 if valid or 0 otherwise
4006 */
4007
4008int
4009xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4010 xmlAttributePtr attr) {
4011 int ret = 1;
4012 int val;
4013 CHECK_DTD;
4014 if(attr == NULL) return(1);
4015
4016 /* Attribute Default Legal */
4017 /* Enumeration */
4018 if (attr->defaultValue != NULL) {
4019 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
4020 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004021 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004022 "Syntax of default value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004023 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004024 }
4025 ret &= val;
4026 }
4027
4028 /* ID Attribute Default */
4029 if ((attr->atype == XML_ATTRIBUTE_ID)&&
4030 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
4031 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004032 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004033 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004034 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004035 ret = 0;
4036 }
4037
4038 /* One ID per Element Type */
4039 if (attr->atype == XML_ATTRIBUTE_ID) {
4040 int nbId;
4041
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004042 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00004043 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
4044 attr->elem);
4045 if (elem != NULL) {
Daniel Veillarddbee0f12005-06-27 13:42:57 +00004046 nbId = xmlScanIDAttributeDecl(NULL, elem, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00004047 } else {
4048 xmlAttributeTablePtr table;
4049
4050 /*
4051 * The attribute may be declared in the internal subset and the
4052 * element in the external subset.
4053 */
4054 nbId = 0;
4055 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
4056 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
4057 xmlValidateAttributeIdCallback, &nbId);
4058 }
4059 if (nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004060
4061 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004062 "Element %s has %d ID attribute defined in the internal subset : %s\n",
4063 attr->elem, nbId, attr->name);
4064 } else if (doc->extSubset != NULL) {
4065 int extId = 0;
4066 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
4067 if (elem != NULL) {
Daniel Veillarddbee0f12005-06-27 13:42:57 +00004068 extId = xmlScanIDAttributeDecl(NULL, elem, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00004069 }
4070 if (extId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004071 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004072 "Element %s has %d ID attribute defined in the external subset : %s\n",
4073 attr->elem, extId, attr->name);
4074 } else if (extId + nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004075 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004076"Element %s has ID attributes defined in the internal and external subset : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004077 attr->elem, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004078 }
4079 }
4080 }
4081
4082 /* Validity Constraint: Enumeration */
4083 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
4084 xmlEnumerationPtr tree = attr->tree;
4085 while (tree != NULL) {
4086 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
4087 tree = tree->next;
4088 }
4089 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004090 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004091"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004092 attr->defaultValue, attr->name, attr->elem);
4093 ret = 0;
4094 }
4095 }
4096
4097 return(ret);
4098}
4099
4100/**
4101 * xmlValidateElementDecl:
4102 * @ctxt: the validation context
4103 * @doc: a document instance
4104 * @elem: an element definition
4105 *
4106 * Try to validate a single element definition
4107 * basically it does the following checks as described by the
4108 * XML-1.0 recommendation:
4109 * - [ VC: One ID per Element Type ]
4110 * - [ VC: No Duplicate Types ]
4111 * - [ VC: Unique Element Type Declaration ]
4112 *
4113 * returns 1 if valid or 0 otherwise
4114 */
4115
4116int
4117xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4118 xmlElementPtr elem) {
4119 int ret = 1;
4120 xmlElementPtr tst;
4121
4122 CHECK_DTD;
4123
4124 if (elem == NULL) return(1);
4125
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004126#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00004127#ifdef LIBXML_REGEXP_ENABLED
4128 /* Build the regexp associated to the content model */
4129 ret = xmlValidBuildContentModel(ctxt, elem);
4130#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004131#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00004132
Owen Taylor3473f882001-02-23 17:55:21 +00004133 /* No Duplicate Types */
4134 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
4135 xmlElementContentPtr cur, next;
4136 const xmlChar *name;
4137
4138 cur = elem->content;
4139 while (cur != NULL) {
4140 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
4141 if (cur->c1 == NULL) break;
4142 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4143 name = cur->c1->name;
4144 next = cur->c2;
4145 while (next != NULL) {
4146 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00004147 if ((xmlStrEqual(next->name, name)) &&
4148 (xmlStrEqual(next->prefix, cur->prefix))) {
4149 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004150 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00004151 "Definition of %s has duplicate references of %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004152 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004153 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004154 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004155 "Definition of %s has duplicate references of %s:%s\n",
4156 elem->name, cur->prefix, name);
4157 }
Owen Taylor3473f882001-02-23 17:55:21 +00004158 ret = 0;
4159 }
4160 break;
4161 }
4162 if (next->c1 == NULL) break;
4163 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00004164 if ((xmlStrEqual(next->c1->name, name)) &&
4165 (xmlStrEqual(next->c1->prefix, cur->prefix))) {
4166 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004167 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004168 "Definition of %s has duplicate references to %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004169 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004170 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004171 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004172 "Definition of %s has duplicate references to %s:%s\n",
4173 elem->name, cur->prefix, name);
4174 }
Owen Taylor3473f882001-02-23 17:55:21 +00004175 ret = 0;
4176 }
4177 next = next->c2;
4178 }
4179 }
4180 cur = cur->c2;
4181 }
4182 }
4183
4184 /* VC: Unique Element Type Declaration */
4185 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004186 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004187 ((tst->prefix == elem->prefix) ||
4188 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004189 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004190 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4191 "Redefinition of element %s\n",
4192 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004193 ret = 0;
4194 }
4195 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004196 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004197 ((tst->prefix == elem->prefix) ||
4198 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004199 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004200 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4201 "Redefinition of element %s\n",
4202 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004203 ret = 0;
4204 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00004205 /* One ID per Element Type
4206 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00004207 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4208 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00004209 } */
Owen Taylor3473f882001-02-23 17:55:21 +00004210 return(ret);
4211}
4212
4213/**
4214 * xmlValidateOneAttribute:
4215 * @ctxt: the validation context
4216 * @doc: a document instance
4217 * @elem: an element instance
4218 * @attr: an attribute instance
4219 * @value: the attribute value (without entities processing)
4220 *
4221 * Try to validate a single attribute for an element
4222 * basically it does the following checks as described by the
4223 * XML-1.0 recommendation:
4224 * - [ VC: Attribute Value Type ]
4225 * - [ VC: Fixed Attribute Default ]
4226 * - [ VC: Entity Name ]
4227 * - [ VC: Name Token ]
4228 * - [ VC: ID ]
4229 * - [ VC: IDREF ]
4230 * - [ VC: Entity Name ]
4231 * - [ VC: Notation Attributes ]
4232 *
4233 * The ID/IDREF uniqueness and matching are done separately
4234 *
4235 * returns 1 if valid or 0 otherwise
4236 */
4237
4238int
4239xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillard07cb8222003-09-10 10:51:05 +00004240 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4241{
Owen Taylor3473f882001-02-23 17:55:21 +00004242 xmlAttributePtr attrDecl = NULL;
4243 int val;
4244 int ret = 1;
4245
4246 CHECK_DTD;
4247 if ((elem == NULL) || (elem->name == NULL)) return(0);
4248 if ((attr == NULL) || (attr->name == NULL)) return(0);
4249
4250 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004251 xmlChar fn[50];
4252 xmlChar *fullname;
4253
4254 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4255 if (fullname == NULL)
4256 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004257 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004258 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004259 attr->name, attr->ns->prefix);
4260 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004261 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004262 attr->name, attr->ns->prefix);
4263 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004264 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004265 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4266 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00004267 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004268 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004269 if ((fullname != fn) && (fullname != elem->name))
4270 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00004271 }
4272 if (attrDecl == NULL) {
4273 if (attr->ns != NULL) {
4274 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4275 attr->name, attr->ns->prefix);
4276 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4277 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4278 attr->name, attr->ns->prefix);
4279 } else {
4280 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4281 elem->name, attr->name);
4282 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4283 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4284 elem->name, attr->name);
4285 }
4286 }
4287
4288
4289 /* Validity Constraint: Attribute Value Type */
4290 if (attrDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004291 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004292 "No declaration for attribute %s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004293 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004294 return(0);
4295 }
4296 attr->atype = attrDecl->atype;
4297
4298 val = xmlValidateAttributeValue(attrDecl->atype, value);
4299 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004300 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004301 "Syntax of value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004302 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004303 ret = 0;
4304 }
4305
4306 /* Validity constraint: Fixed Attribute Default */
4307 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4308 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004309 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004310 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004311 attr->name, elem->name, attrDecl->defaultValue);
4312 ret = 0;
4313 }
4314 }
4315
4316 /* Validity Constraint: ID uniqueness */
4317 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4318 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4319 ret = 0;
4320 }
4321
4322 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4323 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4324 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4325 ret = 0;
4326 }
4327
4328 /* Validity Constraint: Notation Attributes */
4329 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4330 xmlEnumerationPtr tree = attrDecl->tree;
4331 xmlNotationPtr nota;
4332
4333 /* First check that the given NOTATION was declared */
4334 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4335 if (nota == NULL)
4336 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4337
4338 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004339 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004340 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004341 value, attr->name, elem->name);
4342 ret = 0;
4343 }
4344
4345 /* Second, verify that it's among the list */
4346 while (tree != NULL) {
4347 if (xmlStrEqual(tree->name, value)) break;
4348 tree = tree->next;
4349 }
4350 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004351 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004352"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004353 value, attr->name, elem->name);
4354 ret = 0;
4355 }
4356 }
4357
4358 /* Validity Constraint: Enumeration */
4359 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4360 xmlEnumerationPtr tree = attrDecl->tree;
4361 while (tree != NULL) {
4362 if (xmlStrEqual(tree->name, value)) break;
4363 tree = tree->next;
4364 }
4365 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004366 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004367 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004368 value, attr->name, elem->name);
4369 ret = 0;
4370 }
4371 }
4372
4373 /* Fixed Attribute Default */
4374 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4375 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004376 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004377 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004378 attr->name, elem->name, attrDecl->defaultValue);
4379 ret = 0;
4380 }
4381
4382 /* Extra check for the attribute value */
4383 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4384 attrDecl->atype, value);
4385
4386 return(ret);
4387}
4388
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004389/**
4390 * xmlValidateOneNamespace:
4391 * @ctxt: the validation context
4392 * @doc: a document instance
4393 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004394 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004395 * @ns: an namespace declaration instance
4396 * @value: the attribute value (without entities processing)
4397 *
4398 * Try to validate a single namespace declaration for an element
4399 * basically it does the following checks as described by the
4400 * XML-1.0 recommendation:
4401 * - [ VC: Attribute Value Type ]
4402 * - [ VC: Fixed Attribute Default ]
4403 * - [ VC: Entity Name ]
4404 * - [ VC: Name Token ]
4405 * - [ VC: ID ]
4406 * - [ VC: IDREF ]
4407 * - [ VC: Entity Name ]
4408 * - [ VC: Notation Attributes ]
4409 *
4410 * The ID/IDREF uniqueness and matching are done separately
4411 *
4412 * returns 1 if valid or 0 otherwise
4413 */
4414
4415int
4416xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4417xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4418 /* xmlElementPtr elemDecl; */
4419 xmlAttributePtr attrDecl = NULL;
4420 int val;
4421 int ret = 1;
4422
4423 CHECK_DTD;
4424 if ((elem == NULL) || (elem->name == NULL)) return(0);
4425 if ((ns == NULL) || (ns->href == NULL)) return(0);
4426
4427 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004428 xmlChar fn[50];
4429 xmlChar *fullname;
4430
4431 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4432 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004433 xmlVErrMemory(ctxt, "Validating namespace");
Daniel Veillardc00cda82003-04-07 10:22:39 +00004434 return(0);
4435 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004436 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004437 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004438 ns->prefix, BAD_CAST "xmlns");
4439 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004440 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004441 ns->prefix, BAD_CAST "xmlns");
4442 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004443 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004444 BAD_CAST "xmlns");
4445 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004446 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004447 BAD_CAST "xmlns");
4448 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004449 if ((fullname != fn) && (fullname != elem->name))
4450 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004451 }
4452 if (attrDecl == NULL) {
4453 if (ns->prefix != NULL) {
4454 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4455 ns->prefix, BAD_CAST "xmlns");
4456 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4457 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4458 ns->prefix, BAD_CAST "xmlns");
4459 } else {
4460 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4461 elem->name, BAD_CAST "xmlns");
4462 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4463 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4464 elem->name, BAD_CAST "xmlns");
4465 }
4466 }
4467
4468
4469 /* Validity Constraint: Attribute Value Type */
4470 if (attrDecl == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004471 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004472 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004473 "No declaration for attribute xmlns:%s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004474 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004475 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004476 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004477 "No declaration for attribute xmlns of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004478 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004479 }
4480 return(0);
4481 }
4482
4483 val = xmlValidateAttributeValue(attrDecl->atype, value);
4484 if (val == 0) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004485 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004486 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004487 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004488 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004489 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004490 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004491 "Syntax of value for attribute xmlns of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004492 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004493 }
4494 ret = 0;
4495 }
4496
4497 /* Validity constraint: Fixed Attribute Default */
4498 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4499 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004500 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004501 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004502 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4503 ns->prefix, elem->name, attrDecl->defaultValue);
4504 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004505 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004506 "Value for attribute xmlns of %s is different from default \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004507 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004508 }
4509 ret = 0;
4510 }
4511 }
4512
4513 /* Validity Constraint: ID uniqueness */
4514 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4515 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4516 ret = 0;
4517 }
4518
4519 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4520 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4521 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4522 ret = 0;
4523 }
4524
4525 /* Validity Constraint: Notation Attributes */
4526 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4527 xmlEnumerationPtr tree = attrDecl->tree;
4528 xmlNotationPtr nota;
4529
4530 /* First check that the given NOTATION was declared */
4531 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4532 if (nota == NULL)
4533 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4534
4535 if (nota == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004536 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004537 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004538 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4539 value, ns->prefix, elem->name);
4540 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004541 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004542 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004543 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004544 }
4545 ret = 0;
4546 }
4547
4548 /* Second, verify that it's among the list */
4549 while (tree != NULL) {
4550 if (xmlStrEqual(tree->name, value)) break;
4551 tree = tree->next;
4552 }
4553 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004554 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004555 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004556"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4557 value, ns->prefix, elem->name);
4558 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004559 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004560"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004561 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004562 }
4563 ret = 0;
4564 }
4565 }
4566
4567 /* Validity Constraint: Enumeration */
4568 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4569 xmlEnumerationPtr tree = attrDecl->tree;
4570 while (tree != NULL) {
4571 if (xmlStrEqual(tree->name, value)) break;
4572 tree = tree->next;
4573 }
4574 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004575 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004576 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004577"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4578 value, ns->prefix, elem->name);
4579 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004580 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004581"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004582 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004583 }
4584 ret = 0;
4585 }
4586 }
4587
4588 /* Fixed Attribute Default */
4589 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4590 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004591 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004592 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004593 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4594 ns->prefix, elem->name, attrDecl->defaultValue);
4595 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004596 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004597 "Value for attribute xmlns of %s must be \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004598 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004599 }
4600 ret = 0;
4601 }
4602
4603 /* Extra check for the attribute value */
4604 if (ns->prefix != NULL) {
4605 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4606 attrDecl->atype, value);
4607 } else {
4608 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4609 attrDecl->atype, value);
4610 }
4611
4612 return(ret);
4613}
4614
Daniel Veillard118aed72002-09-24 14:13:13 +00004615#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004616/**
4617 * xmlValidateSkipIgnorable:
4618 * @ctxt: the validation context
4619 * @child: the child list
4620 *
4621 * Skip ignorable elements w.r.t. the validation process
4622 *
4623 * returns the first element to consider for validation of the content model
4624 */
4625
4626static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004627xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004628 while (child != NULL) {
4629 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004630 /* These things are ignored (skipped) during validation. */
4631 case XML_PI_NODE:
4632 case XML_COMMENT_NODE:
4633 case XML_XINCLUDE_START:
4634 case XML_XINCLUDE_END:
4635 child = child->next;
4636 break;
4637 case XML_TEXT_NODE:
4638 if (xmlIsBlankNode(child))
4639 child = child->next;
4640 else
4641 return(child);
4642 break;
4643 /* keep current node */
4644 default:
4645 return(child);
4646 }
4647 }
4648 return(child);
4649}
4650
4651/**
4652 * xmlValidateElementType:
4653 * @ctxt: the validation context
4654 *
4655 * Try to validate the content model of an element internal function
4656 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004657 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4658 * reference is found and -3 if the validation succeeded but
4659 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004660 */
4661
4662static int
4663xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004664 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004665 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004666
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004667 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004668 if ((NODE == NULL) && (CONT == NULL))
4669 return(1);
4670 if ((NODE == NULL) &&
4671 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4672 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4673 return(1);
4674 }
4675 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004676 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004677 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004678
4679 /*
4680 * We arrive here when more states need to be examined
4681 */
4682cont:
4683
4684 /*
4685 * We just recovered from a rollback generated by a possible
4686 * epsilon transition, go directly to the analysis phase
4687 */
4688 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004689 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004690 DEBUG_VALID_STATE(NODE, CONT)
4691 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004692 goto analyze;
4693 }
4694
4695 DEBUG_VALID_STATE(NODE, CONT)
4696 /*
4697 * we may have to save a backup state here. This is the equivalent
4698 * of handling epsilon transition in NFAs.
4699 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004700 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004701 ((CONT->parent == NULL) ||
4702 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004703 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004704 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004705 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004706 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004707 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4708 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004709 }
4710
4711
4712 /*
4713 * Check first if the content matches
4714 */
4715 switch (CONT->type) {
4716 case XML_ELEMENT_CONTENT_PCDATA:
4717 if (NODE == NULL) {
4718 DEBUG_VALID_MSG("pcdata failed no node");
4719 ret = 0;
4720 break;
4721 }
4722 if (NODE->type == XML_TEXT_NODE) {
4723 DEBUG_VALID_MSG("pcdata found, skip to next");
4724 /*
4725 * go to next element in the content model
4726 * skipping ignorable elems
4727 */
4728 do {
4729 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004730 NODE = xmlValidateSkipIgnorable(NODE);
4731 if ((NODE != NULL) &&
4732 (NODE->type == XML_ENTITY_REF_NODE))
4733 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004734 } while ((NODE != NULL) &&
4735 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004736 (NODE->type != XML_TEXT_NODE) &&
4737 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004738 ret = 1;
4739 break;
4740 } else {
4741 DEBUG_VALID_MSG("pcdata failed");
4742 ret = 0;
4743 break;
4744 }
4745 break;
4746 case XML_ELEMENT_CONTENT_ELEMENT:
4747 if (NODE == NULL) {
4748 DEBUG_VALID_MSG("element failed no node");
4749 ret = 0;
4750 break;
4751 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004752 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4753 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004754 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004755 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4756 ret = (CONT->prefix == NULL);
4757 } else if (CONT->prefix == NULL) {
4758 ret = 0;
4759 } else {
4760 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4761 }
4762 }
4763 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004764 DEBUG_VALID_MSG("element found, skip to next");
4765 /*
4766 * go to next element in the content model
4767 * skipping ignorable elems
4768 */
4769 do {
4770 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004771 NODE = xmlValidateSkipIgnorable(NODE);
4772 if ((NODE != NULL) &&
4773 (NODE->type == XML_ENTITY_REF_NODE))
4774 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004775 } while ((NODE != NULL) &&
4776 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004777 (NODE->type != XML_TEXT_NODE) &&
4778 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004779 } else {
4780 DEBUG_VALID_MSG("element failed");
4781 ret = 0;
4782 break;
4783 }
4784 break;
4785 case XML_ELEMENT_CONTENT_OR:
4786 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004787 * Small optimization.
4788 */
4789 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4790 if ((NODE == NULL) ||
4791 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4792 DEPTH++;
4793 CONT = CONT->c2;
4794 goto cont;
4795 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004796 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4797 ret = (CONT->c1->prefix == NULL);
4798 } else if (CONT->c1->prefix == NULL) {
4799 ret = 0;
4800 } else {
4801 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4802 }
4803 if (ret == 0) {
4804 DEPTH++;
4805 CONT = CONT->c2;
4806 goto cont;
4807 }
Daniel Veillard85349052001-04-20 13:48:21 +00004808 }
4809
4810 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004811 * save the second branch 'or' branch
4812 */
4813 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004814 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4815 OCCURS, ROLLBACK_OR) < 0)
4816 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004817 DEPTH++;
4818 CONT = CONT->c1;
4819 goto cont;
4820 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004821 /*
4822 * Small optimization.
4823 */
4824 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4825 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4826 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4827 if ((NODE == NULL) ||
4828 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4829 DEPTH++;
4830 CONT = CONT->c2;
4831 goto cont;
4832 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004833 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4834 ret = (CONT->c1->prefix == NULL);
4835 } else if (CONT->c1->prefix == NULL) {
4836 ret = 0;
4837 } else {
4838 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4839 }
4840 if (ret == 0) {
4841 DEPTH++;
4842 CONT = CONT->c2;
4843 goto cont;
4844 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004845 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004846 DEPTH++;
4847 CONT = CONT->c1;
4848 goto cont;
4849 }
4850
4851 /*
4852 * At this point handle going up in the tree
4853 */
4854 if (ret == -1) {
4855 DEBUG_VALID_MSG("error found returning");
4856 return(ret);
4857 }
4858analyze:
4859 while (CONT != NULL) {
4860 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004861 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004862 * this level.
4863 */
4864 if (ret == 0) {
4865 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004866 xmlNodePtr cur;
4867
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004868 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004869 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004870 DEBUG_VALID_MSG("Once branch failed, rollback");
4871 if (vstateVPop(ctxt) < 0 ) {
4872 DEBUG_VALID_MSG("exhaustion, failed");
4873 return(0);
4874 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004875 if (cur != ctxt->vstate->node)
4876 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004877 goto cont;
4878 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004879 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004880 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004881 DEBUG_VALID_MSG("Plus branch failed, rollback");
4882 if (vstateVPop(ctxt) < 0 ) {
4883 DEBUG_VALID_MSG("exhaustion, failed");
4884 return(0);
4885 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004886 if (cur != ctxt->vstate->node)
4887 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004888 goto cont;
4889 }
4890 DEBUG_VALID_MSG("Plus branch found");
4891 ret = 1;
4892 break;
4893 case XML_ELEMENT_CONTENT_MULT:
4894#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004895 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004896 DEBUG_VALID_MSG("Mult branch failed");
4897 } else {
4898 DEBUG_VALID_MSG("Mult branch found");
4899 }
4900#endif
4901 ret = 1;
4902 break;
4903 case XML_ELEMENT_CONTENT_OPT:
4904 DEBUG_VALID_MSG("Option branch failed");
4905 ret = 1;
4906 break;
4907 }
4908 } else {
4909 switch (CONT->ocur) {
4910 case XML_ELEMENT_CONTENT_OPT:
4911 DEBUG_VALID_MSG("Option branch succeeded");
4912 ret = 1;
4913 break;
4914 case XML_ELEMENT_CONTENT_ONCE:
4915 DEBUG_VALID_MSG("Once branch succeeded");
4916 ret = 1;
4917 break;
4918 case XML_ELEMENT_CONTENT_PLUS:
4919 if (STATE == ROLLBACK_PARENT) {
4920 DEBUG_VALID_MSG("Plus branch rollback");
4921 ret = 1;
4922 break;
4923 }
4924 if (NODE == NULL) {
4925 DEBUG_VALID_MSG("Plus branch exhausted");
4926 ret = 1;
4927 break;
4928 }
4929 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004930 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004931 goto cont;
4932 case XML_ELEMENT_CONTENT_MULT:
4933 if (STATE == ROLLBACK_PARENT) {
4934 DEBUG_VALID_MSG("Mult branch rollback");
4935 ret = 1;
4936 break;
4937 }
4938 if (NODE == NULL) {
4939 DEBUG_VALID_MSG("Mult branch exhausted");
4940 ret = 1;
4941 break;
4942 }
4943 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004944 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004945 goto cont;
4946 }
4947 }
4948 STATE = 0;
4949
4950 /*
4951 * Then act accordingly at the parent level
4952 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004953 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004954 if (CONT->parent == NULL)
4955 break;
4956
4957 switch (CONT->parent->type) {
4958 case XML_ELEMENT_CONTENT_PCDATA:
4959 DEBUG_VALID_MSG("Error: parent pcdata");
4960 return(-1);
4961 case XML_ELEMENT_CONTENT_ELEMENT:
4962 DEBUG_VALID_MSG("Error: parent element");
4963 return(-1);
4964 case XML_ELEMENT_CONTENT_OR:
4965 if (ret == 1) {
4966 DEBUG_VALID_MSG("Or succeeded");
4967 CONT = CONT->parent;
4968 DEPTH--;
4969 } else {
4970 DEBUG_VALID_MSG("Or failed");
4971 CONT = CONT->parent;
4972 DEPTH--;
4973 }
4974 break;
4975 case XML_ELEMENT_CONTENT_SEQ:
4976 if (ret == 0) {
4977 DEBUG_VALID_MSG("Sequence failed");
4978 CONT = CONT->parent;
4979 DEPTH--;
4980 } else if (CONT == CONT->parent->c1) {
4981 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4982 CONT = CONT->parent->c2;
4983 goto cont;
4984 } else {
4985 DEBUG_VALID_MSG("Sequence succeeded");
4986 CONT = CONT->parent;
4987 DEPTH--;
4988 }
4989 }
4990 }
4991 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004992 xmlNodePtr cur;
4993
4994 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004995 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4996 if (vstateVPop(ctxt) < 0 ) {
4997 DEBUG_VALID_MSG("exhaustion, failed");
4998 return(0);
4999 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005000 if (cur != ctxt->vstate->node)
5001 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005002 goto cont;
5003 }
5004 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005005 xmlNodePtr cur;
5006
5007 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005008 DEBUG_VALID_MSG("Failure, rollback");
5009 if (vstateVPop(ctxt) < 0 ) {
5010 DEBUG_VALID_MSG("exhaustion, failed");
5011 return(0);
5012 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005013 if (cur != ctxt->vstate->node)
5014 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005015 goto cont;
5016 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005017 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005018}
Daniel Veillard23e73572002-09-19 19:56:43 +00005019#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005020
5021/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00005022 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005023 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00005024 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005025 * @content: An element
5026 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
5027 *
5028 * This will dump the list of elements to the buffer
5029 * Intended just for the debug routine
5030 */
5031static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00005032xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005033 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00005034 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005035
5036 if (node == NULL) return;
5037 if (glob) strcat(buf, "(");
5038 cur = node;
5039 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005040 len = strlen(buf);
5041 if (size - len < 50) {
5042 if ((size - len > 4) && (buf[len - 1] != '.'))
5043 strcat(buf, " ...");
5044 return;
5045 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005046 switch (cur->type) {
5047 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005048 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005049 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005050 if ((size - len > 4) && (buf[len - 1] != '.'))
5051 strcat(buf, " ...");
5052 return;
5053 }
5054 strcat(buf, (char *) cur->ns->prefix);
5055 strcat(buf, ":");
5056 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005057 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005058 if ((size - len > 4) && (buf[len - 1] != '.'))
5059 strcat(buf, " ...");
5060 return;
5061 }
5062 strcat(buf, (char *) cur->name);
5063 if (cur->next != NULL)
5064 strcat(buf, " ");
5065 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005066 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005067 if (xmlIsBlankNode(cur))
5068 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005069 case XML_CDATA_SECTION_NODE:
5070 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005071 strcat(buf, "CDATA");
5072 if (cur->next != NULL)
5073 strcat(buf, " ");
5074 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005075 case XML_ATTRIBUTE_NODE:
5076 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005077#ifdef LIBXML_DOCB_ENABLED
5078 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005079#endif
5080 case XML_HTML_DOCUMENT_NODE:
5081 case XML_DOCUMENT_TYPE_NODE:
5082 case XML_DOCUMENT_FRAG_NODE:
5083 case XML_NOTATION_NODE:
5084 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005085 strcat(buf, "???");
5086 if (cur->next != NULL)
5087 strcat(buf, " ");
5088 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005089 case XML_ENTITY_NODE:
5090 case XML_PI_NODE:
5091 case XML_DTD_NODE:
5092 case XML_COMMENT_NODE:
5093 case XML_ELEMENT_DECL:
5094 case XML_ATTRIBUTE_DECL:
5095 case XML_ENTITY_DECL:
5096 case XML_XINCLUDE_START:
5097 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005098 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005099 }
5100 cur = cur->next;
5101 }
5102 if (glob) strcat(buf, ")");
5103}
5104
5105/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005106 * xmlValidateElementContent:
5107 * @ctxt: the validation context
5108 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005109 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005110 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005111 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005112 *
5113 * Try to validate the content model of an element
5114 *
5115 * returns 1 if valid or 0 if not and -1 in case of error
5116 */
5117
5118static int
5119xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005120 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00005121 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00005122#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00005123 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00005124#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00005125 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005126 xmlElementContentPtr cont;
5127 const xmlChar *name;
5128
5129 if (elemDecl == NULL)
5130 return(-1);
5131 cont = elemDecl->content;
5132 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005133
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005134#ifdef LIBXML_REGEXP_ENABLED
5135 /* Build the regexp associated to the content model */
5136 if (elemDecl->contModel == NULL)
5137 ret = xmlValidBuildContentModel(ctxt, elemDecl);
5138 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005139 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005140 } else {
5141 xmlRegExecCtxtPtr exec;
5142
Daniel Veillardec498e12003-02-05 11:01:50 +00005143 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
5144 return(-1);
5145 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005146 ctxt->nodeMax = 0;
5147 ctxt->nodeNr = 0;
5148 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005149 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
5150 if (exec != NULL) {
5151 cur = child;
5152 while (cur != NULL) {
5153 switch (cur->type) {
5154 case XML_ENTITY_REF_NODE:
5155 /*
5156 * Push the current node to be able to roll back
5157 * and process within the entity
5158 */
5159 if ((cur->children != NULL) &&
5160 (cur->children->children != NULL)) {
5161 nodeVPush(ctxt, cur);
5162 cur = cur->children->children;
5163 continue;
5164 }
5165 break;
5166 case XML_TEXT_NODE:
5167 if (xmlIsBlankNode(cur))
5168 break;
5169 ret = 0;
5170 goto fail;
5171 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00005172 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005173 ret = 0;
5174 goto fail;
5175 case XML_ELEMENT_NODE:
5176 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005177 xmlChar fn[50];
5178 xmlChar *fullname;
5179
5180 fullname = xmlBuildQName(cur->name,
5181 cur->ns->prefix, fn, 50);
5182 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005183 ret = -1;
5184 goto fail;
5185 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005186 ret = xmlRegExecPushString(exec, fullname, NULL);
5187 if ((fullname != fn) && (fullname != cur->name))
5188 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005189 } else {
5190 ret = xmlRegExecPushString(exec, cur->name, NULL);
5191 }
5192 break;
5193 default:
5194 break;
5195 }
5196 /*
5197 * Switch to next element
5198 */
5199 cur = cur->next;
5200 while (cur == NULL) {
5201 cur = nodeVPop(ctxt);
5202 if (cur == NULL)
5203 break;
5204 cur = cur->next;
5205 }
5206 }
5207 ret = xmlRegExecPushString(exec, NULL, NULL);
5208fail:
5209 xmlRegFreeExecCtxt(exec);
5210 }
5211 }
5212#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005213 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005214 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005215 */
5216 ctxt->vstateMax = 8;
5217 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5218 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5219 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005220 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005221 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005222 }
5223 /*
5224 * The first entry in the stack is reserved to the current state
5225 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00005226 ctxt->nodeMax = 0;
5227 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00005228 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005229 ctxt->vstate = &ctxt->vstateTab[0];
5230 ctxt->vstateNr = 1;
5231 CONT = cont;
5232 NODE = child;
5233 DEPTH = 0;
5234 OCCURS = 0;
5235 STATE = 0;
5236 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005237 if ((ret == -3) && (warn)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005238 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
5239 "Content model for Element %s is ambiguous\n",
Daniel Veillard0cc72772003-10-13 14:00:21 +00005240 name, NULL, NULL);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005241 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005242 /*
5243 * An entities reference appeared at this level.
5244 * Buid a minimal representation of this node content
5245 * sufficient to run the validation process on it
5246 */
5247 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005248 cur = child;
5249 while (cur != NULL) {
5250 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005251 case XML_ENTITY_REF_NODE:
5252 /*
5253 * Push the current node to be able to roll back
5254 * and process within the entity
5255 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005256 if ((cur->children != NULL) &&
5257 (cur->children->children != NULL)) {
5258 nodeVPush(ctxt, cur);
5259 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005260 continue;
5261 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00005262 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005263 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005264 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005265 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005266 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005267 case XML_CDATA_SECTION_NODE:
5268 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005269 case XML_ELEMENT_NODE:
5270 /*
5271 * Allocate a new node and minimally fills in
5272 * what's required
5273 */
5274 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5275 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005276 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005277 xmlFreeNodeList(repl);
5278 ret = -1;
5279 goto done;
5280 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005281 tmp->type = cur->type;
5282 tmp->name = cur->name;
5283 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005284 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00005285 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005286 if (repl == NULL)
5287 repl = last = tmp;
5288 else {
5289 last->next = tmp;
5290 last = tmp;
5291 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005292 if (cur->type == XML_CDATA_SECTION_NODE) {
5293 /*
5294 * E59 spaces in CDATA does not match the
5295 * nonterminal S
5296 */
5297 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5298 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005299 break;
5300 default:
5301 break;
5302 }
5303 /*
5304 * Switch to next element
5305 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005306 cur = cur->next;
5307 while (cur == NULL) {
5308 cur = nodeVPop(ctxt);
5309 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005310 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005311 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005312 }
5313 }
5314
5315 /*
5316 * Relaunch the validation
5317 */
5318 ctxt->vstate = &ctxt->vstateTab[0];
5319 ctxt->vstateNr = 1;
5320 CONT = cont;
5321 NODE = repl;
5322 DEPTH = 0;
5323 OCCURS = 0;
5324 STATE = 0;
5325 ret = xmlValidateElementType(ctxt);
5326 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005327#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005328 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005329 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
5330 char expr[5000];
5331 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005332
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005333 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005334 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005335 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00005336#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005337 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005338 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005339 else
Daniel Veillard01992e02002-10-09 10:20:30 +00005340#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005341 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005342
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005343 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005344 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5345 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5346 name, BAD_CAST expr, BAD_CAST list);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005347 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005348 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5349 "Element content does not follow the DTD, expecting %s, got %s\n",
5350 BAD_CAST expr, BAD_CAST list, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005351 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005352 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005353 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005354 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005355 "Element %s content does not follow the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005356 name, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005357 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005358 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5359 "Element content does not follow the DTD\n",
5360 NULL, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005361 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005362 }
5363 ret = 0;
5364 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005365 if (ret == -3)
5366 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005367
Daniel Veillard23e73572002-09-19 19:56:43 +00005368#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005369done:
5370 /*
5371 * Deallocate the copy if done, and free up the validation stack
5372 */
5373 while (repl != NULL) {
5374 tmp = repl->next;
5375 xmlFree(repl);
5376 repl = tmp;
5377 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005378 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005379 if (ctxt->vstateTab != NULL) {
5380 xmlFree(ctxt->vstateTab);
5381 ctxt->vstateTab = NULL;
5382 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005383#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005384 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00005385 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005386 if (ctxt->nodeTab != NULL) {
5387 xmlFree(ctxt->nodeTab);
5388 ctxt->nodeTab = NULL;
5389 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005390 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005391
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005392}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005393
Owen Taylor3473f882001-02-23 17:55:21 +00005394/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005395 * xmlValidateCdataElement:
5396 * @ctxt: the validation context
5397 * @doc: a document instance
5398 * @elem: an element instance
5399 *
5400 * Check that an element follows #CDATA
5401 *
5402 * returns 1 if valid or 0 otherwise
5403 */
5404static int
5405xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5406 xmlNodePtr elem) {
5407 int ret = 1;
5408 xmlNodePtr cur, child;
5409
Daniel Veillardceb09b92002-10-04 11:46:37 +00005410 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005411 return(0);
5412
5413 child = elem->children;
5414
5415 cur = child;
5416 while (cur != NULL) {
5417 switch (cur->type) {
5418 case XML_ENTITY_REF_NODE:
5419 /*
5420 * Push the current node to be able to roll back
5421 * and process within the entity
5422 */
5423 if ((cur->children != NULL) &&
5424 (cur->children->children != NULL)) {
5425 nodeVPush(ctxt, cur);
5426 cur = cur->children->children;
5427 continue;
5428 }
5429 break;
5430 case XML_COMMENT_NODE:
5431 case XML_PI_NODE:
5432 case XML_TEXT_NODE:
5433 case XML_CDATA_SECTION_NODE:
5434 break;
5435 default:
5436 ret = 0;
5437 goto done;
5438 }
5439 /*
5440 * Switch to next element
5441 */
5442 cur = cur->next;
5443 while (cur == NULL) {
5444 cur = nodeVPop(ctxt);
5445 if (cur == NULL)
5446 break;
5447 cur = cur->next;
5448 }
5449 }
5450done:
5451 ctxt->nodeMax = 0;
5452 ctxt->nodeNr = 0;
5453 if (ctxt->nodeTab != NULL) {
5454 xmlFree(ctxt->nodeTab);
5455 ctxt->nodeTab = NULL;
5456 }
5457 return(ret);
5458}
5459
5460/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005461 * xmlValidateCheckMixed:
5462 * @ctxt: the validation context
5463 * @cont: the mixed content model
5464 * @qname: the qualified name as appearing in the serialization
5465 *
5466 * Check if the given node is part of the content model.
5467 *
5468 * Returns 1 if yes, 0 if no, -1 in case of error
5469 */
5470static int
William M. Brackedb65a72004-02-06 07:36:04 +00005471xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005472 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005473 const xmlChar *name;
5474 int plen;
5475 name = xmlSplitQName3(qname, &plen);
5476
5477 if (name == NULL) {
5478 while (cont != NULL) {
5479 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5480 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5481 return(1);
5482 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5483 (cont->c1 != NULL) &&
5484 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5485 if ((cont->c1->prefix == NULL) &&
5486 (xmlStrEqual(cont->c1->name, qname)))
5487 return(1);
5488 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5489 (cont->c1 == NULL) ||
5490 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005491 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5492 "Internal: MIXED struct corrupted\n",
5493 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005494 break;
5495 }
5496 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005497 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005498 } else {
5499 while (cont != NULL) {
5500 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5501 if ((cont->prefix != NULL) &&
5502 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5503 (xmlStrEqual(cont->name, name)))
5504 return(1);
5505 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5506 (cont->c1 != NULL) &&
5507 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5508 if ((cont->c1->prefix != NULL) &&
5509 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5510 (xmlStrEqual(cont->c1->name, name)))
5511 return(1);
5512 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5513 (cont->c1 == NULL) ||
5514 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005515 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5516 "Internal: MIXED struct corrupted\n",
5517 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005518 break;
5519 }
5520 cont = cont->c2;
5521 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005522 }
5523 return(0);
5524}
5525
5526/**
5527 * xmlValidGetElemDecl:
5528 * @ctxt: the validation context
5529 * @doc: a document instance
5530 * @elem: an element instance
5531 * @extsubset: pointer, (out) indicate if the declaration was found
5532 * in the external subset.
5533 *
5534 * Finds a declaration associated to an element in the document.
5535 *
5536 * returns the pointer to the declaration or NULL if not found.
5537 */
5538static xmlElementPtr
5539xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5540 xmlNodePtr elem, int *extsubset) {
5541 xmlElementPtr elemDecl = NULL;
5542 const xmlChar *prefix = NULL;
5543
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005544 if ((ctxt == NULL) || (doc == NULL) ||
5545 (elem == NULL) || (elem->name == NULL))
5546 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005547 if (extsubset != NULL)
5548 *extsubset = 0;
5549
5550 /*
5551 * Fetch the declaration for the qualified name
5552 */
5553 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5554 prefix = elem->ns->prefix;
5555
5556 if (prefix != NULL) {
5557 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5558 elem->name, prefix);
5559 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5560 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5561 elem->name, prefix);
5562 if ((elemDecl != NULL) && (extsubset != NULL))
5563 *extsubset = 1;
5564 }
5565 }
5566
5567 /*
5568 * Fetch the declaration for the non qualified name
5569 * This is "non-strict" validation should be done on the
5570 * full QName but in that case being flexible makes sense.
5571 */
5572 if (elemDecl == NULL) {
5573 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5574 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5575 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5576 if ((elemDecl != NULL) && (extsubset != NULL))
5577 *extsubset = 1;
5578 }
5579 }
5580 if (elemDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005581 xmlErrValidNode(ctxt, elem,
5582 XML_DTD_UNKNOWN_ELEM,
5583 "No declaration for element %s\n",
5584 elem->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005585 }
5586 return(elemDecl);
5587}
5588
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005589#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005590/**
5591 * xmlValidatePushElement:
5592 * @ctxt: the validation context
5593 * @doc: a document instance
5594 * @elem: an element instance
5595 * @qname: the qualified name as appearing in the serialization
5596 *
5597 * Push a new element start on the validation stack.
5598 *
5599 * returns 1 if no validation problem was found or 0 otherwise
5600 */
5601int
5602xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5603 xmlNodePtr elem, const xmlChar *qname) {
5604 int ret = 1;
5605 xmlElementPtr eDecl;
5606 int extsubset = 0;
5607
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005608 if (ctxt == NULL)
5609 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005610/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005611 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5612 xmlValidStatePtr state = ctxt->vstate;
5613 xmlElementPtr elemDecl;
5614
5615 /*
5616 * Check the new element agaisnt the content model of the new elem.
5617 */
5618 if (state->elemDecl != NULL) {
5619 elemDecl = state->elemDecl;
5620
5621 switch(elemDecl->etype) {
5622 case XML_ELEMENT_TYPE_UNDEFINED:
5623 ret = 0;
5624 break;
5625 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005626 xmlErrValidNode(ctxt, state->node,
5627 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005628 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005629 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005630 ret = 0;
5631 break;
5632 case XML_ELEMENT_TYPE_ANY:
5633 /* I don't think anything is required then */
5634 break;
5635 case XML_ELEMENT_TYPE_MIXED:
5636 /* simple case of declared as #PCDATA */
5637 if ((elemDecl->content != NULL) &&
5638 (elemDecl->content->type ==
5639 XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005640 xmlErrValidNode(ctxt, state->node,
5641 XML_DTD_NOT_PCDATA,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005642 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005643 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005644 ret = 0;
5645 } else {
5646 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5647 qname);
5648 if (ret != 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005649 xmlErrValidNode(ctxt, state->node,
5650 XML_DTD_INVALID_CHILD,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005651 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005652 qname, state->node->name, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005653 }
5654 }
5655 break;
5656 case XML_ELEMENT_TYPE_ELEMENT:
5657 /*
5658 * TODO:
5659 * VC: Standalone Document Declaration
5660 * - element types with element content, if white space
5661 * occurs directly within any instance of those types.
5662 */
5663 if (state->exec != NULL) {
5664 ret = xmlRegExecPushString(state->exec, qname, NULL);
5665 if (ret < 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005666 xmlErrValidNode(ctxt, state->node,
5667 XML_DTD_CONTENT_MODEL,
5668 "Element %s content does not follow the DTD, Misplaced %s\n",
5669 state->node->name, qname, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005670 ret = 0;
5671 } else {
5672 ret = 1;
5673 }
5674 }
5675 break;
5676 }
5677 }
5678 }
5679 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5680 vstateVPush(ctxt, eDecl, elem);
5681 return(ret);
5682}
5683
5684/**
5685 * xmlValidatePushCData:
5686 * @ctxt: the validation context
5687 * @data: some character data read
5688 * @len: the lenght of the data
5689 *
5690 * check the CData parsed for validation in the current stack
5691 *
5692 * returns 1 if no validation problem was found or 0 otherwise
5693 */
5694int
5695xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5696 int ret = 1;
5697
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005698/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005699 if (ctxt == NULL)
5700 return(0);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005701 if (len <= 0)
5702 return(ret);
5703 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5704 xmlValidStatePtr state = ctxt->vstate;
5705 xmlElementPtr elemDecl;
5706
5707 /*
5708 * Check the new element agaisnt the content model of the new elem.
5709 */
5710 if (state->elemDecl != NULL) {
5711 elemDecl = state->elemDecl;
5712
5713 switch(elemDecl->etype) {
5714 case XML_ELEMENT_TYPE_UNDEFINED:
5715 ret = 0;
5716 break;
5717 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005718 xmlErrValidNode(ctxt, state->node,
5719 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005720 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005721 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005722 ret = 0;
5723 break;
5724 case XML_ELEMENT_TYPE_ANY:
5725 break;
5726 case XML_ELEMENT_TYPE_MIXED:
5727 break;
5728 case XML_ELEMENT_TYPE_ELEMENT:
5729 if (len > 0) {
5730 int i;
5731
5732 for (i = 0;i < len;i++) {
William M. Brack76e95df2003-10-18 16:20:14 +00005733 if (!IS_BLANK_CH(data[i])) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005734 xmlErrValidNode(ctxt, state->node,
5735 XML_DTD_CONTENT_MODEL,
5736 "Element %s content does not follow the DTD, Text not allowed\n",
5737 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005738 ret = 0;
5739 goto done;
5740 }
5741 }
5742 /*
5743 * TODO:
5744 * VC: Standalone Document Declaration
5745 * element types with element content, if white space
5746 * occurs directly within any instance of those types.
5747 */
5748 }
5749 break;
5750 }
5751 }
5752 }
5753done:
5754 return(ret);
5755}
5756
5757/**
5758 * xmlValidatePopElement:
5759 * @ctxt: the validation context
5760 * @doc: a document instance
5761 * @elem: an element instance
5762 * @qname: the qualified name as appearing in the serialization
5763 *
5764 * Pop the element end from the validation stack.
5765 *
5766 * returns 1 if no validation problem was found or 0 otherwise
5767 */
5768int
5769xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005770 xmlNodePtr elem ATTRIBUTE_UNUSED,
5771 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005772 int ret = 1;
5773
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005774 if (ctxt == NULL)
5775 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005776/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005777 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5778 xmlValidStatePtr state = ctxt->vstate;
5779 xmlElementPtr elemDecl;
5780
5781 /*
5782 * Check the new element agaisnt the content model of the new elem.
5783 */
5784 if (state->elemDecl != NULL) {
5785 elemDecl = state->elemDecl;
5786
5787 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5788 if (state->exec != NULL) {
5789 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5790 if (ret == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005791 xmlErrValidNode(ctxt, state->node,
5792 XML_DTD_CONTENT_MODEL,
5793 "Element %s content does not follow the DTD, Expecting more child\n",
5794 state->node->name, NULL,NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005795 } else {
5796 /*
5797 * previous validation errors should not generate
5798 * a new one here
5799 */
5800 ret = 1;
5801 }
5802 }
5803 }
5804 }
5805 vstateVPop(ctxt);
5806 }
5807 return(ret);
5808}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005809#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005810
5811/**
Owen Taylor3473f882001-02-23 17:55:21 +00005812 * xmlValidateOneElement:
5813 * @ctxt: the validation context
5814 * @doc: a document instance
5815 * @elem: an element instance
5816 *
5817 * Try to validate a single element and it's attributes,
5818 * basically it does the following checks as described by the
5819 * XML-1.0 recommendation:
5820 * - [ VC: Element Valid ]
5821 * - [ VC: Required Attribute ]
5822 * Then call xmlValidateOneAttribute() for each attribute present.
5823 *
5824 * The ID/IDREF checkings are done separately
5825 *
5826 * returns 1 if valid or 0 otherwise
5827 */
5828
5829int
5830xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5831 xmlNodePtr elem) {
5832 xmlElementPtr elemDecl = NULL;
5833 xmlElementContentPtr cont;
5834 xmlAttributePtr attr;
5835 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005836 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005837 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005838 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005839
5840 CHECK_DTD;
5841
5842 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005843 switch (elem->type) {
5844 case XML_ATTRIBUTE_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005845 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5846 "Attribute element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005847 return(0);
5848 case XML_TEXT_NODE:
5849 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005850 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5851 "Text element has children !\n",
5852 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005853 return(0);
5854 }
5855 if (elem->properties != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005856 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5857 "Text element has attribute !\n",
5858 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005859 return(0);
5860 }
5861 if (elem->ns != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005862 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5863 "Text element has namespace !\n",
5864 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005865 return(0);
5866 }
5867 if (elem->nsDef != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005868 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5869 "Text element has namespace !\n",
5870 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005871 return(0);
5872 }
5873 if (elem->content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005874 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5875 "Text element has no content !\n",
5876 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005877 return(0);
5878 }
5879 return(1);
5880 case XML_XINCLUDE_START:
5881 case XML_XINCLUDE_END:
5882 return(1);
5883 case XML_CDATA_SECTION_NODE:
5884 case XML_ENTITY_REF_NODE:
5885 case XML_PI_NODE:
5886 case XML_COMMENT_NODE:
5887 return(1);
5888 case XML_ENTITY_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005889 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5890 "Entity element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005891 return(0);
5892 case XML_NOTATION_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005893 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5894 "Notation element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005895 return(0);
5896 case XML_DOCUMENT_NODE:
5897 case XML_DOCUMENT_TYPE_NODE:
5898 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005899 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5900 "Document element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005901 return(0);
5902 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005903 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5904 "HTML Document not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005905 return(0);
5906 case XML_ELEMENT_NODE:
5907 break;
5908 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005909 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5910 "unknown element type\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005911 return(0);
5912 }
Owen Taylor3473f882001-02-23 17:55:21 +00005913
5914 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005915 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005916 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005917 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5918 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005919 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005920
Daniel Veillardea7751d2002-12-20 00:16:24 +00005921 /*
5922 * If vstateNr is not zero that means continuous validation is
5923 * activated, do not try to check the content model at that level.
5924 */
5925 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005926 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005927 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005928 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005929 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
5930 "No declaration for element %s\n",
5931 elem->name, NULL, NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005932 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005933 case XML_ELEMENT_TYPE_EMPTY:
5934 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005935 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
Owen Taylor3473f882001-02-23 17:55:21 +00005936 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005937 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005938 ret = 0;
5939 }
5940 break;
5941 case XML_ELEMENT_TYPE_ANY:
5942 /* I don't think anything is required then */
5943 break;
5944 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005945
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005946 /* simple case of declared as #PCDATA */
5947 if ((elemDecl->content != NULL) &&
5948 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5949 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5950 if (!ret) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005951 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005952 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005953 elem->name, NULL, NULL);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005954 }
5955 break;
5956 }
Owen Taylor3473f882001-02-23 17:55:21 +00005957 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005958 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005959 while (child != NULL) {
5960 if (child->type == XML_ELEMENT_NODE) {
5961 name = child->name;
5962 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005963 xmlChar fn[50];
5964 xmlChar *fullname;
5965
5966 fullname = xmlBuildQName(child->name, child->ns->prefix,
5967 fn, 50);
5968 if (fullname == NULL)
5969 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005970 cont = elemDecl->content;
5971 while (cont != NULL) {
5972 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005973 if (xmlStrEqual(cont->name, fullname))
5974 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005975 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5976 (cont->c1 != NULL) &&
5977 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005978 if (xmlStrEqual(cont->c1->name, fullname))
5979 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005980 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5981 (cont->c1 == NULL) ||
5982 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005983 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5984 "Internal: MIXED struct corrupted\n",
5985 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005986 break;
5987 }
5988 cont = cont->c2;
5989 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005990 if ((fullname != fn) && (fullname != child->name))
5991 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005992 if (cont != NULL)
5993 goto child_ok;
5994 }
5995 cont = elemDecl->content;
5996 while (cont != NULL) {
5997 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5998 if (xmlStrEqual(cont->name, name)) break;
5999 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
6000 (cont->c1 != NULL) &&
6001 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
6002 if (xmlStrEqual(cont->c1->name, name)) break;
6003 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6004 (cont->c1 == NULL) ||
6005 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006006 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
6007 "Internal: MIXED struct corrupted\n",
6008 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006009 break;
6010 }
6011 cont = cont->c2;
6012 }
6013 if (cont == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006014 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006015 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006016 name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006017 ret = 0;
6018 }
6019 }
6020child_ok:
6021 child = child->next;
6022 }
6023 break;
6024 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006025 if ((doc->standalone == 1) && (extsubset == 1)) {
6026 /*
6027 * VC: Standalone Document Declaration
6028 * - element types with element content, if white space
6029 * occurs directly within any instance of those types.
6030 */
6031 child = elem->children;
6032 while (child != NULL) {
6033 if (child->type == XML_TEXT_NODE) {
6034 const xmlChar *content = child->content;
6035
William M. Brack76e95df2003-10-18 16:20:14 +00006036 while (IS_BLANK_CH(*content))
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006037 content++;
6038 if (*content == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006039 xmlErrValidNode(ctxt, elem,
6040 XML_DTD_STANDALONE_WHITE_SPACE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006041"standalone: %s declared in the external subset contains white spaces nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006042 elem->name, NULL, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006043 ret = 0;
6044 break;
6045 }
6046 }
6047 child =child->next;
6048 }
6049 }
Owen Taylor3473f882001-02-23 17:55:21 +00006050 child = elem->children;
6051 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00006052 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006053 if (tmp <= 0)
6054 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00006055 break;
6056 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00006057 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00006058
6059 /* [ VC: Required Attribute ] */
6060 attr = elemDecl->attributes;
6061 while (attr != NULL) {
6062 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00006063 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006064
Daniel Veillarde4301c82002-02-13 13:32:35 +00006065 if ((attr->prefix == NULL) &&
6066 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6067 xmlNsPtr ns;
6068
6069 ns = elem->nsDef;
6070 while (ns != NULL) {
6071 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006072 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006073 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006074 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006075 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6076 xmlNsPtr ns;
6077
6078 ns = elem->nsDef;
6079 while (ns != NULL) {
6080 if (xmlStrEqual(attr->name, ns->prefix))
6081 goto found;
6082 ns = ns->next;
6083 }
6084 } else {
6085 xmlAttrPtr attrib;
6086
6087 attrib = elem->properties;
6088 while (attrib != NULL) {
6089 if (xmlStrEqual(attrib->name, attr->name)) {
6090 if (attr->prefix != NULL) {
6091 xmlNsPtr nameSpace = attrib->ns;
6092
6093 if (nameSpace == NULL)
6094 nameSpace = elem->ns;
6095 /*
6096 * qualified names handling is problematic, having a
6097 * different prefix should be possible but DTDs don't
6098 * allow to define the URI instead of the prefix :-(
6099 */
6100 if (nameSpace == NULL) {
6101 if (qualified < 0)
6102 qualified = 0;
6103 } else if (!xmlStrEqual(nameSpace->prefix,
6104 attr->prefix)) {
6105 if (qualified < 1)
6106 qualified = 1;
6107 } else
6108 goto found;
6109 } else {
6110 /*
6111 * We should allow applications to define namespaces
6112 * for their application even if the DTD doesn't
6113 * carry one, otherwise, basically we would always
6114 * break.
6115 */
6116 goto found;
6117 }
6118 }
6119 attrib = attrib->next;
6120 }
Owen Taylor3473f882001-02-23 17:55:21 +00006121 }
6122 if (qualified == -1) {
6123 if (attr->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006124 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006125 "Element %s does not carry attribute %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006126 elem->name, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006127 ret = 0;
6128 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006129 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006130 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00006131 elem->name, attr->prefix,attr->name);
6132 ret = 0;
6133 }
6134 } else if (qualified == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006135 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006136 "Element %s required attribute %s:%s has no prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006137 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006138 } else if (qualified == 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006139 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006140 "Element %s required attribute %s:%s has different prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006141 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006142 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006143 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
6144 /*
6145 * Special tests checking #FIXED namespace declarations
6146 * have the right value since this is not done as an
6147 * attribute checking
6148 */
6149 if ((attr->prefix == NULL) &&
6150 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6151 xmlNsPtr ns;
6152
6153 ns = elem->nsDef;
6154 while (ns != NULL) {
6155 if (ns->prefix == NULL) {
6156 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006157 xmlErrValidNode(ctxt, elem,
6158 XML_DTD_ELEM_DEFAULT_NAMESPACE,
Daniel Veillarde4301c82002-02-13 13:32:35 +00006159 "Element %s namespace name for default namespace does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006160 elem->name, NULL, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006161 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006162 }
6163 goto found;
6164 }
6165 ns = ns->next;
6166 }
6167 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6168 xmlNsPtr ns;
6169
6170 ns = elem->nsDef;
6171 while (ns != NULL) {
6172 if (xmlStrEqual(attr->name, ns->prefix)) {
6173 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006174 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006175 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006176 elem->name, ns->prefix, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006177 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006178 }
6179 goto found;
6180 }
6181 ns = ns->next;
6182 }
6183 }
Owen Taylor3473f882001-02-23 17:55:21 +00006184 }
6185found:
6186 attr = attr->nexth;
6187 }
6188 return(ret);
6189}
6190
6191/**
6192 * xmlValidateRoot:
6193 * @ctxt: the validation context
6194 * @doc: a document instance
6195 *
6196 * Try to validate a the root element
6197 * basically it does the following check as described by the
6198 * XML-1.0 recommendation:
6199 * - [ VC: Root Element Type ]
6200 * it doesn't try to recurse or apply other check to the element
6201 *
6202 * returns 1 if valid or 0 otherwise
6203 */
6204
6205int
6206xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6207 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00006208 int ret;
6209
Owen Taylor3473f882001-02-23 17:55:21 +00006210 if (doc == NULL) return(0);
6211
6212 root = xmlDocGetRootElement(doc);
6213 if ((root == NULL) || (root->name == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006214 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
6215 "no root element\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006216 return(0);
6217 }
6218
6219 /*
6220 * When doing post validation against a separate DTD, those may
6221 * no internal subset has been generated
6222 */
6223 if ((doc->intSubset != NULL) &&
6224 (doc->intSubset->name != NULL)) {
6225 /*
6226 * Check first the document root against the NQName
6227 */
6228 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
6229 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006230 xmlChar fn[50];
6231 xmlChar *fullname;
6232
6233 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6234 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006235 xmlVErrMemory(ctxt, NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00006236 return(0);
6237 }
6238 ret = xmlStrEqual(doc->intSubset->name, fullname);
6239 if ((fullname != fn) && (fullname != root->name))
6240 xmlFree(fullname);
6241 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00006242 goto name_ok;
6243 }
6244 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6245 (xmlStrEqual(root->name, BAD_CAST "html")))
6246 goto name_ok;
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006247 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6248 "root and DTD name do not match '%s' and '%s'\n",
6249 root->name, doc->intSubset->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006250 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006251 }
6252 }
6253name_ok:
6254 return(1);
6255}
6256
6257
6258/**
6259 * xmlValidateElement:
6260 * @ctxt: the validation context
6261 * @doc: a document instance
6262 * @elem: an element instance
6263 *
6264 * Try to validate the subtree under an element
6265 *
6266 * returns 1 if valid or 0 otherwise
6267 */
6268
6269int
6270xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
6271 xmlNodePtr child;
6272 xmlAttrPtr attr;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006273 xmlNsPtr ns;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006274 const xmlChar *value;
Owen Taylor3473f882001-02-23 17:55:21 +00006275 int ret = 1;
6276
6277 if (elem == NULL) return(0);
6278
6279 /*
6280 * XInclude elements were added after parsing in the infoset,
6281 * they don't really mean anything validation wise.
6282 */
6283 if ((elem->type == XML_XINCLUDE_START) ||
6284 (elem->type == XML_XINCLUDE_END))
6285 return(1);
6286
6287 CHECK_DTD;
6288
Daniel Veillard10ea86c2001-06-20 13:55:33 +00006289 /*
6290 * Entities references have to be handled separately
6291 */
6292 if (elem->type == XML_ENTITY_REF_NODE) {
6293 return(1);
6294 }
6295
Owen Taylor3473f882001-02-23 17:55:21 +00006296 ret &= xmlValidateOneElement(ctxt, doc, elem);
6297 attr = elem->properties;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006298 while (attr != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006299 value = xmlNodeListGetString(doc, attr->children, 0);
6300 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6301 if (value != NULL)
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006302 xmlFree((char *)value);
Owen Taylor3473f882001-02-23 17:55:21 +00006303 attr= attr->next;
6304 }
Daniel Veillarde133dd82003-10-30 10:42:20 +00006305 ns = elem->nsDef;
6306 while (ns != NULL) {
Daniel Veillard1f5c9892003-12-29 17:09:55 +00006307 if (elem->ns == NULL)
6308 ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
6309 ns, ns->href);
6310 else
6311 ret &= xmlValidateOneNamespace(ctxt, doc, elem, elem->ns->prefix,
6312 ns, ns->href);
Daniel Veillarde133dd82003-10-30 10:42:20 +00006313 ns = ns->next;
6314 }
Owen Taylor3473f882001-02-23 17:55:21 +00006315 child = elem->children;
6316 while (child != NULL) {
6317 ret &= xmlValidateElement(ctxt, doc, child);
6318 child = child->next;
6319 }
6320
6321 return(ret);
6322}
6323
Daniel Veillard8730c562001-02-26 10:49:57 +00006324/**
6325 * xmlValidateRef:
6326 * @ref: A reference to be validated
6327 * @ctxt: Validation context
6328 * @name: Name of ID we are searching for
6329 *
6330 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006331static void
Daniel Veillard8730c562001-02-26 10:49:57 +00006332xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006333 const xmlChar *name) {
6334 xmlAttrPtr id;
6335 xmlAttrPtr attr;
6336
6337 if (ref == NULL)
6338 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006339 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006340 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006341 attr = ref->attr;
6342 if (attr == NULL) {
6343 xmlChar *dup, *str = NULL, *cur, save;
6344
6345 dup = xmlStrdup(name);
6346 if (dup == NULL) {
6347 ctxt->valid = 0;
6348 return;
6349 }
6350 cur = dup;
6351 while (*cur != 0) {
6352 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006353 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006354 save = *cur;
6355 *cur = 0;
6356 id = xmlGetID(ctxt->doc, str);
6357 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006358 xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006359 "attribute %s line %d references an unknown ID \"%s\"\n",
6360 ref->name, ref->lineno, str);
6361 ctxt->valid = 0;
6362 }
6363 if (save == 0)
6364 break;
6365 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006366 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006367 }
6368 xmlFree(dup);
6369 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00006370 id = xmlGetID(ctxt->doc, name);
6371 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006372 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006373 "IDREF attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006374 attr->name, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006375 ctxt->valid = 0;
6376 }
6377 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6378 xmlChar *dup, *str = NULL, *cur, save;
6379
6380 dup = xmlStrdup(name);
6381 if (dup == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006382 xmlVErrMemory(ctxt, "IDREFS split");
Owen Taylor3473f882001-02-23 17:55:21 +00006383 ctxt->valid = 0;
6384 return;
6385 }
6386 cur = dup;
6387 while (*cur != 0) {
6388 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006389 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006390 save = *cur;
6391 *cur = 0;
6392 id = xmlGetID(ctxt->doc, str);
6393 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006394 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006395 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006396 attr->name, str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006397 ctxt->valid = 0;
6398 }
6399 if (save == 0)
6400 break;
6401 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006402 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006403 }
6404 xmlFree(dup);
6405 }
6406}
6407
6408/**
Daniel Veillard8730c562001-02-26 10:49:57 +00006409 * xmlWalkValidateList:
6410 * @data: Contents of current link
6411 * @user: Value supplied by the user
6412 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006413 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00006414 */
6415static int
6416xmlWalkValidateList(const void *data, const void *user)
6417{
6418 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6419 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6420 return 1;
6421}
6422
6423/**
6424 * xmlValidateCheckRefCallback:
6425 * @ref_list: List of references
6426 * @ctxt: Validation context
6427 * @name: Name of ID we are searching for
6428 *
6429 */
6430static void
6431xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6432 const xmlChar *name) {
6433 xmlValidateMemo memo;
6434
6435 if (ref_list == NULL)
6436 return;
6437 memo.ctxt = ctxt;
6438 memo.name = name;
6439
6440 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6441
6442}
6443
6444/**
Owen Taylor3473f882001-02-23 17:55:21 +00006445 * xmlValidateDocumentFinal:
6446 * @ctxt: the validation context
6447 * @doc: a document instance
6448 *
6449 * Does the final step for the document validation once all the
6450 * incremental validation steps have been completed
6451 *
6452 * basically it does the following checks described by the XML Rec
6453 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006454 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00006455 *
6456 * returns 1 if valid or 0 otherwise
6457 */
6458
6459int
6460xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6461 xmlRefTablePtr table;
6462
Daniel Veillardc0be74b2004-11-03 19:16:55 +00006463 if (ctxt == NULL)
6464 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006465 if (doc == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006466 xmlErrValid(ctxt, XML_DTD_NO_DOC,
6467 "xmlValidateDocumentFinal: doc == NULL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006468 return(0);
6469 }
6470
6471 /*
6472 * Check all the NOTATION/NOTATIONS attributes
6473 */
6474 /*
6475 * Check all the ENTITY/ENTITIES attributes definition for validity
6476 */
6477 /*
6478 * Check all the IDREF/IDREFS attributes definition for validity
6479 */
6480 table = (xmlRefTablePtr) doc->refs;
6481 ctxt->doc = doc;
6482 ctxt->valid = 1;
6483 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6484 return(ctxt->valid);
6485}
6486
6487/**
6488 * xmlValidateDtd:
6489 * @ctxt: the validation context
6490 * @doc: a document instance
6491 * @dtd: a dtd instance
6492 *
6493 * Try to validate the document against the dtd instance
6494 *
William M. Brack367df6e2004-10-23 18:14:36 +00006495 * Basically it does check all the definitions in the DtD.
6496 * Note the the internal subset (if present) is de-coupled
6497 * (i.e. not used), which could give problems if ID or IDREF
6498 * is present.
Owen Taylor3473f882001-02-23 17:55:21 +00006499 *
6500 * returns 1 if valid or 0 otherwise
6501 */
6502
6503int
6504xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6505 int ret;
William M. Brack367df6e2004-10-23 18:14:36 +00006506 xmlDtdPtr oldExt, oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006507 xmlNodePtr root;
6508
6509 if (dtd == NULL) return(0);
6510 if (doc == NULL) return(0);
6511 oldExt = doc->extSubset;
William M. Brack367df6e2004-10-23 18:14:36 +00006512 oldInt = doc->intSubset;
Owen Taylor3473f882001-02-23 17:55:21 +00006513 doc->extSubset = dtd;
William M. Brack367df6e2004-10-23 18:14:36 +00006514 doc->intSubset = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006515 ret = xmlValidateRoot(ctxt, doc);
6516 if (ret == 0) {
6517 doc->extSubset = oldExt;
William M. Brack367df6e2004-10-23 18:14:36 +00006518 doc->intSubset = oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006519 return(ret);
6520 }
6521 if (doc->ids != NULL) {
6522 xmlFreeIDTable(doc->ids);
6523 doc->ids = NULL;
6524 }
6525 if (doc->refs != NULL) {
6526 xmlFreeRefTable(doc->refs);
6527 doc->refs = NULL;
6528 }
6529 root = xmlDocGetRootElement(doc);
6530 ret = xmlValidateElement(ctxt, doc, root);
6531 ret &= xmlValidateDocumentFinal(ctxt, doc);
6532 doc->extSubset = oldExt;
William M. Brack367df6e2004-10-23 18:14:36 +00006533 doc->intSubset = oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006534 return(ret);
6535}
6536
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006537static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006538xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6539 const xmlChar *name ATTRIBUTE_UNUSED) {
6540 if (cur == NULL)
6541 return;
6542 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6543 xmlChar *notation = cur->content;
6544
Daniel Veillard878eab02002-02-19 13:46:09 +00006545 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006546 int ret;
6547
6548 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6549 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006550 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006551 }
6552 }
6553 }
6554}
6555
6556static void
Owen Taylor3473f882001-02-23 17:55:21 +00006557xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006558 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006559 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006560 xmlDocPtr doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006561 xmlElementPtr elem = NULL;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006562
Owen Taylor3473f882001-02-23 17:55:21 +00006563 if (cur == NULL)
6564 return;
6565 switch (cur->atype) {
6566 case XML_ATTRIBUTE_CDATA:
6567 case XML_ATTRIBUTE_ID:
6568 case XML_ATTRIBUTE_IDREF :
6569 case XML_ATTRIBUTE_IDREFS:
6570 case XML_ATTRIBUTE_NMTOKEN:
6571 case XML_ATTRIBUTE_NMTOKENS:
6572 case XML_ATTRIBUTE_ENUMERATION:
6573 break;
6574 case XML_ATTRIBUTE_ENTITY:
6575 case XML_ATTRIBUTE_ENTITIES:
6576 case XML_ATTRIBUTE_NOTATION:
6577 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006578
6579 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6580 cur->atype, cur->defaultValue);
6581 if ((ret == 0) && (ctxt->valid == 1))
6582 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006583 }
6584 if (cur->tree != NULL) {
6585 xmlEnumerationPtr tree = cur->tree;
6586 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006587 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006588 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006589 if ((ret == 0) && (ctxt->valid == 1))
6590 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006591 tree = tree->next;
6592 }
6593 }
6594 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006595 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6596 doc = cur->doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006597 if (cur->elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006598 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard878eab02002-02-19 13:46:09 +00006599 "xmlValidateAttributeCallback(%s): internal error\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006600 (const char *) cur->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00006601 return;
6602 }
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006603
6604 if (doc != NULL)
6605 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6606 if ((elem == NULL) && (doc != NULL))
Daniel Veillard878eab02002-02-19 13:46:09 +00006607 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006608 if ((elem == NULL) && (cur->parent != NULL) &&
6609 (cur->parent->type == XML_DTD_NODE))
6610 elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00006611 if (elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006612 xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
Daniel Veillard878eab02002-02-19 13:46:09 +00006613 "attribute %s: could not find decl for element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006614 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006615 return;
6616 }
6617 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006618 xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006619 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006620 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006621 ctxt->valid = 0;
6622 }
6623 }
Owen Taylor3473f882001-02-23 17:55:21 +00006624}
6625
6626/**
6627 * xmlValidateDtdFinal:
6628 * @ctxt: the validation context
6629 * @doc: a document instance
6630 *
6631 * Does the final step for the dtds validation once all the
6632 * subsets have been parsed
6633 *
6634 * basically it does the following checks described by the XML Rec
6635 * - check that ENTITY and ENTITIES type attributes default or
6636 * possible values matches one of the defined entities.
6637 * - check that NOTATION type attributes default or
6638 * possible values matches one of the defined notations.
6639 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006640 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006641 */
6642
6643int
6644xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006645 xmlDtdPtr dtd;
6646 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006647 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006648
6649 if (doc == NULL) return(0);
6650 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6651 return(0);
6652 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006653 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006654 dtd = doc->intSubset;
6655 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6656 table = (xmlAttributeTablePtr) dtd->attributes;
6657 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006658 }
6659 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006660 entities = (xmlEntitiesTablePtr) dtd->entities;
6661 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6662 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006663 }
6664 dtd = doc->extSubset;
6665 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6666 table = (xmlAttributeTablePtr) dtd->attributes;
6667 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006668 }
6669 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006670 entities = (xmlEntitiesTablePtr) dtd->entities;
6671 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6672 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006673 }
6674 return(ctxt->valid);
6675}
6676
6677/**
6678 * xmlValidateDocument:
6679 * @ctxt: the validation context
6680 * @doc: a document instance
6681 *
6682 * Try to validate the document instance
6683 *
6684 * basically it does the all the checks described by the XML Rec
6685 * i.e. validates the internal and external subset (if present)
6686 * and validate the document tree.
6687 *
6688 * returns 1 if valid or 0 otherwise
6689 */
6690
6691int
6692xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6693 int ret;
6694 xmlNodePtr root;
6695
Daniel Veillardc0be74b2004-11-03 19:16:55 +00006696 if (doc == NULL)
6697 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006698 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006699 xmlErrValid(ctxt, XML_DTD_NO_DTD,
6700 "no DTD found!\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006701 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006702 }
Owen Taylor3473f882001-02-23 17:55:21 +00006703 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6704 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
William M. Brack8c22f9f2004-08-06 16:23:27 +00006705 xmlChar *sysID;
6706 if (doc->intSubset->SystemID != NULL) {
William M. Brackbebe7302004-08-05 06:46:47 +00006707 sysID = xmlBuildURI(doc->intSubset->SystemID,
6708 doc->URL);
William M. Brack8c22f9f2004-08-06 16:23:27 +00006709 if (sysID == NULL) {
6710 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6711 "Could not build URI for external subset \"%s\"\n",
6712 (const char *) doc->intSubset->SystemID);
6713 return 0;
6714 }
6715 } else
William M. Brackbebe7302004-08-05 06:46:47 +00006716 sysID = NULL;
William M. Brack8c22f9f2004-08-06 16:23:27 +00006717 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
William M. Brackbebe7302004-08-05 06:46:47 +00006718 (const xmlChar *)sysID);
William M. Brackbebe7302004-08-05 06:46:47 +00006719 if (sysID != NULL)
6720 xmlFree(sysID);
Owen Taylor3473f882001-02-23 17:55:21 +00006721 if (doc->extSubset == NULL) {
6722 if (doc->intSubset->SystemID != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006723 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006724 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006725 (const char *) doc->intSubset->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00006726 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006727 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006728 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006729 (const char *) doc->intSubset->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00006730 }
6731 return(0);
6732 }
6733 }
6734
6735 if (doc->ids != NULL) {
6736 xmlFreeIDTable(doc->ids);
6737 doc->ids = NULL;
6738 }
6739 if (doc->refs != NULL) {
6740 xmlFreeRefTable(doc->refs);
6741 doc->refs = NULL;
6742 }
6743 ret = xmlValidateDtdFinal(ctxt, doc);
6744 if (!xmlValidateRoot(ctxt, doc)) return(0);
6745
6746 root = xmlDocGetRootElement(doc);
6747 ret &= xmlValidateElement(ctxt, doc, root);
6748 ret &= xmlValidateDocumentFinal(ctxt, doc);
6749 return(ret);
6750}
6751
Owen Taylor3473f882001-02-23 17:55:21 +00006752/************************************************************************
6753 * *
6754 * Routines for dynamic validation editing *
6755 * *
6756 ************************************************************************/
6757
6758/**
6759 * xmlValidGetPotentialChildren:
6760 * @ctree: an element content tree
6761 * @list: an array to store the list of child names
6762 * @len: a pointer to the number of element in the list
6763 * @max: the size of the array
6764 *
6765 * Build/extend a list of potential children allowed by the content tree
6766 *
6767 * returns the number of element in the list, or -1 in case of error.
6768 */
6769
6770int
6771xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6772 int *len, int max) {
6773 int i;
6774
6775 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6776 return(-1);
6777 if (*len >= max) return(*len);
6778
6779 switch (ctree->type) {
6780 case XML_ELEMENT_CONTENT_PCDATA:
6781 for (i = 0; i < *len;i++)
6782 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6783 list[(*len)++] = BAD_CAST "#PCDATA";
6784 break;
6785 case XML_ELEMENT_CONTENT_ELEMENT:
6786 for (i = 0; i < *len;i++)
6787 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6788 list[(*len)++] = ctree->name;
6789 break;
6790 case XML_ELEMENT_CONTENT_SEQ:
6791 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6792 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6793 break;
6794 case XML_ELEMENT_CONTENT_OR:
6795 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6796 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6797 break;
6798 }
6799
6800 return(*len);
6801}
6802
William M. Brack9333cc22004-06-24 08:33:40 +00006803/*
6804 * Dummy function to suppress messages while we try out valid elements
6805 */
6806static void xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
6807 const char *msg ATTRIBUTE_UNUSED, ...) {
6808 return;
6809}
6810
Owen Taylor3473f882001-02-23 17:55:21 +00006811/**
6812 * xmlValidGetValidElements:
6813 * @prev: an element to insert after
6814 * @next: an element to insert next
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006815 * @names: an array to store the list of child names
Owen Taylor3473f882001-02-23 17:55:21 +00006816 * @max: the size of the array
6817 *
6818 * This function returns the list of authorized children to insert
6819 * within an existing tree while respecting the validity constraints
6820 * forced by the Dtd. The insertion point is defined using @prev and
6821 * @next in the following ways:
6822 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6823 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6824 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6825 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6826 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6827 *
6828 * pointers to the element names are inserted at the beginning of the array
6829 * and do not need to be freed.
6830 *
6831 * returns the number of element in the list, or -1 in case of error. If
6832 * the function returns the value @max the caller is invited to grow the
6833 * receiving array and retry.
6834 */
6835
6836int
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006837xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
Owen Taylor3473f882001-02-23 17:55:21 +00006838 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006839 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006840 int nb_valid_elements = 0;
6841 const xmlChar *elements[256];
6842 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006843 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006844
6845 xmlNode *ref_node;
6846 xmlNode *parent;
6847 xmlNode *test_node;
6848
6849 xmlNode *prev_next;
6850 xmlNode *next_prev;
6851 xmlNode *parent_childs;
6852 xmlNode *parent_last;
6853
6854 xmlElement *element_desc;
6855
6856 if (prev == NULL && next == NULL)
6857 return(-1);
6858
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006859 if (names == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006860 if (max <= 0) return(-1);
6861
William M. Brack9333cc22004-06-24 08:33:40 +00006862 memset(&vctxt, 0, sizeof (xmlValidCtxt));
6863 vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
6864
Owen Taylor3473f882001-02-23 17:55:21 +00006865 nb_valid_elements = 0;
6866 ref_node = prev ? prev : next;
6867 parent = ref_node->parent;
6868
6869 /*
6870 * Retrieves the parent element declaration
6871 */
6872 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6873 parent->name);
6874 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6875 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6876 parent->name);
6877 if (element_desc == NULL) return(-1);
6878
6879 /*
6880 * Do a backup of the current tree structure
6881 */
6882 prev_next = prev ? prev->next : NULL;
6883 next_prev = next ? next->prev : NULL;
6884 parent_childs = parent->children;
6885 parent_last = parent->last;
6886
6887 /*
6888 * Creates a dummy node and insert it into the tree
6889 */
Daniel Veillard95ddcd32004-10-26 21:53:55 +00006890 test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006891 test_node->parent = parent;
6892 test_node->prev = prev;
6893 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006894 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006895
6896 if (prev) prev->next = test_node;
6897 else parent->children = test_node;
6898
6899 if (next) next->prev = test_node;
6900 else parent->last = test_node;
6901
6902 /*
6903 * Insert each potential child node and check if the parent is
6904 * still valid
6905 */
6906 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6907 elements, &nb_elements, 256);
6908
6909 for (i = 0;i < nb_elements;i++) {
6910 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006911 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006912 int j;
6913
6914 for (j = 0; j < nb_valid_elements;j++)
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006915 if (xmlStrEqual(elements[i], names[j])) break;
6916 names[nb_valid_elements++] = elements[i];
Owen Taylor3473f882001-02-23 17:55:21 +00006917 if (nb_valid_elements >= max) break;
6918 }
6919 }
6920
6921 /*
6922 * Restore the tree structure
6923 */
6924 if (prev) prev->next = prev_next;
6925 if (next) next->prev = next_prev;
6926 parent->children = parent_childs;
6927 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006928
6929 /*
6930 * Free up the dummy node
6931 */
6932 test_node->name = name;
6933 xmlFreeNode(test_node);
6934
Owen Taylor3473f882001-02-23 17:55:21 +00006935 return(nb_valid_elements);
6936}
Daniel Veillard4432df22003-09-28 18:58:27 +00006937#endif /* LIBXML_VALID_ENABLED */
6938
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006939#define bottom_valid
6940#include "elfgcchack.h"