blob: 0f0fd231c7a2bba2552087d0cc50079b278c943c [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) {
Owen Taylor3473f882001-02-23 17:55:21 +0000374 char expr[1000];
375
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 Veillarda646cfd2002-09-17 21:50:03 +0000564 do {
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000565 xmlValidBuildAContentModel(content->c1, ctxt, name);
566 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000567 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
568 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
569 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000570 oldend = ctxt->state;
571 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000572 switch (ocur) {
573 case XML_ELEMENT_CONTENT_ONCE:
574 break;
575 case XML_ELEMENT_CONTENT_OPT:
576 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
577 break;
578 case XML_ELEMENT_CONTENT_MULT:
579 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000580 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000581 break;
582 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000583 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000584 break;
585 }
586 break;
587 }
588 case XML_ELEMENT_CONTENT_OR: {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000589 xmlAutomataStatePtr oldstate, oldend;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000590 xmlElementContentOccur ocur;
591
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000592 ocur = content->ocur;
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000593 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
594 (ocur == XML_ELEMENT_CONTENT_MULT)) {
595 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
596 ctxt->state, NULL);
597 }
598 oldstate = ctxt->state;
599 oldend = xmlAutomataNewState(ctxt->am);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000600
601 /*
602 * iterate over the subtypes and remerge the end with an
603 * epsilon transition
604 */
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000605 do {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000606 ctxt->state = oldstate;
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000607 xmlValidBuildAContentModel(content->c1, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000608 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000609 content = content->c2;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000610 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
611 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000612 ctxt->state = oldstate;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000613 xmlValidBuildAContentModel(content, ctxt, name);
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000614 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
615 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000616 switch (ocur) {
617 case XML_ELEMENT_CONTENT_ONCE:
618 break;
619 case XML_ELEMENT_CONTENT_OPT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000620 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000621 break;
622 case XML_ELEMENT_CONTENT_MULT:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000623 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
624 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000625 break;
626 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000627 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000628 break;
629 }
630 break;
631 }
632 default:
633 VERROR(ctxt->userData, "ContentModel broken for element %s\n",
634 name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000635 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000636 }
Daniel Veillard84d70a42002-09-16 10:51:38 +0000637 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000638}
639/**
640 * xmlValidBuildContentModel:
641 * @ctxt: a validation context
642 * @elem: an element declaration node
643 *
644 * (Re)Build the automata associated to the content model of this
645 * element
646 *
Daniel Veillard84d70a42002-09-16 10:51:38 +0000647 * Returns 1 in case of success, 0 in case of error
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000648 */
649int
650xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
651 xmlAutomataStatePtr start;
652
653 if ((ctxt == NULL) || (elem == NULL))
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000654 return(0);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000655 if (elem->type != XML_ELEMENT_DECL)
656 return(0);
657 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
658 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000659 /* TODO: should we rebuild in this case ? */
660 if (elem->contModel != NULL)
Daniel Veillard84d70a42002-09-16 10:51:38 +0000661 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000662
663 ctxt->am = xmlNewAutomata();
664 if (ctxt->am == NULL) {
665 VERROR(ctxt->userData, "Cannot create automata for element %s\n",
666 elem->name);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000667 return(0);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000668 }
669 start = ctxt->state = xmlAutomataGetInitState(ctxt->am);
670 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
671 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Daniel Veillard84d70a42002-09-16 10:51:38 +0000672 elem->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillard23e73572002-09-19 19:56:43 +0000673 if (!xmlRegexpIsDeterminist(elem->contModel)) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +0000674 char expr[5000];
675 expr[0] = 0;
676 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
677 VERROR(ctxt->userData, "Content model of %s is not determinist: %s\n",
678 elem->name, expr);
679#ifdef DEBUG_REGEXP_ALGO
680 xmlRegexpPrint(stderr, elem->contModel);
681#endif
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000682 ctxt->valid = 0;
683 }
684 ctxt->state = NULL;
685 xmlFreeAutomata(ctxt->am);
686 ctxt->am = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000687 return(1);
Daniel Veillardaeb258a2002-09-13 14:48:12 +0000688}
689
690#endif /* LIBXML_REGEXP_ENABLED */
691
Owen Taylor3473f882001-02-23 17:55:21 +0000692/************************************************************************
693 * *
694 * QName handling helper *
695 * *
696 ************************************************************************/
697
698/**
699 * xmlSplitQName2:
700 * @name: an XML parser context
701 * @prefix: a xmlChar **
702 *
703 * parse an XML qualified name string
704 *
705 * [NS 5] QName ::= (Prefix ':')? LocalPart
706 *
707 * [NS 6] Prefix ::= NCName
708 *
709 * [NS 7] LocalPart ::= NCName
710 *
711 * Returns NULL if not a QName, otherwise the local part, and prefix
712 * is updated to get the Prefix if any.
713 */
714
715xmlChar *
716xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
717 int len = 0;
718 xmlChar *ret = NULL;
719
720 *prefix = NULL;
721
Daniel Veillardf4309d72001-10-02 09:28:58 +0000722#ifndef XML_XML_NAMESPACE
Owen Taylor3473f882001-02-23 17:55:21 +0000723 /* xml: prefix is not really a namespace */
724 if ((name[0] == 'x') && (name[1] == 'm') &&
725 (name[2] == 'l') && (name[3] == ':'))
726 return(NULL);
Daniel Veillardf4309d72001-10-02 09:28:58 +0000727#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000728
729 /* nasty but valid */
730 if (name[0] == ':')
731 return(NULL);
732
733 /*
734 * we are not trying to validate but just to cut, and yes it will
735 * work even if this is as set of UTF-8 encoded chars
736 */
737 while ((name[len] != 0) && (name[len] != ':'))
738 len++;
739
740 if (name[len] == 0)
741 return(NULL);
742
743 *prefix = xmlStrndup(name, len);
744 ret = xmlStrdup(&name[len + 1]);
745
746 return(ret);
747}
748
749/****************************************************************
750 * *
751 * Util functions for data allocation/deallocation *
752 * *
753 ****************************************************************/
754
755/**
756 * xmlNewElementContent:
757 * @name: the subelement name or NULL
758 * @type: the type of element content decl
759 *
760 * Allocate an element content structure.
761 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000762 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000763 */
764xmlElementContentPtr
765xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
766 xmlElementContentPtr ret;
767
768 switch(type) {
769 case XML_ELEMENT_CONTENT_ELEMENT:
770 if (name == NULL) {
771 xmlGenericError(xmlGenericErrorContext,
772 "xmlNewElementContent : name == NULL !\n");
773 }
774 break;
775 case XML_ELEMENT_CONTENT_PCDATA:
776 case XML_ELEMENT_CONTENT_SEQ:
777 case XML_ELEMENT_CONTENT_OR:
778 if (name != NULL) {
779 xmlGenericError(xmlGenericErrorContext,
780 "xmlNewElementContent : name != NULL !\n");
781 }
782 break;
783 default:
784 xmlGenericError(xmlGenericErrorContext,
785 "xmlNewElementContent: unknown type %d\n", type);
786 return(NULL);
787 }
788 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
789 if (ret == NULL) {
790 xmlGenericError(xmlGenericErrorContext,
791 "xmlNewElementContent : out of memory!\n");
792 return(NULL);
793 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000794 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000795 ret->type = type;
796 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000797 if (name != NULL) {
798 xmlChar *prefix = NULL;
799 ret->name = xmlSplitQName2(name, &prefix);
800 if (ret->name == NULL)
801 ret->name = xmlStrdup(name);
802 ret->prefix = prefix;
803 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000804 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000805 ret->prefix = NULL;
806 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000807 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000808 return(ret);
809}
810
811/**
812 * xmlCopyElementContent:
Daniel Veillard01c13b52002-12-10 15:19:08 +0000813 * @cur: An element content pointer.
Owen Taylor3473f882001-02-23 17:55:21 +0000814 *
815 * Build a copy of an element content description.
816 *
817 * Returns the new xmlElementContentPtr or NULL in case of error.
818 */
819xmlElementContentPtr
820xmlCopyElementContent(xmlElementContentPtr cur) {
821 xmlElementContentPtr ret;
822
823 if (cur == NULL) return(NULL);
824 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
825 if (ret == NULL) {
826 xmlGenericError(xmlGenericErrorContext,
827 "xmlCopyElementContent : out of memory\n");
828 return(NULL);
829 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000830 if (cur->prefix != NULL)
831 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000832 ret->ocur = cur->ocur;
833 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000834 if (ret->c1 != NULL)
835 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000836 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000837 if (ret->c2 != NULL)
838 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000839 return(ret);
840}
841
842/**
843 * xmlFreeElementContent:
844 * @cur: the element content tree to free
845 *
846 * Free an element content structure. This is a recursive call !
847 */
848void
849xmlFreeElementContent(xmlElementContentPtr cur) {
850 if (cur == NULL) return;
851 switch (cur->type) {
852 case XML_ELEMENT_CONTENT_PCDATA:
853 case XML_ELEMENT_CONTENT_ELEMENT:
854 case XML_ELEMENT_CONTENT_SEQ:
855 case XML_ELEMENT_CONTENT_OR:
856 break;
857 default:
858 xmlGenericError(xmlGenericErrorContext,
859 "xmlFreeElementContent : type %d\n", cur->type);
860 return;
861 }
862 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
863 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
864 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000865 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000866 xmlFree(cur);
867}
868
869/**
870 * xmlDumpElementContent:
871 * @buf: An XML buffer
872 * @content: An element table
873 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
874 *
875 * This will dump the content of the element table as an XML DTD definition
876 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000877static void
Owen Taylor3473f882001-02-23 17:55:21 +0000878xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
879 if (content == NULL) return;
880
881 if (glob) xmlBufferWriteChar(buf, "(");
882 switch (content->type) {
883 case XML_ELEMENT_CONTENT_PCDATA:
884 xmlBufferWriteChar(buf, "#PCDATA");
885 break;
886 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000887 if (content->prefix != NULL) {
888 xmlBufferWriteCHAR(buf, content->prefix);
889 xmlBufferWriteChar(buf, ":");
890 }
Owen Taylor3473f882001-02-23 17:55:21 +0000891 xmlBufferWriteCHAR(buf, content->name);
892 break;
893 case XML_ELEMENT_CONTENT_SEQ:
894 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
895 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
896 xmlDumpElementContent(buf, content->c1, 1);
897 else
898 xmlDumpElementContent(buf, content->c1, 0);
899 xmlBufferWriteChar(buf, " , ");
900 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
901 xmlDumpElementContent(buf, content->c2, 1);
902 else
903 xmlDumpElementContent(buf, content->c2, 0);
904 break;
905 case XML_ELEMENT_CONTENT_OR:
906 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
907 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
908 xmlDumpElementContent(buf, content->c1, 1);
909 else
910 xmlDumpElementContent(buf, content->c1, 0);
911 xmlBufferWriteChar(buf, " | ");
912 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
913 xmlDumpElementContent(buf, content->c2, 1);
914 else
915 xmlDumpElementContent(buf, content->c2, 0);
916 break;
917 default:
918 xmlGenericError(xmlGenericErrorContext,
919 "xmlDumpElementContent: unknown type %d\n",
920 content->type);
921 }
922 if (glob)
923 xmlBufferWriteChar(buf, ")");
924 switch (content->ocur) {
925 case XML_ELEMENT_CONTENT_ONCE:
926 break;
927 case XML_ELEMENT_CONTENT_OPT:
928 xmlBufferWriteChar(buf, "?");
929 break;
930 case XML_ELEMENT_CONTENT_MULT:
931 xmlBufferWriteChar(buf, "*");
932 break;
933 case XML_ELEMENT_CONTENT_PLUS:
934 xmlBufferWriteChar(buf, "+");
935 break;
936 }
937}
938
939/**
940 * xmlSprintfElementContent:
941 * @buf: an output buffer
942 * @content: An element table
943 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
944 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000945 * Deprecated, unsafe, use xmlSnprintfElementContent
946 */
947void
948xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
949 xmlElementContentPtr content ATTRIBUTE_UNUSED,
950 int glob ATTRIBUTE_UNUSED) {
951}
952
953/**
954 * xmlSnprintfElementContent:
955 * @buf: an output buffer
956 * @size: the buffer size
957 * @content: An element table
958 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
959 *
Owen Taylor3473f882001-02-23 17:55:21 +0000960 * This will dump the content of the element content definition
961 * Intended just for the debug routine
962 */
963void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000964xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
965 int len;
966
Owen Taylor3473f882001-02-23 17:55:21 +0000967 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000968 len = strlen(buf);
969 if (size - len < 50) {
970 if ((size - len > 4) && (buf[len - 1] != '.'))
971 strcat(buf, " ...");
972 return;
973 }
Owen Taylor3473f882001-02-23 17:55:21 +0000974 if (glob) strcat(buf, "(");
975 switch (content->type) {
976 case XML_ELEMENT_CONTENT_PCDATA:
977 strcat(buf, "#PCDATA");
978 break;
979 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000980 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000981 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000982 strcat(buf, " ...");
983 return;
984 }
985 strcat(buf, (char *) content->prefix);
986 strcat(buf, ":");
987 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000988 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +0000989 strcat(buf, " ...");
990 return;
991 }
Owen Taylor3473f882001-02-23 17:55:21 +0000992 strcat(buf, (char *) content->name);
993 break;
994 case XML_ELEMENT_CONTENT_SEQ:
995 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
996 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000997 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000998 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000999 xmlSnprintfElementContent(buf, size, content->c1, 0);
1000 len = strlen(buf);
1001 if (size - len < 50) {
1002 if ((size - len > 4) && (buf[len - 1] != '.'))
1003 strcat(buf, " ...");
1004 return;
1005 }
Owen Taylor3473f882001-02-23 17:55:21 +00001006 strcat(buf, " , ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001007 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1008 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1009 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001010 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001011 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001012 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001013 break;
1014 case XML_ELEMENT_CONTENT_OR:
1015 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1016 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001017 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001018 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001019 xmlSnprintfElementContent(buf, size, content->c1, 0);
1020 len = strlen(buf);
1021 if (size - len < 50) {
1022 if ((size - len > 4) && (buf[len - 1] != '.'))
1023 strcat(buf, " ...");
1024 return;
1025 }
Owen Taylor3473f882001-02-23 17:55:21 +00001026 strcat(buf, " | ");
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00001027 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1028 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1029 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
Daniel Veillardd3d06722001-08-15 12:06:36 +00001030 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001031 else
Daniel Veillardd3d06722001-08-15 12:06:36 +00001032 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001033 break;
1034 }
1035 if (glob)
1036 strcat(buf, ")");
1037 switch (content->ocur) {
1038 case XML_ELEMENT_CONTENT_ONCE:
1039 break;
1040 case XML_ELEMENT_CONTENT_OPT:
1041 strcat(buf, "?");
1042 break;
1043 case XML_ELEMENT_CONTENT_MULT:
1044 strcat(buf, "*");
1045 break;
1046 case XML_ELEMENT_CONTENT_PLUS:
1047 strcat(buf, "+");
1048 break;
1049 }
1050}
1051
1052/****************************************************************
1053 * *
1054 * Registration of DTD declarations *
1055 * *
1056 ****************************************************************/
1057
1058/**
1059 * xmlCreateElementTable:
1060 *
1061 * create and initialize an empty element hash table.
1062 *
1063 * Returns the xmlElementTablePtr just created or NULL in case of error.
1064 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001065static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001066xmlCreateElementTable(void) {
1067 return(xmlHashCreate(0));
1068}
1069
1070/**
1071 * xmlFreeElement:
1072 * @elem: An element
1073 *
1074 * Deallocate the memory used by an element definition
1075 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001076static void
Owen Taylor3473f882001-02-23 17:55:21 +00001077xmlFreeElement(xmlElementPtr elem) {
1078 if (elem == NULL) return;
1079 xmlUnlinkNode((xmlNodePtr) elem);
1080 xmlFreeElementContent(elem->content);
1081 if (elem->name != NULL)
1082 xmlFree((xmlChar *) elem->name);
1083 if (elem->prefix != NULL)
1084 xmlFree((xmlChar *) elem->prefix);
Daniel Veillard84d70a42002-09-16 10:51:38 +00001085#ifdef LIBXML_REGEXP_ENABLED
1086 if (elem->contModel != NULL)
1087 xmlRegFreeRegexp(elem->contModel);
1088#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001089 xmlFree(elem);
1090}
1091
1092
1093/**
1094 * xmlAddElementDecl:
1095 * @ctxt: the validation context
1096 * @dtd: pointer to the DTD
1097 * @name: the entity name
1098 * @type: the element type
1099 * @content: the element content tree or NULL
1100 *
1101 * Register a new element declaration
1102 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001103 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001104 */
1105xmlElementPtr
1106xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
1107 xmlElementTypeVal type,
1108 xmlElementContentPtr content) {
1109 xmlElementPtr ret;
1110 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +00001111 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001112 xmlChar *ns, *uqname;
1113
1114 if (dtd == NULL) {
1115 xmlGenericError(xmlGenericErrorContext,
1116 "xmlAddElementDecl: dtd == NULL\n");
1117 return(NULL);
1118 }
1119 if (name == NULL) {
1120 xmlGenericError(xmlGenericErrorContext,
1121 "xmlAddElementDecl: name == NULL\n");
1122 return(NULL);
1123 }
1124 switch (type) {
1125 case XML_ELEMENT_TYPE_EMPTY:
1126 if (content != NULL) {
1127 xmlGenericError(xmlGenericErrorContext,
1128 "xmlAddElementDecl: content != NULL for EMPTY\n");
1129 return(NULL);
1130 }
1131 break;
1132 case XML_ELEMENT_TYPE_ANY:
1133 if (content != NULL) {
1134 xmlGenericError(xmlGenericErrorContext,
1135 "xmlAddElementDecl: content != NULL for ANY\n");
1136 return(NULL);
1137 }
1138 break;
1139 case XML_ELEMENT_TYPE_MIXED:
1140 if (content == NULL) {
1141 xmlGenericError(xmlGenericErrorContext,
1142 "xmlAddElementDecl: content == NULL for MIXED\n");
1143 return(NULL);
1144 }
1145 break;
1146 case XML_ELEMENT_TYPE_ELEMENT:
1147 if (content == NULL) {
1148 xmlGenericError(xmlGenericErrorContext,
1149 "xmlAddElementDecl: content == NULL for ELEMENT\n");
1150 return(NULL);
1151 }
1152 break;
1153 default:
1154 xmlGenericError(xmlGenericErrorContext,
1155 "xmlAddElementDecl: unknown type %d\n", type);
1156 return(NULL);
1157 }
1158
1159 /*
1160 * check if name is a QName
1161 */
1162 uqname = xmlSplitQName2(name, &ns);
1163 if (uqname != NULL)
1164 name = uqname;
1165
1166 /*
1167 * Create the Element table if needed.
1168 */
1169 table = (xmlElementTablePtr) dtd->elements;
1170 if (table == NULL) {
1171 table = xmlCreateElementTable();
1172 dtd->elements = (void *) table;
1173 }
1174 if (table == NULL) {
1175 xmlGenericError(xmlGenericErrorContext,
1176 "xmlAddElementDecl: Table creation failed!\n");
1177 return(NULL);
1178 }
1179
Daniel Veillarda10efa82001-04-18 13:09:01 +00001180 /*
1181 * lookup old attributes inserted on an undefined element in the
1182 * internal subset.
1183 */
1184 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1185 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1186 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1187 oldAttributes = ret->attributes;
1188 ret->attributes = NULL;
1189 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1190 xmlFreeElement(ret);
1191 }
Owen Taylor3473f882001-02-23 17:55:21 +00001192 }
Owen Taylor3473f882001-02-23 17:55:21 +00001193
1194 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +00001195 * The element may already be present if one of its attribute
1196 * was registered first
1197 */
1198 ret = xmlHashLookup2(table, name, ns);
1199 if (ret != NULL) {
1200 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
1201 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001202 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001203 */
1204 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
1205 if (uqname != NULL)
1206 xmlFree(uqname);
1207 return(NULL);
1208 }
1209 } else {
1210 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1211 if (ret == NULL) {
1212 xmlGenericError(xmlGenericErrorContext,
1213 "xmlAddElementDecl: out of memory\n");
1214 return(NULL);
1215 }
1216 memset(ret, 0, sizeof(xmlElement));
1217 ret->type = XML_ELEMENT_DECL;
1218
1219 /*
1220 * fill the structure.
1221 */
1222 ret->name = xmlStrdup(name);
1223 ret->prefix = ns;
1224
1225 /*
1226 * Validity Check:
1227 * Insertion must not fail
1228 */
1229 if (xmlHashAddEntry2(table, name, ns, ret)) {
1230 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001231 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +00001232 */
1233 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
1234 xmlFreeElement(ret);
1235 if (uqname != NULL)
1236 xmlFree(uqname);
1237 return(NULL);
1238 }
1239 }
1240
1241 /*
1242 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +00001243 */
1244 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +00001245 ret->content = xmlCopyElementContent(content);
Daniel Veillarda10efa82001-04-18 13:09:01 +00001246 ret->attributes = oldAttributes;
Owen Taylor3473f882001-02-23 17:55:21 +00001247
1248 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001249 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001250 */
1251 ret->parent = dtd;
1252 ret->doc = dtd->doc;
1253 if (dtd->last == NULL) {
1254 dtd->children = dtd->last = (xmlNodePtr) ret;
1255 } else {
1256 dtd->last->next = (xmlNodePtr) ret;
1257 ret->prev = dtd->last;
1258 dtd->last = (xmlNodePtr) ret;
1259 }
1260 if (uqname != NULL)
1261 xmlFree(uqname);
1262 return(ret);
1263}
1264
1265/**
1266 * xmlFreeElementTable:
1267 * @table: An element table
1268 *
1269 * Deallocate the memory used by an element hash table.
1270 */
1271void
1272xmlFreeElementTable(xmlElementTablePtr table) {
1273 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1274}
1275
1276/**
1277 * xmlCopyElement:
1278 * @elem: An element
1279 *
1280 * Build a copy of an element.
1281 *
1282 * Returns the new xmlElementPtr or NULL in case of error.
1283 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001284static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001285xmlCopyElement(xmlElementPtr elem) {
1286 xmlElementPtr cur;
1287
1288 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1289 if (cur == NULL) {
1290 xmlGenericError(xmlGenericErrorContext,
1291 "xmlCopyElement: out of memory !\n");
1292 return(NULL);
1293 }
1294 memset(cur, 0, sizeof(xmlElement));
1295 cur->type = XML_ELEMENT_DECL;
1296 cur->etype = elem->etype;
1297 if (elem->name != NULL)
1298 cur->name = xmlStrdup(elem->name);
1299 else
1300 cur->name = NULL;
1301 if (elem->prefix != NULL)
1302 cur->prefix = xmlStrdup(elem->prefix);
1303 else
1304 cur->prefix = NULL;
1305 cur->content = xmlCopyElementContent(elem->content);
1306 /* TODO : rebuild the attribute list on the copy */
1307 cur->attributes = NULL;
1308 return(cur);
1309}
1310
1311/**
1312 * xmlCopyElementTable:
1313 * @table: An element table
1314 *
1315 * Build a copy of an element table.
1316 *
1317 * Returns the new xmlElementTablePtr or NULL in case of error.
1318 */
1319xmlElementTablePtr
1320xmlCopyElementTable(xmlElementTablePtr table) {
1321 return((xmlElementTablePtr) xmlHashCopy(table,
1322 (xmlHashCopier) xmlCopyElement));
1323}
1324
1325/**
1326 * xmlDumpElementDecl:
1327 * @buf: the XML buffer output
1328 * @elem: An element table
1329 *
1330 * This will dump the content of the element declaration as an XML
1331 * DTD definition
1332 */
1333void
1334xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1335 switch (elem->etype) {
1336 case XML_ELEMENT_TYPE_EMPTY:
1337 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001338 if (elem->prefix != NULL) {
1339 xmlBufferWriteCHAR(buf, elem->prefix);
1340 xmlBufferWriteChar(buf, ":");
1341 }
Owen Taylor3473f882001-02-23 17:55:21 +00001342 xmlBufferWriteCHAR(buf, elem->name);
1343 xmlBufferWriteChar(buf, " EMPTY>\n");
1344 break;
1345 case XML_ELEMENT_TYPE_ANY:
1346 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001347 if (elem->prefix != NULL) {
1348 xmlBufferWriteCHAR(buf, elem->prefix);
1349 xmlBufferWriteChar(buf, ":");
1350 }
Owen Taylor3473f882001-02-23 17:55:21 +00001351 xmlBufferWriteCHAR(buf, elem->name);
1352 xmlBufferWriteChar(buf, " ANY>\n");
1353 break;
1354 case XML_ELEMENT_TYPE_MIXED:
1355 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001356 if (elem->prefix != NULL) {
1357 xmlBufferWriteCHAR(buf, elem->prefix);
1358 xmlBufferWriteChar(buf, ":");
1359 }
Owen Taylor3473f882001-02-23 17:55:21 +00001360 xmlBufferWriteCHAR(buf, elem->name);
1361 xmlBufferWriteChar(buf, " ");
1362 xmlDumpElementContent(buf, elem->content, 1);
1363 xmlBufferWriteChar(buf, ">\n");
1364 break;
1365 case XML_ELEMENT_TYPE_ELEMENT:
1366 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001367 if (elem->prefix != NULL) {
1368 xmlBufferWriteCHAR(buf, elem->prefix);
1369 xmlBufferWriteChar(buf, ":");
1370 }
Owen Taylor3473f882001-02-23 17:55:21 +00001371 xmlBufferWriteCHAR(buf, elem->name);
1372 xmlBufferWriteChar(buf, " ");
1373 xmlDumpElementContent(buf, elem->content, 1);
1374 xmlBufferWriteChar(buf, ">\n");
1375 break;
1376 default:
1377 xmlGenericError(xmlGenericErrorContext,
1378 "xmlDumpElementDecl: internal: unknown type %d\n",
1379 elem->etype);
1380 }
1381}
1382
1383/**
1384 * xmlDumpElementTable:
1385 * @buf: the XML buffer output
1386 * @table: An element table
1387 *
1388 * This will dump the content of the element table as an XML DTD definition
1389 */
1390void
1391xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1392 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1393}
1394
1395/**
1396 * xmlCreateEnumeration:
1397 * @name: the enumeration name or NULL
1398 *
1399 * create and initialize an enumeration attribute node.
1400 *
1401 * Returns the xmlEnumerationPtr just created or NULL in case
1402 * of error.
1403 */
1404xmlEnumerationPtr
1405xmlCreateEnumeration(xmlChar *name) {
1406 xmlEnumerationPtr ret;
1407
1408 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1409 if (ret == NULL) {
1410 xmlGenericError(xmlGenericErrorContext,
1411 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1412 (long)sizeof(xmlEnumeration));
1413 return(NULL);
1414 }
1415 memset(ret, 0, sizeof(xmlEnumeration));
1416
1417 if (name != NULL)
1418 ret->name = xmlStrdup(name);
1419 return(ret);
1420}
1421
1422/**
1423 * xmlFreeEnumeration:
1424 * @cur: the tree to free.
1425 *
1426 * free an enumeration attribute node (recursive).
1427 */
1428void
1429xmlFreeEnumeration(xmlEnumerationPtr cur) {
1430 if (cur == NULL) return;
1431
1432 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1433
1434 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001435 xmlFree(cur);
1436}
1437
1438/**
1439 * xmlCopyEnumeration:
1440 * @cur: the tree to copy.
1441 *
1442 * Copy an enumeration attribute node (recursive).
1443 *
1444 * Returns the xmlEnumerationPtr just created or NULL in case
1445 * of error.
1446 */
1447xmlEnumerationPtr
1448xmlCopyEnumeration(xmlEnumerationPtr cur) {
1449 xmlEnumerationPtr ret;
1450
1451 if (cur == NULL) return(NULL);
1452 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1453
1454 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1455 else ret->next = NULL;
1456
1457 return(ret);
1458}
1459
1460/**
1461 * xmlDumpEnumeration:
1462 * @buf: the XML buffer output
1463 * @enum: An enumeration
1464 *
1465 * This will dump the content of the enumeration
1466 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001467static void
Owen Taylor3473f882001-02-23 17:55:21 +00001468xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1469 if (cur == NULL) return;
1470
1471 xmlBufferWriteCHAR(buf, cur->name);
1472 if (cur->next == NULL)
1473 xmlBufferWriteChar(buf, ")");
1474 else {
1475 xmlBufferWriteChar(buf, " | ");
1476 xmlDumpEnumeration(buf, cur->next);
1477 }
1478}
1479
1480/**
1481 * xmlCreateAttributeTable:
1482 *
1483 * create and initialize an empty attribute hash table.
1484 *
1485 * Returns the xmlAttributeTablePtr just created or NULL in case
1486 * of error.
1487 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001488static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001489xmlCreateAttributeTable(void) {
1490 return(xmlHashCreate(0));
1491}
1492
1493/**
1494 * xmlScanAttributeDeclCallback:
1495 * @attr: the attribute decl
1496 * @list: the list to update
1497 *
1498 * Callback called by xmlScanAttributeDecl when a new attribute
1499 * has to be entered in the list.
1500 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001501static void
Owen Taylor3473f882001-02-23 17:55:21 +00001502xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001503 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001504 attr->nexth = *list;
1505 *list = attr;
1506}
1507
1508/**
1509 * xmlScanAttributeDecl:
1510 * @dtd: pointer to the DTD
1511 * @elem: the element name
1512 *
1513 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001514 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001515 *
1516 * Returns the pointer to the first attribute decl in the chain,
1517 * possibly NULL.
1518 */
1519xmlAttributePtr
1520xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1521 xmlAttributePtr ret = NULL;
1522 xmlAttributeTablePtr table;
1523
1524 if (dtd == NULL) {
1525 xmlGenericError(xmlGenericErrorContext,
1526 "xmlScanAttributeDecl: dtd == NULL\n");
1527 return(NULL);
1528 }
1529 if (elem == NULL) {
1530 xmlGenericError(xmlGenericErrorContext,
1531 "xmlScanAttributeDecl: elem == NULL\n");
1532 return(NULL);
1533 }
1534 table = (xmlAttributeTablePtr) dtd->attributes;
1535 if (table == NULL)
1536 return(NULL);
1537
1538 /* WRONG !!! */
1539 xmlHashScan3(table, NULL, NULL, elem,
1540 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1541 return(ret);
1542}
1543
1544/**
1545 * xmlScanIDAttributeDecl:
1546 * @ctxt: the validation context
1547 * @elem: the element name
1548 *
1549 * Verify that the element don't have too many ID attributes
1550 * declared.
1551 *
1552 * Returns the number of ID attributes found.
1553 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001554static int
Owen Taylor3473f882001-02-23 17:55:21 +00001555xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1556 xmlAttributePtr cur;
1557 int ret = 0;
1558
1559 if (elem == NULL) return(0);
1560 cur = elem->attributes;
1561 while (cur != NULL) {
1562 if (cur->atype == XML_ATTRIBUTE_ID) {
1563 ret ++;
1564 if (ret > 1)
1565 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001566 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001567 elem->name, cur->name);
1568 }
1569 cur = cur->nexth;
1570 }
1571 return(ret);
1572}
1573
1574/**
1575 * xmlFreeAttribute:
1576 * @elem: An attribute
1577 *
1578 * Deallocate the memory used by an attribute definition
1579 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001580static void
Owen Taylor3473f882001-02-23 17:55:21 +00001581xmlFreeAttribute(xmlAttributePtr attr) {
1582 if (attr == NULL) return;
1583 xmlUnlinkNode((xmlNodePtr) attr);
1584 if (attr->tree != NULL)
1585 xmlFreeEnumeration(attr->tree);
1586 if (attr->elem != NULL)
1587 xmlFree((xmlChar *) attr->elem);
1588 if (attr->name != NULL)
1589 xmlFree((xmlChar *) attr->name);
1590 if (attr->defaultValue != NULL)
1591 xmlFree((xmlChar *) attr->defaultValue);
1592 if (attr->prefix != NULL)
1593 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001594 xmlFree(attr);
1595}
1596
1597
1598/**
1599 * xmlAddAttributeDecl:
1600 * @ctxt: the validation context
1601 * @dtd: pointer to the DTD
1602 * @elem: the element name
1603 * @name: the attribute name
1604 * @ns: the attribute namespace prefix
1605 * @type: the attribute type
1606 * @def: the attribute default type
1607 * @defaultValue: the attribute default value
1608 * @tree: if it's an enumeration, the associated list
1609 *
1610 * Register a new attribute declaration
1611 * Note that @tree becomes the ownership of the DTD
1612 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001613 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001614 */
1615xmlAttributePtr
1616xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1617 const xmlChar *name, const xmlChar *ns,
1618 xmlAttributeType type, xmlAttributeDefault def,
1619 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1620 xmlAttributePtr ret;
1621 xmlAttributeTablePtr table;
1622 xmlElementPtr elemDef;
1623
1624 if (dtd == NULL) {
1625 xmlGenericError(xmlGenericErrorContext,
1626 "xmlAddAttributeDecl: dtd == NULL\n");
1627 xmlFreeEnumeration(tree);
1628 return(NULL);
1629 }
1630 if (name == NULL) {
1631 xmlGenericError(xmlGenericErrorContext,
1632 "xmlAddAttributeDecl: name == NULL\n");
1633 xmlFreeEnumeration(tree);
1634 return(NULL);
1635 }
1636 if (elem == NULL) {
1637 xmlGenericError(xmlGenericErrorContext,
1638 "xmlAddAttributeDecl: elem == NULL\n");
1639 xmlFreeEnumeration(tree);
1640 return(NULL);
1641 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001642
Owen Taylor3473f882001-02-23 17:55:21 +00001643 /*
1644 * Check the type and possibly the default value.
1645 */
1646 switch (type) {
1647 case XML_ATTRIBUTE_CDATA:
1648 break;
1649 case XML_ATTRIBUTE_ID:
1650 break;
1651 case XML_ATTRIBUTE_IDREF:
1652 break;
1653 case XML_ATTRIBUTE_IDREFS:
1654 break;
1655 case XML_ATTRIBUTE_ENTITY:
1656 break;
1657 case XML_ATTRIBUTE_ENTITIES:
1658 break;
1659 case XML_ATTRIBUTE_NMTOKEN:
1660 break;
1661 case XML_ATTRIBUTE_NMTOKENS:
1662 break;
1663 case XML_ATTRIBUTE_ENUMERATION:
1664 break;
1665 case XML_ATTRIBUTE_NOTATION:
1666 break;
1667 default:
1668 xmlGenericError(xmlGenericErrorContext,
1669 "xmlAddAttributeDecl: unknown type %d\n", type);
1670 xmlFreeEnumeration(tree);
1671 return(NULL);
1672 }
1673 if ((defaultValue != NULL) &&
1674 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillard58e44c92002-08-02 22:19:49 +00001675 VERROR(ctxt->userData, "Attribute %s of %s: invalid default value\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001676 elem, name, defaultValue);
1677 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001678 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001679 }
1680
1681 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001682 * Check first that an attribute defined in the external subset wasn't
1683 * already defined in the internal subset
1684 */
1685 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1686 (dtd->doc->intSubset != NULL) &&
1687 (dtd->doc->intSubset->attributes != NULL)) {
1688 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1689 if (ret != NULL)
1690 return(NULL);
1691 }
1692
1693 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001694 * Create the Attribute table if needed.
1695 */
1696 table = (xmlAttributeTablePtr) dtd->attributes;
1697 if (table == NULL) {
1698 table = xmlCreateAttributeTable();
1699 dtd->attributes = (void *) table;
1700 }
1701 if (table == NULL) {
1702 xmlGenericError(xmlGenericErrorContext,
1703 "xmlAddAttributeDecl: Table creation failed!\n");
1704 return(NULL);
1705 }
1706
1707
1708 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1709 if (ret == NULL) {
1710 xmlGenericError(xmlGenericErrorContext,
1711 "xmlAddAttributeDecl: out of memory\n");
1712 return(NULL);
1713 }
1714 memset(ret, 0, sizeof(xmlAttribute));
1715 ret->type = XML_ATTRIBUTE_DECL;
1716
1717 /*
1718 * fill the structure.
1719 */
1720 ret->atype = type;
1721 ret->name = xmlStrdup(name);
1722 ret->prefix = xmlStrdup(ns);
1723 ret->elem = xmlStrdup(elem);
1724 ret->def = def;
1725 ret->tree = tree;
1726 if (defaultValue != NULL)
1727 ret->defaultValue = xmlStrdup(defaultValue);
1728
1729 /*
1730 * Validity Check:
1731 * Search the DTD for previous declarations of the ATTLIST
1732 */
1733 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1734 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001735 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001736 */
1737 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00001738 "Attribute %s of element %s: already defined\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001739 name, elem);
1740 xmlFreeAttribute(ret);
1741 return(NULL);
1742 }
1743
1744 /*
1745 * Validity Check:
1746 * Multiple ID per element
1747 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001748 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001749 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001750
Owen Taylor3473f882001-02-23 17:55:21 +00001751 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001752 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001753 VERROR(ctxt->userData,
1754 "Element %s has too may ID attributes defined : %s\n",
1755 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001756 ctxt->valid = 0;
1757 }
1758
Daniel Veillard48da9102001-08-07 01:10:10 +00001759 /*
1760 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001761 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001762 */
1763 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1764 ((ret->prefix != NULL &&
1765 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1766 ret->nexth = elemDef->attributes;
1767 elemDef->attributes = ret;
1768 } else {
1769 xmlAttributePtr tmp = elemDef->attributes;
1770
1771 while ((tmp != NULL) &&
1772 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1773 ((ret->prefix != NULL &&
1774 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1775 if (tmp->nexth == NULL)
1776 break;
1777 tmp = tmp->nexth;
1778 }
1779 if (tmp != NULL) {
1780 ret->nexth = tmp->nexth;
1781 tmp->nexth = ret;
1782 } else {
1783 ret->nexth = elemDef->attributes;
1784 elemDef->attributes = ret;
1785 }
1786 }
Owen Taylor3473f882001-02-23 17:55:21 +00001787 }
1788
1789 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001790 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001791 */
1792 ret->parent = dtd;
1793 ret->doc = dtd->doc;
1794 if (dtd->last == NULL) {
1795 dtd->children = dtd->last = (xmlNodePtr) ret;
1796 } else {
1797 dtd->last->next = (xmlNodePtr) ret;
1798 ret->prev = dtd->last;
1799 dtd->last = (xmlNodePtr) ret;
1800 }
1801 return(ret);
1802}
1803
1804/**
1805 * xmlFreeAttributeTable:
1806 * @table: An attribute table
1807 *
1808 * Deallocate the memory used by an entities hash table.
1809 */
1810void
1811xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1812 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1813}
1814
1815/**
1816 * xmlCopyAttribute:
1817 * @attr: An attribute
1818 *
1819 * Build a copy of an attribute.
1820 *
1821 * Returns the new xmlAttributePtr or NULL in case of error.
1822 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001823static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001824xmlCopyAttribute(xmlAttributePtr attr) {
1825 xmlAttributePtr cur;
1826
1827 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1828 if (cur == NULL) {
1829 xmlGenericError(xmlGenericErrorContext,
1830 "xmlCopyAttribute: out of memory !\n");
1831 return(NULL);
1832 }
1833 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001834 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001835 cur->atype = attr->atype;
1836 cur->def = attr->def;
1837 cur->tree = xmlCopyEnumeration(attr->tree);
1838 if (attr->elem != NULL)
1839 cur->elem = xmlStrdup(attr->elem);
1840 if (attr->name != NULL)
1841 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001842 if (attr->prefix != NULL)
1843 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001844 if (attr->defaultValue != NULL)
1845 cur->defaultValue = xmlStrdup(attr->defaultValue);
1846 return(cur);
1847}
1848
1849/**
1850 * xmlCopyAttributeTable:
1851 * @table: An attribute table
1852 *
1853 * Build a copy of an attribute table.
1854 *
1855 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1856 */
1857xmlAttributeTablePtr
1858xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1859 return((xmlAttributeTablePtr) xmlHashCopy(table,
1860 (xmlHashCopier) xmlCopyAttribute));
1861}
1862
1863/**
1864 * xmlDumpAttributeDecl:
1865 * @buf: the XML buffer output
1866 * @attr: An attribute declaration
1867 *
1868 * This will dump the content of the attribute declaration as an XML
1869 * DTD definition
1870 */
1871void
1872xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1873 xmlBufferWriteChar(buf, "<!ATTLIST ");
1874 xmlBufferWriteCHAR(buf, attr->elem);
1875 xmlBufferWriteChar(buf, " ");
1876 if (attr->prefix != NULL) {
1877 xmlBufferWriteCHAR(buf, attr->prefix);
1878 xmlBufferWriteChar(buf, ":");
1879 }
1880 xmlBufferWriteCHAR(buf, attr->name);
1881 switch (attr->atype) {
1882 case XML_ATTRIBUTE_CDATA:
1883 xmlBufferWriteChar(buf, " CDATA");
1884 break;
1885 case XML_ATTRIBUTE_ID:
1886 xmlBufferWriteChar(buf, " ID");
1887 break;
1888 case XML_ATTRIBUTE_IDREF:
1889 xmlBufferWriteChar(buf, " IDREF");
1890 break;
1891 case XML_ATTRIBUTE_IDREFS:
1892 xmlBufferWriteChar(buf, " IDREFS");
1893 break;
1894 case XML_ATTRIBUTE_ENTITY:
1895 xmlBufferWriteChar(buf, " ENTITY");
1896 break;
1897 case XML_ATTRIBUTE_ENTITIES:
1898 xmlBufferWriteChar(buf, " ENTITIES");
1899 break;
1900 case XML_ATTRIBUTE_NMTOKEN:
1901 xmlBufferWriteChar(buf, " NMTOKEN");
1902 break;
1903 case XML_ATTRIBUTE_NMTOKENS:
1904 xmlBufferWriteChar(buf, " NMTOKENS");
1905 break;
1906 case XML_ATTRIBUTE_ENUMERATION:
1907 xmlBufferWriteChar(buf, " (");
1908 xmlDumpEnumeration(buf, attr->tree);
1909 break;
1910 case XML_ATTRIBUTE_NOTATION:
1911 xmlBufferWriteChar(buf, " NOTATION (");
1912 xmlDumpEnumeration(buf, attr->tree);
1913 break;
1914 default:
1915 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001916 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001917 attr->atype);
1918 }
1919 switch (attr->def) {
1920 case XML_ATTRIBUTE_NONE:
1921 break;
1922 case XML_ATTRIBUTE_REQUIRED:
1923 xmlBufferWriteChar(buf, " #REQUIRED");
1924 break;
1925 case XML_ATTRIBUTE_IMPLIED:
1926 xmlBufferWriteChar(buf, " #IMPLIED");
1927 break;
1928 case XML_ATTRIBUTE_FIXED:
1929 xmlBufferWriteChar(buf, " #FIXED");
1930 break;
1931 default:
1932 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001933 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001934 attr->def);
1935 }
1936 if (attr->defaultValue != NULL) {
1937 xmlBufferWriteChar(buf, " ");
1938 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1939 }
1940 xmlBufferWriteChar(buf, ">\n");
1941}
1942
1943/**
1944 * xmlDumpAttributeTable:
1945 * @buf: the XML buffer output
1946 * @table: An attribute table
1947 *
1948 * This will dump the content of the attribute table as an XML DTD definition
1949 */
1950void
1951xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1952 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1953}
1954
1955/************************************************************************
1956 * *
1957 * NOTATIONs *
1958 * *
1959 ************************************************************************/
1960/**
1961 * xmlCreateNotationTable:
1962 *
1963 * create and initialize an empty notation hash table.
1964 *
1965 * Returns the xmlNotationTablePtr just created or NULL in case
1966 * of error.
1967 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001968static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001969xmlCreateNotationTable(void) {
1970 return(xmlHashCreate(0));
1971}
1972
1973/**
1974 * xmlFreeNotation:
1975 * @not: A notation
1976 *
1977 * Deallocate the memory used by an notation definition
1978 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001979static void
Owen Taylor3473f882001-02-23 17:55:21 +00001980xmlFreeNotation(xmlNotationPtr nota) {
1981 if (nota == NULL) return;
1982 if (nota->name != NULL)
1983 xmlFree((xmlChar *) nota->name);
1984 if (nota->PublicID != NULL)
1985 xmlFree((xmlChar *) nota->PublicID);
1986 if (nota->SystemID != NULL)
1987 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001988 xmlFree(nota);
1989}
1990
1991
1992/**
1993 * xmlAddNotationDecl:
1994 * @dtd: pointer to the DTD
1995 * @ctxt: the validation context
1996 * @name: the entity name
1997 * @PublicID: the public identifier or NULL
1998 * @SystemID: the system identifier or NULL
1999 *
2000 * Register a new notation declaration
2001 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002002 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00002003 */
2004xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002005xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002006 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00002007 const xmlChar *PublicID, const xmlChar *SystemID) {
2008 xmlNotationPtr ret;
2009 xmlNotationTablePtr table;
2010
2011 if (dtd == NULL) {
2012 xmlGenericError(xmlGenericErrorContext,
2013 "xmlAddNotationDecl: dtd == NULL\n");
2014 return(NULL);
2015 }
2016 if (name == NULL) {
2017 xmlGenericError(xmlGenericErrorContext,
2018 "xmlAddNotationDecl: name == NULL\n");
2019 return(NULL);
2020 }
2021 if ((PublicID == NULL) && (SystemID == NULL)) {
2022 xmlGenericError(xmlGenericErrorContext,
2023 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00002024 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002025 }
2026
2027 /*
2028 * Create the Notation table if needed.
2029 */
2030 table = (xmlNotationTablePtr) dtd->notations;
2031 if (table == NULL)
2032 dtd->notations = table = xmlCreateNotationTable();
2033 if (table == NULL) {
2034 xmlGenericError(xmlGenericErrorContext,
2035 "xmlAddNotationDecl: Table creation failed!\n");
2036 return(NULL);
2037 }
2038
2039 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2040 if (ret == NULL) {
2041 xmlGenericError(xmlGenericErrorContext,
2042 "xmlAddNotationDecl: out of memory\n");
2043 return(NULL);
2044 }
2045 memset(ret, 0, sizeof(xmlNotation));
2046
2047 /*
2048 * fill the structure.
2049 */
2050 ret->name = xmlStrdup(name);
2051 if (SystemID != NULL)
2052 ret->SystemID = xmlStrdup(SystemID);
2053 if (PublicID != NULL)
2054 ret->PublicID = xmlStrdup(PublicID);
2055
2056 /*
2057 * Validity Check:
2058 * Check the DTD for previous declarations of the ATTLIST
2059 */
2060 if (xmlHashAddEntry(table, name, ret)) {
2061 xmlGenericError(xmlGenericErrorContext,
2062 "xmlAddNotationDecl: %s already defined\n", name);
2063 xmlFreeNotation(ret);
2064 return(NULL);
2065 }
2066 return(ret);
2067}
2068
2069/**
2070 * xmlFreeNotationTable:
2071 * @table: An notation table
2072 *
2073 * Deallocate the memory used by an entities hash table.
2074 */
2075void
2076xmlFreeNotationTable(xmlNotationTablePtr table) {
2077 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2078}
2079
2080/**
2081 * xmlCopyNotation:
2082 * @nota: A notation
2083 *
2084 * Build a copy of a notation.
2085 *
2086 * Returns the new xmlNotationPtr or NULL in case of error.
2087 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002088static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002089xmlCopyNotation(xmlNotationPtr nota) {
2090 xmlNotationPtr cur;
2091
2092 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2093 if (cur == NULL) {
2094 xmlGenericError(xmlGenericErrorContext,
2095 "xmlCopyNotation: out of memory !\n");
2096 return(NULL);
2097 }
2098 if (nota->name != NULL)
2099 cur->name = xmlStrdup(nota->name);
2100 else
2101 cur->name = NULL;
2102 if (nota->PublicID != NULL)
2103 cur->PublicID = xmlStrdup(nota->PublicID);
2104 else
2105 cur->PublicID = NULL;
2106 if (nota->SystemID != NULL)
2107 cur->SystemID = xmlStrdup(nota->SystemID);
2108 else
2109 cur->SystemID = NULL;
2110 return(cur);
2111}
2112
2113/**
2114 * xmlCopyNotationTable:
2115 * @table: A notation table
2116 *
2117 * Build a copy of a notation table.
2118 *
2119 * Returns the new xmlNotationTablePtr or NULL in case of error.
2120 */
2121xmlNotationTablePtr
2122xmlCopyNotationTable(xmlNotationTablePtr table) {
2123 return((xmlNotationTablePtr) xmlHashCopy(table,
2124 (xmlHashCopier) xmlCopyNotation));
2125}
2126
2127/**
2128 * xmlDumpNotationDecl:
2129 * @buf: the XML buffer output
2130 * @nota: A notation declaration
2131 *
2132 * This will dump the content the notation declaration as an XML DTD definition
2133 */
2134void
2135xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2136 xmlBufferWriteChar(buf, "<!NOTATION ");
2137 xmlBufferWriteCHAR(buf, nota->name);
2138 if (nota->PublicID != NULL) {
2139 xmlBufferWriteChar(buf, " PUBLIC ");
2140 xmlBufferWriteQuotedString(buf, nota->PublicID);
2141 if (nota->SystemID != NULL) {
2142 xmlBufferWriteChar(buf, " ");
2143 xmlBufferWriteCHAR(buf, nota->SystemID);
2144 }
2145 } else {
2146 xmlBufferWriteChar(buf, " SYSTEM ");
2147 xmlBufferWriteCHAR(buf, nota->SystemID);
2148 }
2149 xmlBufferWriteChar(buf, " >\n");
2150}
2151
2152/**
2153 * xmlDumpNotationTable:
2154 * @buf: the XML buffer output
2155 * @table: A notation table
2156 *
2157 * This will dump the content of the notation table as an XML DTD definition
2158 */
2159void
2160xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2161 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
2162}
2163
2164/************************************************************************
2165 * *
2166 * IDs *
2167 * *
2168 ************************************************************************/
2169/**
2170 * xmlCreateIDTable:
2171 *
2172 * create and initialize an empty id hash table.
2173 *
2174 * Returns the xmlIDTablePtr just created or NULL in case
2175 * of error.
2176 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002177static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002178xmlCreateIDTable(void) {
2179 return(xmlHashCreate(0));
2180}
2181
2182/**
2183 * xmlFreeID:
2184 * @not: A id
2185 *
2186 * Deallocate the memory used by an id definition
2187 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002188static void
Owen Taylor3473f882001-02-23 17:55:21 +00002189xmlFreeID(xmlIDPtr id) {
2190 if (id == NULL) return;
2191 if (id->value != NULL)
2192 xmlFree((xmlChar *) id->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002193 if (id->name != NULL)
2194 xmlFree((xmlChar *) id->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002195 xmlFree(id);
2196}
2197
2198/**
2199 * xmlAddID:
2200 * @ctxt: the validation context
2201 * @doc: pointer to the document
2202 * @value: the value name
2203 * @attr: the attribute holding the ID
2204 *
2205 * Register a new id declaration
2206 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002207 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002208 */
2209xmlIDPtr
2210xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2211 xmlAttrPtr attr) {
2212 xmlIDPtr ret;
2213 xmlIDTablePtr table;
2214
2215 if (doc == NULL) {
2216 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002217 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002218 return(NULL);
2219 }
2220 if (value == NULL) {
2221 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002222 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002223 return(NULL);
2224 }
2225 if (attr == NULL) {
2226 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002227 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002228 return(NULL);
2229 }
2230
2231 /*
2232 * Create the ID table if needed.
2233 */
2234 table = (xmlIDTablePtr) doc->ids;
2235 if (table == NULL)
2236 doc->ids = table = xmlCreateIDTable();
2237 if (table == NULL) {
2238 xmlGenericError(xmlGenericErrorContext,
2239 "xmlAddID: Table creation failed!\n");
2240 return(NULL);
2241 }
2242
2243 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2244 if (ret == NULL) {
2245 xmlGenericError(xmlGenericErrorContext,
2246 "xmlAddID: out of memory\n");
2247 return(NULL);
2248 }
2249
2250 /*
2251 * fill the structure.
2252 */
2253 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002254 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2255 /*
2256 * Operating in streaming mode, attr is gonna disapear
2257 */
2258 ret->name = xmlStrdup(attr->name);
2259 ret->attr = NULL;
2260 } else {
2261 ret->attr = attr;
2262 ret->name = NULL;
2263 }
2264 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002265
2266 if (xmlHashAddEntry(table, value, ret) < 0) {
2267 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002268 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00002269 */
Daniel Veillard76575762002-09-05 14:21:15 +00002270 if (ctxt != NULL) {
2271 VECTXT(ctxt, attr->parent);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00002272 VERROR(ctxt->userData, "ID %s already defined\n", value);
Daniel Veillard76575762002-09-05 14:21:15 +00002273 }
Owen Taylor3473f882001-02-23 17:55:21 +00002274 xmlFreeID(ret);
2275 return(NULL);
2276 }
2277 return(ret);
2278}
2279
2280/**
2281 * xmlFreeIDTable:
2282 * @table: An id table
2283 *
2284 * Deallocate the memory used by an ID hash table.
2285 */
2286void
2287xmlFreeIDTable(xmlIDTablePtr table) {
2288 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2289}
2290
2291/**
2292 * xmlIsID:
2293 * @doc: the document
2294 * @elem: the element carrying the attribute
2295 * @attr: the attribute
2296 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002297 * Determine whether an attribute is of type ID. In case we have DTD(s)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00002298 * then this is done if DTD loading has been requested. In the case
2299 * of HTML documents parsed with the HTML parser, then ID detection is
2300 * done systematically.
Owen Taylor3473f882001-02-23 17:55:21 +00002301 *
2302 * Returns 0 or 1 depending on the lookup result
2303 */
2304int
2305xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2306 if (doc == NULL) return(0);
2307 if (attr == NULL) return(0);
2308 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2309 return(0);
2310 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2311 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2312 (xmlStrEqual(BAD_CAST "name", attr->name)))
2313 return(1);
2314 return(0);
2315 } else {
2316 xmlAttributePtr attrDecl;
2317
2318 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00002319 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
2320 /*
2321 * TODO: this sucks ... recomputing this every time is stupid
2322 */
2323 int len = xmlStrlen(elem->name) + xmlStrlen(elem->ns->prefix) + 2;
2324 xmlChar *fullname;
2325
2326 fullname = xmlMalloc(len);
2327 if (fullname == NULL)
2328 return(0);
2329 snprintf((char *) fullname, len, "%s:%s", (char *) elem->ns->prefix,
2330 (char *) elem->name);
2331 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
2332 attr->name);
2333 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2334 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
2335 attr->name);
2336 xmlFree(fullname);
2337 } else {
2338 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
2339 attr->name);
2340 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2341 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
2342 attr->name);
2343 }
Owen Taylor3473f882001-02-23 17:55:21 +00002344
2345 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2346 return(1);
2347 }
2348 return(0);
2349}
2350
2351/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002352 * xmlRemoveID:
Owen Taylor3473f882001-02-23 17:55:21 +00002353 * @doc: the document
2354 * @attr: the attribute
2355 *
2356 * Remove the given attribute from the ID table maintained internally.
2357 *
2358 * Returns -1 if the lookup failed and 0 otherwise
2359 */
2360int
2361xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2362 xmlAttrPtr cur;
2363 xmlIDTablePtr table;
2364 xmlChar *ID;
2365
2366 if (doc == NULL) return(-1);
2367 if (attr == NULL) return(-1);
2368 table = (xmlIDTablePtr) doc->ids;
2369 if (table == NULL)
2370 return(-1);
2371
2372 if (attr == NULL)
2373 return(-1);
2374 ID = xmlNodeListGetString(doc, attr->children, 1);
2375 if (ID == NULL)
2376 return(-1);
2377 cur = xmlHashLookup(table, ID);
2378 if (cur != attr) {
2379 xmlFree(ID);
2380 return(-1);
2381 }
2382 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
2383 xmlFree(ID);
2384 return(0);
2385}
2386
2387/**
2388 * xmlGetID:
2389 * @doc: pointer to the document
2390 * @ID: the ID value
2391 *
2392 * Search the attribute declaring the given ID
2393 *
2394 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2395 */
2396xmlAttrPtr
2397xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2398 xmlIDTablePtr table;
2399 xmlIDPtr id;
2400
2401 if (doc == NULL) {
2402 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
2403 return(NULL);
2404 }
2405
2406 if (ID == NULL) {
2407 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
2408 return(NULL);
2409 }
2410
2411 table = (xmlIDTablePtr) doc->ids;
2412 if (table == NULL)
2413 return(NULL);
2414
2415 id = xmlHashLookup(table, ID);
2416 if (id == NULL)
2417 return(NULL);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002418 if (id->attr == NULL) {
2419 /*
2420 * We are operating on a stream, return a well known reference
2421 * since the attribute node doesn't exist anymore
2422 */
2423 return((xmlAttrPtr) doc);
2424 }
Owen Taylor3473f882001-02-23 17:55:21 +00002425 return(id->attr);
2426}
2427
2428/************************************************************************
2429 * *
2430 * Refs *
2431 * *
2432 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002433typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002434{
2435 xmlListPtr l;
2436 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002437} xmlRemoveMemo;
2438
2439typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2440
2441typedef struct xmlValidateMemo_t
2442{
2443 xmlValidCtxtPtr ctxt;
2444 const xmlChar *name;
2445} xmlValidateMemo;
2446
2447typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002448
2449/**
2450 * xmlCreateRefTable:
2451 *
2452 * create and initialize an empty ref hash table.
2453 *
2454 * Returns the xmlRefTablePtr just created or NULL in case
2455 * of error.
2456 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002457static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002458xmlCreateRefTable(void) {
2459 return(xmlHashCreate(0));
2460}
2461
2462/**
2463 * xmlFreeRef:
2464 * @lk: A list link
2465 *
2466 * Deallocate the memory used by a ref definition
2467 */
2468static void
2469xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002470 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2471 if (ref == NULL) return;
2472 if (ref->value != NULL)
2473 xmlFree((xmlChar *)ref->value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002474 if (ref->name != NULL)
2475 xmlFree((xmlChar *)ref->name);
Daniel Veillard37721922001-05-04 15:21:12 +00002476 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002477}
2478
2479/**
2480 * xmlFreeRefList:
2481 * @list_ref: A list of references.
2482 *
2483 * Deallocate the memory used by a list of references
2484 */
2485static void
2486xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002487 if (list_ref == NULL) return;
2488 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002489}
2490
2491/**
2492 * xmlWalkRemoveRef:
2493 * @data: Contents of current link
2494 * @user: Value supplied by the user
2495 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002496 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002497 */
2498static int
2499xmlWalkRemoveRef(const void *data, const void *user)
2500{
Daniel Veillard37721922001-05-04 15:21:12 +00002501 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2502 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2503 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002504
Daniel Veillard37721922001-05-04 15:21:12 +00002505 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2506 xmlListRemoveFirst(ref_list, (void *)data);
2507 return 0;
2508 }
2509 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002510}
2511
2512/**
2513 * xmlAddRef:
2514 * @ctxt: the validation context
2515 * @doc: pointer to the document
2516 * @value: the value name
2517 * @attr: the attribute holding the Ref
2518 *
2519 * Register a new ref declaration
2520 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002521 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002522 */
2523xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002524xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002525 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002526 xmlRefPtr ret;
2527 xmlRefTablePtr table;
2528 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002529
Daniel Veillard37721922001-05-04 15:21:12 +00002530 if (doc == NULL) {
2531 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002532 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002533 return(NULL);
2534 }
2535 if (value == NULL) {
2536 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002537 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002538 return(NULL);
2539 }
2540 if (attr == NULL) {
2541 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002542 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002543 return(NULL);
2544 }
Owen Taylor3473f882001-02-23 17:55:21 +00002545
Daniel Veillard37721922001-05-04 15:21:12 +00002546 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002547 * Create the Ref table if needed.
2548 */
Daniel Veillard37721922001-05-04 15:21:12 +00002549 table = (xmlRefTablePtr) doc->refs;
2550 if (table == NULL)
2551 doc->refs = table = xmlCreateRefTable();
2552 if (table == NULL) {
2553 xmlGenericError(xmlGenericErrorContext,
2554 "xmlAddRef: Table creation failed!\n");
2555 return(NULL);
2556 }
Owen Taylor3473f882001-02-23 17:55:21 +00002557
Daniel Veillard37721922001-05-04 15:21:12 +00002558 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2559 if (ret == NULL) {
2560 xmlGenericError(xmlGenericErrorContext,
2561 "xmlAddRef: out of memory\n");
2562 return(NULL);
2563 }
Owen Taylor3473f882001-02-23 17:55:21 +00002564
Daniel Veillard37721922001-05-04 15:21:12 +00002565 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002566 * fill the structure.
2567 */
Daniel Veillard37721922001-05-04 15:21:12 +00002568 ret->value = xmlStrdup(value);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002569 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2570 /*
2571 * Operating in streaming mode, attr is gonna disapear
2572 */
2573 ret->name = xmlStrdup(attr->name);
2574 ret->attr = NULL;
2575 } else {
2576 ret->name = NULL;
2577 ret->attr = attr;
2578 }
2579 ret->lineno = xmlGetLineNo(attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00002580
Daniel Veillard37721922001-05-04 15:21:12 +00002581 /* To add a reference :-
2582 * References are maintained as a list of references,
2583 * Lookup the entry, if no entry create new nodelist
2584 * Add the owning node to the NodeList
2585 * Return the ref
2586 */
Owen Taylor3473f882001-02-23 17:55:21 +00002587
Daniel Veillard37721922001-05-04 15:21:12 +00002588 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2589 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2590 xmlGenericError(xmlGenericErrorContext,
2591 "xmlAddRef: Reference list creation failed!\n");
2592 return(NULL);
2593 }
2594 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2595 xmlListDelete(ref_list);
2596 xmlGenericError(xmlGenericErrorContext,
2597 "xmlAddRef: Reference list insertion failed!\n");
2598 return(NULL);
2599 }
2600 }
2601 xmlListInsert(ref_list, ret);
2602 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002603}
2604
2605/**
2606 * xmlFreeRefTable:
2607 * @table: An ref table
2608 *
2609 * Deallocate the memory used by an Ref hash table.
2610 */
2611void
2612xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002613 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002614}
2615
2616/**
2617 * xmlIsRef:
2618 * @doc: the document
2619 * @elem: the element carrying the attribute
2620 * @attr: the attribute
2621 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002622 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002623 * then this is simple, otherwise we use an heuristic: name Ref (upper
2624 * or lowercase).
2625 *
2626 * Returns 0 or 1 depending on the lookup result
2627 */
2628int
2629xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002630 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2631 return(0);
2632 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2633 /* TODO @@@ */
2634 return(0);
2635 } else {
2636 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002637
Daniel Veillard37721922001-05-04 15:21:12 +00002638 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2639 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2640 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2641 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002642
Daniel Veillard37721922001-05-04 15:21:12 +00002643 if ((attrDecl != NULL) &&
2644 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2645 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2646 return(1);
2647 }
2648 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002649}
2650
2651/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002652 * xmlRemoveRef:
Owen Taylor3473f882001-02-23 17:55:21 +00002653 * @doc: the document
2654 * @attr: the attribute
2655 *
2656 * Remove the given attribute from the Ref table maintained internally.
2657 *
2658 * Returns -1 if the lookup failed and 0 otherwise
2659 */
2660int
2661xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002662 xmlListPtr ref_list;
2663 xmlRefTablePtr table;
2664 xmlChar *ID;
2665 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002666
Daniel Veillard37721922001-05-04 15:21:12 +00002667 if (doc == NULL) return(-1);
2668 if (attr == NULL) return(-1);
2669 table = (xmlRefTablePtr) doc->refs;
2670 if (table == NULL)
2671 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002672
Daniel Veillard37721922001-05-04 15:21:12 +00002673 if (attr == NULL)
2674 return(-1);
2675 ID = xmlNodeListGetString(doc, attr->children, 1);
2676 if (ID == NULL)
2677 return(-1);
2678 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002679
Daniel Veillard37721922001-05-04 15:21:12 +00002680 if(ref_list == NULL) {
2681 xmlFree(ID);
2682 return (-1);
2683 }
2684 /* At this point, ref_list refers to a list of references which
2685 * have the same key as the supplied attr. Our list of references
2686 * is ordered by reference address and we don't have that information
2687 * here to use when removing. We'll have to walk the list and
2688 * check for a matching attribute, when we find one stop the walk
2689 * and remove the entry.
2690 * The list is ordered by reference, so that means we don't have the
2691 * key. Passing the list and the reference to the walker means we
2692 * will have enough data to be able to remove the entry.
2693 */
2694 target.l = ref_list;
2695 target.ap = attr;
2696
2697 /* Remove the supplied attr from our list */
2698 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002699
Daniel Veillard37721922001-05-04 15:21:12 +00002700 /*If the list is empty then remove the list entry in the hash */
2701 if (xmlListEmpty(ref_list))
2702 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2703 xmlFreeRefList);
2704 xmlFree(ID);
2705 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002706}
2707
2708/**
2709 * xmlGetRefs:
2710 * @doc: pointer to the document
2711 * @ID: the ID value
2712 *
2713 * Find the set of references for the supplied ID.
2714 *
2715 * Returns NULL if not found, otherwise node set for the ID.
2716 */
2717xmlListPtr
2718xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002719 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002720
Daniel Veillard37721922001-05-04 15:21:12 +00002721 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002722 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002723 return(NULL);
2724 }
Owen Taylor3473f882001-02-23 17:55:21 +00002725
Daniel Veillard37721922001-05-04 15:21:12 +00002726 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002727 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002728 return(NULL);
2729 }
Owen Taylor3473f882001-02-23 17:55:21 +00002730
Daniel Veillard37721922001-05-04 15:21:12 +00002731 table = (xmlRefTablePtr) doc->refs;
2732 if (table == NULL)
2733 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002734
Daniel Veillard37721922001-05-04 15:21:12 +00002735 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002736}
2737
2738/************************************************************************
2739 * *
2740 * Routines for validity checking *
2741 * *
2742 ************************************************************************/
2743
2744/**
2745 * xmlGetDtdElementDesc:
2746 * @dtd: a pointer to the DtD to search
2747 * @name: the element name
2748 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002749 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002750 *
2751 * returns the xmlElementPtr if found or NULL
2752 */
2753
2754xmlElementPtr
2755xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2756 xmlElementTablePtr table;
2757 xmlElementPtr cur;
2758 xmlChar *uqname = NULL, *prefix = NULL;
2759
2760 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002761 if (dtd->elements == NULL)
2762 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002763 table = (xmlElementTablePtr) dtd->elements;
2764
2765 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002766 if (uqname != NULL)
2767 name = uqname;
2768 cur = xmlHashLookup2(table, name, prefix);
2769 if (prefix != NULL) xmlFree(prefix);
2770 if (uqname != NULL) xmlFree(uqname);
2771 return(cur);
2772}
2773/**
2774 * xmlGetDtdElementDesc2:
2775 * @dtd: a pointer to the DtD to search
2776 * @name: the element name
2777 * @create: create an empty description if not found
2778 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002779 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002780 *
2781 * returns the xmlElementPtr if found or NULL
2782 */
2783
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002784static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002785xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2786 xmlElementTablePtr table;
2787 xmlElementPtr cur;
2788 xmlChar *uqname = NULL, *prefix = NULL;
2789
2790 if (dtd == NULL) return(NULL);
2791 if (dtd->elements == NULL) {
2792 if (!create)
2793 return(NULL);
2794 /*
2795 * Create the Element table if needed.
2796 */
2797 table = (xmlElementTablePtr) dtd->elements;
2798 if (table == NULL) {
2799 table = xmlCreateElementTable();
2800 dtd->elements = (void *) table;
2801 }
2802 if (table == NULL) {
2803 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002804 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002805 return(NULL);
2806 }
2807 }
2808 table = (xmlElementTablePtr) dtd->elements;
2809
2810 uqname = xmlSplitQName2(name, &prefix);
2811 if (uqname != NULL)
2812 name = uqname;
2813 cur = xmlHashLookup2(table, name, prefix);
2814 if ((cur == NULL) && (create)) {
2815 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2816 if (cur == NULL) {
2817 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002818 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002819 return(NULL);
2820 }
2821 memset(cur, 0, sizeof(xmlElement));
2822 cur->type = XML_ELEMENT_DECL;
2823
2824 /*
2825 * fill the structure.
2826 */
2827 cur->name = xmlStrdup(name);
2828 cur->prefix = xmlStrdup(prefix);
2829 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2830
2831 xmlHashAddEntry2(table, name, prefix, cur);
2832 }
2833 if (prefix != NULL) xmlFree(prefix);
2834 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002835 return(cur);
2836}
2837
2838/**
2839 * xmlGetDtdQElementDesc:
2840 * @dtd: a pointer to the DtD to search
2841 * @name: the element name
2842 * @prefix: the element namespace prefix
2843 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002844 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002845 *
2846 * returns the xmlElementPtr if found or NULL
2847 */
2848
Daniel Veillard48da9102001-08-07 01:10:10 +00002849xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002850xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2851 const xmlChar *prefix) {
2852 xmlElementTablePtr table;
2853
2854 if (dtd == NULL) return(NULL);
2855 if (dtd->elements == NULL) return(NULL);
2856 table = (xmlElementTablePtr) dtd->elements;
2857
2858 return(xmlHashLookup2(table, name, prefix));
2859}
2860
2861/**
2862 * xmlGetDtdAttrDesc:
2863 * @dtd: a pointer to the DtD to search
2864 * @elem: the element name
2865 * @name: the attribute name
2866 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002867 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002868 * this element.
2869 *
2870 * returns the xmlAttributePtr if found or NULL
2871 */
2872
2873xmlAttributePtr
2874xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2875 xmlAttributeTablePtr table;
2876 xmlAttributePtr cur;
2877 xmlChar *uqname = NULL, *prefix = NULL;
2878
2879 if (dtd == NULL) return(NULL);
2880 if (dtd->attributes == NULL) return(NULL);
2881
2882 table = (xmlAttributeTablePtr) dtd->attributes;
2883 if (table == NULL)
2884 return(NULL);
2885
2886 uqname = xmlSplitQName2(name, &prefix);
2887
2888 if (uqname != NULL) {
2889 cur = xmlHashLookup3(table, uqname, prefix, elem);
2890 if (prefix != NULL) xmlFree(prefix);
2891 if (uqname != NULL) xmlFree(uqname);
2892 } else
2893 cur = xmlHashLookup3(table, name, NULL, elem);
2894 return(cur);
2895}
2896
2897/**
2898 * xmlGetDtdQAttrDesc:
2899 * @dtd: a pointer to the DtD to search
2900 * @elem: the element name
2901 * @name: the attribute name
2902 * @prefix: the attribute namespace prefix
2903 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002904 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002905 * this element.
2906 *
2907 * returns the xmlAttributePtr if found or NULL
2908 */
2909
Daniel Veillard48da9102001-08-07 01:10:10 +00002910xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002911xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2912 const xmlChar *prefix) {
2913 xmlAttributeTablePtr table;
2914
2915 if (dtd == NULL) return(NULL);
2916 if (dtd->attributes == NULL) return(NULL);
2917 table = (xmlAttributeTablePtr) dtd->attributes;
2918
2919 return(xmlHashLookup3(table, name, prefix, elem));
2920}
2921
2922/**
2923 * xmlGetDtdNotationDesc:
2924 * @dtd: a pointer to the DtD to search
2925 * @name: the notation name
2926 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002927 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002928 *
2929 * returns the xmlNotationPtr if found or NULL
2930 */
2931
2932xmlNotationPtr
2933xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2934 xmlNotationTablePtr table;
2935
2936 if (dtd == NULL) return(NULL);
2937 if (dtd->notations == NULL) return(NULL);
2938 table = (xmlNotationTablePtr) dtd->notations;
2939
2940 return(xmlHashLookup(table, name));
2941}
2942
2943/**
2944 * xmlValidateNotationUse:
2945 * @ctxt: the validation context
2946 * @doc: the document
2947 * @notationName: the notation name to check
2948 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002949 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002950 * - [ VC: Notation Declared ]
2951 *
2952 * returns 1 if valid or 0 otherwise
2953 */
2954
2955int
2956xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2957 const xmlChar *notationName) {
2958 xmlNotationPtr notaDecl;
2959 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2960
2961 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2962 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2963 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2964
2965 if (notaDecl == NULL) {
2966 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2967 notationName);
2968 return(0);
2969 }
2970 return(1);
2971}
2972
2973/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002974 * xmlIsMixedElement:
Owen Taylor3473f882001-02-23 17:55:21 +00002975 * @doc: the document
2976 * @name: the element name
2977 *
2978 * Search in the DtDs whether an element accept Mixed content (or ANY)
2979 * basically if it is supposed to accept text childs
2980 *
2981 * returns 0 if no, 1 if yes, and -1 if no element description is available
2982 */
2983
2984int
2985xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2986 xmlElementPtr elemDecl;
2987
2988 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2989
2990 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2991 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2992 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2993 if (elemDecl == NULL) return(-1);
2994 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002995 case XML_ELEMENT_TYPE_UNDEFINED:
2996 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002997 case XML_ELEMENT_TYPE_ELEMENT:
2998 return(0);
2999 case XML_ELEMENT_TYPE_EMPTY:
3000 /*
3001 * return 1 for EMPTY since we want VC error to pop up
3002 * on <empty> </empty> for example
3003 */
3004 case XML_ELEMENT_TYPE_ANY:
3005 case XML_ELEMENT_TYPE_MIXED:
3006 return(1);
3007 }
3008 return(1);
3009}
3010
3011/**
3012 * xmlValidateNameValue:
3013 * @value: an Name value
3014 *
3015 * Validate that the given value match Name production
3016 *
3017 * returns 1 if valid or 0 otherwise
3018 */
3019
Daniel Veillard9b731d72002-04-14 12:56:08 +00003020int
Owen Taylor3473f882001-02-23 17:55:21 +00003021xmlValidateNameValue(const xmlChar *value) {
3022 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003023 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003024
3025 if (value == NULL) return(0);
3026 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003027 val = xmlStringCurrentChar(NULL, cur, &len);
3028 cur += len;
3029 if (!IS_LETTER(val) && (val != '_') &&
3030 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003031 return(0);
3032 }
3033
Daniel Veillardd8224e02002-01-13 15:43:22 +00003034 val = xmlStringCurrentChar(NULL, cur, &len);
3035 cur += len;
3036 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3037 (val == '.') || (val == '-') ||
3038 (val == '_') || (val == ':') ||
3039 (IS_COMBINING(val)) ||
3040 (IS_EXTENDER(val))) {
3041 val = xmlStringCurrentChar(NULL, cur, &len);
3042 cur += len;
3043 }
Owen Taylor3473f882001-02-23 17:55:21 +00003044
Daniel Veillardd8224e02002-01-13 15:43:22 +00003045 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003046
3047 return(1);
3048}
3049
3050/**
3051 * xmlValidateNamesValue:
3052 * @value: an Names value
3053 *
3054 * Validate that the given value match Names production
3055 *
3056 * returns 1 if valid or 0 otherwise
3057 */
3058
Daniel Veillard9b731d72002-04-14 12:56:08 +00003059int
Owen Taylor3473f882001-02-23 17:55:21 +00003060xmlValidateNamesValue(const xmlChar *value) {
3061 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003062 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003063
3064 if (value == NULL) return(0);
3065 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003066 val = xmlStringCurrentChar(NULL, cur, &len);
3067 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003068
Daniel Veillardd8224e02002-01-13 15:43:22 +00003069 if (!IS_LETTER(val) && (val != '_') &&
3070 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003071 return(0);
3072 }
3073
Daniel Veillardd8224e02002-01-13 15:43:22 +00003074 val = xmlStringCurrentChar(NULL, cur, &len);
3075 cur += len;
3076 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3077 (val == '.') || (val == '-') ||
3078 (val == '_') || (val == ':') ||
3079 (IS_COMBINING(val)) ||
3080 (IS_EXTENDER(val))) {
3081 val = xmlStringCurrentChar(NULL, cur, &len);
3082 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003083 }
3084
Daniel Veillardd8224e02002-01-13 15:43:22 +00003085 while (IS_BLANK(val)) {
3086 while (IS_BLANK(val)) {
3087 val = xmlStringCurrentChar(NULL, cur, &len);
3088 cur += len;
3089 }
3090
3091 if (!IS_LETTER(val) && (val != '_') &&
3092 (val != ':')) {
3093 return(0);
3094 }
3095 val = xmlStringCurrentChar(NULL, cur, &len);
3096 cur += len;
3097
3098 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3099 (val == '.') || (val == '-') ||
3100 (val == '_') || (val == ':') ||
3101 (IS_COMBINING(val)) ||
3102 (IS_EXTENDER(val))) {
3103 val = xmlStringCurrentChar(NULL, cur, &len);
3104 cur += len;
3105 }
3106 }
3107
3108 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003109
3110 return(1);
3111}
3112
3113/**
3114 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003115 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00003116 *
3117 * Validate that the given value match Nmtoken production
3118 *
3119 * [ VC: Name Token ]
3120 *
3121 * returns 1 if valid or 0 otherwise
3122 */
3123
Daniel Veillard9b731d72002-04-14 12:56:08 +00003124int
Owen Taylor3473f882001-02-23 17:55:21 +00003125xmlValidateNmtokenValue(const xmlChar *value) {
3126 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003127 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003128
3129 if (value == NULL) return(0);
3130 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003131 val = xmlStringCurrentChar(NULL, cur, &len);
3132 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003133
Daniel Veillardd8224e02002-01-13 15:43:22 +00003134 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3135 (val != '.') && (val != '-') &&
3136 (val != '_') && (val != ':') &&
3137 (!IS_COMBINING(val)) &&
3138 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00003139 return(0);
3140
Daniel Veillardd8224e02002-01-13 15:43:22 +00003141 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3142 (val == '.') || (val == '-') ||
3143 (val == '_') || (val == ':') ||
3144 (IS_COMBINING(val)) ||
3145 (IS_EXTENDER(val))) {
3146 val = xmlStringCurrentChar(NULL, cur, &len);
3147 cur += len;
3148 }
Owen Taylor3473f882001-02-23 17:55:21 +00003149
Daniel Veillardd8224e02002-01-13 15:43:22 +00003150 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003151
3152 return(1);
3153}
3154
3155/**
3156 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003157 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00003158 *
3159 * Validate that the given value match Nmtokens production
3160 *
3161 * [ VC: Name Token ]
3162 *
3163 * returns 1 if valid or 0 otherwise
3164 */
3165
Daniel Veillard9b731d72002-04-14 12:56:08 +00003166int
Owen Taylor3473f882001-02-23 17:55:21 +00003167xmlValidateNmtokensValue(const xmlChar *value) {
3168 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003169 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00003170
3171 if (value == NULL) return(0);
3172 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00003173 val = xmlStringCurrentChar(NULL, cur, &len);
3174 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003175
Daniel Veillardd8224e02002-01-13 15:43:22 +00003176 while (IS_BLANK(val)) {
3177 val = xmlStringCurrentChar(NULL, cur, &len);
3178 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00003179 }
3180
Daniel Veillardd8224e02002-01-13 15:43:22 +00003181 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3182 (val != '.') && (val != '-') &&
3183 (val != '_') && (val != ':') &&
3184 (!IS_COMBINING(val)) &&
3185 (!IS_EXTENDER(val)))
3186 return(0);
3187
3188 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3189 (val == '.') || (val == '-') ||
3190 (val == '_') || (val == ':') ||
3191 (IS_COMBINING(val)) ||
3192 (IS_EXTENDER(val))) {
3193 val = xmlStringCurrentChar(NULL, cur, &len);
3194 cur += len;
3195 }
3196
3197 while (IS_BLANK(val)) {
3198 while (IS_BLANK(val)) {
3199 val = xmlStringCurrentChar(NULL, cur, &len);
3200 cur += len;
3201 }
3202 if (val == 0) return(1);
3203
3204 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
3205 (val != '.') && (val != '-') &&
3206 (val != '_') && (val != ':') &&
3207 (!IS_COMBINING(val)) &&
3208 (!IS_EXTENDER(val)))
3209 return(0);
3210
3211 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
3212 (val == '.') || (val == '-') ||
3213 (val == '_') || (val == ':') ||
3214 (IS_COMBINING(val)) ||
3215 (IS_EXTENDER(val))) {
3216 val = xmlStringCurrentChar(NULL, cur, &len);
3217 cur += len;
3218 }
3219 }
3220
3221 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003222
3223 return(1);
3224}
3225
3226/**
3227 * xmlValidateNotationDecl:
3228 * @ctxt: the validation context
3229 * @doc: a document instance
3230 * @nota: a notation definition
3231 *
3232 * Try to validate a single notation definition
3233 * basically it does the following checks as described by the
3234 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003235 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00003236 * But this function get called anyway ...
3237 *
3238 * returns 1 if valid or 0 otherwise
3239 */
3240
3241int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003242xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3243 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003244 int ret = 1;
3245
3246 return(ret);
3247}
3248
3249/**
3250 * xmlValidateAttributeValue:
3251 * @type: an attribute type
3252 * @value: an attribute value
3253 *
3254 * Validate that the given attribute value match the proper production
3255 *
3256 * [ VC: ID ]
3257 * Values of type ID must match the Name production....
3258 *
3259 * [ VC: IDREF ]
3260 * Values of type IDREF must match the Name production, and values
3261 * of type IDREFS must match Names ...
3262 *
3263 * [ VC: Entity Name ]
3264 * Values of type ENTITY must match the Name production, values
3265 * of type ENTITIES must match Names ...
3266 *
3267 * [ VC: Name Token ]
3268 * Values of type NMTOKEN must match the Nmtoken production; values
3269 * of type NMTOKENS must match Nmtokens.
3270 *
3271 * returns 1 if valid or 0 otherwise
3272 */
3273
3274int
3275xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3276 switch (type) {
3277 case XML_ATTRIBUTE_ENTITIES:
3278 case XML_ATTRIBUTE_IDREFS:
3279 return(xmlValidateNamesValue(value));
3280 case XML_ATTRIBUTE_ENTITY:
3281 case XML_ATTRIBUTE_IDREF:
3282 case XML_ATTRIBUTE_ID:
3283 case XML_ATTRIBUTE_NOTATION:
3284 return(xmlValidateNameValue(value));
3285 case XML_ATTRIBUTE_NMTOKENS:
3286 case XML_ATTRIBUTE_ENUMERATION:
3287 return(xmlValidateNmtokensValue(value));
3288 case XML_ATTRIBUTE_NMTOKEN:
3289 return(xmlValidateNmtokenValue(value));
3290 case XML_ATTRIBUTE_CDATA:
3291 break;
3292 }
3293 return(1);
3294}
3295
3296/**
3297 * xmlValidateAttributeValue2:
3298 * @ctxt: the validation context
3299 * @doc: the document
3300 * @name: the attribute name (used for error reporting only)
3301 * @type: the attribute type
3302 * @value: the attribute value
3303 *
3304 * Validate that the given attribute value match a given type.
3305 * This typically cannot be done before having finished parsing
3306 * the subsets.
3307 *
3308 * [ VC: IDREF ]
3309 * Values of type IDREF must match one of the declared IDs
3310 * Values of type IDREFS must match a sequence of the declared IDs
3311 * each Name must match the value of an ID attribute on some element
3312 * in the XML document; i.e. IDREF values must match the value of
3313 * some ID attribute
3314 *
3315 * [ VC: Entity Name ]
3316 * Values of type ENTITY must match one declared entity
3317 * Values of type ENTITIES must match a sequence of declared entities
3318 *
3319 * [ VC: Notation Attributes ]
3320 * all notation names in the declaration must be declared.
3321 *
3322 * returns 1 if valid or 0 otherwise
3323 */
3324
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003325static int
Owen Taylor3473f882001-02-23 17:55:21 +00003326xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3327 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3328 int ret = 1;
3329 switch (type) {
3330 case XML_ATTRIBUTE_IDREFS:
3331 case XML_ATTRIBUTE_IDREF:
3332 case XML_ATTRIBUTE_ID:
3333 case XML_ATTRIBUTE_NMTOKENS:
3334 case XML_ATTRIBUTE_ENUMERATION:
3335 case XML_ATTRIBUTE_NMTOKEN:
3336 case XML_ATTRIBUTE_CDATA:
3337 break;
3338 case XML_ATTRIBUTE_ENTITY: {
3339 xmlEntityPtr ent;
3340
3341 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00003342 if ((ent == NULL) && (doc->standalone == 1)) {
3343 doc->standalone = 0;
3344 ent = xmlGetDocEntity(doc, value);
3345 if (ent != NULL) {
3346 VERROR(ctxt->userData,
3347"standalone problem: attribute %s reference entity \"%s\" in external subset\n",
3348 name, value);
3349 /* WAIT to get answer from the Core WG on this
3350 ret = 0;
3351 */
3352 }
3353 }
Owen Taylor3473f882001-02-23 17:55:21 +00003354 if (ent == NULL) {
3355 VERROR(ctxt->userData,
3356 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3357 name, value);
3358 ret = 0;
3359 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3360 VERROR(ctxt->userData,
3361 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3362 name, value);
3363 ret = 0;
3364 }
3365 break;
3366 }
3367 case XML_ATTRIBUTE_ENTITIES: {
3368 xmlChar *dup, *nam = NULL, *cur, save;
3369 xmlEntityPtr ent;
3370
3371 dup = xmlStrdup(value);
3372 if (dup == NULL)
3373 return(0);
3374 cur = dup;
3375 while (*cur != 0) {
3376 nam = cur;
3377 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
3378 save = *cur;
3379 *cur = 0;
3380 ent = xmlGetDocEntity(doc, nam);
3381 if (ent == NULL) {
3382 VERROR(ctxt->userData,
3383 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3384 name, nam);
3385 ret = 0;
3386 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3387 VERROR(ctxt->userData,
3388 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3389 name, nam);
3390 ret = 0;
3391 }
3392 if (save == 0)
3393 break;
3394 *cur = save;
3395 while (IS_BLANK(*cur)) cur++;
3396 }
3397 xmlFree(dup);
3398 break;
3399 }
3400 case XML_ATTRIBUTE_NOTATION: {
3401 xmlNotationPtr nota;
3402
3403 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3404 if ((nota == NULL) && (doc->extSubset != NULL))
3405 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3406
3407 if (nota == NULL) {
3408 VERROR(ctxt->userData,
3409 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3410 name, value);
3411 ret = 0;
3412 }
3413 break;
3414 }
3415 }
3416 return(ret);
3417}
3418
3419/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003420 * xmlValidCtxtNormalizeAttributeValue:
3421 * @ctxt: the validation context
3422 * @doc: the document
3423 * @elem: the parent
3424 * @name: the attribute name
3425 * @value: the attribute value
3426 * @ctxt: the validation context or NULL
3427 *
3428 * Does the validation related extra step of the normalization of attribute
3429 * values:
3430 *
3431 * If the declared value is not CDATA, then the XML processor must further
3432 * process the normalized attribute value by discarding any leading and
3433 * trailing space (#x20) characters, and by replacing sequences of space
3434 * (#x20) characters by single space (#x20) character.
3435 *
3436 * Also check VC: Standalone Document Declaration in P32, and update
3437 * ctxt->valid accordingly
3438 *
3439 * returns a new normalized string if normalization is needed, NULL otherwise
3440 * the caller must free the returned value.
3441 */
3442
3443xmlChar *
3444xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3445 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3446 xmlChar *ret, *dst;
3447 const xmlChar *src;
3448 xmlAttributePtr attrDecl = NULL;
3449 int extsubset = 0;
3450
3451 if (doc == NULL) return(NULL);
3452 if (elem == NULL) return(NULL);
3453 if (name == NULL) return(NULL);
3454 if (value == NULL) return(NULL);
3455
3456 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3457 xmlChar qname[500];
3458 snprintf((char *) qname, sizeof(qname), "%s:%s",
3459 elem->ns->prefix, elem->name);
3460 qname[sizeof(qname) - 1] = 0;
3461 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3462 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3463 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3464 if (attrDecl != NULL)
3465 extsubset = 1;
3466 }
3467 }
3468 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3469 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3470 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3471 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3472 if (attrDecl != NULL)
3473 extsubset = 1;
3474 }
3475
3476 if (attrDecl == NULL)
3477 return(NULL);
3478 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3479 return(NULL);
3480
3481 ret = xmlStrdup(value);
3482 if (ret == NULL)
3483 return(NULL);
3484 src = value;
3485 dst = ret;
3486 while (*src == 0x20) src++;
3487 while (*src != 0) {
3488 if (*src == 0x20) {
3489 while (*src == 0x20) src++;
3490 if (*src != 0)
3491 *dst++ = 0x20;
3492 } else {
3493 *dst++ = *src++;
3494 }
3495 }
3496 *dst = 0;
3497 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3498 VERROR(ctxt->userData,
3499"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3500 name, elem->name);
3501 ctxt->valid = 0;
3502 }
3503 return(ret);
3504}
3505
3506/**
Owen Taylor3473f882001-02-23 17:55:21 +00003507 * xmlValidNormalizeAttributeValue:
3508 * @doc: the document
3509 * @elem: the parent
3510 * @name: the attribute name
3511 * @value: the attribute value
3512 *
3513 * Does the validation related extra step of the normalization of attribute
3514 * values:
3515 *
3516 * If the declared value is not CDATA, then the XML processor must further
3517 * process the normalized attribute value by discarding any leading and
3518 * trailing space (#x20) characters, and by replacing sequences of space
3519 * (#x20) characters by single space (#x20) character.
3520 *
3521 * returns a new normalized string if normalization is needed, NULL otherwise
3522 * the caller must free the returned value.
3523 */
3524
3525xmlChar *
3526xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3527 const xmlChar *name, const xmlChar *value) {
3528 xmlChar *ret, *dst;
3529 const xmlChar *src;
3530 xmlAttributePtr attrDecl = NULL;
3531
3532 if (doc == NULL) return(NULL);
3533 if (elem == NULL) return(NULL);
3534 if (name == NULL) return(NULL);
3535 if (value == NULL) return(NULL);
3536
3537 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3538 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003539 snprintf((char *) qname, sizeof(qname), "%s:%s",
3540 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003541 qname[sizeof(qname) - 1] = 0;
3542 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3543 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3544 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3545 }
3546 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3547 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3548 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3549
3550 if (attrDecl == NULL)
3551 return(NULL);
3552 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3553 return(NULL);
3554
3555 ret = xmlStrdup(value);
3556 if (ret == NULL)
3557 return(NULL);
3558 src = value;
3559 dst = ret;
3560 while (*src == 0x20) src++;
3561 while (*src != 0) {
3562 if (*src == 0x20) {
3563 while (*src == 0x20) src++;
3564 if (*src != 0)
3565 *dst++ = 0x20;
3566 } else {
3567 *dst++ = *src++;
3568 }
3569 }
3570 *dst = 0;
3571 return(ret);
3572}
3573
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003574static void
Owen Taylor3473f882001-02-23 17:55:21 +00003575xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003576 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003577 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3578}
3579
3580/**
3581 * xmlValidateAttributeDecl:
3582 * @ctxt: the validation context
3583 * @doc: a document instance
3584 * @attr: an attribute definition
3585 *
3586 * Try to validate a single attribute definition
3587 * basically it does the following checks as described by the
3588 * XML-1.0 recommendation:
3589 * - [ VC: Attribute Default Legal ]
3590 * - [ VC: Enumeration ]
3591 * - [ VC: ID Attribute Default ]
3592 *
3593 * The ID/IDREF uniqueness and matching are done separately
3594 *
3595 * returns 1 if valid or 0 otherwise
3596 */
3597
3598int
3599xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3600 xmlAttributePtr attr) {
3601 int ret = 1;
3602 int val;
3603 CHECK_DTD;
3604 if(attr == NULL) return(1);
3605
3606 /* Attribute Default Legal */
3607 /* Enumeration */
3608 if (attr->defaultValue != NULL) {
3609 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3610 if (val == 0) {
3611 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003612 "Syntax of default value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003613 attr->name, attr->elem);
3614 }
3615 ret &= val;
3616 }
3617
3618 /* ID Attribute Default */
3619 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3620 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3621 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3622 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003623 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003624 attr->name, attr->elem);
3625 ret = 0;
3626 }
3627
3628 /* One ID per Element Type */
3629 if (attr->atype == XML_ATTRIBUTE_ID) {
3630 int nbId;
3631
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003632 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003633 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3634 attr->elem);
3635 if (elem != NULL) {
3636 nbId = xmlScanIDAttributeDecl(NULL, elem);
3637 } else {
3638 xmlAttributeTablePtr table;
3639
3640 /*
3641 * The attribute may be declared in the internal subset and the
3642 * element in the external subset.
3643 */
3644 nbId = 0;
3645 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3646 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3647 xmlValidateAttributeIdCallback, &nbId);
3648 }
3649 if (nbId > 1) {
3650 VERROR(ctxt->userData,
3651 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3652 attr->elem, nbId, attr->name);
3653 } else if (doc->extSubset != NULL) {
3654 int extId = 0;
3655 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3656 if (elem != NULL) {
3657 extId = xmlScanIDAttributeDecl(NULL, elem);
3658 }
3659 if (extId > 1) {
3660 VERROR(ctxt->userData,
3661 "Element %s has %d ID attribute defined in the external subset : %s\n",
3662 attr->elem, extId, attr->name);
3663 } else if (extId + nbId > 1) {
3664 VERROR(ctxt->userData,
3665"Element %s has ID attributes defined in the internal and external subset : %s\n",
3666 attr->elem, attr->name);
3667 }
3668 }
3669 }
3670
3671 /* Validity Constraint: Enumeration */
3672 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3673 xmlEnumerationPtr tree = attr->tree;
3674 while (tree != NULL) {
3675 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3676 tree = tree->next;
3677 }
3678 if (tree == NULL) {
3679 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003680"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003681 attr->defaultValue, attr->name, attr->elem);
3682 ret = 0;
3683 }
3684 }
3685
3686 return(ret);
3687}
3688
3689/**
3690 * xmlValidateElementDecl:
3691 * @ctxt: the validation context
3692 * @doc: a document instance
3693 * @elem: an element definition
3694 *
3695 * Try to validate a single element definition
3696 * basically it does the following checks as described by the
3697 * XML-1.0 recommendation:
3698 * - [ VC: One ID per Element Type ]
3699 * - [ VC: No Duplicate Types ]
3700 * - [ VC: Unique Element Type Declaration ]
3701 *
3702 * returns 1 if valid or 0 otherwise
3703 */
3704
3705int
3706xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3707 xmlElementPtr elem) {
3708 int ret = 1;
3709 xmlElementPtr tst;
3710
3711 CHECK_DTD;
3712
3713 if (elem == NULL) return(1);
3714
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003715#if 0
Daniel Veillard84d70a42002-09-16 10:51:38 +00003716#ifdef LIBXML_REGEXP_ENABLED
3717 /* Build the regexp associated to the content model */
3718 ret = xmlValidBuildContentModel(ctxt, elem);
3719#endif
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00003720#endif
Daniel Veillard84d70a42002-09-16 10:51:38 +00003721
Owen Taylor3473f882001-02-23 17:55:21 +00003722 /* No Duplicate Types */
3723 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3724 xmlElementContentPtr cur, next;
3725 const xmlChar *name;
3726
3727 cur = elem->content;
3728 while (cur != NULL) {
3729 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3730 if (cur->c1 == NULL) break;
3731 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3732 name = cur->c1->name;
3733 next = cur->c2;
3734 while (next != NULL) {
3735 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3736 if (xmlStrEqual(next->name, name)) {
3737 VERROR(ctxt->userData,
3738 "Definition of %s has duplicate references of %s\n",
3739 elem->name, name);
3740 ret = 0;
3741 }
3742 break;
3743 }
3744 if (next->c1 == NULL) break;
3745 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3746 if (xmlStrEqual(next->c1->name, name)) {
3747 VERROR(ctxt->userData,
3748 "Definition of %s has duplicate references of %s\n",
3749 elem->name, name);
3750 ret = 0;
3751 }
3752 next = next->c2;
3753 }
3754 }
3755 cur = cur->c2;
3756 }
3757 }
3758
3759 /* VC: Unique Element Type Declaration */
3760 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003761 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003762 ((tst->prefix == elem->prefix) ||
3763 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003764 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003765 VERROR(ctxt->userData, "Redefinition of element %s\n",
3766 elem->name);
3767 ret = 0;
3768 }
3769 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003770 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003771 ((tst->prefix == elem->prefix) ||
3772 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003773 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003774 VERROR(ctxt->userData, "Redefinition of element %s\n",
3775 elem->name);
3776 ret = 0;
3777 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003778 /* One ID per Element Type
3779 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003780 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3781 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003782 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003783 return(ret);
3784}
3785
3786/**
3787 * xmlValidateOneAttribute:
3788 * @ctxt: the validation context
3789 * @doc: a document instance
3790 * @elem: an element instance
3791 * @attr: an attribute instance
3792 * @value: the attribute value (without entities processing)
3793 *
3794 * Try to validate a single attribute for an element
3795 * basically it does the following checks as described by the
3796 * XML-1.0 recommendation:
3797 * - [ VC: Attribute Value Type ]
3798 * - [ VC: Fixed Attribute Default ]
3799 * - [ VC: Entity Name ]
3800 * - [ VC: Name Token ]
3801 * - [ VC: ID ]
3802 * - [ VC: IDREF ]
3803 * - [ VC: Entity Name ]
3804 * - [ VC: Notation Attributes ]
3805 *
3806 * The ID/IDREF uniqueness and matching are done separately
3807 *
3808 * returns 1 if valid or 0 otherwise
3809 */
3810
3811int
3812xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3813 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3814 /* xmlElementPtr elemDecl; */
3815 xmlAttributePtr attrDecl = NULL;
3816 int val;
3817 int ret = 1;
3818
3819 CHECK_DTD;
3820 if ((elem == NULL) || (elem->name == NULL)) return(0);
3821 if ((attr == NULL) || (attr->name == NULL)) return(0);
3822
3823 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3824 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003825 snprintf((char *) qname, sizeof(qname), "%s:%s",
3826 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003827 qname[sizeof(qname) - 1] = 0;
3828 if (attr->ns != NULL) {
3829 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3830 attr->name, attr->ns->prefix);
3831 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3832 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3833 attr->name, attr->ns->prefix);
3834 } else {
3835 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3836 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3837 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3838 qname, attr->name);
3839 }
3840 }
3841 if (attrDecl == NULL) {
3842 if (attr->ns != NULL) {
3843 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3844 attr->name, attr->ns->prefix);
3845 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3846 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3847 attr->name, attr->ns->prefix);
3848 } else {
3849 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3850 elem->name, attr->name);
3851 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3852 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3853 elem->name, attr->name);
3854 }
3855 }
3856
3857
3858 /* Validity Constraint: Attribute Value Type */
3859 if (attrDecl == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003860 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003861 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003862 "No declaration for attribute %s of element %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003863 attr->name, elem->name);
3864 return(0);
3865 }
3866 attr->atype = attrDecl->atype;
3867
3868 val = xmlValidateAttributeValue(attrDecl->atype, value);
3869 if (val == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003870 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003871 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003872 "Syntax of value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003873 attr->name, elem->name);
3874 ret = 0;
3875 }
3876
3877 /* Validity constraint: Fixed Attribute Default */
3878 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3879 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003880 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003881 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003882 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003883 attr->name, elem->name, attrDecl->defaultValue);
3884 ret = 0;
3885 }
3886 }
3887
3888 /* Validity Constraint: ID uniqueness */
3889 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3890 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3891 ret = 0;
3892 }
3893
3894 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3895 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3896 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3897 ret = 0;
3898 }
3899
3900 /* Validity Constraint: Notation Attributes */
3901 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3902 xmlEnumerationPtr tree = attrDecl->tree;
3903 xmlNotationPtr nota;
3904
3905 /* First check that the given NOTATION was declared */
3906 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3907 if (nota == NULL)
3908 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3909
3910 if (nota == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003911 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003912 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003913 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003914 value, attr->name, elem->name);
3915 ret = 0;
3916 }
3917
3918 /* Second, verify that it's among the list */
3919 while (tree != NULL) {
3920 if (xmlStrEqual(tree->name, value)) break;
3921 tree = tree->next;
3922 }
3923 if (tree == 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 among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003927 value, attr->name, elem->name);
3928 ret = 0;
3929 }
3930 }
3931
3932 /* Validity Constraint: Enumeration */
3933 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3934 xmlEnumerationPtr tree = attrDecl->tree;
3935 while (tree != NULL) {
3936 if (xmlStrEqual(tree->name, value)) break;
3937 tree = tree->next;
3938 }
3939 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003940 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003941 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003942 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003943 value, attr->name, elem->name);
3944 ret = 0;
3945 }
3946 }
3947
3948 /* Fixed Attribute Default */
3949 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3950 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003951 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003952 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003953 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003954 attr->name, elem->name, attrDecl->defaultValue);
3955 ret = 0;
3956 }
3957
3958 /* Extra check for the attribute value */
3959 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3960 attrDecl->atype, value);
3961
3962 return(ret);
3963}
3964
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003965/**
3966 * xmlValidateOneNamespace:
3967 * @ctxt: the validation context
3968 * @doc: a document instance
3969 * @elem: an element instance
Daniel Veillarda9b66d02002-12-11 14:23:49 +00003970 * @prefix: the namespace prefix
Daniel Veillard90d68fb2002-09-26 16:10:21 +00003971 * @ns: an namespace declaration instance
3972 * @value: the attribute value (without entities processing)
3973 *
3974 * Try to validate a single namespace declaration for an element
3975 * basically it does the following checks as described by the
3976 * XML-1.0 recommendation:
3977 * - [ VC: Attribute Value Type ]
3978 * - [ VC: Fixed Attribute Default ]
3979 * - [ VC: Entity Name ]
3980 * - [ VC: Name Token ]
3981 * - [ VC: ID ]
3982 * - [ VC: IDREF ]
3983 * - [ VC: Entity Name ]
3984 * - [ VC: Notation Attributes ]
3985 *
3986 * The ID/IDREF uniqueness and matching are done separately
3987 *
3988 * returns 1 if valid or 0 otherwise
3989 */
3990
3991int
3992xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3993xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
3994 /* xmlElementPtr elemDecl; */
3995 xmlAttributePtr attrDecl = NULL;
3996 int val;
3997 int ret = 1;
3998
3999 CHECK_DTD;
4000 if ((elem == NULL) || (elem->name == NULL)) return(0);
4001 if ((ns == NULL) || (ns->href == NULL)) return(0);
4002
4003 if (prefix != NULL) {
4004 xmlChar qname[500];
4005 snprintf((char *) qname, sizeof(qname), "%s:%s",
4006 prefix, elem->name);
4007 qname[sizeof(qname) - 1] = 0;
4008 if (ns->prefix != NULL) {
4009 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
4010 ns->prefix, BAD_CAST "xmlns");
4011 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4012 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
4013 ns->prefix, BAD_CAST "xmlns");
4014 } else {
4015 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname,
4016 BAD_CAST "xmlns");
4017 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4018 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname,
4019 BAD_CAST "xmlns");
4020 }
4021 }
4022 if (attrDecl == NULL) {
4023 if (ns->prefix != NULL) {
4024 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4025 ns->prefix, BAD_CAST "xmlns");
4026 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4027 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4028 ns->prefix, BAD_CAST "xmlns");
4029 } else {
4030 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4031 elem->name, BAD_CAST "xmlns");
4032 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4033 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4034 elem->name, BAD_CAST "xmlns");
4035 }
4036 }
4037
4038
4039 /* Validity Constraint: Attribute Value Type */
4040 if (attrDecl == NULL) {
4041 VECTXT(ctxt, elem);
4042 if (ns->prefix != NULL) {
4043 VERROR(ctxt->userData,
4044 "No declaration for attribute xmlns:%s of element %s\n",
4045 ns->prefix, elem->name);
4046 } else {
4047 VERROR(ctxt->userData,
4048 "No declaration for attribute xmlns of element %s\n",
4049 elem->name);
4050 }
4051 return(0);
4052 }
4053
4054 val = xmlValidateAttributeValue(attrDecl->atype, value);
4055 if (val == 0) {
4056 VECTXT(ctxt, elem);
4057 if (ns->prefix != NULL) {
4058 VERROR(ctxt->userData,
4059 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4060 ns->prefix, elem->name);
4061 } else {
4062 VERROR(ctxt->userData,
4063 "Syntax of value for attribute xmlns of %s is not valid\n",
4064 elem->name);
4065 }
4066 ret = 0;
4067 }
4068
4069 /* Validity constraint: Fixed Attribute Default */
4070 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4071 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4072 VECTXT(ctxt, elem);
4073 if (ns->prefix != NULL) {
4074 VERROR(ctxt->userData,
4075 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4076 ns->prefix, elem->name, attrDecl->defaultValue);
4077 } else {
4078 VERROR(ctxt->userData,
4079 "Value for attribute xmlns of %s is different from default \"%s\"\n",
4080 elem->name, attrDecl->defaultValue);
4081 }
4082 ret = 0;
4083 }
4084 }
4085
4086 /* Validity Constraint: ID uniqueness */
4087 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4088 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4089 ret = 0;
4090 }
4091
4092 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4093 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4094 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4095 ret = 0;
4096 }
4097
4098 /* Validity Constraint: Notation Attributes */
4099 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4100 xmlEnumerationPtr tree = attrDecl->tree;
4101 xmlNotationPtr nota;
4102
4103 /* First check that the given NOTATION was declared */
4104 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4105 if (nota == NULL)
4106 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4107
4108 if (nota == NULL) {
4109 VECTXT(ctxt, elem);
4110 if (ns->prefix != NULL) {
4111 VERROR(ctxt->userData,
4112 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4113 value, ns->prefix, elem->name);
4114 } else {
4115 VERROR(ctxt->userData,
4116 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4117 value, elem->name);
4118 }
4119 ret = 0;
4120 }
4121
4122 /* Second, verify that it's among the list */
4123 while (tree != NULL) {
4124 if (xmlStrEqual(tree->name, value)) break;
4125 tree = tree->next;
4126 }
4127 if (tree == NULL) {
4128 VECTXT(ctxt, elem);
4129 if (ns->prefix != NULL) {
4130 VERROR(ctxt->userData,
4131"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4132 value, ns->prefix, elem->name);
4133 } else {
4134 VERROR(ctxt->userData,
4135"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4136 value, elem->name);
4137 }
4138 ret = 0;
4139 }
4140 }
4141
4142 /* Validity Constraint: Enumeration */
4143 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4144 xmlEnumerationPtr tree = attrDecl->tree;
4145 while (tree != NULL) {
4146 if (xmlStrEqual(tree->name, value)) break;
4147 tree = tree->next;
4148 }
4149 if (tree == NULL) {
4150 VECTXT(ctxt, elem);
4151 if (ns->prefix != NULL) {
4152 VERROR(ctxt->userData,
4153"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4154 value, ns->prefix, elem->name);
4155 } else {
4156 VERROR(ctxt->userData,
4157"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4158 value, elem->name);
4159 }
4160 ret = 0;
4161 }
4162 }
4163
4164 /* Fixed Attribute Default */
4165 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4166 (!xmlStrEqual(attrDecl->defaultValue, value))) {
4167 VECTXT(ctxt, elem);
4168 if (ns->prefix != NULL) {
4169 VERROR(ctxt->userData,
4170 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4171 ns->prefix, elem->name, attrDecl->defaultValue);
4172 } else {
4173 VERROR(ctxt->userData,
4174 "Value for attribute xmlns of %s must be \"%s\"\n",
4175 elem->name, attrDecl->defaultValue);
4176 }
4177 ret = 0;
4178 }
4179
4180 /* Extra check for the attribute value */
4181 if (ns->prefix != NULL) {
4182 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4183 attrDecl->atype, value);
4184 } else {
4185 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4186 attrDecl->atype, value);
4187 }
4188
4189 return(ret);
4190}
4191
Daniel Veillard118aed72002-09-24 14:13:13 +00004192#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004193/**
4194 * xmlValidateSkipIgnorable:
4195 * @ctxt: the validation context
4196 * @child: the child list
4197 *
4198 * Skip ignorable elements w.r.t. the validation process
4199 *
4200 * returns the first element to consider for validation of the content model
4201 */
4202
4203static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004204xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004205 while (child != NULL) {
4206 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004207 /* These things are ignored (skipped) during validation. */
4208 case XML_PI_NODE:
4209 case XML_COMMENT_NODE:
4210 case XML_XINCLUDE_START:
4211 case XML_XINCLUDE_END:
4212 child = child->next;
4213 break;
4214 case XML_TEXT_NODE:
4215 if (xmlIsBlankNode(child))
4216 child = child->next;
4217 else
4218 return(child);
4219 break;
4220 /* keep current node */
4221 default:
4222 return(child);
4223 }
4224 }
4225 return(child);
4226}
4227
4228/**
4229 * xmlValidateElementType:
4230 * @ctxt: the validation context
4231 *
4232 * Try to validate the content model of an element internal function
4233 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004234 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4235 * reference is found and -3 if the validation succeeded but
4236 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004237 */
4238
4239static int
4240xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004241 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004242 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004243
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004244 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004245 if ((NODE == NULL) && (CONT == NULL))
4246 return(1);
4247 if ((NODE == NULL) &&
4248 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4249 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4250 return(1);
4251 }
4252 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00004253 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004254 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004255
4256 /*
4257 * We arrive here when more states need to be examined
4258 */
4259cont:
4260
4261 /*
4262 * We just recovered from a rollback generated by a possible
4263 * epsilon transition, go directly to the analysis phase
4264 */
4265 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004266 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004267 DEBUG_VALID_STATE(NODE, CONT)
4268 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004269 goto analyze;
4270 }
4271
4272 DEBUG_VALID_STATE(NODE, CONT)
4273 /*
4274 * we may have to save a backup state here. This is the equivalent
4275 * of handling epsilon transition in NFAs.
4276 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00004277 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004278 ((CONT->parent == NULL) ||
4279 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004280 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00004281 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00004282 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004283 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004284 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4285 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004286 }
4287
4288
4289 /*
4290 * Check first if the content matches
4291 */
4292 switch (CONT->type) {
4293 case XML_ELEMENT_CONTENT_PCDATA:
4294 if (NODE == NULL) {
4295 DEBUG_VALID_MSG("pcdata failed no node");
4296 ret = 0;
4297 break;
4298 }
4299 if (NODE->type == XML_TEXT_NODE) {
4300 DEBUG_VALID_MSG("pcdata found, skip to next");
4301 /*
4302 * go to next element in the content model
4303 * skipping ignorable elems
4304 */
4305 do {
4306 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004307 NODE = xmlValidateSkipIgnorable(NODE);
4308 if ((NODE != NULL) &&
4309 (NODE->type == XML_ENTITY_REF_NODE))
4310 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004311 } while ((NODE != NULL) &&
4312 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004313 (NODE->type != XML_TEXT_NODE) &&
4314 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004315 ret = 1;
4316 break;
4317 } else {
4318 DEBUG_VALID_MSG("pcdata failed");
4319 ret = 0;
4320 break;
4321 }
4322 break;
4323 case XML_ELEMENT_CONTENT_ELEMENT:
4324 if (NODE == NULL) {
4325 DEBUG_VALID_MSG("element failed no node");
4326 ret = 0;
4327 break;
4328 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00004329 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4330 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004331 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004332 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4333 ret = (CONT->prefix == NULL);
4334 } else if (CONT->prefix == NULL) {
4335 ret = 0;
4336 } else {
4337 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4338 }
4339 }
4340 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004341 DEBUG_VALID_MSG("element found, skip to next");
4342 /*
4343 * go to next element in the content model
4344 * skipping ignorable elems
4345 */
4346 do {
4347 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004348 NODE = xmlValidateSkipIgnorable(NODE);
4349 if ((NODE != NULL) &&
4350 (NODE->type == XML_ENTITY_REF_NODE))
4351 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004352 } while ((NODE != NULL) &&
4353 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004354 (NODE->type != XML_TEXT_NODE) &&
4355 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004356 } else {
4357 DEBUG_VALID_MSG("element failed");
4358 ret = 0;
4359 break;
4360 }
4361 break;
4362 case XML_ELEMENT_CONTENT_OR:
4363 /*
Daniel Veillard85349052001-04-20 13:48:21 +00004364 * Small optimization.
4365 */
4366 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4367 if ((NODE == NULL) ||
4368 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4369 DEPTH++;
4370 CONT = CONT->c2;
4371 goto cont;
4372 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004373 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4374 ret = (CONT->c1->prefix == NULL);
4375 } else if (CONT->c1->prefix == NULL) {
4376 ret = 0;
4377 } else {
4378 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4379 }
4380 if (ret == 0) {
4381 DEPTH++;
4382 CONT = CONT->c2;
4383 goto cont;
4384 }
Daniel Veillard85349052001-04-20 13:48:21 +00004385 }
4386
4387 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004388 * save the second branch 'or' branch
4389 */
4390 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00004391 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4392 OCCURS, ROLLBACK_OR) < 0)
4393 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004394 DEPTH++;
4395 CONT = CONT->c1;
4396 goto cont;
4397 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00004398 /*
4399 * Small optimization.
4400 */
4401 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4402 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4403 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4404 if ((NODE == NULL) ||
4405 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4406 DEPTH++;
4407 CONT = CONT->c2;
4408 goto cont;
4409 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004410 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4411 ret = (CONT->c1->prefix == NULL);
4412 } else if (CONT->c1->prefix == NULL) {
4413 ret = 0;
4414 } else {
4415 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4416 }
4417 if (ret == 0) {
4418 DEPTH++;
4419 CONT = CONT->c2;
4420 goto cont;
4421 }
Daniel Veillard1d047672001-06-09 16:41:01 +00004422 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004423 DEPTH++;
4424 CONT = CONT->c1;
4425 goto cont;
4426 }
4427
4428 /*
4429 * At this point handle going up in the tree
4430 */
4431 if (ret == -1) {
4432 DEBUG_VALID_MSG("error found returning");
4433 return(ret);
4434 }
4435analyze:
4436 while (CONT != NULL) {
4437 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004438 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004439 * this level.
4440 */
4441 if (ret == 0) {
4442 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004443 xmlNodePtr cur;
4444
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004445 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004446 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004447 DEBUG_VALID_MSG("Once branch failed, rollback");
4448 if (vstateVPop(ctxt) < 0 ) {
4449 DEBUG_VALID_MSG("exhaustion, failed");
4450 return(0);
4451 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004452 if (cur != ctxt->vstate->node)
4453 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004454 goto cont;
4455 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00004456 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004457 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004458 DEBUG_VALID_MSG("Plus branch failed, rollback");
4459 if (vstateVPop(ctxt) < 0 ) {
4460 DEBUG_VALID_MSG("exhaustion, failed");
4461 return(0);
4462 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004463 if (cur != ctxt->vstate->node)
4464 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004465 goto cont;
4466 }
4467 DEBUG_VALID_MSG("Plus branch found");
4468 ret = 1;
4469 break;
4470 case XML_ELEMENT_CONTENT_MULT:
4471#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00004472 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004473 DEBUG_VALID_MSG("Mult branch failed");
4474 } else {
4475 DEBUG_VALID_MSG("Mult branch found");
4476 }
4477#endif
4478 ret = 1;
4479 break;
4480 case XML_ELEMENT_CONTENT_OPT:
4481 DEBUG_VALID_MSG("Option branch failed");
4482 ret = 1;
4483 break;
4484 }
4485 } else {
4486 switch (CONT->ocur) {
4487 case XML_ELEMENT_CONTENT_OPT:
4488 DEBUG_VALID_MSG("Option branch succeeded");
4489 ret = 1;
4490 break;
4491 case XML_ELEMENT_CONTENT_ONCE:
4492 DEBUG_VALID_MSG("Once branch succeeded");
4493 ret = 1;
4494 break;
4495 case XML_ELEMENT_CONTENT_PLUS:
4496 if (STATE == ROLLBACK_PARENT) {
4497 DEBUG_VALID_MSG("Plus branch rollback");
4498 ret = 1;
4499 break;
4500 }
4501 if (NODE == NULL) {
4502 DEBUG_VALID_MSG("Plus branch exhausted");
4503 ret = 1;
4504 break;
4505 }
4506 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004507 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004508 goto cont;
4509 case XML_ELEMENT_CONTENT_MULT:
4510 if (STATE == ROLLBACK_PARENT) {
4511 DEBUG_VALID_MSG("Mult branch rollback");
4512 ret = 1;
4513 break;
4514 }
4515 if (NODE == NULL) {
4516 DEBUG_VALID_MSG("Mult branch exhausted");
4517 ret = 1;
4518 break;
4519 }
4520 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00004521 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004522 goto cont;
4523 }
4524 }
4525 STATE = 0;
4526
4527 /*
4528 * Then act accordingly at the parent level
4529 */
Daniel Veillard5344c602001-12-31 16:37:34 +00004530 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004531 if (CONT->parent == NULL)
4532 break;
4533
4534 switch (CONT->parent->type) {
4535 case XML_ELEMENT_CONTENT_PCDATA:
4536 DEBUG_VALID_MSG("Error: parent pcdata");
4537 return(-1);
4538 case XML_ELEMENT_CONTENT_ELEMENT:
4539 DEBUG_VALID_MSG("Error: parent element");
4540 return(-1);
4541 case XML_ELEMENT_CONTENT_OR:
4542 if (ret == 1) {
4543 DEBUG_VALID_MSG("Or succeeded");
4544 CONT = CONT->parent;
4545 DEPTH--;
4546 } else {
4547 DEBUG_VALID_MSG("Or failed");
4548 CONT = CONT->parent;
4549 DEPTH--;
4550 }
4551 break;
4552 case XML_ELEMENT_CONTENT_SEQ:
4553 if (ret == 0) {
4554 DEBUG_VALID_MSG("Sequence failed");
4555 CONT = CONT->parent;
4556 DEPTH--;
4557 } else if (CONT == CONT->parent->c1) {
4558 DEBUG_VALID_MSG("Sequence testing 2nd branch");
4559 CONT = CONT->parent->c2;
4560 goto cont;
4561 } else {
4562 DEBUG_VALID_MSG("Sequence succeeded");
4563 CONT = CONT->parent;
4564 DEPTH--;
4565 }
4566 }
4567 }
4568 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004569 xmlNodePtr cur;
4570
4571 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004572 DEBUG_VALID_MSG("Failed, remaining input, rollback");
4573 if (vstateVPop(ctxt) < 0 ) {
4574 DEBUG_VALID_MSG("exhaustion, failed");
4575 return(0);
4576 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004577 if (cur != ctxt->vstate->node)
4578 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004579 goto cont;
4580 }
4581 if (ret == 0) {
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("Failure, 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 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004594 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004595}
Daniel Veillard23e73572002-09-19 19:56:43 +00004596#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004597
4598/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00004599 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004600 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00004601 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004602 * @content: An element
4603 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
4604 *
4605 * This will dump the list of elements to the buffer
4606 * Intended just for the debug routine
4607 */
4608static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00004609xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004610 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00004611 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004612
4613 if (node == NULL) return;
4614 if (glob) strcat(buf, "(");
4615 cur = node;
4616 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004617 len = strlen(buf);
4618 if (size - len < 50) {
4619 if ((size - len > 4) && (buf[len - 1] != '.'))
4620 strcat(buf, " ...");
4621 return;
4622 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004623 switch (cur->type) {
4624 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004625 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004626 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004627 if ((size - len > 4) && (buf[len - 1] != '.'))
4628 strcat(buf, " ...");
4629 return;
4630 }
4631 strcat(buf, (char *) cur->ns->prefix);
4632 strcat(buf, ":");
4633 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004634 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004635 if ((size - len > 4) && (buf[len - 1] != '.'))
4636 strcat(buf, " ...");
4637 return;
4638 }
4639 strcat(buf, (char *) cur->name);
4640 if (cur->next != NULL)
4641 strcat(buf, " ");
4642 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004643 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004644 if (xmlIsBlankNode(cur))
4645 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004646 case XML_CDATA_SECTION_NODE:
4647 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004648 strcat(buf, "CDATA");
4649 if (cur->next != NULL)
4650 strcat(buf, " ");
4651 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004652 case XML_ATTRIBUTE_NODE:
4653 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004654#ifdef LIBXML_DOCB_ENABLED
4655 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004656#endif
4657 case XML_HTML_DOCUMENT_NODE:
4658 case XML_DOCUMENT_TYPE_NODE:
4659 case XML_DOCUMENT_FRAG_NODE:
4660 case XML_NOTATION_NODE:
4661 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004662 strcat(buf, "???");
4663 if (cur->next != NULL)
4664 strcat(buf, " ");
4665 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004666 case XML_ENTITY_NODE:
4667 case XML_PI_NODE:
4668 case XML_DTD_NODE:
4669 case XML_COMMENT_NODE:
4670 case XML_ELEMENT_DECL:
4671 case XML_ATTRIBUTE_DECL:
4672 case XML_ENTITY_DECL:
4673 case XML_XINCLUDE_START:
4674 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004675 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004676 }
4677 cur = cur->next;
4678 }
4679 if (glob) strcat(buf, ")");
4680}
4681
4682/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004683 * xmlValidateElementContent:
4684 * @ctxt: the validation context
4685 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004686 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004687 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004688 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004689 *
4690 * Try to validate the content model of an element
4691 *
4692 * returns 1 if valid or 0 if not and -1 in case of error
4693 */
4694
4695static int
4696xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004697 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillard5acfd6b2002-09-18 16:29:02 +00004698 int ret = 1;
Daniel Veillard23e73572002-09-19 19:56:43 +00004699#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard01992e02002-10-09 10:20:30 +00004700 xmlNodePtr repl = NULL, last = NULL, tmp;
Daniel Veillard23e73572002-09-19 19:56:43 +00004701#endif
Daniel Veillard01992e02002-10-09 10:20:30 +00004702 xmlNodePtr cur;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004703 xmlElementContentPtr cont;
4704 const xmlChar *name;
4705
4706 if (elemDecl == NULL)
4707 return(-1);
4708 cont = elemDecl->content;
4709 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004710
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004711#ifdef LIBXML_REGEXP_ENABLED
4712 /* Build the regexp associated to the content model */
4713 if (elemDecl->contModel == NULL)
4714 ret = xmlValidBuildContentModel(ctxt, elemDecl);
4715 if (elemDecl->contModel == NULL) {
4716 ret = -1;
4717 } else {
4718 xmlRegExecCtxtPtr exec;
4719
Daniel Veillard01992e02002-10-09 10:20:30 +00004720 ctxt->nodeMax = 0;
4721 ctxt->nodeNr = 0;
4722 ctxt->nodeTab = NULL;
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004723 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
4724 if (exec != NULL) {
4725 cur = child;
4726 while (cur != NULL) {
4727 switch (cur->type) {
4728 case XML_ENTITY_REF_NODE:
4729 /*
4730 * Push the current node to be able to roll back
4731 * and process within the entity
4732 */
4733 if ((cur->children != NULL) &&
4734 (cur->children->children != NULL)) {
4735 nodeVPush(ctxt, cur);
4736 cur = cur->children->children;
4737 continue;
4738 }
4739 break;
4740 case XML_TEXT_NODE:
4741 if (xmlIsBlankNode(cur))
4742 break;
4743 ret = 0;
4744 goto fail;
4745 case XML_CDATA_SECTION_NODE:
Daniel Veillardea7751d2002-12-20 00:16:24 +00004746 /* TODO */
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004747 ret = 0;
4748 goto fail;
4749 case XML_ELEMENT_NODE:
4750 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
4751 xmlChar *QName;
4752 int len;
4753
4754 len = xmlStrlen(cur->name) +
4755 xmlStrlen(cur->ns->prefix) + 2;
4756 QName = xmlMalloc(len);
4757 if (QName == NULL) {
4758 ret = -1;
4759 goto fail;
4760 }
4761 snprintf((char *) QName, len, "%s:%s",
4762 (char *)cur->ns->prefix,
4763 (char *)cur->name);
4764 ret = xmlRegExecPushString(exec, QName, NULL);
4765 xmlFree(QName);
4766 } else {
4767 ret = xmlRegExecPushString(exec, cur->name, NULL);
4768 }
4769 break;
4770 default:
4771 break;
4772 }
4773 /*
4774 * Switch to next element
4775 */
4776 cur = cur->next;
4777 while (cur == NULL) {
4778 cur = nodeVPop(ctxt);
4779 if (cur == NULL)
4780 break;
4781 cur = cur->next;
4782 }
4783 }
4784 ret = xmlRegExecPushString(exec, NULL, NULL);
4785fail:
4786 xmlRegFreeExecCtxt(exec);
4787 }
4788 }
4789#else /* LIBXML_REGEXP_ENABLED */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004790 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004791 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004792 */
4793 ctxt->vstateMax = 8;
4794 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4795 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4796 if (ctxt->vstateTab == NULL) {
4797 xmlGenericError(xmlGenericErrorContext,
4798 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004799 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004800 }
4801 /*
4802 * The first entry in the stack is reserved to the current state
4803 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004804 ctxt->nodeMax = 0;
4805 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004806 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004807 ctxt->vstate = &ctxt->vstateTab[0];
4808 ctxt->vstateNr = 1;
4809 CONT = cont;
4810 NODE = child;
4811 DEPTH = 0;
4812 OCCURS = 0;
4813 STATE = 0;
4814 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004815 if ((ret == -3) && (warn)) {
4816 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004817 "Content model for Element %s is ambiguous\n", name);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004818 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004819 /*
4820 * An entities reference appeared at this level.
4821 * Buid a minimal representation of this node content
4822 * sufficient to run the validation process on it
4823 */
4824 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004825 cur = child;
4826 while (cur != NULL) {
4827 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004828 case XML_ENTITY_REF_NODE:
4829 /*
4830 * Push the current node to be able to roll back
4831 * and process within the entity
4832 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004833 if ((cur->children != NULL) &&
4834 (cur->children->children != NULL)) {
4835 nodeVPush(ctxt, cur);
4836 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004837 continue;
4838 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004839 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004840 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004841 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004842 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004843 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004844 case XML_CDATA_SECTION_NODE:
4845 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004846 case XML_ELEMENT_NODE:
4847 /*
4848 * Allocate a new node and minimally fills in
4849 * what's required
4850 */
4851 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4852 if (tmp == NULL) {
4853 xmlGenericError(xmlGenericErrorContext,
4854 "xmlValidateElementContent : malloc failed\n");
4855 xmlFreeNodeList(repl);
4856 ret = -1;
4857 goto done;
4858 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004859 tmp->type = cur->type;
4860 tmp->name = cur->name;
4861 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004862 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004863 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004864 if (repl == NULL)
4865 repl = last = tmp;
4866 else {
4867 last->next = tmp;
4868 last = tmp;
4869 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004870 if (cur->type == XML_CDATA_SECTION_NODE) {
4871 /*
4872 * E59 spaces in CDATA does not match the
4873 * nonterminal S
4874 */
4875 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4876 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004877 break;
4878 default:
4879 break;
4880 }
4881 /*
4882 * Switch to next element
4883 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004884 cur = cur->next;
4885 while (cur == NULL) {
4886 cur = nodeVPop(ctxt);
4887 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004888 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004889 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004890 }
4891 }
4892
4893 /*
4894 * Relaunch the validation
4895 */
4896 ctxt->vstate = &ctxt->vstateTab[0];
4897 ctxt->vstateNr = 1;
4898 CONT = cont;
4899 NODE = repl;
4900 DEPTH = 0;
4901 OCCURS = 0;
4902 STATE = 0;
4903 ret = xmlValidateElementType(ctxt);
4904 }
Daniel Veillarda646cfd2002-09-17 21:50:03 +00004905#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004906 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004907 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4908 char expr[5000];
4909 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004910
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004911 expr[0] = 0;
4912 xmlSnprintfElementContent(expr, 5000, cont, 1);
4913 list[0] = 0;
Daniel Veillard01992e02002-10-09 10:20:30 +00004914#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004915 if (repl != NULL)
4916 xmlSnprintfElements(list, 5000, repl, 1);
4917 else
Daniel Veillard01992e02002-10-09 10:20:30 +00004918#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004919 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004920
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004921 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004922 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004923 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004924 "Element %s content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004925 name, expr, list);
4926 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004927 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004928 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004929 "Element content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004930 expr, list);
4931 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004932 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004933 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004934 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004935 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004936 "Element %s content does not follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004937 name);
4938 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004939 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004940 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004941 "Element content does not follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004942 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004943 }
4944 ret = 0;
4945 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004946 if (ret == -3)
4947 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004948
Daniel Veillard23e73572002-09-19 19:56:43 +00004949#ifndef LIBXML_REGEXP_ENABLED
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004950done:
4951 /*
4952 * Deallocate the copy if done, and free up the validation stack
4953 */
4954 while (repl != NULL) {
4955 tmp = repl->next;
4956 xmlFree(repl);
4957 repl = tmp;
4958 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004959 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004960 if (ctxt->vstateTab != NULL) {
4961 xmlFree(ctxt->vstateTab);
4962 ctxt->vstateTab = NULL;
4963 }
Daniel Veillard01992e02002-10-09 10:20:30 +00004964#endif
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004965 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004966 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004967 if (ctxt->nodeTab != NULL) {
4968 xmlFree(ctxt->nodeTab);
4969 ctxt->nodeTab = NULL;
4970 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004971 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004972
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004973}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004974
Owen Taylor3473f882001-02-23 17:55:21 +00004975/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004976 * xmlValidateCdataElement:
4977 * @ctxt: the validation context
4978 * @doc: a document instance
4979 * @elem: an element instance
4980 *
4981 * Check that an element follows #CDATA
4982 *
4983 * returns 1 if valid or 0 otherwise
4984 */
4985static int
4986xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4987 xmlNodePtr elem) {
4988 int ret = 1;
4989 xmlNodePtr cur, child;
4990
Daniel Veillardceb09b92002-10-04 11:46:37 +00004991 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004992 return(0);
4993
4994 child = elem->children;
4995
4996 cur = child;
4997 while (cur != NULL) {
4998 switch (cur->type) {
4999 case XML_ENTITY_REF_NODE:
5000 /*
5001 * Push the current node to be able to roll back
5002 * and process within the entity
5003 */
5004 if ((cur->children != NULL) &&
5005 (cur->children->children != NULL)) {
5006 nodeVPush(ctxt, cur);
5007 cur = cur->children->children;
5008 continue;
5009 }
5010 break;
5011 case XML_COMMENT_NODE:
5012 case XML_PI_NODE:
5013 case XML_TEXT_NODE:
5014 case XML_CDATA_SECTION_NODE:
5015 break;
5016 default:
5017 ret = 0;
5018 goto done;
5019 }
5020 /*
5021 * Switch to next element
5022 */
5023 cur = cur->next;
5024 while (cur == NULL) {
5025 cur = nodeVPop(ctxt);
5026 if (cur == NULL)
5027 break;
5028 cur = cur->next;
5029 }
5030 }
5031done:
5032 ctxt->nodeMax = 0;
5033 ctxt->nodeNr = 0;
5034 if (ctxt->nodeTab != NULL) {
5035 xmlFree(ctxt->nodeTab);
5036 ctxt->nodeTab = NULL;
5037 }
5038 return(ret);
5039}
5040
5041/**
Daniel Veillardea7751d2002-12-20 00:16:24 +00005042 * xmlValidateCheckMixed:
5043 * @ctxt: the validation context
5044 * @cont: the mixed content model
5045 * @qname: the qualified name as appearing in the serialization
5046 *
5047 * Check if the given node is part of the content model.
5048 *
5049 * Returns 1 if yes, 0 if no, -1 in case of error
5050 */
5051static int
5052xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
5053 xmlElementContentPtr cont, const xmlChar *qname) {
5054 while (cont != NULL) {
5055 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5056 if (xmlStrEqual(cont->name, qname))
5057 return(1);
5058 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5059 (cont->c1 != NULL) &&
5060 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5061 if (xmlStrEqual(cont->c1->name, qname))
5062 return(1);
5063 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5064 (cont->c1 == NULL) ||
5065 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5066 /* Internal error !!! */
5067 xmlGenericError(xmlGenericErrorContext,
5068 "Internal: MIXED struct bad\n");
5069 break;
5070 }
5071 cont = cont->c2;
5072 }
5073 return(0);
5074}
5075
5076/**
5077 * xmlValidGetElemDecl:
5078 * @ctxt: the validation context
5079 * @doc: a document instance
5080 * @elem: an element instance
5081 * @extsubset: pointer, (out) indicate if the declaration was found
5082 * in the external subset.
5083 *
5084 * Finds a declaration associated to an element in the document.
5085 *
5086 * returns the pointer to the declaration or NULL if not found.
5087 */
5088static xmlElementPtr
5089xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5090 xmlNodePtr elem, int *extsubset) {
5091 xmlElementPtr elemDecl = NULL;
5092 const xmlChar *prefix = NULL;
5093
5094 if ((elem == NULL) || (elem->name == NULL)) return(NULL);
5095 if (extsubset != NULL)
5096 *extsubset = 0;
5097
5098 /*
5099 * Fetch the declaration for the qualified name
5100 */
5101 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5102 prefix = elem->ns->prefix;
5103
5104 if (prefix != NULL) {
5105 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5106 elem->name, prefix);
5107 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5108 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5109 elem->name, prefix);
5110 if ((elemDecl != NULL) && (extsubset != NULL))
5111 *extsubset = 1;
5112 }
5113 }
5114
5115 /*
5116 * Fetch the declaration for the non qualified name
5117 * This is "non-strict" validation should be done on the
5118 * full QName but in that case being flexible makes sense.
5119 */
5120 if (elemDecl == NULL) {
5121 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5122 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5123 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5124 if ((elemDecl != NULL) && (extsubset != NULL))
5125 *extsubset = 1;
5126 }
5127 }
5128 if (elemDecl == NULL) {
5129 VECTXT(ctxt, elem);
5130 VERROR(ctxt->userData, "No declaration for element %s\n",
5131 elem->name);
5132 }
5133 return(elemDecl);
5134}
5135
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005136#ifdef LIBXML_REGEXP_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00005137/**
5138 * xmlValidatePushElement:
5139 * @ctxt: the validation context
5140 * @doc: a document instance
5141 * @elem: an element instance
5142 * @qname: the qualified name as appearing in the serialization
5143 *
5144 * Push a new element start on the validation stack.
5145 *
5146 * returns 1 if no validation problem was found or 0 otherwise
5147 */
5148int
5149xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5150 xmlNodePtr elem, const xmlChar *qname) {
5151 int ret = 1;
5152 xmlElementPtr eDecl;
5153 int extsubset = 0;
5154
5155 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5156 xmlValidStatePtr state = ctxt->vstate;
5157 xmlElementPtr elemDecl;
5158
5159 /*
5160 * Check the new element agaisnt the content model of the new elem.
5161 */
5162 if (state->elemDecl != NULL) {
5163 elemDecl = state->elemDecl;
5164
5165 switch(elemDecl->etype) {
5166 case XML_ELEMENT_TYPE_UNDEFINED:
5167 ret = 0;
5168 break;
5169 case XML_ELEMENT_TYPE_EMPTY:
5170 VECTXT(ctxt, state->node);
5171 VERROR(ctxt->userData,
5172 "Element %s was declared EMPTY this one has content\n",
5173 state->node->name);
5174 ret = 0;
5175 break;
5176 case XML_ELEMENT_TYPE_ANY:
5177 /* I don't think anything is required then */
5178 break;
5179 case XML_ELEMENT_TYPE_MIXED:
5180 /* simple case of declared as #PCDATA */
5181 if ((elemDecl->content != NULL) &&
5182 (elemDecl->content->type ==
5183 XML_ELEMENT_CONTENT_PCDATA)) {
5184 VECTXT(ctxt, state->node);
5185 VERROR(ctxt->userData,
5186 "Element %s was declared #PCDATA but contains non text nodes\n",
5187 state->node->name);
5188 ret = 0;
5189 } else {
5190 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5191 qname);
5192 if (ret != 1) {
5193 VECTXT(ctxt, state->node);
5194 VERROR(ctxt->userData,
5195 "Element %s is not declared in %s list of possible children\n",
5196 qname, state->node->name);
5197 }
5198 }
5199 break;
5200 case XML_ELEMENT_TYPE_ELEMENT:
5201 /*
5202 * TODO:
5203 * VC: Standalone Document Declaration
5204 * - element types with element content, if white space
5205 * occurs directly within any instance of those types.
5206 */
5207 if (state->exec != NULL) {
5208 ret = xmlRegExecPushString(state->exec, qname, NULL);
5209 if (ret < 0) {
5210 VECTXT(ctxt, state->node);
5211 VERROR(ctxt->userData,
5212 "Element %s content does not follow the DTD\nMisplaced %s\n",
5213 state->node->name, qname);
5214 ret = 0;
5215 } else {
5216 ret = 1;
5217 }
5218 }
5219 break;
5220 }
5221 }
5222 }
5223 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5224 vstateVPush(ctxt, eDecl, elem);
5225 return(ret);
5226}
5227
5228/**
5229 * xmlValidatePushCData:
5230 * @ctxt: the validation context
5231 * @data: some character data read
5232 * @len: the lenght of the data
5233 *
5234 * check the CData parsed for validation in the current stack
5235 *
5236 * returns 1 if no validation problem was found or 0 otherwise
5237 */
5238int
5239xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5240 int ret = 1;
5241
5242 if (len <= 0)
5243 return(ret);
5244 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5245 xmlValidStatePtr state = ctxt->vstate;
5246 xmlElementPtr elemDecl;
5247
5248 /*
5249 * Check the new element agaisnt the content model of the new elem.
5250 */
5251 if (state->elemDecl != NULL) {
5252 elemDecl = state->elemDecl;
5253
5254 switch(elemDecl->etype) {
5255 case XML_ELEMENT_TYPE_UNDEFINED:
5256 ret = 0;
5257 break;
5258 case XML_ELEMENT_TYPE_EMPTY:
5259 VECTXT(ctxt, state->node);
5260 VERROR(ctxt->userData,
5261 "Element %s was declared EMPTY this one has content\n",
5262 state->node->name);
5263 ret = 0;
5264 break;
5265 case XML_ELEMENT_TYPE_ANY:
5266 break;
5267 case XML_ELEMENT_TYPE_MIXED:
5268 break;
5269 case XML_ELEMENT_TYPE_ELEMENT:
5270 if (len > 0) {
5271 int i;
5272
5273 for (i = 0;i < len;i++) {
5274 if (!IS_BLANK(data[i])) {
5275 VECTXT(ctxt, state->node);
5276 VERROR(ctxt->userData,
5277 "Element %s content does not follow the DTD\nText not allowed\n",
5278 state->node->name);
5279 ret = 0;
5280 goto done;
5281 }
5282 }
5283 /*
5284 * TODO:
5285 * VC: Standalone Document Declaration
5286 * element types with element content, if white space
5287 * occurs directly within any instance of those types.
5288 */
5289 }
5290 break;
5291 }
5292 }
5293 }
5294done:
5295 return(ret);
5296}
5297
5298/**
5299 * xmlValidatePopElement:
5300 * @ctxt: the validation context
5301 * @doc: a document instance
5302 * @elem: an element instance
5303 * @qname: the qualified name as appearing in the serialization
5304 *
5305 * Pop the element end from the validation stack.
5306 *
5307 * returns 1 if no validation problem was found or 0 otherwise
5308 */
5309int
5310xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
5311 xmlNodePtr elem, const xmlChar *qname ATTRIBUTE_UNUSED) {
5312 int ret = 1;
5313
5314 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5315 xmlValidStatePtr state = ctxt->vstate;
5316 xmlElementPtr elemDecl;
5317
5318 /*
5319 * Check the new element agaisnt the content model of the new elem.
5320 */
5321 if (state->elemDecl != NULL) {
5322 elemDecl = state->elemDecl;
5323
5324 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5325 if (state->exec != NULL) {
5326 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5327 if (ret == 0) {
5328 VECTXT(ctxt, state->node);
5329 VERROR(ctxt->userData,
5330 "Element %s content does not follow the DTD\nExpecting more child\n",
5331 state->node->name);
5332 } else {
5333 /*
5334 * previous validation errors should not generate
5335 * a new one here
5336 */
5337 ret = 1;
5338 }
5339 }
5340 }
5341 }
5342 vstateVPop(ctxt);
5343 }
5344 return(ret);
5345}
Daniel Veillard0e298ad2003-02-04 16:14:33 +00005346#endif /* LIBXML_REGEXP_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005347
5348/**
Owen Taylor3473f882001-02-23 17:55:21 +00005349 * xmlValidateOneElement:
5350 * @ctxt: the validation context
5351 * @doc: a document instance
5352 * @elem: an element instance
5353 *
5354 * Try to validate a single element and it's attributes,
5355 * basically it does the following checks as described by the
5356 * XML-1.0 recommendation:
5357 * - [ VC: Element Valid ]
5358 * - [ VC: Required Attribute ]
5359 * Then call xmlValidateOneAttribute() for each attribute present.
5360 *
5361 * The ID/IDREF checkings are done separately
5362 *
5363 * returns 1 if valid or 0 otherwise
5364 */
5365
5366int
5367xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5368 xmlNodePtr elem) {
5369 xmlElementPtr elemDecl = NULL;
5370 xmlElementContentPtr cont;
5371 xmlAttributePtr attr;
5372 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005373 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005374 const xmlChar *name;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005375 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005376
5377 CHECK_DTD;
5378
5379 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005380 switch (elem->type) {
5381 case XML_ATTRIBUTE_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005382 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005383 VERROR(ctxt->userData,
5384 "Attribute element not expected here\n");
5385 return(0);
5386 case XML_TEXT_NODE:
5387 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005388 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005389 VERROR(ctxt->userData, "Text element has childs !\n");
5390 return(0);
5391 }
5392 if (elem->properties != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005393 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005394 VERROR(ctxt->userData, "Text element has attributes !\n");
5395 return(0);
5396 }
5397 if (elem->ns != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005398 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005399 VERROR(ctxt->userData, "Text element has namespace !\n");
5400 return(0);
5401 }
5402 if (elem->nsDef != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005403 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005404 VERROR(ctxt->userData,
5405 "Text element carries namespace definitions !\n");
5406 return(0);
5407 }
5408 if (elem->content == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005409 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005410 VERROR(ctxt->userData,
5411 "Text element has no content !\n");
5412 return(0);
5413 }
5414 return(1);
5415 case XML_XINCLUDE_START:
5416 case XML_XINCLUDE_END:
5417 return(1);
5418 case XML_CDATA_SECTION_NODE:
5419 case XML_ENTITY_REF_NODE:
5420 case XML_PI_NODE:
5421 case XML_COMMENT_NODE:
5422 return(1);
5423 case XML_ENTITY_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005424 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005425 VERROR(ctxt->userData,
5426 "Entity element not expected here\n");
5427 return(0);
5428 case XML_NOTATION_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005429 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005430 VERROR(ctxt->userData,
5431 "Notation element not expected here\n");
5432 return(0);
5433 case XML_DOCUMENT_NODE:
5434 case XML_DOCUMENT_TYPE_NODE:
5435 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005436 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005437 VERROR(ctxt->userData,
5438 "Document element not expected here\n");
5439 return(0);
5440 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005441 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005442 VERROR(ctxt->userData,
5443 "\n");
5444 return(0);
5445 case XML_ELEMENT_NODE:
5446 break;
5447 default:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005448 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005449 VERROR(ctxt->userData,
5450 "unknown element type %d\n", elem->type);
5451 return(0);
5452 }
Owen Taylor3473f882001-02-23 17:55:21 +00005453
5454 /*
Daniel Veillardea7751d2002-12-20 00:16:24 +00005455 * Fetch the declaration
Owen Taylor3473f882001-02-23 17:55:21 +00005456 */
Daniel Veillardea7751d2002-12-20 00:16:24 +00005457 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5458 if (elemDecl == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005459 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005460
Daniel Veillardea7751d2002-12-20 00:16:24 +00005461 /*
5462 * If vstateNr is not zero that means continuous validation is
5463 * activated, do not try to check the content model at that level.
5464 */
5465 if (ctxt->vstateNr == 0) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005466 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00005467 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00005468 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005469 VECTXT(ctxt, elem);
Daniel Veillarda10efa82001-04-18 13:09:01 +00005470 VERROR(ctxt->userData, "No declaration for element %s\n",
5471 elem->name);
5472 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00005473 case XML_ELEMENT_TYPE_EMPTY:
5474 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005475 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005476 VERROR(ctxt->userData,
5477 "Element %s was declared EMPTY this one has content\n",
5478 elem->name);
5479 ret = 0;
5480 }
5481 break;
5482 case XML_ELEMENT_TYPE_ANY:
5483 /* I don't think anything is required then */
5484 break;
5485 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005486
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005487 /* simple case of declared as #PCDATA */
5488 if ((elemDecl->content != NULL) &&
5489 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5490 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5491 if (!ret) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005492 VECTXT(ctxt, elem);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005493 VERROR(ctxt->userData,
5494 "Element %s was declared #PCDATA but contains non text nodes\n",
5495 elem->name);
5496 }
5497 break;
5498 }
Owen Taylor3473f882001-02-23 17:55:21 +00005499 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00005500 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00005501 while (child != NULL) {
5502 if (child->type == XML_ELEMENT_NODE) {
5503 name = child->name;
5504 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
5505 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00005506 snprintf((char *) qname, sizeof(qname), "%s:%s",
5507 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005508 qname[sizeof(qname) - 1] = 0;
5509 cont = elemDecl->content;
5510 while (cont != NULL) {
5511 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5512 if (xmlStrEqual(cont->name, qname)) break;
5513 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5514 (cont->c1 != NULL) &&
5515 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5516 if (xmlStrEqual(cont->c1->name, qname)) break;
5517 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5518 (cont->c1 == NULL) ||
5519 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5520 /* Internal error !!! */
5521 xmlGenericError(xmlGenericErrorContext,
5522 "Internal: MIXED struct bad\n");
5523 break;
5524 }
5525 cont = cont->c2;
5526 }
5527 if (cont != NULL)
5528 goto child_ok;
5529 }
5530 cont = elemDecl->content;
5531 while (cont != NULL) {
5532 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5533 if (xmlStrEqual(cont->name, name)) break;
5534 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5535 (cont->c1 != NULL) &&
5536 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5537 if (xmlStrEqual(cont->c1->name, name)) break;
5538 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5539 (cont->c1 == NULL) ||
5540 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
5541 /* Internal error !!! */
5542 xmlGenericError(xmlGenericErrorContext,
5543 "Internal: MIXED struct bad\n");
5544 break;
5545 }
5546 cont = cont->c2;
5547 }
5548 if (cont == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005549 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005550 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005551 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005552 name, elem->name);
5553 ret = 0;
5554 }
5555 }
5556child_ok:
5557 child = child->next;
5558 }
5559 break;
5560 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005561 if ((doc->standalone == 1) && (extsubset == 1)) {
5562 /*
5563 * VC: Standalone Document Declaration
5564 * - element types with element content, if white space
5565 * occurs directly within any instance of those types.
5566 */
5567 child = elem->children;
5568 while (child != NULL) {
5569 if (child->type == XML_TEXT_NODE) {
5570 const xmlChar *content = child->content;
5571
5572 while (IS_BLANK(*content))
5573 content++;
5574 if (*content == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005575 VECTXT(ctxt, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005576 VERROR(ctxt->userData,
5577"standalone: %s declared in the external subset contains white spaces nodes\n",
5578 elem->name);
5579 ret = 0;
5580 break;
5581 }
5582 }
5583 child =child->next;
5584 }
5585 }
Owen Taylor3473f882001-02-23 17:55:21 +00005586 child = elem->children;
5587 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005588 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005589 if (tmp <= 0)
5590 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005591 break;
5592 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00005593 } /* not continuous */
Owen Taylor3473f882001-02-23 17:55:21 +00005594
5595 /* [ VC: Required Attribute ] */
5596 attr = elemDecl->attributes;
5597 while (attr != NULL) {
5598 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00005599 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00005600
Daniel Veillarde4301c82002-02-13 13:32:35 +00005601 if ((attr->prefix == NULL) &&
5602 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5603 xmlNsPtr ns;
5604
5605 ns = elem->nsDef;
5606 while (ns != NULL) {
5607 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00005608 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005609 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00005610 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005611 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5612 xmlNsPtr ns;
5613
5614 ns = elem->nsDef;
5615 while (ns != NULL) {
5616 if (xmlStrEqual(attr->name, ns->prefix))
5617 goto found;
5618 ns = ns->next;
5619 }
5620 } else {
5621 xmlAttrPtr attrib;
5622
5623 attrib = elem->properties;
5624 while (attrib != NULL) {
5625 if (xmlStrEqual(attrib->name, attr->name)) {
5626 if (attr->prefix != NULL) {
5627 xmlNsPtr nameSpace = attrib->ns;
5628
5629 if (nameSpace == NULL)
5630 nameSpace = elem->ns;
5631 /*
5632 * qualified names handling is problematic, having a
5633 * different prefix should be possible but DTDs don't
5634 * allow to define the URI instead of the prefix :-(
5635 */
5636 if (nameSpace == NULL) {
5637 if (qualified < 0)
5638 qualified = 0;
5639 } else if (!xmlStrEqual(nameSpace->prefix,
5640 attr->prefix)) {
5641 if (qualified < 1)
5642 qualified = 1;
5643 } else
5644 goto found;
5645 } else {
5646 /*
5647 * We should allow applications to define namespaces
5648 * for their application even if the DTD doesn't
5649 * carry one, otherwise, basically we would always
5650 * break.
5651 */
5652 goto found;
5653 }
5654 }
5655 attrib = attrib->next;
5656 }
Owen Taylor3473f882001-02-23 17:55:21 +00005657 }
5658 if (qualified == -1) {
5659 if (attr->prefix == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005660 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005661 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005662 "Element %s does not carry attribute %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005663 elem->name, attr->name);
5664 ret = 0;
5665 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005666 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00005667 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005668 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005669 elem->name, attr->prefix,attr->name);
5670 ret = 0;
5671 }
5672 } else if (qualified == 0) {
5673 VWARNING(ctxt->userData,
5674 "Element %s required attribute %s:%s has no prefix\n",
5675 elem->name, attr->prefix,attr->name);
5676 } else if (qualified == 1) {
5677 VWARNING(ctxt->userData,
5678 "Element %s required attribute %s:%s has different prefix\n",
5679 elem->name, attr->prefix,attr->name);
5680 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00005681 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
5682 /*
5683 * Special tests checking #FIXED namespace declarations
5684 * have the right value since this is not done as an
5685 * attribute checking
5686 */
5687 if ((attr->prefix == NULL) &&
5688 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5689 xmlNsPtr ns;
5690
5691 ns = elem->nsDef;
5692 while (ns != NULL) {
5693 if (ns->prefix == NULL) {
5694 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005695 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005696 VERROR(ctxt->userData,
5697 "Element %s namespace name for default namespace does not match the DTD\n",
5698 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00005699 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005700 }
5701 goto found;
5702 }
5703 ns = ns->next;
5704 }
5705 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5706 xmlNsPtr ns;
5707
5708 ns = elem->nsDef;
5709 while (ns != NULL) {
5710 if (xmlStrEqual(attr->name, ns->prefix)) {
5711 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00005712 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00005713 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005714 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillarde4301c82002-02-13 13:32:35 +00005715 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00005716 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00005717 }
5718 goto found;
5719 }
5720 ns = ns->next;
5721 }
5722 }
Owen Taylor3473f882001-02-23 17:55:21 +00005723 }
5724found:
5725 attr = attr->nexth;
5726 }
5727 return(ret);
5728}
5729
5730/**
5731 * xmlValidateRoot:
5732 * @ctxt: the validation context
5733 * @doc: a document instance
5734 *
5735 * Try to validate a the root element
5736 * basically it does the following check as described by the
5737 * XML-1.0 recommendation:
5738 * - [ VC: Root Element Type ]
5739 * it doesn't try to recurse or apply other check to the element
5740 *
5741 * returns 1 if valid or 0 otherwise
5742 */
5743
5744int
5745xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5746 xmlNodePtr root;
5747 if (doc == NULL) return(0);
5748
5749 root = xmlDocGetRootElement(doc);
5750 if ((root == NULL) || (root->name == NULL)) {
5751 VERROR(ctxt->userData, "Not valid: no root element\n");
5752 return(0);
5753 }
5754
5755 /*
5756 * When doing post validation against a separate DTD, those may
5757 * no internal subset has been generated
5758 */
5759 if ((doc->intSubset != NULL) &&
5760 (doc->intSubset->name != NULL)) {
5761 /*
5762 * Check first the document root against the NQName
5763 */
5764 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
5765 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
5766 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00005767 snprintf((char *) qname, sizeof(qname), "%s:%s",
5768 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00005769 qname[sizeof(qname) - 1] = 0;
5770 if (xmlStrEqual(doc->intSubset->name, qname))
5771 goto name_ok;
5772 }
5773 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
5774 (xmlStrEqual(root->name, BAD_CAST "html")))
5775 goto name_ok;
Daniel Veillard76575762002-09-05 14:21:15 +00005776 VECTXT(ctxt, root);
Owen Taylor3473f882001-02-23 17:55:21 +00005777 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005778 "Not valid: root and DTD name do not match '%s' and '%s'\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005779 root->name, doc->intSubset->name);
5780 return(0);
5781
5782 }
5783 }
5784name_ok:
5785 return(1);
5786}
5787
5788
5789/**
5790 * xmlValidateElement:
5791 * @ctxt: the validation context
5792 * @doc: a document instance
5793 * @elem: an element instance
5794 *
5795 * Try to validate the subtree under an element
5796 *
5797 * returns 1 if valid or 0 otherwise
5798 */
5799
5800int
5801xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
5802 xmlNodePtr child;
5803 xmlAttrPtr attr;
5804 xmlChar *value;
5805 int ret = 1;
5806
5807 if (elem == NULL) return(0);
5808
5809 /*
5810 * XInclude elements were added after parsing in the infoset,
5811 * they don't really mean anything validation wise.
5812 */
5813 if ((elem->type == XML_XINCLUDE_START) ||
5814 (elem->type == XML_XINCLUDE_END))
5815 return(1);
5816
5817 CHECK_DTD;
5818
Daniel Veillard10ea86c2001-06-20 13:55:33 +00005819 /*
5820 * Entities references have to be handled separately
5821 */
5822 if (elem->type == XML_ENTITY_REF_NODE) {
5823 return(1);
5824 }
5825
Owen Taylor3473f882001-02-23 17:55:21 +00005826 ret &= xmlValidateOneElement(ctxt, doc, elem);
5827 attr = elem->properties;
5828 while(attr != NULL) {
5829 value = xmlNodeListGetString(doc, attr->children, 0);
5830 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
5831 if (value != NULL)
5832 xmlFree(value);
5833 attr= attr->next;
5834 }
5835 child = elem->children;
5836 while (child != NULL) {
5837 ret &= xmlValidateElement(ctxt, doc, child);
5838 child = child->next;
5839 }
5840
5841 return(ret);
5842}
5843
Daniel Veillard8730c562001-02-26 10:49:57 +00005844/**
5845 * xmlValidateRef:
5846 * @ref: A reference to be validated
5847 * @ctxt: Validation context
5848 * @name: Name of ID we are searching for
5849 *
5850 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005851static void
Daniel Veillard8730c562001-02-26 10:49:57 +00005852xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00005853 const xmlChar *name) {
5854 xmlAttrPtr id;
5855 xmlAttrPtr attr;
5856
5857 if (ref == NULL)
5858 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005859 if ((ref->attr == NULL) && (ref->name == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005860 return;
Daniel Veillardea7751d2002-12-20 00:16:24 +00005861 attr = ref->attr;
5862 if (attr == NULL) {
5863 xmlChar *dup, *str = NULL, *cur, save;
5864
5865 dup = xmlStrdup(name);
5866 if (dup == NULL) {
5867 ctxt->valid = 0;
5868 return;
5869 }
5870 cur = dup;
5871 while (*cur != 0) {
5872 str = cur;
5873 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5874 save = *cur;
5875 *cur = 0;
5876 id = xmlGetID(ctxt->doc, str);
5877 if (id == NULL) {
5878 VERROR(ctxt->userData,
5879 "attribute %s line %d references an unknown ID \"%s\"\n",
5880 ref->name, ref->lineno, str);
5881 ctxt->valid = 0;
5882 }
5883 if (save == 0)
5884 break;
5885 *cur = save;
5886 while (IS_BLANK(*cur)) cur++;
5887 }
5888 xmlFree(dup);
5889 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
Owen Taylor3473f882001-02-23 17:55:21 +00005890 id = xmlGetID(ctxt->doc, name);
5891 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005892 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005893 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005894 "IDREF attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005895 attr->name, name);
5896 ctxt->valid = 0;
5897 }
5898 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
5899 xmlChar *dup, *str = NULL, *cur, save;
5900
5901 dup = xmlStrdup(name);
5902 if (dup == NULL) {
5903 ctxt->valid = 0;
5904 return;
5905 }
5906 cur = dup;
5907 while (*cur != 0) {
5908 str = cur;
5909 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
5910 save = *cur;
5911 *cur = 0;
5912 id = xmlGetID(ctxt->doc, str);
5913 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00005914 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005915 VERROR(ctxt->userData,
Daniel Veillardea7751d2002-12-20 00:16:24 +00005916 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00005917 attr->name, str);
5918 ctxt->valid = 0;
5919 }
5920 if (save == 0)
5921 break;
5922 *cur = save;
5923 while (IS_BLANK(*cur)) cur++;
5924 }
5925 xmlFree(dup);
5926 }
5927}
5928
5929/**
Daniel Veillard8730c562001-02-26 10:49:57 +00005930 * xmlWalkValidateList:
5931 * @data: Contents of current link
5932 * @user: Value supplied by the user
5933 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005934 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00005935 */
5936static int
5937xmlWalkValidateList(const void *data, const void *user)
5938{
5939 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
5940 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
5941 return 1;
5942}
5943
5944/**
5945 * xmlValidateCheckRefCallback:
5946 * @ref_list: List of references
5947 * @ctxt: Validation context
5948 * @name: Name of ID we are searching for
5949 *
5950 */
5951static void
5952xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
5953 const xmlChar *name) {
5954 xmlValidateMemo memo;
5955
5956 if (ref_list == NULL)
5957 return;
5958 memo.ctxt = ctxt;
5959 memo.name = name;
5960
5961 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
5962
5963}
5964
5965/**
Owen Taylor3473f882001-02-23 17:55:21 +00005966 * xmlValidateDocumentFinal:
5967 * @ctxt: the validation context
5968 * @doc: a document instance
5969 *
5970 * Does the final step for the document validation once all the
5971 * incremental validation steps have been completed
5972 *
5973 * basically it does the following checks described by the XML Rec
5974 *
5975 *
5976 * returns 1 if valid or 0 otherwise
5977 */
5978
5979int
5980xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5981 xmlRefTablePtr table;
5982
5983 if (doc == NULL) {
5984 xmlGenericError(xmlGenericErrorContext,
5985 "xmlValidateDocumentFinal: doc == NULL\n");
5986 return(0);
5987 }
5988
5989 /*
5990 * Check all the NOTATION/NOTATIONS attributes
5991 */
5992 /*
5993 * Check all the ENTITY/ENTITIES attributes definition for validity
5994 */
5995 /*
5996 * Check all the IDREF/IDREFS attributes definition for validity
5997 */
5998 table = (xmlRefTablePtr) doc->refs;
5999 ctxt->doc = doc;
6000 ctxt->valid = 1;
6001 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6002 return(ctxt->valid);
6003}
6004
6005/**
6006 * xmlValidateDtd:
6007 * @ctxt: the validation context
6008 * @doc: a document instance
6009 * @dtd: a dtd instance
6010 *
6011 * Try to validate the document against the dtd instance
6012 *
6013 * basically it does check all the definitions in the DtD.
6014 *
6015 * returns 1 if valid or 0 otherwise
6016 */
6017
6018int
6019xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6020 int ret;
6021 xmlDtdPtr oldExt;
6022 xmlNodePtr root;
6023
6024 if (dtd == NULL) return(0);
6025 if (doc == NULL) return(0);
6026 oldExt = doc->extSubset;
6027 doc->extSubset = dtd;
6028 ret = xmlValidateRoot(ctxt, doc);
6029 if (ret == 0) {
6030 doc->extSubset = oldExt;
6031 return(ret);
6032 }
6033 if (doc->ids != NULL) {
6034 xmlFreeIDTable(doc->ids);
6035 doc->ids = NULL;
6036 }
6037 if (doc->refs != NULL) {
6038 xmlFreeRefTable(doc->refs);
6039 doc->refs = NULL;
6040 }
6041 root = xmlDocGetRootElement(doc);
6042 ret = xmlValidateElement(ctxt, doc, root);
6043 ret &= xmlValidateDocumentFinal(ctxt, doc);
6044 doc->extSubset = oldExt;
6045 return(ret);
6046}
6047
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006048static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006049xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6050 const xmlChar *name ATTRIBUTE_UNUSED) {
6051 if (cur == NULL)
6052 return;
6053 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6054 xmlChar *notation = cur->content;
6055
Daniel Veillard878eab02002-02-19 13:46:09 +00006056 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006057 int ret;
6058
6059 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6060 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00006061 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006062 }
6063 }
6064 }
6065}
6066
6067static void
Owen Taylor3473f882001-02-23 17:55:21 +00006068xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00006069 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006070 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00006071 xmlDocPtr doc;
6072 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006073
Owen Taylor3473f882001-02-23 17:55:21 +00006074 if (cur == NULL)
6075 return;
6076 switch (cur->atype) {
6077 case XML_ATTRIBUTE_CDATA:
6078 case XML_ATTRIBUTE_ID:
6079 case XML_ATTRIBUTE_IDREF :
6080 case XML_ATTRIBUTE_IDREFS:
6081 case XML_ATTRIBUTE_NMTOKEN:
6082 case XML_ATTRIBUTE_NMTOKENS:
6083 case XML_ATTRIBUTE_ENUMERATION:
6084 break;
6085 case XML_ATTRIBUTE_ENTITY:
6086 case XML_ATTRIBUTE_ENTITIES:
6087 case XML_ATTRIBUTE_NOTATION:
6088 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006089
6090 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6091 cur->atype, cur->defaultValue);
6092 if ((ret == 0) && (ctxt->valid == 1))
6093 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006094 }
6095 if (cur->tree != NULL) {
6096 xmlEnumerationPtr tree = cur->tree;
6097 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006098 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00006099 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006100 if ((ret == 0) && (ctxt->valid == 1))
6101 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006102 tree = tree->next;
6103 }
6104 }
6105 }
Daniel Veillard878eab02002-02-19 13:46:09 +00006106 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6107 doc = cur->doc;
6108 if ((doc == NULL) || (cur->elem == NULL)) {
6109 VERROR(ctxt->userData,
6110 "xmlValidateAttributeCallback(%s): internal error\n",
6111 cur->name);
6112 return;
6113 }
6114 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6115 if (elem == NULL)
6116 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
6117 if (elem == NULL) {
6118 VERROR(ctxt->userData,
6119 "attribute %s: could not find decl for element %s\n",
6120 cur->name, cur->elem);
6121 return;
6122 }
6123 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
6124 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00006125 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillard878eab02002-02-19 13:46:09 +00006126 cur->name, cur->elem);
6127 ctxt->valid = 0;
6128 }
6129 }
Owen Taylor3473f882001-02-23 17:55:21 +00006130}
6131
6132/**
6133 * xmlValidateDtdFinal:
6134 * @ctxt: the validation context
6135 * @doc: a document instance
6136 *
6137 * Does the final step for the dtds validation once all the
6138 * subsets have been parsed
6139 *
6140 * basically it does the following checks described by the XML Rec
6141 * - check that ENTITY and ENTITIES type attributes default or
6142 * possible values matches one of the defined entities.
6143 * - check that NOTATION type attributes default or
6144 * possible values matches one of the defined notations.
6145 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006146 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00006147 */
6148
6149int
6150xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00006151 xmlDtdPtr dtd;
6152 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006153 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00006154
6155 if (doc == NULL) return(0);
6156 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6157 return(0);
6158 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006159 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006160 dtd = doc->intSubset;
6161 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6162 table = (xmlAttributeTablePtr) dtd->attributes;
6163 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006164 }
6165 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006166 entities = (xmlEntitiesTablePtr) dtd->entities;
6167 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6168 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006169 }
6170 dtd = doc->extSubset;
6171 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6172 table = (xmlAttributeTablePtr) dtd->attributes;
6173 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00006174 }
6175 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00006176 entities = (xmlEntitiesTablePtr) dtd->entities;
6177 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6178 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006179 }
6180 return(ctxt->valid);
6181}
6182
6183/**
6184 * xmlValidateDocument:
6185 * @ctxt: the validation context
6186 * @doc: a document instance
6187 *
6188 * Try to validate the document instance
6189 *
6190 * basically it does the all the checks described by the XML Rec
6191 * i.e. validates the internal and external subset (if present)
6192 * and validate the document tree.
6193 *
6194 * returns 1 if valid or 0 otherwise
6195 */
6196
6197int
6198xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6199 int ret;
6200 xmlNodePtr root;
6201
Daniel Veillard2fd85422002-10-16 14:32:41 +00006202 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
6203 VERROR(ctxt->userData, "no DTD found!\n" );
Owen Taylor3473f882001-02-23 17:55:21 +00006204 return(0);
Daniel Veillard2fd85422002-10-16 14:32:41 +00006205 }
Owen Taylor3473f882001-02-23 17:55:21 +00006206 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6207 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6208 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6209 doc->intSubset->SystemID);
6210 if (doc->extSubset == NULL) {
6211 if (doc->intSubset->SystemID != NULL) {
6212 VERROR(ctxt->userData,
6213 "Could not load the external subset \"%s\"\n",
6214 doc->intSubset->SystemID);
6215 } else {
6216 VERROR(ctxt->userData,
6217 "Could not load the external subset \"%s\"\n",
6218 doc->intSubset->ExternalID);
6219 }
6220 return(0);
6221 }
6222 }
6223
6224 if (doc->ids != NULL) {
6225 xmlFreeIDTable(doc->ids);
6226 doc->ids = NULL;
6227 }
6228 if (doc->refs != NULL) {
6229 xmlFreeRefTable(doc->refs);
6230 doc->refs = NULL;
6231 }
6232 ret = xmlValidateDtdFinal(ctxt, doc);
6233 if (!xmlValidateRoot(ctxt, doc)) return(0);
6234
6235 root = xmlDocGetRootElement(doc);
6236 ret &= xmlValidateElement(ctxt, doc, root);
6237 ret &= xmlValidateDocumentFinal(ctxt, doc);
6238 return(ret);
6239}
6240
6241
6242/************************************************************************
6243 * *
6244 * Routines for dynamic validation editing *
6245 * *
6246 ************************************************************************/
6247
6248/**
6249 * xmlValidGetPotentialChildren:
6250 * @ctree: an element content tree
6251 * @list: an array to store the list of child names
6252 * @len: a pointer to the number of element in the list
6253 * @max: the size of the array
6254 *
6255 * Build/extend a list of potential children allowed by the content tree
6256 *
6257 * returns the number of element in the list, or -1 in case of error.
6258 */
6259
6260int
6261xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
6262 int *len, int max) {
6263 int i;
6264
6265 if ((ctree == NULL) || (list == NULL) || (len == NULL))
6266 return(-1);
6267 if (*len >= max) return(*len);
6268
6269 switch (ctree->type) {
6270 case XML_ELEMENT_CONTENT_PCDATA:
6271 for (i = 0; i < *len;i++)
6272 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
6273 list[(*len)++] = BAD_CAST "#PCDATA";
6274 break;
6275 case XML_ELEMENT_CONTENT_ELEMENT:
6276 for (i = 0; i < *len;i++)
6277 if (xmlStrEqual(ctree->name, list[i])) return(*len);
6278 list[(*len)++] = ctree->name;
6279 break;
6280 case XML_ELEMENT_CONTENT_SEQ:
6281 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6282 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6283 break;
6284 case XML_ELEMENT_CONTENT_OR:
6285 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
6286 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
6287 break;
6288 }
6289
6290 return(*len);
6291}
6292
6293/**
6294 * xmlValidGetValidElements:
6295 * @prev: an element to insert after
6296 * @next: an element to insert next
6297 * @list: an array to store the list of child names
6298 * @max: the size of the array
6299 *
6300 * This function returns the list of authorized children to insert
6301 * within an existing tree while respecting the validity constraints
6302 * forced by the Dtd. The insertion point is defined using @prev and
6303 * @next in the following ways:
6304 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6305 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6306 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6307 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6308 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6309 *
6310 * pointers to the element names are inserted at the beginning of the array
6311 * and do not need to be freed.
6312 *
6313 * returns the number of element in the list, or -1 in case of error. If
6314 * the function returns the value @max the caller is invited to grow the
6315 * receiving array and retry.
6316 */
6317
6318int
6319xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
6320 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006321 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00006322 int nb_valid_elements = 0;
6323 const xmlChar *elements[256];
6324 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00006325 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006326
6327 xmlNode *ref_node;
6328 xmlNode *parent;
6329 xmlNode *test_node;
6330
6331 xmlNode *prev_next;
6332 xmlNode *next_prev;
6333 xmlNode *parent_childs;
6334 xmlNode *parent_last;
6335
6336 xmlElement *element_desc;
6337
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00006338 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006339
Owen Taylor3473f882001-02-23 17:55:21 +00006340 if (prev == NULL && next == NULL)
6341 return(-1);
6342
6343 if (list == NULL) return(-1);
6344 if (max <= 0) return(-1);
6345
6346 nb_valid_elements = 0;
6347 ref_node = prev ? prev : next;
6348 parent = ref_node->parent;
6349
6350 /*
6351 * Retrieves the parent element declaration
6352 */
6353 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6354 parent->name);
6355 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6356 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6357 parent->name);
6358 if (element_desc == NULL) return(-1);
6359
6360 /*
6361 * Do a backup of the current tree structure
6362 */
6363 prev_next = prev ? prev->next : NULL;
6364 next_prev = next ? next->prev : NULL;
6365 parent_childs = parent->children;
6366 parent_last = parent->last;
6367
6368 /*
6369 * Creates a dummy node and insert it into the tree
6370 */
6371 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
6372 test_node->doc = ref_node->doc;
6373 test_node->parent = parent;
6374 test_node->prev = prev;
6375 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00006376 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00006377
6378 if (prev) prev->next = test_node;
6379 else parent->children = test_node;
6380
6381 if (next) next->prev = test_node;
6382 else parent->last = test_node;
6383
6384 /*
6385 * Insert each potential child node and check if the parent is
6386 * still valid
6387 */
6388 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6389 elements, &nb_elements, 256);
6390
6391 for (i = 0;i < nb_elements;i++) {
6392 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00006393 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006394 int j;
6395
6396 for (j = 0; j < nb_valid_elements;j++)
6397 if (xmlStrEqual(elements[i], list[j])) break;
6398 list[nb_valid_elements++] = elements[i];
6399 if (nb_valid_elements >= max) break;
6400 }
6401 }
6402
6403 /*
6404 * Restore the tree structure
6405 */
6406 if (prev) prev->next = prev_next;
6407 if (next) next->prev = next_prev;
6408 parent->children = parent_childs;
6409 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00006410
6411 /*
6412 * Free up the dummy node
6413 */
6414 test_node->name = name;
6415 xmlFreeNode(test_node);
6416
Owen Taylor3473f882001-02-23 17:55:21 +00006417 return(nb_valid_elements);
6418}