blob: 1e03a7c7253663858a3d763b3333dcd75c7e9fa5 [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 Veillardf8e3db02012-09-11 13:26:36 +080034#define TODO \
Daniel Veillarda646cfd2002-09-17 21:50:03 +000035 xmlGenericError(xmlGenericErrorContext, \
36 "Unimplemented block at %s:%d\n", \
37 __FILE__, __LINE__);
Owen Taylor3473f882001-02-23 17:55:21 +000038
Daniel Veillardae0765b2008-07-31 19:54:59 +000039#ifdef LIBXML_VALID_ENABLED
40static int
41xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
42 const xmlChar *value);
43#endif
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000044/************************************************************************
45 * *
46 * Error handling routines *
47 * *
48 ************************************************************************/
49
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000050/**
Daniel Veillardce9457f2003-10-05 21:33:18 +000051 * xmlVErrMemory:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000052 * @ctxt: an XML validation parser context
53 * @extra: extra informations
54 *
55 * Handle an out of memory error
56 */
57static void
Daniel Veillardce9457f2003-10-05 21:33:18 +000058xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000059{
Daniel Veillardbb5abab2003-10-03 22:21:51 +000060 xmlGenericErrorFunc channel = NULL;
61 xmlParserCtxtPtr pctxt = NULL;
62 void *data = NULL;
63
64 if (ctxt != NULL) {
65 channel = ctxt->error;
66 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +000067 /* Use the special values to detect if it is part of a parsing
68 context */
69 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
70 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
Daniel Veillardb2dc5672006-08-22 14:45:40 +000071 long delta = (char *) ctxt - (char *) ctxt->userData;
72 if ((delta > 0) && (delta < 250))
73 pctxt = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +000074 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +000075 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000076 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +000077 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000078 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000079 XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
80 "Memory allocation failed : %s\n", extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000081 else
Daniel Veillard73000572003-10-11 11:26:42 +000082 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +000083 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000084 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
85 "Memory allocation failed\n");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000086}
87
88/**
89 * xmlErrValid:
90 * @ctxt: an XML validation parser context
Daniel Veillardbb5abab2003-10-03 22:21:51 +000091 * @error: the error number
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000092 * @extra: extra informations
93 *
94 * Handle a validation error
95 */
96static void
Daniel Veillardf88d8cf2003-12-08 10:25:02 +000097xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +000098 const char *msg, const char *extra)
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000099{
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000100 xmlGenericErrorFunc channel = NULL;
101 xmlParserCtxtPtr pctxt = NULL;
102 void *data = NULL;
103
104 if (ctxt != NULL) {
105 channel = ctxt->error;
106 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000107 /* Use the special values to detect if it is part of a parsing
108 context */
109 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
110 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
Daniel Veillardb2dc5672006-08-22 14:45:40 +0000111 long delta = (char *) ctxt - (char *) ctxt->userData;
112 if ((delta > 0) && (delta < 250))
113 pctxt = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000114 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000115 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000116 if (extra)
Daniel Veillard73000572003-10-11 11:26:42 +0000117 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +0000118 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000119 XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
120 msg, extra);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000121 else
Daniel Veillard73000572003-10-11 11:26:42 +0000122 __xmlRaiseError(NULL, channel, data,
Daniel Veillard72b9e292003-10-28 15:44:17 +0000123 pctxt, NULL, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000124 XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200125 "%s", msg);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000126}
127
Daniel Veillardf54cd532004-02-25 11:52:31 +0000128#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
129/**
130 * xmlErrValidNode:
131 * @ctxt: an XML validation parser context
132 * @node: the node raising the error
133 * @error: the error number
134 * @str1: extra informations
135 * @str2: extra informations
136 * @str3: extra informations
137 *
138 * Handle a validation error, provide contextual informations
139 */
140static void
141xmlErrValidNode(xmlValidCtxtPtr ctxt,
142 xmlNodePtr node, xmlParserErrors error,
143 const char *msg, const xmlChar * str1,
144 const xmlChar * str2, const xmlChar * str3)
145{
146 xmlStructuredErrorFunc schannel = NULL;
147 xmlGenericErrorFunc channel = NULL;
148 xmlParserCtxtPtr pctxt = NULL;
149 void *data = NULL;
150
151 if (ctxt != NULL) {
152 channel = ctxt->error;
153 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000154 /* Use the special values to detect if it is part of a parsing
155 context */
156 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
157 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
Daniel Veillardb2dc5672006-08-22 14:45:40 +0000158 long delta = (char *) ctxt - (char *) ctxt->userData;
159 if ((delta > 0) && (delta < 250))
160 pctxt = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000161 }
Daniel Veillardf54cd532004-02-25 11:52:31 +0000162 }
163 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
164 XML_ERR_ERROR, NULL, 0,
165 (const char *) str1,
166 (const char *) str1,
167 (const char *) str3, 0, 0, msg, str1, str2, str3);
168}
169#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
170
Daniel Veillardd61e8fb2003-10-19 21:59:17 +0000171#ifdef LIBXML_VALID_ENABLED
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000172/**
173 * xmlErrValidNodeNr:
174 * @ctxt: an XML validation parser context
175 * @node: the node raising the error
176 * @error: the error number
177 * @str1: extra informations
178 * @int2: extra informations
179 * @str3: extra informations
180 *
181 * Handle a validation error, provide contextual informations
182 */
183static void
Daniel Veillard72b9e292003-10-28 15:44:17 +0000184xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000185 xmlNodePtr node, xmlParserErrors error,
186 const char *msg, const xmlChar * str1,
187 int int2, const xmlChar * str3)
188{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000189 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000190 xmlGenericErrorFunc channel = NULL;
191 xmlParserCtxtPtr pctxt = NULL;
192 void *data = NULL;
193
194 if (ctxt != NULL) {
195 channel = ctxt->error;
196 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000197 /* Use the special values to detect if it is part of a parsing
198 context */
199 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
200 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
Daniel Veillardb2dc5672006-08-22 14:45:40 +0000201 long delta = (char *) ctxt - (char *) ctxt->userData;
202 if ((delta > 0) && (delta < 250))
203 pctxt = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000204 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000205 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000206 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000207 XML_ERR_ERROR, NULL, 0,
208 (const char *) str1,
209 (const char *) str3,
210 NULL, int2, 0, msg, str1, int2, str3);
211}
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000212
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000213/**
214 * xmlErrValidWarning:
215 * @ctxt: an XML validation parser context
216 * @node: the node raising the error
217 * @error: the error number
William M. Brackedb65a72004-02-06 07:36:04 +0000218 * @str1: extra information
219 * @str2: extra information
220 * @str3: extra information
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000221 *
William M. Brackedb65a72004-02-06 07:36:04 +0000222 * Handle a validation error, provide contextual information
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000223 */
224static void
William M. Brackedb65a72004-02-06 07:36:04 +0000225xmlErrValidWarning(xmlValidCtxtPtr ctxt,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000226 xmlNodePtr node, xmlParserErrors error,
227 const char *msg, const xmlChar * str1,
228 const xmlChar * str2, const xmlChar * str3)
229{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000230 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000231 xmlGenericErrorFunc channel = NULL;
232 xmlParserCtxtPtr pctxt = NULL;
233 void *data = NULL;
234
235 if (ctxt != NULL) {
William M. Bracke4d526f2004-12-18 00:01:21 +0000236 channel = ctxt->warning;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000237 data = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000238 /* Use the special values to detect if it is part of a parsing
239 context */
240 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
241 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
Daniel Veillardb2dc5672006-08-22 14:45:40 +0000242 long delta = (char *) ctxt - (char *) ctxt->userData;
243 if ((delta > 0) && (delta < 250))
244 pctxt = ctxt->userData;
Daniel Veillardeff45a92004-10-29 12:10:55 +0000245 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000246 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000247 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000248 XML_ERR_WARNING, NULL, 0,
249 (const char *) str1,
250 (const char *) str1,
251 (const char *) str3, 0, 0, msg, str1, str2, str3);
252}
253
254
Daniel Veillardea7751d2002-12-20 00:16:24 +0000255
256#ifdef LIBXML_REGEXP_ENABLED
257/*
258 * If regexp are enabled we can do continuous validation without the
259 * need of a tree to validate the content model. this is done in each
260 * callbacks.
261 * Each xmlValidState represent the validation state associated to the
262 * set of nodes currently open from the document root to the current element.
263 */
264
265
266typedef struct _xmlValidState {
267 xmlElementPtr elemDecl; /* pointer to the content model */
268 xmlNodePtr node; /* pointer to the current node */
269 xmlRegExecCtxtPtr exec; /* regexp runtime */
270} _xmlValidState;
271
272
273static int
274vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000275 if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000276 ctxt->vstateMax = 10;
277 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
278 sizeof(ctxt->vstateTab[0]));
279 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000280 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000281 return(-1);
282 }
283 }
284
285 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000286 xmlValidState *tmp;
287
288 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
289 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
290 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000291 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillardea7751d2002-12-20 00:16:24 +0000292 return(-1);
293 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000294 ctxt->vstateMax *= 2;
295 ctxt->vstateTab = tmp;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000296 }
297 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
298 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
299 ctxt->vstateTab[ctxt->vstateNr].node = node;
300 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
301 if (elemDecl->contModel == NULL)
302 xmlValidBuildContentModel(ctxt, elemDecl);
303 if (elemDecl->contModel != NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800304 ctxt->vstateTab[ctxt->vstateNr].exec =
Daniel Veillardea7751d2002-12-20 00:16:24 +0000305 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
306 } else {
307 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000308 xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
309 XML_ERR_INTERNAL_ERROR,
310 "Failed to build content model regexp for %s\n",
311 node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000312 }
313 }
314 return(ctxt->vstateNr++);
315}
316
317static int
318vstateVPop(xmlValidCtxtPtr ctxt) {
319 xmlElementPtr elemDecl;
320
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000321 if (ctxt->vstateNr < 1) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000322 ctxt->vstateNr--;
323 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
324 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
325 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
326 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
327 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
328 }
329 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
330 if (ctxt->vstateNr >= 1)
331 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
332 else
333 ctxt->vstate = NULL;
334 return(ctxt->vstateNr);
335}
336
337#else /* not LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000338/*
Daniel Veillard1c732d22002-11-30 11:22:59 +0000339 * If regexp are not enabled, it uses a home made algorithm less
340 * complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000341 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000342 * only restriction is on the deepness of the tree limited by the
343 * size of the occurs bitfield
344 *
345 * this is the content of a saved state for rollbacks
346 */
347
348#define ROLLBACK_OR 0
349#define ROLLBACK_PARENT 1
350
Daniel Veillardb44025c2001-10-11 22:55:55 +0000351typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000352 xmlElementContentPtr cont; /* pointer to the content model subtree */
353 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000354 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000355 unsigned char depth; /* current depth in the overall tree */
356 unsigned char state; /* ROLLBACK_XXX */
357} _xmlValidState;
358
Daniel Veillardfc57b412002-04-29 15:50:14 +0000359#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000360#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
361#define CONT ctxt->vstate->cont
362#define NODE ctxt->vstate->node
363#define DEPTH ctxt->vstate->depth
364#define OCCURS ctxt->vstate->occurs
365#define STATE ctxt->vstate->state
366
Daniel Veillard5344c602001-12-31 16:37:34 +0000367#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
368#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000369
Daniel Veillard5344c602001-12-31 16:37:34 +0000370#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
371#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000372
373static int
374vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
375 xmlNodePtr node, unsigned char depth, long occurs,
376 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000377 int i = ctxt->vstateNr - 1;
378
Daniel Veillard940492d2002-04-15 10:15:25 +0000379 if (ctxt->vstateNr > MAX_RECURSE) {
380 return(-1);
381 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000382 if (ctxt->vstateTab == NULL) {
383 ctxt->vstateMax = 8;
384 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
385 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
386 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000387 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000388 return(-1);
389 }
390 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000391 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000392 xmlValidState *tmp;
393
394 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
395 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
396 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000397 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard940492d2002-04-15 10:15:25 +0000398 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000399 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000400 ctxt->vstateMax *= 2;
401 ctxt->vstateTab = tmp;
Daniel Veillard06803992001-04-22 10:35:56 +0000402 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000403 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000404 /*
405 * Don't push on the stack a state already here
406 */
407 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
408 (ctxt->vstateTab[i].node == node) &&
409 (ctxt->vstateTab[i].depth == depth) &&
410 (ctxt->vstateTab[i].occurs == occurs) &&
411 (ctxt->vstateTab[i].state == state))
412 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000413 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
414 ctxt->vstateTab[ctxt->vstateNr].node = node;
415 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
416 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
417 ctxt->vstateTab[ctxt->vstateNr].state = state;
418 return(ctxt->vstateNr++);
419}
420
421static int
422vstateVPop(xmlValidCtxtPtr ctxt) {
423 if (ctxt->vstateNr <= 1) return(-1);
424 ctxt->vstateNr--;
425 ctxt->vstate = &ctxt->vstateTab[0];
426 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
427 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
428 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
429 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
430 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
431 return(ctxt->vstateNr);
432}
433
Daniel Veillard118aed72002-09-24 14:13:13 +0000434#endif /* LIBXML_REGEXP_ENABLED */
435
Daniel Veillard1c732d22002-11-30 11:22:59 +0000436static int
437nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
438{
439 if (ctxt->nodeMax <= 0) {
440 ctxt->nodeMax = 4;
441 ctxt->nodeTab =
442 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
443 sizeof(ctxt->nodeTab[0]));
444 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000445 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000446 ctxt->nodeMax = 0;
447 return (0);
448 }
449 }
450 if (ctxt->nodeNr >= ctxt->nodeMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000451 xmlNodePtr *tmp;
452 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
453 ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
454 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000455 xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillard1c732d22002-11-30 11:22:59 +0000456 return (0);
457 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000458 ctxt->nodeMax *= 2;
459 ctxt->nodeTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000460 }
461 ctxt->nodeTab[ctxt->nodeNr] = value;
462 ctxt->node = value;
463 return (ctxt->nodeNr++);
464}
465static xmlNodePtr
466nodeVPop(xmlValidCtxtPtr ctxt)
467{
468 xmlNodePtr ret;
469
470 if (ctxt->nodeNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +0000471 return (NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000472 ctxt->nodeNr--;
473 if (ctxt->nodeNr > 0)
474 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
475 else
476 ctxt->node = NULL;
477 ret = ctxt->nodeTab[ctxt->nodeNr];
Daniel Veillard24505b02005-07-28 23:49:35 +0000478 ctxt->nodeTab[ctxt->nodeNr] = NULL;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000479 return (ret);
480}
Owen Taylor3473f882001-02-23 17:55:21 +0000481
Owen Taylor3473f882001-02-23 17:55:21 +0000482#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000483static void
484xmlValidPrintNode(xmlNodePtr cur) {
485 if (cur == NULL) {
486 xmlGenericError(xmlGenericErrorContext, "null");
487 return;
488 }
489 switch (cur->type) {
490 case XML_ELEMENT_NODE:
491 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
492 break;
493 case XML_TEXT_NODE:
494 xmlGenericError(xmlGenericErrorContext, "text ");
495 break;
496 case XML_CDATA_SECTION_NODE:
497 xmlGenericError(xmlGenericErrorContext, "cdata ");
498 break;
499 case XML_ENTITY_REF_NODE:
500 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
501 break;
502 case XML_PI_NODE:
503 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
504 break;
505 case XML_COMMENT_NODE:
506 xmlGenericError(xmlGenericErrorContext, "comment ");
507 break;
508 case XML_ATTRIBUTE_NODE:
509 xmlGenericError(xmlGenericErrorContext, "?attr? ");
510 break;
511 case XML_ENTITY_NODE:
512 xmlGenericError(xmlGenericErrorContext, "?ent? ");
513 break;
514 case XML_DOCUMENT_NODE:
515 xmlGenericError(xmlGenericErrorContext, "?doc? ");
516 break;
517 case XML_DOCUMENT_TYPE_NODE:
518 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
519 break;
520 case XML_DOCUMENT_FRAG_NODE:
521 xmlGenericError(xmlGenericErrorContext, "?frag? ");
522 break;
523 case XML_NOTATION_NODE:
524 xmlGenericError(xmlGenericErrorContext, "?nota? ");
525 break;
526 case XML_HTML_DOCUMENT_NODE:
527 xmlGenericError(xmlGenericErrorContext, "?html? ");
528 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000529#ifdef LIBXML_DOCB_ENABLED
530 case XML_DOCB_DOCUMENT_NODE:
531 xmlGenericError(xmlGenericErrorContext, "?docb? ");
532 break;
533#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000534 case XML_DTD_NODE:
535 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
536 break;
537 case XML_ELEMENT_DECL:
538 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
539 break;
540 case XML_ATTRIBUTE_DECL:
541 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
542 break;
543 case XML_ENTITY_DECL:
544 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
545 break;
546 case XML_NAMESPACE_DECL:
547 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
548 break;
549 case XML_XINCLUDE_START:
550 xmlGenericError(xmlGenericErrorContext, "incstart ");
551 break;
552 case XML_XINCLUDE_END:
553 xmlGenericError(xmlGenericErrorContext, "incend ");
554 break;
555 }
556}
557
558static void
559xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000560 if (cur == NULL)
561 xmlGenericError(xmlGenericErrorContext, "null ");
562 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000563 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000564 cur = cur->next;
565 }
566}
567
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000568static void
569xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Daniel Veillard83391282003-03-06 21:37:30 +0000570 char expr[5000];
Owen Taylor3473f882001-02-23 17:55:21 +0000571
572 expr[0] = 0;
573 xmlGenericError(xmlGenericErrorContext, "valid: ");
574 xmlValidPrintNodeList(cur);
575 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000576 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000577 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
578}
579
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000580static void
581xmlValidDebugState(xmlValidStatePtr state) {
582 xmlGenericError(xmlGenericErrorContext, "(");
583 if (state->cont == NULL)
584 xmlGenericError(xmlGenericErrorContext, "null,");
585 else
586 switch (state->cont->type) {
587 case XML_ELEMENT_CONTENT_PCDATA:
588 xmlGenericError(xmlGenericErrorContext, "pcdata,");
589 break;
590 case XML_ELEMENT_CONTENT_ELEMENT:
591 xmlGenericError(xmlGenericErrorContext, "%s,",
592 state->cont->name);
593 break;
594 case XML_ELEMENT_CONTENT_SEQ:
595 xmlGenericError(xmlGenericErrorContext, "seq,");
596 break;
597 case XML_ELEMENT_CONTENT_OR:
598 xmlGenericError(xmlGenericErrorContext, "or,");
599 break;
600 }
601 xmlValidPrintNode(state->node);
602 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
603 state->depth, state->occurs, state->state);
604}
605
606static void
607xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
608 int i, j;
609
610 xmlGenericError(xmlGenericErrorContext, "state: ");
611 xmlValidDebugState(ctxt->vstate);
612 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
613 ctxt->vstateNr - 1);
614 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
615 xmlValidDebugState(&ctxt->vstateTab[j]);
616 xmlGenericError(xmlGenericErrorContext, "\n");
617}
618
619/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000620#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000621 *****/
622
623#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000624#define DEBUG_VALID_MSG(m) \
625 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800626
Owen Taylor3473f882001-02-23 17:55:21 +0000627#else
628#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000629#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000630#endif
631
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000632/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000633
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000634
Owen Taylor3473f882001-02-23 17:55:21 +0000635#define CHECK_DTD \
636 if (doc == NULL) return(0); \
637 else if ((doc->intSubset == NULL) && \
638 (doc->extSubset == NULL)) return(0)
639
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000640#ifdef LIBXML_REGEXP_ENABLED
641
642/************************************************************************
643 * *
644 * Content model validation based on the regexps *
645 * *
646 ************************************************************************/
647
648/**
649 * xmlValidBuildAContentModel:
650 * @content: the content model
651 * @ctxt: the schema parser context
652 * @name: the element name whose content is being built
653 *
654 * Generate the automata sequence needed for that type
655 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000656 * Returns 1 if successful or 0 in case of error.
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000657 */
658static int
659xmlValidBuildAContentModel(xmlElementContentPtr content,
660 xmlValidCtxtPtr ctxt,
661 const xmlChar *name) {
662 if (content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000663 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
664 "Found NULL content in content model of %s\n",
665 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000666 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000667 }
668 switch (content->type) {
669 case XML_ELEMENT_CONTENT_PCDATA:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000670 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
671 "Found PCDATA in content model of %s\n",
672 name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000673 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000674 break;
675 case XML_ELEMENT_CONTENT_ELEMENT: {
676 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillardc00cda82003-04-07 10:22:39 +0000677 xmlChar fn[50];
678 xmlChar *fullname;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800679
Daniel Veillardc00cda82003-04-07 10:22:39 +0000680 fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
681 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000682 xmlVErrMemory(ctxt, "Building content model");
Daniel Veillardc00cda82003-04-07 10:22:39 +0000683 return(0);
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000684 }
685
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000686 switch (content->ocur) {
687 case XML_ELEMENT_CONTENT_ONCE:
688 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000689 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000690 break;
691 case XML_ELEMENT_CONTENT_OPT:
692 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000693 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000694 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
695 break;
696 case XML_ELEMENT_CONTENT_PLUS:
697 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000698 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000699 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000700 ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000701 break;
702 case XML_ELEMENT_CONTENT_MULT:
William M. Brack8b0cbb02004-04-17 13:31:06 +0000703 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800704 ctxt->state, NULL);
William M. Brack8b0cbb02004-04-17 13:31:06 +0000705 xmlAutomataNewTransition(ctxt->am,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800706 ctxt->state, ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000707 break;
708 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000709 if ((fullname != fn) && (fullname != content->name))
710 xmlFree(fullname);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000711 break;
712 }
713 case XML_ELEMENT_CONTENT_SEQ: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000714 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000715 xmlElementContentOccur ocur;
716
717 /*
718 * Simply iterate over the content
719 */
720 oldstate = ctxt->state;
721 ocur = content->ocur;
Daniel Veillardf4be0182003-02-24 19:54:33 +0000722 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
723 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
724 oldstate = ctxt->state;
725 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000726 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000727 xmlValidBuildAContentModel(content->c1, ctxt, name);
728 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000729 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
730 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
731 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000732 oldend = ctxt->state;
733 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000734 switch (ocur) {
735 case XML_ELEMENT_CONTENT_ONCE:
736 break;
737 case XML_ELEMENT_CONTENT_OPT:
738 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
739 break;
740 case XML_ELEMENT_CONTENT_MULT:
741 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000742 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000743 break;
744 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000745 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000746 break;
747 }
748 break;
749 }
750 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000751 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000752 xmlElementContentOccur ocur;
753
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000754 ocur = content->ocur;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800755 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000756 (ocur == XML_ELEMENT_CONTENT_MULT)) {
757 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
758 ctxt->state, NULL);
759 }
760 oldstate = ctxt->state;
761 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000762
763 /*
764 * iterate over the subtypes and remerge the end with an
765 * epsilon transition
766 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000767 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000768 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000769 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000770 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000771 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000772 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
773 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000774 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000775 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000776 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
777 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000778 switch (ocur) {
779 case XML_ELEMENT_CONTENT_ONCE:
780 break;
781 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000782 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000783 break;
784 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000785 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
786 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000787 break;
788 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000789 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000790 break;
791 }
792 break;
793 }
794 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000795 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
796 "ContentModel broken for element %s\n",
797 (const char *) name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000798 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000799 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000800 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000801}
802/**
803 * xmlValidBuildContentModel:
804 * @ctxt: a validation context
805 * @elem: an element declaration node
806 *
807 * (Re)Build the automata associated to the content model of this
808 * element
809 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000810 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000811 */
812int
813xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000814
815 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000816 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000817 if (elem->type != XML_ELEMENT_DECL)
818 return(0);
819 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
820 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000821 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000822 if (elem->contModel != NULL) {
823 if (!xmlRegexpIsDeterminist(elem->contModel)) {
824 ctxt->valid = 0;
825 return(0);
826 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000827 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000828 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000829
830 ctxt->am = xmlNewAutomata();
831 if (ctxt->am == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000832 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
833 XML_ERR_INTERNAL_ERROR,
834 "Cannot create automata for element %s\n",
835 elem->name, NULL, NULL);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000836 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000837 }
William M. Brack78637da2003-07-31 14:47:38 +0000838 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000839 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
840 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000841 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000842 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000843 char expr[5000];
844 expr[0] = 0;
845 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000846 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
847 XML_DTD_CONTENT_NOT_DETERMINIST,
848 "Content model of %s is not determinist: %s\n",
849 elem->name, BAD_CAST expr, NULL);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000850#ifdef DEBUG_REGEXP_ALGO
851 xmlRegexpPrint(stderr, elem->contModel);
852#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000853 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000854 ctxt->state = NULL;
855 xmlFreeAutomata(ctxt->am);
856 ctxt->am = NULL;
857 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000858 }
859 ctxt->state = NULL;
860 xmlFreeAutomata(ctxt->am);
861 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000862 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000863}
864
865#endif /* LIBXML_REGEXP_ENABLED */
866
Owen Taylor3473f882001-02-23 17:55:21 +0000867/****************************************************************
868 * *
869 * Util functions for data allocation/deallocation *
870 * *
871 ****************************************************************/
872
873/**
Daniel Veillarda37aab82003-06-09 09:10:36 +0000874 * xmlNewValidCtxt:
875 *
876 * Allocate a validation context structure.
877 *
878 * Returns NULL if not, otherwise the new validation context structure
879 */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000880xmlValidCtxtPtr xmlNewValidCtxt(void) {
Daniel Veillarda37aab82003-06-09 09:10:36 +0000881 xmlValidCtxtPtr ret;
882
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000883 if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000884 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda37aab82003-06-09 09:10:36 +0000885 return (NULL);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000886 }
Daniel Veillarda37aab82003-06-09 09:10:36 +0000887
888 (void) memset(ret, 0, sizeof (xmlValidCtxt));
889
890 return (ret);
891}
892
893/**
894 * xmlFreeValidCtxt:
895 * @cur: the validation context to free
896 *
897 * Free a validation context structure.
898 */
899void
900xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
Daniel Veillardc0be74b2004-11-03 19:16:55 +0000901 if (cur->vstateTab != NULL)
902 xmlFree(cur->vstateTab);
903 if (cur->nodeTab != NULL)
904 xmlFree(cur->nodeTab);
Daniel Veillarda37aab82003-06-09 09:10:36 +0000905 xmlFree(cur);
906}
907
Daniel Veillard4432df22003-09-28 18:58:27 +0000908#endif /* LIBXML_VALID_ENABLED */
909
Daniel Veillarda37aab82003-06-09 09:10:36 +0000910/**
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000911 * xmlNewDocElementContent:
912 * @doc: the document
Owen Taylor3473f882001-02-23 17:55:21 +0000913 * @name: the subelement name or NULL
914 * @type: the type of element content decl
915 *
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000916 * Allocate an element content structure for the document.
Owen Taylor3473f882001-02-23 17:55:21 +0000917 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000918 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000919 */
920xmlElementContentPtr
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000921xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name,
922 xmlElementContentType type) {
Owen Taylor3473f882001-02-23 17:55:21 +0000923 xmlElementContentPtr ret;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000924 xmlDictPtr dict = NULL;
925
926 if (doc != NULL)
927 dict = doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +0000928
929 switch(type) {
930 case XML_ELEMENT_CONTENT_ELEMENT:
931 if (name == NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000932 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
933 "xmlNewElementContent : name == NULL !\n",
934 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000935 }
936 break;
937 case XML_ELEMENT_CONTENT_PCDATA:
938 case XML_ELEMENT_CONTENT_SEQ:
939 case XML_ELEMENT_CONTENT_OR:
940 if (name != NULL) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000941 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
942 "xmlNewElementContent : name != NULL !\n",
943 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000944 }
945 break;
946 default:
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800947 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000948 "Internal: ELEMENT content corrupted invalid type\n",
949 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000950 return(NULL);
951 }
952 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
953 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000954 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +0000955 return(NULL);
956 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000957 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000958 ret->type = type;
959 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000960 if (name != NULL) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000961 int l;
962 const xmlChar *tmp;
963
964 tmp = xmlSplitQName3(name, &l);
965 if (tmp == NULL) {
966 if (dict == NULL)
967 ret->name = xmlStrdup(name);
968 else
969 ret->name = xmlDictLookup(dict, name, -1);
970 } else {
971 if (dict == NULL) {
972 ret->prefix = xmlStrndup(name, l);
973 ret->name = xmlStrdup(tmp);
974 } else {
975 ret->prefix = xmlDictLookup(dict, name, l);
976 ret->name = xmlDictLookup(dict, tmp, -1);
977 }
978 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000979 }
Daniel Veillardcee2b3a2005-01-25 00:22:52 +0000980 return(ret);
981}
982
983/**
984 * xmlNewElementContent:
985 * @name: the subelement name or NULL
986 * @type: the type of element content decl
987 *
988 * Allocate an element content structure.
989 * Deprecated in favor of xmlNewDocElementContent
990 *
991 * Returns NULL if not, otherwise the new element content structure
992 */
993xmlElementContentPtr
994xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
995 return(xmlNewDocElementContent(NULL, name, type));
996}
997
998/**
999 * xmlCopyDocElementContent:
1000 * @doc: the document owning the element declaration
1001 * @cur: An element content pointer.
1002 *
1003 * Build a copy of an element content description.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001004 *
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001005 * Returns the new xmlElementContentPtr or NULL in case of error.
1006 */
1007xmlElementContentPtr
1008xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
1009 xmlElementContentPtr ret = NULL, prev = NULL, tmp;
1010 xmlDictPtr dict = NULL;
1011
1012 if (cur == NULL) return(NULL);
1013
1014 if (doc != NULL)
1015 dict = doc->dict;
1016
1017 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
1018 if (ret == NULL) {
1019 xmlVErrMemory(NULL, "malloc failed");
1020 return(NULL);
1021 }
1022 memset(ret, 0, sizeof(xmlElementContent));
1023 ret->type = cur->type;
1024 ret->ocur = cur->ocur;
1025 if (cur->name != NULL) {
1026 if (dict)
1027 ret->name = xmlDictLookup(dict, cur->name, -1);
1028 else
1029 ret->name = xmlStrdup(cur->name);
1030 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001031
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001032 if (cur->prefix != NULL) {
1033 if (dict)
1034 ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
1035 else
1036 ret->prefix = xmlStrdup(cur->prefix);
1037 }
1038 if (cur->c1 != NULL)
1039 ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
1040 if (ret->c1 != NULL)
1041 ret->c1->parent = ret;
1042 if (cur->c2 != NULL) {
1043 prev = ret;
1044 cur = cur->c2;
1045 while (cur != NULL) {
1046 tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
1047 if (tmp == NULL) {
1048 xmlVErrMemory(NULL, "malloc failed");
1049 return(ret);
1050 }
1051 memset(tmp, 0, sizeof(xmlElementContent));
1052 tmp->type = cur->type;
1053 tmp->ocur = cur->ocur;
1054 prev->c2 = tmp;
1055 if (cur->name != NULL) {
1056 if (dict)
1057 tmp->name = xmlDictLookup(dict, cur->name, -1);
1058 else
1059 tmp->name = xmlStrdup(cur->name);
1060 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001061
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001062 if (cur->prefix != NULL) {
1063 if (dict)
1064 tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
1065 else
1066 tmp->prefix = xmlStrdup(cur->prefix);
1067 }
1068 if (cur->c1 != NULL)
1069 tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
1070 if (tmp->c1 != NULL)
1071 tmp->c1->parent = ret;
1072 prev = tmp;
1073 cur = cur->c2;
1074 }
1075 }
Owen Taylor3473f882001-02-23 17:55:21 +00001076 return(ret);
1077}
1078
1079/**
1080 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +00001081 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +00001082 *
1083 * Build a copy of an element content description.
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001084 * Deprecated, use xmlCopyDocElementContent instead
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001085 *
Owen Taylor3473f882001-02-23 17:55:21 +00001086 * Returns the new xmlElementContentPtr or NULL in case of error.
1087 */
1088xmlElementContentPtr
1089xmlCopyElementContent(xmlElementContentPtr cur) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001090 return(xmlCopyDocElementContent(NULL, cur));
1091}
Owen Taylor3473f882001-02-23 17:55:21 +00001092
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001093/**
1094 * xmlFreeDocElementContent:
1095 * @doc: the document owning the element declaration
1096 * @cur: the element content tree to free
1097 *
1098 * Free an element content structure. The whole subtree is removed.
1099 */
1100void
1101xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
1102 xmlElementContentPtr next;
1103 xmlDictPtr dict = NULL;
1104
1105 if (doc != NULL)
1106 dict = doc->dict;
1107
1108 while (cur != NULL) {
1109 next = cur->c2;
1110 switch (cur->type) {
1111 case XML_ELEMENT_CONTENT_PCDATA:
1112 case XML_ELEMENT_CONTENT_ELEMENT:
1113 case XML_ELEMENT_CONTENT_SEQ:
1114 case XML_ELEMENT_CONTENT_OR:
1115 break;
1116 default:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001117 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001118 "Internal: ELEMENT content corrupted invalid type\n",
1119 NULL);
1120 return;
1121 }
1122 if (cur->c1 != NULL) xmlFreeDocElementContent(doc, cur->c1);
1123 if (dict) {
1124 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
1125 xmlFree((xmlChar *) cur->name);
1126 if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
1127 xmlFree((xmlChar *) cur->prefix);
1128 } else {
1129 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
1130 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
1131 }
1132 xmlFree(cur);
1133 cur = next;
Owen Taylor3473f882001-02-23 17:55:21 +00001134 }
Owen Taylor3473f882001-02-23 17:55:21 +00001135}
1136
1137/**
1138 * xmlFreeElementContent:
1139 * @cur: the element content tree to free
1140 *
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001141 * Free an element content structure. The whole subtree is removed.
1142 * Deprecated, use xmlFreeDocElementContent instead
Owen Taylor3473f882001-02-23 17:55:21 +00001143 */
1144void
1145xmlFreeElementContent(xmlElementContentPtr cur) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001146 xmlFreeDocElementContent(NULL, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001147}
1148
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001149#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001150/**
1151 * xmlDumpElementContent:
1152 * @buf: An XML buffer
1153 * @content: An element table
1154 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1155 *
1156 * This will dump the content of the element table as an XML DTD definition
1157 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001158static void
Owen Taylor3473f882001-02-23 17:55:21 +00001159xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
1160 if (content == NULL) return;
1161
1162 if (glob) xmlBufferWriteChar(buf, "(");
1163 switch (content->type) {
1164 case XML_ELEMENT_CONTENT_PCDATA:
1165 xmlBufferWriteChar(buf, "#PCDATA");
1166 break;
1167 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001168 if (content->prefix != NULL) {
1169 xmlBufferWriteCHAR(buf, content->prefix);
1170 xmlBufferWriteChar(buf, ":");
1171 }
Owen Taylor3473f882001-02-23 17:55:21 +00001172 xmlBufferWriteCHAR(buf, content->name);
1173 break;
1174 case XML_ELEMENT_CONTENT_SEQ:
1175 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1176 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1177 xmlDumpElementContent(buf, content->c1, 1);
1178 else
1179 xmlDumpElementContent(buf, content->c1, 0);
1180 xmlBufferWriteChar(buf, " , ");
William M. Brack4119d1c2004-06-24 02:24:44 +00001181 if ((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1182 ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) &&
1183 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
Owen Taylor3473f882001-02-23 17:55:21 +00001184 xmlDumpElementContent(buf, content->c2, 1);
1185 else
1186 xmlDumpElementContent(buf, content->c2, 0);
1187 break;
1188 case XML_ELEMENT_CONTENT_OR:
1189 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1190 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1191 xmlDumpElementContent(buf, content->c1, 1);
1192 else
1193 xmlDumpElementContent(buf, content->c1, 0);
1194 xmlBufferWriteChar(buf, " | ");
William M. Brack4119d1c2004-06-24 02:24:44 +00001195 if ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1196 ((content->c2->type == XML_ELEMENT_CONTENT_OR) &&
1197 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
Owen Taylor3473f882001-02-23 17:55:21 +00001198 xmlDumpElementContent(buf, content->c2, 1);
1199 else
1200 xmlDumpElementContent(buf, content->c2, 0);
1201 break;
1202 default:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001203 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001204 "Internal: ELEMENT content corrupted invalid type\n",
1205 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001206 }
1207 if (glob)
1208 xmlBufferWriteChar(buf, ")");
1209 switch (content->ocur) {
1210 case XML_ELEMENT_CONTENT_ONCE:
1211 break;
1212 case XML_ELEMENT_CONTENT_OPT:
1213 xmlBufferWriteChar(buf, "?");
1214 break;
1215 case XML_ELEMENT_CONTENT_MULT:
1216 xmlBufferWriteChar(buf, "*");
1217 break;
1218 case XML_ELEMENT_CONTENT_PLUS:
1219 xmlBufferWriteChar(buf, "+");
1220 break;
1221 }
1222}
1223
1224/**
1225 * xmlSprintfElementContent:
1226 * @buf: an output buffer
1227 * @content: An element table
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001228 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001229 *
Daniel Veillardd3d06722001-08-15 12:06:36 +00001230 * Deprecated, unsafe, use xmlSnprintfElementContent
1231 */
1232void
1233xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
1234 xmlElementContentPtr content ATTRIBUTE_UNUSED,
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001235 int englob ATTRIBUTE_UNUSED) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001236}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001237#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardd3d06722001-08-15 12:06:36 +00001238
1239/**
1240 * xmlSnprintfElementContent:
1241 * @buf: an output buffer
1242 * @size: the buffer size
1243 * @content: An element table
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001244 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
Daniel Veillardd3d06722001-08-15 12:06:36 +00001245 *
Owen Taylor3473f882001-02-23 17:55:21 +00001246 * This will dump the content of the element content definition
1247 * Intended just for the debug routine
1248 */
1249void
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001250xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001251 int len;
1252
Owen Taylor3473f882001-02-23 17:55:21 +00001253 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +00001254 len = strlen(buf);
1255 if (size - len < 50) {
1256 if ((size - len > 4) && (buf[len - 1] != '.'))
1257 strcat(buf, " ...");
1258 return;
1259 }
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001260 if (englob) strcat(buf, "(");
Owen Taylor3473f882001-02-23 17:55:21 +00001261 switch (content->type) {
1262 case XML_ELEMENT_CONTENT_PCDATA:
1263 strcat(buf, "#PCDATA");
1264 break;
1265 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001266 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001267 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001268 strcat(buf, " ...");
1269 return;
1270 }
1271 strcat(buf, (char *) content->prefix);
1272 strcat(buf, ":");
1273 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001274 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001275 strcat(buf, " ...");
1276 return;
1277 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001278 if (content->name != NULL)
1279 strcat(buf, (char *) content->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001280 break;
1281 case XML_ELEMENT_CONTENT_SEQ:
1282 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1283 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001284 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001285 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001286 xmlSnprintfElementContent(buf, size, content->c1, 0);
1287 len = strlen(buf);
1288 if (size - len < 50) {
1289 if ((size - len > 4) && (buf[len - 1] != '.'))
1290 strcat(buf, " ...");
1291 return;
1292 }
Owen Taylor3473f882001-02-23 17:55:21 +00001293 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001294 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1295 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1296 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001297 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001298 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001299 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001300 break;
1301 case XML_ELEMENT_CONTENT_OR:
1302 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1303 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001304 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001305 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001306 xmlSnprintfElementContent(buf, size, content->c1, 0);
1307 len = strlen(buf);
1308 if (size - len < 50) {
1309 if ((size - len > 4) && (buf[len - 1] != '.'))
1310 strcat(buf, " ...");
1311 return;
1312 }
Owen Taylor3473f882001-02-23 17:55:21 +00001313 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001314 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1315 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1316 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001317 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001318 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001319 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001320 break;
1321 }
Daniel Veillard1b75c3b2005-06-26 21:49:08 +00001322 if (englob)
Owen Taylor3473f882001-02-23 17:55:21 +00001323 strcat(buf, ")");
1324 switch (content->ocur) {
1325 case XML_ELEMENT_CONTENT_ONCE:
1326 break;
1327 case XML_ELEMENT_CONTENT_OPT:
1328 strcat(buf, "?");
1329 break;
1330 case XML_ELEMENT_CONTENT_MULT:
1331 strcat(buf, "*");
1332 break;
1333 case XML_ELEMENT_CONTENT_PLUS:
1334 strcat(buf, "+");
1335 break;
1336 }
1337}
1338
1339/****************************************************************
1340 * *
1341 * Registration of DTD declarations *
1342 * *
1343 ****************************************************************/
1344
1345/**
Owen Taylor3473f882001-02-23 17:55:21 +00001346 * xmlFreeElement:
1347 * @elem: An element
1348 *
1349 * Deallocate the memory used by an element definition
1350 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001351static void
Owen Taylor3473f882001-02-23 17:55:21 +00001352xmlFreeElement(xmlElementPtr elem) {
1353 if (elem == NULL) return;
1354 xmlUnlinkNode((xmlNodePtr) elem);
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001355 xmlFreeDocElementContent(elem->doc, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001356 if (elem->name != NULL)
1357 xmlFree((xmlChar *) elem->name);
1358 if (elem->prefix != NULL)
1359 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001360#ifdef LIBXML_REGEXP_ENABLED
1361 if (elem->contModel != NULL)
1362 xmlRegFreeRegexp(elem->contModel);
1363#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001364 xmlFree(elem);
1365}
1366
1367
1368/**
1369 * xmlAddElementDecl:
1370 * @ctxt: the validation context
1371 * @dtd: pointer to the DTD
1372 * @name: the entity name
1373 * @type: the element type
1374 * @content: the element content tree or NULL
1375 *
1376 * Register a new element declaration
1377 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001378 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001379 */
1380xmlElementPtr
William M. Brackedb65a72004-02-06 07:36:04 +00001381xmlAddElementDecl(xmlValidCtxtPtr ctxt,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001382 xmlDtdPtr dtd, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001383 xmlElementTypeVal type,
1384 xmlElementContentPtr content) {
1385 xmlElementPtr ret;
1386 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001387 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001388 xmlChar *ns, *uqname;
1389
1390 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001391 return(NULL);
1392 }
1393 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001394 return(NULL);
1395 }
Daniel Veillard316a5c32005-01-23 22:56:39 +00001396
Owen Taylor3473f882001-02-23 17:55:21 +00001397 switch (type) {
1398 case XML_ELEMENT_TYPE_EMPTY:
1399 if (content != NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001400 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001401 "xmlAddElementDecl: content != NULL for EMPTY\n",
1402 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001403 return(NULL);
1404 }
1405 break;
1406 case XML_ELEMENT_TYPE_ANY:
1407 if (content != NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001408 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001409 "xmlAddElementDecl: content != NULL for ANY\n",
1410 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001411 return(NULL);
1412 }
1413 break;
1414 case XML_ELEMENT_TYPE_MIXED:
1415 if (content == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001416 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001417 "xmlAddElementDecl: content == NULL for MIXED\n",
1418 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001419 return(NULL);
1420 }
1421 break;
1422 case XML_ELEMENT_TYPE_ELEMENT:
1423 if (content == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001424 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001425 "xmlAddElementDecl: content == NULL for ELEMENT\n",
1426 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001427 return(NULL);
1428 }
1429 break;
1430 default:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001431 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001432 "Internal: ELEMENT decl corrupted invalid type\n",
1433 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001434 return(NULL);
1435 }
1436
1437 /*
1438 * check if name is a QName
1439 */
1440 uqname = xmlSplitQName2(name, &ns);
1441 if (uqname != NULL)
1442 name = uqname;
1443
1444 /*
1445 * Create the Element table if needed.
1446 */
1447 table = (xmlElementTablePtr) dtd->elements;
1448 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00001449 xmlDictPtr dict = NULL;
1450
1451 if (dtd->doc != NULL)
1452 dict = dtd->doc->dict;
1453 table = xmlHashCreateDict(0, dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001454 dtd->elements = (void *) table;
1455 }
1456 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001457 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001458 "xmlAddElementDecl: Table creation failed!\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001459 if (uqname != NULL)
1460 xmlFree(uqname);
1461 if (ns != NULL)
1462 xmlFree(ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001463 return(NULL);
1464 }
1465
Daniel Veillarda10efa82001-04-18 13:09:01 +00001466 /*
1467 * lookup old attributes inserted on an undefined element in the
1468 * internal subset.
1469 */
1470 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1471 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1472 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1473 oldAttributes = ret->attributes;
1474 ret->attributes = NULL;
1475 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1476 xmlFreeElement(ret);
1477 }
Owen Taylor3473f882001-02-23 17:55:21 +00001478 }
Owen Taylor3473f882001-02-23 17:55:21 +00001479
1480 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001481 * The element may already be present if one of its attribute
1482 * was registered first
1483 */
1484 ret = xmlHashLookup2(table, name, ns);
1485 if (ret != NULL) {
1486 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001487#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001488 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001489 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001490 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001491 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1492 "Redefinition of element %s\n",
1493 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001494#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001495 if (uqname != NULL)
1496 xmlFree(uqname);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001497 if (ns != NULL)
1498 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001499 return(NULL);
1500 }
William M. Brackd6e347e2005-04-15 01:34:41 +00001501 if (ns != NULL) {
1502 xmlFree(ns);
1503 ns = NULL;
1504 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001505 } else {
1506 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1507 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001508 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001509 if (uqname != NULL)
1510 xmlFree(uqname);
1511 if (ns != NULL)
1512 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001513 return(NULL);
1514 }
1515 memset(ret, 0, sizeof(xmlElement));
1516 ret->type = XML_ELEMENT_DECL;
1517
1518 /*
1519 * fill the structure.
1520 */
1521 ret->name = xmlStrdup(name);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001522 if (ret->name == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001523 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001524 if (uqname != NULL)
1525 xmlFree(uqname);
1526 if (ns != NULL)
1527 xmlFree(ns);
1528 xmlFree(ret);
1529 return(NULL);
1530 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001531 ret->prefix = ns;
1532
1533 /*
1534 * Validity Check:
1535 * Insertion must not fail
1536 */
1537 if (xmlHashAddEntry2(table, name, ns, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001538#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001539 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001540 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001541 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001542 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1543 "Redefinition of element %s\n",
1544 name, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00001545#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001546 xmlFreeElement(ret);
1547 if (uqname != NULL)
1548 xmlFree(uqname);
1549 return(NULL);
1550 }
William M. Brack4e52f2f2003-09-14 18:07:39 +00001551 /*
1552 * For new element, may have attributes from earlier
1553 * definition in internal subset
1554 */
1555 ret->attributes = oldAttributes;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001556 }
1557
1558 /*
1559 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001560 */
1561 ret->etype = type;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001562 /*
1563 * Avoid a stupid copy when called by the parser
1564 * and flag it by setting a special parent value
1565 * so the parser doesn't unallocate it.
1566 */
Daniel Veillardc394f732005-01-26 00:04:52 +00001567 if ((ctxt != NULL) &&
1568 ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
1569 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1))) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001570 ret->content = content;
1571 if (content != NULL)
1572 content->parent = (xmlElementContentPtr) 1;
1573 } else {
1574 ret->content = xmlCopyDocElementContent(dtd->doc, content);
1575 }
Owen Taylor3473f882001-02-23 17:55:21 +00001576
1577 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001578 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001579 */
1580 ret->parent = dtd;
1581 ret->doc = dtd->doc;
1582 if (dtd->last == NULL) {
1583 dtd->children = dtd->last = (xmlNodePtr) ret;
1584 } else {
1585 dtd->last->next = (xmlNodePtr) ret;
1586 ret->prev = dtd->last;
1587 dtd->last = (xmlNodePtr) ret;
1588 }
1589 if (uqname != NULL)
1590 xmlFree(uqname);
1591 return(ret);
1592}
1593
1594/**
1595 * xmlFreeElementTable:
1596 * @table: An element table
1597 *
1598 * Deallocate the memory used by an element hash table.
1599 */
1600void
1601xmlFreeElementTable(xmlElementTablePtr table) {
1602 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1603}
1604
Daniel Veillard652327a2003-09-29 18:02:38 +00001605#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001606/**
1607 * xmlCopyElement:
1608 * @elem: An element
1609 *
1610 * Build a copy of an element.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001611 *
Owen Taylor3473f882001-02-23 17:55:21 +00001612 * Returns the new xmlElementPtr or NULL in case of error.
1613 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001614static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001615xmlCopyElement(xmlElementPtr elem) {
1616 xmlElementPtr cur;
1617
1618 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1619 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001620 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001621 return(NULL);
1622 }
1623 memset(cur, 0, sizeof(xmlElement));
1624 cur->type = XML_ELEMENT_DECL;
1625 cur->etype = elem->etype;
1626 if (elem->name != NULL)
1627 cur->name = xmlStrdup(elem->name);
1628 else
1629 cur->name = NULL;
1630 if (elem->prefix != NULL)
1631 cur->prefix = xmlStrdup(elem->prefix);
1632 else
1633 cur->prefix = NULL;
1634 cur->content = xmlCopyElementContent(elem->content);
1635 /* TODO : rebuild the attribute list on the copy */
1636 cur->attributes = NULL;
1637 return(cur);
1638}
1639
1640/**
1641 * xmlCopyElementTable:
1642 * @table: An element table
1643 *
1644 * Build a copy of an element table.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001645 *
Owen Taylor3473f882001-02-23 17:55:21 +00001646 * Returns the new xmlElementTablePtr or NULL in case of error.
1647 */
1648xmlElementTablePtr
1649xmlCopyElementTable(xmlElementTablePtr table) {
1650 return((xmlElementTablePtr) xmlHashCopy(table,
1651 (xmlHashCopier) xmlCopyElement));
1652}
Daniel Veillard652327a2003-09-29 18:02:38 +00001653#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001654
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001655#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001656/**
1657 * xmlDumpElementDecl:
1658 * @buf: the XML buffer output
1659 * @elem: An element table
1660 *
1661 * This will dump the content of the element declaration as an XML
1662 * DTD definition
1663 */
1664void
1665xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00001666 if ((buf == NULL) || (elem == NULL))
1667 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001668 switch (elem->etype) {
1669 case XML_ELEMENT_TYPE_EMPTY:
1670 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001671 if (elem->prefix != NULL) {
1672 xmlBufferWriteCHAR(buf, elem->prefix);
1673 xmlBufferWriteChar(buf, ":");
1674 }
Owen Taylor3473f882001-02-23 17:55:21 +00001675 xmlBufferWriteCHAR(buf, elem->name);
1676 xmlBufferWriteChar(buf, " EMPTY>\n");
1677 break;
1678 case XML_ELEMENT_TYPE_ANY:
1679 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001680 if (elem->prefix != NULL) {
1681 xmlBufferWriteCHAR(buf, elem->prefix);
1682 xmlBufferWriteChar(buf, ":");
1683 }
Owen Taylor3473f882001-02-23 17:55:21 +00001684 xmlBufferWriteCHAR(buf, elem->name);
1685 xmlBufferWriteChar(buf, " ANY>\n");
1686 break;
1687 case XML_ELEMENT_TYPE_MIXED:
1688 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001689 if (elem->prefix != NULL) {
1690 xmlBufferWriteCHAR(buf, elem->prefix);
1691 xmlBufferWriteChar(buf, ":");
1692 }
Owen Taylor3473f882001-02-23 17:55:21 +00001693 xmlBufferWriteCHAR(buf, elem->name);
1694 xmlBufferWriteChar(buf, " ");
1695 xmlDumpElementContent(buf, elem->content, 1);
1696 xmlBufferWriteChar(buf, ">\n");
1697 break;
1698 case XML_ELEMENT_TYPE_ELEMENT:
1699 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001700 if (elem->prefix != NULL) {
1701 xmlBufferWriteCHAR(buf, elem->prefix);
1702 xmlBufferWriteChar(buf, ":");
1703 }
Owen Taylor3473f882001-02-23 17:55:21 +00001704 xmlBufferWriteCHAR(buf, elem->name);
1705 xmlBufferWriteChar(buf, " ");
1706 xmlDumpElementContent(buf, elem->content, 1);
1707 xmlBufferWriteChar(buf, ">\n");
1708 break;
1709 default:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001710 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001711 "Internal: ELEMENT struct corrupted invalid type\n",
1712 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001713 }
1714}
1715
1716/**
William M. Brack9e660592003-10-20 14:56:06 +00001717 * xmlDumpElementDeclScan:
1718 * @elem: An element table
1719 * @buf: the XML buffer output
1720 *
1721 * This routine is used by the hash scan function. It just reverses
1722 * the arguments.
1723 */
1724static void
1725xmlDumpElementDeclScan(xmlElementPtr elem, xmlBufferPtr buf) {
1726 xmlDumpElementDecl(buf, elem);
1727}
1728
1729/**
Owen Taylor3473f882001-02-23 17:55:21 +00001730 * xmlDumpElementTable:
1731 * @buf: the XML buffer output
1732 * @table: An element table
1733 *
1734 * This will dump the content of the element table as an XML DTD definition
1735 */
1736void
1737xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00001738 if ((buf == NULL) || (table == NULL))
1739 return;
William M. Brack9e660592003-10-20 14:56:06 +00001740 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001741}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001742#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001743
1744/**
1745 * xmlCreateEnumeration:
1746 * @name: the enumeration name or NULL
1747 *
1748 * create and initialize an enumeration attribute node.
1749 *
1750 * Returns the xmlEnumerationPtr just created or NULL in case
1751 * of error.
1752 */
1753xmlEnumerationPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001754xmlCreateEnumeration(const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00001755 xmlEnumerationPtr ret;
1756
1757 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1758 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001759 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00001760 return(NULL);
1761 }
1762 memset(ret, 0, sizeof(xmlEnumeration));
1763
1764 if (name != NULL)
1765 ret->name = xmlStrdup(name);
1766 return(ret);
1767}
1768
1769/**
1770 * xmlFreeEnumeration:
1771 * @cur: the tree to free.
1772 *
1773 * free an enumeration attribute node (recursive).
1774 */
1775void
1776xmlFreeEnumeration(xmlEnumerationPtr cur) {
1777 if (cur == NULL) return;
1778
1779 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1780
1781 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001782 xmlFree(cur);
1783}
1784
Daniel Veillard652327a2003-09-29 18:02:38 +00001785#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001786/**
1787 * xmlCopyEnumeration:
1788 * @cur: the tree to copy.
1789 *
1790 * Copy an enumeration attribute node (recursive).
1791 *
1792 * Returns the xmlEnumerationPtr just created or NULL in case
1793 * of error.
1794 */
1795xmlEnumerationPtr
1796xmlCopyEnumeration(xmlEnumerationPtr cur) {
1797 xmlEnumerationPtr ret;
1798
1799 if (cur == NULL) return(NULL);
1800 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1801
1802 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1803 else ret->next = NULL;
1804
1805 return(ret);
1806}
Daniel Veillard652327a2003-09-29 18:02:38 +00001807#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001808
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001809#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001810/**
1811 * xmlDumpEnumeration:
1812 * @buf: the XML buffer output
1813 * @enum: An enumeration
1814 *
1815 * This will dump the content of the enumeration
1816 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001817static void
Owen Taylor3473f882001-02-23 17:55:21 +00001818xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00001819 if ((buf == NULL) || (cur == NULL))
1820 return;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001821
Owen Taylor3473f882001-02-23 17:55:21 +00001822 xmlBufferWriteCHAR(buf, cur->name);
1823 if (cur->next == NULL)
1824 xmlBufferWriteChar(buf, ")");
1825 else {
1826 xmlBufferWriteChar(buf, " | ");
1827 xmlDumpEnumeration(buf, cur->next);
1828 }
1829}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001830#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001831
Daniel Veillard4432df22003-09-28 18:58:27 +00001832#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001833/**
Owen Taylor3473f882001-02-23 17:55:21 +00001834 * xmlScanIDAttributeDecl:
1835 * @ctxt: the validation context
1836 * @elem: the element name
Daniel Veillarddbee0f12005-06-27 13:42:57 +00001837 * @err: whether to raise errors here
Owen Taylor3473f882001-02-23 17:55:21 +00001838 *
1839 * Verify that the element don't have too many ID attributes
1840 * declared.
1841 *
1842 * Returns the number of ID attributes found.
1843 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001844static int
Daniel Veillarddbee0f12005-06-27 13:42:57 +00001845xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) {
Owen Taylor3473f882001-02-23 17:55:21 +00001846 xmlAttributePtr cur;
1847 int ret = 0;
1848
1849 if (elem == NULL) return(0);
1850 cur = elem->attributes;
1851 while (cur != NULL) {
1852 if (cur->atype == XML_ATTRIBUTE_ID) {
1853 ret ++;
Daniel Veillarddbee0f12005-06-27 13:42:57 +00001854 if ((ret > 1) && (err))
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001855 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001856 "Element %s has too many ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001857 elem->name, cur->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001858 }
1859 cur = cur->nexth;
1860 }
1861 return(ret);
1862}
Daniel Veillard4432df22003-09-28 18:58:27 +00001863#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001864
1865/**
1866 * xmlFreeAttribute:
1867 * @elem: An attribute
1868 *
1869 * Deallocate the memory used by an attribute definition
1870 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001871static void
Owen Taylor3473f882001-02-23 17:55:21 +00001872xmlFreeAttribute(xmlAttributePtr attr) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001873 xmlDictPtr dict;
1874
Owen Taylor3473f882001-02-23 17:55:21 +00001875 if (attr == NULL) return;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001876 if (attr->doc != NULL)
1877 dict = attr->doc->dict;
1878 else
1879 dict = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001880 xmlUnlinkNode((xmlNodePtr) attr);
1881 if (attr->tree != NULL)
1882 xmlFreeEnumeration(attr->tree);
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001883 if (dict) {
1884 if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
1885 xmlFree((xmlChar *) attr->elem);
1886 if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
1887 xmlFree((xmlChar *) attr->name);
1888 if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
1889 xmlFree((xmlChar *) attr->prefix);
1890 if ((attr->defaultValue != NULL) &&
1891 (!xmlDictOwns(dict, attr->defaultValue)))
1892 xmlFree((xmlChar *) attr->defaultValue);
1893 } else {
1894 if (attr->elem != NULL)
1895 xmlFree((xmlChar *) attr->elem);
1896 if (attr->name != NULL)
1897 xmlFree((xmlChar *) attr->name);
1898 if (attr->defaultValue != NULL)
1899 xmlFree((xmlChar *) attr->defaultValue);
1900 if (attr->prefix != NULL)
1901 xmlFree((xmlChar *) attr->prefix);
1902 }
Owen Taylor3473f882001-02-23 17:55:21 +00001903 xmlFree(attr);
1904}
1905
1906
1907/**
1908 * xmlAddAttributeDecl:
1909 * @ctxt: the validation context
1910 * @dtd: pointer to the DTD
1911 * @elem: the element name
1912 * @name: the attribute name
1913 * @ns: the attribute namespace prefix
1914 * @type: the attribute type
1915 * @def: the attribute default type
1916 * @defaultValue: the attribute default value
1917 * @tree: if it's an enumeration, the associated list
1918 *
1919 * Register a new attribute declaration
1920 * Note that @tree becomes the ownership of the DTD
1921 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001922 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001923 */
1924xmlAttributePtr
William M. Brackedb65a72004-02-06 07:36:04 +00001925xmlAddAttributeDecl(xmlValidCtxtPtr ctxt,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001926 xmlDtdPtr dtd, const xmlChar *elem,
Owen Taylor3473f882001-02-23 17:55:21 +00001927 const xmlChar *name, const xmlChar *ns,
1928 xmlAttributeType type, xmlAttributeDefault def,
1929 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1930 xmlAttributePtr ret;
1931 xmlAttributeTablePtr table;
1932 xmlElementPtr elemDef;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001933 xmlDictPtr dict = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001934
1935 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001936 xmlFreeEnumeration(tree);
1937 return(NULL);
1938 }
1939 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001940 xmlFreeEnumeration(tree);
1941 return(NULL);
1942 }
1943 if (elem == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001944 xmlFreeEnumeration(tree);
1945 return(NULL);
1946 }
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00001947 if (dtd->doc != NULL)
1948 dict = dtd->doc->dict;
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001949
Daniel Veillard4432df22003-09-28 18:58:27 +00001950#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001951 /*
1952 * Check the type and possibly the default value.
1953 */
1954 switch (type) {
1955 case XML_ATTRIBUTE_CDATA:
1956 break;
1957 case XML_ATTRIBUTE_ID:
1958 break;
1959 case XML_ATTRIBUTE_IDREF:
1960 break;
1961 case XML_ATTRIBUTE_IDREFS:
1962 break;
1963 case XML_ATTRIBUTE_ENTITY:
1964 break;
1965 case XML_ATTRIBUTE_ENTITIES:
1966 break;
1967 case XML_ATTRIBUTE_NMTOKEN:
1968 break;
1969 case XML_ATTRIBUTE_NMTOKENS:
1970 break;
1971 case XML_ATTRIBUTE_ENUMERATION:
1972 break;
1973 case XML_ATTRIBUTE_NOTATION:
1974 break;
1975 default:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001976 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001977 "Internal: ATTRIBUTE struct corrupted invalid type\n",
1978 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001979 xmlFreeEnumeration(tree);
1980 return(NULL);
1981 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001982 if ((defaultValue != NULL) &&
Daniel Veillardae0765b2008-07-31 19:54:59 +00001983 (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00001984 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
1985 "Attribute %s of %s: invalid default value\n",
1986 elem, name, defaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001987 defaultValue = NULL;
Daniel Veillard42595322004-11-08 10:52:06 +00001988 if (ctxt != NULL)
1989 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001990 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001991#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001992
1993 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001994 * Check first that an attribute defined in the external subset wasn't
1995 * already defined in the internal subset
1996 */
1997 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1998 (dtd->doc->intSubset != NULL) &&
1999 (dtd->doc->intSubset->attributes != NULL)) {
2000 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
Daniel Veillardae0765b2008-07-31 19:54:59 +00002001 if (ret != NULL) {
2002 xmlFreeEnumeration(tree);
Daniel Veillardd85f4f42002-03-25 10:48:46 +00002003 return(NULL);
Daniel Veillardae0765b2008-07-31 19:54:59 +00002004 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00002005 }
2006
2007 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002008 * Create the Attribute table if needed.
2009 */
2010 table = (xmlAttributeTablePtr) dtd->attributes;
2011 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00002012 table = xmlHashCreateDict(0, dict);
Owen Taylor3473f882001-02-23 17:55:21 +00002013 dtd->attributes = (void *) table;
2014 }
2015 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002016 xmlVErrMemory(ctxt,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002017 "xmlAddAttributeDecl: Table creation failed!\n");
Daniel Veillardae0765b2008-07-31 19:54:59 +00002018 xmlFreeEnumeration(tree);
Owen Taylor3473f882001-02-23 17:55:21 +00002019 return(NULL);
2020 }
2021
2022
2023 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2024 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002025 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillardae0765b2008-07-31 19:54:59 +00002026 xmlFreeEnumeration(tree);
Owen Taylor3473f882001-02-23 17:55:21 +00002027 return(NULL);
2028 }
2029 memset(ret, 0, sizeof(xmlAttribute));
2030 ret->type = XML_ATTRIBUTE_DECL;
2031
2032 /*
2033 * fill the structure.
2034 */
2035 ret->atype = type;
William M. Brackf810de02005-07-06 22:48:41 +00002036 /*
2037 * doc must be set before possible error causes call
2038 * to xmlFreeAttribute (because it's used to check on
2039 * dict use)
2040 */
2041 ret->doc = dtd->doc;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002042 if (dict) {
2043 ret->name = xmlDictLookup(dict, name, -1);
2044 ret->prefix = xmlDictLookup(dict, ns, -1);
2045 ret->elem = xmlDictLookup(dict, elem, -1);
2046 } else {
2047 ret->name = xmlStrdup(name);
2048 ret->prefix = xmlStrdup(ns);
2049 ret->elem = xmlStrdup(elem);
2050 }
Owen Taylor3473f882001-02-23 17:55:21 +00002051 ret->def = def;
2052 ret->tree = tree;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002053 if (defaultValue != NULL) {
2054 if (dict)
2055 ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
2056 else
2057 ret->defaultValue = xmlStrdup(defaultValue);
2058 }
Owen Taylor3473f882001-02-23 17:55:21 +00002059
2060 /*
2061 * Validity Check:
2062 * Search the DTD for previous declarations of the ATTLIST
2063 */
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00002064 if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002065#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002066 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002067 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002068 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002069 xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00002070 "Attribute %s of element %s: already defined\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002071 name, elem, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00002072#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002073 xmlFreeAttribute(ret);
2074 return(NULL);
2075 }
2076
2077 /*
2078 * Validity Check:
2079 * Multiple ID per element
2080 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00002081 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00002082 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00002083
Daniel Veillard4432df22003-09-28 18:58:27 +00002084#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002085 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillarddbee0f12005-06-27 13:42:57 +00002086 (xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002087 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
Owen Taylor3473f882001-02-23 17:55:21 +00002088 "Element %s has too may ID attributes defined : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00002089 elem, name, NULL);
Daniel Veillard42595322004-11-08 10:52:06 +00002090 if (ctxt != NULL)
2091 ctxt->valid = 0;
Daniel Veillardc7612992002-02-17 22:47:37 +00002092 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002093#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardc7612992002-02-17 22:47:37 +00002094
Daniel Veillard48da9102001-08-07 01:10:10 +00002095 /*
2096 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002097 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00002098 */
2099 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
2100 ((ret->prefix != NULL &&
2101 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
2102 ret->nexth = elemDef->attributes;
2103 elemDef->attributes = ret;
2104 } else {
2105 xmlAttributePtr tmp = elemDef->attributes;
2106
2107 while ((tmp != NULL) &&
2108 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
2109 ((ret->prefix != NULL &&
2110 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
2111 if (tmp->nexth == NULL)
2112 break;
2113 tmp = tmp->nexth;
2114 }
2115 if (tmp != NULL) {
2116 ret->nexth = tmp->nexth;
2117 tmp->nexth = ret;
2118 } else {
2119 ret->nexth = elemDef->attributes;
2120 elemDef->attributes = ret;
2121 }
2122 }
Owen Taylor3473f882001-02-23 17:55:21 +00002123 }
2124
2125 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002126 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00002127 */
2128 ret->parent = dtd;
Owen Taylor3473f882001-02-23 17:55:21 +00002129 if (dtd->last == NULL) {
2130 dtd->children = dtd->last = (xmlNodePtr) ret;
2131 } else {
2132 dtd->last->next = (xmlNodePtr) ret;
2133 ret->prev = dtd->last;
2134 dtd->last = (xmlNodePtr) ret;
2135 }
2136 return(ret);
2137}
2138
2139/**
2140 * xmlFreeAttributeTable:
2141 * @table: An attribute table
2142 *
2143 * Deallocate the memory used by an entities hash table.
2144 */
2145void
2146xmlFreeAttributeTable(xmlAttributeTablePtr table) {
2147 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
2148}
2149
Daniel Veillard652327a2003-09-29 18:02:38 +00002150#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002151/**
2152 * xmlCopyAttribute:
2153 * @attr: An attribute
2154 *
2155 * Build a copy of an attribute.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002156 *
Owen Taylor3473f882001-02-23 17:55:21 +00002157 * Returns the new xmlAttributePtr or NULL in case of error.
2158 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002159static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002160xmlCopyAttribute(xmlAttributePtr attr) {
2161 xmlAttributePtr cur;
2162
2163 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2164 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002165 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002166 return(NULL);
2167 }
2168 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00002169 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00002170 cur->atype = attr->atype;
2171 cur->def = attr->def;
2172 cur->tree = xmlCopyEnumeration(attr->tree);
2173 if (attr->elem != NULL)
2174 cur->elem = xmlStrdup(attr->elem);
2175 if (attr->name != NULL)
2176 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00002177 if (attr->prefix != NULL)
2178 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00002179 if (attr->defaultValue != NULL)
2180 cur->defaultValue = xmlStrdup(attr->defaultValue);
2181 return(cur);
2182}
2183
2184/**
2185 * xmlCopyAttributeTable:
2186 * @table: An attribute table
2187 *
2188 * Build a copy of an attribute table.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002189 *
Owen Taylor3473f882001-02-23 17:55:21 +00002190 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2191 */
2192xmlAttributeTablePtr
2193xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2194 return((xmlAttributeTablePtr) xmlHashCopy(table,
2195 (xmlHashCopier) xmlCopyAttribute));
2196}
Daniel Veillard652327a2003-09-29 18:02:38 +00002197#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002198
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002199#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002200/**
2201 * xmlDumpAttributeDecl:
2202 * @buf: the XML buffer output
2203 * @attr: An attribute declaration
2204 *
2205 * This will dump the content of the attribute declaration as an XML
2206 * DTD definition
2207 */
2208void
2209xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002210 if ((buf == NULL) || (attr == NULL))
2211 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002212 xmlBufferWriteChar(buf, "<!ATTLIST ");
2213 xmlBufferWriteCHAR(buf, attr->elem);
2214 xmlBufferWriteChar(buf, " ");
2215 if (attr->prefix != NULL) {
2216 xmlBufferWriteCHAR(buf, attr->prefix);
2217 xmlBufferWriteChar(buf, ":");
2218 }
2219 xmlBufferWriteCHAR(buf, attr->name);
2220 switch (attr->atype) {
2221 case XML_ATTRIBUTE_CDATA:
2222 xmlBufferWriteChar(buf, " CDATA");
2223 break;
2224 case XML_ATTRIBUTE_ID:
2225 xmlBufferWriteChar(buf, " ID");
2226 break;
2227 case XML_ATTRIBUTE_IDREF:
2228 xmlBufferWriteChar(buf, " IDREF");
2229 break;
2230 case XML_ATTRIBUTE_IDREFS:
2231 xmlBufferWriteChar(buf, " IDREFS");
2232 break;
2233 case XML_ATTRIBUTE_ENTITY:
2234 xmlBufferWriteChar(buf, " ENTITY");
2235 break;
2236 case XML_ATTRIBUTE_ENTITIES:
2237 xmlBufferWriteChar(buf, " ENTITIES");
2238 break;
2239 case XML_ATTRIBUTE_NMTOKEN:
2240 xmlBufferWriteChar(buf, " NMTOKEN");
2241 break;
2242 case XML_ATTRIBUTE_NMTOKENS:
2243 xmlBufferWriteChar(buf, " NMTOKENS");
2244 break;
2245 case XML_ATTRIBUTE_ENUMERATION:
2246 xmlBufferWriteChar(buf, " (");
2247 xmlDumpEnumeration(buf, attr->tree);
2248 break;
2249 case XML_ATTRIBUTE_NOTATION:
2250 xmlBufferWriteChar(buf, " NOTATION (");
2251 xmlDumpEnumeration(buf, attr->tree);
2252 break;
2253 default:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002254 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002255 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2256 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002257 }
2258 switch (attr->def) {
2259 case XML_ATTRIBUTE_NONE:
2260 break;
2261 case XML_ATTRIBUTE_REQUIRED:
2262 xmlBufferWriteChar(buf, " #REQUIRED");
2263 break;
2264 case XML_ATTRIBUTE_IMPLIED:
2265 xmlBufferWriteChar(buf, " #IMPLIED");
2266 break;
2267 case XML_ATTRIBUTE_FIXED:
2268 xmlBufferWriteChar(buf, " #FIXED");
2269 break;
2270 default:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002271 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002272 "Internal: ATTRIBUTE struct corrupted invalid def\n",
2273 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002274 }
2275 if (attr->defaultValue != NULL) {
2276 xmlBufferWriteChar(buf, " ");
2277 xmlBufferWriteQuotedString(buf, attr->defaultValue);
2278 }
2279 xmlBufferWriteChar(buf, ">\n");
2280}
2281
2282/**
William M. Brack9e660592003-10-20 14:56:06 +00002283 * xmlDumpAttributeDeclScan:
2284 * @attr: An attribute declaration
2285 * @buf: the XML buffer output
2286 *
2287 * This is used with the hash scan function - just reverses arguments
2288 */
2289static void
2290xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) {
2291 xmlDumpAttributeDecl(buf, attr);
2292}
2293
2294/**
Owen Taylor3473f882001-02-23 17:55:21 +00002295 * xmlDumpAttributeTable:
2296 * @buf: the XML buffer output
2297 * @table: An attribute table
2298 *
2299 * This will dump the content of the attribute table as an XML DTD definition
2300 */
2301void
2302xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002303 if ((buf == NULL) || (table == NULL))
2304 return;
William M. Brack9e660592003-10-20 14:56:06 +00002305 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002306}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002307#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002308
2309/************************************************************************
2310 * *
2311 * NOTATIONs *
2312 * *
2313 ************************************************************************/
2314/**
Owen Taylor3473f882001-02-23 17:55:21 +00002315 * xmlFreeNotation:
2316 * @not: A notation
2317 *
2318 * Deallocate the memory used by an notation definition
2319 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002320static void
Owen Taylor3473f882001-02-23 17:55:21 +00002321xmlFreeNotation(xmlNotationPtr nota) {
2322 if (nota == NULL) return;
2323 if (nota->name != NULL)
2324 xmlFree((xmlChar *) nota->name);
2325 if (nota->PublicID != NULL)
2326 xmlFree((xmlChar *) nota->PublicID);
2327 if (nota->SystemID != NULL)
2328 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002329 xmlFree(nota);
2330}
2331
2332
2333/**
2334 * xmlAddNotationDecl:
2335 * @dtd: pointer to the DTD
2336 * @ctxt: the validation context
2337 * @name: the entity name
2338 * @PublicID: the public identifier or NULL
2339 * @SystemID: the system identifier or NULL
2340 *
2341 * Register a new notation declaration
2342 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002343 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002344 */
2345xmlNotationPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002346xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002347 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002348 const xmlChar *PublicID, const xmlChar *SystemID) {
2349 xmlNotationPtr ret;
2350 xmlNotationTablePtr table;
2351
2352 if (dtd == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002353 return(NULL);
2354 }
2355 if (name == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002356 return(NULL);
2357 }
2358 if ((PublicID == NULL) && (SystemID == NULL)) {
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002359 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002360 }
2361
2362 /*
2363 * Create the Notation table if needed.
2364 */
2365 table = (xmlNotationTablePtr) dtd->notations;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002366 if (table == NULL) {
2367 xmlDictPtr dict = NULL;
2368 if (dtd->doc != NULL)
2369 dict = dtd->doc->dict;
2370
2371 dtd->notations = table = xmlHashCreateDict(0, dict);
2372 }
Owen Taylor3473f882001-02-23 17:55:21 +00002373 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002374 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002375 "xmlAddNotationDecl: Table creation failed!\n");
2376 return(NULL);
2377 }
2378
2379 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2380 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002381 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002382 return(NULL);
2383 }
2384 memset(ret, 0, sizeof(xmlNotation));
2385
2386 /*
2387 * fill the structure.
2388 */
2389 ret->name = xmlStrdup(name);
2390 if (SystemID != NULL)
2391 ret->SystemID = xmlStrdup(SystemID);
2392 if (PublicID != NULL)
2393 ret->PublicID = xmlStrdup(PublicID);
2394
2395 /*
2396 * Validity Check:
2397 * Check the DTD for previous declarations of the ATTLIST
2398 */
2399 if (xmlHashAddEntry(table, name, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002400#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002401 xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002402 "xmlAddNotationDecl: %s already defined\n",
2403 (const char *) name);
Daniel Veillard4432df22003-09-28 18:58:27 +00002404#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002405 xmlFreeNotation(ret);
2406 return(NULL);
2407 }
2408 return(ret);
2409}
2410
2411/**
2412 * xmlFreeNotationTable:
2413 * @table: An notation table
2414 *
2415 * Deallocate the memory used by an entities hash table.
2416 */
2417void
2418xmlFreeNotationTable(xmlNotationTablePtr table) {
2419 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2420}
2421
Daniel Veillard652327a2003-09-29 18:02:38 +00002422#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002423/**
2424 * xmlCopyNotation:
2425 * @nota: A notation
2426 *
2427 * Build a copy of a notation.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002428 *
Owen Taylor3473f882001-02-23 17:55:21 +00002429 * Returns the new xmlNotationPtr or NULL in case of error.
2430 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002431static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002432xmlCopyNotation(xmlNotationPtr nota) {
2433 xmlNotationPtr cur;
2434
2435 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2436 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002437 xmlVErrMemory(NULL, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002438 return(NULL);
2439 }
2440 if (nota->name != NULL)
2441 cur->name = xmlStrdup(nota->name);
2442 else
2443 cur->name = NULL;
2444 if (nota->PublicID != NULL)
2445 cur->PublicID = xmlStrdup(nota->PublicID);
2446 else
2447 cur->PublicID = NULL;
2448 if (nota->SystemID != NULL)
2449 cur->SystemID = xmlStrdup(nota->SystemID);
2450 else
2451 cur->SystemID = NULL;
2452 return(cur);
2453}
2454
2455/**
2456 * xmlCopyNotationTable:
2457 * @table: A notation table
2458 *
2459 * Build a copy of a notation table.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002460 *
Owen Taylor3473f882001-02-23 17:55:21 +00002461 * Returns the new xmlNotationTablePtr or NULL in case of error.
2462 */
2463xmlNotationTablePtr
2464xmlCopyNotationTable(xmlNotationTablePtr table) {
2465 return((xmlNotationTablePtr) xmlHashCopy(table,
2466 (xmlHashCopier) xmlCopyNotation));
2467}
Daniel Veillard652327a2003-09-29 18:02:38 +00002468#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002469
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002470#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002471/**
2472 * xmlDumpNotationDecl:
2473 * @buf: the XML buffer output
2474 * @nota: A notation declaration
2475 *
2476 * This will dump the content the notation declaration as an XML DTD definition
2477 */
2478void
2479xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002480 if ((buf == NULL) || (nota == NULL))
2481 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002482 xmlBufferWriteChar(buf, "<!NOTATION ");
2483 xmlBufferWriteCHAR(buf, nota->name);
2484 if (nota->PublicID != NULL) {
2485 xmlBufferWriteChar(buf, " PUBLIC ");
2486 xmlBufferWriteQuotedString(buf, nota->PublicID);
2487 if (nota->SystemID != NULL) {
2488 xmlBufferWriteChar(buf, " ");
Daniel Veillard41c4a752004-09-08 20:55:38 +00002489 xmlBufferWriteQuotedString(buf, nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002490 }
2491 } else {
2492 xmlBufferWriteChar(buf, " SYSTEM ");
Daniel Veillard41c4a752004-09-08 20:55:38 +00002493 xmlBufferWriteQuotedString(buf, nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002494 }
2495 xmlBufferWriteChar(buf, " >\n");
2496}
2497
2498/**
William M. Brack9e660592003-10-20 14:56:06 +00002499 * xmlDumpNotationDeclScan:
2500 * @nota: A notation declaration
2501 * @buf: the XML buffer output
2502 *
2503 * This is called with the hash scan function, and just reverses args
2504 */
2505static void
2506xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) {
2507 xmlDumpNotationDecl(buf, nota);
2508}
2509
2510/**
Owen Taylor3473f882001-02-23 17:55:21 +00002511 * xmlDumpNotationTable:
2512 * @buf: the XML buffer output
2513 * @table: A notation table
2514 *
2515 * This will dump the content of the notation table as an XML DTD definition
2516 */
2517void
2518xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
Daniel Veillardce682bc2004-11-05 17:22:25 +00002519 if ((buf == NULL) || (table == NULL))
2520 return;
William M. Brack9e660592003-10-20 14:56:06 +00002521 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002522}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002523#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002524
2525/************************************************************************
2526 * *
2527 * IDs *
2528 * *
2529 ************************************************************************/
2530/**
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002531 * DICT_FREE:
2532 * @str: a string
2533 *
2534 * Free a string if it is not owned by the "dict" dictionnary in the
2535 * current scope
2536 */
2537#define DICT_FREE(str) \
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002538 if ((str) && ((!dict) || \
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002539 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
2540 xmlFree((char *)(str));
2541
2542/**
Owen Taylor3473f882001-02-23 17:55:21 +00002543 * xmlFreeID:
2544 * @not: A id
2545 *
2546 * Deallocate the memory used by an id definition
2547 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002548static void
Owen Taylor3473f882001-02-23 17:55:21 +00002549xmlFreeID(xmlIDPtr id) {
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002550 xmlDictPtr dict = NULL;
2551
Owen Taylor3473f882001-02-23 17:55:21 +00002552 if (id == NULL) return;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002553
2554 if (id->doc != NULL)
2555 dict = id->doc->dict;
2556
Owen Taylor3473f882001-02-23 17:55:21 +00002557 if (id->value != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002558 DICT_FREE(id->value)
Daniel Veillardea7751d2002-12-20 00:16:24 +00002559 if (id->name != NULL)
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002560 DICT_FREE(id->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002561 xmlFree(id);
2562}
2563
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002564
Owen Taylor3473f882001-02-23 17:55:21 +00002565/**
2566 * xmlAddID:
2567 * @ctxt: the validation context
2568 * @doc: pointer to the document
2569 * @value: the value name
2570 * @attr: the attribute holding the ID
2571 *
2572 * Register a new id declaration
2573 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002574 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002575 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002576xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002577xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2578 xmlAttrPtr attr) {
2579 xmlIDPtr ret;
2580 xmlIDTablePtr table;
2581
2582 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002583 return(NULL);
2584 }
2585 if (value == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002586 return(NULL);
2587 }
2588 if (attr == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002589 return(NULL);
2590 }
2591
2592 /*
2593 * Create the ID table if needed.
2594 */
2595 table = (xmlIDTablePtr) doc->ids;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002596 if (table == NULL) {
2597 doc->ids = table = xmlHashCreateDict(0, doc->dict);
2598 }
Owen Taylor3473f882001-02-23 17:55:21 +00002599 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002600 xmlVErrMemory(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00002601 "xmlAddID: Table creation failed!\n");
2602 return(NULL);
2603 }
2604
2605 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2606 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002607 xmlVErrMemory(ctxt, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002608 return(NULL);
2609 }
2610
2611 /*
2612 * fill the structure.
2613 */
2614 ret->value = xmlStrdup(value);
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002615 ret->doc = doc;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002616 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2617 /*
2618 * Operating in streaming mode, attr is gonna disapear
2619 */
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002620 if (doc->dict != NULL)
2621 ret->name = xmlDictLookup(doc->dict, attr->name, -1);
2622 else
2623 ret->name = xmlStrdup(attr->name);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002624 ret->attr = NULL;
2625 } else {
2626 ret->attr = attr;
2627 ret->name = NULL;
2628 }
2629 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002630
2631 if (xmlHashAddEntry(table, value, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002632#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002633 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002634 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002635 */
Daniel Veillarda16eb962014-06-10 16:06:14 +08002636 xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2637 "ID %s already defined\n", value, NULL, NULL);
Daniel Veillard4432df22003-09-28 18:58:27 +00002638#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002639 xmlFreeID(ret);
2640 return(NULL);
2641 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002642 if (attr != NULL)
2643 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002644 return(ret);
2645}
2646
2647/**
2648 * xmlFreeIDTable:
2649 * @table: An id table
2650 *
2651 * Deallocate the memory used by an ID hash table.
2652 */
2653void
2654xmlFreeIDTable(xmlIDTablePtr table) {
2655 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2656}
2657
2658/**
2659 * xmlIsID:
2660 * @doc: the document
2661 * @elem: the element carrying the attribute
2662 * @attr: the attribute
2663 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002664 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002665 * then this is done if DTD loading has been requested. In the case
2666 * of HTML documents parsed with the HTML parser, then ID detection is
2667 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002668 *
2669 * Returns 0 or 1 depending on the lookup result
2670 */
2671int
2672xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002673 if ((attr == NULL) || (attr->name == NULL)) return(0);
2674 if ((attr->ns != NULL) && (attr->ns->prefix != NULL) &&
2675 (!strcmp((char *) attr->name, "id")) &&
2676 (!strcmp((char *) attr->ns->prefix, "xml")))
2677 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00002678 if (doc == NULL) return(0);
Daniel Veillardf3c06692009-10-16 16:47:58 +02002679 if ((doc->intSubset == NULL) && (doc->extSubset == NULL) &&
2680 (doc->type != XML_HTML_DOCUMENT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002681 return(0);
2682 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
Rob Richards04bffc02006-03-03 16:44:37 +00002683 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2684 ((xmlStrEqual(BAD_CAST "name", attr->name)) &&
Daniel Veillard38431c32007-06-12 16:20:09 +00002685 ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a")))))
Owen Taylor3473f882001-02-23 17:55:21 +00002686 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002687 return(0);
Daniel Veillard379a3b72005-08-12 10:18:14 +00002688 } else if (elem == NULL) {
2689 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002690 } else {
Daniel Veillard465a0002005-08-22 12:07:04 +00002691 xmlAttributePtr attrDecl = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002692
Daniel Veillard379a3b72005-08-12 10:18:14 +00002693 xmlChar felem[50], fattr[50];
2694 xmlChar *fullelemname, *fullattrname;
2695
2696 fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ?
2697 xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) :
2698 (xmlChar *)elem->name;
2699
2700 fullattrname = (attr->ns != NULL && attr->ns->prefix != NULL) ?
2701 xmlBuildQName(attr->name, attr->ns->prefix, fattr, 50) :
2702 (xmlChar *)attr->name;
2703
2704 if (fullelemname != NULL && fullattrname != NULL) {
2705 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullelemname,
2706 fullattrname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002707 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillard379a3b72005-08-12 10:18:14 +00002708 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullelemname,
2709 fullattrname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002710 }
Owen Taylor3473f882001-02-23 17:55:21 +00002711
Daniel Veillard379a3b72005-08-12 10:18:14 +00002712 if ((fullattrname != fattr) && (fullattrname != attr->name))
2713 xmlFree(fullattrname);
2714 if ((fullelemname != felem) && (fullelemname != elem->name))
2715 xmlFree(fullelemname);
2716
Owen Taylor3473f882001-02-23 17:55:21 +00002717 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2718 return(1);
2719 }
2720 return(0);
2721}
2722
2723/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002724 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002725 * @doc: the document
2726 * @attr: the attribute
2727 *
2728 * Remove the given attribute from the ID table maintained internally.
2729 *
2730 * Returns -1 if the lookup failed and 0 otherwise
2731 */
2732int
2733xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
Owen Taylor3473f882001-02-23 17:55:21 +00002734 xmlIDTablePtr table;
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002735 xmlIDPtr id;
Owen Taylor3473f882001-02-23 17:55:21 +00002736 xmlChar *ID;
2737
2738 if (doc == NULL) return(-1);
2739 if (attr == NULL) return(-1);
Denis Pauk01461792013-08-06 09:55:55 +03002740
Owen Taylor3473f882001-02-23 17:55:21 +00002741 table = (xmlIDTablePtr) doc->ids;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002742 if (table == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002743 return(-1);
2744
Owen Taylor3473f882001-02-23 17:55:21 +00002745 ID = xmlNodeListGetString(doc, attr->children, 1);
2746 if (ID == NULL)
Denis Pauk01461792013-08-06 09:55:55 +03002747 return(-1);
2748
Daniel Veillard8d7b5c72003-11-15 18:24:36 +00002749 id = xmlHashLookup(table, ID);
2750 if (id == NULL || id->attr != attr) {
Denis Pauk01461792013-08-06 09:55:55 +03002751 xmlFree(ID);
2752 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002753 }
Denis Pauk01461792013-08-06 09:55:55 +03002754
Daniel Veillard91b955c2004-12-10 10:26:42 +00002755 xmlHashRemoveEntry(table, ID, (xmlHashDeallocator) xmlFreeID);
Owen Taylor3473f882001-02-23 17:55:21 +00002756 xmlFree(ID);
Denis Pauk01461792013-08-06 09:55:55 +03002757 attr->atype = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002758 return(0);
2759}
2760
2761/**
2762 * xmlGetID:
2763 * @doc: pointer to the document
2764 * @ID: the ID value
2765 *
2766 * Search the attribute declaring the given ID
2767 *
2768 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2769 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002770xmlAttrPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002771xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2772 xmlIDTablePtr table;
2773 xmlIDPtr id;
2774
2775 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002776 return(NULL);
2777 }
2778
2779 if (ID == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002780 return(NULL);
2781 }
2782
2783 table = (xmlIDTablePtr) doc->ids;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002784 if (table == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002785 return(NULL);
2786
2787 id = xmlHashLookup(table, ID);
2788 if (id == NULL)
2789 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002790 if (id->attr == NULL) {
2791 /*
2792 * We are operating on a stream, return a well known reference
2793 * since the attribute node doesn't exist anymore
2794 */
2795 return((xmlAttrPtr) doc);
2796 }
Owen Taylor3473f882001-02-23 17:55:21 +00002797 return(id->attr);
2798}
2799
2800/************************************************************************
2801 * *
2802 * Refs *
2803 * *
2804 ************************************************************************/
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002805typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002806{
2807 xmlListPtr l;
2808 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002809} xmlRemoveMemo;
2810
2811typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2812
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002813typedef struct xmlValidateMemo_t
Daniel Veillard8730c562001-02-26 10:49:57 +00002814{
2815 xmlValidCtxtPtr ctxt;
2816 const xmlChar *name;
2817} xmlValidateMemo;
2818
2819typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002820
2821/**
Owen Taylor3473f882001-02-23 17:55:21 +00002822 * xmlFreeRef:
2823 * @lk: A list link
2824 *
2825 * Deallocate the memory used by a ref definition
2826 */
2827static void
2828xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002829 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2830 if (ref == NULL) return;
2831 if (ref->value != NULL)
2832 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002833 if (ref->name != NULL)
2834 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002835 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002836}
2837
2838/**
2839 * xmlFreeRefList:
2840 * @list_ref: A list of references.
2841 *
2842 * Deallocate the memory used by a list of references
2843 */
2844static void
2845xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002846 if (list_ref == NULL) return;
2847 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002848}
2849
2850/**
2851 * xmlWalkRemoveRef:
2852 * @data: Contents of current link
2853 * @user: Value supplied by the user
2854 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002855 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002856 */
2857static int
2858xmlWalkRemoveRef(const void *data, const void *user)
2859{
Daniel Veillard37721922001-05-04 15:21:12 +00002860 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2861 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2862 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002863
Daniel Veillard37721922001-05-04 15:21:12 +00002864 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2865 xmlListRemoveFirst(ref_list, (void *)data);
2866 return 0;
2867 }
2868 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002869}
2870
2871/**
Daniel Veillard770075b2004-02-25 10:44:30 +00002872 * xmlDummyCompare
2873 * @data0: Value supplied by the user
2874 * @data1: Value supplied by the user
2875 *
2876 * Do nothing, return 0. Used to create unordered lists.
2877 */
2878static int
Daniel Veillardf54cd532004-02-25 11:52:31 +00002879xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2880 const void *data1 ATTRIBUTE_UNUSED)
Daniel Veillard770075b2004-02-25 10:44:30 +00002881{
2882 return (0);
2883}
2884
2885/**
Owen Taylor3473f882001-02-23 17:55:21 +00002886 * xmlAddRef:
2887 * @ctxt: the validation context
2888 * @doc: pointer to the document
2889 * @value: the value name
2890 * @attr: the attribute holding the Ref
2891 *
2892 * Register a new ref declaration
2893 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002894 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002895 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002896xmlRefPtr
William M. Brackedb65a72004-02-06 07:36:04 +00002897xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002898 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002899 xmlRefPtr ret;
2900 xmlRefTablePtr table;
2901 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002902
Daniel Veillard37721922001-05-04 15:21:12 +00002903 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002904 return(NULL);
2905 }
2906 if (value == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002907 return(NULL);
2908 }
2909 if (attr == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00002910 return(NULL);
2911 }
Owen Taylor3473f882001-02-23 17:55:21 +00002912
Daniel Veillard37721922001-05-04 15:21:12 +00002913 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002914 * Create the Ref table if needed.
2915 */
Daniel Veillard37721922001-05-04 15:21:12 +00002916 table = (xmlRefTablePtr) doc->refs;
Daniel Veillard316a5c32005-01-23 22:56:39 +00002917 if (table == NULL) {
2918 doc->refs = table = xmlHashCreateDict(0, doc->dict);
2919 }
Daniel Veillard37721922001-05-04 15:21:12 +00002920 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002921 xmlVErrMemory(ctxt,
Daniel Veillard37721922001-05-04 15:21:12 +00002922 "xmlAddRef: Table creation failed!\n");
2923 return(NULL);
2924 }
Owen Taylor3473f882001-02-23 17:55:21 +00002925
Daniel Veillard37721922001-05-04 15:21:12 +00002926 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2927 if (ret == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002928 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard37721922001-05-04 15:21:12 +00002929 return(NULL);
2930 }
Owen Taylor3473f882001-02-23 17:55:21 +00002931
Daniel Veillard37721922001-05-04 15:21:12 +00002932 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002933 * fill the structure.
2934 */
Daniel Veillard37721922001-05-04 15:21:12 +00002935 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002936 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2937 /*
2938 * Operating in streaming mode, attr is gonna disapear
2939 */
2940 ret->name = xmlStrdup(attr->name);
2941 ret->attr = NULL;
2942 } else {
2943 ret->name = NULL;
2944 ret->attr = attr;
2945 }
2946 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002947
Daniel Veillard37721922001-05-04 15:21:12 +00002948 /* To add a reference :-
2949 * References are maintained as a list of references,
2950 * Lookup the entry, if no entry create new nodelist
2951 * Add the owning node to the NodeList
2952 * Return the ref
2953 */
Owen Taylor3473f882001-02-23 17:55:21 +00002954
Daniel Veillard37721922001-05-04 15:21:12 +00002955 if (NULL == (ref_list = xmlHashLookup(table, value))) {
Daniel Veillard770075b2004-02-25 10:44:30 +00002956 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002957 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2958 "xmlAddRef: Reference list creation failed!\n",
2959 NULL);
Daniel Veillardf6cf57a2007-05-09 23:53:30 +00002960 goto failed;
Daniel Veillard37721922001-05-04 15:21:12 +00002961 }
2962 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2963 xmlListDelete(ref_list);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002964 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2965 "xmlAddRef: Reference list insertion failed!\n",
2966 NULL);
Daniel Veillardf6cf57a2007-05-09 23:53:30 +00002967 goto failed;
Daniel Veillard37721922001-05-04 15:21:12 +00002968 }
2969 }
Daniel Veillardf6cf57a2007-05-09 23:53:30 +00002970 if (xmlListAppend(ref_list, ret) != 0) {
2971 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2972 "xmlAddRef: Reference list insertion failed!\n",
2973 NULL);
2974 goto failed;
2975 }
Daniel Veillard37721922001-05-04 15:21:12 +00002976 return(ret);
Daniel Veillardf6cf57a2007-05-09 23:53:30 +00002977failed:
2978 if (ret != NULL) {
2979 if (ret->value != NULL)
2980 xmlFree((char *)ret->value);
2981 if (ret->name != NULL)
2982 xmlFree((char *)ret->name);
2983 xmlFree(ret);
2984 }
2985 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002986}
2987
2988/**
2989 * xmlFreeRefTable:
2990 * @table: An ref table
2991 *
2992 * Deallocate the memory used by an Ref hash table.
2993 */
2994void
2995xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002996 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002997}
2998
2999/**
3000 * xmlIsRef:
3001 * @doc: the document
3002 * @elem: the element carrying the attribute
3003 * @attr: the attribute
3004 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003005 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00003006 * then this is simple, otherwise we use an heuristic: name Ref (upper
3007 * or lowercase).
3008 *
3009 * Returns 0 or 1 depending on the lookup result
3010 */
3011int
3012xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillardce244ad2004-11-05 10:03:46 +00003013 if (attr == NULL)
3014 return(0);
3015 if (doc == NULL) {
3016 doc = attr->doc;
3017 if (doc == NULL) return(0);
3018 }
3019
Daniel Veillard37721922001-05-04 15:21:12 +00003020 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
3021 return(0);
3022 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
3023 /* TODO @@@ */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003024 return(0);
Daniel Veillard37721922001-05-04 15:21:12 +00003025 } else {
3026 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00003027
Daniel Veillardce244ad2004-11-05 10:03:46 +00003028 if (elem == NULL) return(0);
Daniel Veillard37721922001-05-04 15:21:12 +00003029 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
3030 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3031 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3032 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003033
Daniel Veillard37721922001-05-04 15:21:12 +00003034 if ((attrDecl != NULL) &&
3035 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
3036 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
3037 return(1);
3038 }
3039 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003040}
3041
3042/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003043 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00003044 * @doc: the document
3045 * @attr: the attribute
3046 *
3047 * Remove the given attribute from the Ref table maintained internally.
3048 *
3049 * Returns -1 if the lookup failed and 0 otherwise
3050 */
3051int
3052xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00003053 xmlListPtr ref_list;
3054 xmlRefTablePtr table;
3055 xmlChar *ID;
3056 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00003057
Daniel Veillard37721922001-05-04 15:21:12 +00003058 if (doc == NULL) return(-1);
3059 if (attr == NULL) return(-1);
Denis Pauk01461792013-08-06 09:55:55 +03003060
Daniel Veillard37721922001-05-04 15:21:12 +00003061 table = (xmlRefTablePtr) doc->refs;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003062 if (table == NULL)
Daniel Veillard37721922001-05-04 15:21:12 +00003063 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003064
Daniel Veillard37721922001-05-04 15:21:12 +00003065 ID = xmlNodeListGetString(doc, attr->children, 1);
3066 if (ID == NULL)
3067 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003068
Denis Pauk01461792013-08-06 09:55:55 +03003069 ref_list = xmlHashLookup(table, ID);
Daniel Veillard37721922001-05-04 15:21:12 +00003070 if(ref_list == NULL) {
3071 xmlFree(ID);
3072 return (-1);
3073 }
Denis Pauk01461792013-08-06 09:55:55 +03003074
Daniel Veillard37721922001-05-04 15:21:12 +00003075 /* At this point, ref_list refers to a list of references which
3076 * have the same key as the supplied attr. Our list of references
3077 * is ordered by reference address and we don't have that information
3078 * here to use when removing. We'll have to walk the list and
3079 * check for a matching attribute, when we find one stop the walk
3080 * and remove the entry.
3081 * The list is ordered by reference, so that means we don't have the
3082 * key. Passing the list and the reference to the walker means we
3083 * will have enough data to be able to remove the entry.
3084 */
3085 target.l = ref_list;
3086 target.ap = attr;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003087
Daniel Veillard37721922001-05-04 15:21:12 +00003088 /* Remove the supplied attr from our list */
3089 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00003090
Daniel Veillard37721922001-05-04 15:21:12 +00003091 /*If the list is empty then remove the list entry in the hash */
3092 if (xmlListEmpty(ref_list))
3093 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
3094 xmlFreeRefList);
3095 xmlFree(ID);
3096 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003097}
3098
3099/**
3100 * xmlGetRefs:
3101 * @doc: pointer to the document
3102 * @ID: the ID value
3103 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003104 * Find the set of references for the supplied ID.
Owen Taylor3473f882001-02-23 17:55:21 +00003105 *
3106 * Returns NULL if not found, otherwise node set for the ID.
3107 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003108xmlListPtr
Owen Taylor3473f882001-02-23 17:55:21 +00003109xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00003110 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00003111
Daniel Veillard37721922001-05-04 15:21:12 +00003112 if (doc == NULL) {
Daniel Veillard37721922001-05-04 15:21:12 +00003113 return(NULL);
3114 }
Owen Taylor3473f882001-02-23 17:55:21 +00003115
Daniel Veillard37721922001-05-04 15:21:12 +00003116 if (ID == 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 table = (xmlRefTablePtr) doc->refs;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003121 if (table == NULL)
Daniel Veillard37721922001-05-04 15:21:12 +00003122 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003123
Daniel Veillard37721922001-05-04 15:21:12 +00003124 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00003125}
3126
3127/************************************************************************
3128 * *
3129 * Routines for validity checking *
3130 * *
3131 ************************************************************************/
3132
3133/**
3134 * xmlGetDtdElementDesc:
3135 * @dtd: a pointer to the DtD to search
3136 * @name: the element name
3137 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003138 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003139 *
3140 * returns the xmlElementPtr if found or NULL
3141 */
3142
3143xmlElementPtr
3144xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
3145 xmlElementTablePtr table;
3146 xmlElementPtr cur;
3147 xmlChar *uqname = NULL, *prefix = NULL;
3148
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003149 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003150 if (dtd->elements == NULL)
3151 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003152 table = (xmlElementTablePtr) dtd->elements;
3153
3154 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003155 if (uqname != NULL)
3156 name = uqname;
3157 cur = xmlHashLookup2(table, name, prefix);
3158 if (prefix != NULL) xmlFree(prefix);
3159 if (uqname != NULL) xmlFree(uqname);
3160 return(cur);
3161}
3162/**
3163 * xmlGetDtdElementDesc2:
3164 * @dtd: a pointer to the DtD to search
3165 * @name: the element name
3166 * @create: create an empty description if not found
3167 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003168 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00003169 *
3170 * returns the xmlElementPtr if found or NULL
3171 */
3172
Daniel Veillard86fd5a72001-12-13 14:55:21 +00003173static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00003174xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
3175 xmlElementTablePtr table;
3176 xmlElementPtr cur;
3177 xmlChar *uqname = NULL, *prefix = NULL;
3178
3179 if (dtd == NULL) return(NULL);
3180 if (dtd->elements == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003181 xmlDictPtr dict = NULL;
3182
3183 if (dtd->doc != NULL)
3184 dict = dtd->doc->dict;
3185
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003186 if (!create)
Daniel Veillarda10efa82001-04-18 13:09:01 +00003187 return(NULL);
3188 /*
3189 * Create the Element table if needed.
3190 */
3191 table = (xmlElementTablePtr) dtd->elements;
3192 if (table == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +00003193 table = xmlHashCreateDict(0, dict);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003194 dtd->elements = (void *) table;
3195 }
3196 if (table == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003197 xmlVErrMemory(NULL, "element table allocation failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003198 return(NULL);
3199 }
3200 }
3201 table = (xmlElementTablePtr) dtd->elements;
3202
3203 uqname = xmlSplitQName2(name, &prefix);
3204 if (uqname != NULL)
3205 name = uqname;
3206 cur = xmlHashLookup2(table, name, prefix);
3207 if ((cur == NULL) && (create)) {
3208 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
3209 if (cur == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00003210 xmlVErrMemory(NULL, "malloc failed");
Daniel Veillarda10efa82001-04-18 13:09:01 +00003211 return(NULL);
3212 }
3213 memset(cur, 0, sizeof(xmlElement));
3214 cur->type = XML_ELEMENT_DECL;
3215
3216 /*
3217 * fill the structure.
3218 */
3219 cur->name = xmlStrdup(name);
3220 cur->prefix = xmlStrdup(prefix);
3221 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3222
3223 xmlHashAddEntry2(table, name, prefix, cur);
3224 }
3225 if (prefix != NULL) xmlFree(prefix);
3226 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00003227 return(cur);
3228}
3229
3230/**
3231 * xmlGetDtdQElementDesc:
3232 * @dtd: a pointer to the DtD to search
3233 * @name: the element name
3234 * @prefix: the element namespace prefix
3235 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003236 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00003237 *
3238 * returns the xmlElementPtr if found or NULL
3239 */
3240
Daniel Veillard48da9102001-08-07 01:10:10 +00003241xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00003242xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3243 const xmlChar *prefix) {
3244 xmlElementTablePtr table;
3245
3246 if (dtd == NULL) return(NULL);
3247 if (dtd->elements == NULL) return(NULL);
3248 table = (xmlElementTablePtr) dtd->elements;
3249
3250 return(xmlHashLookup2(table, name, prefix));
3251}
3252
3253/**
3254 * xmlGetDtdAttrDesc:
3255 * @dtd: a pointer to the DtD to search
3256 * @elem: the element name
3257 * @name: the attribute name
3258 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003259 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003260 * this element.
3261 *
3262 * returns the xmlAttributePtr if found or NULL
3263 */
3264
3265xmlAttributePtr
3266xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3267 xmlAttributeTablePtr table;
3268 xmlAttributePtr cur;
3269 xmlChar *uqname = NULL, *prefix = NULL;
3270
3271 if (dtd == NULL) return(NULL);
3272 if (dtd->attributes == NULL) return(NULL);
3273
3274 table = (xmlAttributeTablePtr) dtd->attributes;
3275 if (table == NULL)
3276 return(NULL);
3277
3278 uqname = xmlSplitQName2(name, &prefix);
3279
3280 if (uqname != NULL) {
3281 cur = xmlHashLookup3(table, uqname, prefix, elem);
3282 if (prefix != NULL) xmlFree(prefix);
3283 if (uqname != NULL) xmlFree(uqname);
3284 } else
3285 cur = xmlHashLookup3(table, name, NULL, elem);
3286 return(cur);
3287}
3288
3289/**
3290 * xmlGetDtdQAttrDesc:
3291 * @dtd: a pointer to the DtD to search
3292 * @elem: the element name
3293 * @name: the attribute name
3294 * @prefix: the attribute namespace prefix
3295 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003296 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00003297 * this element.
3298 *
3299 * returns the xmlAttributePtr if found or NULL
3300 */
3301
Daniel Veillard48da9102001-08-07 01:10:10 +00003302xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00003303xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3304 const xmlChar *prefix) {
3305 xmlAttributeTablePtr table;
3306
3307 if (dtd == NULL) return(NULL);
3308 if (dtd->attributes == NULL) return(NULL);
3309 table = (xmlAttributeTablePtr) dtd->attributes;
3310
3311 return(xmlHashLookup3(table, name, prefix, elem));
3312}
3313
3314/**
3315 * xmlGetDtdNotationDesc:
3316 * @dtd: a pointer to the DtD to search
3317 * @name: the notation name
3318 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003319 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00003320 *
3321 * returns the xmlNotationPtr if found or NULL
3322 */
3323
3324xmlNotationPtr
3325xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3326 xmlNotationTablePtr table;
3327
3328 if (dtd == NULL) return(NULL);
3329 if (dtd->notations == NULL) return(NULL);
3330 table = (xmlNotationTablePtr) dtd->notations;
3331
3332 return(xmlHashLookup(table, name));
3333}
3334
Daniel Veillardf54cd532004-02-25 11:52:31 +00003335#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Owen Taylor3473f882001-02-23 17:55:21 +00003336/**
3337 * xmlValidateNotationUse:
3338 * @ctxt: the validation context
3339 * @doc: the document
3340 * @notationName: the notation name to check
3341 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003342 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00003343 * - [ VC: Notation Declared ]
3344 *
3345 * returns 1 if valid or 0 otherwise
3346 */
3347
3348int
3349xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3350 const xmlChar *notationName) {
3351 xmlNotationPtr notaDecl;
Daniel Veillardeab3ac92009-08-12 10:39:29 +02003352 if ((doc == NULL) || (doc->intSubset == NULL) ||
3353 (notationName == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003354
3355 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3356 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3357 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3358
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003359 if ((notaDecl == NULL) && (ctxt != NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003360 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3361 "NOTATION %s is not declared\n",
3362 notationName, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003363 return(0);
3364 }
3365 return(1);
3366}
Daniel Veillardf54cd532004-02-25 11:52:31 +00003367#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003368
3369/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003370 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00003371 * @doc: the document
3372 * @name: the element name
3373 *
3374 * Search in the DtDs whether an element accept Mixed content (or ANY)
3375 * basically if it is supposed to accept text childs
3376 *
3377 * returns 0 if no, 1 if yes, and -1 if no element description is available
3378 */
3379
3380int
3381xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3382 xmlElementPtr elemDecl;
3383
3384 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3385
3386 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3387 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3388 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3389 if (elemDecl == NULL) return(-1);
3390 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00003391 case XML_ELEMENT_TYPE_UNDEFINED:
3392 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003393 case XML_ELEMENT_TYPE_ELEMENT:
3394 return(0);
3395 case XML_ELEMENT_TYPE_EMPTY:
3396 /*
3397 * return 1 for EMPTY since we want VC error to pop up
3398 * on <empty> </empty> for example
3399 */
3400 case XML_ELEMENT_TYPE_ANY:
3401 case XML_ELEMENT_TYPE_MIXED:
3402 return(1);
3403 }
3404 return(1);
3405}
3406
Daniel Veillard4432df22003-09-28 18:58:27 +00003407#ifdef LIBXML_VALID_ENABLED
Daniel Veillardae0765b2008-07-31 19:54:59 +00003408
3409static int
3410xmlIsDocNameStartChar(xmlDocPtr doc, int c) {
3411 if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3412 /*
3413 * Use the new checks of production [4] [4a] amd [5] of the
3414 * Update 5 of XML-1.0
3415 */
3416 if (((c >= 'a') && (c <= 'z')) ||
3417 ((c >= 'A') && (c <= 'Z')) ||
3418 (c == '_') || (c == ':') ||
3419 ((c >= 0xC0) && (c <= 0xD6)) ||
3420 ((c >= 0xD8) && (c <= 0xF6)) ||
3421 ((c >= 0xF8) && (c <= 0x2FF)) ||
3422 ((c >= 0x370) && (c <= 0x37D)) ||
3423 ((c >= 0x37F) && (c <= 0x1FFF)) ||
3424 ((c >= 0x200C) && (c <= 0x200D)) ||
3425 ((c >= 0x2070) && (c <= 0x218F)) ||
3426 ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3427 ((c >= 0x3001) && (c <= 0xD7FF)) ||
3428 ((c >= 0xF900) && (c <= 0xFDCF)) ||
3429 ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3430 ((c >= 0x10000) && (c <= 0xEFFFF)))
3431 return(1);
3432 } else {
3433 if (IS_LETTER(c) || (c == '_') || (c == ':'))
3434 return(1);
3435 }
3436 return(0);
3437}
3438
3439static int
3440xmlIsDocNameChar(xmlDocPtr doc, int c) {
3441 if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3442 /*
3443 * Use the new checks of production [4] [4a] amd [5] of the
3444 * Update 5 of XML-1.0
3445 */
3446 if (((c >= 'a') && (c <= 'z')) ||
3447 ((c >= 'A') && (c <= 'Z')) ||
3448 ((c >= '0') && (c <= '9')) || /* !start */
3449 (c == '_') || (c == ':') ||
3450 (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
3451 ((c >= 0xC0) && (c <= 0xD6)) ||
3452 ((c >= 0xD8) && (c <= 0xF6)) ||
3453 ((c >= 0xF8) && (c <= 0x2FF)) ||
3454 ((c >= 0x300) && (c <= 0x36F)) || /* !start */
3455 ((c >= 0x370) && (c <= 0x37D)) ||
3456 ((c >= 0x37F) && (c <= 0x1FFF)) ||
3457 ((c >= 0x200C) && (c <= 0x200D)) ||
3458 ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
3459 ((c >= 0x2070) && (c <= 0x218F)) ||
3460 ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3461 ((c >= 0x3001) && (c <= 0xD7FF)) ||
3462 ((c >= 0xF900) && (c <= 0xFDCF)) ||
3463 ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3464 ((c >= 0x10000) && (c <= 0xEFFFF)))
3465 return(1);
3466 } else {
3467 if ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
3468 (c == '.') || (c == '-') ||
3469 (c == '_') || (c == ':') ||
3470 (IS_COMBINING(c)) ||
3471 (IS_EXTENDER(c)))
3472 return(1);
3473 }
3474 return(0);
3475}
3476
3477/**
3478 * xmlValidateNameValue:
3479 * @doc: pointer to the document or NULL
3480 * @value: an Name value
3481 *
3482 * Validate that the given value match Name production
3483 *
3484 * returns 1 if valid or 0 otherwise
3485 */
3486
3487static int
3488xmlValidateNameValueInternal(xmlDocPtr doc, const xmlChar *value) {
3489 const xmlChar *cur;
3490 int val, len;
3491
3492 if (value == NULL) return(0);
3493 cur = value;
3494 val = xmlStringCurrentChar(NULL, cur, &len);
3495 cur += len;
3496 if (!xmlIsDocNameStartChar(doc, val))
3497 return(0);
3498
3499 val = xmlStringCurrentChar(NULL, cur, &len);
3500 cur += len;
3501 while (xmlIsDocNameChar(doc, val)) {
3502 val = xmlStringCurrentChar(NULL, cur, &len);
3503 cur += len;
3504 }
3505
3506 if (val != 0) return(0);
3507
3508 return(1);
3509}
3510
Owen Taylor3473f882001-02-23 17:55:21 +00003511/**
3512 * xmlValidateNameValue:
3513 * @value: an Name value
3514 *
3515 * Validate that the given value match Name production
3516 *
3517 * returns 1 if valid or 0 otherwise
3518 */
3519
Daniel Veillard9b731d72002-04-14 12:56:08 +00003520int
Owen Taylor3473f882001-02-23 17:55:21 +00003521xmlValidateNameValue(const xmlChar *value) {
Daniel Veillardae0765b2008-07-31 19:54:59 +00003522 return(xmlValidateNameValueInternal(NULL, value));
3523}
3524
3525/**
3526 * xmlValidateNamesValueInternal:
3527 * @doc: pointer to the document or NULL
3528 * @value: an Names value
3529 *
3530 * Validate that the given value match Names production
3531 *
3532 * returns 1 if valid or 0 otherwise
3533 */
3534
3535static int
3536xmlValidateNamesValueInternal(xmlDocPtr doc, const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00003537 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003538 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003539
3540 if (value == NULL) return(0);
3541 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003542 val = xmlStringCurrentChar(NULL, cur, &len);
3543 cur += len;
Daniel Veillardae0765b2008-07-31 19:54:59 +00003544
3545 if (!xmlIsDocNameStartChar(doc, val))
Owen Taylor3473f882001-02-23 17:55:21 +00003546 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003547
Daniel Veillardd8224e02002-01-13 15:43:22 +00003548 val = xmlStringCurrentChar(NULL, cur, &len);
3549 cur += len;
Daniel Veillardae0765b2008-07-31 19:54:59 +00003550 while (xmlIsDocNameChar(doc, val)) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003551 val = xmlStringCurrentChar(NULL, cur, &len);
3552 cur += len;
3553 }
Owen Taylor3473f882001-02-23 17:55:21 +00003554
Daniel Veillardae0765b2008-07-31 19:54:59 +00003555 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3556 while (val == 0x20) {
3557 while (val == 0x20) {
3558 val = xmlStringCurrentChar(NULL, cur, &len);
3559 cur += len;
3560 }
3561
3562 if (!xmlIsDocNameStartChar(doc, val))
3563 return(0);
3564
3565 val = xmlStringCurrentChar(NULL, cur, &len);
3566 cur += len;
3567
3568 while (xmlIsDocNameChar(doc, val)) {
3569 val = xmlStringCurrentChar(NULL, cur, &len);
3570 cur += len;
3571 }
3572 }
3573
Daniel Veillardd8224e02002-01-13 15:43:22 +00003574 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003575
3576 return(1);
3577}
3578
3579/**
3580 * xmlValidateNamesValue:
3581 * @value: an Names value
3582 *
3583 * Validate that the given value match Names production
3584 *
3585 * returns 1 if valid or 0 otherwise
3586 */
3587
Daniel Veillard9b731d72002-04-14 12:56:08 +00003588int
Owen Taylor3473f882001-02-23 17:55:21 +00003589xmlValidateNamesValue(const xmlChar *value) {
Daniel Veillardae0765b2008-07-31 19:54:59 +00003590 return(xmlValidateNamesValueInternal(NULL, value));
3591}
3592
3593/**
3594 * xmlValidateNmtokenValueInternal:
3595 * @doc: pointer to the document or NULL
3596 * @value: an Nmtoken value
3597 *
3598 * Validate that the given value match Nmtoken production
3599 *
3600 * [ VC: Name Token ]
3601 *
3602 * returns 1 if valid or 0 otherwise
3603 */
3604
3605static int
3606xmlValidateNmtokenValueInternal(xmlDocPtr doc, const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00003607 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003608 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003609
3610 if (value == NULL) return(0);
3611 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003612 val = xmlStringCurrentChar(NULL, cur, &len);
3613 cur += len;
Daniel Veillardae0765b2008-07-31 19:54:59 +00003614
3615 if (!xmlIsDocNameChar(doc, val))
Owen Taylor3473f882001-02-23 17:55:21 +00003616 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003617
Daniel Veillardd8224e02002-01-13 15:43:22 +00003618 val = xmlStringCurrentChar(NULL, cur, &len);
3619 cur += len;
Daniel Veillardae0765b2008-07-31 19:54:59 +00003620 while (xmlIsDocNameChar(doc, val)) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003621 val = xmlStringCurrentChar(NULL, cur, &len);
3622 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003623 }
3624
Daniel Veillardd8224e02002-01-13 15:43:22 +00003625 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003626
3627 return(1);
3628}
3629
3630/**
3631 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003632 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003633 *
3634 * Validate that the given value match Nmtoken production
3635 *
3636 * [ VC: Name Token ]
Daniel Veillardae0765b2008-07-31 19:54:59 +00003637 *
Owen Taylor3473f882001-02-23 17:55:21 +00003638 * returns 1 if valid or 0 otherwise
3639 */
3640
Daniel Veillard9b731d72002-04-14 12:56:08 +00003641int
Owen Taylor3473f882001-02-23 17:55:21 +00003642xmlValidateNmtokenValue(const xmlChar *value) {
Daniel Veillardae0765b2008-07-31 19:54:59 +00003643 return(xmlValidateNmtokenValueInternal(NULL, value));
3644}
3645
3646/**
3647 * xmlValidateNmtokensValueInternal:
3648 * @doc: pointer to the document or NULL
3649 * @value: an Nmtokens value
3650 *
3651 * Validate that the given value match Nmtokens production
3652 *
3653 * [ VC: Name Token ]
3654 *
3655 * returns 1 if valid or 0 otherwise
3656 */
3657
3658static int
3659xmlValidateNmtokensValueInternal(xmlDocPtr doc, const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00003660 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003661 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003662
3663 if (value == NULL) return(0);
3664 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003665 val = xmlStringCurrentChar(NULL, cur, &len);
3666 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003667
Daniel Veillardae0765b2008-07-31 19:54:59 +00003668 while (IS_BLANK(val)) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00003669 val = xmlStringCurrentChar(NULL, cur, &len);
3670 cur += len;
3671 }
Owen Taylor3473f882001-02-23 17:55:21 +00003672
Daniel Veillardae0765b2008-07-31 19:54:59 +00003673 if (!xmlIsDocNameChar(doc, val))
3674 return(0);
3675
3676 while (xmlIsDocNameChar(doc, val)) {
3677 val = xmlStringCurrentChar(NULL, cur, &len);
3678 cur += len;
3679 }
3680
3681 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3682 while (val == 0x20) {
3683 while (val == 0x20) {
3684 val = xmlStringCurrentChar(NULL, cur, &len);
3685 cur += len;
3686 }
3687 if (val == 0) return(1);
3688
3689 if (!xmlIsDocNameChar(doc, val))
3690 return(0);
3691
3692 val = xmlStringCurrentChar(NULL, cur, &len);
3693 cur += len;
3694
3695 while (xmlIsDocNameChar(doc, val)) {
3696 val = xmlStringCurrentChar(NULL, cur, &len);
3697 cur += len;
3698 }
3699 }
3700
Daniel Veillardd8224e02002-01-13 15:43:22 +00003701 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003702
3703 return(1);
3704}
3705
3706/**
3707 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003708 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003709 *
3710 * Validate that the given value match Nmtokens production
3711 *
3712 * [ VC: Name Token ]
Daniel Veillardae0765b2008-07-31 19:54:59 +00003713 *
Owen Taylor3473f882001-02-23 17:55:21 +00003714 * returns 1 if valid or 0 otherwise
3715 */
3716
Daniel Veillard9b731d72002-04-14 12:56:08 +00003717int
Owen Taylor3473f882001-02-23 17:55:21 +00003718xmlValidateNmtokensValue(const xmlChar *value) {
Daniel Veillardae0765b2008-07-31 19:54:59 +00003719 return(xmlValidateNmtokensValueInternal(NULL, value));
Owen Taylor3473f882001-02-23 17:55:21 +00003720}
3721
3722/**
3723 * xmlValidateNotationDecl:
3724 * @ctxt: the validation context
3725 * @doc: a document instance
3726 * @nota: a notation definition
3727 *
3728 * Try to validate a single notation definition
3729 * basically it does the following checks as described by the
3730 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003731 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003732 * But this function get called anyway ...
3733 *
3734 * returns 1 if valid or 0 otherwise
3735 */
3736
3737int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003738xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3739 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003740 int ret = 1;
3741
3742 return(ret);
3743}
3744
3745/**
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003746 * xmlValidateAttributeValueInternal:
3747 * @doc: the document
Owen Taylor3473f882001-02-23 17:55:21 +00003748 * @type: an attribute type
3749 * @value: an attribute value
3750 *
3751 * Validate that the given attribute value match the proper production
3752 *
Owen Taylor3473f882001-02-23 17:55:21 +00003753 * returns 1 if valid or 0 otherwise
3754 */
3755
Daniel Veillardae0765b2008-07-31 19:54:59 +00003756static int
3757xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
3758 const xmlChar *value) {
Owen Taylor3473f882001-02-23 17:55:21 +00003759 switch (type) {
3760 case XML_ATTRIBUTE_ENTITIES:
3761 case XML_ATTRIBUTE_IDREFS:
Daniel Veillardae0765b2008-07-31 19:54:59 +00003762 return(xmlValidateNamesValueInternal(doc, value));
Owen Taylor3473f882001-02-23 17:55:21 +00003763 case XML_ATTRIBUTE_ENTITY:
3764 case XML_ATTRIBUTE_IDREF:
3765 case XML_ATTRIBUTE_ID:
3766 case XML_ATTRIBUTE_NOTATION:
Daniel Veillardae0765b2008-07-31 19:54:59 +00003767 return(xmlValidateNameValueInternal(doc, value));
Owen Taylor3473f882001-02-23 17:55:21 +00003768 case XML_ATTRIBUTE_NMTOKENS:
3769 case XML_ATTRIBUTE_ENUMERATION:
Daniel Veillardae0765b2008-07-31 19:54:59 +00003770 return(xmlValidateNmtokensValueInternal(doc, value));
Owen Taylor3473f882001-02-23 17:55:21 +00003771 case XML_ATTRIBUTE_NMTOKEN:
Daniel Veillardae0765b2008-07-31 19:54:59 +00003772 return(xmlValidateNmtokenValueInternal(doc, value));
Owen Taylor3473f882001-02-23 17:55:21 +00003773 case XML_ATTRIBUTE_CDATA:
3774 break;
3775 }
3776 return(1);
3777}
3778
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003779/**
3780 * xmlValidateAttributeValue:
3781 * @type: an attribute type
3782 * @value: an attribute value
3783 *
3784 * Validate that the given attribute value match the proper production
3785 *
3786 * [ VC: ID ]
3787 * Values of type ID must match the Name production....
3788 *
3789 * [ VC: IDREF ]
3790 * Values of type IDREF must match the Name production, and values
3791 * of type IDREFS must match Names ...
3792 *
3793 * [ VC: Entity Name ]
3794 * Values of type ENTITY must match the Name production, values
3795 * of type ENTITIES must match Names ...
3796 *
3797 * [ VC: Name Token ]
3798 * Values of type NMTOKEN must match the Nmtoken production; values
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003799 * of type NMTOKENS must match Nmtokens.
Daniel Veillardbe2bd6a2008-11-27 15:26:28 +00003800 *
3801 * returns 1 if valid or 0 otherwise
3802 */
Daniel Veillardae0765b2008-07-31 19:54:59 +00003803int
3804xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3805 return(xmlValidateAttributeValueInternal(NULL, type, value));
3806}
3807
Owen Taylor3473f882001-02-23 17:55:21 +00003808/**
3809 * xmlValidateAttributeValue2:
3810 * @ctxt: the validation context
3811 * @doc: the document
3812 * @name: the attribute name (used for error reporting only)
3813 * @type: the attribute type
3814 * @value: the attribute value
3815 *
3816 * Validate that the given attribute value match a given type.
3817 * This typically cannot be done before having finished parsing
3818 * the subsets.
3819 *
3820 * [ VC: IDREF ]
3821 * Values of type IDREF must match one of the declared IDs
3822 * Values of type IDREFS must match a sequence of the declared IDs
3823 * each Name must match the value of an ID attribute on some element
3824 * in the XML document; i.e. IDREF values must match the value of
3825 * some ID attribute
3826 *
3827 * [ VC: Entity Name ]
3828 * Values of type ENTITY must match one declared entity
3829 * Values of type ENTITIES must match a sequence of declared entities
3830 *
3831 * [ VC: Notation Attributes ]
3832 * all notation names in the declaration must be declared.
3833 *
3834 * returns 1 if valid or 0 otherwise
3835 */
3836
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003837static int
Owen Taylor3473f882001-02-23 17:55:21 +00003838xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3839 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3840 int ret = 1;
3841 switch (type) {
3842 case XML_ATTRIBUTE_IDREFS:
3843 case XML_ATTRIBUTE_IDREF:
3844 case XML_ATTRIBUTE_ID:
3845 case XML_ATTRIBUTE_NMTOKENS:
3846 case XML_ATTRIBUTE_ENUMERATION:
3847 case XML_ATTRIBUTE_NMTOKEN:
3848 case XML_ATTRIBUTE_CDATA:
3849 break;
3850 case XML_ATTRIBUTE_ENTITY: {
3851 xmlEntityPtr ent;
3852
3853 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003854 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003855 if ((ent == NULL) && (doc->standalone == 1)) {
3856 doc->standalone = 0;
3857 ent = xmlGetDocEntity(doc, value);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003858 }
Owen Taylor3473f882001-02-23 17:55:21 +00003859 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003860 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3861 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003862 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003863 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003864 ret = 0;
3865 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003866 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3867 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003868 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003869 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003870 ret = 0;
3871 }
3872 break;
3873 }
3874 case XML_ATTRIBUTE_ENTITIES: {
3875 xmlChar *dup, *nam = NULL, *cur, save;
3876 xmlEntityPtr ent;
3877
3878 dup = xmlStrdup(value);
3879 if (dup == NULL)
3880 return(0);
3881 cur = dup;
3882 while (*cur != 0) {
3883 nam = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00003884 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003885 save = *cur;
3886 *cur = 0;
3887 ent = xmlGetDocEntity(doc, nam);
3888 if (ent == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003889 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3890 XML_DTD_UNKNOWN_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00003891 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003892 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003893 ret = 0;
3894 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003895 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3896 XML_DTD_ENTITY_TYPE,
Owen Taylor3473f882001-02-23 17:55:21 +00003897 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003898 name, nam, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003899 ret = 0;
3900 }
3901 if (save == 0)
3902 break;
3903 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00003904 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00003905 }
3906 xmlFree(dup);
3907 break;
3908 }
3909 case XML_ATTRIBUTE_NOTATION: {
3910 xmlNotationPtr nota;
3911
3912 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3913 if ((nota == NULL) && (doc->extSubset != NULL))
3914 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3915
3916 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003917 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3918 XML_DTD_UNKNOWN_NOTATION,
Owen Taylor3473f882001-02-23 17:55:21 +00003919 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00003920 name, value, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003921 ret = 0;
3922 }
3923 break;
3924 }
3925 }
3926 return(ret);
3927}
3928
3929/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003930 * xmlValidCtxtNormalizeAttributeValue:
3931 * @ctxt: the validation context
3932 * @doc: the document
3933 * @elem: the parent
3934 * @name: the attribute name
3935 * @value: the attribute value
3936 * @ctxt: the validation context or NULL
3937 *
3938 * Does the validation related extra step of the normalization of attribute
3939 * values:
3940 *
3941 * If the declared value is not CDATA, then the XML processor must further
3942 * process the normalized attribute value by discarding any leading and
3943 * trailing space (#x20) characters, and by replacing sequences of space
3944 * (#x20) characters by single space (#x20) character.
3945 *
3946 * Also check VC: Standalone Document Declaration in P32, and update
3947 * ctxt->valid accordingly
3948 *
3949 * returns a new normalized string if normalization is needed, NULL otherwise
3950 * the caller must free the returned value.
3951 */
3952
3953xmlChar *
3954xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3955 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3956 xmlChar *ret, *dst;
3957 const xmlChar *src;
3958 xmlAttributePtr attrDecl = NULL;
3959 int extsubset = 0;
3960
3961 if (doc == NULL) return(NULL);
3962 if (elem == NULL) return(NULL);
3963 if (name == NULL) return(NULL);
3964 if (value == NULL) return(NULL);
3965
3966 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003967 xmlChar fn[50];
3968 xmlChar *fullname;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003969
Daniel Veillardc00cda82003-04-07 10:22:39 +00003970 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3971 if (fullname == NULL)
Daniel Veillard24505b02005-07-28 23:49:35 +00003972 return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00003973 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003974 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003975 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003976 if (attrDecl != NULL)
3977 extsubset = 1;
3978 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003979 if ((fullname != fn) && (fullname != elem->name))
3980 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003981 }
3982 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3983 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3984 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3985 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3986 if (attrDecl != NULL)
3987 extsubset = 1;
3988 }
3989
3990 if (attrDecl == NULL)
3991 return(NULL);
3992 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3993 return(NULL);
3994
3995 ret = xmlStrdup(value);
3996 if (ret == NULL)
3997 return(NULL);
3998 src = value;
3999 dst = ret;
4000 while (*src == 0x20) src++;
4001 while (*src != 0) {
4002 if (*src == 0x20) {
4003 while (*src == 0x20) src++;
4004 if (*src != 0)
4005 *dst++ = 0x20;
4006 } else {
4007 *dst++ = *src++;
4008 }
4009 }
4010 *dst = 0;
4011 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004012 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004013"standalone: %s on %s value had to be normalized based on external subset declaration\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004014 name, elem->name, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004015 ctxt->valid = 0;
4016 }
4017 return(ret);
4018}
4019
4020/**
Owen Taylor3473f882001-02-23 17:55:21 +00004021 * xmlValidNormalizeAttributeValue:
4022 * @doc: the document
4023 * @elem: the parent
4024 * @name: the attribute name
4025 * @value: the attribute value
4026 *
4027 * Does the validation related extra step of the normalization of attribute
4028 * values:
4029 *
4030 * If the declared value is not CDATA, then the XML processor must further
4031 * process the normalized attribute value by discarding any leading and
4032 * trailing space (#x20) characters, and by replacing sequences of space
4033 * (#x20) characters by single space (#x20) character.
4034 *
Daniel Veillard652327a2003-09-29 18:02:38 +00004035 * Returns a new normalized string if normalization is needed, NULL otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00004036 * the caller must free the returned value.
4037 */
4038
4039xmlChar *
4040xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
4041 const xmlChar *name, const xmlChar *value) {
4042 xmlChar *ret, *dst;
4043 const xmlChar *src;
4044 xmlAttributePtr attrDecl = NULL;
4045
4046 if (doc == NULL) return(NULL);
4047 if (elem == NULL) return(NULL);
4048 if (name == NULL) return(NULL);
4049 if (value == NULL) return(NULL);
4050
4051 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004052 xmlChar fn[50];
4053 xmlChar *fullname;
Daniel Veillard594e5df2009-09-07 14:58:47 +02004054
Daniel Veillardc00cda82003-04-07 10:22:39 +00004055 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4056 if (fullname == NULL)
Daniel Veillard24505b02005-07-28 23:49:35 +00004057 return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00004058 if ((fullname != fn) && (fullname != elem->name))
4059 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00004060 }
4061 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
4062 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4063 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
4064
4065 if (attrDecl == NULL)
4066 return(NULL);
4067 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
4068 return(NULL);
4069
4070 ret = xmlStrdup(value);
4071 if (ret == NULL)
4072 return(NULL);
4073 src = value;
4074 dst = ret;
4075 while (*src == 0x20) src++;
4076 while (*src != 0) {
4077 if (*src == 0x20) {
4078 while (*src == 0x20) src++;
4079 if (*src != 0)
4080 *dst++ = 0x20;
4081 } else {
4082 *dst++ = *src++;
4083 }
4084 }
4085 *dst = 0;
4086 return(ret);
4087}
4088
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004089static void
Owen Taylor3473f882001-02-23 17:55:21 +00004090xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004091 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004092 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
4093}
4094
4095/**
4096 * xmlValidateAttributeDecl:
4097 * @ctxt: the validation context
4098 * @doc: a document instance
4099 * @attr: an attribute definition
4100 *
4101 * Try to validate a single attribute definition
4102 * basically it does the following checks as described by the
4103 * XML-1.0 recommendation:
4104 * - [ VC: Attribute Default Legal ]
4105 * - [ VC: Enumeration ]
4106 * - [ VC: ID Attribute Default ]
4107 *
4108 * The ID/IDREF uniqueness and matching are done separately
4109 *
4110 * returns 1 if valid or 0 otherwise
4111 */
4112
4113int
4114xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4115 xmlAttributePtr attr) {
4116 int ret = 1;
4117 int val;
4118 CHECK_DTD;
4119 if(attr == NULL) return(1);
Daniel Veillardae0765b2008-07-31 19:54:59 +00004120
Owen Taylor3473f882001-02-23 17:55:21 +00004121 /* Attribute Default Legal */
4122 /* Enumeration */
4123 if (attr->defaultValue != NULL) {
Daniel Veillardae0765b2008-07-31 19:54:59 +00004124 val = xmlValidateAttributeValueInternal(doc, attr->atype,
4125 attr->defaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00004126 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004127 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004128 "Syntax of default value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004129 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004130 }
4131 ret &= val;
4132 }
4133
4134 /* ID Attribute Default */
4135 if ((attr->atype == XML_ATTRIBUTE_ID)&&
4136 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
4137 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004138 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004139 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004140 attr->name, attr->elem, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004141 ret = 0;
4142 }
4143
4144 /* One ID per Element Type */
4145 if (attr->atype == XML_ATTRIBUTE_ID) {
4146 int nbId;
4147
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004148 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00004149 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
4150 attr->elem);
4151 if (elem != NULL) {
Daniel Veillarddbee0f12005-06-27 13:42:57 +00004152 nbId = xmlScanIDAttributeDecl(NULL, elem, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00004153 } else {
4154 xmlAttributeTablePtr table;
4155
4156 /*
4157 * The attribute may be declared in the internal subset and the
4158 * element in the external subset.
4159 */
4160 nbId = 0;
Daniel Veillard11ce4002006-03-10 00:36:23 +00004161 if (doc->intSubset != NULL) {
4162 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
4163 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
4164 xmlValidateAttributeIdCallback, &nbId);
4165 }
Owen Taylor3473f882001-02-23 17:55:21 +00004166 }
4167 if (nbId > 1) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004168
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004169 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004170 "Element %s has %d ID attribute defined in the internal subset : %s\n",
4171 attr->elem, nbId, attr->name);
4172 } else if (doc->extSubset != NULL) {
4173 int extId = 0;
4174 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
4175 if (elem != NULL) {
Daniel Veillarddbee0f12005-06-27 13:42:57 +00004176 extId = xmlScanIDAttributeDecl(NULL, elem, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00004177 }
4178 if (extId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004179 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004180 "Element %s has %d ID attribute defined in the external subset : %s\n",
4181 attr->elem, extId, attr->name);
4182 } else if (extId + nbId > 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004183 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
Owen Taylor3473f882001-02-23 17:55:21 +00004184"Element %s has ID attributes defined in the internal and external subset : %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004185 attr->elem, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004186 }
4187 }
4188 }
4189
4190 /* Validity Constraint: Enumeration */
4191 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
4192 xmlEnumerationPtr tree = attr->tree;
4193 while (tree != NULL) {
4194 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
4195 tree = tree->next;
4196 }
4197 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004198 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004199"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004200 attr->defaultValue, attr->name, attr->elem);
4201 ret = 0;
4202 }
4203 }
4204
4205 return(ret);
4206}
4207
4208/**
4209 * xmlValidateElementDecl:
4210 * @ctxt: the validation context
4211 * @doc: a document instance
4212 * @elem: an element definition
4213 *
4214 * Try to validate a single element definition
4215 * basically it does the following checks as described by the
4216 * XML-1.0 recommendation:
4217 * - [ VC: One ID per Element Type ]
4218 * - [ VC: No Duplicate Types ]
4219 * - [ VC: Unique Element Type Declaration ]
4220 *
4221 * returns 1 if valid or 0 otherwise
4222 */
4223
4224int
4225xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4226 xmlElementPtr elem) {
4227 int ret = 1;
4228 xmlElementPtr tst;
4229
4230 CHECK_DTD;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004231
Owen Taylor3473f882001-02-23 17:55:21 +00004232 if (elem == NULL) return(1);
4233
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004234#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00004235#ifdef LIBXML_REGEXP_ENABLED
4236 /* Build the regexp associated to the content model */
4237 ret = xmlValidBuildContentModel(ctxt, elem);
4238#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004239#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00004240
Owen Taylor3473f882001-02-23 17:55:21 +00004241 /* No Duplicate Types */
4242 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
4243 xmlElementContentPtr cur, next;
4244 const xmlChar *name;
4245
4246 cur = elem->content;
4247 while (cur != NULL) {
4248 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
4249 if (cur->c1 == NULL) break;
4250 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4251 name = cur->c1->name;
4252 next = cur->c2;
4253 while (next != NULL) {
4254 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00004255 if ((xmlStrEqual(next->name, name)) &&
Daniel Veillarda7216122009-08-21 18:22:58 +02004256 (xmlStrEqual(next->prefix, cur->c1->prefix))) {
4257 if (cur->c1->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004258 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00004259 "Definition of %s has duplicate references of %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004260 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004261 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004262 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004263 "Definition of %s has duplicate references of %s:%s\n",
Daniel Veillarda7216122009-08-21 18:22:58 +02004264 elem->name, cur->c1->prefix, name);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004265 }
Owen Taylor3473f882001-02-23 17:55:21 +00004266 ret = 0;
4267 }
4268 break;
4269 }
4270 if (next->c1 == NULL) break;
4271 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00004272 if ((xmlStrEqual(next->c1->name, name)) &&
Daniel Veillarda7216122009-08-21 18:22:58 +02004273 (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) {
4274 if (cur->c1->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004275 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004276 "Definition of %s has duplicate references to %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004277 elem->name, name, NULL);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004278 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004279 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
Daniel Veillard7b68df92003-08-03 22:58:54 +00004280 "Definition of %s has duplicate references to %s:%s\n",
Daniel Veillarda7216122009-08-21 18:22:58 +02004281 elem->name, cur->c1->prefix, name);
Daniel Veillard7b68df92003-08-03 22:58:54 +00004282 }
Owen Taylor3473f882001-02-23 17:55:21 +00004283 ret = 0;
4284 }
4285 next = next->c2;
4286 }
4287 }
4288 cur = cur->c2;
4289 }
4290 }
4291
4292 /* VC: Unique Element Type Declaration */
4293 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004294 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004295 ((tst->prefix == elem->prefix) ||
4296 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004297 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004298 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4299 "Redefinition of element %s\n",
4300 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004301 ret = 0;
4302 }
4303 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004304 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004305 ((tst->prefix == elem->prefix) ||
4306 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00004307 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004308 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4309 "Redefinition of element %s\n",
4310 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004311 ret = 0;
4312 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00004313 /* One ID per Element Type
4314 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00004315 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4316 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00004317 } */
Owen Taylor3473f882001-02-23 17:55:21 +00004318 return(ret);
4319}
4320
4321/**
4322 * xmlValidateOneAttribute:
4323 * @ctxt: the validation context
4324 * @doc: a document instance
4325 * @elem: an element instance
4326 * @attr: an attribute instance
4327 * @value: the attribute value (without entities processing)
4328 *
4329 * Try to validate a single attribute for an element
4330 * basically it does the following checks as described by the
4331 * XML-1.0 recommendation:
4332 * - [ VC: Attribute Value Type ]
4333 * - [ VC: Fixed Attribute Default ]
4334 * - [ VC: Entity Name ]
4335 * - [ VC: Name Token ]
4336 * - [ VC: ID ]
4337 * - [ VC: IDREF ]
4338 * - [ VC: Entity Name ]
4339 * - [ VC: Notation Attributes ]
4340 *
4341 * The ID/IDREF uniqueness and matching are done separately
4342 *
4343 * returns 1 if valid or 0 otherwise
4344 */
4345
4346int
4347xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004348 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
Daniel Veillard07cb8222003-09-10 10:51:05 +00004349{
Owen Taylor3473f882001-02-23 17:55:21 +00004350 xmlAttributePtr attrDecl = NULL;
4351 int val;
4352 int ret = 1;
4353
4354 CHECK_DTD;
4355 if ((elem == NULL) || (elem->name == NULL)) return(0);
4356 if ((attr == NULL) || (attr->name == NULL)) return(0);
4357
4358 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004359 xmlChar fn[50];
4360 xmlChar *fullname;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004361
Daniel Veillardc00cda82003-04-07 10:22:39 +00004362 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4363 if (fullname == NULL)
4364 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004365 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004366 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004367 attr->name, attr->ns->prefix);
4368 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004369 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00004370 attr->name, attr->ns->prefix);
4371 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004372 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004373 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4374 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00004375 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004376 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004377 if ((fullname != fn) && (fullname != elem->name))
4378 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00004379 }
4380 if (attrDecl == NULL) {
4381 if (attr->ns != NULL) {
4382 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4383 attr->name, attr->ns->prefix);
4384 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4385 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4386 attr->name, attr->ns->prefix);
4387 } else {
4388 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4389 elem->name, attr->name);
4390 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4391 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4392 elem->name, attr->name);
4393 }
4394 }
4395
4396
4397 /* Validity Constraint: Attribute Value Type */
4398 if (attrDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004399 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004400 "No declaration for attribute %s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004401 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004402 return(0);
4403 }
4404 attr->atype = attrDecl->atype;
4405
Daniel Veillardae0765b2008-07-31 19:54:59 +00004406 val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
Owen Taylor3473f882001-02-23 17:55:21 +00004407 if (val == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004408 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004409 "Syntax of value for attribute %s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004410 attr->name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004411 ret = 0;
4412 }
4413
4414 /* Validity constraint: Fixed Attribute Default */
4415 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4416 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004417 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004418 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004419 attr->name, elem->name, attrDecl->defaultValue);
4420 ret = 0;
4421 }
4422 }
4423
4424 /* Validity Constraint: ID uniqueness */
4425 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4426 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4427 ret = 0;
4428 }
4429
4430 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4431 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4432 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4433 ret = 0;
4434 }
4435
4436 /* Validity Constraint: Notation Attributes */
4437 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4438 xmlEnumerationPtr tree = attrDecl->tree;
4439 xmlNotationPtr nota;
4440
4441 /* First check that the given NOTATION was declared */
4442 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4443 if (nota == NULL)
4444 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004445
Owen Taylor3473f882001-02-23 17:55:21 +00004446 if (nota == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004447 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004448 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004449 value, attr->name, elem->name);
4450 ret = 0;
4451 }
4452
4453 /* Second, verify that it's among the list */
4454 while (tree != NULL) {
4455 if (xmlStrEqual(tree->name, value)) break;
4456 tree = tree->next;
4457 }
4458 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004459 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004460"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004461 value, attr->name, elem->name);
4462 ret = 0;
4463 }
4464 }
4465
4466 /* Validity Constraint: Enumeration */
4467 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4468 xmlEnumerationPtr tree = attrDecl->tree;
4469 while (tree != NULL) {
4470 if (xmlStrEqual(tree->name, value)) break;
4471 tree = tree->next;
4472 }
4473 if (tree == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004474 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004475 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004476 value, attr->name, elem->name);
4477 ret = 0;
4478 }
4479 }
4480
4481 /* Fixed Attribute Default */
4482 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4483 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004484 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004485 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004486 attr->name, elem->name, attrDecl->defaultValue);
4487 ret = 0;
4488 }
4489
4490 /* Extra check for the attribute value */
4491 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4492 attrDecl->atype, value);
4493
4494 return(ret);
4495}
4496
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004497/**
4498 * xmlValidateOneNamespace:
4499 * @ctxt: the validation context
4500 * @doc: a document instance
4501 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004502 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004503 * @ns: an namespace declaration instance
4504 * @value: the attribute value (without entities processing)
4505 *
4506 * Try to validate a single namespace declaration for an element
4507 * basically it does the following checks as described by the
4508 * XML-1.0 recommendation:
4509 * - [ VC: Attribute Value Type ]
4510 * - [ VC: Fixed Attribute Default ]
4511 * - [ VC: Entity Name ]
4512 * - [ VC: Name Token ]
4513 * - [ VC: ID ]
4514 * - [ VC: IDREF ]
4515 * - [ VC: Entity Name ]
4516 * - [ VC: Notation Attributes ]
4517 *
4518 * The ID/IDREF uniqueness and matching are done separately
4519 *
4520 * returns 1 if valid or 0 otherwise
4521 */
4522
4523int
4524xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4525xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4526 /* xmlElementPtr elemDecl; */
4527 xmlAttributePtr attrDecl = NULL;
4528 int val;
4529 int ret = 1;
4530
4531 CHECK_DTD;
4532 if ((elem == NULL) || (elem->name == NULL)) return(0);
4533 if ((ns == NULL) || (ns->href == NULL)) return(0);
4534
4535 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004536 xmlChar fn[50];
4537 xmlChar *fullname;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004538
Daniel Veillardc00cda82003-04-07 10:22:39 +00004539 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4540 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00004541 xmlVErrMemory(ctxt, "Validating namespace");
Daniel Veillardc00cda82003-04-07 10:22:39 +00004542 return(0);
4543 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004544 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004545 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004546 ns->prefix, BAD_CAST "xmlns");
4547 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004548 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004549 ns->prefix, BAD_CAST "xmlns");
4550 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004551 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004552 BAD_CAST "xmlns");
4553 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004554 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004555 BAD_CAST "xmlns");
4556 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004557 if ((fullname != fn) && (fullname != elem->name))
4558 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004559 }
4560 if (attrDecl == NULL) {
4561 if (ns->prefix != NULL) {
4562 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4563 ns->prefix, BAD_CAST "xmlns");
4564 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4565 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4566 ns->prefix, BAD_CAST "xmlns");
4567 } else {
4568 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4569 elem->name, BAD_CAST "xmlns");
4570 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4571 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4572 elem->name, BAD_CAST "xmlns");
4573 }
4574 }
4575
4576
4577 /* Validity Constraint: Attribute Value Type */
4578 if (attrDecl == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004579 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004580 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004581 "No declaration for attribute xmlns:%s of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004582 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004583 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004584 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004585 "No declaration for attribute xmlns of element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004586 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004587 }
4588 return(0);
4589 }
4590
Daniel Veillardae0765b2008-07-31 19:54:59 +00004591 val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004592 if (val == 0) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004593 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004594 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004595 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004596 ns->prefix, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004597 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004598 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004599 "Syntax of value for attribute xmlns of %s is not valid\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004600 elem->name, NULL, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004601 }
4602 ret = 0;
4603 }
4604
4605 /* Validity constraint: Fixed Attribute Default */
4606 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4607 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004608 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004609 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004610 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4611 ns->prefix, elem->name, attrDecl->defaultValue);
4612 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004613 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004614 "Value for attribute xmlns of %s is different from default \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004615 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004616 }
4617 ret = 0;
4618 }
4619 }
4620
4621 /* Validity Constraint: ID uniqueness */
4622 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4623 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4624 ret = 0;
4625 }
4626
4627 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4628 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4629 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4630 ret = 0;
4631 }
4632
4633 /* Validity Constraint: Notation Attributes */
4634 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4635 xmlEnumerationPtr tree = attrDecl->tree;
4636 xmlNotationPtr nota;
4637
4638 /* First check that the given NOTATION was declared */
4639 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4640 if (nota == NULL)
4641 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004642
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004643 if (nota == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004644 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004645 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004646 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4647 value, ns->prefix, elem->name);
4648 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004649 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004650 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004651 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004652 }
4653 ret = 0;
4654 }
4655
4656 /* Second, verify that it's among the list */
4657 while (tree != NULL) {
4658 if (xmlStrEqual(tree->name, value)) break;
4659 tree = tree->next;
4660 }
4661 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004662 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004663 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004664"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4665 value, ns->prefix, elem->name);
4666 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004667 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004668"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004669 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004670 }
4671 ret = 0;
4672 }
4673 }
4674
4675 /* Validity Constraint: Enumeration */
4676 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4677 xmlEnumerationPtr tree = attrDecl->tree;
4678 while (tree != NULL) {
4679 if (xmlStrEqual(tree->name, value)) break;
4680 tree = tree->next;
4681 }
4682 if (tree == NULL) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004683 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004684 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004685"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4686 value, ns->prefix, elem->name);
4687 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004688 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004689"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004690 value, elem->name, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004691 }
4692 ret = 0;
4693 }
4694 }
4695
4696 /* Fixed Attribute Default */
4697 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4698 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004699 if (ns->prefix != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004700 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004701 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4702 ns->prefix, elem->name, attrDecl->defaultValue);
4703 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004704 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004705 "Value for attribute xmlns of %s must be \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00004706 elem->name, attrDecl->defaultValue, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004707 }
4708 ret = 0;
4709 }
4710
4711 /* Extra check for the attribute value */
4712 if (ns->prefix != NULL) {
4713 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4714 attrDecl->atype, value);
4715 } else {
4716 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4717 attrDecl->atype, value);
4718 }
4719
4720 return(ret);
4721}
4722
Daniel Veillard118aed72002-09-24 14:13:13 +00004723#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004724/**
4725 * xmlValidateSkipIgnorable:
4726 * @ctxt: the validation context
4727 * @child: the child list
4728 *
4729 * Skip ignorable elements w.r.t. the validation process
4730 *
4731 * returns the first element to consider for validation of the content model
4732 */
4733
4734static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004735xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004736 while (child != NULL) {
4737 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004738 /* These things are ignored (skipped) during validation. */
4739 case XML_PI_NODE:
4740 case XML_COMMENT_NODE:
4741 case XML_XINCLUDE_START:
4742 case XML_XINCLUDE_END:
4743 child = child->next;
4744 break;
4745 case XML_TEXT_NODE:
4746 if (xmlIsBlankNode(child))
4747 child = child->next;
4748 else
4749 return(child);
4750 break;
4751 /* keep current node */
4752 default:
4753 return(child);
4754 }
4755 }
4756 return(child);
4757}
4758
4759/**
4760 * xmlValidateElementType:
4761 * @ctxt: the validation context
4762 *
4763 * Try to validate the content model of an element internal function
4764 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004765 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4766 * reference is found and -3 if the validation succeeded but
4767 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004768 */
4769
4770static int
4771xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004772 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004773 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004774
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004775 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004776 if ((NODE == NULL) && (CONT == NULL))
4777 return(1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004778 if ((NODE == NULL) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004779 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4780 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4781 return(1);
4782 }
4783 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004784 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004785 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004786
4787 /*
4788 * We arrive here when more states need to be examined
4789 */
4790cont:
4791
4792 /*
4793 * We just recovered from a rollback generated by a possible
4794 * epsilon transition, go directly to the analysis phase
4795 */
4796 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004797 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004798 DEBUG_VALID_STATE(NODE, CONT)
4799 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004800 goto analyze;
4801 }
4802
4803 DEBUG_VALID_STATE(NODE, CONT)
4804 /*
4805 * we may have to save a backup state here. This is the equivalent
4806 * of handling epsilon transition in NFAs.
4807 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004808 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004809 ((CONT->parent == NULL) ||
4810 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004811 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004812 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004813 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004814 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004815 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4816 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004817 }
4818
4819
4820 /*
4821 * Check first if the content matches
4822 */
4823 switch (CONT->type) {
4824 case XML_ELEMENT_CONTENT_PCDATA:
4825 if (NODE == NULL) {
4826 DEBUG_VALID_MSG("pcdata failed no node");
4827 ret = 0;
4828 break;
4829 }
4830 if (NODE->type == XML_TEXT_NODE) {
4831 DEBUG_VALID_MSG("pcdata found, skip to next");
4832 /*
4833 * go to next element in the content model
4834 * skipping ignorable elems
4835 */
4836 do {
4837 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004838 NODE = xmlValidateSkipIgnorable(NODE);
4839 if ((NODE != NULL) &&
4840 (NODE->type == XML_ENTITY_REF_NODE))
4841 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004842 } while ((NODE != NULL) &&
4843 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004844 (NODE->type != XML_TEXT_NODE) &&
4845 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004846 ret = 1;
4847 break;
4848 } else {
4849 DEBUG_VALID_MSG("pcdata failed");
4850 ret = 0;
4851 break;
4852 }
4853 break;
4854 case XML_ELEMENT_CONTENT_ELEMENT:
4855 if (NODE == NULL) {
4856 DEBUG_VALID_MSG("element failed no node");
4857 ret = 0;
4858 break;
4859 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004860 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4861 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004862 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004863 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4864 ret = (CONT->prefix == NULL);
4865 } else if (CONT->prefix == NULL) {
4866 ret = 0;
4867 } else {
4868 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4869 }
4870 }
4871 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004872 DEBUG_VALID_MSG("element found, skip to next");
4873 /*
4874 * go to next element in the content model
4875 * skipping ignorable elems
4876 */
4877 do {
4878 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004879 NODE = xmlValidateSkipIgnorable(NODE);
4880 if ((NODE != NULL) &&
4881 (NODE->type == XML_ENTITY_REF_NODE))
4882 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004883 } while ((NODE != NULL) &&
4884 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004885 (NODE->type != XML_TEXT_NODE) &&
4886 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004887 } else {
4888 DEBUG_VALID_MSG("element failed");
4889 ret = 0;
4890 break;
4891 }
4892 break;
4893 case XML_ELEMENT_CONTENT_OR:
4894 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004895 * Small optimization.
4896 */
4897 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4898 if ((NODE == NULL) ||
4899 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4900 DEPTH++;
4901 CONT = CONT->c2;
4902 goto cont;
4903 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004904 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4905 ret = (CONT->c1->prefix == NULL);
4906 } else if (CONT->c1->prefix == NULL) {
4907 ret = 0;
4908 } else {
4909 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4910 }
4911 if (ret == 0) {
4912 DEPTH++;
4913 CONT = CONT->c2;
4914 goto cont;
4915 }
Daniel Veillard85349052001-04-20 13:48:21 +00004916 }
4917
4918 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004919 * save the second branch 'or' branch
4920 */
4921 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004922 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4923 OCCURS, ROLLBACK_OR) < 0)
4924 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004925 DEPTH++;
4926 CONT = CONT->c1;
4927 goto cont;
4928 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004929 /*
4930 * Small optimization.
4931 */
4932 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4933 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4934 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4935 if ((NODE == NULL) ||
4936 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4937 DEPTH++;
4938 CONT = CONT->c2;
4939 goto cont;
4940 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004941 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4942 ret = (CONT->c1->prefix == NULL);
4943 } else if (CONT->c1->prefix == NULL) {
4944 ret = 0;
4945 } else {
4946 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4947 }
4948 if (ret == 0) {
4949 DEPTH++;
4950 CONT = CONT->c2;
4951 goto cont;
4952 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004953 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004954 DEPTH++;
4955 CONT = CONT->c1;
4956 goto cont;
4957 }
4958
4959 /*
4960 * At this point handle going up in the tree
4961 */
4962 if (ret == -1) {
4963 DEBUG_VALID_MSG("error found returning");
4964 return(ret);
4965 }
4966analyze:
4967 while (CONT != NULL) {
4968 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004969 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004970 * this level.
4971 */
4972 if (ret == 0) {
4973 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004974 xmlNodePtr cur;
4975
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004976 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004977 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004978 DEBUG_VALID_MSG("Once branch failed, rollback");
4979 if (vstateVPop(ctxt) < 0 ) {
4980 DEBUG_VALID_MSG("exhaustion, failed");
4981 return(0);
4982 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004983 if (cur != ctxt->vstate->node)
4984 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004985 goto cont;
4986 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004987 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004988 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004989 DEBUG_VALID_MSG("Plus branch failed, rollback");
4990 if (vstateVPop(ctxt) < 0 ) {
4991 DEBUG_VALID_MSG("exhaustion, failed");
4992 return(0);
4993 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004994 if (cur != ctxt->vstate->node)
4995 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004996 goto cont;
4997 }
4998 DEBUG_VALID_MSG("Plus branch found");
4999 ret = 1;
5000 break;
5001 case XML_ELEMENT_CONTENT_MULT:
5002#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00005003 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005004 DEBUG_VALID_MSG("Mult branch failed");
5005 } else {
5006 DEBUG_VALID_MSG("Mult branch found");
5007 }
5008#endif
5009 ret = 1;
5010 break;
5011 case XML_ELEMENT_CONTENT_OPT:
5012 DEBUG_VALID_MSG("Option branch failed");
5013 ret = 1;
5014 break;
5015 }
5016 } else {
5017 switch (CONT->ocur) {
5018 case XML_ELEMENT_CONTENT_OPT:
5019 DEBUG_VALID_MSG("Option branch succeeded");
5020 ret = 1;
5021 break;
5022 case XML_ELEMENT_CONTENT_ONCE:
5023 DEBUG_VALID_MSG("Once branch succeeded");
5024 ret = 1;
5025 break;
5026 case XML_ELEMENT_CONTENT_PLUS:
5027 if (STATE == ROLLBACK_PARENT) {
5028 DEBUG_VALID_MSG("Plus branch rollback");
5029 ret = 1;
5030 break;
5031 }
5032 if (NODE == NULL) {
5033 DEBUG_VALID_MSG("Plus branch exhausted");
5034 ret = 1;
5035 break;
5036 }
5037 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00005038 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005039 goto cont;
5040 case XML_ELEMENT_CONTENT_MULT:
5041 if (STATE == ROLLBACK_PARENT) {
5042 DEBUG_VALID_MSG("Mult branch rollback");
5043 ret = 1;
5044 break;
5045 }
5046 if (NODE == NULL) {
5047 DEBUG_VALID_MSG("Mult branch exhausted");
5048 ret = 1;
5049 break;
5050 }
5051 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00005052 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005053 goto cont;
5054 }
5055 }
5056 STATE = 0;
5057
5058 /*
5059 * Then act accordingly at the parent level
5060 */
Daniel Veillard5344c602001-12-31 16:37:34 +00005061 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005062 if (CONT->parent == NULL)
5063 break;
5064
5065 switch (CONT->parent->type) {
5066 case XML_ELEMENT_CONTENT_PCDATA:
5067 DEBUG_VALID_MSG("Error: parent pcdata");
5068 return(-1);
5069 case XML_ELEMENT_CONTENT_ELEMENT:
5070 DEBUG_VALID_MSG("Error: parent element");
5071 return(-1);
5072 case XML_ELEMENT_CONTENT_OR:
5073 if (ret == 1) {
5074 DEBUG_VALID_MSG("Or succeeded");
5075 CONT = CONT->parent;
5076 DEPTH--;
5077 } else {
5078 DEBUG_VALID_MSG("Or failed");
5079 CONT = CONT->parent;
5080 DEPTH--;
5081 }
5082 break;
5083 case XML_ELEMENT_CONTENT_SEQ:
5084 if (ret == 0) {
5085 DEBUG_VALID_MSG("Sequence failed");
5086 CONT = CONT->parent;
5087 DEPTH--;
5088 } else if (CONT == CONT->parent->c1) {
5089 DEBUG_VALID_MSG("Sequence testing 2nd branch");
5090 CONT = CONT->parent->c2;
5091 goto cont;
5092 } else {
5093 DEBUG_VALID_MSG("Sequence succeeded");
5094 CONT = CONT->parent;
5095 DEPTH--;
5096 }
5097 }
5098 }
5099 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005100 xmlNodePtr cur;
5101
5102 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005103 DEBUG_VALID_MSG("Failed, remaining input, rollback");
5104 if (vstateVPop(ctxt) < 0 ) {
5105 DEBUG_VALID_MSG("exhaustion, failed");
5106 return(0);
5107 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005108 if (cur != ctxt->vstate->node)
5109 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005110 goto cont;
5111 }
5112 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005113 xmlNodePtr cur;
5114
5115 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005116 DEBUG_VALID_MSG("Failure, rollback");
5117 if (vstateVPop(ctxt) < 0 ) {
5118 DEBUG_VALID_MSG("exhaustion, failed");
5119 return(0);
5120 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005121 if (cur != ctxt->vstate->node)
5122 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005123 goto cont;
5124 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005125 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005126}
Daniel Veillard23e73572002-09-19 19:56:43 +00005127#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005128
5129/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00005130 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005131 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00005132 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005133 * @content: An element
5134 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
5135 *
5136 * This will dump the list of elements to the buffer
5137 * Intended just for the debug routine
5138 */
5139static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00005140xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005141 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00005142 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005143
5144 if (node == NULL) return;
5145 if (glob) strcat(buf, "(");
5146 cur = node;
5147 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005148 len = strlen(buf);
5149 if (size - len < 50) {
5150 if ((size - len > 4) && (buf[len - 1] != '.'))
5151 strcat(buf, " ...");
5152 return;
5153 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005154 switch (cur->type) {
5155 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005156 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005157 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005158 if ((size - len > 4) && (buf[len - 1] != '.'))
5159 strcat(buf, " ...");
5160 return;
5161 }
5162 strcat(buf, (char *) cur->ns->prefix);
5163 strcat(buf, ":");
5164 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00005165 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00005166 if ((size - len > 4) && (buf[len - 1] != '.'))
5167 strcat(buf, " ...");
5168 return;
5169 }
5170 strcat(buf, (char *) cur->name);
5171 if (cur->next != NULL)
5172 strcat(buf, " ");
5173 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005174 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005175 if (xmlIsBlankNode(cur))
5176 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005177 case XML_CDATA_SECTION_NODE:
5178 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005179 strcat(buf, "CDATA");
5180 if (cur->next != NULL)
5181 strcat(buf, " ");
5182 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005183 case XML_ATTRIBUTE_NODE:
5184 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005185#ifdef LIBXML_DOCB_ENABLED
5186 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005187#endif
5188 case XML_HTML_DOCUMENT_NODE:
5189 case XML_DOCUMENT_TYPE_NODE:
5190 case XML_DOCUMENT_FRAG_NODE:
5191 case XML_NOTATION_NODE:
5192 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005193 strcat(buf, "???");
5194 if (cur->next != NULL)
5195 strcat(buf, " ");
5196 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005197 case XML_ENTITY_NODE:
5198 case XML_PI_NODE:
5199 case XML_DTD_NODE:
5200 case XML_COMMENT_NODE:
5201 case XML_ELEMENT_DECL:
5202 case XML_ATTRIBUTE_DECL:
5203 case XML_ENTITY_DECL:
5204 case XML_XINCLUDE_START:
5205 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00005206 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005207 }
5208 cur = cur->next;
5209 }
5210 if (glob) strcat(buf, ")");
5211}
5212
5213/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005214 * xmlValidateElementContent:
5215 * @ctxt: the validation context
5216 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005217 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005218 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005219 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005220 *
5221 * Try to validate the content model of an element
5222 *
5223 * returns 1 if valid or 0 if not and -1 in case of error
5224 */
5225
5226static int
5227xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005228 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00005229 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00005230#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00005231 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00005232#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00005233 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005234 xmlElementContentPtr cont;
5235 const xmlChar *name;
5236
Gauravc570b372013-09-30 10:43:47 +08005237 if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL))
Daniel Veillardbe480fb2001-11-08 23:36:42 +00005238 return(-1);
5239 cont = elemDecl->content;
5240 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005241
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005242#ifdef LIBXML_REGEXP_ENABLED
5243 /* Build the regexp associated to the content model */
5244 if (elemDecl->contModel == NULL)
5245 ret = xmlValidBuildContentModel(ctxt, elemDecl);
5246 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005247 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005248 } else {
5249 xmlRegExecCtxtPtr exec;
5250
Daniel Veillardec498e12003-02-05 11:01:50 +00005251 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
5252 return(-1);
5253 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005254 ctxt->nodeMax = 0;
5255 ctxt->nodeNr = 0;
5256 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005257 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
5258 if (exec != NULL) {
5259 cur = child;
5260 while (cur != NULL) {
5261 switch (cur->type) {
5262 case XML_ENTITY_REF_NODE:
5263 /*
5264 * Push the current node to be able to roll back
5265 * and process within the entity
5266 */
5267 if ((cur->children != NULL) &&
5268 (cur->children->children != NULL)) {
5269 nodeVPush(ctxt, cur);
5270 cur = cur->children->children;
5271 continue;
5272 }
5273 break;
5274 case XML_TEXT_NODE:
5275 if (xmlIsBlankNode(cur))
5276 break;
5277 ret = 0;
5278 goto fail;
5279 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00005280 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005281 ret = 0;
5282 goto fail;
5283 case XML_ELEMENT_NODE:
5284 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005285 xmlChar fn[50];
5286 xmlChar *fullname;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005287
Daniel Veillardc00cda82003-04-07 10:22:39 +00005288 fullname = xmlBuildQName(cur->name,
5289 cur->ns->prefix, fn, 50);
5290 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005291 ret = -1;
5292 goto fail;
5293 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005294 ret = xmlRegExecPushString(exec, fullname, NULL);
5295 if ((fullname != fn) && (fullname != cur->name))
5296 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005297 } else {
5298 ret = xmlRegExecPushString(exec, cur->name, NULL);
5299 }
5300 break;
5301 default:
5302 break;
5303 }
5304 /*
5305 * Switch to next element
5306 */
5307 cur = cur->next;
5308 while (cur == NULL) {
5309 cur = nodeVPop(ctxt);
5310 if (cur == NULL)
5311 break;
5312 cur = cur->next;
5313 }
5314 }
5315 ret = xmlRegExecPushString(exec, NULL, NULL);
5316fail:
5317 xmlRegFreeExecCtxt(exec);
5318 }
5319 }
5320#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005321 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005322 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005323 */
5324 ctxt->vstateMax = 8;
5325 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5326 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5327 if (ctxt->vstateTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005328 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005329 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005330 }
5331 /*
5332 * The first entry in the stack is reserved to the current state
5333 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00005334 ctxt->nodeMax = 0;
5335 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00005336 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005337 ctxt->vstate = &ctxt->vstateTab[0];
5338 ctxt->vstateNr = 1;
5339 CONT = cont;
5340 NODE = child;
5341 DEPTH = 0;
5342 OCCURS = 0;
5343 STATE = 0;
5344 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005345 if ((ret == -3) && (warn)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005346 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
5347 "Content model for Element %s is ambiguous\n",
Daniel Veillard0cc72772003-10-13 14:00:21 +00005348 name, NULL, NULL);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005349 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005350 /*
5351 * An entities reference appeared at this level.
5352 * Buid a minimal representation of this node content
5353 * sufficient to run the validation process on it
5354 */
5355 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005356 cur = child;
5357 while (cur != NULL) {
5358 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005359 case XML_ENTITY_REF_NODE:
5360 /*
5361 * Push the current node to be able to roll back
5362 * and process within the entity
5363 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005364 if ((cur->children != NULL) &&
5365 (cur->children->children != NULL)) {
5366 nodeVPush(ctxt, cur);
5367 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005368 continue;
5369 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00005370 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005371 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005372 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005373 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005374 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005375 case XML_CDATA_SECTION_NODE:
5376 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005377 case XML_ELEMENT_NODE:
5378 /*
5379 * Allocate a new node and minimally fills in
5380 * what's required
5381 */
5382 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5383 if (tmp == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00005384 xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005385 xmlFreeNodeList(repl);
5386 ret = -1;
5387 goto done;
5388 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005389 tmp->type = cur->type;
5390 tmp->name = cur->name;
5391 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005392 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00005393 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005394 if (repl == NULL)
5395 repl = last = tmp;
5396 else {
5397 last->next = tmp;
5398 last = tmp;
5399 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005400 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005401 /*
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00005402 * E59 spaces in CDATA does not match the
5403 * nonterminal S
5404 */
5405 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5406 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005407 break;
5408 default:
5409 break;
5410 }
5411 /*
5412 * Switch to next element
5413 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005414 cur = cur->next;
5415 while (cur == NULL) {
5416 cur = nodeVPop(ctxt);
5417 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005418 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005419 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005420 }
5421 }
5422
5423 /*
5424 * Relaunch the validation
5425 */
5426 ctxt->vstate = &ctxt->vstateTab[0];
5427 ctxt->vstateNr = 1;
5428 CONT = cont;
5429 NODE = repl;
5430 DEPTH = 0;
5431 OCCURS = 0;
5432 STATE = 0;
5433 ret = xmlValidateElementType(ctxt);
5434 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00005435#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005436 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillard6ad5c4a2006-10-11 16:43:06 +00005437 if (ctxt != NULL) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005438 char expr[5000];
5439 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005440
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005441 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005442 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005443 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00005444#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005445 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005446 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005447 else
Daniel Veillard01992e02002-10-09 10:20:30 +00005448#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005449 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005450
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005451 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005452 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5453 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5454 name, BAD_CAST expr, BAD_CAST list);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005455 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005456 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5457 "Element content does not follow the DTD, expecting %s, got %s\n",
5458 BAD_CAST expr, BAD_CAST list, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005459 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005460 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005461 if (name != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005462 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005463 "Element %s content does not follow the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005464 name, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005465 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005466 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5467 "Element content does not follow the DTD\n",
5468 NULL, NULL, NULL);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00005469 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005470 }
5471 ret = 0;
5472 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00005473 if (ret == -3)
5474 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005475
Daniel Veillard23e73572002-09-19 19:56:43 +00005476#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005477done:
5478 /*
5479 * Deallocate the copy if done, and free up the validation stack
5480 */
5481 while (repl != NULL) {
5482 tmp = repl->next;
5483 xmlFree(repl);
5484 repl = tmp;
5485 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005486 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005487 if (ctxt->vstateTab != NULL) {
5488 xmlFree(ctxt->vstateTab);
5489 ctxt->vstateTab = NULL;
5490 }
Daniel Veillard01992e02002-10-09 10:20:30 +00005491#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005492 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00005493 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00005494 if (ctxt->nodeTab != NULL) {
5495 xmlFree(ctxt->nodeTab);
5496 ctxt->nodeTab = NULL;
5497 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005498 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005499
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005500}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005501
Owen Taylor3473f882001-02-23 17:55:21 +00005502/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005503 * xmlValidateCdataElement:
5504 * @ctxt: the validation context
5505 * @doc: a document instance
5506 * @elem: an element instance
5507 *
5508 * Check that an element follows #CDATA
5509 *
5510 * returns 1 if valid or 0 otherwise
5511 */
5512static int
5513xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5514 xmlNodePtr elem) {
5515 int ret = 1;
5516 xmlNodePtr cur, child;
5517
Daniel Veillard3e62adb2012-08-09 14:24:02 +08005518 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) ||
5519 (elem->type != XML_ELEMENT_NODE))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005520 return(0);
5521
5522 child = elem->children;
5523
5524 cur = child;
5525 while (cur != NULL) {
5526 switch (cur->type) {
5527 case XML_ENTITY_REF_NODE:
5528 /*
5529 * Push the current node to be able to roll back
5530 * and process within the entity
5531 */
5532 if ((cur->children != NULL) &&
5533 (cur->children->children != NULL)) {
5534 nodeVPush(ctxt, cur);
5535 cur = cur->children->children;
5536 continue;
5537 }
5538 break;
5539 case XML_COMMENT_NODE:
5540 case XML_PI_NODE:
5541 case XML_TEXT_NODE:
5542 case XML_CDATA_SECTION_NODE:
5543 break;
5544 default:
5545 ret = 0;
5546 goto done;
5547 }
5548 /*
5549 * Switch to next element
5550 */
5551 cur = cur->next;
5552 while (cur == NULL) {
5553 cur = nodeVPop(ctxt);
5554 if (cur == NULL)
5555 break;
5556 cur = cur->next;
5557 }
5558 }
5559done:
5560 ctxt->nodeMax = 0;
5561 ctxt->nodeNr = 0;
5562 if (ctxt->nodeTab != NULL) {
5563 xmlFree(ctxt->nodeTab);
5564 ctxt->nodeTab = NULL;
5565 }
5566 return(ret);
5567}
5568
5569/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005570 * xmlValidateCheckMixed:
5571 * @ctxt: the validation context
5572 * @cont: the mixed content model
5573 * @qname: the qualified name as appearing in the serialization
5574 *
5575 * Check if the given node is part of the content model.
5576 *
5577 * Returns 1 if yes, 0 if no, -1 in case of error
5578 */
5579static int
William M. Brackedb65a72004-02-06 07:36:04 +00005580xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005581 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005582 const xmlChar *name;
5583 int plen;
5584 name = xmlSplitQName3(qname, &plen);
5585
5586 if (name == NULL) {
5587 while (cont != NULL) {
5588 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5589 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5590 return(1);
5591 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5592 (cont->c1 != NULL) &&
5593 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5594 if ((cont->c1->prefix == NULL) &&
5595 (xmlStrEqual(cont->c1->name, qname)))
5596 return(1);
5597 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5598 (cont->c1 == NULL) ||
5599 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005600 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005601 "Internal: MIXED struct corrupted\n",
5602 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005603 break;
5604 }
5605 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005606 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005607 } else {
5608 while (cont != NULL) {
5609 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5610 if ((cont->prefix != NULL) &&
5611 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5612 (xmlStrEqual(cont->name, name)))
5613 return(1);
5614 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5615 (cont->c1 != NULL) &&
5616 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5617 if ((cont->c1->prefix != NULL) &&
5618 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5619 (xmlStrEqual(cont->c1->name, name)))
5620 return(1);
5621 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5622 (cont->c1 == NULL) ||
5623 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005624 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00005625 "Internal: MIXED struct corrupted\n",
5626 NULL);
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005627 break;
5628 }
5629 cont = cont->c2;
5630 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005631 }
5632 return(0);
5633}
5634
5635/**
5636 * xmlValidGetElemDecl:
5637 * @ctxt: the validation context
5638 * @doc: a document instance
5639 * @elem: an element instance
5640 * @extsubset: pointer, (out) indicate if the declaration was found
5641 * in the external subset.
5642 *
5643 * Finds a declaration associated to an element in the document.
5644 *
5645 * returns the pointer to the declaration or NULL if not found.
5646 */
5647static xmlElementPtr
5648xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5649 xmlNodePtr elem, int *extsubset) {
5650 xmlElementPtr elemDecl = NULL;
5651 const xmlChar *prefix = NULL;
5652
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005653 if ((ctxt == NULL) || (doc == NULL) ||
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005654 (elem == NULL) || (elem->name == NULL))
5655 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005656 if (extsubset != NULL)
5657 *extsubset = 0;
5658
5659 /*
5660 * Fetch the declaration for the qualified name
5661 */
5662 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5663 prefix = elem->ns->prefix;
5664
5665 if (prefix != NULL) {
5666 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5667 elem->name, prefix);
5668 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5669 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5670 elem->name, prefix);
5671 if ((elemDecl != NULL) && (extsubset != NULL))
5672 *extsubset = 1;
5673 }
5674 }
5675
5676 /*
5677 * Fetch the declaration for the non qualified name
5678 * This is "non-strict" validation should be done on the
5679 * full QName but in that case being flexible makes sense.
5680 */
5681 if (elemDecl == NULL) {
5682 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5683 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5684 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5685 if ((elemDecl != NULL) && (extsubset != NULL))
5686 *extsubset = 1;
5687 }
5688 }
5689 if (elemDecl == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005690 xmlErrValidNode(ctxt, elem,
5691 XML_DTD_UNKNOWN_ELEM,
5692 "No declaration for element %s\n",
5693 elem->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005694 }
5695 return(elemDecl);
5696}
5697
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005698#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005699/**
5700 * xmlValidatePushElement:
5701 * @ctxt: the validation context
5702 * @doc: a document instance
5703 * @elem: an element instance
5704 * @qname: the qualified name as appearing in the serialization
5705 *
5706 * Push a new element start on the validation stack.
5707 *
5708 * returns 1 if no validation problem was found or 0 otherwise
5709 */
5710int
5711xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5712 xmlNodePtr elem, const xmlChar *qname) {
5713 int ret = 1;
5714 xmlElementPtr eDecl;
5715 int extsubset = 0;
5716
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005717 if (ctxt == NULL)
5718 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005719/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005720 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5721 xmlValidStatePtr state = ctxt->vstate;
5722 xmlElementPtr elemDecl;
5723
5724 /*
5725 * Check the new element agaisnt the content model of the new elem.
5726 */
5727 if (state->elemDecl != NULL) {
5728 elemDecl = state->elemDecl;
5729
5730 switch(elemDecl->etype) {
5731 case XML_ELEMENT_TYPE_UNDEFINED:
5732 ret = 0;
5733 break;
5734 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005735 xmlErrValidNode(ctxt, state->node,
5736 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005737 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005738 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005739 ret = 0;
5740 break;
5741 case XML_ELEMENT_TYPE_ANY:
5742 /* I don't think anything is required then */
5743 break;
5744 case XML_ELEMENT_TYPE_MIXED:
5745 /* simple case of declared as #PCDATA */
5746 if ((elemDecl->content != NULL) &&
5747 (elemDecl->content->type ==
5748 XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005749 xmlErrValidNode(ctxt, state->node,
5750 XML_DTD_NOT_PCDATA,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005751 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005752 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005753 ret = 0;
5754 } else {
5755 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5756 qname);
5757 if (ret != 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005758 xmlErrValidNode(ctxt, state->node,
5759 XML_DTD_INVALID_CHILD,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005760 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005761 qname, state->node->name, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005762 }
5763 }
5764 break;
5765 case XML_ELEMENT_TYPE_ELEMENT:
5766 /*
5767 * TODO:
5768 * VC: Standalone Document Declaration
5769 * - element types with element content, if white space
5770 * occurs directly within any instance of those types.
5771 */
5772 if (state->exec != NULL) {
5773 ret = xmlRegExecPushString(state->exec, qname, NULL);
5774 if (ret < 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005775 xmlErrValidNode(ctxt, state->node,
5776 XML_DTD_CONTENT_MODEL,
5777 "Element %s content does not follow the DTD, Misplaced %s\n",
5778 state->node->name, qname, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005779 ret = 0;
5780 } else {
5781 ret = 1;
5782 }
5783 }
5784 break;
5785 }
5786 }
5787 }
5788 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5789 vstateVPush(ctxt, eDecl, elem);
5790 return(ret);
5791}
5792
5793/**
5794 * xmlValidatePushCData:
5795 * @ctxt: the validation context
5796 * @data: some character data read
Michael Woodfb27e2c2012-09-28 08:59:33 +02005797 * @len: the length of the data
Daniel Veillardea7751d2002-12-20 00:16:24 +00005798 *
5799 * check the CData parsed for validation in the current stack
5800 *
5801 * returns 1 if no validation problem was found or 0 otherwise
5802 */
5803int
5804xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5805 int ret = 1;
5806
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005807/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005808 if (ctxt == NULL)
5809 return(0);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005810 if (len <= 0)
5811 return(ret);
5812 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5813 xmlValidStatePtr state = ctxt->vstate;
5814 xmlElementPtr elemDecl;
5815
5816 /*
5817 * Check the new element agaisnt the content model of the new elem.
5818 */
5819 if (state->elemDecl != NULL) {
5820 elemDecl = state->elemDecl;
5821
5822 switch(elemDecl->etype) {
5823 case XML_ELEMENT_TYPE_UNDEFINED:
5824 ret = 0;
5825 break;
5826 case XML_ELEMENT_TYPE_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005827 xmlErrValidNode(ctxt, state->node,
5828 XML_DTD_NOT_EMPTY,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005829 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005830 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005831 ret = 0;
5832 break;
5833 case XML_ELEMENT_TYPE_ANY:
5834 break;
5835 case XML_ELEMENT_TYPE_MIXED:
5836 break;
5837 case XML_ELEMENT_TYPE_ELEMENT:
5838 if (len > 0) {
5839 int i;
5840
5841 for (i = 0;i < len;i++) {
William M. Brack76e95df2003-10-18 16:20:14 +00005842 if (!IS_BLANK_CH(data[i])) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005843 xmlErrValidNode(ctxt, state->node,
5844 XML_DTD_CONTENT_MODEL,
5845 "Element %s content does not follow the DTD, Text not allowed\n",
5846 state->node->name, NULL, NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005847 ret = 0;
5848 goto done;
5849 }
5850 }
5851 /*
5852 * TODO:
5853 * VC: Standalone Document Declaration
5854 * element types with element content, if white space
5855 * occurs directly within any instance of those types.
5856 */
5857 }
5858 break;
5859 }
5860 }
5861 }
5862done:
5863 return(ret);
5864}
5865
5866/**
5867 * xmlValidatePopElement:
5868 * @ctxt: the validation context
5869 * @doc: a document instance
5870 * @elem: an element instance
5871 * @qname: the qualified name as appearing in the serialization
5872 *
5873 * Pop the element end from the validation stack.
5874 *
5875 * returns 1 if no validation problem was found or 0 otherwise
5876 */
5877int
5878xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005879 xmlNodePtr elem ATTRIBUTE_UNUSED,
5880 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005881 int ret = 1;
5882
Daniel Veillardc0be74b2004-11-03 19:16:55 +00005883 if (ctxt == NULL)
5884 return(0);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005885/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005886 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5887 xmlValidStatePtr state = ctxt->vstate;
5888 xmlElementPtr elemDecl;
5889
5890 /*
5891 * Check the new element agaisnt the content model of the new elem.
5892 */
5893 if (state->elemDecl != NULL) {
5894 elemDecl = state->elemDecl;
5895
5896 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5897 if (state->exec != NULL) {
5898 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5899 if (ret == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005900 xmlErrValidNode(ctxt, state->node,
5901 XML_DTD_CONTENT_MODEL,
5902 "Element %s content does not follow the DTD, Expecting more child\n",
5903 state->node->name, NULL,NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00005904 } else {
5905 /*
5906 * previous validation errors should not generate
5907 * a new one here
5908 */
5909 ret = 1;
5910 }
5911 }
5912 }
5913 }
5914 vstateVPop(ctxt);
5915 }
5916 return(ret);
5917}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005918#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005919
5920/**
Owen Taylor3473f882001-02-23 17:55:21 +00005921 * xmlValidateOneElement:
5922 * @ctxt: the validation context
5923 * @doc: a document instance
5924 * @elem: an element instance
5925 *
5926 * Try to validate a single element and it's attributes,
5927 * basically it does the following checks as described by the
5928 * XML-1.0 recommendation:
5929 * - [ VC: Element Valid ]
5930 * - [ VC: Required Attribute ]
5931 * Then call xmlValidateOneAttribute() for each attribute present.
5932 *
5933 * The ID/IDREF checkings are done separately
5934 *
5935 * returns 1 if valid or 0 otherwise
5936 */
5937
5938int
5939xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5940 xmlNodePtr elem) {
5941 xmlElementPtr elemDecl = NULL;
5942 xmlElementContentPtr cont;
5943 xmlAttributePtr attr;
5944 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005945 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005946 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005947 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005948
5949 CHECK_DTD;
5950
5951 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005952 switch (elem->type) {
5953 case XML_ATTRIBUTE_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005954 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5955 "Attribute element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005956 return(0);
5957 case XML_TEXT_NODE:
5958 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005959 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5960 "Text element has children !\n",
5961 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005962 return(0);
5963 }
Owen Taylor3473f882001-02-23 17:55:21 +00005964 if (elem->ns != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005965 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5966 "Text element has namespace !\n",
5967 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005968 return(0);
5969 }
Owen Taylor3473f882001-02-23 17:55:21 +00005970 if (elem->content == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005971 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5972 "Text element has no content !\n",
5973 NULL,NULL,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005974 return(0);
5975 }
5976 return(1);
5977 case XML_XINCLUDE_START:
5978 case XML_XINCLUDE_END:
5979 return(1);
5980 case XML_CDATA_SECTION_NODE:
5981 case XML_ENTITY_REF_NODE:
5982 case XML_PI_NODE:
5983 case XML_COMMENT_NODE:
5984 return(1);
5985 case XML_ENTITY_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005986 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5987 "Entity element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005988 return(0);
5989 case XML_NOTATION_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005990 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5991 "Notation element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005992 return(0);
5993 case XML_DOCUMENT_NODE:
5994 case XML_DOCUMENT_TYPE_NODE:
5995 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00005996 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5997 "Document element not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005998 return(0);
5999 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006000 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6001 "HTML Document not expected\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006002 return(0);
6003 case XML_ELEMENT_NODE:
6004 break;
6005 default:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006006 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6007 "unknown element type\n", NULL, NULL ,NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006008 return(0);
6009 }
Owen Taylor3473f882001-02-23 17:55:21 +00006010
6011 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00006012 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00006013 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00006014 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
6015 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006016 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006017
Daniel Veillardea7751d2002-12-20 00:16:24 +00006018 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006019 * If vstateNr is not zero that means continuous validation is
Daniel Veillardea7751d2002-12-20 00:16:24 +00006020 * activated, do not try to check the content model at that level.
6021 */
6022 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006023 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00006024 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00006025 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006026 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
6027 "No declaration for element %s\n",
6028 elem->name, NULL, NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00006029 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006030 case XML_ELEMENT_TYPE_EMPTY:
6031 if (elem->children != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006032 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
Owen Taylor3473f882001-02-23 17:55:21 +00006033 "Element %s was declared EMPTY this one has content\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006034 elem->name, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006035 ret = 0;
6036 }
6037 break;
6038 case XML_ELEMENT_TYPE_ANY:
6039 /* I don't think anything is required then */
6040 break;
6041 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006042
Daniel Veillard04e2dae2001-07-09 20:07:25 +00006043 /* simple case of declared as #PCDATA */
6044 if ((elemDecl->content != NULL) &&
6045 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
6046 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
6047 if (!ret) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006048 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
Daniel Veillard04e2dae2001-07-09 20:07:25 +00006049 "Element %s was declared #PCDATA but contains non text nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006050 elem->name, NULL, NULL);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00006051 }
6052 break;
6053 }
Owen Taylor3473f882001-02-23 17:55:21 +00006054 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00006055 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00006056 while (child != NULL) {
6057 if (child->type == XML_ELEMENT_NODE) {
6058 name = child->name;
6059 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006060 xmlChar fn[50];
6061 xmlChar *fullname;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006062
Daniel Veillardc00cda82003-04-07 10:22:39 +00006063 fullname = xmlBuildQName(child->name, child->ns->prefix,
6064 fn, 50);
6065 if (fullname == NULL)
6066 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006067 cont = elemDecl->content;
6068 while (cont != NULL) {
6069 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006070 if (xmlStrEqual(cont->name, fullname))
6071 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006072 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
6073 (cont->c1 != NULL) &&
6074 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00006075 if (xmlStrEqual(cont->c1->name, fullname))
6076 break;
Owen Taylor3473f882001-02-23 17:55:21 +00006077 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6078 (cont->c1 == NULL) ||
6079 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006080 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006081 "Internal: MIXED struct corrupted\n",
6082 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006083 break;
6084 }
6085 cont = cont->c2;
6086 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00006087 if ((fullname != fn) && (fullname != child->name))
6088 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00006089 if (cont != NULL)
6090 goto child_ok;
6091 }
6092 cont = elemDecl->content;
6093 while (cont != NULL) {
6094 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
6095 if (xmlStrEqual(cont->name, name)) break;
6096 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
6097 (cont->c1 != NULL) &&
6098 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
6099 if (xmlStrEqual(cont->c1->name, name)) break;
6100 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6101 (cont->c1 == NULL) ||
6102 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006103 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006104 "Internal: MIXED struct corrupted\n",
6105 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006106 break;
6107 }
6108 cont = cont->c2;
6109 }
6110 if (cont == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006111 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006112 "Element %s is not declared in %s list of possible children\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006113 name, elem->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006114 ret = 0;
6115 }
6116 }
6117child_ok:
6118 child = child->next;
6119 }
6120 break;
6121 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006122 if ((doc->standalone == 1) && (extsubset == 1)) {
6123 /*
6124 * VC: Standalone Document Declaration
6125 * - element types with element content, if white space
6126 * occurs directly within any instance of those types.
6127 */
6128 child = elem->children;
6129 while (child != NULL) {
6130 if (child->type == XML_TEXT_NODE) {
6131 const xmlChar *content = child->content;
6132
William M. Brack76e95df2003-10-18 16:20:14 +00006133 while (IS_BLANK_CH(*content))
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006134 content++;
6135 if (*content == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006136 xmlErrValidNode(ctxt, elem,
6137 XML_DTD_STANDALONE_WHITE_SPACE,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006138"standalone: %s declared in the external subset contains white spaces nodes\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006139 elem->name, NULL, NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006140 ret = 0;
6141 break;
6142 }
6143 }
6144 child =child->next;
6145 }
6146 }
Owen Taylor3473f882001-02-23 17:55:21 +00006147 child = elem->children;
6148 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00006149 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00006150 if (tmp <= 0)
6151 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00006152 break;
6153 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00006154 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00006155
6156 /* [ VC: Required Attribute ] */
6157 attr = elemDecl->attributes;
6158 while (attr != NULL) {
6159 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00006160 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00006161
Daniel Veillarde4301c82002-02-13 13:32:35 +00006162 if ((attr->prefix == NULL) &&
6163 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6164 xmlNsPtr ns;
6165
6166 ns = elem->nsDef;
6167 while (ns != NULL) {
6168 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00006169 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006170 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00006171 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006172 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6173 xmlNsPtr ns;
6174
6175 ns = elem->nsDef;
6176 while (ns != NULL) {
6177 if (xmlStrEqual(attr->name, ns->prefix))
6178 goto found;
6179 ns = ns->next;
6180 }
6181 } else {
6182 xmlAttrPtr attrib;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006183
Daniel Veillarde4301c82002-02-13 13:32:35 +00006184 attrib = elem->properties;
6185 while (attrib != NULL) {
6186 if (xmlStrEqual(attrib->name, attr->name)) {
6187 if (attr->prefix != NULL) {
6188 xmlNsPtr nameSpace = attrib->ns;
6189
6190 if (nameSpace == NULL)
6191 nameSpace = elem->ns;
6192 /*
6193 * qualified names handling is problematic, having a
6194 * different prefix should be possible but DTDs don't
6195 * allow to define the URI instead of the prefix :-(
6196 */
6197 if (nameSpace == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006198 if (qualified < 0)
Daniel Veillarde4301c82002-02-13 13:32:35 +00006199 qualified = 0;
6200 } else if (!xmlStrEqual(nameSpace->prefix,
6201 attr->prefix)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006202 if (qualified < 1)
Daniel Veillarde4301c82002-02-13 13:32:35 +00006203 qualified = 1;
6204 } else
6205 goto found;
6206 } else {
6207 /*
6208 * We should allow applications to define namespaces
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006209 * for their application even if the DTD doesn't
Daniel Veillarde4301c82002-02-13 13:32:35 +00006210 * carry one, otherwise, basically we would always
6211 * break.
6212 */
6213 goto found;
6214 }
6215 }
6216 attrib = attrib->next;
6217 }
Owen Taylor3473f882001-02-23 17:55:21 +00006218 }
6219 if (qualified == -1) {
6220 if (attr->prefix == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006221 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006222 "Element %s does not carry attribute %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006223 elem->name, attr->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006224 ret = 0;
6225 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006226 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006227 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00006228 elem->name, attr->prefix,attr->name);
6229 ret = 0;
6230 }
6231 } else if (qualified == 0) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006232 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006233 "Element %s required attribute %s:%s has no prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006234 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006235 } else if (qualified == 1) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006236 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
Owen Taylor3473f882001-02-23 17:55:21 +00006237 "Element %s required attribute %s:%s has different prefix\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006238 elem->name, attr->prefix, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006239 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00006240 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
6241 /*
6242 * Special tests checking #FIXED namespace declarations
6243 * have the right value since this is not done as an
6244 * attribute checking
6245 */
6246 if ((attr->prefix == NULL) &&
6247 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6248 xmlNsPtr ns;
6249
6250 ns = elem->nsDef;
6251 while (ns != NULL) {
6252 if (ns->prefix == NULL) {
6253 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006254 xmlErrValidNode(ctxt, elem,
6255 XML_DTD_ELEM_DEFAULT_NAMESPACE,
Daniel Veillarde4301c82002-02-13 13:32:35 +00006256 "Element %s namespace name for default namespace does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006257 elem->name, NULL, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006258 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006259 }
6260 goto found;
6261 }
6262 ns = ns->next;
6263 }
6264 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6265 xmlNsPtr ns;
6266
6267 ns = elem->nsDef;
6268 while (ns != NULL) {
6269 if (xmlStrEqual(attr->name, ns->prefix)) {
6270 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006271 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006272 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006273 elem->name, ns->prefix, NULL);
Daniel Veillardc7612992002-02-17 22:47:37 +00006274 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00006275 }
6276 goto found;
6277 }
6278 ns = ns->next;
6279 }
6280 }
Owen Taylor3473f882001-02-23 17:55:21 +00006281 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006282found:
Owen Taylor3473f882001-02-23 17:55:21 +00006283 attr = attr->nexth;
6284 }
6285 return(ret);
6286}
6287
6288/**
6289 * xmlValidateRoot:
6290 * @ctxt: the validation context
6291 * @doc: a document instance
6292 *
6293 * Try to validate a the root element
6294 * basically it does the following check as described by the
6295 * XML-1.0 recommendation:
6296 * - [ VC: Root Element Type ]
6297 * it doesn't try to recurse or apply other check to the element
6298 *
6299 * returns 1 if valid or 0 otherwise
6300 */
6301
6302int
6303xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6304 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00006305 int ret;
6306
Owen Taylor3473f882001-02-23 17:55:21 +00006307 if (doc == NULL) return(0);
6308
6309 root = xmlDocGetRootElement(doc);
6310 if ((root == NULL) || (root->name == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006311 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
6312 "no root element\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006313 return(0);
6314 }
6315
6316 /*
6317 * When doing post validation against a separate DTD, those may
6318 * no internal subset has been generated
6319 */
6320 if ((doc->intSubset != NULL) &&
6321 (doc->intSubset->name != NULL)) {
6322 /*
6323 * Check first the document root against the NQName
6324 */
6325 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
6326 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006327 xmlChar fn[50];
6328 xmlChar *fullname;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006329
Daniel Veillardc00cda82003-04-07 10:22:39 +00006330 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6331 if (fullname == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006332 xmlVErrMemory(ctxt, NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +00006333 return(0);
6334 }
6335 ret = xmlStrEqual(doc->intSubset->name, fullname);
6336 if ((fullname != fn) && (fullname != root->name))
6337 xmlFree(fullname);
6338 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00006339 goto name_ok;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006340 }
Owen Taylor3473f882001-02-23 17:55:21 +00006341 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6342 (xmlStrEqual(root->name, BAD_CAST "html")))
6343 goto name_ok;
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006344 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6345 "root and DTD name do not match '%s' and '%s'\n",
6346 root->name, doc->intSubset->name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006347 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006348 }
6349 }
6350name_ok:
6351 return(1);
6352}
6353
6354
6355/**
6356 * xmlValidateElement:
6357 * @ctxt: the validation context
6358 * @doc: a document instance
6359 * @elem: an element instance
6360 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006361 * Try to validate the subtree under an element
Owen Taylor3473f882001-02-23 17:55:21 +00006362 *
6363 * returns 1 if valid or 0 otherwise
6364 */
6365
6366int
6367xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
6368 xmlNodePtr child;
6369 xmlAttrPtr attr;
Daniel Veillarde133dd82003-10-30 10:42:20 +00006370 xmlNsPtr ns;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006371 const xmlChar *value;
Owen Taylor3473f882001-02-23 17:55:21 +00006372 int ret = 1;
6373
6374 if (elem == NULL) return(0);
6375
6376 /*
6377 * XInclude elements were added after parsing in the infoset,
6378 * they don't really mean anything validation wise.
6379 */
6380 if ((elem->type == XML_XINCLUDE_START) ||
Daniel Veillard3e62adb2012-08-09 14:24:02 +08006381 (elem->type == XML_XINCLUDE_END) ||
6382 (elem->type == XML_NAMESPACE_DECL))
Owen Taylor3473f882001-02-23 17:55:21 +00006383 return(1);
6384
6385 CHECK_DTD;
6386
Daniel Veillard10ea86c2001-06-20 13:55:33 +00006387 /*
6388 * Entities references have to be handled separately
6389 */
6390 if (elem->type == XML_ENTITY_REF_NODE) {
6391 return(1);
6392 }
6393
Owen Taylor3473f882001-02-23 17:55:21 +00006394 ret &= xmlValidateOneElement(ctxt, doc, elem);
Daniel Veillard8874b942005-08-25 13:19:21 +00006395 if (elem->type == XML_ELEMENT_NODE) {
6396 attr = elem->properties;
6397 while (attr != NULL) {
6398 value = xmlNodeListGetString(doc, attr->children, 0);
6399 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6400 if (value != NULL)
6401 xmlFree((char *)value);
6402 attr= attr->next;
6403 }
6404 ns = elem->nsDef;
6405 while (ns != NULL) {
6406 if (elem->ns == NULL)
6407 ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
6408 ns, ns->href);
6409 else
6410 ret &= xmlValidateOneNamespace(ctxt, doc, elem,
6411 elem->ns->prefix, ns, ns->href);
6412 ns = ns->next;
6413 }
Daniel Veillarde133dd82003-10-30 10:42:20 +00006414 }
Owen Taylor3473f882001-02-23 17:55:21 +00006415 child = elem->children;
6416 while (child != NULL) {
6417 ret &= xmlValidateElement(ctxt, doc, child);
6418 child = child->next;
6419 }
6420
6421 return(ret);
6422}
6423
Daniel Veillard8730c562001-02-26 10:49:57 +00006424/**
6425 * xmlValidateRef:
6426 * @ref: A reference to be validated
6427 * @ctxt: Validation context
6428 * @name: Name of ID we are searching for
6429 *
6430 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006431static void
Daniel Veillard8730c562001-02-26 10:49:57 +00006432xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006433 const xmlChar *name) {
6434 xmlAttrPtr id;
6435 xmlAttrPtr attr;
6436
6437 if (ref == NULL)
6438 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006439 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006440 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006441 attr = ref->attr;
6442 if (attr == NULL) {
6443 xmlChar *dup, *str = NULL, *cur, save;
6444
6445 dup = xmlStrdup(name);
6446 if (dup == NULL) {
6447 ctxt->valid = 0;
6448 return;
6449 }
6450 cur = dup;
6451 while (*cur != 0) {
6452 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006453 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006454 save = *cur;
6455 *cur = 0;
6456 id = xmlGetID(ctxt->doc, str);
6457 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006458 xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006459 "attribute %s line %d references an unknown ID \"%s\"\n",
6460 ref->name, ref->lineno, str);
6461 ctxt->valid = 0;
6462 }
6463 if (save == 0)
6464 break;
6465 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006466 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardea7751d2002-12-20 00:16:24 +00006467 }
6468 xmlFree(dup);
6469 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00006470 id = xmlGetID(ctxt->doc, name);
6471 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006472 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006473 "IDREF attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006474 attr->name, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006475 ctxt->valid = 0;
6476 }
6477 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6478 xmlChar *dup, *str = NULL, *cur, save;
6479
6480 dup = xmlStrdup(name);
6481 if (dup == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00006482 xmlVErrMemory(ctxt, "IDREFS split");
Owen Taylor3473f882001-02-23 17:55:21 +00006483 ctxt->valid = 0;
6484 return;
6485 }
6486 cur = dup;
6487 while (*cur != 0) {
6488 str = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006489 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006490 save = *cur;
6491 *cur = 0;
6492 id = xmlGetID(ctxt->doc, str);
6493 if (id == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006494 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
Daniel Veillardea7751d2002-12-20 00:16:24 +00006495 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006496 attr->name, str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006497 ctxt->valid = 0;
6498 }
6499 if (save == 0)
6500 break;
6501 *cur = save;
William M. Brack76e95df2003-10-18 16:20:14 +00006502 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00006503 }
6504 xmlFree(dup);
6505 }
6506}
6507
6508/**
Daniel Veillard8730c562001-02-26 10:49:57 +00006509 * xmlWalkValidateList:
6510 * @data: Contents of current link
6511 * @user: Value supplied by the user
6512 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006513 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00006514 */
6515static int
6516xmlWalkValidateList(const void *data, const void *user)
6517{
6518 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6519 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6520 return 1;
6521}
6522
6523/**
6524 * xmlValidateCheckRefCallback:
6525 * @ref_list: List of references
6526 * @ctxt: Validation context
6527 * @name: Name of ID we are searching for
6528 *
6529 */
6530static void
6531xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6532 const xmlChar *name) {
6533 xmlValidateMemo memo;
6534
6535 if (ref_list == NULL)
6536 return;
6537 memo.ctxt = ctxt;
6538 memo.name = name;
6539
6540 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006541
Daniel Veillard8730c562001-02-26 10:49:57 +00006542}
6543
6544/**
Owen Taylor3473f882001-02-23 17:55:21 +00006545 * xmlValidateDocumentFinal:
6546 * @ctxt: the validation context
6547 * @doc: a document instance
6548 *
6549 * Does the final step for the document validation once all the
6550 * incremental validation steps have been completed
6551 *
6552 * basically it does the following checks described by the XML Rec
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006553 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006554 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00006555 *
6556 * returns 1 if valid or 0 otherwise
6557 */
6558
6559int
6560xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6561 xmlRefTablePtr table;
Daniel Veillardcb3549e2011-11-11 11:25:07 +08006562 unsigned int save;
Owen Taylor3473f882001-02-23 17:55:21 +00006563
Daniel Veillardc0be74b2004-11-03 19:16:55 +00006564 if (ctxt == NULL)
6565 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006566 if (doc == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006567 xmlErrValid(ctxt, XML_DTD_NO_DOC,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00006568 "xmlValidateDocumentFinal: doc == NULL\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006569 return(0);
6570 }
6571
Daniel Veillardcb3549e2011-11-11 11:25:07 +08006572 /* trick to get correct line id report */
6573 save = ctxt->finishDtd;
6574 ctxt->finishDtd = 0;
6575
Owen Taylor3473f882001-02-23 17:55:21 +00006576 /*
6577 * Check all the NOTATION/NOTATIONS attributes
6578 */
6579 /*
6580 * Check all the ENTITY/ENTITIES attributes definition for validity
6581 */
6582 /*
6583 * Check all the IDREF/IDREFS attributes definition for validity
6584 */
6585 table = (xmlRefTablePtr) doc->refs;
6586 ctxt->doc = doc;
6587 ctxt->valid = 1;
6588 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
Daniel Veillardcb3549e2011-11-11 11:25:07 +08006589
6590 ctxt->finishDtd = save;
Owen Taylor3473f882001-02-23 17:55:21 +00006591 return(ctxt->valid);
6592}
6593
6594/**
6595 * xmlValidateDtd:
6596 * @ctxt: the validation context
6597 * @doc: a document instance
6598 * @dtd: a dtd instance
6599 *
6600 * Try to validate the document against the dtd instance
6601 *
William M. Brack367df6e2004-10-23 18:14:36 +00006602 * Basically it does check all the definitions in the DtD.
6603 * Note the the internal subset (if present) is de-coupled
6604 * (i.e. not used), which could give problems if ID or IDREF
6605 * is present.
Owen Taylor3473f882001-02-23 17:55:21 +00006606 *
6607 * returns 1 if valid or 0 otherwise
6608 */
6609
6610int
6611xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6612 int ret;
William M. Brack367df6e2004-10-23 18:14:36 +00006613 xmlDtdPtr oldExt, oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006614 xmlNodePtr root;
6615
6616 if (dtd == NULL) return(0);
6617 if (doc == NULL) return(0);
6618 oldExt = doc->extSubset;
William M. Brack367df6e2004-10-23 18:14:36 +00006619 oldInt = doc->intSubset;
Owen Taylor3473f882001-02-23 17:55:21 +00006620 doc->extSubset = dtd;
William M. Brack367df6e2004-10-23 18:14:36 +00006621 doc->intSubset = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006622 ret = xmlValidateRoot(ctxt, doc);
6623 if (ret == 0) {
6624 doc->extSubset = oldExt;
William M. Brack367df6e2004-10-23 18:14:36 +00006625 doc->intSubset = oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006626 return(ret);
6627 }
6628 if (doc->ids != NULL) {
6629 xmlFreeIDTable(doc->ids);
6630 doc->ids = NULL;
6631 }
6632 if (doc->refs != NULL) {
6633 xmlFreeRefTable(doc->refs);
6634 doc->refs = NULL;
6635 }
6636 root = xmlDocGetRootElement(doc);
6637 ret = xmlValidateElement(ctxt, doc, root);
6638 ret &= xmlValidateDocumentFinal(ctxt, doc);
6639 doc->extSubset = oldExt;
William M. Brack367df6e2004-10-23 18:14:36 +00006640 doc->intSubset = oldInt;
Owen Taylor3473f882001-02-23 17:55:21 +00006641 return(ret);
6642}
6643
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006644static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006645xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6646 const xmlChar *name ATTRIBUTE_UNUSED) {
6647 if (cur == NULL)
6648 return;
6649 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6650 xmlChar *notation = cur->content;
6651
Daniel Veillard878eab02002-02-19 13:46:09 +00006652 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006653 int ret;
6654
6655 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6656 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006657 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006658 }
6659 }
6660 }
6661}
6662
6663static void
Owen Taylor3473f882001-02-23 17:55:21 +00006664xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006665 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006666 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006667 xmlDocPtr doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006668 xmlElementPtr elem = NULL;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006669
Owen Taylor3473f882001-02-23 17:55:21 +00006670 if (cur == NULL)
6671 return;
6672 switch (cur->atype) {
6673 case XML_ATTRIBUTE_CDATA:
6674 case XML_ATTRIBUTE_ID:
6675 case XML_ATTRIBUTE_IDREF :
6676 case XML_ATTRIBUTE_IDREFS:
6677 case XML_ATTRIBUTE_NMTOKEN:
6678 case XML_ATTRIBUTE_NMTOKENS:
6679 case XML_ATTRIBUTE_ENUMERATION:
6680 break;
6681 case XML_ATTRIBUTE_ENTITY:
6682 case XML_ATTRIBUTE_ENTITIES:
6683 case XML_ATTRIBUTE_NOTATION:
6684 if (cur->defaultValue != NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006685
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006686 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6687 cur->atype, cur->defaultValue);
6688 if ((ret == 0) && (ctxt->valid == 1))
6689 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006690 }
6691 if (cur->tree != NULL) {
6692 xmlEnumerationPtr tree = cur->tree;
6693 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006694 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006695 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006696 if ((ret == 0) && (ctxt->valid == 1))
6697 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006698 tree = tree->next;
6699 }
6700 }
6701 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006702 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6703 doc = cur->doc;
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006704 if (cur->elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006705 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard878eab02002-02-19 13:46:09 +00006706 "xmlValidateAttributeCallback(%s): internal error\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006707 (const char *) cur->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00006708 return;
6709 }
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006710
6711 if (doc != NULL)
6712 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6713 if ((elem == NULL) && (doc != NULL))
Daniel Veillard878eab02002-02-19 13:46:09 +00006714 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
Daniel Veillarda8ff65d2003-11-03 16:20:10 +00006715 if ((elem == NULL) && (cur->parent != NULL) &&
6716 (cur->parent->type == XML_DTD_NODE))
6717 elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00006718 if (elem == NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006719 xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
Daniel Veillard878eab02002-02-19 13:46:09 +00006720 "attribute %s: could not find decl for element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006721 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006722 return;
6723 }
6724 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006725 xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006726 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006727 cur->name, cur->elem, NULL);
Daniel Veillard878eab02002-02-19 13:46:09 +00006728 ctxt->valid = 0;
6729 }
6730 }
Owen Taylor3473f882001-02-23 17:55:21 +00006731}
6732
6733/**
6734 * xmlValidateDtdFinal:
6735 * @ctxt: the validation context
6736 * @doc: a document instance
6737 *
6738 * Does the final step for the dtds validation once all the
6739 * subsets have been parsed
6740 *
6741 * basically it does the following checks described by the XML Rec
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006742 * - check that ENTITY and ENTITIES type attributes default or
Owen Taylor3473f882001-02-23 17:55:21 +00006743 * possible values matches one of the defined entities.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006744 * - check that NOTATION type attributes default or
Owen Taylor3473f882001-02-23 17:55:21 +00006745 * possible values matches one of the defined notations.
6746 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006747 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006748 */
6749
6750int
6751xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006752 xmlDtdPtr dtd;
6753 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006754 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006755
Daniel Veillard2cba4152008-08-27 11:45:41 +00006756 if ((doc == NULL) || (ctxt == NULL)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006757 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6758 return(0);
6759 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006760 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006761 dtd = doc->intSubset;
6762 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6763 table = (xmlAttributeTablePtr) dtd->attributes;
6764 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006765 }
6766 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006767 entities = (xmlEntitiesTablePtr) dtd->entities;
6768 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6769 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006770 }
6771 dtd = doc->extSubset;
6772 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6773 table = (xmlAttributeTablePtr) dtd->attributes;
6774 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006775 }
6776 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006777 entities = (xmlEntitiesTablePtr) dtd->entities;
6778 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6779 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006780 }
6781 return(ctxt->valid);
6782}
6783
6784/**
6785 * xmlValidateDocument:
6786 * @ctxt: the validation context
6787 * @doc: a document instance
6788 *
6789 * Try to validate the document instance
6790 *
6791 * basically it does the all the checks described by the XML Rec
6792 * i.e. validates the internal and external subset (if present)
6793 * and validate the document tree.
6794 *
6795 * returns 1 if valid or 0 otherwise
6796 */
6797
6798int
6799xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6800 int ret;
6801 xmlNodePtr root;
6802
Daniel Veillardc0be74b2004-11-03 19:16:55 +00006803 if (doc == NULL)
6804 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006805 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006806 xmlErrValid(ctxt, XML_DTD_NO_DTD,
6807 "no DTD found!\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006808 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006809 }
Owen Taylor3473f882001-02-23 17:55:21 +00006810 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6811 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
William M. Brack8c22f9f2004-08-06 16:23:27 +00006812 xmlChar *sysID;
6813 if (doc->intSubset->SystemID != NULL) {
William M. Brackbebe7302004-08-05 06:46:47 +00006814 sysID = xmlBuildURI(doc->intSubset->SystemID,
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006815 doc->URL);
William M. Brack8c22f9f2004-08-06 16:23:27 +00006816 if (sysID == NULL) {
6817 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6818 "Could not build URI for external subset \"%s\"\n",
6819 (const char *) doc->intSubset->SystemID);
6820 return 0;
6821 }
6822 } else
William M. Brackbebe7302004-08-05 06:46:47 +00006823 sysID = NULL;
William M. Brack8c22f9f2004-08-06 16:23:27 +00006824 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
William M. Brackbebe7302004-08-05 06:46:47 +00006825 (const xmlChar *)sysID);
William M. Brackbebe7302004-08-05 06:46:47 +00006826 if (sysID != NULL)
6827 xmlFree(sysID);
Owen Taylor3473f882001-02-23 17:55:21 +00006828 if (doc->extSubset == NULL) {
6829 if (doc->intSubset->SystemID != NULL) {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006830 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006831 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006832 (const char *) doc->intSubset->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00006833 } else {
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006834 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006835 "Could not load the external subset \"%s\"\n",
Daniel Veillardbb5abab2003-10-03 22:21:51 +00006836 (const char *) doc->intSubset->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00006837 }
6838 return(0);
6839 }
6840 }
6841
6842 if (doc->ids != NULL) {
6843 xmlFreeIDTable(doc->ids);
6844 doc->ids = NULL;
6845 }
6846 if (doc->refs != NULL) {
6847 xmlFreeRefTable(doc->refs);
6848 doc->refs = NULL;
6849 }
6850 ret = xmlValidateDtdFinal(ctxt, doc);
6851 if (!xmlValidateRoot(ctxt, doc)) return(0);
6852
6853 root = xmlDocGetRootElement(doc);
6854 ret &= xmlValidateElement(ctxt, doc, root);
6855 ret &= xmlValidateDocumentFinal(ctxt, doc);
6856 return(ret);
6857}
6858
Owen Taylor3473f882001-02-23 17:55:21 +00006859/************************************************************************
6860 * *
6861 * Routines for dynamic validation editing *
6862 * *
6863 ************************************************************************/
6864
6865/**
6866 * xmlValidGetPotentialChildren:
6867 * @ctree: an element content tree
Daniel Veillard7802ba52005-10-27 11:56:20 +00006868 * @names: an array to store the list of child names
Owen Taylor3473f882001-02-23 17:55:21 +00006869 * @len: a pointer to the number of element in the list
6870 * @max: the size of the array
6871 *
6872 * Build/extend a list of potential children allowed by the content tree
6873 *
6874 * returns the number of element in the list, or -1 in case of error.
6875 */
6876
6877int
Daniel Veillard7802ba52005-10-27 11:56:20 +00006878xmlValidGetPotentialChildren(xmlElementContent *ctree,
6879 const xmlChar **names,
Owen Taylor3473f882001-02-23 17:55:21 +00006880 int *len, int max) {
6881 int i;
6882
Daniel Veillard7802ba52005-10-27 11:56:20 +00006883 if ((ctree == NULL) || (names == NULL) || (len == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006884 return(-1);
6885 if (*len >= max) return(*len);
6886
6887 switch (ctree->type) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006888 case XML_ELEMENT_CONTENT_PCDATA:
Owen Taylor3473f882001-02-23 17:55:21 +00006889 for (i = 0; i < *len;i++)
Daniel Veillard7802ba52005-10-27 11:56:20 +00006890 if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len);
6891 names[(*len)++] = BAD_CAST "#PCDATA";
Owen Taylor3473f882001-02-23 17:55:21 +00006892 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006893 case XML_ELEMENT_CONTENT_ELEMENT:
Owen Taylor3473f882001-02-23 17:55:21 +00006894 for (i = 0; i < *len;i++)
Daniel Veillard7802ba52005-10-27 11:56:20 +00006895 if (xmlStrEqual(ctree->name, names[i])) return(*len);
6896 names[(*len)++] = ctree->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006897 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006898 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard7802ba52005-10-27 11:56:20 +00006899 xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6900 xmlValidGetPotentialChildren(ctree->c2, names, len, max);
Owen Taylor3473f882001-02-23 17:55:21 +00006901 break;
6902 case XML_ELEMENT_CONTENT_OR:
Daniel Veillard7802ba52005-10-27 11:56:20 +00006903 xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6904 xmlValidGetPotentialChildren(ctree->c2, names, len, max);
Owen Taylor3473f882001-02-23 17:55:21 +00006905 break;
6906 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006907
Owen Taylor3473f882001-02-23 17:55:21 +00006908 return(*len);
6909}
6910
William M. Brack9333cc22004-06-24 08:33:40 +00006911/*
6912 * Dummy function to suppress messages while we try out valid elements
6913 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00006914static void XMLCDECL xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
William M. Brack9333cc22004-06-24 08:33:40 +00006915 const char *msg ATTRIBUTE_UNUSED, ...) {
6916 return;
6917}
6918
Owen Taylor3473f882001-02-23 17:55:21 +00006919/**
6920 * xmlValidGetValidElements:
6921 * @prev: an element to insert after
6922 * @next: an element to insert next
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006923 * @names: an array to store the list of child names
Owen Taylor3473f882001-02-23 17:55:21 +00006924 * @max: the size of the array
6925 *
6926 * This function returns the list of authorized children to insert
6927 * within an existing tree while respecting the validity constraints
6928 * forced by the Dtd. The insertion point is defined using @prev and
6929 * @next in the following ways:
6930 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6931 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6932 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6933 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6934 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6935 *
6936 * pointers to the element names are inserted at the beginning of the array
6937 * and do not need to be freed.
6938 *
6939 * returns the number of element in the list, or -1 in case of error. If
6940 * the function returns the value @max the caller is invited to grow the
6941 * receiving array and retry.
6942 */
6943
6944int
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006945xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
Owen Taylor3473f882001-02-23 17:55:21 +00006946 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006947 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006948 int nb_valid_elements = 0;
Daniel Veillarde18bce02014-02-06 10:47:20 +01006949 const xmlChar *elements[256]={0};
Owen Taylor3473f882001-02-23 17:55:21 +00006950 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006951 const xmlChar *name;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006952
Owen Taylor3473f882001-02-23 17:55:21 +00006953 xmlNode *ref_node;
6954 xmlNode *parent;
6955 xmlNode *test_node;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006956
Owen Taylor3473f882001-02-23 17:55:21 +00006957 xmlNode *prev_next;
6958 xmlNode *next_prev;
6959 xmlNode *parent_childs;
6960 xmlNode *parent_last;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006961
Owen Taylor3473f882001-02-23 17:55:21 +00006962 xmlElement *element_desc;
6963
6964 if (prev == NULL && next == NULL)
6965 return(-1);
6966
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00006967 if (names == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006968 if (max <= 0) return(-1);
6969
William M. Brack9333cc22004-06-24 08:33:40 +00006970 memset(&vctxt, 0, sizeof (xmlValidCtxt));
6971 vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
6972
Owen Taylor3473f882001-02-23 17:55:21 +00006973 nb_valid_elements = 0;
6974 ref_node = prev ? prev : next;
6975 parent = ref_node->parent;
6976
6977 /*
6978 * Retrieves the parent element declaration
6979 */
6980 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6981 parent->name);
6982 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6983 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6984 parent->name);
6985 if (element_desc == NULL) return(-1);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006986
Owen Taylor3473f882001-02-23 17:55:21 +00006987 /*
6988 * Do a backup of the current tree structure
6989 */
6990 prev_next = prev ? prev->next : NULL;
6991 next_prev = next ? next->prev : NULL;
6992 parent_childs = parent->children;
6993 parent_last = parent->last;
6994
6995 /*
6996 * Creates a dummy node and insert it into the tree
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006997 */
Daniel Veillard95ddcd32004-10-26 21:53:55 +00006998 test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006999 test_node->parent = parent;
7000 test_node->prev = prev;
7001 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00007002 name = test_node->name;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007003
Owen Taylor3473f882001-02-23 17:55:21 +00007004 if (prev) prev->next = test_node;
7005 else parent->children = test_node;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007006
Owen Taylor3473f882001-02-23 17:55:21 +00007007 if (next) next->prev = test_node;
7008 else parent->last = test_node;
7009
7010 /*
7011 * Insert each potential child node and check if the parent is
7012 * still valid
7013 */
7014 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
7015 elements, &nb_elements, 256);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007016
Owen Taylor3473f882001-02-23 17:55:21 +00007017 for (i = 0;i < nb_elements;i++) {
7018 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00007019 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007020 int j;
7021
7022 for (j = 0; j < nb_valid_elements;j++)
Daniel Veillardaecc0dc2004-05-08 02:32:07 +00007023 if (xmlStrEqual(elements[i], names[j])) break;
7024 names[nb_valid_elements++] = elements[i];
Owen Taylor3473f882001-02-23 17:55:21 +00007025 if (nb_valid_elements >= max) break;
7026 }
7027 }
7028
7029 /*
7030 * Restore the tree structure
7031 */
7032 if (prev) prev->next = prev_next;
7033 if (next) next->prev = next_prev;
7034 parent->children = parent_childs;
7035 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00007036
7037 /*
7038 * Free up the dummy node
7039 */
7040 test_node->name = name;
7041 xmlFreeNode(test_node);
7042
Owen Taylor3473f882001-02-23 17:55:21 +00007043 return(nb_valid_elements);
7044}
Daniel Veillard4432df22003-09-28 18:58:27 +00007045#endif /* LIBXML_VALID_ENABLED */
7046
Daniel Veillard5d4644e2005-04-01 13:11:58 +00007047#define bottom_valid
7048#include "elfgcchack.h"