blob: b1555a8d69cf20e2c24e12c6612dd9a7c342ca21 [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 Veillard4432df22003-09-28 18:58:27 +000028static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
29 int create);
30#ifdef LIBXML_VALID_ENABLED
31
Daniel Veillarde62d36c2001-05-15 08:53:16 +000032/* #define DEBUG_VALID_ALGO */
Daniel Veillard5acfd6b2002-09-18 16:29:02 +000033/* #define DEBUG_REGEXP_ALGO */
Daniel Veillarde62d36c2001-05-15 08:53:16 +000034
Daniel Veillarda646cfd2002-09-17 21:50:03 +000035#define TODO \
36 xmlGenericError(xmlGenericErrorContext, \
37 "Unimplemented block at %s:%d\n", \
38 __FILE__, __LINE__);
Owen Taylor3473f882001-02-23 17:55:21 +000039
Daniel Veillardea7751d2002-12-20 00:16:24 +000040#define VERROR \
41 if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error
Owen Taylor3473f882001-02-23 17:55:21 +000042
Daniel Veillardea7751d2002-12-20 00:16:24 +000043#define VWARNING \
44 if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning
45
46
47#ifdef LIBXML_REGEXP_ENABLED
48/*
49 * If regexp are enabled we can do continuous validation without the
50 * need of a tree to validate the content model. this is done in each
51 * callbacks.
52 * Each xmlValidState represent the validation state associated to the
53 * set of nodes currently open from the document root to the current element.
54 */
55
56
57typedef struct _xmlValidState {
58 xmlElementPtr elemDecl; /* pointer to the content model */
59 xmlNodePtr node; /* pointer to the current node */
60 xmlRegExecCtxtPtr exec; /* regexp runtime */
61} _xmlValidState;
62
63
64static int
65vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000066 if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
Daniel Veillardea7751d2002-12-20 00:16:24 +000067 ctxt->vstateMax = 10;
68 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
69 sizeof(ctxt->vstateTab[0]));
70 if (ctxt->vstateTab == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000071 VERROR(ctxt->userData, "malloc failed !n");
Daniel Veillardea7751d2002-12-20 00:16:24 +000072 return(-1);
73 }
74 }
75
76 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000077 xmlValidState *tmp;
78
79 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
80 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
81 if (tmp == NULL) {
Daniel Veillardea7751d2002-12-20 00:16:24 +000082 VERROR(ctxt->userData, "realloc failed !n");
83 return(-1);
84 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000085 ctxt->vstateMax *= 2;
86 ctxt->vstateTab = tmp;
Daniel Veillardea7751d2002-12-20 00:16:24 +000087 }
88 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
89 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
90 ctxt->vstateTab[ctxt->vstateNr].node = node;
91 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
92 if (elemDecl->contModel == NULL)
93 xmlValidBuildContentModel(ctxt, elemDecl);
94 if (elemDecl->contModel != NULL) {
95 ctxt->vstateTab[ctxt->vstateNr].exec =
96 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
97 } else {
98 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
99 VERROR(ctxt->userData,
100 "Failed to build content model regexp for %s", node->name);
101 }
102 }
103 return(ctxt->vstateNr++);
104}
105
106static int
107vstateVPop(xmlValidCtxtPtr ctxt) {
108 xmlElementPtr elemDecl;
109
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000110 if (ctxt->vstateNr < 1) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000111 ctxt->vstateNr--;
112 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
113 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
114 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
115 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
116 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
117 }
118 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
119 if (ctxt->vstateNr >= 1)
120 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
121 else
122 ctxt->vstate = NULL;
123 return(ctxt->vstateNr);
124}
125
126#else /* not LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000127/*
Daniel Veillard1c732d22002-11-30 11:22:59 +0000128 * If regexp are not enabled, it uses a home made algorithm less
129 * complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000130 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000131 * only restriction is on the deepness of the tree limited by the
132 * size of the occurs bitfield
133 *
134 * this is the content of a saved state for rollbacks
135 */
136
137#define ROLLBACK_OR 0
138#define ROLLBACK_PARENT 1
139
Daniel Veillardb44025c2001-10-11 22:55:55 +0000140typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000141 xmlElementContentPtr cont; /* pointer to the content model subtree */
142 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000143 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000144 unsigned char depth; /* current depth in the overall tree */
145 unsigned char state; /* ROLLBACK_XXX */
146} _xmlValidState;
147
Daniel Veillardfc57b412002-04-29 15:50:14 +0000148#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000149#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
150#define CONT ctxt->vstate->cont
151#define NODE ctxt->vstate->node
152#define DEPTH ctxt->vstate->depth
153#define OCCURS ctxt->vstate->occurs
154#define STATE ctxt->vstate->state
155
Daniel Veillard5344c602001-12-31 16:37:34 +0000156#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
157#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000158
Daniel Veillard5344c602001-12-31 16:37:34 +0000159#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
160#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000161
162static int
163vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
164 xmlNodePtr node, unsigned char depth, long occurs,
165 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000166 int i = ctxt->vstateNr - 1;
167
Daniel Veillard940492d2002-04-15 10:15:25 +0000168 if (ctxt->vstateNr > MAX_RECURSE) {
169 return(-1);
170 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000171 if (ctxt->vstateTab == NULL) {
172 ctxt->vstateMax = 8;
173 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
174 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
175 if (ctxt->vstateTab == NULL) {
176 xmlGenericError(xmlGenericErrorContext,
177 "malloc failed !n");
178 return(-1);
179 }
180 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000181 if (ctxt->vstateNr >= ctxt->vstateMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000182 xmlValidState *tmp;
183
184 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
185 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
186 if (tmp == NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000187 xmlGenericError(xmlGenericErrorContext,
188 "realloc failed !n");
Daniel Veillard940492d2002-04-15 10:15:25 +0000189 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000190 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000191 ctxt->vstateMax *= 2;
192 ctxt->vstateTab = tmp;
Daniel Veillard06803992001-04-22 10:35:56 +0000193 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000194 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000195 /*
196 * Don't push on the stack a state already here
197 */
198 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
199 (ctxt->vstateTab[i].node == node) &&
200 (ctxt->vstateTab[i].depth == depth) &&
201 (ctxt->vstateTab[i].occurs == occurs) &&
202 (ctxt->vstateTab[i].state == state))
203 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000204 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
205 ctxt->vstateTab[ctxt->vstateNr].node = node;
206 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
207 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
208 ctxt->vstateTab[ctxt->vstateNr].state = state;
209 return(ctxt->vstateNr++);
210}
211
212static int
213vstateVPop(xmlValidCtxtPtr ctxt) {
214 if (ctxt->vstateNr <= 1) return(-1);
215 ctxt->vstateNr--;
216 ctxt->vstate = &ctxt->vstateTab[0];
217 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
218 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
219 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
220 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
221 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
222 return(ctxt->vstateNr);
223}
224
Daniel Veillard118aed72002-09-24 14:13:13 +0000225#endif /* LIBXML_REGEXP_ENABLED */
226
Daniel Veillard1c732d22002-11-30 11:22:59 +0000227static int
228nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
229{
230 if (ctxt->nodeMax <= 0) {
231 ctxt->nodeMax = 4;
232 ctxt->nodeTab =
233 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
234 sizeof(ctxt->nodeTab[0]));
235 if (ctxt->nodeTab == NULL) {
236 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
237 ctxt->nodeMax = 0;
238 return (0);
239 }
240 }
241 if (ctxt->nodeNr >= ctxt->nodeMax) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000242 xmlNodePtr *tmp;
243 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
244 ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
245 if (tmp == NULL) {
Daniel Veillard1c732d22002-11-30 11:22:59 +0000246 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
247 return (0);
248 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000249 ctxt->nodeMax *= 2;
250 ctxt->nodeTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000251 }
252 ctxt->nodeTab[ctxt->nodeNr] = value;
253 ctxt->node = value;
254 return (ctxt->nodeNr++);
255}
256static xmlNodePtr
257nodeVPop(xmlValidCtxtPtr ctxt)
258{
259 xmlNodePtr ret;
260
261 if (ctxt->nodeNr <= 0)
262 return (0);
263 ctxt->nodeNr--;
264 if (ctxt->nodeNr > 0)
265 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
266 else
267 ctxt->node = NULL;
268 ret = ctxt->nodeTab[ctxt->nodeNr];
269 ctxt->nodeTab[ctxt->nodeNr] = 0;
270 return (ret);
271}
Owen Taylor3473f882001-02-23 17:55:21 +0000272
Owen Taylor3473f882001-02-23 17:55:21 +0000273#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000274static void
275xmlValidPrintNode(xmlNodePtr cur) {
276 if (cur == NULL) {
277 xmlGenericError(xmlGenericErrorContext, "null");
278 return;
279 }
280 switch (cur->type) {
281 case XML_ELEMENT_NODE:
282 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
283 break;
284 case XML_TEXT_NODE:
285 xmlGenericError(xmlGenericErrorContext, "text ");
286 break;
287 case XML_CDATA_SECTION_NODE:
288 xmlGenericError(xmlGenericErrorContext, "cdata ");
289 break;
290 case XML_ENTITY_REF_NODE:
291 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
292 break;
293 case XML_PI_NODE:
294 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
295 break;
296 case XML_COMMENT_NODE:
297 xmlGenericError(xmlGenericErrorContext, "comment ");
298 break;
299 case XML_ATTRIBUTE_NODE:
300 xmlGenericError(xmlGenericErrorContext, "?attr? ");
301 break;
302 case XML_ENTITY_NODE:
303 xmlGenericError(xmlGenericErrorContext, "?ent? ");
304 break;
305 case XML_DOCUMENT_NODE:
306 xmlGenericError(xmlGenericErrorContext, "?doc? ");
307 break;
308 case XML_DOCUMENT_TYPE_NODE:
309 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
310 break;
311 case XML_DOCUMENT_FRAG_NODE:
312 xmlGenericError(xmlGenericErrorContext, "?frag? ");
313 break;
314 case XML_NOTATION_NODE:
315 xmlGenericError(xmlGenericErrorContext, "?nota? ");
316 break;
317 case XML_HTML_DOCUMENT_NODE:
318 xmlGenericError(xmlGenericErrorContext, "?html? ");
319 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000320#ifdef LIBXML_DOCB_ENABLED
321 case XML_DOCB_DOCUMENT_NODE:
322 xmlGenericError(xmlGenericErrorContext, "?docb? ");
323 break;
324#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000325 case XML_DTD_NODE:
326 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
327 break;
328 case XML_ELEMENT_DECL:
329 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
330 break;
331 case XML_ATTRIBUTE_DECL:
332 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
333 break;
334 case XML_ENTITY_DECL:
335 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
336 break;
337 case XML_NAMESPACE_DECL:
338 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
339 break;
340 case XML_XINCLUDE_START:
341 xmlGenericError(xmlGenericErrorContext, "incstart ");
342 break;
343 case XML_XINCLUDE_END:
344 xmlGenericError(xmlGenericErrorContext, "incend ");
345 break;
346 }
347}
348
349static void
350xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000351 if (cur == NULL)
352 xmlGenericError(xmlGenericErrorContext, "null ");
353 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000354 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000355 cur = cur->next;
356 }
357}
358
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000359static void
360xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Daniel Veillard83391282003-03-06 21:37:30 +0000361 char expr[5000];
Owen Taylor3473f882001-02-23 17:55:21 +0000362
363 expr[0] = 0;
364 xmlGenericError(xmlGenericErrorContext, "valid: ");
365 xmlValidPrintNodeList(cur);
366 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000367 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000368 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
369}
370
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000371static void
372xmlValidDebugState(xmlValidStatePtr state) {
373 xmlGenericError(xmlGenericErrorContext, "(");
374 if (state->cont == NULL)
375 xmlGenericError(xmlGenericErrorContext, "null,");
376 else
377 switch (state->cont->type) {
378 case XML_ELEMENT_CONTENT_PCDATA:
379 xmlGenericError(xmlGenericErrorContext, "pcdata,");
380 break;
381 case XML_ELEMENT_CONTENT_ELEMENT:
382 xmlGenericError(xmlGenericErrorContext, "%s,",
383 state->cont->name);
384 break;
385 case XML_ELEMENT_CONTENT_SEQ:
386 xmlGenericError(xmlGenericErrorContext, "seq,");
387 break;
388 case XML_ELEMENT_CONTENT_OR:
389 xmlGenericError(xmlGenericErrorContext, "or,");
390 break;
391 }
392 xmlValidPrintNode(state->node);
393 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
394 state->depth, state->occurs, state->state);
395}
396
397static void
398xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
399 int i, j;
400
401 xmlGenericError(xmlGenericErrorContext, "state: ");
402 xmlValidDebugState(ctxt->vstate);
403 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
404 ctxt->vstateNr - 1);
405 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
406 xmlValidDebugState(&ctxt->vstateTab[j]);
407 xmlGenericError(xmlGenericErrorContext, "\n");
408}
409
410/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000411#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000412 *****/
413
414#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000415#define DEBUG_VALID_MSG(m) \
416 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
417
Owen Taylor3473f882001-02-23 17:55:21 +0000418#else
419#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000420#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000421#endif
422
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000423/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000424
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000425#define VECTXT(ctxt, node) \
426 if ((ctxt != NULL) && (ctxt->error != NULL) && \
Daniel Veillard76575762002-09-05 14:21:15 +0000427 (node != NULL)) { \
428 xmlChar *base = xmlNodeGetBase(NULL,node); \
429 if (base != NULL) { \
430 ctxt->error(ctxt->userData, "%s:%d: ", base, \
Daniel Veillard366a9152002-10-23 20:43:53 +0000431 (int) (long) node->content); \
Daniel Veillard76575762002-09-05 14:21:15 +0000432 xmlFree(base); \
433 } else \
434 ctxt->error(ctxt->userData, ":%d: ", \
Daniel Veillard366a9152002-10-23 20:43:53 +0000435 (int) (long) node->content); \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000436 }
437
438#define VWCTXT(ctxt, node) \
439 if ((ctxt != NULL) && (ctxt->warning != NULL) && \
Daniel Veillard76575762002-09-05 14:21:15 +0000440 (node != NULL)) { \
441 xmlChar *base = xmlNodeGetBase(NULL,node); \
442 if (base != NULL) { \
443 ctxt->warning(ctxt->userData, "%s:%d: ", base, \
Daniel Veillard366a9152002-10-23 20:43:53 +0000444 (int) (long) node->content); \
Daniel Veillard76575762002-09-05 14:21:15 +0000445 xmlFree(base); \
446 } else \
447 ctxt->warning(ctxt->userData, ":%d: ", \
Daniel Veillard366a9152002-10-23 20:43:53 +0000448 (int) (long) node->content); \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000449 }
450
Owen Taylor3473f882001-02-23 17:55:21 +0000451#define CHECK_DTD \
452 if (doc == NULL) return(0); \
453 else if ((doc->intSubset == NULL) && \
454 (doc->extSubset == NULL)) return(0)
455
Owen Taylor3473f882001-02-23 17:55:21 +0000456xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
457
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000458#ifdef LIBXML_REGEXP_ENABLED
459
460/************************************************************************
461 * *
462 * Content model validation based on the regexps *
463 * *
464 ************************************************************************/
465
466/**
467 * xmlValidBuildAContentModel:
468 * @content: the content model
469 * @ctxt: the schema parser context
470 * @name: the element name whose content is being built
471 *
472 * Generate the automata sequence needed for that type
473 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000474 * Returns 1 if successful or 0 in case of error.
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000475 */
476static int
477xmlValidBuildAContentModel(xmlElementContentPtr content,
478 xmlValidCtxtPtr ctxt,
479 const xmlChar *name) {
480 if (content == NULL) {
481 VERROR(ctxt->userData,
482 "Found unexpected type = NULL in %s content model\n", name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000483 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000484 }
485 switch (content->type) {
486 case XML_ELEMENT_CONTENT_PCDATA:
487 VERROR(ctxt->userData, "ContentModel found PCDATA for element %s\n",
488 name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000489 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000490 break;
491 case XML_ELEMENT_CONTENT_ELEMENT: {
492 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillardc00cda82003-04-07 10:22:39 +0000493 xmlChar fn[50];
494 xmlChar *fullname;
495
496 fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
497 if (fullname == NULL) {
498 VERROR(ctxt->userData, "Out of memory\n");
499 return(0);
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000500 }
501
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000502 switch (content->ocur) {
503 case XML_ELEMENT_CONTENT_ONCE:
504 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000505 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000506 break;
507 case XML_ELEMENT_CONTENT_OPT:
508 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000509 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000510 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
511 break;
512 case XML_ELEMENT_CONTENT_PLUS:
513 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000514 ctxt->state, NULL, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000515 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000516 ctxt->state, fullname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000517 break;
518 case XML_ELEMENT_CONTENT_MULT:
519 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillardc00cda82003-04-07 10:22:39 +0000520 ctxt->state, fullname, NULL);
Daniel Veillard57e79b32003-02-04 15:33:12 +0000521 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
522 NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000523 break;
524 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000525 if ((fullname != fn) && (fullname != content->name))
526 xmlFree(fullname);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000527 break;
528 }
529 case XML_ELEMENT_CONTENT_SEQ: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000530 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000531 xmlElementContentOccur ocur;
532
533 /*
534 * Simply iterate over the content
535 */
536 oldstate = ctxt->state;
537 ocur = content->ocur;
Daniel Veillardf4be0182003-02-24 19:54:33 +0000538 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
539 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
540 oldstate = ctxt->state;
541 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000542 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000543 xmlValidBuildAContentModel(content->c1, ctxt, name);
544 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000545 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
546 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
547 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000548 oldend = ctxt->state;
549 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000550 switch (ocur) {
551 case XML_ELEMENT_CONTENT_ONCE:
552 break;
553 case XML_ELEMENT_CONTENT_OPT:
554 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
555 break;
556 case XML_ELEMENT_CONTENT_MULT:
557 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000558 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000559 break;
560 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000561 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000562 break;
563 }
564 break;
565 }
566 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000567 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000568 xmlElementContentOccur ocur;
569
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000570 ocur = content->ocur;
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000571 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
572 (ocur == XML_ELEMENT_CONTENT_MULT)) {
573 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
574 ctxt->state, NULL);
575 }
576 oldstate = ctxt->state;
577 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000578
579 /*
580 * iterate over the subtypes and remerge the end with an
581 * epsilon transition
582 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000583 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000584 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000585 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000586 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000587 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000588 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
589 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000590 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000591 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000592 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
593 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000594 switch (ocur) {
595 case XML_ELEMENT_CONTENT_ONCE:
596 break;
597 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000598 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000599 break;
600 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000601 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
602 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000603 break;
604 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000605 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000606 break;
607 }
608 break;
609 }
610 default:
611 VERROR(ctxt->userData, "ContentModel broken for element %s\n",
612 name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000613 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000614 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000615 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000616}
617/**
618 * xmlValidBuildContentModel:
619 * @ctxt: a validation context
620 * @elem: an element declaration node
621 *
622 * (Re)Build the automata associated to the content model of this
623 * element
624 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000625 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000626 */
627int
628xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000629
630 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000631 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000632 if (elem->type != XML_ELEMENT_DECL)
633 return(0);
634 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
635 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000636 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000637 if (elem->contModel != NULL) {
638 if (!xmlRegexpIsDeterminist(elem->contModel)) {
639 ctxt->valid = 0;
640 return(0);
641 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000642 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000643 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000644
645 ctxt->am = xmlNewAutomata();
646 if (ctxt->am == NULL) {
647 VERROR(ctxt->userData, "Cannot create automata for element %s\n",
648 elem->name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000649 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000650 }
William M. Brack78637da2003-07-31 14:47:38 +0000651 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000652 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
653 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000654 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000655 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000656 char expr[5000];
657 expr[0] = 0;
658 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
659 VERROR(ctxt->userData, "Content model of %s is not determinist: %s\n",
660 elem->name, expr);
661#ifdef DEBUG_REGEXP_ALGO
662 xmlRegexpPrint(stderr, elem->contModel);
663#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000664 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000665 ctxt->state = NULL;
666 xmlFreeAutomata(ctxt->am);
667 ctxt->am = NULL;
668 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000669 }
670 ctxt->state = NULL;
671 xmlFreeAutomata(ctxt->am);
672 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000673 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000674}
675
676#endif /* LIBXML_REGEXP_ENABLED */
677
Owen Taylor3473f882001-02-23 17:55:21 +0000678/****************************************************************
679 * *
680 * Util functions for data allocation/deallocation *
681 * *
682 ****************************************************************/
683
684/**
Daniel Veillarda37aab82003-06-09 09:10:36 +0000685 * xmlNewValidCtxt:
686 *
687 * Allocate a validation context structure.
688 *
689 * Returns NULL if not, otherwise the new validation context structure
690 */
691xmlValidCtxtPtr
692xmlNewValidCtxt(void) {
693 xmlValidCtxtPtr ret;
694
695 if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL)
696 return (NULL);
697
698 (void) memset(ret, 0, sizeof (xmlValidCtxt));
699
700 return (ret);
701}
702
703/**
704 * xmlFreeValidCtxt:
705 * @cur: the validation context to free
706 *
707 * Free a validation context structure.
708 */
709void
710xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
711 xmlFree(cur);
712}
713
Daniel Veillard4432df22003-09-28 18:58:27 +0000714#endif /* LIBXML_VALID_ENABLED */
715
Daniel Veillarda37aab82003-06-09 09:10:36 +0000716/**
Owen Taylor3473f882001-02-23 17:55:21 +0000717 * xmlNewElementContent:
718 * @name: the subelement name or NULL
719 * @type: the type of element content decl
720 *
721 * Allocate an element content structure.
722 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000723 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000724 */
725xmlElementContentPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000726xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
Owen Taylor3473f882001-02-23 17:55:21 +0000727 xmlElementContentPtr ret;
728
729 switch(type) {
730 case XML_ELEMENT_CONTENT_ELEMENT:
731 if (name == NULL) {
732 xmlGenericError(xmlGenericErrorContext,
733 "xmlNewElementContent : name == NULL !\n");
734 }
735 break;
736 case XML_ELEMENT_CONTENT_PCDATA:
737 case XML_ELEMENT_CONTENT_SEQ:
738 case XML_ELEMENT_CONTENT_OR:
739 if (name != NULL) {
740 xmlGenericError(xmlGenericErrorContext,
741 "xmlNewElementContent : name != NULL !\n");
742 }
743 break;
744 default:
745 xmlGenericError(xmlGenericErrorContext,
746 "xmlNewElementContent: unknown type %d\n", type);
747 return(NULL);
748 }
749 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
750 if (ret == NULL) {
751 xmlGenericError(xmlGenericErrorContext,
752 "xmlNewElementContent : out of memory!\n");
753 return(NULL);
754 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000755 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000756 ret->type = type;
757 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000758 if (name != NULL) {
759 xmlChar *prefix = NULL;
760 ret->name = xmlSplitQName2(name, &prefix);
761 if (ret->name == NULL)
762 ret->name = xmlStrdup(name);
763 ret->prefix = prefix;
764 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000765 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000766 ret->prefix = NULL;
767 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000768 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000769 return(ret);
770}
771
772/**
773 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000774 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +0000775 *
776 * Build a copy of an element content description.
777 *
778 * Returns the new xmlElementContentPtr or NULL in case of error.
779 */
780xmlElementContentPtr
781xmlCopyElementContent(xmlElementContentPtr cur) {
782 xmlElementContentPtr ret;
783
784 if (cur == NULL) return(NULL);
785 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
786 if (ret == NULL) {
787 xmlGenericError(xmlGenericErrorContext,
788 "xmlCopyElementContent : out of memory\n");
789 return(NULL);
790 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000791 if (cur->prefix != NULL)
792 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000793 ret->ocur = cur->ocur;
794 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000795 if (ret->c1 != NULL)
796 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000797 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000798 if (ret->c2 != NULL)
799 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000800 return(ret);
801}
802
803/**
804 * xmlFreeElementContent:
805 * @cur: the element content tree to free
806 *
807 * Free an element content structure. This is a recursive call !
808 */
809void
810xmlFreeElementContent(xmlElementContentPtr cur) {
811 if (cur == NULL) return;
812 switch (cur->type) {
813 case XML_ELEMENT_CONTENT_PCDATA:
814 case XML_ELEMENT_CONTENT_ELEMENT:
815 case XML_ELEMENT_CONTENT_SEQ:
816 case XML_ELEMENT_CONTENT_OR:
817 break;
818 default:
819 xmlGenericError(xmlGenericErrorContext,
820 "xmlFreeElementContent : type %d\n", cur->type);
821 return;
822 }
823 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
824 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
825 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000826 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000827 xmlFree(cur);
828}
829
830/**
831 * xmlDumpElementContent:
832 * @buf: An XML buffer
833 * @content: An element table
834 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
835 *
836 * This will dump the content of the element table as an XML DTD definition
837 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000838static void
Owen Taylor3473f882001-02-23 17:55:21 +0000839xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
840 if (content == NULL) return;
841
842 if (glob) xmlBufferWriteChar(buf, "(");
843 switch (content->type) {
844 case XML_ELEMENT_CONTENT_PCDATA:
845 xmlBufferWriteChar(buf, "#PCDATA");
846 break;
847 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000848 if (content->prefix != NULL) {
849 xmlBufferWriteCHAR(buf, content->prefix);
850 xmlBufferWriteChar(buf, ":");
851 }
Owen Taylor3473f882001-02-23 17:55:21 +0000852 xmlBufferWriteCHAR(buf, content->name);
853 break;
854 case XML_ELEMENT_CONTENT_SEQ:
855 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
856 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
857 xmlDumpElementContent(buf, content->c1, 1);
858 else
859 xmlDumpElementContent(buf, content->c1, 0);
860 xmlBufferWriteChar(buf, " , ");
861 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
862 xmlDumpElementContent(buf, content->c2, 1);
863 else
864 xmlDumpElementContent(buf, content->c2, 0);
865 break;
866 case XML_ELEMENT_CONTENT_OR:
867 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
868 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
869 xmlDumpElementContent(buf, content->c1, 1);
870 else
871 xmlDumpElementContent(buf, content->c1, 0);
872 xmlBufferWriteChar(buf, " | ");
873 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
874 xmlDumpElementContent(buf, content->c2, 1);
875 else
876 xmlDumpElementContent(buf, content->c2, 0);
877 break;
878 default:
879 xmlGenericError(xmlGenericErrorContext,
880 "xmlDumpElementContent: unknown type %d\n",
881 content->type);
882 }
883 if (glob)
884 xmlBufferWriteChar(buf, ")");
885 switch (content->ocur) {
886 case XML_ELEMENT_CONTENT_ONCE:
887 break;
888 case XML_ELEMENT_CONTENT_OPT:
889 xmlBufferWriteChar(buf, "?");
890 break;
891 case XML_ELEMENT_CONTENT_MULT:
892 xmlBufferWriteChar(buf, "*");
893 break;
894 case XML_ELEMENT_CONTENT_PLUS:
895 xmlBufferWriteChar(buf, "+");
896 break;
897 }
898}
899
900/**
901 * xmlSprintfElementContent:
902 * @buf: an output buffer
903 * @content: An element table
904 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
905 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000906 * Deprecated, unsafe, use xmlSnprintfElementContent
907 */
908void
909xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
910 xmlElementContentPtr content ATTRIBUTE_UNUSED,
911 int glob ATTRIBUTE_UNUSED) {
912}
913
914/**
915 * xmlSnprintfElementContent:
916 * @buf: an output buffer
917 * @size: the buffer size
918 * @content: An element table
919 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
920 *
Owen Taylor3473f882001-02-23 17:55:21 +0000921 * This will dump the content of the element content definition
922 * Intended just for the debug routine
923 */
924void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000925xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
926 int len;
927
Owen Taylor3473f882001-02-23 17:55:21 +0000928 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000929 len = strlen(buf);
930 if (size - len < 50) {
931 if ((size - len > 4) && (buf[len - 1] != '.'))
932 strcat(buf, " ...");
933 return;
934 }
Owen Taylor3473f882001-02-23 17:55:21 +0000935 if (glob) strcat(buf, "(");
936 switch (content->type) {
937 case XML_ELEMENT_CONTENT_PCDATA:
938 strcat(buf, "#PCDATA");
939 break;
940 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000941 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000942 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000943 strcat(buf, " ...");
944 return;
945 }
946 strcat(buf, (char *) content->prefix);
947 strcat(buf, ":");
948 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000949 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +0000950 strcat(buf, " ...");
951 return;
952 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000953 if (content->name != NULL)
954 strcat(buf, (char *) content->name);
Owen Taylor3473f882001-02-23 17:55:21 +0000955 break;
956 case XML_ELEMENT_CONTENT_SEQ:
957 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
958 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000959 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000960 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000961 xmlSnprintfElementContent(buf, size, content->c1, 0);
962 len = strlen(buf);
963 if (size - len < 50) {
964 if ((size - len > 4) && (buf[len - 1] != '.'))
965 strcat(buf, " ...");
966 return;
967 }
Owen Taylor3473f882001-02-23 17:55:21 +0000968 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000969 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
970 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
971 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000972 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000973 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000974 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000975 break;
976 case XML_ELEMENT_CONTENT_OR:
977 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
978 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000979 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000980 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000981 xmlSnprintfElementContent(buf, size, content->c1, 0);
982 len = strlen(buf);
983 if (size - len < 50) {
984 if ((size - len > 4) && (buf[len - 1] != '.'))
985 strcat(buf, " ...");
986 return;
987 }
Owen Taylor3473f882001-02-23 17:55:21 +0000988 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000989 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
990 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
991 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000992 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000993 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000994 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000995 break;
996 }
997 if (glob)
998 strcat(buf, ")");
999 switch (content->ocur) {
1000 case XML_ELEMENT_CONTENT_ONCE:
1001 break;
1002 case XML_ELEMENT_CONTENT_OPT:
1003 strcat(buf, "?");
1004 break;
1005 case XML_ELEMENT_CONTENT_MULT:
1006 strcat(buf, "*");
1007 break;
1008 case XML_ELEMENT_CONTENT_PLUS:
1009 strcat(buf, "+");
1010 break;
1011 }
1012}
1013
1014/****************************************************************
1015 * *
1016 * Registration of DTD declarations *
1017 * *
1018 ****************************************************************/
1019
1020/**
1021 * xmlCreateElementTable:
1022 *
1023 * create and initialize an empty element hash table.
1024 *
1025 * Returns the xmlElementTablePtr just created or NULL in case of error.
1026 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001027static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001028xmlCreateElementTable(void) {
1029 return(xmlHashCreate(0));
1030}
1031
1032/**
1033 * xmlFreeElement:
1034 * @elem: An element
1035 *
1036 * Deallocate the memory used by an element definition
1037 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001038static void
Owen Taylor3473f882001-02-23 17:55:21 +00001039xmlFreeElement(xmlElementPtr elem) {
1040 if (elem == NULL) return;
1041 xmlUnlinkNode((xmlNodePtr) elem);
1042 xmlFreeElementContent(elem->content);
1043 if (elem->name != NULL)
1044 xmlFree((xmlChar *) elem->name);
1045 if (elem->prefix != NULL)
1046 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001047#ifdef LIBXML_REGEXP_ENABLED
1048 if (elem->contModel != NULL)
1049 xmlRegFreeRegexp(elem->contModel);
1050#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001051 xmlFree(elem);
1052}
1053
1054
1055/**
1056 * xmlAddElementDecl:
1057 * @ctxt: the validation context
1058 * @dtd: pointer to the DTD
1059 * @name: the entity name
1060 * @type: the element type
1061 * @content: the element content tree or NULL
1062 *
1063 * Register a new element declaration
1064 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001065 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001066 */
1067xmlElementPtr
1068xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
1069 xmlElementTypeVal type,
1070 xmlElementContentPtr content) {
1071 xmlElementPtr ret;
1072 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001073 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001074 xmlChar *ns, *uqname;
1075
1076 if (dtd == NULL) {
1077 xmlGenericError(xmlGenericErrorContext,
1078 "xmlAddElementDecl: dtd == NULL\n");
1079 return(NULL);
1080 }
1081 if (name == NULL) {
1082 xmlGenericError(xmlGenericErrorContext,
1083 "xmlAddElementDecl: name == NULL\n");
1084 return(NULL);
1085 }
1086 switch (type) {
1087 case XML_ELEMENT_TYPE_EMPTY:
1088 if (content != NULL) {
1089 xmlGenericError(xmlGenericErrorContext,
1090 "xmlAddElementDecl: content != NULL for EMPTY\n");
1091 return(NULL);
1092 }
1093 break;
1094 case XML_ELEMENT_TYPE_ANY:
1095 if (content != NULL) {
1096 xmlGenericError(xmlGenericErrorContext,
1097 "xmlAddElementDecl: content != NULL for ANY\n");
1098 return(NULL);
1099 }
1100 break;
1101 case XML_ELEMENT_TYPE_MIXED:
1102 if (content == NULL) {
1103 xmlGenericError(xmlGenericErrorContext,
1104 "xmlAddElementDecl: content == NULL for MIXED\n");
1105 return(NULL);
1106 }
1107 break;
1108 case XML_ELEMENT_TYPE_ELEMENT:
1109 if (content == NULL) {
1110 xmlGenericError(xmlGenericErrorContext,
1111 "xmlAddElementDecl: content == NULL for ELEMENT\n");
1112 return(NULL);
1113 }
1114 break;
1115 default:
1116 xmlGenericError(xmlGenericErrorContext,
1117 "xmlAddElementDecl: unknown type %d\n", type);
1118 return(NULL);
1119 }
1120
1121 /*
1122 * check if name is a QName
1123 */
1124 uqname = xmlSplitQName2(name, &ns);
1125 if (uqname != NULL)
1126 name = uqname;
1127
1128 /*
1129 * Create the Element table if needed.
1130 */
1131 table = (xmlElementTablePtr) dtd->elements;
1132 if (table == NULL) {
1133 table = xmlCreateElementTable();
1134 dtd->elements = (void *) table;
1135 }
1136 if (table == NULL) {
1137 xmlGenericError(xmlGenericErrorContext,
1138 "xmlAddElementDecl: Table creation failed!\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001139 if (uqname != NULL)
1140 xmlFree(uqname);
1141 if (ns != NULL)
1142 xmlFree(ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001143 return(NULL);
1144 }
1145
Daniel Veillarda10efa82001-04-18 13:09:01 +00001146 /*
1147 * lookup old attributes inserted on an undefined element in the
1148 * internal subset.
1149 */
1150 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1151 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1152 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1153 oldAttributes = ret->attributes;
1154 ret->attributes = NULL;
1155 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1156 xmlFreeElement(ret);
1157 }
Owen Taylor3473f882001-02-23 17:55:21 +00001158 }
Owen Taylor3473f882001-02-23 17:55:21 +00001159
1160 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001161 * The element may already be present if one of its attribute
1162 * was registered first
1163 */
1164 ret = xmlHashLookup2(table, name, ns);
1165 if (ret != NULL) {
1166 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001167#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001168 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001169 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001170 */
1171 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
Daniel Veillard4432df22003-09-28 18:58:27 +00001172#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001173 if (uqname != NULL)
1174 xmlFree(uqname);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001175 if (ns != NULL)
1176 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001177 return(NULL);
1178 }
1179 } else {
1180 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1181 if (ret == NULL) {
1182 xmlGenericError(xmlGenericErrorContext,
1183 "xmlAddElementDecl: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001184 if (uqname != NULL)
1185 xmlFree(uqname);
1186 if (ns != NULL)
1187 xmlFree(ns);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001188 return(NULL);
1189 }
1190 memset(ret, 0, sizeof(xmlElement));
1191 ret->type = XML_ELEMENT_DECL;
1192
1193 /*
1194 * fill the structure.
1195 */
1196 ret->name = xmlStrdup(name);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001197 if (ret->name == NULL) {
1198 xmlGenericError(xmlGenericErrorContext,
1199 "xmlAddElementDecl: out of memory\n");
1200 if (uqname != NULL)
1201 xmlFree(uqname);
1202 if (ns != NULL)
1203 xmlFree(ns);
1204 xmlFree(ret);
1205 return(NULL);
1206 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00001207 ret->prefix = ns;
1208
1209 /*
1210 * Validity Check:
1211 * Insertion must not fail
1212 */
1213 if (xmlHashAddEntry2(table, name, ns, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001214#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda10efa82001-04-18 13:09:01 +00001215 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001216 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001217 */
1218 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
Daniel Veillard4432df22003-09-28 18:58:27 +00001219#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001220 xmlFreeElement(ret);
1221 if (uqname != NULL)
1222 xmlFree(uqname);
1223 return(NULL);
1224 }
William M. Brack4e52f2f2003-09-14 18:07:39 +00001225 /*
1226 * For new element, may have attributes from earlier
1227 * definition in internal subset
1228 */
1229 ret->attributes = oldAttributes;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001230 }
1231
1232 /*
1233 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001234 */
1235 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +00001236 ret->content = xmlCopyElementContent(content);
Owen Taylor3473f882001-02-23 17:55:21 +00001237
1238 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001239 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001240 */
1241 ret->parent = dtd;
1242 ret->doc = dtd->doc;
1243 if (dtd->last == NULL) {
1244 dtd->children = dtd->last = (xmlNodePtr) ret;
1245 } else {
1246 dtd->last->next = (xmlNodePtr) ret;
1247 ret->prev = dtd->last;
1248 dtd->last = (xmlNodePtr) ret;
1249 }
1250 if (uqname != NULL)
1251 xmlFree(uqname);
1252 return(ret);
1253}
1254
1255/**
1256 * xmlFreeElementTable:
1257 * @table: An element table
1258 *
1259 * Deallocate the memory used by an element hash table.
1260 */
1261void
1262xmlFreeElementTable(xmlElementTablePtr table) {
1263 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1264}
1265
1266/**
1267 * xmlCopyElement:
1268 * @elem: An element
1269 *
1270 * Build a copy of an element.
1271 *
1272 * Returns the new xmlElementPtr or NULL in case of error.
1273 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001274static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001275xmlCopyElement(xmlElementPtr elem) {
1276 xmlElementPtr cur;
1277
1278 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1279 if (cur == NULL) {
1280 xmlGenericError(xmlGenericErrorContext,
1281 "xmlCopyElement: out of memory !\n");
1282 return(NULL);
1283 }
1284 memset(cur, 0, sizeof(xmlElement));
1285 cur->type = XML_ELEMENT_DECL;
1286 cur->etype = elem->etype;
1287 if (elem->name != NULL)
1288 cur->name = xmlStrdup(elem->name);
1289 else
1290 cur->name = NULL;
1291 if (elem->prefix != NULL)
1292 cur->prefix = xmlStrdup(elem->prefix);
1293 else
1294 cur->prefix = NULL;
1295 cur->content = xmlCopyElementContent(elem->content);
1296 /* TODO : rebuild the attribute list on the copy */
1297 cur->attributes = NULL;
1298 return(cur);
1299}
1300
1301/**
1302 * xmlCopyElementTable:
1303 * @table: An element table
1304 *
1305 * Build a copy of an element table.
1306 *
1307 * Returns the new xmlElementTablePtr or NULL in case of error.
1308 */
1309xmlElementTablePtr
1310xmlCopyElementTable(xmlElementTablePtr table) {
1311 return((xmlElementTablePtr) xmlHashCopy(table,
1312 (xmlHashCopier) xmlCopyElement));
1313}
1314
1315/**
1316 * xmlDumpElementDecl:
1317 * @buf: the XML buffer output
1318 * @elem: An element table
1319 *
1320 * This will dump the content of the element declaration as an XML
1321 * DTD definition
1322 */
1323void
1324xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1325 switch (elem->etype) {
1326 case XML_ELEMENT_TYPE_EMPTY:
1327 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001328 if (elem->prefix != NULL) {
1329 xmlBufferWriteCHAR(buf, elem->prefix);
1330 xmlBufferWriteChar(buf, ":");
1331 }
Owen Taylor3473f882001-02-23 17:55:21 +00001332 xmlBufferWriteCHAR(buf, elem->name);
1333 xmlBufferWriteChar(buf, " EMPTY>\n");
1334 break;
1335 case XML_ELEMENT_TYPE_ANY:
1336 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001337 if (elem->prefix != NULL) {
1338 xmlBufferWriteCHAR(buf, elem->prefix);
1339 xmlBufferWriteChar(buf, ":");
1340 }
Owen Taylor3473f882001-02-23 17:55:21 +00001341 xmlBufferWriteCHAR(buf, elem->name);
1342 xmlBufferWriteChar(buf, " ANY>\n");
1343 break;
1344 case XML_ELEMENT_TYPE_MIXED:
1345 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001346 if (elem->prefix != NULL) {
1347 xmlBufferWriteCHAR(buf, elem->prefix);
1348 xmlBufferWriteChar(buf, ":");
1349 }
Owen Taylor3473f882001-02-23 17:55:21 +00001350 xmlBufferWriteCHAR(buf, elem->name);
1351 xmlBufferWriteChar(buf, " ");
1352 xmlDumpElementContent(buf, elem->content, 1);
1353 xmlBufferWriteChar(buf, ">\n");
1354 break;
1355 case XML_ELEMENT_TYPE_ELEMENT:
1356 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001357 if (elem->prefix != NULL) {
1358 xmlBufferWriteCHAR(buf, elem->prefix);
1359 xmlBufferWriteChar(buf, ":");
1360 }
Owen Taylor3473f882001-02-23 17:55:21 +00001361 xmlBufferWriteCHAR(buf, elem->name);
1362 xmlBufferWriteChar(buf, " ");
1363 xmlDumpElementContent(buf, elem->content, 1);
1364 xmlBufferWriteChar(buf, ">\n");
1365 break;
1366 default:
1367 xmlGenericError(xmlGenericErrorContext,
1368 "xmlDumpElementDecl: internal: unknown type %d\n",
1369 elem->etype);
1370 }
1371}
1372
1373/**
1374 * xmlDumpElementTable:
1375 * @buf: the XML buffer output
1376 * @table: An element table
1377 *
1378 * This will dump the content of the element table as an XML DTD definition
1379 */
1380void
1381xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1382 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1383}
1384
1385/**
1386 * xmlCreateEnumeration:
1387 * @name: the enumeration name or NULL
1388 *
1389 * create and initialize an enumeration attribute node.
1390 *
1391 * Returns the xmlEnumerationPtr just created or NULL in case
1392 * of error.
1393 */
1394xmlEnumerationPtr
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001395xmlCreateEnumeration(const xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00001396 xmlEnumerationPtr ret;
1397
1398 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1399 if (ret == NULL) {
1400 xmlGenericError(xmlGenericErrorContext,
1401 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1402 (long)sizeof(xmlEnumeration));
1403 return(NULL);
1404 }
1405 memset(ret, 0, sizeof(xmlEnumeration));
1406
1407 if (name != NULL)
1408 ret->name = xmlStrdup(name);
1409 return(ret);
1410}
1411
1412/**
1413 * xmlFreeEnumeration:
1414 * @cur: the tree to free.
1415 *
1416 * free an enumeration attribute node (recursive).
1417 */
1418void
1419xmlFreeEnumeration(xmlEnumerationPtr cur) {
1420 if (cur == NULL) return;
1421
1422 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1423
1424 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001425 xmlFree(cur);
1426}
1427
1428/**
1429 * xmlCopyEnumeration:
1430 * @cur: the tree to copy.
1431 *
1432 * Copy an enumeration attribute node (recursive).
1433 *
1434 * Returns the xmlEnumerationPtr just created or NULL in case
1435 * of error.
1436 */
1437xmlEnumerationPtr
1438xmlCopyEnumeration(xmlEnumerationPtr cur) {
1439 xmlEnumerationPtr ret;
1440
1441 if (cur == NULL) return(NULL);
1442 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1443
1444 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1445 else ret->next = NULL;
1446
1447 return(ret);
1448}
1449
1450/**
1451 * xmlDumpEnumeration:
1452 * @buf: the XML buffer output
1453 * @enum: An enumeration
1454 *
1455 * This will dump the content of the enumeration
1456 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001457static void
Owen Taylor3473f882001-02-23 17:55:21 +00001458xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1459 if (cur == NULL) return;
1460
1461 xmlBufferWriteCHAR(buf, cur->name);
1462 if (cur->next == NULL)
1463 xmlBufferWriteChar(buf, ")");
1464 else {
1465 xmlBufferWriteChar(buf, " | ");
1466 xmlDumpEnumeration(buf, cur->next);
1467 }
1468}
1469
1470/**
1471 * xmlCreateAttributeTable:
1472 *
1473 * create and initialize an empty attribute hash table.
1474 *
1475 * Returns the xmlAttributeTablePtr just created or NULL in case
1476 * of error.
1477 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001478static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001479xmlCreateAttributeTable(void) {
1480 return(xmlHashCreate(0));
1481}
1482
Daniel Veillard4432df22003-09-28 18:58:27 +00001483#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001484/**
1485 * xmlScanAttributeDeclCallback:
1486 * @attr: the attribute decl
1487 * @list: the list to update
1488 *
1489 * Callback called by xmlScanAttributeDecl when a new attribute
1490 * has to be entered in the list.
1491 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001492static void
Owen Taylor3473f882001-02-23 17:55:21 +00001493xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001494 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001495 attr->nexth = *list;
1496 *list = attr;
1497}
1498
1499/**
1500 * xmlScanAttributeDecl:
1501 * @dtd: pointer to the DTD
1502 * @elem: the element name
1503 *
1504 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001505 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001506 *
1507 * Returns the pointer to the first attribute decl in the chain,
1508 * possibly NULL.
1509 */
1510xmlAttributePtr
1511xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1512 xmlAttributePtr ret = NULL;
1513 xmlAttributeTablePtr table;
1514
1515 if (dtd == NULL) {
1516 xmlGenericError(xmlGenericErrorContext,
1517 "xmlScanAttributeDecl: dtd == NULL\n");
1518 return(NULL);
1519 }
1520 if (elem == NULL) {
1521 xmlGenericError(xmlGenericErrorContext,
1522 "xmlScanAttributeDecl: elem == NULL\n");
1523 return(NULL);
1524 }
1525 table = (xmlAttributeTablePtr) dtd->attributes;
1526 if (table == NULL)
1527 return(NULL);
1528
1529 /* WRONG !!! */
1530 xmlHashScan3(table, NULL, NULL, elem,
1531 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1532 return(ret);
1533}
1534
1535/**
1536 * xmlScanIDAttributeDecl:
1537 * @ctxt: the validation context
1538 * @elem: the element name
1539 *
1540 * Verify that the element don't have too many ID attributes
1541 * declared.
1542 *
1543 * Returns the number of ID attributes found.
1544 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001545static int
Owen Taylor3473f882001-02-23 17:55:21 +00001546xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1547 xmlAttributePtr cur;
1548 int ret = 0;
1549
1550 if (elem == NULL) return(0);
1551 cur = elem->attributes;
1552 while (cur != NULL) {
1553 if (cur->atype == XML_ATTRIBUTE_ID) {
1554 ret ++;
1555 if (ret > 1)
1556 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001557 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001558 elem->name, cur->name);
1559 }
1560 cur = cur->nexth;
1561 }
1562 return(ret);
1563}
Daniel Veillard4432df22003-09-28 18:58:27 +00001564#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001565
1566/**
1567 * xmlFreeAttribute:
1568 * @elem: An attribute
1569 *
1570 * Deallocate the memory used by an attribute definition
1571 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001572static void
Owen Taylor3473f882001-02-23 17:55:21 +00001573xmlFreeAttribute(xmlAttributePtr attr) {
1574 if (attr == NULL) return;
1575 xmlUnlinkNode((xmlNodePtr) attr);
1576 if (attr->tree != NULL)
1577 xmlFreeEnumeration(attr->tree);
1578 if (attr->elem != NULL)
1579 xmlFree((xmlChar *) attr->elem);
1580 if (attr->name != NULL)
1581 xmlFree((xmlChar *) attr->name);
1582 if (attr->defaultValue != NULL)
1583 xmlFree((xmlChar *) attr->defaultValue);
1584 if (attr->prefix != NULL)
1585 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001586 xmlFree(attr);
1587}
1588
1589
1590/**
1591 * xmlAddAttributeDecl:
1592 * @ctxt: the validation context
1593 * @dtd: pointer to the DTD
1594 * @elem: the element name
1595 * @name: the attribute name
1596 * @ns: the attribute namespace prefix
1597 * @type: the attribute type
1598 * @def: the attribute default type
1599 * @defaultValue: the attribute default value
1600 * @tree: if it's an enumeration, the associated list
1601 *
1602 * Register a new attribute declaration
1603 * Note that @tree becomes the ownership of the DTD
1604 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001605 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001606 */
1607xmlAttributePtr
1608xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1609 const xmlChar *name, const xmlChar *ns,
1610 xmlAttributeType type, xmlAttributeDefault def,
1611 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1612 xmlAttributePtr ret;
1613 xmlAttributeTablePtr table;
1614 xmlElementPtr elemDef;
1615
1616 if (dtd == NULL) {
1617 xmlGenericError(xmlGenericErrorContext,
1618 "xmlAddAttributeDecl: dtd == NULL\n");
1619 xmlFreeEnumeration(tree);
1620 return(NULL);
1621 }
1622 if (name == NULL) {
1623 xmlGenericError(xmlGenericErrorContext,
1624 "xmlAddAttributeDecl: name == NULL\n");
1625 xmlFreeEnumeration(tree);
1626 return(NULL);
1627 }
1628 if (elem == NULL) {
1629 xmlGenericError(xmlGenericErrorContext,
1630 "xmlAddAttributeDecl: elem == NULL\n");
1631 xmlFreeEnumeration(tree);
1632 return(NULL);
1633 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001634
Daniel Veillard4432df22003-09-28 18:58:27 +00001635#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001636 /*
1637 * Check the type and possibly the default value.
1638 */
1639 switch (type) {
1640 case XML_ATTRIBUTE_CDATA:
1641 break;
1642 case XML_ATTRIBUTE_ID:
1643 break;
1644 case XML_ATTRIBUTE_IDREF:
1645 break;
1646 case XML_ATTRIBUTE_IDREFS:
1647 break;
1648 case XML_ATTRIBUTE_ENTITY:
1649 break;
1650 case XML_ATTRIBUTE_ENTITIES:
1651 break;
1652 case XML_ATTRIBUTE_NMTOKEN:
1653 break;
1654 case XML_ATTRIBUTE_NMTOKENS:
1655 break;
1656 case XML_ATTRIBUTE_ENUMERATION:
1657 break;
1658 case XML_ATTRIBUTE_NOTATION:
1659 break;
1660 default:
1661 xmlGenericError(xmlGenericErrorContext,
1662 "xmlAddAttributeDecl: unknown type %d\n", type);
1663 xmlFreeEnumeration(tree);
1664 return(NULL);
1665 }
1666 if ((defaultValue != NULL) &&
1667 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillard58e44c92002-08-02 22:19:49 +00001668 VERROR(ctxt->userData, "Attribute %s of %s: invalid default value\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001669 elem, name, defaultValue);
1670 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001671 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001672 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001673#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001674
1675 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001676 * Check first that an attribute defined in the external subset wasn't
1677 * already defined in the internal subset
1678 */
1679 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1680 (dtd->doc->intSubset != NULL) &&
1681 (dtd->doc->intSubset->attributes != NULL)) {
1682 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1683 if (ret != NULL)
1684 return(NULL);
1685 }
1686
1687 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001688 * Create the Attribute table if needed.
1689 */
1690 table = (xmlAttributeTablePtr) dtd->attributes;
1691 if (table == NULL) {
1692 table = xmlCreateAttributeTable();
1693 dtd->attributes = (void *) table;
1694 }
1695 if (table == NULL) {
1696 xmlGenericError(xmlGenericErrorContext,
1697 "xmlAddAttributeDecl: Table creation failed!\n");
1698 return(NULL);
1699 }
1700
1701
1702 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1703 if (ret == NULL) {
1704 xmlGenericError(xmlGenericErrorContext,
1705 "xmlAddAttributeDecl: out of memory\n");
1706 return(NULL);
1707 }
1708 memset(ret, 0, sizeof(xmlAttribute));
1709 ret->type = XML_ATTRIBUTE_DECL;
1710
1711 /*
1712 * fill the structure.
1713 */
1714 ret->atype = type;
1715 ret->name = xmlStrdup(name);
1716 ret->prefix = xmlStrdup(ns);
1717 ret->elem = xmlStrdup(elem);
1718 ret->def = def;
1719 ret->tree = tree;
1720 if (defaultValue != NULL)
1721 ret->defaultValue = xmlStrdup(defaultValue);
1722
1723 /*
1724 * Validity Check:
1725 * Search the DTD for previous declarations of the ATTLIST
1726 */
1727 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001728#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001729 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001730 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001731 */
1732 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00001733 "Attribute %s of element %s: already defined\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001734 name, elem);
Daniel Veillard4432df22003-09-28 18:58:27 +00001735#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001736 xmlFreeAttribute(ret);
1737 return(NULL);
1738 }
1739
1740 /*
1741 * Validity Check:
1742 * Multiple ID per element
1743 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001744 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001745 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001746
Daniel Veillard4432df22003-09-28 18:58:27 +00001747#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001748 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001749 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001750 VERROR(ctxt->userData,
1751 "Element %s has too may ID attributes defined : %s\n",
1752 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001753 ctxt->valid = 0;
1754 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001755#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardc7612992002-02-17 22:47:37 +00001756
Daniel Veillard48da9102001-08-07 01:10:10 +00001757 /*
1758 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001759 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001760 */
1761 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1762 ((ret->prefix != NULL &&
1763 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1764 ret->nexth = elemDef->attributes;
1765 elemDef->attributes = ret;
1766 } else {
1767 xmlAttributePtr tmp = elemDef->attributes;
1768
1769 while ((tmp != NULL) &&
1770 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1771 ((ret->prefix != NULL &&
1772 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1773 if (tmp->nexth == NULL)
1774 break;
1775 tmp = tmp->nexth;
1776 }
1777 if (tmp != NULL) {
1778 ret->nexth = tmp->nexth;
1779 tmp->nexth = ret;
1780 } else {
1781 ret->nexth = elemDef->attributes;
1782 elemDef->attributes = ret;
1783 }
1784 }
Owen Taylor3473f882001-02-23 17:55:21 +00001785 }
1786
1787 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001788 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001789 */
1790 ret->parent = dtd;
1791 ret->doc = dtd->doc;
1792 if (dtd->last == NULL) {
1793 dtd->children = dtd->last = (xmlNodePtr) ret;
1794 } else {
1795 dtd->last->next = (xmlNodePtr) ret;
1796 ret->prev = dtd->last;
1797 dtd->last = (xmlNodePtr) ret;
1798 }
1799 return(ret);
1800}
1801
1802/**
1803 * xmlFreeAttributeTable:
1804 * @table: An attribute table
1805 *
1806 * Deallocate the memory used by an entities hash table.
1807 */
1808void
1809xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1810 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1811}
1812
1813/**
1814 * xmlCopyAttribute:
1815 * @attr: An attribute
1816 *
1817 * Build a copy of an attribute.
1818 *
1819 * Returns the new xmlAttributePtr or NULL in case of error.
1820 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001821static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001822xmlCopyAttribute(xmlAttributePtr attr) {
1823 xmlAttributePtr cur;
1824
1825 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1826 if (cur == NULL) {
1827 xmlGenericError(xmlGenericErrorContext,
1828 "xmlCopyAttribute: out of memory !\n");
1829 return(NULL);
1830 }
1831 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001832 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001833 cur->atype = attr->atype;
1834 cur->def = attr->def;
1835 cur->tree = xmlCopyEnumeration(attr->tree);
1836 if (attr->elem != NULL)
1837 cur->elem = xmlStrdup(attr->elem);
1838 if (attr->name != NULL)
1839 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001840 if (attr->prefix != NULL)
1841 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001842 if (attr->defaultValue != NULL)
1843 cur->defaultValue = xmlStrdup(attr->defaultValue);
1844 return(cur);
1845}
1846
1847/**
1848 * xmlCopyAttributeTable:
1849 * @table: An attribute table
1850 *
1851 * Build a copy of an attribute table.
1852 *
1853 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1854 */
1855xmlAttributeTablePtr
1856xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1857 return((xmlAttributeTablePtr) xmlHashCopy(table,
1858 (xmlHashCopier) xmlCopyAttribute));
1859}
1860
1861/**
1862 * xmlDumpAttributeDecl:
1863 * @buf: the XML buffer output
1864 * @attr: An attribute declaration
1865 *
1866 * This will dump the content of the attribute declaration as an XML
1867 * DTD definition
1868 */
1869void
1870xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1871 xmlBufferWriteChar(buf, "<!ATTLIST ");
1872 xmlBufferWriteCHAR(buf, attr->elem);
1873 xmlBufferWriteChar(buf, " ");
1874 if (attr->prefix != NULL) {
1875 xmlBufferWriteCHAR(buf, attr->prefix);
1876 xmlBufferWriteChar(buf, ":");
1877 }
1878 xmlBufferWriteCHAR(buf, attr->name);
1879 switch (attr->atype) {
1880 case XML_ATTRIBUTE_CDATA:
1881 xmlBufferWriteChar(buf, " CDATA");
1882 break;
1883 case XML_ATTRIBUTE_ID:
1884 xmlBufferWriteChar(buf, " ID");
1885 break;
1886 case XML_ATTRIBUTE_IDREF:
1887 xmlBufferWriteChar(buf, " IDREF");
1888 break;
1889 case XML_ATTRIBUTE_IDREFS:
1890 xmlBufferWriteChar(buf, " IDREFS");
1891 break;
1892 case XML_ATTRIBUTE_ENTITY:
1893 xmlBufferWriteChar(buf, " ENTITY");
1894 break;
1895 case XML_ATTRIBUTE_ENTITIES:
1896 xmlBufferWriteChar(buf, " ENTITIES");
1897 break;
1898 case XML_ATTRIBUTE_NMTOKEN:
1899 xmlBufferWriteChar(buf, " NMTOKEN");
1900 break;
1901 case XML_ATTRIBUTE_NMTOKENS:
1902 xmlBufferWriteChar(buf, " NMTOKENS");
1903 break;
1904 case XML_ATTRIBUTE_ENUMERATION:
1905 xmlBufferWriteChar(buf, " (");
1906 xmlDumpEnumeration(buf, attr->tree);
1907 break;
1908 case XML_ATTRIBUTE_NOTATION:
1909 xmlBufferWriteChar(buf, " NOTATION (");
1910 xmlDumpEnumeration(buf, attr->tree);
1911 break;
1912 default:
1913 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001914 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001915 attr->atype);
1916 }
1917 switch (attr->def) {
1918 case XML_ATTRIBUTE_NONE:
1919 break;
1920 case XML_ATTRIBUTE_REQUIRED:
1921 xmlBufferWriteChar(buf, " #REQUIRED");
1922 break;
1923 case XML_ATTRIBUTE_IMPLIED:
1924 xmlBufferWriteChar(buf, " #IMPLIED");
1925 break;
1926 case XML_ATTRIBUTE_FIXED:
1927 xmlBufferWriteChar(buf, " #FIXED");
1928 break;
1929 default:
1930 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001931 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001932 attr->def);
1933 }
1934 if (attr->defaultValue != NULL) {
1935 xmlBufferWriteChar(buf, " ");
1936 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1937 }
1938 xmlBufferWriteChar(buf, ">\n");
1939}
1940
1941/**
1942 * xmlDumpAttributeTable:
1943 * @buf: the XML buffer output
1944 * @table: An attribute table
1945 *
1946 * This will dump the content of the attribute table as an XML DTD definition
1947 */
1948void
1949xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1950 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1951}
1952
1953/************************************************************************
1954 * *
1955 * NOTATIONs *
1956 * *
1957 ************************************************************************/
1958/**
1959 * xmlCreateNotationTable:
1960 *
1961 * create and initialize an empty notation hash table.
1962 *
1963 * Returns the xmlNotationTablePtr just created or NULL in case
1964 * of error.
1965 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001966static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001967xmlCreateNotationTable(void) {
1968 return(xmlHashCreate(0));
1969}
1970
1971/**
1972 * xmlFreeNotation:
1973 * @not: A notation
1974 *
1975 * Deallocate the memory used by an notation definition
1976 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001977static void
Owen Taylor3473f882001-02-23 17:55:21 +00001978xmlFreeNotation(xmlNotationPtr nota) {
1979 if (nota == NULL) return;
1980 if (nota->name != NULL)
1981 xmlFree((xmlChar *) nota->name);
1982 if (nota->PublicID != NULL)
1983 xmlFree((xmlChar *) nota->PublicID);
1984 if (nota->SystemID != NULL)
1985 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001986 xmlFree(nota);
1987}
1988
1989
1990/**
1991 * xmlAddNotationDecl:
1992 * @dtd: pointer to the DTD
1993 * @ctxt: the validation context
1994 * @name: the entity name
1995 * @PublicID: the public identifier or NULL
1996 * @SystemID: the system identifier or NULL
1997 *
1998 * Register a new notation declaration
1999 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002000 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002001 */
2002xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002003xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002004 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002005 const xmlChar *PublicID, const xmlChar *SystemID) {
2006 xmlNotationPtr ret;
2007 xmlNotationTablePtr table;
2008
2009 if (dtd == NULL) {
2010 xmlGenericError(xmlGenericErrorContext,
2011 "xmlAddNotationDecl: dtd == NULL\n");
2012 return(NULL);
2013 }
2014 if (name == NULL) {
2015 xmlGenericError(xmlGenericErrorContext,
2016 "xmlAddNotationDecl: name == NULL\n");
2017 return(NULL);
2018 }
2019 if ((PublicID == NULL) && (SystemID == NULL)) {
2020 xmlGenericError(xmlGenericErrorContext,
2021 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002022 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002023 }
2024
2025 /*
2026 * Create the Notation table if needed.
2027 */
2028 table = (xmlNotationTablePtr) dtd->notations;
2029 if (table == NULL)
2030 dtd->notations = table = xmlCreateNotationTable();
2031 if (table == NULL) {
2032 xmlGenericError(xmlGenericErrorContext,
2033 "xmlAddNotationDecl: Table creation failed!\n");
2034 return(NULL);
2035 }
2036
2037 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2038 if (ret == NULL) {
2039 xmlGenericError(xmlGenericErrorContext,
2040 "xmlAddNotationDecl: out of memory\n");
2041 return(NULL);
2042 }
2043 memset(ret, 0, sizeof(xmlNotation));
2044
2045 /*
2046 * fill the structure.
2047 */
2048 ret->name = xmlStrdup(name);
2049 if (SystemID != NULL)
2050 ret->SystemID = xmlStrdup(SystemID);
2051 if (PublicID != NULL)
2052 ret->PublicID = xmlStrdup(PublicID);
2053
2054 /*
2055 * Validity Check:
2056 * Check the DTD for previous declarations of the ATTLIST
2057 */
2058 if (xmlHashAddEntry(table, name, ret)) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002059#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002060 xmlGenericError(xmlGenericErrorContext,
2061 "xmlAddNotationDecl: %s already defined\n", name);
Daniel Veillard4432df22003-09-28 18:58:27 +00002062#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002063 xmlFreeNotation(ret);
2064 return(NULL);
2065 }
2066 return(ret);
2067}
2068
2069/**
2070 * xmlFreeNotationTable:
2071 * @table: An notation table
2072 *
2073 * Deallocate the memory used by an entities hash table.
2074 */
2075void
2076xmlFreeNotationTable(xmlNotationTablePtr table) {
2077 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2078}
2079
2080/**
2081 * xmlCopyNotation:
2082 * @nota: A notation
2083 *
2084 * Build a copy of a notation.
2085 *
2086 * Returns the new xmlNotationPtr or NULL in case of error.
2087 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002088static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002089xmlCopyNotation(xmlNotationPtr nota) {
2090 xmlNotationPtr cur;
2091
2092 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2093 if (cur == NULL) {
2094 xmlGenericError(xmlGenericErrorContext,
2095 "xmlCopyNotation: out of memory !\n");
2096 return(NULL);
2097 }
2098 if (nota->name != NULL)
2099 cur->name = xmlStrdup(nota->name);
2100 else
2101 cur->name = NULL;
2102 if (nota->PublicID != NULL)
2103 cur->PublicID = xmlStrdup(nota->PublicID);
2104 else
2105 cur->PublicID = NULL;
2106 if (nota->SystemID != NULL)
2107 cur->SystemID = xmlStrdup(nota->SystemID);
2108 else
2109 cur->SystemID = NULL;
2110 return(cur);
2111}
2112
2113/**
2114 * xmlCopyNotationTable:
2115 * @table: A notation table
2116 *
2117 * Build a copy of a notation table.
2118 *
2119 * Returns the new xmlNotationTablePtr or NULL in case of error.
2120 */
2121xmlNotationTablePtr
2122xmlCopyNotationTable(xmlNotationTablePtr table) {
2123 return((xmlNotationTablePtr) xmlHashCopy(table,
2124 (xmlHashCopier) xmlCopyNotation));
2125}
2126
2127/**
2128 * xmlDumpNotationDecl:
2129 * @buf: the XML buffer output
2130 * @nota: A notation declaration
2131 *
2132 * This will dump the content the notation declaration as an XML DTD definition
2133 */
2134void
2135xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2136 xmlBufferWriteChar(buf, "<!NOTATION ");
2137 xmlBufferWriteCHAR(buf, nota->name);
2138 if (nota->PublicID != NULL) {
2139 xmlBufferWriteChar(buf, " PUBLIC ");
2140 xmlBufferWriteQuotedString(buf, nota->PublicID);
2141 if (nota->SystemID != NULL) {
2142 xmlBufferWriteChar(buf, " ");
2143 xmlBufferWriteCHAR(buf, nota->SystemID);
2144 }
2145 } else {
2146 xmlBufferWriteChar(buf, " SYSTEM ");
2147 xmlBufferWriteCHAR(buf, nota->SystemID);
2148 }
2149 xmlBufferWriteChar(buf, " >\n");
2150}
2151
2152/**
2153 * xmlDumpNotationTable:
2154 * @buf: the XML buffer output
2155 * @table: A notation table
2156 *
2157 * This will dump the content of the notation table as an XML DTD definition
2158 */
2159void
2160xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2161 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
2162}
2163
2164/************************************************************************
2165 * *
2166 * IDs *
2167 * *
2168 ************************************************************************/
2169/**
2170 * xmlCreateIDTable:
2171 *
2172 * create and initialize an empty id hash table.
2173 *
2174 * Returns the xmlIDTablePtr just created or NULL in case
2175 * of error.
2176 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002177static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002178xmlCreateIDTable(void) {
2179 return(xmlHashCreate(0));
2180}
2181
2182/**
2183 * xmlFreeID:
2184 * @not: A id
2185 *
2186 * Deallocate the memory used by an id definition
2187 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002188static void
Owen Taylor3473f882001-02-23 17:55:21 +00002189xmlFreeID(xmlIDPtr id) {
2190 if (id == NULL) return;
2191 if (id->value != NULL)
2192 xmlFree((xmlChar *) id->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002193 if (id->name != NULL)
2194 xmlFree((xmlChar *) id->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002195 xmlFree(id);
2196}
2197
2198/**
2199 * xmlAddID:
2200 * @ctxt: the validation context
2201 * @doc: pointer to the document
2202 * @value: the value name
2203 * @attr: the attribute holding the ID
2204 *
2205 * Register a new id declaration
2206 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002207 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002208 */
2209xmlIDPtr
2210xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2211 xmlAttrPtr attr) {
2212 xmlIDPtr ret;
2213 xmlIDTablePtr table;
2214
2215 if (doc == NULL) {
2216 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002217 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002218 return(NULL);
2219 }
2220 if (value == NULL) {
2221 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002222 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002223 return(NULL);
2224 }
2225 if (attr == NULL) {
2226 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002227 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002228 return(NULL);
2229 }
2230
2231 /*
2232 * Create the ID table if needed.
2233 */
2234 table = (xmlIDTablePtr) doc->ids;
2235 if (table == NULL)
2236 doc->ids = table = xmlCreateIDTable();
2237 if (table == NULL) {
2238 xmlGenericError(xmlGenericErrorContext,
2239 "xmlAddID: Table creation failed!\n");
2240 return(NULL);
2241 }
2242
2243 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2244 if (ret == NULL) {
2245 xmlGenericError(xmlGenericErrorContext,
2246 "xmlAddID: out of memory\n");
2247 return(NULL);
2248 }
2249
2250 /*
2251 * fill the structure.
2252 */
2253 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002254 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2255 /*
2256 * Operating in streaming mode, attr is gonna disapear
2257 */
2258 ret->name = xmlStrdup(attr->name);
2259 ret->attr = NULL;
2260 } else {
2261 ret->attr = attr;
2262 ret->name = NULL;
2263 }
2264 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002265
2266 if (xmlHashAddEntry(table, value, ret) < 0) {
Daniel Veillard4432df22003-09-28 18:58:27 +00002267#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002268 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002269 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002270 */
Daniel Veillard76575762002-09-05 14:21:15 +00002271 if (ctxt != NULL) {
2272 VECTXT(ctxt, attr->parent);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002273 VERROR(ctxt->userData, "ID %s already defined\n", value);
Daniel Veillard76575762002-09-05 14:21:15 +00002274 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002275#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002276 xmlFreeID(ret);
2277 return(NULL);
2278 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002279 if (attr != NULL)
2280 attr->atype = XML_ATTRIBUTE_ID;
Owen Taylor3473f882001-02-23 17:55:21 +00002281 return(ret);
2282}
2283
2284/**
2285 * xmlFreeIDTable:
2286 * @table: An id table
2287 *
2288 * Deallocate the memory used by an ID hash table.
2289 */
2290void
2291xmlFreeIDTable(xmlIDTablePtr table) {
2292 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2293}
2294
2295/**
2296 * xmlIsID:
2297 * @doc: the document
2298 * @elem: the element carrying the attribute
2299 * @attr: the attribute
2300 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002301 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002302 * then this is done if DTD loading has been requested. In the case
2303 * of HTML documents parsed with the HTML parser, then ID detection is
2304 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002305 *
2306 * Returns 0 or 1 depending on the lookup result
2307 */
2308int
2309xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2310 if (doc == NULL) return(0);
2311 if (attr == NULL) return(0);
2312 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2313 return(0);
2314 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2315 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2316 (xmlStrEqual(BAD_CAST "name", attr->name)))
2317 return(1);
2318 return(0);
2319 } else {
2320 xmlAttributePtr attrDecl;
2321
2322 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002323 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00002324 xmlChar fn[50];
Daniel Veillard37f961d2002-07-06 17:53:56 +00002325 xmlChar *fullname;
Daniel Veillardc00cda82003-04-07 10:22:39 +00002326
2327 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002328 if (fullname == NULL)
2329 return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002330 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2331 attr->name);
2332 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2333 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2334 attr->name);
Daniel Veillardc00cda82003-04-07 10:22:39 +00002335 if ((fullname != fn) && (fullname != elem->name))
2336 xmlFree(fullname);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002337 } else {
2338 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2339 attr->name);
2340 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2341 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2342 attr->name);
2343 }
Owen Taylor3473f882001-02-23 17:55:21 +00002344
2345 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2346 return(1);
2347 }
2348 return(0);
2349}
2350
2351/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002352 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002353 * @doc: the document
2354 * @attr: the attribute
2355 *
2356 * Remove the given attribute from the ID table maintained internally.
2357 *
2358 * Returns -1 if the lookup failed and 0 otherwise
2359 */
2360int
2361xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2362 xmlAttrPtr cur;
2363 xmlIDTablePtr table;
2364 xmlChar *ID;
2365
2366 if (doc == NULL) return(-1);
2367 if (attr == NULL) return(-1);
2368 table = (xmlIDTablePtr) doc->ids;
2369 if (table == NULL)
2370 return(-1);
2371
2372 if (attr == NULL)
2373 return(-1);
2374 ID = xmlNodeListGetString(doc, attr->children, 1);
2375 if (ID == NULL)
2376 return(-1);
2377 cur = xmlHashLookup(table, ID);
2378 if (cur != attr) {
2379 xmlFree(ID);
2380 return(-1);
2381 }
2382 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
2383 xmlFree(ID);
2384 return(0);
2385}
2386
2387/**
2388 * xmlGetID:
2389 * @doc: pointer to the document
2390 * @ID: the ID value
2391 *
2392 * Search the attribute declaring the given ID
2393 *
2394 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2395 */
2396xmlAttrPtr
2397xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2398 xmlIDTablePtr table;
2399 xmlIDPtr id;
2400
2401 if (doc == NULL) {
2402 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
2403 return(NULL);
2404 }
2405
2406 if (ID == NULL) {
2407 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
2408 return(NULL);
2409 }
2410
2411 table = (xmlIDTablePtr) doc->ids;
2412 if (table == NULL)
2413 return(NULL);
2414
2415 id = xmlHashLookup(table, ID);
2416 if (id == NULL)
2417 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002418 if (id->attr == NULL) {
2419 /*
2420 * We are operating on a stream, return a well known reference
2421 * since the attribute node doesn't exist anymore
2422 */
2423 return((xmlAttrPtr) doc);
2424 }
Owen Taylor3473f882001-02-23 17:55:21 +00002425 return(id->attr);
2426}
2427
2428/************************************************************************
2429 * *
2430 * Refs *
2431 * *
2432 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002433typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002434{
2435 xmlListPtr l;
2436 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002437} xmlRemoveMemo;
2438
2439typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2440
2441typedef struct xmlValidateMemo_t
2442{
2443 xmlValidCtxtPtr ctxt;
2444 const xmlChar *name;
2445} xmlValidateMemo;
2446
2447typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002448
2449/**
2450 * xmlCreateRefTable:
2451 *
2452 * create and initialize an empty ref hash table.
2453 *
2454 * Returns the xmlRefTablePtr just created or NULL in case
2455 * of error.
2456 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002457static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002458xmlCreateRefTable(void) {
2459 return(xmlHashCreate(0));
2460}
2461
2462/**
2463 * xmlFreeRef:
2464 * @lk: A list link
2465 *
2466 * Deallocate the memory used by a ref definition
2467 */
2468static void
2469xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002470 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2471 if (ref == NULL) return;
2472 if (ref->value != NULL)
2473 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002474 if (ref->name != NULL)
2475 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002476 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002477}
2478
2479/**
2480 * xmlFreeRefList:
2481 * @list_ref: A list of references.
2482 *
2483 * Deallocate the memory used by a list of references
2484 */
2485static void
2486xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002487 if (list_ref == NULL) return;
2488 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002489}
2490
2491/**
2492 * xmlWalkRemoveRef:
2493 * @data: Contents of current link
2494 * @user: Value supplied by the user
2495 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002496 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002497 */
2498static int
2499xmlWalkRemoveRef(const void *data, const void *user)
2500{
Daniel Veillard37721922001-05-04 15:21:12 +00002501 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2502 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2503 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002504
Daniel Veillard37721922001-05-04 15:21:12 +00002505 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2506 xmlListRemoveFirst(ref_list, (void *)data);
2507 return 0;
2508 }
2509 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002510}
2511
2512/**
2513 * xmlAddRef:
2514 * @ctxt: the validation context
2515 * @doc: pointer to the document
2516 * @value: the value name
2517 * @attr: the attribute holding the Ref
2518 *
2519 * Register a new ref declaration
2520 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002521 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002522 */
2523xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002524xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002525 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002526 xmlRefPtr ret;
2527 xmlRefTablePtr table;
2528 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002529
Daniel Veillard37721922001-05-04 15:21:12 +00002530 if (doc == NULL) {
2531 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002532 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002533 return(NULL);
2534 }
2535 if (value == NULL) {
2536 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002537 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002538 return(NULL);
2539 }
2540 if (attr == NULL) {
2541 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002542 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002543 return(NULL);
2544 }
Owen Taylor3473f882001-02-23 17:55:21 +00002545
Daniel Veillard37721922001-05-04 15:21:12 +00002546 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002547 * Create the Ref table if needed.
2548 */
Daniel Veillard37721922001-05-04 15:21:12 +00002549 table = (xmlRefTablePtr) doc->refs;
2550 if (table == NULL)
2551 doc->refs = table = xmlCreateRefTable();
2552 if (table == NULL) {
2553 xmlGenericError(xmlGenericErrorContext,
2554 "xmlAddRef: Table creation failed!\n");
2555 return(NULL);
2556 }
Owen Taylor3473f882001-02-23 17:55:21 +00002557
Daniel Veillard37721922001-05-04 15:21:12 +00002558 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2559 if (ret == NULL) {
2560 xmlGenericError(xmlGenericErrorContext,
2561 "xmlAddRef: out of memory\n");
2562 return(NULL);
2563 }
Owen Taylor3473f882001-02-23 17:55:21 +00002564
Daniel Veillard37721922001-05-04 15:21:12 +00002565 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002566 * fill the structure.
2567 */
Daniel Veillard37721922001-05-04 15:21:12 +00002568 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002569 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2570 /*
2571 * Operating in streaming mode, attr is gonna disapear
2572 */
2573 ret->name = xmlStrdup(attr->name);
2574 ret->attr = NULL;
2575 } else {
2576 ret->name = NULL;
2577 ret->attr = attr;
2578 }
2579 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002580
Daniel Veillard37721922001-05-04 15:21:12 +00002581 /* To add a reference :-
2582 * References are maintained as a list of references,
2583 * Lookup the entry, if no entry create new nodelist
2584 * Add the owning node to the NodeList
2585 * Return the ref
2586 */
Owen Taylor3473f882001-02-23 17:55:21 +00002587
Daniel Veillard37721922001-05-04 15:21:12 +00002588 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2589 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2590 xmlGenericError(xmlGenericErrorContext,
2591 "xmlAddRef: Reference list creation failed!\n");
2592 return(NULL);
2593 }
2594 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2595 xmlListDelete(ref_list);
2596 xmlGenericError(xmlGenericErrorContext,
2597 "xmlAddRef: Reference list insertion failed!\n");
2598 return(NULL);
2599 }
2600 }
2601 xmlListInsert(ref_list, ret);
2602 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002603}
2604
2605/**
2606 * xmlFreeRefTable:
2607 * @table: An ref table
2608 *
2609 * Deallocate the memory used by an Ref hash table.
2610 */
2611void
2612xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002613 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002614}
2615
2616/**
2617 * xmlIsRef:
2618 * @doc: the document
2619 * @elem: the element carrying the attribute
2620 * @attr: the attribute
2621 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002622 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002623 * then this is simple, otherwise we use an heuristic: name Ref (upper
2624 * or lowercase).
2625 *
2626 * Returns 0 or 1 depending on the lookup result
2627 */
2628int
2629xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002630 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2631 return(0);
2632 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2633 /* TODO @@@ */
2634 return(0);
2635 } else {
2636 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002637
Daniel Veillard37721922001-05-04 15:21:12 +00002638 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2639 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2640 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2641 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002642
Daniel Veillard37721922001-05-04 15:21:12 +00002643 if ((attrDecl != NULL) &&
2644 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2645 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2646 return(1);
2647 }
2648 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002649}
2650
2651/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002652 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00002653 * @doc: the document
2654 * @attr: the attribute
2655 *
2656 * Remove the given attribute from the Ref table maintained internally.
2657 *
2658 * Returns -1 if the lookup failed and 0 otherwise
2659 */
2660int
2661xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002662 xmlListPtr ref_list;
2663 xmlRefTablePtr table;
2664 xmlChar *ID;
2665 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002666
Daniel Veillard37721922001-05-04 15:21:12 +00002667 if (doc == NULL) return(-1);
2668 if (attr == NULL) return(-1);
2669 table = (xmlRefTablePtr) doc->refs;
2670 if (table == NULL)
2671 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002672
Daniel Veillard37721922001-05-04 15:21:12 +00002673 if (attr == NULL)
2674 return(-1);
2675 ID = xmlNodeListGetString(doc, attr->children, 1);
2676 if (ID == NULL)
2677 return(-1);
2678 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002679
Daniel Veillard37721922001-05-04 15:21:12 +00002680 if(ref_list == NULL) {
2681 xmlFree(ID);
2682 return (-1);
2683 }
2684 /* At this point, ref_list refers to a list of references which
2685 * have the same key as the supplied attr. Our list of references
2686 * is ordered by reference address and we don't have that information
2687 * here to use when removing. We'll have to walk the list and
2688 * check for a matching attribute, when we find one stop the walk
2689 * and remove the entry.
2690 * The list is ordered by reference, so that means we don't have the
2691 * key. Passing the list and the reference to the walker means we
2692 * will have enough data to be able to remove the entry.
2693 */
2694 target.l = ref_list;
2695 target.ap = attr;
2696
2697 /* Remove the supplied attr from our list */
2698 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002699
Daniel Veillard37721922001-05-04 15:21:12 +00002700 /*If the list is empty then remove the list entry in the hash */
2701 if (xmlListEmpty(ref_list))
2702 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2703 xmlFreeRefList);
2704 xmlFree(ID);
2705 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002706}
2707
2708/**
2709 * xmlGetRefs:
2710 * @doc: pointer to the document
2711 * @ID: the ID value
2712 *
2713 * Find the set of references for the supplied ID.
2714 *
2715 * Returns NULL if not found, otherwise node set for the ID.
2716 */
2717xmlListPtr
2718xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002719 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002720
Daniel Veillard37721922001-05-04 15:21:12 +00002721 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002722 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002723 return(NULL);
2724 }
Owen Taylor3473f882001-02-23 17:55:21 +00002725
Daniel Veillard37721922001-05-04 15:21:12 +00002726 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002727 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002728 return(NULL);
2729 }
Owen Taylor3473f882001-02-23 17:55:21 +00002730
Daniel Veillard37721922001-05-04 15:21:12 +00002731 table = (xmlRefTablePtr) doc->refs;
2732 if (table == NULL)
2733 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002734
Daniel Veillard37721922001-05-04 15:21:12 +00002735 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002736}
2737
2738/************************************************************************
2739 * *
2740 * Routines for validity checking *
2741 * *
2742 ************************************************************************/
2743
2744/**
2745 * xmlGetDtdElementDesc:
2746 * @dtd: a pointer to the DtD to search
2747 * @name: the element name
2748 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002749 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002750 *
2751 * returns the xmlElementPtr if found or NULL
2752 */
2753
2754xmlElementPtr
2755xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2756 xmlElementTablePtr table;
2757 xmlElementPtr cur;
2758 xmlChar *uqname = NULL, *prefix = NULL;
2759
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002760 if ((dtd == NULL) || (name == NULL)) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002761 if (dtd->elements == NULL)
2762 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002763 table = (xmlElementTablePtr) dtd->elements;
2764
2765 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002766 if (uqname != NULL)
2767 name = uqname;
2768 cur = xmlHashLookup2(table, name, prefix);
2769 if (prefix != NULL) xmlFree(prefix);
2770 if (uqname != NULL) xmlFree(uqname);
2771 return(cur);
2772}
2773/**
2774 * xmlGetDtdElementDesc2:
2775 * @dtd: a pointer to the DtD to search
2776 * @name: the element name
2777 * @create: create an empty description if not found
2778 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002779 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002780 *
2781 * returns the xmlElementPtr if found or NULL
2782 */
2783
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002784static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002785xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2786 xmlElementTablePtr table;
2787 xmlElementPtr cur;
2788 xmlChar *uqname = NULL, *prefix = NULL;
2789
2790 if (dtd == NULL) return(NULL);
2791 if (dtd->elements == NULL) {
2792 if (!create)
2793 return(NULL);
2794 /*
2795 * Create the Element table if needed.
2796 */
2797 table = (xmlElementTablePtr) dtd->elements;
2798 if (table == NULL) {
2799 table = xmlCreateElementTable();
2800 dtd->elements = (void *) table;
2801 }
2802 if (table == NULL) {
2803 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002804 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002805 return(NULL);
2806 }
2807 }
2808 table = (xmlElementTablePtr) dtd->elements;
2809
2810 uqname = xmlSplitQName2(name, &prefix);
2811 if (uqname != NULL)
2812 name = uqname;
2813 cur = xmlHashLookup2(table, name, prefix);
2814 if ((cur == NULL) && (create)) {
2815 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2816 if (cur == NULL) {
2817 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002818 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002819 return(NULL);
2820 }
2821 memset(cur, 0, sizeof(xmlElement));
2822 cur->type = XML_ELEMENT_DECL;
2823
2824 /*
2825 * fill the structure.
2826 */
2827 cur->name = xmlStrdup(name);
2828 cur->prefix = xmlStrdup(prefix);
2829 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2830
2831 xmlHashAddEntry2(table, name, prefix, cur);
2832 }
2833 if (prefix != NULL) xmlFree(prefix);
2834 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002835 return(cur);
2836}
2837
2838/**
2839 * xmlGetDtdQElementDesc:
2840 * @dtd: a pointer to the DtD to search
2841 * @name: the element name
2842 * @prefix: the element namespace prefix
2843 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002844 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002845 *
2846 * returns the xmlElementPtr if found or NULL
2847 */
2848
Daniel Veillard48da9102001-08-07 01:10:10 +00002849xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002850xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2851 const xmlChar *prefix) {
2852 xmlElementTablePtr table;
2853
2854 if (dtd == NULL) return(NULL);
2855 if (dtd->elements == NULL) return(NULL);
2856 table = (xmlElementTablePtr) dtd->elements;
2857
2858 return(xmlHashLookup2(table, name, prefix));
2859}
2860
2861/**
2862 * xmlGetDtdAttrDesc:
2863 * @dtd: a pointer to the DtD to search
2864 * @elem: the element name
2865 * @name: the attribute name
2866 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002867 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002868 * this element.
2869 *
2870 * returns the xmlAttributePtr if found or NULL
2871 */
2872
2873xmlAttributePtr
2874xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2875 xmlAttributeTablePtr table;
2876 xmlAttributePtr cur;
2877 xmlChar *uqname = NULL, *prefix = NULL;
2878
2879 if (dtd == NULL) return(NULL);
2880 if (dtd->attributes == NULL) return(NULL);
2881
2882 table = (xmlAttributeTablePtr) dtd->attributes;
2883 if (table == NULL)
2884 return(NULL);
2885
2886 uqname = xmlSplitQName2(name, &prefix);
2887
2888 if (uqname != NULL) {
2889 cur = xmlHashLookup3(table, uqname, prefix, elem);
2890 if (prefix != NULL) xmlFree(prefix);
2891 if (uqname != NULL) xmlFree(uqname);
2892 } else
2893 cur = xmlHashLookup3(table, name, NULL, elem);
2894 return(cur);
2895}
2896
2897/**
2898 * xmlGetDtdQAttrDesc:
2899 * @dtd: a pointer to the DtD to search
2900 * @elem: the element name
2901 * @name: the attribute name
2902 * @prefix: the attribute namespace prefix
2903 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002904 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002905 * this element.
2906 *
2907 * returns the xmlAttributePtr if found or NULL
2908 */
2909
Daniel Veillard48da9102001-08-07 01:10:10 +00002910xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002911xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2912 const xmlChar *prefix) {
2913 xmlAttributeTablePtr table;
2914
2915 if (dtd == NULL) return(NULL);
2916 if (dtd->attributes == NULL) return(NULL);
2917 table = (xmlAttributeTablePtr) dtd->attributes;
2918
2919 return(xmlHashLookup3(table, name, prefix, elem));
2920}
2921
2922/**
2923 * xmlGetDtdNotationDesc:
2924 * @dtd: a pointer to the DtD to search
2925 * @name: the notation name
2926 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002927 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002928 *
2929 * returns the xmlNotationPtr if found or NULL
2930 */
2931
2932xmlNotationPtr
2933xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2934 xmlNotationTablePtr table;
2935
2936 if (dtd == NULL) return(NULL);
2937 if (dtd->notations == NULL) return(NULL);
2938 table = (xmlNotationTablePtr) dtd->notations;
2939
2940 return(xmlHashLookup(table, name));
2941}
2942
Daniel Veillard4432df22003-09-28 18:58:27 +00002943#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002944/**
2945 * xmlValidateNotationUse:
2946 * @ctxt: the validation context
2947 * @doc: the document
2948 * @notationName: the notation name to check
2949 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002950 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002951 * - [ VC: Notation Declared ]
2952 *
2953 * returns 1 if valid or 0 otherwise
2954 */
2955
2956int
2957xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2958 const xmlChar *notationName) {
2959 xmlNotationPtr notaDecl;
2960 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2961
2962 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2963 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2964 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2965
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002966 if ((notaDecl == NULL) && (ctxt != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002967 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2968 notationName);
2969 return(0);
2970 }
2971 return(1);
2972}
Daniel Veillard4432df22003-09-28 18:58:27 +00002973#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002974
2975/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002976 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00002977 * @doc: the document
2978 * @name: the element name
2979 *
2980 * Search in the DtDs whether an element accept Mixed content (or ANY)
2981 * basically if it is supposed to accept text childs
2982 *
2983 * returns 0 if no, 1 if yes, and -1 if no element description is available
2984 */
2985
2986int
2987xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2988 xmlElementPtr elemDecl;
2989
2990 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2991
2992 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2993 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2994 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2995 if (elemDecl == NULL) return(-1);
2996 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002997 case XML_ELEMENT_TYPE_UNDEFINED:
2998 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002999 case XML_ELEMENT_TYPE_ELEMENT:
3000 return(0);
3001 case XML_ELEMENT_TYPE_EMPTY:
3002 /*
3003 * return 1 for EMPTY since we want VC error to pop up
3004 * on <empty> </empty> for example
3005 */
3006 case XML_ELEMENT_TYPE_ANY:
3007 case XML_ELEMENT_TYPE_MIXED:
3008 return(1);
3009 }
3010 return(1);
3011}
3012
Daniel Veillard4432df22003-09-28 18:58:27 +00003013#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003014/**
3015 * xmlValidateNameValue:
3016 * @value: an Name value
3017 *
3018 * Validate that the given value match Name production
3019 *
3020 * returns 1 if valid or 0 otherwise
3021 */
3022
Daniel Veillard9b731d72002-04-14 12:56:08 +00003023int
Owen Taylor3473f882001-02-23 17:55:21 +00003024xmlValidateNameValue(const xmlChar *value) {
3025 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003026 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003027
3028 if (value == NULL) return(0);
3029 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003030 val = xmlStringCurrentChar(NULL, cur, &len);
3031 cur += len;
3032 if (!IS_LETTER(val) && (val != '_') &&
3033 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003034 return(0);
3035 }
3036
Daniel Veillardd8224e02002-01-13 15:43:22 +00003037 val = xmlStringCurrentChar(NULL, cur, &len);
3038 cur += len;
3039 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3040 (val == '.') || (val == '-') ||
3041 (val == '_') || (val == ':') ||
3042 (IS_COMBINING(val)) ||
3043 (IS_EXTENDER(val))) {
3044 val = xmlStringCurrentChar(NULL, cur, &len);
3045 cur += len;
3046 }
Owen Taylor3473f882001-02-23 17:55:21 +00003047
Daniel Veillardd8224e02002-01-13 15:43:22 +00003048 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003049
3050 return(1);
3051}
3052
3053/**
3054 * xmlValidateNamesValue:
3055 * @value: an Names value
3056 *
3057 * Validate that the given value match Names production
3058 *
3059 * returns 1 if valid or 0 otherwise
3060 */
3061
Daniel Veillard9b731d72002-04-14 12:56:08 +00003062int
Owen Taylor3473f882001-02-23 17:55:21 +00003063xmlValidateNamesValue(const xmlChar *value) {
3064 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003065 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003066
3067 if (value == NULL) return(0);
3068 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003069 val = xmlStringCurrentChar(NULL, cur, &len);
3070 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003071
Daniel Veillardd8224e02002-01-13 15:43:22 +00003072 if (!IS_LETTER(val) && (val != '_') &&
3073 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003074 return(0);
3075 }
3076
Daniel Veillardd8224e02002-01-13 15:43:22 +00003077 val = xmlStringCurrentChar(NULL, cur, &len);
3078 cur += len;
3079 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3080 (val == '.') || (val == '-') ||
3081 (val == '_') || (val == ':') ||
3082 (IS_COMBINING(val)) ||
3083 (IS_EXTENDER(val))) {
3084 val = xmlStringCurrentChar(NULL, cur, &len);
3085 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003086 }
3087
Daniel Veillardd8224e02002-01-13 15:43:22 +00003088 while (IS_BLANK(val)) {
3089 while (IS_BLANK(val)) {
3090 val = xmlStringCurrentChar(NULL, cur, &len);
3091 cur += len;
3092 }
3093
3094 if (!IS_LETTER(val) && (val != '_') &&
3095 (val != ':')) {
3096 return(0);
3097 }
3098 val = xmlStringCurrentChar(NULL, cur, &len);
3099 cur += len;
3100
3101 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3102 (val == '.') || (val == '-') ||
3103 (val == '_') || (val == ':') ||
3104 (IS_COMBINING(val)) ||
3105 (IS_EXTENDER(val))) {
3106 val = xmlStringCurrentChar(NULL, cur, &len);
3107 cur += len;
3108 }
3109 }
3110
3111 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003112
3113 return(1);
3114}
3115
3116/**
3117 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003118 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003119 *
3120 * Validate that the given value match Nmtoken production
3121 *
3122 * [ VC: Name Token ]
3123 *
3124 * returns 1 if valid or 0 otherwise
3125 */
3126
Daniel Veillard9b731d72002-04-14 12:56:08 +00003127int
Owen Taylor3473f882001-02-23 17:55:21 +00003128xmlValidateNmtokenValue(const xmlChar *value) {
3129 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003130 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003131
3132 if (value == NULL) return(0);
3133 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003134 val = xmlStringCurrentChar(NULL, cur, &len);
3135 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003136
Daniel Veillardd8224e02002-01-13 15:43:22 +00003137 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3138 (val != '.') && (val != '-') &&
3139 (val != '_') && (val != ':') &&
3140 (!IS_COMBINING(val)) &&
3141 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003142 return(0);
3143
Daniel Veillardd8224e02002-01-13 15:43:22 +00003144 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3145 (val == '.') || (val == '-') ||
3146 (val == '_') || (val == ':') ||
3147 (IS_COMBINING(val)) ||
3148 (IS_EXTENDER(val))) {
3149 val = xmlStringCurrentChar(NULL, cur, &len);
3150 cur += len;
3151 }
Owen Taylor3473f882001-02-23 17:55:21 +00003152
Daniel Veillardd8224e02002-01-13 15:43:22 +00003153 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003154
3155 return(1);
3156}
3157
3158/**
3159 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003160 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003161 *
3162 * Validate that the given value match Nmtokens production
3163 *
3164 * [ VC: Name Token ]
3165 *
3166 * returns 1 if valid or 0 otherwise
3167 */
3168
Daniel Veillard9b731d72002-04-14 12:56:08 +00003169int
Owen Taylor3473f882001-02-23 17:55:21 +00003170xmlValidateNmtokensValue(const xmlChar *value) {
3171 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003172 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003173
3174 if (value == NULL) return(0);
3175 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003176 val = xmlStringCurrentChar(NULL, cur, &len);
3177 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003178
Daniel Veillardd8224e02002-01-13 15:43:22 +00003179 while (IS_BLANK(val)) {
3180 val = xmlStringCurrentChar(NULL, cur, &len);
3181 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003182 }
3183
Daniel Veillardd8224e02002-01-13 15:43:22 +00003184 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 while (IS_BLANK(val)) {
3201 while (IS_BLANK(val)) {
3202 val = xmlStringCurrentChar(NULL, cur, &len);
3203 cur += len;
3204 }
3205 if (val == 0) return(1);
3206
3207 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3208 (val != '.') && (val != '-') &&
3209 (val != '_') && (val != ':') &&
3210 (!IS_COMBINING(val)) &&
3211 (!IS_EXTENDER(val)))
3212 return(0);
3213
3214 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3215 (val == '.') || (val == '-') ||
3216 (val == '_') || (val == ':') ||
3217 (IS_COMBINING(val)) ||
3218 (IS_EXTENDER(val))) {
3219 val = xmlStringCurrentChar(NULL, cur, &len);
3220 cur += len;
3221 }
3222 }
3223
3224 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003225
3226 return(1);
3227}
3228
3229/**
3230 * xmlValidateNotationDecl:
3231 * @ctxt: the validation context
3232 * @doc: a document instance
3233 * @nota: a notation definition
3234 *
3235 * Try to validate a single notation definition
3236 * basically it does the following checks as described by the
3237 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003238 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003239 * But this function get called anyway ...
3240 *
3241 * returns 1 if valid or 0 otherwise
3242 */
3243
3244int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003245xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3246 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003247 int ret = 1;
3248
3249 return(ret);
3250}
3251
3252/**
3253 * xmlValidateAttributeValue:
3254 * @type: an attribute type
3255 * @value: an attribute value
3256 *
3257 * Validate that the given attribute value match the proper production
3258 *
3259 * [ VC: ID ]
3260 * Values of type ID must match the Name production....
3261 *
3262 * [ VC: IDREF ]
3263 * Values of type IDREF must match the Name production, and values
3264 * of type IDREFS must match Names ...
3265 *
3266 * [ VC: Entity Name ]
3267 * Values of type ENTITY must match the Name production, values
3268 * of type ENTITIES must match Names ...
3269 *
3270 * [ VC: Name Token ]
3271 * Values of type NMTOKEN must match the Nmtoken production; values
3272 * of type NMTOKENS must match Nmtokens.
3273 *
3274 * returns 1 if valid or 0 otherwise
3275 */
3276
3277int
3278xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3279 switch (type) {
3280 case XML_ATTRIBUTE_ENTITIES:
3281 case XML_ATTRIBUTE_IDREFS:
3282 return(xmlValidateNamesValue(value));
3283 case XML_ATTRIBUTE_ENTITY:
3284 case XML_ATTRIBUTE_IDREF:
3285 case XML_ATTRIBUTE_ID:
3286 case XML_ATTRIBUTE_NOTATION:
3287 return(xmlValidateNameValue(value));
3288 case XML_ATTRIBUTE_NMTOKENS:
3289 case XML_ATTRIBUTE_ENUMERATION:
3290 return(xmlValidateNmtokensValue(value));
3291 case XML_ATTRIBUTE_NMTOKEN:
3292 return(xmlValidateNmtokenValue(value));
3293 case XML_ATTRIBUTE_CDATA:
3294 break;
3295 }
3296 return(1);
3297}
3298
3299/**
3300 * xmlValidateAttributeValue2:
3301 * @ctxt: the validation context
3302 * @doc: the document
3303 * @name: the attribute name (used for error reporting only)
3304 * @type: the attribute type
3305 * @value: the attribute value
3306 *
3307 * Validate that the given attribute value match a given type.
3308 * This typically cannot be done before having finished parsing
3309 * the subsets.
3310 *
3311 * [ VC: IDREF ]
3312 * Values of type IDREF must match one of the declared IDs
3313 * Values of type IDREFS must match a sequence of the declared IDs
3314 * each Name must match the value of an ID attribute on some element
3315 * in the XML document; i.e. IDREF values must match the value of
3316 * some ID attribute
3317 *
3318 * [ VC: Entity Name ]
3319 * Values of type ENTITY must match one declared entity
3320 * Values of type ENTITIES must match a sequence of declared entities
3321 *
3322 * [ VC: Notation Attributes ]
3323 * all notation names in the declaration must be declared.
3324 *
3325 * returns 1 if valid or 0 otherwise
3326 */
3327
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003328static int
Owen Taylor3473f882001-02-23 17:55:21 +00003329xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3330 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3331 int ret = 1;
3332 switch (type) {
3333 case XML_ATTRIBUTE_IDREFS:
3334 case XML_ATTRIBUTE_IDREF:
3335 case XML_ATTRIBUTE_ID:
3336 case XML_ATTRIBUTE_NMTOKENS:
3337 case XML_ATTRIBUTE_ENUMERATION:
3338 case XML_ATTRIBUTE_NMTOKEN:
3339 case XML_ATTRIBUTE_CDATA:
3340 break;
3341 case XML_ATTRIBUTE_ENTITY: {
3342 xmlEntityPtr ent;
3343
3344 ent = xmlGetDocEntity(doc, value);
Daniel Veillard62998c02003-09-15 12:56:36 +00003345 /* yeah it's a bit messy... */
Daniel Veillard878eab02002-02-19 13:46:09 +00003346 if ((ent == NULL) && (doc->standalone == 1)) {
3347 doc->standalone = 0;
3348 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003349 }
Owen Taylor3473f882001-02-23 17:55:21 +00003350 if (ent == NULL) {
3351 VERROR(ctxt->userData,
3352 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3353 name, value);
3354 ret = 0;
3355 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3356 VERROR(ctxt->userData,
3357 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3358 name, value);
3359 ret = 0;
3360 }
3361 break;
3362 }
3363 case XML_ATTRIBUTE_ENTITIES: {
3364 xmlChar *dup, *nam = NULL, *cur, save;
3365 xmlEntityPtr ent;
3366
3367 dup = xmlStrdup(value);
3368 if (dup == NULL)
3369 return(0);
3370 cur = dup;
3371 while (*cur != 0) {
3372 nam = cur;
3373 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
3374 save = *cur;
3375 *cur = 0;
3376 ent = xmlGetDocEntity(doc, nam);
3377 if (ent == NULL) {
3378 VERROR(ctxt->userData,
3379 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3380 name, nam);
3381 ret = 0;
3382 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3383 VERROR(ctxt->userData,
3384 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3385 name, nam);
3386 ret = 0;
3387 }
3388 if (save == 0)
3389 break;
3390 *cur = save;
3391 while (IS_BLANK(*cur)) cur++;
3392 }
3393 xmlFree(dup);
3394 break;
3395 }
3396 case XML_ATTRIBUTE_NOTATION: {
3397 xmlNotationPtr nota;
3398
3399 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3400 if ((nota == NULL) && (doc->extSubset != NULL))
3401 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3402
3403 if (nota == NULL) {
3404 VERROR(ctxt->userData,
3405 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3406 name, value);
3407 ret = 0;
3408 }
3409 break;
3410 }
3411 }
3412 return(ret);
3413}
3414
3415/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003416 * xmlValidCtxtNormalizeAttributeValue:
3417 * @ctxt: the validation context
3418 * @doc: the document
3419 * @elem: the parent
3420 * @name: the attribute name
3421 * @value: the attribute value
3422 * @ctxt: the validation context or NULL
3423 *
3424 * Does the validation related extra step of the normalization of attribute
3425 * values:
3426 *
3427 * If the declared value is not CDATA, then the XML processor must further
3428 * process the normalized attribute value by discarding any leading and
3429 * trailing space (#x20) characters, and by replacing sequences of space
3430 * (#x20) characters by single space (#x20) character.
3431 *
3432 * Also check VC: Standalone Document Declaration in P32, and update
3433 * ctxt->valid accordingly
3434 *
3435 * returns a new normalized string if normalization is needed, NULL otherwise
3436 * the caller must free the returned value.
3437 */
3438
3439xmlChar *
3440xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3441 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3442 xmlChar *ret, *dst;
3443 const xmlChar *src;
3444 xmlAttributePtr attrDecl = NULL;
3445 int extsubset = 0;
3446
3447 if (doc == NULL) return(NULL);
3448 if (elem == NULL) return(NULL);
3449 if (name == NULL) return(NULL);
3450 if (value == NULL) return(NULL);
3451
3452 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003453 xmlChar fn[50];
3454 xmlChar *fullname;
3455
3456 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3457 if (fullname == NULL)
3458 return(0);
3459 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003460 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003461 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003462 if (attrDecl != NULL)
3463 extsubset = 1;
3464 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003465 if ((fullname != fn) && (fullname != elem->name))
3466 xmlFree(fullname);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003467 }
3468 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3469 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3470 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3471 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3472 if (attrDecl != NULL)
3473 extsubset = 1;
3474 }
3475
3476 if (attrDecl == NULL)
3477 return(NULL);
3478 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3479 return(NULL);
3480
3481 ret = xmlStrdup(value);
3482 if (ret == NULL)
3483 return(NULL);
3484 src = value;
3485 dst = ret;
3486 while (*src == 0x20) src++;
3487 while (*src != 0) {
3488 if (*src == 0x20) {
3489 while (*src == 0x20) src++;
3490 if (*src != 0)
3491 *dst++ = 0x20;
3492 } else {
3493 *dst++ = *src++;
3494 }
3495 }
3496 *dst = 0;
3497 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3498 VERROR(ctxt->userData,
3499"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3500 name, elem->name);
3501 ctxt->valid = 0;
3502 }
3503 return(ret);
3504}
3505
3506/**
Owen Taylor3473f882001-02-23 17:55:21 +00003507 * xmlValidNormalizeAttributeValue:
3508 * @doc: the document
3509 * @elem: the parent
3510 * @name: the attribute name
3511 * @value: the attribute value
3512 *
3513 * Does the validation related extra step of the normalization of attribute
3514 * values:
3515 *
3516 * If the declared value is not CDATA, then the XML processor must further
3517 * process the normalized attribute value by discarding any leading and
3518 * trailing space (#x20) characters, and by replacing sequences of space
3519 * (#x20) characters by single space (#x20) character.
3520 *
3521 * returns a new normalized string if normalization is needed, NULL otherwise
3522 * the caller must free the returned value.
3523 */
3524
3525xmlChar *
3526xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3527 const xmlChar *name, const xmlChar *value) {
3528 xmlChar *ret, *dst;
3529 const xmlChar *src;
3530 xmlAttributePtr attrDecl = NULL;
3531
3532 if (doc == NULL) return(NULL);
3533 if (elem == NULL) return(NULL);
3534 if (name == NULL) return(NULL);
3535 if (value == NULL) return(NULL);
3536
3537 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003538 xmlChar fn[50];
3539 xmlChar *fullname;
3540
3541 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3542 if (fullname == NULL)
3543 return(0);
3544 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
Owen Taylor3473f882001-02-23 17:55:21 +00003545 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003546 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3547 if ((fullname != fn) && (fullname != elem->name))
3548 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003549 }
3550 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3551 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3552 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3553
3554 if (attrDecl == NULL)
3555 return(NULL);
3556 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3557 return(NULL);
3558
3559 ret = xmlStrdup(value);
3560 if (ret == NULL)
3561 return(NULL);
3562 src = value;
3563 dst = ret;
3564 while (*src == 0x20) src++;
3565 while (*src != 0) {
3566 if (*src == 0x20) {
3567 while (*src == 0x20) src++;
3568 if (*src != 0)
3569 *dst++ = 0x20;
3570 } else {
3571 *dst++ = *src++;
3572 }
3573 }
3574 *dst = 0;
3575 return(ret);
3576}
3577
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003578static void
Owen Taylor3473f882001-02-23 17:55:21 +00003579xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003580 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003581 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3582}
3583
3584/**
3585 * xmlValidateAttributeDecl:
3586 * @ctxt: the validation context
3587 * @doc: a document instance
3588 * @attr: an attribute definition
3589 *
3590 * Try to validate a single attribute definition
3591 * basically it does the following checks as described by the
3592 * XML-1.0 recommendation:
3593 * - [ VC: Attribute Default Legal ]
3594 * - [ VC: Enumeration ]
3595 * - [ VC: ID Attribute Default ]
3596 *
3597 * The ID/IDREF uniqueness and matching are done separately
3598 *
3599 * returns 1 if valid or 0 otherwise
3600 */
3601
3602int
3603xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3604 xmlAttributePtr attr) {
3605 int ret = 1;
3606 int val;
3607 CHECK_DTD;
3608 if(attr == NULL) return(1);
3609
3610 /* Attribute Default Legal */
3611 /* Enumeration */
3612 if (attr->defaultValue != NULL) {
3613 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3614 if (val == 0) {
3615 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003616 "Syntax of default value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003617 attr->name, attr->elem);
3618 }
3619 ret &= val;
3620 }
3621
3622 /* ID Attribute Default */
3623 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3624 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3625 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3626 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003627 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003628 attr->name, attr->elem);
3629 ret = 0;
3630 }
3631
3632 /* One ID per Element Type */
3633 if (attr->atype == XML_ATTRIBUTE_ID) {
3634 int nbId;
3635
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003636 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003637 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3638 attr->elem);
3639 if (elem != NULL) {
3640 nbId = xmlScanIDAttributeDecl(NULL, elem);
3641 } else {
3642 xmlAttributeTablePtr table;
3643
3644 /*
3645 * The attribute may be declared in the internal subset and the
3646 * element in the external subset.
3647 */
3648 nbId = 0;
3649 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3650 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3651 xmlValidateAttributeIdCallback, &nbId);
3652 }
3653 if (nbId > 1) {
3654 VERROR(ctxt->userData,
3655 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3656 attr->elem, nbId, attr->name);
3657 } else if (doc->extSubset != NULL) {
3658 int extId = 0;
3659 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3660 if (elem != NULL) {
3661 extId = xmlScanIDAttributeDecl(NULL, elem);
3662 }
3663 if (extId > 1) {
3664 VERROR(ctxt->userData,
3665 "Element %s has %d ID attribute defined in the external subset : %s\n",
3666 attr->elem, extId, attr->name);
3667 } else if (extId + nbId > 1) {
3668 VERROR(ctxt->userData,
3669"Element %s has ID attributes defined in the internal and external subset : %s\n",
3670 attr->elem, attr->name);
3671 }
3672 }
3673 }
3674
3675 /* Validity Constraint: Enumeration */
3676 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3677 xmlEnumerationPtr tree = attr->tree;
3678 while (tree != NULL) {
3679 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3680 tree = tree->next;
3681 }
3682 if (tree == NULL) {
3683 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003684"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003685 attr->defaultValue, attr->name, attr->elem);
3686 ret = 0;
3687 }
3688 }
3689
3690 return(ret);
3691}
3692
3693/**
3694 * xmlValidateElementDecl:
3695 * @ctxt: the validation context
3696 * @doc: a document instance
3697 * @elem: an element definition
3698 *
3699 * Try to validate a single element definition
3700 * basically it does the following checks as described by the
3701 * XML-1.0 recommendation:
3702 * - [ VC: One ID per Element Type ]
3703 * - [ VC: No Duplicate Types ]
3704 * - [ VC: Unique Element Type Declaration ]
3705 *
3706 * returns 1 if valid or 0 otherwise
3707 */
3708
3709int
3710xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3711 xmlElementPtr elem) {
3712 int ret = 1;
3713 xmlElementPtr tst;
3714
3715 CHECK_DTD;
3716
3717 if (elem == NULL) return(1);
3718
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003719#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00003720#ifdef LIBXML_REGEXP_ENABLED
3721 /* Build the regexp associated to the content model */
3722 ret = xmlValidBuildContentModel(ctxt, elem);
3723#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003724#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00003725
Owen Taylor3473f882001-02-23 17:55:21 +00003726 /* No Duplicate Types */
3727 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3728 xmlElementContentPtr cur, next;
3729 const xmlChar *name;
3730
3731 cur = elem->content;
3732 while (cur != NULL) {
3733 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3734 if (cur->c1 == NULL) break;
3735 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3736 name = cur->c1->name;
3737 next = cur->c2;
3738 while (next != NULL) {
3739 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillard7b68df92003-08-03 22:58:54 +00003740 if ((xmlStrEqual(next->name, name)) &&
3741 (xmlStrEqual(next->prefix, cur->prefix))) {
3742 if (cur->prefix == NULL) {
3743 VERROR(ctxt->userData,
Owen Taylor3473f882001-02-23 17:55:21 +00003744 "Definition of %s has duplicate references of %s\n",
Daniel Veillard7b68df92003-08-03 22:58:54 +00003745 elem->name, name);
3746 } else {
3747 VERROR(ctxt->userData,
3748 "Definition of %s has duplicate references of %s:%s\n",
3749 elem->name, cur->prefix, name);
3750 }
Owen Taylor3473f882001-02-23 17:55:21 +00003751 ret = 0;
3752 }
3753 break;
3754 }
3755 if (next->c1 == NULL) break;
3756 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
Daniel Veillard7b68df92003-08-03 22:58:54 +00003757 if ((xmlStrEqual(next->c1->name, name)) &&
3758 (xmlStrEqual(next->c1->prefix, cur->prefix))) {
3759 if (cur->prefix == NULL) {
3760 VERROR(ctxt->userData,
3761 "Definition of %s has duplicate references to %s\n",
3762 elem->name, name);
3763 } else {
3764 VERROR(ctxt->userData,
3765 "Definition of %s has duplicate references to %s:%s\n",
3766 elem->name, cur->prefix, name);
3767 }
Owen Taylor3473f882001-02-23 17:55:21 +00003768 ret = 0;
3769 }
3770 next = next->c2;
3771 }
3772 }
3773 cur = cur->c2;
3774 }
3775 }
3776
3777 /* VC: Unique Element Type Declaration */
3778 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003779 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003780 ((tst->prefix == elem->prefix) ||
3781 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003782 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003783 VERROR(ctxt->userData, "Redefinition of element %s\n",
3784 elem->name);
3785 ret = 0;
3786 }
3787 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003788 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003789 ((tst->prefix == elem->prefix) ||
3790 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003791 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003792 VERROR(ctxt->userData, "Redefinition of element %s\n",
3793 elem->name);
3794 ret = 0;
3795 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003796 /* One ID per Element Type
3797 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003798 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3799 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003800 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003801 return(ret);
3802}
3803
3804/**
3805 * xmlValidateOneAttribute:
3806 * @ctxt: the validation context
3807 * @doc: a document instance
3808 * @elem: an element instance
3809 * @attr: an attribute instance
3810 * @value: the attribute value (without entities processing)
3811 *
3812 * Try to validate a single attribute for an element
3813 * basically it does the following checks as described by the
3814 * XML-1.0 recommendation:
3815 * - [ VC: Attribute Value Type ]
3816 * - [ VC: Fixed Attribute Default ]
3817 * - [ VC: Entity Name ]
3818 * - [ VC: Name Token ]
3819 * - [ VC: ID ]
3820 * - [ VC: IDREF ]
3821 * - [ VC: Entity Name ]
3822 * - [ VC: Notation Attributes ]
3823 *
3824 * The ID/IDREF uniqueness and matching are done separately
3825 *
3826 * returns 1 if valid or 0 otherwise
3827 */
3828
3829int
3830xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
Daniel Veillard07cb8222003-09-10 10:51:05 +00003831 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
3832{
Owen Taylor3473f882001-02-23 17:55:21 +00003833 xmlAttributePtr attrDecl = NULL;
3834 int val;
3835 int ret = 1;
3836
3837 CHECK_DTD;
3838 if ((elem == NULL) || (elem->name == NULL)) return(0);
3839 if ((attr == NULL) || (attr->name == NULL)) return(0);
3840
3841 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003842 xmlChar fn[50];
3843 xmlChar *fullname;
3844
3845 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3846 if (fullname == NULL)
3847 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003848 if (attr->ns != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003849 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00003850 attr->name, attr->ns->prefix);
3851 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00003852 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Owen Taylor3473f882001-02-23 17:55:21 +00003853 attr->name, attr->ns->prefix);
3854 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00003855 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003856 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3857 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
Daniel Veillardc00cda82003-04-07 10:22:39 +00003858 fullname, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003859 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00003860 if ((fullname != fn) && (fullname != elem->name))
3861 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00003862 }
3863 if (attrDecl == NULL) {
3864 if (attr->ns != NULL) {
3865 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3866 attr->name, attr->ns->prefix);
3867 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3868 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3869 attr->name, attr->ns->prefix);
3870 } else {
3871 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3872 elem->name, attr->name);
3873 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3874 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3875 elem->name, attr->name);
3876 }
3877 }
3878
3879
3880 /* Validity Constraint: Attribute Value Type */
3881 if (attrDecl == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003882 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003883 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003884 "No declaration for attribute %s of element %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003885 attr->name, elem->name);
3886 return(0);
3887 }
3888 attr->atype = attrDecl->atype;
3889
3890 val = xmlValidateAttributeValue(attrDecl->atype, value);
3891 if (val == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003892 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003893 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003894 "Syntax of value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003895 attr->name, elem->name);
3896 ret = 0;
3897 }
3898
3899 /* Validity constraint: Fixed Attribute Default */
3900 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3901 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003902 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003903 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003904 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003905 attr->name, elem->name, attrDecl->defaultValue);
3906 ret = 0;
3907 }
3908 }
3909
3910 /* Validity Constraint: ID uniqueness */
3911 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3912 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3913 ret = 0;
3914 }
3915
3916 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3917 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3918 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3919 ret = 0;
3920 }
3921
3922 /* Validity Constraint: Notation Attributes */
3923 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3924 xmlEnumerationPtr tree = attrDecl->tree;
3925 xmlNotationPtr nota;
3926
3927 /* First check that the given NOTATION was declared */
3928 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3929 if (nota == NULL)
3930 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3931
3932 if (nota == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003933 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003934 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003935 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003936 value, attr->name, elem->name);
3937 ret = 0;
3938 }
3939
3940 /* Second, verify that it's among the list */
3941 while (tree != NULL) {
3942 if (xmlStrEqual(tree->name, value)) break;
3943 tree = tree->next;
3944 }
3945 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003946 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003947 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003948"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003949 value, attr->name, elem->name);
3950 ret = 0;
3951 }
3952 }
3953
3954 /* Validity Constraint: Enumeration */
3955 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3956 xmlEnumerationPtr tree = attrDecl->tree;
3957 while (tree != NULL) {
3958 if (xmlStrEqual(tree->name, value)) break;
3959 tree = tree->next;
3960 }
3961 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003962 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003963 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003964 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003965 value, attr->name, elem->name);
3966 ret = 0;
3967 }
3968 }
3969
3970 /* Fixed Attribute Default */
3971 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3972 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003973 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003974 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003975 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003976 attr->name, elem->name, attrDecl->defaultValue);
3977 ret = 0;
3978 }
3979
3980 /* Extra check for the attribute value */
3981 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3982 attrDecl->atype, value);
3983
3984 return(ret);
3985}
3986
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003987/**
3988 * xmlValidateOneNamespace:
3989 * @ctxt: the validation context
3990 * @doc: a document instance
3991 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00003992 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003993 * @ns: an namespace declaration instance
3994 * @value: the attribute value (without entities processing)
3995 *
3996 * Try to validate a single namespace declaration for an element
3997 * basically it does the following checks as described by the
3998 * XML-1.0 recommendation:
3999 * - [ VC: Attribute Value Type ]
4000 * - [ VC: Fixed Attribute Default ]
4001 * - [ VC: Entity Name ]
4002 * - [ VC: Name Token ]
4003 * - [ VC: ID ]
4004 * - [ VC: IDREF ]
4005 * - [ VC: Entity Name ]
4006 * - [ VC: Notation Attributes ]
4007 *
4008 * The ID/IDREF uniqueness and matching are done separately
4009 *
4010 * returns 1 if valid or 0 otherwise
4011 */
4012
4013int
4014xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4015xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4016 /* xmlElementPtr elemDecl; */
4017 xmlAttributePtr attrDecl = NULL;
4018 int val;
4019 int ret = 1;
4020
4021 CHECK_DTD;
4022 if ((elem == NULL) || (elem->name == NULL)) return(0);
4023 if ((ns == NULL) || (ns->href == NULL)) return(0);
4024
4025 if (prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004026 xmlChar fn[50];
4027 xmlChar *fullname;
4028
4029 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4030 if (fullname == NULL) {
4031 VERROR(ctxt->userData, "Out of memory\n");
4032 return(0);
4033 }
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004034 if (ns->prefix != NULL) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004035 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004036 ns->prefix, BAD_CAST "xmlns");
4037 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004038 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004039 ns->prefix, BAD_CAST "xmlns");
4040 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004041 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004042 BAD_CAST "xmlns");
4043 if ((attrDecl == NULL) && (doc->extSubset != NULL))
Daniel Veillardc00cda82003-04-07 10:22:39 +00004044 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004045 BAD_CAST "xmlns");
4046 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004047 if ((fullname != fn) && (fullname != elem->name))
4048 xmlFree(fullname);
Daniel Veillard90d68fb2002-09-26 16:10:21 +00004049 }
4050 if (attrDecl == NULL) {
4051 if (ns->prefix != NULL) {
4052 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4053 ns->prefix, BAD_CAST "xmlns");
4054 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4055 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4056 ns->prefix, BAD_CAST "xmlns");
4057 } else {
4058 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4059 elem->name, BAD_CAST "xmlns");
4060 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4061 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4062 elem->name, BAD_CAST "xmlns");
4063 }
4064 }
4065
4066
4067 /* Validity Constraint: Attribute Value Type */
4068 if (attrDecl == NULL) {
4069 VECTXT(ctxt, elem);
4070 if (ns->prefix != NULL) {
4071 VERROR(ctxt->userData,
4072 "No declaration for attribute xmlns:%s of element %s\n",
4073 ns->prefix, elem->name);
4074 } else {
4075 VERROR(ctxt->userData,
4076 "No declaration for attribute xmlns of element %s\n",
4077 elem->name);
4078 }
4079 return(0);
4080 }
4081
4082 val = xmlValidateAttributeValue(attrDecl->atype, value);
4083 if (val == 0) {
4084 VECTXT(ctxt, elem);
4085 if (ns->prefix != NULL) {
4086 VERROR(ctxt->userData,
4087 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4088 ns->prefix, elem->name);
4089 } else {
4090 VERROR(ctxt->userData,
4091 "Syntax of value for attribute xmlns of %s is not valid\n",
4092 elem->name);
4093 }
4094 ret = 0;
4095 }
4096
4097 /* Validity constraint: Fixed Attribute Default */
4098 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4099 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4100 VECTXT(ctxt, elem);
4101 if (ns->prefix != NULL) {
4102 VERROR(ctxt->userData,
4103 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4104 ns->prefix, elem->name, attrDecl->defaultValue);
4105 } else {
4106 VERROR(ctxt->userData,
4107 "Value for attribute xmlns of %s is different from default \"%s\"\n",
4108 elem->name, attrDecl->defaultValue);
4109 }
4110 ret = 0;
4111 }
4112 }
4113
4114 /* Validity Constraint: ID uniqueness */
4115 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4116 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4117 ret = 0;
4118 }
4119
4120 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4121 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4122 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4123 ret = 0;
4124 }
4125
4126 /* Validity Constraint: Notation Attributes */
4127 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4128 xmlEnumerationPtr tree = attrDecl->tree;
4129 xmlNotationPtr nota;
4130
4131 /* First check that the given NOTATION was declared */
4132 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4133 if (nota == NULL)
4134 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4135
4136 if (nota == NULL) {
4137 VECTXT(ctxt, elem);
4138 if (ns->prefix != NULL) {
4139 VERROR(ctxt->userData,
4140 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4141 value, ns->prefix, elem->name);
4142 } else {
4143 VERROR(ctxt->userData,
4144 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4145 value, elem->name);
4146 }
4147 ret = 0;
4148 }
4149
4150 /* Second, verify that it's among the list */
4151 while (tree != NULL) {
4152 if (xmlStrEqual(tree->name, value)) break;
4153 tree = tree->next;
4154 }
4155 if (tree == NULL) {
4156 VECTXT(ctxt, elem);
4157 if (ns->prefix != NULL) {
4158 VERROR(ctxt->userData,
4159"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4160 value, ns->prefix, elem->name);
4161 } else {
4162 VERROR(ctxt->userData,
4163"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4164 value, elem->name);
4165 }
4166 ret = 0;
4167 }
4168 }
4169
4170 /* Validity Constraint: Enumeration */
4171 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4172 xmlEnumerationPtr tree = attrDecl->tree;
4173 while (tree != NULL) {
4174 if (xmlStrEqual(tree->name, value)) break;
4175 tree = tree->next;
4176 }
4177 if (tree == NULL) {
4178 VECTXT(ctxt, elem);
4179 if (ns->prefix != NULL) {
4180 VERROR(ctxt->userData,
4181"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4182 value, ns->prefix, elem->name);
4183 } else {
4184 VERROR(ctxt->userData,
4185"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4186 value, elem->name);
4187 }
4188 ret = 0;
4189 }
4190 }
4191
4192 /* Fixed Attribute Default */
4193 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4194 (!xmlStrEqual(attrDecl->defaultValue, value))) {
4195 VECTXT(ctxt, elem);
4196 if (ns->prefix != NULL) {
4197 VERROR(ctxt->userData,
4198 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4199 ns->prefix, elem->name, attrDecl->defaultValue);
4200 } else {
4201 VERROR(ctxt->userData,
4202 "Value for attribute xmlns of %s must be \"%s\"\n",
4203 elem->name, attrDecl->defaultValue);
4204 }
4205 ret = 0;
4206 }
4207
4208 /* Extra check for the attribute value */
4209 if (ns->prefix != NULL) {
4210 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4211 attrDecl->atype, value);
4212 } else {
4213 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4214 attrDecl->atype, value);
4215 }
4216
4217 return(ret);
4218}
4219
Daniel Veillard118aed72002-09-24 14:13:13 +00004220#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004221/**
4222 * xmlValidateSkipIgnorable:
4223 * @ctxt: the validation context
4224 * @child: the child list
4225 *
4226 * Skip ignorable elements w.r.t. the validation process
4227 *
4228 * returns the first element to consider for validation of the content model
4229 */
4230
4231static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004232xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004233 while (child != NULL) {
4234 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004235 /* These things are ignored (skipped) during validation. */
4236 case XML_PI_NODE:
4237 case XML_COMMENT_NODE:
4238 case XML_XINCLUDE_START:
4239 case XML_XINCLUDE_END:
4240 child = child->next;
4241 break;
4242 case XML_TEXT_NODE:
4243 if (xmlIsBlankNode(child))
4244 child = child->next;
4245 else
4246 return(child);
4247 break;
4248 /* keep current node */
4249 default:
4250 return(child);
4251 }
4252 }
4253 return(child);
4254}
4255
4256/**
4257 * xmlValidateElementType:
4258 * @ctxt: the validation context
4259 *
4260 * Try to validate the content model of an element internal function
4261 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004262 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4263 * reference is found and -3 if the validation succeeded but
4264 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004265 */
4266
4267static int
4268xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004269 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004270 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004271
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004272 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004273 if ((NODE == NULL) && (CONT == NULL))
4274 return(1);
4275 if ((NODE == NULL) &&
4276 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4277 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4278 return(1);
4279 }
4280 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004281 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004282 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004283
4284 /*
4285 * We arrive here when more states need to be examined
4286 */
4287cont:
4288
4289 /*
4290 * We just recovered from a rollback generated by a possible
4291 * epsilon transition, go directly to the analysis phase
4292 */
4293 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004294 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004295 DEBUG_VALID_STATE(NODE, CONT)
4296 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004297 goto analyze;
4298 }
4299
4300 DEBUG_VALID_STATE(NODE, CONT)
4301 /*
4302 * we may have to save a backup state here. This is the equivalent
4303 * of handling epsilon transition in NFAs.
4304 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004305 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004306 ((CONT->parent == NULL) ||
4307 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004308 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004309 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004310 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004311 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004312 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4313 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004314 }
4315
4316
4317 /*
4318 * Check first if the content matches
4319 */
4320 switch (CONT->type) {
4321 case XML_ELEMENT_CONTENT_PCDATA:
4322 if (NODE == NULL) {
4323 DEBUG_VALID_MSG("pcdata failed no node");
4324 ret = 0;
4325 break;
4326 }
4327 if (NODE->type == XML_TEXT_NODE) {
4328 DEBUG_VALID_MSG("pcdata found, skip to next");
4329 /*
4330 * go to next element in the content model
4331 * skipping ignorable elems
4332 */
4333 do {
4334 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004335 NODE = xmlValidateSkipIgnorable(NODE);
4336 if ((NODE != NULL) &&
4337 (NODE->type == XML_ENTITY_REF_NODE))
4338 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004339 } while ((NODE != NULL) &&
4340 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004341 (NODE->type != XML_TEXT_NODE) &&
4342 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004343 ret = 1;
4344 break;
4345 } else {
4346 DEBUG_VALID_MSG("pcdata failed");
4347 ret = 0;
4348 break;
4349 }
4350 break;
4351 case XML_ELEMENT_CONTENT_ELEMENT:
4352 if (NODE == NULL) {
4353 DEBUG_VALID_MSG("element failed no node");
4354 ret = 0;
4355 break;
4356 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004357 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4358 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004359 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004360 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4361 ret = (CONT->prefix == NULL);
4362 } else if (CONT->prefix == NULL) {
4363 ret = 0;
4364 } else {
4365 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4366 }
4367 }
4368 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004369 DEBUG_VALID_MSG("element found, skip to next");
4370 /*
4371 * go to next element in the content model
4372 * skipping ignorable elems
4373 */
4374 do {
4375 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004376 NODE = xmlValidateSkipIgnorable(NODE);
4377 if ((NODE != NULL) &&
4378 (NODE->type == XML_ENTITY_REF_NODE))
4379 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004380 } while ((NODE != NULL) &&
4381 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004382 (NODE->type != XML_TEXT_NODE) &&
4383 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004384 } else {
4385 DEBUG_VALID_MSG("element failed");
4386 ret = 0;
4387 break;
4388 }
4389 break;
4390 case XML_ELEMENT_CONTENT_OR:
4391 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004392 * Small optimization.
4393 */
4394 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4395 if ((NODE == NULL) ||
4396 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4397 DEPTH++;
4398 CONT = CONT->c2;
4399 goto cont;
4400 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004401 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4402 ret = (CONT->c1->prefix == NULL);
4403 } else if (CONT->c1->prefix == NULL) {
4404 ret = 0;
4405 } else {
4406 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4407 }
4408 if (ret == 0) {
4409 DEPTH++;
4410 CONT = CONT->c2;
4411 goto cont;
4412 }
Daniel Veillard85349052001-04-20 13:48:21 +00004413 }
4414
4415 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004416 * save the second branch 'or' branch
4417 */
4418 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004419 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4420 OCCURS, ROLLBACK_OR) < 0)
4421 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004422 DEPTH++;
4423 CONT = CONT->c1;
4424 goto cont;
4425 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004426 /*
4427 * Small optimization.
4428 */
4429 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4430 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4431 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4432 if ((NODE == NULL) ||
4433 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4434 DEPTH++;
4435 CONT = CONT->c2;
4436 goto cont;
4437 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004438 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4439 ret = (CONT->c1->prefix == NULL);
4440 } else if (CONT->c1->prefix == NULL) {
4441 ret = 0;
4442 } else {
4443 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4444 }
4445 if (ret == 0) {
4446 DEPTH++;
4447 CONT = CONT->c2;
4448 goto cont;
4449 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004450 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004451 DEPTH++;
4452 CONT = CONT->c1;
4453 goto cont;
4454 }
4455
4456 /*
4457 * At this point handle going up in the tree
4458 */
4459 if (ret == -1) {
4460 DEBUG_VALID_MSG("error found returning");
4461 return(ret);
4462 }
4463analyze:
4464 while (CONT != NULL) {
4465 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004466 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004467 * this level.
4468 */
4469 if (ret == 0) {
4470 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004471 xmlNodePtr cur;
4472
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004473 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004474 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004475 DEBUG_VALID_MSG("Once branch failed, rollback");
4476 if (vstateVPop(ctxt) < 0 ) {
4477 DEBUG_VALID_MSG("exhaustion, failed");
4478 return(0);
4479 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004480 if (cur != ctxt->vstate->node)
4481 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004482 goto cont;
4483 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004484 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004485 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004486 DEBUG_VALID_MSG("Plus branch failed, rollback");
4487 if (vstateVPop(ctxt) < 0 ) {
4488 DEBUG_VALID_MSG("exhaustion, failed");
4489 return(0);
4490 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004491 if (cur != ctxt->vstate->node)
4492 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004493 goto cont;
4494 }
4495 DEBUG_VALID_MSG("Plus branch found");
4496 ret = 1;
4497 break;
4498 case XML_ELEMENT_CONTENT_MULT:
4499#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004500 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004501 DEBUG_VALID_MSG("Mult branch failed");
4502 } else {
4503 DEBUG_VALID_MSG("Mult branch found");
4504 }
4505#endif
4506 ret = 1;
4507 break;
4508 case XML_ELEMENT_CONTENT_OPT:
4509 DEBUG_VALID_MSG("Option branch failed");
4510 ret = 1;
4511 break;
4512 }
4513 } else {
4514 switch (CONT->ocur) {
4515 case XML_ELEMENT_CONTENT_OPT:
4516 DEBUG_VALID_MSG("Option branch succeeded");
4517 ret = 1;
4518 break;
4519 case XML_ELEMENT_CONTENT_ONCE:
4520 DEBUG_VALID_MSG("Once branch succeeded");
4521 ret = 1;
4522 break;
4523 case XML_ELEMENT_CONTENT_PLUS:
4524 if (STATE == ROLLBACK_PARENT) {
4525 DEBUG_VALID_MSG("Plus branch rollback");
4526 ret = 1;
4527 break;
4528 }
4529 if (NODE == NULL) {
4530 DEBUG_VALID_MSG("Plus branch exhausted");
4531 ret = 1;
4532 break;
4533 }
4534 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004535 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004536 goto cont;
4537 case XML_ELEMENT_CONTENT_MULT:
4538 if (STATE == ROLLBACK_PARENT) {
4539 DEBUG_VALID_MSG("Mult branch rollback");
4540 ret = 1;
4541 break;
4542 }
4543 if (NODE == NULL) {
4544 DEBUG_VALID_MSG("Mult branch exhausted");
4545 ret = 1;
4546 break;
4547 }
4548 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004549 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004550 goto cont;
4551 }
4552 }
4553 STATE = 0;
4554
4555 /*
4556 * Then act accordingly at the parent level
4557 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004558 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004559 if (CONT->parent == NULL)
4560 break;
4561
4562 switch (CONT->parent->type) {
4563 case XML_ELEMENT_CONTENT_PCDATA:
4564 DEBUG_VALID_MSG("Error: parent pcdata");
4565 return(-1);
4566 case XML_ELEMENT_CONTENT_ELEMENT:
4567 DEBUG_VALID_MSG("Error: parent element");
4568 return(-1);
4569 case XML_ELEMENT_CONTENT_OR:
4570 if (ret == 1) {
4571 DEBUG_VALID_MSG("Or succeeded");
4572 CONT = CONT->parent;
4573 DEPTH--;
4574 } else {
4575 DEBUG_VALID_MSG("Or failed");
4576 CONT = CONT->parent;
4577 DEPTH--;
4578 }
4579 break;
4580 case XML_ELEMENT_CONTENT_SEQ:
4581 if (ret == 0) {
4582 DEBUG_VALID_MSG("Sequence failed");
4583 CONT = CONT->parent;
4584 DEPTH--;
4585 } else if (CONT == CONT->parent->c1) {
4586 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4587 CONT = CONT->parent->c2;
4588 goto cont;
4589 } else {
4590 DEBUG_VALID_MSG("Sequence succeeded");
4591 CONT = CONT->parent;
4592 DEPTH--;
4593 }
4594 }
4595 }
4596 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004597 xmlNodePtr cur;
4598
4599 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004600 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4601 if (vstateVPop(ctxt) < 0 ) {
4602 DEBUG_VALID_MSG("exhaustion, failed");
4603 return(0);
4604 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004605 if (cur != ctxt->vstate->node)
4606 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004607 goto cont;
4608 }
4609 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004610 xmlNodePtr cur;
4611
4612 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004613 DEBUG_VALID_MSG("Failure, rollback");
4614 if (vstateVPop(ctxt) < 0 ) {
4615 DEBUG_VALID_MSG("exhaustion, failed");
4616 return(0);
4617 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004618 if (cur != ctxt->vstate->node)
4619 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004620 goto cont;
4621 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004622 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004623}
Daniel Veillard23e73572002-09-19 19:56:43 +00004624#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004625
4626/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00004627 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004628 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00004629 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004630 * @content: An element
4631 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
4632 *
4633 * This will dump the list of elements to the buffer
4634 * Intended just for the debug routine
4635 */
4636static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00004637xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004638 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00004639 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004640
4641 if (node == NULL) return;
4642 if (glob) strcat(buf, "(");
4643 cur = node;
4644 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004645 len = strlen(buf);
4646 if (size - len < 50) {
4647 if ((size - len > 4) && (buf[len - 1] != '.'))
4648 strcat(buf, " ...");
4649 return;
4650 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004651 switch (cur->type) {
4652 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004653 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004654 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004655 if ((size - len > 4) && (buf[len - 1] != '.'))
4656 strcat(buf, " ...");
4657 return;
4658 }
4659 strcat(buf, (char *) cur->ns->prefix);
4660 strcat(buf, ":");
4661 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004662 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004663 if ((size - len > 4) && (buf[len - 1] != '.'))
4664 strcat(buf, " ...");
4665 return;
4666 }
4667 strcat(buf, (char *) cur->name);
4668 if (cur->next != NULL)
4669 strcat(buf, " ");
4670 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004671 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004672 if (xmlIsBlankNode(cur))
4673 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004674 case XML_CDATA_SECTION_NODE:
4675 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004676 strcat(buf, "CDATA");
4677 if (cur->next != NULL)
4678 strcat(buf, " ");
4679 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004680 case XML_ATTRIBUTE_NODE:
4681 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004682#ifdef LIBXML_DOCB_ENABLED
4683 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004684#endif
4685 case XML_HTML_DOCUMENT_NODE:
4686 case XML_DOCUMENT_TYPE_NODE:
4687 case XML_DOCUMENT_FRAG_NODE:
4688 case XML_NOTATION_NODE:
4689 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004690 strcat(buf, "???");
4691 if (cur->next != NULL)
4692 strcat(buf, " ");
4693 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004694 case XML_ENTITY_NODE:
4695 case XML_PI_NODE:
4696 case XML_DTD_NODE:
4697 case XML_COMMENT_NODE:
4698 case XML_ELEMENT_DECL:
4699 case XML_ATTRIBUTE_DECL:
4700 case XML_ENTITY_DECL:
4701 case XML_XINCLUDE_START:
4702 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004703 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004704 }
4705 cur = cur->next;
4706 }
4707 if (glob) strcat(buf, ")");
4708}
4709
4710/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004711 * xmlValidateElementContent:
4712 * @ctxt: the validation context
4713 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004714 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004715 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004716 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004717 *
4718 * Try to validate the content model of an element
4719 *
4720 * returns 1 if valid or 0 if not and -1 in case of error
4721 */
4722
4723static int
4724xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004725 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004726 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00004727#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00004728 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00004729#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00004730 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004731 xmlElementContentPtr cont;
4732 const xmlChar *name;
4733
4734 if (elemDecl == NULL)
4735 return(-1);
4736 cont = elemDecl->content;
4737 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004738
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004739#ifdef LIBXML_REGEXP_ENABLED
4740 /* Build the regexp associated to the content model */
4741 if (elemDecl->contModel == NULL)
4742 ret = xmlValidBuildContentModel(ctxt, elemDecl);
4743 if (elemDecl->contModel == NULL) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004744 return(-1);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004745 } else {
4746 xmlRegExecCtxtPtr exec;
4747
Daniel Veillardec498e12003-02-05 11:01:50 +00004748 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
4749 return(-1);
4750 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004751 ctxt->nodeMax = 0;
4752 ctxt->nodeNr = 0;
4753 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004754 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
4755 if (exec != NULL) {
4756 cur = child;
4757 while (cur != NULL) {
4758 switch (cur->type) {
4759 case XML_ENTITY_REF_NODE:
4760 /*
4761 * Push the current node to be able to roll back
4762 * and process within the entity
4763 */
4764 if ((cur->children != NULL) &&
4765 (cur->children->children != NULL)) {
4766 nodeVPush(ctxt, cur);
4767 cur = cur->children->children;
4768 continue;
4769 }
4770 break;
4771 case XML_TEXT_NODE:
4772 if (xmlIsBlankNode(cur))
4773 break;
4774 ret = 0;
4775 goto fail;
4776 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00004777 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004778 ret = 0;
4779 goto fail;
4780 case XML_ELEMENT_NODE:
4781 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00004782 xmlChar fn[50];
4783 xmlChar *fullname;
4784
4785 fullname = xmlBuildQName(cur->name,
4786 cur->ns->prefix, fn, 50);
4787 if (fullname == NULL) {
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004788 ret = -1;
4789 goto fail;
4790 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00004791 ret = xmlRegExecPushString(exec, fullname, NULL);
4792 if ((fullname != fn) && (fullname != cur->name))
4793 xmlFree(fullname);
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004794 } else {
4795 ret = xmlRegExecPushString(exec, cur->name, NULL);
4796 }
4797 break;
4798 default:
4799 break;
4800 }
4801 /*
4802 * Switch to next element
4803 */
4804 cur = cur->next;
4805 while (cur == NULL) {
4806 cur = nodeVPop(ctxt);
4807 if (cur == NULL)
4808 break;
4809 cur = cur->next;
4810 }
4811 }
4812 ret = xmlRegExecPushString(exec, NULL, NULL);
4813fail:
4814 xmlRegFreeExecCtxt(exec);
4815 }
4816 }
4817#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004818 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004819 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004820 */
4821 ctxt->vstateMax = 8;
4822 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4823 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4824 if (ctxt->vstateTab == NULL) {
4825 xmlGenericError(xmlGenericErrorContext,
4826 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004827 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004828 }
4829 /*
4830 * The first entry in the stack is reserved to the current state
4831 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004832 ctxt->nodeMax = 0;
4833 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004834 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004835 ctxt->vstate = &ctxt->vstateTab[0];
4836 ctxt->vstateNr = 1;
4837 CONT = cont;
4838 NODE = child;
4839 DEPTH = 0;
4840 OCCURS = 0;
4841 STATE = 0;
4842 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004843 if ((ret == -3) && (warn)) {
4844 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004845 "Content model for Element %s is ambiguous\n", name);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004846 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004847 /*
4848 * An entities reference appeared at this level.
4849 * Buid a minimal representation of this node content
4850 * sufficient to run the validation process on it
4851 */
4852 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004853 cur = child;
4854 while (cur != NULL) {
4855 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004856 case XML_ENTITY_REF_NODE:
4857 /*
4858 * Push the current node to be able to roll back
4859 * and process within the entity
4860 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004861 if ((cur->children != NULL) &&
4862 (cur->children->children != NULL)) {
4863 nodeVPush(ctxt, cur);
4864 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004865 continue;
4866 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004867 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004868 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004869 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004870 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004871 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004872 case XML_CDATA_SECTION_NODE:
4873 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004874 case XML_ELEMENT_NODE:
4875 /*
4876 * Allocate a new node and minimally fills in
4877 * what's required
4878 */
4879 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4880 if (tmp == NULL) {
4881 xmlGenericError(xmlGenericErrorContext,
4882 "xmlValidateElementContent : malloc failed\n");
4883 xmlFreeNodeList(repl);
4884 ret = -1;
4885 goto done;
4886 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004887 tmp->type = cur->type;
4888 tmp->name = cur->name;
4889 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004890 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004891 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004892 if (repl == NULL)
4893 repl = last = tmp;
4894 else {
4895 last->next = tmp;
4896 last = tmp;
4897 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004898 if (cur->type == XML_CDATA_SECTION_NODE) {
4899 /*
4900 * E59 spaces in CDATA does not match the
4901 * nonterminal S
4902 */
4903 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4904 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004905 break;
4906 default:
4907 break;
4908 }
4909 /*
4910 * Switch to next element
4911 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004912 cur = cur->next;
4913 while (cur == NULL) {
4914 cur = nodeVPop(ctxt);
4915 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004916 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004917 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004918 }
4919 }
4920
4921 /*
4922 * Relaunch the validation
4923 */
4924 ctxt->vstate = &ctxt->vstateTab[0];
4925 ctxt->vstateNr = 1;
4926 CONT = cont;
4927 NODE = repl;
4928 DEPTH = 0;
4929 OCCURS = 0;
4930 STATE = 0;
4931 ret = xmlValidateElementType(ctxt);
4932 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004933#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004934 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004935 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4936 char expr[5000];
4937 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004938
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004939 expr[0] = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004940 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004941 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00004942#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004943 if (repl != NULL)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004944 xmlSnprintfElements(&list[0], 5000, repl, 1);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004945 else
Daniel Veillard01992e02002-10-09 10:20:30 +00004946#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004947 xmlSnprintfElements(&list[0], 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004948
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004949 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004950 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004951 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004952 "Element %s content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004953 name, expr, list);
4954 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004955 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004956 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004957 "Element content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004958 expr, list);
4959 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004960 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004961 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004962 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004963 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004964 "Element %s content does not follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004965 name);
4966 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004967 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004968 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004969 "Element content does not follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004970 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004971 }
4972 ret = 0;
4973 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004974 if (ret == -3)
4975 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004976
Daniel Veillard23e73572002-09-19 19:56:43 +00004977#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004978done:
4979 /*
4980 * Deallocate the copy if done, and free up the validation stack
4981 */
4982 while (repl != NULL) {
4983 tmp = repl->next;
4984 xmlFree(repl);
4985 repl = tmp;
4986 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004987 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004988 if (ctxt->vstateTab != NULL) {
4989 xmlFree(ctxt->vstateTab);
4990 ctxt->vstateTab = NULL;
4991 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004992#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004993 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004994 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004995 if (ctxt->nodeTab != NULL) {
4996 xmlFree(ctxt->nodeTab);
4997 ctxt->nodeTab = NULL;
4998 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004999 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00005000
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005001}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005002
Owen Taylor3473f882001-02-23 17:55:21 +00005003/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005004 * xmlValidateCdataElement:
5005 * @ctxt: the validation context
5006 * @doc: a document instance
5007 * @elem: an element instance
5008 *
5009 * Check that an element follows #CDATA
5010 *
5011 * returns 1 if valid or 0 otherwise
5012 */
5013static int
5014xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5015 xmlNodePtr elem) {
5016 int ret = 1;
5017 xmlNodePtr cur, child;
5018
Daniel Veillardceb09b92002-10-04 11:46:37 +00005019 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005020 return(0);
5021
5022 child = elem->children;
5023
5024 cur = child;
5025 while (cur != NULL) {
5026 switch (cur->type) {
5027 case XML_ENTITY_REF_NODE:
5028 /*
5029 * Push the current node to be able to roll back
5030 * and process within the entity
5031 */
5032 if ((cur->children != NULL) &&
5033 (cur->children->children != NULL)) {
5034 nodeVPush(ctxt, cur);
5035 cur = cur->children->children;
5036 continue;
5037 }
5038 break;
5039 case XML_COMMENT_NODE:
5040 case XML_PI_NODE:
5041 case XML_TEXT_NODE:
5042 case XML_CDATA_SECTION_NODE:
5043 break;
5044 default:
5045 ret = 0;
5046 goto done;
5047 }
5048 /*
5049 * Switch to next element
5050 */
5051 cur = cur->next;
5052 while (cur == NULL) {
5053 cur = nodeVPop(ctxt);
5054 if (cur == NULL)
5055 break;
5056 cur = cur->next;
5057 }
5058 }
5059done:
5060 ctxt->nodeMax = 0;
5061 ctxt->nodeNr = 0;
5062 if (ctxt->nodeTab != NULL) {
5063 xmlFree(ctxt->nodeTab);
5064 ctxt->nodeTab = NULL;
5065 }
5066 return(ret);
5067}
5068
5069/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005070 * xmlValidateCheckMixed:
5071 * @ctxt: the validation context
5072 * @cont: the mixed content model
5073 * @qname: the qualified name as appearing in the serialization
5074 *
5075 * Check if the given node is part of the content model.
5076 *
5077 * Returns 1 if yes, 0 if no, -1 in case of error
5078 */
5079static int
5080xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
5081 xmlElementContentPtr cont, const xmlChar *qname) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005082 const xmlChar *name;
5083 int plen;
5084 name = xmlSplitQName3(qname, &plen);
5085
5086 if (name == NULL) {
5087 while (cont != NULL) {
5088 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5089 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
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 (xmlStrEqual(cont->c1->name, qname)))
5096 return(1);
5097 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5098 (cont->c1 == NULL) ||
5099 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5100 /* Internal error !!! */
5101 xmlGenericError(xmlGenericErrorContext,
5102 "Internal: MIXED struct bad\n");
5103 break;
5104 }
5105 cont = cont->c2;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005106 }
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00005107 } else {
5108 while (cont != NULL) {
5109 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5110 if ((cont->prefix != NULL) &&
5111 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5112 (xmlStrEqual(cont->name, name)))
5113 return(1);
5114 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5115 (cont->c1 != NULL) &&
5116 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5117 if ((cont->c1->prefix != NULL) &&
5118 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5119 (xmlStrEqual(cont->c1->name, name)))
5120 return(1);
5121 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5122 (cont->c1 == NULL) ||
5123 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5124 /* Internal error !!! */
5125 xmlGenericError(xmlGenericErrorContext,
5126 "Internal: MIXED struct bad\n");
5127 break;
5128 }
5129 cont = cont->c2;
5130 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005131 }
5132 return(0);
5133}
5134
5135/**
5136 * xmlValidGetElemDecl:
5137 * @ctxt: the validation context
5138 * @doc: a document instance
5139 * @elem: an element instance
5140 * @extsubset: pointer, (out) indicate if the declaration was found
5141 * in the external subset.
5142 *
5143 * Finds a declaration associated to an element in the document.
5144 *
5145 * returns the pointer to the declaration or NULL if not found.
5146 */
5147static xmlElementPtr
5148xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5149 xmlNodePtr elem, int *extsubset) {
5150 xmlElementPtr elemDecl = NULL;
5151 const xmlChar *prefix = NULL;
5152
5153 if ((elem == NULL) || (elem->name == NULL)) return(NULL);
5154 if (extsubset != NULL)
5155 *extsubset = 0;
5156
5157 /*
5158 * Fetch the declaration for the qualified name
5159 */
5160 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5161 prefix = elem->ns->prefix;
5162
5163 if (prefix != NULL) {
5164 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5165 elem->name, prefix);
5166 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5167 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5168 elem->name, prefix);
5169 if ((elemDecl != NULL) && (extsubset != NULL))
5170 *extsubset = 1;
5171 }
5172 }
5173
5174 /*
5175 * Fetch the declaration for the non qualified name
5176 * This is "non-strict" validation should be done on the
5177 * full QName but in that case being flexible makes sense.
5178 */
5179 if (elemDecl == NULL) {
5180 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5181 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5182 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5183 if ((elemDecl != NULL) && (extsubset != NULL))
5184 *extsubset = 1;
5185 }
5186 }
5187 if (elemDecl == NULL) {
5188 VECTXT(ctxt, elem);
5189 VERROR(ctxt->userData, "No declaration for element %s\n",
5190 elem->name);
5191 }
5192 return(elemDecl);
5193}
5194
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005195#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005196/**
5197 * xmlValidatePushElement:
5198 * @ctxt: the validation context
5199 * @doc: a document instance
5200 * @elem: an element instance
5201 * @qname: the qualified name as appearing in the serialization
5202 *
5203 * Push a new element start on the validation stack.
5204 *
5205 * returns 1 if no validation problem was found or 0 otherwise
5206 */
5207int
5208xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5209 xmlNodePtr elem, const xmlChar *qname) {
5210 int ret = 1;
5211 xmlElementPtr eDecl;
5212 int extsubset = 0;
5213
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005214/* printf("PushElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005215 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5216 xmlValidStatePtr state = ctxt->vstate;
5217 xmlElementPtr elemDecl;
5218
5219 /*
5220 * Check the new element agaisnt the content model of the new elem.
5221 */
5222 if (state->elemDecl != NULL) {
5223 elemDecl = state->elemDecl;
5224
5225 switch(elemDecl->etype) {
5226 case XML_ELEMENT_TYPE_UNDEFINED:
5227 ret = 0;
5228 break;
5229 case XML_ELEMENT_TYPE_EMPTY:
5230 VECTXT(ctxt, state->node);
5231 VERROR(ctxt->userData,
5232 "Element %s was declared EMPTY this one has content\n",
5233 state->node->name);
5234 ret = 0;
5235 break;
5236 case XML_ELEMENT_TYPE_ANY:
5237 /* I don't think anything is required then */
5238 break;
5239 case XML_ELEMENT_TYPE_MIXED:
5240 /* simple case of declared as #PCDATA */
5241 if ((elemDecl->content != NULL) &&
5242 (elemDecl->content->type ==
5243 XML_ELEMENT_CONTENT_PCDATA)) {
5244 VECTXT(ctxt, state->node);
5245 VERROR(ctxt->userData,
5246 "Element %s was declared #PCDATA but contains non text nodes\n",
5247 state->node->name);
5248 ret = 0;
5249 } else {
5250 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5251 qname);
5252 if (ret != 1) {
5253 VECTXT(ctxt, state->node);
5254 VERROR(ctxt->userData,
5255 "Element %s is not declared in %s list of possible children\n",
5256 qname, state->node->name);
5257 }
5258 }
5259 break;
5260 case XML_ELEMENT_TYPE_ELEMENT:
5261 /*
5262 * TODO:
5263 * VC: Standalone Document Declaration
5264 * - element types with element content, if white space
5265 * occurs directly within any instance of those types.
5266 */
5267 if (state->exec != NULL) {
5268 ret = xmlRegExecPushString(state->exec, qname, NULL);
5269 if (ret < 0) {
5270 VECTXT(ctxt, state->node);
5271 VERROR(ctxt->userData,
5272 "Element %s content does not follow the DTD\nMisplaced %s\n",
5273 state->node->name, qname);
5274 ret = 0;
5275 } else {
5276 ret = 1;
5277 }
5278 }
5279 break;
5280 }
5281 }
5282 }
5283 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5284 vstateVPush(ctxt, eDecl, elem);
5285 return(ret);
5286}
5287
5288/**
5289 * xmlValidatePushCData:
5290 * @ctxt: the validation context
5291 * @data: some character data read
5292 * @len: the lenght of the data
5293 *
5294 * check the CData parsed for validation in the current stack
5295 *
5296 * returns 1 if no validation problem was found or 0 otherwise
5297 */
5298int
5299xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5300 int ret = 1;
5301
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005302/* printf("CDATA %s %d\n", data, len); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005303 if (len <= 0)
5304 return(ret);
5305 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5306 xmlValidStatePtr state = ctxt->vstate;
5307 xmlElementPtr elemDecl;
5308
5309 /*
5310 * Check the new element agaisnt the content model of the new elem.
5311 */
5312 if (state->elemDecl != NULL) {
5313 elemDecl = state->elemDecl;
5314
5315 switch(elemDecl->etype) {
5316 case XML_ELEMENT_TYPE_UNDEFINED:
5317 ret = 0;
5318 break;
5319 case XML_ELEMENT_TYPE_EMPTY:
5320 VECTXT(ctxt, state->node);
5321 VERROR(ctxt->userData,
5322 "Element %s was declared EMPTY this one has content\n",
5323 state->node->name);
5324 ret = 0;
5325 break;
5326 case XML_ELEMENT_TYPE_ANY:
5327 break;
5328 case XML_ELEMENT_TYPE_MIXED:
5329 break;
5330 case XML_ELEMENT_TYPE_ELEMENT:
5331 if (len > 0) {
5332 int i;
5333
5334 for (i = 0;i < len;i++) {
5335 if (!IS_BLANK(data[i])) {
5336 VECTXT(ctxt, state->node);
5337 VERROR(ctxt->userData,
5338 "Element %s content does not follow the DTD\nText not allowed\n",
5339 state->node->name);
5340 ret = 0;
5341 goto done;
5342 }
5343 }
5344 /*
5345 * TODO:
5346 * VC: Standalone Document Declaration
5347 * element types with element content, if white space
5348 * occurs directly within any instance of those types.
5349 */
5350 }
5351 break;
5352 }
5353 }
5354 }
5355done:
5356 return(ret);
5357}
5358
5359/**
5360 * xmlValidatePopElement:
5361 * @ctxt: the validation context
5362 * @doc: a document instance
5363 * @elem: an element instance
5364 * @qname: the qualified name as appearing in the serialization
5365 *
5366 * Pop the element end from the validation stack.
5367 *
5368 * returns 1 if no validation problem was found or 0 otherwise
5369 */
5370int
5371xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard580ced82003-03-21 21:22:48 +00005372 xmlNodePtr elem ATTRIBUTE_UNUSED,
5373 const xmlChar *qname ATTRIBUTE_UNUSED) {
Daniel Veillardea7751d2002-12-20 00:16:24 +00005374 int ret = 1;
5375
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005376/* printf("PopElem %s\n", qname); */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005377 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5378 xmlValidStatePtr state = ctxt->vstate;
5379 xmlElementPtr elemDecl;
5380
5381 /*
5382 * Check the new element agaisnt the content model of the new elem.
5383 */
5384 if (state->elemDecl != NULL) {
5385 elemDecl = state->elemDecl;
5386
5387 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5388 if (state->exec != NULL) {
5389 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5390 if (ret == 0) {
5391 VECTXT(ctxt, state->node);
5392 VERROR(ctxt->userData,
5393 "Element %s content does not follow the DTD\nExpecting more child\n",
5394 state->node->name);
5395 } else {
5396 /*
5397 * previous validation errors should not generate
5398 * a new one here
5399 */
5400 ret = 1;
5401 }
5402 }
5403 }
5404 }
5405 vstateVPop(ctxt);
5406 }
5407 return(ret);
5408}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005409#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005410
5411/**
Owen Taylor3473f882001-02-23 17:55:21 +00005412 * xmlValidateOneElement:
5413 * @ctxt: the validation context
5414 * @doc: a document instance
5415 * @elem: an element instance
5416 *
5417 * Try to validate a single element and it's attributes,
5418 * basically it does the following checks as described by the
5419 * XML-1.0 recommendation:
5420 * - [ VC: Element Valid ]
5421 * - [ VC: Required Attribute ]
5422 * Then call xmlValidateOneAttribute() for each attribute present.
5423 *
5424 * The ID/IDREF checkings are done separately
5425 *
5426 * returns 1 if valid or 0 otherwise
5427 */
5428
5429int
5430xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5431 xmlNodePtr elem) {
5432 xmlElementPtr elemDecl = NULL;
5433 xmlElementContentPtr cont;
5434 xmlAttributePtr attr;
5435 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005436 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005437 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005438 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005439
5440 CHECK_DTD;
5441
5442 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005443 switch (elem->type) {
5444 case XML_ATTRIBUTE_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005445 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005446 VERROR(ctxt->userData,
5447 "Attribute element not expected here\n");
5448 return(0);
5449 case XML_TEXT_NODE:
5450 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005451 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005452 VERROR(ctxt->userData, "Text element has childs !\n");
5453 return(0);
5454 }
5455 if (elem->properties != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005456 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005457 VERROR(ctxt->userData, "Text element has attributes !\n");
5458 return(0);
5459 }
5460 if (elem->ns != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005461 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005462 VERROR(ctxt->userData, "Text element has namespace !\n");
5463 return(0);
5464 }
5465 if (elem->nsDef != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005466 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005467 VERROR(ctxt->userData,
5468 "Text element carries namespace definitions !\n");
5469 return(0);
5470 }
5471 if (elem->content == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005472 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005473 VERROR(ctxt->userData,
5474 "Text element has no content !\n");
5475 return(0);
5476 }
5477 return(1);
5478 case XML_XINCLUDE_START:
5479 case XML_XINCLUDE_END:
5480 return(1);
5481 case XML_CDATA_SECTION_NODE:
5482 case XML_ENTITY_REF_NODE:
5483 case XML_PI_NODE:
5484 case XML_COMMENT_NODE:
5485 return(1);
5486 case XML_ENTITY_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005487 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005488 VERROR(ctxt->userData,
5489 "Entity element not expected here\n");
5490 return(0);
5491 case XML_NOTATION_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005492 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005493 VERROR(ctxt->userData,
5494 "Notation element not expected here\n");
5495 return(0);
5496 case XML_DOCUMENT_NODE:
5497 case XML_DOCUMENT_TYPE_NODE:
5498 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005499 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005500 VERROR(ctxt->userData,
5501 "Document element not expected here\n");
5502 return(0);
5503 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005504 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005505 VERROR(ctxt->userData,
5506 "\n");
5507 return(0);
5508 case XML_ELEMENT_NODE:
5509 break;
5510 default:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005511 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005512 VERROR(ctxt->userData,
5513 "unknown element type %d\n", elem->type);
5514 return(0);
5515 }
Owen Taylor3473f882001-02-23 17:55:21 +00005516
5517 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005518 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005519 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005520 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5521 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005522 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005523
Daniel Veillardea7751d2002-12-20 00:16:24 +00005524 /*
5525 * If vstateNr is not zero that means continuous validation is
5526 * activated, do not try to check the content model at that level.
5527 */
5528 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005529 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005530 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005531 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005532 VECTXT(ctxt, elem);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005533 VERROR(ctxt->userData, "No declaration for element %s\n",
5534 elem->name);
5535 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005536 case XML_ELEMENT_TYPE_EMPTY:
5537 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005538 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005539 VERROR(ctxt->userData,
5540 "Element %s was declared EMPTY this one has content\n",
5541 elem->name);
5542 ret = 0;
5543 }
5544 break;
5545 case XML_ELEMENT_TYPE_ANY:
5546 /* I don't think anything is required then */
5547 break;
5548 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005549
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005550 /* simple case of declared as #PCDATA */
5551 if ((elemDecl->content != NULL) &&
5552 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5553 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5554 if (!ret) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005555 VECTXT(ctxt, elem);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005556 VERROR(ctxt->userData,
5557 "Element %s was declared #PCDATA but contains non text nodes\n",
5558 elem->name);
5559 }
5560 break;
5561 }
Owen Taylor3473f882001-02-23 17:55:21 +00005562 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005563 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005564 while (child != NULL) {
5565 if (child->type == XML_ELEMENT_NODE) {
5566 name = child->name;
5567 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005568 xmlChar fn[50];
5569 xmlChar *fullname;
5570
5571 fullname = xmlBuildQName(child->name, child->ns->prefix,
5572 fn, 50);
5573 if (fullname == NULL)
5574 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005575 cont = elemDecl->content;
5576 while (cont != NULL) {
5577 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005578 if (xmlStrEqual(cont->name, fullname))
5579 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005580 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5581 (cont->c1 != NULL) &&
5582 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
Daniel Veillardc00cda82003-04-07 10:22:39 +00005583 if (xmlStrEqual(cont->c1->name, fullname))
5584 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005585 } 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 }
Daniel Veillardc00cda82003-04-07 10:22:39 +00005595 if ((fullname != fn) && (fullname != child->name))
5596 xmlFree(fullname);
Owen Taylor3473f882001-02-23 17:55:21 +00005597 if (cont != NULL)
5598 goto child_ok;
5599 }
5600 cont = elemDecl->content;
5601 while (cont != NULL) {
5602 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5603 if (xmlStrEqual(cont->name, name)) break;
5604 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5605 (cont->c1 != NULL) &&
5606 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5607 if (xmlStrEqual(cont->c1->name, name)) break;
5608 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5609 (cont->c1 == NULL) ||
5610 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
5611 /* Internal error !!! */
5612 xmlGenericError(xmlGenericErrorContext,
5613 "Internal: MIXED struct bad\n");
5614 break;
5615 }
5616 cont = cont->c2;
5617 }
5618 if (cont == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005619 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005620 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005621 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005622 name, elem->name);
5623 ret = 0;
5624 }
5625 }
5626child_ok:
5627 child = child->next;
5628 }
5629 break;
5630 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005631 if ((doc->standalone == 1) && (extsubset == 1)) {
5632 /*
5633 * VC: Standalone Document Declaration
5634 * - element types with element content, if white space
5635 * occurs directly within any instance of those types.
5636 */
5637 child = elem->children;
5638 while (child != NULL) {
5639 if (child->type == XML_TEXT_NODE) {
5640 const xmlChar *content = child->content;
5641
5642 while (IS_BLANK(*content))
5643 content++;
5644 if (*content == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005645 VECTXT(ctxt, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005646 VERROR(ctxt->userData,
5647"standalone: %s declared in the external subset contains white spaces nodes\n",
5648 elem->name);
5649 ret = 0;
5650 break;
5651 }
5652 }
5653 child =child->next;
5654 }
5655 }
Owen Taylor3473f882001-02-23 17:55:21 +00005656 child = elem->children;
5657 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005658 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005659 if (tmp <= 0)
5660 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005661 break;
5662 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005663 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00005664
5665 /* [ VC: Required Attribute ] */
5666 attr = elemDecl->attributes;
5667 while (attr != NULL) {
5668 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005669 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00005670
Daniel Veillarde4301c82002-02-13 13:32:35 +00005671 if ((attr->prefix == NULL) &&
5672 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5673 xmlNsPtr ns;
5674
5675 ns = elem->nsDef;
5676 while (ns != NULL) {
5677 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005678 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005679 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00005680 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005681 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5682 xmlNsPtr ns;
5683
5684 ns = elem->nsDef;
5685 while (ns != NULL) {
5686 if (xmlStrEqual(attr->name, ns->prefix))
5687 goto found;
5688 ns = ns->next;
5689 }
5690 } else {
5691 xmlAttrPtr attrib;
5692
5693 attrib = elem->properties;
5694 while (attrib != NULL) {
5695 if (xmlStrEqual(attrib->name, attr->name)) {
5696 if (attr->prefix != NULL) {
5697 xmlNsPtr nameSpace = attrib->ns;
5698
5699 if (nameSpace == NULL)
5700 nameSpace = elem->ns;
5701 /*
5702 * qualified names handling is problematic, having a
5703 * different prefix should be possible but DTDs don't
5704 * allow to define the URI instead of the prefix :-(
5705 */
5706 if (nameSpace == NULL) {
5707 if (qualified < 0)
5708 qualified = 0;
5709 } else if (!xmlStrEqual(nameSpace->prefix,
5710 attr->prefix)) {
5711 if (qualified < 1)
5712 qualified = 1;
5713 } else
5714 goto found;
5715 } else {
5716 /*
5717 * We should allow applications to define namespaces
5718 * for their application even if the DTD doesn't
5719 * carry one, otherwise, basically we would always
5720 * break.
5721 */
5722 goto found;
5723 }
5724 }
5725 attrib = attrib->next;
5726 }
Owen Taylor3473f882001-02-23 17:55:21 +00005727 }
5728 if (qualified == -1) {
5729 if (attr->prefix == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005730 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005731 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005732 "Element %s does not carry attribute %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005733 elem->name, attr->name);
5734 ret = 0;
5735 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005736 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005737 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005738 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005739 elem->name, attr->prefix,attr->name);
5740 ret = 0;
5741 }
5742 } else if (qualified == 0) {
5743 VWARNING(ctxt->userData,
5744 "Element %s required attribute %s:%s has no prefix\n",
5745 elem->name, attr->prefix,attr->name);
5746 } else if (qualified == 1) {
5747 VWARNING(ctxt->userData,
5748 "Element %s required attribute %s:%s has different prefix\n",
5749 elem->name, attr->prefix,attr->name);
5750 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005751 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
5752 /*
5753 * Special tests checking #FIXED namespace declarations
5754 * have the right value since this is not done as an
5755 * attribute checking
5756 */
5757 if ((attr->prefix == NULL) &&
5758 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5759 xmlNsPtr ns;
5760
5761 ns = elem->nsDef;
5762 while (ns != NULL) {
5763 if (ns->prefix == NULL) {
5764 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005765 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005766 VERROR(ctxt->userData,
5767 "Element %s namespace name for default namespace does not match the DTD\n",
5768 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00005769 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005770 }
5771 goto found;
5772 }
5773 ns = ns->next;
5774 }
5775 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5776 xmlNsPtr ns;
5777
5778 ns = elem->nsDef;
5779 while (ns != NULL) {
5780 if (xmlStrEqual(attr->name, ns->prefix)) {
5781 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005782 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005783 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005784 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillarde4301c82002-02-13 13:32:35 +00005785 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00005786 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005787 }
5788 goto found;
5789 }
5790 ns = ns->next;
5791 }
5792 }
Owen Taylor3473f882001-02-23 17:55:21 +00005793 }
5794found:
5795 attr = attr->nexth;
5796 }
5797 return(ret);
5798}
5799
5800/**
5801 * xmlValidateRoot:
5802 * @ctxt: the validation context
5803 * @doc: a document instance
5804 *
5805 * Try to validate a the root element
5806 * basically it does the following check as described by the
5807 * XML-1.0 recommendation:
5808 * - [ VC: Root Element Type ]
5809 * it doesn't try to recurse or apply other check to the element
5810 *
5811 * returns 1 if valid or 0 otherwise
5812 */
5813
5814int
5815xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5816 xmlNodePtr root;
Daniel Veillardc00cda82003-04-07 10:22:39 +00005817 int ret;
5818
Owen Taylor3473f882001-02-23 17:55:21 +00005819 if (doc == NULL) return(0);
5820
5821 root = xmlDocGetRootElement(doc);
5822 if ((root == NULL) || (root->name == NULL)) {
5823 VERROR(ctxt->userData, "Not valid: no root element\n");
5824 return(0);
5825 }
5826
5827 /*
5828 * When doing post validation against a separate DTD, those may
5829 * no internal subset has been generated
5830 */
5831 if ((doc->intSubset != NULL) &&
5832 (doc->intSubset->name != NULL)) {
5833 /*
5834 * Check first the document root against the NQName
5835 */
5836 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
5837 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
Daniel Veillardc00cda82003-04-07 10:22:39 +00005838 xmlChar fn[50];
5839 xmlChar *fullname;
5840
5841 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
5842 if (fullname == NULL) {
5843 VERROR(ctxt->userData, "Out of memory\n");
5844 return(0);
5845 }
5846 ret = xmlStrEqual(doc->intSubset->name, fullname);
5847 if ((fullname != fn) && (fullname != root->name))
5848 xmlFree(fullname);
5849 if (ret == 1)
Owen Taylor3473f882001-02-23 17:55:21 +00005850 goto name_ok;
5851 }
5852 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
5853 (xmlStrEqual(root->name, BAD_CAST "html")))
5854 goto name_ok;
Daniel Veillard76575762002-09-05 14:21:15 +00005855 VECTXT(ctxt, root);
Owen Taylor3473f882001-02-23 17:55:21 +00005856 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005857 "Not valid: root and DTD name do not match '%s' and '%s'\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005858 root->name, doc->intSubset->name);
5859 return(0);
5860
5861 }
5862 }
5863name_ok:
5864 return(1);
5865}
5866
5867
5868/**
5869 * xmlValidateElement:
5870 * @ctxt: the validation context
5871 * @doc: a document instance
5872 * @elem: an element instance
5873 *
5874 * Try to validate the subtree under an element
5875 *
5876 * returns 1 if valid or 0 otherwise
5877 */
5878
5879int
5880xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
5881 xmlNodePtr child;
5882 xmlAttrPtr attr;
5883 xmlChar *value;
5884 int ret = 1;
5885
5886 if (elem == NULL) return(0);
5887
5888 /*
5889 * XInclude elements were added after parsing in the infoset,
5890 * they don't really mean anything validation wise.
5891 */
5892 if ((elem->type == XML_XINCLUDE_START) ||
5893 (elem->type == XML_XINCLUDE_END))
5894 return(1);
5895
5896 CHECK_DTD;
5897
Daniel Veillard10ea86c2001-06-20 13:55:33 +00005898 /*
5899 * Entities references have to be handled separately
5900 */
5901 if (elem->type == XML_ENTITY_REF_NODE) {
5902 return(1);
5903 }
5904
Owen Taylor3473f882001-02-23 17:55:21 +00005905 ret &= xmlValidateOneElement(ctxt, doc, elem);
5906 attr = elem->properties;
5907 while(attr != NULL) {
5908 value = xmlNodeListGetString(doc, attr->children, 0);
5909 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
5910 if (value != NULL)
5911 xmlFree(value);
5912 attr= attr->next;
5913 }
5914 child = elem->children;
5915 while (child != NULL) {
5916 ret &= xmlValidateElement(ctxt, doc, child);
5917 child = child->next;
5918 }
5919
5920 return(ret);
5921}
5922
Daniel Veillard8730c562001-02-26 10:49:57 +00005923/**
5924 * xmlValidateRef:
5925 * @ref: A reference to be validated
5926 * @ctxt: Validation context
5927 * @name: Name of ID we are searching for
5928 *
5929 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005930static void
Daniel Veillard8730c562001-02-26 10:49:57 +00005931xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00005932 const xmlChar *name) {
5933 xmlAttrPtr id;
5934 xmlAttrPtr attr;
5935
5936 if (ref == NULL)
5937 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005938 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005939 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005940 attr = ref->attr;
5941 if (attr == NULL) {
5942 xmlChar *dup, *str = NULL, *cur, save;
5943
5944 dup = xmlStrdup(name);
5945 if (dup == NULL) {
5946 ctxt->valid = 0;
5947 return;
5948 }
5949 cur = dup;
5950 while (*cur != 0) {
5951 str = cur;
5952 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5953 save = *cur;
5954 *cur = 0;
5955 id = xmlGetID(ctxt->doc, str);
5956 if (id == NULL) {
5957 VERROR(ctxt->userData,
5958 "attribute %s line %d references an unknown ID \"%s\"\n",
5959 ref->name, ref->lineno, str);
5960 ctxt->valid = 0;
5961 }
5962 if (save == 0)
5963 break;
5964 *cur = save;
5965 while (IS_BLANK(*cur)) cur++;
5966 }
5967 xmlFree(dup);
5968 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00005969 id = xmlGetID(ctxt->doc, name);
5970 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005971 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005972 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005973 "IDREF attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005974 attr->name, name);
5975 ctxt->valid = 0;
5976 }
5977 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
5978 xmlChar *dup, *str = NULL, *cur, save;
5979
5980 dup = xmlStrdup(name);
5981 if (dup == NULL) {
5982 ctxt->valid = 0;
5983 return;
5984 }
5985 cur = dup;
5986 while (*cur != 0) {
5987 str = cur;
5988 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5989 save = *cur;
5990 *cur = 0;
5991 id = xmlGetID(ctxt->doc, str);
5992 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005993 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005994 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005995 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005996 attr->name, str);
5997 ctxt->valid = 0;
5998 }
5999 if (save == 0)
6000 break;
6001 *cur = save;
6002 while (IS_BLANK(*cur)) cur++;
6003 }
6004 xmlFree(dup);
6005 }
6006}
6007
6008/**
Daniel Veillard8730c562001-02-26 10:49:57 +00006009 * xmlWalkValidateList:
6010 * @data: Contents of current link
6011 * @user: Value supplied by the user
6012 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006013 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00006014 */
6015static int
6016xmlWalkValidateList(const void *data, const void *user)
6017{
6018 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6019 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6020 return 1;
6021}
6022
6023/**
6024 * xmlValidateCheckRefCallback:
6025 * @ref_list: List of references
6026 * @ctxt: Validation context
6027 * @name: Name of ID we are searching for
6028 *
6029 */
6030static void
6031xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6032 const xmlChar *name) {
6033 xmlValidateMemo memo;
6034
6035 if (ref_list == NULL)
6036 return;
6037 memo.ctxt = ctxt;
6038 memo.name = name;
6039
6040 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6041
6042}
6043
6044/**
Owen Taylor3473f882001-02-23 17:55:21 +00006045 * xmlValidateDocumentFinal:
6046 * @ctxt: the validation context
6047 * @doc: a document instance
6048 *
6049 * Does the final step for the document validation once all the
6050 * incremental validation steps have been completed
6051 *
6052 * basically it does the following checks described by the XML Rec
6053 *
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006054 * Check all the IDREF/IDREFS attributes definition for validity
Owen Taylor3473f882001-02-23 17:55:21 +00006055 *
6056 * returns 1 if valid or 0 otherwise
6057 */
6058
6059int
6060xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6061 xmlRefTablePtr table;
6062
6063 if (doc == NULL) {
6064 xmlGenericError(xmlGenericErrorContext,
6065 "xmlValidateDocumentFinal: doc == NULL\n");
6066 return(0);
6067 }
6068
6069 /*
6070 * Check all the NOTATION/NOTATIONS attributes
6071 */
6072 /*
6073 * Check all the ENTITY/ENTITIES attributes definition for validity
6074 */
6075 /*
6076 * Check all the IDREF/IDREFS attributes definition for validity
6077 */
6078 table = (xmlRefTablePtr) doc->refs;
6079 ctxt->doc = doc;
6080 ctxt->valid = 1;
6081 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6082 return(ctxt->valid);
6083}
6084
6085/**
6086 * xmlValidateDtd:
6087 * @ctxt: the validation context
6088 * @doc: a document instance
6089 * @dtd: a dtd instance
6090 *
6091 * Try to validate the document against the dtd instance
6092 *
6093 * basically it does check all the definitions in the DtD.
6094 *
6095 * returns 1 if valid or 0 otherwise
6096 */
6097
6098int
6099xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6100 int ret;
6101 xmlDtdPtr oldExt;
6102 xmlNodePtr root;
6103
6104 if (dtd == NULL) return(0);
6105 if (doc == NULL) return(0);
6106 oldExt = doc->extSubset;
6107 doc->extSubset = dtd;
6108 ret = xmlValidateRoot(ctxt, doc);
6109 if (ret == 0) {
6110 doc->extSubset = oldExt;
6111 return(ret);
6112 }
6113 if (doc->ids != NULL) {
6114 xmlFreeIDTable(doc->ids);
6115 doc->ids = NULL;
6116 }
6117 if (doc->refs != NULL) {
6118 xmlFreeRefTable(doc->refs);
6119 doc->refs = NULL;
6120 }
6121 root = xmlDocGetRootElement(doc);
6122 ret = xmlValidateElement(ctxt, doc, root);
6123 ret &= xmlValidateDocumentFinal(ctxt, doc);
6124 doc->extSubset = oldExt;
6125 return(ret);
6126}
6127
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006128static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006129xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6130 const xmlChar *name ATTRIBUTE_UNUSED) {
6131 if (cur == NULL)
6132 return;
6133 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6134 xmlChar *notation = cur->content;
6135
Daniel Veillard878eab02002-02-19 13:46:09 +00006136 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006137 int ret;
6138
6139 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6140 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006141 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006142 }
6143 }
6144 }
6145}
6146
6147static void
Owen Taylor3473f882001-02-23 17:55:21 +00006148xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006149 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006150 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006151 xmlDocPtr doc;
6152 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006153
Owen Taylor3473f882001-02-23 17:55:21 +00006154 if (cur == NULL)
6155 return;
6156 switch (cur->atype) {
6157 case XML_ATTRIBUTE_CDATA:
6158 case XML_ATTRIBUTE_ID:
6159 case XML_ATTRIBUTE_IDREF :
6160 case XML_ATTRIBUTE_IDREFS:
6161 case XML_ATTRIBUTE_NMTOKEN:
6162 case XML_ATTRIBUTE_NMTOKENS:
6163 case XML_ATTRIBUTE_ENUMERATION:
6164 break;
6165 case XML_ATTRIBUTE_ENTITY:
6166 case XML_ATTRIBUTE_ENTITIES:
6167 case XML_ATTRIBUTE_NOTATION:
6168 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006169
6170 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6171 cur->atype, cur->defaultValue);
6172 if ((ret == 0) && (ctxt->valid == 1))
6173 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006174 }
6175 if (cur->tree != NULL) {
6176 xmlEnumerationPtr tree = cur->tree;
6177 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006178 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006179 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006180 if ((ret == 0) && (ctxt->valid == 1))
6181 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006182 tree = tree->next;
6183 }
6184 }
6185 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006186 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6187 doc = cur->doc;
6188 if ((doc == NULL) || (cur->elem == NULL)) {
6189 VERROR(ctxt->userData,
6190 "xmlValidateAttributeCallback(%s): internal error\n",
6191 cur->name);
6192 return;
6193 }
6194 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6195 if (elem == NULL)
6196 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
6197 if (elem == NULL) {
6198 VERROR(ctxt->userData,
6199 "attribute %s: could not find decl for element %s\n",
6200 cur->name, cur->elem);
6201 return;
6202 }
6203 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
6204 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006205 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillard878eab02002-02-19 13:46:09 +00006206 cur->name, cur->elem);
6207 ctxt->valid = 0;
6208 }
6209 }
Owen Taylor3473f882001-02-23 17:55:21 +00006210}
6211
6212/**
6213 * xmlValidateDtdFinal:
6214 * @ctxt: the validation context
6215 * @doc: a document instance
6216 *
6217 * Does the final step for the dtds validation once all the
6218 * subsets have been parsed
6219 *
6220 * basically it does the following checks described by the XML Rec
6221 * - check that ENTITY and ENTITIES type attributes default or
6222 * possible values matches one of the defined entities.
6223 * - check that NOTATION type attributes default or
6224 * possible values matches one of the defined notations.
6225 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006226 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006227 */
6228
6229int
6230xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006231 xmlDtdPtr dtd;
6232 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006233 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006234
6235 if (doc == NULL) return(0);
6236 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6237 return(0);
6238 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006239 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006240 dtd = doc->intSubset;
6241 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6242 table = (xmlAttributeTablePtr) dtd->attributes;
6243 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006244 }
6245 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006246 entities = (xmlEntitiesTablePtr) dtd->entities;
6247 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6248 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006249 }
6250 dtd = doc->extSubset;
6251 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6252 table = (xmlAttributeTablePtr) dtd->attributes;
6253 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006254 }
6255 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006256 entities = (xmlEntitiesTablePtr) dtd->entities;
6257 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6258 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006259 }
6260 return(ctxt->valid);
6261}
6262
6263/**
6264 * xmlValidateDocument:
6265 * @ctxt: the validation context
6266 * @doc: a document instance
6267 *
6268 * Try to validate the document instance
6269 *
6270 * basically it does the all the checks described by the XML Rec
6271 * i.e. validates the internal and external subset (if present)
6272 * and validate the document tree.
6273 *
6274 * returns 1 if valid or 0 otherwise
6275 */
6276
6277int
6278xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6279 int ret;
6280 xmlNodePtr root;
6281
Daniel Veillard2fd85422002-10-16 14:32:41 +00006282 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
6283 VERROR(ctxt->userData, "no DTD found!\n" );
Owen Taylor3473f882001-02-23 17:55:21 +00006284 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006285 }
Owen Taylor3473f882001-02-23 17:55:21 +00006286 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6287 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6288 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6289 doc->intSubset->SystemID);
6290 if (doc->extSubset == NULL) {
6291 if (doc->intSubset->SystemID != NULL) {
6292 VERROR(ctxt->userData,
6293 "Could not load the external subset \"%s\"\n",
6294 doc->intSubset->SystemID);
6295 } else {
6296 VERROR(ctxt->userData,
6297 "Could not load the external subset \"%s\"\n",
6298 doc->intSubset->ExternalID);
6299 }
6300 return(0);
6301 }
6302 }
6303
6304 if (doc->ids != NULL) {
6305 xmlFreeIDTable(doc->ids);
6306 doc->ids = NULL;
6307 }
6308 if (doc->refs != NULL) {
6309 xmlFreeRefTable(doc->refs);
6310 doc->refs = NULL;
6311 }
6312 ret = xmlValidateDtdFinal(ctxt, doc);
6313 if (!xmlValidateRoot(ctxt, doc)) return(0);
6314
6315 root = xmlDocGetRootElement(doc);
6316 ret &= xmlValidateElement(ctxt, doc, root);
6317 ret &= xmlValidateDocumentFinal(ctxt, doc);
6318 return(ret);
6319}
6320
Owen Taylor3473f882001-02-23 17:55:21 +00006321/************************************************************************
6322 * *
6323 * Routines for dynamic validation editing *
6324 * *
6325 ************************************************************************/
6326
6327/**
6328 * xmlValidGetPotentialChildren:
6329 * @ctree: an element content tree
6330 * @list: an array to store the list of child names
6331 * @len: a pointer to the number of element in the list
6332 * @max: the size of the array
6333 *
6334 * Build/extend a list of potential children allowed by the content tree
6335 *
6336 * returns the number of element in the list, or -1 in case of error.
6337 */
6338
6339int
6340xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6341 int *len, int max) {
6342 int i;
6343
6344 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6345 return(-1);
6346 if (*len >= max) return(*len);
6347
6348 switch (ctree->type) {
6349 case XML_ELEMENT_CONTENT_PCDATA:
6350 for (i = 0; i < *len;i++)
6351 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6352 list[(*len)++] = BAD_CAST "#PCDATA";
6353 break;
6354 case XML_ELEMENT_CONTENT_ELEMENT:
6355 for (i = 0; i < *len;i++)
6356 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6357 list[(*len)++] = ctree->name;
6358 break;
6359 case XML_ELEMENT_CONTENT_SEQ:
6360 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6361 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6362 break;
6363 case XML_ELEMENT_CONTENT_OR:
6364 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6365 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6366 break;
6367 }
6368
6369 return(*len);
6370}
6371
6372/**
6373 * xmlValidGetValidElements:
6374 * @prev: an element to insert after
6375 * @next: an element to insert next
6376 * @list: an array to store the list of child names
6377 * @max: the size of the array
6378 *
6379 * This function returns the list of authorized children to insert
6380 * within an existing tree while respecting the validity constraints
6381 * forced by the Dtd. The insertion point is defined using @prev and
6382 * @next in the following ways:
6383 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6384 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6385 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6386 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6387 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6388 *
6389 * pointers to the element names are inserted at the beginning of the array
6390 * and do not need to be freed.
6391 *
6392 * returns the number of element in the list, or -1 in case of error. If
6393 * the function returns the value @max the caller is invited to grow the
6394 * receiving array and retry.
6395 */
6396
6397int
6398xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
6399 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006400 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006401 int nb_valid_elements = 0;
6402 const xmlChar *elements[256];
6403 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006404 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006405
6406 xmlNode *ref_node;
6407 xmlNode *parent;
6408 xmlNode *test_node;
6409
6410 xmlNode *prev_next;
6411 xmlNode *next_prev;
6412 xmlNode *parent_childs;
6413 xmlNode *parent_last;
6414
6415 xmlElement *element_desc;
6416
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00006417 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006418
Owen Taylor3473f882001-02-23 17:55:21 +00006419 if (prev == NULL && next == NULL)
6420 return(-1);
6421
6422 if (list == NULL) return(-1);
6423 if (max <= 0) return(-1);
6424
6425 nb_valid_elements = 0;
6426 ref_node = prev ? prev : next;
6427 parent = ref_node->parent;
6428
6429 /*
6430 * Retrieves the parent element declaration
6431 */
6432 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6433 parent->name);
6434 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6435 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6436 parent->name);
6437 if (element_desc == NULL) return(-1);
6438
6439 /*
6440 * Do a backup of the current tree structure
6441 */
6442 prev_next = prev ? prev->next : NULL;
6443 next_prev = next ? next->prev : NULL;
6444 parent_childs = parent->children;
6445 parent_last = parent->last;
6446
6447 /*
6448 * Creates a dummy node and insert it into the tree
6449 */
6450 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
6451 test_node->doc = ref_node->doc;
6452 test_node->parent = parent;
6453 test_node->prev = prev;
6454 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006455 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006456
6457 if (prev) prev->next = test_node;
6458 else parent->children = test_node;
6459
6460 if (next) next->prev = test_node;
6461 else parent->last = test_node;
6462
6463 /*
6464 * Insert each potential child node and check if the parent is
6465 * still valid
6466 */
6467 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6468 elements, &nb_elements, 256);
6469
6470 for (i = 0;i < nb_elements;i++) {
6471 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006472 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006473 int j;
6474
6475 for (j = 0; j < nb_valid_elements;j++)
6476 if (xmlStrEqual(elements[i], list[j])) break;
6477 list[nb_valid_elements++] = elements[i];
6478 if (nb_valid_elements >= max) break;
6479 }
6480 }
6481
6482 /*
6483 * Restore the tree structure
6484 */
6485 if (prev) prev->next = prev_next;
6486 if (next) next->prev = next_prev;
6487 parent->children = parent_childs;
6488 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006489
6490 /*
6491 * Free up the dummy node
6492 */
6493 test_node->name = name;
6494 xmlFreeNode(test_node);
6495
Owen Taylor3473f882001-02-23 17:55:21 +00006496 return(nb_valid_elements);
6497}
Daniel Veillard4432df22003-09-28 18:58:27 +00006498#endif /* LIBXML_VALID_ENABLED */
6499