blob: 470f912f683f8de4550ce137d79012e8f47e5f20 [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) {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000627
628 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000629 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000630 if (elem->type != XML_ELEMENT_DECL)
631 return(0);
632 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
633 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000634 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000635 if (elem->contModel != NULL) {
636 if (!xmlRegexpIsDeterminist(elem->contModel)) {
637 ctxt->valid = 0;
638 return(0);
639 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000640 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000641 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000642
643 ctxt->am = xmlNewAutomata();
644 if (ctxt->am == NULL) {
645 VERROR(ctxt->userData, "Cannot create automata for element %s\n",
646 elem->name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000647 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000648 }
William M. Brack78637da2003-07-31 14:47:38 +0000649 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000650 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
651 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000652 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000653 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000654 char expr[5000];
655 expr[0] = 0;
656 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
657 VERROR(ctxt->userData, "Content model of %s is not determinist: %s\n",
658 elem->name, expr);
659#ifdef DEBUG_REGEXP_ALGO
660 xmlRegexpPrint(stderr, elem->contModel);
661#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000662 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000663 ctxt->state = NULL;
664 xmlFreeAutomata(ctxt->am);
665 ctxt->am = NULL;
666 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000667 }
668 ctxt->state = NULL;
669 xmlFreeAutomata(ctxt->am);
670 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000671 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000672}
673
674#endif /* LIBXML_REGEXP_ENABLED */
675
Owen Taylor3473f882001-02-23 17:55:21 +0000676/****************************************************************
677 * *
678 * Util functions for data allocation/deallocation *
679 * *
680 ****************************************************************/
681
682/**
Daniel Veillarda37aab82003-06-09 09:10:36 +0000683 * xmlNewValidCtxt:
684 *
685 * Allocate a validation context structure.
686 *
687 * Returns NULL if not, otherwise the new validation context structure
688 */
689xmlValidCtxtPtr
690xmlNewValidCtxt(void) {
691 xmlValidCtxtPtr ret;
692
693 if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL)
694 return (NULL);
695
696 (void) memset(ret, 0, sizeof (xmlValidCtxt));
697
698 return (ret);
699}
700
701/**
702 * xmlFreeValidCtxt:
703 * @cur: the validation context to free
704 *
705 * Free a validation context structure.
706 */
707void
708xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
709 xmlFree(cur);
710}
711
712/**
Owen Taylor3473f882001-02-23 17:55:21 +0000713 * xmlNewElementContent:
714 * @name: the subelement name or NULL
715 * @type: the type of element content decl
716 *
717 * Allocate an element content structure.
718 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000719 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000720 */
721xmlElementContentPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000722xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
Owen Taylor3473f882001-02-23 17:55:21 +0000723 xmlElementContentPtr ret;
724
725 switch(type) {
726 case XML_ELEMENT_CONTENT_ELEMENT:
727 if (name == NULL) {
728 xmlGenericError(xmlGenericErrorContext,
729 "xmlNewElementContent : name == NULL !\n");
730 }
731 break;
732 case XML_ELEMENT_CONTENT_PCDATA:
733 case XML_ELEMENT_CONTENT_SEQ:
734 case XML_ELEMENT_CONTENT_OR:
735 if (name != NULL) {
736 xmlGenericError(xmlGenericErrorContext,
737 "xmlNewElementContent : name != NULL !\n");
738 }
739 break;
740 default:
741 xmlGenericError(xmlGenericErrorContext,
742 "xmlNewElementContent: unknown type %d\n", type);
743 return(NULL);
744 }
745 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
746 if (ret == NULL) {
747 xmlGenericError(xmlGenericErrorContext,
748 "xmlNewElementContent : out of memory!\n");
749 return(NULL);
750 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000751 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000752 ret->type = type;
753 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000754 if (name != NULL) {
755 xmlChar *prefix = NULL;
756 ret->name = xmlSplitQName2(name, &prefix);
757 if (ret->name == NULL)
758 ret->name = xmlStrdup(name);
759 ret->prefix = prefix;
760 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000761 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000762 ret->prefix = NULL;
763 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000764 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000765 return(ret);
766}
767
768/**
769 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000770 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +0000771 *
772 * Build a copy of an element content description.
773 *
774 * Returns the new xmlElementContentPtr or NULL in case of error.
775 */
776xmlElementContentPtr
777xmlCopyElementContent(xmlElementContentPtr cur) {
778 xmlElementContentPtr ret;
779
780 if (cur == NULL) return(NULL);
781 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
782 if (ret == NULL) {
783 xmlGenericError(xmlGenericErrorContext,
784 "xmlCopyElementContent : out of memory\n");
785 return(NULL);
786 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000787 if (cur->prefix != NULL)
788 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000789 ret->ocur = cur->ocur;
790 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000791 if (ret->c1 != NULL)
792 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000793 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000794 if (ret->c2 != NULL)
795 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000796 return(ret);
797}
798
799/**
800 * xmlFreeElementContent:
801 * @cur: the element content tree to free
802 *
803 * Free an element content structure. This is a recursive call !
804 */
805void
806xmlFreeElementContent(xmlElementContentPtr cur) {
807 if (cur == NULL) return;
808 switch (cur->type) {
809 case XML_ELEMENT_CONTENT_PCDATA:
810 case XML_ELEMENT_CONTENT_ELEMENT:
811 case XML_ELEMENT_CONTENT_SEQ:
812 case XML_ELEMENT_CONTENT_OR:
813 break;
814 default:
815 xmlGenericError(xmlGenericErrorContext,
816 "xmlFreeElementContent : type %d\n", cur->type);
817 return;
818 }
819 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
820 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
821 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000822 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000823 xmlFree(cur);
824}
825
826/**
827 * xmlDumpElementContent:
828 * @buf: An XML buffer
829 * @content: An element table
830 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
831 *
832 * This will dump the content of the element table as an XML DTD definition
833 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000834static void
Owen Taylor3473f882001-02-23 17:55:21 +0000835xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
836 if (content == NULL) return;
837
838 if (glob) xmlBufferWriteChar(buf, "(");
839 switch (content->type) {
840 case XML_ELEMENT_CONTENT_PCDATA:
841 xmlBufferWriteChar(buf, "#PCDATA");
842 break;
843 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000844 if (content->prefix != NULL) {
845 xmlBufferWriteCHAR(buf, content->prefix);
846 xmlBufferWriteChar(buf, ":");
847 }
Owen Taylor3473f882001-02-23 17:55:21 +0000848 xmlBufferWriteCHAR(buf, content->name);
849 break;
850 case XML_ELEMENT_CONTENT_SEQ:
851 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
852 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
853 xmlDumpElementContent(buf, content->c1, 1);
854 else
855 xmlDumpElementContent(buf, content->c1, 0);
856 xmlBufferWriteChar(buf, " , ");
857 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
858 xmlDumpElementContent(buf, content->c2, 1);
859 else
860 xmlDumpElementContent(buf, content->c2, 0);
861 break;
862 case XML_ELEMENT_CONTENT_OR:
863 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
864 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
865 xmlDumpElementContent(buf, content->c1, 1);
866 else
867 xmlDumpElementContent(buf, content->c1, 0);
868 xmlBufferWriteChar(buf, " | ");
869 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
870 xmlDumpElementContent(buf, content->c2, 1);
871 else
872 xmlDumpElementContent(buf, content->c2, 0);
873 break;
874 default:
875 xmlGenericError(xmlGenericErrorContext,
876 "xmlDumpElementContent: unknown type %d\n",
877 content->type);
878 }
879 if (glob)
880 xmlBufferWriteChar(buf, ")");
881 switch (content->ocur) {
882 case XML_ELEMENT_CONTENT_ONCE:
883 break;
884 case XML_ELEMENT_CONTENT_OPT:
885 xmlBufferWriteChar(buf, "?");
886 break;
887 case XML_ELEMENT_CONTENT_MULT:
888 xmlBufferWriteChar(buf, "*");
889 break;
890 case XML_ELEMENT_CONTENT_PLUS:
891 xmlBufferWriteChar(buf, "+");
892 break;
893 }
894}
895
896/**
897 * xmlSprintfElementContent:
898 * @buf: an output buffer
899 * @content: An element table
900 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
901 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000902 * Deprecated, unsafe, use xmlSnprintfElementContent
903 */
904void
905xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
906 xmlElementContentPtr content ATTRIBUTE_UNUSED,
907 int glob ATTRIBUTE_UNUSED) {
908}
909
910/**
911 * xmlSnprintfElementContent:
912 * @buf: an output buffer
913 * @size: the buffer size
914 * @content: An element table
915 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
916 *
Owen Taylor3473f882001-02-23 17:55:21 +0000917 * This will dump the content of the element content definition
918 * Intended just for the debug routine
919 */
920void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000921xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
922 int len;
923
Owen Taylor3473f882001-02-23 17:55:21 +0000924 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000925 len = strlen(buf);
926 if (size - len < 50) {
927 if ((size - len > 4) && (buf[len - 1] != '.'))
928 strcat(buf, " ...");
929 return;
930 }
Owen Taylor3473f882001-02-23 17:55:21 +0000931 if (glob) strcat(buf, "(");
932 switch (content->type) {
933 case XML_ELEMENT_CONTENT_PCDATA:
934 strcat(buf, "#PCDATA");
935 break;
936 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000937 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000938 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000939 strcat(buf, " ...");
940 return;
941 }
942 strcat(buf, (char *) content->prefix);
943 strcat(buf, ":");
944 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000945 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +0000946 strcat(buf, " ...");
947 return;
948 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000949 if (content->name != NULL)
950 strcat(buf, (char *) content->name);
Owen Taylor3473f882001-02-23 17:55:21 +0000951 break;
952 case XML_ELEMENT_CONTENT_SEQ:
953 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
954 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000955 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000956 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000957 xmlSnprintfElementContent(buf, size, content->c1, 0);
958 len = strlen(buf);
959 if (size - len < 50) {
960 if ((size - len > 4) && (buf[len - 1] != '.'))
961 strcat(buf, " ...");
962 return;
963 }
Owen Taylor3473f882001-02-23 17:55:21 +0000964 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000965 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
966 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
967 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000968 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000969 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000970 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000971 break;
972 case XML_ELEMENT_CONTENT_OR:
973 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
974 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000975 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000976 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000977 xmlSnprintfElementContent(buf, size, content->c1, 0);
978 len = strlen(buf);
979 if (size - len < 50) {
980 if ((size - len > 4) && (buf[len - 1] != '.'))
981 strcat(buf, " ...");
982 return;
983 }
Owen Taylor3473f882001-02-23 17:55:21 +0000984 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000985 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
986 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
987 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000988 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000989 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000990 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000991 break;
992 }
993 if (glob)
994 strcat(buf, ")");
995 switch (content->ocur) {
996 case XML_ELEMENT_CONTENT_ONCE:
997 break;
998 case XML_ELEMENT_CONTENT_OPT:
999 strcat(buf, "?");
1000 break;
1001 case XML_ELEMENT_CONTENT_MULT:
1002 strcat(buf, "*");
1003 break;
1004 case XML_ELEMENT_CONTENT_PLUS:
1005 strcat(buf, "+");
1006 break;
1007 }
1008}
1009
1010/****************************************************************
1011 * *
1012 * Registration of DTD declarations *
1013 * *
1014 ****************************************************************/
1015
1016/**
1017 * xmlCreateElementTable:
1018 *
1019 * create and initialize an empty element hash table.
1020 *
1021 * Returns the xmlElementTablePtr just created or NULL in case of error.
1022 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001023static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001024xmlCreateElementTable(void) {
1025 return(xmlHashCreate(0));
1026}
1027
1028/**
1029 * xmlFreeElement:
1030 * @elem: An element
1031 *
1032 * Deallocate the memory used by an element definition
1033 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001034static void
Owen Taylor3473f882001-02-23 17:55:21 +00001035xmlFreeElement(xmlElementPtr elem) {
1036 if (elem == NULL) return;
1037 xmlUnlinkNode((xmlNodePtr) elem);
1038 xmlFreeElementContent(elem->content);
1039 if (elem->name != NULL)
1040 xmlFree((xmlChar *) elem->name);
1041 if (elem->prefix != NULL)
1042 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001043#ifdef LIBXML_REGEXP_ENABLED
1044 if (elem->contModel != NULL)
1045 xmlRegFreeRegexp(elem->contModel);
1046#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001047 xmlFree(elem);
1048}
1049
1050
1051/**
1052 * xmlAddElementDecl:
1053 * @ctxt: the validation context
1054 * @dtd: pointer to the DTD
1055 * @name: the entity name
1056 * @type: the element type
1057 * @content: the element content tree or NULL
1058 *
1059 * Register a new element declaration
1060 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001061 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001062 */
1063xmlElementPtr
1064xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
1065 xmlElementTypeVal type,
1066 xmlElementContentPtr content) {
1067 xmlElementPtr ret;
1068 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001069 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001070 xmlChar *ns, *uqname;
1071
1072 if (dtd == NULL) {
1073 xmlGenericError(xmlGenericErrorContext,
1074 "xmlAddElementDecl: dtd == NULL\n");
1075 return(NULL);
1076 }
1077 if (name == NULL) {
1078 xmlGenericError(xmlGenericErrorContext,
1079 "xmlAddElementDecl: name == NULL\n");
1080 return(NULL);
1081 }
1082 switch (type) {
1083 case XML_ELEMENT_TYPE_EMPTY:
1084 if (content != NULL) {
1085 xmlGenericError(xmlGenericErrorContext,
1086 "xmlAddElementDecl: content != NULL for EMPTY\n");
1087 return(NULL);
1088 }
1089 break;
1090 case XML_ELEMENT_TYPE_ANY:
1091 if (content != NULL) {
1092 xmlGenericError(xmlGenericErrorContext,
1093 "xmlAddElementDecl: content != NULL for ANY\n");
1094 return(NULL);
1095 }
1096 break;
1097 case XML_ELEMENT_TYPE_MIXED:
1098 if (content == NULL) {
1099 xmlGenericError(xmlGenericErrorContext,
1100 "xmlAddElementDecl: content == NULL for MIXED\n");
1101 return(NULL);
1102 }
1103 break;
1104 case XML_ELEMENT_TYPE_ELEMENT:
1105 if (content == NULL) {
1106 xmlGenericError(xmlGenericErrorContext,
1107 "xmlAddElementDecl: content == NULL for ELEMENT\n");
1108 return(NULL);
1109 }
1110 break;
1111 default:
1112 xmlGenericError(xmlGenericErrorContext,
1113 "xmlAddElementDecl: unknown type %d\n", type);
1114 return(NULL);
1115 }
1116
1117 /*
1118 * check if name is a QName
1119 */
1120 uqname = xmlSplitQName2(name, &ns);
1121 if (uqname != NULL)
1122 name = uqname;
1123
1124 /*
1125 * Create the Element table if needed.
1126 */
1127 table = (xmlElementTablePtr) dtd->elements;
1128 if (table == NULL) {
1129 table = xmlCreateElementTable();
1130 dtd->elements = (void *) table;
1131 }
1132 if (table == NULL) {
1133 xmlGenericError(xmlGenericErrorContext,
1134 "xmlAddElementDecl: Table creation failed!\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001135 if (uqname != NULL)
1136 xmlFree(uqname);
1137 if (ns != NULL)
1138 xmlFree(ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001139 return(NULL);
1140 }
1141
Daniel Veillarda10efa82001-04-18 13:09:01 +00001142 /*
1143 * lookup old attributes inserted on an undefined element in the
1144 * internal subset.
1145 */
1146 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1147 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1148 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1149 oldAttributes = ret->attributes;
1150 ret->attributes = NULL;
1151 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1152 xmlFreeElement(ret);
1153 }
Owen Taylor3473f882001-02-23 17:55:21 +00001154 }
Owen Taylor3473f882001-02-23 17:55:21 +00001155
1156 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001157 * The element may already be present if one of its attribute
1158 * was registered first
1159 */
1160 ret = xmlHashLookup2(table, name, ns);
1161 if (ret != NULL) {
1162 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
1163 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001164 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001165 */
1166 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
1167 if (uqname != NULL)
1168 xmlFree(uqname);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001169 if (ns != NULL)
1170 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001171 return(NULL);
1172 }
1173 } else {
1174 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1175 if (ret == NULL) {
1176 xmlGenericError(xmlGenericErrorContext,
1177 "xmlAddElementDecl: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001178 if (uqname != NULL)
1179 xmlFree(uqname);
1180 if (ns != NULL)
1181 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001182 return(NULL);
1183 }
1184 memset(ret, 0, sizeof(xmlElement));
1185 ret->type = XML_ELEMENT_DECL;
1186
1187 /*
1188 * fill the structure.
1189 */
1190 ret->name = xmlStrdup(name);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001191 if (ret->name == NULL) {
1192 xmlGenericError(xmlGenericErrorContext,
1193 "xmlAddElementDecl: out of memory\n");
1194 if (uqname != NULL)
1195 xmlFree(uqname);
1196 if (ns != NULL)
1197 xmlFree(ns);
1198 xmlFree(ret);
1199 return(NULL);
1200 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001201 ret->prefix = ns;
1202
1203 /*
1204 * Validity Check:
1205 * Insertion must not fail
1206 */
1207 if (xmlHashAddEntry2(table, name, ns, ret)) {
1208 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001209 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001210 */
1211 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
1212 xmlFreeElement(ret);
1213 if (uqname != NULL)
1214 xmlFree(uqname);
1215 return(NULL);
1216 }
William M. Brack4e52f2f2003-09-14 18:07:39 +00001217 /*
1218 * For new element, may have attributes from earlier
1219 * definition in internal subset
1220 */
1221 ret->attributes = oldAttributes;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001222 }
1223
1224 /*
1225 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001226 */
1227 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +00001228 ret->content = xmlCopyElementContent(content);
Owen Taylor3473f882001-02-23 17:55:21 +00001229
1230 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001231 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001232 */
1233 ret->parent = dtd;
1234 ret->doc = dtd->doc;
1235 if (dtd->last == NULL) {
1236 dtd->children = dtd->last = (xmlNodePtr) ret;
1237 } else {
1238 dtd->last->next = (xmlNodePtr) ret;
1239 ret->prev = dtd->last;
1240 dtd->last = (xmlNodePtr) ret;
1241 }
1242 if (uqname != NULL)
1243 xmlFree(uqname);
1244 return(ret);
1245}
1246
1247/**
1248 * xmlFreeElementTable:
1249 * @table: An element table
1250 *
1251 * Deallocate the memory used by an element hash table.
1252 */
1253void
1254xmlFreeElementTable(xmlElementTablePtr table) {
1255 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1256}
1257
1258/**
1259 * xmlCopyElement:
1260 * @elem: An element
1261 *
1262 * Build a copy of an element.
1263 *
1264 * Returns the new xmlElementPtr or NULL in case of error.
1265 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001266static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001267xmlCopyElement(xmlElementPtr elem) {
1268 xmlElementPtr cur;
1269
1270 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1271 if (cur == NULL) {
1272 xmlGenericError(xmlGenericErrorContext,
1273 "xmlCopyElement: out of memory !\n");
1274 return(NULL);
1275 }
1276 memset(cur, 0, sizeof(xmlElement));
1277 cur->type = XML_ELEMENT_DECL;
1278 cur->etype = elem->etype;
1279 if (elem->name != NULL)
1280 cur->name = xmlStrdup(elem->name);
1281 else
1282 cur->name = NULL;
1283 if (elem->prefix != NULL)
1284 cur->prefix = xmlStrdup(elem->prefix);
1285 else
1286 cur->prefix = NULL;
1287 cur->content = xmlCopyElementContent(elem->content);
1288 /* TODO : rebuild the attribute list on the copy */
1289 cur->attributes = NULL;
1290 return(cur);
1291}
1292
1293/**
1294 * xmlCopyElementTable:
1295 * @table: An element table
1296 *
1297 * Build a copy of an element table.
1298 *
1299 * Returns the new xmlElementTablePtr or NULL in case of error.
1300 */
1301xmlElementTablePtr
1302xmlCopyElementTable(xmlElementTablePtr table) {
1303 return((xmlElementTablePtr) xmlHashCopy(table,
1304 (xmlHashCopier) xmlCopyElement));
1305}
1306
1307/**
1308 * xmlDumpElementDecl:
1309 * @buf: the XML buffer output
1310 * @elem: An element table
1311 *
1312 * This will dump the content of the element declaration as an XML
1313 * DTD definition
1314 */
1315void
1316xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1317 switch (elem->etype) {
1318 case XML_ELEMENT_TYPE_EMPTY:
1319 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001320 if (elem->prefix != NULL) {
1321 xmlBufferWriteCHAR(buf, elem->prefix);
1322 xmlBufferWriteChar(buf, ":");
1323 }
Owen Taylor3473f882001-02-23 17:55:21 +00001324 xmlBufferWriteCHAR(buf, elem->name);
1325 xmlBufferWriteChar(buf, " EMPTY>\n");
1326 break;
1327 case XML_ELEMENT_TYPE_ANY:
1328 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001329 if (elem->prefix != NULL) {
1330 xmlBufferWriteCHAR(buf, elem->prefix);
1331 xmlBufferWriteChar(buf, ":");
1332 }
Owen Taylor3473f882001-02-23 17:55:21 +00001333 xmlBufferWriteCHAR(buf, elem->name);
1334 xmlBufferWriteChar(buf, " ANY>\n");
1335 break;
1336 case XML_ELEMENT_TYPE_MIXED:
1337 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001338 if (elem->prefix != NULL) {
1339 xmlBufferWriteCHAR(buf, elem->prefix);
1340 xmlBufferWriteChar(buf, ":");
1341 }
Owen Taylor3473f882001-02-23 17:55:21 +00001342 xmlBufferWriteCHAR(buf, elem->name);
1343 xmlBufferWriteChar(buf, " ");
1344 xmlDumpElementContent(buf, elem->content, 1);
1345 xmlBufferWriteChar(buf, ">\n");
1346 break;
1347 case XML_ELEMENT_TYPE_ELEMENT:
1348 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001349 if (elem->prefix != NULL) {
1350 xmlBufferWriteCHAR(buf, elem->prefix);
1351 xmlBufferWriteChar(buf, ":");
1352 }
Owen Taylor3473f882001-02-23 17:55:21 +00001353 xmlBufferWriteCHAR(buf, elem->name);
1354 xmlBufferWriteChar(buf, " ");
1355 xmlDumpElementContent(buf, elem->content, 1);
1356 xmlBufferWriteChar(buf, ">\n");
1357 break;
1358 default:
1359 xmlGenericError(xmlGenericErrorContext,
1360 "xmlDumpElementDecl: internal: unknown type %d\n",
1361 elem->etype);
1362 }
1363}
1364
1365/**
1366 * xmlDumpElementTable:
1367 * @buf: the XML buffer output
1368 * @table: An element table
1369 *
1370 * This will dump the content of the element table as an XML DTD definition
1371 */
1372void
1373xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1374 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1375}
1376
1377/**
1378 * xmlCreateEnumeration:
1379 * @name: the enumeration name or NULL
1380 *
1381 * create and initialize an enumeration attribute node.
1382 *
1383 * Returns the xmlEnumerationPtr just created or NULL in case
1384 * of error.
1385 */
1386xmlEnumerationPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001387xmlCreateEnumeration(const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00001388 xmlEnumerationPtr ret;
1389
1390 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1391 if (ret == NULL) {
1392 xmlGenericError(xmlGenericErrorContext,
1393 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1394 (long)sizeof(xmlEnumeration));
1395 return(NULL);
1396 }
1397 memset(ret, 0, sizeof(xmlEnumeration));
1398
1399 if (name != NULL)
1400 ret->name = xmlStrdup(name);
1401 return(ret);
1402}
1403
1404/**
1405 * xmlFreeEnumeration:
1406 * @cur: the tree to free.
1407 *
1408 * free an enumeration attribute node (recursive).
1409 */
1410void
1411xmlFreeEnumeration(xmlEnumerationPtr cur) {
1412 if (cur == NULL) return;
1413
1414 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1415
1416 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001417 xmlFree(cur);
1418}
1419
1420/**
1421 * xmlCopyEnumeration:
1422 * @cur: the tree to copy.
1423 *
1424 * Copy an enumeration attribute node (recursive).
1425 *
1426 * Returns the xmlEnumerationPtr just created or NULL in case
1427 * of error.
1428 */
1429xmlEnumerationPtr
1430xmlCopyEnumeration(xmlEnumerationPtr cur) {
1431 xmlEnumerationPtr ret;
1432
1433 if (cur == NULL) return(NULL);
1434 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1435
1436 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1437 else ret->next = NULL;
1438
1439 return(ret);
1440}
1441
1442/**
1443 * xmlDumpEnumeration:
1444 * @buf: the XML buffer output
1445 * @enum: An enumeration
1446 *
1447 * This will dump the content of the enumeration
1448 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001449static void
Owen Taylor3473f882001-02-23 17:55:21 +00001450xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1451 if (cur == NULL) return;
1452
1453 xmlBufferWriteCHAR(buf, cur->name);
1454 if (cur->next == NULL)
1455 xmlBufferWriteChar(buf, ")");
1456 else {
1457 xmlBufferWriteChar(buf, " | ");
1458 xmlDumpEnumeration(buf, cur->next);
1459 }
1460}
1461
1462/**
1463 * xmlCreateAttributeTable:
1464 *
1465 * create and initialize an empty attribute hash table.
1466 *
1467 * Returns the xmlAttributeTablePtr just created or NULL in case
1468 * of error.
1469 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001470static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001471xmlCreateAttributeTable(void) {
1472 return(xmlHashCreate(0));
1473}
1474
1475/**
1476 * xmlScanAttributeDeclCallback:
1477 * @attr: the attribute decl
1478 * @list: the list to update
1479 *
1480 * Callback called by xmlScanAttributeDecl when a new attribute
1481 * has to be entered in the list.
1482 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001483static void
Owen Taylor3473f882001-02-23 17:55:21 +00001484xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001485 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001486 attr->nexth = *list;
1487 *list = attr;
1488}
1489
1490/**
1491 * xmlScanAttributeDecl:
1492 * @dtd: pointer to the DTD
1493 * @elem: the element name
1494 *
1495 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001496 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001497 *
1498 * Returns the pointer to the first attribute decl in the chain,
1499 * possibly NULL.
1500 */
1501xmlAttributePtr
1502xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1503 xmlAttributePtr ret = NULL;
1504 xmlAttributeTablePtr table;
1505
1506 if (dtd == NULL) {
1507 xmlGenericError(xmlGenericErrorContext,
1508 "xmlScanAttributeDecl: dtd == NULL\n");
1509 return(NULL);
1510 }
1511 if (elem == NULL) {
1512 xmlGenericError(xmlGenericErrorContext,
1513 "xmlScanAttributeDecl: elem == NULL\n");
1514 return(NULL);
1515 }
1516 table = (xmlAttributeTablePtr) dtd->attributes;
1517 if (table == NULL)
1518 return(NULL);
1519
1520 /* WRONG !!! */
1521 xmlHashScan3(table, NULL, NULL, elem,
1522 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1523 return(ret);
1524}
1525
1526/**
1527 * xmlScanIDAttributeDecl:
1528 * @ctxt: the validation context
1529 * @elem: the element name
1530 *
1531 * Verify that the element don't have too many ID attributes
1532 * declared.
1533 *
1534 * Returns the number of ID attributes found.
1535 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001536static int
Owen Taylor3473f882001-02-23 17:55:21 +00001537xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1538 xmlAttributePtr cur;
1539 int ret = 0;
1540
1541 if (elem == NULL) return(0);
1542 cur = elem->attributes;
1543 while (cur != NULL) {
1544 if (cur->atype == XML_ATTRIBUTE_ID) {
1545 ret ++;
1546 if (ret > 1)
1547 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001548 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001549 elem->name, cur->name);
1550 }
1551 cur = cur->nexth;
1552 }
1553 return(ret);
1554}
1555
1556/**
1557 * xmlFreeAttribute:
1558 * @elem: An attribute
1559 *
1560 * Deallocate the memory used by an attribute definition
1561 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001562static void
Owen Taylor3473f882001-02-23 17:55:21 +00001563xmlFreeAttribute(xmlAttributePtr attr) {
1564 if (attr == NULL) return;
1565 xmlUnlinkNode((xmlNodePtr) attr);
1566 if (attr->tree != NULL)
1567 xmlFreeEnumeration(attr->tree);
1568 if (attr->elem != NULL)
1569 xmlFree((xmlChar *) attr->elem);
1570 if (attr->name != NULL)
1571 xmlFree((xmlChar *) attr->name);
1572 if (attr->defaultValue != NULL)
1573 xmlFree((xmlChar *) attr->defaultValue);
1574 if (attr->prefix != NULL)
1575 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001576 xmlFree(attr);
1577}
1578
1579
1580/**
1581 * xmlAddAttributeDecl:
1582 * @ctxt: the validation context
1583 * @dtd: pointer to the DTD
1584 * @elem: the element name
1585 * @name: the attribute name
1586 * @ns: the attribute namespace prefix
1587 * @type: the attribute type
1588 * @def: the attribute default type
1589 * @defaultValue: the attribute default value
1590 * @tree: if it's an enumeration, the associated list
1591 *
1592 * Register a new attribute declaration
1593 * Note that @tree becomes the ownership of the DTD
1594 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001595 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001596 */
1597xmlAttributePtr
1598xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1599 const xmlChar *name, const xmlChar *ns,
1600 xmlAttributeType type, xmlAttributeDefault def,
1601 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1602 xmlAttributePtr ret;
1603 xmlAttributeTablePtr table;
1604 xmlElementPtr elemDef;
1605
1606 if (dtd == NULL) {
1607 xmlGenericError(xmlGenericErrorContext,
1608 "xmlAddAttributeDecl: dtd == NULL\n");
1609 xmlFreeEnumeration(tree);
1610 return(NULL);
1611 }
1612 if (name == NULL) {
1613 xmlGenericError(xmlGenericErrorContext,
1614 "xmlAddAttributeDecl: name == NULL\n");
1615 xmlFreeEnumeration(tree);
1616 return(NULL);
1617 }
1618 if (elem == NULL) {
1619 xmlGenericError(xmlGenericErrorContext,
1620 "xmlAddAttributeDecl: elem == NULL\n");
1621 xmlFreeEnumeration(tree);
1622 return(NULL);
1623 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001624
Owen Taylor3473f882001-02-23 17:55:21 +00001625 /*
1626 * Check the type and possibly the default value.
1627 */
1628 switch (type) {
1629 case XML_ATTRIBUTE_CDATA:
1630 break;
1631 case XML_ATTRIBUTE_ID:
1632 break;
1633 case XML_ATTRIBUTE_IDREF:
1634 break;
1635 case XML_ATTRIBUTE_IDREFS:
1636 break;
1637 case XML_ATTRIBUTE_ENTITY:
1638 break;
1639 case XML_ATTRIBUTE_ENTITIES:
1640 break;
1641 case XML_ATTRIBUTE_NMTOKEN:
1642 break;
1643 case XML_ATTRIBUTE_NMTOKENS:
1644 break;
1645 case XML_ATTRIBUTE_ENUMERATION:
1646 break;
1647 case XML_ATTRIBUTE_NOTATION:
1648 break;
1649 default:
1650 xmlGenericError(xmlGenericErrorContext,
1651 "xmlAddAttributeDecl: unknown type %d\n", type);
1652 xmlFreeEnumeration(tree);
1653 return(NULL);
1654 }
1655 if ((defaultValue != NULL) &&
1656 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillard58e44c92002-08-02 22:19:49 +00001657 VERROR(ctxt->userData, "Attribute %s of %s: invalid default value\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001658 elem, name, defaultValue);
1659 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001660 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001661 }
1662
1663 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001664 * Check first that an attribute defined in the external subset wasn't
1665 * already defined in the internal subset
1666 */
1667 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1668 (dtd->doc->intSubset != NULL) &&
1669 (dtd->doc->intSubset->attributes != NULL)) {
1670 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1671 if (ret != NULL)
1672 return(NULL);
1673 }
1674
1675 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001676 * Create the Attribute table if needed.
1677 */
1678 table = (xmlAttributeTablePtr) dtd->attributes;
1679 if (table == NULL) {
1680 table = xmlCreateAttributeTable();
1681 dtd->attributes = (void *) table;
1682 }
1683 if (table == NULL) {
1684 xmlGenericError(xmlGenericErrorContext,
1685 "xmlAddAttributeDecl: Table creation failed!\n");
1686 return(NULL);
1687 }
1688
1689
1690 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1691 if (ret == NULL) {
1692 xmlGenericError(xmlGenericErrorContext,
1693 "xmlAddAttributeDecl: out of memory\n");
1694 return(NULL);
1695 }
1696 memset(ret, 0, sizeof(xmlAttribute));
1697 ret->type = XML_ATTRIBUTE_DECL;
1698
1699 /*
1700 * fill the structure.
1701 */
1702 ret->atype = type;
1703 ret->name = xmlStrdup(name);
1704 ret->prefix = xmlStrdup(ns);
1705 ret->elem = xmlStrdup(elem);
1706 ret->def = def;
1707 ret->tree = tree;
1708 if (defaultValue != NULL)
1709 ret->defaultValue = xmlStrdup(defaultValue);
1710
1711 /*
1712 * Validity Check:
1713 * Search the DTD for previous declarations of the ATTLIST
1714 */
1715 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1716 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001717 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001718 */
1719 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00001720 "Attribute %s of element %s: already defined\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001721 name, elem);
1722 xmlFreeAttribute(ret);
1723 return(NULL);
1724 }
1725
1726 /*
1727 * Validity Check:
1728 * Multiple ID per element
1729 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001730 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001731 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001732
Owen Taylor3473f882001-02-23 17:55:21 +00001733 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001734 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001735 VERROR(ctxt->userData,
1736 "Element %s has too may ID attributes defined : %s\n",
1737 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001738 ctxt->valid = 0;
1739 }
1740
Daniel Veillard48da9102001-08-07 01:10:10 +00001741 /*
1742 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001743 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001744 */
1745 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1746 ((ret->prefix != NULL &&
1747 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1748 ret->nexth = elemDef->attributes;
1749 elemDef->attributes = ret;
1750 } else {
1751 xmlAttributePtr tmp = elemDef->attributes;
1752
1753 while ((tmp != NULL) &&
1754 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1755 ((ret->prefix != NULL &&
1756 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1757 if (tmp->nexth == NULL)
1758 break;
1759 tmp = tmp->nexth;
1760 }
1761 if (tmp != NULL) {
1762 ret->nexth = tmp->nexth;
1763 tmp->nexth = ret;
1764 } else {
1765 ret->nexth = elemDef->attributes;
1766 elemDef->attributes = ret;
1767 }
1768 }
Owen Taylor3473f882001-02-23 17:55:21 +00001769 }
1770
1771 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001772 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001773 */
1774 ret->parent = dtd;
1775 ret->doc = dtd->doc;
1776 if (dtd->last == NULL) {
1777 dtd->children = dtd->last = (xmlNodePtr) ret;
1778 } else {
1779 dtd->last->next = (xmlNodePtr) ret;
1780 ret->prev = dtd->last;
1781 dtd->last = (xmlNodePtr) ret;
1782 }
1783 return(ret);
1784}
1785
1786/**
1787 * xmlFreeAttributeTable:
1788 * @table: An attribute table
1789 *
1790 * Deallocate the memory used by an entities hash table.
1791 */
1792void
1793xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1794 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1795}
1796
1797/**
1798 * xmlCopyAttribute:
1799 * @attr: An attribute
1800 *
1801 * Build a copy of an attribute.
1802 *
1803 * Returns the new xmlAttributePtr or NULL in case of error.
1804 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001805static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001806xmlCopyAttribute(xmlAttributePtr attr) {
1807 xmlAttributePtr cur;
1808
1809 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1810 if (cur == NULL) {
1811 xmlGenericError(xmlGenericErrorContext,
1812 "xmlCopyAttribute: out of memory !\n");
1813 return(NULL);
1814 }
1815 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001816 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001817 cur->atype = attr->atype;
1818 cur->def = attr->def;
1819 cur->tree = xmlCopyEnumeration(attr->tree);
1820 if (attr->elem != NULL)
1821 cur->elem = xmlStrdup(attr->elem);
1822 if (attr->name != NULL)
1823 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001824 if (attr->prefix != NULL)
1825 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001826 if (attr->defaultValue != NULL)
1827 cur->defaultValue = xmlStrdup(attr->defaultValue);
1828 return(cur);
1829}
1830
1831/**
1832 * xmlCopyAttributeTable:
1833 * @table: An attribute table
1834 *
1835 * Build a copy of an attribute table.
1836 *
1837 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1838 */
1839xmlAttributeTablePtr
1840xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1841 return((xmlAttributeTablePtr) xmlHashCopy(table,
1842 (xmlHashCopier) xmlCopyAttribute));
1843}
1844
1845/**
1846 * xmlDumpAttributeDecl:
1847 * @buf: the XML buffer output
1848 * @attr: An attribute declaration
1849 *
1850 * This will dump the content of the attribute declaration as an XML
1851 * DTD definition
1852 */
1853void
1854xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1855 xmlBufferWriteChar(buf, "<!ATTLIST ");
1856 xmlBufferWriteCHAR(buf, attr->elem);
1857 xmlBufferWriteChar(buf, " ");
1858 if (attr->prefix != NULL) {
1859 xmlBufferWriteCHAR(buf, attr->prefix);
1860 xmlBufferWriteChar(buf, ":");
1861 }
1862 xmlBufferWriteCHAR(buf, attr->name);
1863 switch (attr->atype) {
1864 case XML_ATTRIBUTE_CDATA:
1865 xmlBufferWriteChar(buf, " CDATA");
1866 break;
1867 case XML_ATTRIBUTE_ID:
1868 xmlBufferWriteChar(buf, " ID");
1869 break;
1870 case XML_ATTRIBUTE_IDREF:
1871 xmlBufferWriteChar(buf, " IDREF");
1872 break;
1873 case XML_ATTRIBUTE_IDREFS:
1874 xmlBufferWriteChar(buf, " IDREFS");
1875 break;
1876 case XML_ATTRIBUTE_ENTITY:
1877 xmlBufferWriteChar(buf, " ENTITY");
1878 break;
1879 case XML_ATTRIBUTE_ENTITIES:
1880 xmlBufferWriteChar(buf, " ENTITIES");
1881 break;
1882 case XML_ATTRIBUTE_NMTOKEN:
1883 xmlBufferWriteChar(buf, " NMTOKEN");
1884 break;
1885 case XML_ATTRIBUTE_NMTOKENS:
1886 xmlBufferWriteChar(buf, " NMTOKENS");
1887 break;
1888 case XML_ATTRIBUTE_ENUMERATION:
1889 xmlBufferWriteChar(buf, " (");
1890 xmlDumpEnumeration(buf, attr->tree);
1891 break;
1892 case XML_ATTRIBUTE_NOTATION:
1893 xmlBufferWriteChar(buf, " NOTATION (");
1894 xmlDumpEnumeration(buf, attr->tree);
1895 break;
1896 default:
1897 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001898 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001899 attr->atype);
1900 }
1901 switch (attr->def) {
1902 case XML_ATTRIBUTE_NONE:
1903 break;
1904 case XML_ATTRIBUTE_REQUIRED:
1905 xmlBufferWriteChar(buf, " #REQUIRED");
1906 break;
1907 case XML_ATTRIBUTE_IMPLIED:
1908 xmlBufferWriteChar(buf, " #IMPLIED");
1909 break;
1910 case XML_ATTRIBUTE_FIXED:
1911 xmlBufferWriteChar(buf, " #FIXED");
1912 break;
1913 default:
1914 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001915 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001916 attr->def);
1917 }
1918 if (attr->defaultValue != NULL) {
1919 xmlBufferWriteChar(buf, " ");
1920 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1921 }
1922 xmlBufferWriteChar(buf, ">\n");
1923}
1924
1925/**
1926 * xmlDumpAttributeTable:
1927 * @buf: the XML buffer output
1928 * @table: An attribute table
1929 *
1930 * This will dump the content of the attribute table as an XML DTD definition
1931 */
1932void
1933xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1934 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1935}
1936
1937/************************************************************************
1938 * *
1939 * NOTATIONs *
1940 * *
1941 ************************************************************************/
1942/**
1943 * xmlCreateNotationTable:
1944 *
1945 * create and initialize an empty notation hash table.
1946 *
1947 * Returns the xmlNotationTablePtr just created or NULL in case
1948 * of error.
1949 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001950static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001951xmlCreateNotationTable(void) {
1952 return(xmlHashCreate(0));
1953}
1954
1955/**
1956 * xmlFreeNotation:
1957 * @not: A notation
1958 *
1959 * Deallocate the memory used by an notation definition
1960 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001961static void
Owen Taylor3473f882001-02-23 17:55:21 +00001962xmlFreeNotation(xmlNotationPtr nota) {
1963 if (nota == NULL) return;
1964 if (nota->name != NULL)
1965 xmlFree((xmlChar *) nota->name);
1966 if (nota->PublicID != NULL)
1967 xmlFree((xmlChar *) nota->PublicID);
1968 if (nota->SystemID != NULL)
1969 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001970 xmlFree(nota);
1971}
1972
1973
1974/**
1975 * xmlAddNotationDecl:
1976 * @dtd: pointer to the DTD
1977 * @ctxt: the validation context
1978 * @name: the entity name
1979 * @PublicID: the public identifier or NULL
1980 * @SystemID: the system identifier or NULL
1981 *
1982 * Register a new notation declaration
1983 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001984 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001985 */
1986xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001987xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001988 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001989 const xmlChar *PublicID, const xmlChar *SystemID) {
1990 xmlNotationPtr ret;
1991 xmlNotationTablePtr table;
1992
1993 if (dtd == NULL) {
1994 xmlGenericError(xmlGenericErrorContext,
1995 "xmlAddNotationDecl: dtd == NULL\n");
1996 return(NULL);
1997 }
1998 if (name == NULL) {
1999 xmlGenericError(xmlGenericErrorContext,
2000 "xmlAddNotationDecl: name == NULL\n");
2001 return(NULL);
2002 }
2003 if ((PublicID == NULL) && (SystemID == NULL)) {
2004 xmlGenericError(xmlGenericErrorContext,
2005 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002006 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002007 }
2008
2009 /*
2010 * Create the Notation table if needed.
2011 */
2012 table = (xmlNotationTablePtr) dtd->notations;
2013 if (table == NULL)
2014 dtd->notations = table = xmlCreateNotationTable();
2015 if (table == NULL) {
2016 xmlGenericError(xmlGenericErrorContext,
2017 "xmlAddNotationDecl: Table creation failed!\n");
2018 return(NULL);
2019 }
2020
2021 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2022 if (ret == NULL) {
2023 xmlGenericError(xmlGenericErrorContext,
2024 "xmlAddNotationDecl: out of memory\n");
2025 return(NULL);
2026 }
2027 memset(ret, 0, sizeof(xmlNotation));
2028
2029 /*
2030 * fill the structure.
2031 */
2032 ret->name = xmlStrdup(name);
2033 if (SystemID != NULL)
2034 ret->SystemID = xmlStrdup(SystemID);
2035 if (PublicID != NULL)
2036 ret->PublicID = xmlStrdup(PublicID);
2037
2038 /*
2039 * Validity Check:
2040 * Check the DTD for previous declarations of the ATTLIST
2041 */
2042 if (xmlHashAddEntry(table, name, ret)) {
2043 xmlGenericError(xmlGenericErrorContext,
2044 "xmlAddNotationDecl: %s already defined\n", name);
2045 xmlFreeNotation(ret);
2046 return(NULL);
2047 }
2048 return(ret);
2049}
2050
2051/**
2052 * xmlFreeNotationTable:
2053 * @table: An notation table
2054 *
2055 * Deallocate the memory used by an entities hash table.
2056 */
2057void
2058xmlFreeNotationTable(xmlNotationTablePtr table) {
2059 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2060}
2061
2062/**
2063 * xmlCopyNotation:
2064 * @nota: A notation
2065 *
2066 * Build a copy of a notation.
2067 *
2068 * Returns the new xmlNotationPtr or NULL in case of error.
2069 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002070static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002071xmlCopyNotation(xmlNotationPtr nota) {
2072 xmlNotationPtr cur;
2073
2074 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2075 if (cur == NULL) {
2076 xmlGenericError(xmlGenericErrorContext,
2077 "xmlCopyNotation: out of memory !\n");
2078 return(NULL);
2079 }
2080 if (nota->name != NULL)
2081 cur->name = xmlStrdup(nota->name);
2082 else
2083 cur->name = NULL;
2084 if (nota->PublicID != NULL)
2085 cur->PublicID = xmlStrdup(nota->PublicID);
2086 else
2087 cur->PublicID = NULL;
2088 if (nota->SystemID != NULL)
2089 cur->SystemID = xmlStrdup(nota->SystemID);
2090 else
2091 cur->SystemID = NULL;
2092 return(cur);
2093}
2094
2095/**
2096 * xmlCopyNotationTable:
2097 * @table: A notation table
2098 *
2099 * Build a copy of a notation table.
2100 *
2101 * Returns the new xmlNotationTablePtr or NULL in case of error.
2102 */
2103xmlNotationTablePtr
2104xmlCopyNotationTable(xmlNotationTablePtr table) {
2105 return((xmlNotationTablePtr) xmlHashCopy(table,
2106 (xmlHashCopier) xmlCopyNotation));
2107}
2108
2109/**
2110 * xmlDumpNotationDecl:
2111 * @buf: the XML buffer output
2112 * @nota: A notation declaration
2113 *
2114 * This will dump the content the notation declaration as an XML DTD definition
2115 */
2116void
2117xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2118 xmlBufferWriteChar(buf, "<!NOTATION ");
2119 xmlBufferWriteCHAR(buf, nota->name);
2120 if (nota->PublicID != NULL) {
2121 xmlBufferWriteChar(buf, " PUBLIC ");
2122 xmlBufferWriteQuotedString(buf, nota->PublicID);
2123 if (nota->SystemID != NULL) {
2124 xmlBufferWriteChar(buf, " ");
2125 xmlBufferWriteCHAR(buf, nota->SystemID);
2126 }
2127 } else {
2128 xmlBufferWriteChar(buf, " SYSTEM ");
2129 xmlBufferWriteCHAR(buf, nota->SystemID);
2130 }
2131 xmlBufferWriteChar(buf, " >\n");
2132}
2133
2134/**
2135 * xmlDumpNotationTable:
2136 * @buf: the XML buffer output
2137 * @table: A notation table
2138 *
2139 * This will dump the content of the notation table as an XML DTD definition
2140 */
2141void
2142xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2143 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
2144}
2145
2146/************************************************************************
2147 * *
2148 * IDs *
2149 * *
2150 ************************************************************************/
2151/**
2152 * xmlCreateIDTable:
2153 *
2154 * create and initialize an empty id hash table.
2155 *
2156 * Returns the xmlIDTablePtr just created or NULL in case
2157 * of error.
2158 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002159static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002160xmlCreateIDTable(void) {
2161 return(xmlHashCreate(0));
2162}
2163
2164/**
2165 * xmlFreeID:
2166 * @not: A id
2167 *
2168 * Deallocate the memory used by an id definition
2169 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002170static void
Owen Taylor3473f882001-02-23 17:55:21 +00002171xmlFreeID(xmlIDPtr id) {
2172 if (id == NULL) return;
2173 if (id->value != NULL)
2174 xmlFree((xmlChar *) id->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002175 if (id->name != NULL)
2176 xmlFree((xmlChar *) id->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002177 xmlFree(id);
2178}
2179
2180/**
2181 * xmlAddID:
2182 * @ctxt: the validation context
2183 * @doc: pointer to the document
2184 * @value: the value name
2185 * @attr: the attribute holding the ID
2186 *
2187 * Register a new id declaration
2188 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002189 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002190 */
2191xmlIDPtr
2192xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2193 xmlAttrPtr attr) {
2194 xmlIDPtr ret;
2195 xmlIDTablePtr table;
2196
2197 if (doc == NULL) {
2198 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002199 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002200 return(NULL);
2201 }
2202 if (value == NULL) {
2203 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002204 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002205 return(NULL);
2206 }
2207 if (attr == NULL) {
2208 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002209 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002210 return(NULL);
2211 }
2212
2213 /*
2214 * Create the ID table if needed.
2215 */
2216 table = (xmlIDTablePtr) doc->ids;
2217 if (table == NULL)
2218 doc->ids = table = xmlCreateIDTable();
2219 if (table == NULL) {
2220 xmlGenericError(xmlGenericErrorContext,
2221 "xmlAddID: Table creation failed!\n");
2222 return(NULL);
2223 }
2224
2225 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2226 if (ret == NULL) {
2227 xmlGenericError(xmlGenericErrorContext,
2228 "xmlAddID: out of memory\n");
2229 return(NULL);
2230 }
2231
2232 /*
2233 * fill the structure.
2234 */
2235 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002236 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2237 /*
2238 * Operating in streaming mode, attr is gonna disapear
2239 */
2240 ret->name = xmlStrdup(attr->name);
2241 ret->attr = NULL;
2242 } else {
2243 ret->attr = attr;
2244 ret->name = NULL;
2245 }
2246 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002247
2248 if (xmlHashAddEntry(table, value, ret) < 0) {
2249 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002250 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002251 */
Daniel Veillard76575762002-09-05 14:21:15 +00002252 if (ctxt != NULL) {
2253 VECTXT(ctxt, attr->parent);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002254 VERROR(ctxt->userData, "ID %s already defined\n", value);
Daniel Veillard76575762002-09-05 14:21:15 +00002255 }
Owen Taylor3473f882001-02-23 17:55:21 +00002256 xmlFreeID(ret);
2257 return(NULL);
2258 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002259 if (attr != NULL)
2260 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002261 return(ret);
2262}
2263
2264/**
2265 * xmlFreeIDTable:
2266 * @table: An id table
2267 *
2268 * Deallocate the memory used by an ID hash table.
2269 */
2270void
2271xmlFreeIDTable(xmlIDTablePtr table) {
2272 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2273}
2274
2275/**
2276 * xmlIsID:
2277 * @doc: the document
2278 * @elem: the element carrying the attribute
2279 * @attr: the attribute
2280 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002281 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002282 * then this is done if DTD loading has been requested. In the case
2283 * of HTML documents parsed with the HTML parser, then ID detection is
2284 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002285 *
2286 * Returns 0 or 1 depending on the lookup result
2287 */
2288int
2289xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2290 if (doc == NULL) return(0);
2291 if (attr == NULL) return(0);
2292 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2293 return(0);
2294 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2295 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2296 (xmlStrEqual(BAD_CAST "name", attr->name)))
2297 return(1);
2298 return(0);
2299 } else {
2300 xmlAttributePtr attrDecl;
2301
2302 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002303 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00002304 xmlChar fn[50];
Daniel Veillard37f961d2002-07-06 17:53:56 +00002305 xmlChar *fullname;
Daniel Veillardc00cda82003-04-07 10:22:39 +00002306
2307 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002308 if (fullname == NULL)
2309 return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002310 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2311 attr->name);
2312 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2313 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2314 attr->name);
Daniel Veillardc00cda82003-04-07 10:22:39 +00002315 if ((fullname != fn) && (fullname != elem->name))
2316 xmlFree(fullname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002317 } else {
2318 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2319 attr->name);
2320 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2321 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2322 attr->name);
2323 }
Owen Taylor3473f882001-02-23 17:55:21 +00002324
2325 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2326 return(1);
2327 }
2328 return(0);
2329}
2330
2331/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002332 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002333 * @doc: the document
2334 * @attr: the attribute
2335 *
2336 * Remove the given attribute from the ID table maintained internally.
2337 *
2338 * Returns -1 if the lookup failed and 0 otherwise
2339 */
2340int
2341xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2342 xmlAttrPtr cur;
2343 xmlIDTablePtr table;
2344 xmlChar *ID;
2345
2346 if (doc == NULL) return(-1);
2347 if (attr == NULL) return(-1);
2348 table = (xmlIDTablePtr) doc->ids;
2349 if (table == NULL)
2350 return(-1);
2351
2352 if (attr == NULL)
2353 return(-1);
2354 ID = xmlNodeListGetString(doc, attr->children, 1);
2355 if (ID == NULL)
2356 return(-1);
2357 cur = xmlHashLookup(table, ID);
2358 if (cur != attr) {
2359 xmlFree(ID);
2360 return(-1);
2361 }
2362 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
2363 xmlFree(ID);
2364 return(0);
2365}
2366
2367/**
2368 * xmlGetID:
2369 * @doc: pointer to the document
2370 * @ID: the ID value
2371 *
2372 * Search the attribute declaring the given ID
2373 *
2374 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2375 */
2376xmlAttrPtr
2377xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2378 xmlIDTablePtr table;
2379 xmlIDPtr id;
2380
2381 if (doc == NULL) {
2382 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
2383 return(NULL);
2384 }
2385
2386 if (ID == NULL) {
2387 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
2388 return(NULL);
2389 }
2390
2391 table = (xmlIDTablePtr) doc->ids;
2392 if (table == NULL)
2393 return(NULL);
2394
2395 id = xmlHashLookup(table, ID);
2396 if (id == NULL)
2397 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002398 if (id->attr == NULL) {
2399 /*
2400 * We are operating on a stream, return a well known reference
2401 * since the attribute node doesn't exist anymore
2402 */
2403 return((xmlAttrPtr) doc);
2404 }
Owen Taylor3473f882001-02-23 17:55:21 +00002405 return(id->attr);
2406}
2407
2408/************************************************************************
2409 * *
2410 * Refs *
2411 * *
2412 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002413typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002414{
2415 xmlListPtr l;
2416 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002417} xmlRemoveMemo;
2418
2419typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2420
2421typedef struct xmlValidateMemo_t
2422{
2423 xmlValidCtxtPtr ctxt;
2424 const xmlChar *name;
2425} xmlValidateMemo;
2426
2427typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002428
2429/**
2430 * xmlCreateRefTable:
2431 *
2432 * create and initialize an empty ref hash table.
2433 *
2434 * Returns the xmlRefTablePtr just created or NULL in case
2435 * of error.
2436 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002437static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002438xmlCreateRefTable(void) {
2439 return(xmlHashCreate(0));
2440}
2441
2442/**
2443 * xmlFreeRef:
2444 * @lk: A list link
2445 *
2446 * Deallocate the memory used by a ref definition
2447 */
2448static void
2449xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002450 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2451 if (ref == NULL) return;
2452 if (ref->value != NULL)
2453 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002454 if (ref->name != NULL)
2455 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002456 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002457}
2458
2459/**
2460 * xmlFreeRefList:
2461 * @list_ref: A list of references.
2462 *
2463 * Deallocate the memory used by a list of references
2464 */
2465static void
2466xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002467 if (list_ref == NULL) return;
2468 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002469}
2470
2471/**
2472 * xmlWalkRemoveRef:
2473 * @data: Contents of current link
2474 * @user: Value supplied by the user
2475 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002476 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002477 */
2478static int
2479xmlWalkRemoveRef(const void *data, const void *user)
2480{
Daniel Veillard37721922001-05-04 15:21:12 +00002481 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2482 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2483 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002484
Daniel Veillard37721922001-05-04 15:21:12 +00002485 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2486 xmlListRemoveFirst(ref_list, (void *)data);
2487 return 0;
2488 }
2489 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002490}
2491
2492/**
2493 * xmlAddRef:
2494 * @ctxt: the validation context
2495 * @doc: pointer to the document
2496 * @value: the value name
2497 * @attr: the attribute holding the Ref
2498 *
2499 * Register a new ref declaration
2500 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002501 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002502 */
2503xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002504xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002505 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002506 xmlRefPtr ret;
2507 xmlRefTablePtr table;
2508 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002509
Daniel Veillard37721922001-05-04 15:21:12 +00002510 if (doc == NULL) {
2511 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002512 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002513 return(NULL);
2514 }
2515 if (value == NULL) {
2516 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002517 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002518 return(NULL);
2519 }
2520 if (attr == NULL) {
2521 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002522 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002523 return(NULL);
2524 }
Owen Taylor3473f882001-02-23 17:55:21 +00002525
Daniel Veillard37721922001-05-04 15:21:12 +00002526 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002527 * Create the Ref table if needed.
2528 */
Daniel Veillard37721922001-05-04 15:21:12 +00002529 table = (xmlRefTablePtr) doc->refs;
2530 if (table == NULL)
2531 doc->refs = table = xmlCreateRefTable();
2532 if (table == NULL) {
2533 xmlGenericError(xmlGenericErrorContext,
2534 "xmlAddRef: Table creation failed!\n");
2535 return(NULL);
2536 }
Owen Taylor3473f882001-02-23 17:55:21 +00002537
Daniel Veillard37721922001-05-04 15:21:12 +00002538 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2539 if (ret == NULL) {
2540 xmlGenericError(xmlGenericErrorContext,
2541 "xmlAddRef: out of memory\n");
2542 return(NULL);
2543 }
Owen Taylor3473f882001-02-23 17:55:21 +00002544
Daniel Veillard37721922001-05-04 15:21:12 +00002545 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002546 * fill the structure.
2547 */
Daniel Veillard37721922001-05-04 15:21:12 +00002548 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002549 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2550 /*
2551 * Operating in streaming mode, attr is gonna disapear
2552 */
2553 ret->name = xmlStrdup(attr->name);
2554 ret->attr = NULL;
2555 } else {
2556 ret->name = NULL;
2557 ret->attr = attr;
2558 }
2559 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002560
Daniel Veillard37721922001-05-04 15:21:12 +00002561 /* To add a reference :-
2562 * References are maintained as a list of references,
2563 * Lookup the entry, if no entry create new nodelist
2564 * Add the owning node to the NodeList
2565 * Return the ref
2566 */
Owen Taylor3473f882001-02-23 17:55:21 +00002567
Daniel Veillard37721922001-05-04 15:21:12 +00002568 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2569 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2570 xmlGenericError(xmlGenericErrorContext,
2571 "xmlAddRef: Reference list creation failed!\n");
2572 return(NULL);
2573 }
2574 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2575 xmlListDelete(ref_list);
2576 xmlGenericError(xmlGenericErrorContext,
2577 "xmlAddRef: Reference list insertion failed!\n");
2578 return(NULL);
2579 }
2580 }
2581 xmlListInsert(ref_list, ret);
2582 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002583}
2584
2585/**
2586 * xmlFreeRefTable:
2587 * @table: An ref table
2588 *
2589 * Deallocate the memory used by an Ref hash table.
2590 */
2591void
2592xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002593 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002594}
2595
2596/**
2597 * xmlIsRef:
2598 * @doc: the document
2599 * @elem: the element carrying the attribute
2600 * @attr: the attribute
2601 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002602 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002603 * then this is simple, otherwise we use an heuristic: name Ref (upper
2604 * or lowercase).
2605 *
2606 * Returns 0 or 1 depending on the lookup result
2607 */
2608int
2609xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002610 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2611 return(0);
2612 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2613 /* TODO @@@ */
2614 return(0);
2615 } else {
2616 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002617
Daniel Veillard37721922001-05-04 15:21:12 +00002618 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2619 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2620 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2621 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002622
Daniel Veillard37721922001-05-04 15:21:12 +00002623 if ((attrDecl != NULL) &&
2624 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2625 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2626 return(1);
2627 }
2628 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002629}
2630
2631/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002632 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00002633 * @doc: the document
2634 * @attr: the attribute
2635 *
2636 * Remove the given attribute from the Ref table maintained internally.
2637 *
2638 * Returns -1 if the lookup failed and 0 otherwise
2639 */
2640int
2641xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002642 xmlListPtr ref_list;
2643 xmlRefTablePtr table;
2644 xmlChar *ID;
2645 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002646
Daniel Veillard37721922001-05-04 15:21:12 +00002647 if (doc == NULL) return(-1);
2648 if (attr == NULL) return(-1);
2649 table = (xmlRefTablePtr) doc->refs;
2650 if (table == NULL)
2651 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002652
Daniel Veillard37721922001-05-04 15:21:12 +00002653 if (attr == NULL)
2654 return(-1);
2655 ID = xmlNodeListGetString(doc, attr->children, 1);
2656 if (ID == NULL)
2657 return(-1);
2658 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002659
Daniel Veillard37721922001-05-04 15:21:12 +00002660 if(ref_list == NULL) {
2661 xmlFree(ID);
2662 return (-1);
2663 }
2664 /* At this point, ref_list refers to a list of references which
2665 * have the same key as the supplied attr. Our list of references
2666 * is ordered by reference address and we don't have that information
2667 * here to use when removing. We'll have to walk the list and
2668 * check for a matching attribute, when we find one stop the walk
2669 * and remove the entry.
2670 * The list is ordered by reference, so that means we don't have the
2671 * key. Passing the list and the reference to the walker means we
2672 * will have enough data to be able to remove the entry.
2673 */
2674 target.l = ref_list;
2675 target.ap = attr;
2676
2677 /* Remove the supplied attr from our list */
2678 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002679
Daniel Veillard37721922001-05-04 15:21:12 +00002680 /*If the list is empty then remove the list entry in the hash */
2681 if (xmlListEmpty(ref_list))
2682 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2683 xmlFreeRefList);
2684 xmlFree(ID);
2685 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002686}
2687
2688/**
2689 * xmlGetRefs:
2690 * @doc: pointer to the document
2691 * @ID: the ID value
2692 *
2693 * Find the set of references for the supplied ID.
2694 *
2695 * Returns NULL if not found, otherwise node set for the ID.
2696 */
2697xmlListPtr
2698xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002699 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002700
Daniel Veillard37721922001-05-04 15:21:12 +00002701 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002702 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002703 return(NULL);
2704 }
Owen Taylor3473f882001-02-23 17:55:21 +00002705
Daniel Veillard37721922001-05-04 15:21:12 +00002706 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002707 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002708 return(NULL);
2709 }
Owen Taylor3473f882001-02-23 17:55:21 +00002710
Daniel Veillard37721922001-05-04 15:21:12 +00002711 table = (xmlRefTablePtr) doc->refs;
2712 if (table == NULL)
2713 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002714
Daniel Veillard37721922001-05-04 15:21:12 +00002715 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002716}
2717
2718/************************************************************************
2719 * *
2720 * Routines for validity checking *
2721 * *
2722 ************************************************************************/
2723
2724/**
2725 * xmlGetDtdElementDesc:
2726 * @dtd: a pointer to the DtD to search
2727 * @name: the element name
2728 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002729 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002730 *
2731 * returns the xmlElementPtr if found or NULL
2732 */
2733
2734xmlElementPtr
2735xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2736 xmlElementTablePtr table;
2737 xmlElementPtr cur;
2738 xmlChar *uqname = NULL, *prefix = NULL;
2739
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002740 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002741 if (dtd->elements == NULL)
2742 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002743 table = (xmlElementTablePtr) dtd->elements;
2744
2745 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002746 if (uqname != NULL)
2747 name = uqname;
2748 cur = xmlHashLookup2(table, name, prefix);
2749 if (prefix != NULL) xmlFree(prefix);
2750 if (uqname != NULL) xmlFree(uqname);
2751 return(cur);
2752}
2753/**
2754 * xmlGetDtdElementDesc2:
2755 * @dtd: a pointer to the DtD to search
2756 * @name: the element name
2757 * @create: create an empty description if not found
2758 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002759 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002760 *
2761 * returns the xmlElementPtr if found or NULL
2762 */
2763
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002764static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002765xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2766 xmlElementTablePtr table;
2767 xmlElementPtr cur;
2768 xmlChar *uqname = NULL, *prefix = NULL;
2769
2770 if (dtd == NULL) return(NULL);
2771 if (dtd->elements == NULL) {
2772 if (!create)
2773 return(NULL);
2774 /*
2775 * Create the Element table if needed.
2776 */
2777 table = (xmlElementTablePtr) dtd->elements;
2778 if (table == NULL) {
2779 table = xmlCreateElementTable();
2780 dtd->elements = (void *) table;
2781 }
2782 if (table == NULL) {
2783 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002784 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002785 return(NULL);
2786 }
2787 }
2788 table = (xmlElementTablePtr) dtd->elements;
2789
2790 uqname = xmlSplitQName2(name, &prefix);
2791 if (uqname != NULL)
2792 name = uqname;
2793 cur = xmlHashLookup2(table, name, prefix);
2794 if ((cur == NULL) && (create)) {
2795 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2796 if (cur == NULL) {
2797 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002798 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002799 return(NULL);
2800 }
2801 memset(cur, 0, sizeof(xmlElement));
2802 cur->type = XML_ELEMENT_DECL;
2803
2804 /*
2805 * fill the structure.
2806 */
2807 cur->name = xmlStrdup(name);
2808 cur->prefix = xmlStrdup(prefix);
2809 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2810
2811 xmlHashAddEntry2(table, name, prefix, cur);
2812 }
2813 if (prefix != NULL) xmlFree(prefix);
2814 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002815 return(cur);
2816}
2817
2818/**
2819 * xmlGetDtdQElementDesc:
2820 * @dtd: a pointer to the DtD to search
2821 * @name: the element name
2822 * @prefix: the element namespace prefix
2823 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002824 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002825 *
2826 * returns the xmlElementPtr if found or NULL
2827 */
2828
Daniel Veillard48da9102001-08-07 01:10:10 +00002829xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002830xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2831 const xmlChar *prefix) {
2832 xmlElementTablePtr table;
2833
2834 if (dtd == NULL) return(NULL);
2835 if (dtd->elements == NULL) return(NULL);
2836 table = (xmlElementTablePtr) dtd->elements;
2837
2838 return(xmlHashLookup2(table, name, prefix));
2839}
2840
2841/**
2842 * xmlGetDtdAttrDesc:
2843 * @dtd: a pointer to the DtD to search
2844 * @elem: the element name
2845 * @name: the attribute name
2846 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002847 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002848 * this element.
2849 *
2850 * returns the xmlAttributePtr if found or NULL
2851 */
2852
2853xmlAttributePtr
2854xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2855 xmlAttributeTablePtr table;
2856 xmlAttributePtr cur;
2857 xmlChar *uqname = NULL, *prefix = NULL;
2858
2859 if (dtd == NULL) return(NULL);
2860 if (dtd->attributes == NULL) return(NULL);
2861
2862 table = (xmlAttributeTablePtr) dtd->attributes;
2863 if (table == NULL)
2864 return(NULL);
2865
2866 uqname = xmlSplitQName2(name, &prefix);
2867
2868 if (uqname != NULL) {
2869 cur = xmlHashLookup3(table, uqname, prefix, elem);
2870 if (prefix != NULL) xmlFree(prefix);
2871 if (uqname != NULL) xmlFree(uqname);
2872 } else
2873 cur = xmlHashLookup3(table, name, NULL, elem);
2874 return(cur);
2875}
2876
2877/**
2878 * xmlGetDtdQAttrDesc:
2879 * @dtd: a pointer to the DtD to search
2880 * @elem: the element name
2881 * @name: the attribute name
2882 * @prefix: the attribute namespace prefix
2883 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002884 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002885 * this element.
2886 *
2887 * returns the xmlAttributePtr if found or NULL
2888 */
2889
Daniel Veillard48da9102001-08-07 01:10:10 +00002890xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002891xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2892 const xmlChar *prefix) {
2893 xmlAttributeTablePtr table;
2894
2895 if (dtd == NULL) return(NULL);
2896 if (dtd->attributes == NULL) return(NULL);
2897 table = (xmlAttributeTablePtr) dtd->attributes;
2898
2899 return(xmlHashLookup3(table, name, prefix, elem));
2900}
2901
2902/**
2903 * xmlGetDtdNotationDesc:
2904 * @dtd: a pointer to the DtD to search
2905 * @name: the notation name
2906 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002907 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002908 *
2909 * returns the xmlNotationPtr if found or NULL
2910 */
2911
2912xmlNotationPtr
2913xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2914 xmlNotationTablePtr table;
2915
2916 if (dtd == NULL) return(NULL);
2917 if (dtd->notations == NULL) return(NULL);
2918 table = (xmlNotationTablePtr) dtd->notations;
2919
2920 return(xmlHashLookup(table, name));
2921}
2922
2923/**
2924 * xmlValidateNotationUse:
2925 * @ctxt: the validation context
2926 * @doc: the document
2927 * @notationName: the notation name to check
2928 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002929 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002930 * - [ VC: Notation Declared ]
2931 *
2932 * returns 1 if valid or 0 otherwise
2933 */
2934
2935int
2936xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2937 const xmlChar *notationName) {
2938 xmlNotationPtr notaDecl;
2939 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2940
2941 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2942 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2943 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2944
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002945 if ((notaDecl == NULL) && (ctxt != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002946 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2947 notationName);
2948 return(0);
2949 }
2950 return(1);
2951}
2952
2953/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002954 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00002955 * @doc: the document
2956 * @name: the element name
2957 *
2958 * Search in the DtDs whether an element accept Mixed content (or ANY)
2959 * basically if it is supposed to accept text childs
2960 *
2961 * returns 0 if no, 1 if yes, and -1 if no element description is available
2962 */
2963
2964int
2965xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2966 xmlElementPtr elemDecl;
2967
2968 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2969
2970 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2971 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2972 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2973 if (elemDecl == NULL) return(-1);
2974 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002975 case XML_ELEMENT_TYPE_UNDEFINED:
2976 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002977 case XML_ELEMENT_TYPE_ELEMENT:
2978 return(0);
2979 case XML_ELEMENT_TYPE_EMPTY:
2980 /*
2981 * return 1 for EMPTY since we want VC error to pop up
2982 * on <empty> </empty> for example
2983 */
2984 case XML_ELEMENT_TYPE_ANY:
2985 case XML_ELEMENT_TYPE_MIXED:
2986 return(1);
2987 }
2988 return(1);
2989}
2990
2991/**
2992 * xmlValidateNameValue:
2993 * @value: an Name value
2994 *
2995 * Validate that the given value match Name production
2996 *
2997 * returns 1 if valid or 0 otherwise
2998 */
2999
Daniel Veillard9b731d72002-04-14 12:56:08 +00003000int
Owen Taylor3473f882001-02-23 17:55:21 +00003001xmlValidateNameValue(const xmlChar *value) {
3002 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003003 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003004
3005 if (value == NULL) return(0);
3006 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003007 val = xmlStringCurrentChar(NULL, cur, &len);
3008 cur += len;
3009 if (!IS_LETTER(val) && (val != '_') &&
3010 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003011 return(0);
3012 }
3013
Daniel Veillardd8224e02002-01-13 15:43:22 +00003014 val = xmlStringCurrentChar(NULL, cur, &len);
3015 cur += len;
3016 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3017 (val == '.') || (val == '-') ||
3018 (val == '_') || (val == ':') ||
3019 (IS_COMBINING(val)) ||
3020 (IS_EXTENDER(val))) {
3021 val = xmlStringCurrentChar(NULL, cur, &len);
3022 cur += len;
3023 }
Owen Taylor3473f882001-02-23 17:55:21 +00003024
Daniel Veillardd8224e02002-01-13 15:43:22 +00003025 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003026
3027 return(1);
3028}
3029
3030/**
3031 * xmlValidateNamesValue:
3032 * @value: an Names value
3033 *
3034 * Validate that the given value match Names production
3035 *
3036 * returns 1 if valid or 0 otherwise
3037 */
3038
Daniel Veillard9b731d72002-04-14 12:56:08 +00003039int
Owen Taylor3473f882001-02-23 17:55:21 +00003040xmlValidateNamesValue(const xmlChar *value) {
3041 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003042 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003043
3044 if (value == NULL) return(0);
3045 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003046 val = xmlStringCurrentChar(NULL, cur, &len);
3047 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003048
Daniel Veillardd8224e02002-01-13 15:43:22 +00003049 if (!IS_LETTER(val) && (val != '_') &&
3050 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003051 return(0);
3052 }
3053
Daniel Veillardd8224e02002-01-13 15:43:22 +00003054 val = xmlStringCurrentChar(NULL, cur, &len);
3055 cur += len;
3056 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3057 (val == '.') || (val == '-') ||
3058 (val == '_') || (val == ':') ||
3059 (IS_COMBINING(val)) ||
3060 (IS_EXTENDER(val))) {
3061 val = xmlStringCurrentChar(NULL, cur, &len);
3062 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003063 }
3064
Daniel Veillardd8224e02002-01-13 15:43:22 +00003065 while (IS_BLANK(val)) {
3066 while (IS_BLANK(val)) {
3067 val = xmlStringCurrentChar(NULL, cur, &len);
3068 cur += len;
3069 }
3070
3071 if (!IS_LETTER(val) && (val != '_') &&
3072 (val != ':')) {
3073 return(0);
3074 }
3075 val = xmlStringCurrentChar(NULL, cur, &len);
3076 cur += len;
3077
3078 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3079 (val == '.') || (val == '-') ||
3080 (val == '_') || (val == ':') ||
3081 (IS_COMBINING(val)) ||
3082 (IS_EXTENDER(val))) {
3083 val = xmlStringCurrentChar(NULL, cur, &len);
3084 cur += len;
3085 }
3086 }
3087
3088 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003089
3090 return(1);
3091}
3092
3093/**
3094 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003095 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003096 *
3097 * Validate that the given value match Nmtoken production
3098 *
3099 * [ VC: Name Token ]
3100 *
3101 * returns 1 if valid or 0 otherwise
3102 */
3103
Daniel Veillard9b731d72002-04-14 12:56:08 +00003104int
Owen Taylor3473f882001-02-23 17:55:21 +00003105xmlValidateNmtokenValue(const xmlChar *value) {
3106 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003107 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003108
3109 if (value == NULL) return(0);
3110 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003111 val = xmlStringCurrentChar(NULL, cur, &len);
3112 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003113
Daniel Veillardd8224e02002-01-13 15:43:22 +00003114 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3115 (val != '.') && (val != '-') &&
3116 (val != '_') && (val != ':') &&
3117 (!IS_COMBINING(val)) &&
3118 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003119 return(0);
3120
Daniel Veillardd8224e02002-01-13 15:43:22 +00003121 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3122 (val == '.') || (val == '-') ||
3123 (val == '_') || (val == ':') ||
3124 (IS_COMBINING(val)) ||
3125 (IS_EXTENDER(val))) {
3126 val = xmlStringCurrentChar(NULL, cur, &len);
3127 cur += len;
3128 }
Owen Taylor3473f882001-02-23 17:55:21 +00003129
Daniel Veillardd8224e02002-01-13 15:43:22 +00003130 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003131
3132 return(1);
3133}
3134
3135/**
3136 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003137 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003138 *
3139 * Validate that the given value match Nmtokens production
3140 *
3141 * [ VC: Name Token ]
3142 *
3143 * returns 1 if valid or 0 otherwise
3144 */
3145
Daniel Veillard9b731d72002-04-14 12:56:08 +00003146int
Owen Taylor3473f882001-02-23 17:55:21 +00003147xmlValidateNmtokensValue(const xmlChar *value) {
3148 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003149 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003150
3151 if (value == NULL) return(0);
3152 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003153 val = xmlStringCurrentChar(NULL, cur, &len);
3154 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003155
Daniel Veillardd8224e02002-01-13 15:43:22 +00003156 while (IS_BLANK(val)) {
3157 val = xmlStringCurrentChar(NULL, cur, &len);
3158 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003159 }
3160
Daniel Veillardd8224e02002-01-13 15:43:22 +00003161 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3162 (val != '.') && (val != '-') &&
3163 (val != '_') && (val != ':') &&
3164 (!IS_COMBINING(val)) &&
3165 (!IS_EXTENDER(val)))
3166 return(0);
3167
3168 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3169 (val == '.') || (val == '-') ||
3170 (val == '_') || (val == ':') ||
3171 (IS_COMBINING(val)) ||
3172 (IS_EXTENDER(val))) {
3173 val = xmlStringCurrentChar(NULL, cur, &len);
3174 cur += len;
3175 }
3176
3177 while (IS_BLANK(val)) {
3178 while (IS_BLANK(val)) {
3179 val = xmlStringCurrentChar(NULL, cur, &len);
3180 cur += len;
3181 }
3182 if (val == 0) return(1);
3183
3184 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3185 (val != '.') && (val != '-') &&
3186 (val != '_') && (val != ':') &&
3187 (!IS_COMBINING(val)) &&
3188 (!IS_EXTENDER(val)))
3189 return(0);
3190
3191 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3192 (val == '.') || (val == '-') ||
3193 (val == '_') || (val == ':') ||
3194 (IS_COMBINING(val)) ||
3195 (IS_EXTENDER(val))) {
3196 val = xmlStringCurrentChar(NULL, cur, &len);
3197 cur += len;
3198 }
3199 }
3200
3201 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003202
3203 return(1);
3204}
3205
3206/**
3207 * xmlValidateNotationDecl:
3208 * @ctxt: the validation context
3209 * @doc: a document instance
3210 * @nota: a notation definition
3211 *
3212 * Try to validate a single notation definition
3213 * basically it does the following checks as described by the
3214 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003215 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003216 * But this function get called anyway ...
3217 *
3218 * returns 1 if valid or 0 otherwise
3219 */
3220
3221int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003222xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3223 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003224 int ret = 1;
3225
3226 return(ret);
3227}
3228
3229/**
3230 * xmlValidateAttributeValue:
3231 * @type: an attribute type
3232 * @value: an attribute value
3233 *
3234 * Validate that the given attribute value match the proper production
3235 *
3236 * [ VC: ID ]
3237 * Values of type ID must match the Name production....
3238 *
3239 * [ VC: IDREF ]
3240 * Values of type IDREF must match the Name production, and values
3241 * of type IDREFS must match Names ...
3242 *
3243 * [ VC: Entity Name ]
3244 * Values of type ENTITY must match the Name production, values
3245 * of type ENTITIES must match Names ...
3246 *
3247 * [ VC: Name Token ]
3248 * Values of type NMTOKEN must match the Nmtoken production; values
3249 * of type NMTOKENS must match Nmtokens.
3250 *
3251 * returns 1 if valid or 0 otherwise
3252 */
3253
3254int
3255xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3256 switch (type) {
3257 case XML_ATTRIBUTE_ENTITIES:
3258 case XML_ATTRIBUTE_IDREFS:
3259 return(xmlValidateNamesValue(value));
3260 case XML_ATTRIBUTE_ENTITY:
3261 case XML_ATTRIBUTE_IDREF:
3262 case XML_ATTRIBUTE_ID:
3263 case XML_ATTRIBUTE_NOTATION:
3264 return(xmlValidateNameValue(value));
3265 case XML_ATTRIBUTE_NMTOKENS:
3266 case XML_ATTRIBUTE_ENUMERATION:
3267 return(xmlValidateNmtokensValue(value));
3268 case XML_ATTRIBUTE_NMTOKEN:
3269 return(xmlValidateNmtokenValue(value));
3270 case XML_ATTRIBUTE_CDATA:
3271 break;
3272 }
3273 return(1);
3274}
3275
3276/**
3277 * xmlValidateAttributeValue2:
3278 * @ctxt: the validation context
3279 * @doc: the document
3280 * @name: the attribute name (used for error reporting only)
3281 * @type: the attribute type
3282 * @value: the attribute value
3283 *
3284 * Validate that the given attribute value match a given type.
3285 * This typically cannot be done before having finished parsing
3286 * the subsets.
3287 *
3288 * [ VC: IDREF ]
3289 * Values of type IDREF must match one of the declared IDs
3290 * Values of type IDREFS must match a sequence of the declared IDs
3291 * each Name must match the value of an ID attribute on some element
3292 * in the XML document; i.e. IDREF values must match the value of
3293 * some ID attribute
3294 *
3295 * [ VC: Entity Name ]
3296 * Values of type ENTITY must match one declared entity
3297 * Values of type ENTITIES must match a sequence of declared entities
3298 *
3299 * [ VC: Notation Attributes ]
3300 * all notation names in the declaration must be declared.
3301 *
3302 * returns 1 if valid or 0 otherwise
3303 */
3304
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003305static int
Owen Taylor3473f882001-02-23 17:55:21 +00003306xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3307 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3308 int ret = 1;
3309 switch (type) {
3310 case XML_ATTRIBUTE_IDREFS:
3311 case XML_ATTRIBUTE_IDREF:
3312 case XML_ATTRIBUTE_ID:
3313 case XML_ATTRIBUTE_NMTOKENS:
3314 case XML_ATTRIBUTE_ENUMERATION:
3315 case XML_ATTRIBUTE_NMTOKEN:
3316 case XML_ATTRIBUTE_CDATA:
3317 break;
3318 case XML_ATTRIBUTE_ENTITY: {
3319 xmlEntityPtr ent;
3320
3321 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003322 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003323 if ((ent == NULL) && (doc->standalone == 1)) {
3324 doc->standalone = 0;
3325 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003326 }
Owen Taylor3473f882001-02-23 17:55:21 +00003327 if (ent == NULL) {
3328 VERROR(ctxt->userData,
3329 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3330 name, value);
3331 ret = 0;
3332 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3333 VERROR(ctxt->userData,
3334 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3335 name, value);
3336 ret = 0;
3337 }
3338 break;
3339 }
3340 case XML_ATTRIBUTE_ENTITIES: {
3341 xmlChar *dup, *nam = NULL, *cur, save;
3342 xmlEntityPtr ent;
3343
3344 dup = xmlStrdup(value);
3345 if (dup == NULL)
3346 return(0);
3347 cur = dup;
3348 while (*cur != 0) {
3349 nam = cur;
3350 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
3351 save = *cur;
3352 *cur = 0;
3353 ent = xmlGetDocEntity(doc, nam);
3354 if (ent == NULL) {
3355 VERROR(ctxt->userData,
3356 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3357 name, nam);
3358 ret = 0;
3359 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3360 VERROR(ctxt->userData,
3361 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3362 name, nam);
3363 ret = 0;
3364 }
3365 if (save == 0)
3366 break;
3367 *cur = save;
3368 while (IS_BLANK(*cur)) cur++;
3369 }
3370 xmlFree(dup);
3371 break;
3372 }
3373 case XML_ATTRIBUTE_NOTATION: {
3374 xmlNotationPtr nota;
3375
3376 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3377 if ((nota == NULL) && (doc->extSubset != NULL))
3378 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3379
3380 if (nota == NULL) {
3381 VERROR(ctxt->userData,
3382 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3383 name, value);
3384 ret = 0;
3385 }
3386 break;
3387 }
3388 }
3389 return(ret);
3390}
3391
3392/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003393 * xmlValidCtxtNormalizeAttributeValue:
3394 * @ctxt: the validation context
3395 * @doc: the document
3396 * @elem: the parent
3397 * @name: the attribute name
3398 * @value: the attribute value
3399 * @ctxt: the validation context or NULL
3400 *
3401 * Does the validation related extra step of the normalization of attribute
3402 * values:
3403 *
3404 * If the declared value is not CDATA, then the XML processor must further
3405 * process the normalized attribute value by discarding any leading and
3406 * trailing space (#x20) characters, and by replacing sequences of space
3407 * (#x20) characters by single space (#x20) character.
3408 *
3409 * Also check VC: Standalone Document Declaration in P32, and update
3410 * ctxt->valid accordingly
3411 *
3412 * returns a new normalized string if normalization is needed, NULL otherwise
3413 * the caller must free the returned value.
3414 */
3415
3416xmlChar *
3417xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3418 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3419 xmlChar *ret, *dst;
3420 const xmlChar *src;
3421 xmlAttributePtr attrDecl = NULL;
3422 int extsubset = 0;
3423
3424 if (doc == NULL) return(NULL);
3425 if (elem == NULL) return(NULL);
3426 if (name == NULL) return(NULL);
3427 if (value == NULL) return(NULL);
3428
3429 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003430 xmlChar fn[50];
3431 xmlChar *fullname;
3432
3433 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3434 if (fullname == NULL)
3435 return(0);
3436 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003437 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003438 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003439 if (attrDecl != NULL)
3440 extsubset = 1;
3441 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003442 if ((fullname != fn) && (fullname != elem->name))
3443 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003444 }
3445 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3446 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3447 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3448 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3449 if (attrDecl != NULL)
3450 extsubset = 1;
3451 }
3452
3453 if (attrDecl == NULL)
3454 return(NULL);
3455 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3456 return(NULL);
3457
3458 ret = xmlStrdup(value);
3459 if (ret == NULL)
3460 return(NULL);
3461 src = value;
3462 dst = ret;
3463 while (*src == 0x20) src++;
3464 while (*src != 0) {
3465 if (*src == 0x20) {
3466 while (*src == 0x20) src++;
3467 if (*src != 0)
3468 *dst++ = 0x20;
3469 } else {
3470 *dst++ = *src++;
3471 }
3472 }
3473 *dst = 0;
3474 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3475 VERROR(ctxt->userData,
3476"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3477 name, elem->name);
3478 ctxt->valid = 0;
3479 }
3480 return(ret);
3481}
3482
3483/**
Owen Taylor3473f882001-02-23 17:55:21 +00003484 * xmlValidNormalizeAttributeValue:
3485 * @doc: the document
3486 * @elem: the parent
3487 * @name: the attribute name
3488 * @value: the attribute value
3489 *
3490 * Does the validation related extra step of the normalization of attribute
3491 * values:
3492 *
3493 * If the declared value is not CDATA, then the XML processor must further
3494 * process the normalized attribute value by discarding any leading and
3495 * trailing space (#x20) characters, and by replacing sequences of space
3496 * (#x20) characters by single space (#x20) character.
3497 *
3498 * returns a new normalized string if normalization is needed, NULL otherwise
3499 * the caller must free the returned value.
3500 */
3501
3502xmlChar *
3503xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3504 const xmlChar *name, const xmlChar *value) {
3505 xmlChar *ret, *dst;
3506 const xmlChar *src;
3507 xmlAttributePtr attrDecl = NULL;
3508
3509 if (doc == NULL) return(NULL);
3510 if (elem == NULL) return(NULL);
3511 if (name == NULL) return(NULL);
3512 if (value == NULL) return(NULL);
3513
3514 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003515 xmlChar fn[50];
3516 xmlChar *fullname;
3517
3518 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3519 if (fullname == NULL)
3520 return(0);
3521 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003522 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003523 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3524 if ((fullname != fn) && (fullname != elem->name))
3525 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003526 }
3527 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3528 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3529 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3530
3531 if (attrDecl == NULL)
3532 return(NULL);
3533 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3534 return(NULL);
3535
3536 ret = xmlStrdup(value);
3537 if (ret == NULL)
3538 return(NULL);
3539 src = value;
3540 dst = ret;
3541 while (*src == 0x20) src++;
3542 while (*src != 0) {
3543 if (*src == 0x20) {
3544 while (*src == 0x20) src++;
3545 if (*src != 0)
3546 *dst++ = 0x20;
3547 } else {
3548 *dst++ = *src++;
3549 }
3550 }
3551 *dst = 0;
3552 return(ret);
3553}
3554
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003555static void
Owen Taylor3473f882001-02-23 17:55:21 +00003556xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003557 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003558 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3559}
3560
3561/**
3562 * xmlValidateAttributeDecl:
3563 * @ctxt: the validation context
3564 * @doc: a document instance
3565 * @attr: an attribute definition
3566 *
3567 * Try to validate a single attribute definition
3568 * basically it does the following checks as described by the
3569 * XML-1.0 recommendation:
3570 * - [ VC: Attribute Default Legal ]
3571 * - [ VC: Enumeration ]
3572 * - [ VC: ID Attribute Default ]
3573 *
3574 * The ID/IDREF uniqueness and matching are done separately
3575 *
3576 * returns 1 if valid or 0 otherwise
3577 */
3578
3579int
3580xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3581 xmlAttributePtr attr) {
3582 int ret = 1;
3583 int val;
3584 CHECK_DTD;
3585 if(attr == NULL) return(1);
3586
3587 /* Attribute Default Legal */
3588 /* Enumeration */
3589 if (attr->defaultValue != NULL) {
3590 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3591 if (val == 0) {
3592 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003593 "Syntax of default value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003594 attr->name, attr->elem);
3595 }
3596 ret &= val;
3597 }
3598
3599 /* ID Attribute Default */
3600 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3601 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3602 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3603 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003604 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003605 attr->name, attr->elem);
3606 ret = 0;
3607 }
3608
3609 /* One ID per Element Type */
3610 if (attr->atype == XML_ATTRIBUTE_ID) {
3611 int nbId;
3612
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003613 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003614 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3615 attr->elem);
3616 if (elem != NULL) {
3617 nbId = xmlScanIDAttributeDecl(NULL, elem);
3618 } else {
3619 xmlAttributeTablePtr table;
3620
3621 /*
3622 * The attribute may be declared in the internal subset and the
3623 * element in the external subset.
3624 */
3625 nbId = 0;
3626 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3627 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3628 xmlValidateAttributeIdCallback, &nbId);
3629 }
3630 if (nbId > 1) {
3631 VERROR(ctxt->userData,
3632 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3633 attr->elem, nbId, attr->name);
3634 } else if (doc->extSubset != NULL) {
3635 int extId = 0;
3636 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3637 if (elem != NULL) {
3638 extId = xmlScanIDAttributeDecl(NULL, elem);
3639 }
3640 if (extId > 1) {
3641 VERROR(ctxt->userData,
3642 "Element %s has %d ID attribute defined in the external subset : %s\n",
3643 attr->elem, extId, attr->name);
3644 } else if (extId + nbId > 1) {
3645 VERROR(ctxt->userData,
3646"Element %s has ID attributes defined in the internal and external subset : %s\n",
3647 attr->elem, attr->name);
3648 }
3649 }
3650 }
3651
3652 /* Validity Constraint: Enumeration */
3653 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3654 xmlEnumerationPtr tree = attr->tree;
3655 while (tree != NULL) {
3656 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3657 tree = tree->next;
3658 }
3659 if (tree == NULL) {
3660 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003661"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003662 attr->defaultValue, attr->name, attr->elem);
3663 ret = 0;
3664 }
3665 }
3666
3667 return(ret);
3668}
3669
3670/**
3671 * xmlValidateElementDecl:
3672 * @ctxt: the validation context
3673 * @doc: a document instance
3674 * @elem: an element definition
3675 *
3676 * Try to validate a single element definition
3677 * basically it does the following checks as described by the
3678 * XML-1.0 recommendation:
3679 * - [ VC: One ID per Element Type ]
3680 * - [ VC: No Duplicate Types ]
3681 * - [ VC: Unique Element Type Declaration ]
3682 *
3683 * returns 1 if valid or 0 otherwise
3684 */
3685
3686int
3687xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3688 xmlElementPtr elem) {
3689 int ret = 1;
3690 xmlElementPtr tst;
3691
3692 CHECK_DTD;
3693
3694 if (elem == NULL) return(1);
3695
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003696#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00003697#ifdef LIBXML_REGEXP_ENABLED
3698 /* Build the regexp associated to the content model */
3699 ret = xmlValidBuildContentModel(ctxt, elem);
3700#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003701#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00003702
Owen Taylor3473f882001-02-23 17:55:21 +00003703 /* No Duplicate Types */
3704 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3705 xmlElementContentPtr cur, next;
3706 const xmlChar *name;
3707
3708 cur = elem->content;
3709 while (cur != NULL) {
3710 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3711 if (cur->c1 == NULL) break;
3712 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3713 name = cur->c1->name;
3714 next = cur->c2;
3715 while (next != NULL) {
3716 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00003717 if ((xmlStrEqual(next->name, name)) &&
3718 (xmlStrEqual(next->prefix, cur->prefix))) {
3719 if (cur->prefix == NULL) {
3720 VERROR(ctxt->userData,
Owen Taylor3473f882001-02-23 17:55:21 +00003721 "Definition of %s has duplicate references of %s\n",
Daniel Veillard7b68df92003-08-03 22:58:54 +00003722 elem->name, name);
3723 } else {
3724 VERROR(ctxt->userData,
3725 "Definition of %s has duplicate references of %s:%s\n",
3726 elem->name, cur->prefix, name);
3727 }
Owen Taylor3473f882001-02-23 17:55:21 +00003728 ret = 0;
3729 }
3730 break;
3731 }
3732 if (next->c1 == NULL) break;
3733 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00003734 if ((xmlStrEqual(next->c1->name, name)) &&
3735 (xmlStrEqual(next->c1->prefix, cur->prefix))) {
3736 if (cur->prefix == NULL) {
3737 VERROR(ctxt->userData,
3738 "Definition of %s has duplicate references to %s\n",
3739 elem->name, name);
3740 } else {
3741 VERROR(ctxt->userData,
3742 "Definition of %s has duplicate references to %s:%s\n",
3743 elem->name, cur->prefix, name);
3744 }
Owen Taylor3473f882001-02-23 17:55:21 +00003745 ret = 0;
3746 }
3747 next = next->c2;
3748 }
3749 }
3750 cur = cur->c2;
3751 }
3752 }
3753
3754 /* VC: Unique Element Type Declaration */
3755 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003756 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003757 ((tst->prefix == elem->prefix) ||
3758 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003759 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003760 VERROR(ctxt->userData, "Redefinition of element %s\n",
3761 elem->name);
3762 ret = 0;
3763 }
3764 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003765 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003766 ((tst->prefix == elem->prefix) ||
3767 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003768 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003769 VERROR(ctxt->userData, "Redefinition of element %s\n",
3770 elem->name);
3771 ret = 0;
3772 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003773 /* One ID per Element Type
3774 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003775 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3776 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003777 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003778 return(ret);
3779}
3780
3781/**
3782 * xmlValidateOneAttribute:
3783 * @ctxt: the validation context
3784 * @doc: a document instance
3785 * @elem: an element instance
3786 * @attr: an attribute instance
3787 * @value: the attribute value (without entities processing)
3788 *
3789 * Try to validate a single attribute for an element
3790 * basically it does the following checks as described by the
3791 * XML-1.0 recommendation:
3792 * - [ VC: Attribute Value Type ]
3793 * - [ VC: Fixed Attribute Default ]
3794 * - [ VC: Entity Name ]
3795 * - [ VC: Name Token ]
3796 * - [ VC: ID ]
3797 * - [ VC: IDREF ]
3798 * - [ VC: Entity Name ]
3799 * - [ VC: Notation Attributes ]
3800 *
3801 * The ID/IDREF uniqueness and matching are done separately
3802 *
3803 * returns 1 if valid or 0 otherwise
3804 */
3805
3806int
3807xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillard07cb8222003-09-10 10:51:05 +00003808 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
3809{
Owen Taylor3473f882001-02-23 17:55:21 +00003810 xmlAttributePtr attrDecl = NULL;
3811 int val;
3812 int ret = 1;
3813
3814 CHECK_DTD;
3815 if ((elem == NULL) || (elem->name == NULL)) return(0);
3816 if ((attr == NULL) || (attr->name == NULL)) return(0);
3817
3818 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003819 xmlChar fn[50];
3820 xmlChar *fullname;
3821
3822 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3823 if (fullname == NULL)
3824 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003825 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003826 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00003827 attr->name, attr->ns->prefix);
3828 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003829 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00003830 attr->name, attr->ns->prefix);
3831 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003832 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003833 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3834 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00003835 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003836 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003837 if ((fullname != fn) && (fullname != elem->name))
3838 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003839 }
3840 if (attrDecl == NULL) {
3841 if (attr->ns != NULL) {
3842 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3843 attr->name, attr->ns->prefix);
3844 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3845 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3846 attr->name, attr->ns->prefix);
3847 } else {
3848 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3849 elem->name, attr->name);
3850 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3851 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3852 elem->name, attr->name);
3853 }
3854 }
3855
3856
3857 /* Validity Constraint: Attribute Value Type */
3858 if (attrDecl == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003859 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003860 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003861 "No declaration for attribute %s of element %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003862 attr->name, elem->name);
3863 return(0);
3864 }
3865 attr->atype = attrDecl->atype;
3866
3867 val = xmlValidateAttributeValue(attrDecl->atype, value);
3868 if (val == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003869 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003870 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003871 "Syntax of value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003872 attr->name, elem->name);
3873 ret = 0;
3874 }
3875
3876 /* Validity constraint: Fixed Attribute Default */
3877 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3878 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003879 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003880 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003881 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003882 attr->name, elem->name, attrDecl->defaultValue);
3883 ret = 0;
3884 }
3885 }
3886
3887 /* Validity Constraint: ID uniqueness */
3888 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3889 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3890 ret = 0;
3891 }
3892
3893 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3894 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3895 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3896 ret = 0;
3897 }
3898
3899 /* Validity Constraint: Notation Attributes */
3900 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3901 xmlEnumerationPtr tree = attrDecl->tree;
3902 xmlNotationPtr nota;
3903
3904 /* First check that the given NOTATION was declared */
3905 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3906 if (nota == NULL)
3907 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3908
3909 if (nota == NULL) {
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 \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003913 value, attr->name, elem->name);
3914 ret = 0;
3915 }
3916
3917 /* Second, verify that it's among the list */
3918 while (tree != NULL) {
3919 if (xmlStrEqual(tree->name, value)) break;
3920 tree = tree->next;
3921 }
3922 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003923 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003924 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003925"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003926 value, attr->name, elem->name);
3927 ret = 0;
3928 }
3929 }
3930
3931 /* Validity Constraint: Enumeration */
3932 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3933 xmlEnumerationPtr tree = attrDecl->tree;
3934 while (tree != NULL) {
3935 if (xmlStrEqual(tree->name, value)) break;
3936 tree = tree->next;
3937 }
3938 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003939 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003940 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003941 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003942 value, attr->name, elem->name);
3943 ret = 0;
3944 }
3945 }
3946
3947 /* Fixed Attribute Default */
3948 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3949 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003950 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003951 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003952 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003953 attr->name, elem->name, attrDecl->defaultValue);
3954 ret = 0;
3955 }
3956
3957 /* Extra check for the attribute value */
3958 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3959 attrDecl->atype, value);
3960
3961 return(ret);
3962}
3963
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003964/**
3965 * xmlValidateOneNamespace:
3966 * @ctxt: the validation context
3967 * @doc: a document instance
3968 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00003969 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003970 * @ns: an namespace declaration instance
3971 * @value: the attribute value (without entities processing)
3972 *
3973 * Try to validate a single namespace declaration for an element
3974 * basically it does the following checks as described by the
3975 * XML-1.0 recommendation:
3976 * - [ VC: Attribute Value Type ]
3977 * - [ VC: Fixed Attribute Default ]
3978 * - [ VC: Entity Name ]
3979 * - [ VC: Name Token ]
3980 * - [ VC: ID ]
3981 * - [ VC: IDREF ]
3982 * - [ VC: Entity Name ]
3983 * - [ VC: Notation Attributes ]
3984 *
3985 * The ID/IDREF uniqueness and matching are done separately
3986 *
3987 * returns 1 if valid or 0 otherwise
3988 */
3989
3990int
3991xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3992xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
3993 /* xmlElementPtr elemDecl; */
3994 xmlAttributePtr attrDecl = NULL;
3995 int val;
3996 int ret = 1;
3997
3998 CHECK_DTD;
3999 if ((elem == NULL) || (elem->name == NULL)) return(0);
4000 if ((ns == NULL) || (ns->href == NULL)) return(0);
4001
4002 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004003 xmlChar fn[50];
4004 xmlChar *fullname;
4005
4006 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4007 if (fullname == NULL) {
4008 VERROR(ctxt->userData, "Out of memory\n");
4009 return(0);
4010 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004011 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004012 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004013 ns->prefix, BAD_CAST "xmlns");
4014 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004015 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004016 ns->prefix, BAD_CAST "xmlns");
4017 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004018 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004019 BAD_CAST "xmlns");
4020 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004021 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004022 BAD_CAST "xmlns");
4023 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004024 if ((fullname != fn) && (fullname != elem->name))
4025 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004026 }
4027 if (attrDecl == NULL) {
4028 if (ns->prefix != NULL) {
4029 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4030 ns->prefix, BAD_CAST "xmlns");
4031 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4032 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4033 ns->prefix, BAD_CAST "xmlns");
4034 } else {
4035 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4036 elem->name, BAD_CAST "xmlns");
4037 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4038 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4039 elem->name, BAD_CAST "xmlns");
4040 }
4041 }
4042
4043
4044 /* Validity Constraint: Attribute Value Type */
4045 if (attrDecl == NULL) {
4046 VECTXT(ctxt, elem);
4047 if (ns->prefix != NULL) {
4048 VERROR(ctxt->userData,
4049 "No declaration for attribute xmlns:%s of element %s\n",
4050 ns->prefix, elem->name);
4051 } else {
4052 VERROR(ctxt->userData,
4053 "No declaration for attribute xmlns of element %s\n",
4054 elem->name);
4055 }
4056 return(0);
4057 }
4058
4059 val = xmlValidateAttributeValue(attrDecl->atype, value);
4060 if (val == 0) {
4061 VECTXT(ctxt, elem);
4062 if (ns->prefix != NULL) {
4063 VERROR(ctxt->userData,
4064 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4065 ns->prefix, elem->name);
4066 } else {
4067 VERROR(ctxt->userData,
4068 "Syntax of value for attribute xmlns of %s is not valid\n",
4069 elem->name);
4070 }
4071 ret = 0;
4072 }
4073
4074 /* Validity constraint: Fixed Attribute Default */
4075 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4076 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4077 VECTXT(ctxt, elem);
4078 if (ns->prefix != NULL) {
4079 VERROR(ctxt->userData,
4080 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4081 ns->prefix, elem->name, attrDecl->defaultValue);
4082 } else {
4083 VERROR(ctxt->userData,
4084 "Value for attribute xmlns of %s is different from default \"%s\"\n",
4085 elem->name, attrDecl->defaultValue);
4086 }
4087 ret = 0;
4088 }
4089 }
4090
4091 /* Validity Constraint: ID uniqueness */
4092 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4093 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4094 ret = 0;
4095 }
4096
4097 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4098 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4099 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4100 ret = 0;
4101 }
4102
4103 /* Validity Constraint: Notation Attributes */
4104 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4105 xmlEnumerationPtr tree = attrDecl->tree;
4106 xmlNotationPtr nota;
4107
4108 /* First check that the given NOTATION was declared */
4109 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4110 if (nota == NULL)
4111 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4112
4113 if (nota == NULL) {
4114 VECTXT(ctxt, elem);
4115 if (ns->prefix != NULL) {
4116 VERROR(ctxt->userData,
4117 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4118 value, ns->prefix, elem->name);
4119 } else {
4120 VERROR(ctxt->userData,
4121 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4122 value, elem->name);
4123 }
4124 ret = 0;
4125 }
4126
4127 /* Second, verify that it's among the list */
4128 while (tree != NULL) {
4129 if (xmlStrEqual(tree->name, value)) break;
4130 tree = tree->next;
4131 }
4132 if (tree == NULL) {
4133 VECTXT(ctxt, elem);
4134 if (ns->prefix != NULL) {
4135 VERROR(ctxt->userData,
4136"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4137 value, ns->prefix, elem->name);
4138 } else {
4139 VERROR(ctxt->userData,
4140"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4141 value, elem->name);
4142 }
4143 ret = 0;
4144 }
4145 }
4146
4147 /* Validity Constraint: Enumeration */
4148 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4149 xmlEnumerationPtr tree = attrDecl->tree;
4150 while (tree != NULL) {
4151 if (xmlStrEqual(tree->name, value)) break;
4152 tree = tree->next;
4153 }
4154 if (tree == NULL) {
4155 VECTXT(ctxt, elem);
4156 if (ns->prefix != NULL) {
4157 VERROR(ctxt->userData,
4158"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4159 value, ns->prefix, elem->name);
4160 } else {
4161 VERROR(ctxt->userData,
4162"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4163 value, elem->name);
4164 }
4165 ret = 0;
4166 }
4167 }
4168
4169 /* Fixed Attribute Default */
4170 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4171 (!xmlStrEqual(attrDecl->defaultValue, value))) {
4172 VECTXT(ctxt, elem);
4173 if (ns->prefix != NULL) {
4174 VERROR(ctxt->userData,
4175 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4176 ns->prefix, elem->name, attrDecl->defaultValue);
4177 } else {
4178 VERROR(ctxt->userData,
4179 "Value for attribute xmlns of %s must be \"%s\"\n",
4180 elem->name, attrDecl->defaultValue);
4181 }
4182 ret = 0;
4183 }
4184
4185 /* Extra check for the attribute value */
4186 if (ns->prefix != NULL) {
4187 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4188 attrDecl->atype, value);
4189 } else {
4190 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4191 attrDecl->atype, value);
4192 }
4193
4194 return(ret);
4195}
4196
Daniel Veillard118aed72002-09-24 14:13:13 +00004197#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004198/**
4199 * xmlValidateSkipIgnorable:
4200 * @ctxt: the validation context
4201 * @child: the child list
4202 *
4203 * Skip ignorable elements w.r.t. the validation process
4204 *
4205 * returns the first element to consider for validation of the content model
4206 */
4207
4208static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004209xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004210 while (child != NULL) {
4211 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004212 /* These things are ignored (skipped) during validation. */
4213 case XML_PI_NODE:
4214 case XML_COMMENT_NODE:
4215 case XML_XINCLUDE_START:
4216 case XML_XINCLUDE_END:
4217 child = child->next;
4218 break;
4219 case XML_TEXT_NODE:
4220 if (xmlIsBlankNode(child))
4221 child = child->next;
4222 else
4223 return(child);
4224 break;
4225 /* keep current node */
4226 default:
4227 return(child);
4228 }
4229 }
4230 return(child);
4231}
4232
4233/**
4234 * xmlValidateElementType:
4235 * @ctxt: the validation context
4236 *
4237 * Try to validate the content model of an element internal function
4238 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004239 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4240 * reference is found and -3 if the validation succeeded but
4241 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004242 */
4243
4244static int
4245xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004246 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004247 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004248
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004249 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004250 if ((NODE == NULL) && (CONT == NULL))
4251 return(1);
4252 if ((NODE == NULL) &&
4253 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4254 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4255 return(1);
4256 }
4257 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004258 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004259 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004260
4261 /*
4262 * We arrive here when more states need to be examined
4263 */
4264cont:
4265
4266 /*
4267 * We just recovered from a rollback generated by a possible
4268 * epsilon transition, go directly to the analysis phase
4269 */
4270 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004271 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004272 DEBUG_VALID_STATE(NODE, CONT)
4273 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004274 goto analyze;
4275 }
4276
4277 DEBUG_VALID_STATE(NODE, CONT)
4278 /*
4279 * we may have to save a backup state here. This is the equivalent
4280 * of handling epsilon transition in NFAs.
4281 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004282 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004283 ((CONT->parent == NULL) ||
4284 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004285 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004286 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004287 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004288 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004289 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4290 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004291 }
4292
4293
4294 /*
4295 * Check first if the content matches
4296 */
4297 switch (CONT->type) {
4298 case XML_ELEMENT_CONTENT_PCDATA:
4299 if (NODE == NULL) {
4300 DEBUG_VALID_MSG("pcdata failed no node");
4301 ret = 0;
4302 break;
4303 }
4304 if (NODE->type == XML_TEXT_NODE) {
4305 DEBUG_VALID_MSG("pcdata found, skip to next");
4306 /*
4307 * go to next element in the content model
4308 * skipping ignorable elems
4309 */
4310 do {
4311 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004312 NODE = xmlValidateSkipIgnorable(NODE);
4313 if ((NODE != NULL) &&
4314 (NODE->type == XML_ENTITY_REF_NODE))
4315 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004316 } while ((NODE != NULL) &&
4317 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004318 (NODE->type != XML_TEXT_NODE) &&
4319 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004320 ret = 1;
4321 break;
4322 } else {
4323 DEBUG_VALID_MSG("pcdata failed");
4324 ret = 0;
4325 break;
4326 }
4327 break;
4328 case XML_ELEMENT_CONTENT_ELEMENT:
4329 if (NODE == NULL) {
4330 DEBUG_VALID_MSG("element failed no node");
4331 ret = 0;
4332 break;
4333 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004334 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4335 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004336 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004337 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4338 ret = (CONT->prefix == NULL);
4339 } else if (CONT->prefix == NULL) {
4340 ret = 0;
4341 } else {
4342 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4343 }
4344 }
4345 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004346 DEBUG_VALID_MSG("element found, skip to next");
4347 /*
4348 * go to next element in the content model
4349 * skipping ignorable elems
4350 */
4351 do {
4352 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004353 NODE = xmlValidateSkipIgnorable(NODE);
4354 if ((NODE != NULL) &&
4355 (NODE->type == XML_ENTITY_REF_NODE))
4356 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004357 } while ((NODE != NULL) &&
4358 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004359 (NODE->type != XML_TEXT_NODE) &&
4360 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004361 } else {
4362 DEBUG_VALID_MSG("element failed");
4363 ret = 0;
4364 break;
4365 }
4366 break;
4367 case XML_ELEMENT_CONTENT_OR:
4368 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004369 * Small optimization.
4370 */
4371 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4372 if ((NODE == NULL) ||
4373 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4374 DEPTH++;
4375 CONT = CONT->c2;
4376 goto cont;
4377 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004378 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4379 ret = (CONT->c1->prefix == NULL);
4380 } else if (CONT->c1->prefix == NULL) {
4381 ret = 0;
4382 } else {
4383 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4384 }
4385 if (ret == 0) {
4386 DEPTH++;
4387 CONT = CONT->c2;
4388 goto cont;
4389 }
Daniel Veillard85349052001-04-20 13:48:21 +00004390 }
4391
4392 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004393 * save the second branch 'or' branch
4394 */
4395 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004396 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4397 OCCURS, ROLLBACK_OR) < 0)
4398 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004399 DEPTH++;
4400 CONT = CONT->c1;
4401 goto cont;
4402 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004403 /*
4404 * Small optimization.
4405 */
4406 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4407 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4408 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4409 if ((NODE == NULL) ||
4410 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4411 DEPTH++;
4412 CONT = CONT->c2;
4413 goto cont;
4414 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004415 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4416 ret = (CONT->c1->prefix == NULL);
4417 } else if (CONT->c1->prefix == NULL) {
4418 ret = 0;
4419 } else {
4420 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4421 }
4422 if (ret == 0) {
4423 DEPTH++;
4424 CONT = CONT->c2;
4425 goto cont;
4426 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004427 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004428 DEPTH++;
4429 CONT = CONT->c1;
4430 goto cont;
4431 }
4432
4433 /*
4434 * At this point handle going up in the tree
4435 */
4436 if (ret == -1) {
4437 DEBUG_VALID_MSG("error found returning");
4438 return(ret);
4439 }
4440analyze:
4441 while (CONT != NULL) {
4442 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004443 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004444 * this level.
4445 */
4446 if (ret == 0) {
4447 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004448 xmlNodePtr cur;
4449
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004450 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004451 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004452 DEBUG_VALID_MSG("Once branch failed, rollback");
4453 if (vstateVPop(ctxt) < 0 ) {
4454 DEBUG_VALID_MSG("exhaustion, failed");
4455 return(0);
4456 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004457 if (cur != ctxt->vstate->node)
4458 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004459 goto cont;
4460 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004461 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004462 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004463 DEBUG_VALID_MSG("Plus branch failed, rollback");
4464 if (vstateVPop(ctxt) < 0 ) {
4465 DEBUG_VALID_MSG("exhaustion, failed");
4466 return(0);
4467 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004468 if (cur != ctxt->vstate->node)
4469 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004470 goto cont;
4471 }
4472 DEBUG_VALID_MSG("Plus branch found");
4473 ret = 1;
4474 break;
4475 case XML_ELEMENT_CONTENT_MULT:
4476#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004477 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004478 DEBUG_VALID_MSG("Mult branch failed");
4479 } else {
4480 DEBUG_VALID_MSG("Mult branch found");
4481 }
4482#endif
4483 ret = 1;
4484 break;
4485 case XML_ELEMENT_CONTENT_OPT:
4486 DEBUG_VALID_MSG("Option branch failed");
4487 ret = 1;
4488 break;
4489 }
4490 } else {
4491 switch (CONT->ocur) {
4492 case XML_ELEMENT_CONTENT_OPT:
4493 DEBUG_VALID_MSG("Option branch succeeded");
4494 ret = 1;
4495 break;
4496 case XML_ELEMENT_CONTENT_ONCE:
4497 DEBUG_VALID_MSG("Once branch succeeded");
4498 ret = 1;
4499 break;
4500 case XML_ELEMENT_CONTENT_PLUS:
4501 if (STATE == ROLLBACK_PARENT) {
4502 DEBUG_VALID_MSG("Plus branch rollback");
4503 ret = 1;
4504 break;
4505 }
4506 if (NODE == NULL) {
4507 DEBUG_VALID_MSG("Plus branch exhausted");
4508 ret = 1;
4509 break;
4510 }
4511 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004512 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004513 goto cont;
4514 case XML_ELEMENT_CONTENT_MULT:
4515 if (STATE == ROLLBACK_PARENT) {
4516 DEBUG_VALID_MSG("Mult branch rollback");
4517 ret = 1;
4518 break;
4519 }
4520 if (NODE == NULL) {
4521 DEBUG_VALID_MSG("Mult branch exhausted");
4522 ret = 1;
4523 break;
4524 }
4525 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004526 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004527 goto cont;
4528 }
4529 }
4530 STATE = 0;
4531
4532 /*
4533 * Then act accordingly at the parent level
4534 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004535 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004536 if (CONT->parent == NULL)
4537 break;
4538
4539 switch (CONT->parent->type) {
4540 case XML_ELEMENT_CONTENT_PCDATA:
4541 DEBUG_VALID_MSG("Error: parent pcdata");
4542 return(-1);
4543 case XML_ELEMENT_CONTENT_ELEMENT:
4544 DEBUG_VALID_MSG("Error: parent element");
4545 return(-1);
4546 case XML_ELEMENT_CONTENT_OR:
4547 if (ret == 1) {
4548 DEBUG_VALID_MSG("Or succeeded");
4549 CONT = CONT->parent;
4550 DEPTH--;
4551 } else {
4552 DEBUG_VALID_MSG("Or failed");
4553 CONT = CONT->parent;
4554 DEPTH--;
4555 }
4556 break;
4557 case XML_ELEMENT_CONTENT_SEQ:
4558 if (ret == 0) {
4559 DEBUG_VALID_MSG("Sequence failed");
4560 CONT = CONT->parent;
4561 DEPTH--;
4562 } else if (CONT == CONT->parent->c1) {
4563 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4564 CONT = CONT->parent->c2;
4565 goto cont;
4566 } else {
4567 DEBUG_VALID_MSG("Sequence succeeded");
4568 CONT = CONT->parent;
4569 DEPTH--;
4570 }
4571 }
4572 }
4573 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004574 xmlNodePtr cur;
4575
4576 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004577 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4578 if (vstateVPop(ctxt) < 0 ) {
4579 DEBUG_VALID_MSG("exhaustion, failed");
4580 return(0);
4581 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004582 if (cur != ctxt->vstate->node)
4583 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004584 goto cont;
4585 }
4586 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004587 xmlNodePtr cur;
4588
4589 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004590 DEBUG_VALID_MSG("Failure, rollback");
4591 if (vstateVPop(ctxt) < 0 ) {
4592 DEBUG_VALID_MSG("exhaustion, failed");
4593 return(0);
4594 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004595 if (cur != ctxt->vstate->node)
4596 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004597 goto cont;
4598 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004599 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004600}
Daniel Veillard23e73572002-09-19 19:56:43 +00004601#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004602
4603/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00004604 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004605 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00004606 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004607 * @content: An element
4608 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
4609 *
4610 * This will dump the list of elements to the buffer
4611 * Intended just for the debug routine
4612 */
4613static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00004614xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004615 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00004616 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004617
4618 if (node == NULL) return;
4619 if (glob) strcat(buf, "(");
4620 cur = node;
4621 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004622 len = strlen(buf);
4623 if (size - len < 50) {
4624 if ((size - len > 4) && (buf[len - 1] != '.'))
4625 strcat(buf, " ...");
4626 return;
4627 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004628 switch (cur->type) {
4629 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004630 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004631 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004632 if ((size - len > 4) && (buf[len - 1] != '.'))
4633 strcat(buf, " ...");
4634 return;
4635 }
4636 strcat(buf, (char *) cur->ns->prefix);
4637 strcat(buf, ":");
4638 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004639 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004640 if ((size - len > 4) && (buf[len - 1] != '.'))
4641 strcat(buf, " ...");
4642 return;
4643 }
4644 strcat(buf, (char *) cur->name);
4645 if (cur->next != NULL)
4646 strcat(buf, " ");
4647 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004648 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004649 if (xmlIsBlankNode(cur))
4650 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004651 case XML_CDATA_SECTION_NODE:
4652 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004653 strcat(buf, "CDATA");
4654 if (cur->next != NULL)
4655 strcat(buf, " ");
4656 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004657 case XML_ATTRIBUTE_NODE:
4658 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004659#ifdef LIBXML_DOCB_ENABLED
4660 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004661#endif
4662 case XML_HTML_DOCUMENT_NODE:
4663 case XML_DOCUMENT_TYPE_NODE:
4664 case XML_DOCUMENT_FRAG_NODE:
4665 case XML_NOTATION_NODE:
4666 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004667 strcat(buf, "???");
4668 if (cur->next != NULL)
4669 strcat(buf, " ");
4670 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004671 case XML_ENTITY_NODE:
4672 case XML_PI_NODE:
4673 case XML_DTD_NODE:
4674 case XML_COMMENT_NODE:
4675 case XML_ELEMENT_DECL:
4676 case XML_ATTRIBUTE_DECL:
4677 case XML_ENTITY_DECL:
4678 case XML_XINCLUDE_START:
4679 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004680 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004681 }
4682 cur = cur->next;
4683 }
4684 if (glob) strcat(buf, ")");
4685}
4686
4687/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004688 * xmlValidateElementContent:
4689 * @ctxt: the validation context
4690 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004691 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004692 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004693 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004694 *
4695 * Try to validate the content model of an element
4696 *
4697 * returns 1 if valid or 0 if not and -1 in case of error
4698 */
4699
4700static int
4701xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004702 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004703 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00004704#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00004705 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00004706#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00004707 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004708 xmlElementContentPtr cont;
4709 const xmlChar *name;
4710
4711 if (elemDecl == NULL)
4712 return(-1);
4713 cont = elemDecl->content;
4714 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004715
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004716#ifdef LIBXML_REGEXP_ENABLED
4717 /* Build the regexp associated to the content model */
4718 if (elemDecl->contModel == NULL)
4719 ret = xmlValidBuildContentModel(ctxt, elemDecl);
4720 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004721 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004722 } else {
4723 xmlRegExecCtxtPtr exec;
4724
Daniel Veillardec498e12003-02-05 11:01:50 +00004725 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
4726 return(-1);
4727 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004728 ctxt->nodeMax = 0;
4729 ctxt->nodeNr = 0;
4730 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004731 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
4732 if (exec != NULL) {
4733 cur = child;
4734 while (cur != NULL) {
4735 switch (cur->type) {
4736 case XML_ENTITY_REF_NODE:
4737 /*
4738 * Push the current node to be able to roll back
4739 * and process within the entity
4740 */
4741 if ((cur->children != NULL) &&
4742 (cur->children->children != NULL)) {
4743 nodeVPush(ctxt, cur);
4744 cur = cur->children->children;
4745 continue;
4746 }
4747 break;
4748 case XML_TEXT_NODE:
4749 if (xmlIsBlankNode(cur))
4750 break;
4751 ret = 0;
4752 goto fail;
4753 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00004754 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004755 ret = 0;
4756 goto fail;
4757 case XML_ELEMENT_NODE:
4758 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004759 xmlChar fn[50];
4760 xmlChar *fullname;
4761
4762 fullname = xmlBuildQName(cur->name,
4763 cur->ns->prefix, fn, 50);
4764 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004765 ret = -1;
4766 goto fail;
4767 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004768 ret = xmlRegExecPushString(exec, fullname, NULL);
4769 if ((fullname != fn) && (fullname != cur->name))
4770 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004771 } else {
4772 ret = xmlRegExecPushString(exec, cur->name, NULL);
4773 }
4774 break;
4775 default:
4776 break;
4777 }
4778 /*
4779 * Switch to next element
4780 */
4781 cur = cur->next;
4782 while (cur == NULL) {
4783 cur = nodeVPop(ctxt);
4784 if (cur == NULL)
4785 break;
4786 cur = cur->next;
4787 }
4788 }
4789 ret = xmlRegExecPushString(exec, NULL, NULL);
4790fail:
4791 xmlRegFreeExecCtxt(exec);
4792 }
4793 }
4794#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004795 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004796 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004797 */
4798 ctxt->vstateMax = 8;
4799 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4800 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4801 if (ctxt->vstateTab == NULL) {
4802 xmlGenericError(xmlGenericErrorContext,
4803 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004804 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004805 }
4806 /*
4807 * The first entry in the stack is reserved to the current state
4808 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004809 ctxt->nodeMax = 0;
4810 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004811 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004812 ctxt->vstate = &ctxt->vstateTab[0];
4813 ctxt->vstateNr = 1;
4814 CONT = cont;
4815 NODE = child;
4816 DEPTH = 0;
4817 OCCURS = 0;
4818 STATE = 0;
4819 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004820 if ((ret == -3) && (warn)) {
4821 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004822 "Content model for Element %s is ambiguous\n", name);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004823 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004824 /*
4825 * An entities reference appeared at this level.
4826 * Buid a minimal representation of this node content
4827 * sufficient to run the validation process on it
4828 */
4829 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004830 cur = child;
4831 while (cur != NULL) {
4832 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004833 case XML_ENTITY_REF_NODE:
4834 /*
4835 * Push the current node to be able to roll back
4836 * and process within the entity
4837 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004838 if ((cur->children != NULL) &&
4839 (cur->children->children != NULL)) {
4840 nodeVPush(ctxt, cur);
4841 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004842 continue;
4843 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004844 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004845 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004846 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004847 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004848 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004849 case XML_CDATA_SECTION_NODE:
4850 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004851 case XML_ELEMENT_NODE:
4852 /*
4853 * Allocate a new node and minimally fills in
4854 * what's required
4855 */
4856 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4857 if (tmp == NULL) {
4858 xmlGenericError(xmlGenericErrorContext,
4859 "xmlValidateElementContent : malloc failed\n");
4860 xmlFreeNodeList(repl);
4861 ret = -1;
4862 goto done;
4863 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004864 tmp->type = cur->type;
4865 tmp->name = cur->name;
4866 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004867 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004868 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004869 if (repl == NULL)
4870 repl = last = tmp;
4871 else {
4872 last->next = tmp;
4873 last = tmp;
4874 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004875 if (cur->type == XML_CDATA_SECTION_NODE) {
4876 /*
4877 * E59 spaces in CDATA does not match the
4878 * nonterminal S
4879 */
4880 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4881 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004882 break;
4883 default:
4884 break;
4885 }
4886 /*
4887 * Switch to next element
4888 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004889 cur = cur->next;
4890 while (cur == NULL) {
4891 cur = nodeVPop(ctxt);
4892 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004893 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004894 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004895 }
4896 }
4897
4898 /*
4899 * Relaunch the validation
4900 */
4901 ctxt->vstate = &ctxt->vstateTab[0];
4902 ctxt->vstateNr = 1;
4903 CONT = cont;
4904 NODE = repl;
4905 DEPTH = 0;
4906 OCCURS = 0;
4907 STATE = 0;
4908 ret = xmlValidateElementType(ctxt);
4909 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004910#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004911 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004912 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4913 char expr[5000];
4914 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004915
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004916 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004917 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004918 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00004919#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004920 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004921 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004922 else
Daniel Veillard01992e02002-10-09 10:20:30 +00004923#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004924 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004925
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004926 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004927 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004928 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004929 "Element %s content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004930 name, expr, list);
4931 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004932 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004933 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004934 "Element content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004935 expr, list);
4936 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004937 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004938 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004939 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004940 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004941 "Element %s content does not follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004942 name);
4943 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004944 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004945 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004946 "Element content does not follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004947 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004948 }
4949 ret = 0;
4950 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004951 if (ret == -3)
4952 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004953
Daniel Veillard23e73572002-09-19 19:56:43 +00004954#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004955done:
4956 /*
4957 * Deallocate the copy if done, and free up the validation stack
4958 */
4959 while (repl != NULL) {
4960 tmp = repl->next;
4961 xmlFree(repl);
4962 repl = tmp;
4963 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004964 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004965 if (ctxt->vstateTab != NULL) {
4966 xmlFree(ctxt->vstateTab);
4967 ctxt->vstateTab = NULL;
4968 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004969#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004970 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004971 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004972 if (ctxt->nodeTab != NULL) {
4973 xmlFree(ctxt->nodeTab);
4974 ctxt->nodeTab = NULL;
4975 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004976 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004977
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004978}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004979
Owen Taylor3473f882001-02-23 17:55:21 +00004980/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004981 * xmlValidateCdataElement:
4982 * @ctxt: the validation context
4983 * @doc: a document instance
4984 * @elem: an element instance
4985 *
4986 * Check that an element follows #CDATA
4987 *
4988 * returns 1 if valid or 0 otherwise
4989 */
4990static int
4991xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4992 xmlNodePtr elem) {
4993 int ret = 1;
4994 xmlNodePtr cur, child;
4995
Daniel Veillardceb09b92002-10-04 11:46:37 +00004996 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004997 return(0);
4998
4999 child = elem->children;
5000
5001 cur = child;
5002 while (cur != NULL) {
5003 switch (cur->type) {
5004 case XML_ENTITY_REF_NODE:
5005 /*
5006 * Push the current node to be able to roll back
5007 * and process within the entity
5008 */
5009 if ((cur->children != NULL) &&
5010 (cur->children->children != NULL)) {
5011 nodeVPush(ctxt, cur);
5012 cur = cur->children->children;
5013 continue;
5014 }
5015 break;
5016 case XML_COMMENT_NODE:
5017 case XML_PI_NODE:
5018 case XML_TEXT_NODE:
5019 case XML_CDATA_SECTION_NODE:
5020 break;
5021 default:
5022 ret = 0;
5023 goto done;
5024 }
5025 /*
5026 * Switch to next element
5027 */
5028 cur = cur->next;
5029 while (cur == NULL) {
5030 cur = nodeVPop(ctxt);
5031 if (cur == NULL)
5032 break;
5033 cur = cur->next;
5034 }
5035 }
5036done:
5037 ctxt->nodeMax = 0;
5038 ctxt->nodeNr = 0;
5039 if (ctxt->nodeTab != NULL) {
5040 xmlFree(ctxt->nodeTab);
5041 ctxt->nodeTab = NULL;
5042 }
5043 return(ret);
5044}
5045
5046/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005047 * xmlValidateCheckMixed:
5048 * @ctxt: the validation context
5049 * @cont: the mixed content model
5050 * @qname: the qualified name as appearing in the serialization
5051 *
5052 * Check if the given node is part of the content model.
5053 *
5054 * Returns 1 if yes, 0 if no, -1 in case of error
5055 */
5056static int
5057xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
5058 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005059 const xmlChar *name;
5060 int plen;
5061 name = xmlSplitQName3(qname, &plen);
5062
5063 if (name == NULL) {
5064 while (cont != NULL) {
5065 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5066 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5067 return(1);
5068 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5069 (cont->c1 != NULL) &&
5070 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5071 if ((cont->c1->prefix == NULL) &&
5072 (xmlStrEqual(cont->c1->name, qname)))
5073 return(1);
5074 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5075 (cont->c1 == NULL) ||
5076 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5077 /* Internal error !!! */
5078 xmlGenericError(xmlGenericErrorContext,
5079 "Internal: MIXED struct bad\n");
5080 break;
5081 }
5082 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005083 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005084 } else {
5085 while (cont != NULL) {
5086 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5087 if ((cont->prefix != NULL) &&
5088 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5089 (xmlStrEqual(cont->name, name)))
5090 return(1);
5091 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5092 (cont->c1 != NULL) &&
5093 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5094 if ((cont->c1->prefix != NULL) &&
5095 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5096 (xmlStrEqual(cont->c1->name, name)))
5097 return(1);
5098 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5099 (cont->c1 == NULL) ||
5100 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5101 /* Internal error !!! */
5102 xmlGenericError(xmlGenericErrorContext,
5103 "Internal: MIXED struct bad\n");
5104 break;
5105 }
5106 cont = cont->c2;
5107 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005108 }
5109 return(0);
5110}
5111
5112/**
5113 * xmlValidGetElemDecl:
5114 * @ctxt: the validation context
5115 * @doc: a document instance
5116 * @elem: an element instance
5117 * @extsubset: pointer, (out) indicate if the declaration was found
5118 * in the external subset.
5119 *
5120 * Finds a declaration associated to an element in the document.
5121 *
5122 * returns the pointer to the declaration or NULL if not found.
5123 */
5124static xmlElementPtr
5125xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5126 xmlNodePtr elem, int *extsubset) {
5127 xmlElementPtr elemDecl = NULL;
5128 const xmlChar *prefix = NULL;
5129
5130 if ((elem == NULL) || (elem->name == NULL)) return(NULL);
5131 if (extsubset != NULL)
5132 *extsubset = 0;
5133
5134 /*
5135 * Fetch the declaration for the qualified name
5136 */
5137 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5138 prefix = elem->ns->prefix;
5139
5140 if (prefix != NULL) {
5141 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5142 elem->name, prefix);
5143 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5144 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5145 elem->name, prefix);
5146 if ((elemDecl != NULL) && (extsubset != NULL))
5147 *extsubset = 1;
5148 }
5149 }
5150
5151 /*
5152 * Fetch the declaration for the non qualified name
5153 * This is "non-strict" validation should be done on the
5154 * full QName but in that case being flexible makes sense.
5155 */
5156 if (elemDecl == NULL) {
5157 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5158 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5159 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5160 if ((elemDecl != NULL) && (extsubset != NULL))
5161 *extsubset = 1;
5162 }
5163 }
5164 if (elemDecl == NULL) {
5165 VECTXT(ctxt, elem);
5166 VERROR(ctxt->userData, "No declaration for element %s\n",
5167 elem->name);
5168 }
5169 return(elemDecl);
5170}
5171
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005172#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005173/**
5174 * xmlValidatePushElement:
5175 * @ctxt: the validation context
5176 * @doc: a document instance
5177 * @elem: an element instance
5178 * @qname: the qualified name as appearing in the serialization
5179 *
5180 * Push a new element start on the validation stack.
5181 *
5182 * returns 1 if no validation problem was found or 0 otherwise
5183 */
5184int
5185xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5186 xmlNodePtr elem, const xmlChar *qname) {
5187 int ret = 1;
5188 xmlElementPtr eDecl;
5189 int extsubset = 0;
5190
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005191/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005192 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5193 xmlValidStatePtr state = ctxt->vstate;
5194 xmlElementPtr elemDecl;
5195
5196 /*
5197 * Check the new element agaisnt the content model of the new elem.
5198 */
5199 if (state->elemDecl != NULL) {
5200 elemDecl = state->elemDecl;
5201
5202 switch(elemDecl->etype) {
5203 case XML_ELEMENT_TYPE_UNDEFINED:
5204 ret = 0;
5205 break;
5206 case XML_ELEMENT_TYPE_EMPTY:
5207 VECTXT(ctxt, state->node);
5208 VERROR(ctxt->userData,
5209 "Element %s was declared EMPTY this one has content\n",
5210 state->node->name);
5211 ret = 0;
5212 break;
5213 case XML_ELEMENT_TYPE_ANY:
5214 /* I don't think anything is required then */
5215 break;
5216 case XML_ELEMENT_TYPE_MIXED:
5217 /* simple case of declared as #PCDATA */
5218 if ((elemDecl->content != NULL) &&
5219 (elemDecl->content->type ==
5220 XML_ELEMENT_CONTENT_PCDATA)) {
5221 VECTXT(ctxt, state->node);
5222 VERROR(ctxt->userData,
5223 "Element %s was declared #PCDATA but contains non text nodes\n",
5224 state->node->name);
5225 ret = 0;
5226 } else {
5227 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5228 qname);
5229 if (ret != 1) {
5230 VECTXT(ctxt, state->node);
5231 VERROR(ctxt->userData,
5232 "Element %s is not declared in %s list of possible children\n",
5233 qname, state->node->name);
5234 }
5235 }
5236 break;
5237 case XML_ELEMENT_TYPE_ELEMENT:
5238 /*
5239 * TODO:
5240 * VC: Standalone Document Declaration
5241 * - element types with element content, if white space
5242 * occurs directly within any instance of those types.
5243 */
5244 if (state->exec != NULL) {
5245 ret = xmlRegExecPushString(state->exec, qname, NULL);
5246 if (ret < 0) {
5247 VECTXT(ctxt, state->node);
5248 VERROR(ctxt->userData,
5249 "Element %s content does not follow the DTD\nMisplaced %s\n",
5250 state->node->name, qname);
5251 ret = 0;
5252 } else {
5253 ret = 1;
5254 }
5255 }
5256 break;
5257 }
5258 }
5259 }
5260 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5261 vstateVPush(ctxt, eDecl, elem);
5262 return(ret);
5263}
5264
5265/**
5266 * xmlValidatePushCData:
5267 * @ctxt: the validation context
5268 * @data: some character data read
5269 * @len: the lenght of the data
5270 *
5271 * check the CData parsed for validation in the current stack
5272 *
5273 * returns 1 if no validation problem was found or 0 otherwise
5274 */
5275int
5276xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5277 int ret = 1;
5278
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005279/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005280 if (len <= 0)
5281 return(ret);
5282 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5283 xmlValidStatePtr state = ctxt->vstate;
5284 xmlElementPtr elemDecl;
5285
5286 /*
5287 * Check the new element agaisnt the content model of the new elem.
5288 */
5289 if (state->elemDecl != NULL) {
5290 elemDecl = state->elemDecl;
5291
5292 switch(elemDecl->etype) {
5293 case XML_ELEMENT_TYPE_UNDEFINED:
5294 ret = 0;
5295 break;
5296 case XML_ELEMENT_TYPE_EMPTY:
5297 VECTXT(ctxt, state->node);
5298 VERROR(ctxt->userData,
5299 "Element %s was declared EMPTY this one has content\n",
5300 state->node->name);
5301 ret = 0;
5302 break;
5303 case XML_ELEMENT_TYPE_ANY:
5304 break;
5305 case XML_ELEMENT_TYPE_MIXED:
5306 break;
5307 case XML_ELEMENT_TYPE_ELEMENT:
5308 if (len > 0) {
5309 int i;
5310
5311 for (i = 0;i < len;i++) {
5312 if (!IS_BLANK(data[i])) {
5313 VECTXT(ctxt, state->node);
5314 VERROR(ctxt->userData,
5315 "Element %s content does not follow the DTD\nText not allowed\n",
5316 state->node->name);
5317 ret = 0;
5318 goto done;
5319 }
5320 }
5321 /*
5322 * TODO:
5323 * VC: Standalone Document Declaration
5324 * element types with element content, if white space
5325 * occurs directly within any instance of those types.
5326 */
5327 }
5328 break;
5329 }
5330 }
5331 }
5332done:
5333 return(ret);
5334}
5335
5336/**
5337 * xmlValidatePopElement:
5338 * @ctxt: the validation context
5339 * @doc: a document instance
5340 * @elem: an element instance
5341 * @qname: the qualified name as appearing in the serialization
5342 *
5343 * Pop the element end from the validation stack.
5344 *
5345 * returns 1 if no validation problem was found or 0 otherwise
5346 */
5347int
5348xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005349 xmlNodePtr elem ATTRIBUTE_UNUSED,
5350 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005351 int ret = 1;
5352
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005353/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005354 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5355 xmlValidStatePtr state = ctxt->vstate;
5356 xmlElementPtr elemDecl;
5357
5358 /*
5359 * Check the new element agaisnt the content model of the new elem.
5360 */
5361 if (state->elemDecl != NULL) {
5362 elemDecl = state->elemDecl;
5363
5364 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5365 if (state->exec != NULL) {
5366 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5367 if (ret == 0) {
5368 VECTXT(ctxt, state->node);
5369 VERROR(ctxt->userData,
5370 "Element %s content does not follow the DTD\nExpecting more child\n",
5371 state->node->name);
5372 } else {
5373 /*
5374 * previous validation errors should not generate
5375 * a new one here
5376 */
5377 ret = 1;
5378 }
5379 }
5380 }
5381 }
5382 vstateVPop(ctxt);
5383 }
5384 return(ret);
5385}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005386#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005387
5388/**
Owen Taylor3473f882001-02-23 17:55:21 +00005389 * xmlValidateOneElement:
5390 * @ctxt: the validation context
5391 * @doc: a document instance
5392 * @elem: an element instance
5393 *
5394 * Try to validate a single element and it's attributes,
5395 * basically it does the following checks as described by the
5396 * XML-1.0 recommendation:
5397 * - [ VC: Element Valid ]
5398 * - [ VC: Required Attribute ]
5399 * Then call xmlValidateOneAttribute() for each attribute present.
5400 *
5401 * The ID/IDREF checkings are done separately
5402 *
5403 * returns 1 if valid or 0 otherwise
5404 */
5405
5406int
5407xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5408 xmlNodePtr elem) {
5409 xmlElementPtr elemDecl = NULL;
5410 xmlElementContentPtr cont;
5411 xmlAttributePtr attr;
5412 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005413 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005414 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005415 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005416
5417 CHECK_DTD;
5418
5419 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005420 switch (elem->type) {
5421 case XML_ATTRIBUTE_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005422 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005423 VERROR(ctxt->userData,
5424 "Attribute element not expected here\n");
5425 return(0);
5426 case XML_TEXT_NODE:
5427 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005428 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005429 VERROR(ctxt->userData, "Text element has childs !\n");
5430 return(0);
5431 }
5432 if (elem->properties != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005433 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005434 VERROR(ctxt->userData, "Text element has attributes !\n");
5435 return(0);
5436 }
5437 if (elem->ns != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005438 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005439 VERROR(ctxt->userData, "Text element has namespace !\n");
5440 return(0);
5441 }
5442 if (elem->nsDef != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005443 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005444 VERROR(ctxt->userData,
5445 "Text element carries namespace definitions !\n");
5446 return(0);
5447 }
5448 if (elem->content == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005449 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005450 VERROR(ctxt->userData,
5451 "Text element has no content !\n");
5452 return(0);
5453 }
5454 return(1);
5455 case XML_XINCLUDE_START:
5456 case XML_XINCLUDE_END:
5457 return(1);
5458 case XML_CDATA_SECTION_NODE:
5459 case XML_ENTITY_REF_NODE:
5460 case XML_PI_NODE:
5461 case XML_COMMENT_NODE:
5462 return(1);
5463 case XML_ENTITY_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005464 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005465 VERROR(ctxt->userData,
5466 "Entity element not expected here\n");
5467 return(0);
5468 case XML_NOTATION_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005469 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005470 VERROR(ctxt->userData,
5471 "Notation element not expected here\n");
5472 return(0);
5473 case XML_DOCUMENT_NODE:
5474 case XML_DOCUMENT_TYPE_NODE:
5475 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005476 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005477 VERROR(ctxt->userData,
5478 "Document element not expected here\n");
5479 return(0);
5480 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005481 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005482 VERROR(ctxt->userData,
5483 "\n");
5484 return(0);
5485 case XML_ELEMENT_NODE:
5486 break;
5487 default:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005488 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005489 VERROR(ctxt->userData,
5490 "unknown element type %d\n", elem->type);
5491 return(0);
5492 }
Owen Taylor3473f882001-02-23 17:55:21 +00005493
5494 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005495 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005496 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005497 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5498 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005499 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005500
Daniel Veillardea7751d2002-12-20 00:16:24 +00005501 /*
5502 * If vstateNr is not zero that means continuous validation is
5503 * activated, do not try to check the content model at that level.
5504 */
5505 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005506 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005507 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005508 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005509 VECTXT(ctxt, elem);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005510 VERROR(ctxt->userData, "No declaration for element %s\n",
5511 elem->name);
5512 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005513 case XML_ELEMENT_TYPE_EMPTY:
5514 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005515 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005516 VERROR(ctxt->userData,
5517 "Element %s was declared EMPTY this one has content\n",
5518 elem->name);
5519 ret = 0;
5520 }
5521 break;
5522 case XML_ELEMENT_TYPE_ANY:
5523 /* I don't think anything is required then */
5524 break;
5525 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005526
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005527 /* simple case of declared as #PCDATA */
5528 if ((elemDecl->content != NULL) &&
5529 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5530 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5531 if (!ret) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005532 VECTXT(ctxt, elem);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005533 VERROR(ctxt->userData,
5534 "Element %s was declared #PCDATA but contains non text nodes\n",
5535 elem->name);
5536 }
5537 break;
5538 }
Owen Taylor3473f882001-02-23 17:55:21 +00005539 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005540 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005541 while (child != NULL) {
5542 if (child->type == XML_ELEMENT_NODE) {
5543 name = child->name;
5544 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005545 xmlChar fn[50];
5546 xmlChar *fullname;
5547
5548 fullname = xmlBuildQName(child->name, child->ns->prefix,
5549 fn, 50);
5550 if (fullname == NULL)
5551 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005552 cont = elemDecl->content;
5553 while (cont != NULL) {
5554 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005555 if (xmlStrEqual(cont->name, fullname))
5556 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005557 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5558 (cont->c1 != NULL) &&
5559 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005560 if (xmlStrEqual(cont->c1->name, fullname))
5561 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005562 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5563 (cont->c1 == NULL) ||
5564 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5565 /* Internal error !!! */
5566 xmlGenericError(xmlGenericErrorContext,
5567 "Internal: MIXED struct bad\n");
5568 break;
5569 }
5570 cont = cont->c2;
5571 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005572 if ((fullname != fn) && (fullname != child->name))
5573 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005574 if (cont != NULL)
5575 goto child_ok;
5576 }
5577 cont = elemDecl->content;
5578 while (cont != NULL) {
5579 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5580 if (xmlStrEqual(cont->name, name)) break;
5581 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5582 (cont->c1 != NULL) &&
5583 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5584 if (xmlStrEqual(cont->c1->name, name)) break;
5585 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5586 (cont->c1 == NULL) ||
5587 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
5588 /* Internal error !!! */
5589 xmlGenericError(xmlGenericErrorContext,
5590 "Internal: MIXED struct bad\n");
5591 break;
5592 }
5593 cont = cont->c2;
5594 }
5595 if (cont == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005596 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005597 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005598 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005599 name, elem->name);
5600 ret = 0;
5601 }
5602 }
5603child_ok:
5604 child = child->next;
5605 }
5606 break;
5607 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005608 if ((doc->standalone == 1) && (extsubset == 1)) {
5609 /*
5610 * VC: Standalone Document Declaration
5611 * - element types with element content, if white space
5612 * occurs directly within any instance of those types.
5613 */
5614 child = elem->children;
5615 while (child != NULL) {
5616 if (child->type == XML_TEXT_NODE) {
5617 const xmlChar *content = child->content;
5618
5619 while (IS_BLANK(*content))
5620 content++;
5621 if (*content == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005622 VECTXT(ctxt, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005623 VERROR(ctxt->userData,
5624"standalone: %s declared in the external subset contains white spaces nodes\n",
5625 elem->name);
5626 ret = 0;
5627 break;
5628 }
5629 }
5630 child =child->next;
5631 }
5632 }
Owen Taylor3473f882001-02-23 17:55:21 +00005633 child = elem->children;
5634 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005635 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005636 if (tmp <= 0)
5637 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005638 break;
5639 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005640 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00005641
5642 /* [ VC: Required Attribute ] */
5643 attr = elemDecl->attributes;
5644 while (attr != NULL) {
5645 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005646 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00005647
Daniel Veillarde4301c82002-02-13 13:32:35 +00005648 if ((attr->prefix == NULL) &&
5649 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5650 xmlNsPtr ns;
5651
5652 ns = elem->nsDef;
5653 while (ns != NULL) {
5654 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005655 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005656 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00005657 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005658 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5659 xmlNsPtr ns;
5660
5661 ns = elem->nsDef;
5662 while (ns != NULL) {
5663 if (xmlStrEqual(attr->name, ns->prefix))
5664 goto found;
5665 ns = ns->next;
5666 }
5667 } else {
5668 xmlAttrPtr attrib;
5669
5670 attrib = elem->properties;
5671 while (attrib != NULL) {
5672 if (xmlStrEqual(attrib->name, attr->name)) {
5673 if (attr->prefix != NULL) {
5674 xmlNsPtr nameSpace = attrib->ns;
5675
5676 if (nameSpace == NULL)
5677 nameSpace = elem->ns;
5678 /*
5679 * qualified names handling is problematic, having a
5680 * different prefix should be possible but DTDs don't
5681 * allow to define the URI instead of the prefix :-(
5682 */
5683 if (nameSpace == NULL) {
5684 if (qualified < 0)
5685 qualified = 0;
5686 } else if (!xmlStrEqual(nameSpace->prefix,
5687 attr->prefix)) {
5688 if (qualified < 1)
5689 qualified = 1;
5690 } else
5691 goto found;
5692 } else {
5693 /*
5694 * We should allow applications to define namespaces
5695 * for their application even if the DTD doesn't
5696 * carry one, otherwise, basically we would always
5697 * break.
5698 */
5699 goto found;
5700 }
5701 }
5702 attrib = attrib->next;
5703 }
Owen Taylor3473f882001-02-23 17:55:21 +00005704 }
5705 if (qualified == -1) {
5706 if (attr->prefix == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005707 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005708 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005709 "Element %s does not carry attribute %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005710 elem->name, attr->name);
5711 ret = 0;
5712 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005713 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005714 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005715 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005716 elem->name, attr->prefix,attr->name);
5717 ret = 0;
5718 }
5719 } else if (qualified == 0) {
5720 VWARNING(ctxt->userData,
5721 "Element %s required attribute %s:%s has no prefix\n",
5722 elem->name, attr->prefix,attr->name);
5723 } else if (qualified == 1) {
5724 VWARNING(ctxt->userData,
5725 "Element %s required attribute %s:%s has different prefix\n",
5726 elem->name, attr->prefix,attr->name);
5727 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005728 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
5729 /*
5730 * Special tests checking #FIXED namespace declarations
5731 * have the right value since this is not done as an
5732 * attribute checking
5733 */
5734 if ((attr->prefix == NULL) &&
5735 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5736 xmlNsPtr ns;
5737
5738 ns = elem->nsDef;
5739 while (ns != NULL) {
5740 if (ns->prefix == NULL) {
5741 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005742 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005743 VERROR(ctxt->userData,
5744 "Element %s namespace name for default namespace does not match the DTD\n",
5745 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00005746 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005747 }
5748 goto found;
5749 }
5750 ns = ns->next;
5751 }
5752 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5753 xmlNsPtr ns;
5754
5755 ns = elem->nsDef;
5756 while (ns != NULL) {
5757 if (xmlStrEqual(attr->name, ns->prefix)) {
5758 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005759 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005760 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005761 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillarde4301c82002-02-13 13:32:35 +00005762 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00005763 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005764 }
5765 goto found;
5766 }
5767 ns = ns->next;
5768 }
5769 }
Owen Taylor3473f882001-02-23 17:55:21 +00005770 }
5771found:
5772 attr = attr->nexth;
5773 }
5774 return(ret);
5775}
5776
5777/**
5778 * xmlValidateRoot:
5779 * @ctxt: the validation context
5780 * @doc: a document instance
5781 *
5782 * Try to validate a the root element
5783 * basically it does the following check as described by the
5784 * XML-1.0 recommendation:
5785 * - [ VC: Root Element Type ]
5786 * it doesn't try to recurse or apply other check to the element
5787 *
5788 * returns 1 if valid or 0 otherwise
5789 */
5790
5791int
5792xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5793 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00005794 int ret;
5795
Owen Taylor3473f882001-02-23 17:55:21 +00005796 if (doc == NULL) return(0);
5797
5798 root = xmlDocGetRootElement(doc);
5799 if ((root == NULL) || (root->name == NULL)) {
5800 VERROR(ctxt->userData, "Not valid: no root element\n");
5801 return(0);
5802 }
5803
5804 /*
5805 * When doing post validation against a separate DTD, those may
5806 * no internal subset has been generated
5807 */
5808 if ((doc->intSubset != NULL) &&
5809 (doc->intSubset->name != NULL)) {
5810 /*
5811 * Check first the document root against the NQName
5812 */
5813 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
5814 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005815 xmlChar fn[50];
5816 xmlChar *fullname;
5817
5818 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
5819 if (fullname == NULL) {
5820 VERROR(ctxt->userData, "Out of memory\n");
5821 return(0);
5822 }
5823 ret = xmlStrEqual(doc->intSubset->name, fullname);
5824 if ((fullname != fn) && (fullname != root->name))
5825 xmlFree(fullname);
5826 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00005827 goto name_ok;
5828 }
5829 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
5830 (xmlStrEqual(root->name, BAD_CAST "html")))
5831 goto name_ok;
Daniel Veillard76575762002-09-05 14:21:15 +00005832 VECTXT(ctxt, root);
Owen Taylor3473f882001-02-23 17:55:21 +00005833 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005834 "Not valid: root and DTD name do not match '%s' and '%s'\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005835 root->name, doc->intSubset->name);
5836 return(0);
5837
5838 }
5839 }
5840name_ok:
5841 return(1);
5842}
5843
5844
5845/**
5846 * xmlValidateElement:
5847 * @ctxt: the validation context
5848 * @doc: a document instance
5849 * @elem: an element instance
5850 *
5851 * Try to validate the subtree under an element
5852 *
5853 * returns 1 if valid or 0 otherwise
5854 */
5855
5856int
5857xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
5858 xmlNodePtr child;
5859 xmlAttrPtr attr;
5860 xmlChar *value;
5861 int ret = 1;
5862
5863 if (elem == NULL) return(0);
5864
5865 /*
5866 * XInclude elements were added after parsing in the infoset,
5867 * they don't really mean anything validation wise.
5868 */
5869 if ((elem->type == XML_XINCLUDE_START) ||
5870 (elem->type == XML_XINCLUDE_END))
5871 return(1);
5872
5873 CHECK_DTD;
5874
Daniel Veillard10ea86c2001-06-20 13:55:33 +00005875 /*
5876 * Entities references have to be handled separately
5877 */
5878 if (elem->type == XML_ENTITY_REF_NODE) {
5879 return(1);
5880 }
5881
Owen Taylor3473f882001-02-23 17:55:21 +00005882 ret &= xmlValidateOneElement(ctxt, doc, elem);
5883 attr = elem->properties;
5884 while(attr != NULL) {
5885 value = xmlNodeListGetString(doc, attr->children, 0);
5886 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
5887 if (value != NULL)
5888 xmlFree(value);
5889 attr= attr->next;
5890 }
5891 child = elem->children;
5892 while (child != NULL) {
5893 ret &= xmlValidateElement(ctxt, doc, child);
5894 child = child->next;
5895 }
5896
5897 return(ret);
5898}
5899
Daniel Veillard8730c562001-02-26 10:49:57 +00005900/**
5901 * xmlValidateRef:
5902 * @ref: A reference to be validated
5903 * @ctxt: Validation context
5904 * @name: Name of ID we are searching for
5905 *
5906 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005907static void
Daniel Veillard8730c562001-02-26 10:49:57 +00005908xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00005909 const xmlChar *name) {
5910 xmlAttrPtr id;
5911 xmlAttrPtr attr;
5912
5913 if (ref == NULL)
5914 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005915 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005916 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005917 attr = ref->attr;
5918 if (attr == NULL) {
5919 xmlChar *dup, *str = NULL, *cur, save;
5920
5921 dup = xmlStrdup(name);
5922 if (dup == NULL) {
5923 ctxt->valid = 0;
5924 return;
5925 }
5926 cur = dup;
5927 while (*cur != 0) {
5928 str = cur;
5929 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5930 save = *cur;
5931 *cur = 0;
5932 id = xmlGetID(ctxt->doc, str);
5933 if (id == NULL) {
5934 VERROR(ctxt->userData,
5935 "attribute %s line %d references an unknown ID \"%s\"\n",
5936 ref->name, ref->lineno, str);
5937 ctxt->valid = 0;
5938 }
5939 if (save == 0)
5940 break;
5941 *cur = save;
5942 while (IS_BLANK(*cur)) cur++;
5943 }
5944 xmlFree(dup);
5945 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00005946 id = xmlGetID(ctxt->doc, name);
5947 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005948 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005949 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005950 "IDREF attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005951 attr->name, name);
5952 ctxt->valid = 0;
5953 }
5954 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
5955 xmlChar *dup, *str = NULL, *cur, save;
5956
5957 dup = xmlStrdup(name);
5958 if (dup == NULL) {
5959 ctxt->valid = 0;
5960 return;
5961 }
5962 cur = dup;
5963 while (*cur != 0) {
5964 str = cur;
5965 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5966 save = *cur;
5967 *cur = 0;
5968 id = xmlGetID(ctxt->doc, str);
5969 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005970 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005971 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005972 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005973 attr->name, str);
5974 ctxt->valid = 0;
5975 }
5976 if (save == 0)
5977 break;
5978 *cur = save;
5979 while (IS_BLANK(*cur)) cur++;
5980 }
5981 xmlFree(dup);
5982 }
5983}
5984
5985/**
Daniel Veillard8730c562001-02-26 10:49:57 +00005986 * xmlWalkValidateList:
5987 * @data: Contents of current link
5988 * @user: Value supplied by the user
5989 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005990 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00005991 */
5992static int
5993xmlWalkValidateList(const void *data, const void *user)
5994{
5995 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
5996 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
5997 return 1;
5998}
5999
6000/**
6001 * xmlValidateCheckRefCallback:
6002 * @ref_list: List of references
6003 * @ctxt: Validation context
6004 * @name: Name of ID we are searching for
6005 *
6006 */
6007static void
6008xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6009 const xmlChar *name) {
6010 xmlValidateMemo memo;
6011
6012 if (ref_list == NULL)
6013 return;
6014 memo.ctxt = ctxt;
6015 memo.name = name;
6016
6017 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6018
6019}
6020
6021/**
Owen Taylor3473f882001-02-23 17:55:21 +00006022 * xmlValidateDocumentFinal:
6023 * @ctxt: the validation context
6024 * @doc: a document instance
6025 *
6026 * Does the final step for the document validation once all the
6027 * incremental validation steps have been completed
6028 *
6029 * basically it does the following checks described by the XML Rec
6030 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006031 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00006032 *
6033 * returns 1 if valid or 0 otherwise
6034 */
6035
6036int
6037xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6038 xmlRefTablePtr table;
6039
6040 if (doc == NULL) {
6041 xmlGenericError(xmlGenericErrorContext,
6042 "xmlValidateDocumentFinal: doc == NULL\n");
6043 return(0);
6044 }
6045
6046 /*
6047 * Check all the NOTATION/NOTATIONS attributes
6048 */
6049 /*
6050 * Check all the ENTITY/ENTITIES attributes definition for validity
6051 */
6052 /*
6053 * Check all the IDREF/IDREFS attributes definition for validity
6054 */
6055 table = (xmlRefTablePtr) doc->refs;
6056 ctxt->doc = doc;
6057 ctxt->valid = 1;
6058 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6059 return(ctxt->valid);
6060}
6061
6062/**
6063 * xmlValidateDtd:
6064 * @ctxt: the validation context
6065 * @doc: a document instance
6066 * @dtd: a dtd instance
6067 *
6068 * Try to validate the document against the dtd instance
6069 *
6070 * basically it does check all the definitions in the DtD.
6071 *
6072 * returns 1 if valid or 0 otherwise
6073 */
6074
6075int
6076xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6077 int ret;
6078 xmlDtdPtr oldExt;
6079 xmlNodePtr root;
6080
6081 if (dtd == NULL) return(0);
6082 if (doc == NULL) return(0);
6083 oldExt = doc->extSubset;
6084 doc->extSubset = dtd;
6085 ret = xmlValidateRoot(ctxt, doc);
6086 if (ret == 0) {
6087 doc->extSubset = oldExt;
6088 return(ret);
6089 }
6090 if (doc->ids != NULL) {
6091 xmlFreeIDTable(doc->ids);
6092 doc->ids = NULL;
6093 }
6094 if (doc->refs != NULL) {
6095 xmlFreeRefTable(doc->refs);
6096 doc->refs = NULL;
6097 }
6098 root = xmlDocGetRootElement(doc);
6099 ret = xmlValidateElement(ctxt, doc, root);
6100 ret &= xmlValidateDocumentFinal(ctxt, doc);
6101 doc->extSubset = oldExt;
6102 return(ret);
6103}
6104
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006105static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006106xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6107 const xmlChar *name ATTRIBUTE_UNUSED) {
6108 if (cur == NULL)
6109 return;
6110 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6111 xmlChar *notation = cur->content;
6112
Daniel Veillard878eab02002-02-19 13:46:09 +00006113 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006114 int ret;
6115
6116 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6117 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006118 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006119 }
6120 }
6121 }
6122}
6123
6124static void
Owen Taylor3473f882001-02-23 17:55:21 +00006125xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006126 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006127 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006128 xmlDocPtr doc;
6129 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006130
Owen Taylor3473f882001-02-23 17:55:21 +00006131 if (cur == NULL)
6132 return;
6133 switch (cur->atype) {
6134 case XML_ATTRIBUTE_CDATA:
6135 case XML_ATTRIBUTE_ID:
6136 case XML_ATTRIBUTE_IDREF :
6137 case XML_ATTRIBUTE_IDREFS:
6138 case XML_ATTRIBUTE_NMTOKEN:
6139 case XML_ATTRIBUTE_NMTOKENS:
6140 case XML_ATTRIBUTE_ENUMERATION:
6141 break;
6142 case XML_ATTRIBUTE_ENTITY:
6143 case XML_ATTRIBUTE_ENTITIES:
6144 case XML_ATTRIBUTE_NOTATION:
6145 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006146
6147 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6148 cur->atype, cur->defaultValue);
6149 if ((ret == 0) && (ctxt->valid == 1))
6150 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006151 }
6152 if (cur->tree != NULL) {
6153 xmlEnumerationPtr tree = cur->tree;
6154 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006155 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006156 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006157 if ((ret == 0) && (ctxt->valid == 1))
6158 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006159 tree = tree->next;
6160 }
6161 }
6162 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006163 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6164 doc = cur->doc;
6165 if ((doc == NULL) || (cur->elem == NULL)) {
6166 VERROR(ctxt->userData,
6167 "xmlValidateAttributeCallback(%s): internal error\n",
6168 cur->name);
6169 return;
6170 }
6171 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6172 if (elem == NULL)
6173 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
6174 if (elem == NULL) {
6175 VERROR(ctxt->userData,
6176 "attribute %s: could not find decl for element %s\n",
6177 cur->name, cur->elem);
6178 return;
6179 }
6180 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
6181 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006182 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillard878eab02002-02-19 13:46:09 +00006183 cur->name, cur->elem);
6184 ctxt->valid = 0;
6185 }
6186 }
Owen Taylor3473f882001-02-23 17:55:21 +00006187}
6188
6189/**
6190 * xmlValidateDtdFinal:
6191 * @ctxt: the validation context
6192 * @doc: a document instance
6193 *
6194 * Does the final step for the dtds validation once all the
6195 * subsets have been parsed
6196 *
6197 * basically it does the following checks described by the XML Rec
6198 * - check that ENTITY and ENTITIES type attributes default or
6199 * possible values matches one of the defined entities.
6200 * - check that NOTATION type attributes default or
6201 * possible values matches one of the defined notations.
6202 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006203 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006204 */
6205
6206int
6207xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006208 xmlDtdPtr dtd;
6209 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006210 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006211
6212 if (doc == NULL) return(0);
6213 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6214 return(0);
6215 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006216 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006217 dtd = doc->intSubset;
6218 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6219 table = (xmlAttributeTablePtr) dtd->attributes;
6220 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006221 }
6222 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006223 entities = (xmlEntitiesTablePtr) dtd->entities;
6224 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6225 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006226 }
6227 dtd = doc->extSubset;
6228 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6229 table = (xmlAttributeTablePtr) dtd->attributes;
6230 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006231 }
6232 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006233 entities = (xmlEntitiesTablePtr) dtd->entities;
6234 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6235 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006236 }
6237 return(ctxt->valid);
6238}
6239
6240/**
6241 * xmlValidateDocument:
6242 * @ctxt: the validation context
6243 * @doc: a document instance
6244 *
6245 * Try to validate the document instance
6246 *
6247 * basically it does the all the checks described by the XML Rec
6248 * i.e. validates the internal and external subset (if present)
6249 * and validate the document tree.
6250 *
6251 * returns 1 if valid or 0 otherwise
6252 */
6253
6254int
6255xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6256 int ret;
6257 xmlNodePtr root;
6258
Daniel Veillard2fd85422002-10-16 14:32:41 +00006259 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
6260 VERROR(ctxt->userData, "no DTD found!\n" );
Owen Taylor3473f882001-02-23 17:55:21 +00006261 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006262 }
Owen Taylor3473f882001-02-23 17:55:21 +00006263 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6264 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6265 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6266 doc->intSubset->SystemID);
6267 if (doc->extSubset == NULL) {
6268 if (doc->intSubset->SystemID != NULL) {
6269 VERROR(ctxt->userData,
6270 "Could not load the external subset \"%s\"\n",
6271 doc->intSubset->SystemID);
6272 } else {
6273 VERROR(ctxt->userData,
6274 "Could not load the external subset \"%s\"\n",
6275 doc->intSubset->ExternalID);
6276 }
6277 return(0);
6278 }
6279 }
6280
6281 if (doc->ids != NULL) {
6282 xmlFreeIDTable(doc->ids);
6283 doc->ids = NULL;
6284 }
6285 if (doc->refs != NULL) {
6286 xmlFreeRefTable(doc->refs);
6287 doc->refs = NULL;
6288 }
6289 ret = xmlValidateDtdFinal(ctxt, doc);
6290 if (!xmlValidateRoot(ctxt, doc)) return(0);
6291
6292 root = xmlDocGetRootElement(doc);
6293 ret &= xmlValidateElement(ctxt, doc, root);
6294 ret &= xmlValidateDocumentFinal(ctxt, doc);
6295 return(ret);
6296}
6297
6298
6299/************************************************************************
6300 * *
6301 * Routines for dynamic validation editing *
6302 * *
6303 ************************************************************************/
6304
6305/**
6306 * xmlValidGetPotentialChildren:
6307 * @ctree: an element content tree
6308 * @list: an array to store the list of child names
6309 * @len: a pointer to the number of element in the list
6310 * @max: the size of the array
6311 *
6312 * Build/extend a list of potential children allowed by the content tree
6313 *
6314 * returns the number of element in the list, or -1 in case of error.
6315 */
6316
6317int
6318xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6319 int *len, int max) {
6320 int i;
6321
6322 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6323 return(-1);
6324 if (*len >= max) return(*len);
6325
6326 switch (ctree->type) {
6327 case XML_ELEMENT_CONTENT_PCDATA:
6328 for (i = 0; i < *len;i++)
6329 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6330 list[(*len)++] = BAD_CAST "#PCDATA";
6331 break;
6332 case XML_ELEMENT_CONTENT_ELEMENT:
6333 for (i = 0; i < *len;i++)
6334 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6335 list[(*len)++] = ctree->name;
6336 break;
6337 case XML_ELEMENT_CONTENT_SEQ:
6338 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6339 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6340 break;
6341 case XML_ELEMENT_CONTENT_OR:
6342 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6343 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6344 break;
6345 }
6346
6347 return(*len);
6348}
6349
6350/**
6351 * xmlValidGetValidElements:
6352 * @prev: an element to insert after
6353 * @next: an element to insert next
6354 * @list: an array to store the list of child names
6355 * @max: the size of the array
6356 *
6357 * This function returns the list of authorized children to insert
6358 * within an existing tree while respecting the validity constraints
6359 * forced by the Dtd. The insertion point is defined using @prev and
6360 * @next in the following ways:
6361 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6362 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6363 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6364 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6365 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6366 *
6367 * pointers to the element names are inserted at the beginning of the array
6368 * and do not need to be freed.
6369 *
6370 * returns the number of element in the list, or -1 in case of error. If
6371 * the function returns the value @max the caller is invited to grow the
6372 * receiving array and retry.
6373 */
6374
6375int
6376xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
6377 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006378 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006379 int nb_valid_elements = 0;
6380 const xmlChar *elements[256];
6381 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006382 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006383
6384 xmlNode *ref_node;
6385 xmlNode *parent;
6386 xmlNode *test_node;
6387
6388 xmlNode *prev_next;
6389 xmlNode *next_prev;
6390 xmlNode *parent_childs;
6391 xmlNode *parent_last;
6392
6393 xmlElement *element_desc;
6394
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00006395 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006396
Owen Taylor3473f882001-02-23 17:55:21 +00006397 if (prev == NULL && next == NULL)
6398 return(-1);
6399
6400 if (list == NULL) return(-1);
6401 if (max <= 0) return(-1);
6402
6403 nb_valid_elements = 0;
6404 ref_node = prev ? prev : next;
6405 parent = ref_node->parent;
6406
6407 /*
6408 * Retrieves the parent element declaration
6409 */
6410 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6411 parent->name);
6412 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6413 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6414 parent->name);
6415 if (element_desc == NULL) return(-1);
6416
6417 /*
6418 * Do a backup of the current tree structure
6419 */
6420 prev_next = prev ? prev->next : NULL;
6421 next_prev = next ? next->prev : NULL;
6422 parent_childs = parent->children;
6423 parent_last = parent->last;
6424
6425 /*
6426 * Creates a dummy node and insert it into the tree
6427 */
6428 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
6429 test_node->doc = ref_node->doc;
6430 test_node->parent = parent;
6431 test_node->prev = prev;
6432 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006433 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006434
6435 if (prev) prev->next = test_node;
6436 else parent->children = test_node;
6437
6438 if (next) next->prev = test_node;
6439 else parent->last = test_node;
6440
6441 /*
6442 * Insert each potential child node and check if the parent is
6443 * still valid
6444 */
6445 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6446 elements, &nb_elements, 256);
6447
6448 for (i = 0;i < nb_elements;i++) {
6449 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006450 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006451 int j;
6452
6453 for (j = 0; j < nb_valid_elements;j++)
6454 if (xmlStrEqual(elements[i], list[j])) break;
6455 list[nb_valid_elements++] = elements[i];
6456 if (nb_valid_elements >= max) break;
6457 }
6458 }
6459
6460 /*
6461 * Restore the tree structure
6462 */
6463 if (prev) prev->next = prev_next;
6464 if (next) next->prev = next_prev;
6465 parent->children = parent_childs;
6466 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006467
6468 /*
6469 * Free up the dummy node
6470 */
6471 test_node->name = name;
6472 xmlFreeNode(test_node);
6473
Owen Taylor3473f882001-02-23 17:55:21 +00006474 return(nb_valid_elements);
6475}