blob: 4a978c3418937d672d29f4ba4b3087902625a915 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * valid.c : part of the code use to do the DTD handling and the validity
3 * checking
4 *
5 * See Copyright for the status of this software.
6 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <string.h>
14
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18
19#include <libxml/xmlmemory.h>
20#include <libxml/hash.h>
21#include <libxml/valid.h>
22#include <libxml/parser.h>
23#include <libxml/parserInternals.h>
24#include <libxml/xmlerror.h>
25#include <libxml/list.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000026#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000027
Daniel Veillarde62d36c2001-05-15 08:53:16 +000028/* #define DEBUG_VALID_ALGO */
Daniel Veillard5acfd6b2002-09-18 16:29:02 +000029/* #define DEBUG_REGEXP_ALGO */
Daniel Veillarde62d36c2001-05-15 08:53:16 +000030
Daniel Veillarda646cfd2002-09-17 21:50:03 +000031#define TODO \
32 xmlGenericError(xmlGenericErrorContext, \
33 "Unimplemented block at %s:%d\n", \
34 __FILE__, __LINE__);
Owen Taylor3473f882001-02-23 17:55:21 +000035
Daniel Veillardea7751d2002-12-20 00:16:24 +000036#define VERROR \
37 if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error
Owen Taylor3473f882001-02-23 17:55:21 +000038
Daniel Veillardea7751d2002-12-20 00:16:24 +000039#define VWARNING \
40 if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning
41
42
43#ifdef LIBXML_REGEXP_ENABLED
44/*
45 * If regexp are enabled we can do continuous validation without the
46 * need of a tree to validate the content model. this is done in each
47 * callbacks.
48 * Each xmlValidState represent the validation state associated to the
49 * set of nodes currently open from the document root to the current element.
50 */
51
52
53typedef struct _xmlValidState {
54 xmlElementPtr elemDecl; /* pointer to the content model */
55 xmlNodePtr node; /* pointer to the current node */
56 xmlRegExecCtxtPtr exec; /* regexp runtime */
57} _xmlValidState;
58
59
60static int
61vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
62 if (ctxt->vstateMax == 0) {
63 ctxt->vstateMax = 10;
64 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
65 sizeof(ctxt->vstateTab[0]));
66 if (ctxt->vstateTab == NULL) {
67 VERROR(ctxt->userData, "realloc failed !n");
68 return(-1);
69 }
70 }
71
72 if (ctxt->vstateNr >= ctxt->vstateMax) {
73 ctxt->vstateMax *= 2;
74 ctxt->vstateTab = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
75 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
76 if (ctxt->vstateTab == NULL) {
77 VERROR(ctxt->userData, "realloc failed !n");
78 return(-1);
79 }
80 }
81 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
82 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
83 ctxt->vstateTab[ctxt->vstateNr].node = node;
84 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
85 if (elemDecl->contModel == NULL)
86 xmlValidBuildContentModel(ctxt, elemDecl);
87 if (elemDecl->contModel != NULL) {
88 ctxt->vstateTab[ctxt->vstateNr].exec =
89 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
90 } else {
91 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
92 VERROR(ctxt->userData,
93 "Failed to build content model regexp for %s", node->name);
94 }
95 }
96 return(ctxt->vstateNr++);
97}
98
99static int
100vstateVPop(xmlValidCtxtPtr ctxt) {
101 xmlElementPtr elemDecl;
102
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000103 if (ctxt->vstateNr < 1) return(-1);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000104 ctxt->vstateNr--;
105 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
106 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
107 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
108 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
109 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
110 }
111 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
112 if (ctxt->vstateNr >= 1)
113 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
114 else
115 ctxt->vstate = NULL;
116 return(ctxt->vstateNr);
117}
118
119#else /* not LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000120/*
Daniel Veillard1c732d22002-11-30 11:22:59 +0000121 * If regexp are not enabled, it uses a home made algorithm less
122 * complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000123 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000124 * only restriction is on the deepness of the tree limited by the
125 * size of the occurs bitfield
126 *
127 * this is the content of a saved state for rollbacks
128 */
129
130#define ROLLBACK_OR 0
131#define ROLLBACK_PARENT 1
132
Daniel Veillardb44025c2001-10-11 22:55:55 +0000133typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000134 xmlElementContentPtr cont; /* pointer to the content model subtree */
135 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000136 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000137 unsigned char depth; /* current depth in the overall tree */
138 unsigned char state; /* ROLLBACK_XXX */
139} _xmlValidState;
140
Daniel Veillardfc57b412002-04-29 15:50:14 +0000141#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000142#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
143#define CONT ctxt->vstate->cont
144#define NODE ctxt->vstate->node
145#define DEPTH ctxt->vstate->depth
146#define OCCURS ctxt->vstate->occurs
147#define STATE ctxt->vstate->state
148
Daniel Veillard5344c602001-12-31 16:37:34 +0000149#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
150#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000151
Daniel Veillard5344c602001-12-31 16:37:34 +0000152#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
153#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000154
155static int
156vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
157 xmlNodePtr node, unsigned char depth, long occurs,
158 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000159 int i = ctxt->vstateNr - 1;
160
Daniel Veillard940492d2002-04-15 10:15:25 +0000161 if (ctxt->vstateNr > MAX_RECURSE) {
162 return(-1);
163 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000164 if (ctxt->vstateNr >= ctxt->vstateMax) {
165 ctxt->vstateMax *= 2;
166 ctxt->vstateTab = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
167 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
168 if (ctxt->vstateTab == NULL) {
169 xmlGenericError(xmlGenericErrorContext,
170 "realloc failed !n");
Daniel Veillard940492d2002-04-15 10:15:25 +0000171 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000172 }
Daniel Veillard06803992001-04-22 10:35:56 +0000173 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000174 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000175 /*
176 * Don't push on the stack a state already here
177 */
178 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
179 (ctxt->vstateTab[i].node == node) &&
180 (ctxt->vstateTab[i].depth == depth) &&
181 (ctxt->vstateTab[i].occurs == occurs) &&
182 (ctxt->vstateTab[i].state == state))
183 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000184 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
185 ctxt->vstateTab[ctxt->vstateNr].node = node;
186 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
187 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
188 ctxt->vstateTab[ctxt->vstateNr].state = state;
189 return(ctxt->vstateNr++);
190}
191
192static int
193vstateVPop(xmlValidCtxtPtr ctxt) {
194 if (ctxt->vstateNr <= 1) return(-1);
195 ctxt->vstateNr--;
196 ctxt->vstate = &ctxt->vstateTab[0];
197 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
198 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
199 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
200 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
201 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
202 return(ctxt->vstateNr);
203}
204
Daniel Veillard118aed72002-09-24 14:13:13 +0000205#endif /* LIBXML_REGEXP_ENABLED */
206
Daniel Veillard1c732d22002-11-30 11:22:59 +0000207static int
208nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
209{
210 if (ctxt->nodeMax <= 0) {
211 ctxt->nodeMax = 4;
212 ctxt->nodeTab =
213 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
214 sizeof(ctxt->nodeTab[0]));
215 if (ctxt->nodeTab == NULL) {
216 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
217 ctxt->nodeMax = 0;
218 return (0);
219 }
220 }
221 if (ctxt->nodeNr >= ctxt->nodeMax) {
222 ctxt->nodeMax *= 2;
223 ctxt->nodeTab =
224 (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
225 ctxt->nodeMax *
226 sizeof(ctxt->nodeTab[0]));
227 if (ctxt->nodeTab == NULL) {
228 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
229 return (0);
230 }
231 }
232 ctxt->nodeTab[ctxt->nodeNr] = value;
233 ctxt->node = value;
234 return (ctxt->nodeNr++);
235}
236static xmlNodePtr
237nodeVPop(xmlValidCtxtPtr ctxt)
238{
239 xmlNodePtr ret;
240
241 if (ctxt->nodeNr <= 0)
242 return (0);
243 ctxt->nodeNr--;
244 if (ctxt->nodeNr > 0)
245 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
246 else
247 ctxt->node = NULL;
248 ret = ctxt->nodeTab[ctxt->nodeNr];
249 ctxt->nodeTab[ctxt->nodeNr] = 0;
250 return (ret);
251}
Owen Taylor3473f882001-02-23 17:55:21 +0000252
Daniel Veillard336fc7d2002-12-27 19:37:04 +0000253#if 0
254/**
255 * xmlFreeValidCtxt:
256 * @ctxt: a validation context
257 *
258 * Free the memory allocated for a validation context
259 */
260void
261xmlFreeValidCtxt(xmlValidCtxtPtr ctxt) {
262 if (ctxt == NULL)
263 return;
264#ifdef LIBXML_REGEXP_ENABLED
265 while (ctxt->vstateNr >= 0)
266 vstateVPop(ctxt);
267 if (ctxt->vstateNr <= 1) return(-1);
268 ctxt->vstateNr--;
269 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
270 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
271 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
272 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
273 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
274 }
275 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
276 if (ctxt->vstateNr >= 1)
277 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
278 else
279 ctxt->vstate = NULL;
280 return(ctxt->vstateNr);
281#else /* ! LIBXML_REGEXP_ENABLED */
282#endif /* LIBXML_REGEXP_ENABLED */
283}
284#endif
285
Owen Taylor3473f882001-02-23 17:55:21 +0000286#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000287static void
288xmlValidPrintNode(xmlNodePtr cur) {
289 if (cur == NULL) {
290 xmlGenericError(xmlGenericErrorContext, "null");
291 return;
292 }
293 switch (cur->type) {
294 case XML_ELEMENT_NODE:
295 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
296 break;
297 case XML_TEXT_NODE:
298 xmlGenericError(xmlGenericErrorContext, "text ");
299 break;
300 case XML_CDATA_SECTION_NODE:
301 xmlGenericError(xmlGenericErrorContext, "cdata ");
302 break;
303 case XML_ENTITY_REF_NODE:
304 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
305 break;
306 case XML_PI_NODE:
307 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
308 break;
309 case XML_COMMENT_NODE:
310 xmlGenericError(xmlGenericErrorContext, "comment ");
311 break;
312 case XML_ATTRIBUTE_NODE:
313 xmlGenericError(xmlGenericErrorContext, "?attr? ");
314 break;
315 case XML_ENTITY_NODE:
316 xmlGenericError(xmlGenericErrorContext, "?ent? ");
317 break;
318 case XML_DOCUMENT_NODE:
319 xmlGenericError(xmlGenericErrorContext, "?doc? ");
320 break;
321 case XML_DOCUMENT_TYPE_NODE:
322 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
323 break;
324 case XML_DOCUMENT_FRAG_NODE:
325 xmlGenericError(xmlGenericErrorContext, "?frag? ");
326 break;
327 case XML_NOTATION_NODE:
328 xmlGenericError(xmlGenericErrorContext, "?nota? ");
329 break;
330 case XML_HTML_DOCUMENT_NODE:
331 xmlGenericError(xmlGenericErrorContext, "?html? ");
332 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000333#ifdef LIBXML_DOCB_ENABLED
334 case XML_DOCB_DOCUMENT_NODE:
335 xmlGenericError(xmlGenericErrorContext, "?docb? ");
336 break;
337#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000338 case XML_DTD_NODE:
339 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
340 break;
341 case XML_ELEMENT_DECL:
342 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
343 break;
344 case XML_ATTRIBUTE_DECL:
345 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
346 break;
347 case XML_ENTITY_DECL:
348 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
349 break;
350 case XML_NAMESPACE_DECL:
351 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
352 break;
353 case XML_XINCLUDE_START:
354 xmlGenericError(xmlGenericErrorContext, "incstart ");
355 break;
356 case XML_XINCLUDE_END:
357 xmlGenericError(xmlGenericErrorContext, "incend ");
358 break;
359 }
360}
361
362static void
363xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000364 if (cur == NULL)
365 xmlGenericError(xmlGenericErrorContext, "null ");
366 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000367 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000368 cur = cur->next;
369 }
370}
371
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000372static void
373xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Daniel Veillard83391282003-03-06 21:37:30 +0000374 char expr[5000];
Owen Taylor3473f882001-02-23 17:55:21 +0000375
376 expr[0] = 0;
377 xmlGenericError(xmlGenericErrorContext, "valid: ");
378 xmlValidPrintNodeList(cur);
379 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000380 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000381 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
382}
383
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000384static void
385xmlValidDebugState(xmlValidStatePtr state) {
386 xmlGenericError(xmlGenericErrorContext, "(");
387 if (state->cont == NULL)
388 xmlGenericError(xmlGenericErrorContext, "null,");
389 else
390 switch (state->cont->type) {
391 case XML_ELEMENT_CONTENT_PCDATA:
392 xmlGenericError(xmlGenericErrorContext, "pcdata,");
393 break;
394 case XML_ELEMENT_CONTENT_ELEMENT:
395 xmlGenericError(xmlGenericErrorContext, "%s,",
396 state->cont->name);
397 break;
398 case XML_ELEMENT_CONTENT_SEQ:
399 xmlGenericError(xmlGenericErrorContext, "seq,");
400 break;
401 case XML_ELEMENT_CONTENT_OR:
402 xmlGenericError(xmlGenericErrorContext, "or,");
403 break;
404 }
405 xmlValidPrintNode(state->node);
406 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
407 state->depth, state->occurs, state->state);
408}
409
410static void
411xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
412 int i, j;
413
414 xmlGenericError(xmlGenericErrorContext, "state: ");
415 xmlValidDebugState(ctxt->vstate);
416 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
417 ctxt->vstateNr - 1);
418 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
419 xmlValidDebugState(&ctxt->vstateTab[j]);
420 xmlGenericError(xmlGenericErrorContext, "\n");
421}
422
423/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000424#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000425 *****/
426
427#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000428#define DEBUG_VALID_MSG(m) \
429 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
430
Owen Taylor3473f882001-02-23 17:55:21 +0000431#else
432#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000433#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000434#endif
435
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000436/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000437
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000438#define VECTXT(ctxt, node) \
439 if ((ctxt != NULL) && (ctxt->error != NULL) && \
Daniel Veillard76575762002-09-05 14:21:15 +0000440 (node != NULL)) { \
441 xmlChar *base = xmlNodeGetBase(NULL,node); \
442 if (base != NULL) { \
443 ctxt->error(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->error(ctxt->userData, ":%d: ", \
Daniel Veillard366a9152002-10-23 20:43:53 +0000448 (int) (long) node->content); \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000449 }
450
451#define VWCTXT(ctxt, node) \
452 if ((ctxt != NULL) && (ctxt->warning != NULL) && \
Daniel Veillard76575762002-09-05 14:21:15 +0000453 (node != NULL)) { \
454 xmlChar *base = xmlNodeGetBase(NULL,node); \
455 if (base != NULL) { \
456 ctxt->warning(ctxt->userData, "%s:%d: ", base, \
Daniel Veillard366a9152002-10-23 20:43:53 +0000457 (int) (long) node->content); \
Daniel Veillard76575762002-09-05 14:21:15 +0000458 xmlFree(base); \
459 } else \
460 ctxt->warning(ctxt->userData, ":%d: ", \
Daniel Veillard366a9152002-10-23 20:43:53 +0000461 (int) (long) node->content); \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000462 }
463
Owen Taylor3473f882001-02-23 17:55:21 +0000464#define CHECK_DTD \
465 if (doc == NULL) return(0); \
466 else if ((doc->intSubset == NULL) && \
467 (doc->extSubset == NULL)) return(0)
468
Daniel Veillarda10efa82001-04-18 13:09:01 +0000469static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
470 int create);
Owen Taylor3473f882001-02-23 17:55:21 +0000471xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
472
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000473#ifdef LIBXML_REGEXP_ENABLED
474
475/************************************************************************
476 * *
477 * Content model validation based on the regexps *
478 * *
479 ************************************************************************/
480
481/**
482 * xmlValidBuildAContentModel:
483 * @content: the content model
484 * @ctxt: the schema parser context
485 * @name: the element name whose content is being built
486 *
487 * Generate the automata sequence needed for that type
488 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000489 * Returns 1 if successful or 0 in case of error.
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000490 */
491static int
492xmlValidBuildAContentModel(xmlElementContentPtr content,
493 xmlValidCtxtPtr ctxt,
494 const xmlChar *name) {
495 if (content == NULL) {
496 VERROR(ctxt->userData,
497 "Found unexpected type = NULL in %s content model\n", name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000498 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000499 }
500 switch (content->type) {
501 case XML_ELEMENT_CONTENT_PCDATA:
502 VERROR(ctxt->userData, "ContentModel found PCDATA for element %s\n",
503 name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000504 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000505 break;
506 case XML_ELEMENT_CONTENT_ELEMENT: {
507 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000508 xmlChar *QName = NULL;
509 const xmlChar *fname = content->name;
510
511 if (content->prefix != NULL) {
512 int len;
513
514 len = xmlStrlen(content->name) +
515 xmlStrlen(content->prefix) + 2;
516 QName = xmlMalloc(len);
517 if (QName == NULL) {
518 VERROR(ctxt->userData,
519 "ContentModel %s : alloc failed\n", name);
520 return(0);
521 }
522 snprintf((char *) QName, len, "%s:%s",
523 (char *)content->prefix,
524 (char *)content->name);
525 fname = QName;
526 }
527
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000528 switch (content->ocur) {
529 case XML_ELEMENT_CONTENT_ONCE:
530 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000531 ctxt->state, NULL, fname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000532 break;
533 case XML_ELEMENT_CONTENT_OPT:
534 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000535 ctxt->state, NULL, fname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000536 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
537 break;
538 case XML_ELEMENT_CONTENT_PLUS:
539 ctxt->state = xmlAutomataNewTransition(ctxt->am,
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000540 ctxt->state, NULL, fname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000541 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000542 ctxt->state, fname, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000543 break;
544 case XML_ELEMENT_CONTENT_MULT:
545 xmlAutomataNewTransition(ctxt->am, ctxt->state,
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000546 ctxt->state, fname, NULL);
Daniel Veillard57e79b32003-02-04 15:33:12 +0000547 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
548 NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000549 break;
550 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000551 if (QName != NULL)
552 xmlFree(QName);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000553 break;
554 }
555 case XML_ELEMENT_CONTENT_SEQ: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000556 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000557 xmlElementContentOccur ocur;
558
559 /*
560 * Simply iterate over the content
561 */
562 oldstate = ctxt->state;
563 ocur = content->ocur;
Daniel Veillardf4be0182003-02-24 19:54:33 +0000564 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
565 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
566 oldstate = ctxt->state;
567 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000568 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000569 xmlValidBuildAContentModel(content->c1, ctxt, name);
570 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000571 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
572 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
573 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000574 oldend = ctxt->state;
575 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000576 switch (ocur) {
577 case XML_ELEMENT_CONTENT_ONCE:
578 break;
579 case XML_ELEMENT_CONTENT_OPT:
580 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
581 break;
582 case XML_ELEMENT_CONTENT_MULT:
583 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000584 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000585 break;
586 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000587 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000588 break;
589 }
590 break;
591 }
592 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000593 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000594 xmlElementContentOccur ocur;
595
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000596 ocur = content->ocur;
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000597 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
598 (ocur == XML_ELEMENT_CONTENT_MULT)) {
599 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
600 ctxt->state, NULL);
601 }
602 oldstate = ctxt->state;
603 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000604
605 /*
606 * iterate over the subtypes and remerge the end with an
607 * epsilon transition
608 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000609 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000610 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000611 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000612 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000613 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000614 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
615 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000616 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000617 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000618 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
619 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000620 switch (ocur) {
621 case XML_ELEMENT_CONTENT_ONCE:
622 break;
623 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000624 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000625 break;
626 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000627 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
628 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000629 break;
630 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000631 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000632 break;
633 }
634 break;
635 }
636 default:
637 VERROR(ctxt->userData, "ContentModel broken for element %s\n",
638 name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000639 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000640 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000641 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000642}
643/**
644 * xmlValidBuildContentModel:
645 * @ctxt: a validation context
646 * @elem: an element declaration node
647 *
648 * (Re)Build the automata associated to the content model of this
649 * element
650 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000651 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000652 */
653int
654xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
655 xmlAutomataStatePtr start;
656
657 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000658 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000659 if (elem->type != XML_ELEMENT_DECL)
660 return(0);
661 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
662 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000663 /* TODO: should we rebuild in this case ? */
Daniel Veillardec498e12003-02-05 11:01:50 +0000664 if (elem->contModel != NULL) {
665 if (!xmlRegexpIsDeterminist(elem->contModel)) {
666 ctxt->valid = 0;
667 return(0);
668 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000669 return(1);
Daniel Veillardec498e12003-02-05 11:01:50 +0000670 }
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000671
672 ctxt->am = xmlNewAutomata();
673 if (ctxt->am == NULL) {
674 VERROR(ctxt->userData, "Cannot create automata for element %s\n",
675 elem->name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000676 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000677 }
678 start = ctxt->state = xmlAutomataGetInitState(ctxt->am);
679 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
680 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000681 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillard23e73572002-09-19 19:56:43 +0000682 if (!xmlRegexpIsDeterminist(elem->contModel)) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000683 char expr[5000];
684 expr[0] = 0;
685 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
686 VERROR(ctxt->userData, "Content model of %s is not determinist: %s\n",
687 elem->name, expr);
688#ifdef DEBUG_REGEXP_ALGO
689 xmlRegexpPrint(stderr, elem->contModel);
690#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000691 ctxt->valid = 0;
Daniel Veillardec498e12003-02-05 11:01:50 +0000692 ctxt->state = NULL;
693 xmlFreeAutomata(ctxt->am);
694 ctxt->am = NULL;
695 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000696 }
697 ctxt->state = NULL;
698 xmlFreeAutomata(ctxt->am);
699 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000700 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000701}
702
703#endif /* LIBXML_REGEXP_ENABLED */
704
Owen Taylor3473f882001-02-23 17:55:21 +0000705/************************************************************************
706 * *
707 * QName handling helper *
708 * *
709 ************************************************************************/
710
711/**
712 * xmlSplitQName2:
713 * @name: an XML parser context
714 * @prefix: a xmlChar **
715 *
716 * parse an XML qualified name string
717 *
718 * [NS 5] QName ::= (Prefix ':')? LocalPart
719 *
720 * [NS 6] Prefix ::= NCName
721 *
722 * [NS 7] LocalPart ::= NCName
723 *
724 * Returns NULL if not a QName, otherwise the local part, and prefix
725 * is updated to get the Prefix if any.
726 */
727
728xmlChar *
729xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
730 int len = 0;
731 xmlChar *ret = NULL;
732
733 *prefix = NULL;
734
Daniel Veillardf4309d72001-10-02 09:28:58 +0000735#ifndef XML_XML_NAMESPACE
Owen Taylor3473f882001-02-23 17:55:21 +0000736 /* xml: prefix is not really a namespace */
737 if ((name[0] == 'x') && (name[1] == 'm') &&
738 (name[2] == 'l') && (name[3] == ':'))
739 return(NULL);
Daniel Veillardf4309d72001-10-02 09:28:58 +0000740#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000741
742 /* nasty but valid */
743 if (name[0] == ':')
744 return(NULL);
745
746 /*
747 * we are not trying to validate but just to cut, and yes it will
748 * work even if this is as set of UTF-8 encoded chars
749 */
750 while ((name[len] != 0) && (name[len] != ':'))
751 len++;
752
753 if (name[len] == 0)
754 return(NULL);
755
756 *prefix = xmlStrndup(name, len);
757 ret = xmlStrdup(&name[len + 1]);
758
759 return(ret);
760}
761
762/****************************************************************
763 * *
764 * Util functions for data allocation/deallocation *
765 * *
766 ****************************************************************/
767
768/**
769 * xmlNewElementContent:
770 * @name: the subelement name or NULL
771 * @type: the type of element content decl
772 *
773 * Allocate an element content structure.
774 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000775 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000776 */
777xmlElementContentPtr
778xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
779 xmlElementContentPtr ret;
780
781 switch(type) {
782 case XML_ELEMENT_CONTENT_ELEMENT:
783 if (name == NULL) {
784 xmlGenericError(xmlGenericErrorContext,
785 "xmlNewElementContent : name == NULL !\n");
786 }
787 break;
788 case XML_ELEMENT_CONTENT_PCDATA:
789 case XML_ELEMENT_CONTENT_SEQ:
790 case XML_ELEMENT_CONTENT_OR:
791 if (name != NULL) {
792 xmlGenericError(xmlGenericErrorContext,
793 "xmlNewElementContent : name != NULL !\n");
794 }
795 break;
796 default:
797 xmlGenericError(xmlGenericErrorContext,
798 "xmlNewElementContent: unknown type %d\n", type);
799 return(NULL);
800 }
801 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
802 if (ret == NULL) {
803 xmlGenericError(xmlGenericErrorContext,
804 "xmlNewElementContent : out of memory!\n");
805 return(NULL);
806 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000807 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000808 ret->type = type;
809 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000810 if (name != NULL) {
811 xmlChar *prefix = NULL;
812 ret->name = xmlSplitQName2(name, &prefix);
813 if (ret->name == NULL)
814 ret->name = xmlStrdup(name);
815 ret->prefix = prefix;
816 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000817 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000818 ret->prefix = NULL;
819 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000820 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000821 return(ret);
822}
823
824/**
825 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000826 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +0000827 *
828 * Build a copy of an element content description.
829 *
830 * Returns the new xmlElementContentPtr or NULL in case of error.
831 */
832xmlElementContentPtr
833xmlCopyElementContent(xmlElementContentPtr cur) {
834 xmlElementContentPtr ret;
835
836 if (cur == NULL) return(NULL);
837 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
838 if (ret == NULL) {
839 xmlGenericError(xmlGenericErrorContext,
840 "xmlCopyElementContent : out of memory\n");
841 return(NULL);
842 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000843 if (cur->prefix != NULL)
844 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000845 ret->ocur = cur->ocur;
846 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000847 if (ret->c1 != NULL)
848 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000849 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000850 if (ret->c2 != NULL)
851 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000852 return(ret);
853}
854
855/**
856 * xmlFreeElementContent:
857 * @cur: the element content tree to free
858 *
859 * Free an element content structure. This is a recursive call !
860 */
861void
862xmlFreeElementContent(xmlElementContentPtr cur) {
863 if (cur == NULL) return;
864 switch (cur->type) {
865 case XML_ELEMENT_CONTENT_PCDATA:
866 case XML_ELEMENT_CONTENT_ELEMENT:
867 case XML_ELEMENT_CONTENT_SEQ:
868 case XML_ELEMENT_CONTENT_OR:
869 break;
870 default:
871 xmlGenericError(xmlGenericErrorContext,
872 "xmlFreeElementContent : type %d\n", cur->type);
873 return;
874 }
875 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
876 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
877 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000878 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000879 xmlFree(cur);
880}
881
882/**
883 * xmlDumpElementContent:
884 * @buf: An XML buffer
885 * @content: An element table
886 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
887 *
888 * This will dump the content of the element table as an XML DTD definition
889 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000890static void
Owen Taylor3473f882001-02-23 17:55:21 +0000891xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
892 if (content == NULL) return;
893
894 if (glob) xmlBufferWriteChar(buf, "(");
895 switch (content->type) {
896 case XML_ELEMENT_CONTENT_PCDATA:
897 xmlBufferWriteChar(buf, "#PCDATA");
898 break;
899 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000900 if (content->prefix != NULL) {
901 xmlBufferWriteCHAR(buf, content->prefix);
902 xmlBufferWriteChar(buf, ":");
903 }
Owen Taylor3473f882001-02-23 17:55:21 +0000904 xmlBufferWriteCHAR(buf, content->name);
905 break;
906 case XML_ELEMENT_CONTENT_SEQ:
907 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
908 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
909 xmlDumpElementContent(buf, content->c1, 1);
910 else
911 xmlDumpElementContent(buf, content->c1, 0);
912 xmlBufferWriteChar(buf, " , ");
913 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
914 xmlDumpElementContent(buf, content->c2, 1);
915 else
916 xmlDumpElementContent(buf, content->c2, 0);
917 break;
918 case XML_ELEMENT_CONTENT_OR:
919 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
920 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
921 xmlDumpElementContent(buf, content->c1, 1);
922 else
923 xmlDumpElementContent(buf, content->c1, 0);
924 xmlBufferWriteChar(buf, " | ");
925 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
926 xmlDumpElementContent(buf, content->c2, 1);
927 else
928 xmlDumpElementContent(buf, content->c2, 0);
929 break;
930 default:
931 xmlGenericError(xmlGenericErrorContext,
932 "xmlDumpElementContent: unknown type %d\n",
933 content->type);
934 }
935 if (glob)
936 xmlBufferWriteChar(buf, ")");
937 switch (content->ocur) {
938 case XML_ELEMENT_CONTENT_ONCE:
939 break;
940 case XML_ELEMENT_CONTENT_OPT:
941 xmlBufferWriteChar(buf, "?");
942 break;
943 case XML_ELEMENT_CONTENT_MULT:
944 xmlBufferWriteChar(buf, "*");
945 break;
946 case XML_ELEMENT_CONTENT_PLUS:
947 xmlBufferWriteChar(buf, "+");
948 break;
949 }
950}
951
952/**
953 * xmlSprintfElementContent:
954 * @buf: an output buffer
955 * @content: An element table
956 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
957 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000958 * Deprecated, unsafe, use xmlSnprintfElementContent
959 */
960void
961xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
962 xmlElementContentPtr content ATTRIBUTE_UNUSED,
963 int glob ATTRIBUTE_UNUSED) {
964}
965
966/**
967 * xmlSnprintfElementContent:
968 * @buf: an output buffer
969 * @size: the buffer size
970 * @content: An element table
971 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
972 *
Owen Taylor3473f882001-02-23 17:55:21 +0000973 * This will dump the content of the element content definition
974 * Intended just for the debug routine
975 */
976void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000977xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
978 int len;
979
Owen Taylor3473f882001-02-23 17:55:21 +0000980 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000981 len = strlen(buf);
982 if (size - len < 50) {
983 if ((size - len > 4) && (buf[len - 1] != '.'))
984 strcat(buf, " ...");
985 return;
986 }
Owen Taylor3473f882001-02-23 17:55:21 +0000987 if (glob) strcat(buf, "(");
988 switch (content->type) {
989 case XML_ELEMENT_CONTENT_PCDATA:
990 strcat(buf, "#PCDATA");
991 break;
992 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000993 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000994 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000995 strcat(buf, " ...");
996 return;
997 }
998 strcat(buf, (char *) content->prefix);
999 strcat(buf, ":");
1000 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00001001 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00001002 strcat(buf, " ...");
1003 return;
1004 }
Owen Taylor3473f882001-02-23 17:55:21 +00001005 strcat(buf, (char *) content->name);
1006 break;
1007 case XML_ELEMENT_CONTENT_SEQ:
1008 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1009 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001010 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001011 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001012 xmlSnprintfElementContent(buf, size, content->c1, 0);
1013 len = strlen(buf);
1014 if (size - len < 50) {
1015 if ((size - len > 4) && (buf[len - 1] != '.'))
1016 strcat(buf, " ...");
1017 return;
1018 }
Owen Taylor3473f882001-02-23 17:55:21 +00001019 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001020 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1021 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1022 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001023 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001024 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001025 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001026 break;
1027 case XML_ELEMENT_CONTENT_OR:
1028 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1029 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001030 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001031 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001032 xmlSnprintfElementContent(buf, size, content->c1, 0);
1033 len = strlen(buf);
1034 if (size - len < 50) {
1035 if ((size - len > 4) && (buf[len - 1] != '.'))
1036 strcat(buf, " ...");
1037 return;
1038 }
Owen Taylor3473f882001-02-23 17:55:21 +00001039 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001040 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1041 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1042 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001043 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001044 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001045 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001046 break;
1047 }
1048 if (glob)
1049 strcat(buf, ")");
1050 switch (content->ocur) {
1051 case XML_ELEMENT_CONTENT_ONCE:
1052 break;
1053 case XML_ELEMENT_CONTENT_OPT:
1054 strcat(buf, "?");
1055 break;
1056 case XML_ELEMENT_CONTENT_MULT:
1057 strcat(buf, "*");
1058 break;
1059 case XML_ELEMENT_CONTENT_PLUS:
1060 strcat(buf, "+");
1061 break;
1062 }
1063}
1064
1065/****************************************************************
1066 * *
1067 * Registration of DTD declarations *
1068 * *
1069 ****************************************************************/
1070
1071/**
1072 * xmlCreateElementTable:
1073 *
1074 * create and initialize an empty element hash table.
1075 *
1076 * Returns the xmlElementTablePtr just created or NULL in case of error.
1077 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001078static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001079xmlCreateElementTable(void) {
1080 return(xmlHashCreate(0));
1081}
1082
1083/**
1084 * xmlFreeElement:
1085 * @elem: An element
1086 *
1087 * Deallocate the memory used by an element definition
1088 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001089static void
Owen Taylor3473f882001-02-23 17:55:21 +00001090xmlFreeElement(xmlElementPtr elem) {
1091 if (elem == NULL) return;
1092 xmlUnlinkNode((xmlNodePtr) elem);
1093 xmlFreeElementContent(elem->content);
1094 if (elem->name != NULL)
1095 xmlFree((xmlChar *) elem->name);
1096 if (elem->prefix != NULL)
1097 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001098#ifdef LIBXML_REGEXP_ENABLED
1099 if (elem->contModel != NULL)
1100 xmlRegFreeRegexp(elem->contModel);
1101#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001102 xmlFree(elem);
1103}
1104
1105
1106/**
1107 * xmlAddElementDecl:
1108 * @ctxt: the validation context
1109 * @dtd: pointer to the DTD
1110 * @name: the entity name
1111 * @type: the element type
1112 * @content: the element content tree or NULL
1113 *
1114 * Register a new element declaration
1115 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001116 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001117 */
1118xmlElementPtr
1119xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
1120 xmlElementTypeVal type,
1121 xmlElementContentPtr content) {
1122 xmlElementPtr ret;
1123 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001124 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001125 xmlChar *ns, *uqname;
1126
1127 if (dtd == NULL) {
1128 xmlGenericError(xmlGenericErrorContext,
1129 "xmlAddElementDecl: dtd == NULL\n");
1130 return(NULL);
1131 }
1132 if (name == NULL) {
1133 xmlGenericError(xmlGenericErrorContext,
1134 "xmlAddElementDecl: name == NULL\n");
1135 return(NULL);
1136 }
1137 switch (type) {
1138 case XML_ELEMENT_TYPE_EMPTY:
1139 if (content != NULL) {
1140 xmlGenericError(xmlGenericErrorContext,
1141 "xmlAddElementDecl: content != NULL for EMPTY\n");
1142 return(NULL);
1143 }
1144 break;
1145 case XML_ELEMENT_TYPE_ANY:
1146 if (content != NULL) {
1147 xmlGenericError(xmlGenericErrorContext,
1148 "xmlAddElementDecl: content != NULL for ANY\n");
1149 return(NULL);
1150 }
1151 break;
1152 case XML_ELEMENT_TYPE_MIXED:
1153 if (content == NULL) {
1154 xmlGenericError(xmlGenericErrorContext,
1155 "xmlAddElementDecl: content == NULL for MIXED\n");
1156 return(NULL);
1157 }
1158 break;
1159 case XML_ELEMENT_TYPE_ELEMENT:
1160 if (content == NULL) {
1161 xmlGenericError(xmlGenericErrorContext,
1162 "xmlAddElementDecl: content == NULL for ELEMENT\n");
1163 return(NULL);
1164 }
1165 break;
1166 default:
1167 xmlGenericError(xmlGenericErrorContext,
1168 "xmlAddElementDecl: unknown type %d\n", type);
1169 return(NULL);
1170 }
1171
1172 /*
1173 * check if name is a QName
1174 */
1175 uqname = xmlSplitQName2(name, &ns);
1176 if (uqname != NULL)
1177 name = uqname;
1178
1179 /*
1180 * Create the Element table if needed.
1181 */
1182 table = (xmlElementTablePtr) dtd->elements;
1183 if (table == NULL) {
1184 table = xmlCreateElementTable();
1185 dtd->elements = (void *) table;
1186 }
1187 if (table == NULL) {
1188 xmlGenericError(xmlGenericErrorContext,
1189 "xmlAddElementDecl: Table creation failed!\n");
1190 return(NULL);
1191 }
1192
Daniel Veillarda10efa82001-04-18 13:09:01 +00001193 /*
1194 * lookup old attributes inserted on an undefined element in the
1195 * internal subset.
1196 */
1197 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1198 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1199 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1200 oldAttributes = ret->attributes;
1201 ret->attributes = NULL;
1202 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1203 xmlFreeElement(ret);
1204 }
Owen Taylor3473f882001-02-23 17:55:21 +00001205 }
Owen Taylor3473f882001-02-23 17:55:21 +00001206
1207 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001208 * The element may already be present if one of its attribute
1209 * was registered first
1210 */
1211 ret = xmlHashLookup2(table, name, ns);
1212 if (ret != NULL) {
1213 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
1214 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001215 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001216 */
1217 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
1218 if (uqname != NULL)
1219 xmlFree(uqname);
1220 return(NULL);
1221 }
1222 } else {
1223 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1224 if (ret == NULL) {
1225 xmlGenericError(xmlGenericErrorContext,
1226 "xmlAddElementDecl: out of memory\n");
1227 return(NULL);
1228 }
1229 memset(ret, 0, sizeof(xmlElement));
1230 ret->type = XML_ELEMENT_DECL;
1231
1232 /*
1233 * fill the structure.
1234 */
1235 ret->name = xmlStrdup(name);
1236 ret->prefix = ns;
1237
1238 /*
1239 * Validity Check:
1240 * Insertion must not fail
1241 */
1242 if (xmlHashAddEntry2(table, name, ns, ret)) {
1243 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001244 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001245 */
1246 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
1247 xmlFreeElement(ret);
1248 if (uqname != NULL)
1249 xmlFree(uqname);
1250 return(NULL);
1251 }
1252 }
1253
1254 /*
1255 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001256 */
1257 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +00001258 ret->content = xmlCopyElementContent(content);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001259 ret->attributes = oldAttributes;
Owen Taylor3473f882001-02-23 17:55:21 +00001260
1261 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001262 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001263 */
1264 ret->parent = dtd;
1265 ret->doc = dtd->doc;
1266 if (dtd->last == NULL) {
1267 dtd->children = dtd->last = (xmlNodePtr) ret;
1268 } else {
1269 dtd->last->next = (xmlNodePtr) ret;
1270 ret->prev = dtd->last;
1271 dtd->last = (xmlNodePtr) ret;
1272 }
1273 if (uqname != NULL)
1274 xmlFree(uqname);
1275 return(ret);
1276}
1277
1278/**
1279 * xmlFreeElementTable:
1280 * @table: An element table
1281 *
1282 * Deallocate the memory used by an element hash table.
1283 */
1284void
1285xmlFreeElementTable(xmlElementTablePtr table) {
1286 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1287}
1288
1289/**
1290 * xmlCopyElement:
1291 * @elem: An element
1292 *
1293 * Build a copy of an element.
1294 *
1295 * Returns the new xmlElementPtr or NULL in case of error.
1296 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001297static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001298xmlCopyElement(xmlElementPtr elem) {
1299 xmlElementPtr cur;
1300
1301 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1302 if (cur == NULL) {
1303 xmlGenericError(xmlGenericErrorContext,
1304 "xmlCopyElement: out of memory !\n");
1305 return(NULL);
1306 }
1307 memset(cur, 0, sizeof(xmlElement));
1308 cur->type = XML_ELEMENT_DECL;
1309 cur->etype = elem->etype;
1310 if (elem->name != NULL)
1311 cur->name = xmlStrdup(elem->name);
1312 else
1313 cur->name = NULL;
1314 if (elem->prefix != NULL)
1315 cur->prefix = xmlStrdup(elem->prefix);
1316 else
1317 cur->prefix = NULL;
1318 cur->content = xmlCopyElementContent(elem->content);
1319 /* TODO : rebuild the attribute list on the copy */
1320 cur->attributes = NULL;
1321 return(cur);
1322}
1323
1324/**
1325 * xmlCopyElementTable:
1326 * @table: An element table
1327 *
1328 * Build a copy of an element table.
1329 *
1330 * Returns the new xmlElementTablePtr or NULL in case of error.
1331 */
1332xmlElementTablePtr
1333xmlCopyElementTable(xmlElementTablePtr table) {
1334 return((xmlElementTablePtr) xmlHashCopy(table,
1335 (xmlHashCopier) xmlCopyElement));
1336}
1337
1338/**
1339 * xmlDumpElementDecl:
1340 * @buf: the XML buffer output
1341 * @elem: An element table
1342 *
1343 * This will dump the content of the element declaration as an XML
1344 * DTD definition
1345 */
1346void
1347xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1348 switch (elem->etype) {
1349 case XML_ELEMENT_TYPE_EMPTY:
1350 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001351 if (elem->prefix != NULL) {
1352 xmlBufferWriteCHAR(buf, elem->prefix);
1353 xmlBufferWriteChar(buf, ":");
1354 }
Owen Taylor3473f882001-02-23 17:55:21 +00001355 xmlBufferWriteCHAR(buf, elem->name);
1356 xmlBufferWriteChar(buf, " EMPTY>\n");
1357 break;
1358 case XML_ELEMENT_TYPE_ANY:
1359 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001360 if (elem->prefix != NULL) {
1361 xmlBufferWriteCHAR(buf, elem->prefix);
1362 xmlBufferWriteChar(buf, ":");
1363 }
Owen Taylor3473f882001-02-23 17:55:21 +00001364 xmlBufferWriteCHAR(buf, elem->name);
1365 xmlBufferWriteChar(buf, " ANY>\n");
1366 break;
1367 case XML_ELEMENT_TYPE_MIXED:
1368 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001369 if (elem->prefix != NULL) {
1370 xmlBufferWriteCHAR(buf, elem->prefix);
1371 xmlBufferWriteChar(buf, ":");
1372 }
Owen Taylor3473f882001-02-23 17:55:21 +00001373 xmlBufferWriteCHAR(buf, elem->name);
1374 xmlBufferWriteChar(buf, " ");
1375 xmlDumpElementContent(buf, elem->content, 1);
1376 xmlBufferWriteChar(buf, ">\n");
1377 break;
1378 case XML_ELEMENT_TYPE_ELEMENT:
1379 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001380 if (elem->prefix != NULL) {
1381 xmlBufferWriteCHAR(buf, elem->prefix);
1382 xmlBufferWriteChar(buf, ":");
1383 }
Owen Taylor3473f882001-02-23 17:55:21 +00001384 xmlBufferWriteCHAR(buf, elem->name);
1385 xmlBufferWriteChar(buf, " ");
1386 xmlDumpElementContent(buf, elem->content, 1);
1387 xmlBufferWriteChar(buf, ">\n");
1388 break;
1389 default:
1390 xmlGenericError(xmlGenericErrorContext,
1391 "xmlDumpElementDecl: internal: unknown type %d\n",
1392 elem->etype);
1393 }
1394}
1395
1396/**
1397 * xmlDumpElementTable:
1398 * @buf: the XML buffer output
1399 * @table: An element table
1400 *
1401 * This will dump the content of the element table as an XML DTD definition
1402 */
1403void
1404xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1405 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1406}
1407
1408/**
1409 * xmlCreateEnumeration:
1410 * @name: the enumeration name or NULL
1411 *
1412 * create and initialize an enumeration attribute node.
1413 *
1414 * Returns the xmlEnumerationPtr just created or NULL in case
1415 * of error.
1416 */
1417xmlEnumerationPtr
1418xmlCreateEnumeration(xmlChar *name) {
1419 xmlEnumerationPtr ret;
1420
1421 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1422 if (ret == NULL) {
1423 xmlGenericError(xmlGenericErrorContext,
1424 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1425 (long)sizeof(xmlEnumeration));
1426 return(NULL);
1427 }
1428 memset(ret, 0, sizeof(xmlEnumeration));
1429
1430 if (name != NULL)
1431 ret->name = xmlStrdup(name);
1432 return(ret);
1433}
1434
1435/**
1436 * xmlFreeEnumeration:
1437 * @cur: the tree to free.
1438 *
1439 * free an enumeration attribute node (recursive).
1440 */
1441void
1442xmlFreeEnumeration(xmlEnumerationPtr cur) {
1443 if (cur == NULL) return;
1444
1445 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1446
1447 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001448 xmlFree(cur);
1449}
1450
1451/**
1452 * xmlCopyEnumeration:
1453 * @cur: the tree to copy.
1454 *
1455 * Copy an enumeration attribute node (recursive).
1456 *
1457 * Returns the xmlEnumerationPtr just created or NULL in case
1458 * of error.
1459 */
1460xmlEnumerationPtr
1461xmlCopyEnumeration(xmlEnumerationPtr cur) {
1462 xmlEnumerationPtr ret;
1463
1464 if (cur == NULL) return(NULL);
1465 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1466
1467 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1468 else ret->next = NULL;
1469
1470 return(ret);
1471}
1472
1473/**
1474 * xmlDumpEnumeration:
1475 * @buf: the XML buffer output
1476 * @enum: An enumeration
1477 *
1478 * This will dump the content of the enumeration
1479 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001480static void
Owen Taylor3473f882001-02-23 17:55:21 +00001481xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1482 if (cur == NULL) return;
1483
1484 xmlBufferWriteCHAR(buf, cur->name);
1485 if (cur->next == NULL)
1486 xmlBufferWriteChar(buf, ")");
1487 else {
1488 xmlBufferWriteChar(buf, " | ");
1489 xmlDumpEnumeration(buf, cur->next);
1490 }
1491}
1492
1493/**
1494 * xmlCreateAttributeTable:
1495 *
1496 * create and initialize an empty attribute hash table.
1497 *
1498 * Returns the xmlAttributeTablePtr just created or NULL in case
1499 * of error.
1500 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001501static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001502xmlCreateAttributeTable(void) {
1503 return(xmlHashCreate(0));
1504}
1505
1506/**
1507 * xmlScanAttributeDeclCallback:
1508 * @attr: the attribute decl
1509 * @list: the list to update
1510 *
1511 * Callback called by xmlScanAttributeDecl when a new attribute
1512 * has to be entered in the list.
1513 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001514static void
Owen Taylor3473f882001-02-23 17:55:21 +00001515xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001516 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001517 attr->nexth = *list;
1518 *list = attr;
1519}
1520
1521/**
1522 * xmlScanAttributeDecl:
1523 * @dtd: pointer to the DTD
1524 * @elem: the element name
1525 *
1526 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001527 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001528 *
1529 * Returns the pointer to the first attribute decl in the chain,
1530 * possibly NULL.
1531 */
1532xmlAttributePtr
1533xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1534 xmlAttributePtr ret = NULL;
1535 xmlAttributeTablePtr table;
1536
1537 if (dtd == NULL) {
1538 xmlGenericError(xmlGenericErrorContext,
1539 "xmlScanAttributeDecl: dtd == NULL\n");
1540 return(NULL);
1541 }
1542 if (elem == NULL) {
1543 xmlGenericError(xmlGenericErrorContext,
1544 "xmlScanAttributeDecl: elem == NULL\n");
1545 return(NULL);
1546 }
1547 table = (xmlAttributeTablePtr) dtd->attributes;
1548 if (table == NULL)
1549 return(NULL);
1550
1551 /* WRONG !!! */
1552 xmlHashScan3(table, NULL, NULL, elem,
1553 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1554 return(ret);
1555}
1556
1557/**
1558 * xmlScanIDAttributeDecl:
1559 * @ctxt: the validation context
1560 * @elem: the element name
1561 *
1562 * Verify that the element don't have too many ID attributes
1563 * declared.
1564 *
1565 * Returns the number of ID attributes found.
1566 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001567static int
Owen Taylor3473f882001-02-23 17:55:21 +00001568xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1569 xmlAttributePtr cur;
1570 int ret = 0;
1571
1572 if (elem == NULL) return(0);
1573 cur = elem->attributes;
1574 while (cur != NULL) {
1575 if (cur->atype == XML_ATTRIBUTE_ID) {
1576 ret ++;
1577 if (ret > 1)
1578 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001579 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001580 elem->name, cur->name);
1581 }
1582 cur = cur->nexth;
1583 }
1584 return(ret);
1585}
1586
1587/**
1588 * xmlFreeAttribute:
1589 * @elem: An attribute
1590 *
1591 * Deallocate the memory used by an attribute definition
1592 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001593static void
Owen Taylor3473f882001-02-23 17:55:21 +00001594xmlFreeAttribute(xmlAttributePtr attr) {
1595 if (attr == NULL) return;
1596 xmlUnlinkNode((xmlNodePtr) attr);
1597 if (attr->tree != NULL)
1598 xmlFreeEnumeration(attr->tree);
1599 if (attr->elem != NULL)
1600 xmlFree((xmlChar *) attr->elem);
1601 if (attr->name != NULL)
1602 xmlFree((xmlChar *) attr->name);
1603 if (attr->defaultValue != NULL)
1604 xmlFree((xmlChar *) attr->defaultValue);
1605 if (attr->prefix != NULL)
1606 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001607 xmlFree(attr);
1608}
1609
1610
1611/**
1612 * xmlAddAttributeDecl:
1613 * @ctxt: the validation context
1614 * @dtd: pointer to the DTD
1615 * @elem: the element name
1616 * @name: the attribute name
1617 * @ns: the attribute namespace prefix
1618 * @type: the attribute type
1619 * @def: the attribute default type
1620 * @defaultValue: the attribute default value
1621 * @tree: if it's an enumeration, the associated list
1622 *
1623 * Register a new attribute declaration
1624 * Note that @tree becomes the ownership of the DTD
1625 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001626 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001627 */
1628xmlAttributePtr
1629xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1630 const xmlChar *name, const xmlChar *ns,
1631 xmlAttributeType type, xmlAttributeDefault def,
1632 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1633 xmlAttributePtr ret;
1634 xmlAttributeTablePtr table;
1635 xmlElementPtr elemDef;
1636
1637 if (dtd == NULL) {
1638 xmlGenericError(xmlGenericErrorContext,
1639 "xmlAddAttributeDecl: dtd == NULL\n");
1640 xmlFreeEnumeration(tree);
1641 return(NULL);
1642 }
1643 if (name == NULL) {
1644 xmlGenericError(xmlGenericErrorContext,
1645 "xmlAddAttributeDecl: name == NULL\n");
1646 xmlFreeEnumeration(tree);
1647 return(NULL);
1648 }
1649 if (elem == NULL) {
1650 xmlGenericError(xmlGenericErrorContext,
1651 "xmlAddAttributeDecl: elem == NULL\n");
1652 xmlFreeEnumeration(tree);
1653 return(NULL);
1654 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001655
Owen Taylor3473f882001-02-23 17:55:21 +00001656 /*
1657 * Check the type and possibly the default value.
1658 */
1659 switch (type) {
1660 case XML_ATTRIBUTE_CDATA:
1661 break;
1662 case XML_ATTRIBUTE_ID:
1663 break;
1664 case XML_ATTRIBUTE_IDREF:
1665 break;
1666 case XML_ATTRIBUTE_IDREFS:
1667 break;
1668 case XML_ATTRIBUTE_ENTITY:
1669 break;
1670 case XML_ATTRIBUTE_ENTITIES:
1671 break;
1672 case XML_ATTRIBUTE_NMTOKEN:
1673 break;
1674 case XML_ATTRIBUTE_NMTOKENS:
1675 break;
1676 case XML_ATTRIBUTE_ENUMERATION:
1677 break;
1678 case XML_ATTRIBUTE_NOTATION:
1679 break;
1680 default:
1681 xmlGenericError(xmlGenericErrorContext,
1682 "xmlAddAttributeDecl: unknown type %d\n", type);
1683 xmlFreeEnumeration(tree);
1684 return(NULL);
1685 }
1686 if ((defaultValue != NULL) &&
1687 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillard58e44c92002-08-02 22:19:49 +00001688 VERROR(ctxt->userData, "Attribute %s of %s: invalid default value\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001689 elem, name, defaultValue);
1690 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001691 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001692 }
1693
1694 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001695 * Check first that an attribute defined in the external subset wasn't
1696 * already defined in the internal subset
1697 */
1698 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1699 (dtd->doc->intSubset != NULL) &&
1700 (dtd->doc->intSubset->attributes != NULL)) {
1701 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1702 if (ret != NULL)
1703 return(NULL);
1704 }
1705
1706 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001707 * Create the Attribute table if needed.
1708 */
1709 table = (xmlAttributeTablePtr) dtd->attributes;
1710 if (table == NULL) {
1711 table = xmlCreateAttributeTable();
1712 dtd->attributes = (void *) table;
1713 }
1714 if (table == NULL) {
1715 xmlGenericError(xmlGenericErrorContext,
1716 "xmlAddAttributeDecl: Table creation failed!\n");
1717 return(NULL);
1718 }
1719
1720
1721 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1722 if (ret == NULL) {
1723 xmlGenericError(xmlGenericErrorContext,
1724 "xmlAddAttributeDecl: out of memory\n");
1725 return(NULL);
1726 }
1727 memset(ret, 0, sizeof(xmlAttribute));
1728 ret->type = XML_ATTRIBUTE_DECL;
1729
1730 /*
1731 * fill the structure.
1732 */
1733 ret->atype = type;
1734 ret->name = xmlStrdup(name);
1735 ret->prefix = xmlStrdup(ns);
1736 ret->elem = xmlStrdup(elem);
1737 ret->def = def;
1738 ret->tree = tree;
1739 if (defaultValue != NULL)
1740 ret->defaultValue = xmlStrdup(defaultValue);
1741
1742 /*
1743 * Validity Check:
1744 * Search the DTD for previous declarations of the ATTLIST
1745 */
1746 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1747 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001748 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001749 */
1750 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00001751 "Attribute %s of element %s: already defined\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001752 name, elem);
1753 xmlFreeAttribute(ret);
1754 return(NULL);
1755 }
1756
1757 /*
1758 * Validity Check:
1759 * Multiple ID per element
1760 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001761 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001762 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001763
Owen Taylor3473f882001-02-23 17:55:21 +00001764 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001765 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001766 VERROR(ctxt->userData,
1767 "Element %s has too may ID attributes defined : %s\n",
1768 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001769 ctxt->valid = 0;
1770 }
1771
Daniel Veillard48da9102001-08-07 01:10:10 +00001772 /*
1773 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001774 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001775 */
1776 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1777 ((ret->prefix != NULL &&
1778 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1779 ret->nexth = elemDef->attributes;
1780 elemDef->attributes = ret;
1781 } else {
1782 xmlAttributePtr tmp = elemDef->attributes;
1783
1784 while ((tmp != NULL) &&
1785 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1786 ((ret->prefix != NULL &&
1787 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1788 if (tmp->nexth == NULL)
1789 break;
1790 tmp = tmp->nexth;
1791 }
1792 if (tmp != NULL) {
1793 ret->nexth = tmp->nexth;
1794 tmp->nexth = ret;
1795 } else {
1796 ret->nexth = elemDef->attributes;
1797 elemDef->attributes = ret;
1798 }
1799 }
Owen Taylor3473f882001-02-23 17:55:21 +00001800 }
1801
1802 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001803 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001804 */
1805 ret->parent = dtd;
1806 ret->doc = dtd->doc;
1807 if (dtd->last == NULL) {
1808 dtd->children = dtd->last = (xmlNodePtr) ret;
1809 } else {
1810 dtd->last->next = (xmlNodePtr) ret;
1811 ret->prev = dtd->last;
1812 dtd->last = (xmlNodePtr) ret;
1813 }
1814 return(ret);
1815}
1816
1817/**
1818 * xmlFreeAttributeTable:
1819 * @table: An attribute table
1820 *
1821 * Deallocate the memory used by an entities hash table.
1822 */
1823void
1824xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1825 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1826}
1827
1828/**
1829 * xmlCopyAttribute:
1830 * @attr: An attribute
1831 *
1832 * Build a copy of an attribute.
1833 *
1834 * Returns the new xmlAttributePtr or NULL in case of error.
1835 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001836static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001837xmlCopyAttribute(xmlAttributePtr attr) {
1838 xmlAttributePtr cur;
1839
1840 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1841 if (cur == NULL) {
1842 xmlGenericError(xmlGenericErrorContext,
1843 "xmlCopyAttribute: out of memory !\n");
1844 return(NULL);
1845 }
1846 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001847 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001848 cur->atype = attr->atype;
1849 cur->def = attr->def;
1850 cur->tree = xmlCopyEnumeration(attr->tree);
1851 if (attr->elem != NULL)
1852 cur->elem = xmlStrdup(attr->elem);
1853 if (attr->name != NULL)
1854 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001855 if (attr->prefix != NULL)
1856 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001857 if (attr->defaultValue != NULL)
1858 cur->defaultValue = xmlStrdup(attr->defaultValue);
1859 return(cur);
1860}
1861
1862/**
1863 * xmlCopyAttributeTable:
1864 * @table: An attribute table
1865 *
1866 * Build a copy of an attribute table.
1867 *
1868 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1869 */
1870xmlAttributeTablePtr
1871xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1872 return((xmlAttributeTablePtr) xmlHashCopy(table,
1873 (xmlHashCopier) xmlCopyAttribute));
1874}
1875
1876/**
1877 * xmlDumpAttributeDecl:
1878 * @buf: the XML buffer output
1879 * @attr: An attribute declaration
1880 *
1881 * This will dump the content of the attribute declaration as an XML
1882 * DTD definition
1883 */
1884void
1885xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1886 xmlBufferWriteChar(buf, "<!ATTLIST ");
1887 xmlBufferWriteCHAR(buf, attr->elem);
1888 xmlBufferWriteChar(buf, " ");
1889 if (attr->prefix != NULL) {
1890 xmlBufferWriteCHAR(buf, attr->prefix);
1891 xmlBufferWriteChar(buf, ":");
1892 }
1893 xmlBufferWriteCHAR(buf, attr->name);
1894 switch (attr->atype) {
1895 case XML_ATTRIBUTE_CDATA:
1896 xmlBufferWriteChar(buf, " CDATA");
1897 break;
1898 case XML_ATTRIBUTE_ID:
1899 xmlBufferWriteChar(buf, " ID");
1900 break;
1901 case XML_ATTRIBUTE_IDREF:
1902 xmlBufferWriteChar(buf, " IDREF");
1903 break;
1904 case XML_ATTRIBUTE_IDREFS:
1905 xmlBufferWriteChar(buf, " IDREFS");
1906 break;
1907 case XML_ATTRIBUTE_ENTITY:
1908 xmlBufferWriteChar(buf, " ENTITY");
1909 break;
1910 case XML_ATTRIBUTE_ENTITIES:
1911 xmlBufferWriteChar(buf, " ENTITIES");
1912 break;
1913 case XML_ATTRIBUTE_NMTOKEN:
1914 xmlBufferWriteChar(buf, " NMTOKEN");
1915 break;
1916 case XML_ATTRIBUTE_NMTOKENS:
1917 xmlBufferWriteChar(buf, " NMTOKENS");
1918 break;
1919 case XML_ATTRIBUTE_ENUMERATION:
1920 xmlBufferWriteChar(buf, " (");
1921 xmlDumpEnumeration(buf, attr->tree);
1922 break;
1923 case XML_ATTRIBUTE_NOTATION:
1924 xmlBufferWriteChar(buf, " NOTATION (");
1925 xmlDumpEnumeration(buf, attr->tree);
1926 break;
1927 default:
1928 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001929 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001930 attr->atype);
1931 }
1932 switch (attr->def) {
1933 case XML_ATTRIBUTE_NONE:
1934 break;
1935 case XML_ATTRIBUTE_REQUIRED:
1936 xmlBufferWriteChar(buf, " #REQUIRED");
1937 break;
1938 case XML_ATTRIBUTE_IMPLIED:
1939 xmlBufferWriteChar(buf, " #IMPLIED");
1940 break;
1941 case XML_ATTRIBUTE_FIXED:
1942 xmlBufferWriteChar(buf, " #FIXED");
1943 break;
1944 default:
1945 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001946 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001947 attr->def);
1948 }
1949 if (attr->defaultValue != NULL) {
1950 xmlBufferWriteChar(buf, " ");
1951 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1952 }
1953 xmlBufferWriteChar(buf, ">\n");
1954}
1955
1956/**
1957 * xmlDumpAttributeTable:
1958 * @buf: the XML buffer output
1959 * @table: An attribute table
1960 *
1961 * This will dump the content of the attribute table as an XML DTD definition
1962 */
1963void
1964xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1965 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1966}
1967
1968/************************************************************************
1969 * *
1970 * NOTATIONs *
1971 * *
1972 ************************************************************************/
1973/**
1974 * xmlCreateNotationTable:
1975 *
1976 * create and initialize an empty notation hash table.
1977 *
1978 * Returns the xmlNotationTablePtr just created or NULL in case
1979 * of error.
1980 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001981static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001982xmlCreateNotationTable(void) {
1983 return(xmlHashCreate(0));
1984}
1985
1986/**
1987 * xmlFreeNotation:
1988 * @not: A notation
1989 *
1990 * Deallocate the memory used by an notation definition
1991 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001992static void
Owen Taylor3473f882001-02-23 17:55:21 +00001993xmlFreeNotation(xmlNotationPtr nota) {
1994 if (nota == NULL) return;
1995 if (nota->name != NULL)
1996 xmlFree((xmlChar *) nota->name);
1997 if (nota->PublicID != NULL)
1998 xmlFree((xmlChar *) nota->PublicID);
1999 if (nota->SystemID != NULL)
2000 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00002001 xmlFree(nota);
2002}
2003
2004
2005/**
2006 * xmlAddNotationDecl:
2007 * @dtd: pointer to the DTD
2008 * @ctxt: the validation context
2009 * @name: the entity name
2010 * @PublicID: the public identifier or NULL
2011 * @SystemID: the system identifier or NULL
2012 *
2013 * Register a new notation declaration
2014 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002015 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002016 */
2017xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002018xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002019 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002020 const xmlChar *PublicID, const xmlChar *SystemID) {
2021 xmlNotationPtr ret;
2022 xmlNotationTablePtr table;
2023
2024 if (dtd == NULL) {
2025 xmlGenericError(xmlGenericErrorContext,
2026 "xmlAddNotationDecl: dtd == NULL\n");
2027 return(NULL);
2028 }
2029 if (name == NULL) {
2030 xmlGenericError(xmlGenericErrorContext,
2031 "xmlAddNotationDecl: name == NULL\n");
2032 return(NULL);
2033 }
2034 if ((PublicID == NULL) && (SystemID == NULL)) {
2035 xmlGenericError(xmlGenericErrorContext,
2036 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002037 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002038 }
2039
2040 /*
2041 * Create the Notation table if needed.
2042 */
2043 table = (xmlNotationTablePtr) dtd->notations;
2044 if (table == NULL)
2045 dtd->notations = table = xmlCreateNotationTable();
2046 if (table == NULL) {
2047 xmlGenericError(xmlGenericErrorContext,
2048 "xmlAddNotationDecl: Table creation failed!\n");
2049 return(NULL);
2050 }
2051
2052 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2053 if (ret == NULL) {
2054 xmlGenericError(xmlGenericErrorContext,
2055 "xmlAddNotationDecl: out of memory\n");
2056 return(NULL);
2057 }
2058 memset(ret, 0, sizeof(xmlNotation));
2059
2060 /*
2061 * fill the structure.
2062 */
2063 ret->name = xmlStrdup(name);
2064 if (SystemID != NULL)
2065 ret->SystemID = xmlStrdup(SystemID);
2066 if (PublicID != NULL)
2067 ret->PublicID = xmlStrdup(PublicID);
2068
2069 /*
2070 * Validity Check:
2071 * Check the DTD for previous declarations of the ATTLIST
2072 */
2073 if (xmlHashAddEntry(table, name, ret)) {
2074 xmlGenericError(xmlGenericErrorContext,
2075 "xmlAddNotationDecl: %s already defined\n", name);
2076 xmlFreeNotation(ret);
2077 return(NULL);
2078 }
2079 return(ret);
2080}
2081
2082/**
2083 * xmlFreeNotationTable:
2084 * @table: An notation table
2085 *
2086 * Deallocate the memory used by an entities hash table.
2087 */
2088void
2089xmlFreeNotationTable(xmlNotationTablePtr table) {
2090 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2091}
2092
2093/**
2094 * xmlCopyNotation:
2095 * @nota: A notation
2096 *
2097 * Build a copy of a notation.
2098 *
2099 * Returns the new xmlNotationPtr or NULL in case of error.
2100 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002101static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002102xmlCopyNotation(xmlNotationPtr nota) {
2103 xmlNotationPtr cur;
2104
2105 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2106 if (cur == NULL) {
2107 xmlGenericError(xmlGenericErrorContext,
2108 "xmlCopyNotation: out of memory !\n");
2109 return(NULL);
2110 }
2111 if (nota->name != NULL)
2112 cur->name = xmlStrdup(nota->name);
2113 else
2114 cur->name = NULL;
2115 if (nota->PublicID != NULL)
2116 cur->PublicID = xmlStrdup(nota->PublicID);
2117 else
2118 cur->PublicID = NULL;
2119 if (nota->SystemID != NULL)
2120 cur->SystemID = xmlStrdup(nota->SystemID);
2121 else
2122 cur->SystemID = NULL;
2123 return(cur);
2124}
2125
2126/**
2127 * xmlCopyNotationTable:
2128 * @table: A notation table
2129 *
2130 * Build a copy of a notation table.
2131 *
2132 * Returns the new xmlNotationTablePtr or NULL in case of error.
2133 */
2134xmlNotationTablePtr
2135xmlCopyNotationTable(xmlNotationTablePtr table) {
2136 return((xmlNotationTablePtr) xmlHashCopy(table,
2137 (xmlHashCopier) xmlCopyNotation));
2138}
2139
2140/**
2141 * xmlDumpNotationDecl:
2142 * @buf: the XML buffer output
2143 * @nota: A notation declaration
2144 *
2145 * This will dump the content the notation declaration as an XML DTD definition
2146 */
2147void
2148xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2149 xmlBufferWriteChar(buf, "<!NOTATION ");
2150 xmlBufferWriteCHAR(buf, nota->name);
2151 if (nota->PublicID != NULL) {
2152 xmlBufferWriteChar(buf, " PUBLIC ");
2153 xmlBufferWriteQuotedString(buf, nota->PublicID);
2154 if (nota->SystemID != NULL) {
2155 xmlBufferWriteChar(buf, " ");
2156 xmlBufferWriteCHAR(buf, nota->SystemID);
2157 }
2158 } else {
2159 xmlBufferWriteChar(buf, " SYSTEM ");
2160 xmlBufferWriteCHAR(buf, nota->SystemID);
2161 }
2162 xmlBufferWriteChar(buf, " >\n");
2163}
2164
2165/**
2166 * xmlDumpNotationTable:
2167 * @buf: the XML buffer output
2168 * @table: A notation table
2169 *
2170 * This will dump the content of the notation table as an XML DTD definition
2171 */
2172void
2173xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2174 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
2175}
2176
2177/************************************************************************
2178 * *
2179 * IDs *
2180 * *
2181 ************************************************************************/
2182/**
2183 * xmlCreateIDTable:
2184 *
2185 * create and initialize an empty id hash table.
2186 *
2187 * Returns the xmlIDTablePtr just created or NULL in case
2188 * of error.
2189 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002190static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002191xmlCreateIDTable(void) {
2192 return(xmlHashCreate(0));
2193}
2194
2195/**
2196 * xmlFreeID:
2197 * @not: A id
2198 *
2199 * Deallocate the memory used by an id definition
2200 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002201static void
Owen Taylor3473f882001-02-23 17:55:21 +00002202xmlFreeID(xmlIDPtr id) {
2203 if (id == NULL) return;
2204 if (id->value != NULL)
2205 xmlFree((xmlChar *) id->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002206 if (id->name != NULL)
2207 xmlFree((xmlChar *) id->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002208 xmlFree(id);
2209}
2210
2211/**
2212 * xmlAddID:
2213 * @ctxt: the validation context
2214 * @doc: pointer to the document
2215 * @value: the value name
2216 * @attr: the attribute holding the ID
2217 *
2218 * Register a new id declaration
2219 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002220 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002221 */
2222xmlIDPtr
2223xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2224 xmlAttrPtr attr) {
2225 xmlIDPtr ret;
2226 xmlIDTablePtr table;
2227
2228 if (doc == NULL) {
2229 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002230 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002231 return(NULL);
2232 }
2233 if (value == NULL) {
2234 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002235 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002236 return(NULL);
2237 }
2238 if (attr == NULL) {
2239 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002240 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002241 return(NULL);
2242 }
2243
2244 /*
2245 * Create the ID table if needed.
2246 */
2247 table = (xmlIDTablePtr) doc->ids;
2248 if (table == NULL)
2249 doc->ids = table = xmlCreateIDTable();
2250 if (table == NULL) {
2251 xmlGenericError(xmlGenericErrorContext,
2252 "xmlAddID: Table creation failed!\n");
2253 return(NULL);
2254 }
2255
2256 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2257 if (ret == NULL) {
2258 xmlGenericError(xmlGenericErrorContext,
2259 "xmlAddID: out of memory\n");
2260 return(NULL);
2261 }
2262
2263 /*
2264 * fill the structure.
2265 */
2266 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002267 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2268 /*
2269 * Operating in streaming mode, attr is gonna disapear
2270 */
2271 ret->name = xmlStrdup(attr->name);
2272 ret->attr = NULL;
2273 } else {
2274 ret->attr = attr;
2275 ret->name = NULL;
2276 }
2277 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002278
2279 if (xmlHashAddEntry(table, value, ret) < 0) {
2280 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002281 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002282 */
Daniel Veillard76575762002-09-05 14:21:15 +00002283 if (ctxt != NULL) {
2284 VECTXT(ctxt, attr->parent);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002285 VERROR(ctxt->userData, "ID %s already defined\n", value);
Daniel Veillard76575762002-09-05 14:21:15 +00002286 }
Owen Taylor3473f882001-02-23 17:55:21 +00002287 xmlFreeID(ret);
2288 return(NULL);
2289 }
2290 return(ret);
2291}
2292
2293/**
2294 * xmlFreeIDTable:
2295 * @table: An id table
2296 *
2297 * Deallocate the memory used by an ID hash table.
2298 */
2299void
2300xmlFreeIDTable(xmlIDTablePtr table) {
2301 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2302}
2303
2304/**
2305 * xmlIsID:
2306 * @doc: the document
2307 * @elem: the element carrying the attribute
2308 * @attr: the attribute
2309 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002310 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002311 * then this is done if DTD loading has been requested. In the case
2312 * of HTML documents parsed with the HTML parser, then ID detection is
2313 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002314 *
2315 * Returns 0 or 1 depending on the lookup result
2316 */
2317int
2318xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2319 if (doc == NULL) return(0);
2320 if (attr == NULL) return(0);
2321 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2322 return(0);
2323 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2324 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2325 (xmlStrEqual(BAD_CAST "name", attr->name)))
2326 return(1);
2327 return(0);
2328 } else {
2329 xmlAttributePtr attrDecl;
2330
2331 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002332 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
2333 /*
2334 * TODO: this sucks ... recomputing this every time is stupid
2335 */
2336 int len = xmlStrlen(elem->name) + xmlStrlen(elem->ns->prefix) + 2;
2337 xmlChar *fullname;
2338
2339 fullname = xmlMalloc(len);
2340 if (fullname == NULL)
2341 return(0);
2342 snprintf((char *) fullname, len, "%s:%s", (char *) elem->ns->prefix,
2343 (char *) elem->name);
2344 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2345 attr->name);
2346 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2347 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2348 attr->name);
2349 xmlFree(fullname);
2350 } else {
2351 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2352 attr->name);
2353 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2354 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2355 attr->name);
2356 }
Owen Taylor3473f882001-02-23 17:55:21 +00002357
2358 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2359 return(1);
2360 }
2361 return(0);
2362}
2363
2364/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002365 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002366 * @doc: the document
2367 * @attr: the attribute
2368 *
2369 * Remove the given attribute from the ID table maintained internally.
2370 *
2371 * Returns -1 if the lookup failed and 0 otherwise
2372 */
2373int
2374xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2375 xmlAttrPtr cur;
2376 xmlIDTablePtr table;
2377 xmlChar *ID;
2378
2379 if (doc == NULL) return(-1);
2380 if (attr == NULL) return(-1);
2381 table = (xmlIDTablePtr) doc->ids;
2382 if (table == NULL)
2383 return(-1);
2384
2385 if (attr == NULL)
2386 return(-1);
2387 ID = xmlNodeListGetString(doc, attr->children, 1);
2388 if (ID == NULL)
2389 return(-1);
2390 cur = xmlHashLookup(table, ID);
2391 if (cur != attr) {
2392 xmlFree(ID);
2393 return(-1);
2394 }
2395 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
2396 xmlFree(ID);
2397 return(0);
2398}
2399
2400/**
2401 * xmlGetID:
2402 * @doc: pointer to the document
2403 * @ID: the ID value
2404 *
2405 * Search the attribute declaring the given ID
2406 *
2407 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2408 */
2409xmlAttrPtr
2410xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2411 xmlIDTablePtr table;
2412 xmlIDPtr id;
2413
2414 if (doc == NULL) {
2415 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
2416 return(NULL);
2417 }
2418
2419 if (ID == NULL) {
2420 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
2421 return(NULL);
2422 }
2423
2424 table = (xmlIDTablePtr) doc->ids;
2425 if (table == NULL)
2426 return(NULL);
2427
2428 id = xmlHashLookup(table, ID);
2429 if (id == NULL)
2430 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002431 if (id->attr == NULL) {
2432 /*
2433 * We are operating on a stream, return a well known reference
2434 * since the attribute node doesn't exist anymore
2435 */
2436 return((xmlAttrPtr) doc);
2437 }
Owen Taylor3473f882001-02-23 17:55:21 +00002438 return(id->attr);
2439}
2440
2441/************************************************************************
2442 * *
2443 * Refs *
2444 * *
2445 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002446typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002447{
2448 xmlListPtr l;
2449 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002450} xmlRemoveMemo;
2451
2452typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2453
2454typedef struct xmlValidateMemo_t
2455{
2456 xmlValidCtxtPtr ctxt;
2457 const xmlChar *name;
2458} xmlValidateMemo;
2459
2460typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002461
2462/**
2463 * xmlCreateRefTable:
2464 *
2465 * create and initialize an empty ref hash table.
2466 *
2467 * Returns the xmlRefTablePtr just created or NULL in case
2468 * of error.
2469 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002470static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002471xmlCreateRefTable(void) {
2472 return(xmlHashCreate(0));
2473}
2474
2475/**
2476 * xmlFreeRef:
2477 * @lk: A list link
2478 *
2479 * Deallocate the memory used by a ref definition
2480 */
2481static void
2482xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002483 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2484 if (ref == NULL) return;
2485 if (ref->value != NULL)
2486 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002487 if (ref->name != NULL)
2488 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002489 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002490}
2491
2492/**
2493 * xmlFreeRefList:
2494 * @list_ref: A list of references.
2495 *
2496 * Deallocate the memory used by a list of references
2497 */
2498static void
2499xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002500 if (list_ref == NULL) return;
2501 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002502}
2503
2504/**
2505 * xmlWalkRemoveRef:
2506 * @data: Contents of current link
2507 * @user: Value supplied by the user
2508 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002509 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002510 */
2511static int
2512xmlWalkRemoveRef(const void *data, const void *user)
2513{
Daniel Veillard37721922001-05-04 15:21:12 +00002514 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2515 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2516 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002517
Daniel Veillard37721922001-05-04 15:21:12 +00002518 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2519 xmlListRemoveFirst(ref_list, (void *)data);
2520 return 0;
2521 }
2522 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002523}
2524
2525/**
2526 * xmlAddRef:
2527 * @ctxt: the validation context
2528 * @doc: pointer to the document
2529 * @value: the value name
2530 * @attr: the attribute holding the Ref
2531 *
2532 * Register a new ref declaration
2533 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002534 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002535 */
2536xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002537xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002538 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002539 xmlRefPtr ret;
2540 xmlRefTablePtr table;
2541 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002542
Daniel Veillard37721922001-05-04 15:21:12 +00002543 if (doc == NULL) {
2544 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002545 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002546 return(NULL);
2547 }
2548 if (value == NULL) {
2549 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002550 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002551 return(NULL);
2552 }
2553 if (attr == NULL) {
2554 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002555 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002556 return(NULL);
2557 }
Owen Taylor3473f882001-02-23 17:55:21 +00002558
Daniel Veillard37721922001-05-04 15:21:12 +00002559 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002560 * Create the Ref table if needed.
2561 */
Daniel Veillard37721922001-05-04 15:21:12 +00002562 table = (xmlRefTablePtr) doc->refs;
2563 if (table == NULL)
2564 doc->refs = table = xmlCreateRefTable();
2565 if (table == NULL) {
2566 xmlGenericError(xmlGenericErrorContext,
2567 "xmlAddRef: Table creation failed!\n");
2568 return(NULL);
2569 }
Owen Taylor3473f882001-02-23 17:55:21 +00002570
Daniel Veillard37721922001-05-04 15:21:12 +00002571 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2572 if (ret == NULL) {
2573 xmlGenericError(xmlGenericErrorContext,
2574 "xmlAddRef: out of memory\n");
2575 return(NULL);
2576 }
Owen Taylor3473f882001-02-23 17:55:21 +00002577
Daniel Veillard37721922001-05-04 15:21:12 +00002578 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002579 * fill the structure.
2580 */
Daniel Veillard37721922001-05-04 15:21:12 +00002581 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002582 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2583 /*
2584 * Operating in streaming mode, attr is gonna disapear
2585 */
2586 ret->name = xmlStrdup(attr->name);
2587 ret->attr = NULL;
2588 } else {
2589 ret->name = NULL;
2590 ret->attr = attr;
2591 }
2592 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002593
Daniel Veillard37721922001-05-04 15:21:12 +00002594 /* To add a reference :-
2595 * References are maintained as a list of references,
2596 * Lookup the entry, if no entry create new nodelist
2597 * Add the owning node to the NodeList
2598 * Return the ref
2599 */
Owen Taylor3473f882001-02-23 17:55:21 +00002600
Daniel Veillard37721922001-05-04 15:21:12 +00002601 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2602 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2603 xmlGenericError(xmlGenericErrorContext,
2604 "xmlAddRef: Reference list creation failed!\n");
2605 return(NULL);
2606 }
2607 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2608 xmlListDelete(ref_list);
2609 xmlGenericError(xmlGenericErrorContext,
2610 "xmlAddRef: Reference list insertion failed!\n");
2611 return(NULL);
2612 }
2613 }
2614 xmlListInsert(ref_list, ret);
2615 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002616}
2617
2618/**
2619 * xmlFreeRefTable:
2620 * @table: An ref table
2621 *
2622 * Deallocate the memory used by an Ref hash table.
2623 */
2624void
2625xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002626 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002627}
2628
2629/**
2630 * xmlIsRef:
2631 * @doc: the document
2632 * @elem: the element carrying the attribute
2633 * @attr: the attribute
2634 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002635 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002636 * then this is simple, otherwise we use an heuristic: name Ref (upper
2637 * or lowercase).
2638 *
2639 * Returns 0 or 1 depending on the lookup result
2640 */
2641int
2642xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002643 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2644 return(0);
2645 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2646 /* TODO @@@ */
2647 return(0);
2648 } else {
2649 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002650
Daniel Veillard37721922001-05-04 15:21:12 +00002651 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2652 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2653 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2654 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002655
Daniel Veillard37721922001-05-04 15:21:12 +00002656 if ((attrDecl != NULL) &&
2657 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2658 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2659 return(1);
2660 }
2661 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002662}
2663
2664/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002665 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00002666 * @doc: the document
2667 * @attr: the attribute
2668 *
2669 * Remove the given attribute from the Ref table maintained internally.
2670 *
2671 * Returns -1 if the lookup failed and 0 otherwise
2672 */
2673int
2674xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002675 xmlListPtr ref_list;
2676 xmlRefTablePtr table;
2677 xmlChar *ID;
2678 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002679
Daniel Veillard37721922001-05-04 15:21:12 +00002680 if (doc == NULL) return(-1);
2681 if (attr == NULL) return(-1);
2682 table = (xmlRefTablePtr) doc->refs;
2683 if (table == NULL)
2684 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002685
Daniel Veillard37721922001-05-04 15:21:12 +00002686 if (attr == NULL)
2687 return(-1);
2688 ID = xmlNodeListGetString(doc, attr->children, 1);
2689 if (ID == NULL)
2690 return(-1);
2691 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002692
Daniel Veillard37721922001-05-04 15:21:12 +00002693 if(ref_list == NULL) {
2694 xmlFree(ID);
2695 return (-1);
2696 }
2697 /* At this point, ref_list refers to a list of references which
2698 * have the same key as the supplied attr. Our list of references
2699 * is ordered by reference address and we don't have that information
2700 * here to use when removing. We'll have to walk the list and
2701 * check for a matching attribute, when we find one stop the walk
2702 * and remove the entry.
2703 * The list is ordered by reference, so that means we don't have the
2704 * key. Passing the list and the reference to the walker means we
2705 * will have enough data to be able to remove the entry.
2706 */
2707 target.l = ref_list;
2708 target.ap = attr;
2709
2710 /* Remove the supplied attr from our list */
2711 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002712
Daniel Veillard37721922001-05-04 15:21:12 +00002713 /*If the list is empty then remove the list entry in the hash */
2714 if (xmlListEmpty(ref_list))
2715 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2716 xmlFreeRefList);
2717 xmlFree(ID);
2718 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002719}
2720
2721/**
2722 * xmlGetRefs:
2723 * @doc: pointer to the document
2724 * @ID: the ID value
2725 *
2726 * Find the set of references for the supplied ID.
2727 *
2728 * Returns NULL if not found, otherwise node set for the ID.
2729 */
2730xmlListPtr
2731xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002732 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002733
Daniel Veillard37721922001-05-04 15:21:12 +00002734 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002735 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002736 return(NULL);
2737 }
Owen Taylor3473f882001-02-23 17:55:21 +00002738
Daniel Veillard37721922001-05-04 15:21:12 +00002739 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002740 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002741 return(NULL);
2742 }
Owen Taylor3473f882001-02-23 17:55:21 +00002743
Daniel Veillard37721922001-05-04 15:21:12 +00002744 table = (xmlRefTablePtr) doc->refs;
2745 if (table == NULL)
2746 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002747
Daniel Veillard37721922001-05-04 15:21:12 +00002748 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002749}
2750
2751/************************************************************************
2752 * *
2753 * Routines for validity checking *
2754 * *
2755 ************************************************************************/
2756
2757/**
2758 * xmlGetDtdElementDesc:
2759 * @dtd: a pointer to the DtD to search
2760 * @name: the element name
2761 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002762 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002763 *
2764 * returns the xmlElementPtr if found or NULL
2765 */
2766
2767xmlElementPtr
2768xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2769 xmlElementTablePtr table;
2770 xmlElementPtr cur;
2771 xmlChar *uqname = NULL, *prefix = NULL;
2772
2773 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002774 if (dtd->elements == NULL)
2775 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002776 table = (xmlElementTablePtr) dtd->elements;
2777
2778 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002779 if (uqname != NULL)
2780 name = uqname;
2781 cur = xmlHashLookup2(table, name, prefix);
2782 if (prefix != NULL) xmlFree(prefix);
2783 if (uqname != NULL) xmlFree(uqname);
2784 return(cur);
2785}
2786/**
2787 * xmlGetDtdElementDesc2:
2788 * @dtd: a pointer to the DtD to search
2789 * @name: the element name
2790 * @create: create an empty description if not found
2791 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002792 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002793 *
2794 * returns the xmlElementPtr if found or NULL
2795 */
2796
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002797static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002798xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2799 xmlElementTablePtr table;
2800 xmlElementPtr cur;
2801 xmlChar *uqname = NULL, *prefix = NULL;
2802
2803 if (dtd == NULL) return(NULL);
2804 if (dtd->elements == NULL) {
2805 if (!create)
2806 return(NULL);
2807 /*
2808 * Create the Element table if needed.
2809 */
2810 table = (xmlElementTablePtr) dtd->elements;
2811 if (table == NULL) {
2812 table = xmlCreateElementTable();
2813 dtd->elements = (void *) table;
2814 }
2815 if (table == NULL) {
2816 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002817 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002818 return(NULL);
2819 }
2820 }
2821 table = (xmlElementTablePtr) dtd->elements;
2822
2823 uqname = xmlSplitQName2(name, &prefix);
2824 if (uqname != NULL)
2825 name = uqname;
2826 cur = xmlHashLookup2(table, name, prefix);
2827 if ((cur == NULL) && (create)) {
2828 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2829 if (cur == NULL) {
2830 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002831 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002832 return(NULL);
2833 }
2834 memset(cur, 0, sizeof(xmlElement));
2835 cur->type = XML_ELEMENT_DECL;
2836
2837 /*
2838 * fill the structure.
2839 */
2840 cur->name = xmlStrdup(name);
2841 cur->prefix = xmlStrdup(prefix);
2842 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2843
2844 xmlHashAddEntry2(table, name, prefix, cur);
2845 }
2846 if (prefix != NULL) xmlFree(prefix);
2847 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002848 return(cur);
2849}
2850
2851/**
2852 * xmlGetDtdQElementDesc:
2853 * @dtd: a pointer to the DtD to search
2854 * @name: the element name
2855 * @prefix: the element namespace prefix
2856 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002857 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002858 *
2859 * returns the xmlElementPtr if found or NULL
2860 */
2861
Daniel Veillard48da9102001-08-07 01:10:10 +00002862xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002863xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2864 const xmlChar *prefix) {
2865 xmlElementTablePtr table;
2866
2867 if (dtd == NULL) return(NULL);
2868 if (dtd->elements == NULL) return(NULL);
2869 table = (xmlElementTablePtr) dtd->elements;
2870
2871 return(xmlHashLookup2(table, name, prefix));
2872}
2873
2874/**
2875 * xmlGetDtdAttrDesc:
2876 * @dtd: a pointer to the DtD to search
2877 * @elem: the element name
2878 * @name: the attribute name
2879 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002880 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002881 * this element.
2882 *
2883 * returns the xmlAttributePtr if found or NULL
2884 */
2885
2886xmlAttributePtr
2887xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2888 xmlAttributeTablePtr table;
2889 xmlAttributePtr cur;
2890 xmlChar *uqname = NULL, *prefix = NULL;
2891
2892 if (dtd == NULL) return(NULL);
2893 if (dtd->attributes == NULL) return(NULL);
2894
2895 table = (xmlAttributeTablePtr) dtd->attributes;
2896 if (table == NULL)
2897 return(NULL);
2898
2899 uqname = xmlSplitQName2(name, &prefix);
2900
2901 if (uqname != NULL) {
2902 cur = xmlHashLookup3(table, uqname, prefix, elem);
2903 if (prefix != NULL) xmlFree(prefix);
2904 if (uqname != NULL) xmlFree(uqname);
2905 } else
2906 cur = xmlHashLookup3(table, name, NULL, elem);
2907 return(cur);
2908}
2909
2910/**
2911 * xmlGetDtdQAttrDesc:
2912 * @dtd: a pointer to the DtD to search
2913 * @elem: the element name
2914 * @name: the attribute name
2915 * @prefix: the attribute namespace prefix
2916 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002917 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002918 * this element.
2919 *
2920 * returns the xmlAttributePtr if found or NULL
2921 */
2922
Daniel Veillard48da9102001-08-07 01:10:10 +00002923xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002924xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2925 const xmlChar *prefix) {
2926 xmlAttributeTablePtr table;
2927
2928 if (dtd == NULL) return(NULL);
2929 if (dtd->attributes == NULL) return(NULL);
2930 table = (xmlAttributeTablePtr) dtd->attributes;
2931
2932 return(xmlHashLookup3(table, name, prefix, elem));
2933}
2934
2935/**
2936 * xmlGetDtdNotationDesc:
2937 * @dtd: a pointer to the DtD to search
2938 * @name: the notation name
2939 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002940 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002941 *
2942 * returns the xmlNotationPtr if found or NULL
2943 */
2944
2945xmlNotationPtr
2946xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2947 xmlNotationTablePtr table;
2948
2949 if (dtd == NULL) return(NULL);
2950 if (dtd->notations == NULL) return(NULL);
2951 table = (xmlNotationTablePtr) dtd->notations;
2952
2953 return(xmlHashLookup(table, name));
2954}
2955
2956/**
2957 * xmlValidateNotationUse:
2958 * @ctxt: the validation context
2959 * @doc: the document
2960 * @notationName: the notation name to check
2961 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002962 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002963 * - [ VC: Notation Declared ]
2964 *
2965 * returns 1 if valid or 0 otherwise
2966 */
2967
2968int
2969xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2970 const xmlChar *notationName) {
2971 xmlNotationPtr notaDecl;
2972 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2973
2974 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2975 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2976 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2977
2978 if (notaDecl == NULL) {
2979 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2980 notationName);
2981 return(0);
2982 }
2983 return(1);
2984}
2985
2986/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002987 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00002988 * @doc: the document
2989 * @name: the element name
2990 *
2991 * Search in the DtDs whether an element accept Mixed content (or ANY)
2992 * basically if it is supposed to accept text childs
2993 *
2994 * returns 0 if no, 1 if yes, and -1 if no element description is available
2995 */
2996
2997int
2998xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2999 xmlElementPtr elemDecl;
3000
3001 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3002
3003 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3004 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3005 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3006 if (elemDecl == NULL) return(-1);
3007 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00003008 case XML_ELEMENT_TYPE_UNDEFINED:
3009 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003010 case XML_ELEMENT_TYPE_ELEMENT:
3011 return(0);
3012 case XML_ELEMENT_TYPE_EMPTY:
3013 /*
3014 * return 1 for EMPTY since we want VC error to pop up
3015 * on <empty> </empty> for example
3016 */
3017 case XML_ELEMENT_TYPE_ANY:
3018 case XML_ELEMENT_TYPE_MIXED:
3019 return(1);
3020 }
3021 return(1);
3022}
3023
3024/**
3025 * xmlValidateNameValue:
3026 * @value: an Name value
3027 *
3028 * Validate that the given value match Name production
3029 *
3030 * returns 1 if valid or 0 otherwise
3031 */
3032
Daniel Veillard9b731d72002-04-14 12:56:08 +00003033int
Owen Taylor3473f882001-02-23 17:55:21 +00003034xmlValidateNameValue(const xmlChar *value) {
3035 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003036 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003037
3038 if (value == NULL) return(0);
3039 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003040 val = xmlStringCurrentChar(NULL, cur, &len);
3041 cur += len;
3042 if (!IS_LETTER(val) && (val != '_') &&
3043 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003044 return(0);
3045 }
3046
Daniel Veillardd8224e02002-01-13 15:43:22 +00003047 val = xmlStringCurrentChar(NULL, cur, &len);
3048 cur += len;
3049 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3050 (val == '.') || (val == '-') ||
3051 (val == '_') || (val == ':') ||
3052 (IS_COMBINING(val)) ||
3053 (IS_EXTENDER(val))) {
3054 val = xmlStringCurrentChar(NULL, cur, &len);
3055 cur += len;
3056 }
Owen Taylor3473f882001-02-23 17:55:21 +00003057
Daniel Veillardd8224e02002-01-13 15:43:22 +00003058 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003059
3060 return(1);
3061}
3062
3063/**
3064 * xmlValidateNamesValue:
3065 * @value: an Names value
3066 *
3067 * Validate that the given value match Names production
3068 *
3069 * returns 1 if valid or 0 otherwise
3070 */
3071
Daniel Veillard9b731d72002-04-14 12:56:08 +00003072int
Owen Taylor3473f882001-02-23 17:55:21 +00003073xmlValidateNamesValue(const xmlChar *value) {
3074 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003075 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003076
3077 if (value == NULL) return(0);
3078 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003079 val = xmlStringCurrentChar(NULL, cur, &len);
3080 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003081
Daniel Veillardd8224e02002-01-13 15:43:22 +00003082 if (!IS_LETTER(val) && (val != '_') &&
3083 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003084 return(0);
3085 }
3086
Daniel Veillardd8224e02002-01-13 15:43:22 +00003087 val = xmlStringCurrentChar(NULL, cur, &len);
3088 cur += len;
3089 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3090 (val == '.') || (val == '-') ||
3091 (val == '_') || (val == ':') ||
3092 (IS_COMBINING(val)) ||
3093 (IS_EXTENDER(val))) {
3094 val = xmlStringCurrentChar(NULL, cur, &len);
3095 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003096 }
3097
Daniel Veillardd8224e02002-01-13 15:43:22 +00003098 while (IS_BLANK(val)) {
3099 while (IS_BLANK(val)) {
3100 val = xmlStringCurrentChar(NULL, cur, &len);
3101 cur += len;
3102 }
3103
3104 if (!IS_LETTER(val) && (val != '_') &&
3105 (val != ':')) {
3106 return(0);
3107 }
3108 val = xmlStringCurrentChar(NULL, cur, &len);
3109 cur += len;
3110
3111 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3112 (val == '.') || (val == '-') ||
3113 (val == '_') || (val == ':') ||
3114 (IS_COMBINING(val)) ||
3115 (IS_EXTENDER(val))) {
3116 val = xmlStringCurrentChar(NULL, cur, &len);
3117 cur += len;
3118 }
3119 }
3120
3121 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003122
3123 return(1);
3124}
3125
3126/**
3127 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003128 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003129 *
3130 * Validate that the given value match Nmtoken production
3131 *
3132 * [ VC: Name Token ]
3133 *
3134 * returns 1 if valid or 0 otherwise
3135 */
3136
Daniel Veillard9b731d72002-04-14 12:56:08 +00003137int
Owen Taylor3473f882001-02-23 17:55:21 +00003138xmlValidateNmtokenValue(const xmlChar *value) {
3139 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003140 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003141
3142 if (value == NULL) return(0);
3143 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003144 val = xmlStringCurrentChar(NULL, cur, &len);
3145 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003146
Daniel Veillardd8224e02002-01-13 15:43:22 +00003147 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3148 (val != '.') && (val != '-') &&
3149 (val != '_') && (val != ':') &&
3150 (!IS_COMBINING(val)) &&
3151 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003152 return(0);
3153
Daniel Veillardd8224e02002-01-13 15:43:22 +00003154 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3155 (val == '.') || (val == '-') ||
3156 (val == '_') || (val == ':') ||
3157 (IS_COMBINING(val)) ||
3158 (IS_EXTENDER(val))) {
3159 val = xmlStringCurrentChar(NULL, cur, &len);
3160 cur += len;
3161 }
Owen Taylor3473f882001-02-23 17:55:21 +00003162
Daniel Veillardd8224e02002-01-13 15:43:22 +00003163 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003164
3165 return(1);
3166}
3167
3168/**
3169 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003170 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003171 *
3172 * Validate that the given value match Nmtokens production
3173 *
3174 * [ VC: Name Token ]
3175 *
3176 * returns 1 if valid or 0 otherwise
3177 */
3178
Daniel Veillard9b731d72002-04-14 12:56:08 +00003179int
Owen Taylor3473f882001-02-23 17:55:21 +00003180xmlValidateNmtokensValue(const xmlChar *value) {
3181 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003182 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003183
3184 if (value == NULL) return(0);
3185 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003186 val = xmlStringCurrentChar(NULL, cur, &len);
3187 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003188
Daniel Veillardd8224e02002-01-13 15:43:22 +00003189 while (IS_BLANK(val)) {
3190 val = xmlStringCurrentChar(NULL, cur, &len);
3191 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003192 }
3193
Daniel Veillardd8224e02002-01-13 15:43:22 +00003194 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3195 (val != '.') && (val != '-') &&
3196 (val != '_') && (val != ':') &&
3197 (!IS_COMBINING(val)) &&
3198 (!IS_EXTENDER(val)))
3199 return(0);
3200
3201 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3202 (val == '.') || (val == '-') ||
3203 (val == '_') || (val == ':') ||
3204 (IS_COMBINING(val)) ||
3205 (IS_EXTENDER(val))) {
3206 val = xmlStringCurrentChar(NULL, cur, &len);
3207 cur += len;
3208 }
3209
3210 while (IS_BLANK(val)) {
3211 while (IS_BLANK(val)) {
3212 val = xmlStringCurrentChar(NULL, cur, &len);
3213 cur += len;
3214 }
3215 if (val == 0) return(1);
3216
3217 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3218 (val != '.') && (val != '-') &&
3219 (val != '_') && (val != ':') &&
3220 (!IS_COMBINING(val)) &&
3221 (!IS_EXTENDER(val)))
3222 return(0);
3223
3224 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3225 (val == '.') || (val == '-') ||
3226 (val == '_') || (val == ':') ||
3227 (IS_COMBINING(val)) ||
3228 (IS_EXTENDER(val))) {
3229 val = xmlStringCurrentChar(NULL, cur, &len);
3230 cur += len;
3231 }
3232 }
3233
3234 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003235
3236 return(1);
3237}
3238
3239/**
3240 * xmlValidateNotationDecl:
3241 * @ctxt: the validation context
3242 * @doc: a document instance
3243 * @nota: a notation definition
3244 *
3245 * Try to validate a single notation definition
3246 * basically it does the following checks as described by the
3247 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003248 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003249 * But this function get called anyway ...
3250 *
3251 * returns 1 if valid or 0 otherwise
3252 */
3253
3254int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003255xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3256 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003257 int ret = 1;
3258
3259 return(ret);
3260}
3261
3262/**
3263 * xmlValidateAttributeValue:
3264 * @type: an attribute type
3265 * @value: an attribute value
3266 *
3267 * Validate that the given attribute value match the proper production
3268 *
3269 * [ VC: ID ]
3270 * Values of type ID must match the Name production....
3271 *
3272 * [ VC: IDREF ]
3273 * Values of type IDREF must match the Name production, and values
3274 * of type IDREFS must match Names ...
3275 *
3276 * [ VC: Entity Name ]
3277 * Values of type ENTITY must match the Name production, values
3278 * of type ENTITIES must match Names ...
3279 *
3280 * [ VC: Name Token ]
3281 * Values of type NMTOKEN must match the Nmtoken production; values
3282 * of type NMTOKENS must match Nmtokens.
3283 *
3284 * returns 1 if valid or 0 otherwise
3285 */
3286
3287int
3288xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3289 switch (type) {
3290 case XML_ATTRIBUTE_ENTITIES:
3291 case XML_ATTRIBUTE_IDREFS:
3292 return(xmlValidateNamesValue(value));
3293 case XML_ATTRIBUTE_ENTITY:
3294 case XML_ATTRIBUTE_IDREF:
3295 case XML_ATTRIBUTE_ID:
3296 case XML_ATTRIBUTE_NOTATION:
3297 return(xmlValidateNameValue(value));
3298 case XML_ATTRIBUTE_NMTOKENS:
3299 case XML_ATTRIBUTE_ENUMERATION:
3300 return(xmlValidateNmtokensValue(value));
3301 case XML_ATTRIBUTE_NMTOKEN:
3302 return(xmlValidateNmtokenValue(value));
3303 case XML_ATTRIBUTE_CDATA:
3304 break;
3305 }
3306 return(1);
3307}
3308
3309/**
3310 * xmlValidateAttributeValue2:
3311 * @ctxt: the validation context
3312 * @doc: the document
3313 * @name: the attribute name (used for error reporting only)
3314 * @type: the attribute type
3315 * @value: the attribute value
3316 *
3317 * Validate that the given attribute value match a given type.
3318 * This typically cannot be done before having finished parsing
3319 * the subsets.
3320 *
3321 * [ VC: IDREF ]
3322 * Values of type IDREF must match one of the declared IDs
3323 * Values of type IDREFS must match a sequence of the declared IDs
3324 * each Name must match the value of an ID attribute on some element
3325 * in the XML document; i.e. IDREF values must match the value of
3326 * some ID attribute
3327 *
3328 * [ VC: Entity Name ]
3329 * Values of type ENTITY must match one declared entity
3330 * Values of type ENTITIES must match a sequence of declared entities
3331 *
3332 * [ VC: Notation Attributes ]
3333 * all notation names in the declaration must be declared.
3334 *
3335 * returns 1 if valid or 0 otherwise
3336 */
3337
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003338static int
Owen Taylor3473f882001-02-23 17:55:21 +00003339xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3340 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3341 int ret = 1;
3342 switch (type) {
3343 case XML_ATTRIBUTE_IDREFS:
3344 case XML_ATTRIBUTE_IDREF:
3345 case XML_ATTRIBUTE_ID:
3346 case XML_ATTRIBUTE_NMTOKENS:
3347 case XML_ATTRIBUTE_ENUMERATION:
3348 case XML_ATTRIBUTE_NMTOKEN:
3349 case XML_ATTRIBUTE_CDATA:
3350 break;
3351 case XML_ATTRIBUTE_ENTITY: {
3352 xmlEntityPtr ent;
3353
3354 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003355 if ((ent == NULL) && (doc->standalone == 1)) {
3356 doc->standalone = 0;
3357 ent = xmlGetDocEntity(doc, value);
3358 if (ent != NULL) {
3359 VERROR(ctxt->userData,
3360"standalone problem: attribute %s reference entity \"%s\" in external subset\n",
3361 name, value);
3362 /* WAIT to get answer from the Core WG on this
3363 ret = 0;
3364 */
3365 }
3366 }
Owen Taylor3473f882001-02-23 17:55:21 +00003367 if (ent == NULL) {
3368 VERROR(ctxt->userData,
3369 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3370 name, value);
3371 ret = 0;
3372 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3373 VERROR(ctxt->userData,
3374 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3375 name, value);
3376 ret = 0;
3377 }
3378 break;
3379 }
3380 case XML_ATTRIBUTE_ENTITIES: {
3381 xmlChar *dup, *nam = NULL, *cur, save;
3382 xmlEntityPtr ent;
3383
3384 dup = xmlStrdup(value);
3385 if (dup == NULL)
3386 return(0);
3387 cur = dup;
3388 while (*cur != 0) {
3389 nam = cur;
3390 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
3391 save = *cur;
3392 *cur = 0;
3393 ent = xmlGetDocEntity(doc, nam);
3394 if (ent == NULL) {
3395 VERROR(ctxt->userData,
3396 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3397 name, nam);
3398 ret = 0;
3399 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3400 VERROR(ctxt->userData,
3401 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3402 name, nam);
3403 ret = 0;
3404 }
3405 if (save == 0)
3406 break;
3407 *cur = save;
3408 while (IS_BLANK(*cur)) cur++;
3409 }
3410 xmlFree(dup);
3411 break;
3412 }
3413 case XML_ATTRIBUTE_NOTATION: {
3414 xmlNotationPtr nota;
3415
3416 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3417 if ((nota == NULL) && (doc->extSubset != NULL))
3418 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3419
3420 if (nota == NULL) {
3421 VERROR(ctxt->userData,
3422 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3423 name, value);
3424 ret = 0;
3425 }
3426 break;
3427 }
3428 }
3429 return(ret);
3430}
3431
3432/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003433 * xmlValidCtxtNormalizeAttributeValue:
3434 * @ctxt: the validation context
3435 * @doc: the document
3436 * @elem: the parent
3437 * @name: the attribute name
3438 * @value: the attribute value
3439 * @ctxt: the validation context or NULL
3440 *
3441 * Does the validation related extra step of the normalization of attribute
3442 * values:
3443 *
3444 * If the declared value is not CDATA, then the XML processor must further
3445 * process the normalized attribute value by discarding any leading and
3446 * trailing space (#x20) characters, and by replacing sequences of space
3447 * (#x20) characters by single space (#x20) character.
3448 *
3449 * Also check VC: Standalone Document Declaration in P32, and update
3450 * ctxt->valid accordingly
3451 *
3452 * returns a new normalized string if normalization is needed, NULL otherwise
3453 * the caller must free the returned value.
3454 */
3455
3456xmlChar *
3457xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3458 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3459 xmlChar *ret, *dst;
3460 const xmlChar *src;
3461 xmlAttributePtr attrDecl = NULL;
3462 int extsubset = 0;
3463
3464 if (doc == NULL) return(NULL);
3465 if (elem == NULL) return(NULL);
3466 if (name == NULL) return(NULL);
3467 if (value == NULL) return(NULL);
3468
3469 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3470 xmlChar qname[500];
3471 snprintf((char *) qname, sizeof(qname), "%s:%s",
3472 elem->ns->prefix, elem->name);
3473 qname[sizeof(qname) - 1] = 0;
3474 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3475 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3476 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3477 if (attrDecl != NULL)
3478 extsubset = 1;
3479 }
3480 }
3481 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3482 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3483 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3484 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3485 if (attrDecl != NULL)
3486 extsubset = 1;
3487 }
3488
3489 if (attrDecl == NULL)
3490 return(NULL);
3491 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3492 return(NULL);
3493
3494 ret = xmlStrdup(value);
3495 if (ret == NULL)
3496 return(NULL);
3497 src = value;
3498 dst = ret;
3499 while (*src == 0x20) src++;
3500 while (*src != 0) {
3501 if (*src == 0x20) {
3502 while (*src == 0x20) src++;
3503 if (*src != 0)
3504 *dst++ = 0x20;
3505 } else {
3506 *dst++ = *src++;
3507 }
3508 }
3509 *dst = 0;
3510 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3511 VERROR(ctxt->userData,
3512"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3513 name, elem->name);
3514 ctxt->valid = 0;
3515 }
3516 return(ret);
3517}
3518
3519/**
Owen Taylor3473f882001-02-23 17:55:21 +00003520 * xmlValidNormalizeAttributeValue:
3521 * @doc: the document
3522 * @elem: the parent
3523 * @name: the attribute name
3524 * @value: the attribute value
3525 *
3526 * Does the validation related extra step of the normalization of attribute
3527 * values:
3528 *
3529 * If the declared value is not CDATA, then the XML processor must further
3530 * process the normalized attribute value by discarding any leading and
3531 * trailing space (#x20) characters, and by replacing sequences of space
3532 * (#x20) characters by single space (#x20) character.
3533 *
3534 * returns a new normalized string if normalization is needed, NULL otherwise
3535 * the caller must free the returned value.
3536 */
3537
3538xmlChar *
3539xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3540 const xmlChar *name, const xmlChar *value) {
3541 xmlChar *ret, *dst;
3542 const xmlChar *src;
3543 xmlAttributePtr attrDecl = NULL;
3544
3545 if (doc == NULL) return(NULL);
3546 if (elem == NULL) return(NULL);
3547 if (name == NULL) return(NULL);
3548 if (value == NULL) return(NULL);
3549
3550 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3551 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003552 snprintf((char *) qname, sizeof(qname), "%s:%s",
3553 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003554 qname[sizeof(qname) - 1] = 0;
3555 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3556 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3557 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3558 }
3559 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3560 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3561 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3562
3563 if (attrDecl == NULL)
3564 return(NULL);
3565 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3566 return(NULL);
3567
3568 ret = xmlStrdup(value);
3569 if (ret == NULL)
3570 return(NULL);
3571 src = value;
3572 dst = ret;
3573 while (*src == 0x20) src++;
3574 while (*src != 0) {
3575 if (*src == 0x20) {
3576 while (*src == 0x20) src++;
3577 if (*src != 0)
3578 *dst++ = 0x20;
3579 } else {
3580 *dst++ = *src++;
3581 }
3582 }
3583 *dst = 0;
3584 return(ret);
3585}
3586
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003587static void
Owen Taylor3473f882001-02-23 17:55:21 +00003588xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003589 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003590 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3591}
3592
3593/**
3594 * xmlValidateAttributeDecl:
3595 * @ctxt: the validation context
3596 * @doc: a document instance
3597 * @attr: an attribute definition
3598 *
3599 * Try to validate a single attribute definition
3600 * basically it does the following checks as described by the
3601 * XML-1.0 recommendation:
3602 * - [ VC: Attribute Default Legal ]
3603 * - [ VC: Enumeration ]
3604 * - [ VC: ID Attribute Default ]
3605 *
3606 * The ID/IDREF uniqueness and matching are done separately
3607 *
3608 * returns 1 if valid or 0 otherwise
3609 */
3610
3611int
3612xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3613 xmlAttributePtr attr) {
3614 int ret = 1;
3615 int val;
3616 CHECK_DTD;
3617 if(attr == NULL) return(1);
3618
3619 /* Attribute Default Legal */
3620 /* Enumeration */
3621 if (attr->defaultValue != NULL) {
3622 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3623 if (val == 0) {
3624 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003625 "Syntax of default value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003626 attr->name, attr->elem);
3627 }
3628 ret &= val;
3629 }
3630
3631 /* ID Attribute Default */
3632 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3633 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3634 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3635 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003636 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003637 attr->name, attr->elem);
3638 ret = 0;
3639 }
3640
3641 /* One ID per Element Type */
3642 if (attr->atype == XML_ATTRIBUTE_ID) {
3643 int nbId;
3644
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003645 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003646 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3647 attr->elem);
3648 if (elem != NULL) {
3649 nbId = xmlScanIDAttributeDecl(NULL, elem);
3650 } else {
3651 xmlAttributeTablePtr table;
3652
3653 /*
3654 * The attribute may be declared in the internal subset and the
3655 * element in the external subset.
3656 */
3657 nbId = 0;
3658 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3659 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3660 xmlValidateAttributeIdCallback, &nbId);
3661 }
3662 if (nbId > 1) {
3663 VERROR(ctxt->userData,
3664 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3665 attr->elem, nbId, attr->name);
3666 } else if (doc->extSubset != NULL) {
3667 int extId = 0;
3668 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3669 if (elem != NULL) {
3670 extId = xmlScanIDAttributeDecl(NULL, elem);
3671 }
3672 if (extId > 1) {
3673 VERROR(ctxt->userData,
3674 "Element %s has %d ID attribute defined in the external subset : %s\n",
3675 attr->elem, extId, attr->name);
3676 } else if (extId + nbId > 1) {
3677 VERROR(ctxt->userData,
3678"Element %s has ID attributes defined in the internal and external subset : %s\n",
3679 attr->elem, attr->name);
3680 }
3681 }
3682 }
3683
3684 /* Validity Constraint: Enumeration */
3685 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3686 xmlEnumerationPtr tree = attr->tree;
3687 while (tree != NULL) {
3688 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3689 tree = tree->next;
3690 }
3691 if (tree == NULL) {
3692 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003693"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003694 attr->defaultValue, attr->name, attr->elem);
3695 ret = 0;
3696 }
3697 }
3698
3699 return(ret);
3700}
3701
3702/**
3703 * xmlValidateElementDecl:
3704 * @ctxt: the validation context
3705 * @doc: a document instance
3706 * @elem: an element definition
3707 *
3708 * Try to validate a single element definition
3709 * basically it does the following checks as described by the
3710 * XML-1.0 recommendation:
3711 * - [ VC: One ID per Element Type ]
3712 * - [ VC: No Duplicate Types ]
3713 * - [ VC: Unique Element Type Declaration ]
3714 *
3715 * returns 1 if valid or 0 otherwise
3716 */
3717
3718int
3719xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3720 xmlElementPtr elem) {
3721 int ret = 1;
3722 xmlElementPtr tst;
3723
3724 CHECK_DTD;
3725
3726 if (elem == NULL) return(1);
3727
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003728#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00003729#ifdef LIBXML_REGEXP_ENABLED
3730 /* Build the regexp associated to the content model */
3731 ret = xmlValidBuildContentModel(ctxt, elem);
3732#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003733#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00003734
Owen Taylor3473f882001-02-23 17:55:21 +00003735 /* No Duplicate Types */
3736 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3737 xmlElementContentPtr cur, next;
3738 const xmlChar *name;
3739
3740 cur = elem->content;
3741 while (cur != NULL) {
3742 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3743 if (cur->c1 == NULL) break;
3744 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3745 name = cur->c1->name;
3746 next = cur->c2;
3747 while (next != NULL) {
3748 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3749 if (xmlStrEqual(next->name, name)) {
3750 VERROR(ctxt->userData,
3751 "Definition of %s has duplicate references of %s\n",
3752 elem->name, name);
3753 ret = 0;
3754 }
3755 break;
3756 }
3757 if (next->c1 == NULL) break;
3758 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3759 if (xmlStrEqual(next->c1->name, name)) {
3760 VERROR(ctxt->userData,
3761 "Definition of %s has duplicate references of %s\n",
3762 elem->name, name);
3763 ret = 0;
3764 }
3765 next = next->c2;
3766 }
3767 }
3768 cur = cur->c2;
3769 }
3770 }
3771
3772 /* VC: Unique Element Type Declaration */
3773 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003774 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003775 ((tst->prefix == elem->prefix) ||
3776 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003777 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003778 VERROR(ctxt->userData, "Redefinition of element %s\n",
3779 elem->name);
3780 ret = 0;
3781 }
3782 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003783 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003784 ((tst->prefix == elem->prefix) ||
3785 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003786 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003787 VERROR(ctxt->userData, "Redefinition of element %s\n",
3788 elem->name);
3789 ret = 0;
3790 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003791 /* One ID per Element Type
3792 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003793 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3794 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003795 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003796 return(ret);
3797}
3798
3799/**
3800 * xmlValidateOneAttribute:
3801 * @ctxt: the validation context
3802 * @doc: a document instance
3803 * @elem: an element instance
3804 * @attr: an attribute instance
3805 * @value: the attribute value (without entities processing)
3806 *
3807 * Try to validate a single attribute for an element
3808 * basically it does the following checks as described by the
3809 * XML-1.0 recommendation:
3810 * - [ VC: Attribute Value Type ]
3811 * - [ VC: Fixed Attribute Default ]
3812 * - [ VC: Entity Name ]
3813 * - [ VC: Name Token ]
3814 * - [ VC: ID ]
3815 * - [ VC: IDREF ]
3816 * - [ VC: Entity Name ]
3817 * - [ VC: Notation Attributes ]
3818 *
3819 * The ID/IDREF uniqueness and matching are done separately
3820 *
3821 * returns 1 if valid or 0 otherwise
3822 */
3823
3824int
3825xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3826 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3827 /* xmlElementPtr elemDecl; */
3828 xmlAttributePtr attrDecl = NULL;
3829 int val;
3830 int ret = 1;
3831
3832 CHECK_DTD;
3833 if ((elem == NULL) || (elem->name == NULL)) return(0);
3834 if ((attr == NULL) || (attr->name == NULL)) return(0);
3835
3836 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3837 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003838 snprintf((char *) qname, sizeof(qname), "%s:%s",
3839 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003840 qname[sizeof(qname) - 1] = 0;
3841 if (attr->ns != NULL) {
3842 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3843 attr->name, attr->ns->prefix);
3844 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3845 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3846 attr->name, attr->ns->prefix);
3847 } else {
3848 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3849 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3850 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3851 qname, attr->name);
3852 }
3853 }
3854 if (attrDecl == NULL) {
3855 if (attr->ns != NULL) {
3856 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3857 attr->name, attr->ns->prefix);
3858 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3859 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3860 attr->name, attr->ns->prefix);
3861 } else {
3862 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3863 elem->name, attr->name);
3864 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3865 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3866 elem->name, attr->name);
3867 }
3868 }
3869
3870
3871 /* Validity Constraint: Attribute Value Type */
3872 if (attrDecl == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003873 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003874 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003875 "No declaration for attribute %s of element %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003876 attr->name, elem->name);
3877 return(0);
3878 }
3879 attr->atype = attrDecl->atype;
3880
3881 val = xmlValidateAttributeValue(attrDecl->atype, value);
3882 if (val == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003883 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003884 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003885 "Syntax of value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003886 attr->name, elem->name);
3887 ret = 0;
3888 }
3889
3890 /* Validity constraint: Fixed Attribute Default */
3891 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3892 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003893 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003894 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003895 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003896 attr->name, elem->name, attrDecl->defaultValue);
3897 ret = 0;
3898 }
3899 }
3900
3901 /* Validity Constraint: ID uniqueness */
3902 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3903 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3904 ret = 0;
3905 }
3906
3907 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3908 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3909 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3910 ret = 0;
3911 }
3912
3913 /* Validity Constraint: Notation Attributes */
3914 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3915 xmlEnumerationPtr tree = attrDecl->tree;
3916 xmlNotationPtr nota;
3917
3918 /* First check that the given NOTATION was declared */
3919 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3920 if (nota == NULL)
3921 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3922
3923 if (nota == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003924 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003925 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003926 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003927 value, attr->name, elem->name);
3928 ret = 0;
3929 }
3930
3931 /* Second, verify that it's among the list */
3932 while (tree != NULL) {
3933 if (xmlStrEqual(tree->name, value)) break;
3934 tree = tree->next;
3935 }
3936 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003937 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003938 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003939"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003940 value, attr->name, elem->name);
3941 ret = 0;
3942 }
3943 }
3944
3945 /* Validity Constraint: Enumeration */
3946 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3947 xmlEnumerationPtr tree = attrDecl->tree;
3948 while (tree != NULL) {
3949 if (xmlStrEqual(tree->name, value)) break;
3950 tree = tree->next;
3951 }
3952 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003953 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003954 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003955 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003956 value, attr->name, elem->name);
3957 ret = 0;
3958 }
3959 }
3960
3961 /* Fixed Attribute Default */
3962 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3963 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003964 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003965 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003966 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003967 attr->name, elem->name, attrDecl->defaultValue);
3968 ret = 0;
3969 }
3970
3971 /* Extra check for the attribute value */
3972 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3973 attrDecl->atype, value);
3974
3975 return(ret);
3976}
3977
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003978/**
3979 * xmlValidateOneNamespace:
3980 * @ctxt: the validation context
3981 * @doc: a document instance
3982 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00003983 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003984 * @ns: an namespace declaration instance
3985 * @value: the attribute value (without entities processing)
3986 *
3987 * Try to validate a single namespace declaration for an element
3988 * basically it does the following checks as described by the
3989 * XML-1.0 recommendation:
3990 * - [ VC: Attribute Value Type ]
3991 * - [ VC: Fixed Attribute Default ]
3992 * - [ VC: Entity Name ]
3993 * - [ VC: Name Token ]
3994 * - [ VC: ID ]
3995 * - [ VC: IDREF ]
3996 * - [ VC: Entity Name ]
3997 * - [ VC: Notation Attributes ]
3998 *
3999 * The ID/IDREF uniqueness and matching are done separately
4000 *
4001 * returns 1 if valid or 0 otherwise
4002 */
4003
4004int
4005xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4006xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4007 /* xmlElementPtr elemDecl; */
4008 xmlAttributePtr attrDecl = NULL;
4009 int val;
4010 int ret = 1;
4011
4012 CHECK_DTD;
4013 if ((elem == NULL) || (elem->name == NULL)) return(0);
4014 if ((ns == NULL) || (ns->href == NULL)) return(0);
4015
4016 if (prefix != NULL) {
4017 xmlChar qname[500];
4018 snprintf((char *) qname, sizeof(qname), "%s:%s",
4019 prefix, elem->name);
4020 qname[sizeof(qname) - 1] = 0;
4021 if (ns->prefix != NULL) {
4022 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
4023 ns->prefix, BAD_CAST "xmlns");
4024 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4025 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
4026 ns->prefix, BAD_CAST "xmlns");
4027 } else {
4028 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname,
4029 BAD_CAST "xmlns");
4030 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4031 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname,
4032 BAD_CAST "xmlns");
4033 }
4034 }
4035 if (attrDecl == NULL) {
4036 if (ns->prefix != NULL) {
4037 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4038 ns->prefix, BAD_CAST "xmlns");
4039 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4040 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4041 ns->prefix, BAD_CAST "xmlns");
4042 } else {
4043 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4044 elem->name, BAD_CAST "xmlns");
4045 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4046 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4047 elem->name, BAD_CAST "xmlns");
4048 }
4049 }
4050
4051
4052 /* Validity Constraint: Attribute Value Type */
4053 if (attrDecl == NULL) {
4054 VECTXT(ctxt, elem);
4055 if (ns->prefix != NULL) {
4056 VERROR(ctxt->userData,
4057 "No declaration for attribute xmlns:%s of element %s\n",
4058 ns->prefix, elem->name);
4059 } else {
4060 VERROR(ctxt->userData,
4061 "No declaration for attribute xmlns of element %s\n",
4062 elem->name);
4063 }
4064 return(0);
4065 }
4066
4067 val = xmlValidateAttributeValue(attrDecl->atype, value);
4068 if (val == 0) {
4069 VECTXT(ctxt, elem);
4070 if (ns->prefix != NULL) {
4071 VERROR(ctxt->userData,
4072 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4073 ns->prefix, elem->name);
4074 } else {
4075 VERROR(ctxt->userData,
4076 "Syntax of value for attribute xmlns of %s is not valid\n",
4077 elem->name);
4078 }
4079 ret = 0;
4080 }
4081
4082 /* Validity constraint: Fixed Attribute Default */
4083 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4084 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4085 VECTXT(ctxt, elem);
4086 if (ns->prefix != NULL) {
4087 VERROR(ctxt->userData,
4088 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4089 ns->prefix, elem->name, attrDecl->defaultValue);
4090 } else {
4091 VERROR(ctxt->userData,
4092 "Value for attribute xmlns of %s is different from default \"%s\"\n",
4093 elem->name, attrDecl->defaultValue);
4094 }
4095 ret = 0;
4096 }
4097 }
4098
4099 /* Validity Constraint: ID uniqueness */
4100 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4101 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4102 ret = 0;
4103 }
4104
4105 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4106 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4107 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4108 ret = 0;
4109 }
4110
4111 /* Validity Constraint: Notation Attributes */
4112 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4113 xmlEnumerationPtr tree = attrDecl->tree;
4114 xmlNotationPtr nota;
4115
4116 /* First check that the given NOTATION was declared */
4117 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4118 if (nota == NULL)
4119 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4120
4121 if (nota == NULL) {
4122 VECTXT(ctxt, elem);
4123 if (ns->prefix != NULL) {
4124 VERROR(ctxt->userData,
4125 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4126 value, ns->prefix, elem->name);
4127 } else {
4128 VERROR(ctxt->userData,
4129 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4130 value, elem->name);
4131 }
4132 ret = 0;
4133 }
4134
4135 /* Second, verify that it's among the list */
4136 while (tree != NULL) {
4137 if (xmlStrEqual(tree->name, value)) break;
4138 tree = tree->next;
4139 }
4140 if (tree == NULL) {
4141 VECTXT(ctxt, elem);
4142 if (ns->prefix != NULL) {
4143 VERROR(ctxt->userData,
4144"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4145 value, ns->prefix, elem->name);
4146 } else {
4147 VERROR(ctxt->userData,
4148"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4149 value, elem->name);
4150 }
4151 ret = 0;
4152 }
4153 }
4154
4155 /* Validity Constraint: Enumeration */
4156 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4157 xmlEnumerationPtr tree = attrDecl->tree;
4158 while (tree != NULL) {
4159 if (xmlStrEqual(tree->name, value)) break;
4160 tree = tree->next;
4161 }
4162 if (tree == NULL) {
4163 VECTXT(ctxt, elem);
4164 if (ns->prefix != NULL) {
4165 VERROR(ctxt->userData,
4166"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4167 value, ns->prefix, elem->name);
4168 } else {
4169 VERROR(ctxt->userData,
4170"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4171 value, elem->name);
4172 }
4173 ret = 0;
4174 }
4175 }
4176
4177 /* Fixed Attribute Default */
4178 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4179 (!xmlStrEqual(attrDecl->defaultValue, value))) {
4180 VECTXT(ctxt, elem);
4181 if (ns->prefix != NULL) {
4182 VERROR(ctxt->userData,
4183 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4184 ns->prefix, elem->name, attrDecl->defaultValue);
4185 } else {
4186 VERROR(ctxt->userData,
4187 "Value for attribute xmlns of %s must be \"%s\"\n",
4188 elem->name, attrDecl->defaultValue);
4189 }
4190 ret = 0;
4191 }
4192
4193 /* Extra check for the attribute value */
4194 if (ns->prefix != NULL) {
4195 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4196 attrDecl->atype, value);
4197 } else {
4198 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4199 attrDecl->atype, value);
4200 }
4201
4202 return(ret);
4203}
4204
Daniel Veillard118aed72002-09-24 14:13:13 +00004205#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004206/**
4207 * xmlValidateSkipIgnorable:
4208 * @ctxt: the validation context
4209 * @child: the child list
4210 *
4211 * Skip ignorable elements w.r.t. the validation process
4212 *
4213 * returns the first element to consider for validation of the content model
4214 */
4215
4216static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004217xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004218 while (child != NULL) {
4219 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004220 /* These things are ignored (skipped) during validation. */
4221 case XML_PI_NODE:
4222 case XML_COMMENT_NODE:
4223 case XML_XINCLUDE_START:
4224 case XML_XINCLUDE_END:
4225 child = child->next;
4226 break;
4227 case XML_TEXT_NODE:
4228 if (xmlIsBlankNode(child))
4229 child = child->next;
4230 else
4231 return(child);
4232 break;
4233 /* keep current node */
4234 default:
4235 return(child);
4236 }
4237 }
4238 return(child);
4239}
4240
4241/**
4242 * xmlValidateElementType:
4243 * @ctxt: the validation context
4244 *
4245 * Try to validate the content model of an element internal function
4246 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004247 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4248 * reference is found and -3 if the validation succeeded but
4249 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004250 */
4251
4252static int
4253xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004254 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004255 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004256
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004257 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004258 if ((NODE == NULL) && (CONT == NULL))
4259 return(1);
4260 if ((NODE == NULL) &&
4261 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4262 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4263 return(1);
4264 }
4265 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004266 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004267 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004268
4269 /*
4270 * We arrive here when more states need to be examined
4271 */
4272cont:
4273
4274 /*
4275 * We just recovered from a rollback generated by a possible
4276 * epsilon transition, go directly to the analysis phase
4277 */
4278 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004279 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004280 DEBUG_VALID_STATE(NODE, CONT)
4281 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004282 goto analyze;
4283 }
4284
4285 DEBUG_VALID_STATE(NODE, CONT)
4286 /*
4287 * we may have to save a backup state here. This is the equivalent
4288 * of handling epsilon transition in NFAs.
4289 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004290 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004291 ((CONT->parent == NULL) ||
4292 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004293 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004294 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004295 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004296 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004297 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4298 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004299 }
4300
4301
4302 /*
4303 * Check first if the content matches
4304 */
4305 switch (CONT->type) {
4306 case XML_ELEMENT_CONTENT_PCDATA:
4307 if (NODE == NULL) {
4308 DEBUG_VALID_MSG("pcdata failed no node");
4309 ret = 0;
4310 break;
4311 }
4312 if (NODE->type == XML_TEXT_NODE) {
4313 DEBUG_VALID_MSG("pcdata found, skip to next");
4314 /*
4315 * go to next element in the content model
4316 * skipping ignorable elems
4317 */
4318 do {
4319 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004320 NODE = xmlValidateSkipIgnorable(NODE);
4321 if ((NODE != NULL) &&
4322 (NODE->type == XML_ENTITY_REF_NODE))
4323 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004324 } while ((NODE != NULL) &&
4325 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004326 (NODE->type != XML_TEXT_NODE) &&
4327 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004328 ret = 1;
4329 break;
4330 } else {
4331 DEBUG_VALID_MSG("pcdata failed");
4332 ret = 0;
4333 break;
4334 }
4335 break;
4336 case XML_ELEMENT_CONTENT_ELEMENT:
4337 if (NODE == NULL) {
4338 DEBUG_VALID_MSG("element failed no node");
4339 ret = 0;
4340 break;
4341 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004342 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4343 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004344 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004345 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4346 ret = (CONT->prefix == NULL);
4347 } else if (CONT->prefix == NULL) {
4348 ret = 0;
4349 } else {
4350 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4351 }
4352 }
4353 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004354 DEBUG_VALID_MSG("element found, skip to next");
4355 /*
4356 * go to next element in the content model
4357 * skipping ignorable elems
4358 */
4359 do {
4360 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004361 NODE = xmlValidateSkipIgnorable(NODE);
4362 if ((NODE != NULL) &&
4363 (NODE->type == XML_ENTITY_REF_NODE))
4364 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004365 } while ((NODE != NULL) &&
4366 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004367 (NODE->type != XML_TEXT_NODE) &&
4368 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004369 } else {
4370 DEBUG_VALID_MSG("element failed");
4371 ret = 0;
4372 break;
4373 }
4374 break;
4375 case XML_ELEMENT_CONTENT_OR:
4376 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004377 * Small optimization.
4378 */
4379 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4380 if ((NODE == NULL) ||
4381 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4382 DEPTH++;
4383 CONT = CONT->c2;
4384 goto cont;
4385 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004386 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4387 ret = (CONT->c1->prefix == NULL);
4388 } else if (CONT->c1->prefix == NULL) {
4389 ret = 0;
4390 } else {
4391 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4392 }
4393 if (ret == 0) {
4394 DEPTH++;
4395 CONT = CONT->c2;
4396 goto cont;
4397 }
Daniel Veillard85349052001-04-20 13:48:21 +00004398 }
4399
4400 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004401 * save the second branch 'or' branch
4402 */
4403 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004404 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4405 OCCURS, ROLLBACK_OR) < 0)
4406 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004407 DEPTH++;
4408 CONT = CONT->c1;
4409 goto cont;
4410 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004411 /*
4412 * Small optimization.
4413 */
4414 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4415 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4416 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4417 if ((NODE == NULL) ||
4418 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4419 DEPTH++;
4420 CONT = CONT->c2;
4421 goto cont;
4422 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004423 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4424 ret = (CONT->c1->prefix == NULL);
4425 } else if (CONT->c1->prefix == NULL) {
4426 ret = 0;
4427 } else {
4428 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4429 }
4430 if (ret == 0) {
4431 DEPTH++;
4432 CONT = CONT->c2;
4433 goto cont;
4434 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004435 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004436 DEPTH++;
4437 CONT = CONT->c1;
4438 goto cont;
4439 }
4440
4441 /*
4442 * At this point handle going up in the tree
4443 */
4444 if (ret == -1) {
4445 DEBUG_VALID_MSG("error found returning");
4446 return(ret);
4447 }
4448analyze:
4449 while (CONT != NULL) {
4450 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004451 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004452 * this level.
4453 */
4454 if (ret == 0) {
4455 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004456 xmlNodePtr cur;
4457
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004458 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004459 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004460 DEBUG_VALID_MSG("Once branch failed, rollback");
4461 if (vstateVPop(ctxt) < 0 ) {
4462 DEBUG_VALID_MSG("exhaustion, failed");
4463 return(0);
4464 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004465 if (cur != ctxt->vstate->node)
4466 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004467 goto cont;
4468 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004469 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004470 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004471 DEBUG_VALID_MSG("Plus branch failed, rollback");
4472 if (vstateVPop(ctxt) < 0 ) {
4473 DEBUG_VALID_MSG("exhaustion, failed");
4474 return(0);
4475 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004476 if (cur != ctxt->vstate->node)
4477 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004478 goto cont;
4479 }
4480 DEBUG_VALID_MSG("Plus branch found");
4481 ret = 1;
4482 break;
4483 case XML_ELEMENT_CONTENT_MULT:
4484#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004485 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004486 DEBUG_VALID_MSG("Mult branch failed");
4487 } else {
4488 DEBUG_VALID_MSG("Mult branch found");
4489 }
4490#endif
4491 ret = 1;
4492 break;
4493 case XML_ELEMENT_CONTENT_OPT:
4494 DEBUG_VALID_MSG("Option branch failed");
4495 ret = 1;
4496 break;
4497 }
4498 } else {
4499 switch (CONT->ocur) {
4500 case XML_ELEMENT_CONTENT_OPT:
4501 DEBUG_VALID_MSG("Option branch succeeded");
4502 ret = 1;
4503 break;
4504 case XML_ELEMENT_CONTENT_ONCE:
4505 DEBUG_VALID_MSG("Once branch succeeded");
4506 ret = 1;
4507 break;
4508 case XML_ELEMENT_CONTENT_PLUS:
4509 if (STATE == ROLLBACK_PARENT) {
4510 DEBUG_VALID_MSG("Plus branch rollback");
4511 ret = 1;
4512 break;
4513 }
4514 if (NODE == NULL) {
4515 DEBUG_VALID_MSG("Plus branch exhausted");
4516 ret = 1;
4517 break;
4518 }
4519 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004520 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004521 goto cont;
4522 case XML_ELEMENT_CONTENT_MULT:
4523 if (STATE == ROLLBACK_PARENT) {
4524 DEBUG_VALID_MSG("Mult branch rollback");
4525 ret = 1;
4526 break;
4527 }
4528 if (NODE == NULL) {
4529 DEBUG_VALID_MSG("Mult branch exhausted");
4530 ret = 1;
4531 break;
4532 }
4533 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004534 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004535 goto cont;
4536 }
4537 }
4538 STATE = 0;
4539
4540 /*
4541 * Then act accordingly at the parent level
4542 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004543 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004544 if (CONT->parent == NULL)
4545 break;
4546
4547 switch (CONT->parent->type) {
4548 case XML_ELEMENT_CONTENT_PCDATA:
4549 DEBUG_VALID_MSG("Error: parent pcdata");
4550 return(-1);
4551 case XML_ELEMENT_CONTENT_ELEMENT:
4552 DEBUG_VALID_MSG("Error: parent element");
4553 return(-1);
4554 case XML_ELEMENT_CONTENT_OR:
4555 if (ret == 1) {
4556 DEBUG_VALID_MSG("Or succeeded");
4557 CONT = CONT->parent;
4558 DEPTH--;
4559 } else {
4560 DEBUG_VALID_MSG("Or failed");
4561 CONT = CONT->parent;
4562 DEPTH--;
4563 }
4564 break;
4565 case XML_ELEMENT_CONTENT_SEQ:
4566 if (ret == 0) {
4567 DEBUG_VALID_MSG("Sequence failed");
4568 CONT = CONT->parent;
4569 DEPTH--;
4570 } else if (CONT == CONT->parent->c1) {
4571 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4572 CONT = CONT->parent->c2;
4573 goto cont;
4574 } else {
4575 DEBUG_VALID_MSG("Sequence succeeded");
4576 CONT = CONT->parent;
4577 DEPTH--;
4578 }
4579 }
4580 }
4581 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004582 xmlNodePtr cur;
4583
4584 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004585 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4586 if (vstateVPop(ctxt) < 0 ) {
4587 DEBUG_VALID_MSG("exhaustion, failed");
4588 return(0);
4589 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004590 if (cur != ctxt->vstate->node)
4591 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004592 goto cont;
4593 }
4594 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004595 xmlNodePtr cur;
4596
4597 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004598 DEBUG_VALID_MSG("Failure, rollback");
4599 if (vstateVPop(ctxt) < 0 ) {
4600 DEBUG_VALID_MSG("exhaustion, failed");
4601 return(0);
4602 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004603 if (cur != ctxt->vstate->node)
4604 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004605 goto cont;
4606 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004607 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004608}
Daniel Veillard23e73572002-09-19 19:56:43 +00004609#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004610
4611/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00004612 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004613 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00004614 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004615 * @content: An element
4616 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
4617 *
4618 * This will dump the list of elements to the buffer
4619 * Intended just for the debug routine
4620 */
4621static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00004622xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004623 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00004624 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004625
4626 if (node == NULL) return;
4627 if (glob) strcat(buf, "(");
4628 cur = node;
4629 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004630 len = strlen(buf);
4631 if (size - len < 50) {
4632 if ((size - len > 4) && (buf[len - 1] != '.'))
4633 strcat(buf, " ...");
4634 return;
4635 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004636 switch (cur->type) {
4637 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004638 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004639 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004640 if ((size - len > 4) && (buf[len - 1] != '.'))
4641 strcat(buf, " ...");
4642 return;
4643 }
4644 strcat(buf, (char *) cur->ns->prefix);
4645 strcat(buf, ":");
4646 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004647 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004648 if ((size - len > 4) && (buf[len - 1] != '.'))
4649 strcat(buf, " ...");
4650 return;
4651 }
4652 strcat(buf, (char *) cur->name);
4653 if (cur->next != NULL)
4654 strcat(buf, " ");
4655 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004656 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004657 if (xmlIsBlankNode(cur))
4658 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004659 case XML_CDATA_SECTION_NODE:
4660 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004661 strcat(buf, "CDATA");
4662 if (cur->next != NULL)
4663 strcat(buf, " ");
4664 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004665 case XML_ATTRIBUTE_NODE:
4666 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004667#ifdef LIBXML_DOCB_ENABLED
4668 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004669#endif
4670 case XML_HTML_DOCUMENT_NODE:
4671 case XML_DOCUMENT_TYPE_NODE:
4672 case XML_DOCUMENT_FRAG_NODE:
4673 case XML_NOTATION_NODE:
4674 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004675 strcat(buf, "???");
4676 if (cur->next != NULL)
4677 strcat(buf, " ");
4678 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004679 case XML_ENTITY_NODE:
4680 case XML_PI_NODE:
4681 case XML_DTD_NODE:
4682 case XML_COMMENT_NODE:
4683 case XML_ELEMENT_DECL:
4684 case XML_ATTRIBUTE_DECL:
4685 case XML_ENTITY_DECL:
4686 case XML_XINCLUDE_START:
4687 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004688 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004689 }
4690 cur = cur->next;
4691 }
4692 if (glob) strcat(buf, ")");
4693}
4694
4695/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004696 * xmlValidateElementContent:
4697 * @ctxt: the validation context
4698 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004699 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004700 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004701 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004702 *
4703 * Try to validate the content model of an element
4704 *
4705 * returns 1 if valid or 0 if not and -1 in case of error
4706 */
4707
4708static int
4709xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004710 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004711 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00004712#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00004713 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00004714#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00004715 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004716 xmlElementContentPtr cont;
4717 const xmlChar *name;
4718
4719 if (elemDecl == NULL)
4720 return(-1);
4721 cont = elemDecl->content;
4722 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004723
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004724#ifdef LIBXML_REGEXP_ENABLED
4725 /* Build the regexp associated to the content model */
4726 if (elemDecl->contModel == NULL)
4727 ret = xmlValidBuildContentModel(ctxt, elemDecl);
4728 if (elemDecl->contModel == NULL) {
4729 ret = -1;
4730 } else {
4731 xmlRegExecCtxtPtr exec;
4732
Daniel Veillardec498e12003-02-05 11:01:50 +00004733 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
4734 return(-1);
4735 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004736 ctxt->nodeMax = 0;
4737 ctxt->nodeNr = 0;
4738 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004739 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
4740 if (exec != NULL) {
4741 cur = child;
4742 while (cur != NULL) {
4743 switch (cur->type) {
4744 case XML_ENTITY_REF_NODE:
4745 /*
4746 * Push the current node to be able to roll back
4747 * and process within the entity
4748 */
4749 if ((cur->children != NULL) &&
4750 (cur->children->children != NULL)) {
4751 nodeVPush(ctxt, cur);
4752 cur = cur->children->children;
4753 continue;
4754 }
4755 break;
4756 case XML_TEXT_NODE:
4757 if (xmlIsBlankNode(cur))
4758 break;
4759 ret = 0;
4760 goto fail;
4761 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00004762 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004763 ret = 0;
4764 goto fail;
4765 case XML_ELEMENT_NODE:
4766 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
4767 xmlChar *QName;
4768 int len;
4769
4770 len = xmlStrlen(cur->name) +
4771 xmlStrlen(cur->ns->prefix) + 2;
4772 QName = xmlMalloc(len);
4773 if (QName == NULL) {
4774 ret = -1;
4775 goto fail;
4776 }
4777 snprintf((char *) QName, len, "%s:%s",
4778 (char *)cur->ns->prefix,
4779 (char *)cur->name);
4780 ret = xmlRegExecPushString(exec, QName, NULL);
4781 xmlFree(QName);
4782 } else {
4783 ret = xmlRegExecPushString(exec, cur->name, NULL);
4784 }
4785 break;
4786 default:
4787 break;
4788 }
4789 /*
4790 * Switch to next element
4791 */
4792 cur = cur->next;
4793 while (cur == NULL) {
4794 cur = nodeVPop(ctxt);
4795 if (cur == NULL)
4796 break;
4797 cur = cur->next;
4798 }
4799 }
4800 ret = xmlRegExecPushString(exec, NULL, NULL);
4801fail:
4802 xmlRegFreeExecCtxt(exec);
4803 }
4804 }
4805#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004806 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004807 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004808 */
4809 ctxt->vstateMax = 8;
4810 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4811 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4812 if (ctxt->vstateTab == NULL) {
4813 xmlGenericError(xmlGenericErrorContext,
4814 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004815 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004816 }
4817 /*
4818 * The first entry in the stack is reserved to the current state
4819 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004820 ctxt->nodeMax = 0;
4821 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004822 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004823 ctxt->vstate = &ctxt->vstateTab[0];
4824 ctxt->vstateNr = 1;
4825 CONT = cont;
4826 NODE = child;
4827 DEPTH = 0;
4828 OCCURS = 0;
4829 STATE = 0;
4830 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004831 if ((ret == -3) && (warn)) {
4832 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004833 "Content model for Element %s is ambiguous\n", name);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004834 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004835 /*
4836 * An entities reference appeared at this level.
4837 * Buid a minimal representation of this node content
4838 * sufficient to run the validation process on it
4839 */
4840 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004841 cur = child;
4842 while (cur != NULL) {
4843 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004844 case XML_ENTITY_REF_NODE:
4845 /*
4846 * Push the current node to be able to roll back
4847 * and process within the entity
4848 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004849 if ((cur->children != NULL) &&
4850 (cur->children->children != NULL)) {
4851 nodeVPush(ctxt, cur);
4852 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004853 continue;
4854 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004855 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004856 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004857 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004858 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004859 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004860 case XML_CDATA_SECTION_NODE:
4861 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004862 case XML_ELEMENT_NODE:
4863 /*
4864 * Allocate a new node and minimally fills in
4865 * what's required
4866 */
4867 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4868 if (tmp == NULL) {
4869 xmlGenericError(xmlGenericErrorContext,
4870 "xmlValidateElementContent : malloc failed\n");
4871 xmlFreeNodeList(repl);
4872 ret = -1;
4873 goto done;
4874 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004875 tmp->type = cur->type;
4876 tmp->name = cur->name;
4877 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004878 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004879 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004880 if (repl == NULL)
4881 repl = last = tmp;
4882 else {
4883 last->next = tmp;
4884 last = tmp;
4885 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004886 if (cur->type == XML_CDATA_SECTION_NODE) {
4887 /*
4888 * E59 spaces in CDATA does not match the
4889 * nonterminal S
4890 */
4891 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4892 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004893 break;
4894 default:
4895 break;
4896 }
4897 /*
4898 * Switch to next element
4899 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004900 cur = cur->next;
4901 while (cur == NULL) {
4902 cur = nodeVPop(ctxt);
4903 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004904 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004905 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004906 }
4907 }
4908
4909 /*
4910 * Relaunch the validation
4911 */
4912 ctxt->vstate = &ctxt->vstateTab[0];
4913 ctxt->vstateNr = 1;
4914 CONT = cont;
4915 NODE = repl;
4916 DEPTH = 0;
4917 OCCURS = 0;
4918 STATE = 0;
4919 ret = xmlValidateElementType(ctxt);
4920 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004921#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004922 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004923 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4924 char expr[5000];
4925 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004926
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004927 expr[0] = 0;
4928 xmlSnprintfElementContent(expr, 5000, cont, 1);
4929 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00004930#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004931 if (repl != NULL)
4932 xmlSnprintfElements(list, 5000, repl, 1);
4933 else
Daniel Veillard01992e02002-10-09 10:20:30 +00004934#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004935 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004936
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004937 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004938 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004939 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004940 "Element %s content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004941 name, expr, list);
4942 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004943 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004944 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004945 "Element content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004946 expr, list);
4947 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004948 } else {
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\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004953 name);
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\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004958 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004959 }
4960 ret = 0;
4961 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004962 if (ret == -3)
4963 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004964
Daniel Veillard23e73572002-09-19 19:56:43 +00004965#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004966done:
4967 /*
4968 * Deallocate the copy if done, and free up the validation stack
4969 */
4970 while (repl != NULL) {
4971 tmp = repl->next;
4972 xmlFree(repl);
4973 repl = tmp;
4974 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004975 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004976 if (ctxt->vstateTab != NULL) {
4977 xmlFree(ctxt->vstateTab);
4978 ctxt->vstateTab = NULL;
4979 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004980#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004981 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004982 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004983 if (ctxt->nodeTab != NULL) {
4984 xmlFree(ctxt->nodeTab);
4985 ctxt->nodeTab = NULL;
4986 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004987 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004988
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004989}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004990
Owen Taylor3473f882001-02-23 17:55:21 +00004991/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004992 * xmlValidateCdataElement:
4993 * @ctxt: the validation context
4994 * @doc: a document instance
4995 * @elem: an element instance
4996 *
4997 * Check that an element follows #CDATA
4998 *
4999 * returns 1 if valid or 0 otherwise
5000 */
5001static int
5002xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5003 xmlNodePtr elem) {
5004 int ret = 1;
5005 xmlNodePtr cur, child;
5006
Daniel Veillardceb09b92002-10-04 11:46:37 +00005007 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005008 return(0);
5009
5010 child = elem->children;
5011
5012 cur = child;
5013 while (cur != NULL) {
5014 switch (cur->type) {
5015 case XML_ENTITY_REF_NODE:
5016 /*
5017 * Push the current node to be able to roll back
5018 * and process within the entity
5019 */
5020 if ((cur->children != NULL) &&
5021 (cur->children->children != NULL)) {
5022 nodeVPush(ctxt, cur);
5023 cur = cur->children->children;
5024 continue;
5025 }
5026 break;
5027 case XML_COMMENT_NODE:
5028 case XML_PI_NODE:
5029 case XML_TEXT_NODE:
5030 case XML_CDATA_SECTION_NODE:
5031 break;
5032 default:
5033 ret = 0;
5034 goto done;
5035 }
5036 /*
5037 * Switch to next element
5038 */
5039 cur = cur->next;
5040 while (cur == NULL) {
5041 cur = nodeVPop(ctxt);
5042 if (cur == NULL)
5043 break;
5044 cur = cur->next;
5045 }
5046 }
5047done:
5048 ctxt->nodeMax = 0;
5049 ctxt->nodeNr = 0;
5050 if (ctxt->nodeTab != NULL) {
5051 xmlFree(ctxt->nodeTab);
5052 ctxt->nodeTab = NULL;
5053 }
5054 return(ret);
5055}
5056
5057/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005058 * xmlValidateCheckMixed:
5059 * @ctxt: the validation context
5060 * @cont: the mixed content model
5061 * @qname: the qualified name as appearing in the serialization
5062 *
5063 * Check if the given node is part of the content model.
5064 *
5065 * Returns 1 if yes, 0 if no, -1 in case of error
5066 */
5067static int
5068xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
5069 xmlElementContentPtr cont, const xmlChar *qname) {
5070 while (cont != NULL) {
5071 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5072 if (xmlStrEqual(cont->name, qname))
5073 return(1);
5074 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5075 (cont->c1 != NULL) &&
5076 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5077 if (xmlStrEqual(cont->c1->name, qname))
5078 return(1);
5079 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5080 (cont->c1 == NULL) ||
5081 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5082 /* Internal error !!! */
5083 xmlGenericError(xmlGenericErrorContext,
5084 "Internal: MIXED struct bad\n");
5085 break;
5086 }
5087 cont = cont->c2;
5088 }
5089 return(0);
5090}
5091
5092/**
5093 * xmlValidGetElemDecl:
5094 * @ctxt: the validation context
5095 * @doc: a document instance
5096 * @elem: an element instance
5097 * @extsubset: pointer, (out) indicate if the declaration was found
5098 * in the external subset.
5099 *
5100 * Finds a declaration associated to an element in the document.
5101 *
5102 * returns the pointer to the declaration or NULL if not found.
5103 */
5104static xmlElementPtr
5105xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5106 xmlNodePtr elem, int *extsubset) {
5107 xmlElementPtr elemDecl = NULL;
5108 const xmlChar *prefix = NULL;
5109
5110 if ((elem == NULL) || (elem->name == NULL)) return(NULL);
5111 if (extsubset != NULL)
5112 *extsubset = 0;
5113
5114 /*
5115 * Fetch the declaration for the qualified name
5116 */
5117 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5118 prefix = elem->ns->prefix;
5119
5120 if (prefix != NULL) {
5121 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5122 elem->name, prefix);
5123 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5124 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5125 elem->name, prefix);
5126 if ((elemDecl != NULL) && (extsubset != NULL))
5127 *extsubset = 1;
5128 }
5129 }
5130
5131 /*
5132 * Fetch the declaration for the non qualified name
5133 * This is "non-strict" validation should be done on the
5134 * full QName but in that case being flexible makes sense.
5135 */
5136 if (elemDecl == NULL) {
5137 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5138 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5139 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5140 if ((elemDecl != NULL) && (extsubset != NULL))
5141 *extsubset = 1;
5142 }
5143 }
5144 if (elemDecl == NULL) {
5145 VECTXT(ctxt, elem);
5146 VERROR(ctxt->userData, "No declaration for element %s\n",
5147 elem->name);
5148 }
5149 return(elemDecl);
5150}
5151
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005152#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005153/**
5154 * xmlValidatePushElement:
5155 * @ctxt: the validation context
5156 * @doc: a document instance
5157 * @elem: an element instance
5158 * @qname: the qualified name as appearing in the serialization
5159 *
5160 * Push a new element start on the validation stack.
5161 *
5162 * returns 1 if no validation problem was found or 0 otherwise
5163 */
5164int
5165xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5166 xmlNodePtr elem, const xmlChar *qname) {
5167 int ret = 1;
5168 xmlElementPtr eDecl;
5169 int extsubset = 0;
5170
5171 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5172 xmlValidStatePtr state = ctxt->vstate;
5173 xmlElementPtr elemDecl;
5174
5175 /*
5176 * Check the new element agaisnt the content model of the new elem.
5177 */
5178 if (state->elemDecl != NULL) {
5179 elemDecl = state->elemDecl;
5180
5181 switch(elemDecl->etype) {
5182 case XML_ELEMENT_TYPE_UNDEFINED:
5183 ret = 0;
5184 break;
5185 case XML_ELEMENT_TYPE_EMPTY:
5186 VECTXT(ctxt, state->node);
5187 VERROR(ctxt->userData,
5188 "Element %s was declared EMPTY this one has content\n",
5189 state->node->name);
5190 ret = 0;
5191 break;
5192 case XML_ELEMENT_TYPE_ANY:
5193 /* I don't think anything is required then */
5194 break;
5195 case XML_ELEMENT_TYPE_MIXED:
5196 /* simple case of declared as #PCDATA */
5197 if ((elemDecl->content != NULL) &&
5198 (elemDecl->content->type ==
5199 XML_ELEMENT_CONTENT_PCDATA)) {
5200 VECTXT(ctxt, state->node);
5201 VERROR(ctxt->userData,
5202 "Element %s was declared #PCDATA but contains non text nodes\n",
5203 state->node->name);
5204 ret = 0;
5205 } else {
5206 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5207 qname);
5208 if (ret != 1) {
5209 VECTXT(ctxt, state->node);
5210 VERROR(ctxt->userData,
5211 "Element %s is not declared in %s list of possible children\n",
5212 qname, state->node->name);
5213 }
5214 }
5215 break;
5216 case XML_ELEMENT_TYPE_ELEMENT:
5217 /*
5218 * TODO:
5219 * VC: Standalone Document Declaration
5220 * - element types with element content, if white space
5221 * occurs directly within any instance of those types.
5222 */
5223 if (state->exec != NULL) {
5224 ret = xmlRegExecPushString(state->exec, qname, NULL);
5225 if (ret < 0) {
5226 VECTXT(ctxt, state->node);
5227 VERROR(ctxt->userData,
5228 "Element %s content does not follow the DTD\nMisplaced %s\n",
5229 state->node->name, qname);
5230 ret = 0;
5231 } else {
5232 ret = 1;
5233 }
5234 }
5235 break;
5236 }
5237 }
5238 }
5239 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5240 vstateVPush(ctxt, eDecl, elem);
5241 return(ret);
5242}
5243
5244/**
5245 * xmlValidatePushCData:
5246 * @ctxt: the validation context
5247 * @data: some character data read
5248 * @len: the lenght of the data
5249 *
5250 * check the CData parsed for validation in the current stack
5251 *
5252 * returns 1 if no validation problem was found or 0 otherwise
5253 */
5254int
5255xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5256 int ret = 1;
5257
5258 if (len <= 0)
5259 return(ret);
5260 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5261 xmlValidStatePtr state = ctxt->vstate;
5262 xmlElementPtr elemDecl;
5263
5264 /*
5265 * Check the new element agaisnt the content model of the new elem.
5266 */
5267 if (state->elemDecl != NULL) {
5268 elemDecl = state->elemDecl;
5269
5270 switch(elemDecl->etype) {
5271 case XML_ELEMENT_TYPE_UNDEFINED:
5272 ret = 0;
5273 break;
5274 case XML_ELEMENT_TYPE_EMPTY:
5275 VECTXT(ctxt, state->node);
5276 VERROR(ctxt->userData,
5277 "Element %s was declared EMPTY this one has content\n",
5278 state->node->name);
5279 ret = 0;
5280 break;
5281 case XML_ELEMENT_TYPE_ANY:
5282 break;
5283 case XML_ELEMENT_TYPE_MIXED:
5284 break;
5285 case XML_ELEMENT_TYPE_ELEMENT:
5286 if (len > 0) {
5287 int i;
5288
5289 for (i = 0;i < len;i++) {
5290 if (!IS_BLANK(data[i])) {
5291 VECTXT(ctxt, state->node);
5292 VERROR(ctxt->userData,
5293 "Element %s content does not follow the DTD\nText not allowed\n",
5294 state->node->name);
5295 ret = 0;
5296 goto done;
5297 }
5298 }
5299 /*
5300 * TODO:
5301 * VC: Standalone Document Declaration
5302 * element types with element content, if white space
5303 * occurs directly within any instance of those types.
5304 */
5305 }
5306 break;
5307 }
5308 }
5309 }
5310done:
5311 return(ret);
5312}
5313
5314/**
5315 * xmlValidatePopElement:
5316 * @ctxt: the validation context
5317 * @doc: a document instance
5318 * @elem: an element instance
5319 * @qname: the qualified name as appearing in the serialization
5320 *
5321 * Pop the element end from the validation stack.
5322 *
5323 * returns 1 if no validation problem was found or 0 otherwise
5324 */
5325int
5326xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
5327 xmlNodePtr elem, const xmlChar *qname ATTRIBUTE_UNUSED) {
5328 int ret = 1;
5329
5330 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5331 xmlValidStatePtr state = ctxt->vstate;
5332 xmlElementPtr elemDecl;
5333
5334 /*
5335 * Check the new element agaisnt the content model of the new elem.
5336 */
5337 if (state->elemDecl != NULL) {
5338 elemDecl = state->elemDecl;
5339
5340 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5341 if (state->exec != NULL) {
5342 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5343 if (ret == 0) {
5344 VECTXT(ctxt, state->node);
5345 VERROR(ctxt->userData,
5346 "Element %s content does not follow the DTD\nExpecting more child\n",
5347 state->node->name);
5348 } else {
5349 /*
5350 * previous validation errors should not generate
5351 * a new one here
5352 */
5353 ret = 1;
5354 }
5355 }
5356 }
5357 }
5358 vstateVPop(ctxt);
5359 }
5360 return(ret);
5361}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005362#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005363
5364/**
Owen Taylor3473f882001-02-23 17:55:21 +00005365 * xmlValidateOneElement:
5366 * @ctxt: the validation context
5367 * @doc: a document instance
5368 * @elem: an element instance
5369 *
5370 * Try to validate a single element and it's attributes,
5371 * basically it does the following checks as described by the
5372 * XML-1.0 recommendation:
5373 * - [ VC: Element Valid ]
5374 * - [ VC: Required Attribute ]
5375 * Then call xmlValidateOneAttribute() for each attribute present.
5376 *
5377 * The ID/IDREF checkings are done separately
5378 *
5379 * returns 1 if valid or 0 otherwise
5380 */
5381
5382int
5383xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5384 xmlNodePtr elem) {
5385 xmlElementPtr elemDecl = NULL;
5386 xmlElementContentPtr cont;
5387 xmlAttributePtr attr;
5388 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005389 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005390 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005391 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005392
5393 CHECK_DTD;
5394
5395 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005396 switch (elem->type) {
5397 case XML_ATTRIBUTE_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005398 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005399 VERROR(ctxt->userData,
5400 "Attribute element not expected here\n");
5401 return(0);
5402 case XML_TEXT_NODE:
5403 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005404 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005405 VERROR(ctxt->userData, "Text element has childs !\n");
5406 return(0);
5407 }
5408 if (elem->properties != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005409 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005410 VERROR(ctxt->userData, "Text element has attributes !\n");
5411 return(0);
5412 }
5413 if (elem->ns != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005414 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005415 VERROR(ctxt->userData, "Text element has namespace !\n");
5416 return(0);
5417 }
5418 if (elem->nsDef != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005419 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005420 VERROR(ctxt->userData,
5421 "Text element carries namespace definitions !\n");
5422 return(0);
5423 }
5424 if (elem->content == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005425 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005426 VERROR(ctxt->userData,
5427 "Text element has no content !\n");
5428 return(0);
5429 }
5430 return(1);
5431 case XML_XINCLUDE_START:
5432 case XML_XINCLUDE_END:
5433 return(1);
5434 case XML_CDATA_SECTION_NODE:
5435 case XML_ENTITY_REF_NODE:
5436 case XML_PI_NODE:
5437 case XML_COMMENT_NODE:
5438 return(1);
5439 case XML_ENTITY_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005440 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005441 VERROR(ctxt->userData,
5442 "Entity element not expected here\n");
5443 return(0);
5444 case XML_NOTATION_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005445 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005446 VERROR(ctxt->userData,
5447 "Notation element not expected here\n");
5448 return(0);
5449 case XML_DOCUMENT_NODE:
5450 case XML_DOCUMENT_TYPE_NODE:
5451 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005452 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005453 VERROR(ctxt->userData,
5454 "Document element not expected here\n");
5455 return(0);
5456 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005457 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005458 VERROR(ctxt->userData,
5459 "\n");
5460 return(0);
5461 case XML_ELEMENT_NODE:
5462 break;
5463 default:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005464 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005465 VERROR(ctxt->userData,
5466 "unknown element type %d\n", elem->type);
5467 return(0);
5468 }
Owen Taylor3473f882001-02-23 17:55:21 +00005469
5470 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005471 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005472 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005473 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5474 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005475 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005476
Daniel Veillardea7751d2002-12-20 00:16:24 +00005477 /*
5478 * If vstateNr is not zero that means continuous validation is
5479 * activated, do not try to check the content model at that level.
5480 */
5481 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005482 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005483 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005484 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005485 VECTXT(ctxt, elem);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005486 VERROR(ctxt->userData, "No declaration for element %s\n",
5487 elem->name);
5488 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005489 case XML_ELEMENT_TYPE_EMPTY:
5490 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005491 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005492 VERROR(ctxt->userData,
5493 "Element %s was declared EMPTY this one has content\n",
5494 elem->name);
5495 ret = 0;
5496 }
5497 break;
5498 case XML_ELEMENT_TYPE_ANY:
5499 /* I don't think anything is required then */
5500 break;
5501 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005502
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005503 /* simple case of declared as #PCDATA */
5504 if ((elemDecl->content != NULL) &&
5505 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5506 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5507 if (!ret) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005508 VECTXT(ctxt, elem);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005509 VERROR(ctxt->userData,
5510 "Element %s was declared #PCDATA but contains non text nodes\n",
5511 elem->name);
5512 }
5513 break;
5514 }
Owen Taylor3473f882001-02-23 17:55:21 +00005515 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005516 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005517 while (child != NULL) {
5518 if (child->type == XML_ELEMENT_NODE) {
5519 name = child->name;
5520 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
5521 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00005522 snprintf((char *) qname, sizeof(qname), "%s:%s",
5523 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005524 qname[sizeof(qname) - 1] = 0;
5525 cont = elemDecl->content;
5526 while (cont != NULL) {
5527 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5528 if (xmlStrEqual(cont->name, qname)) break;
5529 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5530 (cont->c1 != NULL) &&
5531 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5532 if (xmlStrEqual(cont->c1->name, qname)) break;
5533 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5534 (cont->c1 == NULL) ||
5535 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5536 /* Internal error !!! */
5537 xmlGenericError(xmlGenericErrorContext,
5538 "Internal: MIXED struct bad\n");
5539 break;
5540 }
5541 cont = cont->c2;
5542 }
5543 if (cont != NULL)
5544 goto child_ok;
5545 }
5546 cont = elemDecl->content;
5547 while (cont != NULL) {
5548 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5549 if (xmlStrEqual(cont->name, name)) break;
5550 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5551 (cont->c1 != NULL) &&
5552 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5553 if (xmlStrEqual(cont->c1->name, name)) break;
5554 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5555 (cont->c1 == NULL) ||
5556 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
5557 /* Internal error !!! */
5558 xmlGenericError(xmlGenericErrorContext,
5559 "Internal: MIXED struct bad\n");
5560 break;
5561 }
5562 cont = cont->c2;
5563 }
5564 if (cont == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005565 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005566 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005567 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005568 name, elem->name);
5569 ret = 0;
5570 }
5571 }
5572child_ok:
5573 child = child->next;
5574 }
5575 break;
5576 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005577 if ((doc->standalone == 1) && (extsubset == 1)) {
5578 /*
5579 * VC: Standalone Document Declaration
5580 * - element types with element content, if white space
5581 * occurs directly within any instance of those types.
5582 */
5583 child = elem->children;
5584 while (child != NULL) {
5585 if (child->type == XML_TEXT_NODE) {
5586 const xmlChar *content = child->content;
5587
5588 while (IS_BLANK(*content))
5589 content++;
5590 if (*content == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005591 VECTXT(ctxt, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005592 VERROR(ctxt->userData,
5593"standalone: %s declared in the external subset contains white spaces nodes\n",
5594 elem->name);
5595 ret = 0;
5596 break;
5597 }
5598 }
5599 child =child->next;
5600 }
5601 }
Owen Taylor3473f882001-02-23 17:55:21 +00005602 child = elem->children;
5603 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005604 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005605 if (tmp <= 0)
5606 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005607 break;
5608 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005609 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00005610
5611 /* [ VC: Required Attribute ] */
5612 attr = elemDecl->attributes;
5613 while (attr != NULL) {
5614 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005615 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00005616
Daniel Veillarde4301c82002-02-13 13:32:35 +00005617 if ((attr->prefix == NULL) &&
5618 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5619 xmlNsPtr ns;
5620
5621 ns = elem->nsDef;
5622 while (ns != NULL) {
5623 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005624 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005625 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00005626 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005627 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5628 xmlNsPtr ns;
5629
5630 ns = elem->nsDef;
5631 while (ns != NULL) {
5632 if (xmlStrEqual(attr->name, ns->prefix))
5633 goto found;
5634 ns = ns->next;
5635 }
5636 } else {
5637 xmlAttrPtr attrib;
5638
5639 attrib = elem->properties;
5640 while (attrib != NULL) {
5641 if (xmlStrEqual(attrib->name, attr->name)) {
5642 if (attr->prefix != NULL) {
5643 xmlNsPtr nameSpace = attrib->ns;
5644
5645 if (nameSpace == NULL)
5646 nameSpace = elem->ns;
5647 /*
5648 * qualified names handling is problematic, having a
5649 * different prefix should be possible but DTDs don't
5650 * allow to define the URI instead of the prefix :-(
5651 */
5652 if (nameSpace == NULL) {
5653 if (qualified < 0)
5654 qualified = 0;
5655 } else if (!xmlStrEqual(nameSpace->prefix,
5656 attr->prefix)) {
5657 if (qualified < 1)
5658 qualified = 1;
5659 } else
5660 goto found;
5661 } else {
5662 /*
5663 * We should allow applications to define namespaces
5664 * for their application even if the DTD doesn't
5665 * carry one, otherwise, basically we would always
5666 * break.
5667 */
5668 goto found;
5669 }
5670 }
5671 attrib = attrib->next;
5672 }
Owen Taylor3473f882001-02-23 17:55:21 +00005673 }
5674 if (qualified == -1) {
5675 if (attr->prefix == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005676 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005677 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005678 "Element %s does not carry attribute %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005679 elem->name, attr->name);
5680 ret = 0;
5681 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005682 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005683 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005684 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005685 elem->name, attr->prefix,attr->name);
5686 ret = 0;
5687 }
5688 } else if (qualified == 0) {
5689 VWARNING(ctxt->userData,
5690 "Element %s required attribute %s:%s has no prefix\n",
5691 elem->name, attr->prefix,attr->name);
5692 } else if (qualified == 1) {
5693 VWARNING(ctxt->userData,
5694 "Element %s required attribute %s:%s has different prefix\n",
5695 elem->name, attr->prefix,attr->name);
5696 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005697 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
5698 /*
5699 * Special tests checking #FIXED namespace declarations
5700 * have the right value since this is not done as an
5701 * attribute checking
5702 */
5703 if ((attr->prefix == NULL) &&
5704 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5705 xmlNsPtr ns;
5706
5707 ns = elem->nsDef;
5708 while (ns != NULL) {
5709 if (ns->prefix == NULL) {
5710 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005711 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005712 VERROR(ctxt->userData,
5713 "Element %s namespace name for default namespace does not match the DTD\n",
5714 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00005715 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005716 }
5717 goto found;
5718 }
5719 ns = ns->next;
5720 }
5721 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5722 xmlNsPtr ns;
5723
5724 ns = elem->nsDef;
5725 while (ns != NULL) {
5726 if (xmlStrEqual(attr->name, ns->prefix)) {
5727 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005728 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005729 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005730 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillarde4301c82002-02-13 13:32:35 +00005731 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00005732 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005733 }
5734 goto found;
5735 }
5736 ns = ns->next;
5737 }
5738 }
Owen Taylor3473f882001-02-23 17:55:21 +00005739 }
5740found:
5741 attr = attr->nexth;
5742 }
5743 return(ret);
5744}
5745
5746/**
5747 * xmlValidateRoot:
5748 * @ctxt: the validation context
5749 * @doc: a document instance
5750 *
5751 * Try to validate a the root element
5752 * basically it does the following check as described by the
5753 * XML-1.0 recommendation:
5754 * - [ VC: Root Element Type ]
5755 * it doesn't try to recurse or apply other check to the element
5756 *
5757 * returns 1 if valid or 0 otherwise
5758 */
5759
5760int
5761xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5762 xmlNodePtr root;
5763 if (doc == NULL) return(0);
5764
5765 root = xmlDocGetRootElement(doc);
5766 if ((root == NULL) || (root->name == NULL)) {
5767 VERROR(ctxt->userData, "Not valid: no root element\n");
5768 return(0);
5769 }
5770
5771 /*
5772 * When doing post validation against a separate DTD, those may
5773 * no internal subset has been generated
5774 */
5775 if ((doc->intSubset != NULL) &&
5776 (doc->intSubset->name != NULL)) {
5777 /*
5778 * Check first the document root against the NQName
5779 */
5780 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
5781 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
5782 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00005783 snprintf((char *) qname, sizeof(qname), "%s:%s",
5784 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005785 qname[sizeof(qname) - 1] = 0;
5786 if (xmlStrEqual(doc->intSubset->name, qname))
5787 goto name_ok;
5788 }
5789 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
5790 (xmlStrEqual(root->name, BAD_CAST "html")))
5791 goto name_ok;
Daniel Veillard76575762002-09-05 14:21:15 +00005792 VECTXT(ctxt, root);
Owen Taylor3473f882001-02-23 17:55:21 +00005793 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005794 "Not valid: root and DTD name do not match '%s' and '%s'\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005795 root->name, doc->intSubset->name);
5796 return(0);
5797
5798 }
5799 }
5800name_ok:
5801 return(1);
5802}
5803
5804
5805/**
5806 * xmlValidateElement:
5807 * @ctxt: the validation context
5808 * @doc: a document instance
5809 * @elem: an element instance
5810 *
5811 * Try to validate the subtree under an element
5812 *
5813 * returns 1 if valid or 0 otherwise
5814 */
5815
5816int
5817xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
5818 xmlNodePtr child;
5819 xmlAttrPtr attr;
5820 xmlChar *value;
5821 int ret = 1;
5822
5823 if (elem == NULL) return(0);
5824
5825 /*
5826 * XInclude elements were added after parsing in the infoset,
5827 * they don't really mean anything validation wise.
5828 */
5829 if ((elem->type == XML_XINCLUDE_START) ||
5830 (elem->type == XML_XINCLUDE_END))
5831 return(1);
5832
5833 CHECK_DTD;
5834
Daniel Veillard10ea86c2001-06-20 13:55:33 +00005835 /*
5836 * Entities references have to be handled separately
5837 */
5838 if (elem->type == XML_ENTITY_REF_NODE) {
5839 return(1);
5840 }
5841
Owen Taylor3473f882001-02-23 17:55:21 +00005842 ret &= xmlValidateOneElement(ctxt, doc, elem);
5843 attr = elem->properties;
5844 while(attr != NULL) {
5845 value = xmlNodeListGetString(doc, attr->children, 0);
5846 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
5847 if (value != NULL)
5848 xmlFree(value);
5849 attr= attr->next;
5850 }
5851 child = elem->children;
5852 while (child != NULL) {
5853 ret &= xmlValidateElement(ctxt, doc, child);
5854 child = child->next;
5855 }
5856
5857 return(ret);
5858}
5859
Daniel Veillard8730c562001-02-26 10:49:57 +00005860/**
5861 * xmlValidateRef:
5862 * @ref: A reference to be validated
5863 * @ctxt: Validation context
5864 * @name: Name of ID we are searching for
5865 *
5866 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005867static void
Daniel Veillard8730c562001-02-26 10:49:57 +00005868xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00005869 const xmlChar *name) {
5870 xmlAttrPtr id;
5871 xmlAttrPtr attr;
5872
5873 if (ref == NULL)
5874 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005875 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005876 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005877 attr = ref->attr;
5878 if (attr == NULL) {
5879 xmlChar *dup, *str = NULL, *cur, save;
5880
5881 dup = xmlStrdup(name);
5882 if (dup == NULL) {
5883 ctxt->valid = 0;
5884 return;
5885 }
5886 cur = dup;
5887 while (*cur != 0) {
5888 str = cur;
5889 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5890 save = *cur;
5891 *cur = 0;
5892 id = xmlGetID(ctxt->doc, str);
5893 if (id == NULL) {
5894 VERROR(ctxt->userData,
5895 "attribute %s line %d references an unknown ID \"%s\"\n",
5896 ref->name, ref->lineno, str);
5897 ctxt->valid = 0;
5898 }
5899 if (save == 0)
5900 break;
5901 *cur = save;
5902 while (IS_BLANK(*cur)) cur++;
5903 }
5904 xmlFree(dup);
5905 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00005906 id = xmlGetID(ctxt->doc, name);
5907 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005908 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005909 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005910 "IDREF attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005911 attr->name, name);
5912 ctxt->valid = 0;
5913 }
5914 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
5915 xmlChar *dup, *str = NULL, *cur, save;
5916
5917 dup = xmlStrdup(name);
5918 if (dup == NULL) {
5919 ctxt->valid = 0;
5920 return;
5921 }
5922 cur = dup;
5923 while (*cur != 0) {
5924 str = cur;
5925 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5926 save = *cur;
5927 *cur = 0;
5928 id = xmlGetID(ctxt->doc, str);
5929 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005930 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005931 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005932 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005933 attr->name, str);
5934 ctxt->valid = 0;
5935 }
5936 if (save == 0)
5937 break;
5938 *cur = save;
5939 while (IS_BLANK(*cur)) cur++;
5940 }
5941 xmlFree(dup);
5942 }
5943}
5944
5945/**
Daniel Veillard8730c562001-02-26 10:49:57 +00005946 * xmlWalkValidateList:
5947 * @data: Contents of current link
5948 * @user: Value supplied by the user
5949 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005950 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00005951 */
5952static int
5953xmlWalkValidateList(const void *data, const void *user)
5954{
5955 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
5956 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
5957 return 1;
5958}
5959
5960/**
5961 * xmlValidateCheckRefCallback:
5962 * @ref_list: List of references
5963 * @ctxt: Validation context
5964 * @name: Name of ID we are searching for
5965 *
5966 */
5967static void
5968xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
5969 const xmlChar *name) {
5970 xmlValidateMemo memo;
5971
5972 if (ref_list == NULL)
5973 return;
5974 memo.ctxt = ctxt;
5975 memo.name = name;
5976
5977 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
5978
5979}
5980
5981/**
Owen Taylor3473f882001-02-23 17:55:21 +00005982 * xmlValidateDocumentFinal:
5983 * @ctxt: the validation context
5984 * @doc: a document instance
5985 *
5986 * Does the final step for the document validation once all the
5987 * incremental validation steps have been completed
5988 *
5989 * basically it does the following checks described by the XML Rec
5990 *
5991 *
5992 * returns 1 if valid or 0 otherwise
5993 */
5994
5995int
5996xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5997 xmlRefTablePtr table;
5998
5999 if (doc == NULL) {
6000 xmlGenericError(xmlGenericErrorContext,
6001 "xmlValidateDocumentFinal: doc == NULL\n");
6002 return(0);
6003 }
6004
6005 /*
6006 * Check all the NOTATION/NOTATIONS attributes
6007 */
6008 /*
6009 * Check all the ENTITY/ENTITIES attributes definition for validity
6010 */
6011 /*
6012 * Check all the IDREF/IDREFS attributes definition for validity
6013 */
6014 table = (xmlRefTablePtr) doc->refs;
6015 ctxt->doc = doc;
6016 ctxt->valid = 1;
6017 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6018 return(ctxt->valid);
6019}
6020
6021/**
6022 * xmlValidateDtd:
6023 * @ctxt: the validation context
6024 * @doc: a document instance
6025 * @dtd: a dtd instance
6026 *
6027 * Try to validate the document against the dtd instance
6028 *
6029 * basically it does check all the definitions in the DtD.
6030 *
6031 * returns 1 if valid or 0 otherwise
6032 */
6033
6034int
6035xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6036 int ret;
6037 xmlDtdPtr oldExt;
6038 xmlNodePtr root;
6039
6040 if (dtd == NULL) return(0);
6041 if (doc == NULL) return(0);
6042 oldExt = doc->extSubset;
6043 doc->extSubset = dtd;
6044 ret = xmlValidateRoot(ctxt, doc);
6045 if (ret == 0) {
6046 doc->extSubset = oldExt;
6047 return(ret);
6048 }
6049 if (doc->ids != NULL) {
6050 xmlFreeIDTable(doc->ids);
6051 doc->ids = NULL;
6052 }
6053 if (doc->refs != NULL) {
6054 xmlFreeRefTable(doc->refs);
6055 doc->refs = NULL;
6056 }
6057 root = xmlDocGetRootElement(doc);
6058 ret = xmlValidateElement(ctxt, doc, root);
6059 ret &= xmlValidateDocumentFinal(ctxt, doc);
6060 doc->extSubset = oldExt;
6061 return(ret);
6062}
6063
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006064static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006065xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6066 const xmlChar *name ATTRIBUTE_UNUSED) {
6067 if (cur == NULL)
6068 return;
6069 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6070 xmlChar *notation = cur->content;
6071
Daniel Veillard878eab02002-02-19 13:46:09 +00006072 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006073 int ret;
6074
6075 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6076 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006077 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006078 }
6079 }
6080 }
6081}
6082
6083static void
Owen Taylor3473f882001-02-23 17:55:21 +00006084xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006085 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006086 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006087 xmlDocPtr doc;
6088 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006089
Owen Taylor3473f882001-02-23 17:55:21 +00006090 if (cur == NULL)
6091 return;
6092 switch (cur->atype) {
6093 case XML_ATTRIBUTE_CDATA:
6094 case XML_ATTRIBUTE_ID:
6095 case XML_ATTRIBUTE_IDREF :
6096 case XML_ATTRIBUTE_IDREFS:
6097 case XML_ATTRIBUTE_NMTOKEN:
6098 case XML_ATTRIBUTE_NMTOKENS:
6099 case XML_ATTRIBUTE_ENUMERATION:
6100 break;
6101 case XML_ATTRIBUTE_ENTITY:
6102 case XML_ATTRIBUTE_ENTITIES:
6103 case XML_ATTRIBUTE_NOTATION:
6104 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006105
6106 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6107 cur->atype, cur->defaultValue);
6108 if ((ret == 0) && (ctxt->valid == 1))
6109 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006110 }
6111 if (cur->tree != NULL) {
6112 xmlEnumerationPtr tree = cur->tree;
6113 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006114 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006115 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006116 if ((ret == 0) && (ctxt->valid == 1))
6117 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006118 tree = tree->next;
6119 }
6120 }
6121 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006122 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6123 doc = cur->doc;
6124 if ((doc == NULL) || (cur->elem == NULL)) {
6125 VERROR(ctxt->userData,
6126 "xmlValidateAttributeCallback(%s): internal error\n",
6127 cur->name);
6128 return;
6129 }
6130 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6131 if (elem == NULL)
6132 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
6133 if (elem == NULL) {
6134 VERROR(ctxt->userData,
6135 "attribute %s: could not find decl for element %s\n",
6136 cur->name, cur->elem);
6137 return;
6138 }
6139 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
6140 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006141 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillard878eab02002-02-19 13:46:09 +00006142 cur->name, cur->elem);
6143 ctxt->valid = 0;
6144 }
6145 }
Owen Taylor3473f882001-02-23 17:55:21 +00006146}
6147
6148/**
6149 * xmlValidateDtdFinal:
6150 * @ctxt: the validation context
6151 * @doc: a document instance
6152 *
6153 * Does the final step for the dtds validation once all the
6154 * subsets have been parsed
6155 *
6156 * basically it does the following checks described by the XML Rec
6157 * - check that ENTITY and ENTITIES type attributes default or
6158 * possible values matches one of the defined entities.
6159 * - check that NOTATION type attributes default or
6160 * possible values matches one of the defined notations.
6161 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006162 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006163 */
6164
6165int
6166xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006167 xmlDtdPtr dtd;
6168 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006169 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006170
6171 if (doc == NULL) return(0);
6172 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6173 return(0);
6174 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006175 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006176 dtd = doc->intSubset;
6177 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6178 table = (xmlAttributeTablePtr) dtd->attributes;
6179 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006180 }
6181 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006182 entities = (xmlEntitiesTablePtr) dtd->entities;
6183 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6184 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006185 }
6186 dtd = doc->extSubset;
6187 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6188 table = (xmlAttributeTablePtr) dtd->attributes;
6189 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006190 }
6191 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006192 entities = (xmlEntitiesTablePtr) dtd->entities;
6193 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6194 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006195 }
6196 return(ctxt->valid);
6197}
6198
6199/**
6200 * xmlValidateDocument:
6201 * @ctxt: the validation context
6202 * @doc: a document instance
6203 *
6204 * Try to validate the document instance
6205 *
6206 * basically it does the all the checks described by the XML Rec
6207 * i.e. validates the internal and external subset (if present)
6208 * and validate the document tree.
6209 *
6210 * returns 1 if valid or 0 otherwise
6211 */
6212
6213int
6214xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6215 int ret;
6216 xmlNodePtr root;
6217
Daniel Veillard2fd85422002-10-16 14:32:41 +00006218 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
6219 VERROR(ctxt->userData, "no DTD found!\n" );
Owen Taylor3473f882001-02-23 17:55:21 +00006220 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006221 }
Owen Taylor3473f882001-02-23 17:55:21 +00006222 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6223 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6224 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6225 doc->intSubset->SystemID);
6226 if (doc->extSubset == NULL) {
6227 if (doc->intSubset->SystemID != NULL) {
6228 VERROR(ctxt->userData,
6229 "Could not load the external subset \"%s\"\n",
6230 doc->intSubset->SystemID);
6231 } else {
6232 VERROR(ctxt->userData,
6233 "Could not load the external subset \"%s\"\n",
6234 doc->intSubset->ExternalID);
6235 }
6236 return(0);
6237 }
6238 }
6239
6240 if (doc->ids != NULL) {
6241 xmlFreeIDTable(doc->ids);
6242 doc->ids = NULL;
6243 }
6244 if (doc->refs != NULL) {
6245 xmlFreeRefTable(doc->refs);
6246 doc->refs = NULL;
6247 }
6248 ret = xmlValidateDtdFinal(ctxt, doc);
6249 if (!xmlValidateRoot(ctxt, doc)) return(0);
6250
6251 root = xmlDocGetRootElement(doc);
6252 ret &= xmlValidateElement(ctxt, doc, root);
6253 ret &= xmlValidateDocumentFinal(ctxt, doc);
6254 return(ret);
6255}
6256
6257
6258/************************************************************************
6259 * *
6260 * Routines for dynamic validation editing *
6261 * *
6262 ************************************************************************/
6263
6264/**
6265 * xmlValidGetPotentialChildren:
6266 * @ctree: an element content tree
6267 * @list: an array to store the list of child names
6268 * @len: a pointer to the number of element in the list
6269 * @max: the size of the array
6270 *
6271 * Build/extend a list of potential children allowed by the content tree
6272 *
6273 * returns the number of element in the list, or -1 in case of error.
6274 */
6275
6276int
6277xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6278 int *len, int max) {
6279 int i;
6280
6281 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6282 return(-1);
6283 if (*len >= max) return(*len);
6284
6285 switch (ctree->type) {
6286 case XML_ELEMENT_CONTENT_PCDATA:
6287 for (i = 0; i < *len;i++)
6288 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6289 list[(*len)++] = BAD_CAST "#PCDATA";
6290 break;
6291 case XML_ELEMENT_CONTENT_ELEMENT:
6292 for (i = 0; i < *len;i++)
6293 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6294 list[(*len)++] = ctree->name;
6295 break;
6296 case XML_ELEMENT_CONTENT_SEQ:
6297 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6298 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6299 break;
6300 case XML_ELEMENT_CONTENT_OR:
6301 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6302 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6303 break;
6304 }
6305
6306 return(*len);
6307}
6308
6309/**
6310 * xmlValidGetValidElements:
6311 * @prev: an element to insert after
6312 * @next: an element to insert next
6313 * @list: an array to store the list of child names
6314 * @max: the size of the array
6315 *
6316 * This function returns the list of authorized children to insert
6317 * within an existing tree while respecting the validity constraints
6318 * forced by the Dtd. The insertion point is defined using @prev and
6319 * @next in the following ways:
6320 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6321 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6322 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6323 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6324 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6325 *
6326 * pointers to the element names are inserted at the beginning of the array
6327 * and do not need to be freed.
6328 *
6329 * returns the number of element in the list, or -1 in case of error. If
6330 * the function returns the value @max the caller is invited to grow the
6331 * receiving array and retry.
6332 */
6333
6334int
6335xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
6336 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006337 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006338 int nb_valid_elements = 0;
6339 const xmlChar *elements[256];
6340 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006341 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006342
6343 xmlNode *ref_node;
6344 xmlNode *parent;
6345 xmlNode *test_node;
6346
6347 xmlNode *prev_next;
6348 xmlNode *next_prev;
6349 xmlNode *parent_childs;
6350 xmlNode *parent_last;
6351
6352 xmlElement *element_desc;
6353
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00006354 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006355
Owen Taylor3473f882001-02-23 17:55:21 +00006356 if (prev == NULL && next == NULL)
6357 return(-1);
6358
6359 if (list == NULL) return(-1);
6360 if (max <= 0) return(-1);
6361
6362 nb_valid_elements = 0;
6363 ref_node = prev ? prev : next;
6364 parent = ref_node->parent;
6365
6366 /*
6367 * Retrieves the parent element declaration
6368 */
6369 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6370 parent->name);
6371 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6372 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6373 parent->name);
6374 if (element_desc == NULL) return(-1);
6375
6376 /*
6377 * Do a backup of the current tree structure
6378 */
6379 prev_next = prev ? prev->next : NULL;
6380 next_prev = next ? next->prev : NULL;
6381 parent_childs = parent->children;
6382 parent_last = parent->last;
6383
6384 /*
6385 * Creates a dummy node and insert it into the tree
6386 */
6387 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
6388 test_node->doc = ref_node->doc;
6389 test_node->parent = parent;
6390 test_node->prev = prev;
6391 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006392 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006393
6394 if (prev) prev->next = test_node;
6395 else parent->children = test_node;
6396
6397 if (next) next->prev = test_node;
6398 else parent->last = test_node;
6399
6400 /*
6401 * Insert each potential child node and check if the parent is
6402 * still valid
6403 */
6404 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6405 elements, &nb_elements, 256);
6406
6407 for (i = 0;i < nb_elements;i++) {
6408 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006409 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006410 int j;
6411
6412 for (j = 0; j < nb_valid_elements;j++)
6413 if (xmlStrEqual(elements[i], list[j])) break;
6414 list[nb_valid_elements++] = elements[i];
6415 if (nb_valid_elements >= max) break;
6416 }
6417 }
6418
6419 /*
6420 * Restore the tree structure
6421 */
6422 if (prev) prev->next = prev_next;
6423 if (next) next->prev = next_prev;
6424 parent->children = parent_childs;
6425 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006426
6427 /*
6428 * Free up the dummy node
6429 */
6430 test_node->name = name;
6431 xmlFreeNode(test_node);
6432
Owen Taylor3473f882001-02-23 17:55:21 +00006433 return(nb_valid_elements);
6434}