blob: ed89c6caa5f7e48dfbc0891a46c46bd0b2fdd9db [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;
William M. Brackf810de02005-07-06 22:48:41 +00002066 /*
2067 * doc must be set before possible error causes call
2068 * to xmlFreeAttribute (because it's used to check on
2069 * dict use)
2070 */
2071 ret->doc = dtd->doc;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002072 if (dict) {
2073 ret->name = xmlDictLookup(dict, name, -1);
2074 ret->prefix = xmlDictLookup(dict, ns, -1);
2075 ret->elem = xmlDictLookup(dict, elem, -1);
2076 } else {
2077 ret->name = xmlStrdup(name);
2078 ret->prefix = xmlStrdup(ns);
2079 ret->elem = xmlStrdup(elem);
2080 }
Owen Taylor3473f882001-02-23 17:55:21 +00002081 ret->def = def;
2082 ret->tree = tree;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002083 if (defaultValue != NULL) {
2084 if (dict)
2085 ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
2086 else
2087 ret->defaultValue = xmlStrdup(defaultValue);
2088 }
Owen Taylor3473f882001-02-23 17:55:21 +00002089
2090 /*
2091 * Validity Check:
2092 * Search the DTD for previous declarations of the ATTLIST
2093 */
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002094 if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002095#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002096 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002097 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002098 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002099 xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00002100 "Attribute %s of element %s: already defined\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002101 name, elem, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00002102#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002103 xmlFreeAttribute(ret);
2104 return(NULL);
2105 }
2106
2107 /*
2108 * Validity Check:
2109 * Multiple ID per element
2110 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00002111 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00002112 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00002113
Daniel Veillard4432df22003-09-28 18:58:27 +00002114#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002115 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillarddbee0f12005-06-27 13:42:57 +00002116 (xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002117 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
Owen Taylor3473f882001-02-23 17:55:21 +00002118 "Element %s has too may ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002119 elem, name, NULL);
Daniel Veillard42595322004-11-08 10:52:06 +00002120 if (ctxt != NULL)
2121 ctxt->valid = 0;
Daniel Veillardc7612992002-02-17 22:47:37 +00002122 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002123#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardc7612992002-02-17 22:47:37 +00002124
Daniel Veillard48da9102001-08-07 01:10:10 +00002125 /*
2126 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002127 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00002128 */
2129 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
2130 ((ret->prefix != NULL &&
2131 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
2132 ret->nexth = elemDef->attributes;
2133 elemDef->attributes = ret;
2134 } else {
2135 xmlAttributePtr tmp = elemDef->attributes;
2136
2137 while ((tmp != NULL) &&
2138 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
2139 ((ret->prefix != NULL &&
2140 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
2141 if (tmp->nexth == NULL)
2142 break;
2143 tmp = tmp->nexth;
2144 }
2145 if (tmp != NULL) {
2146 ret->nexth = tmp->nexth;
2147 tmp->nexth = ret;
2148 } else {
2149 ret->nexth = elemDef->attributes;
2150 elemDef->attributes = ret;
2151 }
2152 }
Owen Taylor3473f882001-02-23 17:55:21 +00002153 }
2154
2155 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002156 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00002157 */
2158 ret->parent = dtd;
Owen Taylor3473f882001-02-23 17:55:21 +00002159 if (dtd->last == NULL) {
2160 dtd->children = dtd->last = (xmlNodePtr) ret;
2161 } else {
2162 dtd->last->next = (xmlNodePtr) ret;
2163 ret->prev = dtd->last;
2164 dtd->last = (xmlNodePtr) ret;
2165 }
2166 return(ret);
2167}
2168
2169/**
2170 * xmlFreeAttributeTable:
2171 * @table: An attribute table
2172 *
2173 * Deallocate the memory used by an entities hash table.
2174 */
2175void
2176xmlFreeAttributeTable(xmlAttributeTablePtr table) {
2177 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
2178}
2179
Daniel Veillard652327a2003-09-29 18:02:38 +00002180#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002181/**
2182 * xmlCopyAttribute:
2183 * @attr: An attribute
2184 *
2185 * Build a copy of an attribute.
2186 *
2187 * Returns the new xmlAttributePtr or NULL in case of error.
2188 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002189static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002190xmlCopyAttribute(xmlAttributePtr attr) {
2191 xmlAttributePtr cur;
2192
2193 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2194 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002195 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002196 return(NULL);
2197 }
2198 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00002199 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00002200 cur->atype = attr->atype;
2201 cur->def = attr->def;
2202 cur->tree = xmlCopyEnumeration(attr->tree);
2203 if (attr->elem != NULL)
2204 cur->elem = xmlStrdup(attr->elem);
2205 if (attr->name != NULL)
2206 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00002207 if (attr->prefix != NULL)
2208 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002209 if (attr->defaultValue != NULL)
2210 cur->defaultValue = xmlStrdup(attr->defaultValue);
2211 return(cur);
2212}
2213
2214/**
2215 * xmlCopyAttributeTable:
2216 * @table: An attribute table
2217 *
2218 * Build a copy of an attribute table.
2219 *
2220 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2221 */
2222xmlAttributeTablePtr
2223xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2224 return((xmlAttributeTablePtr) xmlHashCopy(table,
2225 (xmlHashCopier) xmlCopyAttribute));
2226}
Daniel Veillard652327a2003-09-29 18:02:38 +00002227#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002228
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002229#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002230/**
2231 * xmlDumpAttributeDecl:
2232 * @buf: the XML buffer output
2233 * @attr: An attribute declaration
2234 *
2235 * This will dump the content of the attribute declaration as an XML
2236 * DTD definition
2237 */
2238void
2239xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002240 if ((buf == NULL) || (attr == NULL))
2241 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002242 xmlBufferWriteChar(buf, "<!ATTLIST ");
2243 xmlBufferWriteCHAR(buf, attr->elem);
2244 xmlBufferWriteChar(buf, " ");
2245 if (attr->prefix != NULL) {
2246 xmlBufferWriteCHAR(buf, attr->prefix);
2247 xmlBufferWriteChar(buf, ":");
2248 }
2249 xmlBufferWriteCHAR(buf, attr->name);
2250 switch (attr->atype) {
2251 case XML_ATTRIBUTE_CDATA:
2252 xmlBufferWriteChar(buf, " CDATA");
2253 break;
2254 case XML_ATTRIBUTE_ID:
2255 xmlBufferWriteChar(buf, " ID");
2256 break;
2257 case XML_ATTRIBUTE_IDREF:
2258 xmlBufferWriteChar(buf, " IDREF");
2259 break;
2260 case XML_ATTRIBUTE_IDREFS:
2261 xmlBufferWriteChar(buf, " IDREFS");
2262 break;
2263 case XML_ATTRIBUTE_ENTITY:
2264 xmlBufferWriteChar(buf, " ENTITY");
2265 break;
2266 case XML_ATTRIBUTE_ENTITIES:
2267 xmlBufferWriteChar(buf, " ENTITIES");
2268 break;
2269 case XML_ATTRIBUTE_NMTOKEN:
2270 xmlBufferWriteChar(buf, " NMTOKEN");
2271 break;
2272 case XML_ATTRIBUTE_NMTOKENS:
2273 xmlBufferWriteChar(buf, " NMTOKENS");
2274 break;
2275 case XML_ATTRIBUTE_ENUMERATION:
2276 xmlBufferWriteChar(buf, " (");
2277 xmlDumpEnumeration(buf, attr->tree);
2278 break;
2279 case XML_ATTRIBUTE_NOTATION:
2280 xmlBufferWriteChar(buf, " NOTATION (");
2281 xmlDumpEnumeration(buf, attr->tree);
2282 break;
2283 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002284 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2285 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2286 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002287 }
2288 switch (attr->def) {
2289 case XML_ATTRIBUTE_NONE:
2290 break;
2291 case XML_ATTRIBUTE_REQUIRED:
2292 xmlBufferWriteChar(buf, " #REQUIRED");
2293 break;
2294 case XML_ATTRIBUTE_IMPLIED:
2295 xmlBufferWriteChar(buf, " #IMPLIED");
2296 break;
2297 case XML_ATTRIBUTE_FIXED:
2298 xmlBufferWriteChar(buf, " #FIXED");
2299 break;
2300 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002301 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2302 "Internal: ATTRIBUTE struct corrupted invalid def\n",
2303 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002304 }
2305 if (attr->defaultValue != NULL) {
2306 xmlBufferWriteChar(buf, " ");
2307 xmlBufferWriteQuotedString(buf, attr->defaultValue);
2308 }
2309 xmlBufferWriteChar(buf, ">\n");
2310}
2311
2312/**
William M. Brack9e660592003-10-20 14:56:06 +00002313 * xmlDumpAttributeDeclScan:
2314 * @attr: An attribute declaration
2315 * @buf: the XML buffer output
2316 *
2317 * This is used with the hash scan function - just reverses arguments
2318 */
2319static void
2320xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) {
2321 xmlDumpAttributeDecl(buf, attr);
2322}
2323
2324/**
Owen Taylor3473f882001-02-23 17:55:21 +00002325 * xmlDumpAttributeTable:
2326 * @buf: the XML buffer output
2327 * @table: An attribute table
2328 *
2329 * This will dump the content of the attribute table as an XML DTD definition
2330 */
2331void
2332xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002333 if ((buf == NULL) || (table == NULL))
2334 return;
William M. Brack9e660592003-10-20 14:56:06 +00002335 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002336}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002337#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002338
2339/************************************************************************
2340 * *
2341 * NOTATIONs *
2342 * *
2343 ************************************************************************/
2344/**
Owen Taylor3473f882001-02-23 17:55:21 +00002345 * xmlFreeNotation:
2346 * @not: A notation
2347 *
2348 * Deallocate the memory used by an notation definition
2349 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002350static void
Owen Taylor3473f882001-02-23 17:55:21 +00002351xmlFreeNotation(xmlNotationPtr nota) {
2352 if (nota == NULL) return;
2353 if (nota->name != NULL)
2354 xmlFree((xmlChar *) nota->name);
2355 if (nota->PublicID != NULL)
2356 xmlFree((xmlChar *) nota->PublicID);
2357 if (nota->SystemID != NULL)
2358 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002359 xmlFree(nota);
2360}
2361
2362
2363/**
2364 * xmlAddNotationDecl:
2365 * @dtd: pointer to the DTD
2366 * @ctxt: the validation context
2367 * @name: the entity name
2368 * @PublicID: the public identifier or NULL
2369 * @SystemID: the system identifier or NULL
2370 *
2371 * Register a new notation declaration
2372 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002373 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002374 */
2375xmlNotationPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002376xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002377 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002378 const xmlChar *PublicID, const xmlChar *SystemID) {
2379 xmlNotationPtr ret;
2380 xmlNotationTablePtr table;
2381
2382 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002383 return(NULL);
2384 }
2385 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002386 return(NULL);
2387 }
2388 if ((PublicID == NULL) && (SystemID == NULL)) {
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002389 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002390 }
2391
2392 /*
2393 * Create the Notation table if needed.
2394 */
2395 table = (xmlNotationTablePtr) dtd->notations;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002396 if (table == NULL) {
2397 xmlDictPtr dict = NULL;
2398 if (dtd->doc != NULL)
2399 dict = dtd->doc->dict;
2400
2401 dtd->notations = table = xmlHashCreateDict(0, dict);
2402 }
Owen Taylor3473f882001-02-23 17:55:21 +00002403 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002404 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002405 "xmlAddNotationDecl: Table creation failed!\n");
2406 return(NULL);
2407 }
2408
2409 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2410 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002411 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002412 return(NULL);
2413 }
2414 memset(ret, 0, sizeof(xmlNotation));
2415
2416 /*
2417 * fill the structure.
2418 */
2419 ret->name = xmlStrdup(name);
2420 if (SystemID != NULL)
2421 ret->SystemID = xmlStrdup(SystemID);
2422 if (PublicID != NULL)
2423 ret->PublicID = xmlStrdup(PublicID);
2424
2425 /*
2426 * Validity Check:
2427 * Check the DTD for previous declarations of the ATTLIST
2428 */
2429 if (xmlHashAddEntry(table, name, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002430#ifdef LIBXML_VALID_ENABLED
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002431 xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
2432 "xmlAddNotationDecl: %s already defined\n",
2433 (const char *) name);
Daniel Veillard4432df22003-09-28 18:58:27 +00002434#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002435 xmlFreeNotation(ret);
2436 return(NULL);
2437 }
2438 return(ret);
2439}
2440
2441/**
2442 * xmlFreeNotationTable:
2443 * @table: An notation table
2444 *
2445 * Deallocate the memory used by an entities hash table.
2446 */
2447void
2448xmlFreeNotationTable(xmlNotationTablePtr table) {
2449 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2450}
2451
Daniel Veillard652327a2003-09-29 18:02:38 +00002452#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002453/**
2454 * xmlCopyNotation:
2455 * @nota: A notation
2456 *
2457 * Build a copy of a notation.
2458 *
2459 * Returns the new xmlNotationPtr or NULL in case of error.
2460 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002461static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002462xmlCopyNotation(xmlNotationPtr nota) {
2463 xmlNotationPtr cur;
2464
2465 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2466 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002467 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002468 return(NULL);
2469 }
2470 if (nota->name != NULL)
2471 cur->name = xmlStrdup(nota->name);
2472 else
2473 cur->name = NULL;
2474 if (nota->PublicID != NULL)
2475 cur->PublicID = xmlStrdup(nota->PublicID);
2476 else
2477 cur->PublicID = NULL;
2478 if (nota->SystemID != NULL)
2479 cur->SystemID = xmlStrdup(nota->SystemID);
2480 else
2481 cur->SystemID = NULL;
2482 return(cur);
2483}
2484
2485/**
2486 * xmlCopyNotationTable:
2487 * @table: A notation table
2488 *
2489 * Build a copy of a notation table.
2490 *
2491 * Returns the new xmlNotationTablePtr or NULL in case of error.
2492 */
2493xmlNotationTablePtr
2494xmlCopyNotationTable(xmlNotationTablePtr table) {
2495 return((xmlNotationTablePtr) xmlHashCopy(table,
2496 (xmlHashCopier) xmlCopyNotation));
2497}
Daniel Veillard652327a2003-09-29 18:02:38 +00002498#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002499
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002500#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002501/**
2502 * xmlDumpNotationDecl:
2503 * @buf: the XML buffer output
2504 * @nota: A notation declaration
2505 *
2506 * This will dump the content the notation declaration as an XML DTD definition
2507 */
2508void
2509xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002510 if ((buf == NULL) || (nota == NULL))
2511 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002512 xmlBufferWriteChar(buf, "<!NOTATION ");
2513 xmlBufferWriteCHAR(buf, nota->name);
2514 if (nota->PublicID != NULL) {
2515 xmlBufferWriteChar(buf, " PUBLIC ");
2516 xmlBufferWriteQuotedString(buf, nota->PublicID);
2517 if (nota->SystemID != NULL) {
2518 xmlBufferWriteChar(buf, " ");
Daniel Veillard41c4a752004-09-08 20:55:38 +00002519 xmlBufferWriteQuotedString(buf, nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002520 }
2521 } else {
2522 xmlBufferWriteChar(buf, " SYSTEM ");
Daniel Veillard41c4a752004-09-08 20:55:38 +00002523 xmlBufferWriteQuotedString(buf, nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002524 }
2525 xmlBufferWriteChar(buf, " >\n");
2526}
2527
2528/**
William M. Brack9e660592003-10-20 14:56:06 +00002529 * xmlDumpNotationDeclScan:
2530 * @nota: A notation declaration
2531 * @buf: the XML buffer output
2532 *
2533 * This is called with the hash scan function, and just reverses args
2534 */
2535static void
2536xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) {
2537 xmlDumpNotationDecl(buf, nota);
2538}
2539
2540/**
Owen Taylor3473f882001-02-23 17:55:21 +00002541 * xmlDumpNotationTable:
2542 * @buf: the XML buffer output
2543 * @table: A notation table
2544 *
2545 * This will dump the content of the notation table as an XML DTD definition
2546 */
2547void
2548xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002549 if ((buf == NULL) || (table == NULL))
2550 return;
William M. Brack9e660592003-10-20 14:56:06 +00002551 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002552}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002553#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002554
2555/************************************************************************
2556 * *
2557 * IDs *
2558 * *
2559 ************************************************************************/
2560/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002561 * DICT_FREE:
2562 * @str: a string
2563 *
2564 * Free a string if it is not owned by the "dict" dictionnary in the
2565 * current scope
2566 */
2567#define DICT_FREE(str) \
2568 if ((str) && ((!dict) || \
2569 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
2570 xmlFree((char *)(str));
2571
2572/**
Owen Taylor3473f882001-02-23 17:55:21 +00002573 * xmlFreeID:
2574 * @not: A id
2575 *
2576 * Deallocate the memory used by an id definition
2577 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002578static void
Owen Taylor3473f882001-02-23 17:55:21 +00002579xmlFreeID(xmlIDPtr id) {
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002580 xmlDictPtr dict = NULL;
2581
Owen Taylor3473f882001-02-23 17:55:21 +00002582 if (id == NULL) return;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002583
2584 if (id->doc != NULL)
2585 dict = id->doc->dict;
2586
Owen Taylor3473f882001-02-23 17:55:21 +00002587 if (id->value != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002588 DICT_FREE(id->value)
Daniel Veillardea7751d2002-12-20 00:16:24 +00002589 if (id->name != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002590 DICT_FREE(id->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002591 xmlFree(id);
2592}
2593
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002594
Owen Taylor3473f882001-02-23 17:55:21 +00002595/**
2596 * xmlAddID:
2597 * @ctxt: the validation context
2598 * @doc: pointer to the document
2599 * @value: the value name
2600 * @attr: the attribute holding the ID
2601 *
2602 * Register a new id declaration
2603 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002604 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002605 */
2606xmlIDPtr
2607xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2608 xmlAttrPtr attr) {
2609 xmlIDPtr ret;
2610 xmlIDTablePtr table;
2611
2612 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002613 return(NULL);
2614 }
2615 if (value == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002616 return(NULL);
2617 }
2618 if (attr == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002619 return(NULL);
2620 }
2621
2622 /*
2623 * Create the ID table if needed.
2624 */
2625 table = (xmlIDTablePtr) doc->ids;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002626 if (table == NULL) {
2627 doc->ids = table = xmlHashCreateDict(0, doc->dict);
2628 }
Owen Taylor3473f882001-02-23 17:55:21 +00002629 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002630 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002631 "xmlAddID: Table creation failed!\n");
2632 return(NULL);
2633 }
2634
2635 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2636 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002637 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002638 return(NULL);
2639 }
2640
2641 /*
2642 * fill the structure.
2643 */
2644 ret->value = xmlStrdup(value);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002645 ret->doc = doc;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002646 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2647 /*
2648 * Operating in streaming mode, attr is gonna disapear
2649 */
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002650 if (doc->dict != NULL)
2651 ret->name = xmlDictLookup(doc->dict, attr->name, -1);
2652 else
2653 ret->name = xmlStrdup(attr->name);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002654 ret->attr = NULL;
2655 } else {
2656 ret->attr = attr;
2657 ret->name = NULL;
2658 }
2659 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002660
2661 if (xmlHashAddEntry(table, value, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002662#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002663 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002664 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002665 */
Daniel Veillardd3669b22004-02-25 12:34:55 +00002666 if ((ctxt != NULL) && (ctxt->error != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002667 xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2668 "ID %s already defined\n",
2669 value, NULL, NULL);
Daniel Veillard76575762002-09-05 14:21:15 +00002670 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002671#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002672 xmlFreeID(ret);
2673 return(NULL);
2674 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002675 if (attr != NULL)
2676 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002677 return(ret);
2678}
2679
2680/**
2681 * xmlFreeIDTable:
2682 * @table: An id table
2683 *
2684 * Deallocate the memory used by an ID hash table.
2685 */
2686void
2687xmlFreeIDTable(xmlIDTablePtr table) {
2688 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2689}
2690
2691/**
2692 * xmlIsID:
2693 * @doc: the document
2694 * @elem: the element carrying the attribute
2695 * @attr: the attribute
2696 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002697 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002698 * then this is done if DTD loading has been requested. In the case
2699 * of HTML documents parsed with the HTML parser, then ID detection is
2700 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002701 *
2702 * Returns 0 or 1 depending on the lookup result
2703 */
2704int
2705xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2706 if (doc == NULL) return(0);
2707 if (attr == NULL) return(0);
2708 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2709 return(0);
2710 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillard9ba8e382003-10-28 21:31:45 +00002711 if (((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2712 (xmlStrEqual(BAD_CAST "name", attr->name))) &&
2713 ((elem != NULL) && (!xmlStrEqual(elem->name, BAD_CAST "input"))))
Owen Taylor3473f882001-02-23 17:55:21 +00002714 return(1);
2715 return(0);
2716 } else {
2717 xmlAttributePtr attrDecl;
2718
2719 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002720 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00002721 xmlChar fn[50];
Daniel Veillard37f961d2002-07-06 17:53:56 +00002722 xmlChar *fullname;
Daniel Veillardc00cda82003-04-07 10:22:39 +00002723
2724 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002725 if (fullname == NULL)
2726 return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002727 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2728 attr->name);
2729 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2730 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2731 attr->name);
Daniel Veillardc00cda82003-04-07 10:22:39 +00002732 if ((fullname != fn) && (fullname != elem->name))
2733 xmlFree(fullname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002734 } else {
2735 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2736 attr->name);
2737 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2738 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2739 attr->name);
2740 }
Owen Taylor3473f882001-02-23 17:55:21 +00002741
2742 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2743 return(1);
2744 }
2745 return(0);
2746}
2747
2748/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002749 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002750 * @doc: the document
2751 * @attr: the attribute
2752 *
2753 * Remove the given attribute from the ID table maintained internally.
2754 *
2755 * Returns -1 if the lookup failed and 0 otherwise
2756 */
2757int
2758xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002759 xmlIDTablePtr table;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002760 xmlIDPtr id;
Owen Taylor3473f882001-02-23 17:55:21 +00002761 xmlChar *ID;
2762
2763 if (doc == NULL) return(-1);
2764 if (attr == NULL) return(-1);
2765 table = (xmlIDTablePtr) doc->ids;
2766 if (table == NULL)
2767 return(-1);
2768
2769 if (attr == NULL)
2770 return(-1);
2771 ID = xmlNodeListGetString(doc, attr->children, 1);
2772 if (ID == NULL)
2773 return(-1);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002774 id = xmlHashLookup(table, ID);
2775 if (id == NULL || id->attr != attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002776 xmlFree(ID);
2777 return(-1);
2778 }
Daniel Veillard91b955c2004-12-10 10:26:42 +00002779 xmlHashRemoveEntry(table, ID, (xmlHashDeallocator) xmlFreeID);
Owen Taylor3473f882001-02-23 17:55:21 +00002780 xmlFree(ID);
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002781 attr->atype = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002782 return(0);
2783}
2784
2785/**
2786 * xmlGetID:
2787 * @doc: pointer to the document
2788 * @ID: the ID value
2789 *
2790 * Search the attribute declaring the given ID
2791 *
2792 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2793 */
2794xmlAttrPtr
2795xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2796 xmlIDTablePtr table;
2797 xmlIDPtr id;
2798
2799 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002800 return(NULL);
2801 }
2802
2803 if (ID == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002804 return(NULL);
2805 }
2806
2807 table = (xmlIDTablePtr) doc->ids;
2808 if (table == NULL)
2809 return(NULL);
2810
2811 id = xmlHashLookup(table, ID);
2812 if (id == NULL)
2813 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002814 if (id->attr == NULL) {
2815 /*
2816 * We are operating on a stream, return a well known reference
2817 * since the attribute node doesn't exist anymore
2818 */
2819 return((xmlAttrPtr) doc);
2820 }
Owen Taylor3473f882001-02-23 17:55:21 +00002821 return(id->attr);
2822}
2823
2824/************************************************************************
2825 * *
2826 * Refs *
2827 * *
2828 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002829typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002830{
2831 xmlListPtr l;
2832 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002833} xmlRemoveMemo;
2834
2835typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2836
2837typedef struct xmlValidateMemo_t
2838{
2839 xmlValidCtxtPtr ctxt;
2840 const xmlChar *name;
2841} xmlValidateMemo;
2842
2843typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002844
2845/**
Owen Taylor3473f882001-02-23 17:55:21 +00002846 * xmlFreeRef:
2847 * @lk: A list link
2848 *
2849 * Deallocate the memory used by a ref definition
2850 */
2851static void
2852xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002853 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2854 if (ref == NULL) return;
2855 if (ref->value != NULL)
2856 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002857 if (ref->name != NULL)
2858 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002859 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002860}
2861
2862/**
2863 * xmlFreeRefList:
2864 * @list_ref: A list of references.
2865 *
2866 * Deallocate the memory used by a list of references
2867 */
2868static void
2869xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002870 if (list_ref == NULL) return;
2871 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002872}
2873
2874/**
2875 * xmlWalkRemoveRef:
2876 * @data: Contents of current link
2877 * @user: Value supplied by the user
2878 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002879 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002880 */
2881static int
2882xmlWalkRemoveRef(const void *data, const void *user)
2883{
Daniel Veillard37721922001-05-04 15:21:12 +00002884 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2885 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2886 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002887
Daniel Veillard37721922001-05-04 15:21:12 +00002888 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2889 xmlListRemoveFirst(ref_list, (void *)data);
2890 return 0;
2891 }
2892 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002893}
2894
2895/**
Daniel Veillard770075b2004-02-25 10:44:30 +00002896 * xmlDummyCompare
2897 * @data0: Value supplied by the user
2898 * @data1: Value supplied by the user
2899 *
2900 * Do nothing, return 0. Used to create unordered lists.
2901 */
2902static int
Daniel Veillardf54cd532004-02-25 11:52:31 +00002903xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2904 const void *data1 ATTRIBUTE_UNUSED)
Daniel Veillard770075b2004-02-25 10:44:30 +00002905{
2906 return (0);
2907}
2908
2909/**
Owen Taylor3473f882001-02-23 17:55:21 +00002910 * xmlAddRef:
2911 * @ctxt: the validation context
2912 * @doc: pointer to the document
2913 * @value: the value name
2914 * @attr: the attribute holding the Ref
2915 *
2916 * Register a new ref declaration
2917 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002918 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002919 */
2920xmlRefPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002921xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002922 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002923 xmlRefPtr ret;
2924 xmlRefTablePtr table;
2925 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002926
Daniel Veillard37721922001-05-04 15:21:12 +00002927 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002928 return(NULL);
2929 }
2930 if (value == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002931 return(NULL);
2932 }
2933 if (attr == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002934 return(NULL);
2935 }
Owen Taylor3473f882001-02-23 17:55:21 +00002936
Daniel Veillard37721922001-05-04 15:21:12 +00002937 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002938 * Create the Ref table if needed.
2939 */
Daniel Veillard37721922001-05-04 15:21:12 +00002940 table = (xmlRefTablePtr) doc->refs;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002941 if (table == NULL) {
2942 doc->refs = table = xmlHashCreateDict(0, doc->dict);
2943 }
Daniel Veillard37721922001-05-04 15:21:12 +00002944 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002945 xmlVErrMemory(ctxt,
Daniel Veillard37721922001-05-04 15:21:12 +00002946 "xmlAddRef: Table creation failed!\n");
2947 return(NULL);
2948 }
Owen Taylor3473f882001-02-23 17:55:21 +00002949
Daniel Veillard37721922001-05-04 15:21:12 +00002950 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2951 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002952 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard37721922001-05-04 15:21:12 +00002953 return(NULL);
2954 }
Owen Taylor3473f882001-02-23 17:55:21 +00002955
Daniel Veillard37721922001-05-04 15:21:12 +00002956 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002957 * fill the structure.
2958 */
Daniel Veillard37721922001-05-04 15:21:12 +00002959 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002960 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2961 /*
2962 * Operating in streaming mode, attr is gonna disapear
2963 */
2964 ret->name = xmlStrdup(attr->name);
2965 ret->attr = NULL;
2966 } else {
2967 ret->name = NULL;
2968 ret->attr = attr;
2969 }
2970 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002971
Daniel Veillard37721922001-05-04 15:21:12 +00002972 /* To add a reference :-
2973 * References are maintained as a list of references,
2974 * Lookup the entry, if no entry create new nodelist
2975 * Add the owning node to the NodeList
2976 * Return the ref
2977 */
Owen Taylor3473f882001-02-23 17:55:21 +00002978
Daniel Veillard37721922001-05-04 15:21:12 +00002979 if (NULL == (ref_list = xmlHashLookup(table, value))) {
Daniel Veillard770075b2004-02-25 10:44:30 +00002980 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002981 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2982 "xmlAddRef: Reference list creation failed!\n",
2983 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002984 return(NULL);
2985 }
2986 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2987 xmlListDelete(ref_list);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002988 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2989 "xmlAddRef: Reference list insertion failed!\n",
2990 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002991 return(NULL);
2992 }
2993 }
Daniel Veillard965983a2004-02-17 16:30:24 +00002994/* xmlListInsert(ref_list, ret); */
2995 xmlListAppend(ref_list, ret);
Daniel Veillard37721922001-05-04 15:21:12 +00002996 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002997}
2998
2999/**
3000 * xmlFreeRefTable:
3001 * @table: An ref table
3002 *
3003 * Deallocate the memory used by an Ref hash table.
3004 */
3005void
3006xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00003007 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00003008}
3009
3010/**
3011 * xmlIsRef:
3012 * @doc: the document
3013 * @elem: the element carrying the attribute
3014 * @attr: the attribute
3015 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003016 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00003017 * then this is simple, otherwise we use an heuristic: name Ref (upper
3018 * or lowercase).
3019 *
3020 * Returns 0 or 1 depending on the lookup result
3021 */
3022int
3023xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003024 if (attr == NULL)
3025 return(0);
3026 if (doc == NULL) {
3027 doc = attr->doc;
3028 if (doc == NULL) return(0);
3029 }
3030
Daniel Veillard37721922001-05-04 15:21:12 +00003031 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
3032 return(0);
3033 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
3034 /* TODO @@@ */
3035 return(0);
3036 } else {
3037 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00003038
Daniel Veillardce244ad2004-11-05 10:03:46 +00003039 if (elem == NULL) return(0);
Daniel Veillard37721922001-05-04 15:21:12 +00003040 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
3041 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3042 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3043 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003044
Daniel Veillard37721922001-05-04 15:21:12 +00003045 if ((attrDecl != NULL) &&
3046 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
3047 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
3048 return(1);
3049 }
3050 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003051}
3052
3053/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003054 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00003055 * @doc: the document
3056 * @attr: the attribute
3057 *
3058 * Remove the given attribute from the Ref table maintained internally.
3059 *
3060 * Returns -1 if the lookup failed and 0 otherwise
3061 */
3062int
3063xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00003064 xmlListPtr ref_list;
3065 xmlRefTablePtr table;
3066 xmlChar *ID;
3067 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00003068
Daniel Veillard37721922001-05-04 15:21:12 +00003069 if (doc == NULL) return(-1);
3070 if (attr == NULL) return(-1);
3071 table = (xmlRefTablePtr) doc->refs;
3072 if (table == NULL)
3073 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003074
Daniel Veillard37721922001-05-04 15:21:12 +00003075 if (attr == NULL)
3076 return(-1);
3077 ID = xmlNodeListGetString(doc, attr->children, 1);
3078 if (ID == NULL)
3079 return(-1);
3080 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00003081
Daniel Veillard37721922001-05-04 15:21:12 +00003082 if(ref_list == NULL) {
3083 xmlFree(ID);
3084 return (-1);
3085 }
3086 /* At this point, ref_list refers to a list of references which
3087 * have the same key as the supplied attr. Our list of references
3088 * is ordered by reference address and we don't have that information
3089 * here to use when removing. We'll have to walk the list and
3090 * check for a matching attribute, when we find one stop the walk
3091 * and remove the entry.
3092 * The list is ordered by reference, so that means we don't have the
3093 * key. Passing the list and the reference to the walker means we
3094 * will have enough data to be able to remove the entry.
3095 */
3096 target.l = ref_list;
3097 target.ap = attr;
3098
3099 /* Remove the supplied attr from our list */
3100 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00003101
Daniel Veillard37721922001-05-04 15:21:12 +00003102 /*If the list is empty then remove the list entry in the hash */
3103 if (xmlListEmpty(ref_list))
3104 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
3105 xmlFreeRefList);
3106 xmlFree(ID);
3107 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003108}
3109
3110/**
3111 * xmlGetRefs:
3112 * @doc: pointer to the document
3113 * @ID: the ID value
3114 *
3115 * Find the set of references for the supplied ID.
3116 *
3117 * Returns NULL if not found, otherwise node set for the ID.
3118 */
3119xmlListPtr
3120xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00003121 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00003122
Daniel Veillard37721922001-05-04 15:21:12 +00003123 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00003124 return(NULL);
3125 }
Owen Taylor3473f882001-02-23 17:55:21 +00003126
Daniel Veillard37721922001-05-04 15:21:12 +00003127 if (ID == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00003128 return(NULL);
3129 }
Owen Taylor3473f882001-02-23 17:55:21 +00003130
Daniel Veillard37721922001-05-04 15:21:12 +00003131 table = (xmlRefTablePtr) doc->refs;
3132 if (table == NULL)
3133 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003134
Daniel Veillard37721922001-05-04 15:21:12 +00003135 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00003136}
3137
3138/************************************************************************
3139 * *
3140 * Routines for validity checking *
3141 * *
3142 ************************************************************************/
3143
3144/**
3145 * xmlGetDtdElementDesc:
3146 * @dtd: a pointer to the DtD to search
3147 * @name: the element name
3148 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003149 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003150 *
3151 * returns the xmlElementPtr if found or NULL
3152 */
3153
3154xmlElementPtr
3155xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
3156 xmlElementTablePtr table;
3157 xmlElementPtr cur;
3158 xmlChar *uqname = NULL, *prefix = NULL;
3159
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003160 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003161 if (dtd->elements == NULL)
3162 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003163 table = (xmlElementTablePtr) dtd->elements;
3164
3165 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003166 if (uqname != NULL)
3167 name = uqname;
3168 cur = xmlHashLookup2(table, name, prefix);
3169 if (prefix != NULL) xmlFree(prefix);
3170 if (uqname != NULL) xmlFree(uqname);
3171 return(cur);
3172}
3173/**
3174 * xmlGetDtdElementDesc2:
3175 * @dtd: a pointer to the DtD to search
3176 * @name: the element name
3177 * @create: create an empty description if not found
3178 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003179 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00003180 *
3181 * returns the xmlElementPtr if found or NULL
3182 */
3183
Daniel Veillard86fd5a72001-12-13 14:55:21 +00003184static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00003185xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
3186 xmlElementTablePtr table;
3187 xmlElementPtr cur;
3188 xmlChar *uqname = NULL, *prefix = NULL;
3189
3190 if (dtd == NULL) return(NULL);
3191 if (dtd->elements == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003192 xmlDictPtr dict = NULL;
3193
3194 if (dtd->doc != NULL)
3195 dict = dtd->doc->dict;
3196
Daniel Veillarda10efa82001-04-18 13:09:01 +00003197 if (!create)
3198 return(NULL);
3199 /*
3200 * Create the Element table if needed.
3201 */
3202 table = (xmlElementTablePtr) dtd->elements;
3203 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003204 table = xmlHashCreateDict(0, dict);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003205 dtd->elements = (void *) table;
3206 }
3207 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003208 xmlVErrMemory(NULL, "element table allocation failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003209 return(NULL);
3210 }
3211 }
3212 table = (xmlElementTablePtr) dtd->elements;
3213
3214 uqname = xmlSplitQName2(name, &prefix);
3215 if (uqname != NULL)
3216 name = uqname;
3217 cur = xmlHashLookup2(table, name, prefix);
3218 if ((cur == NULL) && (create)) {
3219 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
3220 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003221 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003222 return(NULL);
3223 }
3224 memset(cur, 0, sizeof(xmlElement));
3225 cur->type = XML_ELEMENT_DECL;
3226
3227 /*
3228 * fill the structure.
3229 */
3230 cur->name = xmlStrdup(name);
3231 cur->prefix = xmlStrdup(prefix);
3232 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3233
3234 xmlHashAddEntry2(table, name, prefix, cur);
3235 }
3236 if (prefix != NULL) xmlFree(prefix);
3237 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00003238 return(cur);
3239}
3240
3241/**
3242 * xmlGetDtdQElementDesc:
3243 * @dtd: a pointer to the DtD to search
3244 * @name: the element name
3245 * @prefix: the element namespace prefix
3246 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003247 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003248 *
3249 * returns the xmlElementPtr if found or NULL
3250 */
3251
Daniel Veillard48da9102001-08-07 01:10:10 +00003252xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00003253xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3254 const xmlChar *prefix) {
3255 xmlElementTablePtr table;
3256
3257 if (dtd == NULL) return(NULL);
3258 if (dtd->elements == NULL) return(NULL);
3259 table = (xmlElementTablePtr) dtd->elements;
3260
3261 return(xmlHashLookup2(table, name, prefix));
3262}
3263
3264/**
3265 * xmlGetDtdAttrDesc:
3266 * @dtd: a pointer to the DtD to search
3267 * @elem: the element name
3268 * @name: the attribute name
3269 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003270 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003271 * this element.
3272 *
3273 * returns the xmlAttributePtr if found or NULL
3274 */
3275
3276xmlAttributePtr
3277xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3278 xmlAttributeTablePtr table;
3279 xmlAttributePtr cur;
3280 xmlChar *uqname = NULL, *prefix = NULL;
3281
3282 if (dtd == NULL) return(NULL);
3283 if (dtd->attributes == NULL) return(NULL);
3284
3285 table = (xmlAttributeTablePtr) dtd->attributes;
3286 if (table == NULL)
3287 return(NULL);
3288
3289 uqname = xmlSplitQName2(name, &prefix);
3290
3291 if (uqname != NULL) {
3292 cur = xmlHashLookup3(table, uqname, prefix, elem);
3293 if (prefix != NULL) xmlFree(prefix);
3294 if (uqname != NULL) xmlFree(uqname);
3295 } else
3296 cur = xmlHashLookup3(table, name, NULL, elem);
3297 return(cur);
3298}
3299
3300/**
3301 * xmlGetDtdQAttrDesc:
3302 * @dtd: a pointer to the DtD to search
3303 * @elem: the element name
3304 * @name: the attribute name
3305 * @prefix: the attribute namespace prefix
3306 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003307 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003308 * this element.
3309 *
3310 * returns the xmlAttributePtr if found or NULL
3311 */
3312
Daniel Veillard48da9102001-08-07 01:10:10 +00003313xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00003314xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3315 const xmlChar *prefix) {
3316 xmlAttributeTablePtr table;
3317
3318 if (dtd == NULL) return(NULL);
3319 if (dtd->attributes == NULL) return(NULL);
3320 table = (xmlAttributeTablePtr) dtd->attributes;
3321
3322 return(xmlHashLookup3(table, name, prefix, elem));
3323}
3324
3325/**
3326 * xmlGetDtdNotationDesc:
3327 * @dtd: a pointer to the DtD to search
3328 * @name: the notation name
3329 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003330 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00003331 *
3332 * returns the xmlNotationPtr if found or NULL
3333 */
3334
3335xmlNotationPtr
3336xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3337 xmlNotationTablePtr table;
3338
3339 if (dtd == NULL) return(NULL);
3340 if (dtd->notations == NULL) return(NULL);
3341 table = (xmlNotationTablePtr) dtd->notations;
3342
3343 return(xmlHashLookup(table, name));
3344}
3345
Daniel Veillardf54cd532004-02-25 11:52:31 +00003346#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003347/**
3348 * xmlValidateNotationUse:
3349 * @ctxt: the validation context
3350 * @doc: the document
3351 * @notationName: the notation name to check
3352 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003353 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00003354 * - [ VC: Notation Declared ]
3355 *
3356 * returns 1 if valid or 0 otherwise
3357 */
3358
3359int
3360xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3361 const xmlChar *notationName) {
3362 xmlNotationPtr notaDecl;
3363 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3364
3365 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3366 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3367 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3368
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003369 if ((notaDecl == NULL) && (ctxt != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003370 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3371 "NOTATION %s is not declared\n",
3372 notationName, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003373 return(0);
3374 }
3375 return(1);
3376}
Daniel Veillardf54cd532004-02-25 11:52:31 +00003377#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003378
3379/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003380 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00003381 * @doc: the document
3382 * @name: the element name
3383 *
3384 * Search in the DtDs whether an element accept Mixed content (or ANY)
3385 * basically if it is supposed to accept text childs
3386 *
3387 * returns 0 if no, 1 if yes, and -1 if no element description is available
3388 */
3389
3390int
3391xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3392 xmlElementPtr elemDecl;
3393
3394 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3395
3396 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3397 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3398 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3399 if (elemDecl == NULL) return(-1);
3400 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00003401 case XML_ELEMENT_TYPE_UNDEFINED:
3402 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003403 case XML_ELEMENT_TYPE_ELEMENT:
3404 return(0);
3405 case XML_ELEMENT_TYPE_EMPTY:
3406 /*
3407 * return 1 for EMPTY since we want VC error to pop up
3408 * on <empty> </empty> for example
3409 */
3410 case XML_ELEMENT_TYPE_ANY:
3411 case XML_ELEMENT_TYPE_MIXED:
3412 return(1);
3413 }
3414 return(1);
3415}
3416
Daniel Veillard4432df22003-09-28 18:58:27 +00003417#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003418/**
3419 * xmlValidateNameValue:
3420 * @value: an Name value
3421 *
3422 * Validate that the given value match Name production
3423 *
3424 * returns 1 if valid or 0 otherwise
3425 */
3426
Daniel Veillard9b731d72002-04-14 12:56:08 +00003427int
Owen Taylor3473f882001-02-23 17:55:21 +00003428xmlValidateNameValue(const xmlChar *value) {
3429 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003430 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003431
3432 if (value == NULL) return(0);
3433 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003434 val = xmlStringCurrentChar(NULL, cur, &len);
3435 cur += len;
3436 if (!IS_LETTER(val) && (val != '_') &&
3437 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003438 return(0);
3439 }
3440
Daniel Veillardd8224e02002-01-13 15:43:22 +00003441 val = xmlStringCurrentChar(NULL, cur, &len);
3442 cur += len;
3443 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3444 (val == '.') || (val == '-') ||
3445 (val == '_') || (val == ':') ||
3446 (IS_COMBINING(val)) ||
3447 (IS_EXTENDER(val))) {
3448 val = xmlStringCurrentChar(NULL, cur, &len);
3449 cur += len;
3450 }
Owen Taylor3473f882001-02-23 17:55:21 +00003451
Daniel Veillardd8224e02002-01-13 15:43:22 +00003452 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003453
3454 return(1);
3455}
3456
3457/**
3458 * xmlValidateNamesValue:
3459 * @value: an Names value
3460 *
3461 * Validate that the given value match Names production
3462 *
3463 * returns 1 if valid or 0 otherwise
3464 */
3465
Daniel Veillard9b731d72002-04-14 12:56:08 +00003466int
Owen Taylor3473f882001-02-23 17:55:21 +00003467xmlValidateNamesValue(const xmlChar *value) {
3468 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003469 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003470
3471 if (value == NULL) return(0);
3472 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003473 val = xmlStringCurrentChar(NULL, cur, &len);
3474 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003475
Daniel Veillardd8224e02002-01-13 15:43:22 +00003476 if (!IS_LETTER(val) && (val != '_') &&
3477 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003478 return(0);
3479 }
3480
Daniel Veillardd8224e02002-01-13 15:43:22 +00003481 val = xmlStringCurrentChar(NULL, cur, &len);
3482 cur += len;
3483 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3484 (val == '.') || (val == '-') ||
3485 (val == '_') || (val == ':') ||
3486 (IS_COMBINING(val)) ||
3487 (IS_EXTENDER(val))) {
3488 val = xmlStringCurrentChar(NULL, cur, &len);
3489 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003490 }
3491
Daniel Veillard807b4de2004-09-26 14:42:56 +00003492 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3493 while (val == 0x20) {
3494 while (val == 0x20) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003495 val = xmlStringCurrentChar(NULL, cur, &len);
3496 cur += len;
3497 }
3498
3499 if (!IS_LETTER(val) && (val != '_') &&
3500 (val != ':')) {
3501 return(0);
3502 }
3503 val = xmlStringCurrentChar(NULL, cur, &len);
3504 cur += len;
3505
3506 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3507 (val == '.') || (val == '-') ||
3508 (val == '_') || (val == ':') ||
3509 (IS_COMBINING(val)) ||
3510 (IS_EXTENDER(val))) {
3511 val = xmlStringCurrentChar(NULL, cur, &len);
3512 cur += len;
3513 }
3514 }
3515
3516 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003517
3518 return(1);
3519}
3520
3521/**
3522 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003523 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003524 *
3525 * Validate that the given value match Nmtoken production
3526 *
3527 * [ VC: Name Token ]
3528 *
3529 * returns 1 if valid or 0 otherwise
3530 */
3531
Daniel Veillard9b731d72002-04-14 12:56:08 +00003532int
Owen Taylor3473f882001-02-23 17:55:21 +00003533xmlValidateNmtokenValue(const xmlChar *value) {
3534 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003535 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003536
3537 if (value == NULL) return(0);
3538 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003539 val = xmlStringCurrentChar(NULL, cur, &len);
3540 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003541
Daniel Veillardd8224e02002-01-13 15:43:22 +00003542 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3543 (val != '.') && (val != '-') &&
3544 (val != '_') && (val != ':') &&
3545 (!IS_COMBINING(val)) &&
3546 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003547 return(0);
3548
Daniel Veillardd8224e02002-01-13 15:43:22 +00003549 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3550 (val == '.') || (val == '-') ||
3551 (val == '_') || (val == ':') ||
3552 (IS_COMBINING(val)) ||
3553 (IS_EXTENDER(val))) {
3554 val = xmlStringCurrentChar(NULL, cur, &len);
3555 cur += len;
3556 }
Owen Taylor3473f882001-02-23 17:55:21 +00003557
Daniel Veillardd8224e02002-01-13 15:43:22 +00003558 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003559
3560 return(1);
3561}
3562
3563/**
3564 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003565 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003566 *
3567 * Validate that the given value match Nmtokens production
3568 *
3569 * [ VC: Name Token ]
3570 *
3571 * returns 1 if valid or 0 otherwise
3572 */
3573
Daniel Veillard9b731d72002-04-14 12:56:08 +00003574int
Owen Taylor3473f882001-02-23 17:55:21 +00003575xmlValidateNmtokensValue(const xmlChar *value) {
3576 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003577 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003578
3579 if (value == NULL) return(0);
3580 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003581 val = xmlStringCurrentChar(NULL, cur, &len);
3582 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003583
Daniel Veillardd8224e02002-01-13 15:43:22 +00003584 while (IS_BLANK(val)) {
3585 val = xmlStringCurrentChar(NULL, cur, &len);
3586 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003587 }
3588
Daniel Veillardd8224e02002-01-13 15:43:22 +00003589 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3590 (val != '.') && (val != '-') &&
3591 (val != '_') && (val != ':') &&
3592 (!IS_COMBINING(val)) &&
3593 (!IS_EXTENDER(val)))
3594 return(0);
3595
3596 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3597 (val == '.') || (val == '-') ||
3598 (val == '_') || (val == ':') ||
3599 (IS_COMBINING(val)) ||
3600 (IS_EXTENDER(val))) {
3601 val = xmlStringCurrentChar(NULL, cur, &len);
3602 cur += len;
3603 }
3604
Daniel Veillard807b4de2004-09-26 14:42:56 +00003605 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3606 while (val == 0x20) {
3607 while (val == 0x20) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003608 val = xmlStringCurrentChar(NULL, cur, &len);
3609 cur += len;
3610 }
3611 if (val == 0) return(1);
3612
3613 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3614 (val != '.') && (val != '-') &&
3615 (val != '_') && (val != ':') &&
3616 (!IS_COMBINING(val)) &&
3617 (!IS_EXTENDER(val)))
3618 return(0);
3619
3620 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3621 (val == '.') || (val == '-') ||
3622 (val == '_') || (val == ':') ||
3623 (IS_COMBINING(val)) ||
3624 (IS_EXTENDER(val))) {
3625 val = xmlStringCurrentChar(NULL, cur, &len);
3626 cur += len;
3627 }
3628 }
3629
3630 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003631
3632 return(1);
3633}
3634
3635/**
3636 * xmlValidateNotationDecl:
3637 * @ctxt: the validation context
3638 * @doc: a document instance
3639 * @nota: a notation definition
3640 *
3641 * Try to validate a single notation definition
3642 * basically it does the following checks as described by the
3643 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003644 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003645 * But this function get called anyway ...
3646 *
3647 * returns 1 if valid or 0 otherwise
3648 */
3649
3650int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003651xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3652 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003653 int ret = 1;
3654
3655 return(ret);
3656}
3657
3658/**
3659 * xmlValidateAttributeValue:
3660 * @type: an attribute type
3661 * @value: an attribute value
3662 *
3663 * Validate that the given attribute value match the proper production
3664 *
3665 * [ VC: ID ]
3666 * Values of type ID must match the Name production....
3667 *
3668 * [ VC: IDREF ]
3669 * Values of type IDREF must match the Name production, and values
3670 * of type IDREFS must match Names ...
3671 *
3672 * [ VC: Entity Name ]
3673 * Values of type ENTITY must match the Name production, values
3674 * of type ENTITIES must match Names ...
3675 *
3676 * [ VC: Name Token ]
3677 * Values of type NMTOKEN must match the Nmtoken production; values
3678 * of type NMTOKENS must match Nmtokens.
3679 *
3680 * returns 1 if valid or 0 otherwise
3681 */
3682
3683int
3684xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3685 switch (type) {
3686 case XML_ATTRIBUTE_ENTITIES:
3687 case XML_ATTRIBUTE_IDREFS:
3688 return(xmlValidateNamesValue(value));
3689 case XML_ATTRIBUTE_ENTITY:
3690 case XML_ATTRIBUTE_IDREF:
3691 case XML_ATTRIBUTE_ID:
3692 case XML_ATTRIBUTE_NOTATION:
3693 return(xmlValidateNameValue(value));
3694 case XML_ATTRIBUTE_NMTOKENS:
3695 case XML_ATTRIBUTE_ENUMERATION:
3696 return(xmlValidateNmtokensValue(value));
3697 case XML_ATTRIBUTE_NMTOKEN:
3698 return(xmlValidateNmtokenValue(value));
3699 case XML_ATTRIBUTE_CDATA:
3700 break;
3701 }
3702 return(1);
3703}
3704
3705/**
3706 * xmlValidateAttributeValue2:
3707 * @ctxt: the validation context
3708 * @doc: the document
3709 * @name: the attribute name (used for error reporting only)
3710 * @type: the attribute type
3711 * @value: the attribute value
3712 *
3713 * Validate that the given attribute value match a given type.
3714 * This typically cannot be done before having finished parsing
3715 * the subsets.
3716 *
3717 * [ VC: IDREF ]
3718 * Values of type IDREF must match one of the declared IDs
3719 * Values of type IDREFS must match a sequence of the declared IDs
3720 * each Name must match the value of an ID attribute on some element
3721 * in the XML document; i.e. IDREF values must match the value of
3722 * some ID attribute
3723 *
3724 * [ VC: Entity Name ]
3725 * Values of type ENTITY must match one declared entity
3726 * Values of type ENTITIES must match a sequence of declared entities
3727 *
3728 * [ VC: Notation Attributes ]
3729 * all notation names in the declaration must be declared.
3730 *
3731 * returns 1 if valid or 0 otherwise
3732 */
3733
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003734static int
Owen Taylor3473f882001-02-23 17:55:21 +00003735xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3736 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3737 int ret = 1;
3738 switch (type) {
3739 case XML_ATTRIBUTE_IDREFS:
3740 case XML_ATTRIBUTE_IDREF:
3741 case XML_ATTRIBUTE_ID:
3742 case XML_ATTRIBUTE_NMTOKENS:
3743 case XML_ATTRIBUTE_ENUMERATION:
3744 case XML_ATTRIBUTE_NMTOKEN:
3745 case XML_ATTRIBUTE_CDATA:
3746 break;
3747 case XML_ATTRIBUTE_ENTITY: {
3748 xmlEntityPtr ent;
3749
3750 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003751 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003752 if ((ent == NULL) && (doc->standalone == 1)) {
3753 doc->standalone = 0;
3754 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003755 }
Owen Taylor3473f882001-02-23 17:55:21 +00003756 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003757 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3758 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003759 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003760 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003761 ret = 0;
3762 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003763 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3764 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003765 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003766 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003767 ret = 0;
3768 }
3769 break;
3770 }
3771 case XML_ATTRIBUTE_ENTITIES: {
3772 xmlChar *dup, *nam = NULL, *cur, save;
3773 xmlEntityPtr ent;
3774
3775 dup = xmlStrdup(value);
3776 if (dup == NULL)
3777 return(0);
3778 cur = dup;
3779 while (*cur != 0) {
3780 nam = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00003781 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003782 save = *cur;
3783 *cur = 0;
3784 ent = xmlGetDocEntity(doc, nam);
3785 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003786 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3787 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003788 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003789 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003790 ret = 0;
3791 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003792 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3793 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003794 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003795 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003796 ret = 0;
3797 }
3798 if (save == 0)
3799 break;
3800 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00003801 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003802 }
3803 xmlFree(dup);
3804 break;
3805 }
3806 case XML_ATTRIBUTE_NOTATION: {
3807 xmlNotationPtr nota;
3808
3809 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3810 if ((nota == NULL) && (doc->extSubset != NULL))
3811 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3812
3813 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003814 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3815 XML_DTD_UNKNOWN_NOTATION,
Owen Taylor3473f882001-02-23 17:55:21 +00003816 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003817 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003818 ret = 0;
3819 }
3820 break;
3821 }
3822 }
3823 return(ret);
3824}
3825
3826/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003827 * xmlValidCtxtNormalizeAttributeValue:
3828 * @ctxt: the validation context
3829 * @doc: the document
3830 * @elem: the parent
3831 * @name: the attribute name
3832 * @value: the attribute value
3833 * @ctxt: the validation context or NULL
3834 *
3835 * Does the validation related extra step of the normalization of attribute
3836 * values:
3837 *
3838 * If the declared value is not CDATA, then the XML processor must further
3839 * process the normalized attribute value by discarding any leading and
3840 * trailing space (#x20) characters, and by replacing sequences of space
3841 * (#x20) characters by single space (#x20) character.
3842 *
3843 * Also check VC: Standalone Document Declaration in P32, and update
3844 * ctxt->valid accordingly
3845 *
3846 * returns a new normalized string if normalization is needed, NULL otherwise
3847 * the caller must free the returned value.
3848 */
3849
3850xmlChar *
3851xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3852 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3853 xmlChar *ret, *dst;
3854 const xmlChar *src;
3855 xmlAttributePtr attrDecl = NULL;
3856 int extsubset = 0;
3857
3858 if (doc == NULL) return(NULL);
3859 if (elem == NULL) return(NULL);
3860 if (name == NULL) return(NULL);
3861 if (value == NULL) return(NULL);
3862
3863 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003864 xmlChar fn[50];
3865 xmlChar *fullname;
3866
3867 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3868 if (fullname == NULL)
3869 return(0);
3870 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003871 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003872 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003873 if (attrDecl != NULL)
3874 extsubset = 1;
3875 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003876 if ((fullname != fn) && (fullname != elem->name))
3877 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003878 }
3879 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3880 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3881 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3882 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3883 if (attrDecl != NULL)
3884 extsubset = 1;
3885 }
3886
3887 if (attrDecl == NULL)
3888 return(NULL);
3889 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3890 return(NULL);
3891
3892 ret = xmlStrdup(value);
3893 if (ret == NULL)
3894 return(NULL);
3895 src = value;
3896 dst = ret;
3897 while (*src == 0x20) src++;
3898 while (*src != 0) {
3899 if (*src == 0x20) {
3900 while (*src == 0x20) src++;
3901 if (*src != 0)
3902 *dst++ = 0x20;
3903 } else {
3904 *dst++ = *src++;
3905 }
3906 }
3907 *dst = 0;
3908 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003909 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003910"standalone: %s on %s value had to be normalized based on external subset declaration\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003911 name, elem->name, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003912 ctxt->valid = 0;
3913 }
3914 return(ret);
3915}
3916
3917/**
Owen Taylor3473f882001-02-23 17:55:21 +00003918 * xmlValidNormalizeAttributeValue:
3919 * @doc: the document
3920 * @elem: the parent
3921 * @name: the attribute name
3922 * @value: the attribute value
3923 *
3924 * Does the validation related extra step of the normalization of attribute
3925 * values:
3926 *
3927 * If the declared value is not CDATA, then the XML processor must further
3928 * process the normalized attribute value by discarding any leading and
3929 * trailing space (#x20) characters, and by replacing sequences of space
3930 * (#x20) characters by single space (#x20) character.
3931 *
Daniel Veillard652327a2003-09-29 18:02:38 +00003932 * Returns a new normalized string if normalization is needed, NULL otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00003933 * the caller must free the returned value.
3934 */
3935
3936xmlChar *
3937xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3938 const xmlChar *name, const xmlChar *value) {
3939 xmlChar *ret, *dst;
3940 const xmlChar *src;
3941 xmlAttributePtr attrDecl = NULL;
3942
3943 if (doc == NULL) return(NULL);
3944 if (elem == NULL) return(NULL);
3945 if (name == NULL) return(NULL);
3946 if (value == NULL) return(NULL);
3947
3948 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003949 xmlChar fn[50];
3950 xmlChar *fullname;
3951
3952 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3953 if (fullname == NULL)
3954 return(0);
3955 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003956 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003957 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3958 if ((fullname != fn) && (fullname != elem->name))
3959 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003960 }
3961 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3962 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3963 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3964
3965 if (attrDecl == NULL)
3966 return(NULL);
3967 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3968 return(NULL);
3969
3970 ret = xmlStrdup(value);
3971 if (ret == NULL)
3972 return(NULL);
3973 src = value;
3974 dst = ret;
3975 while (*src == 0x20) src++;
3976 while (*src != 0) {
3977 if (*src == 0x20) {
3978 while (*src == 0x20) src++;
3979 if (*src != 0)
3980 *dst++ = 0x20;
3981 } else {
3982 *dst++ = *src++;
3983 }
3984 }
3985 *dst = 0;
3986 return(ret);
3987}
3988
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003989static void
Owen Taylor3473f882001-02-23 17:55:21 +00003990xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003991 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003992 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3993}
3994
3995/**
3996 * xmlValidateAttributeDecl:
3997 * @ctxt: the validation context
3998 * @doc: a document instance
3999 * @attr: an attribute definition
4000 *
4001 * Try to validate a single attribute definition
4002 * basically it does the following checks as described by the
4003 * XML-1.0 recommendation:
4004 * - [ VC: Attribute Default Legal ]
4005 * - [ VC: Enumeration ]
4006 * - [ VC: ID Attribute Default ]
4007 *
4008 * The ID/IDREF uniqueness and matching are done separately
4009 *
4010 * returns 1 if valid or 0 otherwise
4011 */
4012
4013int
4014xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4015 xmlAttributePtr attr) {
4016 int ret = 1;
4017 int val;
4018 CHECK_DTD;
4019 if(attr == NULL) return(1);
4020
4021 /* Attribute Default Legal */
4022 /* Enumeration */
4023 if (attr->defaultValue != NULL) {
4024 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
4025 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004026 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004027 "Syntax of default value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004028 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004029 }
4030 ret &= val;
4031 }
4032
4033 /* ID Attribute Default */
4034 if ((attr->atype == XML_ATTRIBUTE_ID)&&
4035 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
4036 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004037 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004038 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004039 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004040 ret = 0;
4041 }
4042
4043 /* One ID per Element Type */
4044 if (attr->atype == XML_ATTRIBUTE_ID) {
4045 int nbId;
4046
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004047 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00004048 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
4049 attr->elem);
4050 if (elem != NULL) {
Daniel Veillarddbee0f12005-06-27 13:42:57 +00004051 nbId = xmlScanIDAttributeDecl(NULL, elem, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00004052 } else {
4053 xmlAttributeTablePtr table;
4054
4055 /*
4056 * The attribute may be declared in the internal subset and the
4057 * element in the external subset.
4058 */
4059 nbId = 0;
4060 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
4061 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
4062 xmlValidateAttributeIdCallback, &nbId);
4063 }
4064 if (nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004065
4066 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004067 "Element %s has %d ID attribute defined in the internal subset : %s\n",
4068 attr->elem, nbId, attr->name);
4069 } else if (doc->extSubset != NULL) {
4070 int extId = 0;
4071 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
4072 if (elem != NULL) {
Daniel Veillarddbee0f12005-06-27 13:42:57 +00004073 extId = xmlScanIDAttributeDecl(NULL, elem, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00004074 }
4075 if (extId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004076 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004077 "Element %s has %d ID attribute defined in the external subset : %s\n",
4078 attr->elem, extId, attr->name);
4079 } else if (extId + nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004080 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004081"Element %s has ID attributes defined in the internal and external subset : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004082 attr->elem, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004083 }
4084 }
4085 }
4086
4087 /* Validity Constraint: Enumeration */
4088 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
4089 xmlEnumerationPtr tree = attr->tree;
4090 while (tree != NULL) {
4091 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
4092 tree = tree->next;
4093 }
4094 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004095 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004096"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004097 attr->defaultValue, attr->name, attr->elem);
4098 ret = 0;
4099 }
4100 }
4101
4102 return(ret);
4103}
4104
4105/**
4106 * xmlValidateElementDecl:
4107 * @ctxt: the validation context
4108 * @doc: a document instance
4109 * @elem: an element definition
4110 *
4111 * Try to validate a single element definition
4112 * basically it does the following checks as described by the
4113 * XML-1.0 recommendation:
4114 * - [ VC: One ID per Element Type ]
4115 * - [ VC: No Duplicate Types ]
4116 * - [ VC: Unique Element Type Declaration ]
4117 *
4118 * returns 1 if valid or 0 otherwise
4119 */
4120
4121int
4122xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4123 xmlElementPtr elem) {
4124 int ret = 1;
4125 xmlElementPtr tst;
4126
4127 CHECK_DTD;
4128
4129 if (elem == NULL) return(1);
4130
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004131#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00004132#ifdef LIBXML_REGEXP_ENABLED
4133 /* Build the regexp associated to the content model */
4134 ret = xmlValidBuildContentModel(ctxt, elem);
4135#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004136#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00004137
Owen Taylor3473f882001-02-23 17:55:21 +00004138 /* No Duplicate Types */
4139 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
4140 xmlElementContentPtr cur, next;
4141 const xmlChar *name;
4142
4143 cur = elem->content;
4144 while (cur != NULL) {
4145 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
4146 if (cur->c1 == NULL) break;
4147 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4148 name = cur->c1->name;
4149 next = cur->c2;
4150 while (next != NULL) {
4151 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00004152 if ((xmlStrEqual(next->name, name)) &&
4153 (xmlStrEqual(next->prefix, cur->prefix))) {
4154 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004155 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00004156 "Definition of %s has duplicate references of %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004157 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004158 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004159 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004160 "Definition of %s has duplicate references of %s:%s\n",
4161 elem->name, cur->prefix, name);
4162 }
Owen Taylor3473f882001-02-23 17:55:21 +00004163 ret = 0;
4164 }
4165 break;
4166 }
4167 if (next->c1 == NULL) break;
4168 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00004169 if ((xmlStrEqual(next->c1->name, name)) &&
4170 (xmlStrEqual(next->c1->prefix, cur->prefix))) {
4171 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004172 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004173 "Definition of %s has duplicate references to %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004174 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004175 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004176 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004177 "Definition of %s has duplicate references to %s:%s\n",
4178 elem->name, cur->prefix, name);
4179 }
Owen Taylor3473f882001-02-23 17:55:21 +00004180 ret = 0;
4181 }
4182 next = next->c2;
4183 }
4184 }
4185 cur = cur->c2;
4186 }
4187 }
4188
4189 /* VC: Unique Element Type Declaration */
4190 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004191 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004192 ((tst->prefix == elem->prefix) ||
4193 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004194 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004195 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4196 "Redefinition of element %s\n",
4197 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004198 ret = 0;
4199 }
4200 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004201 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004202 ((tst->prefix == elem->prefix) ||
4203 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004204 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004205 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4206 "Redefinition of element %s\n",
4207 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004208 ret = 0;
4209 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00004210 /* One ID per Element Type
4211 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00004212 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4213 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00004214 } */
Owen Taylor3473f882001-02-23 17:55:21 +00004215 return(ret);
4216}
4217
4218/**
4219 * xmlValidateOneAttribute:
4220 * @ctxt: the validation context
4221 * @doc: a document instance
4222 * @elem: an element instance
4223 * @attr: an attribute instance
4224 * @value: the attribute value (without entities processing)
4225 *
4226 * Try to validate a single attribute for an element
4227 * basically it does the following checks as described by the
4228 * XML-1.0 recommendation:
4229 * - [ VC: Attribute Value Type ]
4230 * - [ VC: Fixed Attribute Default ]
4231 * - [ VC: Entity Name ]
4232 * - [ VC: Name Token ]
4233 * - [ VC: ID ]
4234 * - [ VC: IDREF ]
4235 * - [ VC: Entity Name ]
4236 * - [ VC: Notation Attributes ]
4237 *
4238 * The ID/IDREF uniqueness and matching are done separately
4239 *
4240 * returns 1 if valid or 0 otherwise
4241 */
4242
4243int
4244xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillard07cb8222003-09-10 10:51:05 +00004245 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4246{
Owen Taylor3473f882001-02-23 17:55:21 +00004247 xmlAttributePtr attrDecl = NULL;
4248 int val;
4249 int ret = 1;
4250
4251 CHECK_DTD;
4252 if ((elem == NULL) || (elem->name == NULL)) return(0);
4253 if ((attr == NULL) || (attr->name == NULL)) return(0);
4254
4255 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004256 xmlChar fn[50];
4257 xmlChar *fullname;
4258
4259 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4260 if (fullname == NULL)
4261 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004262 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004263 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004264 attr->name, attr->ns->prefix);
4265 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004266 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004267 attr->name, attr->ns->prefix);
4268 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004269 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004270 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4271 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00004272 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004273 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004274 if ((fullname != fn) && (fullname != elem->name))
4275 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00004276 }
4277 if (attrDecl == NULL) {
4278 if (attr->ns != NULL) {
4279 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4280 attr->name, attr->ns->prefix);
4281 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4282 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4283 attr->name, attr->ns->prefix);
4284 } else {
4285 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4286 elem->name, attr->name);
4287 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4288 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4289 elem->name, attr->name);
4290 }
4291 }
4292
4293
4294 /* Validity Constraint: Attribute Value Type */
4295 if (attrDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004296 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004297 "No declaration for attribute %s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004298 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004299 return(0);
4300 }
4301 attr->atype = attrDecl->atype;
4302
4303 val = xmlValidateAttributeValue(attrDecl->atype, value);
4304 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004305 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004306 "Syntax of value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004307 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004308 ret = 0;
4309 }
4310
4311 /* Validity constraint: Fixed Attribute Default */
4312 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4313 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004314 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004315 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004316 attr->name, elem->name, attrDecl->defaultValue);
4317 ret = 0;
4318 }
4319 }
4320
4321 /* Validity Constraint: ID uniqueness */
4322 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4323 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4324 ret = 0;
4325 }
4326
4327 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4328 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4329 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4330 ret = 0;
4331 }
4332
4333 /* Validity Constraint: Notation Attributes */
4334 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4335 xmlEnumerationPtr tree = attrDecl->tree;
4336 xmlNotationPtr nota;
4337
4338 /* First check that the given NOTATION was declared */
4339 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4340 if (nota == NULL)
4341 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4342
4343 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004344 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004345 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004346 value, attr->name, elem->name);
4347 ret = 0;
4348 }
4349
4350 /* Second, verify that it's among the list */
4351 while (tree != NULL) {
4352 if (xmlStrEqual(tree->name, value)) break;
4353 tree = tree->next;
4354 }
4355 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004356 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004357"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004358 value, attr->name, elem->name);
4359 ret = 0;
4360 }
4361 }
4362
4363 /* Validity Constraint: Enumeration */
4364 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4365 xmlEnumerationPtr tree = attrDecl->tree;
4366 while (tree != NULL) {
4367 if (xmlStrEqual(tree->name, value)) break;
4368 tree = tree->next;
4369 }
4370 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004371 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004372 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004373 value, attr->name, elem->name);
4374 ret = 0;
4375 }
4376 }
4377
4378 /* Fixed Attribute Default */
4379 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4380 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004381 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004382 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004383 attr->name, elem->name, attrDecl->defaultValue);
4384 ret = 0;
4385 }
4386
4387 /* Extra check for the attribute value */
4388 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4389 attrDecl->atype, value);
4390
4391 return(ret);
4392}
4393
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004394/**
4395 * xmlValidateOneNamespace:
4396 * @ctxt: the validation context
4397 * @doc: a document instance
4398 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004399 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004400 * @ns: an namespace declaration instance
4401 * @value: the attribute value (without entities processing)
4402 *
4403 * Try to validate a single namespace declaration for an element
4404 * basically it does the following checks as described by the
4405 * XML-1.0 recommendation:
4406 * - [ VC: Attribute Value Type ]
4407 * - [ VC: Fixed Attribute Default ]
4408 * - [ VC: Entity Name ]
4409 * - [ VC: Name Token ]
4410 * - [ VC: ID ]
4411 * - [ VC: IDREF ]
4412 * - [ VC: Entity Name ]
4413 * - [ VC: Notation Attributes ]
4414 *
4415 * The ID/IDREF uniqueness and matching are done separately
4416 *
4417 * returns 1 if valid or 0 otherwise
4418 */
4419
4420int
4421xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4422xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4423 /* xmlElementPtr elemDecl; */
4424 xmlAttributePtr attrDecl = NULL;
4425 int val;
4426 int ret = 1;
4427
4428 CHECK_DTD;
4429 if ((elem == NULL) || (elem->name == NULL)) return(0);
4430 if ((ns == NULL) || (ns->href == NULL)) return(0);
4431
4432 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004433 xmlChar fn[50];
4434 xmlChar *fullname;
4435
4436 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4437 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004438 xmlVErrMemory(ctxt, "Validating namespace");
Daniel Veillardc00cda82003-04-07 10:22:39 +00004439 return(0);
4440 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004441 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004442 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004443 ns->prefix, BAD_CAST "xmlns");
4444 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004445 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004446 ns->prefix, BAD_CAST "xmlns");
4447 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004448 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004449 BAD_CAST "xmlns");
4450 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004451 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004452 BAD_CAST "xmlns");
4453 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004454 if ((fullname != fn) && (fullname != elem->name))
4455 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004456 }
4457 if (attrDecl == NULL) {
4458 if (ns->prefix != NULL) {
4459 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4460 ns->prefix, BAD_CAST "xmlns");
4461 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4462 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4463 ns->prefix, BAD_CAST "xmlns");
4464 } else {
4465 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4466 elem->name, BAD_CAST "xmlns");
4467 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4468 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4469 elem->name, BAD_CAST "xmlns");
4470 }
4471 }
4472
4473
4474 /* Validity Constraint: Attribute Value Type */
4475 if (attrDecl == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004476 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004477 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004478 "No declaration for attribute xmlns:%s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004479 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004480 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004481 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004482 "No declaration for attribute xmlns of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004483 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004484 }
4485 return(0);
4486 }
4487
4488 val = xmlValidateAttributeValue(attrDecl->atype, value);
4489 if (val == 0) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004490 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004491 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004492 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004493 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004494 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004495 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004496 "Syntax of value for attribute xmlns of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004497 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004498 }
4499 ret = 0;
4500 }
4501
4502 /* Validity constraint: Fixed Attribute Default */
4503 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4504 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004505 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004506 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004507 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4508 ns->prefix, elem->name, attrDecl->defaultValue);
4509 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004510 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004511 "Value for attribute xmlns of %s is different from default \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004512 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004513 }
4514 ret = 0;
4515 }
4516 }
4517
4518 /* Validity Constraint: ID uniqueness */
4519 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4520 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4521 ret = 0;
4522 }
4523
4524 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4525 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4526 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4527 ret = 0;
4528 }
4529
4530 /* Validity Constraint: Notation Attributes */
4531 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4532 xmlEnumerationPtr tree = attrDecl->tree;
4533 xmlNotationPtr nota;
4534
4535 /* First check that the given NOTATION was declared */
4536 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4537 if (nota == NULL)
4538 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4539
4540 if (nota == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004541 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004542 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004543 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4544 value, ns->prefix, elem->name);
4545 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004546 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004547 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004548 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004549 }
4550 ret = 0;
4551 }
4552
4553 /* Second, verify that it's among the list */
4554 while (tree != NULL) {
4555 if (xmlStrEqual(tree->name, value)) break;
4556 tree = tree->next;
4557 }
4558 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004559 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004560 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004561"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4562 value, ns->prefix, elem->name);
4563 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004564 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004565"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004566 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004567 }
4568 ret = 0;
4569 }
4570 }
4571
4572 /* Validity Constraint: Enumeration */
4573 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4574 xmlEnumerationPtr tree = attrDecl->tree;
4575 while (tree != NULL) {
4576 if (xmlStrEqual(tree->name, value)) break;
4577 tree = tree->next;
4578 }
4579 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004580 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004581 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004582"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4583 value, ns->prefix, elem->name);
4584 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004585 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004586"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004587 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004588 }
4589 ret = 0;
4590 }
4591 }
4592
4593 /* Fixed Attribute Default */
4594 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4595 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004596 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004597 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004598 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4599 ns->prefix, elem->name, attrDecl->defaultValue);
4600 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004601 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004602 "Value for attribute xmlns of %s must be \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004603 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004604 }
4605 ret = 0;
4606 }
4607
4608 /* Extra check for the attribute value */
4609 if (ns->prefix != NULL) {
4610 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4611 attrDecl->atype, value);
4612 } else {
4613 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4614 attrDecl->atype, value);
4615 }
4616
4617 return(ret);
4618}
4619
Daniel Veillard118aed72002-09-24 14:13:13 +00004620#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004621/**
4622 * xmlValidateSkipIgnorable:
4623 * @ctxt: the validation context
4624 * @child: the child list
4625 *
4626 * Skip ignorable elements w.r.t. the validation process
4627 *
4628 * returns the first element to consider for validation of the content model
4629 */
4630
4631static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004632xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004633 while (child != NULL) {
4634 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004635 /* These things are ignored (skipped) during validation. */
4636 case XML_PI_NODE:
4637 case XML_COMMENT_NODE:
4638 case XML_XINCLUDE_START:
4639 case XML_XINCLUDE_END:
4640 child = child->next;
4641 break;
4642 case XML_TEXT_NODE:
4643 if (xmlIsBlankNode(child))
4644 child = child->next;
4645 else
4646 return(child);
4647 break;
4648 /* keep current node */
4649 default:
4650 return(child);
4651 }
4652 }
4653 return(child);
4654}
4655
4656/**
4657 * xmlValidateElementType:
4658 * @ctxt: the validation context
4659 *
4660 * Try to validate the content model of an element internal function
4661 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004662 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4663 * reference is found and -3 if the validation succeeded but
4664 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004665 */
4666
4667static int
4668xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004669 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004670 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004671
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004672 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004673 if ((NODE == NULL) && (CONT == NULL))
4674 return(1);
4675 if ((NODE == NULL) &&
4676 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4677 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4678 return(1);
4679 }
4680 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004681 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004682 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004683
4684 /*
4685 * We arrive here when more states need to be examined
4686 */
4687cont:
4688
4689 /*
4690 * We just recovered from a rollback generated by a possible
4691 * epsilon transition, go directly to the analysis phase
4692 */
4693 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004694 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004695 DEBUG_VALID_STATE(NODE, CONT)
4696 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004697 goto analyze;
4698 }
4699
4700 DEBUG_VALID_STATE(NODE, CONT)
4701 /*
4702 * we may have to save a backup state here. This is the equivalent
4703 * of handling epsilon transition in NFAs.
4704 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004705 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004706 ((CONT->parent == NULL) ||
4707 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004708 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004709 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004710 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004711 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004712 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4713 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004714 }
4715
4716
4717 /*
4718 * Check first if the content matches
4719 */
4720 switch (CONT->type) {
4721 case XML_ELEMENT_CONTENT_PCDATA:
4722 if (NODE == NULL) {
4723 DEBUG_VALID_MSG("pcdata failed no node");
4724 ret = 0;
4725 break;
4726 }
4727 if (NODE->type == XML_TEXT_NODE) {
4728 DEBUG_VALID_MSG("pcdata found, skip to next");
4729 /*
4730 * go to next element in the content model
4731 * skipping ignorable elems
4732 */
4733 do {
4734 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004735 NODE = xmlValidateSkipIgnorable(NODE);
4736 if ((NODE != NULL) &&
4737 (NODE->type == XML_ENTITY_REF_NODE))
4738 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004739 } while ((NODE != NULL) &&
4740 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004741 (NODE->type != XML_TEXT_NODE) &&
4742 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004743 ret = 1;
4744 break;
4745 } else {
4746 DEBUG_VALID_MSG("pcdata failed");
4747 ret = 0;
4748 break;
4749 }
4750 break;
4751 case XML_ELEMENT_CONTENT_ELEMENT:
4752 if (NODE == NULL) {
4753 DEBUG_VALID_MSG("element failed no node");
4754 ret = 0;
4755 break;
4756 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004757 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4758 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004759 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004760 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4761 ret = (CONT->prefix == NULL);
4762 } else if (CONT->prefix == NULL) {
4763 ret = 0;
4764 } else {
4765 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4766 }
4767 }
4768 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004769 DEBUG_VALID_MSG("element found, skip to next");
4770 /*
4771 * go to next element in the content model
4772 * skipping ignorable elems
4773 */
4774 do {
4775 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004776 NODE = xmlValidateSkipIgnorable(NODE);
4777 if ((NODE != NULL) &&
4778 (NODE->type == XML_ENTITY_REF_NODE))
4779 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004780 } while ((NODE != NULL) &&
4781 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004782 (NODE->type != XML_TEXT_NODE) &&
4783 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004784 } else {
4785 DEBUG_VALID_MSG("element failed");
4786 ret = 0;
4787 break;
4788 }
4789 break;
4790 case XML_ELEMENT_CONTENT_OR:
4791 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004792 * Small optimization.
4793 */
4794 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4795 if ((NODE == NULL) ||
4796 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4797 DEPTH++;
4798 CONT = CONT->c2;
4799 goto cont;
4800 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004801 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4802 ret = (CONT->c1->prefix == NULL);
4803 } else if (CONT->c1->prefix == NULL) {
4804 ret = 0;
4805 } else {
4806 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4807 }
4808 if (ret == 0) {
4809 DEPTH++;
4810 CONT = CONT->c2;
4811 goto cont;
4812 }
Daniel Veillard85349052001-04-20 13:48:21 +00004813 }
4814
4815 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004816 * save the second branch 'or' branch
4817 */
4818 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004819 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4820 OCCURS, ROLLBACK_OR) < 0)
4821 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004822 DEPTH++;
4823 CONT = CONT->c1;
4824 goto cont;
4825 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004826 /*
4827 * Small optimization.
4828 */
4829 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4830 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4831 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4832 if ((NODE == NULL) ||
4833 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4834 DEPTH++;
4835 CONT = CONT->c2;
4836 goto cont;
4837 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004838 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4839 ret = (CONT->c1->prefix == NULL);
4840 } else if (CONT->c1->prefix == NULL) {
4841 ret = 0;
4842 } else {
4843 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4844 }
4845 if (ret == 0) {
4846 DEPTH++;
4847 CONT = CONT->c2;
4848 goto cont;
4849 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004850 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004851 DEPTH++;
4852 CONT = CONT->c1;
4853 goto cont;
4854 }
4855
4856 /*
4857 * At this point handle going up in the tree
4858 */
4859 if (ret == -1) {
4860 DEBUG_VALID_MSG("error found returning");
4861 return(ret);
4862 }
4863analyze:
4864 while (CONT != NULL) {
4865 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004866 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004867 * this level.
4868 */
4869 if (ret == 0) {
4870 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004871 xmlNodePtr cur;
4872
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004873 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004874 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004875 DEBUG_VALID_MSG("Once branch failed, rollback");
4876 if (vstateVPop(ctxt) < 0 ) {
4877 DEBUG_VALID_MSG("exhaustion, failed");
4878 return(0);
4879 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004880 if (cur != ctxt->vstate->node)
4881 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004882 goto cont;
4883 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004884 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004885 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004886 DEBUG_VALID_MSG("Plus branch failed, rollback");
4887 if (vstateVPop(ctxt) < 0 ) {
4888 DEBUG_VALID_MSG("exhaustion, failed");
4889 return(0);
4890 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004891 if (cur != ctxt->vstate->node)
4892 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004893 goto cont;
4894 }
4895 DEBUG_VALID_MSG("Plus branch found");
4896 ret = 1;
4897 break;
4898 case XML_ELEMENT_CONTENT_MULT:
4899#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004900 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004901 DEBUG_VALID_MSG("Mult branch failed");
4902 } else {
4903 DEBUG_VALID_MSG("Mult branch found");
4904 }
4905#endif
4906 ret = 1;
4907 break;
4908 case XML_ELEMENT_CONTENT_OPT:
4909 DEBUG_VALID_MSG("Option branch failed");
4910 ret = 1;
4911 break;
4912 }
4913 } else {
4914 switch (CONT->ocur) {
4915 case XML_ELEMENT_CONTENT_OPT:
4916 DEBUG_VALID_MSG("Option branch succeeded");
4917 ret = 1;
4918 break;
4919 case XML_ELEMENT_CONTENT_ONCE:
4920 DEBUG_VALID_MSG("Once branch succeeded");
4921 ret = 1;
4922 break;
4923 case XML_ELEMENT_CONTENT_PLUS:
4924 if (STATE == ROLLBACK_PARENT) {
4925 DEBUG_VALID_MSG("Plus branch rollback");
4926 ret = 1;
4927 break;
4928 }
4929 if (NODE == NULL) {
4930 DEBUG_VALID_MSG("Plus branch exhausted");
4931 ret = 1;
4932 break;
4933 }
4934 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004935 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004936 goto cont;
4937 case XML_ELEMENT_CONTENT_MULT:
4938 if (STATE == ROLLBACK_PARENT) {
4939 DEBUG_VALID_MSG("Mult branch rollback");
4940 ret = 1;
4941 break;
4942 }
4943 if (NODE == NULL) {
4944 DEBUG_VALID_MSG("Mult branch exhausted");
4945 ret = 1;
4946 break;
4947 }
4948 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004949 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004950 goto cont;
4951 }
4952 }
4953 STATE = 0;
4954
4955 /*
4956 * Then act accordingly at the parent level
4957 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004958 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004959 if (CONT->parent == NULL)
4960 break;
4961
4962 switch (CONT->parent->type) {
4963 case XML_ELEMENT_CONTENT_PCDATA:
4964 DEBUG_VALID_MSG("Error: parent pcdata");
4965 return(-1);
4966 case XML_ELEMENT_CONTENT_ELEMENT:
4967 DEBUG_VALID_MSG("Error: parent element");
4968 return(-1);
4969 case XML_ELEMENT_CONTENT_OR:
4970 if (ret == 1) {
4971 DEBUG_VALID_MSG("Or succeeded");
4972 CONT = CONT->parent;
4973 DEPTH--;
4974 } else {
4975 DEBUG_VALID_MSG("Or failed");
4976 CONT = CONT->parent;
4977 DEPTH--;
4978 }
4979 break;
4980 case XML_ELEMENT_CONTENT_SEQ:
4981 if (ret == 0) {
4982 DEBUG_VALID_MSG("Sequence failed");
4983 CONT = CONT->parent;
4984 DEPTH--;
4985 } else if (CONT == CONT->parent->c1) {
4986 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4987 CONT = CONT->parent->c2;
4988 goto cont;
4989 } else {
4990 DEBUG_VALID_MSG("Sequence succeeded");
4991 CONT = CONT->parent;
4992 DEPTH--;
4993 }
4994 }
4995 }
4996 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004997 xmlNodePtr cur;
4998
4999 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005000 DEBUG_VALID_MSG("Failed, remaining input, rollback");
5001 if (vstateVPop(ctxt) < 0 ) {
5002 DEBUG_VALID_MSG("exhaustion, failed");
5003 return(0);
5004 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005005 if (cur != ctxt->vstate->node)
5006 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005007 goto cont;
5008 }
5009 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005010 xmlNodePtr cur;
5011
5012 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005013 DEBUG_VALID_MSG("Failure, rollback");
5014 if (vstateVPop(ctxt) < 0 ) {
5015 DEBUG_VALID_MSG("exhaustion, failed");
5016 return(0);
5017 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005018 if (cur != ctxt->vstate->node)
5019 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005020 goto cont;
5021 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005022 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005023}
Daniel Veillard23e73572002-09-19 19:56:43 +00005024#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005025
5026/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00005027 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005028 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00005029 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005030 * @content: An element
5031 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
5032 *
5033 * This will dump the list of elements to the buffer
5034 * Intended just for the debug routine
5035 */
5036static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00005037xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005038 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00005039 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005040
5041 if (node == NULL) return;
5042 if (glob) strcat(buf, "(");
5043 cur = node;
5044 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005045 len = strlen(buf);
5046 if (size - len < 50) {
5047 if ((size - len > 4) && (buf[len - 1] != '.'))
5048 strcat(buf, " ...");
5049 return;
5050 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005051 switch (cur->type) {
5052 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005053 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005054 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005055 if ((size - len > 4) && (buf[len - 1] != '.'))
5056 strcat(buf, " ...");
5057 return;
5058 }
5059 strcat(buf, (char *) cur->ns->prefix);
5060 strcat(buf, ":");
5061 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005062 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005063 if ((size - len > 4) && (buf[len - 1] != '.'))
5064 strcat(buf, " ...");
5065 return;
5066 }
5067 strcat(buf, (char *) cur->name);
5068 if (cur->next != NULL)
5069 strcat(buf, " ");
5070 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005071 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005072 if (xmlIsBlankNode(cur))
5073 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005074 case XML_CDATA_SECTION_NODE:
5075 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005076 strcat(buf, "CDATA");
5077 if (cur->next != NULL)
5078 strcat(buf, " ");
5079 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005080 case XML_ATTRIBUTE_NODE:
5081 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005082#ifdef LIBXML_DOCB_ENABLED
5083 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005084#endif
5085 case XML_HTML_DOCUMENT_NODE:
5086 case XML_DOCUMENT_TYPE_NODE:
5087 case XML_DOCUMENT_FRAG_NODE:
5088 case XML_NOTATION_NODE:
5089 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005090 strcat(buf, "???");
5091 if (cur->next != NULL)
5092 strcat(buf, " ");
5093 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005094 case XML_ENTITY_NODE:
5095 case XML_PI_NODE:
5096 case XML_DTD_NODE:
5097 case XML_COMMENT_NODE:
5098 case XML_ELEMENT_DECL:
5099 case XML_ATTRIBUTE_DECL:
5100 case XML_ENTITY_DECL:
5101 case XML_XINCLUDE_START:
5102 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005103 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005104 }
5105 cur = cur->next;
5106 }
5107 if (glob) strcat(buf, ")");
5108}
5109
5110/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005111 * xmlValidateElementContent:
5112 * @ctxt: the validation context
5113 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005114 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005115 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005116 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005117 *
5118 * Try to validate the content model of an element
5119 *
5120 * returns 1 if valid or 0 if not and -1 in case of error
5121 */
5122
5123static int
5124xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005125 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00005126 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00005127#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00005128 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00005129#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00005130 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005131 xmlElementContentPtr cont;
5132 const xmlChar *name;
5133
5134 if (elemDecl == NULL)
5135 return(-1);
5136 cont = elemDecl->content;
5137 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005138
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005139#ifdef LIBXML_REGEXP_ENABLED
5140 /* Build the regexp associated to the content model */
5141 if (elemDecl->contModel == NULL)
5142 ret = xmlValidBuildContentModel(ctxt, elemDecl);
5143 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005144 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005145 } else {
5146 xmlRegExecCtxtPtr exec;
5147
Daniel Veillardec498e12003-02-05 11:01:50 +00005148 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
5149 return(-1);
5150 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005151 ctxt->nodeMax = 0;
5152 ctxt->nodeNr = 0;
5153 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005154 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
5155 if (exec != NULL) {
5156 cur = child;
5157 while (cur != NULL) {
5158 switch (cur->type) {
5159 case XML_ENTITY_REF_NODE:
5160 /*
5161 * Push the current node to be able to roll back
5162 * and process within the entity
5163 */
5164 if ((cur->children != NULL) &&
5165 (cur->children->children != NULL)) {
5166 nodeVPush(ctxt, cur);
5167 cur = cur->children->children;
5168 continue;
5169 }
5170 break;
5171 case XML_TEXT_NODE:
5172 if (xmlIsBlankNode(cur))
5173 break;
5174 ret = 0;
5175 goto fail;
5176 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00005177 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005178 ret = 0;
5179 goto fail;
5180 case XML_ELEMENT_NODE:
5181 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005182 xmlChar fn[50];
5183 xmlChar *fullname;
5184
5185 fullname = xmlBuildQName(cur->name,
5186 cur->ns->prefix, fn, 50);
5187 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005188 ret = -1;
5189 goto fail;
5190 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005191 ret = xmlRegExecPushString(exec, fullname, NULL);
5192 if ((fullname != fn) && (fullname != cur->name))
5193 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005194 } else {
5195 ret = xmlRegExecPushString(exec, cur->name, NULL);
5196 }
5197 break;
5198 default:
5199 break;
5200 }
5201 /*
5202 * Switch to next element
5203 */
5204 cur = cur->next;
5205 while (cur == NULL) {
5206 cur = nodeVPop(ctxt);
5207 if (cur == NULL)
5208 break;
5209 cur = cur->next;
5210 }
5211 }
5212 ret = xmlRegExecPushString(exec, NULL, NULL);
5213fail:
5214 xmlRegFreeExecCtxt(exec);
5215 }
5216 }
5217#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005218 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005219 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005220 */
5221 ctxt->vstateMax = 8;
5222 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5223 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5224 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005225 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005226 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005227 }
5228 /*
5229 * The first entry in the stack is reserved to the current state
5230 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00005231 ctxt->nodeMax = 0;
5232 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00005233 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005234 ctxt->vstate = &ctxt->vstateTab[0];
5235 ctxt->vstateNr = 1;
5236 CONT = cont;
5237 NODE = child;
5238 DEPTH = 0;
5239 OCCURS = 0;
5240 STATE = 0;
5241 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005242 if ((ret == -3) && (warn)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005243 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
5244 "Content model for Element %s is ambiguous\n",
Daniel Veillard0cc72772003-10-13 14:00:21 +00005245 name, NULL, NULL);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005246 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005247 /*
5248 * An entities reference appeared at this level.
5249 * Buid a minimal representation of this node content
5250 * sufficient to run the validation process on it
5251 */
5252 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005253 cur = child;
5254 while (cur != NULL) {
5255 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005256 case XML_ENTITY_REF_NODE:
5257 /*
5258 * Push the current node to be able to roll back
5259 * and process within the entity
5260 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005261 if ((cur->children != NULL) &&
5262 (cur->children->children != NULL)) {
5263 nodeVPush(ctxt, cur);
5264 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005265 continue;
5266 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00005267 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005268 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005269 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005270 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005271 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005272 case XML_CDATA_SECTION_NODE:
5273 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005274 case XML_ELEMENT_NODE:
5275 /*
5276 * Allocate a new node and minimally fills in
5277 * what's required
5278 */
5279 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5280 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005281 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005282 xmlFreeNodeList(repl);
5283 ret = -1;
5284 goto done;
5285 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005286 tmp->type = cur->type;
5287 tmp->name = cur->name;
5288 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005289 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00005290 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005291 if (repl == NULL)
5292 repl = last = tmp;
5293 else {
5294 last->next = tmp;
5295 last = tmp;
5296 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005297 if (cur->type == XML_CDATA_SECTION_NODE) {
5298 /*
5299 * E59 spaces in CDATA does not match the
5300 * nonterminal S
5301 */
5302 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5303 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005304 break;
5305 default:
5306 break;
5307 }
5308 /*
5309 * Switch to next element
5310 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005311 cur = cur->next;
5312 while (cur == NULL) {
5313 cur = nodeVPop(ctxt);
5314 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005315 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005316 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005317 }
5318 }
5319
5320 /*
5321 * Relaunch the validation
5322 */
5323 ctxt->vstate = &ctxt->vstateTab[0];
5324 ctxt->vstateNr = 1;
5325 CONT = cont;
5326 NODE = repl;
5327 DEPTH = 0;
5328 OCCURS = 0;
5329 STATE = 0;
5330 ret = xmlValidateElementType(ctxt);
5331 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005332#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005333 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005334 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
5335 char expr[5000];
5336 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005337
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005338 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005339 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005340 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00005341#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005342 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005343 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005344 else
Daniel Veillard01992e02002-10-09 10:20:30 +00005345#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005346 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005347
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005348 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005349 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5350 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5351 name, BAD_CAST expr, BAD_CAST list);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005352 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005353 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5354 "Element content does not follow the DTD, expecting %s, got %s\n",
5355 BAD_CAST expr, BAD_CAST list, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005356 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005357 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005358 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005359 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005360 "Element %s content does not follow the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005361 name, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005362 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005363 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5364 "Element content does not follow the DTD\n",
5365 NULL, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005366 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005367 }
5368 ret = 0;
5369 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005370 if (ret == -3)
5371 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005372
Daniel Veillard23e73572002-09-19 19:56:43 +00005373#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005374done:
5375 /*
5376 * Deallocate the copy if done, and free up the validation stack
5377 */
5378 while (repl != NULL) {
5379 tmp = repl->next;
5380 xmlFree(repl);
5381 repl = tmp;
5382 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005383 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005384 if (ctxt->vstateTab != NULL) {
5385 xmlFree(ctxt->vstateTab);
5386 ctxt->vstateTab = NULL;
5387 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005388#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005389 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00005390 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005391 if (ctxt->nodeTab != NULL) {
5392 xmlFree(ctxt->nodeTab);
5393 ctxt->nodeTab = NULL;
5394 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005395 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005396
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005397}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005398
Owen Taylor3473f882001-02-23 17:55:21 +00005399/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005400 * xmlValidateCdataElement:
5401 * @ctxt: the validation context
5402 * @doc: a document instance
5403 * @elem: an element instance
5404 *
5405 * Check that an element follows #CDATA
5406 *
5407 * returns 1 if valid or 0 otherwise
5408 */
5409static int
5410xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5411 xmlNodePtr elem) {
5412 int ret = 1;
5413 xmlNodePtr cur, child;
5414
Daniel Veillardceb09b92002-10-04 11:46:37 +00005415 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005416 return(0);
5417
5418 child = elem->children;
5419
5420 cur = child;
5421 while (cur != NULL) {
5422 switch (cur->type) {
5423 case XML_ENTITY_REF_NODE:
5424 /*
5425 * Push the current node to be able to roll back
5426 * and process within the entity
5427 */
5428 if ((cur->children != NULL) &&
5429 (cur->children->children != NULL)) {
5430 nodeVPush(ctxt, cur);
5431 cur = cur->children->children;
5432 continue;
5433 }
5434 break;
5435 case XML_COMMENT_NODE:
5436 case XML_PI_NODE:
5437 case XML_TEXT_NODE:
5438 case XML_CDATA_SECTION_NODE:
5439 break;
5440 default:
5441 ret = 0;
5442 goto done;
5443 }
5444 /*
5445 * Switch to next element
5446 */
5447 cur = cur->next;
5448 while (cur == NULL) {
5449 cur = nodeVPop(ctxt);
5450 if (cur == NULL)
5451 break;
5452 cur = cur->next;
5453 }
5454 }
5455done:
5456 ctxt->nodeMax = 0;
5457 ctxt->nodeNr = 0;
5458 if (ctxt->nodeTab != NULL) {
5459 xmlFree(ctxt->nodeTab);
5460 ctxt->nodeTab = NULL;
5461 }
5462 return(ret);
5463}
5464
5465/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005466 * xmlValidateCheckMixed:
5467 * @ctxt: the validation context
5468 * @cont: the mixed content model
5469 * @qname: the qualified name as appearing in the serialization
5470 *
5471 * Check if the given node is part of the content model.
5472 *
5473 * Returns 1 if yes, 0 if no, -1 in case of error
5474 */
5475static int
William M. Brackedb65a72004-02-06 07:36:04 +00005476xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005477 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005478 const xmlChar *name;
5479 int plen;
5480 name = xmlSplitQName3(qname, &plen);
5481
5482 if (name == NULL) {
5483 while (cont != NULL) {
5484 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5485 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5486 return(1);
5487 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5488 (cont->c1 != NULL) &&
5489 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5490 if ((cont->c1->prefix == NULL) &&
5491 (xmlStrEqual(cont->c1->name, qname)))
5492 return(1);
5493 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5494 (cont->c1 == NULL) ||
5495 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005496 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5497 "Internal: MIXED struct corrupted\n",
5498 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005499 break;
5500 }
5501 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005502 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005503 } else {
5504 while (cont != NULL) {
5505 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5506 if ((cont->prefix != NULL) &&
5507 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5508 (xmlStrEqual(cont->name, name)))
5509 return(1);
5510 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5511 (cont->c1 != NULL) &&
5512 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5513 if ((cont->c1->prefix != NULL) &&
5514 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5515 (xmlStrEqual(cont->c1->name, name)))
5516 return(1);
5517 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5518 (cont->c1 == NULL) ||
5519 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005520 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5521 "Internal: MIXED struct corrupted\n",
5522 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005523 break;
5524 }
5525 cont = cont->c2;
5526 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005527 }
5528 return(0);
5529}
5530
5531/**
5532 * xmlValidGetElemDecl:
5533 * @ctxt: the validation context
5534 * @doc: a document instance
5535 * @elem: an element instance
5536 * @extsubset: pointer, (out) indicate if the declaration was found
5537 * in the external subset.
5538 *
5539 * Finds a declaration associated to an element in the document.
5540 *
5541 * returns the pointer to the declaration or NULL if not found.
5542 */
5543static xmlElementPtr
5544xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5545 xmlNodePtr elem, int *extsubset) {
5546 xmlElementPtr elemDecl = NULL;
5547 const xmlChar *prefix = NULL;
5548
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005549 if ((ctxt == NULL) || (doc == NULL) ||
5550 (elem == NULL) || (elem->name == NULL))
5551 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005552 if (extsubset != NULL)
5553 *extsubset = 0;
5554
5555 /*
5556 * Fetch the declaration for the qualified name
5557 */
5558 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5559 prefix = elem->ns->prefix;
5560
5561 if (prefix != NULL) {
5562 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5563 elem->name, prefix);
5564 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5565 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5566 elem->name, prefix);
5567 if ((elemDecl != NULL) && (extsubset != NULL))
5568 *extsubset = 1;
5569 }
5570 }
5571
5572 /*
5573 * Fetch the declaration for the non qualified name
5574 * This is "non-strict" validation should be done on the
5575 * full QName but in that case being flexible makes sense.
5576 */
5577 if (elemDecl == NULL) {
5578 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5579 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5580 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5581 if ((elemDecl != NULL) && (extsubset != NULL))
5582 *extsubset = 1;
5583 }
5584 }
5585 if (elemDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005586 xmlErrValidNode(ctxt, elem,
5587 XML_DTD_UNKNOWN_ELEM,
5588 "No declaration for element %s\n",
5589 elem->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005590 }
5591 return(elemDecl);
5592}
5593
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005594#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005595/**
5596 * xmlValidatePushElement:
5597 * @ctxt: the validation context
5598 * @doc: a document instance
5599 * @elem: an element instance
5600 * @qname: the qualified name as appearing in the serialization
5601 *
5602 * Push a new element start on the validation stack.
5603 *
5604 * returns 1 if no validation problem was found or 0 otherwise
5605 */
5606int
5607xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5608 xmlNodePtr elem, const xmlChar *qname) {
5609 int ret = 1;
5610 xmlElementPtr eDecl;
5611 int extsubset = 0;
5612
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005613 if (ctxt == NULL)
5614 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005615/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005616 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5617 xmlValidStatePtr state = ctxt->vstate;
5618 xmlElementPtr elemDecl;
5619
5620 /*
5621 * Check the new element agaisnt the content model of the new elem.
5622 */
5623 if (state->elemDecl != NULL) {
5624 elemDecl = state->elemDecl;
5625
5626 switch(elemDecl->etype) {
5627 case XML_ELEMENT_TYPE_UNDEFINED:
5628 ret = 0;
5629 break;
5630 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005631 xmlErrValidNode(ctxt, state->node,
5632 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005633 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005634 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005635 ret = 0;
5636 break;
5637 case XML_ELEMENT_TYPE_ANY:
5638 /* I don't think anything is required then */
5639 break;
5640 case XML_ELEMENT_TYPE_MIXED:
5641 /* simple case of declared as #PCDATA */
5642 if ((elemDecl->content != NULL) &&
5643 (elemDecl->content->type ==
5644 XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005645 xmlErrValidNode(ctxt, state->node,
5646 XML_DTD_NOT_PCDATA,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005647 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005648 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005649 ret = 0;
5650 } else {
5651 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5652 qname);
5653 if (ret != 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005654 xmlErrValidNode(ctxt, state->node,
5655 XML_DTD_INVALID_CHILD,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005656 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005657 qname, state->node->name, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005658 }
5659 }
5660 break;
5661 case XML_ELEMENT_TYPE_ELEMENT:
5662 /*
5663 * TODO:
5664 * VC: Standalone Document Declaration
5665 * - element types with element content, if white space
5666 * occurs directly within any instance of those types.
5667 */
5668 if (state->exec != NULL) {
5669 ret = xmlRegExecPushString(state->exec, qname, NULL);
5670 if (ret < 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005671 xmlErrValidNode(ctxt, state->node,
5672 XML_DTD_CONTENT_MODEL,
5673 "Element %s content does not follow the DTD, Misplaced %s\n",
5674 state->node->name, qname, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005675 ret = 0;
5676 } else {
5677 ret = 1;
5678 }
5679 }
5680 break;
5681 }
5682 }
5683 }
5684 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5685 vstateVPush(ctxt, eDecl, elem);
5686 return(ret);
5687}
5688
5689/**
5690 * xmlValidatePushCData:
5691 * @ctxt: the validation context
5692 * @data: some character data read
5693 * @len: the lenght of the data
5694 *
5695 * check the CData parsed for validation in the current stack
5696 *
5697 * returns 1 if no validation problem was found or 0 otherwise
5698 */
5699int
5700xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5701 int ret = 1;
5702
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005703/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005704 if (ctxt == NULL)
5705 return(0);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005706 if (len <= 0)
5707 return(ret);
5708 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5709 xmlValidStatePtr state = ctxt->vstate;
5710 xmlElementPtr elemDecl;
5711
5712 /*
5713 * Check the new element agaisnt the content model of the new elem.
5714 */
5715 if (state->elemDecl != NULL) {
5716 elemDecl = state->elemDecl;
5717
5718 switch(elemDecl->etype) {
5719 case XML_ELEMENT_TYPE_UNDEFINED:
5720 ret = 0;
5721 break;
5722 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005723 xmlErrValidNode(ctxt, state->node,
5724 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005725 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005726 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005727 ret = 0;
5728 break;
5729 case XML_ELEMENT_TYPE_ANY:
5730 break;
5731 case XML_ELEMENT_TYPE_MIXED:
5732 break;
5733 case XML_ELEMENT_TYPE_ELEMENT:
5734 if (len > 0) {
5735 int i;
5736
5737 for (i = 0;i < len;i++) {
William M. Brack76e95df2003-10-18 16:20:14 +00005738 if (!IS_BLANK_CH(data[i])) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005739 xmlErrValidNode(ctxt, state->node,
5740 XML_DTD_CONTENT_MODEL,
5741 "Element %s content does not follow the DTD, Text not allowed\n",
5742 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005743 ret = 0;
5744 goto done;
5745 }
5746 }
5747 /*
5748 * TODO:
5749 * VC: Standalone Document Declaration
5750 * element types with element content, if white space
5751 * occurs directly within any instance of those types.
5752 */
5753 }
5754 break;
5755 }
5756 }
5757 }
5758done:
5759 return(ret);
5760}
5761
5762/**
5763 * xmlValidatePopElement:
5764 * @ctxt: the validation context
5765 * @doc: a document instance
5766 * @elem: an element instance
5767 * @qname: the qualified name as appearing in the serialization
5768 *
5769 * Pop the element end from the validation stack.
5770 *
5771 * returns 1 if no validation problem was found or 0 otherwise
5772 */
5773int
5774xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005775 xmlNodePtr elem ATTRIBUTE_UNUSED,
5776 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005777 int ret = 1;
5778
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005779 if (ctxt == NULL)
5780 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005781/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005782 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5783 xmlValidStatePtr state = ctxt->vstate;
5784 xmlElementPtr elemDecl;
5785
5786 /*
5787 * Check the new element agaisnt the content model of the new elem.
5788 */
5789 if (state->elemDecl != NULL) {
5790 elemDecl = state->elemDecl;
5791
5792 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5793 if (state->exec != NULL) {
5794 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5795 if (ret == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005796 xmlErrValidNode(ctxt, state->node,
5797 XML_DTD_CONTENT_MODEL,
5798 "Element %s content does not follow the DTD, Expecting more child\n",
5799 state->node->name, NULL,NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005800 } else {
5801 /*
5802 * previous validation errors should not generate
5803 * a new one here
5804 */
5805 ret = 1;
5806 }
5807 }
5808 }
5809 }
5810 vstateVPop(ctxt);
5811 }
5812 return(ret);
5813}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005814#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005815
5816/**
Owen Taylor3473f882001-02-23 17:55:21 +00005817 * xmlValidateOneElement:
5818 * @ctxt: the validation context
5819 * @doc: a document instance
5820 * @elem: an element instance
5821 *
5822 * Try to validate a single element and it's attributes,
5823 * basically it does the following checks as described by the
5824 * XML-1.0 recommendation:
5825 * - [ VC: Element Valid ]
5826 * - [ VC: Required Attribute ]
5827 * Then call xmlValidateOneAttribute() for each attribute present.
5828 *
5829 * The ID/IDREF checkings are done separately
5830 *
5831 * returns 1 if valid or 0 otherwise
5832 */
5833
5834int
5835xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5836 xmlNodePtr elem) {
5837 xmlElementPtr elemDecl = NULL;
5838 xmlElementContentPtr cont;
5839 xmlAttributePtr attr;
5840 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005841 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005842 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005843 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005844
5845 CHECK_DTD;
5846
5847 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005848 switch (elem->type) {
5849 case XML_ATTRIBUTE_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005850 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5851 "Attribute element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005852 return(0);
5853 case XML_TEXT_NODE:
5854 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005855 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5856 "Text element has children !\n",
5857 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005858 return(0);
5859 }
5860 if (elem->properties != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005861 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5862 "Text element has attribute !\n",
5863 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005864 return(0);
5865 }
5866 if (elem->ns != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005867 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5868 "Text element has namespace !\n",
5869 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005870 return(0);
5871 }
5872 if (elem->nsDef != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005873 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5874 "Text element has namespace !\n",
5875 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005876 return(0);
5877 }
5878 if (elem->content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005879 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5880 "Text element has no content !\n",
5881 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005882 return(0);
5883 }
5884 return(1);
5885 case XML_XINCLUDE_START:
5886 case XML_XINCLUDE_END:
5887 return(1);
5888 case XML_CDATA_SECTION_NODE:
5889 case XML_ENTITY_REF_NODE:
5890 case XML_PI_NODE:
5891 case XML_COMMENT_NODE:
5892 return(1);
5893 case XML_ENTITY_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005894 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5895 "Entity element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005896 return(0);
5897 case XML_NOTATION_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005898 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5899 "Notation element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005900 return(0);
5901 case XML_DOCUMENT_NODE:
5902 case XML_DOCUMENT_TYPE_NODE:
5903 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005904 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5905 "Document element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005906 return(0);
5907 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005908 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5909 "HTML Document not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005910 return(0);
5911 case XML_ELEMENT_NODE:
5912 break;
5913 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005914 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5915 "unknown element type\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005916 return(0);
5917 }
Owen Taylor3473f882001-02-23 17:55:21 +00005918
5919 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005920 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005921 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005922 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5923 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005924 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005925
Daniel Veillardea7751d2002-12-20 00:16:24 +00005926 /*
5927 * If vstateNr is not zero that means continuous validation is
5928 * activated, do not try to check the content model at that level.
5929 */
5930 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005931 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005932 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005933 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005934 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
5935 "No declaration for element %s\n",
5936 elem->name, NULL, NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005937 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005938 case XML_ELEMENT_TYPE_EMPTY:
5939 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005940 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
Owen Taylor3473f882001-02-23 17:55:21 +00005941 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005942 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005943 ret = 0;
5944 }
5945 break;
5946 case XML_ELEMENT_TYPE_ANY:
5947 /* I don't think anything is required then */
5948 break;
5949 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005950
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005951 /* simple case of declared as #PCDATA */
5952 if ((elemDecl->content != NULL) &&
5953 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5954 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5955 if (!ret) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005956 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005957 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005958 elem->name, NULL, NULL);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005959 }
5960 break;
5961 }
Owen Taylor3473f882001-02-23 17:55:21 +00005962 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005963 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005964 while (child != NULL) {
5965 if (child->type == XML_ELEMENT_NODE) {
5966 name = child->name;
5967 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005968 xmlChar fn[50];
5969 xmlChar *fullname;
5970
5971 fullname = xmlBuildQName(child->name, child->ns->prefix,
5972 fn, 50);
5973 if (fullname == NULL)
5974 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005975 cont = elemDecl->content;
5976 while (cont != NULL) {
5977 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005978 if (xmlStrEqual(cont->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_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005983 if (xmlStrEqual(cont->c1->name, fullname))
5984 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005985 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5986 (cont->c1 == NULL) ||
5987 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005988 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5989 "Internal: MIXED struct corrupted\n",
5990 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005991 break;
5992 }
5993 cont = cont->c2;
5994 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005995 if ((fullname != fn) && (fullname != child->name))
5996 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005997 if (cont != NULL)
5998 goto child_ok;
5999 }
6000 cont = elemDecl->content;
6001 while (cont != NULL) {
6002 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
6003 if (xmlStrEqual(cont->name, name)) break;
6004 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
6005 (cont->c1 != NULL) &&
6006 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
6007 if (xmlStrEqual(cont->c1->name, name)) break;
6008 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6009 (cont->c1 == NULL) ||
6010 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006011 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
6012 "Internal: MIXED struct corrupted\n",
6013 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006014 break;
6015 }
6016 cont = cont->c2;
6017 }
6018 if (cont == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006019 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006020 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006021 name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006022 ret = 0;
6023 }
6024 }
6025child_ok:
6026 child = child->next;
6027 }
6028 break;
6029 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006030 if ((doc->standalone == 1) && (extsubset == 1)) {
6031 /*
6032 * VC: Standalone Document Declaration
6033 * - element types with element content, if white space
6034 * occurs directly within any instance of those types.
6035 */
6036 child = elem->children;
6037 while (child != NULL) {
6038 if (child->type == XML_TEXT_NODE) {
6039 const xmlChar *content = child->content;
6040
William M. Brack76e95df2003-10-18 16:20:14 +00006041 while (IS_BLANK_CH(*content))
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006042 content++;
6043 if (*content == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006044 xmlErrValidNode(ctxt, elem,
6045 XML_DTD_STANDALONE_WHITE_SPACE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006046"standalone: %s declared in the external subset contains white spaces nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006047 elem->name, NULL, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006048 ret = 0;
6049 break;
6050 }
6051 }
6052 child =child->next;
6053 }
6054 }
Owen Taylor3473f882001-02-23 17:55:21 +00006055 child = elem->children;
6056 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00006057 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006058 if (tmp <= 0)
6059 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00006060 break;
6061 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00006062 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00006063
6064 /* [ VC: Required Attribute ] */
6065 attr = elemDecl->attributes;
6066 while (attr != NULL) {
6067 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00006068 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006069
Daniel Veillarde4301c82002-02-13 13:32:35 +00006070 if ((attr->prefix == NULL) &&
6071 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6072 xmlNsPtr ns;
6073
6074 ns = elem->nsDef;
6075 while (ns != NULL) {
6076 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006077 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006078 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006079 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006080 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6081 xmlNsPtr ns;
6082
6083 ns = elem->nsDef;
6084 while (ns != NULL) {
6085 if (xmlStrEqual(attr->name, ns->prefix))
6086 goto found;
6087 ns = ns->next;
6088 }
6089 } else {
6090 xmlAttrPtr attrib;
6091
6092 attrib = elem->properties;
6093 while (attrib != NULL) {
6094 if (xmlStrEqual(attrib->name, attr->name)) {
6095 if (attr->prefix != NULL) {
6096 xmlNsPtr nameSpace = attrib->ns;
6097
6098 if (nameSpace == NULL)
6099 nameSpace = elem->ns;
6100 /*
6101 * qualified names handling is problematic, having a
6102 * different prefix should be possible but DTDs don't
6103 * allow to define the URI instead of the prefix :-(
6104 */
6105 if (nameSpace == NULL) {
6106 if (qualified < 0)
6107 qualified = 0;
6108 } else if (!xmlStrEqual(nameSpace->prefix,
6109 attr->prefix)) {
6110 if (qualified < 1)
6111 qualified = 1;
6112 } else
6113 goto found;
6114 } else {
6115 /*
6116 * We should allow applications to define namespaces
6117 * for their application even if the DTD doesn't
6118 * carry one, otherwise, basically we would always
6119 * break.
6120 */
6121 goto found;
6122 }
6123 }
6124 attrib = attrib->next;
6125 }
Owen Taylor3473f882001-02-23 17:55:21 +00006126 }
6127 if (qualified == -1) {
6128 if (attr->prefix == NULL) {
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\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006131 elem->name, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006132 ret = 0;
6133 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006134 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006135 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00006136 elem->name, attr->prefix,attr->name);
6137 ret = 0;
6138 }
6139 } else if (qualified == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006140 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006141 "Element %s required attribute %s:%s has no prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006142 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006143 } else if (qualified == 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006144 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006145 "Element %s required attribute %s:%s has different prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006146 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006147 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006148 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
6149 /*
6150 * Special tests checking #FIXED namespace declarations
6151 * have the right value since this is not done as an
6152 * attribute checking
6153 */
6154 if ((attr->prefix == NULL) &&
6155 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6156 xmlNsPtr ns;
6157
6158 ns = elem->nsDef;
6159 while (ns != NULL) {
6160 if (ns->prefix == NULL) {
6161 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006162 xmlErrValidNode(ctxt, elem,
6163 XML_DTD_ELEM_DEFAULT_NAMESPACE,
Daniel Veillarde4301c82002-02-13 13:32:35 +00006164 "Element %s namespace name for default namespace does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006165 elem->name, NULL, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006166 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006167 }
6168 goto found;
6169 }
6170 ns = ns->next;
6171 }
6172 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6173 xmlNsPtr ns;
6174
6175 ns = elem->nsDef;
6176 while (ns != NULL) {
6177 if (xmlStrEqual(attr->name, ns->prefix)) {
6178 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006179 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006180 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006181 elem->name, ns->prefix, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006182 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006183 }
6184 goto found;
6185 }
6186 ns = ns->next;
6187 }
6188 }
Owen Taylor3473f882001-02-23 17:55:21 +00006189 }
6190found:
6191 attr = attr->nexth;
6192 }
6193 return(ret);
6194}
6195
6196/**
6197 * xmlValidateRoot:
6198 * @ctxt: the validation context
6199 * @doc: a document instance
6200 *
6201 * Try to validate a the root element
6202 * basically it does the following check as described by the
6203 * XML-1.0 recommendation:
6204 * - [ VC: Root Element Type ]
6205 * it doesn't try to recurse or apply other check to the element
6206 *
6207 * returns 1 if valid or 0 otherwise
6208 */
6209
6210int
6211xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6212 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00006213 int ret;
6214
Owen Taylor3473f882001-02-23 17:55:21 +00006215 if (doc == NULL) return(0);
6216
6217 root = xmlDocGetRootElement(doc);
6218 if ((root == NULL) || (root->name == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006219 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
6220 "no root element\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006221 return(0);
6222 }
6223
6224 /*
6225 * When doing post validation against a separate DTD, those may
6226 * no internal subset has been generated
6227 */
6228 if ((doc->intSubset != NULL) &&
6229 (doc->intSubset->name != NULL)) {
6230 /*
6231 * Check first the document root against the NQName
6232 */
6233 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
6234 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006235 xmlChar fn[50];
6236 xmlChar *fullname;
6237
6238 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6239 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006240 xmlVErrMemory(ctxt, NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00006241 return(0);
6242 }
6243 ret = xmlStrEqual(doc->intSubset->name, fullname);
6244 if ((fullname != fn) && (fullname != root->name))
6245 xmlFree(fullname);
6246 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00006247 goto name_ok;
6248 }
6249 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6250 (xmlStrEqual(root->name, BAD_CAST "html")))
6251 goto name_ok;
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006252 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6253 "root and DTD name do not match '%s' and '%s'\n",
6254 root->name, doc->intSubset->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006255 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006256 }
6257 }
6258name_ok:
6259 return(1);
6260}
6261
6262
6263/**
6264 * xmlValidateElement:
6265 * @ctxt: the validation context
6266 * @doc: a document instance
6267 * @elem: an element instance
6268 *
6269 * Try to validate the subtree under an element
6270 *
6271 * returns 1 if valid or 0 otherwise
6272 */
6273
6274int
6275xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
6276 xmlNodePtr child;
6277 xmlAttrPtr attr;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006278 xmlNsPtr ns;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006279 const xmlChar *value;
Owen Taylor3473f882001-02-23 17:55:21 +00006280 int ret = 1;
6281
6282 if (elem == NULL) return(0);
6283
6284 /*
6285 * XInclude elements were added after parsing in the infoset,
6286 * they don't really mean anything validation wise.
6287 */
6288 if ((elem->type == XML_XINCLUDE_START) ||
6289 (elem->type == XML_XINCLUDE_END))
6290 return(1);
6291
6292 CHECK_DTD;
6293
Daniel Veillard10ea86c2001-06-20 13:55:33 +00006294 /*
6295 * Entities references have to be handled separately
6296 */
6297 if (elem->type == XML_ENTITY_REF_NODE) {
6298 return(1);
6299 }
6300
Owen Taylor3473f882001-02-23 17:55:21 +00006301 ret &= xmlValidateOneElement(ctxt, doc, elem);
6302 attr = elem->properties;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006303 while (attr != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006304 value = xmlNodeListGetString(doc, attr->children, 0);
6305 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6306 if (value != NULL)
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006307 xmlFree((char *)value);
Owen Taylor3473f882001-02-23 17:55:21 +00006308 attr= attr->next;
6309 }
Daniel Veillarde133dd82003-10-30 10:42:20 +00006310 ns = elem->nsDef;
6311 while (ns != NULL) {
Daniel Veillard1f5c9892003-12-29 17:09:55 +00006312 if (elem->ns == NULL)
6313 ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
6314 ns, ns->href);
6315 else
6316 ret &= xmlValidateOneNamespace(ctxt, doc, elem, elem->ns->prefix,
6317 ns, ns->href);
Daniel Veillarde133dd82003-10-30 10:42:20 +00006318 ns = ns->next;
6319 }
Owen Taylor3473f882001-02-23 17:55:21 +00006320 child = elem->children;
6321 while (child != NULL) {
6322 ret &= xmlValidateElement(ctxt, doc, child);
6323 child = child->next;
6324 }
6325
6326 return(ret);
6327}
6328
Daniel Veillard8730c562001-02-26 10:49:57 +00006329/**
6330 * xmlValidateRef:
6331 * @ref: A reference to be validated
6332 * @ctxt: Validation context
6333 * @name: Name of ID we are searching for
6334 *
6335 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006336static void
Daniel Veillard8730c562001-02-26 10:49:57 +00006337xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006338 const xmlChar *name) {
6339 xmlAttrPtr id;
6340 xmlAttrPtr attr;
6341
6342 if (ref == NULL)
6343 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006344 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006345 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006346 attr = ref->attr;
6347 if (attr == NULL) {
6348 xmlChar *dup, *str = NULL, *cur, save;
6349
6350 dup = xmlStrdup(name);
6351 if (dup == NULL) {
6352 ctxt->valid = 0;
6353 return;
6354 }
6355 cur = dup;
6356 while (*cur != 0) {
6357 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006358 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006359 save = *cur;
6360 *cur = 0;
6361 id = xmlGetID(ctxt->doc, str);
6362 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006363 xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006364 "attribute %s line %d references an unknown ID \"%s\"\n",
6365 ref->name, ref->lineno, str);
6366 ctxt->valid = 0;
6367 }
6368 if (save == 0)
6369 break;
6370 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006371 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006372 }
6373 xmlFree(dup);
6374 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00006375 id = xmlGetID(ctxt->doc, name);
6376 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006377 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006378 "IDREF attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006379 attr->name, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006380 ctxt->valid = 0;
6381 }
6382 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6383 xmlChar *dup, *str = NULL, *cur, save;
6384
6385 dup = xmlStrdup(name);
6386 if (dup == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006387 xmlVErrMemory(ctxt, "IDREFS split");
Owen Taylor3473f882001-02-23 17:55:21 +00006388 ctxt->valid = 0;
6389 return;
6390 }
6391 cur = dup;
6392 while (*cur != 0) {
6393 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006394 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006395 save = *cur;
6396 *cur = 0;
6397 id = xmlGetID(ctxt->doc, str);
6398 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006399 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006400 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006401 attr->name, str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006402 ctxt->valid = 0;
6403 }
6404 if (save == 0)
6405 break;
6406 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006407 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006408 }
6409 xmlFree(dup);
6410 }
6411}
6412
6413/**
Daniel Veillard8730c562001-02-26 10:49:57 +00006414 * xmlWalkValidateList:
6415 * @data: Contents of current link
6416 * @user: Value supplied by the user
6417 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006418 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00006419 */
6420static int
6421xmlWalkValidateList(const void *data, const void *user)
6422{
6423 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6424 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6425 return 1;
6426}
6427
6428/**
6429 * xmlValidateCheckRefCallback:
6430 * @ref_list: List of references
6431 * @ctxt: Validation context
6432 * @name: Name of ID we are searching for
6433 *
6434 */
6435static void
6436xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6437 const xmlChar *name) {
6438 xmlValidateMemo memo;
6439
6440 if (ref_list == NULL)
6441 return;
6442 memo.ctxt = ctxt;
6443 memo.name = name;
6444
6445 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6446
6447}
6448
6449/**
Owen Taylor3473f882001-02-23 17:55:21 +00006450 * xmlValidateDocumentFinal:
6451 * @ctxt: the validation context
6452 * @doc: a document instance
6453 *
6454 * Does the final step for the document validation once all the
6455 * incremental validation steps have been completed
6456 *
6457 * basically it does the following checks described by the XML Rec
6458 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006459 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00006460 *
6461 * returns 1 if valid or 0 otherwise
6462 */
6463
6464int
6465xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6466 xmlRefTablePtr table;
6467
Daniel Veillardc0be74b2004-11-03 19:16:55 +00006468 if (ctxt == NULL)
6469 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006470 if (doc == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006471 xmlErrValid(ctxt, XML_DTD_NO_DOC,
6472 "xmlValidateDocumentFinal: doc == NULL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006473 return(0);
6474 }
6475
6476 /*
6477 * Check all the NOTATION/NOTATIONS attributes
6478 */
6479 /*
6480 * Check all the ENTITY/ENTITIES attributes definition for validity
6481 */
6482 /*
6483 * Check all the IDREF/IDREFS attributes definition for validity
6484 */
6485 table = (xmlRefTablePtr) doc->refs;
6486 ctxt->doc = doc;
6487 ctxt->valid = 1;
6488 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6489 return(ctxt->valid);
6490}
6491
6492/**
6493 * xmlValidateDtd:
6494 * @ctxt: the validation context
6495 * @doc: a document instance
6496 * @dtd: a dtd instance
6497 *
6498 * Try to validate the document against the dtd instance
6499 *
William M. Brack367df6e2004-10-23 18:14:36 +00006500 * Basically it does check all the definitions in the DtD.
6501 * Note the the internal subset (if present) is de-coupled
6502 * (i.e. not used), which could give problems if ID or IDREF
6503 * is present.
Owen Taylor3473f882001-02-23 17:55:21 +00006504 *
6505 * returns 1 if valid or 0 otherwise
6506 */
6507
6508int
6509xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6510 int ret;
William M. Brack367df6e2004-10-23 18:14:36 +00006511 xmlDtdPtr oldExt, oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006512 xmlNodePtr root;
6513
6514 if (dtd == NULL) return(0);
6515 if (doc == NULL) return(0);
6516 oldExt = doc->extSubset;
William M. Brack367df6e2004-10-23 18:14:36 +00006517 oldInt = doc->intSubset;
Owen Taylor3473f882001-02-23 17:55:21 +00006518 doc->extSubset = dtd;
William M. Brack367df6e2004-10-23 18:14:36 +00006519 doc->intSubset = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006520 ret = xmlValidateRoot(ctxt, doc);
6521 if (ret == 0) {
6522 doc->extSubset = oldExt;
William M. Brack367df6e2004-10-23 18:14:36 +00006523 doc->intSubset = oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006524 return(ret);
6525 }
6526 if (doc->ids != NULL) {
6527 xmlFreeIDTable(doc->ids);
6528 doc->ids = NULL;
6529 }
6530 if (doc->refs != NULL) {
6531 xmlFreeRefTable(doc->refs);
6532 doc->refs = NULL;
6533 }
6534 root = xmlDocGetRootElement(doc);
6535 ret = xmlValidateElement(ctxt, doc, root);
6536 ret &= xmlValidateDocumentFinal(ctxt, doc);
6537 doc->extSubset = oldExt;
William M. Brack367df6e2004-10-23 18:14:36 +00006538 doc->intSubset = oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006539 return(ret);
6540}
6541
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006542static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006543xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6544 const xmlChar *name ATTRIBUTE_UNUSED) {
6545 if (cur == NULL)
6546 return;
6547 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6548 xmlChar *notation = cur->content;
6549
Daniel Veillard878eab02002-02-19 13:46:09 +00006550 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006551 int ret;
6552
6553 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6554 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006555 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006556 }
6557 }
6558 }
6559}
6560
6561static void
Owen Taylor3473f882001-02-23 17:55:21 +00006562xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006563 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006564 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006565 xmlDocPtr doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006566 xmlElementPtr elem = NULL;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006567
Owen Taylor3473f882001-02-23 17:55:21 +00006568 if (cur == NULL)
6569 return;
6570 switch (cur->atype) {
6571 case XML_ATTRIBUTE_CDATA:
6572 case XML_ATTRIBUTE_ID:
6573 case XML_ATTRIBUTE_IDREF :
6574 case XML_ATTRIBUTE_IDREFS:
6575 case XML_ATTRIBUTE_NMTOKEN:
6576 case XML_ATTRIBUTE_NMTOKENS:
6577 case XML_ATTRIBUTE_ENUMERATION:
6578 break;
6579 case XML_ATTRIBUTE_ENTITY:
6580 case XML_ATTRIBUTE_ENTITIES:
6581 case XML_ATTRIBUTE_NOTATION:
6582 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006583
6584 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6585 cur->atype, cur->defaultValue);
6586 if ((ret == 0) && (ctxt->valid == 1))
6587 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006588 }
6589 if (cur->tree != NULL) {
6590 xmlEnumerationPtr tree = cur->tree;
6591 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006592 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006593 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006594 if ((ret == 0) && (ctxt->valid == 1))
6595 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006596 tree = tree->next;
6597 }
6598 }
6599 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006600 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6601 doc = cur->doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006602 if (cur->elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006603 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard878eab02002-02-19 13:46:09 +00006604 "xmlValidateAttributeCallback(%s): internal error\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006605 (const char *) cur->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00006606 return;
6607 }
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006608
6609 if (doc != NULL)
6610 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6611 if ((elem == NULL) && (doc != NULL))
Daniel Veillard878eab02002-02-19 13:46:09 +00006612 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006613 if ((elem == NULL) && (cur->parent != NULL) &&
6614 (cur->parent->type == XML_DTD_NODE))
6615 elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00006616 if (elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006617 xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
Daniel Veillard878eab02002-02-19 13:46:09 +00006618 "attribute %s: could not find decl for element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006619 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006620 return;
6621 }
6622 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006623 xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006624 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006625 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006626 ctxt->valid = 0;
6627 }
6628 }
Owen Taylor3473f882001-02-23 17:55:21 +00006629}
6630
6631/**
6632 * xmlValidateDtdFinal:
6633 * @ctxt: the validation context
6634 * @doc: a document instance
6635 *
6636 * Does the final step for the dtds validation once all the
6637 * subsets have been parsed
6638 *
6639 * basically it does the following checks described by the XML Rec
6640 * - check that ENTITY and ENTITIES type attributes default or
6641 * possible values matches one of the defined entities.
6642 * - check that NOTATION type attributes default or
6643 * possible values matches one of the defined notations.
6644 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006645 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006646 */
6647
6648int
6649xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006650 xmlDtdPtr dtd;
6651 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006652 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006653
6654 if (doc == NULL) return(0);
6655 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6656 return(0);
6657 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006658 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006659 dtd = doc->intSubset;
6660 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6661 table = (xmlAttributeTablePtr) dtd->attributes;
6662 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006663 }
6664 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006665 entities = (xmlEntitiesTablePtr) dtd->entities;
6666 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6667 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006668 }
6669 dtd = doc->extSubset;
6670 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6671 table = (xmlAttributeTablePtr) dtd->attributes;
6672 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006673 }
6674 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006675 entities = (xmlEntitiesTablePtr) dtd->entities;
6676 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6677 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006678 }
6679 return(ctxt->valid);
6680}
6681
6682/**
6683 * xmlValidateDocument:
6684 * @ctxt: the validation context
6685 * @doc: a document instance
6686 *
6687 * Try to validate the document instance
6688 *
6689 * basically it does the all the checks described by the XML Rec
6690 * i.e. validates the internal and external subset (if present)
6691 * and validate the document tree.
6692 *
6693 * returns 1 if valid or 0 otherwise
6694 */
6695
6696int
6697xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6698 int ret;
6699 xmlNodePtr root;
6700
Daniel Veillardc0be74b2004-11-03 19:16:55 +00006701 if (doc == NULL)
6702 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006703 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006704 xmlErrValid(ctxt, XML_DTD_NO_DTD,
6705 "no DTD found!\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006706 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006707 }
Owen Taylor3473f882001-02-23 17:55:21 +00006708 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6709 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
William M. Brack8c22f9f2004-08-06 16:23:27 +00006710 xmlChar *sysID;
6711 if (doc->intSubset->SystemID != NULL) {
William M. Brackbebe7302004-08-05 06:46:47 +00006712 sysID = xmlBuildURI(doc->intSubset->SystemID,
6713 doc->URL);
William M. Brack8c22f9f2004-08-06 16:23:27 +00006714 if (sysID == NULL) {
6715 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6716 "Could not build URI for external subset \"%s\"\n",
6717 (const char *) doc->intSubset->SystemID);
6718 return 0;
6719 }
6720 } else
William M. Brackbebe7302004-08-05 06:46:47 +00006721 sysID = NULL;
William M. Brack8c22f9f2004-08-06 16:23:27 +00006722 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
William M. Brackbebe7302004-08-05 06:46:47 +00006723 (const xmlChar *)sysID);
William M. Brackbebe7302004-08-05 06:46:47 +00006724 if (sysID != NULL)
6725 xmlFree(sysID);
Owen Taylor3473f882001-02-23 17:55:21 +00006726 if (doc->extSubset == NULL) {
6727 if (doc->intSubset->SystemID != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006728 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006729 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006730 (const char *) doc->intSubset->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00006731 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006732 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006733 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006734 (const char *) doc->intSubset->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00006735 }
6736 return(0);
6737 }
6738 }
6739
6740 if (doc->ids != NULL) {
6741 xmlFreeIDTable(doc->ids);
6742 doc->ids = NULL;
6743 }
6744 if (doc->refs != NULL) {
6745 xmlFreeRefTable(doc->refs);
6746 doc->refs = NULL;
6747 }
6748 ret = xmlValidateDtdFinal(ctxt, doc);
6749 if (!xmlValidateRoot(ctxt, doc)) return(0);
6750
6751 root = xmlDocGetRootElement(doc);
6752 ret &= xmlValidateElement(ctxt, doc, root);
6753 ret &= xmlValidateDocumentFinal(ctxt, doc);
6754 return(ret);
6755}
6756
Owen Taylor3473f882001-02-23 17:55:21 +00006757/************************************************************************
6758 * *
6759 * Routines for dynamic validation editing *
6760 * *
6761 ************************************************************************/
6762
6763/**
6764 * xmlValidGetPotentialChildren:
6765 * @ctree: an element content tree
6766 * @list: an array to store the list of child names
6767 * @len: a pointer to the number of element in the list
6768 * @max: the size of the array
6769 *
6770 * Build/extend a list of potential children allowed by the content tree
6771 *
6772 * returns the number of element in the list, or -1 in case of error.
6773 */
6774
6775int
6776xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6777 int *len, int max) {
6778 int i;
6779
6780 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6781 return(-1);
6782 if (*len >= max) return(*len);
6783
6784 switch (ctree->type) {
6785 case XML_ELEMENT_CONTENT_PCDATA:
6786 for (i = 0; i < *len;i++)
6787 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6788 list[(*len)++] = BAD_CAST "#PCDATA";
6789 break;
6790 case XML_ELEMENT_CONTENT_ELEMENT:
6791 for (i = 0; i < *len;i++)
6792 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6793 list[(*len)++] = ctree->name;
6794 break;
6795 case XML_ELEMENT_CONTENT_SEQ:
6796 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6797 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6798 break;
6799 case XML_ELEMENT_CONTENT_OR:
6800 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6801 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6802 break;
6803 }
6804
6805 return(*len);
6806}
6807
William M. Brack9333cc22004-06-24 08:33:40 +00006808/*
6809 * Dummy function to suppress messages while we try out valid elements
6810 */
6811static void xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
6812 const char *msg ATTRIBUTE_UNUSED, ...) {
6813 return;
6814}
6815
Owen Taylor3473f882001-02-23 17:55:21 +00006816/**
6817 * xmlValidGetValidElements:
6818 * @prev: an element to insert after
6819 * @next: an element to insert next
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006820 * @names: an array to store the list of child names
Owen Taylor3473f882001-02-23 17:55:21 +00006821 * @max: the size of the array
6822 *
6823 * This function returns the list of authorized children to insert
6824 * within an existing tree while respecting the validity constraints
6825 * forced by the Dtd. The insertion point is defined using @prev and
6826 * @next in the following ways:
6827 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6828 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6829 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6830 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6831 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6832 *
6833 * pointers to the element names are inserted at the beginning of the array
6834 * and do not need to be freed.
6835 *
6836 * returns the number of element in the list, or -1 in case of error. If
6837 * the function returns the value @max the caller is invited to grow the
6838 * receiving array and retry.
6839 */
6840
6841int
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006842xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
Owen Taylor3473f882001-02-23 17:55:21 +00006843 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006844 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006845 int nb_valid_elements = 0;
6846 const xmlChar *elements[256];
6847 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006848 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006849
6850 xmlNode *ref_node;
6851 xmlNode *parent;
6852 xmlNode *test_node;
6853
6854 xmlNode *prev_next;
6855 xmlNode *next_prev;
6856 xmlNode *parent_childs;
6857 xmlNode *parent_last;
6858
6859 xmlElement *element_desc;
6860
6861 if (prev == NULL && next == NULL)
6862 return(-1);
6863
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006864 if (names == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006865 if (max <= 0) return(-1);
6866
William M. Brack9333cc22004-06-24 08:33:40 +00006867 memset(&vctxt, 0, sizeof (xmlValidCtxt));
6868 vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
6869
Owen Taylor3473f882001-02-23 17:55:21 +00006870 nb_valid_elements = 0;
6871 ref_node = prev ? prev : next;
6872 parent = ref_node->parent;
6873
6874 /*
6875 * Retrieves the parent element declaration
6876 */
6877 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6878 parent->name);
6879 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6880 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6881 parent->name);
6882 if (element_desc == NULL) return(-1);
6883
6884 /*
6885 * Do a backup of the current tree structure
6886 */
6887 prev_next = prev ? prev->next : NULL;
6888 next_prev = next ? next->prev : NULL;
6889 parent_childs = parent->children;
6890 parent_last = parent->last;
6891
6892 /*
6893 * Creates a dummy node and insert it into the tree
6894 */
Daniel Veillard95ddcd32004-10-26 21:53:55 +00006895 test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006896 test_node->parent = parent;
6897 test_node->prev = prev;
6898 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006899 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006900
6901 if (prev) prev->next = test_node;
6902 else parent->children = test_node;
6903
6904 if (next) next->prev = test_node;
6905 else parent->last = test_node;
6906
6907 /*
6908 * Insert each potential child node and check if the parent is
6909 * still valid
6910 */
6911 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6912 elements, &nb_elements, 256);
6913
6914 for (i = 0;i < nb_elements;i++) {
6915 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006916 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006917 int j;
6918
6919 for (j = 0; j < nb_valid_elements;j++)
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006920 if (xmlStrEqual(elements[i], names[j])) break;
6921 names[nb_valid_elements++] = elements[i];
Owen Taylor3473f882001-02-23 17:55:21 +00006922 if (nb_valid_elements >= max) break;
6923 }
6924 }
6925
6926 /*
6927 * Restore the tree structure
6928 */
6929 if (prev) prev->next = prev_next;
6930 if (next) next->prev = next_prev;
6931 parent->children = parent_childs;
6932 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006933
6934 /*
6935 * Free up the dummy node
6936 */
6937 test_node->name = name;
6938 xmlFreeNode(test_node);
6939
Owen Taylor3473f882001-02-23 17:55:21 +00006940 return(nb_valid_elements);
6941}
Daniel Veillard4432df22003-09-28 18:58:27 +00006942#endif /* LIBXML_VALID_ENABLED */
6943
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006944#define bottom_valid
6945#include "elfgcchack.h"