blob: ca0d29d6a31f55a86be49621b6f5ec9c5b3c684d [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 */
29
Owen Taylor3473f882001-02-23 17:55:21 +000030/*
31 * Generic function for accessing stacks in the Validity Context
32 */
33
34#define PUSH_AND_POP(scope, type, name) \
35scope int name##VPush(xmlValidCtxtPtr ctxt, type value) { \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000036 if (ctxt->name##Max <= 0) { \
37 ctxt->name##Max = 4; \
38 ctxt->name##Tab = (type *) xmlMalloc( \
39 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
40 if (ctxt->name##Tab == NULL) { \
41 xmlGenericError(xmlGenericErrorContext, \
42 "malloc failed !\n"); \
Daniel Veillarda9142e72001-06-19 11:07:54 +000043 ctxt->name##Max = 0; \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000044 return(0); \
45 } \
46 } \
Owen Taylor3473f882001-02-23 17:55:21 +000047 if (ctxt->name##Nr >= ctxt->name##Max) { \
48 ctxt->name##Max *= 2; \
49 ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \
50 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
51 if (ctxt->name##Tab == NULL) { \
52 xmlGenericError(xmlGenericErrorContext, \
53 "realloc failed !\n"); \
54 return(0); \
55 } \
56 } \
57 ctxt->name##Tab[ctxt->name##Nr] = value; \
58 ctxt->name = value; \
59 return(ctxt->name##Nr++); \
60} \
61scope type name##VPop(xmlValidCtxtPtr ctxt) { \
62 type ret; \
63 if (ctxt->name##Nr <= 0) return(0); \
64 ctxt->name##Nr--; \
65 if (ctxt->name##Nr > 0) \
66 ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \
67 else \
68 ctxt->name = NULL; \
69 ret = ctxt->name##Tab[ctxt->name##Nr]; \
70 ctxt->name##Tab[ctxt->name##Nr] = 0; \
71 return(ret); \
72} \
73
Daniel Veillarddab4cb32001-04-20 13:03:48 +000074/*
Daniel Veillardb44025c2001-10-11 22:55:55 +000075 * I use a home made algorithm less complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +000076 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +000077 * only restriction is on the deepness of the tree limited by the
78 * size of the occurs bitfield
79 *
80 * this is the content of a saved state for rollbacks
81 */
82
83#define ROLLBACK_OR 0
84#define ROLLBACK_PARENT 1
85
Daniel Veillardb44025c2001-10-11 22:55:55 +000086typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +000087 xmlElementContentPtr cont; /* pointer to the content model subtree */
88 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +000089 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +000090 unsigned char depth; /* current depth in the overall tree */
91 unsigned char state; /* ROLLBACK_XXX */
92} _xmlValidState;
93
Daniel Veillardfc57b412002-04-29 15:50:14 +000094#define MAX_RECURSE 25000
Daniel Veillarddab4cb32001-04-20 13:03:48 +000095#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
96#define CONT ctxt->vstate->cont
97#define NODE ctxt->vstate->node
98#define DEPTH ctxt->vstate->depth
99#define OCCURS ctxt->vstate->occurs
100#define STATE ctxt->vstate->state
101
Daniel Veillard5344c602001-12-31 16:37:34 +0000102#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
103#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000104
Daniel Veillard5344c602001-12-31 16:37:34 +0000105#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
106#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000107
108static int
109vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
110 xmlNodePtr node, unsigned char depth, long occurs,
111 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000112 int i = ctxt->vstateNr - 1;
113
Daniel Veillard940492d2002-04-15 10:15:25 +0000114 if (ctxt->vstateNr > MAX_RECURSE) {
115 return(-1);
116 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000117 if (ctxt->vstateNr >= ctxt->vstateMax) {
118 ctxt->vstateMax *= 2;
119 ctxt->vstateTab = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
120 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
121 if (ctxt->vstateTab == NULL) {
122 xmlGenericError(xmlGenericErrorContext,
123 "realloc failed !n");
Daniel Veillard940492d2002-04-15 10:15:25 +0000124 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000125 }
Daniel Veillard06803992001-04-22 10:35:56 +0000126 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000127 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000128 /*
129 * Don't push on the stack a state already here
130 */
131 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
132 (ctxt->vstateTab[i].node == node) &&
133 (ctxt->vstateTab[i].depth == depth) &&
134 (ctxt->vstateTab[i].occurs == occurs) &&
135 (ctxt->vstateTab[i].state == state))
136 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000137 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
138 ctxt->vstateTab[ctxt->vstateNr].node = node;
139 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
140 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
141 ctxt->vstateTab[ctxt->vstateNr].state = state;
142 return(ctxt->vstateNr++);
143}
144
145static int
146vstateVPop(xmlValidCtxtPtr ctxt) {
147 if (ctxt->vstateNr <= 1) return(-1);
148 ctxt->vstateNr--;
149 ctxt->vstate = &ctxt->vstateTab[0];
150 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
151 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
152 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
153 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
154 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
155 return(ctxt->vstateNr);
156}
157
Owen Taylor3473f882001-02-23 17:55:21 +0000158PUSH_AND_POP(static, xmlNodePtr, node)
159
Owen Taylor3473f882001-02-23 17:55:21 +0000160#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000161static void
162xmlValidPrintNode(xmlNodePtr cur) {
163 if (cur == NULL) {
164 xmlGenericError(xmlGenericErrorContext, "null");
165 return;
166 }
167 switch (cur->type) {
168 case XML_ELEMENT_NODE:
169 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
170 break;
171 case XML_TEXT_NODE:
172 xmlGenericError(xmlGenericErrorContext, "text ");
173 break;
174 case XML_CDATA_SECTION_NODE:
175 xmlGenericError(xmlGenericErrorContext, "cdata ");
176 break;
177 case XML_ENTITY_REF_NODE:
178 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
179 break;
180 case XML_PI_NODE:
181 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
182 break;
183 case XML_COMMENT_NODE:
184 xmlGenericError(xmlGenericErrorContext, "comment ");
185 break;
186 case XML_ATTRIBUTE_NODE:
187 xmlGenericError(xmlGenericErrorContext, "?attr? ");
188 break;
189 case XML_ENTITY_NODE:
190 xmlGenericError(xmlGenericErrorContext, "?ent? ");
191 break;
192 case XML_DOCUMENT_NODE:
193 xmlGenericError(xmlGenericErrorContext, "?doc? ");
194 break;
195 case XML_DOCUMENT_TYPE_NODE:
196 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
197 break;
198 case XML_DOCUMENT_FRAG_NODE:
199 xmlGenericError(xmlGenericErrorContext, "?frag? ");
200 break;
201 case XML_NOTATION_NODE:
202 xmlGenericError(xmlGenericErrorContext, "?nota? ");
203 break;
204 case XML_HTML_DOCUMENT_NODE:
205 xmlGenericError(xmlGenericErrorContext, "?html? ");
206 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000207#ifdef LIBXML_DOCB_ENABLED
208 case XML_DOCB_DOCUMENT_NODE:
209 xmlGenericError(xmlGenericErrorContext, "?docb? ");
210 break;
211#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000212 case XML_DTD_NODE:
213 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
214 break;
215 case XML_ELEMENT_DECL:
216 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
217 break;
218 case XML_ATTRIBUTE_DECL:
219 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
220 break;
221 case XML_ENTITY_DECL:
222 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
223 break;
224 case XML_NAMESPACE_DECL:
225 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
226 break;
227 case XML_XINCLUDE_START:
228 xmlGenericError(xmlGenericErrorContext, "incstart ");
229 break;
230 case XML_XINCLUDE_END:
231 xmlGenericError(xmlGenericErrorContext, "incend ");
232 break;
233 }
234}
235
236static void
237xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000238 if (cur == NULL)
239 xmlGenericError(xmlGenericErrorContext, "null ");
240 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000241 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000242 cur = cur->next;
243 }
244}
245
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000246static void
247xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Owen Taylor3473f882001-02-23 17:55:21 +0000248 char expr[1000];
249
250 expr[0] = 0;
251 xmlGenericError(xmlGenericErrorContext, "valid: ");
252 xmlValidPrintNodeList(cur);
253 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000254 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000255 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
256}
257
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000258static void
259xmlValidDebugState(xmlValidStatePtr state) {
260 xmlGenericError(xmlGenericErrorContext, "(");
261 if (state->cont == NULL)
262 xmlGenericError(xmlGenericErrorContext, "null,");
263 else
264 switch (state->cont->type) {
265 case XML_ELEMENT_CONTENT_PCDATA:
266 xmlGenericError(xmlGenericErrorContext, "pcdata,");
267 break;
268 case XML_ELEMENT_CONTENT_ELEMENT:
269 xmlGenericError(xmlGenericErrorContext, "%s,",
270 state->cont->name);
271 break;
272 case XML_ELEMENT_CONTENT_SEQ:
273 xmlGenericError(xmlGenericErrorContext, "seq,");
274 break;
275 case XML_ELEMENT_CONTENT_OR:
276 xmlGenericError(xmlGenericErrorContext, "or,");
277 break;
278 }
279 xmlValidPrintNode(state->node);
280 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
281 state->depth, state->occurs, state->state);
282}
283
284static void
285xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
286 int i, j;
287
288 xmlGenericError(xmlGenericErrorContext, "state: ");
289 xmlValidDebugState(ctxt->vstate);
290 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
291 ctxt->vstateNr - 1);
292 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
293 xmlValidDebugState(&ctxt->vstateTab[j]);
294 xmlGenericError(xmlGenericErrorContext, "\n");
295}
296
297/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000298#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000299 *****/
300
301#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000302#define DEBUG_VALID_MSG(m) \
303 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
304
Owen Taylor3473f882001-02-23 17:55:21 +0000305#else
306#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000307#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000308#endif
309
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000310/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000311
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000312#define VECTXT(ctxt, node) \
313 if ((ctxt != NULL) && (ctxt->error != NULL) && \
Daniel Veillard76575762002-09-05 14:21:15 +0000314 (node != NULL)) { \
315 xmlChar *base = xmlNodeGetBase(NULL,node); \
316 if (base != NULL) { \
317 ctxt->error(ctxt->userData, "%s:%d: ", base, \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000318 (int) node->content); \
Daniel Veillard76575762002-09-05 14:21:15 +0000319 xmlFree(base); \
320 } else \
321 ctxt->error(ctxt->userData, ":%d: ", \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000322 (int) node->content); \
323 }
324
325#define VWCTXT(ctxt, node) \
326 if ((ctxt != NULL) && (ctxt->warning != NULL) && \
Daniel Veillard76575762002-09-05 14:21:15 +0000327 (node != NULL)) { \
328 xmlChar *base = xmlNodeGetBase(NULL,node); \
329 if (base != NULL) { \
330 ctxt->warning(ctxt->userData, "%s:%d: ", base, \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000331 (int) node->content); \
Daniel Veillard76575762002-09-05 14:21:15 +0000332 xmlFree(base); \
333 } else \
334 ctxt->warning(ctxt->userData, ":%d: ", \
Daniel Veillardb9cd8b42002-09-05 10:58:49 +0000335 (int) node->content); \
336 }
337
Owen Taylor3473f882001-02-23 17:55:21 +0000338#define VERROR \
339 if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error
340
341#define VWARNING \
342 if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning
343
344#define CHECK_DTD \
345 if (doc == NULL) return(0); \
346 else if ((doc->intSubset == NULL) && \
347 (doc->extSubset == NULL)) return(0)
348
Daniel Veillarda10efa82001-04-18 13:09:01 +0000349static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
350 int create);
Owen Taylor3473f882001-02-23 17:55:21 +0000351xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
352
353/************************************************************************
354 * *
355 * QName handling helper *
356 * *
357 ************************************************************************/
358
359/**
360 * xmlSplitQName2:
361 * @name: an XML parser context
362 * @prefix: a xmlChar **
363 *
364 * parse an XML qualified name string
365 *
366 * [NS 5] QName ::= (Prefix ':')? LocalPart
367 *
368 * [NS 6] Prefix ::= NCName
369 *
370 * [NS 7] LocalPart ::= NCName
371 *
372 * Returns NULL if not a QName, otherwise the local part, and prefix
373 * is updated to get the Prefix if any.
374 */
375
376xmlChar *
377xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
378 int len = 0;
379 xmlChar *ret = NULL;
380
381 *prefix = NULL;
382
Daniel Veillardf4309d72001-10-02 09:28:58 +0000383#ifndef XML_XML_NAMESPACE
Owen Taylor3473f882001-02-23 17:55:21 +0000384 /* xml: prefix is not really a namespace */
385 if ((name[0] == 'x') && (name[1] == 'm') &&
386 (name[2] == 'l') && (name[3] == ':'))
387 return(NULL);
Daniel Veillardf4309d72001-10-02 09:28:58 +0000388#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000389
390 /* nasty but valid */
391 if (name[0] == ':')
392 return(NULL);
393
394 /*
395 * we are not trying to validate but just to cut, and yes it will
396 * work even if this is as set of UTF-8 encoded chars
397 */
398 while ((name[len] != 0) && (name[len] != ':'))
399 len++;
400
401 if (name[len] == 0)
402 return(NULL);
403
404 *prefix = xmlStrndup(name, len);
405 ret = xmlStrdup(&name[len + 1]);
406
407 return(ret);
408}
409
410/****************************************************************
411 * *
412 * Util functions for data allocation/deallocation *
413 * *
414 ****************************************************************/
415
416/**
417 * xmlNewElementContent:
418 * @name: the subelement name or NULL
419 * @type: the type of element content decl
420 *
421 * Allocate an element content structure.
422 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000423 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000424 */
425xmlElementContentPtr
426xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
427 xmlElementContentPtr ret;
428
429 switch(type) {
430 case XML_ELEMENT_CONTENT_ELEMENT:
431 if (name == NULL) {
432 xmlGenericError(xmlGenericErrorContext,
433 "xmlNewElementContent : name == NULL !\n");
434 }
435 break;
436 case XML_ELEMENT_CONTENT_PCDATA:
437 case XML_ELEMENT_CONTENT_SEQ:
438 case XML_ELEMENT_CONTENT_OR:
439 if (name != NULL) {
440 xmlGenericError(xmlGenericErrorContext,
441 "xmlNewElementContent : name != NULL !\n");
442 }
443 break;
444 default:
445 xmlGenericError(xmlGenericErrorContext,
446 "xmlNewElementContent: unknown type %d\n", type);
447 return(NULL);
448 }
449 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
450 if (ret == NULL) {
451 xmlGenericError(xmlGenericErrorContext,
452 "xmlNewElementContent : out of memory!\n");
453 return(NULL);
454 }
Daniel Veillardd54fa3e2002-02-20 16:48:52 +0000455 memset(ret, 0, sizeof(xmlElementContent));
Owen Taylor3473f882001-02-23 17:55:21 +0000456 ret->type = type;
457 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000458 if (name != NULL) {
459 xmlChar *prefix = NULL;
460 ret->name = xmlSplitQName2(name, &prefix);
461 if (ret->name == NULL)
462 ret->name = xmlStrdup(name);
463 ret->prefix = prefix;
464 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000465 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000466 ret->prefix = NULL;
467 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000468 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000469 return(ret);
470}
471
472/**
473 * xmlCopyElementContent:
474 * @content: An element content pointer.
475 *
476 * Build a copy of an element content description.
477 *
478 * Returns the new xmlElementContentPtr or NULL in case of error.
479 */
480xmlElementContentPtr
481xmlCopyElementContent(xmlElementContentPtr cur) {
482 xmlElementContentPtr ret;
483
484 if (cur == NULL) return(NULL);
485 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
486 if (ret == NULL) {
487 xmlGenericError(xmlGenericErrorContext,
488 "xmlCopyElementContent : out of memory\n");
489 return(NULL);
490 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000491 if (cur->prefix != NULL)
492 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000493 ret->ocur = cur->ocur;
494 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000495 if (ret->c1 != NULL)
496 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000497 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000498 if (ret->c2 != NULL)
499 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000500 return(ret);
501}
502
503/**
504 * xmlFreeElementContent:
505 * @cur: the element content tree to free
506 *
507 * Free an element content structure. This is a recursive call !
508 */
509void
510xmlFreeElementContent(xmlElementContentPtr cur) {
511 if (cur == NULL) return;
512 switch (cur->type) {
513 case XML_ELEMENT_CONTENT_PCDATA:
514 case XML_ELEMENT_CONTENT_ELEMENT:
515 case XML_ELEMENT_CONTENT_SEQ:
516 case XML_ELEMENT_CONTENT_OR:
517 break;
518 default:
519 xmlGenericError(xmlGenericErrorContext,
520 "xmlFreeElementContent : type %d\n", cur->type);
521 return;
522 }
523 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
524 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
525 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000526 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000527 xmlFree(cur);
528}
529
530/**
531 * xmlDumpElementContent:
532 * @buf: An XML buffer
533 * @content: An element table
534 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
535 *
536 * This will dump the content of the element table as an XML DTD definition
537 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000538static void
Owen Taylor3473f882001-02-23 17:55:21 +0000539xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
540 if (content == NULL) return;
541
542 if (glob) xmlBufferWriteChar(buf, "(");
543 switch (content->type) {
544 case XML_ELEMENT_CONTENT_PCDATA:
545 xmlBufferWriteChar(buf, "#PCDATA");
546 break;
547 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000548 if (content->prefix != NULL) {
549 xmlBufferWriteCHAR(buf, content->prefix);
550 xmlBufferWriteChar(buf, ":");
551 }
Owen Taylor3473f882001-02-23 17:55:21 +0000552 xmlBufferWriteCHAR(buf, content->name);
553 break;
554 case XML_ELEMENT_CONTENT_SEQ:
555 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
556 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
557 xmlDumpElementContent(buf, content->c1, 1);
558 else
559 xmlDumpElementContent(buf, content->c1, 0);
560 xmlBufferWriteChar(buf, " , ");
561 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
562 xmlDumpElementContent(buf, content->c2, 1);
563 else
564 xmlDumpElementContent(buf, content->c2, 0);
565 break;
566 case XML_ELEMENT_CONTENT_OR:
567 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
568 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
569 xmlDumpElementContent(buf, content->c1, 1);
570 else
571 xmlDumpElementContent(buf, content->c1, 0);
572 xmlBufferWriteChar(buf, " | ");
573 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
574 xmlDumpElementContent(buf, content->c2, 1);
575 else
576 xmlDumpElementContent(buf, content->c2, 0);
577 break;
578 default:
579 xmlGenericError(xmlGenericErrorContext,
580 "xmlDumpElementContent: unknown type %d\n",
581 content->type);
582 }
583 if (glob)
584 xmlBufferWriteChar(buf, ")");
585 switch (content->ocur) {
586 case XML_ELEMENT_CONTENT_ONCE:
587 break;
588 case XML_ELEMENT_CONTENT_OPT:
589 xmlBufferWriteChar(buf, "?");
590 break;
591 case XML_ELEMENT_CONTENT_MULT:
592 xmlBufferWriteChar(buf, "*");
593 break;
594 case XML_ELEMENT_CONTENT_PLUS:
595 xmlBufferWriteChar(buf, "+");
596 break;
597 }
598}
599
600/**
601 * xmlSprintfElementContent:
602 * @buf: an output buffer
603 * @content: An element table
604 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
605 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000606 * Deprecated, unsafe, use xmlSnprintfElementContent
607 */
608void
609xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
610 xmlElementContentPtr content ATTRIBUTE_UNUSED,
611 int glob ATTRIBUTE_UNUSED) {
612}
613
614/**
615 * xmlSnprintfElementContent:
616 * @buf: an output buffer
617 * @size: the buffer size
618 * @content: An element table
619 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
620 *
Owen Taylor3473f882001-02-23 17:55:21 +0000621 * This will dump the content of the element content definition
622 * Intended just for the debug routine
623 */
624void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000625xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
626 int len;
627
Owen Taylor3473f882001-02-23 17:55:21 +0000628 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000629 len = strlen(buf);
630 if (size - len < 50) {
631 if ((size - len > 4) && (buf[len - 1] != '.'))
632 strcat(buf, " ...");
633 return;
634 }
Owen Taylor3473f882001-02-23 17:55:21 +0000635 if (glob) strcat(buf, "(");
636 switch (content->type) {
637 case XML_ELEMENT_CONTENT_PCDATA:
638 strcat(buf, "#PCDATA");
639 break;
640 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000641 if (content->prefix != NULL) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000642 if (size - len < xmlStrlen(content->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000643 strcat(buf, " ...");
644 return;
645 }
646 strcat(buf, (char *) content->prefix);
647 strcat(buf, ":");
648 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +0000649 if (size - len < xmlStrlen(content->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +0000650 strcat(buf, " ...");
651 return;
652 }
Owen Taylor3473f882001-02-23 17:55:21 +0000653 strcat(buf, (char *) content->name);
654 break;
655 case XML_ELEMENT_CONTENT_SEQ:
656 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
657 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000658 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000659 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000660 xmlSnprintfElementContent(buf, size, content->c1, 0);
661 len = strlen(buf);
662 if (size - len < 50) {
663 if ((size - len > 4) && (buf[len - 1] != '.'))
664 strcat(buf, " ...");
665 return;
666 }
Owen Taylor3473f882001-02-23 17:55:21 +0000667 strcat(buf, " , ");
668 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000669 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000670 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000671 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000672 break;
673 case XML_ELEMENT_CONTENT_OR:
674 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
675 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000676 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000677 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000678 xmlSnprintfElementContent(buf, size, content->c1, 0);
679 len = strlen(buf);
680 if (size - len < 50) {
681 if ((size - len > 4) && (buf[len - 1] != '.'))
682 strcat(buf, " ...");
683 return;
684 }
Owen Taylor3473f882001-02-23 17:55:21 +0000685 strcat(buf, " | ");
686 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000687 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000688 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000689 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000690 break;
691 }
692 if (glob)
693 strcat(buf, ")");
694 switch (content->ocur) {
695 case XML_ELEMENT_CONTENT_ONCE:
696 break;
697 case XML_ELEMENT_CONTENT_OPT:
698 strcat(buf, "?");
699 break;
700 case XML_ELEMENT_CONTENT_MULT:
701 strcat(buf, "*");
702 break;
703 case XML_ELEMENT_CONTENT_PLUS:
704 strcat(buf, "+");
705 break;
706 }
707}
708
709/****************************************************************
710 * *
711 * Registration of DTD declarations *
712 * *
713 ****************************************************************/
714
715/**
716 * xmlCreateElementTable:
717 *
718 * create and initialize an empty element hash table.
719 *
720 * Returns the xmlElementTablePtr just created or NULL in case of error.
721 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000722static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000723xmlCreateElementTable(void) {
724 return(xmlHashCreate(0));
725}
726
727/**
728 * xmlFreeElement:
729 * @elem: An element
730 *
731 * Deallocate the memory used by an element definition
732 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000733static void
Owen Taylor3473f882001-02-23 17:55:21 +0000734xmlFreeElement(xmlElementPtr elem) {
735 if (elem == NULL) return;
736 xmlUnlinkNode((xmlNodePtr) elem);
737 xmlFreeElementContent(elem->content);
738 if (elem->name != NULL)
739 xmlFree((xmlChar *) elem->name);
740 if (elem->prefix != NULL)
741 xmlFree((xmlChar *) elem->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000742 xmlFree(elem);
743}
744
745
746/**
747 * xmlAddElementDecl:
748 * @ctxt: the validation context
749 * @dtd: pointer to the DTD
750 * @name: the entity name
751 * @type: the element type
752 * @content: the element content tree or NULL
753 *
754 * Register a new element declaration
755 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000756 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000757 */
758xmlElementPtr
759xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
760 xmlElementTypeVal type,
761 xmlElementContentPtr content) {
762 xmlElementPtr ret;
763 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +0000764 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000765 xmlChar *ns, *uqname;
766
767 if (dtd == NULL) {
768 xmlGenericError(xmlGenericErrorContext,
769 "xmlAddElementDecl: dtd == NULL\n");
770 return(NULL);
771 }
772 if (name == NULL) {
773 xmlGenericError(xmlGenericErrorContext,
774 "xmlAddElementDecl: name == NULL\n");
775 return(NULL);
776 }
777 switch (type) {
778 case XML_ELEMENT_TYPE_EMPTY:
779 if (content != NULL) {
780 xmlGenericError(xmlGenericErrorContext,
781 "xmlAddElementDecl: content != NULL for EMPTY\n");
782 return(NULL);
783 }
784 break;
785 case XML_ELEMENT_TYPE_ANY:
786 if (content != NULL) {
787 xmlGenericError(xmlGenericErrorContext,
788 "xmlAddElementDecl: content != NULL for ANY\n");
789 return(NULL);
790 }
791 break;
792 case XML_ELEMENT_TYPE_MIXED:
793 if (content == NULL) {
794 xmlGenericError(xmlGenericErrorContext,
795 "xmlAddElementDecl: content == NULL for MIXED\n");
796 return(NULL);
797 }
798 break;
799 case XML_ELEMENT_TYPE_ELEMENT:
800 if (content == NULL) {
801 xmlGenericError(xmlGenericErrorContext,
802 "xmlAddElementDecl: content == NULL for ELEMENT\n");
803 return(NULL);
804 }
805 break;
806 default:
807 xmlGenericError(xmlGenericErrorContext,
808 "xmlAddElementDecl: unknown type %d\n", type);
809 return(NULL);
810 }
811
812 /*
813 * check if name is a QName
814 */
815 uqname = xmlSplitQName2(name, &ns);
816 if (uqname != NULL)
817 name = uqname;
818
819 /*
820 * Create the Element table if needed.
821 */
822 table = (xmlElementTablePtr) dtd->elements;
823 if (table == NULL) {
824 table = xmlCreateElementTable();
825 dtd->elements = (void *) table;
826 }
827 if (table == NULL) {
828 xmlGenericError(xmlGenericErrorContext,
829 "xmlAddElementDecl: Table creation failed!\n");
830 return(NULL);
831 }
832
Daniel Veillarda10efa82001-04-18 13:09:01 +0000833 /*
834 * lookup old attributes inserted on an undefined element in the
835 * internal subset.
836 */
837 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
838 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
839 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
840 oldAttributes = ret->attributes;
841 ret->attributes = NULL;
842 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
843 xmlFreeElement(ret);
844 }
Owen Taylor3473f882001-02-23 17:55:21 +0000845 }
Owen Taylor3473f882001-02-23 17:55:21 +0000846
847 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +0000848 * The element may already be present if one of its attribute
849 * was registered first
850 */
851 ret = xmlHashLookup2(table, name, ns);
852 if (ret != NULL) {
853 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
854 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000855 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000856 */
857 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
858 if (uqname != NULL)
859 xmlFree(uqname);
860 return(NULL);
861 }
862 } else {
863 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
864 if (ret == NULL) {
865 xmlGenericError(xmlGenericErrorContext,
866 "xmlAddElementDecl: out of memory\n");
867 return(NULL);
868 }
869 memset(ret, 0, sizeof(xmlElement));
870 ret->type = XML_ELEMENT_DECL;
871
872 /*
873 * fill the structure.
874 */
875 ret->name = xmlStrdup(name);
876 ret->prefix = ns;
877
878 /*
879 * Validity Check:
880 * Insertion must not fail
881 */
882 if (xmlHashAddEntry2(table, name, ns, ret)) {
883 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000884 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000885 */
886 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
887 xmlFreeElement(ret);
888 if (uqname != NULL)
889 xmlFree(uqname);
890 return(NULL);
891 }
892 }
893
894 /*
895 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +0000896 */
897 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +0000898 ret->content = xmlCopyElementContent(content);
Daniel Veillarda10efa82001-04-18 13:09:01 +0000899 ret->attributes = oldAttributes;
Owen Taylor3473f882001-02-23 17:55:21 +0000900
901 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000902 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000903 */
904 ret->parent = dtd;
905 ret->doc = dtd->doc;
906 if (dtd->last == NULL) {
907 dtd->children = dtd->last = (xmlNodePtr) ret;
908 } else {
909 dtd->last->next = (xmlNodePtr) ret;
910 ret->prev = dtd->last;
911 dtd->last = (xmlNodePtr) ret;
912 }
913 if (uqname != NULL)
914 xmlFree(uqname);
915 return(ret);
916}
917
918/**
919 * xmlFreeElementTable:
920 * @table: An element table
921 *
922 * Deallocate the memory used by an element hash table.
923 */
924void
925xmlFreeElementTable(xmlElementTablePtr table) {
926 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
927}
928
929/**
930 * xmlCopyElement:
931 * @elem: An element
932 *
933 * Build a copy of an element.
934 *
935 * Returns the new xmlElementPtr or NULL in case of error.
936 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000937static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000938xmlCopyElement(xmlElementPtr elem) {
939 xmlElementPtr cur;
940
941 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
942 if (cur == NULL) {
943 xmlGenericError(xmlGenericErrorContext,
944 "xmlCopyElement: out of memory !\n");
945 return(NULL);
946 }
947 memset(cur, 0, sizeof(xmlElement));
948 cur->type = XML_ELEMENT_DECL;
949 cur->etype = elem->etype;
950 if (elem->name != NULL)
951 cur->name = xmlStrdup(elem->name);
952 else
953 cur->name = NULL;
954 if (elem->prefix != NULL)
955 cur->prefix = xmlStrdup(elem->prefix);
956 else
957 cur->prefix = NULL;
958 cur->content = xmlCopyElementContent(elem->content);
959 /* TODO : rebuild the attribute list on the copy */
960 cur->attributes = NULL;
961 return(cur);
962}
963
964/**
965 * xmlCopyElementTable:
966 * @table: An element table
967 *
968 * Build a copy of an element table.
969 *
970 * Returns the new xmlElementTablePtr or NULL in case of error.
971 */
972xmlElementTablePtr
973xmlCopyElementTable(xmlElementTablePtr table) {
974 return((xmlElementTablePtr) xmlHashCopy(table,
975 (xmlHashCopier) xmlCopyElement));
976}
977
978/**
979 * xmlDumpElementDecl:
980 * @buf: the XML buffer output
981 * @elem: An element table
982 *
983 * This will dump the content of the element declaration as an XML
984 * DTD definition
985 */
986void
987xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
988 switch (elem->etype) {
989 case XML_ELEMENT_TYPE_EMPTY:
990 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000991 if (elem->prefix != NULL) {
992 xmlBufferWriteCHAR(buf, elem->prefix);
993 xmlBufferWriteChar(buf, ":");
994 }
Owen Taylor3473f882001-02-23 17:55:21 +0000995 xmlBufferWriteCHAR(buf, elem->name);
996 xmlBufferWriteChar(buf, " EMPTY>\n");
997 break;
998 case XML_ELEMENT_TYPE_ANY:
999 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001000 if (elem->prefix != NULL) {
1001 xmlBufferWriteCHAR(buf, elem->prefix);
1002 xmlBufferWriteChar(buf, ":");
1003 }
Owen Taylor3473f882001-02-23 17:55:21 +00001004 xmlBufferWriteCHAR(buf, elem->name);
1005 xmlBufferWriteChar(buf, " ANY>\n");
1006 break;
1007 case XML_ELEMENT_TYPE_MIXED:
1008 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001009 if (elem->prefix != NULL) {
1010 xmlBufferWriteCHAR(buf, elem->prefix);
1011 xmlBufferWriteChar(buf, ":");
1012 }
Owen Taylor3473f882001-02-23 17:55:21 +00001013 xmlBufferWriteCHAR(buf, elem->name);
1014 xmlBufferWriteChar(buf, " ");
1015 xmlDumpElementContent(buf, elem->content, 1);
1016 xmlBufferWriteChar(buf, ">\n");
1017 break;
1018 case XML_ELEMENT_TYPE_ELEMENT:
1019 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +00001020 if (elem->prefix != NULL) {
1021 xmlBufferWriteCHAR(buf, elem->prefix);
1022 xmlBufferWriteChar(buf, ":");
1023 }
Owen Taylor3473f882001-02-23 17:55:21 +00001024 xmlBufferWriteCHAR(buf, elem->name);
1025 xmlBufferWriteChar(buf, " ");
1026 xmlDumpElementContent(buf, elem->content, 1);
1027 xmlBufferWriteChar(buf, ">\n");
1028 break;
1029 default:
1030 xmlGenericError(xmlGenericErrorContext,
1031 "xmlDumpElementDecl: internal: unknown type %d\n",
1032 elem->etype);
1033 }
1034}
1035
1036/**
1037 * xmlDumpElementTable:
1038 * @buf: the XML buffer output
1039 * @table: An element table
1040 *
1041 * This will dump the content of the element table as an XML DTD definition
1042 */
1043void
1044xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1045 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1046}
1047
1048/**
1049 * xmlCreateEnumeration:
1050 * @name: the enumeration name or NULL
1051 *
1052 * create and initialize an enumeration attribute node.
1053 *
1054 * Returns the xmlEnumerationPtr just created or NULL in case
1055 * of error.
1056 */
1057xmlEnumerationPtr
1058xmlCreateEnumeration(xmlChar *name) {
1059 xmlEnumerationPtr ret;
1060
1061 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1062 if (ret == NULL) {
1063 xmlGenericError(xmlGenericErrorContext,
1064 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1065 (long)sizeof(xmlEnumeration));
1066 return(NULL);
1067 }
1068 memset(ret, 0, sizeof(xmlEnumeration));
1069
1070 if (name != NULL)
1071 ret->name = xmlStrdup(name);
1072 return(ret);
1073}
1074
1075/**
1076 * xmlFreeEnumeration:
1077 * @cur: the tree to free.
1078 *
1079 * free an enumeration attribute node (recursive).
1080 */
1081void
1082xmlFreeEnumeration(xmlEnumerationPtr cur) {
1083 if (cur == NULL) return;
1084
1085 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1086
1087 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001088 xmlFree(cur);
1089}
1090
1091/**
1092 * xmlCopyEnumeration:
1093 * @cur: the tree to copy.
1094 *
1095 * Copy an enumeration attribute node (recursive).
1096 *
1097 * Returns the xmlEnumerationPtr just created or NULL in case
1098 * of error.
1099 */
1100xmlEnumerationPtr
1101xmlCopyEnumeration(xmlEnumerationPtr cur) {
1102 xmlEnumerationPtr ret;
1103
1104 if (cur == NULL) return(NULL);
1105 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1106
1107 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1108 else ret->next = NULL;
1109
1110 return(ret);
1111}
1112
1113/**
1114 * xmlDumpEnumeration:
1115 * @buf: the XML buffer output
1116 * @enum: An enumeration
1117 *
1118 * This will dump the content of the enumeration
1119 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001120static void
Owen Taylor3473f882001-02-23 17:55:21 +00001121xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1122 if (cur == NULL) return;
1123
1124 xmlBufferWriteCHAR(buf, cur->name);
1125 if (cur->next == NULL)
1126 xmlBufferWriteChar(buf, ")");
1127 else {
1128 xmlBufferWriteChar(buf, " | ");
1129 xmlDumpEnumeration(buf, cur->next);
1130 }
1131}
1132
1133/**
1134 * xmlCreateAttributeTable:
1135 *
1136 * create and initialize an empty attribute hash table.
1137 *
1138 * Returns the xmlAttributeTablePtr just created or NULL in case
1139 * of error.
1140 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001141static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001142xmlCreateAttributeTable(void) {
1143 return(xmlHashCreate(0));
1144}
1145
1146/**
1147 * xmlScanAttributeDeclCallback:
1148 * @attr: the attribute decl
1149 * @list: the list to update
1150 *
1151 * Callback called by xmlScanAttributeDecl when a new attribute
1152 * has to be entered in the list.
1153 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001154static void
Owen Taylor3473f882001-02-23 17:55:21 +00001155xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001156 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001157 attr->nexth = *list;
1158 *list = attr;
1159}
1160
1161/**
1162 * xmlScanAttributeDecl:
1163 * @dtd: pointer to the DTD
1164 * @elem: the element name
1165 *
1166 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001167 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001168 *
1169 * Returns the pointer to the first attribute decl in the chain,
1170 * possibly NULL.
1171 */
1172xmlAttributePtr
1173xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1174 xmlAttributePtr ret = NULL;
1175 xmlAttributeTablePtr table;
1176
1177 if (dtd == NULL) {
1178 xmlGenericError(xmlGenericErrorContext,
1179 "xmlScanAttributeDecl: dtd == NULL\n");
1180 return(NULL);
1181 }
1182 if (elem == NULL) {
1183 xmlGenericError(xmlGenericErrorContext,
1184 "xmlScanAttributeDecl: elem == NULL\n");
1185 return(NULL);
1186 }
1187 table = (xmlAttributeTablePtr) dtd->attributes;
1188 if (table == NULL)
1189 return(NULL);
1190
1191 /* WRONG !!! */
1192 xmlHashScan3(table, NULL, NULL, elem,
1193 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1194 return(ret);
1195}
1196
1197/**
1198 * xmlScanIDAttributeDecl:
1199 * @ctxt: the validation context
1200 * @elem: the element name
1201 *
1202 * Verify that the element don't have too many ID attributes
1203 * declared.
1204 *
1205 * Returns the number of ID attributes found.
1206 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001207static int
Owen Taylor3473f882001-02-23 17:55:21 +00001208xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1209 xmlAttributePtr cur;
1210 int ret = 0;
1211
1212 if (elem == NULL) return(0);
1213 cur = elem->attributes;
1214 while (cur != NULL) {
1215 if (cur->atype == XML_ATTRIBUTE_ID) {
1216 ret ++;
1217 if (ret > 1)
1218 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001219 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001220 elem->name, cur->name);
1221 }
1222 cur = cur->nexth;
1223 }
1224 return(ret);
1225}
1226
1227/**
1228 * xmlFreeAttribute:
1229 * @elem: An attribute
1230 *
1231 * Deallocate the memory used by an attribute definition
1232 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001233static void
Owen Taylor3473f882001-02-23 17:55:21 +00001234xmlFreeAttribute(xmlAttributePtr attr) {
1235 if (attr == NULL) return;
1236 xmlUnlinkNode((xmlNodePtr) attr);
1237 if (attr->tree != NULL)
1238 xmlFreeEnumeration(attr->tree);
1239 if (attr->elem != NULL)
1240 xmlFree((xmlChar *) attr->elem);
1241 if (attr->name != NULL)
1242 xmlFree((xmlChar *) attr->name);
1243 if (attr->defaultValue != NULL)
1244 xmlFree((xmlChar *) attr->defaultValue);
1245 if (attr->prefix != NULL)
1246 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001247 xmlFree(attr);
1248}
1249
1250
1251/**
1252 * xmlAddAttributeDecl:
1253 * @ctxt: the validation context
1254 * @dtd: pointer to the DTD
1255 * @elem: the element name
1256 * @name: the attribute name
1257 * @ns: the attribute namespace prefix
1258 * @type: the attribute type
1259 * @def: the attribute default type
1260 * @defaultValue: the attribute default value
1261 * @tree: if it's an enumeration, the associated list
1262 *
1263 * Register a new attribute declaration
1264 * Note that @tree becomes the ownership of the DTD
1265 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001266 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001267 */
1268xmlAttributePtr
1269xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1270 const xmlChar *name, const xmlChar *ns,
1271 xmlAttributeType type, xmlAttributeDefault def,
1272 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1273 xmlAttributePtr ret;
1274 xmlAttributeTablePtr table;
1275 xmlElementPtr elemDef;
1276
1277 if (dtd == NULL) {
1278 xmlGenericError(xmlGenericErrorContext,
1279 "xmlAddAttributeDecl: dtd == NULL\n");
1280 xmlFreeEnumeration(tree);
1281 return(NULL);
1282 }
1283 if (name == NULL) {
1284 xmlGenericError(xmlGenericErrorContext,
1285 "xmlAddAttributeDecl: name == NULL\n");
1286 xmlFreeEnumeration(tree);
1287 return(NULL);
1288 }
1289 if (elem == NULL) {
1290 xmlGenericError(xmlGenericErrorContext,
1291 "xmlAddAttributeDecl: elem == NULL\n");
1292 xmlFreeEnumeration(tree);
1293 return(NULL);
1294 }
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001295
Owen Taylor3473f882001-02-23 17:55:21 +00001296 /*
1297 * Check the type and possibly the default value.
1298 */
1299 switch (type) {
1300 case XML_ATTRIBUTE_CDATA:
1301 break;
1302 case XML_ATTRIBUTE_ID:
1303 break;
1304 case XML_ATTRIBUTE_IDREF:
1305 break;
1306 case XML_ATTRIBUTE_IDREFS:
1307 break;
1308 case XML_ATTRIBUTE_ENTITY:
1309 break;
1310 case XML_ATTRIBUTE_ENTITIES:
1311 break;
1312 case XML_ATTRIBUTE_NMTOKEN:
1313 break;
1314 case XML_ATTRIBUTE_NMTOKENS:
1315 break;
1316 case XML_ATTRIBUTE_ENUMERATION:
1317 break;
1318 case XML_ATTRIBUTE_NOTATION:
1319 break;
1320 default:
1321 xmlGenericError(xmlGenericErrorContext,
1322 "xmlAddAttributeDecl: unknown type %d\n", type);
1323 xmlFreeEnumeration(tree);
1324 return(NULL);
1325 }
1326 if ((defaultValue != NULL) &&
1327 (!xmlValidateAttributeValue(type, defaultValue))) {
Daniel Veillard58e44c92002-08-02 22:19:49 +00001328 VERROR(ctxt->userData, "Attribute %s of %s: invalid default value\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001329 elem, name, defaultValue);
1330 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001331 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001332 }
1333
1334 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001335 * Check first that an attribute defined in the external subset wasn't
1336 * already defined in the internal subset
1337 */
1338 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1339 (dtd->doc->intSubset != NULL) &&
1340 (dtd->doc->intSubset->attributes != NULL)) {
1341 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1342 if (ret != NULL)
1343 return(NULL);
1344 }
1345
1346 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001347 * Create the Attribute table if needed.
1348 */
1349 table = (xmlAttributeTablePtr) dtd->attributes;
1350 if (table == NULL) {
1351 table = xmlCreateAttributeTable();
1352 dtd->attributes = (void *) table;
1353 }
1354 if (table == NULL) {
1355 xmlGenericError(xmlGenericErrorContext,
1356 "xmlAddAttributeDecl: Table creation failed!\n");
1357 return(NULL);
1358 }
1359
1360
1361 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1362 if (ret == NULL) {
1363 xmlGenericError(xmlGenericErrorContext,
1364 "xmlAddAttributeDecl: out of memory\n");
1365 return(NULL);
1366 }
1367 memset(ret, 0, sizeof(xmlAttribute));
1368 ret->type = XML_ATTRIBUTE_DECL;
1369
1370 /*
1371 * fill the structure.
1372 */
1373 ret->atype = type;
1374 ret->name = xmlStrdup(name);
1375 ret->prefix = xmlStrdup(ns);
1376 ret->elem = xmlStrdup(elem);
1377 ret->def = def;
1378 ret->tree = tree;
1379 if (defaultValue != NULL)
1380 ret->defaultValue = xmlStrdup(defaultValue);
1381
1382 /*
1383 * Validity Check:
1384 * Search the DTD for previous declarations of the ATTLIST
1385 */
1386 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1387 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001388 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001389 */
1390 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00001391 "Attribute %s of element %s: already defined\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001392 name, elem);
1393 xmlFreeAttribute(ret);
1394 return(NULL);
1395 }
1396
1397 /*
1398 * Validity Check:
1399 * Multiple ID per element
1400 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001401 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001402 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001403
Owen Taylor3473f882001-02-23 17:55:21 +00001404 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001405 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001406 VERROR(ctxt->userData,
1407 "Element %s has too may ID attributes defined : %s\n",
1408 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001409 ctxt->valid = 0;
1410 }
1411
Daniel Veillard48da9102001-08-07 01:10:10 +00001412 /*
1413 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001414 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001415 */
1416 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1417 ((ret->prefix != NULL &&
1418 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1419 ret->nexth = elemDef->attributes;
1420 elemDef->attributes = ret;
1421 } else {
1422 xmlAttributePtr tmp = elemDef->attributes;
1423
1424 while ((tmp != NULL) &&
1425 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1426 ((ret->prefix != NULL &&
1427 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1428 if (tmp->nexth == NULL)
1429 break;
1430 tmp = tmp->nexth;
1431 }
1432 if (tmp != NULL) {
1433 ret->nexth = tmp->nexth;
1434 tmp->nexth = ret;
1435 } else {
1436 ret->nexth = elemDef->attributes;
1437 elemDef->attributes = ret;
1438 }
1439 }
Owen Taylor3473f882001-02-23 17:55:21 +00001440 }
1441
1442 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001443 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001444 */
1445 ret->parent = dtd;
1446 ret->doc = dtd->doc;
1447 if (dtd->last == NULL) {
1448 dtd->children = dtd->last = (xmlNodePtr) ret;
1449 } else {
1450 dtd->last->next = (xmlNodePtr) ret;
1451 ret->prev = dtd->last;
1452 dtd->last = (xmlNodePtr) ret;
1453 }
1454 return(ret);
1455}
1456
1457/**
1458 * xmlFreeAttributeTable:
1459 * @table: An attribute table
1460 *
1461 * Deallocate the memory used by an entities hash table.
1462 */
1463void
1464xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1465 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1466}
1467
1468/**
1469 * xmlCopyAttribute:
1470 * @attr: An attribute
1471 *
1472 * Build a copy of an attribute.
1473 *
1474 * Returns the new xmlAttributePtr or NULL in case of error.
1475 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001476static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001477xmlCopyAttribute(xmlAttributePtr attr) {
1478 xmlAttributePtr cur;
1479
1480 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1481 if (cur == NULL) {
1482 xmlGenericError(xmlGenericErrorContext,
1483 "xmlCopyAttribute: out of memory !\n");
1484 return(NULL);
1485 }
1486 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001487 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001488 cur->atype = attr->atype;
1489 cur->def = attr->def;
1490 cur->tree = xmlCopyEnumeration(attr->tree);
1491 if (attr->elem != NULL)
1492 cur->elem = xmlStrdup(attr->elem);
1493 if (attr->name != NULL)
1494 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001495 if (attr->prefix != NULL)
1496 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001497 if (attr->defaultValue != NULL)
1498 cur->defaultValue = xmlStrdup(attr->defaultValue);
1499 return(cur);
1500}
1501
1502/**
1503 * xmlCopyAttributeTable:
1504 * @table: An attribute table
1505 *
1506 * Build a copy of an attribute table.
1507 *
1508 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1509 */
1510xmlAttributeTablePtr
1511xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1512 return((xmlAttributeTablePtr) xmlHashCopy(table,
1513 (xmlHashCopier) xmlCopyAttribute));
1514}
1515
1516/**
1517 * xmlDumpAttributeDecl:
1518 * @buf: the XML buffer output
1519 * @attr: An attribute declaration
1520 *
1521 * This will dump the content of the attribute declaration as an XML
1522 * DTD definition
1523 */
1524void
1525xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1526 xmlBufferWriteChar(buf, "<!ATTLIST ");
1527 xmlBufferWriteCHAR(buf, attr->elem);
1528 xmlBufferWriteChar(buf, " ");
1529 if (attr->prefix != NULL) {
1530 xmlBufferWriteCHAR(buf, attr->prefix);
1531 xmlBufferWriteChar(buf, ":");
1532 }
1533 xmlBufferWriteCHAR(buf, attr->name);
1534 switch (attr->atype) {
1535 case XML_ATTRIBUTE_CDATA:
1536 xmlBufferWriteChar(buf, " CDATA");
1537 break;
1538 case XML_ATTRIBUTE_ID:
1539 xmlBufferWriteChar(buf, " ID");
1540 break;
1541 case XML_ATTRIBUTE_IDREF:
1542 xmlBufferWriteChar(buf, " IDREF");
1543 break;
1544 case XML_ATTRIBUTE_IDREFS:
1545 xmlBufferWriteChar(buf, " IDREFS");
1546 break;
1547 case XML_ATTRIBUTE_ENTITY:
1548 xmlBufferWriteChar(buf, " ENTITY");
1549 break;
1550 case XML_ATTRIBUTE_ENTITIES:
1551 xmlBufferWriteChar(buf, " ENTITIES");
1552 break;
1553 case XML_ATTRIBUTE_NMTOKEN:
1554 xmlBufferWriteChar(buf, " NMTOKEN");
1555 break;
1556 case XML_ATTRIBUTE_NMTOKENS:
1557 xmlBufferWriteChar(buf, " NMTOKENS");
1558 break;
1559 case XML_ATTRIBUTE_ENUMERATION:
1560 xmlBufferWriteChar(buf, " (");
1561 xmlDumpEnumeration(buf, attr->tree);
1562 break;
1563 case XML_ATTRIBUTE_NOTATION:
1564 xmlBufferWriteChar(buf, " NOTATION (");
1565 xmlDumpEnumeration(buf, attr->tree);
1566 break;
1567 default:
1568 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001569 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001570 attr->atype);
1571 }
1572 switch (attr->def) {
1573 case XML_ATTRIBUTE_NONE:
1574 break;
1575 case XML_ATTRIBUTE_REQUIRED:
1576 xmlBufferWriteChar(buf, " #REQUIRED");
1577 break;
1578 case XML_ATTRIBUTE_IMPLIED:
1579 xmlBufferWriteChar(buf, " #IMPLIED");
1580 break;
1581 case XML_ATTRIBUTE_FIXED:
1582 xmlBufferWriteChar(buf, " #FIXED");
1583 break;
1584 default:
1585 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001586 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001587 attr->def);
1588 }
1589 if (attr->defaultValue != NULL) {
1590 xmlBufferWriteChar(buf, " ");
1591 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1592 }
1593 xmlBufferWriteChar(buf, ">\n");
1594}
1595
1596/**
1597 * xmlDumpAttributeTable:
1598 * @buf: the XML buffer output
1599 * @table: An attribute table
1600 *
1601 * This will dump the content of the attribute table as an XML DTD definition
1602 */
1603void
1604xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1605 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1606}
1607
1608/************************************************************************
1609 * *
1610 * NOTATIONs *
1611 * *
1612 ************************************************************************/
1613/**
1614 * xmlCreateNotationTable:
1615 *
1616 * create and initialize an empty notation hash table.
1617 *
1618 * Returns the xmlNotationTablePtr just created or NULL in case
1619 * of error.
1620 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001621static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001622xmlCreateNotationTable(void) {
1623 return(xmlHashCreate(0));
1624}
1625
1626/**
1627 * xmlFreeNotation:
1628 * @not: A notation
1629 *
1630 * Deallocate the memory used by an notation definition
1631 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001632static void
Owen Taylor3473f882001-02-23 17:55:21 +00001633xmlFreeNotation(xmlNotationPtr nota) {
1634 if (nota == NULL) return;
1635 if (nota->name != NULL)
1636 xmlFree((xmlChar *) nota->name);
1637 if (nota->PublicID != NULL)
1638 xmlFree((xmlChar *) nota->PublicID);
1639 if (nota->SystemID != NULL)
1640 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001641 xmlFree(nota);
1642}
1643
1644
1645/**
1646 * xmlAddNotationDecl:
1647 * @dtd: pointer to the DTD
1648 * @ctxt: the validation context
1649 * @name: the entity name
1650 * @PublicID: the public identifier or NULL
1651 * @SystemID: the system identifier or NULL
1652 *
1653 * Register a new notation declaration
1654 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001655 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001656 */
1657xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001658xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001659 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001660 const xmlChar *PublicID, const xmlChar *SystemID) {
1661 xmlNotationPtr ret;
1662 xmlNotationTablePtr table;
1663
1664 if (dtd == NULL) {
1665 xmlGenericError(xmlGenericErrorContext,
1666 "xmlAddNotationDecl: dtd == NULL\n");
1667 return(NULL);
1668 }
1669 if (name == NULL) {
1670 xmlGenericError(xmlGenericErrorContext,
1671 "xmlAddNotationDecl: name == NULL\n");
1672 return(NULL);
1673 }
1674 if ((PublicID == NULL) && (SystemID == NULL)) {
1675 xmlGenericError(xmlGenericErrorContext,
1676 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00001677 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001678 }
1679
1680 /*
1681 * Create the Notation table if needed.
1682 */
1683 table = (xmlNotationTablePtr) dtd->notations;
1684 if (table == NULL)
1685 dtd->notations = table = xmlCreateNotationTable();
1686 if (table == NULL) {
1687 xmlGenericError(xmlGenericErrorContext,
1688 "xmlAddNotationDecl: Table creation failed!\n");
1689 return(NULL);
1690 }
1691
1692 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1693 if (ret == NULL) {
1694 xmlGenericError(xmlGenericErrorContext,
1695 "xmlAddNotationDecl: out of memory\n");
1696 return(NULL);
1697 }
1698 memset(ret, 0, sizeof(xmlNotation));
1699
1700 /*
1701 * fill the structure.
1702 */
1703 ret->name = xmlStrdup(name);
1704 if (SystemID != NULL)
1705 ret->SystemID = xmlStrdup(SystemID);
1706 if (PublicID != NULL)
1707 ret->PublicID = xmlStrdup(PublicID);
1708
1709 /*
1710 * Validity Check:
1711 * Check the DTD for previous declarations of the ATTLIST
1712 */
1713 if (xmlHashAddEntry(table, name, ret)) {
1714 xmlGenericError(xmlGenericErrorContext,
1715 "xmlAddNotationDecl: %s already defined\n", name);
1716 xmlFreeNotation(ret);
1717 return(NULL);
1718 }
1719 return(ret);
1720}
1721
1722/**
1723 * xmlFreeNotationTable:
1724 * @table: An notation table
1725 *
1726 * Deallocate the memory used by an entities hash table.
1727 */
1728void
1729xmlFreeNotationTable(xmlNotationTablePtr table) {
1730 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
1731}
1732
1733/**
1734 * xmlCopyNotation:
1735 * @nota: A notation
1736 *
1737 * Build a copy of a notation.
1738 *
1739 * Returns the new xmlNotationPtr or NULL in case of error.
1740 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001741static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001742xmlCopyNotation(xmlNotationPtr nota) {
1743 xmlNotationPtr cur;
1744
1745 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1746 if (cur == NULL) {
1747 xmlGenericError(xmlGenericErrorContext,
1748 "xmlCopyNotation: out of memory !\n");
1749 return(NULL);
1750 }
1751 if (nota->name != NULL)
1752 cur->name = xmlStrdup(nota->name);
1753 else
1754 cur->name = NULL;
1755 if (nota->PublicID != NULL)
1756 cur->PublicID = xmlStrdup(nota->PublicID);
1757 else
1758 cur->PublicID = NULL;
1759 if (nota->SystemID != NULL)
1760 cur->SystemID = xmlStrdup(nota->SystemID);
1761 else
1762 cur->SystemID = NULL;
1763 return(cur);
1764}
1765
1766/**
1767 * xmlCopyNotationTable:
1768 * @table: A notation table
1769 *
1770 * Build a copy of a notation table.
1771 *
1772 * Returns the new xmlNotationTablePtr or NULL in case of error.
1773 */
1774xmlNotationTablePtr
1775xmlCopyNotationTable(xmlNotationTablePtr table) {
1776 return((xmlNotationTablePtr) xmlHashCopy(table,
1777 (xmlHashCopier) xmlCopyNotation));
1778}
1779
1780/**
1781 * xmlDumpNotationDecl:
1782 * @buf: the XML buffer output
1783 * @nota: A notation declaration
1784 *
1785 * This will dump the content the notation declaration as an XML DTD definition
1786 */
1787void
1788xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
1789 xmlBufferWriteChar(buf, "<!NOTATION ");
1790 xmlBufferWriteCHAR(buf, nota->name);
1791 if (nota->PublicID != NULL) {
1792 xmlBufferWriteChar(buf, " PUBLIC ");
1793 xmlBufferWriteQuotedString(buf, nota->PublicID);
1794 if (nota->SystemID != NULL) {
1795 xmlBufferWriteChar(buf, " ");
1796 xmlBufferWriteCHAR(buf, nota->SystemID);
1797 }
1798 } else {
1799 xmlBufferWriteChar(buf, " SYSTEM ");
1800 xmlBufferWriteCHAR(buf, nota->SystemID);
1801 }
1802 xmlBufferWriteChar(buf, " >\n");
1803}
1804
1805/**
1806 * xmlDumpNotationTable:
1807 * @buf: the XML buffer output
1808 * @table: A notation table
1809 *
1810 * This will dump the content of the notation table as an XML DTD definition
1811 */
1812void
1813xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
1814 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
1815}
1816
1817/************************************************************************
1818 * *
1819 * IDs *
1820 * *
1821 ************************************************************************/
1822/**
1823 * xmlCreateIDTable:
1824 *
1825 * create and initialize an empty id hash table.
1826 *
1827 * Returns the xmlIDTablePtr just created or NULL in case
1828 * of error.
1829 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001830static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001831xmlCreateIDTable(void) {
1832 return(xmlHashCreate(0));
1833}
1834
1835/**
1836 * xmlFreeID:
1837 * @not: A id
1838 *
1839 * Deallocate the memory used by an id definition
1840 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001841static void
Owen Taylor3473f882001-02-23 17:55:21 +00001842xmlFreeID(xmlIDPtr id) {
1843 if (id == NULL) return;
1844 if (id->value != NULL)
1845 xmlFree((xmlChar *) id->value);
Owen Taylor3473f882001-02-23 17:55:21 +00001846 xmlFree(id);
1847}
1848
1849/**
1850 * xmlAddID:
1851 * @ctxt: the validation context
1852 * @doc: pointer to the document
1853 * @value: the value name
1854 * @attr: the attribute holding the ID
1855 *
1856 * Register a new id declaration
1857 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001858 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001859 */
1860xmlIDPtr
1861xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
1862 xmlAttrPtr attr) {
1863 xmlIDPtr ret;
1864 xmlIDTablePtr table;
1865
1866 if (doc == NULL) {
1867 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001868 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001869 return(NULL);
1870 }
1871 if (value == NULL) {
1872 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001873 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001874 return(NULL);
1875 }
1876 if (attr == NULL) {
1877 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001878 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001879 return(NULL);
1880 }
1881
1882 /*
1883 * Create the ID table if needed.
1884 */
1885 table = (xmlIDTablePtr) doc->ids;
1886 if (table == NULL)
1887 doc->ids = table = xmlCreateIDTable();
1888 if (table == NULL) {
1889 xmlGenericError(xmlGenericErrorContext,
1890 "xmlAddID: Table creation failed!\n");
1891 return(NULL);
1892 }
1893
1894 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
1895 if (ret == NULL) {
1896 xmlGenericError(xmlGenericErrorContext,
1897 "xmlAddID: out of memory\n");
1898 return(NULL);
1899 }
1900
1901 /*
1902 * fill the structure.
1903 */
1904 ret->value = xmlStrdup(value);
1905 ret->attr = attr;
1906
1907 if (xmlHashAddEntry(table, value, ret) < 0) {
1908 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001909 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001910 */
Daniel Veillard76575762002-09-05 14:21:15 +00001911 if (ctxt != NULL) {
1912 VECTXT(ctxt, attr->parent);
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001913 VERROR(ctxt->userData, "ID %s already defined\n", value);
Daniel Veillard76575762002-09-05 14:21:15 +00001914 }
Owen Taylor3473f882001-02-23 17:55:21 +00001915 xmlFreeID(ret);
1916 return(NULL);
1917 }
1918 return(ret);
1919}
1920
1921/**
1922 * xmlFreeIDTable:
1923 * @table: An id table
1924 *
1925 * Deallocate the memory used by an ID hash table.
1926 */
1927void
1928xmlFreeIDTable(xmlIDTablePtr table) {
1929 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
1930}
1931
1932/**
1933 * xmlIsID:
1934 * @doc: the document
1935 * @elem: the element carrying the attribute
1936 * @attr: the attribute
1937 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001938 * Determine whether an attribute is of type ID. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00001939 * then this is simple, otherwise we use an heuristic: name ID (upper
1940 * or lowercase).
1941 *
1942 * Returns 0 or 1 depending on the lookup result
1943 */
1944int
1945xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
1946 if (doc == NULL) return(0);
1947 if (attr == NULL) return(0);
1948 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
1949 return(0);
1950 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1951 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
1952 (xmlStrEqual(BAD_CAST "name", attr->name)))
1953 return(1);
1954 return(0);
1955 } else {
1956 xmlAttributePtr attrDecl;
1957
1958 if (elem == NULL) return(0);
Daniel Veillard37f961d2002-07-06 17:53:56 +00001959 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
1960 /*
1961 * TODO: this sucks ... recomputing this every time is stupid
1962 */
1963 int len = xmlStrlen(elem->name) + xmlStrlen(elem->ns->prefix) + 2;
1964 xmlChar *fullname;
1965
1966 fullname = xmlMalloc(len);
1967 if (fullname == NULL)
1968 return(0);
1969 snprintf((char *) fullname, len, "%s:%s", (char *) elem->ns->prefix,
1970 (char *) elem->name);
1971 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
1972 attr->name);
1973 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1974 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
1975 attr->name);
1976 xmlFree(fullname);
1977 } else {
1978 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
1979 attr->name);
1980 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1981 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
1982 attr->name);
1983 }
Owen Taylor3473f882001-02-23 17:55:21 +00001984
1985 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
1986 return(1);
1987 }
1988 return(0);
1989}
1990
1991/**
1992 * xmlRemoveID
1993 * @doc: the document
1994 * @attr: the attribute
1995 *
1996 * Remove the given attribute from the ID table maintained internally.
1997 *
1998 * Returns -1 if the lookup failed and 0 otherwise
1999 */
2000int
2001xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2002 xmlAttrPtr cur;
2003 xmlIDTablePtr table;
2004 xmlChar *ID;
2005
2006 if (doc == NULL) return(-1);
2007 if (attr == NULL) return(-1);
2008 table = (xmlIDTablePtr) doc->ids;
2009 if (table == NULL)
2010 return(-1);
2011
2012 if (attr == NULL)
2013 return(-1);
2014 ID = xmlNodeListGetString(doc, attr->children, 1);
2015 if (ID == NULL)
2016 return(-1);
2017 cur = xmlHashLookup(table, ID);
2018 if (cur != attr) {
2019 xmlFree(ID);
2020 return(-1);
2021 }
2022 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
2023 xmlFree(ID);
2024 return(0);
2025}
2026
2027/**
2028 * xmlGetID:
2029 * @doc: pointer to the document
2030 * @ID: the ID value
2031 *
2032 * Search the attribute declaring the given ID
2033 *
2034 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2035 */
2036xmlAttrPtr
2037xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2038 xmlIDTablePtr table;
2039 xmlIDPtr id;
2040
2041 if (doc == NULL) {
2042 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
2043 return(NULL);
2044 }
2045
2046 if (ID == NULL) {
2047 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
2048 return(NULL);
2049 }
2050
2051 table = (xmlIDTablePtr) doc->ids;
2052 if (table == NULL)
2053 return(NULL);
2054
2055 id = xmlHashLookup(table, ID);
2056 if (id == NULL)
2057 return(NULL);
2058 return(id->attr);
2059}
2060
2061/************************************************************************
2062 * *
2063 * Refs *
2064 * *
2065 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00002066typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00002067{
2068 xmlListPtr l;
2069 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002070} xmlRemoveMemo;
2071
2072typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2073
2074typedef struct xmlValidateMemo_t
2075{
2076 xmlValidCtxtPtr ctxt;
2077 const xmlChar *name;
2078} xmlValidateMemo;
2079
2080typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002081
2082/**
2083 * xmlCreateRefTable:
2084 *
2085 * create and initialize an empty ref hash table.
2086 *
2087 * Returns the xmlRefTablePtr just created or NULL in case
2088 * of error.
2089 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002090static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002091xmlCreateRefTable(void) {
2092 return(xmlHashCreate(0));
2093}
2094
2095/**
2096 * xmlFreeRef:
2097 * @lk: A list link
2098 *
2099 * Deallocate the memory used by a ref definition
2100 */
2101static void
2102xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002103 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2104 if (ref == NULL) return;
2105 if (ref->value != NULL)
2106 xmlFree((xmlChar *)ref->value);
2107 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002108}
2109
2110/**
2111 * xmlFreeRefList:
2112 * @list_ref: A list of references.
2113 *
2114 * Deallocate the memory used by a list of references
2115 */
2116static void
2117xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002118 if (list_ref == NULL) return;
2119 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002120}
2121
2122/**
2123 * xmlWalkRemoveRef:
2124 * @data: Contents of current link
2125 * @user: Value supplied by the user
2126 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002127 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002128 */
2129static int
2130xmlWalkRemoveRef(const void *data, const void *user)
2131{
Daniel Veillard37721922001-05-04 15:21:12 +00002132 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2133 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2134 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002135
Daniel Veillard37721922001-05-04 15:21:12 +00002136 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2137 xmlListRemoveFirst(ref_list, (void *)data);
2138 return 0;
2139 }
2140 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002141}
2142
2143/**
2144 * xmlAddRef:
2145 * @ctxt: the validation context
2146 * @doc: pointer to the document
2147 * @value: the value name
2148 * @attr: the attribute holding the Ref
2149 *
2150 * Register a new ref declaration
2151 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002152 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002153 */
2154xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002155xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002156 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002157 xmlRefPtr ret;
2158 xmlRefTablePtr table;
2159 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002160
Daniel Veillard37721922001-05-04 15:21:12 +00002161 if (doc == NULL) {
2162 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002163 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002164 return(NULL);
2165 }
2166 if (value == NULL) {
2167 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002168 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002169 return(NULL);
2170 }
2171 if (attr == NULL) {
2172 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002173 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002174 return(NULL);
2175 }
Owen Taylor3473f882001-02-23 17:55:21 +00002176
Daniel Veillard37721922001-05-04 15:21:12 +00002177 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002178 * Create the Ref table if needed.
2179 */
Daniel Veillard37721922001-05-04 15:21:12 +00002180 table = (xmlRefTablePtr) doc->refs;
2181 if (table == NULL)
2182 doc->refs = table = xmlCreateRefTable();
2183 if (table == NULL) {
2184 xmlGenericError(xmlGenericErrorContext,
2185 "xmlAddRef: Table creation failed!\n");
2186 return(NULL);
2187 }
Owen Taylor3473f882001-02-23 17:55:21 +00002188
Daniel Veillard37721922001-05-04 15:21:12 +00002189 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2190 if (ret == NULL) {
2191 xmlGenericError(xmlGenericErrorContext,
2192 "xmlAddRef: out of memory\n");
2193 return(NULL);
2194 }
Owen Taylor3473f882001-02-23 17:55:21 +00002195
Daniel Veillard37721922001-05-04 15:21:12 +00002196 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002197 * fill the structure.
2198 */
Daniel Veillard37721922001-05-04 15:21:12 +00002199 ret->value = xmlStrdup(value);
2200 ret->attr = attr;
Owen Taylor3473f882001-02-23 17:55:21 +00002201
Daniel Veillard37721922001-05-04 15:21:12 +00002202 /* To add a reference :-
2203 * References are maintained as a list of references,
2204 * Lookup the entry, if no entry create new nodelist
2205 * Add the owning node to the NodeList
2206 * Return the ref
2207 */
Owen Taylor3473f882001-02-23 17:55:21 +00002208
Daniel Veillard37721922001-05-04 15:21:12 +00002209 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2210 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2211 xmlGenericError(xmlGenericErrorContext,
2212 "xmlAddRef: Reference list creation failed!\n");
2213 return(NULL);
2214 }
2215 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2216 xmlListDelete(ref_list);
2217 xmlGenericError(xmlGenericErrorContext,
2218 "xmlAddRef: Reference list insertion failed!\n");
2219 return(NULL);
2220 }
2221 }
2222 xmlListInsert(ref_list, ret);
2223 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002224}
2225
2226/**
2227 * xmlFreeRefTable:
2228 * @table: An ref table
2229 *
2230 * Deallocate the memory used by an Ref hash table.
2231 */
2232void
2233xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002234 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002235}
2236
2237/**
2238 * xmlIsRef:
2239 * @doc: the document
2240 * @elem: the element carrying the attribute
2241 * @attr: the attribute
2242 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002243 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002244 * then this is simple, otherwise we use an heuristic: name Ref (upper
2245 * or lowercase).
2246 *
2247 * Returns 0 or 1 depending on the lookup result
2248 */
2249int
2250xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002251 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2252 return(0);
2253 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2254 /* TODO @@@ */
2255 return(0);
2256 } else {
2257 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002258
Daniel Veillard37721922001-05-04 15:21:12 +00002259 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2260 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2261 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2262 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002263
Daniel Veillard37721922001-05-04 15:21:12 +00002264 if ((attrDecl != NULL) &&
2265 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2266 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2267 return(1);
2268 }
2269 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002270}
2271
2272/**
2273 * xmlRemoveRef
2274 * @doc: the document
2275 * @attr: the attribute
2276 *
2277 * Remove the given attribute from the Ref table maintained internally.
2278 *
2279 * Returns -1 if the lookup failed and 0 otherwise
2280 */
2281int
2282xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002283 xmlListPtr ref_list;
2284 xmlRefTablePtr table;
2285 xmlChar *ID;
2286 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002287
Daniel Veillard37721922001-05-04 15:21:12 +00002288 if (doc == NULL) return(-1);
2289 if (attr == NULL) return(-1);
2290 table = (xmlRefTablePtr) doc->refs;
2291 if (table == NULL)
2292 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002293
Daniel Veillard37721922001-05-04 15:21:12 +00002294 if (attr == NULL)
2295 return(-1);
2296 ID = xmlNodeListGetString(doc, attr->children, 1);
2297 if (ID == NULL)
2298 return(-1);
2299 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002300
Daniel Veillard37721922001-05-04 15:21:12 +00002301 if(ref_list == NULL) {
2302 xmlFree(ID);
2303 return (-1);
2304 }
2305 /* At this point, ref_list refers to a list of references which
2306 * have the same key as the supplied attr. Our list of references
2307 * is ordered by reference address and we don't have that information
2308 * here to use when removing. We'll have to walk the list and
2309 * check for a matching attribute, when we find one stop the walk
2310 * and remove the entry.
2311 * The list is ordered by reference, so that means we don't have the
2312 * key. Passing the list and the reference to the walker means we
2313 * will have enough data to be able to remove the entry.
2314 */
2315 target.l = ref_list;
2316 target.ap = attr;
2317
2318 /* Remove the supplied attr from our list */
2319 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002320
Daniel Veillard37721922001-05-04 15:21:12 +00002321 /*If the list is empty then remove the list entry in the hash */
2322 if (xmlListEmpty(ref_list))
2323 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2324 xmlFreeRefList);
2325 xmlFree(ID);
2326 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002327}
2328
2329/**
2330 * xmlGetRefs:
2331 * @doc: pointer to the document
2332 * @ID: the ID value
2333 *
2334 * Find the set of references for the supplied ID.
2335 *
2336 * Returns NULL if not found, otherwise node set for the ID.
2337 */
2338xmlListPtr
2339xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002340 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002341
Daniel Veillard37721922001-05-04 15:21:12 +00002342 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002343 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002344 return(NULL);
2345 }
Owen Taylor3473f882001-02-23 17:55:21 +00002346
Daniel Veillard37721922001-05-04 15:21:12 +00002347 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002348 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002349 return(NULL);
2350 }
Owen Taylor3473f882001-02-23 17:55:21 +00002351
Daniel Veillard37721922001-05-04 15:21:12 +00002352 table = (xmlRefTablePtr) doc->refs;
2353 if (table == NULL)
2354 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002355
Daniel Veillard37721922001-05-04 15:21:12 +00002356 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002357}
2358
2359/************************************************************************
2360 * *
2361 * Routines for validity checking *
2362 * *
2363 ************************************************************************/
2364
2365/**
2366 * xmlGetDtdElementDesc:
2367 * @dtd: a pointer to the DtD to search
2368 * @name: the element name
2369 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002370 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002371 *
2372 * returns the xmlElementPtr if found or NULL
2373 */
2374
2375xmlElementPtr
2376xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2377 xmlElementTablePtr table;
2378 xmlElementPtr cur;
2379 xmlChar *uqname = NULL, *prefix = NULL;
2380
2381 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002382 if (dtd->elements == NULL)
2383 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002384 table = (xmlElementTablePtr) dtd->elements;
2385
2386 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002387 if (uqname != NULL)
2388 name = uqname;
2389 cur = xmlHashLookup2(table, name, prefix);
2390 if (prefix != NULL) xmlFree(prefix);
2391 if (uqname != NULL) xmlFree(uqname);
2392 return(cur);
2393}
2394/**
2395 * xmlGetDtdElementDesc2:
2396 * @dtd: a pointer to the DtD to search
2397 * @name: the element name
2398 * @create: create an empty description if not found
2399 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002400 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002401 *
2402 * returns the xmlElementPtr if found or NULL
2403 */
2404
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002405static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002406xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2407 xmlElementTablePtr table;
2408 xmlElementPtr cur;
2409 xmlChar *uqname = NULL, *prefix = NULL;
2410
2411 if (dtd == NULL) return(NULL);
2412 if (dtd->elements == NULL) {
2413 if (!create)
2414 return(NULL);
2415 /*
2416 * Create the Element table if needed.
2417 */
2418 table = (xmlElementTablePtr) dtd->elements;
2419 if (table == NULL) {
2420 table = xmlCreateElementTable();
2421 dtd->elements = (void *) table;
2422 }
2423 if (table == NULL) {
2424 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002425 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002426 return(NULL);
2427 }
2428 }
2429 table = (xmlElementTablePtr) dtd->elements;
2430
2431 uqname = xmlSplitQName2(name, &prefix);
2432 if (uqname != NULL)
2433 name = uqname;
2434 cur = xmlHashLookup2(table, name, prefix);
2435 if ((cur == NULL) && (create)) {
2436 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2437 if (cur == NULL) {
2438 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002439 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002440 return(NULL);
2441 }
2442 memset(cur, 0, sizeof(xmlElement));
2443 cur->type = XML_ELEMENT_DECL;
2444
2445 /*
2446 * fill the structure.
2447 */
2448 cur->name = xmlStrdup(name);
2449 cur->prefix = xmlStrdup(prefix);
2450 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2451
2452 xmlHashAddEntry2(table, name, prefix, cur);
2453 }
2454 if (prefix != NULL) xmlFree(prefix);
2455 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002456 return(cur);
2457}
2458
2459/**
2460 * xmlGetDtdQElementDesc:
2461 * @dtd: a pointer to the DtD to search
2462 * @name: the element name
2463 * @prefix: the element namespace prefix
2464 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002465 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002466 *
2467 * returns the xmlElementPtr if found or NULL
2468 */
2469
Daniel Veillard48da9102001-08-07 01:10:10 +00002470xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002471xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2472 const xmlChar *prefix) {
2473 xmlElementTablePtr table;
2474
2475 if (dtd == NULL) return(NULL);
2476 if (dtd->elements == NULL) return(NULL);
2477 table = (xmlElementTablePtr) dtd->elements;
2478
2479 return(xmlHashLookup2(table, name, prefix));
2480}
2481
2482/**
2483 * xmlGetDtdAttrDesc:
2484 * @dtd: a pointer to the DtD to search
2485 * @elem: the element name
2486 * @name: the attribute name
2487 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002488 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002489 * this element.
2490 *
2491 * returns the xmlAttributePtr if found or NULL
2492 */
2493
2494xmlAttributePtr
2495xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2496 xmlAttributeTablePtr table;
2497 xmlAttributePtr cur;
2498 xmlChar *uqname = NULL, *prefix = NULL;
2499
2500 if (dtd == NULL) return(NULL);
2501 if (dtd->attributes == NULL) return(NULL);
2502
2503 table = (xmlAttributeTablePtr) dtd->attributes;
2504 if (table == NULL)
2505 return(NULL);
2506
2507 uqname = xmlSplitQName2(name, &prefix);
2508
2509 if (uqname != NULL) {
2510 cur = xmlHashLookup3(table, uqname, prefix, elem);
2511 if (prefix != NULL) xmlFree(prefix);
2512 if (uqname != NULL) xmlFree(uqname);
2513 } else
2514 cur = xmlHashLookup3(table, name, NULL, elem);
2515 return(cur);
2516}
2517
2518/**
2519 * xmlGetDtdQAttrDesc:
2520 * @dtd: a pointer to the DtD to search
2521 * @elem: the element name
2522 * @name: the attribute name
2523 * @prefix: the attribute namespace prefix
2524 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002525 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002526 * this element.
2527 *
2528 * returns the xmlAttributePtr if found or NULL
2529 */
2530
Daniel Veillard48da9102001-08-07 01:10:10 +00002531xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002532xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2533 const xmlChar *prefix) {
2534 xmlAttributeTablePtr table;
2535
2536 if (dtd == NULL) return(NULL);
2537 if (dtd->attributes == NULL) return(NULL);
2538 table = (xmlAttributeTablePtr) dtd->attributes;
2539
2540 return(xmlHashLookup3(table, name, prefix, elem));
2541}
2542
2543/**
2544 * xmlGetDtdNotationDesc:
2545 * @dtd: a pointer to the DtD to search
2546 * @name: the notation name
2547 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002548 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002549 *
2550 * returns the xmlNotationPtr if found or NULL
2551 */
2552
2553xmlNotationPtr
2554xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2555 xmlNotationTablePtr table;
2556
2557 if (dtd == NULL) return(NULL);
2558 if (dtd->notations == NULL) return(NULL);
2559 table = (xmlNotationTablePtr) dtd->notations;
2560
2561 return(xmlHashLookup(table, name));
2562}
2563
2564/**
2565 * xmlValidateNotationUse:
2566 * @ctxt: the validation context
2567 * @doc: the document
2568 * @notationName: the notation name to check
2569 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002570 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002571 * - [ VC: Notation Declared ]
2572 *
2573 * returns 1 if valid or 0 otherwise
2574 */
2575
2576int
2577xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2578 const xmlChar *notationName) {
2579 xmlNotationPtr notaDecl;
2580 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2581
2582 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2583 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2584 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2585
2586 if (notaDecl == NULL) {
2587 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2588 notationName);
2589 return(0);
2590 }
2591 return(1);
2592}
2593
2594/**
2595 * xmlIsMixedElement
2596 * @doc: the document
2597 * @name: the element name
2598 *
2599 * Search in the DtDs whether an element accept Mixed content (or ANY)
2600 * basically if it is supposed to accept text childs
2601 *
2602 * returns 0 if no, 1 if yes, and -1 if no element description is available
2603 */
2604
2605int
2606xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2607 xmlElementPtr elemDecl;
2608
2609 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2610
2611 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2612 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2613 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2614 if (elemDecl == NULL) return(-1);
2615 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002616 case XML_ELEMENT_TYPE_UNDEFINED:
2617 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002618 case XML_ELEMENT_TYPE_ELEMENT:
2619 return(0);
2620 case XML_ELEMENT_TYPE_EMPTY:
2621 /*
2622 * return 1 for EMPTY since we want VC error to pop up
2623 * on <empty> </empty> for example
2624 */
2625 case XML_ELEMENT_TYPE_ANY:
2626 case XML_ELEMENT_TYPE_MIXED:
2627 return(1);
2628 }
2629 return(1);
2630}
2631
2632/**
2633 * xmlValidateNameValue:
2634 * @value: an Name value
2635 *
2636 * Validate that the given value match Name production
2637 *
2638 * returns 1 if valid or 0 otherwise
2639 */
2640
Daniel Veillard9b731d72002-04-14 12:56:08 +00002641int
Owen Taylor3473f882001-02-23 17:55:21 +00002642xmlValidateNameValue(const xmlChar *value) {
2643 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002644 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002645
2646 if (value == NULL) return(0);
2647 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002648 val = xmlStringCurrentChar(NULL, cur, &len);
2649 cur += len;
2650 if (!IS_LETTER(val) && (val != '_') &&
2651 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002652 return(0);
2653 }
2654
Daniel Veillardd8224e02002-01-13 15:43:22 +00002655 val = xmlStringCurrentChar(NULL, cur, &len);
2656 cur += len;
2657 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2658 (val == '.') || (val == '-') ||
2659 (val == '_') || (val == ':') ||
2660 (IS_COMBINING(val)) ||
2661 (IS_EXTENDER(val))) {
2662 val = xmlStringCurrentChar(NULL, cur, &len);
2663 cur += len;
2664 }
Owen Taylor3473f882001-02-23 17:55:21 +00002665
Daniel Veillardd8224e02002-01-13 15:43:22 +00002666 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002667
2668 return(1);
2669}
2670
2671/**
2672 * xmlValidateNamesValue:
2673 * @value: an Names value
2674 *
2675 * Validate that the given value match Names production
2676 *
2677 * returns 1 if valid or 0 otherwise
2678 */
2679
Daniel Veillard9b731d72002-04-14 12:56:08 +00002680int
Owen Taylor3473f882001-02-23 17:55:21 +00002681xmlValidateNamesValue(const xmlChar *value) {
2682 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002683 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002684
2685 if (value == NULL) return(0);
2686 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002687 val = xmlStringCurrentChar(NULL, cur, &len);
2688 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002689
Daniel Veillardd8224e02002-01-13 15:43:22 +00002690 if (!IS_LETTER(val) && (val != '_') &&
2691 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002692 return(0);
2693 }
2694
Daniel Veillardd8224e02002-01-13 15:43:22 +00002695 val = xmlStringCurrentChar(NULL, cur, &len);
2696 cur += len;
2697 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2698 (val == '.') || (val == '-') ||
2699 (val == '_') || (val == ':') ||
2700 (IS_COMBINING(val)) ||
2701 (IS_EXTENDER(val))) {
2702 val = xmlStringCurrentChar(NULL, cur, &len);
2703 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002704 }
2705
Daniel Veillardd8224e02002-01-13 15:43:22 +00002706 while (IS_BLANK(val)) {
2707 while (IS_BLANK(val)) {
2708 val = xmlStringCurrentChar(NULL, cur, &len);
2709 cur += len;
2710 }
2711
2712 if (!IS_LETTER(val) && (val != '_') &&
2713 (val != ':')) {
2714 return(0);
2715 }
2716 val = xmlStringCurrentChar(NULL, cur, &len);
2717 cur += len;
2718
2719 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2720 (val == '.') || (val == '-') ||
2721 (val == '_') || (val == ':') ||
2722 (IS_COMBINING(val)) ||
2723 (IS_EXTENDER(val))) {
2724 val = xmlStringCurrentChar(NULL, cur, &len);
2725 cur += len;
2726 }
2727 }
2728
2729 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002730
2731 return(1);
2732}
2733
2734/**
2735 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002736 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00002737 *
2738 * Validate that the given value match Nmtoken production
2739 *
2740 * [ VC: Name Token ]
2741 *
2742 * returns 1 if valid or 0 otherwise
2743 */
2744
Daniel Veillard9b731d72002-04-14 12:56:08 +00002745int
Owen Taylor3473f882001-02-23 17:55:21 +00002746xmlValidateNmtokenValue(const xmlChar *value) {
2747 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002748 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002749
2750 if (value == NULL) return(0);
2751 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002752 val = xmlStringCurrentChar(NULL, cur, &len);
2753 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002754
Daniel Veillardd8224e02002-01-13 15:43:22 +00002755 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2756 (val != '.') && (val != '-') &&
2757 (val != '_') && (val != ':') &&
2758 (!IS_COMBINING(val)) &&
2759 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00002760 return(0);
2761
Daniel Veillardd8224e02002-01-13 15:43:22 +00002762 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2763 (val == '.') || (val == '-') ||
2764 (val == '_') || (val == ':') ||
2765 (IS_COMBINING(val)) ||
2766 (IS_EXTENDER(val))) {
2767 val = xmlStringCurrentChar(NULL, cur, &len);
2768 cur += len;
2769 }
Owen Taylor3473f882001-02-23 17:55:21 +00002770
Daniel Veillardd8224e02002-01-13 15:43:22 +00002771 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002772
2773 return(1);
2774}
2775
2776/**
2777 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002778 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00002779 *
2780 * Validate that the given value match Nmtokens production
2781 *
2782 * [ VC: Name Token ]
2783 *
2784 * returns 1 if valid or 0 otherwise
2785 */
2786
Daniel Veillard9b731d72002-04-14 12:56:08 +00002787int
Owen Taylor3473f882001-02-23 17:55:21 +00002788xmlValidateNmtokensValue(const xmlChar *value) {
2789 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002790 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002791
2792 if (value == NULL) return(0);
2793 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002794 val = xmlStringCurrentChar(NULL, cur, &len);
2795 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002796
Daniel Veillardd8224e02002-01-13 15:43:22 +00002797 while (IS_BLANK(val)) {
2798 val = xmlStringCurrentChar(NULL, cur, &len);
2799 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002800 }
2801
Daniel Veillardd8224e02002-01-13 15:43:22 +00002802 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2803 (val != '.') && (val != '-') &&
2804 (val != '_') && (val != ':') &&
2805 (!IS_COMBINING(val)) &&
2806 (!IS_EXTENDER(val)))
2807 return(0);
2808
2809 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2810 (val == '.') || (val == '-') ||
2811 (val == '_') || (val == ':') ||
2812 (IS_COMBINING(val)) ||
2813 (IS_EXTENDER(val))) {
2814 val = xmlStringCurrentChar(NULL, cur, &len);
2815 cur += len;
2816 }
2817
2818 while (IS_BLANK(val)) {
2819 while (IS_BLANK(val)) {
2820 val = xmlStringCurrentChar(NULL, cur, &len);
2821 cur += len;
2822 }
2823 if (val == 0) return(1);
2824
2825 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2826 (val != '.') && (val != '-') &&
2827 (val != '_') && (val != ':') &&
2828 (!IS_COMBINING(val)) &&
2829 (!IS_EXTENDER(val)))
2830 return(0);
2831
2832 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2833 (val == '.') || (val == '-') ||
2834 (val == '_') || (val == ':') ||
2835 (IS_COMBINING(val)) ||
2836 (IS_EXTENDER(val))) {
2837 val = xmlStringCurrentChar(NULL, cur, &len);
2838 cur += len;
2839 }
2840 }
2841
2842 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002843
2844 return(1);
2845}
2846
2847/**
2848 * xmlValidateNotationDecl:
2849 * @ctxt: the validation context
2850 * @doc: a document instance
2851 * @nota: a notation definition
2852 *
2853 * Try to validate a single notation definition
2854 * basically it does the following checks as described by the
2855 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002856 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00002857 * But this function get called anyway ...
2858 *
2859 * returns 1 if valid or 0 otherwise
2860 */
2861
2862int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002863xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
2864 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002865 int ret = 1;
2866
2867 return(ret);
2868}
2869
2870/**
2871 * xmlValidateAttributeValue:
2872 * @type: an attribute type
2873 * @value: an attribute value
2874 *
2875 * Validate that the given attribute value match the proper production
2876 *
2877 * [ VC: ID ]
2878 * Values of type ID must match the Name production....
2879 *
2880 * [ VC: IDREF ]
2881 * Values of type IDREF must match the Name production, and values
2882 * of type IDREFS must match Names ...
2883 *
2884 * [ VC: Entity Name ]
2885 * Values of type ENTITY must match the Name production, values
2886 * of type ENTITIES must match Names ...
2887 *
2888 * [ VC: Name Token ]
2889 * Values of type NMTOKEN must match the Nmtoken production; values
2890 * of type NMTOKENS must match Nmtokens.
2891 *
2892 * returns 1 if valid or 0 otherwise
2893 */
2894
2895int
2896xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
2897 switch (type) {
2898 case XML_ATTRIBUTE_ENTITIES:
2899 case XML_ATTRIBUTE_IDREFS:
2900 return(xmlValidateNamesValue(value));
2901 case XML_ATTRIBUTE_ENTITY:
2902 case XML_ATTRIBUTE_IDREF:
2903 case XML_ATTRIBUTE_ID:
2904 case XML_ATTRIBUTE_NOTATION:
2905 return(xmlValidateNameValue(value));
2906 case XML_ATTRIBUTE_NMTOKENS:
2907 case XML_ATTRIBUTE_ENUMERATION:
2908 return(xmlValidateNmtokensValue(value));
2909 case XML_ATTRIBUTE_NMTOKEN:
2910 return(xmlValidateNmtokenValue(value));
2911 case XML_ATTRIBUTE_CDATA:
2912 break;
2913 }
2914 return(1);
2915}
2916
2917/**
2918 * xmlValidateAttributeValue2:
2919 * @ctxt: the validation context
2920 * @doc: the document
2921 * @name: the attribute name (used for error reporting only)
2922 * @type: the attribute type
2923 * @value: the attribute value
2924 *
2925 * Validate that the given attribute value match a given type.
2926 * This typically cannot be done before having finished parsing
2927 * the subsets.
2928 *
2929 * [ VC: IDREF ]
2930 * Values of type IDREF must match one of the declared IDs
2931 * Values of type IDREFS must match a sequence of the declared IDs
2932 * each Name must match the value of an ID attribute on some element
2933 * in the XML document; i.e. IDREF values must match the value of
2934 * some ID attribute
2935 *
2936 * [ VC: Entity Name ]
2937 * Values of type ENTITY must match one declared entity
2938 * Values of type ENTITIES must match a sequence of declared entities
2939 *
2940 * [ VC: Notation Attributes ]
2941 * all notation names in the declaration must be declared.
2942 *
2943 * returns 1 if valid or 0 otherwise
2944 */
2945
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002946static int
Owen Taylor3473f882001-02-23 17:55:21 +00002947xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2948 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
2949 int ret = 1;
2950 switch (type) {
2951 case XML_ATTRIBUTE_IDREFS:
2952 case XML_ATTRIBUTE_IDREF:
2953 case XML_ATTRIBUTE_ID:
2954 case XML_ATTRIBUTE_NMTOKENS:
2955 case XML_ATTRIBUTE_ENUMERATION:
2956 case XML_ATTRIBUTE_NMTOKEN:
2957 case XML_ATTRIBUTE_CDATA:
2958 break;
2959 case XML_ATTRIBUTE_ENTITY: {
2960 xmlEntityPtr ent;
2961
2962 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00002963 if ((ent == NULL) && (doc->standalone == 1)) {
2964 doc->standalone = 0;
2965 ent = xmlGetDocEntity(doc, value);
2966 if (ent != NULL) {
2967 VERROR(ctxt->userData,
2968"standalone problem: attribute %s reference entity \"%s\" in external subset\n",
2969 name, value);
2970 /* WAIT to get answer from the Core WG on this
2971 ret = 0;
2972 */
2973 }
2974 }
Owen Taylor3473f882001-02-23 17:55:21 +00002975 if (ent == NULL) {
2976 VERROR(ctxt->userData,
2977 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2978 name, value);
2979 ret = 0;
2980 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2981 VERROR(ctxt->userData,
2982 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2983 name, value);
2984 ret = 0;
2985 }
2986 break;
2987 }
2988 case XML_ATTRIBUTE_ENTITIES: {
2989 xmlChar *dup, *nam = NULL, *cur, save;
2990 xmlEntityPtr ent;
2991
2992 dup = xmlStrdup(value);
2993 if (dup == NULL)
2994 return(0);
2995 cur = dup;
2996 while (*cur != 0) {
2997 nam = cur;
2998 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2999 save = *cur;
3000 *cur = 0;
3001 ent = xmlGetDocEntity(doc, nam);
3002 if (ent == NULL) {
3003 VERROR(ctxt->userData,
3004 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3005 name, nam);
3006 ret = 0;
3007 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3008 VERROR(ctxt->userData,
3009 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3010 name, nam);
3011 ret = 0;
3012 }
3013 if (save == 0)
3014 break;
3015 *cur = save;
3016 while (IS_BLANK(*cur)) cur++;
3017 }
3018 xmlFree(dup);
3019 break;
3020 }
3021 case XML_ATTRIBUTE_NOTATION: {
3022 xmlNotationPtr nota;
3023
3024 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3025 if ((nota == NULL) && (doc->extSubset != NULL))
3026 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3027
3028 if (nota == NULL) {
3029 VERROR(ctxt->userData,
3030 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3031 name, value);
3032 ret = 0;
3033 }
3034 break;
3035 }
3036 }
3037 return(ret);
3038}
3039
3040/**
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003041 * xmlValidCtxtNormalizeAttributeValue:
3042 * @ctxt: the validation context
3043 * @doc: the document
3044 * @elem: the parent
3045 * @name: the attribute name
3046 * @value: the attribute value
3047 * @ctxt: the validation context or NULL
3048 *
3049 * Does the validation related extra step of the normalization of attribute
3050 * values:
3051 *
3052 * If the declared value is not CDATA, then the XML processor must further
3053 * process the normalized attribute value by discarding any leading and
3054 * trailing space (#x20) characters, and by replacing sequences of space
3055 * (#x20) characters by single space (#x20) character.
3056 *
3057 * Also check VC: Standalone Document Declaration in P32, and update
3058 * ctxt->valid accordingly
3059 *
3060 * returns a new normalized string if normalization is needed, NULL otherwise
3061 * the caller must free the returned value.
3062 */
3063
3064xmlChar *
3065xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3066 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3067 xmlChar *ret, *dst;
3068 const xmlChar *src;
3069 xmlAttributePtr attrDecl = NULL;
3070 int extsubset = 0;
3071
3072 if (doc == NULL) return(NULL);
3073 if (elem == NULL) return(NULL);
3074 if (name == NULL) return(NULL);
3075 if (value == NULL) return(NULL);
3076
3077 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3078 xmlChar qname[500];
3079 snprintf((char *) qname, sizeof(qname), "%s:%s",
3080 elem->ns->prefix, elem->name);
3081 qname[sizeof(qname) - 1] = 0;
3082 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3083 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3084 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3085 if (attrDecl != NULL)
3086 extsubset = 1;
3087 }
3088 }
3089 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3090 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3091 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3092 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3093 if (attrDecl != NULL)
3094 extsubset = 1;
3095 }
3096
3097 if (attrDecl == NULL)
3098 return(NULL);
3099 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3100 return(NULL);
3101
3102 ret = xmlStrdup(value);
3103 if (ret == NULL)
3104 return(NULL);
3105 src = value;
3106 dst = ret;
3107 while (*src == 0x20) src++;
3108 while (*src != 0) {
3109 if (*src == 0x20) {
3110 while (*src == 0x20) src++;
3111 if (*src != 0)
3112 *dst++ = 0x20;
3113 } else {
3114 *dst++ = *src++;
3115 }
3116 }
3117 *dst = 0;
3118 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3119 VERROR(ctxt->userData,
3120"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3121 name, elem->name);
3122 ctxt->valid = 0;
3123 }
3124 return(ret);
3125}
3126
3127/**
Owen Taylor3473f882001-02-23 17:55:21 +00003128 * xmlValidNormalizeAttributeValue:
3129 * @doc: the document
3130 * @elem: the parent
3131 * @name: the attribute name
3132 * @value: the attribute value
Daniel Veillard8dc16a62002-02-19 21:08:48 +00003133 * @ctxt: the validation context or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00003134 *
3135 * Does the validation related extra step of the normalization of attribute
3136 * values:
3137 *
3138 * If the declared value is not CDATA, then the XML processor must further
3139 * process the normalized attribute value by discarding any leading and
3140 * trailing space (#x20) characters, and by replacing sequences of space
3141 * (#x20) characters by single space (#x20) character.
3142 *
3143 * returns a new normalized string if normalization is needed, NULL otherwise
3144 * the caller must free the returned value.
3145 */
3146
3147xmlChar *
3148xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3149 const xmlChar *name, const xmlChar *value) {
3150 xmlChar *ret, *dst;
3151 const xmlChar *src;
3152 xmlAttributePtr attrDecl = NULL;
3153
3154 if (doc == NULL) return(NULL);
3155 if (elem == NULL) return(NULL);
3156 if (name == NULL) return(NULL);
3157 if (value == NULL) return(NULL);
3158
3159 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3160 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003161 snprintf((char *) qname, sizeof(qname), "%s:%s",
3162 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003163 qname[sizeof(qname) - 1] = 0;
3164 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3165 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3166 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3167 }
3168 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3169 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3170 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3171
3172 if (attrDecl == NULL)
3173 return(NULL);
3174 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3175 return(NULL);
3176
3177 ret = xmlStrdup(value);
3178 if (ret == NULL)
3179 return(NULL);
3180 src = value;
3181 dst = ret;
3182 while (*src == 0x20) src++;
3183 while (*src != 0) {
3184 if (*src == 0x20) {
3185 while (*src == 0x20) src++;
3186 if (*src != 0)
3187 *dst++ = 0x20;
3188 } else {
3189 *dst++ = *src++;
3190 }
3191 }
3192 *dst = 0;
3193 return(ret);
3194}
3195
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003196static void
Owen Taylor3473f882001-02-23 17:55:21 +00003197xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003198 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003199 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3200}
3201
3202/**
3203 * xmlValidateAttributeDecl:
3204 * @ctxt: the validation context
3205 * @doc: a document instance
3206 * @attr: an attribute definition
3207 *
3208 * Try to validate a single attribute definition
3209 * basically it does the following checks as described by the
3210 * XML-1.0 recommendation:
3211 * - [ VC: Attribute Default Legal ]
3212 * - [ VC: Enumeration ]
3213 * - [ VC: ID Attribute Default ]
3214 *
3215 * The ID/IDREF uniqueness and matching are done separately
3216 *
3217 * returns 1 if valid or 0 otherwise
3218 */
3219
3220int
3221xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3222 xmlAttributePtr attr) {
3223 int ret = 1;
3224 int val;
3225 CHECK_DTD;
3226 if(attr == NULL) return(1);
3227
3228 /* Attribute Default Legal */
3229 /* Enumeration */
3230 if (attr->defaultValue != NULL) {
3231 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3232 if (val == 0) {
3233 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003234 "Syntax of default value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003235 attr->name, attr->elem);
3236 }
3237 ret &= val;
3238 }
3239
3240 /* ID Attribute Default */
3241 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3242 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3243 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3244 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003245 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003246 attr->name, attr->elem);
3247 ret = 0;
3248 }
3249
3250 /* One ID per Element Type */
3251 if (attr->atype == XML_ATTRIBUTE_ID) {
3252 int nbId;
3253
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003254 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003255 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3256 attr->elem);
3257 if (elem != NULL) {
3258 nbId = xmlScanIDAttributeDecl(NULL, elem);
3259 } else {
3260 xmlAttributeTablePtr table;
3261
3262 /*
3263 * The attribute may be declared in the internal subset and the
3264 * element in the external subset.
3265 */
3266 nbId = 0;
3267 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3268 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3269 xmlValidateAttributeIdCallback, &nbId);
3270 }
3271 if (nbId > 1) {
3272 VERROR(ctxt->userData,
3273 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3274 attr->elem, nbId, attr->name);
3275 } else if (doc->extSubset != NULL) {
3276 int extId = 0;
3277 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3278 if (elem != NULL) {
3279 extId = xmlScanIDAttributeDecl(NULL, elem);
3280 }
3281 if (extId > 1) {
3282 VERROR(ctxt->userData,
3283 "Element %s has %d ID attribute defined in the external subset : %s\n",
3284 attr->elem, extId, attr->name);
3285 } else if (extId + nbId > 1) {
3286 VERROR(ctxt->userData,
3287"Element %s has ID attributes defined in the internal and external subset : %s\n",
3288 attr->elem, attr->name);
3289 }
3290 }
3291 }
3292
3293 /* Validity Constraint: Enumeration */
3294 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3295 xmlEnumerationPtr tree = attr->tree;
3296 while (tree != NULL) {
3297 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3298 tree = tree->next;
3299 }
3300 if (tree == NULL) {
3301 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003302"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003303 attr->defaultValue, attr->name, attr->elem);
3304 ret = 0;
3305 }
3306 }
3307
3308 return(ret);
3309}
3310
3311/**
3312 * xmlValidateElementDecl:
3313 * @ctxt: the validation context
3314 * @doc: a document instance
3315 * @elem: an element definition
3316 *
3317 * Try to validate a single element definition
3318 * basically it does the following checks as described by the
3319 * XML-1.0 recommendation:
3320 * - [ VC: One ID per Element Type ]
3321 * - [ VC: No Duplicate Types ]
3322 * - [ VC: Unique Element Type Declaration ]
3323 *
3324 * returns 1 if valid or 0 otherwise
3325 */
3326
3327int
3328xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3329 xmlElementPtr elem) {
3330 int ret = 1;
3331 xmlElementPtr tst;
3332
3333 CHECK_DTD;
3334
3335 if (elem == NULL) return(1);
3336
3337 /* No Duplicate Types */
3338 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3339 xmlElementContentPtr cur, next;
3340 const xmlChar *name;
3341
3342 cur = elem->content;
3343 while (cur != NULL) {
3344 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3345 if (cur->c1 == NULL) break;
3346 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3347 name = cur->c1->name;
3348 next = cur->c2;
3349 while (next != NULL) {
3350 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3351 if (xmlStrEqual(next->name, name)) {
3352 VERROR(ctxt->userData,
3353 "Definition of %s has duplicate references of %s\n",
3354 elem->name, name);
3355 ret = 0;
3356 }
3357 break;
3358 }
3359 if (next->c1 == NULL) break;
3360 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3361 if (xmlStrEqual(next->c1->name, name)) {
3362 VERROR(ctxt->userData,
3363 "Definition of %s has duplicate references of %s\n",
3364 elem->name, name);
3365 ret = 0;
3366 }
3367 next = next->c2;
3368 }
3369 }
3370 cur = cur->c2;
3371 }
3372 }
3373
3374 /* VC: Unique Element Type Declaration */
3375 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003376 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003377 ((tst->prefix == elem->prefix) ||
3378 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003379 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003380 VERROR(ctxt->userData, "Redefinition of element %s\n",
3381 elem->name);
3382 ret = 0;
3383 }
3384 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003385 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003386 ((tst->prefix == elem->prefix) ||
3387 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003388 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003389 VERROR(ctxt->userData, "Redefinition of element %s\n",
3390 elem->name);
3391 ret = 0;
3392 }
Daniel Veillarda10efa82001-04-18 13:09:01 +00003393 /* One ID per Element Type
3394 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003395 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3396 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003397 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003398 return(ret);
3399}
3400
3401/**
3402 * xmlValidateOneAttribute:
3403 * @ctxt: the validation context
3404 * @doc: a document instance
3405 * @elem: an element instance
3406 * @attr: an attribute instance
3407 * @value: the attribute value (without entities processing)
3408 *
3409 * Try to validate a single attribute for an element
3410 * basically it does the following checks as described by the
3411 * XML-1.0 recommendation:
3412 * - [ VC: Attribute Value Type ]
3413 * - [ VC: Fixed Attribute Default ]
3414 * - [ VC: Entity Name ]
3415 * - [ VC: Name Token ]
3416 * - [ VC: ID ]
3417 * - [ VC: IDREF ]
3418 * - [ VC: Entity Name ]
3419 * - [ VC: Notation Attributes ]
3420 *
3421 * The ID/IDREF uniqueness and matching are done separately
3422 *
3423 * returns 1 if valid or 0 otherwise
3424 */
3425
3426int
3427xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3428 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3429 /* xmlElementPtr elemDecl; */
3430 xmlAttributePtr attrDecl = NULL;
3431 int val;
3432 int ret = 1;
3433
3434 CHECK_DTD;
3435 if ((elem == NULL) || (elem->name == NULL)) return(0);
3436 if ((attr == NULL) || (attr->name == NULL)) return(0);
3437
3438 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3439 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003440 snprintf((char *) qname, sizeof(qname), "%s:%s",
3441 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003442 qname[sizeof(qname) - 1] = 0;
3443 if (attr->ns != NULL) {
3444 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3445 attr->name, attr->ns->prefix);
3446 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3447 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3448 attr->name, attr->ns->prefix);
3449 } else {
3450 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3451 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3452 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3453 qname, attr->name);
3454 }
3455 }
3456 if (attrDecl == NULL) {
3457 if (attr->ns != NULL) {
3458 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3459 attr->name, attr->ns->prefix);
3460 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3461 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3462 attr->name, attr->ns->prefix);
3463 } else {
3464 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3465 elem->name, attr->name);
3466 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3467 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3468 elem->name, attr->name);
3469 }
3470 }
3471
3472
3473 /* Validity Constraint: Attribute Value Type */
3474 if (attrDecl == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003475 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003476 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003477 "No declaration for attribute %s of element %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003478 attr->name, elem->name);
3479 return(0);
3480 }
3481 attr->atype = attrDecl->atype;
3482
3483 val = xmlValidateAttributeValue(attrDecl->atype, value);
3484 if (val == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003485 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003486 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003487 "Syntax of value for attribute %s of %s is not valid\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003488 attr->name, elem->name);
3489 ret = 0;
3490 }
3491
3492 /* Validity constraint: Fixed Attribute Default */
3493 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3494 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003495 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003496 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003497 "Value for attribute %s of %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003498 attr->name, elem->name, attrDecl->defaultValue);
3499 ret = 0;
3500 }
3501 }
3502
3503 /* Validity Constraint: ID uniqueness */
3504 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3505 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3506 ret = 0;
3507 }
3508
3509 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3510 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3511 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3512 ret = 0;
3513 }
3514
3515 /* Validity Constraint: Notation Attributes */
3516 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3517 xmlEnumerationPtr tree = attrDecl->tree;
3518 xmlNotationPtr nota;
3519
3520 /* First check that the given NOTATION was declared */
3521 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3522 if (nota == NULL)
3523 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3524
3525 if (nota == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003526 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003527 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003528 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003529 value, attr->name, elem->name);
3530 ret = 0;
3531 }
3532
3533 /* Second, verify that it's among the list */
3534 while (tree != NULL) {
3535 if (xmlStrEqual(tree->name, value)) break;
3536 tree = tree->next;
3537 }
3538 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003539 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003540 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003541"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003542 value, attr->name, elem->name);
3543 ret = 0;
3544 }
3545 }
3546
3547 /* Validity Constraint: Enumeration */
3548 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3549 xmlEnumerationPtr tree = attrDecl->tree;
3550 while (tree != NULL) {
3551 if (xmlStrEqual(tree->name, value)) break;
3552 tree = tree->next;
3553 }
3554 if (tree == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003555 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003556 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003557 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003558 value, attr->name, elem->name);
3559 ret = 0;
3560 }
3561 }
3562
3563 /* Fixed Attribute Default */
3564 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3565 (!xmlStrEqual(attrDecl->defaultValue, value))) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00003566 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00003567 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00003568 "Value for attribute %s of %s must be \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003569 attr->name, elem->name, attrDecl->defaultValue);
3570 ret = 0;
3571 }
3572
3573 /* Extra check for the attribute value */
3574 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3575 attrDecl->atype, value);
3576
3577 return(ret);
3578}
3579
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003580/**
3581 * xmlValidateSkipIgnorable:
3582 * @ctxt: the validation context
3583 * @child: the child list
3584 *
3585 * Skip ignorable elements w.r.t. the validation process
3586 *
3587 * returns the first element to consider for validation of the content model
3588 */
3589
3590static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003591xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003592 while (child != NULL) {
3593 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003594 /* These things are ignored (skipped) during validation. */
3595 case XML_PI_NODE:
3596 case XML_COMMENT_NODE:
3597 case XML_XINCLUDE_START:
3598 case XML_XINCLUDE_END:
3599 child = child->next;
3600 break;
3601 case XML_TEXT_NODE:
3602 if (xmlIsBlankNode(child))
3603 child = child->next;
3604 else
3605 return(child);
3606 break;
3607 /* keep current node */
3608 default:
3609 return(child);
3610 }
3611 }
3612 return(child);
3613}
3614
3615/**
3616 * xmlValidateElementType:
3617 * @ctxt: the validation context
3618 *
3619 * Try to validate the content model of an element internal function
3620 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003621 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3622 * reference is found and -3 if the validation succeeded but
3623 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003624 */
3625
3626static int
3627xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003628 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003629 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003630
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003631 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003632 if ((NODE == NULL) && (CONT == NULL))
3633 return(1);
3634 if ((NODE == NULL) &&
3635 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3636 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3637 return(1);
3638 }
3639 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003640 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003641 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003642
3643 /*
3644 * We arrive here when more states need to be examined
3645 */
3646cont:
3647
3648 /*
3649 * We just recovered from a rollback generated by a possible
3650 * epsilon transition, go directly to the analysis phase
3651 */
3652 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003653 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003654 DEBUG_VALID_STATE(NODE, CONT)
3655 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003656 goto analyze;
3657 }
3658
3659 DEBUG_VALID_STATE(NODE, CONT)
3660 /*
3661 * we may have to save a backup state here. This is the equivalent
3662 * of handling epsilon transition in NFAs.
3663 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003664 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003665 ((CONT->parent == NULL) ||
3666 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003667 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003668 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00003669 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003670 DEBUG_VALID_MSG("saving parent branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00003671 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
3672 return(0);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003673 }
3674
3675
3676 /*
3677 * Check first if the content matches
3678 */
3679 switch (CONT->type) {
3680 case XML_ELEMENT_CONTENT_PCDATA:
3681 if (NODE == NULL) {
3682 DEBUG_VALID_MSG("pcdata failed no node");
3683 ret = 0;
3684 break;
3685 }
3686 if (NODE->type == XML_TEXT_NODE) {
3687 DEBUG_VALID_MSG("pcdata found, skip to next");
3688 /*
3689 * go to next element in the content model
3690 * skipping ignorable elems
3691 */
3692 do {
3693 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003694 NODE = xmlValidateSkipIgnorable(NODE);
3695 if ((NODE != NULL) &&
3696 (NODE->type == XML_ENTITY_REF_NODE))
3697 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003698 } while ((NODE != NULL) &&
3699 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003700 (NODE->type != XML_TEXT_NODE) &&
3701 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003702 ret = 1;
3703 break;
3704 } else {
3705 DEBUG_VALID_MSG("pcdata failed");
3706 ret = 0;
3707 break;
3708 }
3709 break;
3710 case XML_ELEMENT_CONTENT_ELEMENT:
3711 if (NODE == NULL) {
3712 DEBUG_VALID_MSG("element failed no node");
3713 ret = 0;
3714 break;
3715 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003716 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3717 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003718 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003719 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3720 ret = (CONT->prefix == NULL);
3721 } else if (CONT->prefix == NULL) {
3722 ret = 0;
3723 } else {
3724 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3725 }
3726 }
3727 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003728 DEBUG_VALID_MSG("element found, skip to next");
3729 /*
3730 * go to next element in the content model
3731 * skipping ignorable elems
3732 */
3733 do {
3734 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003735 NODE = xmlValidateSkipIgnorable(NODE);
3736 if ((NODE != NULL) &&
3737 (NODE->type == XML_ENTITY_REF_NODE))
3738 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003739 } while ((NODE != NULL) &&
3740 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003741 (NODE->type != XML_TEXT_NODE) &&
3742 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003743 } else {
3744 DEBUG_VALID_MSG("element failed");
3745 ret = 0;
3746 break;
3747 }
3748 break;
3749 case XML_ELEMENT_CONTENT_OR:
3750 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003751 * Small optimization.
3752 */
3753 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3754 if ((NODE == NULL) ||
3755 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3756 DEPTH++;
3757 CONT = CONT->c2;
3758 goto cont;
3759 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003760 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3761 ret = (CONT->c1->prefix == NULL);
3762 } else if (CONT->c1->prefix == NULL) {
3763 ret = 0;
3764 } else {
3765 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3766 }
3767 if (ret == 0) {
3768 DEPTH++;
3769 CONT = CONT->c2;
3770 goto cont;
3771 }
Daniel Veillard85349052001-04-20 13:48:21 +00003772 }
3773
3774 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003775 * save the second branch 'or' branch
3776 */
3777 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard940492d2002-04-15 10:15:25 +00003778 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3779 OCCURS, ROLLBACK_OR) < 0)
3780 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003781 DEPTH++;
3782 CONT = CONT->c1;
3783 goto cont;
3784 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003785 /*
3786 * Small optimization.
3787 */
3788 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3789 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3790 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3791 if ((NODE == NULL) ||
3792 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3793 DEPTH++;
3794 CONT = CONT->c2;
3795 goto cont;
3796 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003797 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3798 ret = (CONT->c1->prefix == NULL);
3799 } else if (CONT->c1->prefix == NULL) {
3800 ret = 0;
3801 } else {
3802 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3803 }
3804 if (ret == 0) {
3805 DEPTH++;
3806 CONT = CONT->c2;
3807 goto cont;
3808 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003809 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003810 DEPTH++;
3811 CONT = CONT->c1;
3812 goto cont;
3813 }
3814
3815 /*
3816 * At this point handle going up in the tree
3817 */
3818 if (ret == -1) {
3819 DEBUG_VALID_MSG("error found returning");
3820 return(ret);
3821 }
3822analyze:
3823 while (CONT != NULL) {
3824 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003825 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003826 * this level.
3827 */
3828 if (ret == 0) {
3829 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003830 xmlNodePtr cur;
3831
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003832 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003833 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003834 DEBUG_VALID_MSG("Once branch failed, rollback");
3835 if (vstateVPop(ctxt) < 0 ) {
3836 DEBUG_VALID_MSG("exhaustion, failed");
3837 return(0);
3838 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003839 if (cur != ctxt->vstate->node)
3840 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003841 goto cont;
3842 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00003843 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003844 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003845 DEBUG_VALID_MSG("Plus branch failed, rollback");
3846 if (vstateVPop(ctxt) < 0 ) {
3847 DEBUG_VALID_MSG("exhaustion, failed");
3848 return(0);
3849 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003850 if (cur != ctxt->vstate->node)
3851 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003852 goto cont;
3853 }
3854 DEBUG_VALID_MSG("Plus branch found");
3855 ret = 1;
3856 break;
3857 case XML_ELEMENT_CONTENT_MULT:
3858#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00003859 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003860 DEBUG_VALID_MSG("Mult branch failed");
3861 } else {
3862 DEBUG_VALID_MSG("Mult branch found");
3863 }
3864#endif
3865 ret = 1;
3866 break;
3867 case XML_ELEMENT_CONTENT_OPT:
3868 DEBUG_VALID_MSG("Option branch failed");
3869 ret = 1;
3870 break;
3871 }
3872 } else {
3873 switch (CONT->ocur) {
3874 case XML_ELEMENT_CONTENT_OPT:
3875 DEBUG_VALID_MSG("Option branch succeeded");
3876 ret = 1;
3877 break;
3878 case XML_ELEMENT_CONTENT_ONCE:
3879 DEBUG_VALID_MSG("Once branch succeeded");
3880 ret = 1;
3881 break;
3882 case XML_ELEMENT_CONTENT_PLUS:
3883 if (STATE == ROLLBACK_PARENT) {
3884 DEBUG_VALID_MSG("Plus branch rollback");
3885 ret = 1;
3886 break;
3887 }
3888 if (NODE == NULL) {
3889 DEBUG_VALID_MSG("Plus branch exhausted");
3890 ret = 1;
3891 break;
3892 }
3893 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003894 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003895 goto cont;
3896 case XML_ELEMENT_CONTENT_MULT:
3897 if (STATE == ROLLBACK_PARENT) {
3898 DEBUG_VALID_MSG("Mult branch rollback");
3899 ret = 1;
3900 break;
3901 }
3902 if (NODE == NULL) {
3903 DEBUG_VALID_MSG("Mult branch exhausted");
3904 ret = 1;
3905 break;
3906 }
3907 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003908 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003909 goto cont;
3910 }
3911 }
3912 STATE = 0;
3913
3914 /*
3915 * Then act accordingly at the parent level
3916 */
Daniel Veillard5344c602001-12-31 16:37:34 +00003917 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003918 if (CONT->parent == NULL)
3919 break;
3920
3921 switch (CONT->parent->type) {
3922 case XML_ELEMENT_CONTENT_PCDATA:
3923 DEBUG_VALID_MSG("Error: parent pcdata");
3924 return(-1);
3925 case XML_ELEMENT_CONTENT_ELEMENT:
3926 DEBUG_VALID_MSG("Error: parent element");
3927 return(-1);
3928 case XML_ELEMENT_CONTENT_OR:
3929 if (ret == 1) {
3930 DEBUG_VALID_MSG("Or succeeded");
3931 CONT = CONT->parent;
3932 DEPTH--;
3933 } else {
3934 DEBUG_VALID_MSG("Or failed");
3935 CONT = CONT->parent;
3936 DEPTH--;
3937 }
3938 break;
3939 case XML_ELEMENT_CONTENT_SEQ:
3940 if (ret == 0) {
3941 DEBUG_VALID_MSG("Sequence failed");
3942 CONT = CONT->parent;
3943 DEPTH--;
3944 } else if (CONT == CONT->parent->c1) {
3945 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3946 CONT = CONT->parent->c2;
3947 goto cont;
3948 } else {
3949 DEBUG_VALID_MSG("Sequence succeeded");
3950 CONT = CONT->parent;
3951 DEPTH--;
3952 }
3953 }
3954 }
3955 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003956 xmlNodePtr cur;
3957
3958 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003959 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3960 if (vstateVPop(ctxt) < 0 ) {
3961 DEBUG_VALID_MSG("exhaustion, failed");
3962 return(0);
3963 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003964 if (cur != ctxt->vstate->node)
3965 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003966 goto cont;
3967 }
3968 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003969 xmlNodePtr cur;
3970
3971 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003972 DEBUG_VALID_MSG("Failure, rollback");
3973 if (vstateVPop(ctxt) < 0 ) {
3974 DEBUG_VALID_MSG("exhaustion, failed");
3975 return(0);
3976 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003977 if (cur != ctxt->vstate->node)
3978 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003979 goto cont;
3980 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003981 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003982}
3983
3984/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003985 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003986 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003987 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003988 * @content: An element
3989 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3990 *
3991 * This will dump the list of elements to the buffer
3992 * Intended just for the debug routine
3993 */
3994static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003995xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003996 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003997 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003998
3999 if (node == NULL) return;
4000 if (glob) strcat(buf, "(");
4001 cur = node;
4002 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004003 len = strlen(buf);
4004 if (size - len < 50) {
4005 if ((size - len > 4) && (buf[len - 1] != '.'))
4006 strcat(buf, " ...");
4007 return;
4008 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004009 switch (cur->type) {
4010 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004011 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004012 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004013 if ((size - len > 4) && (buf[len - 1] != '.'))
4014 strcat(buf, " ...");
4015 return;
4016 }
4017 strcat(buf, (char *) cur->ns->prefix);
4018 strcat(buf, ":");
4019 }
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00004020 if (size - len < xmlStrlen(cur->name) + 10) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00004021 if ((size - len > 4) && (buf[len - 1] != '.'))
4022 strcat(buf, " ...");
4023 return;
4024 }
4025 strcat(buf, (char *) cur->name);
4026 if (cur->next != NULL)
4027 strcat(buf, " ");
4028 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004029 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004030 if (xmlIsBlankNode(cur))
4031 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004032 case XML_CDATA_SECTION_NODE:
4033 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004034 strcat(buf, "CDATA");
4035 if (cur->next != NULL)
4036 strcat(buf, " ");
4037 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004038 case XML_ATTRIBUTE_NODE:
4039 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004040#ifdef LIBXML_DOCB_ENABLED
4041 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004042#endif
4043 case XML_HTML_DOCUMENT_NODE:
4044 case XML_DOCUMENT_TYPE_NODE:
4045 case XML_DOCUMENT_FRAG_NODE:
4046 case XML_NOTATION_NODE:
4047 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004048 strcat(buf, "???");
4049 if (cur->next != NULL)
4050 strcat(buf, " ");
4051 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004052 case XML_ENTITY_NODE:
4053 case XML_PI_NODE:
4054 case XML_DTD_NODE:
4055 case XML_COMMENT_NODE:
4056 case XML_ELEMENT_DECL:
4057 case XML_ATTRIBUTE_DECL:
4058 case XML_ENTITY_DECL:
4059 case XML_XINCLUDE_START:
4060 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00004061 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004062 }
4063 cur = cur->next;
4064 }
4065 if (glob) strcat(buf, ")");
4066}
4067
4068/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004069 * xmlValidateElementContent:
4070 * @ctxt: the validation context
4071 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004072 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004073 * @warn: emit the error message
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004074 * @parent: the parent element (for error reporting)
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004075 *
4076 * Try to validate the content model of an element
4077 *
4078 * returns 1 if valid or 0 if not and -1 in case of error
4079 */
4080
4081static int
4082xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004083 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004084 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004085 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004086 xmlElementContentPtr cont;
4087 const xmlChar *name;
4088
4089 if (elemDecl == NULL)
4090 return(-1);
4091 cont = elemDecl->content;
4092 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004093
4094 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004095 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004096 */
4097 ctxt->vstateMax = 8;
4098 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
4099 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4100 if (ctxt->vstateTab == NULL) {
4101 xmlGenericError(xmlGenericErrorContext,
4102 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004103 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004104 }
4105 /*
4106 * The first entry in the stack is reserved to the current state
4107 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00004108 ctxt->nodeMax = 0;
4109 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00004110 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004111 ctxt->vstate = &ctxt->vstateTab[0];
4112 ctxt->vstateNr = 1;
4113 CONT = cont;
4114 NODE = child;
4115 DEPTH = 0;
4116 OCCURS = 0;
4117 STATE = 0;
4118 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004119 if ((ret == -3) && (warn)) {
4120 VWARNING(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004121 "Content model for Element %s is ambiguous\n", name);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004122 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004123 /*
4124 * An entities reference appeared at this level.
4125 * Buid a minimal representation of this node content
4126 * sufficient to run the validation process on it
4127 */
4128 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004129 cur = child;
4130 while (cur != NULL) {
4131 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004132 case XML_ENTITY_REF_NODE:
4133 /*
4134 * Push the current node to be able to roll back
4135 * and process within the entity
4136 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004137 if ((cur->children != NULL) &&
4138 (cur->children->children != NULL)) {
4139 nodeVPush(ctxt, cur);
4140 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004141 continue;
4142 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00004143 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004144 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004145 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004146 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004147 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004148 case XML_CDATA_SECTION_NODE:
4149 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004150 case XML_ELEMENT_NODE:
4151 /*
4152 * Allocate a new node and minimally fills in
4153 * what's required
4154 */
4155 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4156 if (tmp == NULL) {
4157 xmlGenericError(xmlGenericErrorContext,
4158 "xmlValidateElementContent : malloc failed\n");
4159 xmlFreeNodeList(repl);
4160 ret = -1;
4161 goto done;
4162 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004163 tmp->type = cur->type;
4164 tmp->name = cur->name;
4165 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004166 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004167 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004168 if (repl == NULL)
4169 repl = last = tmp;
4170 else {
4171 last->next = tmp;
4172 last = tmp;
4173 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004174 if (cur->type == XML_CDATA_SECTION_NODE) {
4175 /*
4176 * E59 spaces in CDATA does not match the
4177 * nonterminal S
4178 */
4179 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4180 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004181 break;
4182 default:
4183 break;
4184 }
4185 /*
4186 * Switch to next element
4187 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004188 cur = cur->next;
4189 while (cur == NULL) {
4190 cur = nodeVPop(ctxt);
4191 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004192 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004193 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004194 }
4195 }
4196
4197 /*
4198 * Relaunch the validation
4199 */
4200 ctxt->vstate = &ctxt->vstateTab[0];
4201 ctxt->vstateNr = 1;
4202 CONT = cont;
4203 NODE = repl;
4204 DEPTH = 0;
4205 OCCURS = 0;
4206 STATE = 0;
4207 ret = xmlValidateElementType(ctxt);
4208 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004209 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004210 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4211 char expr[5000];
4212 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004213
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004214 expr[0] = 0;
4215 xmlSnprintfElementContent(expr, 5000, cont, 1);
4216 list[0] = 0;
4217 if (repl != NULL)
4218 xmlSnprintfElements(list, 5000, repl, 1);
4219 else
4220 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004221
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004222 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004223 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004224 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004225 "Element %s content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004226 name, expr, list);
4227 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004228 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004229 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004230 "Element content does not follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004231 expr, list);
4232 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004233 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004234 if (name != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004235 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004236 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004237 "Element %s content does not follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004238 name);
4239 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004240 if (parent != NULL) VECTXT(ctxt, parent);
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004241 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004242 "Element content does not follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004243 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004244 }
4245 ret = 0;
4246 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004247 if (ret == -3)
4248 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004249
4250
4251done:
4252 /*
4253 * Deallocate the copy if done, and free up the validation stack
4254 */
4255 while (repl != NULL) {
4256 tmp = repl->next;
4257 xmlFree(repl);
4258 repl = tmp;
4259 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004260 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004261 if (ctxt->vstateTab != NULL) {
4262 xmlFree(ctxt->vstateTab);
4263 ctxt->vstateTab = NULL;
4264 }
4265 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004266 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004267 if (ctxt->nodeTab != NULL) {
4268 xmlFree(ctxt->nodeTab);
4269 ctxt->nodeTab = NULL;
4270 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004271 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004272
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004273}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004274
Owen Taylor3473f882001-02-23 17:55:21 +00004275/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004276 * xmlValidateCdataElement:
4277 * @ctxt: the validation context
4278 * @doc: a document instance
4279 * @elem: an element instance
4280 *
4281 * Check that an element follows #CDATA
4282 *
4283 * returns 1 if valid or 0 otherwise
4284 */
4285static int
4286xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4287 xmlNodePtr elem) {
4288 int ret = 1;
4289 xmlNodePtr cur, child;
4290
4291 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4292 return(0);
4293
4294 child = elem->children;
4295
4296 cur = child;
4297 while (cur != NULL) {
4298 switch (cur->type) {
4299 case XML_ENTITY_REF_NODE:
4300 /*
4301 * Push the current node to be able to roll back
4302 * and process within the entity
4303 */
4304 if ((cur->children != NULL) &&
4305 (cur->children->children != NULL)) {
4306 nodeVPush(ctxt, cur);
4307 cur = cur->children->children;
4308 continue;
4309 }
4310 break;
4311 case XML_COMMENT_NODE:
4312 case XML_PI_NODE:
4313 case XML_TEXT_NODE:
4314 case XML_CDATA_SECTION_NODE:
4315 break;
4316 default:
4317 ret = 0;
4318 goto done;
4319 }
4320 /*
4321 * Switch to next element
4322 */
4323 cur = cur->next;
4324 while (cur == NULL) {
4325 cur = nodeVPop(ctxt);
4326 if (cur == NULL)
4327 break;
4328 cur = cur->next;
4329 }
4330 }
4331done:
4332 ctxt->nodeMax = 0;
4333 ctxt->nodeNr = 0;
4334 if (ctxt->nodeTab != NULL) {
4335 xmlFree(ctxt->nodeTab);
4336 ctxt->nodeTab = NULL;
4337 }
4338 return(ret);
4339}
4340
4341/**
Owen Taylor3473f882001-02-23 17:55:21 +00004342 * xmlValidateOneElement:
4343 * @ctxt: the validation context
4344 * @doc: a document instance
4345 * @elem: an element instance
4346 *
4347 * Try to validate a single element and it's attributes,
4348 * basically it does the following checks as described by the
4349 * XML-1.0 recommendation:
4350 * - [ VC: Element Valid ]
4351 * - [ VC: Required Attribute ]
4352 * Then call xmlValidateOneAttribute() for each attribute present.
4353 *
4354 * The ID/IDREF checkings are done separately
4355 *
4356 * returns 1 if valid or 0 otherwise
4357 */
4358
4359int
4360xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4361 xmlNodePtr elem) {
4362 xmlElementPtr elemDecl = NULL;
4363 xmlElementContentPtr cont;
4364 xmlAttributePtr attr;
4365 xmlNodePtr child;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004366 int ret = 1, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004367 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004368 const xmlChar *prefix = NULL;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004369 int extsubset = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004370
4371 CHECK_DTD;
4372
4373 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004374 switch (elem->type) {
4375 case XML_ATTRIBUTE_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004376 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004377 VERROR(ctxt->userData,
4378 "Attribute element not expected here\n");
4379 return(0);
4380 case XML_TEXT_NODE:
4381 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004382 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004383 VERROR(ctxt->userData, "Text element has childs !\n");
4384 return(0);
4385 }
4386 if (elem->properties != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004387 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004388 VERROR(ctxt->userData, "Text element has attributes !\n");
4389 return(0);
4390 }
4391 if (elem->ns != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004392 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004393 VERROR(ctxt->userData, "Text element has namespace !\n");
4394 return(0);
4395 }
4396 if (elem->nsDef != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004397 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004398 VERROR(ctxt->userData,
4399 "Text element carries namespace definitions !\n");
4400 return(0);
4401 }
4402 if (elem->content == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004403 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004404 VERROR(ctxt->userData,
4405 "Text element has no content !\n");
4406 return(0);
4407 }
4408 return(1);
4409 case XML_XINCLUDE_START:
4410 case XML_XINCLUDE_END:
4411 return(1);
4412 case XML_CDATA_SECTION_NODE:
4413 case XML_ENTITY_REF_NODE:
4414 case XML_PI_NODE:
4415 case XML_COMMENT_NODE:
4416 return(1);
4417 case XML_ENTITY_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004418 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004419 VERROR(ctxt->userData,
4420 "Entity element not expected here\n");
4421 return(0);
4422 case XML_NOTATION_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004423 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004424 VERROR(ctxt->userData,
4425 "Notation element not expected here\n");
4426 return(0);
4427 case XML_DOCUMENT_NODE:
4428 case XML_DOCUMENT_TYPE_NODE:
4429 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004430 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004431 VERROR(ctxt->userData,
4432 "Document element not expected here\n");
4433 return(0);
4434 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004435 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004436 VERROR(ctxt->userData,
4437 "\n");
4438 return(0);
4439 case XML_ELEMENT_NODE:
4440 break;
4441 default:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004442 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004443 VERROR(ctxt->userData,
4444 "unknown element type %d\n", elem->type);
4445 return(0);
4446 }
4447 if (elem->name == NULL) return(0);
4448
4449 /*
4450 * Fetch the declaration for the qualified name
4451 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004452 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4453 prefix = elem->ns->prefix;
4454
4455 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004456 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004457 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004458 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004459 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004460 elem->name, prefix);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004461 if (elemDecl != NULL)
4462 extsubset = 1;
4463 }
Owen Taylor3473f882001-02-23 17:55:21 +00004464 }
4465
4466 /*
4467 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004468 * This is "non-strict" validation should be done on the
4469 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004470 */
4471 if (elemDecl == NULL) {
4472 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004473 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004474 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004475 if (elemDecl != NULL)
4476 extsubset = 1;
4477 }
Owen Taylor3473f882001-02-23 17:55:21 +00004478 }
4479 if (elemDecl == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004480 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004481 VERROR(ctxt->userData, "No declaration for element %s\n",
4482 elem->name);
4483 return(0);
4484 }
4485
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004486 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00004487 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004488 case XML_ELEMENT_TYPE_UNDEFINED:
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004489 VECTXT(ctxt, elem);
Daniel Veillarda10efa82001-04-18 13:09:01 +00004490 VERROR(ctxt->userData, "No declaration for element %s\n",
4491 elem->name);
4492 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004493 case XML_ELEMENT_TYPE_EMPTY:
4494 if (elem->children != NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004495 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004496 VERROR(ctxt->userData,
4497 "Element %s was declared EMPTY this one has content\n",
4498 elem->name);
4499 ret = 0;
4500 }
4501 break;
4502 case XML_ELEMENT_TYPE_ANY:
4503 /* I don't think anything is required then */
4504 break;
4505 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004506
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004507 /* simple case of declared as #PCDATA */
4508 if ((elemDecl->content != NULL) &&
4509 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4510 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4511 if (!ret) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004512 VECTXT(ctxt, elem);
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004513 VERROR(ctxt->userData,
4514 "Element %s was declared #PCDATA but contains non text nodes\n",
4515 elem->name);
4516 }
4517 break;
4518 }
Owen Taylor3473f882001-02-23 17:55:21 +00004519 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004520 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004521 while (child != NULL) {
4522 if (child->type == XML_ELEMENT_NODE) {
4523 name = child->name;
4524 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4525 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004526 snprintf((char *) qname, sizeof(qname), "%s:%s",
4527 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004528 qname[sizeof(qname) - 1] = 0;
4529 cont = elemDecl->content;
4530 while (cont != NULL) {
4531 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4532 if (xmlStrEqual(cont->name, qname)) break;
4533 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4534 (cont->c1 != NULL) &&
4535 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4536 if (xmlStrEqual(cont->c1->name, qname)) break;
4537 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4538 (cont->c1 == NULL) ||
4539 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4540 /* Internal error !!! */
4541 xmlGenericError(xmlGenericErrorContext,
4542 "Internal: MIXED struct bad\n");
4543 break;
4544 }
4545 cont = cont->c2;
4546 }
4547 if (cont != NULL)
4548 goto child_ok;
4549 }
4550 cont = elemDecl->content;
4551 while (cont != NULL) {
4552 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4553 if (xmlStrEqual(cont->name, name)) break;
4554 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4555 (cont->c1 != NULL) &&
4556 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4557 if (xmlStrEqual(cont->c1->name, name)) break;
4558 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4559 (cont->c1 == NULL) ||
4560 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4561 /* Internal error !!! */
4562 xmlGenericError(xmlGenericErrorContext,
4563 "Internal: MIXED struct bad\n");
4564 break;
4565 }
4566 cont = cont->c2;
4567 }
4568 if (cont == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004569 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004570 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00004571 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004572 name, elem->name);
4573 ret = 0;
4574 }
4575 }
4576child_ok:
4577 child = child->next;
4578 }
4579 break;
4580 case XML_ELEMENT_TYPE_ELEMENT:
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004581 if ((doc->standalone == 1) && (extsubset == 1)) {
4582 /*
4583 * VC: Standalone Document Declaration
4584 * - element types with element content, if white space
4585 * occurs directly within any instance of those types.
4586 */
4587 child = elem->children;
4588 while (child != NULL) {
4589 if (child->type == XML_TEXT_NODE) {
4590 const xmlChar *content = child->content;
4591
4592 while (IS_BLANK(*content))
4593 content++;
4594 if (*content == 0) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004595 VECTXT(ctxt, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004596 VERROR(ctxt->userData,
4597"standalone: %s declared in the external subset contains white spaces nodes\n",
4598 elem->name);
4599 ret = 0;
4600 break;
4601 }
4602 }
4603 child =child->next;
4604 }
4605 }
Owen Taylor3473f882001-02-23 17:55:21 +00004606 child = elem->children;
4607 cont = elemDecl->content;
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004608 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004609 if (tmp <= 0)
4610 ret = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004611 break;
4612 }
4613
4614 /* [ VC: Required Attribute ] */
4615 attr = elemDecl->attributes;
4616 while (attr != NULL) {
4617 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004618 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00004619
Daniel Veillarde4301c82002-02-13 13:32:35 +00004620 if ((attr->prefix == NULL) &&
4621 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4622 xmlNsPtr ns;
4623
4624 ns = elem->nsDef;
4625 while (ns != NULL) {
4626 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004627 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004628 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00004629 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004630 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4631 xmlNsPtr ns;
4632
4633 ns = elem->nsDef;
4634 while (ns != NULL) {
4635 if (xmlStrEqual(attr->name, ns->prefix))
4636 goto found;
4637 ns = ns->next;
4638 }
4639 } else {
4640 xmlAttrPtr attrib;
4641
4642 attrib = elem->properties;
4643 while (attrib != NULL) {
4644 if (xmlStrEqual(attrib->name, attr->name)) {
4645 if (attr->prefix != NULL) {
4646 xmlNsPtr nameSpace = attrib->ns;
4647
4648 if (nameSpace == NULL)
4649 nameSpace = elem->ns;
4650 /*
4651 * qualified names handling is problematic, having a
4652 * different prefix should be possible but DTDs don't
4653 * allow to define the URI instead of the prefix :-(
4654 */
4655 if (nameSpace == NULL) {
4656 if (qualified < 0)
4657 qualified = 0;
4658 } else if (!xmlStrEqual(nameSpace->prefix,
4659 attr->prefix)) {
4660 if (qualified < 1)
4661 qualified = 1;
4662 } else
4663 goto found;
4664 } else {
4665 /*
4666 * We should allow applications to define namespaces
4667 * for their application even if the DTD doesn't
4668 * carry one, otherwise, basically we would always
4669 * break.
4670 */
4671 goto found;
4672 }
4673 }
4674 attrib = attrib->next;
4675 }
Owen Taylor3473f882001-02-23 17:55:21 +00004676 }
4677 if (qualified == -1) {
4678 if (attr->prefix == NULL) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004679 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004680 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004681 "Element %s does not carry attribute %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004682 elem->name, attr->name);
4683 ret = 0;
4684 } else {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004685 VECTXT(ctxt, elem);
Owen Taylor3473f882001-02-23 17:55:21 +00004686 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004687 "Element %s does not carry attribute %s:%s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004688 elem->name, attr->prefix,attr->name);
4689 ret = 0;
4690 }
4691 } else if (qualified == 0) {
4692 VWARNING(ctxt->userData,
4693 "Element %s required attribute %s:%s has no prefix\n",
4694 elem->name, attr->prefix,attr->name);
4695 } else if (qualified == 1) {
4696 VWARNING(ctxt->userData,
4697 "Element %s required attribute %s:%s has different prefix\n",
4698 elem->name, attr->prefix,attr->name);
4699 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004700 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
4701 /*
4702 * Special tests checking #FIXED namespace declarations
4703 * have the right value since this is not done as an
4704 * attribute checking
4705 */
4706 if ((attr->prefix == NULL) &&
4707 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4708 xmlNsPtr ns;
4709
4710 ns = elem->nsDef;
4711 while (ns != NULL) {
4712 if (ns->prefix == NULL) {
4713 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004714 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00004715 VERROR(ctxt->userData,
4716 "Element %s namespace name for default namespace does not match the DTD\n",
4717 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00004718 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004719 }
4720 goto found;
4721 }
4722 ns = ns->next;
4723 }
4724 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4725 xmlNsPtr ns;
4726
4727 ns = elem->nsDef;
4728 while (ns != NULL) {
4729 if (xmlStrEqual(attr->name, ns->prefix)) {
4730 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
Daniel Veillardb9cd8b42002-09-05 10:58:49 +00004731 VECTXT(ctxt, elem);
Daniel Veillarde4301c82002-02-13 13:32:35 +00004732 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004733 "Element %s namespace name for %s does not match the DTD\n",
Daniel Veillarde4301c82002-02-13 13:32:35 +00004734 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00004735 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004736 }
4737 goto found;
4738 }
4739 ns = ns->next;
4740 }
4741 }
Owen Taylor3473f882001-02-23 17:55:21 +00004742 }
4743found:
4744 attr = attr->nexth;
4745 }
4746 return(ret);
4747}
4748
4749/**
4750 * xmlValidateRoot:
4751 * @ctxt: the validation context
4752 * @doc: a document instance
4753 *
4754 * Try to validate a the root element
4755 * basically it does the following check as described by the
4756 * XML-1.0 recommendation:
4757 * - [ VC: Root Element Type ]
4758 * it doesn't try to recurse or apply other check to the element
4759 *
4760 * returns 1 if valid or 0 otherwise
4761 */
4762
4763int
4764xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4765 xmlNodePtr root;
4766 if (doc == NULL) return(0);
4767
4768 root = xmlDocGetRootElement(doc);
4769 if ((root == NULL) || (root->name == NULL)) {
4770 VERROR(ctxt->userData, "Not valid: no root element\n");
4771 return(0);
4772 }
4773
4774 /*
4775 * When doing post validation against a separate DTD, those may
4776 * no internal subset has been generated
4777 */
4778 if ((doc->intSubset != NULL) &&
4779 (doc->intSubset->name != NULL)) {
4780 /*
4781 * Check first the document root against the NQName
4782 */
4783 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4784 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4785 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004786 snprintf((char *) qname, sizeof(qname), "%s:%s",
4787 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004788 qname[sizeof(qname) - 1] = 0;
4789 if (xmlStrEqual(doc->intSubset->name, qname))
4790 goto name_ok;
4791 }
4792 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4793 (xmlStrEqual(root->name, BAD_CAST "html")))
4794 goto name_ok;
Daniel Veillard76575762002-09-05 14:21:15 +00004795 VECTXT(ctxt, root);
Owen Taylor3473f882001-02-23 17:55:21 +00004796 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004797 "Not valid: root and DTD name do not match '%s' and '%s'\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004798 root->name, doc->intSubset->name);
4799 return(0);
4800
4801 }
4802 }
4803name_ok:
4804 return(1);
4805}
4806
4807
4808/**
4809 * xmlValidateElement:
4810 * @ctxt: the validation context
4811 * @doc: a document instance
4812 * @elem: an element instance
4813 *
4814 * Try to validate the subtree under an element
4815 *
4816 * returns 1 if valid or 0 otherwise
4817 */
4818
4819int
4820xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4821 xmlNodePtr child;
4822 xmlAttrPtr attr;
4823 xmlChar *value;
4824 int ret = 1;
4825
4826 if (elem == NULL) return(0);
4827
4828 /*
4829 * XInclude elements were added after parsing in the infoset,
4830 * they don't really mean anything validation wise.
4831 */
4832 if ((elem->type == XML_XINCLUDE_START) ||
4833 (elem->type == XML_XINCLUDE_END))
4834 return(1);
4835
4836 CHECK_DTD;
4837
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004838 /*
4839 * Entities references have to be handled separately
4840 */
4841 if (elem->type == XML_ENTITY_REF_NODE) {
4842 return(1);
4843 }
4844
Owen Taylor3473f882001-02-23 17:55:21 +00004845 ret &= xmlValidateOneElement(ctxt, doc, elem);
4846 attr = elem->properties;
4847 while(attr != NULL) {
4848 value = xmlNodeListGetString(doc, attr->children, 0);
4849 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4850 if (value != NULL)
4851 xmlFree(value);
4852 attr= attr->next;
4853 }
4854 child = elem->children;
4855 while (child != NULL) {
4856 ret &= xmlValidateElement(ctxt, doc, child);
4857 child = child->next;
4858 }
4859
4860 return(ret);
4861}
4862
Daniel Veillard8730c562001-02-26 10:49:57 +00004863/**
4864 * xmlValidateRef:
4865 * @ref: A reference to be validated
4866 * @ctxt: Validation context
4867 * @name: Name of ID we are searching for
4868 *
4869 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004870static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004871xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004872 const xmlChar *name) {
4873 xmlAttrPtr id;
4874 xmlAttrPtr attr;
4875
4876 if (ref == NULL)
4877 return;
4878 attr = ref->attr;
4879 if (attr == NULL)
4880 return;
4881 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4882 id = xmlGetID(ctxt->doc, name);
4883 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00004884 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00004885 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004886 "IDREF attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004887 attr->name, name);
4888 ctxt->valid = 0;
4889 }
4890 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4891 xmlChar *dup, *str = NULL, *cur, save;
4892
4893 dup = xmlStrdup(name);
4894 if (dup == NULL) {
4895 ctxt->valid = 0;
4896 return;
4897 }
4898 cur = dup;
4899 while (*cur != 0) {
4900 str = cur;
4901 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4902 save = *cur;
4903 *cur = 0;
4904 id = xmlGetID(ctxt->doc, str);
4905 if (id == NULL) {
Daniel Veillard76575762002-09-05 14:21:15 +00004906 VECTXT(ctxt, attr->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00004907 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00004908 "IDREFS attribute %s references an unknown ID \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004909 attr->name, str);
4910 ctxt->valid = 0;
4911 }
4912 if (save == 0)
4913 break;
4914 *cur = save;
4915 while (IS_BLANK(*cur)) cur++;
4916 }
4917 xmlFree(dup);
4918 }
4919}
4920
4921/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004922 * xmlWalkValidateList:
4923 * @data: Contents of current link
4924 * @user: Value supplied by the user
4925 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004926 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00004927 */
4928static int
4929xmlWalkValidateList(const void *data, const void *user)
4930{
4931 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4932 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4933 return 1;
4934}
4935
4936/**
4937 * xmlValidateCheckRefCallback:
4938 * @ref_list: List of references
4939 * @ctxt: Validation context
4940 * @name: Name of ID we are searching for
4941 *
4942 */
4943static void
4944xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4945 const xmlChar *name) {
4946 xmlValidateMemo memo;
4947
4948 if (ref_list == NULL)
4949 return;
4950 memo.ctxt = ctxt;
4951 memo.name = name;
4952
4953 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4954
4955}
4956
4957/**
Owen Taylor3473f882001-02-23 17:55:21 +00004958 * xmlValidateDocumentFinal:
4959 * @ctxt: the validation context
4960 * @doc: a document instance
4961 *
4962 * Does the final step for the document validation once all the
4963 * incremental validation steps have been completed
4964 *
4965 * basically it does the following checks described by the XML Rec
4966 *
4967 *
4968 * returns 1 if valid or 0 otherwise
4969 */
4970
4971int
4972xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4973 xmlRefTablePtr table;
4974
4975 if (doc == NULL) {
4976 xmlGenericError(xmlGenericErrorContext,
4977 "xmlValidateDocumentFinal: doc == NULL\n");
4978 return(0);
4979 }
4980
4981 /*
4982 * Check all the NOTATION/NOTATIONS attributes
4983 */
4984 /*
4985 * Check all the ENTITY/ENTITIES attributes definition for validity
4986 */
4987 /*
4988 * Check all the IDREF/IDREFS attributes definition for validity
4989 */
4990 table = (xmlRefTablePtr) doc->refs;
4991 ctxt->doc = doc;
4992 ctxt->valid = 1;
4993 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4994 return(ctxt->valid);
4995}
4996
4997/**
4998 * xmlValidateDtd:
4999 * @ctxt: the validation context
5000 * @doc: a document instance
5001 * @dtd: a dtd instance
5002 *
5003 * Try to validate the document against the dtd instance
5004 *
5005 * basically it does check all the definitions in the DtD.
5006 *
5007 * returns 1 if valid or 0 otherwise
5008 */
5009
5010int
5011xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
5012 int ret;
5013 xmlDtdPtr oldExt;
5014 xmlNodePtr root;
5015
5016 if (dtd == NULL) return(0);
5017 if (doc == NULL) return(0);
5018 oldExt = doc->extSubset;
5019 doc->extSubset = dtd;
5020 ret = xmlValidateRoot(ctxt, doc);
5021 if (ret == 0) {
5022 doc->extSubset = oldExt;
5023 return(ret);
5024 }
5025 if (doc->ids != NULL) {
5026 xmlFreeIDTable(doc->ids);
5027 doc->ids = NULL;
5028 }
5029 if (doc->refs != NULL) {
5030 xmlFreeRefTable(doc->refs);
5031 doc->refs = NULL;
5032 }
5033 root = xmlDocGetRootElement(doc);
5034 ret = xmlValidateElement(ctxt, doc, root);
5035 ret &= xmlValidateDocumentFinal(ctxt, doc);
5036 doc->extSubset = oldExt;
5037 return(ret);
5038}
5039
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005040static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005041xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
5042 const xmlChar *name ATTRIBUTE_UNUSED) {
5043 if (cur == NULL)
5044 return;
5045 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
5046 xmlChar *notation = cur->content;
5047
Daniel Veillard878eab02002-02-19 13:46:09 +00005048 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005049 int ret;
5050
5051 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
5052 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00005053 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005054 }
5055 }
5056 }
5057}
5058
5059static void
Owen Taylor3473f882001-02-23 17:55:21 +00005060xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00005061 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005062 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00005063 xmlDocPtr doc;
5064 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005065
Owen Taylor3473f882001-02-23 17:55:21 +00005066 if (cur == NULL)
5067 return;
5068 switch (cur->atype) {
5069 case XML_ATTRIBUTE_CDATA:
5070 case XML_ATTRIBUTE_ID:
5071 case XML_ATTRIBUTE_IDREF :
5072 case XML_ATTRIBUTE_IDREFS:
5073 case XML_ATTRIBUTE_NMTOKEN:
5074 case XML_ATTRIBUTE_NMTOKENS:
5075 case XML_ATTRIBUTE_ENUMERATION:
5076 break;
5077 case XML_ATTRIBUTE_ENTITY:
5078 case XML_ATTRIBUTE_ENTITIES:
5079 case XML_ATTRIBUTE_NOTATION:
5080 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005081
5082 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
5083 cur->atype, cur->defaultValue);
5084 if ((ret == 0) && (ctxt->valid == 1))
5085 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005086 }
5087 if (cur->tree != NULL) {
5088 xmlEnumerationPtr tree = cur->tree;
5089 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005090 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00005091 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005092 if ((ret == 0) && (ctxt->valid == 1))
5093 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005094 tree = tree->next;
5095 }
5096 }
5097 }
Daniel Veillard878eab02002-02-19 13:46:09 +00005098 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
5099 doc = cur->doc;
5100 if ((doc == NULL) || (cur->elem == NULL)) {
5101 VERROR(ctxt->userData,
5102 "xmlValidateAttributeCallback(%s): internal error\n",
5103 cur->name);
5104 return;
5105 }
5106 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
5107 if (elem == NULL)
5108 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
5109 if (elem == NULL) {
5110 VERROR(ctxt->userData,
5111 "attribute %s: could not find decl for element %s\n",
5112 cur->name, cur->elem);
5113 return;
5114 }
5115 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
5116 VERROR(ctxt->userData,
Daniel Veillard58e44c92002-08-02 22:19:49 +00005117 "NOTATION attribute %s declared for EMPTY element %s\n",
Daniel Veillard878eab02002-02-19 13:46:09 +00005118 cur->name, cur->elem);
5119 ctxt->valid = 0;
5120 }
5121 }
Owen Taylor3473f882001-02-23 17:55:21 +00005122}
5123
5124/**
5125 * xmlValidateDtdFinal:
5126 * @ctxt: the validation context
5127 * @doc: a document instance
5128 *
5129 * Does the final step for the dtds validation once all the
5130 * subsets have been parsed
5131 *
5132 * basically it does the following checks described by the XML Rec
5133 * - check that ENTITY and ENTITIES type attributes default or
5134 * possible values matches one of the defined entities.
5135 * - check that NOTATION type attributes default or
5136 * possible values matches one of the defined notations.
5137 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005138 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00005139 */
5140
5141int
5142xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00005143 xmlDtdPtr dtd;
5144 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005145 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00005146
5147 if (doc == NULL) return(0);
5148 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5149 return(0);
5150 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005151 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005152 dtd = doc->intSubset;
5153 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5154 table = (xmlAttributeTablePtr) dtd->attributes;
5155 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005156 }
5157 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005158 entities = (xmlEntitiesTablePtr) dtd->entities;
5159 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5160 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005161 }
5162 dtd = doc->extSubset;
5163 if ((dtd != NULL) && (dtd->attributes != NULL)) {
5164 table = (xmlAttributeTablePtr) dtd->attributes;
5165 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00005166 }
5167 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00005168 entities = (xmlEntitiesTablePtr) dtd->entities;
5169 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
5170 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005171 }
5172 return(ctxt->valid);
5173}
5174
5175/**
5176 * xmlValidateDocument:
5177 * @ctxt: the validation context
5178 * @doc: a document instance
5179 *
5180 * Try to validate the document instance
5181 *
5182 * basically it does the all the checks described by the XML Rec
5183 * i.e. validates the internal and external subset (if present)
5184 * and validate the document tree.
5185 *
5186 * returns 1 if valid or 0 otherwise
5187 */
5188
5189int
5190xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
5191 int ret;
5192 xmlNodePtr root;
5193
5194 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
5195 return(0);
5196 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
5197 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
5198 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
5199 doc->intSubset->SystemID);
5200 if (doc->extSubset == NULL) {
5201 if (doc->intSubset->SystemID != NULL) {
5202 VERROR(ctxt->userData,
5203 "Could not load the external subset \"%s\"\n",
5204 doc->intSubset->SystemID);
5205 } else {
5206 VERROR(ctxt->userData,
5207 "Could not load the external subset \"%s\"\n",
5208 doc->intSubset->ExternalID);
5209 }
5210 return(0);
5211 }
5212 }
5213
5214 if (doc->ids != NULL) {
5215 xmlFreeIDTable(doc->ids);
5216 doc->ids = NULL;
5217 }
5218 if (doc->refs != NULL) {
5219 xmlFreeRefTable(doc->refs);
5220 doc->refs = NULL;
5221 }
5222 ret = xmlValidateDtdFinal(ctxt, doc);
5223 if (!xmlValidateRoot(ctxt, doc)) return(0);
5224
5225 root = xmlDocGetRootElement(doc);
5226 ret &= xmlValidateElement(ctxt, doc, root);
5227 ret &= xmlValidateDocumentFinal(ctxt, doc);
5228 return(ret);
5229}
5230
5231
5232/************************************************************************
5233 * *
5234 * Routines for dynamic validation editing *
5235 * *
5236 ************************************************************************/
5237
5238/**
5239 * xmlValidGetPotentialChildren:
5240 * @ctree: an element content tree
5241 * @list: an array to store the list of child names
5242 * @len: a pointer to the number of element in the list
5243 * @max: the size of the array
5244 *
5245 * Build/extend a list of potential children allowed by the content tree
5246 *
5247 * returns the number of element in the list, or -1 in case of error.
5248 */
5249
5250int
5251xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
5252 int *len, int max) {
5253 int i;
5254
5255 if ((ctree == NULL) || (list == NULL) || (len == NULL))
5256 return(-1);
5257 if (*len >= max) return(*len);
5258
5259 switch (ctree->type) {
5260 case XML_ELEMENT_CONTENT_PCDATA:
5261 for (i = 0; i < *len;i++)
5262 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
5263 list[(*len)++] = BAD_CAST "#PCDATA";
5264 break;
5265 case XML_ELEMENT_CONTENT_ELEMENT:
5266 for (i = 0; i < *len;i++)
5267 if (xmlStrEqual(ctree->name, list[i])) return(*len);
5268 list[(*len)++] = ctree->name;
5269 break;
5270 case XML_ELEMENT_CONTENT_SEQ:
5271 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5272 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5273 break;
5274 case XML_ELEMENT_CONTENT_OR:
5275 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5276 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5277 break;
5278 }
5279
5280 return(*len);
5281}
5282
5283/**
5284 * xmlValidGetValidElements:
5285 * @prev: an element to insert after
5286 * @next: an element to insert next
5287 * @list: an array to store the list of child names
5288 * @max: the size of the array
5289 *
5290 * This function returns the list of authorized children to insert
5291 * within an existing tree while respecting the validity constraints
5292 * forced by the Dtd. The insertion point is defined using @prev and
5293 * @next in the following ways:
5294 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
5295 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
5296 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
5297 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
5298 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
5299 *
5300 * pointers to the element names are inserted at the beginning of the array
5301 * and do not need to be freed.
5302 *
5303 * returns the number of element in the list, or -1 in case of error. If
5304 * the function returns the value @max the caller is invited to grow the
5305 * receiving array and retry.
5306 */
5307
5308int
5309xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
5310 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005311 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00005312 int nb_valid_elements = 0;
5313 const xmlChar *elements[256];
5314 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005315 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005316
5317 xmlNode *ref_node;
5318 xmlNode *parent;
5319 xmlNode *test_node;
5320
5321 xmlNode *prev_next;
5322 xmlNode *next_prev;
5323 xmlNode *parent_childs;
5324 xmlNode *parent_last;
5325
5326 xmlElement *element_desc;
5327
Daniel Veillardbb4e46d2002-03-10 16:49:08 +00005328 memset(&vctxt, 0, sizeof (xmlValidCtxt));
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005329
Owen Taylor3473f882001-02-23 17:55:21 +00005330 if (prev == NULL && next == NULL)
5331 return(-1);
5332
5333 if (list == NULL) return(-1);
5334 if (max <= 0) return(-1);
5335
5336 nb_valid_elements = 0;
5337 ref_node = prev ? prev : next;
5338 parent = ref_node->parent;
5339
5340 /*
5341 * Retrieves the parent element declaration
5342 */
5343 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
5344 parent->name);
5345 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
5346 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
5347 parent->name);
5348 if (element_desc == NULL) return(-1);
5349
5350 /*
5351 * Do a backup of the current tree structure
5352 */
5353 prev_next = prev ? prev->next : NULL;
5354 next_prev = next ? next->prev : NULL;
5355 parent_childs = parent->children;
5356 parent_last = parent->last;
5357
5358 /*
5359 * Creates a dummy node and insert it into the tree
5360 */
5361 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
5362 test_node->doc = ref_node->doc;
5363 test_node->parent = parent;
5364 test_node->prev = prev;
5365 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00005366 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00005367
5368 if (prev) prev->next = test_node;
5369 else parent->children = test_node;
5370
5371 if (next) next->prev = test_node;
5372 else parent->last = test_node;
5373
5374 /*
5375 * Insert each potential child node and check if the parent is
5376 * still valid
5377 */
5378 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
5379 elements, &nb_elements, 256);
5380
5381 for (i = 0;i < nb_elements;i++) {
5382 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005383 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005384 int j;
5385
5386 for (j = 0; j < nb_valid_elements;j++)
5387 if (xmlStrEqual(elements[i], list[j])) break;
5388 list[nb_valid_elements++] = elements[i];
5389 if (nb_valid_elements >= max) break;
5390 }
5391 }
5392
5393 /*
5394 * Restore the tree structure
5395 */
5396 if (prev) prev->next = prev_next;
5397 if (next) next->prev = next_prev;
5398 parent->children = parent_childs;
5399 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00005400
5401 /*
5402 * Free up the dummy node
5403 */
5404 test_node->name = name;
5405 xmlFreeNode(test_node);
5406
Owen Taylor3473f882001-02-23 17:55:21 +00005407 return(nb_valid_elements);
5408}