blob: 82ca7fc0669bb49101baadf0022985b36e5a584e [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * valid.c : part of the code use to do the DTD handling and the validity
3 * checking
4 *
5 * See Copyright for the status of this software.
6 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <string.h>
14
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18
19#include <libxml/xmlmemory.h>
20#include <libxml/hash.h>
Daniel Veillard29b17482004-08-16 00:39:03 +000021#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000022#include <libxml/valid.h>
23#include <libxml/parser.h>
24#include <libxml/parserInternals.h>
25#include <libxml/xmlerror.h>
26#include <libxml/list.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000027#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000028
Daniel Veillard4432df22003-09-28 18:58:27 +000029static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
30 int create);
Daniel Veillarde62d36c2001-05-15 08:53:16 +000031/* #define DEBUG_VALID_ALGO */
Daniel Veillard5acfd6b2002-09-18 16:29:02 +000032/* #define DEBUG_REGEXP_ALGO */
Daniel Veillarde62d36c2001-05-15 08:53:16 +000033
Daniel Veillarda646cfd2002-09-17 21:50:03 +000034#define TODO \
35 xmlGenericError(xmlGenericErrorContext, \
36 "Unimplemented block at %s:%d\n", \
37 __FILE__, __LINE__);
Owen Taylor3473f882001-02-23 17:55:21 +000038
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000039/************************************************************************
40 * *
41 * Error handling routines *
42 * *
43 ************************************************************************/
44
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000045/**
Daniel Veillardce9457f2003-10-05 21:33:18 +000046 * xmlVErrMemory:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000047 * @ctxt: an XML validation parser context
48 * @extra: extra informations
49 *
50 * Handle an out of memory error
51 */
52static void
Daniel Veillardce9457f2003-10-05 21:33:18 +000053xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000054{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000055 xmlGenericErrorFunc channel = NULL;
56 xmlParserCtxtPtr pctxt = NULL;
57 void *data = NULL;
58
59 if (ctxt != NULL) {
60 channel = ctxt->error;
61 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +000062 /* Use the special values to detect if it is part of a parsing
63 context */
64 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
65 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
66 pctxt = ctxt->userData;
67 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +000068 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000069 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +000070 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000071 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000072 XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
73 "Memory allocation failed : %s\n", extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000074 else
Daniel Veillard73000572003-10-11 11:26:42 +000075 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000076 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000077 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
78 "Memory allocation failed\n");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000079}
80
81/**
82 * xmlErrValid:
83 * @ctxt: an XML validation parser context
Daniel Veillardbb5abab2003-10-03 22:21:51 +000084 * @error: the error number
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000085 * @extra: extra informations
86 *
87 * Handle a validation error
88 */
89static void
Daniel Veillardf88d8cf2003-12-08 10:25:02 +000090xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000091 const char *msg, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000092{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000093 xmlGenericErrorFunc channel = NULL;
94 xmlParserCtxtPtr pctxt = NULL;
95 void *data = NULL;
96
97 if (ctxt != NULL) {
98 channel = ctxt->error;
99 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000100 /* Use the special values to detect if it is part of a parsing
101 context */
102 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
103 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
104 pctxt = ctxt->userData;
105 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000106 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000107 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +0000108 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +0000109 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000110 XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
111 msg, extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000112 else
Daniel Veillard73000572003-10-11 11:26:42 +0000113 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +0000114 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000115 XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
116 msg);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000117}
118
Daniel Veillardf54cd532004-02-25 11:52:31 +0000119#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
120/**
121 * xmlErrValidNode:
122 * @ctxt: an XML validation parser context
123 * @node: the node raising the error
124 * @error: the error number
125 * @str1: extra informations
126 * @str2: extra informations
127 * @str3: extra informations
128 *
129 * Handle a validation error, provide contextual informations
130 */
131static void
132xmlErrValidNode(xmlValidCtxtPtr ctxt,
133 xmlNodePtr node, xmlParserErrors error,
134 const char *msg, const xmlChar * str1,
135 const xmlChar * str2, const xmlChar * str3)
136{
137 xmlStructuredErrorFunc schannel = NULL;
138 xmlGenericErrorFunc channel = NULL;
139 xmlParserCtxtPtr pctxt = NULL;
140 void *data = NULL;
141
142 if (ctxt != NULL) {
143 channel = ctxt->error;
144 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000145 /* Use the special values to detect if it is part of a parsing
146 context */
147 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
148 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
149 pctxt = ctxt->userData;
150 }
Daniel Veillardf54cd532004-02-25 11:52:31 +0000151 }
152 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
153 XML_ERR_ERROR, NULL, 0,
154 (const char *) str1,
155 (const char *) str1,
156 (const char *) str3, 0, 0, msg, str1, str2, str3);
157}
158#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
159
Daniel Veillardd61e8fb2003-10-19 21:59:17 +0000160#ifdef LIBXML_VALID_ENABLED
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000161/**
162 * xmlErrValidNodeNr:
163 * @ctxt: an XML validation parser context
164 * @node: the node raising the error
165 * @error: the error number
166 * @str1: extra informations
167 * @int2: extra informations
168 * @str3: extra informations
169 *
170 * Handle a validation error, provide contextual informations
171 */
172static void
Daniel Veillard72b9e292003-10-28 15:44:17 +0000173xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000174 xmlNodePtr node, xmlParserErrors error,
175 const char *msg, const xmlChar * str1,
176 int int2, const xmlChar * str3)
177{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000178 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000179 xmlGenericErrorFunc channel = NULL;
180 xmlParserCtxtPtr pctxt = NULL;
181 void *data = NULL;
182
183 if (ctxt != NULL) {
184 channel = ctxt->error;
185 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000186 /* Use the special values to detect if it is part of a parsing
187 context */
188 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
189 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
190 pctxt = ctxt->userData;
191 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000192 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000193 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000194 XML_ERR_ERROR, NULL, 0,
195 (const char *) str1,
196 (const char *) str3,
197 NULL, int2, 0, msg, str1, int2, str3);
198}
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000199
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000200/**
201 * xmlErrValidWarning:
202 * @ctxt: an XML validation parser context
203 * @node: the node raising the error
204 * @error: the error number
William M. Brackedb65a72004-02-06 07:36:04 +0000205 * @str1: extra information
206 * @str2: extra information
207 * @str3: extra information
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000208 *
William M. Brackedb65a72004-02-06 07:36:04 +0000209 * Handle a validation error, provide contextual information
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000210 */
211static void
William M. Brackedb65a72004-02-06 07:36:04 +0000212xmlErrValidWarning(xmlValidCtxtPtr ctxt,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000213 xmlNodePtr node, xmlParserErrors error,
214 const char *msg, const xmlChar * str1,
215 const xmlChar * str2, const xmlChar * str3)
216{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000217 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000218 xmlGenericErrorFunc channel = NULL;
219 xmlParserCtxtPtr pctxt = NULL;
220 void *data = NULL;
221
222 if (ctxt != NULL) {
William M. Bracke4d526f2004-12-18 00:01:21 +0000223 channel = ctxt->warning;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000224 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000225 /* Use the special values to detect if it is part of a parsing
226 context */
227 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
228 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
229 pctxt = ctxt->userData;
230 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000231 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000232 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000233 XML_ERR_WARNING, NULL, 0,
234 (const char *) str1,
235 (const char *) str1,
236 (const char *) str3, 0, 0, msg, str1, str2, str3);
237}
238
239
Daniel Veillardea7751d2002-12-20 00:16:24 +0000240
241#ifdef LIBXML_REGEXP_ENABLED
242/*
243 * If regexp are enabled we can do continuous validation without the
244 * need of a tree to validate the content model. this is done in each
245 * callbacks.
246 * Each xmlValidState represent the validation state associated to the
247 * set of nodes currently open from the document root to the current element.
248 */
249
250
251typedef struct _xmlValidState {
252 xmlElementPtr elemDecl; /* pointer to the content model */
253 xmlNodePtr node; /* pointer to the current node */
254 xmlRegExecCtxtPtr exec; /* regexp runtime */
255} _xmlValidState;
256
257
258static int
259vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000260 if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000261 ctxt->vstateMax = 10;
262 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
263 sizeof(ctxt->vstateTab[0]));
264 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000265 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000266 return(-1);
267 }
268 }
269
270 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000271 xmlValidState *tmp;
272
273 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
274 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
275 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000276 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000277 return(-1);
278 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000279 ctxt->vstateMax *= 2;
280 ctxt->vstateTab = tmp;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000281 }
282 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
283 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
284 ctxt->vstateTab[ctxt->vstateNr].node = node;
285 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
286 if (elemDecl->contModel == NULL)
287 xmlValidBuildContentModel(ctxt, elemDecl);
288 if (elemDecl->contModel != NULL) {
289 ctxt->vstateTab[ctxt->vstateNr].exec =
290 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
291 } else {
292 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000293 xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
294 XML_ERR_INTERNAL_ERROR,
295 "Failed to build content model regexp for %s\n",
296 node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000297 }
298 }
299 return(ctxt->vstateNr++);
300}
301
302static int
303vstateVPop(xmlValidCtxtPtr ctxt) {
304 xmlElementPtr elemDecl;
305
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000306 if (ctxt->vstateNr < 1) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000307 ctxt->vstateNr--;
308 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
309 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
310 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
311 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
312 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
313 }
314 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
315 if (ctxt->vstateNr >= 1)
316 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
317 else
318 ctxt->vstate = NULL;
319 return(ctxt->vstateNr);
320}
321
322#else /* not LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000323/*
Daniel Veillard1c732d22002-11-30 11:22:59 +0000324 * If regexp are not enabled, it uses a home made algorithm less
325 * complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000326 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000327 * only restriction is on the deepness of the tree limited by the
328 * size of the occurs bitfield
329 *
330 * this is the content of a saved state for rollbacks
331 */
332
333#define ROLLBACK_OR 0
334#define ROLLBACK_PARENT 1
335
Daniel Veillardb44025c2001-10-11 22:55:55 +0000336typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000337 xmlElementContentPtr cont; /* pointer to the content model subtree */
338 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000339 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000340 unsigned char depth; /* current depth in the overall tree */
341 unsigned char state; /* ROLLBACK_XXX */
342} _xmlValidState;
343
Daniel Veillardfc57b412002-04-29 15:50:14 +0000344#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000345#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
346#define CONT ctxt->vstate->cont
347#define NODE ctxt->vstate->node
348#define DEPTH ctxt->vstate->depth
349#define OCCURS ctxt->vstate->occurs
350#define STATE ctxt->vstate->state
351
Daniel Veillard5344c602001-12-31 16:37:34 +0000352#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
353#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000354
Daniel Veillard5344c602001-12-31 16:37:34 +0000355#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
356#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000357
358static int
359vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
360 xmlNodePtr node, unsigned char depth, long occurs,
361 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000362 int i = ctxt->vstateNr - 1;
363
Daniel Veillard940492d2002-04-15 10:15:25 +0000364 if (ctxt->vstateNr > MAX_RECURSE) {
365 return(-1);
366 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000367 if (ctxt->vstateTab == NULL) {
368 ctxt->vstateMax = 8;
369 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
370 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
371 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000372 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000373 return(-1);
374 }
375 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000376 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000377 xmlValidState *tmp;
378
379 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
380 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
381 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000382 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard940492d2002-04-15 10:15:25 +0000383 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000384 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000385 ctxt->vstateMax *= 2;
386 ctxt->vstateTab = tmp;
Daniel Veillard06803992001-04-22 10:35:56 +0000387 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000388 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000389 /*
390 * Don't push on the stack a state already here
391 */
392 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
393 (ctxt->vstateTab[i].node == node) &&
394 (ctxt->vstateTab[i].depth == depth) &&
395 (ctxt->vstateTab[i].occurs == occurs) &&
396 (ctxt->vstateTab[i].state == state))
397 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000398 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
399 ctxt->vstateTab[ctxt->vstateNr].node = node;
400 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
401 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
402 ctxt->vstateTab[ctxt->vstateNr].state = state;
403 return(ctxt->vstateNr++);
404}
405
406static int
407vstateVPop(xmlValidCtxtPtr ctxt) {
408 if (ctxt->vstateNr <= 1) return(-1);
409 ctxt->vstateNr--;
410 ctxt->vstate = &ctxt->vstateTab[0];
411 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
412 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
413 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
414 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
415 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
416 return(ctxt->vstateNr);
417}
418
Daniel Veillard118aed72002-09-24 14:13:13 +0000419#endif /* LIBXML_REGEXP_ENABLED */
420
Daniel Veillard1c732d22002-11-30 11:22:59 +0000421static int
422nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
423{
424 if (ctxt->nodeMax <= 0) {
425 ctxt->nodeMax = 4;
426 ctxt->nodeTab =
427 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
428 sizeof(ctxt->nodeTab[0]));
429 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000430 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000431 ctxt->nodeMax = 0;
432 return (0);
433 }
434 }
435 if (ctxt->nodeNr >= ctxt->nodeMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000436 xmlNodePtr *tmp;
437 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
438 ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
439 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000440 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000441 return (0);
442 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000443 ctxt->nodeMax *= 2;
444 ctxt->nodeTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000445 }
446 ctxt->nodeTab[ctxt->nodeNr] = value;
447 ctxt->node = value;
448 return (ctxt->nodeNr++);
449}
450static xmlNodePtr
451nodeVPop(xmlValidCtxtPtr ctxt)
452{
453 xmlNodePtr ret;
454
455 if (ctxt->nodeNr <= 0)
456 return (0);
457 ctxt->nodeNr--;
458 if (ctxt->nodeNr > 0)
459 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
460 else
461 ctxt->node = NULL;
462 ret = ctxt->nodeTab[ctxt->nodeNr];
463 ctxt->nodeTab[ctxt->nodeNr] = 0;
464 return (ret);
465}
Owen Taylor3473f882001-02-23 17:55:21 +0000466
Owen Taylor3473f882001-02-23 17:55:21 +0000467#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000468static void
469xmlValidPrintNode(xmlNodePtr cur) {
470 if (cur == NULL) {
471 xmlGenericError(xmlGenericErrorContext, "null");
472 return;
473 }
474 switch (cur->type) {
475 case XML_ELEMENT_NODE:
476 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
477 break;
478 case XML_TEXT_NODE:
479 xmlGenericError(xmlGenericErrorContext, "text ");
480 break;
481 case XML_CDATA_SECTION_NODE:
482 xmlGenericError(xmlGenericErrorContext, "cdata ");
483 break;
484 case XML_ENTITY_REF_NODE:
485 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
486 break;
487 case XML_PI_NODE:
488 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
489 break;
490 case XML_COMMENT_NODE:
491 xmlGenericError(xmlGenericErrorContext, "comment ");
492 break;
493 case XML_ATTRIBUTE_NODE:
494 xmlGenericError(xmlGenericErrorContext, "?attr? ");
495 break;
496 case XML_ENTITY_NODE:
497 xmlGenericError(xmlGenericErrorContext, "?ent? ");
498 break;
499 case XML_DOCUMENT_NODE:
500 xmlGenericError(xmlGenericErrorContext, "?doc? ");
501 break;
502 case XML_DOCUMENT_TYPE_NODE:
503 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
504 break;
505 case XML_DOCUMENT_FRAG_NODE:
506 xmlGenericError(xmlGenericErrorContext, "?frag? ");
507 break;
508 case XML_NOTATION_NODE:
509 xmlGenericError(xmlGenericErrorContext, "?nota? ");
510 break;
511 case XML_HTML_DOCUMENT_NODE:
512 xmlGenericError(xmlGenericErrorContext, "?html? ");
513 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000514#ifdef LIBXML_DOCB_ENABLED
515 case XML_DOCB_DOCUMENT_NODE:
516 xmlGenericError(xmlGenericErrorContext, "?docb? ");
517 break;
518#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000519 case XML_DTD_NODE:
520 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
521 break;
522 case XML_ELEMENT_DECL:
523 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
524 break;
525 case XML_ATTRIBUTE_DECL:
526 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
527 break;
528 case XML_ENTITY_DECL:
529 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
530 break;
531 case XML_NAMESPACE_DECL:
532 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
533 break;
534 case XML_XINCLUDE_START:
535 xmlGenericError(xmlGenericErrorContext, "incstart ");
536 break;
537 case XML_XINCLUDE_END:
538 xmlGenericError(xmlGenericErrorContext, "incend ");
539 break;
540 }
541}
542
543static void
544xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000545 if (cur == NULL)
546 xmlGenericError(xmlGenericErrorContext, "null ");
547 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000548 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000549 cur = cur->next;
550 }
551}
552
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000553static void
554xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Daniel Veillard83391282003-03-06 21:37:30 +0000555 char expr[5000];
Owen Taylor3473f882001-02-23 17:55:21 +0000556
557 expr[0] = 0;
558 xmlGenericError(xmlGenericErrorContext, "valid: ");
559 xmlValidPrintNodeList(cur);
560 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000561 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000562 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
563}
564
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000565static void
566xmlValidDebugState(xmlValidStatePtr state) {
567 xmlGenericError(xmlGenericErrorContext, "(");
568 if (state->cont == NULL)
569 xmlGenericError(xmlGenericErrorContext, "null,");
570 else
571 switch (state->cont->type) {
572 case XML_ELEMENT_CONTENT_PCDATA:
573 xmlGenericError(xmlGenericErrorContext, "pcdata,");
574 break;
575 case XML_ELEMENT_CONTENT_ELEMENT:
576 xmlGenericError(xmlGenericErrorContext, "%s,",
577 state->cont->name);
578 break;
579 case XML_ELEMENT_CONTENT_SEQ:
580 xmlGenericError(xmlGenericErrorContext, "seq,");
581 break;
582 case XML_ELEMENT_CONTENT_OR:
583 xmlGenericError(xmlGenericErrorContext, "or,");
584 break;
585 }
586 xmlValidPrintNode(state->node);
587 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
588 state->depth, state->occurs, state->state);
589}
590
591static void
592xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
593 int i, j;
594
595 xmlGenericError(xmlGenericErrorContext, "state: ");
596 xmlValidDebugState(ctxt->vstate);
597 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
598 ctxt->vstateNr - 1);
599 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
600 xmlValidDebugState(&ctxt->vstateTab[j]);
601 xmlGenericError(xmlGenericErrorContext, "\n");
602}
603
604/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000605#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000606 *****/
607
608#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000609#define DEBUG_VALID_MSG(m) \
610 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
611
Owen Taylor3473f882001-02-23 17:55:21 +0000612#else
613#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000614#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000615#endif
616
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000617/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000618
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000619
Owen Taylor3473f882001-02-23 17:55:21 +0000620#define CHECK_DTD \
621 if (doc == NULL) return(0); \
622 else if ((doc->intSubset == NULL) && \
623 (doc->extSubset == NULL)) return(0)
624
Owen Taylor3473f882001-02-23 17:55:21 +0000625xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
626
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000627#ifdef LIBXML_REGEXP_ENABLED
628
629/************************************************************************
630 * *
631 * Content model validation based on the regexps *
632 * *
633 ************************************************************************/
634
635/**
636 * xmlValidBuildAContentModel:
637 * @content: the content model
638 * @ctxt: the schema parser context
639 * @name: the element name whose content is being built
640 *
641 * Generate the automata sequence needed for that type
642 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000643 * Returns 1 if successful or 0 in case of error.
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000644 */
645static int
646xmlValidBuildAContentModel(xmlElementContentPtr content,
647 xmlValidCtxtPtr ctxt,
648 const xmlChar *name) {
649 if (content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000650 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
651 "Found NULL content in content model of %s\n",
652 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000653 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000654 }
655 switch (content->type) {
656 case XML_ELEMENT_CONTENT_PCDATA:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000657 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
658 "Found PCDATA in content model of %s\n",
659 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000660 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000661 break;
662 case XML_ELEMENT_CONTENT_ELEMENT: {
663 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillardc00cda82003-04-07 10:22:39 +0000664 xmlChar fn[50];
665 xmlChar *fullname;
666
667 fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
668 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000669 xmlVErrMemory(ctxt, "Building content model");
Daniel Veillardc00cda82003-04-07 10:22:39 +0000670 return(0);
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000671 }
672
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000673 switch (content->ocur) {
674 case XML_ELEMENT_CONTENT_ONCE:
675 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000676 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000677 break;
678 case XML_ELEMENT_CONTENT_OPT:
679 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000680 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000681 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
682 break;
683 case XML_ELEMENT_CONTENT_PLUS:
684 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000685 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000686 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000687 ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000688 break;
689 case XML_ELEMENT_CONTENT_MULT:
William M. Brack8b0cbb02004-04-17 13:31:06 +0000690 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
691 ctxt->state, NULL);
692 xmlAutomataNewTransition(ctxt->am,
693 ctxt->state, ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000694 break;
695 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000696 if ((fullname != fn) && (fullname != content->name))
697 xmlFree(fullname);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000698 break;
699 }
700 case XML_ELEMENT_CONTENT_SEQ: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000701 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000702 xmlElementContentOccur ocur;
703
704 /*
705 * Simply iterate over the content
706 */
707 oldstate = ctxt->state;
708 ocur = content->ocur;
Daniel Veillardf4be0182003-02-24 19:54:33 +0000709 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
710 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
711 oldstate = ctxt->state;
712 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000713 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000714 xmlValidBuildAContentModel(content->c1, ctxt, name);
715 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000716 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
717 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
718 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000719 oldend = ctxt->state;
720 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000721 switch (ocur) {
722 case XML_ELEMENT_CONTENT_ONCE:
723 break;
724 case XML_ELEMENT_CONTENT_OPT:
725 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
726 break;
727 case XML_ELEMENT_CONTENT_MULT:
728 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000729 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000730 break;
731 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000732 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000733 break;
734 }
735 break;
736 }
737 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000738 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000739 xmlElementContentOccur ocur;
740
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000741 ocur = content->ocur;
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000742 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
743 (ocur == XML_ELEMENT_CONTENT_MULT)) {
744 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
745 ctxt->state, NULL);
746 }
747 oldstate = ctxt->state;
748 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000749
750 /*
751 * iterate over the subtypes and remerge the end with an
752 * epsilon transition
753 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000754 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000755 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000756 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000757 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000758 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000759 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
760 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000761 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000762 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000763 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
764 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000765 switch (ocur) {
766 case XML_ELEMENT_CONTENT_ONCE:
767 break;
768 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000769 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000770 break;
771 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000772 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
773 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000774 break;
775 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000776 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000777 break;
778 }
779 break;
780 }
781 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000782 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
783 "ContentModel broken for element %s\n",
784 (const char *) name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000785 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000786 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000787 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000788}
789/**
790 * xmlValidBuildContentModel:
791 * @ctxt: a validation context
792 * @elem: an element declaration node
793 *
794 * (Re)Build the automata associated to the content model of this
795 * element
796 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000797 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000798 */
799int
800xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000801
802 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000803 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000804 if (elem->type != XML_ELEMENT_DECL)
805 return(0);
806 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
807 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000808 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000809 if (elem->contModel != NULL) {
810 if (!xmlRegexpIsDeterminist(elem->contModel)) {
811 ctxt->valid = 0;
812 return(0);
813 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000814 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000815 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000816
817 ctxt->am = xmlNewAutomata();
818 if (ctxt->am == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000819 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
820 XML_ERR_INTERNAL_ERROR,
821 "Cannot create automata for element %s\n",
822 elem->name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000823 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000824 }
William M. Brack78637da2003-07-31 14:47:38 +0000825 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000826 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
827 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000828 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000829 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000830 char expr[5000];
831 expr[0] = 0;
832 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000833 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
834 XML_DTD_CONTENT_NOT_DETERMINIST,
835 "Content model of %s is not determinist: %s\n",
836 elem->name, BAD_CAST expr, NULL);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000837#ifdef DEBUG_REGEXP_ALGO
838 xmlRegexpPrint(stderr, elem->contModel);
839#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000840 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000841 ctxt->state = NULL;
842 xmlFreeAutomata(ctxt->am);
843 ctxt->am = NULL;
844 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000845 }
846 ctxt->state = NULL;
847 xmlFreeAutomata(ctxt->am);
848 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000849 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000850}
851
852#endif /* LIBXML_REGEXP_ENABLED */
853
Owen Taylor3473f882001-02-23 17:55:21 +0000854/****************************************************************
855 * *
856 * Util functions for data allocation/deallocation *
857 * *
858 ****************************************************************/
859
860/**
Daniel Veillarda37aab82003-06-09 09:10:36 +0000861 * xmlNewValidCtxt:
862 *
863 * Allocate a validation context structure.
864 *
865 * Returns NULL if not, otherwise the new validation context structure
866 */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000867xmlValidCtxtPtr xmlNewValidCtxt(void) {
Daniel Veillarda37aab82003-06-09 09:10:36 +0000868 xmlValidCtxtPtr ret;
869
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000870 if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000871 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda37aab82003-06-09 09:10:36 +0000872 return (NULL);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000873 }
Daniel Veillarda37aab82003-06-09 09:10:36 +0000874
875 (void) memset(ret, 0, sizeof (xmlValidCtxt));
876
877 return (ret);
878}
879
880/**
881 * xmlFreeValidCtxt:
882 * @cur: the validation context to free
883 *
884 * Free a validation context structure.
885 */
886void
887xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
Daniel Veillardc0be74b2004-11-03 19:16:55 +0000888 if (cur->vstateTab != NULL)
889 xmlFree(cur->vstateTab);
890 if (cur->nodeTab != NULL)
891 xmlFree(cur->nodeTab);
Daniel Veillarda37aab82003-06-09 09:10:36 +0000892 xmlFree(cur);
893}
894
Daniel Veillard4432df22003-09-28 18:58:27 +0000895#endif /* LIBXML_VALID_ENABLED */
896
Daniel Veillarda37aab82003-06-09 09:10:36 +0000897/**
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000898 * xmlNewDocElementContent:
899 * @doc: the document
Owen Taylor3473f882001-02-23 17:55:21 +0000900 * @name: the subelement name or NULL
901 * @type: the type of element content decl
902 *
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000903 * Allocate an element content structure for the document.
Owen Taylor3473f882001-02-23 17:55:21 +0000904 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000905 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000906 */
907xmlElementContentPtr
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000908xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name,
909 xmlElementContentType type) {
Owen Taylor3473f882001-02-23 17:55:21 +0000910 xmlElementContentPtr ret;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000911 xmlDictPtr dict = NULL;
912
913 if (doc != NULL)
914 dict = doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +0000915
916 switch(type) {
917 case XML_ELEMENT_CONTENT_ELEMENT:
918 if (name == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000919 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
920 "xmlNewElementContent : name == NULL !\n",
921 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000922 }
923 break;
924 case XML_ELEMENT_CONTENT_PCDATA:
925 case XML_ELEMENT_CONTENT_SEQ:
926 case XML_ELEMENT_CONTENT_OR:
927 if (name != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000928 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
929 "xmlNewElementContent : name != NULL !\n",
930 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000931 }
932 break;
933 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000934 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
935 "Internal: ELEMENT content corrupted invalid type\n",
936 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000937 return(NULL);
938 }
939 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
940 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000941 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000942 return(NULL);
943 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000944 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000945 ret->type = type;
946 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000947 if (name != NULL) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000948 int l;
949 const xmlChar *tmp;
950
951 tmp = xmlSplitQName3(name, &l);
952 if (tmp == NULL) {
953 if (dict == NULL)
954 ret->name = xmlStrdup(name);
955 else
956 ret->name = xmlDictLookup(dict, name, -1);
957 } else {
958 if (dict == NULL) {
959 ret->prefix = xmlStrndup(name, l);
960 ret->name = xmlStrdup(tmp);
961 } else {
962 ret->prefix = xmlDictLookup(dict, name, l);
963 ret->name = xmlDictLookup(dict, tmp, -1);
964 }
965 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000966 }
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000967 return(ret);
968}
969
970/**
971 * xmlNewElementContent:
972 * @name: the subelement name or NULL
973 * @type: the type of element content decl
974 *
975 * Allocate an element content structure.
976 * Deprecated in favor of xmlNewDocElementContent
977 *
978 * Returns NULL if not, otherwise the new element content structure
979 */
980xmlElementContentPtr
981xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
982 return(xmlNewDocElementContent(NULL, name, type));
983}
984
985/**
986 * xmlCopyDocElementContent:
987 * @doc: the document owning the element declaration
988 * @cur: An element content pointer.
989 *
990 * Build a copy of an element content description.
991 *
992 * Returns the new xmlElementContentPtr or NULL in case of error.
993 */
994xmlElementContentPtr
995xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
996 xmlElementContentPtr ret = NULL, prev = NULL, tmp;
997 xmlDictPtr dict = NULL;
998
999 if (cur == NULL) return(NULL);
1000
1001 if (doc != NULL)
1002 dict = doc->dict;
1003
1004 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
1005 if (ret == NULL) {
1006 xmlVErrMemory(NULL, "malloc failed");
1007 return(NULL);
1008 }
1009 memset(ret, 0, sizeof(xmlElementContent));
1010 ret->type = cur->type;
1011 ret->ocur = cur->ocur;
1012 if (cur->name != NULL) {
1013 if (dict)
1014 ret->name = xmlDictLookup(dict, cur->name, -1);
1015 else
1016 ret->name = xmlStrdup(cur->name);
1017 }
1018
1019 if (cur->prefix != NULL) {
1020 if (dict)
1021 ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
1022 else
1023 ret->prefix = xmlStrdup(cur->prefix);
1024 }
1025 if (cur->c1 != NULL)
1026 ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
1027 if (ret->c1 != NULL)
1028 ret->c1->parent = ret;
1029 if (cur->c2 != NULL) {
1030 prev = ret;
1031 cur = cur->c2;
1032 while (cur != NULL) {
1033 tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
1034 if (tmp == NULL) {
1035 xmlVErrMemory(NULL, "malloc failed");
1036 return(ret);
1037 }
1038 memset(tmp, 0, sizeof(xmlElementContent));
1039 tmp->type = cur->type;
1040 tmp->ocur = cur->ocur;
1041 prev->c2 = tmp;
1042 if (cur->name != NULL) {
1043 if (dict)
1044 tmp->name = xmlDictLookup(dict, cur->name, -1);
1045 else
1046 tmp->name = xmlStrdup(cur->name);
1047 }
1048
1049 if (cur->prefix != NULL) {
1050 if (dict)
1051 tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
1052 else
1053 tmp->prefix = xmlStrdup(cur->prefix);
1054 }
1055 if (cur->c1 != NULL)
1056 tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
1057 if (tmp->c1 != NULL)
1058 tmp->c1->parent = ret;
1059 prev = tmp;
1060 cur = cur->c2;
1061 }
1062 }
Owen Taylor3473f882001-02-23 17:55:21 +00001063 return(ret);
1064}
1065
1066/**
1067 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +00001068 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +00001069 *
1070 * Build a copy of an element content description.
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001071 * Deprecated, use xmlCopyDocElementContent instead
Owen Taylor3473f882001-02-23 17:55:21 +00001072 *
1073 * Returns the new xmlElementContentPtr or NULL in case of error.
1074 */
1075xmlElementContentPtr
1076xmlCopyElementContent(xmlElementContentPtr cur) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001077 return(xmlCopyDocElementContent(NULL, cur));
1078}
Owen Taylor3473f882001-02-23 17:55:21 +00001079
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001080/**
1081 * xmlFreeDocElementContent:
1082 * @doc: the document owning the element declaration
1083 * @cur: the element content tree to free
1084 *
1085 * Free an element content structure. The whole subtree is removed.
1086 */
1087void
1088xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
1089 xmlElementContentPtr next;
1090 xmlDictPtr dict = NULL;
1091
1092 if (doc != NULL)
1093 dict = doc->dict;
1094
1095 while (cur != NULL) {
1096 next = cur->c2;
1097 switch (cur->type) {
1098 case XML_ELEMENT_CONTENT_PCDATA:
1099 case XML_ELEMENT_CONTENT_ELEMENT:
1100 case XML_ELEMENT_CONTENT_SEQ:
1101 case XML_ELEMENT_CONTENT_OR:
1102 break;
1103 default:
1104 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1105 "Internal: ELEMENT content corrupted invalid type\n",
1106 NULL);
1107 return;
1108 }
1109 if (cur->c1 != NULL) xmlFreeDocElementContent(doc, cur->c1);
1110 if (dict) {
1111 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
1112 xmlFree((xmlChar *) cur->name);
1113 if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
1114 xmlFree((xmlChar *) cur->prefix);
1115 } else {
1116 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
1117 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
1118 }
1119 xmlFree(cur);
1120 cur = next;
Owen Taylor3473f882001-02-23 17:55:21 +00001121 }
Owen Taylor3473f882001-02-23 17:55:21 +00001122}
1123
1124/**
1125 * xmlFreeElementContent:
1126 * @cur: the element content tree to free
1127 *
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001128 * Free an element content structure. The whole subtree is removed.
1129 * Deprecated, use xmlFreeDocElementContent instead
Owen Taylor3473f882001-02-23 17:55:21 +00001130 */
1131void
1132xmlFreeElementContent(xmlElementContentPtr cur) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001133 xmlFreeDocElementContent(NULL, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001134}
1135
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001136#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001137/**
1138 * xmlDumpElementContent:
1139 * @buf: An XML buffer
1140 * @content: An element table
1141 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1142 *
1143 * This will dump the content of the element table as an XML DTD definition
1144 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001145static void
Owen Taylor3473f882001-02-23 17:55:21 +00001146xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
1147 if (content == NULL) return;
1148
1149 if (glob) xmlBufferWriteChar(buf, "(");
1150 switch (content->type) {
1151 case XML_ELEMENT_CONTENT_PCDATA:
1152 xmlBufferWriteChar(buf, "#PCDATA");
1153 break;
1154 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001155 if (content->prefix != NULL) {
1156 xmlBufferWriteCHAR(buf, content->prefix);
1157 xmlBufferWriteChar(buf, ":");
1158 }
Owen Taylor3473f882001-02-23 17:55:21 +00001159 xmlBufferWriteCHAR(buf, content->name);
1160 break;
1161 case XML_ELEMENT_CONTENT_SEQ:
1162 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1163 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1164 xmlDumpElementContent(buf, content->c1, 1);
1165 else
1166 xmlDumpElementContent(buf, content->c1, 0);
1167 xmlBufferWriteChar(buf, " , ");
William M. Brack4119d1c2004-06-24 02:24:44 +00001168 if ((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1169 ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) &&
1170 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
Owen Taylor3473f882001-02-23 17:55:21 +00001171 xmlDumpElementContent(buf, content->c2, 1);
1172 else
1173 xmlDumpElementContent(buf, content->c2, 0);
1174 break;
1175 case XML_ELEMENT_CONTENT_OR:
1176 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1177 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1178 xmlDumpElementContent(buf, content->c1, 1);
1179 else
1180 xmlDumpElementContent(buf, content->c1, 0);
1181 xmlBufferWriteChar(buf, " | ");
William M. Brack4119d1c2004-06-24 02:24:44 +00001182 if ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1183 ((content->c2->type == XML_ELEMENT_CONTENT_OR) &&
1184 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
Owen Taylor3473f882001-02-23 17:55:21 +00001185 xmlDumpElementContent(buf, content->c2, 1);
1186 else
1187 xmlDumpElementContent(buf, content->c2, 0);
1188 break;
1189 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001190 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1191 "Internal: ELEMENT content corrupted invalid type\n",
1192 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001193 }
1194 if (glob)
1195 xmlBufferWriteChar(buf, ")");
1196 switch (content->ocur) {
1197 case XML_ELEMENT_CONTENT_ONCE:
1198 break;
1199 case XML_ELEMENT_CONTENT_OPT:
1200 xmlBufferWriteChar(buf, "?");
1201 break;
1202 case XML_ELEMENT_CONTENT_MULT:
1203 xmlBufferWriteChar(buf, "*");
1204 break;
1205 case XML_ELEMENT_CONTENT_PLUS:
1206 xmlBufferWriteChar(buf, "+");
1207 break;
1208 }
1209}
1210
1211/**
1212 * xmlSprintfElementContent:
1213 * @buf: an output buffer
1214 * @content: An element table
1215 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1216 *
Daniel Veillardd3d06722001-08-15 12:06:36 +00001217 * Deprecated, unsafe, use xmlSnprintfElementContent
1218 */
1219void
1220xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
1221 xmlElementContentPtr content ATTRIBUTE_UNUSED,
1222 int glob ATTRIBUTE_UNUSED) {
1223}
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
1231 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1232 *
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 Veillardd3d06722001-08-15 12:06:36 +00001237xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
1238 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 }
Owen Taylor3473f882001-02-23 17:55:21 +00001247 if (glob) strcat(buf, "(");
1248 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 }
1309 if (glob)
1310 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
1871 *
1872 * Verify that the element don't have too many ID attributes
1873 * declared.
1874 *
1875 * Returns the number of ID attributes found.
1876 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001877static int
Owen Taylor3473f882001-02-23 17:55:21 +00001878xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1879 xmlAttributePtr cur;
1880 int ret = 0;
1881
1882 if (elem == NULL) return(0);
1883 cur = elem->attributes;
1884 while (cur != NULL) {
1885 if (cur->atype == XML_ATTRIBUTE_ID) {
1886 ret ++;
1887 if (ret > 1)
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001888 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001889 "Element %s has too many ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001890 elem->name, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001891 }
1892 cur = cur->nexth;
1893 }
1894 return(ret);
1895}
Daniel Veillard4432df22003-09-28 18:58:27 +00001896#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001897
1898/**
1899 * xmlFreeAttribute:
1900 * @elem: An attribute
1901 *
1902 * Deallocate the memory used by an attribute definition
1903 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001904static void
Owen Taylor3473f882001-02-23 17:55:21 +00001905xmlFreeAttribute(xmlAttributePtr attr) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001906 xmlDictPtr dict;
1907
Owen Taylor3473f882001-02-23 17:55:21 +00001908 if (attr == NULL) return;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001909 if (attr->doc != NULL)
1910 dict = attr->doc->dict;
1911 else
1912 dict = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001913 xmlUnlinkNode((xmlNodePtr) attr);
1914 if (attr->tree != NULL)
1915 xmlFreeEnumeration(attr->tree);
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001916 if (dict) {
1917 if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
1918 xmlFree((xmlChar *) attr->elem);
1919 if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
1920 xmlFree((xmlChar *) attr->name);
1921 if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
1922 xmlFree((xmlChar *) attr->prefix);
1923 if ((attr->defaultValue != NULL) &&
1924 (!xmlDictOwns(dict, attr->defaultValue)))
1925 xmlFree((xmlChar *) attr->defaultValue);
1926 } else {
1927 if (attr->elem != NULL)
1928 xmlFree((xmlChar *) attr->elem);
1929 if (attr->name != NULL)
1930 xmlFree((xmlChar *) attr->name);
1931 if (attr->defaultValue != NULL)
1932 xmlFree((xmlChar *) attr->defaultValue);
1933 if (attr->prefix != NULL)
1934 xmlFree((xmlChar *) attr->prefix);
1935 }
Owen Taylor3473f882001-02-23 17:55:21 +00001936 xmlFree(attr);
1937}
1938
1939
1940/**
1941 * xmlAddAttributeDecl:
1942 * @ctxt: the validation context
1943 * @dtd: pointer to the DTD
1944 * @elem: the element name
1945 * @name: the attribute name
1946 * @ns: the attribute namespace prefix
1947 * @type: the attribute type
1948 * @def: the attribute default type
1949 * @defaultValue: the attribute default value
1950 * @tree: if it's an enumeration, the associated list
1951 *
1952 * Register a new attribute declaration
1953 * Note that @tree becomes the ownership of the DTD
1954 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001955 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001956 */
1957xmlAttributePtr
William M. Brackedb65a72004-02-06 07:36:04 +00001958xmlAddAttributeDecl(xmlValidCtxtPtr ctxt,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001959 xmlDtdPtr dtd, const xmlChar *elem,
Owen Taylor3473f882001-02-23 17:55:21 +00001960 const xmlChar *name, const xmlChar *ns,
1961 xmlAttributeType type, xmlAttributeDefault def,
1962 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1963 xmlAttributePtr ret;
1964 xmlAttributeTablePtr table;
1965 xmlElementPtr elemDef;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001966 xmlDictPtr dict = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001967
1968 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001969 xmlFreeEnumeration(tree);
1970 return(NULL);
1971 }
1972 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001973 xmlFreeEnumeration(tree);
1974 return(NULL);
1975 }
1976 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001977 xmlFreeEnumeration(tree);
1978 return(NULL);
1979 }
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001980 if (dtd->doc != NULL)
1981 dict = dtd->doc->dict;
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001982
Daniel Veillard4432df22003-09-28 18:58:27 +00001983#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001984 /*
1985 * Check the type and possibly the default value.
1986 */
1987 switch (type) {
1988 case XML_ATTRIBUTE_CDATA:
1989 break;
1990 case XML_ATTRIBUTE_ID:
1991 break;
1992 case XML_ATTRIBUTE_IDREF:
1993 break;
1994 case XML_ATTRIBUTE_IDREFS:
1995 break;
1996 case XML_ATTRIBUTE_ENTITY:
1997 break;
1998 case XML_ATTRIBUTE_ENTITIES:
1999 break;
2000 case XML_ATTRIBUTE_NMTOKEN:
2001 break;
2002 case XML_ATTRIBUTE_NMTOKENS:
2003 break;
2004 case XML_ATTRIBUTE_ENUMERATION:
2005 break;
2006 case XML_ATTRIBUTE_NOTATION:
2007 break;
2008 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002009 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
2010 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2011 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002012 xmlFreeEnumeration(tree);
2013 return(NULL);
2014 }
2015 if ((defaultValue != NULL) &&
2016 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002017 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
2018 "Attribute %s of %s: invalid default value\n",
2019 elem, name, defaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00002020 defaultValue = NULL;
Daniel Veillard42595322004-11-08 10:52:06 +00002021 if (ctxt != NULL)
2022 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002023 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002024#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002025
2026 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00002027 * Check first that an attribute defined in the external subset wasn't
2028 * already defined in the internal subset
2029 */
2030 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
2031 (dtd->doc->intSubset != NULL) &&
2032 (dtd->doc->intSubset->attributes != NULL)) {
2033 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
2034 if (ret != NULL)
2035 return(NULL);
2036 }
2037
2038 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002039 * Create the Attribute table if needed.
2040 */
2041 table = (xmlAttributeTablePtr) dtd->attributes;
2042 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00002043 table = xmlHashCreateDict(0, dict);
Owen Taylor3473f882001-02-23 17:55:21 +00002044 dtd->attributes = (void *) table;
2045 }
2046 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002047 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002048 "xmlAddAttributeDecl: Table creation failed!\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002049 return(NULL);
2050 }
2051
2052
2053 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2054 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002055 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002056 return(NULL);
2057 }
2058 memset(ret, 0, sizeof(xmlAttribute));
2059 ret->type = XML_ATTRIBUTE_DECL;
2060
2061 /*
2062 * fill the structure.
2063 */
2064 ret->atype = type;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002065 if (dict) {
2066 ret->name = xmlDictLookup(dict, name, -1);
2067 ret->prefix = xmlDictLookup(dict, ns, -1);
2068 ret->elem = xmlDictLookup(dict, elem, -1);
2069 } else {
2070 ret->name = xmlStrdup(name);
2071 ret->prefix = xmlStrdup(ns);
2072 ret->elem = xmlStrdup(elem);
2073 }
Owen Taylor3473f882001-02-23 17:55:21 +00002074 ret->def = def;
2075 ret->tree = tree;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002076 if (defaultValue != NULL) {
2077 if (dict)
2078 ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
2079 else
2080 ret->defaultValue = xmlStrdup(defaultValue);
2081 }
Owen Taylor3473f882001-02-23 17:55:21 +00002082
2083 /*
2084 * Validity Check:
2085 * Search the DTD for previous declarations of the ATTLIST
2086 */
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002087 if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002088#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002089 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002090 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002091 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002092 xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00002093 "Attribute %s of element %s: already defined\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002094 name, elem, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00002095#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002096 xmlFreeAttribute(ret);
2097 return(NULL);
2098 }
2099
2100 /*
2101 * Validity Check:
2102 * Multiple ID per element
2103 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00002104 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00002105 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00002106
Daniel Veillard4432df22003-09-28 18:58:27 +00002107#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002108 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00002109 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002110 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
Owen Taylor3473f882001-02-23 17:55:21 +00002111 "Element %s has too may ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002112 elem, name, NULL);
Daniel Veillard42595322004-11-08 10:52:06 +00002113 if (ctxt != NULL)
2114 ctxt->valid = 0;
Daniel Veillardc7612992002-02-17 22:47:37 +00002115 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002116#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardc7612992002-02-17 22:47:37 +00002117
Daniel Veillard48da9102001-08-07 01:10:10 +00002118 /*
2119 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002120 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00002121 */
2122 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
2123 ((ret->prefix != NULL &&
2124 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
2125 ret->nexth = elemDef->attributes;
2126 elemDef->attributes = ret;
2127 } else {
2128 xmlAttributePtr tmp = elemDef->attributes;
2129
2130 while ((tmp != NULL) &&
2131 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
2132 ((ret->prefix != NULL &&
2133 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
2134 if (tmp->nexth == NULL)
2135 break;
2136 tmp = tmp->nexth;
2137 }
2138 if (tmp != NULL) {
2139 ret->nexth = tmp->nexth;
2140 tmp->nexth = ret;
2141 } else {
2142 ret->nexth = elemDef->attributes;
2143 elemDef->attributes = ret;
2144 }
2145 }
Owen Taylor3473f882001-02-23 17:55:21 +00002146 }
2147
2148 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002149 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00002150 */
2151 ret->parent = dtd;
2152 ret->doc = dtd->doc;
2153 if (dtd->last == NULL) {
2154 dtd->children = dtd->last = (xmlNodePtr) ret;
2155 } else {
2156 dtd->last->next = (xmlNodePtr) ret;
2157 ret->prev = dtd->last;
2158 dtd->last = (xmlNodePtr) ret;
2159 }
2160 return(ret);
2161}
2162
2163/**
2164 * xmlFreeAttributeTable:
2165 * @table: An attribute table
2166 *
2167 * Deallocate the memory used by an entities hash table.
2168 */
2169void
2170xmlFreeAttributeTable(xmlAttributeTablePtr table) {
2171 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
2172}
2173
Daniel Veillard652327a2003-09-29 18:02:38 +00002174#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002175/**
2176 * xmlCopyAttribute:
2177 * @attr: An attribute
2178 *
2179 * Build a copy of an attribute.
2180 *
2181 * Returns the new xmlAttributePtr or NULL in case of error.
2182 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002183static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002184xmlCopyAttribute(xmlAttributePtr attr) {
2185 xmlAttributePtr cur;
2186
2187 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2188 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002189 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002190 return(NULL);
2191 }
2192 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00002193 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00002194 cur->atype = attr->atype;
2195 cur->def = attr->def;
2196 cur->tree = xmlCopyEnumeration(attr->tree);
2197 if (attr->elem != NULL)
2198 cur->elem = xmlStrdup(attr->elem);
2199 if (attr->name != NULL)
2200 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00002201 if (attr->prefix != NULL)
2202 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002203 if (attr->defaultValue != NULL)
2204 cur->defaultValue = xmlStrdup(attr->defaultValue);
2205 return(cur);
2206}
2207
2208/**
2209 * xmlCopyAttributeTable:
2210 * @table: An attribute table
2211 *
2212 * Build a copy of an attribute table.
2213 *
2214 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2215 */
2216xmlAttributeTablePtr
2217xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2218 return((xmlAttributeTablePtr) xmlHashCopy(table,
2219 (xmlHashCopier) xmlCopyAttribute));
2220}
Daniel Veillard652327a2003-09-29 18:02:38 +00002221#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002222
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002223#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002224/**
2225 * xmlDumpAttributeDecl:
2226 * @buf: the XML buffer output
2227 * @attr: An attribute declaration
2228 *
2229 * This will dump the content of the attribute declaration as an XML
2230 * DTD definition
2231 */
2232void
2233xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002234 if ((buf == NULL) || (attr == NULL))
2235 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002236 xmlBufferWriteChar(buf, "<!ATTLIST ");
2237 xmlBufferWriteCHAR(buf, attr->elem);
2238 xmlBufferWriteChar(buf, " ");
2239 if (attr->prefix != NULL) {
2240 xmlBufferWriteCHAR(buf, attr->prefix);
2241 xmlBufferWriteChar(buf, ":");
2242 }
2243 xmlBufferWriteCHAR(buf, attr->name);
2244 switch (attr->atype) {
2245 case XML_ATTRIBUTE_CDATA:
2246 xmlBufferWriteChar(buf, " CDATA");
2247 break;
2248 case XML_ATTRIBUTE_ID:
2249 xmlBufferWriteChar(buf, " ID");
2250 break;
2251 case XML_ATTRIBUTE_IDREF:
2252 xmlBufferWriteChar(buf, " IDREF");
2253 break;
2254 case XML_ATTRIBUTE_IDREFS:
2255 xmlBufferWriteChar(buf, " IDREFS");
2256 break;
2257 case XML_ATTRIBUTE_ENTITY:
2258 xmlBufferWriteChar(buf, " ENTITY");
2259 break;
2260 case XML_ATTRIBUTE_ENTITIES:
2261 xmlBufferWriteChar(buf, " ENTITIES");
2262 break;
2263 case XML_ATTRIBUTE_NMTOKEN:
2264 xmlBufferWriteChar(buf, " NMTOKEN");
2265 break;
2266 case XML_ATTRIBUTE_NMTOKENS:
2267 xmlBufferWriteChar(buf, " NMTOKENS");
2268 break;
2269 case XML_ATTRIBUTE_ENUMERATION:
2270 xmlBufferWriteChar(buf, " (");
2271 xmlDumpEnumeration(buf, attr->tree);
2272 break;
2273 case XML_ATTRIBUTE_NOTATION:
2274 xmlBufferWriteChar(buf, " NOTATION (");
2275 xmlDumpEnumeration(buf, attr->tree);
2276 break;
2277 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002278 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2279 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2280 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002281 }
2282 switch (attr->def) {
2283 case XML_ATTRIBUTE_NONE:
2284 break;
2285 case XML_ATTRIBUTE_REQUIRED:
2286 xmlBufferWriteChar(buf, " #REQUIRED");
2287 break;
2288 case XML_ATTRIBUTE_IMPLIED:
2289 xmlBufferWriteChar(buf, " #IMPLIED");
2290 break;
2291 case XML_ATTRIBUTE_FIXED:
2292 xmlBufferWriteChar(buf, " #FIXED");
2293 break;
2294 default:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002295 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2296 "Internal: ATTRIBUTE struct corrupted invalid def\n",
2297 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002298 }
2299 if (attr->defaultValue != NULL) {
2300 xmlBufferWriteChar(buf, " ");
2301 xmlBufferWriteQuotedString(buf, attr->defaultValue);
2302 }
2303 xmlBufferWriteChar(buf, ">\n");
2304}
2305
2306/**
William M. Brack9e660592003-10-20 14:56:06 +00002307 * xmlDumpAttributeDeclScan:
2308 * @attr: An attribute declaration
2309 * @buf: the XML buffer output
2310 *
2311 * This is used with the hash scan function - just reverses arguments
2312 */
2313static void
2314xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) {
2315 xmlDumpAttributeDecl(buf, attr);
2316}
2317
2318/**
Owen Taylor3473f882001-02-23 17:55:21 +00002319 * xmlDumpAttributeTable:
2320 * @buf: the XML buffer output
2321 * @table: An attribute table
2322 *
2323 * This will dump the content of the attribute table as an XML DTD definition
2324 */
2325void
2326xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002327 if ((buf == NULL) || (table == NULL))
2328 return;
William M. Brack9e660592003-10-20 14:56:06 +00002329 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002330}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002331#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002332
2333/************************************************************************
2334 * *
2335 * NOTATIONs *
2336 * *
2337 ************************************************************************/
2338/**
Owen Taylor3473f882001-02-23 17:55:21 +00002339 * xmlFreeNotation:
2340 * @not: A notation
2341 *
2342 * Deallocate the memory used by an notation definition
2343 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002344static void
Owen Taylor3473f882001-02-23 17:55:21 +00002345xmlFreeNotation(xmlNotationPtr nota) {
2346 if (nota == NULL) return;
2347 if (nota->name != NULL)
2348 xmlFree((xmlChar *) nota->name);
2349 if (nota->PublicID != NULL)
2350 xmlFree((xmlChar *) nota->PublicID);
2351 if (nota->SystemID != NULL)
2352 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002353 xmlFree(nota);
2354}
2355
2356
2357/**
2358 * xmlAddNotationDecl:
2359 * @dtd: pointer to the DTD
2360 * @ctxt: the validation context
2361 * @name: the entity name
2362 * @PublicID: the public identifier or NULL
2363 * @SystemID: the system identifier or NULL
2364 *
2365 * Register a new notation declaration
2366 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002367 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002368 */
2369xmlNotationPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002370xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002371 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002372 const xmlChar *PublicID, const xmlChar *SystemID) {
2373 xmlNotationPtr ret;
2374 xmlNotationTablePtr table;
2375
2376 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002377 return(NULL);
2378 }
2379 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002380 return(NULL);
2381 }
2382 if ((PublicID == NULL) && (SystemID == NULL)) {
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002383 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002384 }
2385
2386 /*
2387 * Create the Notation table if needed.
2388 */
2389 table = (xmlNotationTablePtr) dtd->notations;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002390 if (table == NULL) {
2391 xmlDictPtr dict = NULL;
2392 if (dtd->doc != NULL)
2393 dict = dtd->doc->dict;
2394
2395 dtd->notations = table = xmlHashCreateDict(0, dict);
2396 }
Owen Taylor3473f882001-02-23 17:55:21 +00002397 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002398 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002399 "xmlAddNotationDecl: Table creation failed!\n");
2400 return(NULL);
2401 }
2402
2403 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2404 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002405 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002406 return(NULL);
2407 }
2408 memset(ret, 0, sizeof(xmlNotation));
2409
2410 /*
2411 * fill the structure.
2412 */
2413 ret->name = xmlStrdup(name);
2414 if (SystemID != NULL)
2415 ret->SystemID = xmlStrdup(SystemID);
2416 if (PublicID != NULL)
2417 ret->PublicID = xmlStrdup(PublicID);
2418
2419 /*
2420 * Validity Check:
2421 * Check the DTD for previous declarations of the ATTLIST
2422 */
2423 if (xmlHashAddEntry(table, name, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002424#ifdef LIBXML_VALID_ENABLED
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002425 xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
2426 "xmlAddNotationDecl: %s already defined\n",
2427 (const char *) name);
Daniel Veillard4432df22003-09-28 18:58:27 +00002428#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002429 xmlFreeNotation(ret);
2430 return(NULL);
2431 }
2432 return(ret);
2433}
2434
2435/**
2436 * xmlFreeNotationTable:
2437 * @table: An notation table
2438 *
2439 * Deallocate the memory used by an entities hash table.
2440 */
2441void
2442xmlFreeNotationTable(xmlNotationTablePtr table) {
2443 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2444}
2445
Daniel Veillard652327a2003-09-29 18:02:38 +00002446#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002447/**
2448 * xmlCopyNotation:
2449 * @nota: A notation
2450 *
2451 * Build a copy of a notation.
2452 *
2453 * Returns the new xmlNotationPtr or NULL in case of error.
2454 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002455static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002456xmlCopyNotation(xmlNotationPtr nota) {
2457 xmlNotationPtr cur;
2458
2459 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2460 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002461 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002462 return(NULL);
2463 }
2464 if (nota->name != NULL)
2465 cur->name = xmlStrdup(nota->name);
2466 else
2467 cur->name = NULL;
2468 if (nota->PublicID != NULL)
2469 cur->PublicID = xmlStrdup(nota->PublicID);
2470 else
2471 cur->PublicID = NULL;
2472 if (nota->SystemID != NULL)
2473 cur->SystemID = xmlStrdup(nota->SystemID);
2474 else
2475 cur->SystemID = NULL;
2476 return(cur);
2477}
2478
2479/**
2480 * xmlCopyNotationTable:
2481 * @table: A notation table
2482 *
2483 * Build a copy of a notation table.
2484 *
2485 * Returns the new xmlNotationTablePtr or NULL in case of error.
2486 */
2487xmlNotationTablePtr
2488xmlCopyNotationTable(xmlNotationTablePtr table) {
2489 return((xmlNotationTablePtr) xmlHashCopy(table,
2490 (xmlHashCopier) xmlCopyNotation));
2491}
Daniel Veillard652327a2003-09-29 18:02:38 +00002492#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002493
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002494#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002495/**
2496 * xmlDumpNotationDecl:
2497 * @buf: the XML buffer output
2498 * @nota: A notation declaration
2499 *
2500 * This will dump the content the notation declaration as an XML DTD definition
2501 */
2502void
2503xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002504 if ((buf == NULL) || (nota == NULL))
2505 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002506 xmlBufferWriteChar(buf, "<!NOTATION ");
2507 xmlBufferWriteCHAR(buf, nota->name);
2508 if (nota->PublicID != NULL) {
2509 xmlBufferWriteChar(buf, " PUBLIC ");
2510 xmlBufferWriteQuotedString(buf, nota->PublicID);
2511 if (nota->SystemID != NULL) {
2512 xmlBufferWriteChar(buf, " ");
Daniel Veillard41c4a752004-09-08 20:55:38 +00002513 xmlBufferWriteQuotedString(buf, nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002514 }
2515 } else {
2516 xmlBufferWriteChar(buf, " SYSTEM ");
Daniel Veillard41c4a752004-09-08 20:55:38 +00002517 xmlBufferWriteQuotedString(buf, nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002518 }
2519 xmlBufferWriteChar(buf, " >\n");
2520}
2521
2522/**
William M. Brack9e660592003-10-20 14:56:06 +00002523 * xmlDumpNotationDeclScan:
2524 * @nota: A notation declaration
2525 * @buf: the XML buffer output
2526 *
2527 * This is called with the hash scan function, and just reverses args
2528 */
2529static void
2530xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) {
2531 xmlDumpNotationDecl(buf, nota);
2532}
2533
2534/**
Owen Taylor3473f882001-02-23 17:55:21 +00002535 * xmlDumpNotationTable:
2536 * @buf: the XML buffer output
2537 * @table: A notation table
2538 *
2539 * This will dump the content of the notation table as an XML DTD definition
2540 */
2541void
2542xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002543 if ((buf == NULL) || (table == NULL))
2544 return;
William M. Brack9e660592003-10-20 14:56:06 +00002545 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002546}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002547#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002548
2549/************************************************************************
2550 * *
2551 * IDs *
2552 * *
2553 ************************************************************************/
2554/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002555 * DICT_FREE:
2556 * @str: a string
2557 *
2558 * Free a string if it is not owned by the "dict" dictionnary in the
2559 * current scope
2560 */
2561#define DICT_FREE(str) \
2562 if ((str) && ((!dict) || \
2563 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
2564 xmlFree((char *)(str));
2565
2566/**
Owen Taylor3473f882001-02-23 17:55:21 +00002567 * xmlFreeID:
2568 * @not: A id
2569 *
2570 * Deallocate the memory used by an id definition
2571 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002572static void
Owen Taylor3473f882001-02-23 17:55:21 +00002573xmlFreeID(xmlIDPtr id) {
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002574 xmlDictPtr dict = NULL;
2575
Owen Taylor3473f882001-02-23 17:55:21 +00002576 if (id == NULL) return;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002577
2578 if (id->doc != NULL)
2579 dict = id->doc->dict;
2580
Owen Taylor3473f882001-02-23 17:55:21 +00002581 if (id->value != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002582 DICT_FREE(id->value)
Daniel Veillardea7751d2002-12-20 00:16:24 +00002583 if (id->name != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002584 DICT_FREE(id->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002585 xmlFree(id);
2586}
2587
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002588
Owen Taylor3473f882001-02-23 17:55:21 +00002589/**
2590 * xmlAddID:
2591 * @ctxt: the validation context
2592 * @doc: pointer to the document
2593 * @value: the value name
2594 * @attr: the attribute holding the ID
2595 *
2596 * Register a new id declaration
2597 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002598 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002599 */
2600xmlIDPtr
2601xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2602 xmlAttrPtr attr) {
2603 xmlIDPtr ret;
2604 xmlIDTablePtr table;
2605
2606 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002607 return(NULL);
2608 }
2609 if (value == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002610 return(NULL);
2611 }
2612 if (attr == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002613 return(NULL);
2614 }
2615
2616 /*
2617 * Create the ID table if needed.
2618 */
2619 table = (xmlIDTablePtr) doc->ids;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002620 if (table == NULL) {
2621 doc->ids = table = xmlHashCreateDict(0, doc->dict);
2622 }
Owen Taylor3473f882001-02-23 17:55:21 +00002623 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002624 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002625 "xmlAddID: Table creation failed!\n");
2626 return(NULL);
2627 }
2628
2629 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2630 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002631 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002632 return(NULL);
2633 }
2634
2635 /*
2636 * fill the structure.
2637 */
2638 ret->value = xmlStrdup(value);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002639 ret->doc = doc;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002640 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2641 /*
2642 * Operating in streaming mode, attr is gonna disapear
2643 */
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002644 if (doc->dict != NULL)
2645 ret->name = xmlDictLookup(doc->dict, attr->name, -1);
2646 else
2647 ret->name = xmlStrdup(attr->name);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002648 ret->attr = NULL;
2649 } else {
2650 ret->attr = attr;
2651 ret->name = NULL;
2652 }
2653 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002654
2655 if (xmlHashAddEntry(table, value, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002656#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002657 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002658 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002659 */
Daniel Veillardd3669b22004-02-25 12:34:55 +00002660 if ((ctxt != NULL) && (ctxt->error != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002661 xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2662 "ID %s already defined\n",
2663 value, NULL, NULL);
Daniel Veillard76575762002-09-05 14:21:15 +00002664 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002665#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002666 xmlFreeID(ret);
2667 return(NULL);
2668 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002669 if (attr != NULL)
2670 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002671 return(ret);
2672}
2673
2674/**
2675 * xmlFreeIDTable:
2676 * @table: An id table
2677 *
2678 * Deallocate the memory used by an ID hash table.
2679 */
2680void
2681xmlFreeIDTable(xmlIDTablePtr table) {
2682 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2683}
2684
2685/**
2686 * xmlIsID:
2687 * @doc: the document
2688 * @elem: the element carrying the attribute
2689 * @attr: the attribute
2690 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002691 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002692 * then this is done if DTD loading has been requested. In the case
2693 * of HTML documents parsed with the HTML parser, then ID detection is
2694 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002695 *
2696 * Returns 0 or 1 depending on the lookup result
2697 */
2698int
2699xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2700 if (doc == NULL) return(0);
2701 if (attr == NULL) return(0);
2702 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2703 return(0);
2704 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillard9ba8e382003-10-28 21:31:45 +00002705 if (((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2706 (xmlStrEqual(BAD_CAST "name", attr->name))) &&
2707 ((elem != NULL) && (!xmlStrEqual(elem->name, BAD_CAST "input"))))
Owen Taylor3473f882001-02-23 17:55:21 +00002708 return(1);
2709 return(0);
2710 } else {
2711 xmlAttributePtr attrDecl;
2712
2713 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002714 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00002715 xmlChar fn[50];
Daniel Veillard37f961d2002-07-06 17:53:56 +00002716 xmlChar *fullname;
Daniel Veillardc00cda82003-04-07 10:22:39 +00002717
2718 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002719 if (fullname == NULL)
2720 return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002721 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2722 attr->name);
2723 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2724 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2725 attr->name);
Daniel Veillardc00cda82003-04-07 10:22:39 +00002726 if ((fullname != fn) && (fullname != elem->name))
2727 xmlFree(fullname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002728 } else {
2729 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2730 attr->name);
2731 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2732 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2733 attr->name);
2734 }
Owen Taylor3473f882001-02-23 17:55:21 +00002735
2736 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2737 return(1);
2738 }
2739 return(0);
2740}
2741
2742/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002743 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002744 * @doc: the document
2745 * @attr: the attribute
2746 *
2747 * Remove the given attribute from the ID table maintained internally.
2748 *
2749 * Returns -1 if the lookup failed and 0 otherwise
2750 */
2751int
2752xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002753 xmlIDTablePtr table;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002754 xmlIDPtr id;
Owen Taylor3473f882001-02-23 17:55:21 +00002755 xmlChar *ID;
2756
2757 if (doc == NULL) return(-1);
2758 if (attr == NULL) return(-1);
2759 table = (xmlIDTablePtr) doc->ids;
2760 if (table == NULL)
2761 return(-1);
2762
2763 if (attr == NULL)
2764 return(-1);
2765 ID = xmlNodeListGetString(doc, attr->children, 1);
2766 if (ID == NULL)
2767 return(-1);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002768 id = xmlHashLookup(table, ID);
2769 if (id == NULL || id->attr != attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002770 xmlFree(ID);
2771 return(-1);
2772 }
Daniel Veillard91b955c2004-12-10 10:26:42 +00002773 xmlHashRemoveEntry(table, ID, (xmlHashDeallocator) xmlFreeID);
Owen Taylor3473f882001-02-23 17:55:21 +00002774 xmlFree(ID);
2775 return(0);
2776}
2777
2778/**
2779 * xmlGetID:
2780 * @doc: pointer to the document
2781 * @ID: the ID value
2782 *
2783 * Search the attribute declaring the given ID
2784 *
2785 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2786 */
2787xmlAttrPtr
2788xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2789 xmlIDTablePtr table;
2790 xmlIDPtr id;
2791
2792 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002793 return(NULL);
2794 }
2795
2796 if (ID == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002797 return(NULL);
2798 }
2799
2800 table = (xmlIDTablePtr) doc->ids;
2801 if (table == NULL)
2802 return(NULL);
2803
2804 id = xmlHashLookup(table, ID);
2805 if (id == NULL)
2806 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002807 if (id->attr == NULL) {
2808 /*
2809 * We are operating on a stream, return a well known reference
2810 * since the attribute node doesn't exist anymore
2811 */
2812 return((xmlAttrPtr) doc);
2813 }
Owen Taylor3473f882001-02-23 17:55:21 +00002814 return(id->attr);
2815}
2816
2817/************************************************************************
2818 * *
2819 * Refs *
2820 * *
2821 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002822typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002823{
2824 xmlListPtr l;
2825 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002826} xmlRemoveMemo;
2827
2828typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2829
2830typedef struct xmlValidateMemo_t
2831{
2832 xmlValidCtxtPtr ctxt;
2833 const xmlChar *name;
2834} xmlValidateMemo;
2835
2836typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002837
2838/**
Owen Taylor3473f882001-02-23 17:55:21 +00002839 * xmlFreeRef:
2840 * @lk: A list link
2841 *
2842 * Deallocate the memory used by a ref definition
2843 */
2844static void
2845xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002846 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2847 if (ref == NULL) return;
2848 if (ref->value != NULL)
2849 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002850 if (ref->name != NULL)
2851 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002852 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002853}
2854
2855/**
2856 * xmlFreeRefList:
2857 * @list_ref: A list of references.
2858 *
2859 * Deallocate the memory used by a list of references
2860 */
2861static void
2862xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002863 if (list_ref == NULL) return;
2864 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002865}
2866
2867/**
2868 * xmlWalkRemoveRef:
2869 * @data: Contents of current link
2870 * @user: Value supplied by the user
2871 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002872 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002873 */
2874static int
2875xmlWalkRemoveRef(const void *data, const void *user)
2876{
Daniel Veillard37721922001-05-04 15:21:12 +00002877 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2878 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2879 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002880
Daniel Veillard37721922001-05-04 15:21:12 +00002881 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2882 xmlListRemoveFirst(ref_list, (void *)data);
2883 return 0;
2884 }
2885 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002886}
2887
2888/**
Daniel Veillard770075b2004-02-25 10:44:30 +00002889 * xmlDummyCompare
2890 * @data0: Value supplied by the user
2891 * @data1: Value supplied by the user
2892 *
2893 * Do nothing, return 0. Used to create unordered lists.
2894 */
2895static int
Daniel Veillardf54cd532004-02-25 11:52:31 +00002896xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2897 const void *data1 ATTRIBUTE_UNUSED)
Daniel Veillard770075b2004-02-25 10:44:30 +00002898{
2899 return (0);
2900}
2901
2902/**
Owen Taylor3473f882001-02-23 17:55:21 +00002903 * xmlAddRef:
2904 * @ctxt: the validation context
2905 * @doc: pointer to the document
2906 * @value: the value name
2907 * @attr: the attribute holding the Ref
2908 *
2909 * Register a new ref declaration
2910 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002911 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002912 */
2913xmlRefPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002914xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002915 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002916 xmlRefPtr ret;
2917 xmlRefTablePtr table;
2918 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002919
Daniel Veillard37721922001-05-04 15:21:12 +00002920 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002921 return(NULL);
2922 }
2923 if (value == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002924 return(NULL);
2925 }
2926 if (attr == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002927 return(NULL);
2928 }
Owen Taylor3473f882001-02-23 17:55:21 +00002929
Daniel Veillard37721922001-05-04 15:21:12 +00002930 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002931 * Create the Ref table if needed.
2932 */
Daniel Veillard37721922001-05-04 15:21:12 +00002933 table = (xmlRefTablePtr) doc->refs;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002934 if (table == NULL) {
2935 doc->refs = table = xmlHashCreateDict(0, doc->dict);
2936 }
Daniel Veillard37721922001-05-04 15:21:12 +00002937 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002938 xmlVErrMemory(ctxt,
Daniel Veillard37721922001-05-04 15:21:12 +00002939 "xmlAddRef: Table creation failed!\n");
2940 return(NULL);
2941 }
Owen Taylor3473f882001-02-23 17:55:21 +00002942
Daniel Veillard37721922001-05-04 15:21:12 +00002943 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2944 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002945 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard37721922001-05-04 15:21:12 +00002946 return(NULL);
2947 }
Owen Taylor3473f882001-02-23 17:55:21 +00002948
Daniel Veillard37721922001-05-04 15:21:12 +00002949 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002950 * fill the structure.
2951 */
Daniel Veillard37721922001-05-04 15:21:12 +00002952 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002953 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2954 /*
2955 * Operating in streaming mode, attr is gonna disapear
2956 */
2957 ret->name = xmlStrdup(attr->name);
2958 ret->attr = NULL;
2959 } else {
2960 ret->name = NULL;
2961 ret->attr = attr;
2962 }
2963 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002964
Daniel Veillard37721922001-05-04 15:21:12 +00002965 /* To add a reference :-
2966 * References are maintained as a list of references,
2967 * Lookup the entry, if no entry create new nodelist
2968 * Add the owning node to the NodeList
2969 * Return the ref
2970 */
Owen Taylor3473f882001-02-23 17:55:21 +00002971
Daniel Veillard37721922001-05-04 15:21:12 +00002972 if (NULL == (ref_list = xmlHashLookup(table, value))) {
Daniel Veillard770075b2004-02-25 10:44:30 +00002973 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002974 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2975 "xmlAddRef: Reference list creation failed!\n",
2976 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002977 return(NULL);
2978 }
2979 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2980 xmlListDelete(ref_list);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002981 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2982 "xmlAddRef: Reference list insertion failed!\n",
2983 NULL);
Daniel Veillard37721922001-05-04 15:21:12 +00002984 return(NULL);
2985 }
2986 }
Daniel Veillard965983a2004-02-17 16:30:24 +00002987/* xmlListInsert(ref_list, ret); */
2988 xmlListAppend(ref_list, ret);
Daniel Veillard37721922001-05-04 15:21:12 +00002989 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002990}
2991
2992/**
2993 * xmlFreeRefTable:
2994 * @table: An ref table
2995 *
2996 * Deallocate the memory used by an Ref hash table.
2997 */
2998void
2999xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00003000 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00003001}
3002
3003/**
3004 * xmlIsRef:
3005 * @doc: the document
3006 * @elem: the element carrying the attribute
3007 * @attr: the attribute
3008 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003009 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00003010 * then this is simple, otherwise we use an heuristic: name Ref (upper
3011 * or lowercase).
3012 *
3013 * Returns 0 or 1 depending on the lookup result
3014 */
3015int
3016xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003017 if (attr == NULL)
3018 return(0);
3019 if (doc == NULL) {
3020 doc = attr->doc;
3021 if (doc == NULL) return(0);
3022 }
3023
Daniel Veillard37721922001-05-04 15:21:12 +00003024 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
3025 return(0);
3026 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
3027 /* TODO @@@ */
3028 return(0);
3029 } else {
3030 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00003031
Daniel Veillardce244ad2004-11-05 10:03:46 +00003032 if (elem == NULL) return(0);
Daniel Veillard37721922001-05-04 15:21:12 +00003033 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
3034 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3035 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3036 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003037
Daniel Veillard37721922001-05-04 15:21:12 +00003038 if ((attrDecl != NULL) &&
3039 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
3040 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
3041 return(1);
3042 }
3043 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003044}
3045
3046/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003047 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00003048 * @doc: the document
3049 * @attr: the attribute
3050 *
3051 * Remove the given attribute from the Ref table maintained internally.
3052 *
3053 * Returns -1 if the lookup failed and 0 otherwise
3054 */
3055int
3056xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00003057 xmlListPtr ref_list;
3058 xmlRefTablePtr table;
3059 xmlChar *ID;
3060 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00003061
Daniel Veillard37721922001-05-04 15:21:12 +00003062 if (doc == NULL) return(-1);
3063 if (attr == NULL) return(-1);
3064 table = (xmlRefTablePtr) doc->refs;
3065 if (table == NULL)
3066 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003067
Daniel Veillard37721922001-05-04 15:21:12 +00003068 if (attr == NULL)
3069 return(-1);
3070 ID = xmlNodeListGetString(doc, attr->children, 1);
3071 if (ID == NULL)
3072 return(-1);
3073 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00003074
Daniel Veillard37721922001-05-04 15:21:12 +00003075 if(ref_list == NULL) {
3076 xmlFree(ID);
3077 return (-1);
3078 }
3079 /* At this point, ref_list refers to a list of references which
3080 * have the same key as the supplied attr. Our list of references
3081 * is ordered by reference address and we don't have that information
3082 * here to use when removing. We'll have to walk the list and
3083 * check for a matching attribute, when we find one stop the walk
3084 * and remove the entry.
3085 * The list is ordered by reference, so that means we don't have the
3086 * key. Passing the list and the reference to the walker means we
3087 * will have enough data to be able to remove the entry.
3088 */
3089 target.l = ref_list;
3090 target.ap = attr;
3091
3092 /* Remove the supplied attr from our list */
3093 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00003094
Daniel Veillard37721922001-05-04 15:21:12 +00003095 /*If the list is empty then remove the list entry in the hash */
3096 if (xmlListEmpty(ref_list))
3097 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
3098 xmlFreeRefList);
3099 xmlFree(ID);
3100 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003101}
3102
3103/**
3104 * xmlGetRefs:
3105 * @doc: pointer to the document
3106 * @ID: the ID value
3107 *
3108 * Find the set of references for the supplied ID.
3109 *
3110 * Returns NULL if not found, otherwise node set for the ID.
3111 */
3112xmlListPtr
3113xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00003114 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00003115
Daniel Veillard37721922001-05-04 15:21:12 +00003116 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00003117 return(NULL);
3118 }
Owen Taylor3473f882001-02-23 17:55:21 +00003119
Daniel Veillard37721922001-05-04 15:21:12 +00003120 if (ID == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00003121 return(NULL);
3122 }
Owen Taylor3473f882001-02-23 17:55:21 +00003123
Daniel Veillard37721922001-05-04 15:21:12 +00003124 table = (xmlRefTablePtr) doc->refs;
3125 if (table == NULL)
3126 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003127
Daniel Veillard37721922001-05-04 15:21:12 +00003128 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00003129}
3130
3131/************************************************************************
3132 * *
3133 * Routines for validity checking *
3134 * *
3135 ************************************************************************/
3136
3137/**
3138 * xmlGetDtdElementDesc:
3139 * @dtd: a pointer to the DtD to search
3140 * @name: the element name
3141 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003142 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003143 *
3144 * returns the xmlElementPtr if found or NULL
3145 */
3146
3147xmlElementPtr
3148xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
3149 xmlElementTablePtr table;
3150 xmlElementPtr cur;
3151 xmlChar *uqname = NULL, *prefix = NULL;
3152
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003153 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003154 if (dtd->elements == NULL)
3155 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003156 table = (xmlElementTablePtr) dtd->elements;
3157
3158 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003159 if (uqname != NULL)
3160 name = uqname;
3161 cur = xmlHashLookup2(table, name, prefix);
3162 if (prefix != NULL) xmlFree(prefix);
3163 if (uqname != NULL) xmlFree(uqname);
3164 return(cur);
3165}
3166/**
3167 * xmlGetDtdElementDesc2:
3168 * @dtd: a pointer to the DtD to search
3169 * @name: the element name
3170 * @create: create an empty description if not found
3171 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003172 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00003173 *
3174 * returns the xmlElementPtr if found or NULL
3175 */
3176
Daniel Veillard86fd5a72001-12-13 14:55:21 +00003177static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00003178xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
3179 xmlElementTablePtr table;
3180 xmlElementPtr cur;
3181 xmlChar *uqname = NULL, *prefix = NULL;
3182
3183 if (dtd == NULL) return(NULL);
3184 if (dtd->elements == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003185 xmlDictPtr dict = NULL;
3186
3187 if (dtd->doc != NULL)
3188 dict = dtd->doc->dict;
3189
Daniel Veillarda10efa82001-04-18 13:09:01 +00003190 if (!create)
3191 return(NULL);
3192 /*
3193 * Create the Element table if needed.
3194 */
3195 table = (xmlElementTablePtr) dtd->elements;
3196 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003197 table = xmlHashCreateDict(0, dict);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003198 dtd->elements = (void *) table;
3199 }
3200 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003201 xmlVErrMemory(NULL, "element table allocation failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003202 return(NULL);
3203 }
3204 }
3205 table = (xmlElementTablePtr) dtd->elements;
3206
3207 uqname = xmlSplitQName2(name, &prefix);
3208 if (uqname != NULL)
3209 name = uqname;
3210 cur = xmlHashLookup2(table, name, prefix);
3211 if ((cur == NULL) && (create)) {
3212 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
3213 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003214 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003215 return(NULL);
3216 }
3217 memset(cur, 0, sizeof(xmlElement));
3218 cur->type = XML_ELEMENT_DECL;
3219
3220 /*
3221 * fill the structure.
3222 */
3223 cur->name = xmlStrdup(name);
3224 cur->prefix = xmlStrdup(prefix);
3225 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3226
3227 xmlHashAddEntry2(table, name, prefix, cur);
3228 }
3229 if (prefix != NULL) xmlFree(prefix);
3230 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00003231 return(cur);
3232}
3233
3234/**
3235 * xmlGetDtdQElementDesc:
3236 * @dtd: a pointer to the DtD to search
3237 * @name: the element name
3238 * @prefix: the element namespace prefix
3239 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003240 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003241 *
3242 * returns the xmlElementPtr if found or NULL
3243 */
3244
Daniel Veillard48da9102001-08-07 01:10:10 +00003245xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00003246xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3247 const xmlChar *prefix) {
3248 xmlElementTablePtr table;
3249
3250 if (dtd == NULL) return(NULL);
3251 if (dtd->elements == NULL) return(NULL);
3252 table = (xmlElementTablePtr) dtd->elements;
3253
3254 return(xmlHashLookup2(table, name, prefix));
3255}
3256
3257/**
3258 * xmlGetDtdAttrDesc:
3259 * @dtd: a pointer to the DtD to search
3260 * @elem: the element name
3261 * @name: the attribute name
3262 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003263 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003264 * this element.
3265 *
3266 * returns the xmlAttributePtr if found or NULL
3267 */
3268
3269xmlAttributePtr
3270xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3271 xmlAttributeTablePtr table;
3272 xmlAttributePtr cur;
3273 xmlChar *uqname = NULL, *prefix = NULL;
3274
3275 if (dtd == NULL) return(NULL);
3276 if (dtd->attributes == NULL) return(NULL);
3277
3278 table = (xmlAttributeTablePtr) dtd->attributes;
3279 if (table == NULL)
3280 return(NULL);
3281
3282 uqname = xmlSplitQName2(name, &prefix);
3283
3284 if (uqname != NULL) {
3285 cur = xmlHashLookup3(table, uqname, prefix, elem);
3286 if (prefix != NULL) xmlFree(prefix);
3287 if (uqname != NULL) xmlFree(uqname);
3288 } else
3289 cur = xmlHashLookup3(table, name, NULL, elem);
3290 return(cur);
3291}
3292
3293/**
3294 * xmlGetDtdQAttrDesc:
3295 * @dtd: a pointer to the DtD to search
3296 * @elem: the element name
3297 * @name: the attribute name
3298 * @prefix: the attribute namespace prefix
3299 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003300 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003301 * this element.
3302 *
3303 * returns the xmlAttributePtr if found or NULL
3304 */
3305
Daniel Veillard48da9102001-08-07 01:10:10 +00003306xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00003307xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3308 const xmlChar *prefix) {
3309 xmlAttributeTablePtr table;
3310
3311 if (dtd == NULL) return(NULL);
3312 if (dtd->attributes == NULL) return(NULL);
3313 table = (xmlAttributeTablePtr) dtd->attributes;
3314
3315 return(xmlHashLookup3(table, name, prefix, elem));
3316}
3317
3318/**
3319 * xmlGetDtdNotationDesc:
3320 * @dtd: a pointer to the DtD to search
3321 * @name: the notation name
3322 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003323 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00003324 *
3325 * returns the xmlNotationPtr if found or NULL
3326 */
3327
3328xmlNotationPtr
3329xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3330 xmlNotationTablePtr table;
3331
3332 if (dtd == NULL) return(NULL);
3333 if (dtd->notations == NULL) return(NULL);
3334 table = (xmlNotationTablePtr) dtd->notations;
3335
3336 return(xmlHashLookup(table, name));
3337}
3338
Daniel Veillardf54cd532004-02-25 11:52:31 +00003339#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003340/**
3341 * xmlValidateNotationUse:
3342 * @ctxt: the validation context
3343 * @doc: the document
3344 * @notationName: the notation name to check
3345 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003346 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00003347 * - [ VC: Notation Declared ]
3348 *
3349 * returns 1 if valid or 0 otherwise
3350 */
3351
3352int
3353xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3354 const xmlChar *notationName) {
3355 xmlNotationPtr notaDecl;
3356 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3357
3358 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3359 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3360 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3361
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003362 if ((notaDecl == NULL) && (ctxt != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003363 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3364 "NOTATION %s is not declared\n",
3365 notationName, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003366 return(0);
3367 }
3368 return(1);
3369}
Daniel Veillardf54cd532004-02-25 11:52:31 +00003370#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003371
3372/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003373 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00003374 * @doc: the document
3375 * @name: the element name
3376 *
3377 * Search in the DtDs whether an element accept Mixed content (or ANY)
3378 * basically if it is supposed to accept text childs
3379 *
3380 * returns 0 if no, 1 if yes, and -1 if no element description is available
3381 */
3382
3383int
3384xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3385 xmlElementPtr elemDecl;
3386
3387 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3388
3389 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3390 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3391 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3392 if (elemDecl == NULL) return(-1);
3393 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00003394 case XML_ELEMENT_TYPE_UNDEFINED:
3395 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003396 case XML_ELEMENT_TYPE_ELEMENT:
3397 return(0);
3398 case XML_ELEMENT_TYPE_EMPTY:
3399 /*
3400 * return 1 for EMPTY since we want VC error to pop up
3401 * on <empty> </empty> for example
3402 */
3403 case XML_ELEMENT_TYPE_ANY:
3404 case XML_ELEMENT_TYPE_MIXED:
3405 return(1);
3406 }
3407 return(1);
3408}
3409
Daniel Veillard4432df22003-09-28 18:58:27 +00003410#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003411/**
3412 * xmlValidateNameValue:
3413 * @value: an Name value
3414 *
3415 * Validate that the given value match Name production
3416 *
3417 * returns 1 if valid or 0 otherwise
3418 */
3419
Daniel Veillard9b731d72002-04-14 12:56:08 +00003420int
Owen Taylor3473f882001-02-23 17:55:21 +00003421xmlValidateNameValue(const xmlChar *value) {
3422 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003423 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003424
3425 if (value == NULL) return(0);
3426 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003427 val = xmlStringCurrentChar(NULL, cur, &len);
3428 cur += len;
3429 if (!IS_LETTER(val) && (val != '_') &&
3430 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003431 return(0);
3432 }
3433
Daniel Veillardd8224e02002-01-13 15:43:22 +00003434 val = xmlStringCurrentChar(NULL, cur, &len);
3435 cur += len;
3436 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3437 (val == '.') || (val == '-') ||
3438 (val == '_') || (val == ':') ||
3439 (IS_COMBINING(val)) ||
3440 (IS_EXTENDER(val))) {
3441 val = xmlStringCurrentChar(NULL, cur, &len);
3442 cur += len;
3443 }
Owen Taylor3473f882001-02-23 17:55:21 +00003444
Daniel Veillardd8224e02002-01-13 15:43:22 +00003445 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003446
3447 return(1);
3448}
3449
3450/**
3451 * xmlValidateNamesValue:
3452 * @value: an Names value
3453 *
3454 * Validate that the given value match Names production
3455 *
3456 * returns 1 if valid or 0 otherwise
3457 */
3458
Daniel Veillard9b731d72002-04-14 12:56:08 +00003459int
Owen Taylor3473f882001-02-23 17:55:21 +00003460xmlValidateNamesValue(const xmlChar *value) {
3461 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003462 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003463
3464 if (value == NULL) return(0);
3465 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003466 val = xmlStringCurrentChar(NULL, cur, &len);
3467 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003468
Daniel Veillardd8224e02002-01-13 15:43:22 +00003469 if (!IS_LETTER(val) && (val != '_') &&
3470 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003471 return(0);
3472 }
3473
Daniel Veillardd8224e02002-01-13 15:43:22 +00003474 val = xmlStringCurrentChar(NULL, cur, &len);
3475 cur += len;
3476 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3477 (val == '.') || (val == '-') ||
3478 (val == '_') || (val == ':') ||
3479 (IS_COMBINING(val)) ||
3480 (IS_EXTENDER(val))) {
3481 val = xmlStringCurrentChar(NULL, cur, &len);
3482 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003483 }
3484
Daniel Veillard807b4de2004-09-26 14:42:56 +00003485 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3486 while (val == 0x20) {
3487 while (val == 0x20) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003488 val = xmlStringCurrentChar(NULL, cur, &len);
3489 cur += len;
3490 }
3491
3492 if (!IS_LETTER(val) && (val != '_') &&
3493 (val != ':')) {
3494 return(0);
3495 }
3496 val = xmlStringCurrentChar(NULL, cur, &len);
3497 cur += len;
3498
3499 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3500 (val == '.') || (val == '-') ||
3501 (val == '_') || (val == ':') ||
3502 (IS_COMBINING(val)) ||
3503 (IS_EXTENDER(val))) {
3504 val = xmlStringCurrentChar(NULL, cur, &len);
3505 cur += len;
3506 }
3507 }
3508
3509 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003510
3511 return(1);
3512}
3513
3514/**
3515 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003516 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003517 *
3518 * Validate that the given value match Nmtoken production
3519 *
3520 * [ VC: Name Token ]
3521 *
3522 * returns 1 if valid or 0 otherwise
3523 */
3524
Daniel Veillard9b731d72002-04-14 12:56:08 +00003525int
Owen Taylor3473f882001-02-23 17:55:21 +00003526xmlValidateNmtokenValue(const xmlChar *value) {
3527 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003528 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003529
3530 if (value == NULL) return(0);
3531 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003532 val = xmlStringCurrentChar(NULL, cur, &len);
3533 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003534
Daniel Veillardd8224e02002-01-13 15:43:22 +00003535 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3536 (val != '.') && (val != '-') &&
3537 (val != '_') && (val != ':') &&
3538 (!IS_COMBINING(val)) &&
3539 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003540 return(0);
3541
Daniel Veillardd8224e02002-01-13 15:43:22 +00003542 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3543 (val == '.') || (val == '-') ||
3544 (val == '_') || (val == ':') ||
3545 (IS_COMBINING(val)) ||
3546 (IS_EXTENDER(val))) {
3547 val = xmlStringCurrentChar(NULL, cur, &len);
3548 cur += len;
3549 }
Owen Taylor3473f882001-02-23 17:55:21 +00003550
Daniel Veillardd8224e02002-01-13 15:43:22 +00003551 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003552
3553 return(1);
3554}
3555
3556/**
3557 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003558 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003559 *
3560 * Validate that the given value match Nmtokens production
3561 *
3562 * [ VC: Name Token ]
3563 *
3564 * returns 1 if valid or 0 otherwise
3565 */
3566
Daniel Veillard9b731d72002-04-14 12:56:08 +00003567int
Owen Taylor3473f882001-02-23 17:55:21 +00003568xmlValidateNmtokensValue(const xmlChar *value) {
3569 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003570 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003571
3572 if (value == NULL) return(0);
3573 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003574 val = xmlStringCurrentChar(NULL, cur, &len);
3575 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003576
Daniel Veillardd8224e02002-01-13 15:43:22 +00003577 while (IS_BLANK(val)) {
3578 val = xmlStringCurrentChar(NULL, cur, &len);
3579 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003580 }
3581
Daniel Veillardd8224e02002-01-13 15:43:22 +00003582 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3583 (val != '.') && (val != '-') &&
3584 (val != '_') && (val != ':') &&
3585 (!IS_COMBINING(val)) &&
3586 (!IS_EXTENDER(val)))
3587 return(0);
3588
3589 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3590 (val == '.') || (val == '-') ||
3591 (val == '_') || (val == ':') ||
3592 (IS_COMBINING(val)) ||
3593 (IS_EXTENDER(val))) {
3594 val = xmlStringCurrentChar(NULL, cur, &len);
3595 cur += len;
3596 }
3597
Daniel Veillard807b4de2004-09-26 14:42:56 +00003598 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3599 while (val == 0x20) {
3600 while (val == 0x20) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003601 val = xmlStringCurrentChar(NULL, cur, &len);
3602 cur += len;
3603 }
3604 if (val == 0) return(1);
3605
3606 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3607 (val != '.') && (val != '-') &&
3608 (val != '_') && (val != ':') &&
3609 (!IS_COMBINING(val)) &&
3610 (!IS_EXTENDER(val)))
3611 return(0);
3612
3613 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3614 (val == '.') || (val == '-') ||
3615 (val == '_') || (val == ':') ||
3616 (IS_COMBINING(val)) ||
3617 (IS_EXTENDER(val))) {
3618 val = xmlStringCurrentChar(NULL, cur, &len);
3619 cur += len;
3620 }
3621 }
3622
3623 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003624
3625 return(1);
3626}
3627
3628/**
3629 * xmlValidateNotationDecl:
3630 * @ctxt: the validation context
3631 * @doc: a document instance
3632 * @nota: a notation definition
3633 *
3634 * Try to validate a single notation definition
3635 * basically it does the following checks as described by the
3636 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003637 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003638 * But this function get called anyway ...
3639 *
3640 * returns 1 if valid or 0 otherwise
3641 */
3642
3643int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003644xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3645 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003646 int ret = 1;
3647
3648 return(ret);
3649}
3650
3651/**
3652 * xmlValidateAttributeValue:
3653 * @type: an attribute type
3654 * @value: an attribute value
3655 *
3656 * Validate that the given attribute value match the proper production
3657 *
3658 * [ VC: ID ]
3659 * Values of type ID must match the Name production....
3660 *
3661 * [ VC: IDREF ]
3662 * Values of type IDREF must match the Name production, and values
3663 * of type IDREFS must match Names ...
3664 *
3665 * [ VC: Entity Name ]
3666 * Values of type ENTITY must match the Name production, values
3667 * of type ENTITIES must match Names ...
3668 *
3669 * [ VC: Name Token ]
3670 * Values of type NMTOKEN must match the Nmtoken production; values
3671 * of type NMTOKENS must match Nmtokens.
3672 *
3673 * returns 1 if valid or 0 otherwise
3674 */
3675
3676int
3677xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3678 switch (type) {
3679 case XML_ATTRIBUTE_ENTITIES:
3680 case XML_ATTRIBUTE_IDREFS:
3681 return(xmlValidateNamesValue(value));
3682 case XML_ATTRIBUTE_ENTITY:
3683 case XML_ATTRIBUTE_IDREF:
3684 case XML_ATTRIBUTE_ID:
3685 case XML_ATTRIBUTE_NOTATION:
3686 return(xmlValidateNameValue(value));
3687 case XML_ATTRIBUTE_NMTOKENS:
3688 case XML_ATTRIBUTE_ENUMERATION:
3689 return(xmlValidateNmtokensValue(value));
3690 case XML_ATTRIBUTE_NMTOKEN:
3691 return(xmlValidateNmtokenValue(value));
3692 case XML_ATTRIBUTE_CDATA:
3693 break;
3694 }
3695 return(1);
3696}
3697
3698/**
3699 * xmlValidateAttributeValue2:
3700 * @ctxt: the validation context
3701 * @doc: the document
3702 * @name: the attribute name (used for error reporting only)
3703 * @type: the attribute type
3704 * @value: the attribute value
3705 *
3706 * Validate that the given attribute value match a given type.
3707 * This typically cannot be done before having finished parsing
3708 * the subsets.
3709 *
3710 * [ VC: IDREF ]
3711 * Values of type IDREF must match one of the declared IDs
3712 * Values of type IDREFS must match a sequence of the declared IDs
3713 * each Name must match the value of an ID attribute on some element
3714 * in the XML document; i.e. IDREF values must match the value of
3715 * some ID attribute
3716 *
3717 * [ VC: Entity Name ]
3718 * Values of type ENTITY must match one declared entity
3719 * Values of type ENTITIES must match a sequence of declared entities
3720 *
3721 * [ VC: Notation Attributes ]
3722 * all notation names in the declaration must be declared.
3723 *
3724 * returns 1 if valid or 0 otherwise
3725 */
3726
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003727static int
Owen Taylor3473f882001-02-23 17:55:21 +00003728xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3729 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3730 int ret = 1;
3731 switch (type) {
3732 case XML_ATTRIBUTE_IDREFS:
3733 case XML_ATTRIBUTE_IDREF:
3734 case XML_ATTRIBUTE_ID:
3735 case XML_ATTRIBUTE_NMTOKENS:
3736 case XML_ATTRIBUTE_ENUMERATION:
3737 case XML_ATTRIBUTE_NMTOKEN:
3738 case XML_ATTRIBUTE_CDATA:
3739 break;
3740 case XML_ATTRIBUTE_ENTITY: {
3741 xmlEntityPtr ent;
3742
3743 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003744 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003745 if ((ent == NULL) && (doc->standalone == 1)) {
3746 doc->standalone = 0;
3747 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003748 }
Owen Taylor3473f882001-02-23 17:55:21 +00003749 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003750 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3751 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003752 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003753 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003754 ret = 0;
3755 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003756 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3757 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003758 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003759 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003760 ret = 0;
3761 }
3762 break;
3763 }
3764 case XML_ATTRIBUTE_ENTITIES: {
3765 xmlChar *dup, *nam = NULL, *cur, save;
3766 xmlEntityPtr ent;
3767
3768 dup = xmlStrdup(value);
3769 if (dup == NULL)
3770 return(0);
3771 cur = dup;
3772 while (*cur != 0) {
3773 nam = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00003774 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003775 save = *cur;
3776 *cur = 0;
3777 ent = xmlGetDocEntity(doc, nam);
3778 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003779 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3780 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003781 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003782 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003783 ret = 0;
3784 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003785 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3786 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003787 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003788 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003789 ret = 0;
3790 }
3791 if (save == 0)
3792 break;
3793 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00003794 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003795 }
3796 xmlFree(dup);
3797 break;
3798 }
3799 case XML_ATTRIBUTE_NOTATION: {
3800 xmlNotationPtr nota;
3801
3802 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3803 if ((nota == NULL) && (doc->extSubset != NULL))
3804 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3805
3806 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003807 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3808 XML_DTD_UNKNOWN_NOTATION,
Owen Taylor3473f882001-02-23 17:55:21 +00003809 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003810 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003811 ret = 0;
3812 }
3813 break;
3814 }
3815 }
3816 return(ret);
3817}
3818
3819/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003820 * xmlValidCtxtNormalizeAttributeValue:
3821 * @ctxt: the validation context
3822 * @doc: the document
3823 * @elem: the parent
3824 * @name: the attribute name
3825 * @value: the attribute value
3826 * @ctxt: the validation context or NULL
3827 *
3828 * Does the validation related extra step of the normalization of attribute
3829 * values:
3830 *
3831 * If the declared value is not CDATA, then the XML processor must further
3832 * process the normalized attribute value by discarding any leading and
3833 * trailing space (#x20) characters, and by replacing sequences of space
3834 * (#x20) characters by single space (#x20) character.
3835 *
3836 * Also check VC: Standalone Document Declaration in P32, and update
3837 * ctxt->valid accordingly
3838 *
3839 * returns a new normalized string if normalization is needed, NULL otherwise
3840 * the caller must free the returned value.
3841 */
3842
3843xmlChar *
3844xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3845 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3846 xmlChar *ret, *dst;
3847 const xmlChar *src;
3848 xmlAttributePtr attrDecl = NULL;
3849 int extsubset = 0;
3850
3851 if (doc == NULL) return(NULL);
3852 if (elem == NULL) return(NULL);
3853 if (name == NULL) return(NULL);
3854 if (value == NULL) return(NULL);
3855
3856 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003857 xmlChar fn[50];
3858 xmlChar *fullname;
3859
3860 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3861 if (fullname == NULL)
3862 return(0);
3863 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003864 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003865 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003866 if (attrDecl != NULL)
3867 extsubset = 1;
3868 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003869 if ((fullname != fn) && (fullname != elem->name))
3870 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003871 }
3872 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3873 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3874 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3875 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3876 if (attrDecl != NULL)
3877 extsubset = 1;
3878 }
3879
3880 if (attrDecl == NULL)
3881 return(NULL);
3882 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3883 return(NULL);
3884
3885 ret = xmlStrdup(value);
3886 if (ret == NULL)
3887 return(NULL);
3888 src = value;
3889 dst = ret;
3890 while (*src == 0x20) src++;
3891 while (*src != 0) {
3892 if (*src == 0x20) {
3893 while (*src == 0x20) src++;
3894 if (*src != 0)
3895 *dst++ = 0x20;
3896 } else {
3897 *dst++ = *src++;
3898 }
3899 }
3900 *dst = 0;
3901 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003902 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003903"standalone: %s on %s value had to be normalized based on external subset declaration\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003904 name, elem->name, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003905 ctxt->valid = 0;
3906 }
3907 return(ret);
3908}
3909
3910/**
Owen Taylor3473f882001-02-23 17:55:21 +00003911 * xmlValidNormalizeAttributeValue:
3912 * @doc: the document
3913 * @elem: the parent
3914 * @name: the attribute name
3915 * @value: the attribute value
3916 *
3917 * Does the validation related extra step of the normalization of attribute
3918 * values:
3919 *
3920 * If the declared value is not CDATA, then the XML processor must further
3921 * process the normalized attribute value by discarding any leading and
3922 * trailing space (#x20) characters, and by replacing sequences of space
3923 * (#x20) characters by single space (#x20) character.
3924 *
Daniel Veillard652327a2003-09-29 18:02:38 +00003925 * Returns a new normalized string if normalization is needed, NULL otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00003926 * the caller must free the returned value.
3927 */
3928
3929xmlChar *
3930xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3931 const xmlChar *name, const xmlChar *value) {
3932 xmlChar *ret, *dst;
3933 const xmlChar *src;
3934 xmlAttributePtr attrDecl = NULL;
3935
3936 if (doc == NULL) return(NULL);
3937 if (elem == NULL) return(NULL);
3938 if (name == NULL) return(NULL);
3939 if (value == NULL) return(NULL);
3940
3941 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003942 xmlChar fn[50];
3943 xmlChar *fullname;
3944
3945 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3946 if (fullname == NULL)
3947 return(0);
3948 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003949 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003950 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3951 if ((fullname != fn) && (fullname != elem->name))
3952 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003953 }
3954 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3955 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3956 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3957
3958 if (attrDecl == NULL)
3959 return(NULL);
3960 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3961 return(NULL);
3962
3963 ret = xmlStrdup(value);
3964 if (ret == NULL)
3965 return(NULL);
3966 src = value;
3967 dst = ret;
3968 while (*src == 0x20) src++;
3969 while (*src != 0) {
3970 if (*src == 0x20) {
3971 while (*src == 0x20) src++;
3972 if (*src != 0)
3973 *dst++ = 0x20;
3974 } else {
3975 *dst++ = *src++;
3976 }
3977 }
3978 *dst = 0;
3979 return(ret);
3980}
3981
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003982static void
Owen Taylor3473f882001-02-23 17:55:21 +00003983xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003984 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003985 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3986}
3987
3988/**
3989 * xmlValidateAttributeDecl:
3990 * @ctxt: the validation context
3991 * @doc: a document instance
3992 * @attr: an attribute definition
3993 *
3994 * Try to validate a single attribute definition
3995 * basically it does the following checks as described by the
3996 * XML-1.0 recommendation:
3997 * - [ VC: Attribute Default Legal ]
3998 * - [ VC: Enumeration ]
3999 * - [ VC: ID Attribute Default ]
4000 *
4001 * The ID/IDREF uniqueness and matching are done separately
4002 *
4003 * returns 1 if valid or 0 otherwise
4004 */
4005
4006int
4007xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4008 xmlAttributePtr attr) {
4009 int ret = 1;
4010 int val;
4011 CHECK_DTD;
4012 if(attr == NULL) return(1);
4013
4014 /* Attribute Default Legal */
4015 /* Enumeration */
4016 if (attr->defaultValue != NULL) {
4017 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
4018 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004019 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004020 "Syntax of default value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004021 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004022 }
4023 ret &= val;
4024 }
4025
4026 /* ID Attribute Default */
4027 if ((attr->atype == XML_ATTRIBUTE_ID)&&
4028 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
4029 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004030 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004031 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004032 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004033 ret = 0;
4034 }
4035
4036 /* One ID per Element Type */
4037 if (attr->atype == XML_ATTRIBUTE_ID) {
4038 int nbId;
4039
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004040 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00004041 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
4042 attr->elem);
4043 if (elem != NULL) {
4044 nbId = xmlScanIDAttributeDecl(NULL, elem);
4045 } else {
4046 xmlAttributeTablePtr table;
4047
4048 /*
4049 * The attribute may be declared in the internal subset and the
4050 * element in the external subset.
4051 */
4052 nbId = 0;
4053 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
4054 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
4055 xmlValidateAttributeIdCallback, &nbId);
4056 }
4057 if (nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004058
4059 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004060 "Element %s has %d ID attribute defined in the internal subset : %s\n",
4061 attr->elem, nbId, attr->name);
4062 } else if (doc->extSubset != NULL) {
4063 int extId = 0;
4064 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
4065 if (elem != NULL) {
4066 extId = xmlScanIDAttributeDecl(NULL, elem);
4067 }
4068 if (extId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004069 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 external subset : %s\n",
4071 attr->elem, extId, attr->name);
4072 } else if (extId + nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004073 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004074"Element %s has ID attributes defined in the internal and external subset : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004075 attr->elem, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004076 }
4077 }
4078 }
4079
4080 /* Validity Constraint: Enumeration */
4081 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
4082 xmlEnumerationPtr tree = attr->tree;
4083 while (tree != NULL) {
4084 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
4085 tree = tree->next;
4086 }
4087 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004088 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004089"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004090 attr->defaultValue, attr->name, attr->elem);
4091 ret = 0;
4092 }
4093 }
4094
4095 return(ret);
4096}
4097
4098/**
4099 * xmlValidateElementDecl:
4100 * @ctxt: the validation context
4101 * @doc: a document instance
4102 * @elem: an element definition
4103 *
4104 * Try to validate a single element definition
4105 * basically it does the following checks as described by the
4106 * XML-1.0 recommendation:
4107 * - [ VC: One ID per Element Type ]
4108 * - [ VC: No Duplicate Types ]
4109 * - [ VC: Unique Element Type Declaration ]
4110 *
4111 * returns 1 if valid or 0 otherwise
4112 */
4113
4114int
4115xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4116 xmlElementPtr elem) {
4117 int ret = 1;
4118 xmlElementPtr tst;
4119
4120 CHECK_DTD;
4121
4122 if (elem == NULL) return(1);
4123
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004124#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00004125#ifdef LIBXML_REGEXP_ENABLED
4126 /* Build the regexp associated to the content model */
4127 ret = xmlValidBuildContentModel(ctxt, elem);
4128#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004129#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00004130
Owen Taylor3473f882001-02-23 17:55:21 +00004131 /* No Duplicate Types */
4132 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
4133 xmlElementContentPtr cur, next;
4134 const xmlChar *name;
4135
4136 cur = elem->content;
4137 while (cur != NULL) {
4138 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
4139 if (cur->c1 == NULL) break;
4140 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4141 name = cur->c1->name;
4142 next = cur->c2;
4143 while (next != NULL) {
4144 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00004145 if ((xmlStrEqual(next->name, name)) &&
4146 (xmlStrEqual(next->prefix, cur->prefix))) {
4147 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004148 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00004149 "Definition of %s has duplicate references of %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004150 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004151 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004152 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004153 "Definition of %s has duplicate references of %s:%s\n",
4154 elem->name, cur->prefix, name);
4155 }
Owen Taylor3473f882001-02-23 17:55:21 +00004156 ret = 0;
4157 }
4158 break;
4159 }
4160 if (next->c1 == NULL) break;
4161 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00004162 if ((xmlStrEqual(next->c1->name, name)) &&
4163 (xmlStrEqual(next->c1->prefix, cur->prefix))) {
4164 if (cur->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004165 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004166 "Definition of %s has duplicate references to %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004167 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004168 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004169 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004170 "Definition of %s has duplicate references to %s:%s\n",
4171 elem->name, cur->prefix, name);
4172 }
Owen Taylor3473f882001-02-23 17:55:21 +00004173 ret = 0;
4174 }
4175 next = next->c2;
4176 }
4177 }
4178 cur = cur->c2;
4179 }
4180 }
4181
4182 /* VC: Unique Element Type Declaration */
4183 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004184 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004185 ((tst->prefix == elem->prefix) ||
4186 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004187 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004188 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4189 "Redefinition of element %s\n",
4190 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004191 ret = 0;
4192 }
4193 tst = xmlGetDtdElementDesc(doc->extSubset, 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 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00004203 /* One ID per Element Type
4204 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00004205 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4206 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00004207 } */
Owen Taylor3473f882001-02-23 17:55:21 +00004208 return(ret);
4209}
4210
4211/**
4212 * xmlValidateOneAttribute:
4213 * @ctxt: the validation context
4214 * @doc: a document instance
4215 * @elem: an element instance
4216 * @attr: an attribute instance
4217 * @value: the attribute value (without entities processing)
4218 *
4219 * Try to validate a single attribute for an element
4220 * basically it does the following checks as described by the
4221 * XML-1.0 recommendation:
4222 * - [ VC: Attribute Value Type ]
4223 * - [ VC: Fixed Attribute Default ]
4224 * - [ VC: Entity Name ]
4225 * - [ VC: Name Token ]
4226 * - [ VC: ID ]
4227 * - [ VC: IDREF ]
4228 * - [ VC: Entity Name ]
4229 * - [ VC: Notation Attributes ]
4230 *
4231 * The ID/IDREF uniqueness and matching are done separately
4232 *
4233 * returns 1 if valid or 0 otherwise
4234 */
4235
4236int
4237xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillard07cb8222003-09-10 10:51:05 +00004238 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4239{
Owen Taylor3473f882001-02-23 17:55:21 +00004240 xmlAttributePtr attrDecl = NULL;
4241 int val;
4242 int ret = 1;
4243
4244 CHECK_DTD;
4245 if ((elem == NULL) || (elem->name == NULL)) return(0);
4246 if ((attr == NULL) || (attr->name == NULL)) return(0);
4247
4248 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004249 xmlChar fn[50];
4250 xmlChar *fullname;
4251
4252 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4253 if (fullname == NULL)
4254 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004255 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004256 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004257 attr->name, attr->ns->prefix);
4258 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004259 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004260 attr->name, attr->ns->prefix);
4261 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004262 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004263 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4264 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00004265 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004266 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004267 if ((fullname != fn) && (fullname != elem->name))
4268 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00004269 }
4270 if (attrDecl == NULL) {
4271 if (attr->ns != NULL) {
4272 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4273 attr->name, attr->ns->prefix);
4274 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4275 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4276 attr->name, attr->ns->prefix);
4277 } else {
4278 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4279 elem->name, attr->name);
4280 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4281 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4282 elem->name, attr->name);
4283 }
4284 }
4285
4286
4287 /* Validity Constraint: Attribute Value Type */
4288 if (attrDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004289 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004290 "No declaration for attribute %s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004291 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004292 return(0);
4293 }
4294 attr->atype = attrDecl->atype;
4295
4296 val = xmlValidateAttributeValue(attrDecl->atype, value);
4297 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004298 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004299 "Syntax of value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004300 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004301 ret = 0;
4302 }
4303
4304 /* Validity constraint: Fixed Attribute Default */
4305 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4306 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004307 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004308 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004309 attr->name, elem->name, attrDecl->defaultValue);
4310 ret = 0;
4311 }
4312 }
4313
4314 /* Validity Constraint: ID uniqueness */
4315 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4316 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4317 ret = 0;
4318 }
4319
4320 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4321 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4322 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4323 ret = 0;
4324 }
4325
4326 /* Validity Constraint: Notation Attributes */
4327 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4328 xmlEnumerationPtr tree = attrDecl->tree;
4329 xmlNotationPtr nota;
4330
4331 /* First check that the given NOTATION was declared */
4332 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4333 if (nota == NULL)
4334 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4335
4336 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004337 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004338 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004339 value, attr->name, elem->name);
4340 ret = 0;
4341 }
4342
4343 /* Second, verify that it's among the list */
4344 while (tree != NULL) {
4345 if (xmlStrEqual(tree->name, value)) break;
4346 tree = tree->next;
4347 }
4348 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004349 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004350"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004351 value, attr->name, elem->name);
4352 ret = 0;
4353 }
4354 }
4355
4356 /* Validity Constraint: Enumeration */
4357 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4358 xmlEnumerationPtr tree = attrDecl->tree;
4359 while (tree != NULL) {
4360 if (xmlStrEqual(tree->name, value)) break;
4361 tree = tree->next;
4362 }
4363 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004364 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004365 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004366 value, attr->name, elem->name);
4367 ret = 0;
4368 }
4369 }
4370
4371 /* Fixed Attribute Default */
4372 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4373 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004374 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004375 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004376 attr->name, elem->name, attrDecl->defaultValue);
4377 ret = 0;
4378 }
4379
4380 /* Extra check for the attribute value */
4381 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4382 attrDecl->atype, value);
4383
4384 return(ret);
4385}
4386
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004387/**
4388 * xmlValidateOneNamespace:
4389 * @ctxt: the validation context
4390 * @doc: a document instance
4391 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004392 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004393 * @ns: an namespace declaration instance
4394 * @value: the attribute value (without entities processing)
4395 *
4396 * Try to validate a single namespace declaration for an element
4397 * basically it does the following checks as described by the
4398 * XML-1.0 recommendation:
4399 * - [ VC: Attribute Value Type ]
4400 * - [ VC: Fixed Attribute Default ]
4401 * - [ VC: Entity Name ]
4402 * - [ VC: Name Token ]
4403 * - [ VC: ID ]
4404 * - [ VC: IDREF ]
4405 * - [ VC: Entity Name ]
4406 * - [ VC: Notation Attributes ]
4407 *
4408 * The ID/IDREF uniqueness and matching are done separately
4409 *
4410 * returns 1 if valid or 0 otherwise
4411 */
4412
4413int
4414xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4415xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4416 /* xmlElementPtr elemDecl; */
4417 xmlAttributePtr attrDecl = NULL;
4418 int val;
4419 int ret = 1;
4420
4421 CHECK_DTD;
4422 if ((elem == NULL) || (elem->name == NULL)) return(0);
4423 if ((ns == NULL) || (ns->href == NULL)) return(0);
4424
4425 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004426 xmlChar fn[50];
4427 xmlChar *fullname;
4428
4429 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4430 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004431 xmlVErrMemory(ctxt, "Validating namespace");
Daniel Veillardc00cda82003-04-07 10:22:39 +00004432 return(0);
4433 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004434 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004435 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004436 ns->prefix, BAD_CAST "xmlns");
4437 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004438 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004439 ns->prefix, BAD_CAST "xmlns");
4440 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004441 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004442 BAD_CAST "xmlns");
4443 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004444 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004445 BAD_CAST "xmlns");
4446 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004447 if ((fullname != fn) && (fullname != elem->name))
4448 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004449 }
4450 if (attrDecl == NULL) {
4451 if (ns->prefix != NULL) {
4452 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4453 ns->prefix, BAD_CAST "xmlns");
4454 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4455 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4456 ns->prefix, BAD_CAST "xmlns");
4457 } else {
4458 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4459 elem->name, BAD_CAST "xmlns");
4460 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4461 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4462 elem->name, BAD_CAST "xmlns");
4463 }
4464 }
4465
4466
4467 /* Validity Constraint: Attribute Value Type */
4468 if (attrDecl == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004469 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004470 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004471 "No declaration for attribute xmlns:%s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004472 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004473 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004474 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004475 "No declaration for attribute xmlns of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004476 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004477 }
4478 return(0);
4479 }
4480
4481 val = xmlValidateAttributeValue(attrDecl->atype, value);
4482 if (val == 0) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004483 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004484 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004485 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004486 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004487 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004488 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004489 "Syntax of value for attribute xmlns of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004490 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004491 }
4492 ret = 0;
4493 }
4494
4495 /* Validity constraint: Fixed Attribute Default */
4496 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4497 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004498 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004499 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004500 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4501 ns->prefix, elem->name, attrDecl->defaultValue);
4502 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004503 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004504 "Value for attribute xmlns of %s is different from default \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004505 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004506 }
4507 ret = 0;
4508 }
4509 }
4510
4511 /* Validity Constraint: ID uniqueness */
4512 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4513 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4514 ret = 0;
4515 }
4516
4517 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4518 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4519 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4520 ret = 0;
4521 }
4522
4523 /* Validity Constraint: Notation Attributes */
4524 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4525 xmlEnumerationPtr tree = attrDecl->tree;
4526 xmlNotationPtr nota;
4527
4528 /* First check that the given NOTATION was declared */
4529 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4530 if (nota == NULL)
4531 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4532
4533 if (nota == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004534 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004535 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004536 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4537 value, ns->prefix, elem->name);
4538 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004539 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004540 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004541 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004542 }
4543 ret = 0;
4544 }
4545
4546 /* Second, verify that it's among the list */
4547 while (tree != NULL) {
4548 if (xmlStrEqual(tree->name, value)) break;
4549 tree = tree->next;
4550 }
4551 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004552 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004553 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004554"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4555 value, ns->prefix, elem->name);
4556 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004557 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004558"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004559 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004560 }
4561 ret = 0;
4562 }
4563 }
4564
4565 /* Validity Constraint: Enumeration */
4566 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4567 xmlEnumerationPtr tree = attrDecl->tree;
4568 while (tree != NULL) {
4569 if (xmlStrEqual(tree->name, value)) break;
4570 tree = tree->next;
4571 }
4572 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004573 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004574 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004575"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4576 value, ns->prefix, elem->name);
4577 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004578 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004579"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004580 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004581 }
4582 ret = 0;
4583 }
4584 }
4585
4586 /* Fixed Attribute Default */
4587 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4588 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004589 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004590 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004591 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4592 ns->prefix, elem->name, attrDecl->defaultValue);
4593 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004594 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004595 "Value for attribute xmlns of %s must be \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004596 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004597 }
4598 ret = 0;
4599 }
4600
4601 /* Extra check for the attribute value */
4602 if (ns->prefix != NULL) {
4603 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4604 attrDecl->atype, value);
4605 } else {
4606 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4607 attrDecl->atype, value);
4608 }
4609
4610 return(ret);
4611}
4612
Daniel Veillard118aed72002-09-24 14:13:13 +00004613#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004614/**
4615 * xmlValidateSkipIgnorable:
4616 * @ctxt: the validation context
4617 * @child: the child list
4618 *
4619 * Skip ignorable elements w.r.t. the validation process
4620 *
4621 * returns the first element to consider for validation of the content model
4622 */
4623
4624static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004625xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004626 while (child != NULL) {
4627 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004628 /* These things are ignored (skipped) during validation. */
4629 case XML_PI_NODE:
4630 case XML_COMMENT_NODE:
4631 case XML_XINCLUDE_START:
4632 case XML_XINCLUDE_END:
4633 child = child->next;
4634 break;
4635 case XML_TEXT_NODE:
4636 if (xmlIsBlankNode(child))
4637 child = child->next;
4638 else
4639 return(child);
4640 break;
4641 /* keep current node */
4642 default:
4643 return(child);
4644 }
4645 }
4646 return(child);
4647}
4648
4649/**
4650 * xmlValidateElementType:
4651 * @ctxt: the validation context
4652 *
4653 * Try to validate the content model of an element internal function
4654 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004655 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4656 * reference is found and -3 if the validation succeeded but
4657 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004658 */
4659
4660static int
4661xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004662 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004663 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004664
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004665 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004666 if ((NODE == NULL) && (CONT == NULL))
4667 return(1);
4668 if ((NODE == NULL) &&
4669 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4670 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4671 return(1);
4672 }
4673 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004674 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004675 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004676
4677 /*
4678 * We arrive here when more states need to be examined
4679 */
4680cont:
4681
4682 /*
4683 * We just recovered from a rollback generated by a possible
4684 * epsilon transition, go directly to the analysis phase
4685 */
4686 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004687 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004688 DEBUG_VALID_STATE(NODE, CONT)
4689 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004690 goto analyze;
4691 }
4692
4693 DEBUG_VALID_STATE(NODE, CONT)
4694 /*
4695 * we may have to save a backup state here. This is the equivalent
4696 * of handling epsilon transition in NFAs.
4697 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004698 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004699 ((CONT->parent == NULL) ||
4700 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004701 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004702 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004703 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004704 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004705 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4706 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004707 }
4708
4709
4710 /*
4711 * Check first if the content matches
4712 */
4713 switch (CONT->type) {
4714 case XML_ELEMENT_CONTENT_PCDATA:
4715 if (NODE == NULL) {
4716 DEBUG_VALID_MSG("pcdata failed no node");
4717 ret = 0;
4718 break;
4719 }
4720 if (NODE->type == XML_TEXT_NODE) {
4721 DEBUG_VALID_MSG("pcdata found, skip to next");
4722 /*
4723 * go to next element in the content model
4724 * skipping ignorable elems
4725 */
4726 do {
4727 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004728 NODE = xmlValidateSkipIgnorable(NODE);
4729 if ((NODE != NULL) &&
4730 (NODE->type == XML_ENTITY_REF_NODE))
4731 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004732 } while ((NODE != NULL) &&
4733 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004734 (NODE->type != XML_TEXT_NODE) &&
4735 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004736 ret = 1;
4737 break;
4738 } else {
4739 DEBUG_VALID_MSG("pcdata failed");
4740 ret = 0;
4741 break;
4742 }
4743 break;
4744 case XML_ELEMENT_CONTENT_ELEMENT:
4745 if (NODE == NULL) {
4746 DEBUG_VALID_MSG("element failed no node");
4747 ret = 0;
4748 break;
4749 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004750 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4751 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004752 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004753 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4754 ret = (CONT->prefix == NULL);
4755 } else if (CONT->prefix == NULL) {
4756 ret = 0;
4757 } else {
4758 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4759 }
4760 }
4761 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004762 DEBUG_VALID_MSG("element found, skip to next");
4763 /*
4764 * go to next element in the content model
4765 * skipping ignorable elems
4766 */
4767 do {
4768 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004769 NODE = xmlValidateSkipIgnorable(NODE);
4770 if ((NODE != NULL) &&
4771 (NODE->type == XML_ENTITY_REF_NODE))
4772 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004773 } while ((NODE != NULL) &&
4774 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004775 (NODE->type != XML_TEXT_NODE) &&
4776 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004777 } else {
4778 DEBUG_VALID_MSG("element failed");
4779 ret = 0;
4780 break;
4781 }
4782 break;
4783 case XML_ELEMENT_CONTENT_OR:
4784 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004785 * Small optimization.
4786 */
4787 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4788 if ((NODE == NULL) ||
4789 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4790 DEPTH++;
4791 CONT = CONT->c2;
4792 goto cont;
4793 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004794 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4795 ret = (CONT->c1->prefix == NULL);
4796 } else if (CONT->c1->prefix == NULL) {
4797 ret = 0;
4798 } else {
4799 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4800 }
4801 if (ret == 0) {
4802 DEPTH++;
4803 CONT = CONT->c2;
4804 goto cont;
4805 }
Daniel Veillard85349052001-04-20 13:48:21 +00004806 }
4807
4808 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004809 * save the second branch 'or' branch
4810 */
4811 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004812 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4813 OCCURS, ROLLBACK_OR) < 0)
4814 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004815 DEPTH++;
4816 CONT = CONT->c1;
4817 goto cont;
4818 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004819 /*
4820 * Small optimization.
4821 */
4822 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4823 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4824 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4825 if ((NODE == NULL) ||
4826 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4827 DEPTH++;
4828 CONT = CONT->c2;
4829 goto cont;
4830 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004831 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4832 ret = (CONT->c1->prefix == NULL);
4833 } else if (CONT->c1->prefix == NULL) {
4834 ret = 0;
4835 } else {
4836 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4837 }
4838 if (ret == 0) {
4839 DEPTH++;
4840 CONT = CONT->c2;
4841 goto cont;
4842 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004843 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004844 DEPTH++;
4845 CONT = CONT->c1;
4846 goto cont;
4847 }
4848
4849 /*
4850 * At this point handle going up in the tree
4851 */
4852 if (ret == -1) {
4853 DEBUG_VALID_MSG("error found returning");
4854 return(ret);
4855 }
4856analyze:
4857 while (CONT != NULL) {
4858 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004859 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004860 * this level.
4861 */
4862 if (ret == 0) {
4863 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004864 xmlNodePtr cur;
4865
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004866 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004867 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004868 DEBUG_VALID_MSG("Once branch failed, rollback");
4869 if (vstateVPop(ctxt) < 0 ) {
4870 DEBUG_VALID_MSG("exhaustion, failed");
4871 return(0);
4872 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004873 if (cur != ctxt->vstate->node)
4874 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004875 goto cont;
4876 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004877 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004878 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004879 DEBUG_VALID_MSG("Plus branch failed, rollback");
4880 if (vstateVPop(ctxt) < 0 ) {
4881 DEBUG_VALID_MSG("exhaustion, failed");
4882 return(0);
4883 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004884 if (cur != ctxt->vstate->node)
4885 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004886 goto cont;
4887 }
4888 DEBUG_VALID_MSG("Plus branch found");
4889 ret = 1;
4890 break;
4891 case XML_ELEMENT_CONTENT_MULT:
4892#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004893 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004894 DEBUG_VALID_MSG("Mult branch failed");
4895 } else {
4896 DEBUG_VALID_MSG("Mult branch found");
4897 }
4898#endif
4899 ret = 1;
4900 break;
4901 case XML_ELEMENT_CONTENT_OPT:
4902 DEBUG_VALID_MSG("Option branch failed");
4903 ret = 1;
4904 break;
4905 }
4906 } else {
4907 switch (CONT->ocur) {
4908 case XML_ELEMENT_CONTENT_OPT:
4909 DEBUG_VALID_MSG("Option branch succeeded");
4910 ret = 1;
4911 break;
4912 case XML_ELEMENT_CONTENT_ONCE:
4913 DEBUG_VALID_MSG("Once branch succeeded");
4914 ret = 1;
4915 break;
4916 case XML_ELEMENT_CONTENT_PLUS:
4917 if (STATE == ROLLBACK_PARENT) {
4918 DEBUG_VALID_MSG("Plus branch rollback");
4919 ret = 1;
4920 break;
4921 }
4922 if (NODE == NULL) {
4923 DEBUG_VALID_MSG("Plus branch exhausted");
4924 ret = 1;
4925 break;
4926 }
4927 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004928 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004929 goto cont;
4930 case XML_ELEMENT_CONTENT_MULT:
4931 if (STATE == ROLLBACK_PARENT) {
4932 DEBUG_VALID_MSG("Mult branch rollback");
4933 ret = 1;
4934 break;
4935 }
4936 if (NODE == NULL) {
4937 DEBUG_VALID_MSG("Mult branch exhausted");
4938 ret = 1;
4939 break;
4940 }
4941 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004942 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004943 goto cont;
4944 }
4945 }
4946 STATE = 0;
4947
4948 /*
4949 * Then act accordingly at the parent level
4950 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004951 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004952 if (CONT->parent == NULL)
4953 break;
4954
4955 switch (CONT->parent->type) {
4956 case XML_ELEMENT_CONTENT_PCDATA:
4957 DEBUG_VALID_MSG("Error: parent pcdata");
4958 return(-1);
4959 case XML_ELEMENT_CONTENT_ELEMENT:
4960 DEBUG_VALID_MSG("Error: parent element");
4961 return(-1);
4962 case XML_ELEMENT_CONTENT_OR:
4963 if (ret == 1) {
4964 DEBUG_VALID_MSG("Or succeeded");
4965 CONT = CONT->parent;
4966 DEPTH--;
4967 } else {
4968 DEBUG_VALID_MSG("Or failed");
4969 CONT = CONT->parent;
4970 DEPTH--;
4971 }
4972 break;
4973 case XML_ELEMENT_CONTENT_SEQ:
4974 if (ret == 0) {
4975 DEBUG_VALID_MSG("Sequence failed");
4976 CONT = CONT->parent;
4977 DEPTH--;
4978 } else if (CONT == CONT->parent->c1) {
4979 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4980 CONT = CONT->parent->c2;
4981 goto cont;
4982 } else {
4983 DEBUG_VALID_MSG("Sequence succeeded");
4984 CONT = CONT->parent;
4985 DEPTH--;
4986 }
4987 }
4988 }
4989 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004990 xmlNodePtr cur;
4991
4992 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004993 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4994 if (vstateVPop(ctxt) < 0 ) {
4995 DEBUG_VALID_MSG("exhaustion, failed");
4996 return(0);
4997 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004998 if (cur != ctxt->vstate->node)
4999 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005000 goto cont;
5001 }
5002 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005003 xmlNodePtr cur;
5004
5005 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005006 DEBUG_VALID_MSG("Failure, rollback");
5007 if (vstateVPop(ctxt) < 0 ) {
5008 DEBUG_VALID_MSG("exhaustion, failed");
5009 return(0);
5010 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005011 if (cur != ctxt->vstate->node)
5012 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005013 goto cont;
5014 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005015 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005016}
Daniel Veillard23e73572002-09-19 19:56:43 +00005017#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005018
5019/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00005020 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005021 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00005022 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005023 * @content: An element
5024 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
5025 *
5026 * This will dump the list of elements to the buffer
5027 * Intended just for the debug routine
5028 */
5029static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00005030xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005031 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00005032 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005033
5034 if (node == NULL) return;
5035 if (glob) strcat(buf, "(");
5036 cur = node;
5037 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005038 len = strlen(buf);
5039 if (size - len < 50) {
5040 if ((size - len > 4) && (buf[len - 1] != '.'))
5041 strcat(buf, " ...");
5042 return;
5043 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005044 switch (cur->type) {
5045 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005046 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005047 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005048 if ((size - len > 4) && (buf[len - 1] != '.'))
5049 strcat(buf, " ...");
5050 return;
5051 }
5052 strcat(buf, (char *) cur->ns->prefix);
5053 strcat(buf, ":");
5054 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005055 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005056 if ((size - len > 4) && (buf[len - 1] != '.'))
5057 strcat(buf, " ...");
5058 return;
5059 }
5060 strcat(buf, (char *) cur->name);
5061 if (cur->next != NULL)
5062 strcat(buf, " ");
5063 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005064 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005065 if (xmlIsBlankNode(cur))
5066 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005067 case XML_CDATA_SECTION_NODE:
5068 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005069 strcat(buf, "CDATA");
5070 if (cur->next != NULL)
5071 strcat(buf, " ");
5072 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005073 case XML_ATTRIBUTE_NODE:
5074 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005075#ifdef LIBXML_DOCB_ENABLED
5076 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005077#endif
5078 case XML_HTML_DOCUMENT_NODE:
5079 case XML_DOCUMENT_TYPE_NODE:
5080 case XML_DOCUMENT_FRAG_NODE:
5081 case XML_NOTATION_NODE:
5082 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005083 strcat(buf, "???");
5084 if (cur->next != NULL)
5085 strcat(buf, " ");
5086 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005087 case XML_ENTITY_NODE:
5088 case XML_PI_NODE:
5089 case XML_DTD_NODE:
5090 case XML_COMMENT_NODE:
5091 case XML_ELEMENT_DECL:
5092 case XML_ATTRIBUTE_DECL:
5093 case XML_ENTITY_DECL:
5094 case XML_XINCLUDE_START:
5095 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005096 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005097 }
5098 cur = cur->next;
5099 }
5100 if (glob) strcat(buf, ")");
5101}
5102
5103/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005104 * xmlValidateElementContent:
5105 * @ctxt: the validation context
5106 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005107 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005108 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005109 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005110 *
5111 * Try to validate the content model of an element
5112 *
5113 * returns 1 if valid or 0 if not and -1 in case of error
5114 */
5115
5116static int
5117xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005118 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00005119 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00005120#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00005121 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00005122#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00005123 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005124 xmlElementContentPtr cont;
5125 const xmlChar *name;
5126
5127 if (elemDecl == NULL)
5128 return(-1);
5129 cont = elemDecl->content;
5130 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005131
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005132#ifdef LIBXML_REGEXP_ENABLED
5133 /* Build the regexp associated to the content model */
5134 if (elemDecl->contModel == NULL)
5135 ret = xmlValidBuildContentModel(ctxt, elemDecl);
5136 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005137 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005138 } else {
5139 xmlRegExecCtxtPtr exec;
5140
Daniel Veillardec498e12003-02-05 11:01:50 +00005141 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
5142 return(-1);
5143 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005144 ctxt->nodeMax = 0;
5145 ctxt->nodeNr = 0;
5146 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005147 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
5148 if (exec != NULL) {
5149 cur = child;
5150 while (cur != NULL) {
5151 switch (cur->type) {
5152 case XML_ENTITY_REF_NODE:
5153 /*
5154 * Push the current node to be able to roll back
5155 * and process within the entity
5156 */
5157 if ((cur->children != NULL) &&
5158 (cur->children->children != NULL)) {
5159 nodeVPush(ctxt, cur);
5160 cur = cur->children->children;
5161 continue;
5162 }
5163 break;
5164 case XML_TEXT_NODE:
5165 if (xmlIsBlankNode(cur))
5166 break;
5167 ret = 0;
5168 goto fail;
5169 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00005170 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005171 ret = 0;
5172 goto fail;
5173 case XML_ELEMENT_NODE:
5174 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005175 xmlChar fn[50];
5176 xmlChar *fullname;
5177
5178 fullname = xmlBuildQName(cur->name,
5179 cur->ns->prefix, fn, 50);
5180 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005181 ret = -1;
5182 goto fail;
5183 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005184 ret = xmlRegExecPushString(exec, fullname, NULL);
5185 if ((fullname != fn) && (fullname != cur->name))
5186 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005187 } else {
5188 ret = xmlRegExecPushString(exec, cur->name, NULL);
5189 }
5190 break;
5191 default:
5192 break;
5193 }
5194 /*
5195 * Switch to next element
5196 */
5197 cur = cur->next;
5198 while (cur == NULL) {
5199 cur = nodeVPop(ctxt);
5200 if (cur == NULL)
5201 break;
5202 cur = cur->next;
5203 }
5204 }
5205 ret = xmlRegExecPushString(exec, NULL, NULL);
5206fail:
5207 xmlRegFreeExecCtxt(exec);
5208 }
5209 }
5210#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005211 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005212 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005213 */
5214 ctxt->vstateMax = 8;
5215 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5216 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5217 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005218 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005219 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005220 }
5221 /*
5222 * The first entry in the stack is reserved to the current state
5223 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00005224 ctxt->nodeMax = 0;
5225 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00005226 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005227 ctxt->vstate = &ctxt->vstateTab[0];
5228 ctxt->vstateNr = 1;
5229 CONT = cont;
5230 NODE = child;
5231 DEPTH = 0;
5232 OCCURS = 0;
5233 STATE = 0;
5234 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005235 if ((ret == -3) && (warn)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005236 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
5237 "Content model for Element %s is ambiguous\n",
Daniel Veillard0cc72772003-10-13 14:00:21 +00005238 name, NULL, NULL);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005239 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005240 /*
5241 * An entities reference appeared at this level.
5242 * Buid a minimal representation of this node content
5243 * sufficient to run the validation process on it
5244 */
5245 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005246 cur = child;
5247 while (cur != NULL) {
5248 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005249 case XML_ENTITY_REF_NODE:
5250 /*
5251 * Push the current node to be able to roll back
5252 * and process within the entity
5253 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005254 if ((cur->children != NULL) &&
5255 (cur->children->children != NULL)) {
5256 nodeVPush(ctxt, cur);
5257 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005258 continue;
5259 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00005260 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005261 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005262 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005263 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005264 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005265 case XML_CDATA_SECTION_NODE:
5266 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005267 case XML_ELEMENT_NODE:
5268 /*
5269 * Allocate a new node and minimally fills in
5270 * what's required
5271 */
5272 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5273 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005274 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005275 xmlFreeNodeList(repl);
5276 ret = -1;
5277 goto done;
5278 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005279 tmp->type = cur->type;
5280 tmp->name = cur->name;
5281 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005282 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00005283 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005284 if (repl == NULL)
5285 repl = last = tmp;
5286 else {
5287 last->next = tmp;
5288 last = tmp;
5289 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005290 if (cur->type == XML_CDATA_SECTION_NODE) {
5291 /*
5292 * E59 spaces in CDATA does not match the
5293 * nonterminal S
5294 */
5295 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5296 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005297 break;
5298 default:
5299 break;
5300 }
5301 /*
5302 * Switch to next element
5303 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005304 cur = cur->next;
5305 while (cur == NULL) {
5306 cur = nodeVPop(ctxt);
5307 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005308 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005309 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005310 }
5311 }
5312
5313 /*
5314 * Relaunch the validation
5315 */
5316 ctxt->vstate = &ctxt->vstateTab[0];
5317 ctxt->vstateNr = 1;
5318 CONT = cont;
5319 NODE = repl;
5320 DEPTH = 0;
5321 OCCURS = 0;
5322 STATE = 0;
5323 ret = xmlValidateElementType(ctxt);
5324 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005325#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005326 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005327 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
5328 char expr[5000];
5329 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005330
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005331 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005332 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005333 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00005334#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005335 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005336 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005337 else
Daniel Veillard01992e02002-10-09 10:20:30 +00005338#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005339 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005340
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005341 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005342 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5343 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5344 name, BAD_CAST expr, BAD_CAST list);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005345 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005346 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5347 "Element content does not follow the DTD, expecting %s, got %s\n",
5348 BAD_CAST expr, BAD_CAST list, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005349 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005350 } else {
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,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005353 "Element %s content does not follow the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005354 name, NULL, NULL);
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\n",
5358 NULL, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005359 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005360 }
5361 ret = 0;
5362 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005363 if (ret == -3)
5364 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005365
Daniel Veillard23e73572002-09-19 19:56:43 +00005366#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005367done:
5368 /*
5369 * Deallocate the copy if done, and free up the validation stack
5370 */
5371 while (repl != NULL) {
5372 tmp = repl->next;
5373 xmlFree(repl);
5374 repl = tmp;
5375 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005376 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005377 if (ctxt->vstateTab != NULL) {
5378 xmlFree(ctxt->vstateTab);
5379 ctxt->vstateTab = NULL;
5380 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005381#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005382 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00005383 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005384 if (ctxt->nodeTab != NULL) {
5385 xmlFree(ctxt->nodeTab);
5386 ctxt->nodeTab = NULL;
5387 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005388 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005389
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005390}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005391
Owen Taylor3473f882001-02-23 17:55:21 +00005392/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005393 * xmlValidateCdataElement:
5394 * @ctxt: the validation context
5395 * @doc: a document instance
5396 * @elem: an element instance
5397 *
5398 * Check that an element follows #CDATA
5399 *
5400 * returns 1 if valid or 0 otherwise
5401 */
5402static int
5403xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5404 xmlNodePtr elem) {
5405 int ret = 1;
5406 xmlNodePtr cur, child;
5407
Daniel Veillardceb09b92002-10-04 11:46:37 +00005408 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005409 return(0);
5410
5411 child = elem->children;
5412
5413 cur = child;
5414 while (cur != NULL) {
5415 switch (cur->type) {
5416 case XML_ENTITY_REF_NODE:
5417 /*
5418 * Push the current node to be able to roll back
5419 * and process within the entity
5420 */
5421 if ((cur->children != NULL) &&
5422 (cur->children->children != NULL)) {
5423 nodeVPush(ctxt, cur);
5424 cur = cur->children->children;
5425 continue;
5426 }
5427 break;
5428 case XML_COMMENT_NODE:
5429 case XML_PI_NODE:
5430 case XML_TEXT_NODE:
5431 case XML_CDATA_SECTION_NODE:
5432 break;
5433 default:
5434 ret = 0;
5435 goto done;
5436 }
5437 /*
5438 * Switch to next element
5439 */
5440 cur = cur->next;
5441 while (cur == NULL) {
5442 cur = nodeVPop(ctxt);
5443 if (cur == NULL)
5444 break;
5445 cur = cur->next;
5446 }
5447 }
5448done:
5449 ctxt->nodeMax = 0;
5450 ctxt->nodeNr = 0;
5451 if (ctxt->nodeTab != NULL) {
5452 xmlFree(ctxt->nodeTab);
5453 ctxt->nodeTab = NULL;
5454 }
5455 return(ret);
5456}
5457
5458/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005459 * xmlValidateCheckMixed:
5460 * @ctxt: the validation context
5461 * @cont: the mixed content model
5462 * @qname: the qualified name as appearing in the serialization
5463 *
5464 * Check if the given node is part of the content model.
5465 *
5466 * Returns 1 if yes, 0 if no, -1 in case of error
5467 */
5468static int
William M. Brackedb65a72004-02-06 07:36:04 +00005469xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005470 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005471 const xmlChar *name;
5472 int plen;
5473 name = xmlSplitQName3(qname, &plen);
5474
5475 if (name == NULL) {
5476 while (cont != NULL) {
5477 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5478 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5479 return(1);
5480 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5481 (cont->c1 != NULL) &&
5482 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5483 if ((cont->c1->prefix == NULL) &&
5484 (xmlStrEqual(cont->c1->name, qname)))
5485 return(1);
5486 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5487 (cont->c1 == NULL) ||
5488 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005489 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5490 "Internal: MIXED struct corrupted\n",
5491 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005492 break;
5493 }
5494 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005495 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005496 } else {
5497 while (cont != NULL) {
5498 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5499 if ((cont->prefix != NULL) &&
5500 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5501 (xmlStrEqual(cont->name, name)))
5502 return(1);
5503 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5504 (cont->c1 != NULL) &&
5505 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5506 if ((cont->c1->prefix != NULL) &&
5507 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5508 (xmlStrEqual(cont->c1->name, name)))
5509 return(1);
5510 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5511 (cont->c1 == NULL) ||
5512 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005513 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5514 "Internal: MIXED struct corrupted\n",
5515 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005516 break;
5517 }
5518 cont = cont->c2;
5519 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005520 }
5521 return(0);
5522}
5523
5524/**
5525 * xmlValidGetElemDecl:
5526 * @ctxt: the validation context
5527 * @doc: a document instance
5528 * @elem: an element instance
5529 * @extsubset: pointer, (out) indicate if the declaration was found
5530 * in the external subset.
5531 *
5532 * Finds a declaration associated to an element in the document.
5533 *
5534 * returns the pointer to the declaration or NULL if not found.
5535 */
5536static xmlElementPtr
5537xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5538 xmlNodePtr elem, int *extsubset) {
5539 xmlElementPtr elemDecl = NULL;
5540 const xmlChar *prefix = NULL;
5541
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005542 if ((ctxt == NULL) || (doc == NULL) ||
5543 (elem == NULL) || (elem->name == NULL))
5544 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005545 if (extsubset != NULL)
5546 *extsubset = 0;
5547
5548 /*
5549 * Fetch the declaration for the qualified name
5550 */
5551 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5552 prefix = elem->ns->prefix;
5553
5554 if (prefix != NULL) {
5555 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5556 elem->name, prefix);
5557 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5558 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5559 elem->name, prefix);
5560 if ((elemDecl != NULL) && (extsubset != NULL))
5561 *extsubset = 1;
5562 }
5563 }
5564
5565 /*
5566 * Fetch the declaration for the non qualified name
5567 * This is "non-strict" validation should be done on the
5568 * full QName but in that case being flexible makes sense.
5569 */
5570 if (elemDecl == NULL) {
5571 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5572 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5573 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5574 if ((elemDecl != NULL) && (extsubset != NULL))
5575 *extsubset = 1;
5576 }
5577 }
5578 if (elemDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005579 xmlErrValidNode(ctxt, elem,
5580 XML_DTD_UNKNOWN_ELEM,
5581 "No declaration for element %s\n",
5582 elem->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005583 }
5584 return(elemDecl);
5585}
5586
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005587#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005588/**
5589 * xmlValidatePushElement:
5590 * @ctxt: the validation context
5591 * @doc: a document instance
5592 * @elem: an element instance
5593 * @qname: the qualified name as appearing in the serialization
5594 *
5595 * Push a new element start on the validation stack.
5596 *
5597 * returns 1 if no validation problem was found or 0 otherwise
5598 */
5599int
5600xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5601 xmlNodePtr elem, const xmlChar *qname) {
5602 int ret = 1;
5603 xmlElementPtr eDecl;
5604 int extsubset = 0;
5605
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005606 if (ctxt == NULL)
5607 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005608/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005609 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5610 xmlValidStatePtr state = ctxt->vstate;
5611 xmlElementPtr elemDecl;
5612
5613 /*
5614 * Check the new element agaisnt the content model of the new elem.
5615 */
5616 if (state->elemDecl != NULL) {
5617 elemDecl = state->elemDecl;
5618
5619 switch(elemDecl->etype) {
5620 case XML_ELEMENT_TYPE_UNDEFINED:
5621 ret = 0;
5622 break;
5623 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005624 xmlErrValidNode(ctxt, state->node,
5625 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005626 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005627 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005628 ret = 0;
5629 break;
5630 case XML_ELEMENT_TYPE_ANY:
5631 /* I don't think anything is required then */
5632 break;
5633 case XML_ELEMENT_TYPE_MIXED:
5634 /* simple case of declared as #PCDATA */
5635 if ((elemDecl->content != NULL) &&
5636 (elemDecl->content->type ==
5637 XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005638 xmlErrValidNode(ctxt, state->node,
5639 XML_DTD_NOT_PCDATA,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005640 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005641 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005642 ret = 0;
5643 } else {
5644 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5645 qname);
5646 if (ret != 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005647 xmlErrValidNode(ctxt, state->node,
5648 XML_DTD_INVALID_CHILD,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005649 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005650 qname, state->node->name, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005651 }
5652 }
5653 break;
5654 case XML_ELEMENT_TYPE_ELEMENT:
5655 /*
5656 * TODO:
5657 * VC: Standalone Document Declaration
5658 * - element types with element content, if white space
5659 * occurs directly within any instance of those types.
5660 */
5661 if (state->exec != NULL) {
5662 ret = xmlRegExecPushString(state->exec, qname, NULL);
5663 if (ret < 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005664 xmlErrValidNode(ctxt, state->node,
5665 XML_DTD_CONTENT_MODEL,
5666 "Element %s content does not follow the DTD, Misplaced %s\n",
5667 state->node->name, qname, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005668 ret = 0;
5669 } else {
5670 ret = 1;
5671 }
5672 }
5673 break;
5674 }
5675 }
5676 }
5677 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5678 vstateVPush(ctxt, eDecl, elem);
5679 return(ret);
5680}
5681
5682/**
5683 * xmlValidatePushCData:
5684 * @ctxt: the validation context
5685 * @data: some character data read
5686 * @len: the lenght of the data
5687 *
5688 * check the CData parsed for validation in the current stack
5689 *
5690 * returns 1 if no validation problem was found or 0 otherwise
5691 */
5692int
5693xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5694 int ret = 1;
5695
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005696/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005697 if (ctxt == NULL)
5698 return(0);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005699 if (len <= 0)
5700 return(ret);
5701 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5702 xmlValidStatePtr state = ctxt->vstate;
5703 xmlElementPtr elemDecl;
5704
5705 /*
5706 * Check the new element agaisnt the content model of the new elem.
5707 */
5708 if (state->elemDecl != NULL) {
5709 elemDecl = state->elemDecl;
5710
5711 switch(elemDecl->etype) {
5712 case XML_ELEMENT_TYPE_UNDEFINED:
5713 ret = 0;
5714 break;
5715 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005716 xmlErrValidNode(ctxt, state->node,
5717 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005718 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005719 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005720 ret = 0;
5721 break;
5722 case XML_ELEMENT_TYPE_ANY:
5723 break;
5724 case XML_ELEMENT_TYPE_MIXED:
5725 break;
5726 case XML_ELEMENT_TYPE_ELEMENT:
5727 if (len > 0) {
5728 int i;
5729
5730 for (i = 0;i < len;i++) {
William M. Brack76e95df2003-10-18 16:20:14 +00005731 if (!IS_BLANK_CH(data[i])) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005732 xmlErrValidNode(ctxt, state->node,
5733 XML_DTD_CONTENT_MODEL,
5734 "Element %s content does not follow the DTD, Text not allowed\n",
5735 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005736 ret = 0;
5737 goto done;
5738 }
5739 }
5740 /*
5741 * TODO:
5742 * VC: Standalone Document Declaration
5743 * element types with element content, if white space
5744 * occurs directly within any instance of those types.
5745 */
5746 }
5747 break;
5748 }
5749 }
5750 }
5751done:
5752 return(ret);
5753}
5754
5755/**
5756 * xmlValidatePopElement:
5757 * @ctxt: the validation context
5758 * @doc: a document instance
5759 * @elem: an element instance
5760 * @qname: the qualified name as appearing in the serialization
5761 *
5762 * Pop the element end from the validation stack.
5763 *
5764 * returns 1 if no validation problem was found or 0 otherwise
5765 */
5766int
5767xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005768 xmlNodePtr elem ATTRIBUTE_UNUSED,
5769 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005770 int ret = 1;
5771
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005772 if (ctxt == NULL)
5773 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005774/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005775 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5776 xmlValidStatePtr state = ctxt->vstate;
5777 xmlElementPtr elemDecl;
5778
5779 /*
5780 * Check the new element agaisnt the content model of the new elem.
5781 */
5782 if (state->elemDecl != NULL) {
5783 elemDecl = state->elemDecl;
5784
5785 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5786 if (state->exec != NULL) {
5787 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5788 if (ret == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005789 xmlErrValidNode(ctxt, state->node,
5790 XML_DTD_CONTENT_MODEL,
5791 "Element %s content does not follow the DTD, Expecting more child\n",
5792 state->node->name, NULL,NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005793 } else {
5794 /*
5795 * previous validation errors should not generate
5796 * a new one here
5797 */
5798 ret = 1;
5799 }
5800 }
5801 }
5802 }
5803 vstateVPop(ctxt);
5804 }
5805 return(ret);
5806}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005807#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005808
5809/**
Owen Taylor3473f882001-02-23 17:55:21 +00005810 * xmlValidateOneElement:
5811 * @ctxt: the validation context
5812 * @doc: a document instance
5813 * @elem: an element instance
5814 *
5815 * Try to validate a single element and it's attributes,
5816 * basically it does the following checks as described by the
5817 * XML-1.0 recommendation:
5818 * - [ VC: Element Valid ]
5819 * - [ VC: Required Attribute ]
5820 * Then call xmlValidateOneAttribute() for each attribute present.
5821 *
5822 * The ID/IDREF checkings are done separately
5823 *
5824 * returns 1 if valid or 0 otherwise
5825 */
5826
5827int
5828xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5829 xmlNodePtr elem) {
5830 xmlElementPtr elemDecl = NULL;
5831 xmlElementContentPtr cont;
5832 xmlAttributePtr attr;
5833 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005834 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005835 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005836 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005837
5838 CHECK_DTD;
5839
5840 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005841 switch (elem->type) {
5842 case XML_ATTRIBUTE_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005843 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5844 "Attribute element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005845 return(0);
5846 case XML_TEXT_NODE:
5847 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005848 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5849 "Text element has children !\n",
5850 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005851 return(0);
5852 }
5853 if (elem->properties != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005854 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5855 "Text element has attribute !\n",
5856 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005857 return(0);
5858 }
5859 if (elem->ns != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005860 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5861 "Text element has namespace !\n",
5862 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005863 return(0);
5864 }
5865 if (elem->nsDef != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005866 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5867 "Text element has namespace !\n",
5868 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005869 return(0);
5870 }
5871 if (elem->content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005872 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5873 "Text element has no content !\n",
5874 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005875 return(0);
5876 }
5877 return(1);
5878 case XML_XINCLUDE_START:
5879 case XML_XINCLUDE_END:
5880 return(1);
5881 case XML_CDATA_SECTION_NODE:
5882 case XML_ENTITY_REF_NODE:
5883 case XML_PI_NODE:
5884 case XML_COMMENT_NODE:
5885 return(1);
5886 case XML_ENTITY_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005887 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5888 "Entity element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005889 return(0);
5890 case XML_NOTATION_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005891 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5892 "Notation element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005893 return(0);
5894 case XML_DOCUMENT_NODE:
5895 case XML_DOCUMENT_TYPE_NODE:
5896 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005897 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5898 "Document element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005899 return(0);
5900 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005901 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5902 "HTML Document not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005903 return(0);
5904 case XML_ELEMENT_NODE:
5905 break;
5906 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005907 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5908 "unknown element type\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005909 return(0);
5910 }
Owen Taylor3473f882001-02-23 17:55:21 +00005911
5912 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005913 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005914 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005915 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5916 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005917 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005918
Daniel Veillardea7751d2002-12-20 00:16:24 +00005919 /*
5920 * If vstateNr is not zero that means continuous validation is
5921 * activated, do not try to check the content model at that level.
5922 */
5923 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005924 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005925 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005926 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005927 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
5928 "No declaration for element %s\n",
5929 elem->name, NULL, NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005930 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005931 case XML_ELEMENT_TYPE_EMPTY:
5932 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005933 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
Owen Taylor3473f882001-02-23 17:55:21 +00005934 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005935 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005936 ret = 0;
5937 }
5938 break;
5939 case XML_ELEMENT_TYPE_ANY:
5940 /* I don't think anything is required then */
5941 break;
5942 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005943
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005944 /* simple case of declared as #PCDATA */
5945 if ((elemDecl->content != NULL) &&
5946 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5947 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5948 if (!ret) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005949 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005950 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005951 elem->name, NULL, NULL);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005952 }
5953 break;
5954 }
Owen Taylor3473f882001-02-23 17:55:21 +00005955 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005956 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005957 while (child != NULL) {
5958 if (child->type == XML_ELEMENT_NODE) {
5959 name = child->name;
5960 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005961 xmlChar fn[50];
5962 xmlChar *fullname;
5963
5964 fullname = xmlBuildQName(child->name, child->ns->prefix,
5965 fn, 50);
5966 if (fullname == NULL)
5967 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005968 cont = elemDecl->content;
5969 while (cont != NULL) {
5970 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005971 if (xmlStrEqual(cont->name, fullname))
5972 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005973 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5974 (cont->c1 != NULL) &&
5975 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005976 if (xmlStrEqual(cont->c1->name, fullname))
5977 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005978 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5979 (cont->c1 == NULL) ||
5980 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005981 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5982 "Internal: MIXED struct corrupted\n",
5983 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005984 break;
5985 }
5986 cont = cont->c2;
5987 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005988 if ((fullname != fn) && (fullname != child->name))
5989 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005990 if (cont != NULL)
5991 goto child_ok;
5992 }
5993 cont = elemDecl->content;
5994 while (cont != NULL) {
5995 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5996 if (xmlStrEqual(cont->name, name)) break;
5997 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5998 (cont->c1 != NULL) &&
5999 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
6000 if (xmlStrEqual(cont->c1->name, name)) break;
6001 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6002 (cont->c1 == NULL) ||
6003 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006004 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
6005 "Internal: MIXED struct corrupted\n",
6006 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006007 break;
6008 }
6009 cont = cont->c2;
6010 }
6011 if (cont == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006012 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006013 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006014 name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006015 ret = 0;
6016 }
6017 }
6018child_ok:
6019 child = child->next;
6020 }
6021 break;
6022 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006023 if ((doc->standalone == 1) && (extsubset == 1)) {
6024 /*
6025 * VC: Standalone Document Declaration
6026 * - element types with element content, if white space
6027 * occurs directly within any instance of those types.
6028 */
6029 child = elem->children;
6030 while (child != NULL) {
6031 if (child->type == XML_TEXT_NODE) {
6032 const xmlChar *content = child->content;
6033
William M. Brack76e95df2003-10-18 16:20:14 +00006034 while (IS_BLANK_CH(*content))
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006035 content++;
6036 if (*content == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006037 xmlErrValidNode(ctxt, elem,
6038 XML_DTD_STANDALONE_WHITE_SPACE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006039"standalone: %s declared in the external subset contains white spaces nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006040 elem->name, NULL, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006041 ret = 0;
6042 break;
6043 }
6044 }
6045 child =child->next;
6046 }
6047 }
Owen Taylor3473f882001-02-23 17:55:21 +00006048 child = elem->children;
6049 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00006050 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006051 if (tmp <= 0)
6052 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00006053 break;
6054 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00006055 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00006056
6057 /* [ VC: Required Attribute ] */
6058 attr = elemDecl->attributes;
6059 while (attr != NULL) {
6060 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00006061 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006062
Daniel Veillarde4301c82002-02-13 13:32:35 +00006063 if ((attr->prefix == NULL) &&
6064 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6065 xmlNsPtr ns;
6066
6067 ns = elem->nsDef;
6068 while (ns != NULL) {
6069 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006070 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006071 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006072 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006073 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6074 xmlNsPtr ns;
6075
6076 ns = elem->nsDef;
6077 while (ns != NULL) {
6078 if (xmlStrEqual(attr->name, ns->prefix))
6079 goto found;
6080 ns = ns->next;
6081 }
6082 } else {
6083 xmlAttrPtr attrib;
6084
6085 attrib = elem->properties;
6086 while (attrib != NULL) {
6087 if (xmlStrEqual(attrib->name, attr->name)) {
6088 if (attr->prefix != NULL) {
6089 xmlNsPtr nameSpace = attrib->ns;
6090
6091 if (nameSpace == NULL)
6092 nameSpace = elem->ns;
6093 /*
6094 * qualified names handling is problematic, having a
6095 * different prefix should be possible but DTDs don't
6096 * allow to define the URI instead of the prefix :-(
6097 */
6098 if (nameSpace == NULL) {
6099 if (qualified < 0)
6100 qualified = 0;
6101 } else if (!xmlStrEqual(nameSpace->prefix,
6102 attr->prefix)) {
6103 if (qualified < 1)
6104 qualified = 1;
6105 } else
6106 goto found;
6107 } else {
6108 /*
6109 * We should allow applications to define namespaces
6110 * for their application even if the DTD doesn't
6111 * carry one, otherwise, basically we would always
6112 * break.
6113 */
6114 goto found;
6115 }
6116 }
6117 attrib = attrib->next;
6118 }
Owen Taylor3473f882001-02-23 17:55:21 +00006119 }
6120 if (qualified == -1) {
6121 if (attr->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006122 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006123 "Element %s does not carry attribute %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006124 elem->name, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006125 ret = 0;
6126 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006127 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006128 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00006129 elem->name, attr->prefix,attr->name);
6130 ret = 0;
6131 }
6132 } else if (qualified == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006133 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006134 "Element %s required attribute %s:%s has no prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006135 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006136 } else if (qualified == 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006137 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006138 "Element %s required attribute %s:%s has different prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006139 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006140 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006141 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
6142 /*
6143 * Special tests checking #FIXED namespace declarations
6144 * have the right value since this is not done as an
6145 * attribute checking
6146 */
6147 if ((attr->prefix == NULL) &&
6148 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6149 xmlNsPtr ns;
6150
6151 ns = elem->nsDef;
6152 while (ns != NULL) {
6153 if (ns->prefix == NULL) {
6154 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006155 xmlErrValidNode(ctxt, elem,
6156 XML_DTD_ELEM_DEFAULT_NAMESPACE,
Daniel Veillarde4301c82002-02-13 13:32:35 +00006157 "Element %s namespace name for default namespace does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006158 elem->name, NULL, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006159 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006160 }
6161 goto found;
6162 }
6163 ns = ns->next;
6164 }
6165 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6166 xmlNsPtr ns;
6167
6168 ns = elem->nsDef;
6169 while (ns != NULL) {
6170 if (xmlStrEqual(attr->name, ns->prefix)) {
6171 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006172 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006173 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006174 elem->name, ns->prefix, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006175 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006176 }
6177 goto found;
6178 }
6179 ns = ns->next;
6180 }
6181 }
Owen Taylor3473f882001-02-23 17:55:21 +00006182 }
6183found:
6184 attr = attr->nexth;
6185 }
6186 return(ret);
6187}
6188
6189/**
6190 * xmlValidateRoot:
6191 * @ctxt: the validation context
6192 * @doc: a document instance
6193 *
6194 * Try to validate a the root element
6195 * basically it does the following check as described by the
6196 * XML-1.0 recommendation:
6197 * - [ VC: Root Element Type ]
6198 * it doesn't try to recurse or apply other check to the element
6199 *
6200 * returns 1 if valid or 0 otherwise
6201 */
6202
6203int
6204xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6205 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00006206 int ret;
6207
Owen Taylor3473f882001-02-23 17:55:21 +00006208 if (doc == NULL) return(0);
6209
6210 root = xmlDocGetRootElement(doc);
6211 if ((root == NULL) || (root->name == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006212 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
6213 "no root element\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006214 return(0);
6215 }
6216
6217 /*
6218 * When doing post validation against a separate DTD, those may
6219 * no internal subset has been generated
6220 */
6221 if ((doc->intSubset != NULL) &&
6222 (doc->intSubset->name != NULL)) {
6223 /*
6224 * Check first the document root against the NQName
6225 */
6226 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
6227 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006228 xmlChar fn[50];
6229 xmlChar *fullname;
6230
6231 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6232 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006233 xmlVErrMemory(ctxt, NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00006234 return(0);
6235 }
6236 ret = xmlStrEqual(doc->intSubset->name, fullname);
6237 if ((fullname != fn) && (fullname != root->name))
6238 xmlFree(fullname);
6239 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00006240 goto name_ok;
6241 }
6242 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6243 (xmlStrEqual(root->name, BAD_CAST "html")))
6244 goto name_ok;
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006245 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6246 "root and DTD name do not match '%s' and '%s'\n",
6247 root->name, doc->intSubset->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006248 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006249 }
6250 }
6251name_ok:
6252 return(1);
6253}
6254
6255
6256/**
6257 * xmlValidateElement:
6258 * @ctxt: the validation context
6259 * @doc: a document instance
6260 * @elem: an element instance
6261 *
6262 * Try to validate the subtree under an element
6263 *
6264 * returns 1 if valid or 0 otherwise
6265 */
6266
6267int
6268xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
6269 xmlNodePtr child;
6270 xmlAttrPtr attr;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006271 xmlNsPtr ns;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006272 const xmlChar *value;
Owen Taylor3473f882001-02-23 17:55:21 +00006273 int ret = 1;
6274
6275 if (elem == NULL) return(0);
6276
6277 /*
6278 * XInclude elements were added after parsing in the infoset,
6279 * they don't really mean anything validation wise.
6280 */
6281 if ((elem->type == XML_XINCLUDE_START) ||
6282 (elem->type == XML_XINCLUDE_END))
6283 return(1);
6284
6285 CHECK_DTD;
6286
Daniel Veillard10ea86c2001-06-20 13:55:33 +00006287 /*
6288 * Entities references have to be handled separately
6289 */
6290 if (elem->type == XML_ENTITY_REF_NODE) {
6291 return(1);
6292 }
6293
Owen Taylor3473f882001-02-23 17:55:21 +00006294 ret &= xmlValidateOneElement(ctxt, doc, elem);
6295 attr = elem->properties;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006296 while (attr != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006297 value = xmlNodeListGetString(doc, attr->children, 0);
6298 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6299 if (value != NULL)
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006300 xmlFree((char *)value);
Owen Taylor3473f882001-02-23 17:55:21 +00006301 attr= attr->next;
6302 }
Daniel Veillarde133dd82003-10-30 10:42:20 +00006303 ns = elem->nsDef;
6304 while (ns != NULL) {
Daniel Veillard1f5c9892003-12-29 17:09:55 +00006305 if (elem->ns == NULL)
6306 ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
6307 ns, ns->href);
6308 else
6309 ret &= xmlValidateOneNamespace(ctxt, doc, elem, elem->ns->prefix,
6310 ns, ns->href);
Daniel Veillarde133dd82003-10-30 10:42:20 +00006311 ns = ns->next;
6312 }
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 */
6804static void xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
6805 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"