blob: 77e7b874618a6e9418fda924cf20a61bcb6813c6 [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>
21#include <libxml/valid.h>
22#include <libxml/parser.h>
23#include <libxml/parserInternals.h>
24#include <libxml/xmlerror.h>
25#include <libxml/list.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000026#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000027
Daniel Veillarde62d36c2001-05-15 08:53:16 +000028/* #define DEBUG_VALID_ALGO */
Daniel Veillard5acfd6b2002-09-18 16:29:02 +000029/* #define DEBUG_REGEXP_ALGO */
Daniel Veillarde62d36c2001-05-15 08:53:16 +000030
Daniel Veillarda646cfd2002-09-17 21:50:03 +000031#define TODO \
32 xmlGenericError(xmlGenericErrorContext, \
33 "Unimplemented block at %s:%d\n", \
34 __FILE__, __LINE__);
Owen Taylor3473f882001-02-23 17:55:21 +000035
Daniel Veillardea7751d2002-12-20 00:16:24 +000036#define VERROR \
37 if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error
Owen Taylor3473f882001-02-23 17:55:21 +000038
Daniel Veillardea7751d2002-12-20 00:16:24 +000039#define VWARNING \
40 if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning
41
42
43#ifdef LIBXML_REGEXP_ENABLED
44/*
45 * If regexp are enabled we can do continuous validation without the
46 * need of a tree to validate the content model. this is done in each
47 * callbacks.
48 * Each xmlValidState represent the validation state associated to the
49 * set of nodes currently open from the document root to the current element.
50 */
51
52
53typedef struct _xmlValidState {
54 xmlElementPtr elemDecl; /* pointer to the content model */
55 xmlNodePtr node; /* pointer to the current node */
56 xmlRegExecCtxtPtr exec; /* regexp runtime */
57} _xmlValidState;
58
59
60static int
61vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000062 if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +000063 ctxt->vstateMax = 10;
64 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
65 sizeof(ctxt->vstateTab[0]));
66 if (ctxt->vstateTab == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000067 VERROR(ctxt->userData, "malloc failed !n");
Daniel Veillardea7751d2002-12-20 00:16:24 +000068 return(-1);
69 }
70 }
71
72 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000073 xmlValidState *tmp;
74
75 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
76 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
77 if (tmp == NULL) {
Daniel Veillardea7751d2002-12-20 00:16:24 +000078 VERROR(ctxt->userData, "realloc failed !n");
79 return(-1);
80 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000081 ctxt->vstateMax *= 2;
82 ctxt->vstateTab = tmp;
Daniel Veillardea7751d2002-12-20 00:16:24 +000083 }
84 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
85 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
86 ctxt->vstateTab[ctxt->vstateNr].node = node;
87 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
88 if (elemDecl->contModel == NULL)
89 xmlValidBuildContentModel(ctxt, elemDecl);
90 if (elemDecl->contModel != NULL) {
91 ctxt->vstateTab[ctxt->vstateNr].exec =
92 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
93 } else {
94 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
95 VERROR(ctxt->userData,
96 "Failed to build content model regexp for %s", node->name);
97 }
98 }
99 return(ctxt->vstateNr++);
100}
101
102static int
103vstateVPop(xmlValidCtxtPtr ctxt) {
104 xmlElementPtr elemDecl;
105
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000106 if (ctxt->vstateNr < 1) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000107 ctxt->vstateNr--;
108 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
109 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
110 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
111 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
112 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
113 }
114 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
115 if (ctxt->vstateNr >= 1)
116 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
117 else
118 ctxt->vstate = NULL;
119 return(ctxt->vstateNr);
120}
121
122#else /* not LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000123/*
Daniel Veillard1c732d22002-11-30 11:22:59 +0000124 * If regexp are not enabled, it uses a home made algorithm less
125 * complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000126 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000127 * only restriction is on the deepness of the tree limited by the
128 * size of the occurs bitfield
129 *
130 * this is the content of a saved state for rollbacks
131 */
132
133#define ROLLBACK_OR 0
134#define ROLLBACK_PARENT 1
135
Daniel Veillardb44025c2001-10-11 22:55:55 +0000136typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000137 xmlElementContentPtr cont; /* pointer to the content model subtree */
138 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000139 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000140 unsigned char depth; /* current depth in the overall tree */
141 unsigned char state; /* ROLLBACK_XXX */
142} _xmlValidState;
143
Daniel Veillardfc57b412002-04-29 15:50:14 +0000144#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000145#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
146#define CONT ctxt->vstate->cont
147#define NODE ctxt->vstate->node
148#define DEPTH ctxt->vstate->depth
149#define OCCURS ctxt->vstate->occurs
150#define STATE ctxt->vstate->state
151
Daniel Veillard5344c602001-12-31 16:37:34 +0000152#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
153#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000154
Daniel Veillard5344c602001-12-31 16:37:34 +0000155#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
156#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000157
158static int
159vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
160 xmlNodePtr node, unsigned char depth, long occurs,
161 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000162 int i = ctxt->vstateNr - 1;
163
Daniel Veillard940492d2002-04-15 10:15:25 +0000164 if (ctxt->vstateNr > MAX_RECURSE) {
165 return(-1);
166 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000167 if (ctxt->vstateTab == NULL) {
168 ctxt->vstateMax = 8;
169 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
170 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
171 if (ctxt->vstateTab == NULL) {
172 xmlGenericError(xmlGenericErrorContext,
173 "malloc failed !n");
174 return(-1);
175 }
176 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000177 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000178 xmlValidState *tmp;
179
180 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
181 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
182 if (tmp == NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000183 xmlGenericError(xmlGenericErrorContext,
184 "realloc failed !n");
Daniel Veillard940492d2002-04-15 10:15:25 +0000185 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000186 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000187 ctxt->vstateMax *= 2;
188 ctxt->vstateTab = tmp;
Daniel Veillard06803992001-04-22 10:35:56 +0000189 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000190 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000191 /*
192 * Don't push on the stack a state already here
193 */
194 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
195 (ctxt->vstateTab[i].node == node) &&
196 (ctxt->vstateTab[i].depth == depth) &&
197 (ctxt->vstateTab[i].occurs == occurs) &&
198 (ctxt->vstateTab[i].state == state))
199 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000200 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
201 ctxt->vstateTab[ctxt->vstateNr].node = node;
202 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
203 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
204 ctxt->vstateTab[ctxt->vstateNr].state = state;
205 return(ctxt->vstateNr++);
206}
207
208static int
209vstateVPop(xmlValidCtxtPtr ctxt) {
210 if (ctxt->vstateNr <= 1) return(-1);
211 ctxt->vstateNr--;
212 ctxt->vstate = &ctxt->vstateTab[0];
213 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
214 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
215 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
216 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
217 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
218 return(ctxt->vstateNr);
219}
220
Daniel Veillard118aed72002-09-24 14:13:13 +0000221#endif /* LIBXML_REGEXP_ENABLED */
222
Daniel Veillard1c732d22002-11-30 11:22:59 +0000223static int
224nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
225{
226 if (ctxt->nodeMax <= 0) {
227 ctxt->nodeMax = 4;
228 ctxt->nodeTab =
229 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
230 sizeof(ctxt->nodeTab[0]));
231 if (ctxt->nodeTab == NULL) {
232 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
233 ctxt->nodeMax = 0;
234 return (0);
235 }
236 }
237 if (ctxt->nodeNr >= ctxt->nodeMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000238 xmlNodePtr *tmp;
239 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
240 ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
241 if (tmp == NULL) {
Daniel Veillard1c732d22002-11-30 11:22:59 +0000242 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
243 return (0);
244 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000245 ctxt->nodeMax *= 2;
246 ctxt->nodeTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000247 }
248 ctxt->nodeTab[ctxt->nodeNr] = value;
249 ctxt->node = value;
250 return (ctxt->nodeNr++);
251}
252static xmlNodePtr
253nodeVPop(xmlValidCtxtPtr ctxt)
254{
255 xmlNodePtr ret;
256
257 if (ctxt->nodeNr <= 0)
258 return (0);
259 ctxt->nodeNr--;
260 if (ctxt->nodeNr > 0)
261 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
262 else
263 ctxt->node = NULL;
264 ret = ctxt->nodeTab[ctxt->nodeNr];
265 ctxt->nodeTab[ctxt->nodeNr] = 0;
266 return (ret);
267}
Owen Taylor3473f882001-02-23 17:55:21 +0000268
Owen Taylor3473f882001-02-23 17:55:21 +0000269#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000270static void
271xmlValidPrintNode(xmlNodePtr cur) {
272 if (cur == NULL) {
273 xmlGenericError(xmlGenericErrorContext, "null");
274 return;
275 }
276 switch (cur->type) {
277 case XML_ELEMENT_NODE:
278 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
279 break;
280 case XML_TEXT_NODE:
281 xmlGenericError(xmlGenericErrorContext, "text ");
282 break;
283 case XML_CDATA_SECTION_NODE:
284 xmlGenericError(xmlGenericErrorContext, "cdata ");
285 break;
286 case XML_ENTITY_REF_NODE:
287 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
288 break;
289 case XML_PI_NODE:
290 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
291 break;
292 case XML_COMMENT_NODE:
293 xmlGenericError(xmlGenericErrorContext, "comment ");
294 break;
295 case XML_ATTRIBUTE_NODE:
296 xmlGenericError(xmlGenericErrorContext, "?attr? ");
297 break;
298 case XML_ENTITY_NODE:
299 xmlGenericError(xmlGenericErrorContext, "?ent? ");
300 break;
301 case XML_DOCUMENT_NODE:
302 xmlGenericError(xmlGenericErrorContext, "?doc? ");
303 break;
304 case XML_DOCUMENT_TYPE_NODE:
305 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
306 break;
307 case XML_DOCUMENT_FRAG_NODE:
308 xmlGenericError(xmlGenericErrorContext, "?frag? ");
309 break;
310 case XML_NOTATION_NODE:
311 xmlGenericError(xmlGenericErrorContext, "?nota? ");
312 break;
313 case XML_HTML_DOCUMENT_NODE:
314 xmlGenericError(xmlGenericErrorContext, "?html? ");
315 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000316#ifdef LIBXML_DOCB_ENABLED
317 case XML_DOCB_DOCUMENT_NODE:
318 xmlGenericError(xmlGenericErrorContext, "?docb? ");
319 break;
320#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000321 case XML_DTD_NODE:
322 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
323 break;
324 case XML_ELEMENT_DECL:
325 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
326 break;
327 case XML_ATTRIBUTE_DECL:
328 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
329 break;
330 case XML_ENTITY_DECL:
331 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
332 break;
333 case XML_NAMESPACE_DECL:
334 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
335 break;
336 case XML_XINCLUDE_START:
337 xmlGenericError(xmlGenericErrorContext, "incstart ");
338 break;
339 case XML_XINCLUDE_END:
340 xmlGenericError(xmlGenericErrorContext, "incend ");
341 break;
342 }
343}
344
345static void
346xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000347 if (cur == NULL)
348 xmlGenericError(xmlGenericErrorContext, "null ");
349 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000350 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000351 cur = cur->next;
352 }
353}
354
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000355static void
356xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Daniel Veillard83391282003-03-06 21:37:30 +0000357 char expr[5000];
Owen Taylor3473f882001-02-23 17:55:21 +0000358
359 expr[0] = 0;
360 xmlGenericError(xmlGenericErrorContext, "valid: ");
361 xmlValidPrintNodeList(cur);
362 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000363 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000364 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
365}
366
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000367static void
368xmlValidDebugState(xmlValidStatePtr state) {
369 xmlGenericError(xmlGenericErrorContext, "(");
370 if (state->cont == NULL)
371 xmlGenericError(xmlGenericErrorContext, "null,");
372 else
373 switch (state->cont->type) {
374 case XML_ELEMENT_CONTENT_PCDATA:
375 xmlGenericError(xmlGenericErrorContext, "pcdata,");
376 break;
377 case XML_ELEMENT_CONTENT_ELEMENT:
378 xmlGenericError(xmlGenericErrorContext, "%s,",
379 state->cont->name);
380 break;
381 case XML_ELEMENT_CONTENT_SEQ:
382 xmlGenericError(xmlGenericErrorContext, "seq,");
383 break;
384 case XML_ELEMENT_CONTENT_OR:
385 xmlGenericError(xmlGenericErrorContext, "or,");
386 break;
387 }
388 xmlValidPrintNode(state->node);
389 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
390 state->depth, state->occurs, state->state);
391}
392
393static void
394xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
395 int i, j;
396
397 xmlGenericError(xmlGenericErrorContext, "state: ");
398 xmlValidDebugState(ctxt->vstate);
399 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
400 ctxt->vstateNr - 1);
401 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
402 xmlValidDebugState(&ctxt->vstateTab[j]);
403 xmlGenericError(xmlGenericErrorContext, "\n");
404}
405
406/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000407#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000408 *****/
409
410#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000411#define DEBUG_VALID_MSG(m) \
412 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
413
Owen Taylor3473f882001-02-23 17:55:21 +0000414#else
415#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000416#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000417#endif
418
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000419/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000420
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000421#define VECTXT(ctxt, node) \
422 if ((ctxt != NULL) && (ctxt->error != NULL) && \
Daniel Veillard76575762002-09-05 14:21:15 +0000423 (node != NULL)) { \
424 xmlChar *base = xmlNodeGetBase(NULL,node); \
425 if (base != NULL) { \
426 ctxt->error(ctxt->userData, "%s:%d: ", base, \
Daniel Veillard366a9152002-10-23 20:43:53 +0000427 (int) (long) node->content); \
Daniel Veillard76575762002-09-05 14:21:15 +0000428 xmlFree(base); \
429 } else \
430 ctxt->error(ctxt->userData, ":%d: ", \
Daniel Veillard366a9152002-10-23 20:43:53 +0000431 (int) (long) node->content); \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000432 }
433
434#define VWCTXT(ctxt, node) \
435 if ((ctxt != NULL) && (ctxt->warning != NULL) && \
Daniel Veillard76575762002-09-05 14:21:15 +0000436 (node != NULL)) { \
437 xmlChar *base = xmlNodeGetBase(NULL,node); \
438 if (base != NULL) { \
439 ctxt->warning(ctxt->userData, "%s:%d: ", base, \
Daniel Veillard366a9152002-10-23 20:43:53 +0000440 (int) (long) node->content); \
Daniel Veillard76575762002-09-05 14:21:15 +0000441 xmlFree(base); \
442 } else \
443 ctxt->warning(ctxt->userData, ":%d: ", \
Daniel Veillard366a9152002-10-23 20:43:53 +0000444 (int) (long) node->content); \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000445 }
446
Owen Taylor3473f882001-02-23 17:55:21 +0000447#define CHECK_DTD \
448 if (doc == NULL) return(0); \
449 else if ((doc->intSubset == NULL) && \
450 (doc->extSubset == NULL)) return(0)
451
Daniel Veillarda10efa82001-04-18 13:09:01 +0000452static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
453 int create);
Owen Taylor3473f882001-02-23 17:55:21 +0000454xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
455
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000456#ifdef LIBXML_REGEXP_ENABLED
457
458/************************************************************************
459 * *
460 * Content model validation based on the regexps *
461 * *
462 ************************************************************************/
463
464/**
465 * xmlValidBuildAContentModel:
466 * @content: the content model
467 * @ctxt: the schema parser context
468 * @name: the element name whose content is being built
469 *
470 * Generate the automata sequence needed for that type
471 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000472 * Returns 1 if successful or 0 in case of error.
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000473 */
474static int
475xmlValidBuildAContentModel(xmlElementContentPtr content,
476 xmlValidCtxtPtr ctxt,
477 const xmlChar *name) {
478 if (content == NULL) {
479 VERROR(ctxt->userData,
480 "Found unexpected type = NULL in %s content model\n", name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000481 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000482 }
483 switch (content->type) {
484 case XML_ELEMENT_CONTENT_PCDATA:
485 VERROR(ctxt->userData, "ContentModel found PCDATA for element %s\n",
486 name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000487 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000488 break;
489 case XML_ELEMENT_CONTENT_ELEMENT: {
490 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillardc00cda82003-04-07 10:22:39 +0000491 xmlChar fn[50];
492 xmlChar *fullname;
493
494 fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
495 if (fullname == NULL) {
496 VERROR(ctxt->userData, "Out of memory\n");
497 return(0);
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000498 }
499
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000500 switch (content->ocur) {
501 case XML_ELEMENT_CONTENT_ONCE:
502 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000503 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000504 break;
505 case XML_ELEMENT_CONTENT_OPT:
506 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000507 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000508 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
509 break;
510 case XML_ELEMENT_CONTENT_PLUS:
511 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000512 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000513 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000514 ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000515 break;
516 case XML_ELEMENT_CONTENT_MULT:
517 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000518 ctxt->state, fullname, NULL);
Daniel Veillard57e79b32003-02-04 15:33:12 +0000519 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
520 NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000521 break;
522 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000523 if ((fullname != fn) && (fullname != content->name))
524 xmlFree(fullname);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000525 break;
526 }
527 case XML_ELEMENT_CONTENT_SEQ: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000528 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000529 xmlElementContentOccur ocur;
530
531 /*
532 * Simply iterate over the content
533 */
534 oldstate = ctxt->state;
535 ocur = content->ocur;
Daniel Veillardf4be0182003-02-24 19:54:33 +0000536 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
537 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
538 oldstate = ctxt->state;
539 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000540 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000541 xmlValidBuildAContentModel(content->c1, ctxt, name);
542 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000543 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
544 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
545 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000546 oldend = ctxt->state;
547 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000548 switch (ocur) {
549 case XML_ELEMENT_CONTENT_ONCE:
550 break;
551 case XML_ELEMENT_CONTENT_OPT:
552 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
553 break;
554 case XML_ELEMENT_CONTENT_MULT:
555 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000556 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000557 break;
558 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000559 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000560 break;
561 }
562 break;
563 }
564 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000565 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000566 xmlElementContentOccur ocur;
567
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000568 ocur = content->ocur;
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000569 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
570 (ocur == XML_ELEMENT_CONTENT_MULT)) {
571 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
572 ctxt->state, NULL);
573 }
574 oldstate = ctxt->state;
575 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000576
577 /*
578 * iterate over the subtypes and remerge the end with an
579 * epsilon transition
580 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000581 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000582 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000583 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000584 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000585 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000586 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
587 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000588 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000589 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000590 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
591 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000592 switch (ocur) {
593 case XML_ELEMENT_CONTENT_ONCE:
594 break;
595 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000596 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000597 break;
598 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000599 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
600 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000601 break;
602 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000603 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000604 break;
605 }
606 break;
607 }
608 default:
609 VERROR(ctxt->userData, "ContentModel broken for element %s\n",
610 name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000611 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000612 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000613 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000614}
615/**
616 * xmlValidBuildContentModel:
617 * @ctxt: a validation context
618 * @elem: an element declaration node
619 *
620 * (Re)Build the automata associated to the content model of this
621 * element
622 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000623 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000624 */
625int
626xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
627 xmlAutomataStatePtr start;
628
629 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000630 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000631 if (elem->type != XML_ELEMENT_DECL)
632 return(0);
633 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
634 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000635 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000636 if (elem->contModel != NULL) {
637 if (!xmlRegexpIsDeterminist(elem->contModel)) {
638 ctxt->valid = 0;
639 return(0);
640 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000641 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000642 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000643
644 ctxt->am = xmlNewAutomata();
645 if (ctxt->am == NULL) {
646 VERROR(ctxt->userData, "Cannot create automata for element %s\n",
647 elem->name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000648 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000649 }
650 start = ctxt->state = xmlAutomataGetInitState(ctxt->am);
651 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
652 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000653 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000654 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000655 char expr[5000];
656 expr[0] = 0;
657 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
658 VERROR(ctxt->userData, "Content model of %s is not determinist: %s\n",
659 elem->name, expr);
660#ifdef DEBUG_REGEXP_ALGO
661 xmlRegexpPrint(stderr, elem->contModel);
662#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000663 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000664 ctxt->state = NULL;
665 xmlFreeAutomata(ctxt->am);
666 ctxt->am = NULL;
667 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000668 }
669 ctxt->state = NULL;
670 xmlFreeAutomata(ctxt->am);
671 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000672 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000673}
674
675#endif /* LIBXML_REGEXP_ENABLED */
676
Owen Taylor3473f882001-02-23 17:55:21 +0000677/****************************************************************
678 * *
679 * Util functions for data allocation/deallocation *
680 * *
681 ****************************************************************/
682
683/**
684 * xmlNewElementContent:
685 * @name: the subelement name or NULL
686 * @type: the type of element content decl
687 *
688 * Allocate an element content structure.
689 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000690 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000691 */
692xmlElementContentPtr
693xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
694 xmlElementContentPtr ret;
695
696 switch(type) {
697 case XML_ELEMENT_CONTENT_ELEMENT:
698 if (name == NULL) {
699 xmlGenericError(xmlGenericErrorContext,
700 "xmlNewElementContent : name == NULL !\n");
701 }
702 break;
703 case XML_ELEMENT_CONTENT_PCDATA:
704 case XML_ELEMENT_CONTENT_SEQ:
705 case XML_ELEMENT_CONTENT_OR:
706 if (name != NULL) {
707 xmlGenericError(xmlGenericErrorContext,
708 "xmlNewElementContent : name != NULL !\n");
709 }
710 break;
711 default:
712 xmlGenericError(xmlGenericErrorContext,
713 "xmlNewElementContent: unknown type %d\n", type);
714 return(NULL);
715 }
716 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
717 if (ret == NULL) {
718 xmlGenericError(xmlGenericErrorContext,
719 "xmlNewElementContent : out of memory!\n");
720 return(NULL);
721 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000722 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000723 ret->type = type;
724 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000725 if (name != NULL) {
726 xmlChar *prefix = NULL;
727 ret->name = xmlSplitQName2(name, &prefix);
728 if (ret->name == NULL)
729 ret->name = xmlStrdup(name);
730 ret->prefix = prefix;
731 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000732 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000733 ret->prefix = NULL;
734 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000735 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000736 return(ret);
737}
738
739/**
740 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000741 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +0000742 *
743 * Build a copy of an element content description.
744 *
745 * Returns the new xmlElementContentPtr or NULL in case of error.
746 */
747xmlElementContentPtr
748xmlCopyElementContent(xmlElementContentPtr cur) {
749 xmlElementContentPtr ret;
750
751 if (cur == NULL) return(NULL);
752 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
753 if (ret == NULL) {
754 xmlGenericError(xmlGenericErrorContext,
755 "xmlCopyElementContent : out of memory\n");
756 return(NULL);
757 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000758 if (cur->prefix != NULL)
759 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000760 ret->ocur = cur->ocur;
761 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000762 if (ret->c1 != NULL)
763 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000764 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000765 if (ret->c2 != NULL)
766 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000767 return(ret);
768}
769
770/**
771 * xmlFreeElementContent:
772 * @cur: the element content tree to free
773 *
774 * Free an element content structure. This is a recursive call !
775 */
776void
777xmlFreeElementContent(xmlElementContentPtr cur) {
778 if (cur == NULL) return;
779 switch (cur->type) {
780 case XML_ELEMENT_CONTENT_PCDATA:
781 case XML_ELEMENT_CONTENT_ELEMENT:
782 case XML_ELEMENT_CONTENT_SEQ:
783 case XML_ELEMENT_CONTENT_OR:
784 break;
785 default:
786 xmlGenericError(xmlGenericErrorContext,
787 "xmlFreeElementContent : type %d\n", cur->type);
788 return;
789 }
790 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
791 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
792 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000793 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000794 xmlFree(cur);
795}
796
797/**
798 * xmlDumpElementContent:
799 * @buf: An XML buffer
800 * @content: An element table
801 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
802 *
803 * This will dump the content of the element table as an XML DTD definition
804 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000805static void
Owen Taylor3473f882001-02-23 17:55:21 +0000806xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
807 if (content == NULL) return;
808
809 if (glob) xmlBufferWriteChar(buf, "(");
810 switch (content->type) {
811 case XML_ELEMENT_CONTENT_PCDATA:
812 xmlBufferWriteChar(buf, "#PCDATA");
813 break;
814 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000815 if (content->prefix != NULL) {
816 xmlBufferWriteCHAR(buf, content->prefix);
817 xmlBufferWriteChar(buf, ":");
818 }
Owen Taylor3473f882001-02-23 17:55:21 +0000819 xmlBufferWriteCHAR(buf, content->name);
820 break;
821 case XML_ELEMENT_CONTENT_SEQ:
822 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
823 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
824 xmlDumpElementContent(buf, content->c1, 1);
825 else
826 xmlDumpElementContent(buf, content->c1, 0);
827 xmlBufferWriteChar(buf, " , ");
828 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
829 xmlDumpElementContent(buf, content->c2, 1);
830 else
831 xmlDumpElementContent(buf, content->c2, 0);
832 break;
833 case XML_ELEMENT_CONTENT_OR:
834 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
835 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
836 xmlDumpElementContent(buf, content->c1, 1);
837 else
838 xmlDumpElementContent(buf, content->c1, 0);
839 xmlBufferWriteChar(buf, " | ");
840 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
841 xmlDumpElementContent(buf, content->c2, 1);
842 else
843 xmlDumpElementContent(buf, content->c2, 0);
844 break;
845 default:
846 xmlGenericError(xmlGenericErrorContext,
847 "xmlDumpElementContent: unknown type %d\n",
848 content->type);
849 }
850 if (glob)
851 xmlBufferWriteChar(buf, ")");
852 switch (content->ocur) {
853 case XML_ELEMENT_CONTENT_ONCE:
854 break;
855 case XML_ELEMENT_CONTENT_OPT:
856 xmlBufferWriteChar(buf, "?");
857 break;
858 case XML_ELEMENT_CONTENT_MULT:
859 xmlBufferWriteChar(buf, "*");
860 break;
861 case XML_ELEMENT_CONTENT_PLUS:
862 xmlBufferWriteChar(buf, "+");
863 break;
864 }
865}
866
867/**
868 * xmlSprintfElementContent:
869 * @buf: an output buffer
870 * @content: An element table
871 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
872 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000873 * Deprecated, unsafe, use xmlSnprintfElementContent
874 */
875void
876xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
877 xmlElementContentPtr content ATTRIBUTE_UNUSED,
878 int glob ATTRIBUTE_UNUSED) {
879}
880
881/**
882 * xmlSnprintfElementContent:
883 * @buf: an output buffer
884 * @size: the buffer size
885 * @content: An element table
886 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
887 *
Owen Taylor3473f882001-02-23 17:55:21 +0000888 * This will dump the content of the element content definition
889 * Intended just for the debug routine
890 */
891void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000892xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
893 int len;
894
Owen Taylor3473f882001-02-23 17:55:21 +0000895 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000896 len = strlen(buf);
897 if (size - len < 50) {
898 if ((size - len > 4) && (buf[len - 1] != '.'))
899 strcat(buf, " ...");
900 return;
901 }
Owen Taylor3473f882001-02-23 17:55:21 +0000902 if (glob) strcat(buf, "(");
903 switch (content->type) {
904 case XML_ELEMENT_CONTENT_PCDATA:
905 strcat(buf, "#PCDATA");
906 break;
907 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000908 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000909 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000910 strcat(buf, " ...");
911 return;
912 }
913 strcat(buf, (char *) content->prefix);
914 strcat(buf, ":");
915 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000916 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +0000917 strcat(buf, " ...");
918 return;
919 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000920 if (content->name != NULL)
921 strcat(buf, (char *) content->name);
Owen Taylor3473f882001-02-23 17:55:21 +0000922 break;
923 case XML_ELEMENT_CONTENT_SEQ:
924 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
925 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000926 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000927 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000928 xmlSnprintfElementContent(buf, size, content->c1, 0);
929 len = strlen(buf);
930 if (size - len < 50) {
931 if ((size - len > 4) && (buf[len - 1] != '.'))
932 strcat(buf, " ...");
933 return;
934 }
Owen Taylor3473f882001-02-23 17:55:21 +0000935 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000936 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
937 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
938 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000939 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000940 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000941 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000942 break;
943 case XML_ELEMENT_CONTENT_OR:
944 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
945 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000946 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000947 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000948 xmlSnprintfElementContent(buf, size, content->c1, 0);
949 len = strlen(buf);
950 if (size - len < 50) {
951 if ((size - len > 4) && (buf[len - 1] != '.'))
952 strcat(buf, " ...");
953 return;
954 }
Owen Taylor3473f882001-02-23 17:55:21 +0000955 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000956 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
957 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
958 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000959 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000960 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000961 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000962 break;
963 }
964 if (glob)
965 strcat(buf, ")");
966 switch (content->ocur) {
967 case XML_ELEMENT_CONTENT_ONCE:
968 break;
969 case XML_ELEMENT_CONTENT_OPT:
970 strcat(buf, "?");
971 break;
972 case XML_ELEMENT_CONTENT_MULT:
973 strcat(buf, "*");
974 break;
975 case XML_ELEMENT_CONTENT_PLUS:
976 strcat(buf, "+");
977 break;
978 }
979}
980
981/****************************************************************
982 * *
983 * Registration of DTD declarations *
984 * *
985 ****************************************************************/
986
987/**
988 * xmlCreateElementTable:
989 *
990 * create and initialize an empty element hash table.
991 *
992 * Returns the xmlElementTablePtr just created or NULL in case of error.
993 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000994static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000995xmlCreateElementTable(void) {
996 return(xmlHashCreate(0));
997}
998
999/**
1000 * xmlFreeElement:
1001 * @elem: An element
1002 *
1003 * Deallocate the memory used by an element definition
1004 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001005static void
Owen Taylor3473f882001-02-23 17:55:21 +00001006xmlFreeElement(xmlElementPtr elem) {
1007 if (elem == NULL) return;
1008 xmlUnlinkNode((xmlNodePtr) elem);
1009 xmlFreeElementContent(elem->content);
1010 if (elem->name != NULL)
1011 xmlFree((xmlChar *) elem->name);
1012 if (elem->prefix != NULL)
1013 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001014#ifdef LIBXML_REGEXP_ENABLED
1015 if (elem->contModel != NULL)
1016 xmlRegFreeRegexp(elem->contModel);
1017#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001018 xmlFree(elem);
1019}
1020
1021
1022/**
1023 * xmlAddElementDecl:
1024 * @ctxt: the validation context
1025 * @dtd: pointer to the DTD
1026 * @name: the entity name
1027 * @type: the element type
1028 * @content: the element content tree or NULL
1029 *
1030 * Register a new element declaration
1031 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001032 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001033 */
1034xmlElementPtr
1035xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
1036 xmlElementTypeVal type,
1037 xmlElementContentPtr content) {
1038 xmlElementPtr ret;
1039 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001040 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001041 xmlChar *ns, *uqname;
1042
1043 if (dtd == NULL) {
1044 xmlGenericError(xmlGenericErrorContext,
1045 "xmlAddElementDecl: dtd == NULL\n");
1046 return(NULL);
1047 }
1048 if (name == NULL) {
1049 xmlGenericError(xmlGenericErrorContext,
1050 "xmlAddElementDecl: name == NULL\n");
1051 return(NULL);
1052 }
1053 switch (type) {
1054 case XML_ELEMENT_TYPE_EMPTY:
1055 if (content != NULL) {
1056 xmlGenericError(xmlGenericErrorContext,
1057 "xmlAddElementDecl: content != NULL for EMPTY\n");
1058 return(NULL);
1059 }
1060 break;
1061 case XML_ELEMENT_TYPE_ANY:
1062 if (content != NULL) {
1063 xmlGenericError(xmlGenericErrorContext,
1064 "xmlAddElementDecl: content != NULL for ANY\n");
1065 return(NULL);
1066 }
1067 break;
1068 case XML_ELEMENT_TYPE_MIXED:
1069 if (content == NULL) {
1070 xmlGenericError(xmlGenericErrorContext,
1071 "xmlAddElementDecl: content == NULL for MIXED\n");
1072 return(NULL);
1073 }
1074 break;
1075 case XML_ELEMENT_TYPE_ELEMENT:
1076 if (content == NULL) {
1077 xmlGenericError(xmlGenericErrorContext,
1078 "xmlAddElementDecl: content == NULL for ELEMENT\n");
1079 return(NULL);
1080 }
1081 break;
1082 default:
1083 xmlGenericError(xmlGenericErrorContext,
1084 "xmlAddElementDecl: unknown type %d\n", type);
1085 return(NULL);
1086 }
1087
1088 /*
1089 * check if name is a QName
1090 */
1091 uqname = xmlSplitQName2(name, &ns);
1092 if (uqname != NULL)
1093 name = uqname;
1094
1095 /*
1096 * Create the Element table if needed.
1097 */
1098 table = (xmlElementTablePtr) dtd->elements;
1099 if (table == NULL) {
1100 table = xmlCreateElementTable();
1101 dtd->elements = (void *) table;
1102 }
1103 if (table == NULL) {
1104 xmlGenericError(xmlGenericErrorContext,
1105 "xmlAddElementDecl: Table creation failed!\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001106 if (uqname != NULL)
1107 xmlFree(uqname);
1108 if (ns != NULL)
1109 xmlFree(ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001110 return(NULL);
1111 }
1112
Daniel Veillarda10efa82001-04-18 13:09:01 +00001113 /*
1114 * lookup old attributes inserted on an undefined element in the
1115 * internal subset.
1116 */
1117 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1118 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1119 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1120 oldAttributes = ret->attributes;
1121 ret->attributes = NULL;
1122 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1123 xmlFreeElement(ret);
1124 }
Owen Taylor3473f882001-02-23 17:55:21 +00001125 }
Owen Taylor3473f882001-02-23 17:55:21 +00001126
1127 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001128 * The element may already be present if one of its attribute
1129 * was registered first
1130 */
1131 ret = xmlHashLookup2(table, name, ns);
1132 if (ret != NULL) {
1133 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
1134 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001135 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001136 */
1137 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
1138 if (uqname != NULL)
1139 xmlFree(uqname);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001140 if (ns != NULL)
1141 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001142 return(NULL);
1143 }
1144 } else {
1145 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1146 if (ret == NULL) {
1147 xmlGenericError(xmlGenericErrorContext,
1148 "xmlAddElementDecl: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001149 if (uqname != NULL)
1150 xmlFree(uqname);
1151 if (ns != NULL)
1152 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001153 return(NULL);
1154 }
1155 memset(ret, 0, sizeof(xmlElement));
1156 ret->type = XML_ELEMENT_DECL;
1157
1158 /*
1159 * fill the structure.
1160 */
1161 ret->name = xmlStrdup(name);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001162 if (ret->name == NULL) {
1163 xmlGenericError(xmlGenericErrorContext,
1164 "xmlAddElementDecl: out of memory\n");
1165 if (uqname != NULL)
1166 xmlFree(uqname);
1167 if (ns != NULL)
1168 xmlFree(ns);
1169 xmlFree(ret);
1170 return(NULL);
1171 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001172 ret->prefix = ns;
1173
1174 /*
1175 * Validity Check:
1176 * Insertion must not fail
1177 */
1178 if (xmlHashAddEntry2(table, name, ns, ret)) {
1179 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001180 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001181 */
1182 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
1183 xmlFreeElement(ret);
1184 if (uqname != NULL)
1185 xmlFree(uqname);
1186 return(NULL);
1187 }
1188 }
1189
1190 /*
1191 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001192 */
1193 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +00001194 ret->content = xmlCopyElementContent(content);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001195 ret->attributes = oldAttributes;
Owen Taylor3473f882001-02-23 17:55:21 +00001196
1197 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001198 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001199 */
1200 ret->parent = dtd;
1201 ret->doc = dtd->doc;
1202 if (dtd->last == NULL) {
1203 dtd->children = dtd->last = (xmlNodePtr) ret;
1204 } else {
1205 dtd->last->next = (xmlNodePtr) ret;
1206 ret->prev = dtd->last;
1207 dtd->last = (xmlNodePtr) ret;
1208 }
1209 if (uqname != NULL)
1210 xmlFree(uqname);
1211 return(ret);
1212}
1213
1214/**
1215 * xmlFreeElementTable:
1216 * @table: An element table
1217 *
1218 * Deallocate the memory used by an element hash table.
1219 */
1220void
1221xmlFreeElementTable(xmlElementTablePtr table) {
1222 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1223}
1224
1225/**
1226 * xmlCopyElement:
1227 * @elem: An element
1228 *
1229 * Build a copy of an element.
1230 *
1231 * Returns the new xmlElementPtr or NULL in case of error.
1232 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001233static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001234xmlCopyElement(xmlElementPtr elem) {
1235 xmlElementPtr cur;
1236
1237 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1238 if (cur == NULL) {
1239 xmlGenericError(xmlGenericErrorContext,
1240 "xmlCopyElement: out of memory !\n");
1241 return(NULL);
1242 }
1243 memset(cur, 0, sizeof(xmlElement));
1244 cur->type = XML_ELEMENT_DECL;
1245 cur->etype = elem->etype;
1246 if (elem->name != NULL)
1247 cur->name = xmlStrdup(elem->name);
1248 else
1249 cur->name = NULL;
1250 if (elem->prefix != NULL)
1251 cur->prefix = xmlStrdup(elem->prefix);
1252 else
1253 cur->prefix = NULL;
1254 cur->content = xmlCopyElementContent(elem->content);
1255 /* TODO : rebuild the attribute list on the copy */
1256 cur->attributes = NULL;
1257 return(cur);
1258}
1259
1260/**
1261 * xmlCopyElementTable:
1262 * @table: An element table
1263 *
1264 * Build a copy of an element table.
1265 *
1266 * Returns the new xmlElementTablePtr or NULL in case of error.
1267 */
1268xmlElementTablePtr
1269xmlCopyElementTable(xmlElementTablePtr table) {
1270 return((xmlElementTablePtr) xmlHashCopy(table,
1271 (xmlHashCopier) xmlCopyElement));
1272}
1273
1274/**
1275 * xmlDumpElementDecl:
1276 * @buf: the XML buffer output
1277 * @elem: An element table
1278 *
1279 * This will dump the content of the element declaration as an XML
1280 * DTD definition
1281 */
1282void
1283xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1284 switch (elem->etype) {
1285 case XML_ELEMENT_TYPE_EMPTY:
1286 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001287 if (elem->prefix != NULL) {
1288 xmlBufferWriteCHAR(buf, elem->prefix);
1289 xmlBufferWriteChar(buf, ":");
1290 }
Owen Taylor3473f882001-02-23 17:55:21 +00001291 xmlBufferWriteCHAR(buf, elem->name);
1292 xmlBufferWriteChar(buf, " EMPTY>\n");
1293 break;
1294 case XML_ELEMENT_TYPE_ANY:
1295 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001296 if (elem->prefix != NULL) {
1297 xmlBufferWriteCHAR(buf, elem->prefix);
1298 xmlBufferWriteChar(buf, ":");
1299 }
Owen Taylor3473f882001-02-23 17:55:21 +00001300 xmlBufferWriteCHAR(buf, elem->name);
1301 xmlBufferWriteChar(buf, " ANY>\n");
1302 break;
1303 case XML_ELEMENT_TYPE_MIXED:
1304 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001305 if (elem->prefix != NULL) {
1306 xmlBufferWriteCHAR(buf, elem->prefix);
1307 xmlBufferWriteChar(buf, ":");
1308 }
Owen Taylor3473f882001-02-23 17:55:21 +00001309 xmlBufferWriteCHAR(buf, elem->name);
1310 xmlBufferWriteChar(buf, " ");
1311 xmlDumpElementContent(buf, elem->content, 1);
1312 xmlBufferWriteChar(buf, ">\n");
1313 break;
1314 case XML_ELEMENT_TYPE_ELEMENT:
1315 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001316 if (elem->prefix != NULL) {
1317 xmlBufferWriteCHAR(buf, elem->prefix);
1318 xmlBufferWriteChar(buf, ":");
1319 }
Owen Taylor3473f882001-02-23 17:55:21 +00001320 xmlBufferWriteCHAR(buf, elem->name);
1321 xmlBufferWriteChar(buf, " ");
1322 xmlDumpElementContent(buf, elem->content, 1);
1323 xmlBufferWriteChar(buf, ">\n");
1324 break;
1325 default:
1326 xmlGenericError(xmlGenericErrorContext,
1327 "xmlDumpElementDecl: internal: unknown type %d\n",
1328 elem->etype);
1329 }
1330}
1331
1332/**
1333 * xmlDumpElementTable:
1334 * @buf: the XML buffer output
1335 * @table: An element table
1336 *
1337 * This will dump the content of the element table as an XML DTD definition
1338 */
1339void
1340xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1341 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1342}
1343
1344/**
1345 * xmlCreateEnumeration:
1346 * @name: the enumeration name or NULL
1347 *
1348 * create and initialize an enumeration attribute node.
1349 *
1350 * Returns the xmlEnumerationPtr just created or NULL in case
1351 * of error.
1352 */
1353xmlEnumerationPtr
1354xmlCreateEnumeration(xmlChar *name) {
1355 xmlEnumerationPtr ret;
1356
1357 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1358 if (ret == NULL) {
1359 xmlGenericError(xmlGenericErrorContext,
1360 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1361 (long)sizeof(xmlEnumeration));
1362 return(NULL);
1363 }
1364 memset(ret, 0, sizeof(xmlEnumeration));
1365
1366 if (name != NULL)
1367 ret->name = xmlStrdup(name);
1368 return(ret);
1369}
1370
1371/**
1372 * xmlFreeEnumeration:
1373 * @cur: the tree to free.
1374 *
1375 * free an enumeration attribute node (recursive).
1376 */
1377void
1378xmlFreeEnumeration(xmlEnumerationPtr cur) {
1379 if (cur == NULL) return;
1380
1381 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1382
1383 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001384 xmlFree(cur);
1385}
1386
1387/**
1388 * xmlCopyEnumeration:
1389 * @cur: the tree to copy.
1390 *
1391 * Copy an enumeration attribute node (recursive).
1392 *
1393 * Returns the xmlEnumerationPtr just created or NULL in case
1394 * of error.
1395 */
1396xmlEnumerationPtr
1397xmlCopyEnumeration(xmlEnumerationPtr cur) {
1398 xmlEnumerationPtr ret;
1399
1400 if (cur == NULL) return(NULL);
1401 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1402
1403 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1404 else ret->next = NULL;
1405
1406 return(ret);
1407}
1408
1409/**
1410 * xmlDumpEnumeration:
1411 * @buf: the XML buffer output
1412 * @enum: An enumeration
1413 *
1414 * This will dump the content of the enumeration
1415 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001416static void
Owen Taylor3473f882001-02-23 17:55:21 +00001417xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1418 if (cur == NULL) return;
1419
1420 xmlBufferWriteCHAR(buf, cur->name);
1421 if (cur->next == NULL)
1422 xmlBufferWriteChar(buf, ")");
1423 else {
1424 xmlBufferWriteChar(buf, " | ");
1425 xmlDumpEnumeration(buf, cur->next);
1426 }
1427}
1428
1429/**
1430 * xmlCreateAttributeTable:
1431 *
1432 * create and initialize an empty attribute hash table.
1433 *
1434 * Returns the xmlAttributeTablePtr just created or NULL in case
1435 * of error.
1436 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001437static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001438xmlCreateAttributeTable(void) {
1439 return(xmlHashCreate(0));
1440}
1441
1442/**
1443 * xmlScanAttributeDeclCallback:
1444 * @attr: the attribute decl
1445 * @list: the list to update
1446 *
1447 * Callback called by xmlScanAttributeDecl when a new attribute
1448 * has to be entered in the list.
1449 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001450static void
Owen Taylor3473f882001-02-23 17:55:21 +00001451xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001452 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001453 attr->nexth = *list;
1454 *list = attr;
1455}
1456
1457/**
1458 * xmlScanAttributeDecl:
1459 * @dtd: pointer to the DTD
1460 * @elem: the element name
1461 *
1462 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001463 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001464 *
1465 * Returns the pointer to the first attribute decl in the chain,
1466 * possibly NULL.
1467 */
1468xmlAttributePtr
1469xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1470 xmlAttributePtr ret = NULL;
1471 xmlAttributeTablePtr table;
1472
1473 if (dtd == NULL) {
1474 xmlGenericError(xmlGenericErrorContext,
1475 "xmlScanAttributeDecl: dtd == NULL\n");
1476 return(NULL);
1477 }
1478 if (elem == NULL) {
1479 xmlGenericError(xmlGenericErrorContext,
1480 "xmlScanAttributeDecl: elem == NULL\n");
1481 return(NULL);
1482 }
1483 table = (xmlAttributeTablePtr) dtd->attributes;
1484 if (table == NULL)
1485 return(NULL);
1486
1487 /* WRONG !!! */
1488 xmlHashScan3(table, NULL, NULL, elem,
1489 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1490 return(ret);
1491}
1492
1493/**
1494 * xmlScanIDAttributeDecl:
1495 * @ctxt: the validation context
1496 * @elem: the element name
1497 *
1498 * Verify that the element don't have too many ID attributes
1499 * declared.
1500 *
1501 * Returns the number of ID attributes found.
1502 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001503static int
Owen Taylor3473f882001-02-23 17:55:21 +00001504xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1505 xmlAttributePtr cur;
1506 int ret = 0;
1507
1508 if (elem == NULL) return(0);
1509 cur = elem->attributes;
1510 while (cur != NULL) {
1511 if (cur->atype == XML_ATTRIBUTE_ID) {
1512 ret ++;
1513 if (ret > 1)
1514 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001515 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001516 elem->name, cur->name);
1517 }
1518 cur = cur->nexth;
1519 }
1520 return(ret);
1521}
1522
1523/**
1524 * xmlFreeAttribute:
1525 * @elem: An attribute
1526 *
1527 * Deallocate the memory used by an attribute definition
1528 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001529static void
Owen Taylor3473f882001-02-23 17:55:21 +00001530xmlFreeAttribute(xmlAttributePtr attr) {
1531 if (attr == NULL) return;
1532 xmlUnlinkNode((xmlNodePtr) attr);
1533 if (attr->tree != NULL)
1534 xmlFreeEnumeration(attr->tree);
1535 if (attr->elem != NULL)
1536 xmlFree((xmlChar *) attr->elem);
1537 if (attr->name != NULL)
1538 xmlFree((xmlChar *) attr->name);
1539 if (attr->defaultValue != NULL)
1540 xmlFree((xmlChar *) attr->defaultValue);
1541 if (attr->prefix != NULL)
1542 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001543 xmlFree(attr);
1544}
1545
1546
1547/**
1548 * xmlAddAttributeDecl:
1549 * @ctxt: the validation context
1550 * @dtd: pointer to the DTD
1551 * @elem: the element name
1552 * @name: the attribute name
1553 * @ns: the attribute namespace prefix
1554 * @type: the attribute type
1555 * @def: the attribute default type
1556 * @defaultValue: the attribute default value
1557 * @tree: if it's an enumeration, the associated list
1558 *
1559 * Register a new attribute declaration
1560 * Note that @tree becomes the ownership of the DTD
1561 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001562 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001563 */
1564xmlAttributePtr
1565xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1566 const xmlChar *name, const xmlChar *ns,
1567 xmlAttributeType type, xmlAttributeDefault def,
1568 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1569 xmlAttributePtr ret;
1570 xmlAttributeTablePtr table;
1571 xmlElementPtr elemDef;
1572
1573 if (dtd == NULL) {
1574 xmlGenericError(xmlGenericErrorContext,
1575 "xmlAddAttributeDecl: dtd == NULL\n");
1576 xmlFreeEnumeration(tree);
1577 return(NULL);
1578 }
1579 if (name == NULL) {
1580 xmlGenericError(xmlGenericErrorContext,
1581 "xmlAddAttributeDecl: name == NULL\n");
1582 xmlFreeEnumeration(tree);
1583 return(NULL);
1584 }
1585 if (elem == NULL) {
1586 xmlGenericError(xmlGenericErrorContext,
1587 "xmlAddAttributeDecl: elem == NULL\n");
1588 xmlFreeEnumeration(tree);
1589 return(NULL);
1590 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001591
Owen Taylor3473f882001-02-23 17:55:21 +00001592 /*
1593 * Check the type and possibly the default value.
1594 */
1595 switch (type) {
1596 case XML_ATTRIBUTE_CDATA:
1597 break;
1598 case XML_ATTRIBUTE_ID:
1599 break;
1600 case XML_ATTRIBUTE_IDREF:
1601 break;
1602 case XML_ATTRIBUTE_IDREFS:
1603 break;
1604 case XML_ATTRIBUTE_ENTITY:
1605 break;
1606 case XML_ATTRIBUTE_ENTITIES:
1607 break;
1608 case XML_ATTRIBUTE_NMTOKEN:
1609 break;
1610 case XML_ATTRIBUTE_NMTOKENS:
1611 break;
1612 case XML_ATTRIBUTE_ENUMERATION:
1613 break;
1614 case XML_ATTRIBUTE_NOTATION:
1615 break;
1616 default:
1617 xmlGenericError(xmlGenericErrorContext,
1618 "xmlAddAttributeDecl: unknown type %d\n", type);
1619 xmlFreeEnumeration(tree);
1620 return(NULL);
1621 }
1622 if ((defaultValue != NULL) &&
1623 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillard58e44c92002-08-02 22:19:49 +00001624 VERROR(ctxt->userData, "Attribute %s of %s: invalid default value\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001625 elem, name, defaultValue);
1626 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001627 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001628 }
1629
1630 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001631 * Check first that an attribute defined in the external subset wasn't
1632 * already defined in the internal subset
1633 */
1634 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1635 (dtd->doc->intSubset != NULL) &&
1636 (dtd->doc->intSubset->attributes != NULL)) {
1637 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1638 if (ret != NULL)
1639 return(NULL);
1640 }
1641
1642 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001643 * Create the Attribute table if needed.
1644 */
1645 table = (xmlAttributeTablePtr) dtd->attributes;
1646 if (table == NULL) {
1647 table = xmlCreateAttributeTable();
1648 dtd->attributes = (void *) table;
1649 }
1650 if (table == NULL) {
1651 xmlGenericError(xmlGenericErrorContext,
1652 "xmlAddAttributeDecl: Table creation failed!\n");
1653 return(NULL);
1654 }
1655
1656
1657 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1658 if (ret == NULL) {
1659 xmlGenericError(xmlGenericErrorContext,
1660 "xmlAddAttributeDecl: out of memory\n");
1661 return(NULL);
1662 }
1663 memset(ret, 0, sizeof(xmlAttribute));
1664 ret->type = XML_ATTRIBUTE_DECL;
1665
1666 /*
1667 * fill the structure.
1668 */
1669 ret->atype = type;
1670 ret->name = xmlStrdup(name);
1671 ret->prefix = xmlStrdup(ns);
1672 ret->elem = xmlStrdup(elem);
1673 ret->def = def;
1674 ret->tree = tree;
1675 if (defaultValue != NULL)
1676 ret->defaultValue = xmlStrdup(defaultValue);
1677
1678 /*
1679 * Validity Check:
1680 * Search the DTD for previous declarations of the ATTLIST
1681 */
1682 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1683 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001684 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001685 */
1686 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00001687 "Attribute %s of element %s: already defined\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001688 name, elem);
1689 xmlFreeAttribute(ret);
1690 return(NULL);
1691 }
1692
1693 /*
1694 * Validity Check:
1695 * Multiple ID per element
1696 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001697 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001698 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001699
Owen Taylor3473f882001-02-23 17:55:21 +00001700 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001701 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001702 VERROR(ctxt->userData,
1703 "Element %s has too may ID attributes defined : %s\n",
1704 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001705 ctxt->valid = 0;
1706 }
1707
Daniel Veillard48da9102001-08-07 01:10:10 +00001708 /*
1709 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001710 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001711 */
1712 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1713 ((ret->prefix != NULL &&
1714 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1715 ret->nexth = elemDef->attributes;
1716 elemDef->attributes = ret;
1717 } else {
1718 xmlAttributePtr tmp = elemDef->attributes;
1719
1720 while ((tmp != NULL) &&
1721 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1722 ((ret->prefix != NULL &&
1723 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1724 if (tmp->nexth == NULL)
1725 break;
1726 tmp = tmp->nexth;
1727 }
1728 if (tmp != NULL) {
1729 ret->nexth = tmp->nexth;
1730 tmp->nexth = ret;
1731 } else {
1732 ret->nexth = elemDef->attributes;
1733 elemDef->attributes = ret;
1734 }
1735 }
Owen Taylor3473f882001-02-23 17:55:21 +00001736 }
1737
1738 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001739 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001740 */
1741 ret->parent = dtd;
1742 ret->doc = dtd->doc;
1743 if (dtd->last == NULL) {
1744 dtd->children = dtd->last = (xmlNodePtr) ret;
1745 } else {
1746 dtd->last->next = (xmlNodePtr) ret;
1747 ret->prev = dtd->last;
1748 dtd->last = (xmlNodePtr) ret;
1749 }
1750 return(ret);
1751}
1752
1753/**
1754 * xmlFreeAttributeTable:
1755 * @table: An attribute table
1756 *
1757 * Deallocate the memory used by an entities hash table.
1758 */
1759void
1760xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1761 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1762}
1763
1764/**
1765 * xmlCopyAttribute:
1766 * @attr: An attribute
1767 *
1768 * Build a copy of an attribute.
1769 *
1770 * Returns the new xmlAttributePtr or NULL in case of error.
1771 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001772static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001773xmlCopyAttribute(xmlAttributePtr attr) {
1774 xmlAttributePtr cur;
1775
1776 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1777 if (cur == NULL) {
1778 xmlGenericError(xmlGenericErrorContext,
1779 "xmlCopyAttribute: out of memory !\n");
1780 return(NULL);
1781 }
1782 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001783 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001784 cur->atype = attr->atype;
1785 cur->def = attr->def;
1786 cur->tree = xmlCopyEnumeration(attr->tree);
1787 if (attr->elem != NULL)
1788 cur->elem = xmlStrdup(attr->elem);
1789 if (attr->name != NULL)
1790 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001791 if (attr->prefix != NULL)
1792 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001793 if (attr->defaultValue != NULL)
1794 cur->defaultValue = xmlStrdup(attr->defaultValue);
1795 return(cur);
1796}
1797
1798/**
1799 * xmlCopyAttributeTable:
1800 * @table: An attribute table
1801 *
1802 * Build a copy of an attribute table.
1803 *
1804 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1805 */
1806xmlAttributeTablePtr
1807xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1808 return((xmlAttributeTablePtr) xmlHashCopy(table,
1809 (xmlHashCopier) xmlCopyAttribute));
1810}
1811
1812/**
1813 * xmlDumpAttributeDecl:
1814 * @buf: the XML buffer output
1815 * @attr: An attribute declaration
1816 *
1817 * This will dump the content of the attribute declaration as an XML
1818 * DTD definition
1819 */
1820void
1821xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1822 xmlBufferWriteChar(buf, "<!ATTLIST ");
1823 xmlBufferWriteCHAR(buf, attr->elem);
1824 xmlBufferWriteChar(buf, " ");
1825 if (attr->prefix != NULL) {
1826 xmlBufferWriteCHAR(buf, attr->prefix);
1827 xmlBufferWriteChar(buf, ":");
1828 }
1829 xmlBufferWriteCHAR(buf, attr->name);
1830 switch (attr->atype) {
1831 case XML_ATTRIBUTE_CDATA:
1832 xmlBufferWriteChar(buf, " CDATA");
1833 break;
1834 case XML_ATTRIBUTE_ID:
1835 xmlBufferWriteChar(buf, " ID");
1836 break;
1837 case XML_ATTRIBUTE_IDREF:
1838 xmlBufferWriteChar(buf, " IDREF");
1839 break;
1840 case XML_ATTRIBUTE_IDREFS:
1841 xmlBufferWriteChar(buf, " IDREFS");
1842 break;
1843 case XML_ATTRIBUTE_ENTITY:
1844 xmlBufferWriteChar(buf, " ENTITY");
1845 break;
1846 case XML_ATTRIBUTE_ENTITIES:
1847 xmlBufferWriteChar(buf, " ENTITIES");
1848 break;
1849 case XML_ATTRIBUTE_NMTOKEN:
1850 xmlBufferWriteChar(buf, " NMTOKEN");
1851 break;
1852 case XML_ATTRIBUTE_NMTOKENS:
1853 xmlBufferWriteChar(buf, " NMTOKENS");
1854 break;
1855 case XML_ATTRIBUTE_ENUMERATION:
1856 xmlBufferWriteChar(buf, " (");
1857 xmlDumpEnumeration(buf, attr->tree);
1858 break;
1859 case XML_ATTRIBUTE_NOTATION:
1860 xmlBufferWriteChar(buf, " NOTATION (");
1861 xmlDumpEnumeration(buf, attr->tree);
1862 break;
1863 default:
1864 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001865 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001866 attr->atype);
1867 }
1868 switch (attr->def) {
1869 case XML_ATTRIBUTE_NONE:
1870 break;
1871 case XML_ATTRIBUTE_REQUIRED:
1872 xmlBufferWriteChar(buf, " #REQUIRED");
1873 break;
1874 case XML_ATTRIBUTE_IMPLIED:
1875 xmlBufferWriteChar(buf, " #IMPLIED");
1876 break;
1877 case XML_ATTRIBUTE_FIXED:
1878 xmlBufferWriteChar(buf, " #FIXED");
1879 break;
1880 default:
1881 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001882 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001883 attr->def);
1884 }
1885 if (attr->defaultValue != NULL) {
1886 xmlBufferWriteChar(buf, " ");
1887 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1888 }
1889 xmlBufferWriteChar(buf, ">\n");
1890}
1891
1892/**
1893 * xmlDumpAttributeTable:
1894 * @buf: the XML buffer output
1895 * @table: An attribute table
1896 *
1897 * This will dump the content of the attribute table as an XML DTD definition
1898 */
1899void
1900xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1901 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1902}
1903
1904/************************************************************************
1905 * *
1906 * NOTATIONs *
1907 * *
1908 ************************************************************************/
1909/**
1910 * xmlCreateNotationTable:
1911 *
1912 * create and initialize an empty notation hash table.
1913 *
1914 * Returns the xmlNotationTablePtr just created or NULL in case
1915 * of error.
1916 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001917static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001918xmlCreateNotationTable(void) {
1919 return(xmlHashCreate(0));
1920}
1921
1922/**
1923 * xmlFreeNotation:
1924 * @not: A notation
1925 *
1926 * Deallocate the memory used by an notation definition
1927 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001928static void
Owen Taylor3473f882001-02-23 17:55:21 +00001929xmlFreeNotation(xmlNotationPtr nota) {
1930 if (nota == NULL) return;
1931 if (nota->name != NULL)
1932 xmlFree((xmlChar *) nota->name);
1933 if (nota->PublicID != NULL)
1934 xmlFree((xmlChar *) nota->PublicID);
1935 if (nota->SystemID != NULL)
1936 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001937 xmlFree(nota);
1938}
1939
1940
1941/**
1942 * xmlAddNotationDecl:
1943 * @dtd: pointer to the DTD
1944 * @ctxt: the validation context
1945 * @name: the entity name
1946 * @PublicID: the public identifier or NULL
1947 * @SystemID: the system identifier or NULL
1948 *
1949 * Register a new notation declaration
1950 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001951 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001952 */
1953xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001954xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001955 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001956 const xmlChar *PublicID, const xmlChar *SystemID) {
1957 xmlNotationPtr ret;
1958 xmlNotationTablePtr table;
1959
1960 if (dtd == NULL) {
1961 xmlGenericError(xmlGenericErrorContext,
1962 "xmlAddNotationDecl: dtd == NULL\n");
1963 return(NULL);
1964 }
1965 if (name == NULL) {
1966 xmlGenericError(xmlGenericErrorContext,
1967 "xmlAddNotationDecl: name == NULL\n");
1968 return(NULL);
1969 }
1970 if ((PublicID == NULL) && (SystemID == NULL)) {
1971 xmlGenericError(xmlGenericErrorContext,
1972 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00001973 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001974 }
1975
1976 /*
1977 * Create the Notation table if needed.
1978 */
1979 table = (xmlNotationTablePtr) dtd->notations;
1980 if (table == NULL)
1981 dtd->notations = table = xmlCreateNotationTable();
1982 if (table == NULL) {
1983 xmlGenericError(xmlGenericErrorContext,
1984 "xmlAddNotationDecl: Table creation failed!\n");
1985 return(NULL);
1986 }
1987
1988 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1989 if (ret == NULL) {
1990 xmlGenericError(xmlGenericErrorContext,
1991 "xmlAddNotationDecl: out of memory\n");
1992 return(NULL);
1993 }
1994 memset(ret, 0, sizeof(xmlNotation));
1995
1996 /*
1997 * fill the structure.
1998 */
1999 ret->name = xmlStrdup(name);
2000 if (SystemID != NULL)
2001 ret->SystemID = xmlStrdup(SystemID);
2002 if (PublicID != NULL)
2003 ret->PublicID = xmlStrdup(PublicID);
2004
2005 /*
2006 * Validity Check:
2007 * Check the DTD for previous declarations of the ATTLIST
2008 */
2009 if (xmlHashAddEntry(table, name, ret)) {
2010 xmlGenericError(xmlGenericErrorContext,
2011 "xmlAddNotationDecl: %s already defined\n", name);
2012 xmlFreeNotation(ret);
2013 return(NULL);
2014 }
2015 return(ret);
2016}
2017
2018/**
2019 * xmlFreeNotationTable:
2020 * @table: An notation table
2021 *
2022 * Deallocate the memory used by an entities hash table.
2023 */
2024void
2025xmlFreeNotationTable(xmlNotationTablePtr table) {
2026 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2027}
2028
2029/**
2030 * xmlCopyNotation:
2031 * @nota: A notation
2032 *
2033 * Build a copy of a notation.
2034 *
2035 * Returns the new xmlNotationPtr or NULL in case of error.
2036 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002037static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002038xmlCopyNotation(xmlNotationPtr nota) {
2039 xmlNotationPtr cur;
2040
2041 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2042 if (cur == NULL) {
2043 xmlGenericError(xmlGenericErrorContext,
2044 "xmlCopyNotation: out of memory !\n");
2045 return(NULL);
2046 }
2047 if (nota->name != NULL)
2048 cur->name = xmlStrdup(nota->name);
2049 else
2050 cur->name = NULL;
2051 if (nota->PublicID != NULL)
2052 cur->PublicID = xmlStrdup(nota->PublicID);
2053 else
2054 cur->PublicID = NULL;
2055 if (nota->SystemID != NULL)
2056 cur->SystemID = xmlStrdup(nota->SystemID);
2057 else
2058 cur->SystemID = NULL;
2059 return(cur);
2060}
2061
2062/**
2063 * xmlCopyNotationTable:
2064 * @table: A notation table
2065 *
2066 * Build a copy of a notation table.
2067 *
2068 * Returns the new xmlNotationTablePtr or NULL in case of error.
2069 */
2070xmlNotationTablePtr
2071xmlCopyNotationTable(xmlNotationTablePtr table) {
2072 return((xmlNotationTablePtr) xmlHashCopy(table,
2073 (xmlHashCopier) xmlCopyNotation));
2074}
2075
2076/**
2077 * xmlDumpNotationDecl:
2078 * @buf: the XML buffer output
2079 * @nota: A notation declaration
2080 *
2081 * This will dump the content the notation declaration as an XML DTD definition
2082 */
2083void
2084xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2085 xmlBufferWriteChar(buf, "<!NOTATION ");
2086 xmlBufferWriteCHAR(buf, nota->name);
2087 if (nota->PublicID != NULL) {
2088 xmlBufferWriteChar(buf, " PUBLIC ");
2089 xmlBufferWriteQuotedString(buf, nota->PublicID);
2090 if (nota->SystemID != NULL) {
2091 xmlBufferWriteChar(buf, " ");
2092 xmlBufferWriteCHAR(buf, nota->SystemID);
2093 }
2094 } else {
2095 xmlBufferWriteChar(buf, " SYSTEM ");
2096 xmlBufferWriteCHAR(buf, nota->SystemID);
2097 }
2098 xmlBufferWriteChar(buf, " >\n");
2099}
2100
2101/**
2102 * xmlDumpNotationTable:
2103 * @buf: the XML buffer output
2104 * @table: A notation table
2105 *
2106 * This will dump the content of the notation table as an XML DTD definition
2107 */
2108void
2109xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2110 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
2111}
2112
2113/************************************************************************
2114 * *
2115 * IDs *
2116 * *
2117 ************************************************************************/
2118/**
2119 * xmlCreateIDTable:
2120 *
2121 * create and initialize an empty id hash table.
2122 *
2123 * Returns the xmlIDTablePtr just created or NULL in case
2124 * of error.
2125 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002126static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002127xmlCreateIDTable(void) {
2128 return(xmlHashCreate(0));
2129}
2130
2131/**
2132 * xmlFreeID:
2133 * @not: A id
2134 *
2135 * Deallocate the memory used by an id definition
2136 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002137static void
Owen Taylor3473f882001-02-23 17:55:21 +00002138xmlFreeID(xmlIDPtr id) {
2139 if (id == NULL) return;
2140 if (id->value != NULL)
2141 xmlFree((xmlChar *) id->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002142 if (id->name != NULL)
2143 xmlFree((xmlChar *) id->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002144 xmlFree(id);
2145}
2146
2147/**
2148 * xmlAddID:
2149 * @ctxt: the validation context
2150 * @doc: pointer to the document
2151 * @value: the value name
2152 * @attr: the attribute holding the ID
2153 *
2154 * Register a new id declaration
2155 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002156 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002157 */
2158xmlIDPtr
2159xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2160 xmlAttrPtr attr) {
2161 xmlIDPtr ret;
2162 xmlIDTablePtr table;
2163
2164 if (doc == NULL) {
2165 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002166 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002167 return(NULL);
2168 }
2169 if (value == NULL) {
2170 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002171 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002172 return(NULL);
2173 }
2174 if (attr == NULL) {
2175 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002176 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002177 return(NULL);
2178 }
2179
2180 /*
2181 * Create the ID table if needed.
2182 */
2183 table = (xmlIDTablePtr) doc->ids;
2184 if (table == NULL)
2185 doc->ids = table = xmlCreateIDTable();
2186 if (table == NULL) {
2187 xmlGenericError(xmlGenericErrorContext,
2188 "xmlAddID: Table creation failed!\n");
2189 return(NULL);
2190 }
2191
2192 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2193 if (ret == NULL) {
2194 xmlGenericError(xmlGenericErrorContext,
2195 "xmlAddID: out of memory\n");
2196 return(NULL);
2197 }
2198
2199 /*
2200 * fill the structure.
2201 */
2202 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002203 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2204 /*
2205 * Operating in streaming mode, attr is gonna disapear
2206 */
2207 ret->name = xmlStrdup(attr->name);
2208 ret->attr = NULL;
2209 } else {
2210 ret->attr = attr;
2211 ret->name = NULL;
2212 }
2213 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002214
2215 if (xmlHashAddEntry(table, value, ret) < 0) {
2216 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002217 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002218 */
Daniel Veillard76575762002-09-05 14:21:15 +00002219 if (ctxt != NULL) {
2220 VECTXT(ctxt, attr->parent);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002221 VERROR(ctxt->userData, "ID %s already defined\n", value);
Daniel Veillard76575762002-09-05 14:21:15 +00002222 }
Owen Taylor3473f882001-02-23 17:55:21 +00002223 xmlFreeID(ret);
2224 return(NULL);
2225 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002226 if (attr != NULL)
2227 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002228 return(ret);
2229}
2230
2231/**
2232 * xmlFreeIDTable:
2233 * @table: An id table
2234 *
2235 * Deallocate the memory used by an ID hash table.
2236 */
2237void
2238xmlFreeIDTable(xmlIDTablePtr table) {
2239 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2240}
2241
2242/**
2243 * xmlIsID:
2244 * @doc: the document
2245 * @elem: the element carrying the attribute
2246 * @attr: the attribute
2247 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002248 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002249 * then this is done if DTD loading has been requested. In the case
2250 * of HTML documents parsed with the HTML parser, then ID detection is
2251 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002252 *
2253 * Returns 0 or 1 depending on the lookup result
2254 */
2255int
2256xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2257 if (doc == NULL) return(0);
2258 if (attr == NULL) return(0);
2259 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2260 return(0);
2261 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2262 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2263 (xmlStrEqual(BAD_CAST "name", attr->name)))
2264 return(1);
2265 return(0);
2266 } else {
2267 xmlAttributePtr attrDecl;
2268
2269 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002270 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00002271 xmlChar fn[50];
Daniel Veillard37f961d2002-07-06 17:53:56 +00002272 xmlChar *fullname;
Daniel Veillardc00cda82003-04-07 10:22:39 +00002273
2274 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002275 if (fullname == NULL)
2276 return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002277 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2278 attr->name);
2279 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2280 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2281 attr->name);
Daniel Veillardc00cda82003-04-07 10:22:39 +00002282 if ((fullname != fn) && (fullname != elem->name))
2283 xmlFree(fullname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002284 } else {
2285 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2286 attr->name);
2287 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2288 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2289 attr->name);
2290 }
Owen Taylor3473f882001-02-23 17:55:21 +00002291
2292 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2293 return(1);
2294 }
2295 return(0);
2296}
2297
2298/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002299 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002300 * @doc: the document
2301 * @attr: the attribute
2302 *
2303 * Remove the given attribute from the ID table maintained internally.
2304 *
2305 * Returns -1 if the lookup failed and 0 otherwise
2306 */
2307int
2308xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2309 xmlAttrPtr cur;
2310 xmlIDTablePtr table;
2311 xmlChar *ID;
2312
2313 if (doc == NULL) return(-1);
2314 if (attr == NULL) return(-1);
2315 table = (xmlIDTablePtr) doc->ids;
2316 if (table == NULL)
2317 return(-1);
2318
2319 if (attr == NULL)
2320 return(-1);
2321 ID = xmlNodeListGetString(doc, attr->children, 1);
2322 if (ID == NULL)
2323 return(-1);
2324 cur = xmlHashLookup(table, ID);
2325 if (cur != attr) {
2326 xmlFree(ID);
2327 return(-1);
2328 }
2329 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
2330 xmlFree(ID);
2331 return(0);
2332}
2333
2334/**
2335 * xmlGetID:
2336 * @doc: pointer to the document
2337 * @ID: the ID value
2338 *
2339 * Search the attribute declaring the given ID
2340 *
2341 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2342 */
2343xmlAttrPtr
2344xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2345 xmlIDTablePtr table;
2346 xmlIDPtr id;
2347
2348 if (doc == NULL) {
2349 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
2350 return(NULL);
2351 }
2352
2353 if (ID == NULL) {
2354 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
2355 return(NULL);
2356 }
2357
2358 table = (xmlIDTablePtr) doc->ids;
2359 if (table == NULL)
2360 return(NULL);
2361
2362 id = xmlHashLookup(table, ID);
2363 if (id == NULL)
2364 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002365 if (id->attr == NULL) {
2366 /*
2367 * We are operating on a stream, return a well known reference
2368 * since the attribute node doesn't exist anymore
2369 */
2370 return((xmlAttrPtr) doc);
2371 }
Owen Taylor3473f882001-02-23 17:55:21 +00002372 return(id->attr);
2373}
2374
2375/************************************************************************
2376 * *
2377 * Refs *
2378 * *
2379 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002380typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002381{
2382 xmlListPtr l;
2383 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002384} xmlRemoveMemo;
2385
2386typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2387
2388typedef struct xmlValidateMemo_t
2389{
2390 xmlValidCtxtPtr ctxt;
2391 const xmlChar *name;
2392} xmlValidateMemo;
2393
2394typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002395
2396/**
2397 * xmlCreateRefTable:
2398 *
2399 * create and initialize an empty ref hash table.
2400 *
2401 * Returns the xmlRefTablePtr just created or NULL in case
2402 * of error.
2403 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002404static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002405xmlCreateRefTable(void) {
2406 return(xmlHashCreate(0));
2407}
2408
2409/**
2410 * xmlFreeRef:
2411 * @lk: A list link
2412 *
2413 * Deallocate the memory used by a ref definition
2414 */
2415static void
2416xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002417 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2418 if (ref == NULL) return;
2419 if (ref->value != NULL)
2420 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002421 if (ref->name != NULL)
2422 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002423 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002424}
2425
2426/**
2427 * xmlFreeRefList:
2428 * @list_ref: A list of references.
2429 *
2430 * Deallocate the memory used by a list of references
2431 */
2432static void
2433xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002434 if (list_ref == NULL) return;
2435 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002436}
2437
2438/**
2439 * xmlWalkRemoveRef:
2440 * @data: Contents of current link
2441 * @user: Value supplied by the user
2442 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002443 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002444 */
2445static int
2446xmlWalkRemoveRef(const void *data, const void *user)
2447{
Daniel Veillard37721922001-05-04 15:21:12 +00002448 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2449 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2450 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002451
Daniel Veillard37721922001-05-04 15:21:12 +00002452 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2453 xmlListRemoveFirst(ref_list, (void *)data);
2454 return 0;
2455 }
2456 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002457}
2458
2459/**
2460 * xmlAddRef:
2461 * @ctxt: the validation context
2462 * @doc: pointer to the document
2463 * @value: the value name
2464 * @attr: the attribute holding the Ref
2465 *
2466 * Register a new ref declaration
2467 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002468 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002469 */
2470xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002471xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002472 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002473 xmlRefPtr ret;
2474 xmlRefTablePtr table;
2475 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002476
Daniel Veillard37721922001-05-04 15:21:12 +00002477 if (doc == NULL) {
2478 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002479 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002480 return(NULL);
2481 }
2482 if (value == NULL) {
2483 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002484 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002485 return(NULL);
2486 }
2487 if (attr == NULL) {
2488 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002489 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002490 return(NULL);
2491 }
Owen Taylor3473f882001-02-23 17:55:21 +00002492
Daniel Veillard37721922001-05-04 15:21:12 +00002493 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002494 * Create the Ref table if needed.
2495 */
Daniel Veillard37721922001-05-04 15:21:12 +00002496 table = (xmlRefTablePtr) doc->refs;
2497 if (table == NULL)
2498 doc->refs = table = xmlCreateRefTable();
2499 if (table == NULL) {
2500 xmlGenericError(xmlGenericErrorContext,
2501 "xmlAddRef: Table creation failed!\n");
2502 return(NULL);
2503 }
Owen Taylor3473f882001-02-23 17:55:21 +00002504
Daniel Veillard37721922001-05-04 15:21:12 +00002505 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2506 if (ret == NULL) {
2507 xmlGenericError(xmlGenericErrorContext,
2508 "xmlAddRef: out of memory\n");
2509 return(NULL);
2510 }
Owen Taylor3473f882001-02-23 17:55:21 +00002511
Daniel Veillard37721922001-05-04 15:21:12 +00002512 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002513 * fill the structure.
2514 */
Daniel Veillard37721922001-05-04 15:21:12 +00002515 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002516 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2517 /*
2518 * Operating in streaming mode, attr is gonna disapear
2519 */
2520 ret->name = xmlStrdup(attr->name);
2521 ret->attr = NULL;
2522 } else {
2523 ret->name = NULL;
2524 ret->attr = attr;
2525 }
2526 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002527
Daniel Veillard37721922001-05-04 15:21:12 +00002528 /* To add a reference :-
2529 * References are maintained as a list of references,
2530 * Lookup the entry, if no entry create new nodelist
2531 * Add the owning node to the NodeList
2532 * Return the ref
2533 */
Owen Taylor3473f882001-02-23 17:55:21 +00002534
Daniel Veillard37721922001-05-04 15:21:12 +00002535 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2536 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2537 xmlGenericError(xmlGenericErrorContext,
2538 "xmlAddRef: Reference list creation failed!\n");
2539 return(NULL);
2540 }
2541 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2542 xmlListDelete(ref_list);
2543 xmlGenericError(xmlGenericErrorContext,
2544 "xmlAddRef: Reference list insertion failed!\n");
2545 return(NULL);
2546 }
2547 }
2548 xmlListInsert(ref_list, ret);
2549 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002550}
2551
2552/**
2553 * xmlFreeRefTable:
2554 * @table: An ref table
2555 *
2556 * Deallocate the memory used by an Ref hash table.
2557 */
2558void
2559xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002560 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002561}
2562
2563/**
2564 * xmlIsRef:
2565 * @doc: the document
2566 * @elem: the element carrying the attribute
2567 * @attr: the attribute
2568 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002569 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002570 * then this is simple, otherwise we use an heuristic: name Ref (upper
2571 * or lowercase).
2572 *
2573 * Returns 0 or 1 depending on the lookup result
2574 */
2575int
2576xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002577 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2578 return(0);
2579 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2580 /* TODO @@@ */
2581 return(0);
2582 } else {
2583 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002584
Daniel Veillard37721922001-05-04 15:21:12 +00002585 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2586 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2587 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2588 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002589
Daniel Veillard37721922001-05-04 15:21:12 +00002590 if ((attrDecl != NULL) &&
2591 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2592 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2593 return(1);
2594 }
2595 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002596}
2597
2598/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002599 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00002600 * @doc: the document
2601 * @attr: the attribute
2602 *
2603 * Remove the given attribute from the Ref table maintained internally.
2604 *
2605 * Returns -1 if the lookup failed and 0 otherwise
2606 */
2607int
2608xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002609 xmlListPtr ref_list;
2610 xmlRefTablePtr table;
2611 xmlChar *ID;
2612 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002613
Daniel Veillard37721922001-05-04 15:21:12 +00002614 if (doc == NULL) return(-1);
2615 if (attr == NULL) return(-1);
2616 table = (xmlRefTablePtr) doc->refs;
2617 if (table == NULL)
2618 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002619
Daniel Veillard37721922001-05-04 15:21:12 +00002620 if (attr == NULL)
2621 return(-1);
2622 ID = xmlNodeListGetString(doc, attr->children, 1);
2623 if (ID == NULL)
2624 return(-1);
2625 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002626
Daniel Veillard37721922001-05-04 15:21:12 +00002627 if(ref_list == NULL) {
2628 xmlFree(ID);
2629 return (-1);
2630 }
2631 /* At this point, ref_list refers to a list of references which
2632 * have the same key as the supplied attr. Our list of references
2633 * is ordered by reference address and we don't have that information
2634 * here to use when removing. We'll have to walk the list and
2635 * check for a matching attribute, when we find one stop the walk
2636 * and remove the entry.
2637 * The list is ordered by reference, so that means we don't have the
2638 * key. Passing the list and the reference to the walker means we
2639 * will have enough data to be able to remove the entry.
2640 */
2641 target.l = ref_list;
2642 target.ap = attr;
2643
2644 /* Remove the supplied attr from our list */
2645 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002646
Daniel Veillard37721922001-05-04 15:21:12 +00002647 /*If the list is empty then remove the list entry in the hash */
2648 if (xmlListEmpty(ref_list))
2649 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2650 xmlFreeRefList);
2651 xmlFree(ID);
2652 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002653}
2654
2655/**
2656 * xmlGetRefs:
2657 * @doc: pointer to the document
2658 * @ID: the ID value
2659 *
2660 * Find the set of references for the supplied ID.
2661 *
2662 * Returns NULL if not found, otherwise node set for the ID.
2663 */
2664xmlListPtr
2665xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002666 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002667
Daniel Veillard37721922001-05-04 15:21:12 +00002668 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002669 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002670 return(NULL);
2671 }
Owen Taylor3473f882001-02-23 17:55:21 +00002672
Daniel Veillard37721922001-05-04 15:21:12 +00002673 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002674 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002675 return(NULL);
2676 }
Owen Taylor3473f882001-02-23 17:55:21 +00002677
Daniel Veillard37721922001-05-04 15:21:12 +00002678 table = (xmlRefTablePtr) doc->refs;
2679 if (table == NULL)
2680 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002681
Daniel Veillard37721922001-05-04 15:21:12 +00002682 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002683}
2684
2685/************************************************************************
2686 * *
2687 * Routines for validity checking *
2688 * *
2689 ************************************************************************/
2690
2691/**
2692 * xmlGetDtdElementDesc:
2693 * @dtd: a pointer to the DtD to search
2694 * @name: the element name
2695 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002696 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002697 *
2698 * returns the xmlElementPtr if found or NULL
2699 */
2700
2701xmlElementPtr
2702xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2703 xmlElementTablePtr table;
2704 xmlElementPtr cur;
2705 xmlChar *uqname = NULL, *prefix = NULL;
2706
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002707 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002708 if (dtd->elements == NULL)
2709 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002710 table = (xmlElementTablePtr) dtd->elements;
2711
2712 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002713 if (uqname != NULL)
2714 name = uqname;
2715 cur = xmlHashLookup2(table, name, prefix);
2716 if (prefix != NULL) xmlFree(prefix);
2717 if (uqname != NULL) xmlFree(uqname);
2718 return(cur);
2719}
2720/**
2721 * xmlGetDtdElementDesc2:
2722 * @dtd: a pointer to the DtD to search
2723 * @name: the element name
2724 * @create: create an empty description if not found
2725 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002726 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002727 *
2728 * returns the xmlElementPtr if found or NULL
2729 */
2730
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002731static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002732xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2733 xmlElementTablePtr table;
2734 xmlElementPtr cur;
2735 xmlChar *uqname = NULL, *prefix = NULL;
2736
2737 if (dtd == NULL) return(NULL);
2738 if (dtd->elements == NULL) {
2739 if (!create)
2740 return(NULL);
2741 /*
2742 * Create the Element table if needed.
2743 */
2744 table = (xmlElementTablePtr) dtd->elements;
2745 if (table == NULL) {
2746 table = xmlCreateElementTable();
2747 dtd->elements = (void *) table;
2748 }
2749 if (table == NULL) {
2750 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002751 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002752 return(NULL);
2753 }
2754 }
2755 table = (xmlElementTablePtr) dtd->elements;
2756
2757 uqname = xmlSplitQName2(name, &prefix);
2758 if (uqname != NULL)
2759 name = uqname;
2760 cur = xmlHashLookup2(table, name, prefix);
2761 if ((cur == NULL) && (create)) {
2762 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2763 if (cur == NULL) {
2764 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002765 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002766 return(NULL);
2767 }
2768 memset(cur, 0, sizeof(xmlElement));
2769 cur->type = XML_ELEMENT_DECL;
2770
2771 /*
2772 * fill the structure.
2773 */
2774 cur->name = xmlStrdup(name);
2775 cur->prefix = xmlStrdup(prefix);
2776 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2777
2778 xmlHashAddEntry2(table, name, prefix, cur);
2779 }
2780 if (prefix != NULL) xmlFree(prefix);
2781 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002782 return(cur);
2783}
2784
2785/**
2786 * xmlGetDtdQElementDesc:
2787 * @dtd: a pointer to the DtD to search
2788 * @name: the element name
2789 * @prefix: the element namespace prefix
2790 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002791 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002792 *
2793 * returns the xmlElementPtr if found or NULL
2794 */
2795
Daniel Veillard48da9102001-08-07 01:10:10 +00002796xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002797xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2798 const xmlChar *prefix) {
2799 xmlElementTablePtr table;
2800
2801 if (dtd == NULL) return(NULL);
2802 if (dtd->elements == NULL) return(NULL);
2803 table = (xmlElementTablePtr) dtd->elements;
2804
2805 return(xmlHashLookup2(table, name, prefix));
2806}
2807
2808/**
2809 * xmlGetDtdAttrDesc:
2810 * @dtd: a pointer to the DtD to search
2811 * @elem: the element name
2812 * @name: the attribute name
2813 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002814 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002815 * this element.
2816 *
2817 * returns the xmlAttributePtr if found or NULL
2818 */
2819
2820xmlAttributePtr
2821xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2822 xmlAttributeTablePtr table;
2823 xmlAttributePtr cur;
2824 xmlChar *uqname = NULL, *prefix = NULL;
2825
2826 if (dtd == NULL) return(NULL);
2827 if (dtd->attributes == NULL) return(NULL);
2828
2829 table = (xmlAttributeTablePtr) dtd->attributes;
2830 if (table == NULL)
2831 return(NULL);
2832
2833 uqname = xmlSplitQName2(name, &prefix);
2834
2835 if (uqname != NULL) {
2836 cur = xmlHashLookup3(table, uqname, prefix, elem);
2837 if (prefix != NULL) xmlFree(prefix);
2838 if (uqname != NULL) xmlFree(uqname);
2839 } else
2840 cur = xmlHashLookup3(table, name, NULL, elem);
2841 return(cur);
2842}
2843
2844/**
2845 * xmlGetDtdQAttrDesc:
2846 * @dtd: a pointer to the DtD to search
2847 * @elem: the element name
2848 * @name: the attribute name
2849 * @prefix: the attribute namespace prefix
2850 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002851 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002852 * this element.
2853 *
2854 * returns the xmlAttributePtr if found or NULL
2855 */
2856
Daniel Veillard48da9102001-08-07 01:10:10 +00002857xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002858xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2859 const xmlChar *prefix) {
2860 xmlAttributeTablePtr table;
2861
2862 if (dtd == NULL) return(NULL);
2863 if (dtd->attributes == NULL) return(NULL);
2864 table = (xmlAttributeTablePtr) dtd->attributes;
2865
2866 return(xmlHashLookup3(table, name, prefix, elem));
2867}
2868
2869/**
2870 * xmlGetDtdNotationDesc:
2871 * @dtd: a pointer to the DtD to search
2872 * @name: the notation name
2873 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002874 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002875 *
2876 * returns the xmlNotationPtr if found or NULL
2877 */
2878
2879xmlNotationPtr
2880xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2881 xmlNotationTablePtr table;
2882
2883 if (dtd == NULL) return(NULL);
2884 if (dtd->notations == NULL) return(NULL);
2885 table = (xmlNotationTablePtr) dtd->notations;
2886
2887 return(xmlHashLookup(table, name));
2888}
2889
2890/**
2891 * xmlValidateNotationUse:
2892 * @ctxt: the validation context
2893 * @doc: the document
2894 * @notationName: the notation name to check
2895 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002896 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002897 * - [ VC: Notation Declared ]
2898 *
2899 * returns 1 if valid or 0 otherwise
2900 */
2901
2902int
2903xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2904 const xmlChar *notationName) {
2905 xmlNotationPtr notaDecl;
2906 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2907
2908 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2909 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2910 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2911
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002912 if ((notaDecl == NULL) && (ctxt != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002913 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2914 notationName);
2915 return(0);
2916 }
2917 return(1);
2918}
2919
2920/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002921 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00002922 * @doc: the document
2923 * @name: the element name
2924 *
2925 * Search in the DtDs whether an element accept Mixed content (or ANY)
2926 * basically if it is supposed to accept text childs
2927 *
2928 * returns 0 if no, 1 if yes, and -1 if no element description is available
2929 */
2930
2931int
2932xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2933 xmlElementPtr elemDecl;
2934
2935 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2936
2937 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2938 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2939 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2940 if (elemDecl == NULL) return(-1);
2941 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002942 case XML_ELEMENT_TYPE_UNDEFINED:
2943 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002944 case XML_ELEMENT_TYPE_ELEMENT:
2945 return(0);
2946 case XML_ELEMENT_TYPE_EMPTY:
2947 /*
2948 * return 1 for EMPTY since we want VC error to pop up
2949 * on <empty> </empty> for example
2950 */
2951 case XML_ELEMENT_TYPE_ANY:
2952 case XML_ELEMENT_TYPE_MIXED:
2953 return(1);
2954 }
2955 return(1);
2956}
2957
2958/**
2959 * xmlValidateNameValue:
2960 * @value: an Name value
2961 *
2962 * Validate that the given value match Name production
2963 *
2964 * returns 1 if valid or 0 otherwise
2965 */
2966
Daniel Veillard9b731d72002-04-14 12:56:08 +00002967int
Owen Taylor3473f882001-02-23 17:55:21 +00002968xmlValidateNameValue(const xmlChar *value) {
2969 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002970 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002971
2972 if (value == NULL) return(0);
2973 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002974 val = xmlStringCurrentChar(NULL, cur, &len);
2975 cur += len;
2976 if (!IS_LETTER(val) && (val != '_') &&
2977 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002978 return(0);
2979 }
2980
Daniel Veillardd8224e02002-01-13 15:43:22 +00002981 val = xmlStringCurrentChar(NULL, cur, &len);
2982 cur += len;
2983 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2984 (val == '.') || (val == '-') ||
2985 (val == '_') || (val == ':') ||
2986 (IS_COMBINING(val)) ||
2987 (IS_EXTENDER(val))) {
2988 val = xmlStringCurrentChar(NULL, cur, &len);
2989 cur += len;
2990 }
Owen Taylor3473f882001-02-23 17:55:21 +00002991
Daniel Veillardd8224e02002-01-13 15:43:22 +00002992 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002993
2994 return(1);
2995}
2996
2997/**
2998 * xmlValidateNamesValue:
2999 * @value: an Names value
3000 *
3001 * Validate that the given value match Names production
3002 *
3003 * returns 1 if valid or 0 otherwise
3004 */
3005
Daniel Veillard9b731d72002-04-14 12:56:08 +00003006int
Owen Taylor3473f882001-02-23 17:55:21 +00003007xmlValidateNamesValue(const xmlChar *value) {
3008 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003009 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003010
3011 if (value == NULL) return(0);
3012 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003013 val = xmlStringCurrentChar(NULL, cur, &len);
3014 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003015
Daniel Veillardd8224e02002-01-13 15:43:22 +00003016 if (!IS_LETTER(val) && (val != '_') &&
3017 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003018 return(0);
3019 }
3020
Daniel Veillardd8224e02002-01-13 15:43:22 +00003021 val = xmlStringCurrentChar(NULL, cur, &len);
3022 cur += len;
3023 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3024 (val == '.') || (val == '-') ||
3025 (val == '_') || (val == ':') ||
3026 (IS_COMBINING(val)) ||
3027 (IS_EXTENDER(val))) {
3028 val = xmlStringCurrentChar(NULL, cur, &len);
3029 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003030 }
3031
Daniel Veillardd8224e02002-01-13 15:43:22 +00003032 while (IS_BLANK(val)) {
3033 while (IS_BLANK(val)) {
3034 val = xmlStringCurrentChar(NULL, cur, &len);
3035 cur += len;
3036 }
3037
3038 if (!IS_LETTER(val) && (val != '_') &&
3039 (val != ':')) {
3040 return(0);
3041 }
3042 val = xmlStringCurrentChar(NULL, cur, &len);
3043 cur += len;
3044
3045 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3046 (val == '.') || (val == '-') ||
3047 (val == '_') || (val == ':') ||
3048 (IS_COMBINING(val)) ||
3049 (IS_EXTENDER(val))) {
3050 val = xmlStringCurrentChar(NULL, cur, &len);
3051 cur += len;
3052 }
3053 }
3054
3055 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003056
3057 return(1);
3058}
3059
3060/**
3061 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003062 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003063 *
3064 * Validate that the given value match Nmtoken production
3065 *
3066 * [ VC: Name Token ]
3067 *
3068 * returns 1 if valid or 0 otherwise
3069 */
3070
Daniel Veillard9b731d72002-04-14 12:56:08 +00003071int
Owen Taylor3473f882001-02-23 17:55:21 +00003072xmlValidateNmtokenValue(const xmlChar *value) {
3073 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003074 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003075
3076 if (value == NULL) return(0);
3077 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003078 val = xmlStringCurrentChar(NULL, cur, &len);
3079 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003080
Daniel Veillardd8224e02002-01-13 15:43:22 +00003081 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3082 (val != '.') && (val != '-') &&
3083 (val != '_') && (val != ':') &&
3084 (!IS_COMBINING(val)) &&
3085 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003086 return(0);
3087
Daniel Veillardd8224e02002-01-13 15:43:22 +00003088 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3089 (val == '.') || (val == '-') ||
3090 (val == '_') || (val == ':') ||
3091 (IS_COMBINING(val)) ||
3092 (IS_EXTENDER(val))) {
3093 val = xmlStringCurrentChar(NULL, cur, &len);
3094 cur += len;
3095 }
Owen Taylor3473f882001-02-23 17:55:21 +00003096
Daniel Veillardd8224e02002-01-13 15:43:22 +00003097 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003098
3099 return(1);
3100}
3101
3102/**
3103 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003104 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003105 *
3106 * Validate that the given value match Nmtokens production
3107 *
3108 * [ VC: Name Token ]
3109 *
3110 * returns 1 if valid or 0 otherwise
3111 */
3112
Daniel Veillard9b731d72002-04-14 12:56:08 +00003113int
Owen Taylor3473f882001-02-23 17:55:21 +00003114xmlValidateNmtokensValue(const xmlChar *value) {
3115 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003116 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003117
3118 if (value == NULL) return(0);
3119 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003120 val = xmlStringCurrentChar(NULL, cur, &len);
3121 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003122
Daniel Veillardd8224e02002-01-13 15:43:22 +00003123 while (IS_BLANK(val)) {
3124 val = xmlStringCurrentChar(NULL, cur, &len);
3125 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003126 }
3127
Daniel Veillardd8224e02002-01-13 15:43:22 +00003128 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3129 (val != '.') && (val != '-') &&
3130 (val != '_') && (val != ':') &&
3131 (!IS_COMBINING(val)) &&
3132 (!IS_EXTENDER(val)))
3133 return(0);
3134
3135 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3136 (val == '.') || (val == '-') ||
3137 (val == '_') || (val == ':') ||
3138 (IS_COMBINING(val)) ||
3139 (IS_EXTENDER(val))) {
3140 val = xmlStringCurrentChar(NULL, cur, &len);
3141 cur += len;
3142 }
3143
3144 while (IS_BLANK(val)) {
3145 while (IS_BLANK(val)) {
3146 val = xmlStringCurrentChar(NULL, cur, &len);
3147 cur += len;
3148 }
3149 if (val == 0) return(1);
3150
3151 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3152 (val != '.') && (val != '-') &&
3153 (val != '_') && (val != ':') &&
3154 (!IS_COMBINING(val)) &&
3155 (!IS_EXTENDER(val)))
3156 return(0);
3157
3158 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3159 (val == '.') || (val == '-') ||
3160 (val == '_') || (val == ':') ||
3161 (IS_COMBINING(val)) ||
3162 (IS_EXTENDER(val))) {
3163 val = xmlStringCurrentChar(NULL, cur, &len);
3164 cur += len;
3165 }
3166 }
3167
3168 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003169
3170 return(1);
3171}
3172
3173/**
3174 * xmlValidateNotationDecl:
3175 * @ctxt: the validation context
3176 * @doc: a document instance
3177 * @nota: a notation definition
3178 *
3179 * Try to validate a single notation definition
3180 * basically it does the following checks as described by the
3181 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003182 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003183 * But this function get called anyway ...
3184 *
3185 * returns 1 if valid or 0 otherwise
3186 */
3187
3188int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003189xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3190 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003191 int ret = 1;
3192
3193 return(ret);
3194}
3195
3196/**
3197 * xmlValidateAttributeValue:
3198 * @type: an attribute type
3199 * @value: an attribute value
3200 *
3201 * Validate that the given attribute value match the proper production
3202 *
3203 * [ VC: ID ]
3204 * Values of type ID must match the Name production....
3205 *
3206 * [ VC: IDREF ]
3207 * Values of type IDREF must match the Name production, and values
3208 * of type IDREFS must match Names ...
3209 *
3210 * [ VC: Entity Name ]
3211 * Values of type ENTITY must match the Name production, values
3212 * of type ENTITIES must match Names ...
3213 *
3214 * [ VC: Name Token ]
3215 * Values of type NMTOKEN must match the Nmtoken production; values
3216 * of type NMTOKENS must match Nmtokens.
3217 *
3218 * returns 1 if valid or 0 otherwise
3219 */
3220
3221int
3222xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3223 switch (type) {
3224 case XML_ATTRIBUTE_ENTITIES:
3225 case XML_ATTRIBUTE_IDREFS:
3226 return(xmlValidateNamesValue(value));
3227 case XML_ATTRIBUTE_ENTITY:
3228 case XML_ATTRIBUTE_IDREF:
3229 case XML_ATTRIBUTE_ID:
3230 case XML_ATTRIBUTE_NOTATION:
3231 return(xmlValidateNameValue(value));
3232 case XML_ATTRIBUTE_NMTOKENS:
3233 case XML_ATTRIBUTE_ENUMERATION:
3234 return(xmlValidateNmtokensValue(value));
3235 case XML_ATTRIBUTE_NMTOKEN:
3236 return(xmlValidateNmtokenValue(value));
3237 case XML_ATTRIBUTE_CDATA:
3238 break;
3239 }
3240 return(1);
3241}
3242
3243/**
3244 * xmlValidateAttributeValue2:
3245 * @ctxt: the validation context
3246 * @doc: the document
3247 * @name: the attribute name (used for error reporting only)
3248 * @type: the attribute type
3249 * @value: the attribute value
3250 *
3251 * Validate that the given attribute value match a given type.
3252 * This typically cannot be done before having finished parsing
3253 * the subsets.
3254 *
3255 * [ VC: IDREF ]
3256 * Values of type IDREF must match one of the declared IDs
3257 * Values of type IDREFS must match a sequence of the declared IDs
3258 * each Name must match the value of an ID attribute on some element
3259 * in the XML document; i.e. IDREF values must match the value of
3260 * some ID attribute
3261 *
3262 * [ VC: Entity Name ]
3263 * Values of type ENTITY must match one declared entity
3264 * Values of type ENTITIES must match a sequence of declared entities
3265 *
3266 * [ VC: Notation Attributes ]
3267 * all notation names in the declaration must be declared.
3268 *
3269 * returns 1 if valid or 0 otherwise
3270 */
3271
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003272static int
Owen Taylor3473f882001-02-23 17:55:21 +00003273xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3274 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3275 int ret = 1;
3276 switch (type) {
3277 case XML_ATTRIBUTE_IDREFS:
3278 case XML_ATTRIBUTE_IDREF:
3279 case XML_ATTRIBUTE_ID:
3280 case XML_ATTRIBUTE_NMTOKENS:
3281 case XML_ATTRIBUTE_ENUMERATION:
3282 case XML_ATTRIBUTE_NMTOKEN:
3283 case XML_ATTRIBUTE_CDATA:
3284 break;
3285 case XML_ATTRIBUTE_ENTITY: {
3286 xmlEntityPtr ent;
3287
3288 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003289 if ((ent == NULL) && (doc->standalone == 1)) {
3290 doc->standalone = 0;
3291 ent = xmlGetDocEntity(doc, value);
3292 if (ent != NULL) {
3293 VERROR(ctxt->userData,
3294"standalone problem: attribute %s reference entity \"%s\" in external subset\n",
3295 name, value);
3296 /* WAIT to get answer from the Core WG on this
3297 ret = 0;
3298 */
3299 }
3300 }
Owen Taylor3473f882001-02-23 17:55:21 +00003301 if (ent == NULL) {
3302 VERROR(ctxt->userData,
3303 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3304 name, value);
3305 ret = 0;
3306 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3307 VERROR(ctxt->userData,
3308 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3309 name, value);
3310 ret = 0;
3311 }
3312 break;
3313 }
3314 case XML_ATTRIBUTE_ENTITIES: {
3315 xmlChar *dup, *nam = NULL, *cur, save;
3316 xmlEntityPtr ent;
3317
3318 dup = xmlStrdup(value);
3319 if (dup == NULL)
3320 return(0);
3321 cur = dup;
3322 while (*cur != 0) {
3323 nam = cur;
3324 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
3325 save = *cur;
3326 *cur = 0;
3327 ent = xmlGetDocEntity(doc, nam);
3328 if (ent == NULL) {
3329 VERROR(ctxt->userData,
3330 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3331 name, nam);
3332 ret = 0;
3333 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3334 VERROR(ctxt->userData,
3335 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3336 name, nam);
3337 ret = 0;
3338 }
3339 if (save == 0)
3340 break;
3341 *cur = save;
3342 while (IS_BLANK(*cur)) cur++;
3343 }
3344 xmlFree(dup);
3345 break;
3346 }
3347 case XML_ATTRIBUTE_NOTATION: {
3348 xmlNotationPtr nota;
3349
3350 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3351 if ((nota == NULL) && (doc->extSubset != NULL))
3352 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3353
3354 if (nota == NULL) {
3355 VERROR(ctxt->userData,
3356 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3357 name, value);
3358 ret = 0;
3359 }
3360 break;
3361 }
3362 }
3363 return(ret);
3364}
3365
3366/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003367 * xmlValidCtxtNormalizeAttributeValue:
3368 * @ctxt: the validation context
3369 * @doc: the document
3370 * @elem: the parent
3371 * @name: the attribute name
3372 * @value: the attribute value
3373 * @ctxt: the validation context or NULL
3374 *
3375 * Does the validation related extra step of the normalization of attribute
3376 * values:
3377 *
3378 * If the declared value is not CDATA, then the XML processor must further
3379 * process the normalized attribute value by discarding any leading and
3380 * trailing space (#x20) characters, and by replacing sequences of space
3381 * (#x20) characters by single space (#x20) character.
3382 *
3383 * Also check VC: Standalone Document Declaration in P32, and update
3384 * ctxt->valid accordingly
3385 *
3386 * returns a new normalized string if normalization is needed, NULL otherwise
3387 * the caller must free the returned value.
3388 */
3389
3390xmlChar *
3391xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3392 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3393 xmlChar *ret, *dst;
3394 const xmlChar *src;
3395 xmlAttributePtr attrDecl = NULL;
3396 int extsubset = 0;
3397
3398 if (doc == NULL) return(NULL);
3399 if (elem == NULL) return(NULL);
3400 if (name == NULL) return(NULL);
3401 if (value == NULL) return(NULL);
3402
3403 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003404 xmlChar fn[50];
3405 xmlChar *fullname;
3406
3407 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3408 if (fullname == NULL)
3409 return(0);
3410 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003411 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003412 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003413 if (attrDecl != NULL)
3414 extsubset = 1;
3415 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003416 if ((fullname != fn) && (fullname != elem->name))
3417 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003418 }
3419 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3420 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3421 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3422 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3423 if (attrDecl != NULL)
3424 extsubset = 1;
3425 }
3426
3427 if (attrDecl == NULL)
3428 return(NULL);
3429 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3430 return(NULL);
3431
3432 ret = xmlStrdup(value);
3433 if (ret == NULL)
3434 return(NULL);
3435 src = value;
3436 dst = ret;
3437 while (*src == 0x20) src++;
3438 while (*src != 0) {
3439 if (*src == 0x20) {
3440 while (*src == 0x20) src++;
3441 if (*src != 0)
3442 *dst++ = 0x20;
3443 } else {
3444 *dst++ = *src++;
3445 }
3446 }
3447 *dst = 0;
3448 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3449 VERROR(ctxt->userData,
3450"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3451 name, elem->name);
3452 ctxt->valid = 0;
3453 }
3454 return(ret);
3455}
3456
3457/**
Owen Taylor3473f882001-02-23 17:55:21 +00003458 * xmlValidNormalizeAttributeValue:
3459 * @doc: the document
3460 * @elem: the parent
3461 * @name: the attribute name
3462 * @value: the attribute value
3463 *
3464 * Does the validation related extra step of the normalization of attribute
3465 * values:
3466 *
3467 * If the declared value is not CDATA, then the XML processor must further
3468 * process the normalized attribute value by discarding any leading and
3469 * trailing space (#x20) characters, and by replacing sequences of space
3470 * (#x20) characters by single space (#x20) character.
3471 *
3472 * returns a new normalized string if normalization is needed, NULL otherwise
3473 * the caller must free the returned value.
3474 */
3475
3476xmlChar *
3477xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3478 const xmlChar *name, const xmlChar *value) {
3479 xmlChar *ret, *dst;
3480 const xmlChar *src;
3481 xmlAttributePtr attrDecl = NULL;
3482
3483 if (doc == NULL) return(NULL);
3484 if (elem == NULL) return(NULL);
3485 if (name == NULL) return(NULL);
3486 if (value == NULL) return(NULL);
3487
3488 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003489 xmlChar fn[50];
3490 xmlChar *fullname;
3491
3492 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3493 if (fullname == NULL)
3494 return(0);
3495 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003496 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003497 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3498 if ((fullname != fn) && (fullname != elem->name))
3499 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003500 }
3501 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3502 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3503 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3504
3505 if (attrDecl == NULL)
3506 return(NULL);
3507 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3508 return(NULL);
3509
3510 ret = xmlStrdup(value);
3511 if (ret == NULL)
3512 return(NULL);
3513 src = value;
3514 dst = ret;
3515 while (*src == 0x20) src++;
3516 while (*src != 0) {
3517 if (*src == 0x20) {
3518 while (*src == 0x20) src++;
3519 if (*src != 0)
3520 *dst++ = 0x20;
3521 } else {
3522 *dst++ = *src++;
3523 }
3524 }
3525 *dst = 0;
3526 return(ret);
3527}
3528
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003529static void
Owen Taylor3473f882001-02-23 17:55:21 +00003530xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003531 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003532 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3533}
3534
3535/**
3536 * xmlValidateAttributeDecl:
3537 * @ctxt: the validation context
3538 * @doc: a document instance
3539 * @attr: an attribute definition
3540 *
3541 * Try to validate a single attribute definition
3542 * basically it does the following checks as described by the
3543 * XML-1.0 recommendation:
3544 * - [ VC: Attribute Default Legal ]
3545 * - [ VC: Enumeration ]
3546 * - [ VC: ID Attribute Default ]
3547 *
3548 * The ID/IDREF uniqueness and matching are done separately
3549 *
3550 * returns 1 if valid or 0 otherwise
3551 */
3552
3553int
3554xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3555 xmlAttributePtr attr) {
3556 int ret = 1;
3557 int val;
3558 CHECK_DTD;
3559 if(attr == NULL) return(1);
3560
3561 /* Attribute Default Legal */
3562 /* Enumeration */
3563 if (attr->defaultValue != NULL) {
3564 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3565 if (val == 0) {
3566 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003567 "Syntax of default value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003568 attr->name, attr->elem);
3569 }
3570 ret &= val;
3571 }
3572
3573 /* ID Attribute Default */
3574 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3575 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3576 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3577 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003578 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003579 attr->name, attr->elem);
3580 ret = 0;
3581 }
3582
3583 /* One ID per Element Type */
3584 if (attr->atype == XML_ATTRIBUTE_ID) {
3585 int nbId;
3586
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003587 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003588 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3589 attr->elem);
3590 if (elem != NULL) {
3591 nbId = xmlScanIDAttributeDecl(NULL, elem);
3592 } else {
3593 xmlAttributeTablePtr table;
3594
3595 /*
3596 * The attribute may be declared in the internal subset and the
3597 * element in the external subset.
3598 */
3599 nbId = 0;
3600 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3601 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3602 xmlValidateAttributeIdCallback, &nbId);
3603 }
3604 if (nbId > 1) {
3605 VERROR(ctxt->userData,
3606 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3607 attr->elem, nbId, attr->name);
3608 } else if (doc->extSubset != NULL) {
3609 int extId = 0;
3610 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3611 if (elem != NULL) {
3612 extId = xmlScanIDAttributeDecl(NULL, elem);
3613 }
3614 if (extId > 1) {
3615 VERROR(ctxt->userData,
3616 "Element %s has %d ID attribute defined in the external subset : %s\n",
3617 attr->elem, extId, attr->name);
3618 } else if (extId + nbId > 1) {
3619 VERROR(ctxt->userData,
3620"Element %s has ID attributes defined in the internal and external subset : %s\n",
3621 attr->elem, attr->name);
3622 }
3623 }
3624 }
3625
3626 /* Validity Constraint: Enumeration */
3627 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3628 xmlEnumerationPtr tree = attr->tree;
3629 while (tree != NULL) {
3630 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3631 tree = tree->next;
3632 }
3633 if (tree == NULL) {
3634 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003635"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003636 attr->defaultValue, attr->name, attr->elem);
3637 ret = 0;
3638 }
3639 }
3640
3641 return(ret);
3642}
3643
3644/**
3645 * xmlValidateElementDecl:
3646 * @ctxt: the validation context
3647 * @doc: a document instance
3648 * @elem: an element definition
3649 *
3650 * Try to validate a single element definition
3651 * basically it does the following checks as described by the
3652 * XML-1.0 recommendation:
3653 * - [ VC: One ID per Element Type ]
3654 * - [ VC: No Duplicate Types ]
3655 * - [ VC: Unique Element Type Declaration ]
3656 *
3657 * returns 1 if valid or 0 otherwise
3658 */
3659
3660int
3661xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3662 xmlElementPtr elem) {
3663 int ret = 1;
3664 xmlElementPtr tst;
3665
3666 CHECK_DTD;
3667
3668 if (elem == NULL) return(1);
3669
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003670#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00003671#ifdef LIBXML_REGEXP_ENABLED
3672 /* Build the regexp associated to the content model */
3673 ret = xmlValidBuildContentModel(ctxt, elem);
3674#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003675#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00003676
Owen Taylor3473f882001-02-23 17:55:21 +00003677 /* No Duplicate Types */
3678 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3679 xmlElementContentPtr cur, next;
3680 const xmlChar *name;
3681
3682 cur = elem->content;
3683 while (cur != NULL) {
3684 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3685 if (cur->c1 == NULL) break;
3686 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3687 name = cur->c1->name;
3688 next = cur->c2;
3689 while (next != NULL) {
3690 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3691 if (xmlStrEqual(next->name, name)) {
3692 VERROR(ctxt->userData,
3693 "Definition of %s has duplicate references of %s\n",
3694 elem->name, name);
3695 ret = 0;
3696 }
3697 break;
3698 }
3699 if (next->c1 == NULL) break;
3700 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3701 if (xmlStrEqual(next->c1->name, name)) {
3702 VERROR(ctxt->userData,
3703 "Definition of %s has duplicate references of %s\n",
3704 elem->name, name);
3705 ret = 0;
3706 }
3707 next = next->c2;
3708 }
3709 }
3710 cur = cur->c2;
3711 }
3712 }
3713
3714 /* VC: Unique Element Type Declaration */
3715 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003716 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003717 ((tst->prefix == elem->prefix) ||
3718 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003719 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003720 VERROR(ctxt->userData, "Redefinition of element %s\n",
3721 elem->name);
3722 ret = 0;
3723 }
3724 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003725 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003726 ((tst->prefix == elem->prefix) ||
3727 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003728 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003729 VERROR(ctxt->userData, "Redefinition of element %s\n",
3730 elem->name);
3731 ret = 0;
3732 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003733 /* One ID per Element Type
3734 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003735 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3736 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003737 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003738 return(ret);
3739}
3740
3741/**
3742 * xmlValidateOneAttribute:
3743 * @ctxt: the validation context
3744 * @doc: a document instance
3745 * @elem: an element instance
3746 * @attr: an attribute instance
3747 * @value: the attribute value (without entities processing)
3748 *
3749 * Try to validate a single attribute for an element
3750 * basically it does the following checks as described by the
3751 * XML-1.0 recommendation:
3752 * - [ VC: Attribute Value Type ]
3753 * - [ VC: Fixed Attribute Default ]
3754 * - [ VC: Entity Name ]
3755 * - [ VC: Name Token ]
3756 * - [ VC: ID ]
3757 * - [ VC: IDREF ]
3758 * - [ VC: Entity Name ]
3759 * - [ VC: Notation Attributes ]
3760 *
3761 * The ID/IDREF uniqueness and matching are done separately
3762 *
3763 * returns 1 if valid or 0 otherwise
3764 */
3765
3766int
3767xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3768 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3769 /* xmlElementPtr elemDecl; */
3770 xmlAttributePtr attrDecl = NULL;
3771 int val;
3772 int ret = 1;
3773
3774 CHECK_DTD;
3775 if ((elem == NULL) || (elem->name == NULL)) return(0);
3776 if ((attr == NULL) || (attr->name == NULL)) return(0);
3777
3778 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003779 xmlChar fn[50];
3780 xmlChar *fullname;
3781
3782 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3783 if (fullname == NULL)
3784 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003785 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003786 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00003787 attr->name, attr->ns->prefix);
3788 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003789 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00003790 attr->name, attr->ns->prefix);
3791 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003792 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003793 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3794 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00003795 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003796 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003797 if ((fullname != fn) && (fullname != elem->name))
3798 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003799 }
3800 if (attrDecl == NULL) {
3801 if (attr->ns != NULL) {
3802 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3803 attr->name, attr->ns->prefix);
3804 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3805 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3806 attr->name, attr->ns->prefix);
3807 } else {
3808 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3809 elem->name, attr->name);
3810 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3811 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3812 elem->name, attr->name);
3813 }
3814 }
3815
3816
3817 /* Validity Constraint: Attribute Value Type */
3818 if (attrDecl == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003819 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003820 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003821 "No declaration for attribute %s of element %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003822 attr->name, elem->name);
3823 return(0);
3824 }
3825 attr->atype = attrDecl->atype;
3826
3827 val = xmlValidateAttributeValue(attrDecl->atype, value);
3828 if (val == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003829 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003830 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003831 "Syntax of value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003832 attr->name, elem->name);
3833 ret = 0;
3834 }
3835
3836 /* Validity constraint: Fixed Attribute Default */
3837 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3838 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003839 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003840 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003841 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003842 attr->name, elem->name, attrDecl->defaultValue);
3843 ret = 0;
3844 }
3845 }
3846
3847 /* Validity Constraint: ID uniqueness */
3848 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3849 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3850 ret = 0;
3851 }
3852
3853 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3854 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3855 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3856 ret = 0;
3857 }
3858
3859 /* Validity Constraint: Notation Attributes */
3860 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3861 xmlEnumerationPtr tree = attrDecl->tree;
3862 xmlNotationPtr nota;
3863
3864 /* First check that the given NOTATION was declared */
3865 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3866 if (nota == NULL)
3867 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3868
3869 if (nota == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003870 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003871 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003872 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003873 value, attr->name, elem->name);
3874 ret = 0;
3875 }
3876
3877 /* Second, verify that it's among the list */
3878 while (tree != NULL) {
3879 if (xmlStrEqual(tree->name, value)) break;
3880 tree = tree->next;
3881 }
3882 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003883 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003884 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003885"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003886 value, attr->name, elem->name);
3887 ret = 0;
3888 }
3889 }
3890
3891 /* Validity Constraint: Enumeration */
3892 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3893 xmlEnumerationPtr tree = attrDecl->tree;
3894 while (tree != NULL) {
3895 if (xmlStrEqual(tree->name, value)) break;
3896 tree = tree->next;
3897 }
3898 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003899 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003900 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003901 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003902 value, attr->name, elem->name);
3903 ret = 0;
3904 }
3905 }
3906
3907 /* Fixed Attribute Default */
3908 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3909 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003910 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003911 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003912 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003913 attr->name, elem->name, attrDecl->defaultValue);
3914 ret = 0;
3915 }
3916
3917 /* Extra check for the attribute value */
3918 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3919 attrDecl->atype, value);
3920
3921 return(ret);
3922}
3923
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003924/**
3925 * xmlValidateOneNamespace:
3926 * @ctxt: the validation context
3927 * @doc: a document instance
3928 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00003929 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003930 * @ns: an namespace declaration instance
3931 * @value: the attribute value (without entities processing)
3932 *
3933 * Try to validate a single namespace declaration for an element
3934 * basically it does the following checks as described by the
3935 * XML-1.0 recommendation:
3936 * - [ VC: Attribute Value Type ]
3937 * - [ VC: Fixed Attribute Default ]
3938 * - [ VC: Entity Name ]
3939 * - [ VC: Name Token ]
3940 * - [ VC: ID ]
3941 * - [ VC: IDREF ]
3942 * - [ VC: Entity Name ]
3943 * - [ VC: Notation Attributes ]
3944 *
3945 * The ID/IDREF uniqueness and matching are done separately
3946 *
3947 * returns 1 if valid or 0 otherwise
3948 */
3949
3950int
3951xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3952xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
3953 /* xmlElementPtr elemDecl; */
3954 xmlAttributePtr attrDecl = NULL;
3955 int val;
3956 int ret = 1;
3957
3958 CHECK_DTD;
3959 if ((elem == NULL) || (elem->name == NULL)) return(0);
3960 if ((ns == NULL) || (ns->href == NULL)) return(0);
3961
3962 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003963 xmlChar fn[50];
3964 xmlChar *fullname;
3965
3966 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
3967 if (fullname == NULL) {
3968 VERROR(ctxt->userData, "Out of memory\n");
3969 return(0);
3970 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003971 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003972 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003973 ns->prefix, BAD_CAST "xmlns");
3974 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003975 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003976 ns->prefix, BAD_CAST "xmlns");
3977 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003978 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003979 BAD_CAST "xmlns");
3980 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003981 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003982 BAD_CAST "xmlns");
3983 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003984 if ((fullname != fn) && (fullname != elem->name))
3985 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003986 }
3987 if (attrDecl == NULL) {
3988 if (ns->prefix != NULL) {
3989 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3990 ns->prefix, BAD_CAST "xmlns");
3991 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3992 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3993 ns->prefix, BAD_CAST "xmlns");
3994 } else {
3995 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3996 elem->name, BAD_CAST "xmlns");
3997 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3998 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3999 elem->name, BAD_CAST "xmlns");
4000 }
4001 }
4002
4003
4004 /* Validity Constraint: Attribute Value Type */
4005 if (attrDecl == NULL) {
4006 VECTXT(ctxt, elem);
4007 if (ns->prefix != NULL) {
4008 VERROR(ctxt->userData,
4009 "No declaration for attribute xmlns:%s of element %s\n",
4010 ns->prefix, elem->name);
4011 } else {
4012 VERROR(ctxt->userData,
4013 "No declaration for attribute xmlns of element %s\n",
4014 elem->name);
4015 }
4016 return(0);
4017 }
4018
4019 val = xmlValidateAttributeValue(attrDecl->atype, value);
4020 if (val == 0) {
4021 VECTXT(ctxt, elem);
4022 if (ns->prefix != NULL) {
4023 VERROR(ctxt->userData,
4024 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4025 ns->prefix, elem->name);
4026 } else {
4027 VERROR(ctxt->userData,
4028 "Syntax of value for attribute xmlns of %s is not valid\n",
4029 elem->name);
4030 }
4031 ret = 0;
4032 }
4033
4034 /* Validity constraint: Fixed Attribute Default */
4035 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4036 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4037 VECTXT(ctxt, elem);
4038 if (ns->prefix != NULL) {
4039 VERROR(ctxt->userData,
4040 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4041 ns->prefix, elem->name, attrDecl->defaultValue);
4042 } else {
4043 VERROR(ctxt->userData,
4044 "Value for attribute xmlns of %s is different from default \"%s\"\n",
4045 elem->name, attrDecl->defaultValue);
4046 }
4047 ret = 0;
4048 }
4049 }
4050
4051 /* Validity Constraint: ID uniqueness */
4052 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4053 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4054 ret = 0;
4055 }
4056
4057 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4058 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4059 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4060 ret = 0;
4061 }
4062
4063 /* Validity Constraint: Notation Attributes */
4064 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4065 xmlEnumerationPtr tree = attrDecl->tree;
4066 xmlNotationPtr nota;
4067
4068 /* First check that the given NOTATION was declared */
4069 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4070 if (nota == NULL)
4071 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4072
4073 if (nota == NULL) {
4074 VECTXT(ctxt, elem);
4075 if (ns->prefix != NULL) {
4076 VERROR(ctxt->userData,
4077 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4078 value, ns->prefix, elem->name);
4079 } else {
4080 VERROR(ctxt->userData,
4081 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4082 value, elem->name);
4083 }
4084 ret = 0;
4085 }
4086
4087 /* Second, verify that it's among the list */
4088 while (tree != NULL) {
4089 if (xmlStrEqual(tree->name, value)) break;
4090 tree = tree->next;
4091 }
4092 if (tree == NULL) {
4093 VECTXT(ctxt, elem);
4094 if (ns->prefix != NULL) {
4095 VERROR(ctxt->userData,
4096"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4097 value, ns->prefix, elem->name);
4098 } else {
4099 VERROR(ctxt->userData,
4100"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4101 value, elem->name);
4102 }
4103 ret = 0;
4104 }
4105 }
4106
4107 /* Validity Constraint: Enumeration */
4108 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4109 xmlEnumerationPtr tree = attrDecl->tree;
4110 while (tree != NULL) {
4111 if (xmlStrEqual(tree->name, value)) break;
4112 tree = tree->next;
4113 }
4114 if (tree == NULL) {
4115 VECTXT(ctxt, elem);
4116 if (ns->prefix != NULL) {
4117 VERROR(ctxt->userData,
4118"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4119 value, ns->prefix, elem->name);
4120 } else {
4121 VERROR(ctxt->userData,
4122"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4123 value, elem->name);
4124 }
4125 ret = 0;
4126 }
4127 }
4128
4129 /* Fixed Attribute Default */
4130 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4131 (!xmlStrEqual(attrDecl->defaultValue, value))) {
4132 VECTXT(ctxt, elem);
4133 if (ns->prefix != NULL) {
4134 VERROR(ctxt->userData,
4135 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4136 ns->prefix, elem->name, attrDecl->defaultValue);
4137 } else {
4138 VERROR(ctxt->userData,
4139 "Value for attribute xmlns of %s must be \"%s\"\n",
4140 elem->name, attrDecl->defaultValue);
4141 }
4142 ret = 0;
4143 }
4144
4145 /* Extra check for the attribute value */
4146 if (ns->prefix != NULL) {
4147 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4148 attrDecl->atype, value);
4149 } else {
4150 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4151 attrDecl->atype, value);
4152 }
4153
4154 return(ret);
4155}
4156
Daniel Veillard118aed72002-09-24 14:13:13 +00004157#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004158/**
4159 * xmlValidateSkipIgnorable:
4160 * @ctxt: the validation context
4161 * @child: the child list
4162 *
4163 * Skip ignorable elements w.r.t. the validation process
4164 *
4165 * returns the first element to consider for validation of the content model
4166 */
4167
4168static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004169xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004170 while (child != NULL) {
4171 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004172 /* These things are ignored (skipped) during validation. */
4173 case XML_PI_NODE:
4174 case XML_COMMENT_NODE:
4175 case XML_XINCLUDE_START:
4176 case XML_XINCLUDE_END:
4177 child = child->next;
4178 break;
4179 case XML_TEXT_NODE:
4180 if (xmlIsBlankNode(child))
4181 child = child->next;
4182 else
4183 return(child);
4184 break;
4185 /* keep current node */
4186 default:
4187 return(child);
4188 }
4189 }
4190 return(child);
4191}
4192
4193/**
4194 * xmlValidateElementType:
4195 * @ctxt: the validation context
4196 *
4197 * Try to validate the content model of an element internal function
4198 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004199 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4200 * reference is found and -3 if the validation succeeded but
4201 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004202 */
4203
4204static int
4205xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004206 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004207 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004208
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004209 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004210 if ((NODE == NULL) && (CONT == NULL))
4211 return(1);
4212 if ((NODE == NULL) &&
4213 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4214 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4215 return(1);
4216 }
4217 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004218 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004219 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004220
4221 /*
4222 * We arrive here when more states need to be examined
4223 */
4224cont:
4225
4226 /*
4227 * We just recovered from a rollback generated by a possible
4228 * epsilon transition, go directly to the analysis phase
4229 */
4230 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004231 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004232 DEBUG_VALID_STATE(NODE, CONT)
4233 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004234 goto analyze;
4235 }
4236
4237 DEBUG_VALID_STATE(NODE, CONT)
4238 /*
4239 * we may have to save a backup state here. This is the equivalent
4240 * of handling epsilon transition in NFAs.
4241 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004242 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004243 ((CONT->parent == NULL) ||
4244 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004245 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004246 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004247 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004248 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004249 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4250 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004251 }
4252
4253
4254 /*
4255 * Check first if the content matches
4256 */
4257 switch (CONT->type) {
4258 case XML_ELEMENT_CONTENT_PCDATA:
4259 if (NODE == NULL) {
4260 DEBUG_VALID_MSG("pcdata failed no node");
4261 ret = 0;
4262 break;
4263 }
4264 if (NODE->type == XML_TEXT_NODE) {
4265 DEBUG_VALID_MSG("pcdata found, skip to next");
4266 /*
4267 * go to next element in the content model
4268 * skipping ignorable elems
4269 */
4270 do {
4271 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004272 NODE = xmlValidateSkipIgnorable(NODE);
4273 if ((NODE != NULL) &&
4274 (NODE->type == XML_ENTITY_REF_NODE))
4275 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004276 } while ((NODE != NULL) &&
4277 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004278 (NODE->type != XML_TEXT_NODE) &&
4279 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004280 ret = 1;
4281 break;
4282 } else {
4283 DEBUG_VALID_MSG("pcdata failed");
4284 ret = 0;
4285 break;
4286 }
4287 break;
4288 case XML_ELEMENT_CONTENT_ELEMENT:
4289 if (NODE == NULL) {
4290 DEBUG_VALID_MSG("element failed no node");
4291 ret = 0;
4292 break;
4293 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004294 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4295 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004296 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004297 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4298 ret = (CONT->prefix == NULL);
4299 } else if (CONT->prefix == NULL) {
4300 ret = 0;
4301 } else {
4302 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4303 }
4304 }
4305 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004306 DEBUG_VALID_MSG("element found, skip to next");
4307 /*
4308 * go to next element in the content model
4309 * skipping ignorable elems
4310 */
4311 do {
4312 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004313 NODE = xmlValidateSkipIgnorable(NODE);
4314 if ((NODE != NULL) &&
4315 (NODE->type == XML_ENTITY_REF_NODE))
4316 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004317 } while ((NODE != NULL) &&
4318 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004319 (NODE->type != XML_TEXT_NODE) &&
4320 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004321 } else {
4322 DEBUG_VALID_MSG("element failed");
4323 ret = 0;
4324 break;
4325 }
4326 break;
4327 case XML_ELEMENT_CONTENT_OR:
4328 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004329 * Small optimization.
4330 */
4331 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4332 if ((NODE == NULL) ||
4333 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4334 DEPTH++;
4335 CONT = CONT->c2;
4336 goto cont;
4337 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004338 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4339 ret = (CONT->c1->prefix == NULL);
4340 } else if (CONT->c1->prefix == NULL) {
4341 ret = 0;
4342 } else {
4343 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4344 }
4345 if (ret == 0) {
4346 DEPTH++;
4347 CONT = CONT->c2;
4348 goto cont;
4349 }
Daniel Veillard85349052001-04-20 13:48:21 +00004350 }
4351
4352 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004353 * save the second branch 'or' branch
4354 */
4355 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004356 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4357 OCCURS, ROLLBACK_OR) < 0)
4358 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004359 DEPTH++;
4360 CONT = CONT->c1;
4361 goto cont;
4362 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004363 /*
4364 * Small optimization.
4365 */
4366 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4367 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4368 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4369 if ((NODE == NULL) ||
4370 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4371 DEPTH++;
4372 CONT = CONT->c2;
4373 goto cont;
4374 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004375 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4376 ret = (CONT->c1->prefix == NULL);
4377 } else if (CONT->c1->prefix == NULL) {
4378 ret = 0;
4379 } else {
4380 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4381 }
4382 if (ret == 0) {
4383 DEPTH++;
4384 CONT = CONT->c2;
4385 goto cont;
4386 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004387 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004388 DEPTH++;
4389 CONT = CONT->c1;
4390 goto cont;
4391 }
4392
4393 /*
4394 * At this point handle going up in the tree
4395 */
4396 if (ret == -1) {
4397 DEBUG_VALID_MSG("error found returning");
4398 return(ret);
4399 }
4400analyze:
4401 while (CONT != NULL) {
4402 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004403 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004404 * this level.
4405 */
4406 if (ret == 0) {
4407 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004408 xmlNodePtr cur;
4409
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004410 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004411 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004412 DEBUG_VALID_MSG("Once branch failed, rollback");
4413 if (vstateVPop(ctxt) < 0 ) {
4414 DEBUG_VALID_MSG("exhaustion, failed");
4415 return(0);
4416 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004417 if (cur != ctxt->vstate->node)
4418 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004419 goto cont;
4420 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004421 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004422 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004423 DEBUG_VALID_MSG("Plus branch failed, rollback");
4424 if (vstateVPop(ctxt) < 0 ) {
4425 DEBUG_VALID_MSG("exhaustion, failed");
4426 return(0);
4427 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004428 if (cur != ctxt->vstate->node)
4429 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004430 goto cont;
4431 }
4432 DEBUG_VALID_MSG("Plus branch found");
4433 ret = 1;
4434 break;
4435 case XML_ELEMENT_CONTENT_MULT:
4436#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004437 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004438 DEBUG_VALID_MSG("Mult branch failed");
4439 } else {
4440 DEBUG_VALID_MSG("Mult branch found");
4441 }
4442#endif
4443 ret = 1;
4444 break;
4445 case XML_ELEMENT_CONTENT_OPT:
4446 DEBUG_VALID_MSG("Option branch failed");
4447 ret = 1;
4448 break;
4449 }
4450 } else {
4451 switch (CONT->ocur) {
4452 case XML_ELEMENT_CONTENT_OPT:
4453 DEBUG_VALID_MSG("Option branch succeeded");
4454 ret = 1;
4455 break;
4456 case XML_ELEMENT_CONTENT_ONCE:
4457 DEBUG_VALID_MSG("Once branch succeeded");
4458 ret = 1;
4459 break;
4460 case XML_ELEMENT_CONTENT_PLUS:
4461 if (STATE == ROLLBACK_PARENT) {
4462 DEBUG_VALID_MSG("Plus branch rollback");
4463 ret = 1;
4464 break;
4465 }
4466 if (NODE == NULL) {
4467 DEBUG_VALID_MSG("Plus branch exhausted");
4468 ret = 1;
4469 break;
4470 }
4471 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004472 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004473 goto cont;
4474 case XML_ELEMENT_CONTENT_MULT:
4475 if (STATE == ROLLBACK_PARENT) {
4476 DEBUG_VALID_MSG("Mult branch rollback");
4477 ret = 1;
4478 break;
4479 }
4480 if (NODE == NULL) {
4481 DEBUG_VALID_MSG("Mult branch exhausted");
4482 ret = 1;
4483 break;
4484 }
4485 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004486 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004487 goto cont;
4488 }
4489 }
4490 STATE = 0;
4491
4492 /*
4493 * Then act accordingly at the parent level
4494 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004495 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004496 if (CONT->parent == NULL)
4497 break;
4498
4499 switch (CONT->parent->type) {
4500 case XML_ELEMENT_CONTENT_PCDATA:
4501 DEBUG_VALID_MSG("Error: parent pcdata");
4502 return(-1);
4503 case XML_ELEMENT_CONTENT_ELEMENT:
4504 DEBUG_VALID_MSG("Error: parent element");
4505 return(-1);
4506 case XML_ELEMENT_CONTENT_OR:
4507 if (ret == 1) {
4508 DEBUG_VALID_MSG("Or succeeded");
4509 CONT = CONT->parent;
4510 DEPTH--;
4511 } else {
4512 DEBUG_VALID_MSG("Or failed");
4513 CONT = CONT->parent;
4514 DEPTH--;
4515 }
4516 break;
4517 case XML_ELEMENT_CONTENT_SEQ:
4518 if (ret == 0) {
4519 DEBUG_VALID_MSG("Sequence failed");
4520 CONT = CONT->parent;
4521 DEPTH--;
4522 } else if (CONT == CONT->parent->c1) {
4523 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4524 CONT = CONT->parent->c2;
4525 goto cont;
4526 } else {
4527 DEBUG_VALID_MSG("Sequence succeeded");
4528 CONT = CONT->parent;
4529 DEPTH--;
4530 }
4531 }
4532 }
4533 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004534 xmlNodePtr cur;
4535
4536 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004537 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4538 if (vstateVPop(ctxt) < 0 ) {
4539 DEBUG_VALID_MSG("exhaustion, failed");
4540 return(0);
4541 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004542 if (cur != ctxt->vstate->node)
4543 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004544 goto cont;
4545 }
4546 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004547 xmlNodePtr cur;
4548
4549 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004550 DEBUG_VALID_MSG("Failure, rollback");
4551 if (vstateVPop(ctxt) < 0 ) {
4552 DEBUG_VALID_MSG("exhaustion, failed");
4553 return(0);
4554 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004555 if (cur != ctxt->vstate->node)
4556 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004557 goto cont;
4558 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004559 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004560}
Daniel Veillard23e73572002-09-19 19:56:43 +00004561#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004562
4563/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00004564 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004565 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00004566 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004567 * @content: An element
4568 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
4569 *
4570 * This will dump the list of elements to the buffer
4571 * Intended just for the debug routine
4572 */
4573static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00004574xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004575 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00004576 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004577
4578 if (node == NULL) return;
4579 if (glob) strcat(buf, "(");
4580 cur = node;
4581 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004582 len = strlen(buf);
4583 if (size - len < 50) {
4584 if ((size - len > 4) && (buf[len - 1] != '.'))
4585 strcat(buf, " ...");
4586 return;
4587 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004588 switch (cur->type) {
4589 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004590 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004591 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004592 if ((size - len > 4) && (buf[len - 1] != '.'))
4593 strcat(buf, " ...");
4594 return;
4595 }
4596 strcat(buf, (char *) cur->ns->prefix);
4597 strcat(buf, ":");
4598 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004599 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004600 if ((size - len > 4) && (buf[len - 1] != '.'))
4601 strcat(buf, " ...");
4602 return;
4603 }
4604 strcat(buf, (char *) cur->name);
4605 if (cur->next != NULL)
4606 strcat(buf, " ");
4607 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004608 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004609 if (xmlIsBlankNode(cur))
4610 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004611 case XML_CDATA_SECTION_NODE:
4612 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004613 strcat(buf, "CDATA");
4614 if (cur->next != NULL)
4615 strcat(buf, " ");
4616 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004617 case XML_ATTRIBUTE_NODE:
4618 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004619#ifdef LIBXML_DOCB_ENABLED
4620 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004621#endif
4622 case XML_HTML_DOCUMENT_NODE:
4623 case XML_DOCUMENT_TYPE_NODE:
4624 case XML_DOCUMENT_FRAG_NODE:
4625 case XML_NOTATION_NODE:
4626 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004627 strcat(buf, "???");
4628 if (cur->next != NULL)
4629 strcat(buf, " ");
4630 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004631 case XML_ENTITY_NODE:
4632 case XML_PI_NODE:
4633 case XML_DTD_NODE:
4634 case XML_COMMENT_NODE:
4635 case XML_ELEMENT_DECL:
4636 case XML_ATTRIBUTE_DECL:
4637 case XML_ENTITY_DECL:
4638 case XML_XINCLUDE_START:
4639 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004640 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004641 }
4642 cur = cur->next;
4643 }
4644 if (glob) strcat(buf, ")");
4645}
4646
4647/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004648 * xmlValidateElementContent:
4649 * @ctxt: the validation context
4650 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004651 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004652 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004653 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004654 *
4655 * Try to validate the content model of an element
4656 *
4657 * returns 1 if valid or 0 if not and -1 in case of error
4658 */
4659
4660static int
4661xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004662 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004663 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00004664#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00004665 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00004666#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00004667 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004668 xmlElementContentPtr cont;
4669 const xmlChar *name;
4670
4671 if (elemDecl == NULL)
4672 return(-1);
4673 cont = elemDecl->content;
4674 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004675
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004676#ifdef LIBXML_REGEXP_ENABLED
4677 /* Build the regexp associated to the content model */
4678 if (elemDecl->contModel == NULL)
4679 ret = xmlValidBuildContentModel(ctxt, elemDecl);
4680 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004681 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004682 } else {
4683 xmlRegExecCtxtPtr exec;
4684
Daniel Veillardec498e12003-02-05 11:01:50 +00004685 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
4686 return(-1);
4687 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004688 ctxt->nodeMax = 0;
4689 ctxt->nodeNr = 0;
4690 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004691 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
4692 if (exec != NULL) {
4693 cur = child;
4694 while (cur != NULL) {
4695 switch (cur->type) {
4696 case XML_ENTITY_REF_NODE:
4697 /*
4698 * Push the current node to be able to roll back
4699 * and process within the entity
4700 */
4701 if ((cur->children != NULL) &&
4702 (cur->children->children != NULL)) {
4703 nodeVPush(ctxt, cur);
4704 cur = cur->children->children;
4705 continue;
4706 }
4707 break;
4708 case XML_TEXT_NODE:
4709 if (xmlIsBlankNode(cur))
4710 break;
4711 ret = 0;
4712 goto fail;
4713 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00004714 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004715 ret = 0;
4716 goto fail;
4717 case XML_ELEMENT_NODE:
4718 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004719 xmlChar fn[50];
4720 xmlChar *fullname;
4721
4722 fullname = xmlBuildQName(cur->name,
4723 cur->ns->prefix, fn, 50);
4724 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004725 ret = -1;
4726 goto fail;
4727 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004728 ret = xmlRegExecPushString(exec, fullname, NULL);
4729 if ((fullname != fn) && (fullname != cur->name))
4730 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004731 } else {
4732 ret = xmlRegExecPushString(exec, cur->name, NULL);
4733 }
4734 break;
4735 default:
4736 break;
4737 }
4738 /*
4739 * Switch to next element
4740 */
4741 cur = cur->next;
4742 while (cur == NULL) {
4743 cur = nodeVPop(ctxt);
4744 if (cur == NULL)
4745 break;
4746 cur = cur->next;
4747 }
4748 }
4749 ret = xmlRegExecPushString(exec, NULL, NULL);
4750fail:
4751 xmlRegFreeExecCtxt(exec);
4752 }
4753 }
4754#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004755 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004756 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004757 */
4758 ctxt->vstateMax = 8;
4759 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4760 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4761 if (ctxt->vstateTab == NULL) {
4762 xmlGenericError(xmlGenericErrorContext,
4763 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004764 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004765 }
4766 /*
4767 * The first entry in the stack is reserved to the current state
4768 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004769 ctxt->nodeMax = 0;
4770 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004771 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004772 ctxt->vstate = &ctxt->vstateTab[0];
4773 ctxt->vstateNr = 1;
4774 CONT = cont;
4775 NODE = child;
4776 DEPTH = 0;
4777 OCCURS = 0;
4778 STATE = 0;
4779 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004780 if ((ret == -3) && (warn)) {
4781 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004782 "Content model for Element %s is ambiguous\n", name);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004783 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004784 /*
4785 * An entities reference appeared at this level.
4786 * Buid a minimal representation of this node content
4787 * sufficient to run the validation process on it
4788 */
4789 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004790 cur = child;
4791 while (cur != NULL) {
4792 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004793 case XML_ENTITY_REF_NODE:
4794 /*
4795 * Push the current node to be able to roll back
4796 * and process within the entity
4797 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004798 if ((cur->children != NULL) &&
4799 (cur->children->children != NULL)) {
4800 nodeVPush(ctxt, cur);
4801 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004802 continue;
4803 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004804 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004805 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004806 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004807 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004808 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004809 case XML_CDATA_SECTION_NODE:
4810 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004811 case XML_ELEMENT_NODE:
4812 /*
4813 * Allocate a new node and minimally fills in
4814 * what's required
4815 */
4816 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4817 if (tmp == NULL) {
4818 xmlGenericError(xmlGenericErrorContext,
4819 "xmlValidateElementContent : malloc failed\n");
4820 xmlFreeNodeList(repl);
4821 ret = -1;
4822 goto done;
4823 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004824 tmp->type = cur->type;
4825 tmp->name = cur->name;
4826 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004827 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004828 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004829 if (repl == NULL)
4830 repl = last = tmp;
4831 else {
4832 last->next = tmp;
4833 last = tmp;
4834 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004835 if (cur->type == XML_CDATA_SECTION_NODE) {
4836 /*
4837 * E59 spaces in CDATA does not match the
4838 * nonterminal S
4839 */
4840 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4841 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004842 break;
4843 default:
4844 break;
4845 }
4846 /*
4847 * Switch to next element
4848 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004849 cur = cur->next;
4850 while (cur == NULL) {
4851 cur = nodeVPop(ctxt);
4852 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004853 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004854 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004855 }
4856 }
4857
4858 /*
4859 * Relaunch the validation
4860 */
4861 ctxt->vstate = &ctxt->vstateTab[0];
4862 ctxt->vstateNr = 1;
4863 CONT = cont;
4864 NODE = repl;
4865 DEPTH = 0;
4866 OCCURS = 0;
4867 STATE = 0;
4868 ret = xmlValidateElementType(ctxt);
4869 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004870#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004871 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004872 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4873 char expr[5000];
4874 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004875
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004876 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004877 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004878 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00004879#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004880 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004881 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004882 else
Daniel Veillard01992e02002-10-09 10:20:30 +00004883#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004884 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004885
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004886 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004887 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004888 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004889 "Element %s content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004890 name, expr, list);
4891 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004892 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004893 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004894 "Element content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004895 expr, list);
4896 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004897 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004898 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004899 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004900 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004901 "Element %s content does not follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004902 name);
4903 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004904 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004905 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004906 "Element content does not follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004907 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004908 }
4909 ret = 0;
4910 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004911 if (ret == -3)
4912 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004913
Daniel Veillard23e73572002-09-19 19:56:43 +00004914#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004915done:
4916 /*
4917 * Deallocate the copy if done, and free up the validation stack
4918 */
4919 while (repl != NULL) {
4920 tmp = repl->next;
4921 xmlFree(repl);
4922 repl = tmp;
4923 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004924 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004925 if (ctxt->vstateTab != NULL) {
4926 xmlFree(ctxt->vstateTab);
4927 ctxt->vstateTab = NULL;
4928 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004929#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004930 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004931 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004932 if (ctxt->nodeTab != NULL) {
4933 xmlFree(ctxt->nodeTab);
4934 ctxt->nodeTab = NULL;
4935 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004936 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004937
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004938}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004939
Owen Taylor3473f882001-02-23 17:55:21 +00004940/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004941 * xmlValidateCdataElement:
4942 * @ctxt: the validation context
4943 * @doc: a document instance
4944 * @elem: an element instance
4945 *
4946 * Check that an element follows #CDATA
4947 *
4948 * returns 1 if valid or 0 otherwise
4949 */
4950static int
4951xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4952 xmlNodePtr elem) {
4953 int ret = 1;
4954 xmlNodePtr cur, child;
4955
Daniel Veillardceb09b92002-10-04 11:46:37 +00004956 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004957 return(0);
4958
4959 child = elem->children;
4960
4961 cur = child;
4962 while (cur != NULL) {
4963 switch (cur->type) {
4964 case XML_ENTITY_REF_NODE:
4965 /*
4966 * Push the current node to be able to roll back
4967 * and process within the entity
4968 */
4969 if ((cur->children != NULL) &&
4970 (cur->children->children != NULL)) {
4971 nodeVPush(ctxt, cur);
4972 cur = cur->children->children;
4973 continue;
4974 }
4975 break;
4976 case XML_COMMENT_NODE:
4977 case XML_PI_NODE:
4978 case XML_TEXT_NODE:
4979 case XML_CDATA_SECTION_NODE:
4980 break;
4981 default:
4982 ret = 0;
4983 goto done;
4984 }
4985 /*
4986 * Switch to next element
4987 */
4988 cur = cur->next;
4989 while (cur == NULL) {
4990 cur = nodeVPop(ctxt);
4991 if (cur == NULL)
4992 break;
4993 cur = cur->next;
4994 }
4995 }
4996done:
4997 ctxt->nodeMax = 0;
4998 ctxt->nodeNr = 0;
4999 if (ctxt->nodeTab != NULL) {
5000 xmlFree(ctxt->nodeTab);
5001 ctxt->nodeTab = NULL;
5002 }
5003 return(ret);
5004}
5005
5006/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005007 * xmlValidateCheckMixed:
5008 * @ctxt: the validation context
5009 * @cont: the mixed content model
5010 * @qname: the qualified name as appearing in the serialization
5011 *
5012 * Check if the given node is part of the content model.
5013 *
5014 * Returns 1 if yes, 0 if no, -1 in case of error
5015 */
5016static int
5017xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
5018 xmlElementContentPtr cont, const xmlChar *qname) {
5019 while (cont != NULL) {
5020 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5021 if (xmlStrEqual(cont->name, qname))
5022 return(1);
5023 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5024 (cont->c1 != NULL) &&
5025 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5026 if (xmlStrEqual(cont->c1->name, qname))
5027 return(1);
5028 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5029 (cont->c1 == NULL) ||
5030 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5031 /* Internal error !!! */
5032 xmlGenericError(xmlGenericErrorContext,
5033 "Internal: MIXED struct bad\n");
5034 break;
5035 }
5036 cont = cont->c2;
5037 }
5038 return(0);
5039}
5040
5041/**
5042 * xmlValidGetElemDecl:
5043 * @ctxt: the validation context
5044 * @doc: a document instance
5045 * @elem: an element instance
5046 * @extsubset: pointer, (out) indicate if the declaration was found
5047 * in the external subset.
5048 *
5049 * Finds a declaration associated to an element in the document.
5050 *
5051 * returns the pointer to the declaration or NULL if not found.
5052 */
5053static xmlElementPtr
5054xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5055 xmlNodePtr elem, int *extsubset) {
5056 xmlElementPtr elemDecl = NULL;
5057 const xmlChar *prefix = NULL;
5058
5059 if ((elem == NULL) || (elem->name == NULL)) return(NULL);
5060 if (extsubset != NULL)
5061 *extsubset = 0;
5062
5063 /*
5064 * Fetch the declaration for the qualified name
5065 */
5066 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5067 prefix = elem->ns->prefix;
5068
5069 if (prefix != NULL) {
5070 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5071 elem->name, prefix);
5072 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5073 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5074 elem->name, prefix);
5075 if ((elemDecl != NULL) && (extsubset != NULL))
5076 *extsubset = 1;
5077 }
5078 }
5079
5080 /*
5081 * Fetch the declaration for the non qualified name
5082 * This is "non-strict" validation should be done on the
5083 * full QName but in that case being flexible makes sense.
5084 */
5085 if (elemDecl == NULL) {
5086 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5087 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5088 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5089 if ((elemDecl != NULL) && (extsubset != NULL))
5090 *extsubset = 1;
5091 }
5092 }
5093 if (elemDecl == NULL) {
5094 VECTXT(ctxt, elem);
5095 VERROR(ctxt->userData, "No declaration for element %s\n",
5096 elem->name);
5097 }
5098 return(elemDecl);
5099}
5100
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005101#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005102/**
5103 * xmlValidatePushElement:
5104 * @ctxt: the validation context
5105 * @doc: a document instance
5106 * @elem: an element instance
5107 * @qname: the qualified name as appearing in the serialization
5108 *
5109 * Push a new element start on the validation stack.
5110 *
5111 * returns 1 if no validation problem was found or 0 otherwise
5112 */
5113int
5114xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5115 xmlNodePtr elem, const xmlChar *qname) {
5116 int ret = 1;
5117 xmlElementPtr eDecl;
5118 int extsubset = 0;
5119
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005120/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005121 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5122 xmlValidStatePtr state = ctxt->vstate;
5123 xmlElementPtr elemDecl;
5124
5125 /*
5126 * Check the new element agaisnt the content model of the new elem.
5127 */
5128 if (state->elemDecl != NULL) {
5129 elemDecl = state->elemDecl;
5130
5131 switch(elemDecl->etype) {
5132 case XML_ELEMENT_TYPE_UNDEFINED:
5133 ret = 0;
5134 break;
5135 case XML_ELEMENT_TYPE_EMPTY:
5136 VECTXT(ctxt, state->node);
5137 VERROR(ctxt->userData,
5138 "Element %s was declared EMPTY this one has content\n",
5139 state->node->name);
5140 ret = 0;
5141 break;
5142 case XML_ELEMENT_TYPE_ANY:
5143 /* I don't think anything is required then */
5144 break;
5145 case XML_ELEMENT_TYPE_MIXED:
5146 /* simple case of declared as #PCDATA */
5147 if ((elemDecl->content != NULL) &&
5148 (elemDecl->content->type ==
5149 XML_ELEMENT_CONTENT_PCDATA)) {
5150 VECTXT(ctxt, state->node);
5151 VERROR(ctxt->userData,
5152 "Element %s was declared #PCDATA but contains non text nodes\n",
5153 state->node->name);
5154 ret = 0;
5155 } else {
5156 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5157 qname);
5158 if (ret != 1) {
5159 VECTXT(ctxt, state->node);
5160 VERROR(ctxt->userData,
5161 "Element %s is not declared in %s list of possible children\n",
5162 qname, state->node->name);
5163 }
5164 }
5165 break;
5166 case XML_ELEMENT_TYPE_ELEMENT:
5167 /*
5168 * TODO:
5169 * VC: Standalone Document Declaration
5170 * - element types with element content, if white space
5171 * occurs directly within any instance of those types.
5172 */
5173 if (state->exec != NULL) {
5174 ret = xmlRegExecPushString(state->exec, qname, NULL);
5175 if (ret < 0) {
5176 VECTXT(ctxt, state->node);
5177 VERROR(ctxt->userData,
5178 "Element %s content does not follow the DTD\nMisplaced %s\n",
5179 state->node->name, qname);
5180 ret = 0;
5181 } else {
5182 ret = 1;
5183 }
5184 }
5185 break;
5186 }
5187 }
5188 }
5189 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5190 vstateVPush(ctxt, eDecl, elem);
5191 return(ret);
5192}
5193
5194/**
5195 * xmlValidatePushCData:
5196 * @ctxt: the validation context
5197 * @data: some character data read
5198 * @len: the lenght of the data
5199 *
5200 * check the CData parsed for validation in the current stack
5201 *
5202 * returns 1 if no validation problem was found or 0 otherwise
5203 */
5204int
5205xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5206 int ret = 1;
5207
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005208/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005209 if (len <= 0)
5210 return(ret);
5211 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5212 xmlValidStatePtr state = ctxt->vstate;
5213 xmlElementPtr elemDecl;
5214
5215 /*
5216 * Check the new element agaisnt the content model of the new elem.
5217 */
5218 if (state->elemDecl != NULL) {
5219 elemDecl = state->elemDecl;
5220
5221 switch(elemDecl->etype) {
5222 case XML_ELEMENT_TYPE_UNDEFINED:
5223 ret = 0;
5224 break;
5225 case XML_ELEMENT_TYPE_EMPTY:
5226 VECTXT(ctxt, state->node);
5227 VERROR(ctxt->userData,
5228 "Element %s was declared EMPTY this one has content\n",
5229 state->node->name);
5230 ret = 0;
5231 break;
5232 case XML_ELEMENT_TYPE_ANY:
5233 break;
5234 case XML_ELEMENT_TYPE_MIXED:
5235 break;
5236 case XML_ELEMENT_TYPE_ELEMENT:
5237 if (len > 0) {
5238 int i;
5239
5240 for (i = 0;i < len;i++) {
5241 if (!IS_BLANK(data[i])) {
5242 VECTXT(ctxt, state->node);
5243 VERROR(ctxt->userData,
5244 "Element %s content does not follow the DTD\nText not allowed\n",
5245 state->node->name);
5246 ret = 0;
5247 goto done;
5248 }
5249 }
5250 /*
5251 * TODO:
5252 * VC: Standalone Document Declaration
5253 * element types with element content, if white space
5254 * occurs directly within any instance of those types.
5255 */
5256 }
5257 break;
5258 }
5259 }
5260 }
5261done:
5262 return(ret);
5263}
5264
5265/**
5266 * xmlValidatePopElement:
5267 * @ctxt: the validation context
5268 * @doc: a document instance
5269 * @elem: an element instance
5270 * @qname: the qualified name as appearing in the serialization
5271 *
5272 * Pop the element end from the validation stack.
5273 *
5274 * returns 1 if no validation problem was found or 0 otherwise
5275 */
5276int
5277xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005278 xmlNodePtr elem ATTRIBUTE_UNUSED,
5279 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005280 int ret = 1;
5281
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005282/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005283 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5284 xmlValidStatePtr state = ctxt->vstate;
5285 xmlElementPtr elemDecl;
5286
5287 /*
5288 * Check the new element agaisnt the content model of the new elem.
5289 */
5290 if (state->elemDecl != NULL) {
5291 elemDecl = state->elemDecl;
5292
5293 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5294 if (state->exec != NULL) {
5295 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5296 if (ret == 0) {
5297 VECTXT(ctxt, state->node);
5298 VERROR(ctxt->userData,
5299 "Element %s content does not follow the DTD\nExpecting more child\n",
5300 state->node->name);
5301 } else {
5302 /*
5303 * previous validation errors should not generate
5304 * a new one here
5305 */
5306 ret = 1;
5307 }
5308 }
5309 }
5310 }
5311 vstateVPop(ctxt);
5312 }
5313 return(ret);
5314}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005315#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005316
5317/**
Owen Taylor3473f882001-02-23 17:55:21 +00005318 * xmlValidateOneElement:
5319 * @ctxt: the validation context
5320 * @doc: a document instance
5321 * @elem: an element instance
5322 *
5323 * Try to validate a single element and it's attributes,
5324 * basically it does the following checks as described by the
5325 * XML-1.0 recommendation:
5326 * - [ VC: Element Valid ]
5327 * - [ VC: Required Attribute ]
5328 * Then call xmlValidateOneAttribute() for each attribute present.
5329 *
5330 * The ID/IDREF checkings are done separately
5331 *
5332 * returns 1 if valid or 0 otherwise
5333 */
5334
5335int
5336xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5337 xmlNodePtr elem) {
5338 xmlElementPtr elemDecl = NULL;
5339 xmlElementContentPtr cont;
5340 xmlAttributePtr attr;
5341 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005342 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005343 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005344 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005345
5346 CHECK_DTD;
5347
5348 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005349 switch (elem->type) {
5350 case XML_ATTRIBUTE_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005351 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005352 VERROR(ctxt->userData,
5353 "Attribute element not expected here\n");
5354 return(0);
5355 case XML_TEXT_NODE:
5356 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005357 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005358 VERROR(ctxt->userData, "Text element has childs !\n");
5359 return(0);
5360 }
5361 if (elem->properties != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005362 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005363 VERROR(ctxt->userData, "Text element has attributes !\n");
5364 return(0);
5365 }
5366 if (elem->ns != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005367 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005368 VERROR(ctxt->userData, "Text element has namespace !\n");
5369 return(0);
5370 }
5371 if (elem->nsDef != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005372 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005373 VERROR(ctxt->userData,
5374 "Text element carries namespace definitions !\n");
5375 return(0);
5376 }
5377 if (elem->content == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005378 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005379 VERROR(ctxt->userData,
5380 "Text element has no content !\n");
5381 return(0);
5382 }
5383 return(1);
5384 case XML_XINCLUDE_START:
5385 case XML_XINCLUDE_END:
5386 return(1);
5387 case XML_CDATA_SECTION_NODE:
5388 case XML_ENTITY_REF_NODE:
5389 case XML_PI_NODE:
5390 case XML_COMMENT_NODE:
5391 return(1);
5392 case XML_ENTITY_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005393 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005394 VERROR(ctxt->userData,
5395 "Entity element not expected here\n");
5396 return(0);
5397 case XML_NOTATION_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005398 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005399 VERROR(ctxt->userData,
5400 "Notation element not expected here\n");
5401 return(0);
5402 case XML_DOCUMENT_NODE:
5403 case XML_DOCUMENT_TYPE_NODE:
5404 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005405 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005406 VERROR(ctxt->userData,
5407 "Document element not expected here\n");
5408 return(0);
5409 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005410 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005411 VERROR(ctxt->userData,
5412 "\n");
5413 return(0);
5414 case XML_ELEMENT_NODE:
5415 break;
5416 default:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005417 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005418 VERROR(ctxt->userData,
5419 "unknown element type %d\n", elem->type);
5420 return(0);
5421 }
Owen Taylor3473f882001-02-23 17:55:21 +00005422
5423 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005424 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005425 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005426 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5427 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005428 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005429
Daniel Veillardea7751d2002-12-20 00:16:24 +00005430 /*
5431 * If vstateNr is not zero that means continuous validation is
5432 * activated, do not try to check the content model at that level.
5433 */
5434 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005435 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005436 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005437 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005438 VECTXT(ctxt, elem);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005439 VERROR(ctxt->userData, "No declaration for element %s\n",
5440 elem->name);
5441 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005442 case XML_ELEMENT_TYPE_EMPTY:
5443 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005444 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005445 VERROR(ctxt->userData,
5446 "Element %s was declared EMPTY this one has content\n",
5447 elem->name);
5448 ret = 0;
5449 }
5450 break;
5451 case XML_ELEMENT_TYPE_ANY:
5452 /* I don't think anything is required then */
5453 break;
5454 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005455
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005456 /* simple case of declared as #PCDATA */
5457 if ((elemDecl->content != NULL) &&
5458 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5459 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5460 if (!ret) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005461 VECTXT(ctxt, elem);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005462 VERROR(ctxt->userData,
5463 "Element %s was declared #PCDATA but contains non text nodes\n",
5464 elem->name);
5465 }
5466 break;
5467 }
Owen Taylor3473f882001-02-23 17:55:21 +00005468 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005469 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005470 while (child != NULL) {
5471 if (child->type == XML_ELEMENT_NODE) {
5472 name = child->name;
5473 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005474 xmlChar fn[50];
5475 xmlChar *fullname;
5476
5477 fullname = xmlBuildQName(child->name, child->ns->prefix,
5478 fn, 50);
5479 if (fullname == NULL)
5480 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005481 cont = elemDecl->content;
5482 while (cont != NULL) {
5483 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005484 if (xmlStrEqual(cont->name, fullname))
5485 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005486 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5487 (cont->c1 != NULL) &&
5488 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005489 if (xmlStrEqual(cont->c1->name, fullname))
5490 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005491 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5492 (cont->c1 == NULL) ||
5493 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5494 /* Internal error !!! */
5495 xmlGenericError(xmlGenericErrorContext,
5496 "Internal: MIXED struct bad\n");
5497 break;
5498 }
5499 cont = cont->c2;
5500 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005501 if ((fullname != fn) && (fullname != child->name))
5502 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005503 if (cont != NULL)
5504 goto child_ok;
5505 }
5506 cont = elemDecl->content;
5507 while (cont != NULL) {
5508 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5509 if (xmlStrEqual(cont->name, name)) break;
5510 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5511 (cont->c1 != NULL) &&
5512 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5513 if (xmlStrEqual(cont->c1->name, name)) break;
5514 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5515 (cont->c1 == NULL) ||
5516 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
5517 /* Internal error !!! */
5518 xmlGenericError(xmlGenericErrorContext,
5519 "Internal: MIXED struct bad\n");
5520 break;
5521 }
5522 cont = cont->c2;
5523 }
5524 if (cont == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005525 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005526 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005527 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005528 name, elem->name);
5529 ret = 0;
5530 }
5531 }
5532child_ok:
5533 child = child->next;
5534 }
5535 break;
5536 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005537 if ((doc->standalone == 1) && (extsubset == 1)) {
5538 /*
5539 * VC: Standalone Document Declaration
5540 * - element types with element content, if white space
5541 * occurs directly within any instance of those types.
5542 */
5543 child = elem->children;
5544 while (child != NULL) {
5545 if (child->type == XML_TEXT_NODE) {
5546 const xmlChar *content = child->content;
5547
5548 while (IS_BLANK(*content))
5549 content++;
5550 if (*content == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005551 VECTXT(ctxt, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005552 VERROR(ctxt->userData,
5553"standalone: %s declared in the external subset contains white spaces nodes\n",
5554 elem->name);
5555 ret = 0;
5556 break;
5557 }
5558 }
5559 child =child->next;
5560 }
5561 }
Owen Taylor3473f882001-02-23 17:55:21 +00005562 child = elem->children;
5563 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005564 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005565 if (tmp <= 0)
5566 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005567 break;
5568 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005569 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00005570
5571 /* [ VC: Required Attribute ] */
5572 attr = elemDecl->attributes;
5573 while (attr != NULL) {
5574 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005575 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00005576
Daniel Veillarde4301c82002-02-13 13:32:35 +00005577 if ((attr->prefix == NULL) &&
5578 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5579 xmlNsPtr ns;
5580
5581 ns = elem->nsDef;
5582 while (ns != NULL) {
5583 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005584 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005585 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00005586 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005587 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5588 xmlNsPtr ns;
5589
5590 ns = elem->nsDef;
5591 while (ns != NULL) {
5592 if (xmlStrEqual(attr->name, ns->prefix))
5593 goto found;
5594 ns = ns->next;
5595 }
5596 } else {
5597 xmlAttrPtr attrib;
5598
5599 attrib = elem->properties;
5600 while (attrib != NULL) {
5601 if (xmlStrEqual(attrib->name, attr->name)) {
5602 if (attr->prefix != NULL) {
5603 xmlNsPtr nameSpace = attrib->ns;
5604
5605 if (nameSpace == NULL)
5606 nameSpace = elem->ns;
5607 /*
5608 * qualified names handling is problematic, having a
5609 * different prefix should be possible but DTDs don't
5610 * allow to define the URI instead of the prefix :-(
5611 */
5612 if (nameSpace == NULL) {
5613 if (qualified < 0)
5614 qualified = 0;
5615 } else if (!xmlStrEqual(nameSpace->prefix,
5616 attr->prefix)) {
5617 if (qualified < 1)
5618 qualified = 1;
5619 } else
5620 goto found;
5621 } else {
5622 /*
5623 * We should allow applications to define namespaces
5624 * for their application even if the DTD doesn't
5625 * carry one, otherwise, basically we would always
5626 * break.
5627 */
5628 goto found;
5629 }
5630 }
5631 attrib = attrib->next;
5632 }
Owen Taylor3473f882001-02-23 17:55:21 +00005633 }
5634 if (qualified == -1) {
5635 if (attr->prefix == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005636 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005637 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005638 "Element %s does not carry attribute %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005639 elem->name, attr->name);
5640 ret = 0;
5641 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005642 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005643 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005644 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005645 elem->name, attr->prefix,attr->name);
5646 ret = 0;
5647 }
5648 } else if (qualified == 0) {
5649 VWARNING(ctxt->userData,
5650 "Element %s required attribute %s:%s has no prefix\n",
5651 elem->name, attr->prefix,attr->name);
5652 } else if (qualified == 1) {
5653 VWARNING(ctxt->userData,
5654 "Element %s required attribute %s:%s has different prefix\n",
5655 elem->name, attr->prefix,attr->name);
5656 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005657 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
5658 /*
5659 * Special tests checking #FIXED namespace declarations
5660 * have the right value since this is not done as an
5661 * attribute checking
5662 */
5663 if ((attr->prefix == NULL) &&
5664 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5665 xmlNsPtr ns;
5666
5667 ns = elem->nsDef;
5668 while (ns != NULL) {
5669 if (ns->prefix == NULL) {
5670 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005671 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005672 VERROR(ctxt->userData,
5673 "Element %s namespace name for default namespace does not match the DTD\n",
5674 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00005675 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005676 }
5677 goto found;
5678 }
5679 ns = ns->next;
5680 }
5681 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5682 xmlNsPtr ns;
5683
5684 ns = elem->nsDef;
5685 while (ns != NULL) {
5686 if (xmlStrEqual(attr->name, ns->prefix)) {
5687 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005688 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005689 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005690 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillarde4301c82002-02-13 13:32:35 +00005691 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00005692 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005693 }
5694 goto found;
5695 }
5696 ns = ns->next;
5697 }
5698 }
Owen Taylor3473f882001-02-23 17:55:21 +00005699 }
5700found:
5701 attr = attr->nexth;
5702 }
5703 return(ret);
5704}
5705
5706/**
5707 * xmlValidateRoot:
5708 * @ctxt: the validation context
5709 * @doc: a document instance
5710 *
5711 * Try to validate a the root element
5712 * basically it does the following check as described by the
5713 * XML-1.0 recommendation:
5714 * - [ VC: Root Element Type ]
5715 * it doesn't try to recurse or apply other check to the element
5716 *
5717 * returns 1 if valid or 0 otherwise
5718 */
5719
5720int
5721xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5722 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00005723 int ret;
5724
Owen Taylor3473f882001-02-23 17:55:21 +00005725 if (doc == NULL) return(0);
5726
5727 root = xmlDocGetRootElement(doc);
5728 if ((root == NULL) || (root->name == NULL)) {
5729 VERROR(ctxt->userData, "Not valid: no root element\n");
5730 return(0);
5731 }
5732
5733 /*
5734 * When doing post validation against a separate DTD, those may
5735 * no internal subset has been generated
5736 */
5737 if ((doc->intSubset != NULL) &&
5738 (doc->intSubset->name != NULL)) {
5739 /*
5740 * Check first the document root against the NQName
5741 */
5742 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
5743 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005744 xmlChar fn[50];
5745 xmlChar *fullname;
5746
5747 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
5748 if (fullname == NULL) {
5749 VERROR(ctxt->userData, "Out of memory\n");
5750 return(0);
5751 }
5752 ret = xmlStrEqual(doc->intSubset->name, fullname);
5753 if ((fullname != fn) && (fullname != root->name))
5754 xmlFree(fullname);
5755 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00005756 goto name_ok;
5757 }
5758 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
5759 (xmlStrEqual(root->name, BAD_CAST "html")))
5760 goto name_ok;
Daniel Veillard76575762002-09-05 14:21:15 +00005761 VECTXT(ctxt, root);
Owen Taylor3473f882001-02-23 17:55:21 +00005762 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005763 "Not valid: root and DTD name do not match '%s' and '%s'\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005764 root->name, doc->intSubset->name);
5765 return(0);
5766
5767 }
5768 }
5769name_ok:
5770 return(1);
5771}
5772
5773
5774/**
5775 * xmlValidateElement:
5776 * @ctxt: the validation context
5777 * @doc: a document instance
5778 * @elem: an element instance
5779 *
5780 * Try to validate the subtree under an element
5781 *
5782 * returns 1 if valid or 0 otherwise
5783 */
5784
5785int
5786xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
5787 xmlNodePtr child;
5788 xmlAttrPtr attr;
5789 xmlChar *value;
5790 int ret = 1;
5791
5792 if (elem == NULL) return(0);
5793
5794 /*
5795 * XInclude elements were added after parsing in the infoset,
5796 * they don't really mean anything validation wise.
5797 */
5798 if ((elem->type == XML_XINCLUDE_START) ||
5799 (elem->type == XML_XINCLUDE_END))
5800 return(1);
5801
5802 CHECK_DTD;
5803
Daniel Veillard10ea86c2001-06-20 13:55:33 +00005804 /*
5805 * Entities references have to be handled separately
5806 */
5807 if (elem->type == XML_ENTITY_REF_NODE) {
5808 return(1);
5809 }
5810
Owen Taylor3473f882001-02-23 17:55:21 +00005811 ret &= xmlValidateOneElement(ctxt, doc, elem);
5812 attr = elem->properties;
5813 while(attr != NULL) {
5814 value = xmlNodeListGetString(doc, attr->children, 0);
5815 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
5816 if (value != NULL)
5817 xmlFree(value);
5818 attr= attr->next;
5819 }
5820 child = elem->children;
5821 while (child != NULL) {
5822 ret &= xmlValidateElement(ctxt, doc, child);
5823 child = child->next;
5824 }
5825
5826 return(ret);
5827}
5828
Daniel Veillard8730c562001-02-26 10:49:57 +00005829/**
5830 * xmlValidateRef:
5831 * @ref: A reference to be validated
5832 * @ctxt: Validation context
5833 * @name: Name of ID we are searching for
5834 *
5835 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005836static void
Daniel Veillard8730c562001-02-26 10:49:57 +00005837xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00005838 const xmlChar *name) {
5839 xmlAttrPtr id;
5840 xmlAttrPtr attr;
5841
5842 if (ref == NULL)
5843 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005844 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005845 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005846 attr = ref->attr;
5847 if (attr == NULL) {
5848 xmlChar *dup, *str = NULL, *cur, save;
5849
5850 dup = xmlStrdup(name);
5851 if (dup == NULL) {
5852 ctxt->valid = 0;
5853 return;
5854 }
5855 cur = dup;
5856 while (*cur != 0) {
5857 str = cur;
5858 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5859 save = *cur;
5860 *cur = 0;
5861 id = xmlGetID(ctxt->doc, str);
5862 if (id == NULL) {
5863 VERROR(ctxt->userData,
5864 "attribute %s line %d references an unknown ID \"%s\"\n",
5865 ref->name, ref->lineno, str);
5866 ctxt->valid = 0;
5867 }
5868 if (save == 0)
5869 break;
5870 *cur = save;
5871 while (IS_BLANK(*cur)) cur++;
5872 }
5873 xmlFree(dup);
5874 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00005875 id = xmlGetID(ctxt->doc, name);
5876 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005877 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005878 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005879 "IDREF attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005880 attr->name, name);
5881 ctxt->valid = 0;
5882 }
5883 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
5884 xmlChar *dup, *str = NULL, *cur, save;
5885
5886 dup = xmlStrdup(name);
5887 if (dup == NULL) {
5888 ctxt->valid = 0;
5889 return;
5890 }
5891 cur = dup;
5892 while (*cur != 0) {
5893 str = cur;
5894 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5895 save = *cur;
5896 *cur = 0;
5897 id = xmlGetID(ctxt->doc, str);
5898 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005899 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005900 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005901 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005902 attr->name, str);
5903 ctxt->valid = 0;
5904 }
5905 if (save == 0)
5906 break;
5907 *cur = save;
5908 while (IS_BLANK(*cur)) cur++;
5909 }
5910 xmlFree(dup);
5911 }
5912}
5913
5914/**
Daniel Veillard8730c562001-02-26 10:49:57 +00005915 * xmlWalkValidateList:
5916 * @data: Contents of current link
5917 * @user: Value supplied by the user
5918 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005919 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00005920 */
5921static int
5922xmlWalkValidateList(const void *data, const void *user)
5923{
5924 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
5925 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
5926 return 1;
5927}
5928
5929/**
5930 * xmlValidateCheckRefCallback:
5931 * @ref_list: List of references
5932 * @ctxt: Validation context
5933 * @name: Name of ID we are searching for
5934 *
5935 */
5936static void
5937xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
5938 const xmlChar *name) {
5939 xmlValidateMemo memo;
5940
5941 if (ref_list == NULL)
5942 return;
5943 memo.ctxt = ctxt;
5944 memo.name = name;
5945
5946 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
5947
5948}
5949
5950/**
Owen Taylor3473f882001-02-23 17:55:21 +00005951 * xmlValidateDocumentFinal:
5952 * @ctxt: the validation context
5953 * @doc: a document instance
5954 *
5955 * Does the final step for the document validation once all the
5956 * incremental validation steps have been completed
5957 *
5958 * basically it does the following checks described by the XML Rec
5959 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00005960 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00005961 *
5962 * returns 1 if valid or 0 otherwise
5963 */
5964
5965int
5966xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5967 xmlRefTablePtr table;
5968
5969 if (doc == NULL) {
5970 xmlGenericError(xmlGenericErrorContext,
5971 "xmlValidateDocumentFinal: doc == NULL\n");
5972 return(0);
5973 }
5974
5975 /*
5976 * Check all the NOTATION/NOTATIONS attributes
5977 */
5978 /*
5979 * Check all the ENTITY/ENTITIES attributes definition for validity
5980 */
5981 /*
5982 * Check all the IDREF/IDREFS attributes definition for validity
5983 */
5984 table = (xmlRefTablePtr) doc->refs;
5985 ctxt->doc = doc;
5986 ctxt->valid = 1;
5987 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
5988 return(ctxt->valid);
5989}
5990
5991/**
5992 * xmlValidateDtd:
5993 * @ctxt: the validation context
5994 * @doc: a document instance
5995 * @dtd: a dtd instance
5996 *
5997 * Try to validate the document against the dtd instance
5998 *
5999 * basically it does check all the definitions in the DtD.
6000 *
6001 * returns 1 if valid or 0 otherwise
6002 */
6003
6004int
6005xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6006 int ret;
6007 xmlDtdPtr oldExt;
6008 xmlNodePtr root;
6009
6010 if (dtd == NULL) return(0);
6011 if (doc == NULL) return(0);
6012 oldExt = doc->extSubset;
6013 doc->extSubset = dtd;
6014 ret = xmlValidateRoot(ctxt, doc);
6015 if (ret == 0) {
6016 doc->extSubset = oldExt;
6017 return(ret);
6018 }
6019 if (doc->ids != NULL) {
6020 xmlFreeIDTable(doc->ids);
6021 doc->ids = NULL;
6022 }
6023 if (doc->refs != NULL) {
6024 xmlFreeRefTable(doc->refs);
6025 doc->refs = NULL;
6026 }
6027 root = xmlDocGetRootElement(doc);
6028 ret = xmlValidateElement(ctxt, doc, root);
6029 ret &= xmlValidateDocumentFinal(ctxt, doc);
6030 doc->extSubset = oldExt;
6031 return(ret);
6032}
6033
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006034static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006035xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6036 const xmlChar *name ATTRIBUTE_UNUSED) {
6037 if (cur == NULL)
6038 return;
6039 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6040 xmlChar *notation = cur->content;
6041
Daniel Veillard878eab02002-02-19 13:46:09 +00006042 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006043 int ret;
6044
6045 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6046 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006047 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006048 }
6049 }
6050 }
6051}
6052
6053static void
Owen Taylor3473f882001-02-23 17:55:21 +00006054xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006055 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006056 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006057 xmlDocPtr doc;
6058 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006059
Owen Taylor3473f882001-02-23 17:55:21 +00006060 if (cur == NULL)
6061 return;
6062 switch (cur->atype) {
6063 case XML_ATTRIBUTE_CDATA:
6064 case XML_ATTRIBUTE_ID:
6065 case XML_ATTRIBUTE_IDREF :
6066 case XML_ATTRIBUTE_IDREFS:
6067 case XML_ATTRIBUTE_NMTOKEN:
6068 case XML_ATTRIBUTE_NMTOKENS:
6069 case XML_ATTRIBUTE_ENUMERATION:
6070 break;
6071 case XML_ATTRIBUTE_ENTITY:
6072 case XML_ATTRIBUTE_ENTITIES:
6073 case XML_ATTRIBUTE_NOTATION:
6074 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006075
6076 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6077 cur->atype, cur->defaultValue);
6078 if ((ret == 0) && (ctxt->valid == 1))
6079 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006080 }
6081 if (cur->tree != NULL) {
6082 xmlEnumerationPtr tree = cur->tree;
6083 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006084 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006085 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006086 if ((ret == 0) && (ctxt->valid == 1))
6087 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006088 tree = tree->next;
6089 }
6090 }
6091 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006092 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6093 doc = cur->doc;
6094 if ((doc == NULL) || (cur->elem == NULL)) {
6095 VERROR(ctxt->userData,
6096 "xmlValidateAttributeCallback(%s): internal error\n",
6097 cur->name);
6098 return;
6099 }
6100 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6101 if (elem == NULL)
6102 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
6103 if (elem == NULL) {
6104 VERROR(ctxt->userData,
6105 "attribute %s: could not find decl for element %s\n",
6106 cur->name, cur->elem);
6107 return;
6108 }
6109 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
6110 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006111 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillard878eab02002-02-19 13:46:09 +00006112 cur->name, cur->elem);
6113 ctxt->valid = 0;
6114 }
6115 }
Owen Taylor3473f882001-02-23 17:55:21 +00006116}
6117
6118/**
6119 * xmlValidateDtdFinal:
6120 * @ctxt: the validation context
6121 * @doc: a document instance
6122 *
6123 * Does the final step for the dtds validation once all the
6124 * subsets have been parsed
6125 *
6126 * basically it does the following checks described by the XML Rec
6127 * - check that ENTITY and ENTITIES type attributes default or
6128 * possible values matches one of the defined entities.
6129 * - check that NOTATION type attributes default or
6130 * possible values matches one of the defined notations.
6131 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006132 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006133 */
6134
6135int
6136xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006137 xmlDtdPtr dtd;
6138 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006139 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006140
6141 if (doc == NULL) return(0);
6142 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6143 return(0);
6144 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006145 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006146 dtd = doc->intSubset;
6147 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6148 table = (xmlAttributeTablePtr) dtd->attributes;
6149 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006150 }
6151 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006152 entities = (xmlEntitiesTablePtr) dtd->entities;
6153 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6154 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006155 }
6156 dtd = doc->extSubset;
6157 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6158 table = (xmlAttributeTablePtr) dtd->attributes;
6159 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006160 }
6161 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006162 entities = (xmlEntitiesTablePtr) dtd->entities;
6163 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6164 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006165 }
6166 return(ctxt->valid);
6167}
6168
6169/**
6170 * xmlValidateDocument:
6171 * @ctxt: the validation context
6172 * @doc: a document instance
6173 *
6174 * Try to validate the document instance
6175 *
6176 * basically it does the all the checks described by the XML Rec
6177 * i.e. validates the internal and external subset (if present)
6178 * and validate the document tree.
6179 *
6180 * returns 1 if valid or 0 otherwise
6181 */
6182
6183int
6184xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6185 int ret;
6186 xmlNodePtr root;
6187
Daniel Veillard2fd85422002-10-16 14:32:41 +00006188 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
6189 VERROR(ctxt->userData, "no DTD found!\n" );
Owen Taylor3473f882001-02-23 17:55:21 +00006190 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006191 }
Owen Taylor3473f882001-02-23 17:55:21 +00006192 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6193 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6194 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6195 doc->intSubset->SystemID);
6196 if (doc->extSubset == NULL) {
6197 if (doc->intSubset->SystemID != NULL) {
6198 VERROR(ctxt->userData,
6199 "Could not load the external subset \"%s\"\n",
6200 doc->intSubset->SystemID);
6201 } else {
6202 VERROR(ctxt->userData,
6203 "Could not load the external subset \"%s\"\n",
6204 doc->intSubset->ExternalID);
6205 }
6206 return(0);
6207 }
6208 }
6209
6210 if (doc->ids != NULL) {
6211 xmlFreeIDTable(doc->ids);
6212 doc->ids = NULL;
6213 }
6214 if (doc->refs != NULL) {
6215 xmlFreeRefTable(doc->refs);
6216 doc->refs = NULL;
6217 }
6218 ret = xmlValidateDtdFinal(ctxt, doc);
6219 if (!xmlValidateRoot(ctxt, doc)) return(0);
6220
6221 root = xmlDocGetRootElement(doc);
6222 ret &= xmlValidateElement(ctxt, doc, root);
6223 ret &= xmlValidateDocumentFinal(ctxt, doc);
6224 return(ret);
6225}
6226
6227
6228/************************************************************************
6229 * *
6230 * Routines for dynamic validation editing *
6231 * *
6232 ************************************************************************/
6233
6234/**
6235 * xmlValidGetPotentialChildren:
6236 * @ctree: an element content tree
6237 * @list: an array to store the list of child names
6238 * @len: a pointer to the number of element in the list
6239 * @max: the size of the array
6240 *
6241 * Build/extend a list of potential children allowed by the content tree
6242 *
6243 * returns the number of element in the list, or -1 in case of error.
6244 */
6245
6246int
6247xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6248 int *len, int max) {
6249 int i;
6250
6251 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6252 return(-1);
6253 if (*len >= max) return(*len);
6254
6255 switch (ctree->type) {
6256 case XML_ELEMENT_CONTENT_PCDATA:
6257 for (i = 0; i < *len;i++)
6258 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6259 list[(*len)++] = BAD_CAST "#PCDATA";
6260 break;
6261 case XML_ELEMENT_CONTENT_ELEMENT:
6262 for (i = 0; i < *len;i++)
6263 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6264 list[(*len)++] = ctree->name;
6265 break;
6266 case XML_ELEMENT_CONTENT_SEQ:
6267 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6268 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6269 break;
6270 case XML_ELEMENT_CONTENT_OR:
6271 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6272 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6273 break;
6274 }
6275
6276 return(*len);
6277}
6278
6279/**
6280 * xmlValidGetValidElements:
6281 * @prev: an element to insert after
6282 * @next: an element to insert next
6283 * @list: an array to store the list of child names
6284 * @max: the size of the array
6285 *
6286 * This function returns the list of authorized children to insert
6287 * within an existing tree while respecting the validity constraints
6288 * forced by the Dtd. The insertion point is defined using @prev and
6289 * @next in the following ways:
6290 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6291 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6292 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6293 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6294 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6295 *
6296 * pointers to the element names are inserted at the beginning of the array
6297 * and do not need to be freed.
6298 *
6299 * returns the number of element in the list, or -1 in case of error. If
6300 * the function returns the value @max the caller is invited to grow the
6301 * receiving array and retry.
6302 */
6303
6304int
6305xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
6306 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006307 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006308 int nb_valid_elements = 0;
6309 const xmlChar *elements[256];
6310 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006311 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006312
6313 xmlNode *ref_node;
6314 xmlNode *parent;
6315 xmlNode *test_node;
6316
6317 xmlNode *prev_next;
6318 xmlNode *next_prev;
6319 xmlNode *parent_childs;
6320 xmlNode *parent_last;
6321
6322 xmlElement *element_desc;
6323
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00006324 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006325
Owen Taylor3473f882001-02-23 17:55:21 +00006326 if (prev == NULL && next == NULL)
6327 return(-1);
6328
6329 if (list == NULL) return(-1);
6330 if (max <= 0) return(-1);
6331
6332 nb_valid_elements = 0;
6333 ref_node = prev ? prev : next;
6334 parent = ref_node->parent;
6335
6336 /*
6337 * Retrieves the parent element declaration
6338 */
6339 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6340 parent->name);
6341 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6342 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6343 parent->name);
6344 if (element_desc == NULL) return(-1);
6345
6346 /*
6347 * Do a backup of the current tree structure
6348 */
6349 prev_next = prev ? prev->next : NULL;
6350 next_prev = next ? next->prev : NULL;
6351 parent_childs = parent->children;
6352 parent_last = parent->last;
6353
6354 /*
6355 * Creates a dummy node and insert it into the tree
6356 */
6357 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
6358 test_node->doc = ref_node->doc;
6359 test_node->parent = parent;
6360 test_node->prev = prev;
6361 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006362 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006363
6364 if (prev) prev->next = test_node;
6365 else parent->children = test_node;
6366
6367 if (next) next->prev = test_node;
6368 else parent->last = test_node;
6369
6370 /*
6371 * Insert each potential child node and check if the parent is
6372 * still valid
6373 */
6374 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6375 elements, &nb_elements, 256);
6376
6377 for (i = 0;i < nb_elements;i++) {
6378 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006379 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006380 int j;
6381
6382 for (j = 0; j < nb_valid_elements;j++)
6383 if (xmlStrEqual(elements[i], list[j])) break;
6384 list[nb_valid_elements++] = elements[i];
6385 if (nb_valid_elements >= max) break;
6386 }
6387 }
6388
6389 /*
6390 * Restore the tree structure
6391 */
6392 if (prev) prev->next = prev_next;
6393 if (next) next->prev = next_prev;
6394 parent->children = parent_childs;
6395 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006396
6397 /*
6398 * Free up the dummy node
6399 */
6400 test_node->name = name;
6401 xmlFreeNode(test_node);
6402
Owen Taylor3473f882001-02-23 17:55:21 +00006403 return(nb_valid_elements);
6404}