blob: c0141ff19b75e9f8fa21cdd7c74489ead63b525d [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)
Daniel Veillard24505b02005-07-28 23:49:35 +0000456 return (NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000457 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];
Daniel Veillard24505b02005-07-28 23:49:35 +0000463 ctxt->nodeTab[ctxt->nodeNr] = NULL;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000464 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);
Daniel Veillard379a3b72005-08-12 10:18:14 +00002716 } else if (elem == NULL) {
2717 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002718 } else {
Daniel Veillard465a0002005-08-22 12:07:04 +00002719 xmlAttributePtr attrDecl = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002720
Daniel Veillard379a3b72005-08-12 10:18:14 +00002721 xmlChar felem[50], fattr[50];
2722 xmlChar *fullelemname, *fullattrname;
2723
2724 fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ?
2725 xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) :
2726 (xmlChar *)elem->name;
2727
2728 fullattrname = (attr->ns != NULL && attr->ns->prefix != NULL) ?
2729 xmlBuildQName(attr->name, attr->ns->prefix, fattr, 50) :
2730 (xmlChar *)attr->name;
2731
2732 if (fullelemname != NULL && fullattrname != NULL) {
2733 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullelemname,
2734 fullattrname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002735 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillard379a3b72005-08-12 10:18:14 +00002736 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullelemname,
2737 fullattrname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002738 }
Owen Taylor3473f882001-02-23 17:55:21 +00002739
Daniel Veillard379a3b72005-08-12 10:18:14 +00002740 if ((fullattrname != fattr) && (fullattrname != attr->name))
2741 xmlFree(fullattrname);
2742 if ((fullelemname != felem) && (fullelemname != elem->name))
2743 xmlFree(fullelemname);
2744
Owen Taylor3473f882001-02-23 17:55:21 +00002745 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2746 return(1);
2747 }
2748 return(0);
2749}
2750
2751/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002752 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002753 * @doc: the document
2754 * @attr: the attribute
2755 *
2756 * Remove the given attribute from the ID table maintained internally.
2757 *
2758 * Returns -1 if the lookup failed and 0 otherwise
2759 */
2760int
2761xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002762 xmlIDTablePtr table;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002763 xmlIDPtr id;
Owen Taylor3473f882001-02-23 17:55:21 +00002764 xmlChar *ID;
2765
2766 if (doc == NULL) return(-1);
2767 if (attr == NULL) return(-1);
2768 table = (xmlIDTablePtr) doc->ids;
2769 if (table == NULL)
2770 return(-1);
2771
2772 if (attr == NULL)
2773 return(-1);
2774 ID = xmlNodeListGetString(doc, attr->children, 1);
2775 if (ID == NULL)
2776 return(-1);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002777 id = xmlHashLookup(table, ID);
2778 if (id == NULL || id->attr != attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002779 xmlFree(ID);
2780 return(-1);
2781 }
Daniel Veillard91b955c2004-12-10 10:26:42 +00002782 xmlHashRemoveEntry(table, ID, (xmlHashDeallocator) xmlFreeID);
Owen Taylor3473f882001-02-23 17:55:21 +00002783 xmlFree(ID);
Daniel Veillardda6f4af2005-06-20 17:17:54 +00002784 attr->atype = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002785 return(0);
2786}
2787
2788/**
2789 * xmlGetID:
2790 * @doc: pointer to the document
2791 * @ID: the ID value
2792 *
2793 * Search the attribute declaring the given ID
2794 *
2795 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2796 */
2797xmlAttrPtr
2798xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2799 xmlIDTablePtr table;
2800 xmlIDPtr id;
2801
2802 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002803 return(NULL);
2804 }
2805
2806 if (ID == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002807 return(NULL);
2808 }
2809
2810 table = (xmlIDTablePtr) doc->ids;
2811 if (table == NULL)
2812 return(NULL);
2813
2814 id = xmlHashLookup(table, ID);
2815 if (id == NULL)
2816 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002817 if (id->attr == NULL) {
2818 /*
2819 * We are operating on a stream, return a well known reference
2820 * since the attribute node doesn't exist anymore
2821 */
2822 return((xmlAttrPtr) doc);
2823 }
Owen Taylor3473f882001-02-23 17:55:21 +00002824 return(id->attr);
2825}
2826
2827/************************************************************************
2828 * *
2829 * Refs *
2830 * *
2831 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002832typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002833{
2834 xmlListPtr l;
2835 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002836} xmlRemoveMemo;
2837
2838typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2839
2840typedef struct xmlValidateMemo_t
2841{
2842 xmlValidCtxtPtr ctxt;
2843 const xmlChar *name;
2844} xmlValidateMemo;
2845
2846typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002847
2848/**
Owen Taylor3473f882001-02-23 17:55:21 +00002849 * xmlFreeRef:
2850 * @lk: A list link
2851 *
2852 * Deallocate the memory used by a ref definition
2853 */
2854static void
2855xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002856 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2857 if (ref == NULL) return;
2858 if (ref->value != NULL)
2859 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002860 if (ref->name != NULL)
2861 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002862 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002863}
2864
2865/**
2866 * xmlFreeRefList:
2867 * @list_ref: A list of references.
2868 *
2869 * Deallocate the memory used by a list of references
2870 */
2871static void
2872xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002873 if (list_ref == NULL) return;
2874 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002875}
2876
2877/**
2878 * xmlWalkRemoveRef:
2879 * @data: Contents of current link
2880 * @user: Value supplied by the user
2881 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002882 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002883 */
2884static int
2885xmlWalkRemoveRef(const void *data, const void *user)
2886{
Daniel Veillard37721922001-05-04 15:21:12 +00002887 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2888 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2889 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002890
Daniel Veillard37721922001-05-04 15:21:12 +00002891 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2892 xmlListRemoveFirst(ref_list, (void *)data);
2893 return 0;
2894 }
2895 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002896}
2897
2898/**
Daniel Veillard770075b2004-02-25 10:44:30 +00002899 * xmlDummyCompare
2900 * @data0: Value supplied by the user
2901 * @data1: Value supplied by the user
2902 *
2903 * Do nothing, return 0. Used to create unordered lists.
2904 */
2905static int
Daniel Veillardf54cd532004-02-25 11:52:31 +00002906xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2907 const void *data1 ATTRIBUTE_UNUSED)
Daniel Veillard770075b2004-02-25 10:44:30 +00002908{
2909 return (0);
2910}
2911
2912/**
Owen Taylor3473f882001-02-23 17:55:21 +00002913 * xmlAddRef:
2914 * @ctxt: the validation context
2915 * @doc: pointer to the document
2916 * @value: the value name
2917 * @attr: the attribute holding the Ref
2918 *
2919 * Register a new ref declaration
2920 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002921 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002922 */
2923xmlRefPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002924xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002925 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002926 xmlRefPtr ret;
2927 xmlRefTablePtr table;
2928 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002929
Daniel Veillard37721922001-05-04 15:21:12 +00002930 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002931 return(NULL);
2932 }
2933 if (value == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002934 return(NULL);
2935 }
2936 if (attr == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002937 return(NULL);
2938 }
Owen Taylor3473f882001-02-23 17:55:21 +00002939
Daniel Veillard37721922001-05-04 15:21:12 +00002940 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002941 * Create the Ref table if needed.
2942 */
Daniel Veillard37721922001-05-04 15:21:12 +00002943 table = (xmlRefTablePtr) doc->refs;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002944 if (table == NULL) {
2945 doc->refs = table = xmlHashCreateDict(0, doc->dict);
2946 }
Daniel Veillard37721922001-05-04 15:21:12 +00002947 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002948 xmlVErrMemory(ctxt,
Daniel Veillard37721922001-05-04 15:21:12 +00002949 "xmlAddRef: Table creation failed!\n");
2950 return(NULL);
2951 }
Owen Taylor3473f882001-02-23 17:55:21 +00002952
Daniel Veillard37721922001-05-04 15:21:12 +00002953 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2954 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002955 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard37721922001-05-04 15:21:12 +00002956 return(NULL);
2957 }
Owen Taylor3473f882001-02-23 17:55:21 +00002958
Daniel Veillard37721922001-05-04 15:21:12 +00002959 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002960 * fill the structure.
2961 */
Daniel Veillard37721922001-05-04 15:21:12 +00002962 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002963 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2964 /*
2965 * Operating in streaming mode, attr is gonna disapear
2966 */
2967 ret->name = xmlStrdup(attr->name);
2968 ret->attr = NULL;
2969 } else {
2970 ret->name = NULL;
2971 ret->attr = attr;
2972 }
2973 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002974
Daniel Veillard37721922001-05-04 15:21:12 +00002975 /* To add a reference :-
2976 * References are maintained as a list of references,
2977 * Lookup the entry, if no entry create new nodelist
2978 * Add the owning node to the NodeList
2979 * Return the ref
2980 */
Owen Taylor3473f882001-02-23 17:55:21 +00002981
Daniel Veillard37721922001-05-04 15:21:12 +00002982 if (NULL == (ref_list = xmlHashLookup(table, value))) {
Daniel Veillard770075b2004-02-25 10:44:30 +00002983 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002984 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2985 "xmlAddRef: Reference list creation failed!\n",
2986 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002987 return(NULL);
2988 }
2989 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2990 xmlListDelete(ref_list);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002991 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2992 "xmlAddRef: Reference list insertion failed!\n",
2993 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002994 return(NULL);
2995 }
2996 }
Daniel Veillard965983a2004-02-17 16:30:24 +00002997/* xmlListInsert(ref_list, ret); */
2998 xmlListAppend(ref_list, ret);
Daniel Veillard37721922001-05-04 15:21:12 +00002999 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003000}
3001
3002/**
3003 * xmlFreeRefTable:
3004 * @table: An ref table
3005 *
3006 * Deallocate the memory used by an Ref hash table.
3007 */
3008void
3009xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00003010 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00003011}
3012
3013/**
3014 * xmlIsRef:
3015 * @doc: the document
3016 * @elem: the element carrying the attribute
3017 * @attr: the attribute
3018 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003019 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00003020 * then this is simple, otherwise we use an heuristic: name Ref (upper
3021 * or lowercase).
3022 *
3023 * Returns 0 or 1 depending on the lookup result
3024 */
3025int
3026xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003027 if (attr == NULL)
3028 return(0);
3029 if (doc == NULL) {
3030 doc = attr->doc;
3031 if (doc == NULL) return(0);
3032 }
3033
Daniel Veillard37721922001-05-04 15:21:12 +00003034 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
3035 return(0);
3036 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
3037 /* TODO @@@ */
3038 return(0);
3039 } else {
3040 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00003041
Daniel Veillardce244ad2004-11-05 10:03:46 +00003042 if (elem == NULL) return(0);
Daniel Veillard37721922001-05-04 15:21:12 +00003043 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
3044 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3045 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3046 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003047
Daniel Veillard37721922001-05-04 15:21:12 +00003048 if ((attrDecl != NULL) &&
3049 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
3050 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
3051 return(1);
3052 }
3053 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003054}
3055
3056/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003057 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00003058 * @doc: the document
3059 * @attr: the attribute
3060 *
3061 * Remove the given attribute from the Ref table maintained internally.
3062 *
3063 * Returns -1 if the lookup failed and 0 otherwise
3064 */
3065int
3066xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00003067 xmlListPtr ref_list;
3068 xmlRefTablePtr table;
3069 xmlChar *ID;
3070 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00003071
Daniel Veillard37721922001-05-04 15:21:12 +00003072 if (doc == NULL) return(-1);
3073 if (attr == NULL) return(-1);
3074 table = (xmlRefTablePtr) doc->refs;
3075 if (table == NULL)
3076 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003077
Daniel Veillard37721922001-05-04 15:21:12 +00003078 if (attr == NULL)
3079 return(-1);
3080 ID = xmlNodeListGetString(doc, attr->children, 1);
3081 if (ID == NULL)
3082 return(-1);
3083 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00003084
Daniel Veillard37721922001-05-04 15:21:12 +00003085 if(ref_list == NULL) {
3086 xmlFree(ID);
3087 return (-1);
3088 }
3089 /* At this point, ref_list refers to a list of references which
3090 * have the same key as the supplied attr. Our list of references
3091 * is ordered by reference address and we don't have that information
3092 * here to use when removing. We'll have to walk the list and
3093 * check for a matching attribute, when we find one stop the walk
3094 * and remove the entry.
3095 * The list is ordered by reference, so that means we don't have the
3096 * key. Passing the list and the reference to the walker means we
3097 * will have enough data to be able to remove the entry.
3098 */
3099 target.l = ref_list;
3100 target.ap = attr;
3101
3102 /* Remove the supplied attr from our list */
3103 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00003104
Daniel Veillard37721922001-05-04 15:21:12 +00003105 /*If the list is empty then remove the list entry in the hash */
3106 if (xmlListEmpty(ref_list))
3107 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
3108 xmlFreeRefList);
3109 xmlFree(ID);
3110 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003111}
3112
3113/**
3114 * xmlGetRefs:
3115 * @doc: pointer to the document
3116 * @ID: the ID value
3117 *
3118 * Find the set of references for the supplied ID.
3119 *
3120 * Returns NULL if not found, otherwise node set for the ID.
3121 */
3122xmlListPtr
3123xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00003124 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00003125
Daniel Veillard37721922001-05-04 15:21:12 +00003126 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00003127 return(NULL);
3128 }
Owen Taylor3473f882001-02-23 17:55:21 +00003129
Daniel Veillard37721922001-05-04 15:21:12 +00003130 if (ID == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00003131 return(NULL);
3132 }
Owen Taylor3473f882001-02-23 17:55:21 +00003133
Daniel Veillard37721922001-05-04 15:21:12 +00003134 table = (xmlRefTablePtr) doc->refs;
3135 if (table == NULL)
3136 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003137
Daniel Veillard37721922001-05-04 15:21:12 +00003138 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00003139}
3140
3141/************************************************************************
3142 * *
3143 * Routines for validity checking *
3144 * *
3145 ************************************************************************/
3146
3147/**
3148 * xmlGetDtdElementDesc:
3149 * @dtd: a pointer to the DtD to search
3150 * @name: the element name
3151 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003152 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003153 *
3154 * returns the xmlElementPtr if found or NULL
3155 */
3156
3157xmlElementPtr
3158xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
3159 xmlElementTablePtr table;
3160 xmlElementPtr cur;
3161 xmlChar *uqname = NULL, *prefix = NULL;
3162
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003163 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003164 if (dtd->elements == NULL)
3165 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003166 table = (xmlElementTablePtr) dtd->elements;
3167
3168 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003169 if (uqname != NULL)
3170 name = uqname;
3171 cur = xmlHashLookup2(table, name, prefix);
3172 if (prefix != NULL) xmlFree(prefix);
3173 if (uqname != NULL) xmlFree(uqname);
3174 return(cur);
3175}
3176/**
3177 * xmlGetDtdElementDesc2:
3178 * @dtd: a pointer to the DtD to search
3179 * @name: the element name
3180 * @create: create an empty description if not found
3181 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003182 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00003183 *
3184 * returns the xmlElementPtr if found or NULL
3185 */
3186
Daniel Veillard86fd5a72001-12-13 14:55:21 +00003187static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00003188xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
3189 xmlElementTablePtr table;
3190 xmlElementPtr cur;
3191 xmlChar *uqname = NULL, *prefix = NULL;
3192
3193 if (dtd == NULL) return(NULL);
3194 if (dtd->elements == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003195 xmlDictPtr dict = NULL;
3196
3197 if (dtd->doc != NULL)
3198 dict = dtd->doc->dict;
3199
Daniel Veillarda10efa82001-04-18 13:09:01 +00003200 if (!create)
3201 return(NULL);
3202 /*
3203 * Create the Element table if needed.
3204 */
3205 table = (xmlElementTablePtr) dtd->elements;
3206 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003207 table = xmlHashCreateDict(0, dict);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003208 dtd->elements = (void *) table;
3209 }
3210 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003211 xmlVErrMemory(NULL, "element table allocation failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003212 return(NULL);
3213 }
3214 }
3215 table = (xmlElementTablePtr) dtd->elements;
3216
3217 uqname = xmlSplitQName2(name, &prefix);
3218 if (uqname != NULL)
3219 name = uqname;
3220 cur = xmlHashLookup2(table, name, prefix);
3221 if ((cur == NULL) && (create)) {
3222 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
3223 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003224 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003225 return(NULL);
3226 }
3227 memset(cur, 0, sizeof(xmlElement));
3228 cur->type = XML_ELEMENT_DECL;
3229
3230 /*
3231 * fill the structure.
3232 */
3233 cur->name = xmlStrdup(name);
3234 cur->prefix = xmlStrdup(prefix);
3235 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3236
3237 xmlHashAddEntry2(table, name, prefix, cur);
3238 }
3239 if (prefix != NULL) xmlFree(prefix);
3240 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00003241 return(cur);
3242}
3243
3244/**
3245 * xmlGetDtdQElementDesc:
3246 * @dtd: a pointer to the DtD to search
3247 * @name: the element name
3248 * @prefix: the element namespace prefix
3249 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003250 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003251 *
3252 * returns the xmlElementPtr if found or NULL
3253 */
3254
Daniel Veillard48da9102001-08-07 01:10:10 +00003255xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00003256xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3257 const xmlChar *prefix) {
3258 xmlElementTablePtr table;
3259
3260 if (dtd == NULL) return(NULL);
3261 if (dtd->elements == NULL) return(NULL);
3262 table = (xmlElementTablePtr) dtd->elements;
3263
3264 return(xmlHashLookup2(table, name, prefix));
3265}
3266
3267/**
3268 * xmlGetDtdAttrDesc:
3269 * @dtd: a pointer to the DtD to search
3270 * @elem: the element name
3271 * @name: the attribute name
3272 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003273 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003274 * this element.
3275 *
3276 * returns the xmlAttributePtr if found or NULL
3277 */
3278
3279xmlAttributePtr
3280xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3281 xmlAttributeTablePtr table;
3282 xmlAttributePtr cur;
3283 xmlChar *uqname = NULL, *prefix = NULL;
3284
3285 if (dtd == NULL) return(NULL);
3286 if (dtd->attributes == NULL) return(NULL);
3287
3288 table = (xmlAttributeTablePtr) dtd->attributes;
3289 if (table == NULL)
3290 return(NULL);
3291
3292 uqname = xmlSplitQName2(name, &prefix);
3293
3294 if (uqname != NULL) {
3295 cur = xmlHashLookup3(table, uqname, prefix, elem);
3296 if (prefix != NULL) xmlFree(prefix);
3297 if (uqname != NULL) xmlFree(uqname);
3298 } else
3299 cur = xmlHashLookup3(table, name, NULL, elem);
3300 return(cur);
3301}
3302
3303/**
3304 * xmlGetDtdQAttrDesc:
3305 * @dtd: a pointer to the DtD to search
3306 * @elem: the element name
3307 * @name: the attribute name
3308 * @prefix: the attribute namespace prefix
3309 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003310 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003311 * this element.
3312 *
3313 * returns the xmlAttributePtr if found or NULL
3314 */
3315
Daniel Veillard48da9102001-08-07 01:10:10 +00003316xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00003317xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3318 const xmlChar *prefix) {
3319 xmlAttributeTablePtr table;
3320
3321 if (dtd == NULL) return(NULL);
3322 if (dtd->attributes == NULL) return(NULL);
3323 table = (xmlAttributeTablePtr) dtd->attributes;
3324
3325 return(xmlHashLookup3(table, name, prefix, elem));
3326}
3327
3328/**
3329 * xmlGetDtdNotationDesc:
3330 * @dtd: a pointer to the DtD to search
3331 * @name: the notation name
3332 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003333 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00003334 *
3335 * returns the xmlNotationPtr if found or NULL
3336 */
3337
3338xmlNotationPtr
3339xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3340 xmlNotationTablePtr table;
3341
3342 if (dtd == NULL) return(NULL);
3343 if (dtd->notations == NULL) return(NULL);
3344 table = (xmlNotationTablePtr) dtd->notations;
3345
3346 return(xmlHashLookup(table, name));
3347}
3348
Daniel Veillardf54cd532004-02-25 11:52:31 +00003349#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003350/**
3351 * xmlValidateNotationUse:
3352 * @ctxt: the validation context
3353 * @doc: the document
3354 * @notationName: the notation name to check
3355 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003356 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00003357 * - [ VC: Notation Declared ]
3358 *
3359 * returns 1 if valid or 0 otherwise
3360 */
3361
3362int
3363xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3364 const xmlChar *notationName) {
3365 xmlNotationPtr notaDecl;
3366 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3367
3368 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3369 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3370 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3371
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003372 if ((notaDecl == NULL) && (ctxt != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003373 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3374 "NOTATION %s is not declared\n",
3375 notationName, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003376 return(0);
3377 }
3378 return(1);
3379}
Daniel Veillardf54cd532004-02-25 11:52:31 +00003380#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003381
3382/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003383 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00003384 * @doc: the document
3385 * @name: the element name
3386 *
3387 * Search in the DtDs whether an element accept Mixed content (or ANY)
3388 * basically if it is supposed to accept text childs
3389 *
3390 * returns 0 if no, 1 if yes, and -1 if no element description is available
3391 */
3392
3393int
3394xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3395 xmlElementPtr elemDecl;
3396
3397 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3398
3399 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3400 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3401 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3402 if (elemDecl == NULL) return(-1);
3403 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00003404 case XML_ELEMENT_TYPE_UNDEFINED:
3405 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003406 case XML_ELEMENT_TYPE_ELEMENT:
3407 return(0);
3408 case XML_ELEMENT_TYPE_EMPTY:
3409 /*
3410 * return 1 for EMPTY since we want VC error to pop up
3411 * on <empty> </empty> for example
3412 */
3413 case XML_ELEMENT_TYPE_ANY:
3414 case XML_ELEMENT_TYPE_MIXED:
3415 return(1);
3416 }
3417 return(1);
3418}
3419
Daniel Veillard4432df22003-09-28 18:58:27 +00003420#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003421/**
3422 * xmlValidateNameValue:
3423 * @value: an Name value
3424 *
3425 * Validate that the given value match Name production
3426 *
3427 * returns 1 if valid or 0 otherwise
3428 */
3429
Daniel Veillard9b731d72002-04-14 12:56:08 +00003430int
Owen Taylor3473f882001-02-23 17:55:21 +00003431xmlValidateNameValue(const xmlChar *value) {
3432 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003433 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003434
3435 if (value == NULL) return(0);
3436 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003437 val = xmlStringCurrentChar(NULL, cur, &len);
3438 cur += len;
3439 if (!IS_LETTER(val) && (val != '_') &&
3440 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003441 return(0);
3442 }
3443
Daniel Veillardd8224e02002-01-13 15:43:22 +00003444 val = xmlStringCurrentChar(NULL, cur, &len);
3445 cur += len;
3446 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3447 (val == '.') || (val == '-') ||
3448 (val == '_') || (val == ':') ||
3449 (IS_COMBINING(val)) ||
3450 (IS_EXTENDER(val))) {
3451 val = xmlStringCurrentChar(NULL, cur, &len);
3452 cur += len;
3453 }
Owen Taylor3473f882001-02-23 17:55:21 +00003454
Daniel Veillardd8224e02002-01-13 15:43:22 +00003455 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003456
3457 return(1);
3458}
3459
3460/**
3461 * xmlValidateNamesValue:
3462 * @value: an Names value
3463 *
3464 * Validate that the given value match Names production
3465 *
3466 * returns 1 if valid or 0 otherwise
3467 */
3468
Daniel Veillard9b731d72002-04-14 12:56:08 +00003469int
Owen Taylor3473f882001-02-23 17:55:21 +00003470xmlValidateNamesValue(const xmlChar *value) {
3471 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003472 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003473
3474 if (value == NULL) return(0);
3475 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003476 val = xmlStringCurrentChar(NULL, cur, &len);
3477 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003478
Daniel Veillardd8224e02002-01-13 15:43:22 +00003479 if (!IS_LETTER(val) && (val != '_') &&
3480 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003481 return(0);
3482 }
3483
Daniel Veillardd8224e02002-01-13 15:43:22 +00003484 val = xmlStringCurrentChar(NULL, cur, &len);
3485 cur += len;
3486 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3487 (val == '.') || (val == '-') ||
3488 (val == '_') || (val == ':') ||
3489 (IS_COMBINING(val)) ||
3490 (IS_EXTENDER(val))) {
3491 val = xmlStringCurrentChar(NULL, cur, &len);
3492 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003493 }
3494
Daniel Veillard807b4de2004-09-26 14:42:56 +00003495 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3496 while (val == 0x20) {
3497 while (val == 0x20) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003498 val = xmlStringCurrentChar(NULL, cur, &len);
3499 cur += len;
3500 }
3501
3502 if (!IS_LETTER(val) && (val != '_') &&
3503 (val != ':')) {
3504 return(0);
3505 }
3506 val = xmlStringCurrentChar(NULL, cur, &len);
3507 cur += len;
3508
3509 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3510 (val == '.') || (val == '-') ||
3511 (val == '_') || (val == ':') ||
3512 (IS_COMBINING(val)) ||
3513 (IS_EXTENDER(val))) {
3514 val = xmlStringCurrentChar(NULL, cur, &len);
3515 cur += len;
3516 }
3517 }
3518
3519 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003520
3521 return(1);
3522}
3523
3524/**
3525 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003526 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003527 *
3528 * Validate that the given value match Nmtoken production
3529 *
3530 * [ VC: Name Token ]
3531 *
3532 * returns 1 if valid or 0 otherwise
3533 */
3534
Daniel Veillard9b731d72002-04-14 12:56:08 +00003535int
Owen Taylor3473f882001-02-23 17:55:21 +00003536xmlValidateNmtokenValue(const xmlChar *value) {
3537 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003538 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003539
3540 if (value == NULL) return(0);
3541 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003542 val = xmlStringCurrentChar(NULL, cur, &len);
3543 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003544
Daniel Veillardd8224e02002-01-13 15:43:22 +00003545 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3546 (val != '.') && (val != '-') &&
3547 (val != '_') && (val != ':') &&
3548 (!IS_COMBINING(val)) &&
3549 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003550 return(0);
3551
Daniel Veillardd8224e02002-01-13 15:43:22 +00003552 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3553 (val == '.') || (val == '-') ||
3554 (val == '_') || (val == ':') ||
3555 (IS_COMBINING(val)) ||
3556 (IS_EXTENDER(val))) {
3557 val = xmlStringCurrentChar(NULL, cur, &len);
3558 cur += len;
3559 }
Owen Taylor3473f882001-02-23 17:55:21 +00003560
Daniel Veillardd8224e02002-01-13 15:43:22 +00003561 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003562
3563 return(1);
3564}
3565
3566/**
3567 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003568 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003569 *
3570 * Validate that the given value match Nmtokens production
3571 *
3572 * [ VC: Name Token ]
3573 *
3574 * returns 1 if valid or 0 otherwise
3575 */
3576
Daniel Veillard9b731d72002-04-14 12:56:08 +00003577int
Owen Taylor3473f882001-02-23 17:55:21 +00003578xmlValidateNmtokensValue(const xmlChar *value) {
3579 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003580 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003581
3582 if (value == NULL) return(0);
3583 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003584 val = xmlStringCurrentChar(NULL, cur, &len);
3585 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003586
Daniel Veillardd8224e02002-01-13 15:43:22 +00003587 while (IS_BLANK(val)) {
3588 val = xmlStringCurrentChar(NULL, cur, &len);
3589 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003590 }
3591
Daniel Veillardd8224e02002-01-13 15:43:22 +00003592 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3593 (val != '.') && (val != '-') &&
3594 (val != '_') && (val != ':') &&
3595 (!IS_COMBINING(val)) &&
3596 (!IS_EXTENDER(val)))
3597 return(0);
3598
3599 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3600 (val == '.') || (val == '-') ||
3601 (val == '_') || (val == ':') ||
3602 (IS_COMBINING(val)) ||
3603 (IS_EXTENDER(val))) {
3604 val = xmlStringCurrentChar(NULL, cur, &len);
3605 cur += len;
3606 }
3607
Daniel Veillard807b4de2004-09-26 14:42:56 +00003608 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3609 while (val == 0x20) {
3610 while (val == 0x20) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003611 val = xmlStringCurrentChar(NULL, cur, &len);
3612 cur += len;
3613 }
3614 if (val == 0) return(1);
3615
3616 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3617 (val != '.') && (val != '-') &&
3618 (val != '_') && (val != ':') &&
3619 (!IS_COMBINING(val)) &&
3620 (!IS_EXTENDER(val)))
3621 return(0);
3622
3623 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3624 (val == '.') || (val == '-') ||
3625 (val == '_') || (val == ':') ||
3626 (IS_COMBINING(val)) ||
3627 (IS_EXTENDER(val))) {
3628 val = xmlStringCurrentChar(NULL, cur, &len);
3629 cur += len;
3630 }
3631 }
3632
3633 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003634
3635 return(1);
3636}
3637
3638/**
3639 * xmlValidateNotationDecl:
3640 * @ctxt: the validation context
3641 * @doc: a document instance
3642 * @nota: a notation definition
3643 *
3644 * Try to validate a single notation definition
3645 * basically it does the following checks as described by the
3646 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003647 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003648 * But this function get called anyway ...
3649 *
3650 * returns 1 if valid or 0 otherwise
3651 */
3652
3653int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003654xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3655 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003656 int ret = 1;
3657
3658 return(ret);
3659}
3660
3661/**
3662 * xmlValidateAttributeValue:
3663 * @type: an attribute type
3664 * @value: an attribute value
3665 *
3666 * Validate that the given attribute value match the proper production
3667 *
3668 * [ VC: ID ]
3669 * Values of type ID must match the Name production....
3670 *
3671 * [ VC: IDREF ]
3672 * Values of type IDREF must match the Name production, and values
3673 * of type IDREFS must match Names ...
3674 *
3675 * [ VC: Entity Name ]
3676 * Values of type ENTITY must match the Name production, values
3677 * of type ENTITIES must match Names ...
3678 *
3679 * [ VC: Name Token ]
3680 * Values of type NMTOKEN must match the Nmtoken production; values
3681 * of type NMTOKENS must match Nmtokens.
3682 *
3683 * returns 1 if valid or 0 otherwise
3684 */
3685
3686int
3687xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3688 switch (type) {
3689 case XML_ATTRIBUTE_ENTITIES:
3690 case XML_ATTRIBUTE_IDREFS:
3691 return(xmlValidateNamesValue(value));
3692 case XML_ATTRIBUTE_ENTITY:
3693 case XML_ATTRIBUTE_IDREF:
3694 case XML_ATTRIBUTE_ID:
3695 case XML_ATTRIBUTE_NOTATION:
3696 return(xmlValidateNameValue(value));
3697 case XML_ATTRIBUTE_NMTOKENS:
3698 case XML_ATTRIBUTE_ENUMERATION:
3699 return(xmlValidateNmtokensValue(value));
3700 case XML_ATTRIBUTE_NMTOKEN:
3701 return(xmlValidateNmtokenValue(value));
3702 case XML_ATTRIBUTE_CDATA:
3703 break;
3704 }
3705 return(1);
3706}
3707
3708/**
3709 * xmlValidateAttributeValue2:
3710 * @ctxt: the validation context
3711 * @doc: the document
3712 * @name: the attribute name (used for error reporting only)
3713 * @type: the attribute type
3714 * @value: the attribute value
3715 *
3716 * Validate that the given attribute value match a given type.
3717 * This typically cannot be done before having finished parsing
3718 * the subsets.
3719 *
3720 * [ VC: IDREF ]
3721 * Values of type IDREF must match one of the declared IDs
3722 * Values of type IDREFS must match a sequence of the declared IDs
3723 * each Name must match the value of an ID attribute on some element
3724 * in the XML document; i.e. IDREF values must match the value of
3725 * some ID attribute
3726 *
3727 * [ VC: Entity Name ]
3728 * Values of type ENTITY must match one declared entity
3729 * Values of type ENTITIES must match a sequence of declared entities
3730 *
3731 * [ VC: Notation Attributes ]
3732 * all notation names in the declaration must be declared.
3733 *
3734 * returns 1 if valid or 0 otherwise
3735 */
3736
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003737static int
Owen Taylor3473f882001-02-23 17:55:21 +00003738xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3739 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3740 int ret = 1;
3741 switch (type) {
3742 case XML_ATTRIBUTE_IDREFS:
3743 case XML_ATTRIBUTE_IDREF:
3744 case XML_ATTRIBUTE_ID:
3745 case XML_ATTRIBUTE_NMTOKENS:
3746 case XML_ATTRIBUTE_ENUMERATION:
3747 case XML_ATTRIBUTE_NMTOKEN:
3748 case XML_ATTRIBUTE_CDATA:
3749 break;
3750 case XML_ATTRIBUTE_ENTITY: {
3751 xmlEntityPtr ent;
3752
3753 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003754 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003755 if ((ent == NULL) && (doc->standalone == 1)) {
3756 doc->standalone = 0;
3757 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003758 }
Owen Taylor3473f882001-02-23 17:55:21 +00003759 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003760 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3761 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003762 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003763 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003764 ret = 0;
3765 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003766 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3767 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003768 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003769 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003770 ret = 0;
3771 }
3772 break;
3773 }
3774 case XML_ATTRIBUTE_ENTITIES: {
3775 xmlChar *dup, *nam = NULL, *cur, save;
3776 xmlEntityPtr ent;
3777
3778 dup = xmlStrdup(value);
3779 if (dup == NULL)
3780 return(0);
3781 cur = dup;
3782 while (*cur != 0) {
3783 nam = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00003784 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003785 save = *cur;
3786 *cur = 0;
3787 ent = xmlGetDocEntity(doc, nam);
3788 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003789 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3790 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003791 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003792 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003793 ret = 0;
3794 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003795 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3796 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003797 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003798 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003799 ret = 0;
3800 }
3801 if (save == 0)
3802 break;
3803 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00003804 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003805 }
3806 xmlFree(dup);
3807 break;
3808 }
3809 case XML_ATTRIBUTE_NOTATION: {
3810 xmlNotationPtr nota;
3811
3812 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3813 if ((nota == NULL) && (doc->extSubset != NULL))
3814 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3815
3816 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003817 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3818 XML_DTD_UNKNOWN_NOTATION,
Owen Taylor3473f882001-02-23 17:55:21 +00003819 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003820 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003821 ret = 0;
3822 }
3823 break;
3824 }
3825 }
3826 return(ret);
3827}
3828
3829/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003830 * xmlValidCtxtNormalizeAttributeValue:
3831 * @ctxt: the validation context
3832 * @doc: the document
3833 * @elem: the parent
3834 * @name: the attribute name
3835 * @value: the attribute value
3836 * @ctxt: the validation context or NULL
3837 *
3838 * Does the validation related extra step of the normalization of attribute
3839 * values:
3840 *
3841 * If the declared value is not CDATA, then the XML processor must further
3842 * process the normalized attribute value by discarding any leading and
3843 * trailing space (#x20) characters, and by replacing sequences of space
3844 * (#x20) characters by single space (#x20) character.
3845 *
3846 * Also check VC: Standalone Document Declaration in P32, and update
3847 * ctxt->valid accordingly
3848 *
3849 * returns a new normalized string if normalization is needed, NULL otherwise
3850 * the caller must free the returned value.
3851 */
3852
3853xmlChar *
3854xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3855 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3856 xmlChar *ret, *dst;
3857 const xmlChar *src;
3858 xmlAttributePtr attrDecl = NULL;
3859 int extsubset = 0;
3860
3861 if (doc == NULL) return(NULL);
3862 if (elem == NULL) return(NULL);
3863 if (name == NULL) return(NULL);
3864 if (value == NULL) return(NULL);
3865
3866 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003867 xmlChar fn[50];
3868 xmlChar *fullname;
3869
3870 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3871 if (fullname == NULL)
Daniel Veillard24505b02005-07-28 23:49:35 +00003872 return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00003873 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003874 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003875 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003876 if (attrDecl != NULL)
3877 extsubset = 1;
3878 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003879 if ((fullname != fn) && (fullname != elem->name))
3880 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003881 }
3882 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3883 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3884 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3885 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3886 if (attrDecl != NULL)
3887 extsubset = 1;
3888 }
3889
3890 if (attrDecl == NULL)
3891 return(NULL);
3892 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3893 return(NULL);
3894
3895 ret = xmlStrdup(value);
3896 if (ret == NULL)
3897 return(NULL);
3898 src = value;
3899 dst = ret;
3900 while (*src == 0x20) src++;
3901 while (*src != 0) {
3902 if (*src == 0x20) {
3903 while (*src == 0x20) src++;
3904 if (*src != 0)
3905 *dst++ = 0x20;
3906 } else {
3907 *dst++ = *src++;
3908 }
3909 }
3910 *dst = 0;
3911 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003912 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003913"standalone: %s on %s value had to be normalized based on external subset declaration\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003914 name, elem->name, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003915 ctxt->valid = 0;
3916 }
3917 return(ret);
3918}
3919
3920/**
Owen Taylor3473f882001-02-23 17:55:21 +00003921 * xmlValidNormalizeAttributeValue:
3922 * @doc: the document
3923 * @elem: the parent
3924 * @name: the attribute name
3925 * @value: the attribute value
3926 *
3927 * Does the validation related extra step of the normalization of attribute
3928 * values:
3929 *
3930 * If the declared value is not CDATA, then the XML processor must further
3931 * process the normalized attribute value by discarding any leading and
3932 * trailing space (#x20) characters, and by replacing sequences of space
3933 * (#x20) characters by single space (#x20) character.
3934 *
Daniel Veillard652327a2003-09-29 18:02:38 +00003935 * Returns a new normalized string if normalization is needed, NULL otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00003936 * the caller must free the returned value.
3937 */
3938
3939xmlChar *
3940xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3941 const xmlChar *name, const xmlChar *value) {
3942 xmlChar *ret, *dst;
3943 const xmlChar *src;
3944 xmlAttributePtr attrDecl = NULL;
3945
3946 if (doc == NULL) return(NULL);
3947 if (elem == NULL) return(NULL);
3948 if (name == NULL) return(NULL);
3949 if (value == NULL) return(NULL);
3950
3951 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003952 xmlChar fn[50];
3953 xmlChar *fullname;
3954
3955 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3956 if (fullname == NULL)
Daniel Veillard24505b02005-07-28 23:49:35 +00003957 return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00003958 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003959 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003960 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3961 if ((fullname != fn) && (fullname != elem->name))
3962 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003963 }
3964 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3965 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3966 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3967
3968 if (attrDecl == NULL)
3969 return(NULL);
3970 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3971 return(NULL);
3972
3973 ret = xmlStrdup(value);
3974 if (ret == NULL)
3975 return(NULL);
3976 src = value;
3977 dst = ret;
3978 while (*src == 0x20) src++;
3979 while (*src != 0) {
3980 if (*src == 0x20) {
3981 while (*src == 0x20) src++;
3982 if (*src != 0)
3983 *dst++ = 0x20;
3984 } else {
3985 *dst++ = *src++;
3986 }
3987 }
3988 *dst = 0;
3989 return(ret);
3990}
3991
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003992static void
Owen Taylor3473f882001-02-23 17:55:21 +00003993xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003994 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003995 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3996}
3997
3998/**
3999 * xmlValidateAttributeDecl:
4000 * @ctxt: the validation context
4001 * @doc: a document instance
4002 * @attr: an attribute definition
4003 *
4004 * Try to validate a single attribute definition
4005 * basically it does the following checks as described by the
4006 * XML-1.0 recommendation:
4007 * - [ VC: Attribute Default Legal ]
4008 * - [ VC: Enumeration ]
4009 * - [ VC: ID Attribute Default ]
4010 *
4011 * The ID/IDREF uniqueness and matching are done separately
4012 *
4013 * returns 1 if valid or 0 otherwise
4014 */
4015
4016int
4017xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4018 xmlAttributePtr attr) {
4019 int ret = 1;
4020 int val;
4021 CHECK_DTD;
4022 if(attr == NULL) return(1);
4023
4024 /* Attribute Default Legal */
4025 /* Enumeration */
4026 if (attr->defaultValue != NULL) {
4027 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
4028 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004029 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004030 "Syntax of default value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004031 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004032 }
4033 ret &= val;
4034 }
4035
4036 /* ID Attribute Default */
4037 if ((attr->atype == XML_ATTRIBUTE_ID)&&
4038 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
4039 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004040 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004041 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004042 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004043 ret = 0;
4044 }
4045
4046 /* One ID per Element Type */
4047 if (attr->atype == XML_ATTRIBUTE_ID) {
4048 int nbId;
4049
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004050 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00004051 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
4052 attr->elem);
4053 if (elem != NULL) {
Daniel Veillarddbee0f12005-06-27 13:42:57 +00004054 nbId = xmlScanIDAttributeDecl(NULL, elem, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00004055 } else {
4056 xmlAttributeTablePtr table;
4057
4058 /*
4059 * The attribute may be declared in the internal subset and the
4060 * element in the external subset.
4061 */
4062 nbId = 0;
4063 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
4064 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
4065 xmlValidateAttributeIdCallback, &nbId);
4066 }
4067 if (nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004068
4069 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004070 "Element %s has %d ID attribute defined in the internal subset : %s\n",
4071 attr->elem, nbId, attr->name);
4072 } else if (doc->extSubset != NULL) {
4073 int extId = 0;
4074 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
4075 if (elem != NULL) {
Daniel Veillarddbee0f12005-06-27 13:42:57 +00004076 extId = xmlScanIDAttributeDecl(NULL, elem, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00004077 }
4078 if (extId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004079 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004080 "Element %s has %d ID attribute defined in the external subset : %s\n",
4081 attr->elem, extId, attr->name);
4082 } else if (extId + nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004083 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004084"Element %s has ID attributes defined in the internal and external subset : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004085 attr->elem, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004086 }
4087 }
4088 }
4089
4090 /* Validity Constraint: Enumeration */
4091 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
4092 xmlEnumerationPtr tree = attr->tree;
4093 while (tree != NULL) {
4094 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
4095 tree = tree->next;
4096 }
4097 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004098 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004099"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004100 attr->defaultValue, attr->name, attr->elem);
4101 ret = 0;
4102 }
4103 }
4104
4105 return(ret);
4106}
4107
4108/**
4109 * xmlValidateElementDecl:
4110 * @ctxt: the validation context
4111 * @doc: a document instance
4112 * @elem: an element definition
4113 *
4114 * Try to validate a single element definition
4115 * basically it does the following checks as described by the
4116 * XML-1.0 recommendation:
4117 * - [ VC: One ID per Element Type ]
4118 * - [ VC: No Duplicate Types ]
4119 * - [ VC: Unique Element Type Declaration ]
4120 *
4121 * returns 1 if valid or 0 otherwise
4122 */
4123
4124int
4125xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4126 xmlElementPtr elem) {
4127 int ret = 1;
4128 xmlElementPtr tst;
4129
4130 CHECK_DTD;
4131
4132 if (elem == NULL) return(1);
4133
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004134#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00004135#ifdef LIBXML_REGEXP_ENABLED
4136 /* Build the regexp associated to the content model */
4137 ret = xmlValidBuildContentModel(ctxt, elem);
4138#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004139#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00004140
Owen Taylor3473f882001-02-23 17:55:21 +00004141 /* No Duplicate Types */
4142 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
4143 xmlElementContentPtr cur, next;
4144 const xmlChar *name;
4145
4146 cur = elem->content;
4147 while (cur != NULL) {
4148 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
4149 if (cur->c1 == NULL) break;
4150 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4151 name = cur->c1->name;
4152 next = cur->c2;
4153 while (next != NULL) {
4154 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00004155 if ((xmlStrEqual(next->name, name)) &&
4156 (xmlStrEqual(next->prefix, cur->prefix))) {
4157 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004158 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00004159 "Definition of %s has duplicate references of %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004160 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004161 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004162 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004163 "Definition of %s has duplicate references of %s:%s\n",
4164 elem->name, cur->prefix, name);
4165 }
Owen Taylor3473f882001-02-23 17:55:21 +00004166 ret = 0;
4167 }
4168 break;
4169 }
4170 if (next->c1 == NULL) break;
4171 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00004172 if ((xmlStrEqual(next->c1->name, name)) &&
4173 (xmlStrEqual(next->c1->prefix, cur->prefix))) {
4174 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004175 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004176 "Definition of %s has duplicate references to %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004177 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004178 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004179 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004180 "Definition of %s has duplicate references to %s:%s\n",
4181 elem->name, cur->prefix, name);
4182 }
Owen Taylor3473f882001-02-23 17:55:21 +00004183 ret = 0;
4184 }
4185 next = next->c2;
4186 }
4187 }
4188 cur = cur->c2;
4189 }
4190 }
4191
4192 /* VC: Unique Element Type Declaration */
4193 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004194 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004195 ((tst->prefix == elem->prefix) ||
4196 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004197 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004198 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4199 "Redefinition of element %s\n",
4200 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004201 ret = 0;
4202 }
4203 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004204 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004205 ((tst->prefix == elem->prefix) ||
4206 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004207 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004208 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4209 "Redefinition of element %s\n",
4210 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004211 ret = 0;
4212 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00004213 /* One ID per Element Type
4214 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00004215 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4216 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00004217 } */
Owen Taylor3473f882001-02-23 17:55:21 +00004218 return(ret);
4219}
4220
4221/**
4222 * xmlValidateOneAttribute:
4223 * @ctxt: the validation context
4224 * @doc: a document instance
4225 * @elem: an element instance
4226 * @attr: an attribute instance
4227 * @value: the attribute value (without entities processing)
4228 *
4229 * Try to validate a single attribute for an element
4230 * basically it does the following checks as described by the
4231 * XML-1.0 recommendation:
4232 * - [ VC: Attribute Value Type ]
4233 * - [ VC: Fixed Attribute Default ]
4234 * - [ VC: Entity Name ]
4235 * - [ VC: Name Token ]
4236 * - [ VC: ID ]
4237 * - [ VC: IDREF ]
4238 * - [ VC: Entity Name ]
4239 * - [ VC: Notation Attributes ]
4240 *
4241 * The ID/IDREF uniqueness and matching are done separately
4242 *
4243 * returns 1 if valid or 0 otherwise
4244 */
4245
4246int
4247xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillard07cb8222003-09-10 10:51:05 +00004248 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4249{
Owen Taylor3473f882001-02-23 17:55:21 +00004250 xmlAttributePtr attrDecl = NULL;
4251 int val;
4252 int ret = 1;
4253
4254 CHECK_DTD;
4255 if ((elem == NULL) || (elem->name == NULL)) return(0);
4256 if ((attr == NULL) || (attr->name == NULL)) return(0);
4257
4258 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004259 xmlChar fn[50];
4260 xmlChar *fullname;
4261
4262 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4263 if (fullname == NULL)
4264 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004265 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004266 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004267 attr->name, attr->ns->prefix);
4268 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004269 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004270 attr->name, attr->ns->prefix);
4271 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004272 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004273 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4274 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00004275 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004276 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004277 if ((fullname != fn) && (fullname != elem->name))
4278 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00004279 }
4280 if (attrDecl == NULL) {
4281 if (attr->ns != NULL) {
4282 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4283 attr->name, attr->ns->prefix);
4284 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4285 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4286 attr->name, attr->ns->prefix);
4287 } else {
4288 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4289 elem->name, attr->name);
4290 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4291 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4292 elem->name, attr->name);
4293 }
4294 }
4295
4296
4297 /* Validity Constraint: Attribute Value Type */
4298 if (attrDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004299 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004300 "No declaration for attribute %s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004301 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004302 return(0);
4303 }
4304 attr->atype = attrDecl->atype;
4305
4306 val = xmlValidateAttributeValue(attrDecl->atype, value);
4307 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004308 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004309 "Syntax of value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004310 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004311 ret = 0;
4312 }
4313
4314 /* Validity constraint: Fixed Attribute Default */
4315 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4316 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004317 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004318 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004319 attr->name, elem->name, attrDecl->defaultValue);
4320 ret = 0;
4321 }
4322 }
4323
4324 /* Validity Constraint: ID uniqueness */
4325 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4326 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4327 ret = 0;
4328 }
4329
4330 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4331 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4332 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4333 ret = 0;
4334 }
4335
4336 /* Validity Constraint: Notation Attributes */
4337 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4338 xmlEnumerationPtr tree = attrDecl->tree;
4339 xmlNotationPtr nota;
4340
4341 /* First check that the given NOTATION was declared */
4342 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4343 if (nota == NULL)
4344 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4345
4346 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004347 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004348 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004349 value, attr->name, elem->name);
4350 ret = 0;
4351 }
4352
4353 /* Second, verify that it's among the list */
4354 while (tree != NULL) {
4355 if (xmlStrEqual(tree->name, value)) break;
4356 tree = tree->next;
4357 }
4358 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004359 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004360"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004361 value, attr->name, elem->name);
4362 ret = 0;
4363 }
4364 }
4365
4366 /* Validity Constraint: Enumeration */
4367 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4368 xmlEnumerationPtr tree = attrDecl->tree;
4369 while (tree != NULL) {
4370 if (xmlStrEqual(tree->name, value)) break;
4371 tree = tree->next;
4372 }
4373 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004374 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004375 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004376 value, attr->name, elem->name);
4377 ret = 0;
4378 }
4379 }
4380
4381 /* Fixed Attribute Default */
4382 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4383 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004384 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004385 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004386 attr->name, elem->name, attrDecl->defaultValue);
4387 ret = 0;
4388 }
4389
4390 /* Extra check for the attribute value */
4391 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4392 attrDecl->atype, value);
4393
4394 return(ret);
4395}
4396
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004397/**
4398 * xmlValidateOneNamespace:
4399 * @ctxt: the validation context
4400 * @doc: a document instance
4401 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004402 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004403 * @ns: an namespace declaration instance
4404 * @value: the attribute value (without entities processing)
4405 *
4406 * Try to validate a single namespace declaration for an element
4407 * basically it does the following checks as described by the
4408 * XML-1.0 recommendation:
4409 * - [ VC: Attribute Value Type ]
4410 * - [ VC: Fixed Attribute Default ]
4411 * - [ VC: Entity Name ]
4412 * - [ VC: Name Token ]
4413 * - [ VC: ID ]
4414 * - [ VC: IDREF ]
4415 * - [ VC: Entity Name ]
4416 * - [ VC: Notation Attributes ]
4417 *
4418 * The ID/IDREF uniqueness and matching are done separately
4419 *
4420 * returns 1 if valid or 0 otherwise
4421 */
4422
4423int
4424xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4425xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4426 /* xmlElementPtr elemDecl; */
4427 xmlAttributePtr attrDecl = NULL;
4428 int val;
4429 int ret = 1;
4430
4431 CHECK_DTD;
4432 if ((elem == NULL) || (elem->name == NULL)) return(0);
4433 if ((ns == NULL) || (ns->href == NULL)) return(0);
4434
4435 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004436 xmlChar fn[50];
4437 xmlChar *fullname;
4438
4439 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4440 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004441 xmlVErrMemory(ctxt, "Validating namespace");
Daniel Veillardc00cda82003-04-07 10:22:39 +00004442 return(0);
4443 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004444 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004445 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004446 ns->prefix, BAD_CAST "xmlns");
4447 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004448 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004449 ns->prefix, BAD_CAST "xmlns");
4450 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004451 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004452 BAD_CAST "xmlns");
4453 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004454 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004455 BAD_CAST "xmlns");
4456 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004457 if ((fullname != fn) && (fullname != elem->name))
4458 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004459 }
4460 if (attrDecl == NULL) {
4461 if (ns->prefix != NULL) {
4462 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4463 ns->prefix, BAD_CAST "xmlns");
4464 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4465 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4466 ns->prefix, BAD_CAST "xmlns");
4467 } else {
4468 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4469 elem->name, BAD_CAST "xmlns");
4470 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4471 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4472 elem->name, BAD_CAST "xmlns");
4473 }
4474 }
4475
4476
4477 /* Validity Constraint: Attribute Value Type */
4478 if (attrDecl == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004479 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004480 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004481 "No declaration for attribute xmlns:%s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004482 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004483 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004484 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004485 "No declaration for attribute xmlns of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004486 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004487 }
4488 return(0);
4489 }
4490
4491 val = xmlValidateAttributeValue(attrDecl->atype, value);
4492 if (val == 0) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004493 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004494 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004495 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004496 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004497 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004498 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004499 "Syntax of value for attribute xmlns of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004500 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004501 }
4502 ret = 0;
4503 }
4504
4505 /* Validity constraint: Fixed Attribute Default */
4506 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4507 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004508 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004509 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004510 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4511 ns->prefix, elem->name, attrDecl->defaultValue);
4512 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004513 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004514 "Value for attribute xmlns of %s is different from default \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004515 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004516 }
4517 ret = 0;
4518 }
4519 }
4520
4521 /* Validity Constraint: ID uniqueness */
4522 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4523 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4524 ret = 0;
4525 }
4526
4527 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4528 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4529 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4530 ret = 0;
4531 }
4532
4533 /* Validity Constraint: Notation Attributes */
4534 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4535 xmlEnumerationPtr tree = attrDecl->tree;
4536 xmlNotationPtr nota;
4537
4538 /* First check that the given NOTATION was declared */
4539 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4540 if (nota == NULL)
4541 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4542
4543 if (nota == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004544 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004545 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004546 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4547 value, ns->prefix, elem->name);
4548 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004549 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004550 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004551 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004552 }
4553 ret = 0;
4554 }
4555
4556 /* Second, verify that it's among the list */
4557 while (tree != NULL) {
4558 if (xmlStrEqual(tree->name, value)) break;
4559 tree = tree->next;
4560 }
4561 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004562 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004563 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004564"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4565 value, ns->prefix, elem->name);
4566 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004567 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004568"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004569 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004570 }
4571 ret = 0;
4572 }
4573 }
4574
4575 /* Validity Constraint: Enumeration */
4576 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4577 xmlEnumerationPtr tree = attrDecl->tree;
4578 while (tree != NULL) {
4579 if (xmlStrEqual(tree->name, value)) break;
4580 tree = tree->next;
4581 }
4582 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004583 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004584 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004585"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4586 value, ns->prefix, elem->name);
4587 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004588 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004589"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004590 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004591 }
4592 ret = 0;
4593 }
4594 }
4595
4596 /* Fixed Attribute Default */
4597 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4598 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004599 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004600 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004601 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4602 ns->prefix, elem->name, attrDecl->defaultValue);
4603 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004604 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004605 "Value for attribute xmlns of %s must be \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004606 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004607 }
4608 ret = 0;
4609 }
4610
4611 /* Extra check for the attribute value */
4612 if (ns->prefix != NULL) {
4613 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4614 attrDecl->atype, value);
4615 } else {
4616 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4617 attrDecl->atype, value);
4618 }
4619
4620 return(ret);
4621}
4622
Daniel Veillard118aed72002-09-24 14:13:13 +00004623#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004624/**
4625 * xmlValidateSkipIgnorable:
4626 * @ctxt: the validation context
4627 * @child: the child list
4628 *
4629 * Skip ignorable elements w.r.t. the validation process
4630 *
4631 * returns the first element to consider for validation of the content model
4632 */
4633
4634static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004635xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004636 while (child != NULL) {
4637 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004638 /* These things are ignored (skipped) during validation. */
4639 case XML_PI_NODE:
4640 case XML_COMMENT_NODE:
4641 case XML_XINCLUDE_START:
4642 case XML_XINCLUDE_END:
4643 child = child->next;
4644 break;
4645 case XML_TEXT_NODE:
4646 if (xmlIsBlankNode(child))
4647 child = child->next;
4648 else
4649 return(child);
4650 break;
4651 /* keep current node */
4652 default:
4653 return(child);
4654 }
4655 }
4656 return(child);
4657}
4658
4659/**
4660 * xmlValidateElementType:
4661 * @ctxt: the validation context
4662 *
4663 * Try to validate the content model of an element internal function
4664 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004665 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4666 * reference is found and -3 if the validation succeeded but
4667 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004668 */
4669
4670static int
4671xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004672 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004673 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004674
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004675 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004676 if ((NODE == NULL) && (CONT == NULL))
4677 return(1);
4678 if ((NODE == NULL) &&
4679 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4680 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4681 return(1);
4682 }
4683 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004684 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004685 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004686
4687 /*
4688 * We arrive here when more states need to be examined
4689 */
4690cont:
4691
4692 /*
4693 * We just recovered from a rollback generated by a possible
4694 * epsilon transition, go directly to the analysis phase
4695 */
4696 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004697 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004698 DEBUG_VALID_STATE(NODE, CONT)
4699 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004700 goto analyze;
4701 }
4702
4703 DEBUG_VALID_STATE(NODE, CONT)
4704 /*
4705 * we may have to save a backup state here. This is the equivalent
4706 * of handling epsilon transition in NFAs.
4707 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004708 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004709 ((CONT->parent == NULL) ||
4710 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004711 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004712 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004713 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004714 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004715 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4716 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004717 }
4718
4719
4720 /*
4721 * Check first if the content matches
4722 */
4723 switch (CONT->type) {
4724 case XML_ELEMENT_CONTENT_PCDATA:
4725 if (NODE == NULL) {
4726 DEBUG_VALID_MSG("pcdata failed no node");
4727 ret = 0;
4728 break;
4729 }
4730 if (NODE->type == XML_TEXT_NODE) {
4731 DEBUG_VALID_MSG("pcdata found, skip to next");
4732 /*
4733 * go to next element in the content model
4734 * skipping ignorable elems
4735 */
4736 do {
4737 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004738 NODE = xmlValidateSkipIgnorable(NODE);
4739 if ((NODE != NULL) &&
4740 (NODE->type == XML_ENTITY_REF_NODE))
4741 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004742 } while ((NODE != NULL) &&
4743 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004744 (NODE->type != XML_TEXT_NODE) &&
4745 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004746 ret = 1;
4747 break;
4748 } else {
4749 DEBUG_VALID_MSG("pcdata failed");
4750 ret = 0;
4751 break;
4752 }
4753 break;
4754 case XML_ELEMENT_CONTENT_ELEMENT:
4755 if (NODE == NULL) {
4756 DEBUG_VALID_MSG("element failed no node");
4757 ret = 0;
4758 break;
4759 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004760 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4761 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004762 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004763 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4764 ret = (CONT->prefix == NULL);
4765 } else if (CONT->prefix == NULL) {
4766 ret = 0;
4767 } else {
4768 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4769 }
4770 }
4771 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004772 DEBUG_VALID_MSG("element found, skip to next");
4773 /*
4774 * go to next element in the content model
4775 * skipping ignorable elems
4776 */
4777 do {
4778 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004779 NODE = xmlValidateSkipIgnorable(NODE);
4780 if ((NODE != NULL) &&
4781 (NODE->type == XML_ENTITY_REF_NODE))
4782 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004783 } while ((NODE != NULL) &&
4784 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004785 (NODE->type != XML_TEXT_NODE) &&
4786 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004787 } else {
4788 DEBUG_VALID_MSG("element failed");
4789 ret = 0;
4790 break;
4791 }
4792 break;
4793 case XML_ELEMENT_CONTENT_OR:
4794 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004795 * Small optimization.
4796 */
4797 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4798 if ((NODE == NULL) ||
4799 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4800 DEPTH++;
4801 CONT = CONT->c2;
4802 goto cont;
4803 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004804 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4805 ret = (CONT->c1->prefix == NULL);
4806 } else if (CONT->c1->prefix == NULL) {
4807 ret = 0;
4808 } else {
4809 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4810 }
4811 if (ret == 0) {
4812 DEPTH++;
4813 CONT = CONT->c2;
4814 goto cont;
4815 }
Daniel Veillard85349052001-04-20 13:48:21 +00004816 }
4817
4818 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004819 * save the second branch 'or' branch
4820 */
4821 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004822 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4823 OCCURS, ROLLBACK_OR) < 0)
4824 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004825 DEPTH++;
4826 CONT = CONT->c1;
4827 goto cont;
4828 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004829 /*
4830 * Small optimization.
4831 */
4832 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4833 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4834 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4835 if ((NODE == NULL) ||
4836 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4837 DEPTH++;
4838 CONT = CONT->c2;
4839 goto cont;
4840 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004841 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4842 ret = (CONT->c1->prefix == NULL);
4843 } else if (CONT->c1->prefix == NULL) {
4844 ret = 0;
4845 } else {
4846 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4847 }
4848 if (ret == 0) {
4849 DEPTH++;
4850 CONT = CONT->c2;
4851 goto cont;
4852 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004853 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004854 DEPTH++;
4855 CONT = CONT->c1;
4856 goto cont;
4857 }
4858
4859 /*
4860 * At this point handle going up in the tree
4861 */
4862 if (ret == -1) {
4863 DEBUG_VALID_MSG("error found returning");
4864 return(ret);
4865 }
4866analyze:
4867 while (CONT != NULL) {
4868 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004869 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004870 * this level.
4871 */
4872 if (ret == 0) {
4873 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004874 xmlNodePtr cur;
4875
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004876 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004877 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004878 DEBUG_VALID_MSG("Once branch failed, rollback");
4879 if (vstateVPop(ctxt) < 0 ) {
4880 DEBUG_VALID_MSG("exhaustion, failed");
4881 return(0);
4882 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004883 if (cur != ctxt->vstate->node)
4884 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004885 goto cont;
4886 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004887 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004888 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004889 DEBUG_VALID_MSG("Plus branch failed, rollback");
4890 if (vstateVPop(ctxt) < 0 ) {
4891 DEBUG_VALID_MSG("exhaustion, failed");
4892 return(0);
4893 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004894 if (cur != ctxt->vstate->node)
4895 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004896 goto cont;
4897 }
4898 DEBUG_VALID_MSG("Plus branch found");
4899 ret = 1;
4900 break;
4901 case XML_ELEMENT_CONTENT_MULT:
4902#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004903 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004904 DEBUG_VALID_MSG("Mult branch failed");
4905 } else {
4906 DEBUG_VALID_MSG("Mult branch found");
4907 }
4908#endif
4909 ret = 1;
4910 break;
4911 case XML_ELEMENT_CONTENT_OPT:
4912 DEBUG_VALID_MSG("Option branch failed");
4913 ret = 1;
4914 break;
4915 }
4916 } else {
4917 switch (CONT->ocur) {
4918 case XML_ELEMENT_CONTENT_OPT:
4919 DEBUG_VALID_MSG("Option branch succeeded");
4920 ret = 1;
4921 break;
4922 case XML_ELEMENT_CONTENT_ONCE:
4923 DEBUG_VALID_MSG("Once branch succeeded");
4924 ret = 1;
4925 break;
4926 case XML_ELEMENT_CONTENT_PLUS:
4927 if (STATE == ROLLBACK_PARENT) {
4928 DEBUG_VALID_MSG("Plus branch rollback");
4929 ret = 1;
4930 break;
4931 }
4932 if (NODE == NULL) {
4933 DEBUG_VALID_MSG("Plus branch exhausted");
4934 ret = 1;
4935 break;
4936 }
4937 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004938 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004939 goto cont;
4940 case XML_ELEMENT_CONTENT_MULT:
4941 if (STATE == ROLLBACK_PARENT) {
4942 DEBUG_VALID_MSG("Mult branch rollback");
4943 ret = 1;
4944 break;
4945 }
4946 if (NODE == NULL) {
4947 DEBUG_VALID_MSG("Mult branch exhausted");
4948 ret = 1;
4949 break;
4950 }
4951 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004952 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004953 goto cont;
4954 }
4955 }
4956 STATE = 0;
4957
4958 /*
4959 * Then act accordingly at the parent level
4960 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004961 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004962 if (CONT->parent == NULL)
4963 break;
4964
4965 switch (CONT->parent->type) {
4966 case XML_ELEMENT_CONTENT_PCDATA:
4967 DEBUG_VALID_MSG("Error: parent pcdata");
4968 return(-1);
4969 case XML_ELEMENT_CONTENT_ELEMENT:
4970 DEBUG_VALID_MSG("Error: parent element");
4971 return(-1);
4972 case XML_ELEMENT_CONTENT_OR:
4973 if (ret == 1) {
4974 DEBUG_VALID_MSG("Or succeeded");
4975 CONT = CONT->parent;
4976 DEPTH--;
4977 } else {
4978 DEBUG_VALID_MSG("Or failed");
4979 CONT = CONT->parent;
4980 DEPTH--;
4981 }
4982 break;
4983 case XML_ELEMENT_CONTENT_SEQ:
4984 if (ret == 0) {
4985 DEBUG_VALID_MSG("Sequence failed");
4986 CONT = CONT->parent;
4987 DEPTH--;
4988 } else if (CONT == CONT->parent->c1) {
4989 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4990 CONT = CONT->parent->c2;
4991 goto cont;
4992 } else {
4993 DEBUG_VALID_MSG("Sequence succeeded");
4994 CONT = CONT->parent;
4995 DEPTH--;
4996 }
4997 }
4998 }
4999 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005000 xmlNodePtr cur;
5001
5002 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005003 DEBUG_VALID_MSG("Failed, remaining input, rollback");
5004 if (vstateVPop(ctxt) < 0 ) {
5005 DEBUG_VALID_MSG("exhaustion, failed");
5006 return(0);
5007 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005008 if (cur != ctxt->vstate->node)
5009 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005010 goto cont;
5011 }
5012 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005013 xmlNodePtr cur;
5014
5015 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005016 DEBUG_VALID_MSG("Failure, rollback");
5017 if (vstateVPop(ctxt) < 0 ) {
5018 DEBUG_VALID_MSG("exhaustion, failed");
5019 return(0);
5020 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005021 if (cur != ctxt->vstate->node)
5022 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005023 goto cont;
5024 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005025 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005026}
Daniel Veillard23e73572002-09-19 19:56:43 +00005027#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005028
5029/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00005030 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005031 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00005032 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005033 * @content: An element
5034 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
5035 *
5036 * This will dump the list of elements to the buffer
5037 * Intended just for the debug routine
5038 */
5039static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00005040xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005041 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00005042 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005043
5044 if (node == NULL) return;
5045 if (glob) strcat(buf, "(");
5046 cur = node;
5047 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005048 len = strlen(buf);
5049 if (size - len < 50) {
5050 if ((size - len > 4) && (buf[len - 1] != '.'))
5051 strcat(buf, " ...");
5052 return;
5053 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005054 switch (cur->type) {
5055 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005056 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005057 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005058 if ((size - len > 4) && (buf[len - 1] != '.'))
5059 strcat(buf, " ...");
5060 return;
5061 }
5062 strcat(buf, (char *) cur->ns->prefix);
5063 strcat(buf, ":");
5064 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005065 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005066 if ((size - len > 4) && (buf[len - 1] != '.'))
5067 strcat(buf, " ...");
5068 return;
5069 }
5070 strcat(buf, (char *) cur->name);
5071 if (cur->next != NULL)
5072 strcat(buf, " ");
5073 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005074 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005075 if (xmlIsBlankNode(cur))
5076 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005077 case XML_CDATA_SECTION_NODE:
5078 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005079 strcat(buf, "CDATA");
5080 if (cur->next != NULL)
5081 strcat(buf, " ");
5082 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005083 case XML_ATTRIBUTE_NODE:
5084 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005085#ifdef LIBXML_DOCB_ENABLED
5086 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005087#endif
5088 case XML_HTML_DOCUMENT_NODE:
5089 case XML_DOCUMENT_TYPE_NODE:
5090 case XML_DOCUMENT_FRAG_NODE:
5091 case XML_NOTATION_NODE:
5092 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005093 strcat(buf, "???");
5094 if (cur->next != NULL)
5095 strcat(buf, " ");
5096 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005097 case XML_ENTITY_NODE:
5098 case XML_PI_NODE:
5099 case XML_DTD_NODE:
5100 case XML_COMMENT_NODE:
5101 case XML_ELEMENT_DECL:
5102 case XML_ATTRIBUTE_DECL:
5103 case XML_ENTITY_DECL:
5104 case XML_XINCLUDE_START:
5105 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005106 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005107 }
5108 cur = cur->next;
5109 }
5110 if (glob) strcat(buf, ")");
5111}
5112
5113/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005114 * xmlValidateElementContent:
5115 * @ctxt: the validation context
5116 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005117 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005118 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005119 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005120 *
5121 * Try to validate the content model of an element
5122 *
5123 * returns 1 if valid or 0 if not and -1 in case of error
5124 */
5125
5126static int
5127xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005128 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00005129 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00005130#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00005131 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00005132#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00005133 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005134 xmlElementContentPtr cont;
5135 const xmlChar *name;
5136
5137 if (elemDecl == NULL)
5138 return(-1);
5139 cont = elemDecl->content;
5140 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005141
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005142#ifdef LIBXML_REGEXP_ENABLED
5143 /* Build the regexp associated to the content model */
5144 if (elemDecl->contModel == NULL)
5145 ret = xmlValidBuildContentModel(ctxt, elemDecl);
5146 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005147 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005148 } else {
5149 xmlRegExecCtxtPtr exec;
5150
Daniel Veillardec498e12003-02-05 11:01:50 +00005151 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
5152 return(-1);
5153 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005154 ctxt->nodeMax = 0;
5155 ctxt->nodeNr = 0;
5156 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005157 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
5158 if (exec != NULL) {
5159 cur = child;
5160 while (cur != NULL) {
5161 switch (cur->type) {
5162 case XML_ENTITY_REF_NODE:
5163 /*
5164 * Push the current node to be able to roll back
5165 * and process within the entity
5166 */
5167 if ((cur->children != NULL) &&
5168 (cur->children->children != NULL)) {
5169 nodeVPush(ctxt, cur);
5170 cur = cur->children->children;
5171 continue;
5172 }
5173 break;
5174 case XML_TEXT_NODE:
5175 if (xmlIsBlankNode(cur))
5176 break;
5177 ret = 0;
5178 goto fail;
5179 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00005180 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005181 ret = 0;
5182 goto fail;
5183 case XML_ELEMENT_NODE:
5184 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005185 xmlChar fn[50];
5186 xmlChar *fullname;
5187
5188 fullname = xmlBuildQName(cur->name,
5189 cur->ns->prefix, fn, 50);
5190 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005191 ret = -1;
5192 goto fail;
5193 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005194 ret = xmlRegExecPushString(exec, fullname, NULL);
5195 if ((fullname != fn) && (fullname != cur->name))
5196 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005197 } else {
5198 ret = xmlRegExecPushString(exec, cur->name, NULL);
5199 }
5200 break;
5201 default:
5202 break;
5203 }
5204 /*
5205 * Switch to next element
5206 */
5207 cur = cur->next;
5208 while (cur == NULL) {
5209 cur = nodeVPop(ctxt);
5210 if (cur == NULL)
5211 break;
5212 cur = cur->next;
5213 }
5214 }
5215 ret = xmlRegExecPushString(exec, NULL, NULL);
5216fail:
5217 xmlRegFreeExecCtxt(exec);
5218 }
5219 }
5220#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005221 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005222 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005223 */
5224 ctxt->vstateMax = 8;
5225 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5226 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5227 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005228 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005229 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005230 }
5231 /*
5232 * The first entry in the stack is reserved to the current state
5233 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00005234 ctxt->nodeMax = 0;
5235 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00005236 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005237 ctxt->vstate = &ctxt->vstateTab[0];
5238 ctxt->vstateNr = 1;
5239 CONT = cont;
5240 NODE = child;
5241 DEPTH = 0;
5242 OCCURS = 0;
5243 STATE = 0;
5244 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005245 if ((ret == -3) && (warn)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005246 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
5247 "Content model for Element %s is ambiguous\n",
Daniel Veillard0cc72772003-10-13 14:00:21 +00005248 name, NULL, NULL);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005249 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005250 /*
5251 * An entities reference appeared at this level.
5252 * Buid a minimal representation of this node content
5253 * sufficient to run the validation process on it
5254 */
5255 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005256 cur = child;
5257 while (cur != NULL) {
5258 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005259 case XML_ENTITY_REF_NODE:
5260 /*
5261 * Push the current node to be able to roll back
5262 * and process within the entity
5263 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005264 if ((cur->children != NULL) &&
5265 (cur->children->children != NULL)) {
5266 nodeVPush(ctxt, cur);
5267 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005268 continue;
5269 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00005270 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005271 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005272 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005273 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005274 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005275 case XML_CDATA_SECTION_NODE:
5276 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005277 case XML_ELEMENT_NODE:
5278 /*
5279 * Allocate a new node and minimally fills in
5280 * what's required
5281 */
5282 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5283 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005284 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005285 xmlFreeNodeList(repl);
5286 ret = -1;
5287 goto done;
5288 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005289 tmp->type = cur->type;
5290 tmp->name = cur->name;
5291 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005292 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00005293 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005294 if (repl == NULL)
5295 repl = last = tmp;
5296 else {
5297 last->next = tmp;
5298 last = tmp;
5299 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005300 if (cur->type == XML_CDATA_SECTION_NODE) {
5301 /*
5302 * E59 spaces in CDATA does not match the
5303 * nonterminal S
5304 */
5305 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5306 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005307 break;
5308 default:
5309 break;
5310 }
5311 /*
5312 * Switch to next element
5313 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005314 cur = cur->next;
5315 while (cur == NULL) {
5316 cur = nodeVPop(ctxt);
5317 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005318 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005319 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005320 }
5321 }
5322
5323 /*
5324 * Relaunch the validation
5325 */
5326 ctxt->vstate = &ctxt->vstateTab[0];
5327 ctxt->vstateNr = 1;
5328 CONT = cont;
5329 NODE = repl;
5330 DEPTH = 0;
5331 OCCURS = 0;
5332 STATE = 0;
5333 ret = xmlValidateElementType(ctxt);
5334 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005335#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005336 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005337 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
5338 char expr[5000];
5339 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005340
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005341 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005342 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005343 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00005344#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005345 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005346 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005347 else
Daniel Veillard01992e02002-10-09 10:20:30 +00005348#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005349 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005350
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005351 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005352 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5353 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5354 name, BAD_CAST expr, BAD_CAST list);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005355 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005356 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5357 "Element content does not follow the DTD, expecting %s, got %s\n",
5358 BAD_CAST expr, BAD_CAST list, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005359 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005360 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005361 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005362 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005363 "Element %s content does not follow the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005364 name, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005365 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005366 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5367 "Element content does not follow the DTD\n",
5368 NULL, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005369 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005370 }
5371 ret = 0;
5372 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005373 if (ret == -3)
5374 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005375
Daniel Veillard23e73572002-09-19 19:56:43 +00005376#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005377done:
5378 /*
5379 * Deallocate the copy if done, and free up the validation stack
5380 */
5381 while (repl != NULL) {
5382 tmp = repl->next;
5383 xmlFree(repl);
5384 repl = tmp;
5385 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005386 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005387 if (ctxt->vstateTab != NULL) {
5388 xmlFree(ctxt->vstateTab);
5389 ctxt->vstateTab = NULL;
5390 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005391#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005392 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00005393 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005394 if (ctxt->nodeTab != NULL) {
5395 xmlFree(ctxt->nodeTab);
5396 ctxt->nodeTab = NULL;
5397 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005398 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005399
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005400}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005401
Owen Taylor3473f882001-02-23 17:55:21 +00005402/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005403 * xmlValidateCdataElement:
5404 * @ctxt: the validation context
5405 * @doc: a document instance
5406 * @elem: an element instance
5407 *
5408 * Check that an element follows #CDATA
5409 *
5410 * returns 1 if valid or 0 otherwise
5411 */
5412static int
5413xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5414 xmlNodePtr elem) {
5415 int ret = 1;
5416 xmlNodePtr cur, child;
5417
Daniel Veillardceb09b92002-10-04 11:46:37 +00005418 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005419 return(0);
5420
5421 child = elem->children;
5422
5423 cur = child;
5424 while (cur != NULL) {
5425 switch (cur->type) {
5426 case XML_ENTITY_REF_NODE:
5427 /*
5428 * Push the current node to be able to roll back
5429 * and process within the entity
5430 */
5431 if ((cur->children != NULL) &&
5432 (cur->children->children != NULL)) {
5433 nodeVPush(ctxt, cur);
5434 cur = cur->children->children;
5435 continue;
5436 }
5437 break;
5438 case XML_COMMENT_NODE:
5439 case XML_PI_NODE:
5440 case XML_TEXT_NODE:
5441 case XML_CDATA_SECTION_NODE:
5442 break;
5443 default:
5444 ret = 0;
5445 goto done;
5446 }
5447 /*
5448 * Switch to next element
5449 */
5450 cur = cur->next;
5451 while (cur == NULL) {
5452 cur = nodeVPop(ctxt);
5453 if (cur == NULL)
5454 break;
5455 cur = cur->next;
5456 }
5457 }
5458done:
5459 ctxt->nodeMax = 0;
5460 ctxt->nodeNr = 0;
5461 if (ctxt->nodeTab != NULL) {
5462 xmlFree(ctxt->nodeTab);
5463 ctxt->nodeTab = NULL;
5464 }
5465 return(ret);
5466}
5467
5468/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005469 * xmlValidateCheckMixed:
5470 * @ctxt: the validation context
5471 * @cont: the mixed content model
5472 * @qname: the qualified name as appearing in the serialization
5473 *
5474 * Check if the given node is part of the content model.
5475 *
5476 * Returns 1 if yes, 0 if no, -1 in case of error
5477 */
5478static int
William M. Brackedb65a72004-02-06 07:36:04 +00005479xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005480 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005481 const xmlChar *name;
5482 int plen;
5483 name = xmlSplitQName3(qname, &plen);
5484
5485 if (name == NULL) {
5486 while (cont != NULL) {
5487 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5488 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5489 return(1);
5490 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5491 (cont->c1 != NULL) &&
5492 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5493 if ((cont->c1->prefix == NULL) &&
5494 (xmlStrEqual(cont->c1->name, qname)))
5495 return(1);
5496 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5497 (cont->c1 == NULL) ||
5498 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005499 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5500 "Internal: MIXED struct corrupted\n",
5501 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005502 break;
5503 }
5504 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005505 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005506 } else {
5507 while (cont != NULL) {
5508 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5509 if ((cont->prefix != NULL) &&
5510 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5511 (xmlStrEqual(cont->name, name)))
5512 return(1);
5513 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5514 (cont->c1 != NULL) &&
5515 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5516 if ((cont->c1->prefix != NULL) &&
5517 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5518 (xmlStrEqual(cont->c1->name, name)))
5519 return(1);
5520 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5521 (cont->c1 == NULL) ||
5522 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005523 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5524 "Internal: MIXED struct corrupted\n",
5525 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005526 break;
5527 }
5528 cont = cont->c2;
5529 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005530 }
5531 return(0);
5532}
5533
5534/**
5535 * xmlValidGetElemDecl:
5536 * @ctxt: the validation context
5537 * @doc: a document instance
5538 * @elem: an element instance
5539 * @extsubset: pointer, (out) indicate if the declaration was found
5540 * in the external subset.
5541 *
5542 * Finds a declaration associated to an element in the document.
5543 *
5544 * returns the pointer to the declaration or NULL if not found.
5545 */
5546static xmlElementPtr
5547xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5548 xmlNodePtr elem, int *extsubset) {
5549 xmlElementPtr elemDecl = NULL;
5550 const xmlChar *prefix = NULL;
5551
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005552 if ((ctxt == NULL) || (doc == NULL) ||
5553 (elem == NULL) || (elem->name == NULL))
5554 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005555 if (extsubset != NULL)
5556 *extsubset = 0;
5557
5558 /*
5559 * Fetch the declaration for the qualified name
5560 */
5561 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5562 prefix = elem->ns->prefix;
5563
5564 if (prefix != NULL) {
5565 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5566 elem->name, prefix);
5567 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5568 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5569 elem->name, prefix);
5570 if ((elemDecl != NULL) && (extsubset != NULL))
5571 *extsubset = 1;
5572 }
5573 }
5574
5575 /*
5576 * Fetch the declaration for the non qualified name
5577 * This is "non-strict" validation should be done on the
5578 * full QName but in that case being flexible makes sense.
5579 */
5580 if (elemDecl == NULL) {
5581 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5582 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5583 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5584 if ((elemDecl != NULL) && (extsubset != NULL))
5585 *extsubset = 1;
5586 }
5587 }
5588 if (elemDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005589 xmlErrValidNode(ctxt, elem,
5590 XML_DTD_UNKNOWN_ELEM,
5591 "No declaration for element %s\n",
5592 elem->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005593 }
5594 return(elemDecl);
5595}
5596
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005597#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005598/**
5599 * xmlValidatePushElement:
5600 * @ctxt: the validation context
5601 * @doc: a document instance
5602 * @elem: an element instance
5603 * @qname: the qualified name as appearing in the serialization
5604 *
5605 * Push a new element start on the validation stack.
5606 *
5607 * returns 1 if no validation problem was found or 0 otherwise
5608 */
5609int
5610xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5611 xmlNodePtr elem, const xmlChar *qname) {
5612 int ret = 1;
5613 xmlElementPtr eDecl;
5614 int extsubset = 0;
5615
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005616 if (ctxt == NULL)
5617 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005618/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005619 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5620 xmlValidStatePtr state = ctxt->vstate;
5621 xmlElementPtr elemDecl;
5622
5623 /*
5624 * Check the new element agaisnt the content model of the new elem.
5625 */
5626 if (state->elemDecl != NULL) {
5627 elemDecl = state->elemDecl;
5628
5629 switch(elemDecl->etype) {
5630 case XML_ELEMENT_TYPE_UNDEFINED:
5631 ret = 0;
5632 break;
5633 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005634 xmlErrValidNode(ctxt, state->node,
5635 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005636 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005637 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005638 ret = 0;
5639 break;
5640 case XML_ELEMENT_TYPE_ANY:
5641 /* I don't think anything is required then */
5642 break;
5643 case XML_ELEMENT_TYPE_MIXED:
5644 /* simple case of declared as #PCDATA */
5645 if ((elemDecl->content != NULL) &&
5646 (elemDecl->content->type ==
5647 XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005648 xmlErrValidNode(ctxt, state->node,
5649 XML_DTD_NOT_PCDATA,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005650 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005651 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005652 ret = 0;
5653 } else {
5654 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5655 qname);
5656 if (ret != 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005657 xmlErrValidNode(ctxt, state->node,
5658 XML_DTD_INVALID_CHILD,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005659 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005660 qname, state->node->name, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005661 }
5662 }
5663 break;
5664 case XML_ELEMENT_TYPE_ELEMENT:
5665 /*
5666 * TODO:
5667 * VC: Standalone Document Declaration
5668 * - element types with element content, if white space
5669 * occurs directly within any instance of those types.
5670 */
5671 if (state->exec != NULL) {
5672 ret = xmlRegExecPushString(state->exec, qname, NULL);
5673 if (ret < 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005674 xmlErrValidNode(ctxt, state->node,
5675 XML_DTD_CONTENT_MODEL,
5676 "Element %s content does not follow the DTD, Misplaced %s\n",
5677 state->node->name, qname, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005678 ret = 0;
5679 } else {
5680 ret = 1;
5681 }
5682 }
5683 break;
5684 }
5685 }
5686 }
5687 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5688 vstateVPush(ctxt, eDecl, elem);
5689 return(ret);
5690}
5691
5692/**
5693 * xmlValidatePushCData:
5694 * @ctxt: the validation context
5695 * @data: some character data read
5696 * @len: the lenght of the data
5697 *
5698 * check the CData parsed for validation in the current stack
5699 *
5700 * returns 1 if no validation problem was found or 0 otherwise
5701 */
5702int
5703xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5704 int ret = 1;
5705
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005706/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005707 if (ctxt == NULL)
5708 return(0);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005709 if (len <= 0)
5710 return(ret);
5711 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5712 xmlValidStatePtr state = ctxt->vstate;
5713 xmlElementPtr elemDecl;
5714
5715 /*
5716 * Check the new element agaisnt the content model of the new elem.
5717 */
5718 if (state->elemDecl != NULL) {
5719 elemDecl = state->elemDecl;
5720
5721 switch(elemDecl->etype) {
5722 case XML_ELEMENT_TYPE_UNDEFINED:
5723 ret = 0;
5724 break;
5725 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005726 xmlErrValidNode(ctxt, state->node,
5727 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005728 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005729 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005730 ret = 0;
5731 break;
5732 case XML_ELEMENT_TYPE_ANY:
5733 break;
5734 case XML_ELEMENT_TYPE_MIXED:
5735 break;
5736 case XML_ELEMENT_TYPE_ELEMENT:
5737 if (len > 0) {
5738 int i;
5739
5740 for (i = 0;i < len;i++) {
William M. Brack76e95df2003-10-18 16:20:14 +00005741 if (!IS_BLANK_CH(data[i])) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005742 xmlErrValidNode(ctxt, state->node,
5743 XML_DTD_CONTENT_MODEL,
5744 "Element %s content does not follow the DTD, Text not allowed\n",
5745 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005746 ret = 0;
5747 goto done;
5748 }
5749 }
5750 /*
5751 * TODO:
5752 * VC: Standalone Document Declaration
5753 * element types with element content, if white space
5754 * occurs directly within any instance of those types.
5755 */
5756 }
5757 break;
5758 }
5759 }
5760 }
5761done:
5762 return(ret);
5763}
5764
5765/**
5766 * xmlValidatePopElement:
5767 * @ctxt: the validation context
5768 * @doc: a document instance
5769 * @elem: an element instance
5770 * @qname: the qualified name as appearing in the serialization
5771 *
5772 * Pop the element end from the validation stack.
5773 *
5774 * returns 1 if no validation problem was found or 0 otherwise
5775 */
5776int
5777xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005778 xmlNodePtr elem ATTRIBUTE_UNUSED,
5779 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005780 int ret = 1;
5781
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005782 if (ctxt == NULL)
5783 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005784/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005785 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5786 xmlValidStatePtr state = ctxt->vstate;
5787 xmlElementPtr elemDecl;
5788
5789 /*
5790 * Check the new element agaisnt the content model of the new elem.
5791 */
5792 if (state->elemDecl != NULL) {
5793 elemDecl = state->elemDecl;
5794
5795 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5796 if (state->exec != NULL) {
5797 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5798 if (ret == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005799 xmlErrValidNode(ctxt, state->node,
5800 XML_DTD_CONTENT_MODEL,
5801 "Element %s content does not follow the DTD, Expecting more child\n",
5802 state->node->name, NULL,NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005803 } else {
5804 /*
5805 * previous validation errors should not generate
5806 * a new one here
5807 */
5808 ret = 1;
5809 }
5810 }
5811 }
5812 }
5813 vstateVPop(ctxt);
5814 }
5815 return(ret);
5816}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005817#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005818
5819/**
Owen Taylor3473f882001-02-23 17:55:21 +00005820 * xmlValidateOneElement:
5821 * @ctxt: the validation context
5822 * @doc: a document instance
5823 * @elem: an element instance
5824 *
5825 * Try to validate a single element and it's attributes,
5826 * basically it does the following checks as described by the
5827 * XML-1.0 recommendation:
5828 * - [ VC: Element Valid ]
5829 * - [ VC: Required Attribute ]
5830 * Then call xmlValidateOneAttribute() for each attribute present.
5831 *
5832 * The ID/IDREF checkings are done separately
5833 *
5834 * returns 1 if valid or 0 otherwise
5835 */
5836
5837int
5838xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5839 xmlNodePtr elem) {
5840 xmlElementPtr elemDecl = NULL;
5841 xmlElementContentPtr cont;
5842 xmlAttributePtr attr;
5843 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005844 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005845 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005846 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005847
5848 CHECK_DTD;
5849
5850 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005851 switch (elem->type) {
5852 case XML_ATTRIBUTE_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005853 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5854 "Attribute element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005855 return(0);
5856 case XML_TEXT_NODE:
5857 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005858 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5859 "Text element has children !\n",
5860 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005861 return(0);
5862 }
Owen Taylor3473f882001-02-23 17:55:21 +00005863 if (elem->ns != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005864 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5865 "Text element has namespace !\n",
5866 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005867 return(0);
5868 }
Owen Taylor3473f882001-02-23 17:55:21 +00005869 if (elem->content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005870 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5871 "Text element has no content !\n",
5872 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005873 return(0);
5874 }
5875 return(1);
5876 case XML_XINCLUDE_START:
5877 case XML_XINCLUDE_END:
5878 return(1);
5879 case XML_CDATA_SECTION_NODE:
5880 case XML_ENTITY_REF_NODE:
5881 case XML_PI_NODE:
5882 case XML_COMMENT_NODE:
5883 return(1);
5884 case XML_ENTITY_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005885 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5886 "Entity element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005887 return(0);
5888 case XML_NOTATION_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005889 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5890 "Notation element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005891 return(0);
5892 case XML_DOCUMENT_NODE:
5893 case XML_DOCUMENT_TYPE_NODE:
5894 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005895 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5896 "Document element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005897 return(0);
5898 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005899 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5900 "HTML Document not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005901 return(0);
5902 case XML_ELEMENT_NODE:
5903 break;
5904 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005905 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5906 "unknown element type\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005907 return(0);
5908 }
Owen Taylor3473f882001-02-23 17:55:21 +00005909
5910 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005911 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005912 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005913 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5914 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005915 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005916
Daniel Veillardea7751d2002-12-20 00:16:24 +00005917 /*
5918 * If vstateNr is not zero that means continuous validation is
5919 * activated, do not try to check the content model at that level.
5920 */
5921 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005922 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005923 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005924 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005925 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
5926 "No declaration for element %s\n",
5927 elem->name, NULL, NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005928 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005929 case XML_ELEMENT_TYPE_EMPTY:
5930 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005931 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
Owen Taylor3473f882001-02-23 17:55:21 +00005932 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005933 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005934 ret = 0;
5935 }
5936 break;
5937 case XML_ELEMENT_TYPE_ANY:
5938 /* I don't think anything is required then */
5939 break;
5940 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005941
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005942 /* simple case of declared as #PCDATA */
5943 if ((elemDecl->content != NULL) &&
5944 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5945 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5946 if (!ret) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005947 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005948 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005949 elem->name, NULL, NULL);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005950 }
5951 break;
5952 }
Owen Taylor3473f882001-02-23 17:55:21 +00005953 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005954 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005955 while (child != NULL) {
5956 if (child->type == XML_ELEMENT_NODE) {
5957 name = child->name;
5958 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005959 xmlChar fn[50];
5960 xmlChar *fullname;
5961
5962 fullname = xmlBuildQName(child->name, child->ns->prefix,
5963 fn, 50);
5964 if (fullname == NULL)
5965 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005966 cont = elemDecl->content;
5967 while (cont != NULL) {
5968 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005969 if (xmlStrEqual(cont->name, fullname))
5970 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005971 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5972 (cont->c1 != NULL) &&
5973 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005974 if (xmlStrEqual(cont->c1->name, fullname))
5975 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005976 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5977 (cont->c1 == NULL) ||
5978 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005979 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5980 "Internal: MIXED struct corrupted\n",
5981 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005982 break;
5983 }
5984 cont = cont->c2;
5985 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005986 if ((fullname != fn) && (fullname != child->name))
5987 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005988 if (cont != NULL)
5989 goto child_ok;
5990 }
5991 cont = elemDecl->content;
5992 while (cont != NULL) {
5993 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5994 if (xmlStrEqual(cont->name, name)) break;
5995 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5996 (cont->c1 != NULL) &&
5997 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5998 if (xmlStrEqual(cont->c1->name, name)) break;
5999 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6000 (cont->c1 == NULL) ||
6001 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006002 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
6003 "Internal: MIXED struct corrupted\n",
6004 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006005 break;
6006 }
6007 cont = cont->c2;
6008 }
6009 if (cont == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006010 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006011 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006012 name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006013 ret = 0;
6014 }
6015 }
6016child_ok:
6017 child = child->next;
6018 }
6019 break;
6020 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006021 if ((doc->standalone == 1) && (extsubset == 1)) {
6022 /*
6023 * VC: Standalone Document Declaration
6024 * - element types with element content, if white space
6025 * occurs directly within any instance of those types.
6026 */
6027 child = elem->children;
6028 while (child != NULL) {
6029 if (child->type == XML_TEXT_NODE) {
6030 const xmlChar *content = child->content;
6031
William M. Brack76e95df2003-10-18 16:20:14 +00006032 while (IS_BLANK_CH(*content))
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006033 content++;
6034 if (*content == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006035 xmlErrValidNode(ctxt, elem,
6036 XML_DTD_STANDALONE_WHITE_SPACE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006037"standalone: %s declared in the external subset contains white spaces nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006038 elem->name, NULL, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006039 ret = 0;
6040 break;
6041 }
6042 }
6043 child =child->next;
6044 }
6045 }
Owen Taylor3473f882001-02-23 17:55:21 +00006046 child = elem->children;
6047 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00006048 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006049 if (tmp <= 0)
6050 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00006051 break;
6052 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00006053 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00006054
6055 /* [ VC: Required Attribute ] */
6056 attr = elemDecl->attributes;
6057 while (attr != NULL) {
6058 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00006059 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006060
Daniel Veillarde4301c82002-02-13 13:32:35 +00006061 if ((attr->prefix == NULL) &&
6062 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6063 xmlNsPtr ns;
6064
6065 ns = elem->nsDef;
6066 while (ns != NULL) {
6067 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006068 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006069 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006070 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006071 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6072 xmlNsPtr ns;
6073
6074 ns = elem->nsDef;
6075 while (ns != NULL) {
6076 if (xmlStrEqual(attr->name, ns->prefix))
6077 goto found;
6078 ns = ns->next;
6079 }
6080 } else {
6081 xmlAttrPtr attrib;
6082
6083 attrib = elem->properties;
6084 while (attrib != NULL) {
6085 if (xmlStrEqual(attrib->name, attr->name)) {
6086 if (attr->prefix != NULL) {
6087 xmlNsPtr nameSpace = attrib->ns;
6088
6089 if (nameSpace == NULL)
6090 nameSpace = elem->ns;
6091 /*
6092 * qualified names handling is problematic, having a
6093 * different prefix should be possible but DTDs don't
6094 * allow to define the URI instead of the prefix :-(
6095 */
6096 if (nameSpace == NULL) {
6097 if (qualified < 0)
6098 qualified = 0;
6099 } else if (!xmlStrEqual(nameSpace->prefix,
6100 attr->prefix)) {
6101 if (qualified < 1)
6102 qualified = 1;
6103 } else
6104 goto found;
6105 } else {
6106 /*
6107 * We should allow applications to define namespaces
6108 * for their application even if the DTD doesn't
6109 * carry one, otherwise, basically we would always
6110 * break.
6111 */
6112 goto found;
6113 }
6114 }
6115 attrib = attrib->next;
6116 }
Owen Taylor3473f882001-02-23 17:55:21 +00006117 }
6118 if (qualified == -1) {
6119 if (attr->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006120 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006121 "Element %s does not carry attribute %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006122 elem->name, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006123 ret = 0;
6124 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006125 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006126 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00006127 elem->name, attr->prefix,attr->name);
6128 ret = 0;
6129 }
6130 } else if (qualified == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006131 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006132 "Element %s required attribute %s:%s has no prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006133 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006134 } else if (qualified == 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006135 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006136 "Element %s required attribute %s:%s has different prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006137 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006138 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006139 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
6140 /*
6141 * Special tests checking #FIXED namespace declarations
6142 * have the right value since this is not done as an
6143 * attribute checking
6144 */
6145 if ((attr->prefix == NULL) &&
6146 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6147 xmlNsPtr ns;
6148
6149 ns = elem->nsDef;
6150 while (ns != NULL) {
6151 if (ns->prefix == NULL) {
6152 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006153 xmlErrValidNode(ctxt, elem,
6154 XML_DTD_ELEM_DEFAULT_NAMESPACE,
Daniel Veillarde4301c82002-02-13 13:32:35 +00006155 "Element %s namespace name for default namespace does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006156 elem->name, NULL, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006157 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006158 }
6159 goto found;
6160 }
6161 ns = ns->next;
6162 }
6163 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6164 xmlNsPtr ns;
6165
6166 ns = elem->nsDef;
6167 while (ns != NULL) {
6168 if (xmlStrEqual(attr->name, ns->prefix)) {
6169 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006170 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006171 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006172 elem->name, ns->prefix, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006173 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006174 }
6175 goto found;
6176 }
6177 ns = ns->next;
6178 }
6179 }
Owen Taylor3473f882001-02-23 17:55:21 +00006180 }
6181found:
6182 attr = attr->nexth;
6183 }
6184 return(ret);
6185}
6186
6187/**
6188 * xmlValidateRoot:
6189 * @ctxt: the validation context
6190 * @doc: a document instance
6191 *
6192 * Try to validate a the root element
6193 * basically it does the following check as described by the
6194 * XML-1.0 recommendation:
6195 * - [ VC: Root Element Type ]
6196 * it doesn't try to recurse or apply other check to the element
6197 *
6198 * returns 1 if valid or 0 otherwise
6199 */
6200
6201int
6202xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6203 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00006204 int ret;
6205
Owen Taylor3473f882001-02-23 17:55:21 +00006206 if (doc == NULL) return(0);
6207
6208 root = xmlDocGetRootElement(doc);
6209 if ((root == NULL) || (root->name == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006210 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
6211 "no root element\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006212 return(0);
6213 }
6214
6215 /*
6216 * When doing post validation against a separate DTD, those may
6217 * no internal subset has been generated
6218 */
6219 if ((doc->intSubset != NULL) &&
6220 (doc->intSubset->name != NULL)) {
6221 /*
6222 * Check first the document root against the NQName
6223 */
6224 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
6225 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006226 xmlChar fn[50];
6227 xmlChar *fullname;
6228
6229 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6230 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006231 xmlVErrMemory(ctxt, NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00006232 return(0);
6233 }
6234 ret = xmlStrEqual(doc->intSubset->name, fullname);
6235 if ((fullname != fn) && (fullname != root->name))
6236 xmlFree(fullname);
6237 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00006238 goto name_ok;
6239 }
6240 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6241 (xmlStrEqual(root->name, BAD_CAST "html")))
6242 goto name_ok;
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006243 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6244 "root and DTD name do not match '%s' and '%s'\n",
6245 root->name, doc->intSubset->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006246 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006247 }
6248 }
6249name_ok:
6250 return(1);
6251}
6252
6253
6254/**
6255 * xmlValidateElement:
6256 * @ctxt: the validation context
6257 * @doc: a document instance
6258 * @elem: an element instance
6259 *
6260 * Try to validate the subtree under an element
6261 *
6262 * returns 1 if valid or 0 otherwise
6263 */
6264
6265int
6266xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
6267 xmlNodePtr child;
6268 xmlAttrPtr attr;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006269 xmlNsPtr ns;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006270 const xmlChar *value;
Owen Taylor3473f882001-02-23 17:55:21 +00006271 int ret = 1;
6272
6273 if (elem == NULL) return(0);
6274
6275 /*
6276 * XInclude elements were added after parsing in the infoset,
6277 * they don't really mean anything validation wise.
6278 */
6279 if ((elem->type == XML_XINCLUDE_START) ||
6280 (elem->type == XML_XINCLUDE_END))
6281 return(1);
6282
6283 CHECK_DTD;
6284
Daniel Veillard10ea86c2001-06-20 13:55:33 +00006285 /*
6286 * Entities references have to be handled separately
6287 */
6288 if (elem->type == XML_ENTITY_REF_NODE) {
6289 return(1);
6290 }
6291
Owen Taylor3473f882001-02-23 17:55:21 +00006292 ret &= xmlValidateOneElement(ctxt, doc, elem);
Daniel Veillard8874b942005-08-25 13:19:21 +00006293 if (elem->type == XML_ELEMENT_NODE) {
6294 attr = elem->properties;
6295 while (attr != NULL) {
6296 value = xmlNodeListGetString(doc, attr->children, 0);
6297 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6298 if (value != NULL)
6299 xmlFree((char *)value);
6300 attr= attr->next;
6301 }
6302 ns = elem->nsDef;
6303 while (ns != NULL) {
6304 if (elem->ns == NULL)
6305 ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
6306 ns, ns->href);
6307 else
6308 ret &= xmlValidateOneNamespace(ctxt, doc, elem,
6309 elem->ns->prefix, ns, ns->href);
6310 ns = ns->next;
6311 }
Daniel Veillarde133dd82003-10-30 10:42:20 +00006312 }
Owen Taylor3473f882001-02-23 17:55:21 +00006313 child = elem->children;
6314 while (child != NULL) {
6315 ret &= xmlValidateElement(ctxt, doc, child);
6316 child = child->next;
6317 }
6318
6319 return(ret);
6320}
6321
Daniel Veillard8730c562001-02-26 10:49:57 +00006322/**
6323 * xmlValidateRef:
6324 * @ref: A reference to be validated
6325 * @ctxt: Validation context
6326 * @name: Name of ID we are searching for
6327 *
6328 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006329static void
Daniel Veillard8730c562001-02-26 10:49:57 +00006330xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006331 const xmlChar *name) {
6332 xmlAttrPtr id;
6333 xmlAttrPtr attr;
6334
6335 if (ref == NULL)
6336 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006337 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006338 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006339 attr = ref->attr;
6340 if (attr == NULL) {
6341 xmlChar *dup, *str = NULL, *cur, save;
6342
6343 dup = xmlStrdup(name);
6344 if (dup == NULL) {
6345 ctxt->valid = 0;
6346 return;
6347 }
6348 cur = dup;
6349 while (*cur != 0) {
6350 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006351 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006352 save = *cur;
6353 *cur = 0;
6354 id = xmlGetID(ctxt->doc, str);
6355 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006356 xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006357 "attribute %s line %d references an unknown ID \"%s\"\n",
6358 ref->name, ref->lineno, str);
6359 ctxt->valid = 0;
6360 }
6361 if (save == 0)
6362 break;
6363 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006364 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006365 }
6366 xmlFree(dup);
6367 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00006368 id = xmlGetID(ctxt->doc, name);
6369 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006370 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006371 "IDREF attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006372 attr->name, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006373 ctxt->valid = 0;
6374 }
6375 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6376 xmlChar *dup, *str = NULL, *cur, save;
6377
6378 dup = xmlStrdup(name);
6379 if (dup == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006380 xmlVErrMemory(ctxt, "IDREFS split");
Owen Taylor3473f882001-02-23 17:55:21 +00006381 ctxt->valid = 0;
6382 return;
6383 }
6384 cur = dup;
6385 while (*cur != 0) {
6386 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006387 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006388 save = *cur;
6389 *cur = 0;
6390 id = xmlGetID(ctxt->doc, str);
6391 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006392 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006393 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006394 attr->name, str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006395 ctxt->valid = 0;
6396 }
6397 if (save == 0)
6398 break;
6399 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006400 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006401 }
6402 xmlFree(dup);
6403 }
6404}
6405
6406/**
Daniel Veillard8730c562001-02-26 10:49:57 +00006407 * xmlWalkValidateList:
6408 * @data: Contents of current link
6409 * @user: Value supplied by the user
6410 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006411 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00006412 */
6413static int
6414xmlWalkValidateList(const void *data, const void *user)
6415{
6416 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6417 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6418 return 1;
6419}
6420
6421/**
6422 * xmlValidateCheckRefCallback:
6423 * @ref_list: List of references
6424 * @ctxt: Validation context
6425 * @name: Name of ID we are searching for
6426 *
6427 */
6428static void
6429xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6430 const xmlChar *name) {
6431 xmlValidateMemo memo;
6432
6433 if (ref_list == NULL)
6434 return;
6435 memo.ctxt = ctxt;
6436 memo.name = name;
6437
6438 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6439
6440}
6441
6442/**
Owen Taylor3473f882001-02-23 17:55:21 +00006443 * xmlValidateDocumentFinal:
6444 * @ctxt: the validation context
6445 * @doc: a document instance
6446 *
6447 * Does the final step for the document validation once all the
6448 * incremental validation steps have been completed
6449 *
6450 * basically it does the following checks described by the XML Rec
6451 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006452 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00006453 *
6454 * returns 1 if valid or 0 otherwise
6455 */
6456
6457int
6458xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6459 xmlRefTablePtr table;
6460
Daniel Veillardc0be74b2004-11-03 19:16:55 +00006461 if (ctxt == NULL)
6462 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006463 if (doc == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006464 xmlErrValid(ctxt, XML_DTD_NO_DOC,
6465 "xmlValidateDocumentFinal: doc == NULL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006466 return(0);
6467 }
6468
6469 /*
6470 * Check all the NOTATION/NOTATIONS attributes
6471 */
6472 /*
6473 * Check all the ENTITY/ENTITIES attributes definition for validity
6474 */
6475 /*
6476 * Check all the IDREF/IDREFS attributes definition for validity
6477 */
6478 table = (xmlRefTablePtr) doc->refs;
6479 ctxt->doc = doc;
6480 ctxt->valid = 1;
6481 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6482 return(ctxt->valid);
6483}
6484
6485/**
6486 * xmlValidateDtd:
6487 * @ctxt: the validation context
6488 * @doc: a document instance
6489 * @dtd: a dtd instance
6490 *
6491 * Try to validate the document against the dtd instance
6492 *
William M. Brack367df6e2004-10-23 18:14:36 +00006493 * Basically it does check all the definitions in the DtD.
6494 * Note the the internal subset (if present) is de-coupled
6495 * (i.e. not used), which could give problems if ID or IDREF
6496 * is present.
Owen Taylor3473f882001-02-23 17:55:21 +00006497 *
6498 * returns 1 if valid or 0 otherwise
6499 */
6500
6501int
6502xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6503 int ret;
William M. Brack367df6e2004-10-23 18:14:36 +00006504 xmlDtdPtr oldExt, oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006505 xmlNodePtr root;
6506
6507 if (dtd == NULL) return(0);
6508 if (doc == NULL) return(0);
6509 oldExt = doc->extSubset;
William M. Brack367df6e2004-10-23 18:14:36 +00006510 oldInt = doc->intSubset;
Owen Taylor3473f882001-02-23 17:55:21 +00006511 doc->extSubset = dtd;
William M. Brack367df6e2004-10-23 18:14:36 +00006512 doc->intSubset = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006513 ret = xmlValidateRoot(ctxt, doc);
6514 if (ret == 0) {
6515 doc->extSubset = oldExt;
William M. Brack367df6e2004-10-23 18:14:36 +00006516 doc->intSubset = oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006517 return(ret);
6518 }
6519 if (doc->ids != NULL) {
6520 xmlFreeIDTable(doc->ids);
6521 doc->ids = NULL;
6522 }
6523 if (doc->refs != NULL) {
6524 xmlFreeRefTable(doc->refs);
6525 doc->refs = NULL;
6526 }
6527 root = xmlDocGetRootElement(doc);
6528 ret = xmlValidateElement(ctxt, doc, root);
6529 ret &= xmlValidateDocumentFinal(ctxt, doc);
6530 doc->extSubset = oldExt;
William M. Brack367df6e2004-10-23 18:14:36 +00006531 doc->intSubset = oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006532 return(ret);
6533}
6534
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006535static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006536xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6537 const xmlChar *name ATTRIBUTE_UNUSED) {
6538 if (cur == NULL)
6539 return;
6540 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6541 xmlChar *notation = cur->content;
6542
Daniel Veillard878eab02002-02-19 13:46:09 +00006543 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006544 int ret;
6545
6546 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6547 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006548 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006549 }
6550 }
6551 }
6552}
6553
6554static void
Owen Taylor3473f882001-02-23 17:55:21 +00006555xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006556 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006557 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006558 xmlDocPtr doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006559 xmlElementPtr elem = NULL;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006560
Owen Taylor3473f882001-02-23 17:55:21 +00006561 if (cur == NULL)
6562 return;
6563 switch (cur->atype) {
6564 case XML_ATTRIBUTE_CDATA:
6565 case XML_ATTRIBUTE_ID:
6566 case XML_ATTRIBUTE_IDREF :
6567 case XML_ATTRIBUTE_IDREFS:
6568 case XML_ATTRIBUTE_NMTOKEN:
6569 case XML_ATTRIBUTE_NMTOKENS:
6570 case XML_ATTRIBUTE_ENUMERATION:
6571 break;
6572 case XML_ATTRIBUTE_ENTITY:
6573 case XML_ATTRIBUTE_ENTITIES:
6574 case XML_ATTRIBUTE_NOTATION:
6575 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006576
6577 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6578 cur->atype, cur->defaultValue);
6579 if ((ret == 0) && (ctxt->valid == 1))
6580 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006581 }
6582 if (cur->tree != NULL) {
6583 xmlEnumerationPtr tree = cur->tree;
6584 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006585 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006586 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006587 if ((ret == 0) && (ctxt->valid == 1))
6588 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006589 tree = tree->next;
6590 }
6591 }
6592 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006593 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6594 doc = cur->doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006595 if (cur->elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006596 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard878eab02002-02-19 13:46:09 +00006597 "xmlValidateAttributeCallback(%s): internal error\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006598 (const char *) cur->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00006599 return;
6600 }
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006601
6602 if (doc != NULL)
6603 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6604 if ((elem == NULL) && (doc != NULL))
Daniel Veillard878eab02002-02-19 13:46:09 +00006605 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006606 if ((elem == NULL) && (cur->parent != NULL) &&
6607 (cur->parent->type == XML_DTD_NODE))
6608 elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00006609 if (elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006610 xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
Daniel Veillard878eab02002-02-19 13:46:09 +00006611 "attribute %s: could not find decl for element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006612 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006613 return;
6614 }
6615 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006616 xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006617 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006618 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006619 ctxt->valid = 0;
6620 }
6621 }
Owen Taylor3473f882001-02-23 17:55:21 +00006622}
6623
6624/**
6625 * xmlValidateDtdFinal:
6626 * @ctxt: the validation context
6627 * @doc: a document instance
6628 *
6629 * Does the final step for the dtds validation once all the
6630 * subsets have been parsed
6631 *
6632 * basically it does the following checks described by the XML Rec
6633 * - check that ENTITY and ENTITIES type attributes default or
6634 * possible values matches one of the defined entities.
6635 * - check that NOTATION type attributes default or
6636 * possible values matches one of the defined notations.
6637 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006638 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006639 */
6640
6641int
6642xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006643 xmlDtdPtr dtd;
6644 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006645 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006646
6647 if (doc == NULL) return(0);
6648 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6649 return(0);
6650 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006651 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006652 dtd = doc->intSubset;
6653 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6654 table = (xmlAttributeTablePtr) dtd->attributes;
6655 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006656 }
6657 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006658 entities = (xmlEntitiesTablePtr) dtd->entities;
6659 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6660 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006661 }
6662 dtd = doc->extSubset;
6663 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6664 table = (xmlAttributeTablePtr) dtd->attributes;
6665 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006666 }
6667 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006668 entities = (xmlEntitiesTablePtr) dtd->entities;
6669 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6670 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006671 }
6672 return(ctxt->valid);
6673}
6674
6675/**
6676 * xmlValidateDocument:
6677 * @ctxt: the validation context
6678 * @doc: a document instance
6679 *
6680 * Try to validate the document instance
6681 *
6682 * basically it does the all the checks described by the XML Rec
6683 * i.e. validates the internal and external subset (if present)
6684 * and validate the document tree.
6685 *
6686 * returns 1 if valid or 0 otherwise
6687 */
6688
6689int
6690xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6691 int ret;
6692 xmlNodePtr root;
6693
Daniel Veillardc0be74b2004-11-03 19:16:55 +00006694 if (doc == NULL)
6695 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006696 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006697 xmlErrValid(ctxt, XML_DTD_NO_DTD,
6698 "no DTD found!\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006699 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006700 }
Owen Taylor3473f882001-02-23 17:55:21 +00006701 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6702 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
William M. Brack8c22f9f2004-08-06 16:23:27 +00006703 xmlChar *sysID;
6704 if (doc->intSubset->SystemID != NULL) {
William M. Brackbebe7302004-08-05 06:46:47 +00006705 sysID = xmlBuildURI(doc->intSubset->SystemID,
6706 doc->URL);
William M. Brack8c22f9f2004-08-06 16:23:27 +00006707 if (sysID == NULL) {
6708 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6709 "Could not build URI for external subset \"%s\"\n",
6710 (const char *) doc->intSubset->SystemID);
6711 return 0;
6712 }
6713 } else
William M. Brackbebe7302004-08-05 06:46:47 +00006714 sysID = NULL;
William M. Brack8c22f9f2004-08-06 16:23:27 +00006715 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
William M. Brackbebe7302004-08-05 06:46:47 +00006716 (const xmlChar *)sysID);
William M. Brackbebe7302004-08-05 06:46:47 +00006717 if (sysID != NULL)
6718 xmlFree(sysID);
Owen Taylor3473f882001-02-23 17:55:21 +00006719 if (doc->extSubset == NULL) {
6720 if (doc->intSubset->SystemID != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006721 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006722 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006723 (const char *) doc->intSubset->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00006724 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006725 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006726 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006727 (const char *) doc->intSubset->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00006728 }
6729 return(0);
6730 }
6731 }
6732
6733 if (doc->ids != NULL) {
6734 xmlFreeIDTable(doc->ids);
6735 doc->ids = NULL;
6736 }
6737 if (doc->refs != NULL) {
6738 xmlFreeRefTable(doc->refs);
6739 doc->refs = NULL;
6740 }
6741 ret = xmlValidateDtdFinal(ctxt, doc);
6742 if (!xmlValidateRoot(ctxt, doc)) return(0);
6743
6744 root = xmlDocGetRootElement(doc);
6745 ret &= xmlValidateElement(ctxt, doc, root);
6746 ret &= xmlValidateDocumentFinal(ctxt, doc);
6747 return(ret);
6748}
6749
Owen Taylor3473f882001-02-23 17:55:21 +00006750/************************************************************************
6751 * *
6752 * Routines for dynamic validation editing *
6753 * *
6754 ************************************************************************/
6755
6756/**
6757 * xmlValidGetPotentialChildren:
6758 * @ctree: an element content tree
6759 * @list: an array to store the list of child names
6760 * @len: a pointer to the number of element in the list
6761 * @max: the size of the array
6762 *
6763 * Build/extend a list of potential children allowed by the content tree
6764 *
6765 * returns the number of element in the list, or -1 in case of error.
6766 */
6767
6768int
6769xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6770 int *len, int max) {
6771 int i;
6772
6773 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6774 return(-1);
6775 if (*len >= max) return(*len);
6776
6777 switch (ctree->type) {
6778 case XML_ELEMENT_CONTENT_PCDATA:
6779 for (i = 0; i < *len;i++)
6780 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6781 list[(*len)++] = BAD_CAST "#PCDATA";
6782 break;
6783 case XML_ELEMENT_CONTENT_ELEMENT:
6784 for (i = 0; i < *len;i++)
6785 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6786 list[(*len)++] = ctree->name;
6787 break;
6788 case XML_ELEMENT_CONTENT_SEQ:
6789 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6790 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6791 break;
6792 case XML_ELEMENT_CONTENT_OR:
6793 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6794 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6795 break;
6796 }
6797
6798 return(*len);
6799}
6800
William M. Brack9333cc22004-06-24 08:33:40 +00006801/*
6802 * Dummy function to suppress messages while we try out valid elements
6803 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00006804static void XMLCDECL xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
William M. Brack9333cc22004-06-24 08:33:40 +00006805 const char *msg ATTRIBUTE_UNUSED, ...) {
6806 return;
6807}
6808
Owen Taylor3473f882001-02-23 17:55:21 +00006809/**
6810 * xmlValidGetValidElements:
6811 * @prev: an element to insert after
6812 * @next: an element to insert next
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006813 * @names: an array to store the list of child names
Owen Taylor3473f882001-02-23 17:55:21 +00006814 * @max: the size of the array
6815 *
6816 * This function returns the list of authorized children to insert
6817 * within an existing tree while respecting the validity constraints
6818 * forced by the Dtd. The insertion point is defined using @prev and
6819 * @next in the following ways:
6820 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6821 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6822 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6823 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6824 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6825 *
6826 * pointers to the element names are inserted at the beginning of the array
6827 * and do not need to be freed.
6828 *
6829 * returns the number of element in the list, or -1 in case of error. If
6830 * the function returns the value @max the caller is invited to grow the
6831 * receiving array and retry.
6832 */
6833
6834int
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006835xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
Owen Taylor3473f882001-02-23 17:55:21 +00006836 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006837 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006838 int nb_valid_elements = 0;
6839 const xmlChar *elements[256];
6840 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006841 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006842
6843 xmlNode *ref_node;
6844 xmlNode *parent;
6845 xmlNode *test_node;
6846
6847 xmlNode *prev_next;
6848 xmlNode *next_prev;
6849 xmlNode *parent_childs;
6850 xmlNode *parent_last;
6851
6852 xmlElement *element_desc;
6853
6854 if (prev == NULL && next == NULL)
6855 return(-1);
6856
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006857 if (names == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006858 if (max <= 0) return(-1);
6859
William M. Brack9333cc22004-06-24 08:33:40 +00006860 memset(&vctxt, 0, sizeof (xmlValidCtxt));
6861 vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
6862
Owen Taylor3473f882001-02-23 17:55:21 +00006863 nb_valid_elements = 0;
6864 ref_node = prev ? prev : next;
6865 parent = ref_node->parent;
6866
6867 /*
6868 * Retrieves the parent element declaration
6869 */
6870 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6871 parent->name);
6872 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6873 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6874 parent->name);
6875 if (element_desc == NULL) return(-1);
6876
6877 /*
6878 * Do a backup of the current tree structure
6879 */
6880 prev_next = prev ? prev->next : NULL;
6881 next_prev = next ? next->prev : NULL;
6882 parent_childs = parent->children;
6883 parent_last = parent->last;
6884
6885 /*
6886 * Creates a dummy node and insert it into the tree
6887 */
Daniel Veillard95ddcd32004-10-26 21:53:55 +00006888 test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006889 test_node->parent = parent;
6890 test_node->prev = prev;
6891 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006892 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006893
6894 if (prev) prev->next = test_node;
6895 else parent->children = test_node;
6896
6897 if (next) next->prev = test_node;
6898 else parent->last = test_node;
6899
6900 /*
6901 * Insert each potential child node and check if the parent is
6902 * still valid
6903 */
6904 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6905 elements, &nb_elements, 256);
6906
6907 for (i = 0;i < nb_elements;i++) {
6908 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006909 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006910 int j;
6911
6912 for (j = 0; j < nb_valid_elements;j++)
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006913 if (xmlStrEqual(elements[i], names[j])) break;
6914 names[nb_valid_elements++] = elements[i];
Owen Taylor3473f882001-02-23 17:55:21 +00006915 if (nb_valid_elements >= max) break;
6916 }
6917 }
6918
6919 /*
6920 * Restore the tree structure
6921 */
6922 if (prev) prev->next = prev_next;
6923 if (next) next->prev = next_prev;
6924 parent->children = parent_childs;
6925 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006926
6927 /*
6928 * Free up the dummy node
6929 */
6930 test_node->name = name;
6931 xmlFreeNode(test_node);
6932
Owen Taylor3473f882001-02-23 17:55:21 +00006933 return(nb_valid_elements);
6934}
Daniel Veillard4432df22003-09-28 18:58:27 +00006935#endif /* LIBXML_VALID_ENABLED */
6936
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006937#define bottom_valid
6938#include "elfgcchack.h"