blob: 79a35122f2b8e166acecf9a809f37080e3f755f5 [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
Bjorn Reese70a9da52001-04-21 16:57:29 +000010#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000011
Owen Taylor3473f882001-02-23 17:55:21 +000012#include <string.h>
13
14#ifdef HAVE_STDLIB_H
15#include <stdlib.h>
16#endif
17
18#include <libxml/xmlmemory.h>
19#include <libxml/hash.h>
20#include <libxml/valid.h>
21#include <libxml/parser.h>
22#include <libxml/parserInternals.h>
23#include <libxml/xmlerror.h>
24#include <libxml/list.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000025#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000026
Daniel Veillarde62d36c2001-05-15 08:53:16 +000027/* #define DEBUG_VALID_ALGO */
28
Owen Taylor3473f882001-02-23 17:55:21 +000029/*
30 * Generic function for accessing stacks in the Validity Context
31 */
32
33#define PUSH_AND_POP(scope, type, name) \
34scope int name##VPush(xmlValidCtxtPtr ctxt, type value) { \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000035 if (ctxt->name##Max <= 0) { \
36 ctxt->name##Max = 4; \
37 ctxt->name##Tab = (type *) xmlMalloc( \
38 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
39 if (ctxt->name##Tab == NULL) { \
40 xmlGenericError(xmlGenericErrorContext, \
41 "malloc failed !\n"); \
Daniel Veillarda9142e72001-06-19 11:07:54 +000042 ctxt->name##Max = 0; \
Daniel Veillard34b1b3a2001-04-21 14:16:10 +000043 return(0); \
44 } \
45 } \
Owen Taylor3473f882001-02-23 17:55:21 +000046 if (ctxt->name##Nr >= ctxt->name##Max) { \
47 ctxt->name##Max *= 2; \
48 ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \
49 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
50 if (ctxt->name##Tab == NULL) { \
51 xmlGenericError(xmlGenericErrorContext, \
52 "realloc failed !\n"); \
53 return(0); \
54 } \
55 } \
56 ctxt->name##Tab[ctxt->name##Nr] = value; \
57 ctxt->name = value; \
58 return(ctxt->name##Nr++); \
59} \
60scope type name##VPop(xmlValidCtxtPtr ctxt) { \
61 type ret; \
62 if (ctxt->name##Nr <= 0) return(0); \
63 ctxt->name##Nr--; \
64 if (ctxt->name##Nr > 0) \
65 ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \
66 else \
67 ctxt->name = NULL; \
68 ret = ctxt->name##Tab[ctxt->name##Nr]; \
69 ctxt->name##Tab[ctxt->name##Nr] = 0; \
70 return(ret); \
71} \
72
Daniel Veillarddab4cb32001-04-20 13:03:48 +000073/*
Daniel Veillardb44025c2001-10-11 22:55:55 +000074 * I use a home made algorithm less complex and easier to
Daniel Veillardcbaf3992001-12-31 16:16:02 +000075 * debug/maintain than a generic NFA -> DFA state based algo. The
Daniel Veillarddab4cb32001-04-20 13:03:48 +000076 * only restriction is on the deepness of the tree limited by the
77 * size of the occurs bitfield
78 *
79 * this is the content of a saved state for rollbacks
80 */
81
82#define ROLLBACK_OR 0
83#define ROLLBACK_PARENT 1
84
Daniel Veillardb44025c2001-10-11 22:55:55 +000085typedef struct _xmlValidState {
Daniel Veillarddab4cb32001-04-20 13:03:48 +000086 xmlElementContentPtr cont; /* pointer to the content model subtree */
87 xmlNodePtr node; /* pointer to the current node in the list */
Daniel Veillardcbaf3992001-12-31 16:16:02 +000088 long occurs;/* bitfield for multiple occurrences */
Daniel Veillarddab4cb32001-04-20 13:03:48 +000089 unsigned char depth; /* current depth in the overall tree */
90 unsigned char state; /* ROLLBACK_XXX */
91} _xmlValidState;
92
93#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
94#define CONT ctxt->vstate->cont
95#define NODE ctxt->vstate->node
96#define DEPTH ctxt->vstate->depth
97#define OCCURS ctxt->vstate->occurs
98#define STATE ctxt->vstate->state
99
Daniel Veillard5344c602001-12-31 16:37:34 +0000100#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
101#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000102
Daniel Veillard5344c602001-12-31 16:37:34 +0000103#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
104#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000105
106static int
107vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
108 xmlNodePtr node, unsigned char depth, long occurs,
109 unsigned char state) {
Daniel Veillardbed7b052001-05-19 14:59:49 +0000110 int i = ctxt->vstateNr - 1;
111
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000112 if (ctxt->vstateNr >= ctxt->vstateMax) {
113 ctxt->vstateMax *= 2;
114 ctxt->vstateTab = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
115 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
116 if (ctxt->vstateTab == NULL) {
117 xmlGenericError(xmlGenericErrorContext,
118 "realloc failed !n");
119 return(0);
120 }
Daniel Veillard06803992001-04-22 10:35:56 +0000121 ctxt->vstate = &ctxt->vstateTab[0];
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000122 }
Daniel Veillardbed7b052001-05-19 14:59:49 +0000123 /*
124 * Don't push on the stack a state already here
125 */
126 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
127 (ctxt->vstateTab[i].node == node) &&
128 (ctxt->vstateTab[i].depth == depth) &&
129 (ctxt->vstateTab[i].occurs == occurs) &&
130 (ctxt->vstateTab[i].state == state))
131 return(ctxt->vstateNr);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000132 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
133 ctxt->vstateTab[ctxt->vstateNr].node = node;
134 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
135 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
136 ctxt->vstateTab[ctxt->vstateNr].state = state;
137 return(ctxt->vstateNr++);
138}
139
140static int
141vstateVPop(xmlValidCtxtPtr ctxt) {
142 if (ctxt->vstateNr <= 1) return(-1);
143 ctxt->vstateNr--;
144 ctxt->vstate = &ctxt->vstateTab[0];
145 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
146 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
147 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
148 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
149 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
150 return(ctxt->vstateNr);
151}
152
Owen Taylor3473f882001-02-23 17:55:21 +0000153PUSH_AND_POP(static, xmlNodePtr, node)
154
Owen Taylor3473f882001-02-23 17:55:21 +0000155#ifdef DEBUG_VALID_ALGO
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000156static void
157xmlValidPrintNode(xmlNodePtr cur) {
158 if (cur == NULL) {
159 xmlGenericError(xmlGenericErrorContext, "null");
160 return;
161 }
162 switch (cur->type) {
163 case XML_ELEMENT_NODE:
164 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
165 break;
166 case XML_TEXT_NODE:
167 xmlGenericError(xmlGenericErrorContext, "text ");
168 break;
169 case XML_CDATA_SECTION_NODE:
170 xmlGenericError(xmlGenericErrorContext, "cdata ");
171 break;
172 case XML_ENTITY_REF_NODE:
173 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
174 break;
175 case XML_PI_NODE:
176 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
177 break;
178 case XML_COMMENT_NODE:
179 xmlGenericError(xmlGenericErrorContext, "comment ");
180 break;
181 case XML_ATTRIBUTE_NODE:
182 xmlGenericError(xmlGenericErrorContext, "?attr? ");
183 break;
184 case XML_ENTITY_NODE:
185 xmlGenericError(xmlGenericErrorContext, "?ent? ");
186 break;
187 case XML_DOCUMENT_NODE:
188 xmlGenericError(xmlGenericErrorContext, "?doc? ");
189 break;
190 case XML_DOCUMENT_TYPE_NODE:
191 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
192 break;
193 case XML_DOCUMENT_FRAG_NODE:
194 xmlGenericError(xmlGenericErrorContext, "?frag? ");
195 break;
196 case XML_NOTATION_NODE:
197 xmlGenericError(xmlGenericErrorContext, "?nota? ");
198 break;
199 case XML_HTML_DOCUMENT_NODE:
200 xmlGenericError(xmlGenericErrorContext, "?html? ");
201 break;
Daniel Veillardce2c2f02001-10-18 14:57:24 +0000202#ifdef LIBXML_DOCB_ENABLED
203 case XML_DOCB_DOCUMENT_NODE:
204 xmlGenericError(xmlGenericErrorContext, "?docb? ");
205 break;
206#endif
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000207 case XML_DTD_NODE:
208 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
209 break;
210 case XML_ELEMENT_DECL:
211 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
212 break;
213 case XML_ATTRIBUTE_DECL:
214 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
215 break;
216 case XML_ENTITY_DECL:
217 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
218 break;
219 case XML_NAMESPACE_DECL:
220 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
221 break;
222 case XML_XINCLUDE_START:
223 xmlGenericError(xmlGenericErrorContext, "incstart ");
224 break;
225 case XML_XINCLUDE_END:
226 xmlGenericError(xmlGenericErrorContext, "incend ");
227 break;
228 }
229}
230
231static void
232xmlValidPrintNodeList(xmlNodePtr cur) {
Owen Taylor3473f882001-02-23 17:55:21 +0000233 if (cur == NULL)
234 xmlGenericError(xmlGenericErrorContext, "null ");
235 while (cur != NULL) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000236 xmlValidPrintNode(cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000237 cur = cur->next;
238 }
239}
240
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000241static void
242xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
Owen Taylor3473f882001-02-23 17:55:21 +0000243 char expr[1000];
244
245 expr[0] = 0;
246 xmlGenericError(xmlGenericErrorContext, "valid: ");
247 xmlValidPrintNodeList(cur);
248 xmlGenericError(xmlGenericErrorContext, "against ");
Daniel Veillardd3d06722001-08-15 12:06:36 +0000249 xmlSnprintfElementContent(expr, 5000, cont, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000250 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
251}
252
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000253static void
254xmlValidDebugState(xmlValidStatePtr state) {
255 xmlGenericError(xmlGenericErrorContext, "(");
256 if (state->cont == NULL)
257 xmlGenericError(xmlGenericErrorContext, "null,");
258 else
259 switch (state->cont->type) {
260 case XML_ELEMENT_CONTENT_PCDATA:
261 xmlGenericError(xmlGenericErrorContext, "pcdata,");
262 break;
263 case XML_ELEMENT_CONTENT_ELEMENT:
264 xmlGenericError(xmlGenericErrorContext, "%s,",
265 state->cont->name);
266 break;
267 case XML_ELEMENT_CONTENT_SEQ:
268 xmlGenericError(xmlGenericErrorContext, "seq,");
269 break;
270 case XML_ELEMENT_CONTENT_OR:
271 xmlGenericError(xmlGenericErrorContext, "or,");
272 break;
273 }
274 xmlValidPrintNode(state->node);
275 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
276 state->depth, state->occurs, state->state);
277}
278
279static void
280xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
281 int i, j;
282
283 xmlGenericError(xmlGenericErrorContext, "state: ");
284 xmlValidDebugState(ctxt->vstate);
285 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
286 ctxt->vstateNr - 1);
287 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
288 xmlValidDebugState(&ctxt->vstateTab[j]);
289 xmlGenericError(xmlGenericErrorContext, "\n");
290}
291
292/*****
Owen Taylor3473f882001-02-23 17:55:21 +0000293#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000294 *****/
295
296#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
Daniel Veillarde356c282001-03-10 12:32:04 +0000297#define DEBUG_VALID_MSG(m) \
298 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
299
Owen Taylor3473f882001-02-23 17:55:21 +0000300#else
301#define DEBUG_VALID_STATE(n,c)
Daniel Veillarde356c282001-03-10 12:32:04 +0000302#define DEBUG_VALID_MSG(m)
Owen Taylor3473f882001-02-23 17:55:21 +0000303#endif
304
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000305/* TODO: use hash table for accesses to elem and attribute definitions */
Owen Taylor3473f882001-02-23 17:55:21 +0000306
307#define VERROR \
308 if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error
309
310#define VWARNING \
311 if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning
312
313#define CHECK_DTD \
314 if (doc == NULL) return(0); \
315 else if ((doc->intSubset == NULL) && \
316 (doc->extSubset == NULL)) return(0)
317
Daniel Veillarda10efa82001-04-18 13:09:01 +0000318static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
319 int create);
Owen Taylor3473f882001-02-23 17:55:21 +0000320xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
321
322/************************************************************************
323 * *
324 * QName handling helper *
325 * *
326 ************************************************************************/
327
328/**
329 * xmlSplitQName2:
330 * @name: an XML parser context
331 * @prefix: a xmlChar **
332 *
333 * parse an XML qualified name string
334 *
335 * [NS 5] QName ::= (Prefix ':')? LocalPart
336 *
337 * [NS 6] Prefix ::= NCName
338 *
339 * [NS 7] LocalPart ::= NCName
340 *
341 * Returns NULL if not a QName, otherwise the local part, and prefix
342 * is updated to get the Prefix if any.
343 */
344
345xmlChar *
346xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
347 int len = 0;
348 xmlChar *ret = NULL;
349
350 *prefix = NULL;
351
Daniel Veillardf4309d72001-10-02 09:28:58 +0000352#ifndef XML_XML_NAMESPACE
Owen Taylor3473f882001-02-23 17:55:21 +0000353 /* xml: prefix is not really a namespace */
354 if ((name[0] == 'x') && (name[1] == 'm') &&
355 (name[2] == 'l') && (name[3] == ':'))
356 return(NULL);
Daniel Veillardf4309d72001-10-02 09:28:58 +0000357#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000358
359 /* nasty but valid */
360 if (name[0] == ':')
361 return(NULL);
362
363 /*
364 * we are not trying to validate but just to cut, and yes it will
365 * work even if this is as set of UTF-8 encoded chars
366 */
367 while ((name[len] != 0) && (name[len] != ':'))
368 len++;
369
370 if (name[len] == 0)
371 return(NULL);
372
373 *prefix = xmlStrndup(name, len);
374 ret = xmlStrdup(&name[len + 1]);
375
376 return(ret);
377}
378
379/****************************************************************
380 * *
381 * Util functions for data allocation/deallocation *
382 * *
383 ****************************************************************/
384
385/**
386 * xmlNewElementContent:
387 * @name: the subelement name or NULL
388 * @type: the type of element content decl
389 *
390 * Allocate an element content structure.
391 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000392 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000393 */
394xmlElementContentPtr
395xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
396 xmlElementContentPtr ret;
397
398 switch(type) {
399 case XML_ELEMENT_CONTENT_ELEMENT:
400 if (name == NULL) {
401 xmlGenericError(xmlGenericErrorContext,
402 "xmlNewElementContent : name == NULL !\n");
403 }
404 break;
405 case XML_ELEMENT_CONTENT_PCDATA:
406 case XML_ELEMENT_CONTENT_SEQ:
407 case XML_ELEMENT_CONTENT_OR:
408 if (name != NULL) {
409 xmlGenericError(xmlGenericErrorContext,
410 "xmlNewElementContent : name != NULL !\n");
411 }
412 break;
413 default:
414 xmlGenericError(xmlGenericErrorContext,
415 "xmlNewElementContent: unknown type %d\n", type);
416 return(NULL);
417 }
418 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
419 if (ret == NULL) {
420 xmlGenericError(xmlGenericErrorContext,
421 "xmlNewElementContent : out of memory!\n");
422 return(NULL);
423 }
424 ret->type = type;
425 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000426 if (name != NULL) {
427 xmlChar *prefix = NULL;
428 ret->name = xmlSplitQName2(name, &prefix);
429 if (ret->name == NULL)
430 ret->name = xmlStrdup(name);
431 ret->prefix = prefix;
432 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000433 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000434 ret->prefix = NULL;
435 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000436 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000437 return(ret);
438}
439
440/**
441 * xmlCopyElementContent:
442 * @content: An element content pointer.
443 *
444 * Build a copy of an element content description.
445 *
446 * Returns the new xmlElementContentPtr or NULL in case of error.
447 */
448xmlElementContentPtr
449xmlCopyElementContent(xmlElementContentPtr cur) {
450 xmlElementContentPtr ret;
451
452 if (cur == NULL) return(NULL);
453 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
454 if (ret == NULL) {
455 xmlGenericError(xmlGenericErrorContext,
456 "xmlCopyElementContent : out of memory\n");
457 return(NULL);
458 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000459 if (cur->prefix != NULL)
460 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000461 ret->ocur = cur->ocur;
462 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000463 if (ret->c1 != NULL)
464 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000465 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000466 if (ret->c2 != NULL)
467 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000468 return(ret);
469}
470
471/**
472 * xmlFreeElementContent:
473 * @cur: the element content tree to free
474 *
475 * Free an element content structure. This is a recursive call !
476 */
477void
478xmlFreeElementContent(xmlElementContentPtr cur) {
479 if (cur == NULL) return;
480 switch (cur->type) {
481 case XML_ELEMENT_CONTENT_PCDATA:
482 case XML_ELEMENT_CONTENT_ELEMENT:
483 case XML_ELEMENT_CONTENT_SEQ:
484 case XML_ELEMENT_CONTENT_OR:
485 break;
486 default:
487 xmlGenericError(xmlGenericErrorContext,
488 "xmlFreeElementContent : type %d\n", cur->type);
489 return;
490 }
491 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
492 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
493 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000494 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000495 xmlFree(cur);
496}
497
498/**
499 * xmlDumpElementContent:
500 * @buf: An XML buffer
501 * @content: An element table
502 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
503 *
504 * This will dump the content of the element table as an XML DTD definition
505 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000506static void
Owen Taylor3473f882001-02-23 17:55:21 +0000507xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
508 if (content == NULL) return;
509
510 if (glob) xmlBufferWriteChar(buf, "(");
511 switch (content->type) {
512 case XML_ELEMENT_CONTENT_PCDATA:
513 xmlBufferWriteChar(buf, "#PCDATA");
514 break;
515 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000516 if (content->prefix != NULL) {
517 xmlBufferWriteCHAR(buf, content->prefix);
518 xmlBufferWriteChar(buf, ":");
519 }
Owen Taylor3473f882001-02-23 17:55:21 +0000520 xmlBufferWriteCHAR(buf, content->name);
521 break;
522 case XML_ELEMENT_CONTENT_SEQ:
523 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
524 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
525 xmlDumpElementContent(buf, content->c1, 1);
526 else
527 xmlDumpElementContent(buf, content->c1, 0);
528 xmlBufferWriteChar(buf, " , ");
529 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
530 xmlDumpElementContent(buf, content->c2, 1);
531 else
532 xmlDumpElementContent(buf, content->c2, 0);
533 break;
534 case XML_ELEMENT_CONTENT_OR:
535 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
536 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
537 xmlDumpElementContent(buf, content->c1, 1);
538 else
539 xmlDumpElementContent(buf, content->c1, 0);
540 xmlBufferWriteChar(buf, " | ");
541 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
542 xmlDumpElementContent(buf, content->c2, 1);
543 else
544 xmlDumpElementContent(buf, content->c2, 0);
545 break;
546 default:
547 xmlGenericError(xmlGenericErrorContext,
548 "xmlDumpElementContent: unknown type %d\n",
549 content->type);
550 }
551 if (glob)
552 xmlBufferWriteChar(buf, ")");
553 switch (content->ocur) {
554 case XML_ELEMENT_CONTENT_ONCE:
555 break;
556 case XML_ELEMENT_CONTENT_OPT:
557 xmlBufferWriteChar(buf, "?");
558 break;
559 case XML_ELEMENT_CONTENT_MULT:
560 xmlBufferWriteChar(buf, "*");
561 break;
562 case XML_ELEMENT_CONTENT_PLUS:
563 xmlBufferWriteChar(buf, "+");
564 break;
565 }
566}
567
568/**
569 * xmlSprintfElementContent:
570 * @buf: an output buffer
571 * @content: An element table
572 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
573 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000574 * Deprecated, unsafe, use xmlSnprintfElementContent
575 */
576void
577xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
578 xmlElementContentPtr content ATTRIBUTE_UNUSED,
579 int glob ATTRIBUTE_UNUSED) {
580}
581
582/**
583 * xmlSnprintfElementContent:
584 * @buf: an output buffer
585 * @size: the buffer size
586 * @content: An element table
587 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
588 *
Owen Taylor3473f882001-02-23 17:55:21 +0000589 * This will dump the content of the element content definition
590 * Intended just for the debug routine
591 */
592void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000593xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
594 int len;
595
Owen Taylor3473f882001-02-23 17:55:21 +0000596 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000597 len = strlen(buf);
598 if (size - len < 50) {
599 if ((size - len > 4) && (buf[len - 1] != '.'))
600 strcat(buf, " ...");
601 return;
602 }
Owen Taylor3473f882001-02-23 17:55:21 +0000603 if (glob) strcat(buf, "(");
604 switch (content->type) {
605 case XML_ELEMENT_CONTENT_PCDATA:
606 strcat(buf, "#PCDATA");
607 break;
608 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000609 if (content->prefix != NULL) {
610 if (size - len < xmlStrlen(content->prefix + 10)) {
611 strcat(buf, " ...");
612 return;
613 }
614 strcat(buf, (char *) content->prefix);
615 strcat(buf, ":");
616 }
Daniel Veillardd3d06722001-08-15 12:06:36 +0000617 if (size - len < xmlStrlen(content->name + 10)) {
618 strcat(buf, " ...");
619 return;
620 }
Owen Taylor3473f882001-02-23 17:55:21 +0000621 strcat(buf, (char *) content->name);
622 break;
623 case XML_ELEMENT_CONTENT_SEQ:
624 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
625 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000626 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000627 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000628 xmlSnprintfElementContent(buf, size, content->c1, 0);
629 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 strcat(buf, " , ");
636 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000637 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000638 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000639 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000640 break;
641 case XML_ELEMENT_CONTENT_OR:
642 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
643 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000644 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000645 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000646 xmlSnprintfElementContent(buf, size, content->c1, 0);
647 len = strlen(buf);
648 if (size - len < 50) {
649 if ((size - len > 4) && (buf[len - 1] != '.'))
650 strcat(buf, " ...");
651 return;
652 }
Owen Taylor3473f882001-02-23 17:55:21 +0000653 strcat(buf, " | ");
654 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000655 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000656 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000657 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000658 break;
659 }
660 if (glob)
661 strcat(buf, ")");
662 switch (content->ocur) {
663 case XML_ELEMENT_CONTENT_ONCE:
664 break;
665 case XML_ELEMENT_CONTENT_OPT:
666 strcat(buf, "?");
667 break;
668 case XML_ELEMENT_CONTENT_MULT:
669 strcat(buf, "*");
670 break;
671 case XML_ELEMENT_CONTENT_PLUS:
672 strcat(buf, "+");
673 break;
674 }
675}
676
677/****************************************************************
678 * *
679 * Registration of DTD declarations *
680 * *
681 ****************************************************************/
682
683/**
684 * xmlCreateElementTable:
685 *
686 * create and initialize an empty element hash table.
687 *
688 * Returns the xmlElementTablePtr just created or NULL in case of error.
689 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000690static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000691xmlCreateElementTable(void) {
692 return(xmlHashCreate(0));
693}
694
695/**
696 * xmlFreeElement:
697 * @elem: An element
698 *
699 * Deallocate the memory used by an element definition
700 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000701static void
Owen Taylor3473f882001-02-23 17:55:21 +0000702xmlFreeElement(xmlElementPtr elem) {
703 if (elem == NULL) return;
704 xmlUnlinkNode((xmlNodePtr) elem);
705 xmlFreeElementContent(elem->content);
706 if (elem->name != NULL)
707 xmlFree((xmlChar *) elem->name);
708 if (elem->prefix != NULL)
709 xmlFree((xmlChar *) elem->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000710 xmlFree(elem);
711}
712
713
714/**
715 * xmlAddElementDecl:
716 * @ctxt: the validation context
717 * @dtd: pointer to the DTD
718 * @name: the entity name
719 * @type: the element type
720 * @content: the element content tree or NULL
721 *
722 * Register a new element declaration
723 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000724 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000725 */
726xmlElementPtr
727xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
728 xmlElementTypeVal type,
729 xmlElementContentPtr content) {
730 xmlElementPtr ret;
731 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +0000732 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000733 xmlChar *ns, *uqname;
734
735 if (dtd == NULL) {
736 xmlGenericError(xmlGenericErrorContext,
737 "xmlAddElementDecl: dtd == NULL\n");
738 return(NULL);
739 }
740 if (name == NULL) {
741 xmlGenericError(xmlGenericErrorContext,
742 "xmlAddElementDecl: name == NULL\n");
743 return(NULL);
744 }
745 switch (type) {
746 case XML_ELEMENT_TYPE_EMPTY:
747 if (content != NULL) {
748 xmlGenericError(xmlGenericErrorContext,
749 "xmlAddElementDecl: content != NULL for EMPTY\n");
750 return(NULL);
751 }
752 break;
753 case XML_ELEMENT_TYPE_ANY:
754 if (content != NULL) {
755 xmlGenericError(xmlGenericErrorContext,
756 "xmlAddElementDecl: content != NULL for ANY\n");
757 return(NULL);
758 }
759 break;
760 case XML_ELEMENT_TYPE_MIXED:
761 if (content == NULL) {
762 xmlGenericError(xmlGenericErrorContext,
763 "xmlAddElementDecl: content == NULL for MIXED\n");
764 return(NULL);
765 }
766 break;
767 case XML_ELEMENT_TYPE_ELEMENT:
768 if (content == NULL) {
769 xmlGenericError(xmlGenericErrorContext,
770 "xmlAddElementDecl: content == NULL for ELEMENT\n");
771 return(NULL);
772 }
773 break;
774 default:
775 xmlGenericError(xmlGenericErrorContext,
776 "xmlAddElementDecl: unknown type %d\n", type);
777 return(NULL);
778 }
779
780 /*
781 * check if name is a QName
782 */
783 uqname = xmlSplitQName2(name, &ns);
784 if (uqname != NULL)
785 name = uqname;
786
787 /*
788 * Create the Element table if needed.
789 */
790 table = (xmlElementTablePtr) dtd->elements;
791 if (table == NULL) {
792 table = xmlCreateElementTable();
793 dtd->elements = (void *) table;
794 }
795 if (table == NULL) {
796 xmlGenericError(xmlGenericErrorContext,
797 "xmlAddElementDecl: Table creation failed!\n");
798 return(NULL);
799 }
800
Daniel Veillarda10efa82001-04-18 13:09:01 +0000801 /*
802 * lookup old attributes inserted on an undefined element in the
803 * internal subset.
804 */
805 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
806 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
807 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
808 oldAttributes = ret->attributes;
809 ret->attributes = NULL;
810 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
811 xmlFreeElement(ret);
812 }
Owen Taylor3473f882001-02-23 17:55:21 +0000813 }
Owen Taylor3473f882001-02-23 17:55:21 +0000814
815 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +0000816 * The element may already be present if one of its attribute
817 * was registered first
818 */
819 ret = xmlHashLookup2(table, name, ns);
820 if (ret != NULL) {
821 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
822 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000823 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000824 */
825 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
826 if (uqname != NULL)
827 xmlFree(uqname);
828 return(NULL);
829 }
830 } else {
831 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
832 if (ret == NULL) {
833 xmlGenericError(xmlGenericErrorContext,
834 "xmlAddElementDecl: out of memory\n");
835 return(NULL);
836 }
837 memset(ret, 0, sizeof(xmlElement));
838 ret->type = XML_ELEMENT_DECL;
839
840 /*
841 * fill the structure.
842 */
843 ret->name = xmlStrdup(name);
844 ret->prefix = ns;
845
846 /*
847 * Validity Check:
848 * Insertion must not fail
849 */
850 if (xmlHashAddEntry2(table, name, ns, ret)) {
851 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000852 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000853 */
854 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
855 xmlFreeElement(ret);
856 if (uqname != NULL)
857 xmlFree(uqname);
858 return(NULL);
859 }
860 }
861
862 /*
863 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +0000864 */
865 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +0000866 ret->content = xmlCopyElementContent(content);
Daniel Veillarda10efa82001-04-18 13:09:01 +0000867 ret->attributes = oldAttributes;
Owen Taylor3473f882001-02-23 17:55:21 +0000868
869 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000870 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000871 */
872 ret->parent = dtd;
873 ret->doc = dtd->doc;
874 if (dtd->last == NULL) {
875 dtd->children = dtd->last = (xmlNodePtr) ret;
876 } else {
877 dtd->last->next = (xmlNodePtr) ret;
878 ret->prev = dtd->last;
879 dtd->last = (xmlNodePtr) ret;
880 }
881 if (uqname != NULL)
882 xmlFree(uqname);
883 return(ret);
884}
885
886/**
887 * xmlFreeElementTable:
888 * @table: An element table
889 *
890 * Deallocate the memory used by an element hash table.
891 */
892void
893xmlFreeElementTable(xmlElementTablePtr table) {
894 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
895}
896
897/**
898 * xmlCopyElement:
899 * @elem: An element
900 *
901 * Build a copy of an element.
902 *
903 * Returns the new xmlElementPtr or NULL in case of error.
904 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000905static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000906xmlCopyElement(xmlElementPtr elem) {
907 xmlElementPtr cur;
908
909 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
910 if (cur == NULL) {
911 xmlGenericError(xmlGenericErrorContext,
912 "xmlCopyElement: out of memory !\n");
913 return(NULL);
914 }
915 memset(cur, 0, sizeof(xmlElement));
916 cur->type = XML_ELEMENT_DECL;
917 cur->etype = elem->etype;
918 if (elem->name != NULL)
919 cur->name = xmlStrdup(elem->name);
920 else
921 cur->name = NULL;
922 if (elem->prefix != NULL)
923 cur->prefix = xmlStrdup(elem->prefix);
924 else
925 cur->prefix = NULL;
926 cur->content = xmlCopyElementContent(elem->content);
927 /* TODO : rebuild the attribute list on the copy */
928 cur->attributes = NULL;
929 return(cur);
930}
931
932/**
933 * xmlCopyElementTable:
934 * @table: An element table
935 *
936 * Build a copy of an element table.
937 *
938 * Returns the new xmlElementTablePtr or NULL in case of error.
939 */
940xmlElementTablePtr
941xmlCopyElementTable(xmlElementTablePtr table) {
942 return((xmlElementTablePtr) xmlHashCopy(table,
943 (xmlHashCopier) xmlCopyElement));
944}
945
946/**
947 * xmlDumpElementDecl:
948 * @buf: the XML buffer output
949 * @elem: An element table
950 *
951 * This will dump the content of the element declaration as an XML
952 * DTD definition
953 */
954void
955xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
956 switch (elem->etype) {
957 case XML_ELEMENT_TYPE_EMPTY:
958 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000959 if (elem->prefix != NULL) {
960 xmlBufferWriteCHAR(buf, elem->prefix);
961 xmlBufferWriteChar(buf, ":");
962 }
Owen Taylor3473f882001-02-23 17:55:21 +0000963 xmlBufferWriteCHAR(buf, elem->name);
964 xmlBufferWriteChar(buf, " EMPTY>\n");
965 break;
966 case XML_ELEMENT_TYPE_ANY:
967 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000968 if (elem->prefix != NULL) {
969 xmlBufferWriteCHAR(buf, elem->prefix);
970 xmlBufferWriteChar(buf, ":");
971 }
Owen Taylor3473f882001-02-23 17:55:21 +0000972 xmlBufferWriteCHAR(buf, elem->name);
973 xmlBufferWriteChar(buf, " ANY>\n");
974 break;
975 case XML_ELEMENT_TYPE_MIXED:
976 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000977 if (elem->prefix != NULL) {
978 xmlBufferWriteCHAR(buf, elem->prefix);
979 xmlBufferWriteChar(buf, ":");
980 }
Owen Taylor3473f882001-02-23 17:55:21 +0000981 xmlBufferWriteCHAR(buf, elem->name);
982 xmlBufferWriteChar(buf, " ");
983 xmlDumpElementContent(buf, elem->content, 1);
984 xmlBufferWriteChar(buf, ">\n");
985 break;
986 case XML_ELEMENT_TYPE_ELEMENT:
987 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000988 if (elem->prefix != NULL) {
989 xmlBufferWriteCHAR(buf, elem->prefix);
990 xmlBufferWriteChar(buf, ":");
991 }
Owen Taylor3473f882001-02-23 17:55:21 +0000992 xmlBufferWriteCHAR(buf, elem->name);
993 xmlBufferWriteChar(buf, " ");
994 xmlDumpElementContent(buf, elem->content, 1);
995 xmlBufferWriteChar(buf, ">\n");
996 break;
997 default:
998 xmlGenericError(xmlGenericErrorContext,
999 "xmlDumpElementDecl: internal: unknown type %d\n",
1000 elem->etype);
1001 }
1002}
1003
1004/**
1005 * xmlDumpElementTable:
1006 * @buf: the XML buffer output
1007 * @table: An element table
1008 *
1009 * This will dump the content of the element table as an XML DTD definition
1010 */
1011void
1012xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1013 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1014}
1015
1016/**
1017 * xmlCreateEnumeration:
1018 * @name: the enumeration name or NULL
1019 *
1020 * create and initialize an enumeration attribute node.
1021 *
1022 * Returns the xmlEnumerationPtr just created or NULL in case
1023 * of error.
1024 */
1025xmlEnumerationPtr
1026xmlCreateEnumeration(xmlChar *name) {
1027 xmlEnumerationPtr ret;
1028
1029 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1030 if (ret == NULL) {
1031 xmlGenericError(xmlGenericErrorContext,
1032 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1033 (long)sizeof(xmlEnumeration));
1034 return(NULL);
1035 }
1036 memset(ret, 0, sizeof(xmlEnumeration));
1037
1038 if (name != NULL)
1039 ret->name = xmlStrdup(name);
1040 return(ret);
1041}
1042
1043/**
1044 * xmlFreeEnumeration:
1045 * @cur: the tree to free.
1046 *
1047 * free an enumeration attribute node (recursive).
1048 */
1049void
1050xmlFreeEnumeration(xmlEnumerationPtr cur) {
1051 if (cur == NULL) return;
1052
1053 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1054
1055 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001056 xmlFree(cur);
1057}
1058
1059/**
1060 * xmlCopyEnumeration:
1061 * @cur: the tree to copy.
1062 *
1063 * Copy an enumeration attribute node (recursive).
1064 *
1065 * Returns the xmlEnumerationPtr just created or NULL in case
1066 * of error.
1067 */
1068xmlEnumerationPtr
1069xmlCopyEnumeration(xmlEnumerationPtr cur) {
1070 xmlEnumerationPtr ret;
1071
1072 if (cur == NULL) return(NULL);
1073 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1074
1075 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1076 else ret->next = NULL;
1077
1078 return(ret);
1079}
1080
1081/**
1082 * xmlDumpEnumeration:
1083 * @buf: the XML buffer output
1084 * @enum: An enumeration
1085 *
1086 * This will dump the content of the enumeration
1087 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001088static void
Owen Taylor3473f882001-02-23 17:55:21 +00001089xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1090 if (cur == NULL) return;
1091
1092 xmlBufferWriteCHAR(buf, cur->name);
1093 if (cur->next == NULL)
1094 xmlBufferWriteChar(buf, ")");
1095 else {
1096 xmlBufferWriteChar(buf, " | ");
1097 xmlDumpEnumeration(buf, cur->next);
1098 }
1099}
1100
1101/**
1102 * xmlCreateAttributeTable:
1103 *
1104 * create and initialize an empty attribute hash table.
1105 *
1106 * Returns the xmlAttributeTablePtr just created or NULL in case
1107 * of error.
1108 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001109static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001110xmlCreateAttributeTable(void) {
1111 return(xmlHashCreate(0));
1112}
1113
1114/**
1115 * xmlScanAttributeDeclCallback:
1116 * @attr: the attribute decl
1117 * @list: the list to update
1118 *
1119 * Callback called by xmlScanAttributeDecl when a new attribute
1120 * has to be entered in the list.
1121 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001122static void
Owen Taylor3473f882001-02-23 17:55:21 +00001123xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001124 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001125 attr->nexth = *list;
1126 *list = attr;
1127}
1128
1129/**
1130 * xmlScanAttributeDecl:
1131 * @dtd: pointer to the DTD
1132 * @elem: the element name
1133 *
1134 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001135 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001136 *
1137 * Returns the pointer to the first attribute decl in the chain,
1138 * possibly NULL.
1139 */
1140xmlAttributePtr
1141xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1142 xmlAttributePtr ret = NULL;
1143 xmlAttributeTablePtr table;
1144
1145 if (dtd == NULL) {
1146 xmlGenericError(xmlGenericErrorContext,
1147 "xmlScanAttributeDecl: dtd == NULL\n");
1148 return(NULL);
1149 }
1150 if (elem == NULL) {
1151 xmlGenericError(xmlGenericErrorContext,
1152 "xmlScanAttributeDecl: elem == NULL\n");
1153 return(NULL);
1154 }
1155 table = (xmlAttributeTablePtr) dtd->attributes;
1156 if (table == NULL)
1157 return(NULL);
1158
1159 /* WRONG !!! */
1160 xmlHashScan3(table, NULL, NULL, elem,
1161 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1162 return(ret);
1163}
1164
1165/**
1166 * xmlScanIDAttributeDecl:
1167 * @ctxt: the validation context
1168 * @elem: the element name
1169 *
1170 * Verify that the element don't have too many ID attributes
1171 * declared.
1172 *
1173 * Returns the number of ID attributes found.
1174 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001175static int
Owen Taylor3473f882001-02-23 17:55:21 +00001176xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1177 xmlAttributePtr cur;
1178 int ret = 0;
1179
1180 if (elem == NULL) return(0);
1181 cur = elem->attributes;
1182 while (cur != NULL) {
1183 if (cur->atype == XML_ATTRIBUTE_ID) {
1184 ret ++;
1185 if (ret > 1)
1186 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001187 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001188 elem->name, cur->name);
1189 }
1190 cur = cur->nexth;
1191 }
1192 return(ret);
1193}
1194
1195/**
1196 * xmlFreeAttribute:
1197 * @elem: An attribute
1198 *
1199 * Deallocate the memory used by an attribute definition
1200 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001201static void
Owen Taylor3473f882001-02-23 17:55:21 +00001202xmlFreeAttribute(xmlAttributePtr attr) {
1203 if (attr == NULL) return;
1204 xmlUnlinkNode((xmlNodePtr) attr);
1205 if (attr->tree != NULL)
1206 xmlFreeEnumeration(attr->tree);
1207 if (attr->elem != NULL)
1208 xmlFree((xmlChar *) attr->elem);
1209 if (attr->name != NULL)
1210 xmlFree((xmlChar *) attr->name);
1211 if (attr->defaultValue != NULL)
1212 xmlFree((xmlChar *) attr->defaultValue);
1213 if (attr->prefix != NULL)
1214 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001215 xmlFree(attr);
1216}
1217
1218
1219/**
1220 * xmlAddAttributeDecl:
1221 * @ctxt: the validation context
1222 * @dtd: pointer to the DTD
1223 * @elem: the element name
1224 * @name: the attribute name
1225 * @ns: the attribute namespace prefix
1226 * @type: the attribute type
1227 * @def: the attribute default type
1228 * @defaultValue: the attribute default value
1229 * @tree: if it's an enumeration, the associated list
1230 *
1231 * Register a new attribute declaration
1232 * Note that @tree becomes the ownership of the DTD
1233 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001234 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001235 */
1236xmlAttributePtr
1237xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1238 const xmlChar *name, const xmlChar *ns,
1239 xmlAttributeType type, xmlAttributeDefault def,
1240 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1241 xmlAttributePtr ret;
1242 xmlAttributeTablePtr table;
1243 xmlElementPtr elemDef;
1244
1245 if (dtd == NULL) {
1246 xmlGenericError(xmlGenericErrorContext,
1247 "xmlAddAttributeDecl: dtd == NULL\n");
1248 xmlFreeEnumeration(tree);
1249 return(NULL);
1250 }
1251 if (name == NULL) {
1252 xmlGenericError(xmlGenericErrorContext,
1253 "xmlAddAttributeDecl: name == NULL\n");
1254 xmlFreeEnumeration(tree);
1255 return(NULL);
1256 }
1257 if (elem == NULL) {
1258 xmlGenericError(xmlGenericErrorContext,
1259 "xmlAddAttributeDecl: elem == NULL\n");
1260 xmlFreeEnumeration(tree);
1261 return(NULL);
1262 }
1263 /*
1264 * Check the type and possibly the default value.
1265 */
1266 switch (type) {
1267 case XML_ATTRIBUTE_CDATA:
1268 break;
1269 case XML_ATTRIBUTE_ID:
1270 break;
1271 case XML_ATTRIBUTE_IDREF:
1272 break;
1273 case XML_ATTRIBUTE_IDREFS:
1274 break;
1275 case XML_ATTRIBUTE_ENTITY:
1276 break;
1277 case XML_ATTRIBUTE_ENTITIES:
1278 break;
1279 case XML_ATTRIBUTE_NMTOKEN:
1280 break;
1281 case XML_ATTRIBUTE_NMTOKENS:
1282 break;
1283 case XML_ATTRIBUTE_ENUMERATION:
1284 break;
1285 case XML_ATTRIBUTE_NOTATION:
1286 break;
1287 default:
1288 xmlGenericError(xmlGenericErrorContext,
1289 "xmlAddAttributeDecl: unknown type %d\n", type);
1290 xmlFreeEnumeration(tree);
1291 return(NULL);
1292 }
1293 if ((defaultValue != NULL) &&
1294 (!xmlValidateAttributeValue(type, defaultValue))) {
1295 VERROR(ctxt->userData, "Attribute %s on %s: invalid default value\n",
1296 elem, name, defaultValue);
1297 defaultValue = NULL;
Daniel Veillardd01fd3e2002-02-18 22:27:47 +00001298 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001299 }
1300
1301 /*
1302 * Create the Attribute table if needed.
1303 */
1304 table = (xmlAttributeTablePtr) dtd->attributes;
1305 if (table == NULL) {
1306 table = xmlCreateAttributeTable();
1307 dtd->attributes = (void *) table;
1308 }
1309 if (table == NULL) {
1310 xmlGenericError(xmlGenericErrorContext,
1311 "xmlAddAttributeDecl: Table creation failed!\n");
1312 return(NULL);
1313 }
1314
1315
1316 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1317 if (ret == NULL) {
1318 xmlGenericError(xmlGenericErrorContext,
1319 "xmlAddAttributeDecl: out of memory\n");
1320 return(NULL);
1321 }
1322 memset(ret, 0, sizeof(xmlAttribute));
1323 ret->type = XML_ATTRIBUTE_DECL;
1324
1325 /*
1326 * fill the structure.
1327 */
1328 ret->atype = type;
1329 ret->name = xmlStrdup(name);
1330 ret->prefix = xmlStrdup(ns);
1331 ret->elem = xmlStrdup(elem);
1332 ret->def = def;
1333 ret->tree = tree;
1334 if (defaultValue != NULL)
1335 ret->defaultValue = xmlStrdup(defaultValue);
1336
1337 /*
1338 * Validity Check:
1339 * Search the DTD for previous declarations of the ATTLIST
1340 */
1341 if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
1342 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001343 * The attribute is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001344 */
1345 VWARNING(ctxt->userData,
1346 "Attribute %s on %s: already defined\n",
1347 name, elem);
1348 xmlFreeAttribute(ret);
1349 return(NULL);
1350 }
1351
1352 /*
1353 * Validity Check:
1354 * Multiple ID per element
1355 */
Daniel Veillarda10efa82001-04-18 13:09:01 +00001356 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00001357 if (elemDef != NULL) {
Daniel Veillard48da9102001-08-07 01:10:10 +00001358
Owen Taylor3473f882001-02-23 17:55:21 +00001359 if ((type == XML_ATTRIBUTE_ID) &&
Daniel Veillardc7612992002-02-17 22:47:37 +00001360 (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001361 VERROR(ctxt->userData,
1362 "Element %s has too may ID attributes defined : %s\n",
1363 elem, name);
Daniel Veillardc7612992002-02-17 22:47:37 +00001364 ctxt->valid = 0;
1365 }
1366
Daniel Veillard48da9102001-08-07 01:10:10 +00001367 /*
1368 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001369 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001370 */
1371 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1372 ((ret->prefix != NULL &&
1373 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1374 ret->nexth = elemDef->attributes;
1375 elemDef->attributes = ret;
1376 } else {
1377 xmlAttributePtr tmp = elemDef->attributes;
1378
1379 while ((tmp != NULL) &&
1380 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1381 ((ret->prefix != NULL &&
1382 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1383 if (tmp->nexth == NULL)
1384 break;
1385 tmp = tmp->nexth;
1386 }
1387 if (tmp != NULL) {
1388 ret->nexth = tmp->nexth;
1389 tmp->nexth = ret;
1390 } else {
1391 ret->nexth = elemDef->attributes;
1392 elemDef->attributes = ret;
1393 }
1394 }
Owen Taylor3473f882001-02-23 17:55:21 +00001395 }
1396
1397 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001398 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001399 */
1400 ret->parent = dtd;
1401 ret->doc = dtd->doc;
1402 if (dtd->last == NULL) {
1403 dtd->children = dtd->last = (xmlNodePtr) ret;
1404 } else {
1405 dtd->last->next = (xmlNodePtr) ret;
1406 ret->prev = dtd->last;
1407 dtd->last = (xmlNodePtr) ret;
1408 }
1409 return(ret);
1410}
1411
1412/**
1413 * xmlFreeAttributeTable:
1414 * @table: An attribute table
1415 *
1416 * Deallocate the memory used by an entities hash table.
1417 */
1418void
1419xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1420 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1421}
1422
1423/**
1424 * xmlCopyAttribute:
1425 * @attr: An attribute
1426 *
1427 * Build a copy of an attribute.
1428 *
1429 * Returns the new xmlAttributePtr or NULL in case of error.
1430 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001431static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001432xmlCopyAttribute(xmlAttributePtr attr) {
1433 xmlAttributePtr cur;
1434
1435 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1436 if (cur == NULL) {
1437 xmlGenericError(xmlGenericErrorContext,
1438 "xmlCopyAttribute: out of memory !\n");
1439 return(NULL);
1440 }
1441 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001442 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001443 cur->atype = attr->atype;
1444 cur->def = attr->def;
1445 cur->tree = xmlCopyEnumeration(attr->tree);
1446 if (attr->elem != NULL)
1447 cur->elem = xmlStrdup(attr->elem);
1448 if (attr->name != NULL)
1449 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001450 if (attr->prefix != NULL)
1451 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001452 if (attr->defaultValue != NULL)
1453 cur->defaultValue = xmlStrdup(attr->defaultValue);
1454 return(cur);
1455}
1456
1457/**
1458 * xmlCopyAttributeTable:
1459 * @table: An attribute table
1460 *
1461 * Build a copy of an attribute table.
1462 *
1463 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1464 */
1465xmlAttributeTablePtr
1466xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1467 return((xmlAttributeTablePtr) xmlHashCopy(table,
1468 (xmlHashCopier) xmlCopyAttribute));
1469}
1470
1471/**
1472 * xmlDumpAttributeDecl:
1473 * @buf: the XML buffer output
1474 * @attr: An attribute declaration
1475 *
1476 * This will dump the content of the attribute declaration as an XML
1477 * DTD definition
1478 */
1479void
1480xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1481 xmlBufferWriteChar(buf, "<!ATTLIST ");
1482 xmlBufferWriteCHAR(buf, attr->elem);
1483 xmlBufferWriteChar(buf, " ");
1484 if (attr->prefix != NULL) {
1485 xmlBufferWriteCHAR(buf, attr->prefix);
1486 xmlBufferWriteChar(buf, ":");
1487 }
1488 xmlBufferWriteCHAR(buf, attr->name);
1489 switch (attr->atype) {
1490 case XML_ATTRIBUTE_CDATA:
1491 xmlBufferWriteChar(buf, " CDATA");
1492 break;
1493 case XML_ATTRIBUTE_ID:
1494 xmlBufferWriteChar(buf, " ID");
1495 break;
1496 case XML_ATTRIBUTE_IDREF:
1497 xmlBufferWriteChar(buf, " IDREF");
1498 break;
1499 case XML_ATTRIBUTE_IDREFS:
1500 xmlBufferWriteChar(buf, " IDREFS");
1501 break;
1502 case XML_ATTRIBUTE_ENTITY:
1503 xmlBufferWriteChar(buf, " ENTITY");
1504 break;
1505 case XML_ATTRIBUTE_ENTITIES:
1506 xmlBufferWriteChar(buf, " ENTITIES");
1507 break;
1508 case XML_ATTRIBUTE_NMTOKEN:
1509 xmlBufferWriteChar(buf, " NMTOKEN");
1510 break;
1511 case XML_ATTRIBUTE_NMTOKENS:
1512 xmlBufferWriteChar(buf, " NMTOKENS");
1513 break;
1514 case XML_ATTRIBUTE_ENUMERATION:
1515 xmlBufferWriteChar(buf, " (");
1516 xmlDumpEnumeration(buf, attr->tree);
1517 break;
1518 case XML_ATTRIBUTE_NOTATION:
1519 xmlBufferWriteChar(buf, " NOTATION (");
1520 xmlDumpEnumeration(buf, attr->tree);
1521 break;
1522 default:
1523 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001524 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001525 attr->atype);
1526 }
1527 switch (attr->def) {
1528 case XML_ATTRIBUTE_NONE:
1529 break;
1530 case XML_ATTRIBUTE_REQUIRED:
1531 xmlBufferWriteChar(buf, " #REQUIRED");
1532 break;
1533 case XML_ATTRIBUTE_IMPLIED:
1534 xmlBufferWriteChar(buf, " #IMPLIED");
1535 break;
1536 case XML_ATTRIBUTE_FIXED:
1537 xmlBufferWriteChar(buf, " #FIXED");
1538 break;
1539 default:
1540 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001541 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001542 attr->def);
1543 }
1544 if (attr->defaultValue != NULL) {
1545 xmlBufferWriteChar(buf, " ");
1546 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1547 }
1548 xmlBufferWriteChar(buf, ">\n");
1549}
1550
1551/**
1552 * xmlDumpAttributeTable:
1553 * @buf: the XML buffer output
1554 * @table: An attribute table
1555 *
1556 * This will dump the content of the attribute table as an XML DTD definition
1557 */
1558void
1559xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1560 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1561}
1562
1563/************************************************************************
1564 * *
1565 * NOTATIONs *
1566 * *
1567 ************************************************************************/
1568/**
1569 * xmlCreateNotationTable:
1570 *
1571 * create and initialize an empty notation hash table.
1572 *
1573 * Returns the xmlNotationTablePtr just created or NULL in case
1574 * of error.
1575 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001576static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001577xmlCreateNotationTable(void) {
1578 return(xmlHashCreate(0));
1579}
1580
1581/**
1582 * xmlFreeNotation:
1583 * @not: A notation
1584 *
1585 * Deallocate the memory used by an notation definition
1586 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001587static void
Owen Taylor3473f882001-02-23 17:55:21 +00001588xmlFreeNotation(xmlNotationPtr nota) {
1589 if (nota == NULL) return;
1590 if (nota->name != NULL)
1591 xmlFree((xmlChar *) nota->name);
1592 if (nota->PublicID != NULL)
1593 xmlFree((xmlChar *) nota->PublicID);
1594 if (nota->SystemID != NULL)
1595 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001596 xmlFree(nota);
1597}
1598
1599
1600/**
1601 * xmlAddNotationDecl:
1602 * @dtd: pointer to the DTD
1603 * @ctxt: the validation context
1604 * @name: the entity name
1605 * @PublicID: the public identifier or NULL
1606 * @SystemID: the system identifier or NULL
1607 *
1608 * Register a new notation declaration
1609 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001610 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001611 */
1612xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001613xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001614 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001615 const xmlChar *PublicID, const xmlChar *SystemID) {
1616 xmlNotationPtr ret;
1617 xmlNotationTablePtr table;
1618
1619 if (dtd == NULL) {
1620 xmlGenericError(xmlGenericErrorContext,
1621 "xmlAddNotationDecl: dtd == NULL\n");
1622 return(NULL);
1623 }
1624 if (name == NULL) {
1625 xmlGenericError(xmlGenericErrorContext,
1626 "xmlAddNotationDecl: name == NULL\n");
1627 return(NULL);
1628 }
1629 if ((PublicID == NULL) && (SystemID == NULL)) {
1630 xmlGenericError(xmlGenericErrorContext,
1631 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
Daniel Veillard7aea52d2002-02-17 23:07:47 +00001632 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001633 }
1634
1635 /*
1636 * Create the Notation table if needed.
1637 */
1638 table = (xmlNotationTablePtr) dtd->notations;
1639 if (table == NULL)
1640 dtd->notations = table = xmlCreateNotationTable();
1641 if (table == NULL) {
1642 xmlGenericError(xmlGenericErrorContext,
1643 "xmlAddNotationDecl: Table creation failed!\n");
1644 return(NULL);
1645 }
1646
1647 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1648 if (ret == NULL) {
1649 xmlGenericError(xmlGenericErrorContext,
1650 "xmlAddNotationDecl: out of memory\n");
1651 return(NULL);
1652 }
1653 memset(ret, 0, sizeof(xmlNotation));
1654
1655 /*
1656 * fill the structure.
1657 */
1658 ret->name = xmlStrdup(name);
1659 if (SystemID != NULL)
1660 ret->SystemID = xmlStrdup(SystemID);
1661 if (PublicID != NULL)
1662 ret->PublicID = xmlStrdup(PublicID);
1663
1664 /*
1665 * Validity Check:
1666 * Check the DTD for previous declarations of the ATTLIST
1667 */
1668 if (xmlHashAddEntry(table, name, ret)) {
1669 xmlGenericError(xmlGenericErrorContext,
1670 "xmlAddNotationDecl: %s already defined\n", name);
1671 xmlFreeNotation(ret);
1672 return(NULL);
1673 }
1674 return(ret);
1675}
1676
1677/**
1678 * xmlFreeNotationTable:
1679 * @table: An notation table
1680 *
1681 * Deallocate the memory used by an entities hash table.
1682 */
1683void
1684xmlFreeNotationTable(xmlNotationTablePtr table) {
1685 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
1686}
1687
1688/**
1689 * xmlCopyNotation:
1690 * @nota: A notation
1691 *
1692 * Build a copy of a notation.
1693 *
1694 * Returns the new xmlNotationPtr or NULL in case of error.
1695 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001696static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001697xmlCopyNotation(xmlNotationPtr nota) {
1698 xmlNotationPtr cur;
1699
1700 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1701 if (cur == NULL) {
1702 xmlGenericError(xmlGenericErrorContext,
1703 "xmlCopyNotation: out of memory !\n");
1704 return(NULL);
1705 }
1706 if (nota->name != NULL)
1707 cur->name = xmlStrdup(nota->name);
1708 else
1709 cur->name = NULL;
1710 if (nota->PublicID != NULL)
1711 cur->PublicID = xmlStrdup(nota->PublicID);
1712 else
1713 cur->PublicID = NULL;
1714 if (nota->SystemID != NULL)
1715 cur->SystemID = xmlStrdup(nota->SystemID);
1716 else
1717 cur->SystemID = NULL;
1718 return(cur);
1719}
1720
1721/**
1722 * xmlCopyNotationTable:
1723 * @table: A notation table
1724 *
1725 * Build a copy of a notation table.
1726 *
1727 * Returns the new xmlNotationTablePtr or NULL in case of error.
1728 */
1729xmlNotationTablePtr
1730xmlCopyNotationTable(xmlNotationTablePtr table) {
1731 return((xmlNotationTablePtr) xmlHashCopy(table,
1732 (xmlHashCopier) xmlCopyNotation));
1733}
1734
1735/**
1736 * xmlDumpNotationDecl:
1737 * @buf: the XML buffer output
1738 * @nota: A notation declaration
1739 *
1740 * This will dump the content the notation declaration as an XML DTD definition
1741 */
1742void
1743xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
1744 xmlBufferWriteChar(buf, "<!NOTATION ");
1745 xmlBufferWriteCHAR(buf, nota->name);
1746 if (nota->PublicID != NULL) {
1747 xmlBufferWriteChar(buf, " PUBLIC ");
1748 xmlBufferWriteQuotedString(buf, nota->PublicID);
1749 if (nota->SystemID != NULL) {
1750 xmlBufferWriteChar(buf, " ");
1751 xmlBufferWriteCHAR(buf, nota->SystemID);
1752 }
1753 } else {
1754 xmlBufferWriteChar(buf, " SYSTEM ");
1755 xmlBufferWriteCHAR(buf, nota->SystemID);
1756 }
1757 xmlBufferWriteChar(buf, " >\n");
1758}
1759
1760/**
1761 * xmlDumpNotationTable:
1762 * @buf: the XML buffer output
1763 * @table: A notation table
1764 *
1765 * This will dump the content of the notation table as an XML DTD definition
1766 */
1767void
1768xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
1769 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
1770}
1771
1772/************************************************************************
1773 * *
1774 * IDs *
1775 * *
1776 ************************************************************************/
1777/**
1778 * xmlCreateIDTable:
1779 *
1780 * create and initialize an empty id hash table.
1781 *
1782 * Returns the xmlIDTablePtr just created or NULL in case
1783 * of error.
1784 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001785static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001786xmlCreateIDTable(void) {
1787 return(xmlHashCreate(0));
1788}
1789
1790/**
1791 * xmlFreeID:
1792 * @not: A id
1793 *
1794 * Deallocate the memory used by an id definition
1795 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001796static void
Owen Taylor3473f882001-02-23 17:55:21 +00001797xmlFreeID(xmlIDPtr id) {
1798 if (id == NULL) return;
1799 if (id->value != NULL)
1800 xmlFree((xmlChar *) id->value);
Owen Taylor3473f882001-02-23 17:55:21 +00001801 xmlFree(id);
1802}
1803
1804/**
1805 * xmlAddID:
1806 * @ctxt: the validation context
1807 * @doc: pointer to the document
1808 * @value: the value name
1809 * @attr: the attribute holding the ID
1810 *
1811 * Register a new id declaration
1812 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001813 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001814 */
1815xmlIDPtr
1816xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
1817 xmlAttrPtr attr) {
1818 xmlIDPtr ret;
1819 xmlIDTablePtr table;
1820
1821 if (doc == NULL) {
1822 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001823 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001824 return(NULL);
1825 }
1826 if (value == NULL) {
1827 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001828 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001829 return(NULL);
1830 }
1831 if (attr == NULL) {
1832 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001833 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001834 return(NULL);
1835 }
1836
1837 /*
1838 * Create the ID table if needed.
1839 */
1840 table = (xmlIDTablePtr) doc->ids;
1841 if (table == NULL)
1842 doc->ids = table = xmlCreateIDTable();
1843 if (table == NULL) {
1844 xmlGenericError(xmlGenericErrorContext,
1845 "xmlAddID: Table creation failed!\n");
1846 return(NULL);
1847 }
1848
1849 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
1850 if (ret == NULL) {
1851 xmlGenericError(xmlGenericErrorContext,
1852 "xmlAddID: out of memory\n");
1853 return(NULL);
1854 }
1855
1856 /*
1857 * fill the structure.
1858 */
1859 ret->value = xmlStrdup(value);
1860 ret->attr = attr;
1861
1862 if (xmlHashAddEntry(table, value, ret) < 0) {
1863 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001864 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001865 */
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00001866 if (ctxt != NULL)
1867 VERROR(ctxt->userData, "ID %s already defined\n", value);
Owen Taylor3473f882001-02-23 17:55:21 +00001868 xmlFreeID(ret);
1869 return(NULL);
1870 }
1871 return(ret);
1872}
1873
1874/**
1875 * xmlFreeIDTable:
1876 * @table: An id table
1877 *
1878 * Deallocate the memory used by an ID hash table.
1879 */
1880void
1881xmlFreeIDTable(xmlIDTablePtr table) {
1882 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
1883}
1884
1885/**
1886 * xmlIsID:
1887 * @doc: the document
1888 * @elem: the element carrying the attribute
1889 * @attr: the attribute
1890 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001891 * Determine whether an attribute is of type ID. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00001892 * then this is simple, otherwise we use an heuristic: name ID (upper
1893 * or lowercase).
1894 *
1895 * Returns 0 or 1 depending on the lookup result
1896 */
1897int
1898xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
1899 if (doc == NULL) return(0);
1900 if (attr == NULL) return(0);
1901 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
1902 return(0);
1903 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1904 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
1905 (xmlStrEqual(BAD_CAST "name", attr->name)))
1906 return(1);
1907 return(0);
1908 } else {
1909 xmlAttributePtr attrDecl;
1910
1911 if (elem == NULL) return(0);
1912 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
1913 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1914 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
1915 attr->name);
1916
1917 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
1918 return(1);
1919 }
1920 return(0);
1921}
1922
1923/**
1924 * xmlRemoveID
1925 * @doc: the document
1926 * @attr: the attribute
1927 *
1928 * Remove the given attribute from the ID table maintained internally.
1929 *
1930 * Returns -1 if the lookup failed and 0 otherwise
1931 */
1932int
1933xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
1934 xmlAttrPtr cur;
1935 xmlIDTablePtr table;
1936 xmlChar *ID;
1937
1938 if (doc == NULL) return(-1);
1939 if (attr == NULL) return(-1);
1940 table = (xmlIDTablePtr) doc->ids;
1941 if (table == NULL)
1942 return(-1);
1943
1944 if (attr == NULL)
1945 return(-1);
1946 ID = xmlNodeListGetString(doc, attr->children, 1);
1947 if (ID == NULL)
1948 return(-1);
1949 cur = xmlHashLookup(table, ID);
1950 if (cur != attr) {
1951 xmlFree(ID);
1952 return(-1);
1953 }
1954 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
1955 xmlFree(ID);
1956 return(0);
1957}
1958
1959/**
1960 * xmlGetID:
1961 * @doc: pointer to the document
1962 * @ID: the ID value
1963 *
1964 * Search the attribute declaring the given ID
1965 *
1966 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
1967 */
1968xmlAttrPtr
1969xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
1970 xmlIDTablePtr table;
1971 xmlIDPtr id;
1972
1973 if (doc == NULL) {
1974 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
1975 return(NULL);
1976 }
1977
1978 if (ID == NULL) {
1979 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
1980 return(NULL);
1981 }
1982
1983 table = (xmlIDTablePtr) doc->ids;
1984 if (table == NULL)
1985 return(NULL);
1986
1987 id = xmlHashLookup(table, ID);
1988 if (id == NULL)
1989 return(NULL);
1990 return(id->attr);
1991}
1992
1993/************************************************************************
1994 * *
1995 * Refs *
1996 * *
1997 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00001998typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00001999{
2000 xmlListPtr l;
2001 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00002002} xmlRemoveMemo;
2003
2004typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2005
2006typedef struct xmlValidateMemo_t
2007{
2008 xmlValidCtxtPtr ctxt;
2009 const xmlChar *name;
2010} xmlValidateMemo;
2011
2012typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002013
2014/**
2015 * xmlCreateRefTable:
2016 *
2017 * create and initialize an empty ref hash table.
2018 *
2019 * Returns the xmlRefTablePtr just created or NULL in case
2020 * of error.
2021 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002022static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002023xmlCreateRefTable(void) {
2024 return(xmlHashCreate(0));
2025}
2026
2027/**
2028 * xmlFreeRef:
2029 * @lk: A list link
2030 *
2031 * Deallocate the memory used by a ref definition
2032 */
2033static void
2034xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002035 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2036 if (ref == NULL) return;
2037 if (ref->value != NULL)
2038 xmlFree((xmlChar *)ref->value);
2039 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002040}
2041
2042/**
2043 * xmlFreeRefList:
2044 * @list_ref: A list of references.
2045 *
2046 * Deallocate the memory used by a list of references
2047 */
2048static void
2049xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002050 if (list_ref == NULL) return;
2051 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002052}
2053
2054/**
2055 * xmlWalkRemoveRef:
2056 * @data: Contents of current link
2057 * @user: Value supplied by the user
2058 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002059 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002060 */
2061static int
2062xmlWalkRemoveRef(const void *data, const void *user)
2063{
Daniel Veillard37721922001-05-04 15:21:12 +00002064 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2065 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2066 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002067
Daniel Veillard37721922001-05-04 15:21:12 +00002068 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2069 xmlListRemoveFirst(ref_list, (void *)data);
2070 return 0;
2071 }
2072 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002073}
2074
2075/**
2076 * xmlAddRef:
2077 * @ctxt: the validation context
2078 * @doc: pointer to the document
2079 * @value: the value name
2080 * @attr: the attribute holding the Ref
2081 *
2082 * Register a new ref declaration
2083 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002084 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002085 */
2086xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002087xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002088 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002089 xmlRefPtr ret;
2090 xmlRefTablePtr table;
2091 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002092
Daniel Veillard37721922001-05-04 15:21:12 +00002093 if (doc == NULL) {
2094 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002095 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002096 return(NULL);
2097 }
2098 if (value == NULL) {
2099 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002100 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002101 return(NULL);
2102 }
2103 if (attr == NULL) {
2104 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002105 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002106 return(NULL);
2107 }
Owen Taylor3473f882001-02-23 17:55:21 +00002108
Daniel Veillard37721922001-05-04 15:21:12 +00002109 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002110 * Create the Ref table if needed.
2111 */
Daniel Veillard37721922001-05-04 15:21:12 +00002112 table = (xmlRefTablePtr) doc->refs;
2113 if (table == NULL)
2114 doc->refs = table = xmlCreateRefTable();
2115 if (table == NULL) {
2116 xmlGenericError(xmlGenericErrorContext,
2117 "xmlAddRef: Table creation failed!\n");
2118 return(NULL);
2119 }
Owen Taylor3473f882001-02-23 17:55:21 +00002120
Daniel Veillard37721922001-05-04 15:21:12 +00002121 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2122 if (ret == NULL) {
2123 xmlGenericError(xmlGenericErrorContext,
2124 "xmlAddRef: out of memory\n");
2125 return(NULL);
2126 }
Owen Taylor3473f882001-02-23 17:55:21 +00002127
Daniel Veillard37721922001-05-04 15:21:12 +00002128 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002129 * fill the structure.
2130 */
Daniel Veillard37721922001-05-04 15:21:12 +00002131 ret->value = xmlStrdup(value);
2132 ret->attr = attr;
Owen Taylor3473f882001-02-23 17:55:21 +00002133
Daniel Veillard37721922001-05-04 15:21:12 +00002134 /* To add a reference :-
2135 * References are maintained as a list of references,
2136 * Lookup the entry, if no entry create new nodelist
2137 * Add the owning node to the NodeList
2138 * Return the ref
2139 */
Owen Taylor3473f882001-02-23 17:55:21 +00002140
Daniel Veillard37721922001-05-04 15:21:12 +00002141 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2142 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2143 xmlGenericError(xmlGenericErrorContext,
2144 "xmlAddRef: Reference list creation failed!\n");
2145 return(NULL);
2146 }
2147 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2148 xmlListDelete(ref_list);
2149 xmlGenericError(xmlGenericErrorContext,
2150 "xmlAddRef: Reference list insertion failed!\n");
2151 return(NULL);
2152 }
2153 }
2154 xmlListInsert(ref_list, ret);
2155 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002156}
2157
2158/**
2159 * xmlFreeRefTable:
2160 * @table: An ref table
2161 *
2162 * Deallocate the memory used by an Ref hash table.
2163 */
2164void
2165xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002166 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002167}
2168
2169/**
2170 * xmlIsRef:
2171 * @doc: the document
2172 * @elem: the element carrying the attribute
2173 * @attr: the attribute
2174 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002175 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002176 * then this is simple, otherwise we use an heuristic: name Ref (upper
2177 * or lowercase).
2178 *
2179 * Returns 0 or 1 depending on the lookup result
2180 */
2181int
2182xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002183 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2184 return(0);
2185 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2186 /* TODO @@@ */
2187 return(0);
2188 } else {
2189 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002190
Daniel Veillard37721922001-05-04 15:21:12 +00002191 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2192 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2193 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2194 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002195
Daniel Veillard37721922001-05-04 15:21:12 +00002196 if ((attrDecl != NULL) &&
2197 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2198 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2199 return(1);
2200 }
2201 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002202}
2203
2204/**
2205 * xmlRemoveRef
2206 * @doc: the document
2207 * @attr: the attribute
2208 *
2209 * Remove the given attribute from the Ref table maintained internally.
2210 *
2211 * Returns -1 if the lookup failed and 0 otherwise
2212 */
2213int
2214xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002215 xmlListPtr ref_list;
2216 xmlRefTablePtr table;
2217 xmlChar *ID;
2218 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002219
Daniel Veillard37721922001-05-04 15:21:12 +00002220 if (doc == NULL) return(-1);
2221 if (attr == NULL) return(-1);
2222 table = (xmlRefTablePtr) doc->refs;
2223 if (table == NULL)
2224 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002225
Daniel Veillard37721922001-05-04 15:21:12 +00002226 if (attr == NULL)
2227 return(-1);
2228 ID = xmlNodeListGetString(doc, attr->children, 1);
2229 if (ID == NULL)
2230 return(-1);
2231 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002232
Daniel Veillard37721922001-05-04 15:21:12 +00002233 if(ref_list == NULL) {
2234 xmlFree(ID);
2235 return (-1);
2236 }
2237 /* At this point, ref_list refers to a list of references which
2238 * have the same key as the supplied attr. Our list of references
2239 * is ordered by reference address and we don't have that information
2240 * here to use when removing. We'll have to walk the list and
2241 * check for a matching attribute, when we find one stop the walk
2242 * and remove the entry.
2243 * The list is ordered by reference, so that means we don't have the
2244 * key. Passing the list and the reference to the walker means we
2245 * will have enough data to be able to remove the entry.
2246 */
2247 target.l = ref_list;
2248 target.ap = attr;
2249
2250 /* Remove the supplied attr from our list */
2251 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002252
Daniel Veillard37721922001-05-04 15:21:12 +00002253 /*If the list is empty then remove the list entry in the hash */
2254 if (xmlListEmpty(ref_list))
2255 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2256 xmlFreeRefList);
2257 xmlFree(ID);
2258 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002259}
2260
2261/**
2262 * xmlGetRefs:
2263 * @doc: pointer to the document
2264 * @ID: the ID value
2265 *
2266 * Find the set of references for the supplied ID.
2267 *
2268 * Returns NULL if not found, otherwise node set for the ID.
2269 */
2270xmlListPtr
2271xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002272 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002273
Daniel Veillard37721922001-05-04 15:21:12 +00002274 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002275 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002276 return(NULL);
2277 }
Owen Taylor3473f882001-02-23 17:55:21 +00002278
Daniel Veillard37721922001-05-04 15:21:12 +00002279 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002280 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002281 return(NULL);
2282 }
Owen Taylor3473f882001-02-23 17:55:21 +00002283
Daniel Veillard37721922001-05-04 15:21:12 +00002284 table = (xmlRefTablePtr) doc->refs;
2285 if (table == NULL)
2286 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002287
Daniel Veillard37721922001-05-04 15:21:12 +00002288 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002289}
2290
2291/************************************************************************
2292 * *
2293 * Routines for validity checking *
2294 * *
2295 ************************************************************************/
2296
2297/**
2298 * xmlGetDtdElementDesc:
2299 * @dtd: a pointer to the DtD to search
2300 * @name: the element name
2301 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002302 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002303 *
2304 * returns the xmlElementPtr if found or NULL
2305 */
2306
2307xmlElementPtr
2308xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2309 xmlElementTablePtr table;
2310 xmlElementPtr cur;
2311 xmlChar *uqname = NULL, *prefix = NULL;
2312
2313 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002314 if (dtd->elements == NULL)
2315 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002316 table = (xmlElementTablePtr) dtd->elements;
2317
2318 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002319 if (uqname != NULL)
2320 name = uqname;
2321 cur = xmlHashLookup2(table, name, prefix);
2322 if (prefix != NULL) xmlFree(prefix);
2323 if (uqname != NULL) xmlFree(uqname);
2324 return(cur);
2325}
2326/**
2327 * xmlGetDtdElementDesc2:
2328 * @dtd: a pointer to the DtD to search
2329 * @name: the element name
2330 * @create: create an empty description if not found
2331 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002332 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002333 *
2334 * returns the xmlElementPtr if found or NULL
2335 */
2336
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002337static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002338xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2339 xmlElementTablePtr table;
2340 xmlElementPtr cur;
2341 xmlChar *uqname = NULL, *prefix = NULL;
2342
2343 if (dtd == NULL) return(NULL);
2344 if (dtd->elements == NULL) {
2345 if (!create)
2346 return(NULL);
2347 /*
2348 * Create the Element table if needed.
2349 */
2350 table = (xmlElementTablePtr) dtd->elements;
2351 if (table == NULL) {
2352 table = xmlCreateElementTable();
2353 dtd->elements = (void *) table;
2354 }
2355 if (table == NULL) {
2356 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002357 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002358 return(NULL);
2359 }
2360 }
2361 table = (xmlElementTablePtr) dtd->elements;
2362
2363 uqname = xmlSplitQName2(name, &prefix);
2364 if (uqname != NULL)
2365 name = uqname;
2366 cur = xmlHashLookup2(table, name, prefix);
2367 if ((cur == NULL) && (create)) {
2368 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2369 if (cur == NULL) {
2370 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002371 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002372 return(NULL);
2373 }
2374 memset(cur, 0, sizeof(xmlElement));
2375 cur->type = XML_ELEMENT_DECL;
2376
2377 /*
2378 * fill the structure.
2379 */
2380 cur->name = xmlStrdup(name);
2381 cur->prefix = xmlStrdup(prefix);
2382 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2383
2384 xmlHashAddEntry2(table, name, prefix, cur);
2385 }
2386 if (prefix != NULL) xmlFree(prefix);
2387 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002388 return(cur);
2389}
2390
2391/**
2392 * xmlGetDtdQElementDesc:
2393 * @dtd: a pointer to the DtD to search
2394 * @name: the element name
2395 * @prefix: the element namespace prefix
2396 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002397 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002398 *
2399 * returns the xmlElementPtr if found or NULL
2400 */
2401
Daniel Veillard48da9102001-08-07 01:10:10 +00002402xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002403xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2404 const xmlChar *prefix) {
2405 xmlElementTablePtr table;
2406
2407 if (dtd == NULL) return(NULL);
2408 if (dtd->elements == NULL) return(NULL);
2409 table = (xmlElementTablePtr) dtd->elements;
2410
2411 return(xmlHashLookup2(table, name, prefix));
2412}
2413
2414/**
2415 * xmlGetDtdAttrDesc:
2416 * @dtd: a pointer to the DtD to search
2417 * @elem: the element name
2418 * @name: the attribute name
2419 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002420 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002421 * this element.
2422 *
2423 * returns the xmlAttributePtr if found or NULL
2424 */
2425
2426xmlAttributePtr
2427xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2428 xmlAttributeTablePtr table;
2429 xmlAttributePtr cur;
2430 xmlChar *uqname = NULL, *prefix = NULL;
2431
2432 if (dtd == NULL) return(NULL);
2433 if (dtd->attributes == NULL) return(NULL);
2434
2435 table = (xmlAttributeTablePtr) dtd->attributes;
2436 if (table == NULL)
2437 return(NULL);
2438
2439 uqname = xmlSplitQName2(name, &prefix);
2440
2441 if (uqname != NULL) {
2442 cur = xmlHashLookup3(table, uqname, prefix, elem);
2443 if (prefix != NULL) xmlFree(prefix);
2444 if (uqname != NULL) xmlFree(uqname);
2445 } else
2446 cur = xmlHashLookup3(table, name, NULL, elem);
2447 return(cur);
2448}
2449
2450/**
2451 * xmlGetDtdQAttrDesc:
2452 * @dtd: a pointer to the DtD to search
2453 * @elem: the element name
2454 * @name: the attribute name
2455 * @prefix: the attribute namespace prefix
2456 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002457 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002458 * this element.
2459 *
2460 * returns the xmlAttributePtr if found or NULL
2461 */
2462
Daniel Veillard48da9102001-08-07 01:10:10 +00002463xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002464xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2465 const xmlChar *prefix) {
2466 xmlAttributeTablePtr table;
2467
2468 if (dtd == NULL) return(NULL);
2469 if (dtd->attributes == NULL) return(NULL);
2470 table = (xmlAttributeTablePtr) dtd->attributes;
2471
2472 return(xmlHashLookup3(table, name, prefix, elem));
2473}
2474
2475/**
2476 * xmlGetDtdNotationDesc:
2477 * @dtd: a pointer to the DtD to search
2478 * @name: the notation name
2479 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002480 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002481 *
2482 * returns the xmlNotationPtr if found or NULL
2483 */
2484
2485xmlNotationPtr
2486xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2487 xmlNotationTablePtr table;
2488
2489 if (dtd == NULL) return(NULL);
2490 if (dtd->notations == NULL) return(NULL);
2491 table = (xmlNotationTablePtr) dtd->notations;
2492
2493 return(xmlHashLookup(table, name));
2494}
2495
2496/**
2497 * xmlValidateNotationUse:
2498 * @ctxt: the validation context
2499 * @doc: the document
2500 * @notationName: the notation name to check
2501 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002502 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002503 * - [ VC: Notation Declared ]
2504 *
2505 * returns 1 if valid or 0 otherwise
2506 */
2507
2508int
2509xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2510 const xmlChar *notationName) {
2511 xmlNotationPtr notaDecl;
2512 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2513
2514 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2515 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2516 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2517
2518 if (notaDecl == NULL) {
2519 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2520 notationName);
2521 return(0);
2522 }
2523 return(1);
2524}
2525
2526/**
2527 * xmlIsMixedElement
2528 * @doc: the document
2529 * @name: the element name
2530 *
2531 * Search in the DtDs whether an element accept Mixed content (or ANY)
2532 * basically if it is supposed to accept text childs
2533 *
2534 * returns 0 if no, 1 if yes, and -1 if no element description is available
2535 */
2536
2537int
2538xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2539 xmlElementPtr elemDecl;
2540
2541 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2542
2543 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2544 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2545 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2546 if (elemDecl == NULL) return(-1);
2547 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002548 case XML_ELEMENT_TYPE_UNDEFINED:
2549 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002550 case XML_ELEMENT_TYPE_ELEMENT:
2551 return(0);
2552 case XML_ELEMENT_TYPE_EMPTY:
2553 /*
2554 * return 1 for EMPTY since we want VC error to pop up
2555 * on <empty> </empty> for example
2556 */
2557 case XML_ELEMENT_TYPE_ANY:
2558 case XML_ELEMENT_TYPE_MIXED:
2559 return(1);
2560 }
2561 return(1);
2562}
2563
2564/**
2565 * xmlValidateNameValue:
2566 * @value: an Name value
2567 *
2568 * Validate that the given value match Name production
2569 *
2570 * returns 1 if valid or 0 otherwise
2571 */
2572
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002573static int
Owen Taylor3473f882001-02-23 17:55:21 +00002574xmlValidateNameValue(const xmlChar *value) {
2575 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002576 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002577
2578 if (value == NULL) return(0);
2579 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002580 val = xmlStringCurrentChar(NULL, cur, &len);
2581 cur += len;
2582 if (!IS_LETTER(val) && (val != '_') &&
2583 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002584 return(0);
2585 }
2586
Daniel Veillardd8224e02002-01-13 15:43:22 +00002587 val = xmlStringCurrentChar(NULL, cur, &len);
2588 cur += len;
2589 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2590 (val == '.') || (val == '-') ||
2591 (val == '_') || (val == ':') ||
2592 (IS_COMBINING(val)) ||
2593 (IS_EXTENDER(val))) {
2594 val = xmlStringCurrentChar(NULL, cur, &len);
2595 cur += len;
2596 }
Owen Taylor3473f882001-02-23 17:55:21 +00002597
Daniel Veillardd8224e02002-01-13 15:43:22 +00002598 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002599
2600 return(1);
2601}
2602
2603/**
2604 * xmlValidateNamesValue:
2605 * @value: an Names value
2606 *
2607 * Validate that the given value match Names production
2608 *
2609 * returns 1 if valid or 0 otherwise
2610 */
2611
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002612static int
Owen Taylor3473f882001-02-23 17:55:21 +00002613xmlValidateNamesValue(const xmlChar *value) {
2614 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002615 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002616
2617 if (value == NULL) return(0);
2618 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002619 val = xmlStringCurrentChar(NULL, cur, &len);
2620 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002621
Daniel Veillardd8224e02002-01-13 15:43:22 +00002622 if (!IS_LETTER(val) && (val != '_') &&
2623 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002624 return(0);
2625 }
2626
Daniel Veillardd8224e02002-01-13 15:43:22 +00002627 val = xmlStringCurrentChar(NULL, cur, &len);
2628 cur += len;
2629 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2630 (val == '.') || (val == '-') ||
2631 (val == '_') || (val == ':') ||
2632 (IS_COMBINING(val)) ||
2633 (IS_EXTENDER(val))) {
2634 val = xmlStringCurrentChar(NULL, cur, &len);
2635 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002636 }
2637
Daniel Veillardd8224e02002-01-13 15:43:22 +00002638 while (IS_BLANK(val)) {
2639 while (IS_BLANK(val)) {
2640 val = xmlStringCurrentChar(NULL, cur, &len);
2641 cur += len;
2642 }
2643
2644 if (!IS_LETTER(val) && (val != '_') &&
2645 (val != ':')) {
2646 return(0);
2647 }
2648 val = xmlStringCurrentChar(NULL, cur, &len);
2649 cur += len;
2650
2651 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2652 (val == '.') || (val == '-') ||
2653 (val == '_') || (val == ':') ||
2654 (IS_COMBINING(val)) ||
2655 (IS_EXTENDER(val))) {
2656 val = xmlStringCurrentChar(NULL, cur, &len);
2657 cur += len;
2658 }
2659 }
2660
2661 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002662
2663 return(1);
2664}
2665
2666/**
2667 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002668 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00002669 *
2670 * Validate that the given value match Nmtoken production
2671 *
2672 * [ VC: Name Token ]
2673 *
2674 * returns 1 if valid or 0 otherwise
2675 */
2676
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002677static int
Owen Taylor3473f882001-02-23 17:55:21 +00002678xmlValidateNmtokenValue(const xmlChar *value) {
2679 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002680 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002681
2682 if (value == NULL) return(0);
2683 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002684 val = xmlStringCurrentChar(NULL, cur, &len);
2685 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002686
Daniel Veillardd8224e02002-01-13 15:43:22 +00002687 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2688 (val != '.') && (val != '-') &&
2689 (val != '_') && (val != ':') &&
2690 (!IS_COMBINING(val)) &&
2691 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00002692 return(0);
2693
Daniel Veillardd8224e02002-01-13 15:43:22 +00002694 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2695 (val == '.') || (val == '-') ||
2696 (val == '_') || (val == ':') ||
2697 (IS_COMBINING(val)) ||
2698 (IS_EXTENDER(val))) {
2699 val = xmlStringCurrentChar(NULL, cur, &len);
2700 cur += len;
2701 }
Owen Taylor3473f882001-02-23 17:55:21 +00002702
Daniel Veillardd8224e02002-01-13 15:43:22 +00002703 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002704
2705 return(1);
2706}
2707
2708/**
2709 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002710 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00002711 *
2712 * Validate that the given value match Nmtokens production
2713 *
2714 * [ VC: Name Token ]
2715 *
2716 * returns 1 if valid or 0 otherwise
2717 */
2718
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002719static int
Owen Taylor3473f882001-02-23 17:55:21 +00002720xmlValidateNmtokensValue(const xmlChar *value) {
2721 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002722 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002723
2724 if (value == NULL) return(0);
2725 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002726 val = xmlStringCurrentChar(NULL, cur, &len);
2727 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002728
Daniel Veillardd8224e02002-01-13 15:43:22 +00002729 while (IS_BLANK(val)) {
2730 val = xmlStringCurrentChar(NULL, cur, &len);
2731 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002732 }
2733
Daniel Veillardd8224e02002-01-13 15:43:22 +00002734 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2735 (val != '.') && (val != '-') &&
2736 (val != '_') && (val != ':') &&
2737 (!IS_COMBINING(val)) &&
2738 (!IS_EXTENDER(val)))
2739 return(0);
2740
2741 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2742 (val == '.') || (val == '-') ||
2743 (val == '_') || (val == ':') ||
2744 (IS_COMBINING(val)) ||
2745 (IS_EXTENDER(val))) {
2746 val = xmlStringCurrentChar(NULL, cur, &len);
2747 cur += len;
2748 }
2749
2750 while (IS_BLANK(val)) {
2751 while (IS_BLANK(val)) {
2752 val = xmlStringCurrentChar(NULL, cur, &len);
2753 cur += len;
2754 }
2755 if (val == 0) return(1);
2756
2757 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2758 (val != '.') && (val != '-') &&
2759 (val != '_') && (val != ':') &&
2760 (!IS_COMBINING(val)) &&
2761 (!IS_EXTENDER(val)))
2762 return(0);
2763
2764 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2765 (val == '.') || (val == '-') ||
2766 (val == '_') || (val == ':') ||
2767 (IS_COMBINING(val)) ||
2768 (IS_EXTENDER(val))) {
2769 val = xmlStringCurrentChar(NULL, cur, &len);
2770 cur += len;
2771 }
2772 }
2773
2774 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002775
2776 return(1);
2777}
2778
2779/**
2780 * xmlValidateNotationDecl:
2781 * @ctxt: the validation context
2782 * @doc: a document instance
2783 * @nota: a notation definition
2784 *
2785 * Try to validate a single notation definition
2786 * basically it does the following checks as described by the
2787 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002788 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00002789 * But this function get called anyway ...
2790 *
2791 * returns 1 if valid or 0 otherwise
2792 */
2793
2794int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002795xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
2796 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002797 int ret = 1;
2798
2799 return(ret);
2800}
2801
2802/**
2803 * xmlValidateAttributeValue:
2804 * @type: an attribute type
2805 * @value: an attribute value
2806 *
2807 * Validate that the given attribute value match the proper production
2808 *
2809 * [ VC: ID ]
2810 * Values of type ID must match the Name production....
2811 *
2812 * [ VC: IDREF ]
2813 * Values of type IDREF must match the Name production, and values
2814 * of type IDREFS must match Names ...
2815 *
2816 * [ VC: Entity Name ]
2817 * Values of type ENTITY must match the Name production, values
2818 * of type ENTITIES must match Names ...
2819 *
2820 * [ VC: Name Token ]
2821 * Values of type NMTOKEN must match the Nmtoken production; values
2822 * of type NMTOKENS must match Nmtokens.
2823 *
2824 * returns 1 if valid or 0 otherwise
2825 */
2826
2827int
2828xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
2829 switch (type) {
2830 case XML_ATTRIBUTE_ENTITIES:
2831 case XML_ATTRIBUTE_IDREFS:
2832 return(xmlValidateNamesValue(value));
2833 case XML_ATTRIBUTE_ENTITY:
2834 case XML_ATTRIBUTE_IDREF:
2835 case XML_ATTRIBUTE_ID:
2836 case XML_ATTRIBUTE_NOTATION:
2837 return(xmlValidateNameValue(value));
2838 case XML_ATTRIBUTE_NMTOKENS:
2839 case XML_ATTRIBUTE_ENUMERATION:
2840 return(xmlValidateNmtokensValue(value));
2841 case XML_ATTRIBUTE_NMTOKEN:
2842 return(xmlValidateNmtokenValue(value));
2843 case XML_ATTRIBUTE_CDATA:
2844 break;
2845 }
2846 return(1);
2847}
2848
2849/**
2850 * xmlValidateAttributeValue2:
2851 * @ctxt: the validation context
2852 * @doc: the document
2853 * @name: the attribute name (used for error reporting only)
2854 * @type: the attribute type
2855 * @value: the attribute value
2856 *
2857 * Validate that the given attribute value match a given type.
2858 * This typically cannot be done before having finished parsing
2859 * the subsets.
2860 *
2861 * [ VC: IDREF ]
2862 * Values of type IDREF must match one of the declared IDs
2863 * Values of type IDREFS must match a sequence of the declared IDs
2864 * each Name must match the value of an ID attribute on some element
2865 * in the XML document; i.e. IDREF values must match the value of
2866 * some ID attribute
2867 *
2868 * [ VC: Entity Name ]
2869 * Values of type ENTITY must match one declared entity
2870 * Values of type ENTITIES must match a sequence of declared entities
2871 *
2872 * [ VC: Notation Attributes ]
2873 * all notation names in the declaration must be declared.
2874 *
2875 * returns 1 if valid or 0 otherwise
2876 */
2877
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002878static int
Owen Taylor3473f882001-02-23 17:55:21 +00002879xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2880 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
2881 int ret = 1;
2882 switch (type) {
2883 case XML_ATTRIBUTE_IDREFS:
2884 case XML_ATTRIBUTE_IDREF:
2885 case XML_ATTRIBUTE_ID:
2886 case XML_ATTRIBUTE_NMTOKENS:
2887 case XML_ATTRIBUTE_ENUMERATION:
2888 case XML_ATTRIBUTE_NMTOKEN:
2889 case XML_ATTRIBUTE_CDATA:
2890 break;
2891 case XML_ATTRIBUTE_ENTITY: {
2892 xmlEntityPtr ent;
2893
2894 ent = xmlGetDocEntity(doc, value);
Daniel Veillard878eab02002-02-19 13:46:09 +00002895 if ((ent == NULL) && (doc->standalone == 1)) {
2896 doc->standalone = 0;
2897 ent = xmlGetDocEntity(doc, value);
2898 if (ent != NULL) {
2899 VERROR(ctxt->userData,
2900"standalone problem: attribute %s reference entity \"%s\" in external subset\n",
2901 name, value);
2902 /* WAIT to get answer from the Core WG on this
2903 ret = 0;
2904 */
2905 }
2906 }
Owen Taylor3473f882001-02-23 17:55:21 +00002907 if (ent == NULL) {
2908 VERROR(ctxt->userData,
2909 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2910 name, value);
2911 ret = 0;
2912 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2913 VERROR(ctxt->userData,
2914 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2915 name, value);
2916 ret = 0;
2917 }
2918 break;
2919 }
2920 case XML_ATTRIBUTE_ENTITIES: {
2921 xmlChar *dup, *nam = NULL, *cur, save;
2922 xmlEntityPtr ent;
2923
2924 dup = xmlStrdup(value);
2925 if (dup == NULL)
2926 return(0);
2927 cur = dup;
2928 while (*cur != 0) {
2929 nam = cur;
2930 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2931 save = *cur;
2932 *cur = 0;
2933 ent = xmlGetDocEntity(doc, nam);
2934 if (ent == NULL) {
2935 VERROR(ctxt->userData,
2936 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
2937 name, nam);
2938 ret = 0;
2939 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2940 VERROR(ctxt->userData,
2941 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
2942 name, nam);
2943 ret = 0;
2944 }
2945 if (save == 0)
2946 break;
2947 *cur = save;
2948 while (IS_BLANK(*cur)) cur++;
2949 }
2950 xmlFree(dup);
2951 break;
2952 }
2953 case XML_ATTRIBUTE_NOTATION: {
2954 xmlNotationPtr nota;
2955
2956 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
2957 if ((nota == NULL) && (doc->extSubset != NULL))
2958 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
2959
2960 if (nota == NULL) {
2961 VERROR(ctxt->userData,
2962 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
2963 name, value);
2964 ret = 0;
2965 }
2966 break;
2967 }
2968 }
2969 return(ret);
2970}
2971
2972/**
2973 * xmlValidNormalizeAttributeValue:
2974 * @doc: the document
2975 * @elem: the parent
2976 * @name: the attribute name
2977 * @value: the attribute value
2978 *
2979 * Does the validation related extra step of the normalization of attribute
2980 * values:
2981 *
2982 * If the declared value is not CDATA, then the XML processor must further
2983 * process the normalized attribute value by discarding any leading and
2984 * trailing space (#x20) characters, and by replacing sequences of space
2985 * (#x20) characters by single space (#x20) character.
2986 *
2987 * returns a new normalized string if normalization is needed, NULL otherwise
2988 * the caller must free the returned value.
2989 */
2990
2991xmlChar *
2992xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
2993 const xmlChar *name, const xmlChar *value) {
2994 xmlChar *ret, *dst;
2995 const xmlChar *src;
2996 xmlAttributePtr attrDecl = NULL;
2997
2998 if (doc == NULL) return(NULL);
2999 if (elem == NULL) return(NULL);
3000 if (name == NULL) return(NULL);
3001 if (value == NULL) return(NULL);
3002
3003 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3004 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003005 snprintf((char *) qname, sizeof(qname), "%s:%s",
3006 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003007 qname[sizeof(qname) - 1] = 0;
3008 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
3009 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3010 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
3011 }
3012 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3013 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3014 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3015
3016 if (attrDecl == NULL)
3017 return(NULL);
3018 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3019 return(NULL);
3020
3021 ret = xmlStrdup(value);
3022 if (ret == NULL)
3023 return(NULL);
3024 src = value;
3025 dst = ret;
3026 while (*src == 0x20) src++;
3027 while (*src != 0) {
3028 if (*src == 0x20) {
3029 while (*src == 0x20) src++;
3030 if (*src != 0)
3031 *dst++ = 0x20;
3032 } else {
3033 *dst++ = *src++;
3034 }
3035 }
3036 *dst = 0;
3037 return(ret);
3038}
3039
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003040static void
Owen Taylor3473f882001-02-23 17:55:21 +00003041xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003042 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003043 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3044}
3045
3046/**
3047 * xmlValidateAttributeDecl:
3048 * @ctxt: the validation context
3049 * @doc: a document instance
3050 * @attr: an attribute definition
3051 *
3052 * Try to validate a single attribute definition
3053 * basically it does the following checks as described by the
3054 * XML-1.0 recommendation:
3055 * - [ VC: Attribute Default Legal ]
3056 * - [ VC: Enumeration ]
3057 * - [ VC: ID Attribute Default ]
3058 *
3059 * The ID/IDREF uniqueness and matching are done separately
3060 *
3061 * returns 1 if valid or 0 otherwise
3062 */
3063
3064int
3065xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3066 xmlAttributePtr attr) {
3067 int ret = 1;
3068 int val;
3069 CHECK_DTD;
3070 if(attr == NULL) return(1);
3071
3072 /* Attribute Default Legal */
3073 /* Enumeration */
3074 if (attr->defaultValue != NULL) {
3075 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3076 if (val == 0) {
3077 VERROR(ctxt->userData,
3078 "Syntax of default value for attribute %s on %s is not valid\n",
3079 attr->name, attr->elem);
3080 }
3081 ret &= val;
3082 }
3083
3084 /* ID Attribute Default */
3085 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3086 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3087 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3088 VERROR(ctxt->userData,
3089 "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n",
3090 attr->name, attr->elem);
3091 ret = 0;
3092 }
3093
3094 /* One ID per Element Type */
3095 if (attr->atype == XML_ATTRIBUTE_ID) {
3096 int nbId;
3097
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003098 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003099 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3100 attr->elem);
3101 if (elem != NULL) {
3102 nbId = xmlScanIDAttributeDecl(NULL, elem);
3103 } else {
3104 xmlAttributeTablePtr table;
3105
3106 /*
3107 * The attribute may be declared in the internal subset and the
3108 * element in the external subset.
3109 */
3110 nbId = 0;
3111 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3112 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3113 xmlValidateAttributeIdCallback, &nbId);
3114 }
3115 if (nbId > 1) {
3116 VERROR(ctxt->userData,
3117 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3118 attr->elem, nbId, attr->name);
3119 } else if (doc->extSubset != NULL) {
3120 int extId = 0;
3121 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3122 if (elem != NULL) {
3123 extId = xmlScanIDAttributeDecl(NULL, elem);
3124 }
3125 if (extId > 1) {
3126 VERROR(ctxt->userData,
3127 "Element %s has %d ID attribute defined in the external subset : %s\n",
3128 attr->elem, extId, attr->name);
3129 } else if (extId + nbId > 1) {
3130 VERROR(ctxt->userData,
3131"Element %s has ID attributes defined in the internal and external subset : %s\n",
3132 attr->elem, attr->name);
3133 }
3134 }
3135 }
3136
3137 /* Validity Constraint: Enumeration */
3138 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3139 xmlEnumerationPtr tree = attr->tree;
3140 while (tree != NULL) {
3141 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3142 tree = tree->next;
3143 }
3144 if (tree == NULL) {
3145 VERROR(ctxt->userData,
3146"Default value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3147 attr->defaultValue, attr->name, attr->elem);
3148 ret = 0;
3149 }
3150 }
3151
3152 return(ret);
3153}
3154
3155/**
3156 * xmlValidateElementDecl:
3157 * @ctxt: the validation context
3158 * @doc: a document instance
3159 * @elem: an element definition
3160 *
3161 * Try to validate a single element definition
3162 * basically it does the following checks as described by the
3163 * XML-1.0 recommendation:
3164 * - [ VC: One ID per Element Type ]
3165 * - [ VC: No Duplicate Types ]
3166 * - [ VC: Unique Element Type Declaration ]
3167 *
3168 * returns 1 if valid or 0 otherwise
3169 */
3170
3171int
3172xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3173 xmlElementPtr elem) {
3174 int ret = 1;
3175 xmlElementPtr tst;
3176
3177 CHECK_DTD;
3178
3179 if (elem == NULL) return(1);
3180
3181 /* No Duplicate Types */
3182 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3183 xmlElementContentPtr cur, next;
3184 const xmlChar *name;
3185
3186 cur = elem->content;
3187 while (cur != NULL) {
3188 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3189 if (cur->c1 == NULL) break;
3190 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3191 name = cur->c1->name;
3192 next = cur->c2;
3193 while (next != NULL) {
3194 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3195 if (xmlStrEqual(next->name, name)) {
3196 VERROR(ctxt->userData,
3197 "Definition of %s has duplicate references of %s\n",
3198 elem->name, name);
3199 ret = 0;
3200 }
3201 break;
3202 }
3203 if (next->c1 == NULL) break;
3204 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3205 if (xmlStrEqual(next->c1->name, name)) {
3206 VERROR(ctxt->userData,
3207 "Definition of %s has duplicate references of %s\n",
3208 elem->name, name);
3209 ret = 0;
3210 }
3211 next = next->c2;
3212 }
3213 }
3214 cur = cur->c2;
3215 }
3216 }
3217
3218 /* VC: Unique Element Type Declaration */
3219 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003220 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003221 ((tst->prefix == elem->prefix) ||
3222 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003223 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003224 VERROR(ctxt->userData, "Redefinition of element %s\n",
3225 elem->name);
3226 ret = 0;
3227 }
3228 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003229 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003230 ((tst->prefix == elem->prefix) ||
3231 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003232 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003233 VERROR(ctxt->userData, "Redefinition of element %s\n",
3234 elem->name);
3235 ret = 0;
3236 }
3237
Daniel Veillarda10efa82001-04-18 13:09:01 +00003238 /* One ID per Element Type
3239 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003240 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3241 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003242 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003243 return(ret);
3244}
3245
3246/**
3247 * xmlValidateOneAttribute:
3248 * @ctxt: the validation context
3249 * @doc: a document instance
3250 * @elem: an element instance
3251 * @attr: an attribute instance
3252 * @value: the attribute value (without entities processing)
3253 *
3254 * Try to validate a single attribute for an element
3255 * basically it does the following checks as described by the
3256 * XML-1.0 recommendation:
3257 * - [ VC: Attribute Value Type ]
3258 * - [ VC: Fixed Attribute Default ]
3259 * - [ VC: Entity Name ]
3260 * - [ VC: Name Token ]
3261 * - [ VC: ID ]
3262 * - [ VC: IDREF ]
3263 * - [ VC: Entity Name ]
3264 * - [ VC: Notation Attributes ]
3265 *
3266 * The ID/IDREF uniqueness and matching are done separately
3267 *
3268 * returns 1 if valid or 0 otherwise
3269 */
3270
3271int
3272xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3273 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3274 /* xmlElementPtr elemDecl; */
3275 xmlAttributePtr attrDecl = NULL;
3276 int val;
3277 int ret = 1;
3278
3279 CHECK_DTD;
3280 if ((elem == NULL) || (elem->name == NULL)) return(0);
3281 if ((attr == NULL) || (attr->name == NULL)) return(0);
3282
3283 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3284 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003285 snprintf((char *) qname, sizeof(qname), "%s:%s",
3286 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003287 qname[sizeof(qname) - 1] = 0;
3288 if (attr->ns != NULL) {
3289 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3290 attr->name, attr->ns->prefix);
3291 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3292 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3293 attr->name, attr->ns->prefix);
3294 } else {
3295 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3296 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3297 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3298 qname, attr->name);
3299 }
3300 }
3301 if (attrDecl == NULL) {
3302 if (attr->ns != NULL) {
3303 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3304 attr->name, attr->ns->prefix);
3305 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3306 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3307 attr->name, attr->ns->prefix);
3308 } else {
3309 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3310 elem->name, attr->name);
3311 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3312 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3313 elem->name, attr->name);
3314 }
3315 }
3316
3317
3318 /* Validity Constraint: Attribute Value Type */
3319 if (attrDecl == NULL) {
3320 VERROR(ctxt->userData,
3321 "No declaration for attribute %s on element %s\n",
3322 attr->name, elem->name);
3323 return(0);
3324 }
3325 attr->atype = attrDecl->atype;
3326
3327 val = xmlValidateAttributeValue(attrDecl->atype, value);
3328 if (val == 0) {
3329 VERROR(ctxt->userData,
3330 "Syntax of value for attribute %s on %s is not valid\n",
3331 attr->name, elem->name);
3332 ret = 0;
3333 }
3334
3335 /* Validity constraint: Fixed Attribute Default */
3336 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3337 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3338 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003339 "Value for attribute %s on %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003340 attr->name, elem->name, attrDecl->defaultValue);
3341 ret = 0;
3342 }
3343 }
3344
3345 /* Validity Constraint: ID uniqueness */
3346 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3347 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3348 ret = 0;
3349 }
3350
3351 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3352 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3353 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3354 ret = 0;
3355 }
3356
3357 /* Validity Constraint: Notation Attributes */
3358 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3359 xmlEnumerationPtr tree = attrDecl->tree;
3360 xmlNotationPtr nota;
3361
3362 /* First check that the given NOTATION was declared */
3363 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3364 if (nota == NULL)
3365 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3366
3367 if (nota == NULL) {
3368 VERROR(ctxt->userData,
3369 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3370 value, attr->name, elem->name);
3371 ret = 0;
3372 }
3373
3374 /* Second, verify that it's among the list */
3375 while (tree != NULL) {
3376 if (xmlStrEqual(tree->name, value)) break;
3377 tree = tree->next;
3378 }
3379 if (tree == NULL) {
3380 VERROR(ctxt->userData,
3381"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3382 value, attr->name, elem->name);
3383 ret = 0;
3384 }
3385 }
3386
3387 /* Validity Constraint: Enumeration */
3388 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3389 xmlEnumerationPtr tree = attrDecl->tree;
3390 while (tree != NULL) {
3391 if (xmlStrEqual(tree->name, value)) break;
3392 tree = tree->next;
3393 }
3394 if (tree == NULL) {
3395 VERROR(ctxt->userData,
3396 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3397 value, attr->name, elem->name);
3398 ret = 0;
3399 }
3400 }
3401
3402 /* Fixed Attribute Default */
3403 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3404 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3405 VERROR(ctxt->userData,
3406 "Value for attribute %s on %s must be \"%s\"\n",
3407 attr->name, elem->name, attrDecl->defaultValue);
3408 ret = 0;
3409 }
3410
3411 /* Extra check for the attribute value */
3412 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3413 attrDecl->atype, value);
3414
3415 return(ret);
3416}
3417
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003418/**
3419 * xmlValidateSkipIgnorable:
3420 * @ctxt: the validation context
3421 * @child: the child list
3422 *
3423 * Skip ignorable elements w.r.t. the validation process
3424 *
3425 * returns the first element to consider for validation of the content model
3426 */
3427
3428static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003429xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003430 while (child != NULL) {
3431 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003432 /* These things are ignored (skipped) during validation. */
3433 case XML_PI_NODE:
3434 case XML_COMMENT_NODE:
3435 case XML_XINCLUDE_START:
3436 case XML_XINCLUDE_END:
3437 child = child->next;
3438 break;
3439 case XML_TEXT_NODE:
3440 if (xmlIsBlankNode(child))
3441 child = child->next;
3442 else
3443 return(child);
3444 break;
3445 /* keep current node */
3446 default:
3447 return(child);
3448 }
3449 }
3450 return(child);
3451}
3452
3453/**
3454 * xmlValidateElementType:
3455 * @ctxt: the validation context
3456 *
3457 * Try to validate the content model of an element internal function
3458 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003459 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3460 * reference is found and -3 if the validation succeeded but
3461 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003462 */
3463
3464static int
3465xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003466 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003467 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003468
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003469 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003470 if ((NODE == NULL) && (CONT == NULL))
3471 return(1);
3472 if ((NODE == NULL) &&
3473 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3474 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3475 return(1);
3476 }
3477 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003478 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003479 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003480
3481 /*
3482 * We arrive here when more states need to be examined
3483 */
3484cont:
3485
3486 /*
3487 * We just recovered from a rollback generated by a possible
3488 * epsilon transition, go directly to the analysis phase
3489 */
3490 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003491 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003492 DEBUG_VALID_STATE(NODE, CONT)
3493 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003494 goto analyze;
3495 }
3496
3497 DEBUG_VALID_STATE(NODE, CONT)
3498 /*
3499 * we may have to save a backup state here. This is the equivalent
3500 * of handling epsilon transition in NFAs.
3501 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003502 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003503 ((CONT->parent == NULL) ||
3504 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003505 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003506 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00003507 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003508 DEBUG_VALID_MSG("saving parent branch");
3509 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3510 }
3511
3512
3513 /*
3514 * Check first if the content matches
3515 */
3516 switch (CONT->type) {
3517 case XML_ELEMENT_CONTENT_PCDATA:
3518 if (NODE == NULL) {
3519 DEBUG_VALID_MSG("pcdata failed no node");
3520 ret = 0;
3521 break;
3522 }
3523 if (NODE->type == XML_TEXT_NODE) {
3524 DEBUG_VALID_MSG("pcdata found, skip to next");
3525 /*
3526 * go to next element in the content model
3527 * skipping ignorable elems
3528 */
3529 do {
3530 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003531 NODE = xmlValidateSkipIgnorable(NODE);
3532 if ((NODE != NULL) &&
3533 (NODE->type == XML_ENTITY_REF_NODE))
3534 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003535 } while ((NODE != NULL) &&
3536 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003537 (NODE->type != XML_TEXT_NODE) &&
3538 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003539 ret = 1;
3540 break;
3541 } else {
3542 DEBUG_VALID_MSG("pcdata failed");
3543 ret = 0;
3544 break;
3545 }
3546 break;
3547 case XML_ELEMENT_CONTENT_ELEMENT:
3548 if (NODE == NULL) {
3549 DEBUG_VALID_MSG("element failed no node");
3550 ret = 0;
3551 break;
3552 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003553 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3554 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003555 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003556 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3557 ret = (CONT->prefix == NULL);
3558 } else if (CONT->prefix == NULL) {
3559 ret = 0;
3560 } else {
3561 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3562 }
3563 }
3564 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003565 DEBUG_VALID_MSG("element found, skip to next");
3566 /*
3567 * go to next element in the content model
3568 * skipping ignorable elems
3569 */
3570 do {
3571 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003572 NODE = xmlValidateSkipIgnorable(NODE);
3573 if ((NODE != NULL) &&
3574 (NODE->type == XML_ENTITY_REF_NODE))
3575 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003576 } while ((NODE != NULL) &&
3577 ((NODE->type != XML_ELEMENT_NODE) &&
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003578 (NODE->type != XML_TEXT_NODE) &&
3579 (NODE->type != XML_CDATA_SECTION_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003580 } else {
3581 DEBUG_VALID_MSG("element failed");
3582 ret = 0;
3583 break;
3584 }
3585 break;
3586 case XML_ELEMENT_CONTENT_OR:
3587 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003588 * Small optimization.
3589 */
3590 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3591 if ((NODE == NULL) ||
3592 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3593 DEPTH++;
3594 CONT = CONT->c2;
3595 goto cont;
3596 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003597 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3598 ret = (CONT->c1->prefix == NULL);
3599 } else if (CONT->c1->prefix == NULL) {
3600 ret = 0;
3601 } else {
3602 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3603 }
3604 if (ret == 0) {
3605 DEPTH++;
3606 CONT = CONT->c2;
3607 goto cont;
3608 }
Daniel Veillard85349052001-04-20 13:48:21 +00003609 }
3610
3611 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003612 * save the second branch 'or' branch
3613 */
3614 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003615 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3616 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003617
3618 DEPTH++;
3619 CONT = CONT->c1;
3620 goto cont;
3621 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003622 /*
3623 * Small optimization.
3624 */
3625 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3626 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3627 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3628 if ((NODE == NULL) ||
3629 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3630 DEPTH++;
3631 CONT = CONT->c2;
3632 goto cont;
3633 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003634 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3635 ret = (CONT->c1->prefix == NULL);
3636 } else if (CONT->c1->prefix == NULL) {
3637 ret = 0;
3638 } else {
3639 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3640 }
3641 if (ret == 0) {
3642 DEPTH++;
3643 CONT = CONT->c2;
3644 goto cont;
3645 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003646 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003647 DEPTH++;
3648 CONT = CONT->c1;
3649 goto cont;
3650 }
3651
3652 /*
3653 * At this point handle going up in the tree
3654 */
3655 if (ret == -1) {
3656 DEBUG_VALID_MSG("error found returning");
3657 return(ret);
3658 }
3659analyze:
3660 while (CONT != NULL) {
3661 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003662 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003663 * this level.
3664 */
3665 if (ret == 0) {
3666 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003667 xmlNodePtr cur;
3668
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003669 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003670 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003671 DEBUG_VALID_MSG("Once branch failed, rollback");
3672 if (vstateVPop(ctxt) < 0 ) {
3673 DEBUG_VALID_MSG("exhaustion, failed");
3674 return(0);
3675 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003676 if (cur != ctxt->vstate->node)
3677 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003678 goto cont;
3679 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00003680 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003681 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003682 DEBUG_VALID_MSG("Plus branch failed, rollback");
3683 if (vstateVPop(ctxt) < 0 ) {
3684 DEBUG_VALID_MSG("exhaustion, failed");
3685 return(0);
3686 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003687 if (cur != ctxt->vstate->node)
3688 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003689 goto cont;
3690 }
3691 DEBUG_VALID_MSG("Plus branch found");
3692 ret = 1;
3693 break;
3694 case XML_ELEMENT_CONTENT_MULT:
3695#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00003696 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003697 DEBUG_VALID_MSG("Mult branch failed");
3698 } else {
3699 DEBUG_VALID_MSG("Mult branch found");
3700 }
3701#endif
3702 ret = 1;
3703 break;
3704 case XML_ELEMENT_CONTENT_OPT:
3705 DEBUG_VALID_MSG("Option branch failed");
3706 ret = 1;
3707 break;
3708 }
3709 } else {
3710 switch (CONT->ocur) {
3711 case XML_ELEMENT_CONTENT_OPT:
3712 DEBUG_VALID_MSG("Option branch succeeded");
3713 ret = 1;
3714 break;
3715 case XML_ELEMENT_CONTENT_ONCE:
3716 DEBUG_VALID_MSG("Once branch succeeded");
3717 ret = 1;
3718 break;
3719 case XML_ELEMENT_CONTENT_PLUS:
3720 if (STATE == ROLLBACK_PARENT) {
3721 DEBUG_VALID_MSG("Plus branch rollback");
3722 ret = 1;
3723 break;
3724 }
3725 if (NODE == NULL) {
3726 DEBUG_VALID_MSG("Plus branch exhausted");
3727 ret = 1;
3728 break;
3729 }
3730 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003731 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003732 goto cont;
3733 case XML_ELEMENT_CONTENT_MULT:
3734 if (STATE == ROLLBACK_PARENT) {
3735 DEBUG_VALID_MSG("Mult branch rollback");
3736 ret = 1;
3737 break;
3738 }
3739 if (NODE == NULL) {
3740 DEBUG_VALID_MSG("Mult branch exhausted");
3741 ret = 1;
3742 break;
3743 }
3744 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003745 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003746 goto cont;
3747 }
3748 }
3749 STATE = 0;
3750
3751 /*
3752 * Then act accordingly at the parent level
3753 */
Daniel Veillard5344c602001-12-31 16:37:34 +00003754 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003755 if (CONT->parent == NULL)
3756 break;
3757
3758 switch (CONT->parent->type) {
3759 case XML_ELEMENT_CONTENT_PCDATA:
3760 DEBUG_VALID_MSG("Error: parent pcdata");
3761 return(-1);
3762 case XML_ELEMENT_CONTENT_ELEMENT:
3763 DEBUG_VALID_MSG("Error: parent element");
3764 return(-1);
3765 case XML_ELEMENT_CONTENT_OR:
3766 if (ret == 1) {
3767 DEBUG_VALID_MSG("Or succeeded");
3768 CONT = CONT->parent;
3769 DEPTH--;
3770 } else {
3771 DEBUG_VALID_MSG("Or failed");
3772 CONT = CONT->parent;
3773 DEPTH--;
3774 }
3775 break;
3776 case XML_ELEMENT_CONTENT_SEQ:
3777 if (ret == 0) {
3778 DEBUG_VALID_MSG("Sequence failed");
3779 CONT = CONT->parent;
3780 DEPTH--;
3781 } else if (CONT == CONT->parent->c1) {
3782 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3783 CONT = CONT->parent->c2;
3784 goto cont;
3785 } else {
3786 DEBUG_VALID_MSG("Sequence succeeded");
3787 CONT = CONT->parent;
3788 DEPTH--;
3789 }
3790 }
3791 }
3792 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003793 xmlNodePtr cur;
3794
3795 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003796 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3797 if (vstateVPop(ctxt) < 0 ) {
3798 DEBUG_VALID_MSG("exhaustion, failed");
3799 return(0);
3800 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003801 if (cur != ctxt->vstate->node)
3802 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003803 goto cont;
3804 }
3805 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003806 xmlNodePtr cur;
3807
3808 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003809 DEBUG_VALID_MSG("Failure, rollback");
3810 if (vstateVPop(ctxt) < 0 ) {
3811 DEBUG_VALID_MSG("exhaustion, failed");
3812 return(0);
3813 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003814 if (cur != ctxt->vstate->node)
3815 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003816 goto cont;
3817 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003818 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003819}
3820
3821/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003822 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003823 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003824 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003825 * @content: An element
3826 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3827 *
3828 * This will dump the list of elements to the buffer
3829 * Intended just for the debug routine
3830 */
3831static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003832xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003833 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003834 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003835
3836 if (node == NULL) return;
3837 if (glob) strcat(buf, "(");
3838 cur = node;
3839 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003840 len = strlen(buf);
3841 if (size - len < 50) {
3842 if ((size - len > 4) && (buf[len - 1] != '.'))
3843 strcat(buf, " ...");
3844 return;
3845 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003846 switch (cur->type) {
3847 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003848 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
3849 if (size - len < xmlStrlen(cur->ns->prefix + 10)) {
3850 if ((size - len > 4) && (buf[len - 1] != '.'))
3851 strcat(buf, " ...");
3852 return;
3853 }
3854 strcat(buf, (char *) cur->ns->prefix);
3855 strcat(buf, ":");
3856 }
Daniel Veillardd3d06722001-08-15 12:06:36 +00003857 if (size - len < xmlStrlen(cur->name + 10)) {
3858 if ((size - len > 4) && (buf[len - 1] != '.'))
3859 strcat(buf, " ...");
3860 return;
3861 }
3862 strcat(buf, (char *) cur->name);
3863 if (cur->next != NULL)
3864 strcat(buf, " ");
3865 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003866 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003867 if (xmlIsBlankNode(cur))
3868 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003869 case XML_CDATA_SECTION_NODE:
3870 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003871 strcat(buf, "CDATA");
3872 if (cur->next != NULL)
3873 strcat(buf, " ");
3874 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003875 case XML_ATTRIBUTE_NODE:
3876 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003877#ifdef LIBXML_DOCB_ENABLED
3878 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003879#endif
3880 case XML_HTML_DOCUMENT_NODE:
3881 case XML_DOCUMENT_TYPE_NODE:
3882 case XML_DOCUMENT_FRAG_NODE:
3883 case XML_NOTATION_NODE:
3884 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003885 strcat(buf, "???");
3886 if (cur->next != NULL)
3887 strcat(buf, " ");
3888 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003889 case XML_ENTITY_NODE:
3890 case XML_PI_NODE:
3891 case XML_DTD_NODE:
3892 case XML_COMMENT_NODE:
3893 case XML_ELEMENT_DECL:
3894 case XML_ATTRIBUTE_DECL:
3895 case XML_ENTITY_DECL:
3896 case XML_XINCLUDE_START:
3897 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003898 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003899 }
3900 cur = cur->next;
3901 }
3902 if (glob) strcat(buf, ")");
3903}
3904
3905/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003906 * xmlValidateElementContent:
3907 * @ctxt: the validation context
3908 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003909 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003910 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003911 *
3912 * Try to validate the content model of an element
3913 *
3914 * returns 1 if valid or 0 if not and -1 in case of error
3915 */
3916
3917static int
3918xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003919 xmlElementPtr elemDecl, int warn) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003920 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003921 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003922 xmlElementContentPtr cont;
3923 const xmlChar *name;
3924
3925 if (elemDecl == NULL)
3926 return(-1);
3927 cont = elemDecl->content;
3928 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003929
3930 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003931 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003932 */
3933 ctxt->vstateMax = 8;
3934 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
3935 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
3936 if (ctxt->vstateTab == NULL) {
3937 xmlGenericError(xmlGenericErrorContext,
3938 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003939 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003940 }
3941 /*
3942 * The first entry in the stack is reserved to the current state
3943 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00003944 ctxt->nodeMax = 0;
3945 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00003946 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003947 ctxt->vstate = &ctxt->vstateTab[0];
3948 ctxt->vstateNr = 1;
3949 CONT = cont;
3950 NODE = child;
3951 DEPTH = 0;
3952 OCCURS = 0;
3953 STATE = 0;
3954 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003955 if ((ret == -3) && (warn)) {
3956 VWARNING(ctxt->userData,
3957 "Element %s content model is ambiguous\n", name);
3958 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003959 /*
3960 * An entities reference appeared at this level.
3961 * Buid a minimal representation of this node content
3962 * sufficient to run the validation process on it
3963 */
3964 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003965 cur = child;
3966 while (cur != NULL) {
3967 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003968 case XML_ENTITY_REF_NODE:
3969 /*
3970 * Push the current node to be able to roll back
3971 * and process within the entity
3972 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003973 if ((cur->children != NULL) &&
3974 (cur->children->children != NULL)) {
3975 nodeVPush(ctxt, cur);
3976 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003977 continue;
3978 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00003979 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003980 case XML_TEXT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003981 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003982 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003983 /* no break on purpose */
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00003984 case XML_CDATA_SECTION_NODE:
3985 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003986 case XML_ELEMENT_NODE:
3987 /*
3988 * Allocate a new node and minimally fills in
3989 * what's required
3990 */
3991 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3992 if (tmp == NULL) {
3993 xmlGenericError(xmlGenericErrorContext,
3994 "xmlValidateElementContent : malloc failed\n");
3995 xmlFreeNodeList(repl);
3996 ret = -1;
3997 goto done;
3998 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003999 tmp->type = cur->type;
4000 tmp->name = cur->name;
4001 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004002 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004003 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004004 if (repl == NULL)
4005 repl = last = tmp;
4006 else {
4007 last->next = tmp;
4008 last = tmp;
4009 }
Daniel Veillardd6dc4cb2002-02-19 14:18:08 +00004010 if (cur->type == XML_CDATA_SECTION_NODE) {
4011 /*
4012 * E59 spaces in CDATA does not match the
4013 * nonterminal S
4014 */
4015 tmp->content = xmlStrdup(BAD_CAST "CDATA");
4016 }
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004017 break;
4018 default:
4019 break;
4020 }
4021 /*
4022 * Switch to next element
4023 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004024 cur = cur->next;
4025 while (cur == NULL) {
4026 cur = nodeVPop(ctxt);
4027 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004028 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004029 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004030 }
4031 }
4032
4033 /*
4034 * Relaunch the validation
4035 */
4036 ctxt->vstate = &ctxt->vstateTab[0];
4037 ctxt->vstateNr = 1;
4038 CONT = cont;
4039 NODE = repl;
4040 DEPTH = 0;
4041 OCCURS = 0;
4042 STATE = 0;
4043 ret = xmlValidateElementType(ctxt);
4044 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004045 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004046 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4047 char expr[5000];
4048 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004049
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004050 expr[0] = 0;
4051 xmlSnprintfElementContent(expr, 5000, cont, 1);
4052 list[0] = 0;
4053 if (repl != NULL)
4054 xmlSnprintfElements(list, 5000, repl, 1);
4055 else
4056 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004057
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004058 if (name != NULL) {
4059 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004060 "Element %s content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004061 name, expr, list);
4062 } else {
4063 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004064 "Element content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004065 expr, list);
4066 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004067 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004068 if (name != NULL) {
4069 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004070 "Element %s content doesn't follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004071 name);
4072 } else {
4073 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004074 "Element content doesn't follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004075 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004076 }
4077 ret = 0;
4078 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004079 if (ret == -3)
4080 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004081
4082
4083done:
4084 /*
4085 * Deallocate the copy if done, and free up the validation stack
4086 */
4087 while (repl != NULL) {
4088 tmp = repl->next;
4089 xmlFree(repl);
4090 repl = tmp;
4091 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004092 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004093 if (ctxt->vstateTab != NULL) {
4094 xmlFree(ctxt->vstateTab);
4095 ctxt->vstateTab = NULL;
4096 }
4097 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004098 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004099 if (ctxt->nodeTab != NULL) {
4100 xmlFree(ctxt->nodeTab);
4101 ctxt->nodeTab = NULL;
4102 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004103 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004104
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004105}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004106
Owen Taylor3473f882001-02-23 17:55:21 +00004107/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004108 * xmlValidateCdataElement:
4109 * @ctxt: the validation context
4110 * @doc: a document instance
4111 * @elem: an element instance
4112 *
4113 * Check that an element follows #CDATA
4114 *
4115 * returns 1 if valid or 0 otherwise
4116 */
4117static int
4118xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4119 xmlNodePtr elem) {
4120 int ret = 1;
4121 xmlNodePtr cur, child;
4122
4123 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4124 return(0);
4125
4126 child = elem->children;
4127
4128 cur = child;
4129 while (cur != NULL) {
4130 switch (cur->type) {
4131 case XML_ENTITY_REF_NODE:
4132 /*
4133 * Push the current node to be able to roll back
4134 * and process within the entity
4135 */
4136 if ((cur->children != NULL) &&
4137 (cur->children->children != NULL)) {
4138 nodeVPush(ctxt, cur);
4139 cur = cur->children->children;
4140 continue;
4141 }
4142 break;
4143 case XML_COMMENT_NODE:
4144 case XML_PI_NODE:
4145 case XML_TEXT_NODE:
4146 case XML_CDATA_SECTION_NODE:
4147 break;
4148 default:
4149 ret = 0;
4150 goto done;
4151 }
4152 /*
4153 * Switch to next element
4154 */
4155 cur = cur->next;
4156 while (cur == NULL) {
4157 cur = nodeVPop(ctxt);
4158 if (cur == NULL)
4159 break;
4160 cur = cur->next;
4161 }
4162 }
4163done:
4164 ctxt->nodeMax = 0;
4165 ctxt->nodeNr = 0;
4166 if (ctxt->nodeTab != NULL) {
4167 xmlFree(ctxt->nodeTab);
4168 ctxt->nodeTab = NULL;
4169 }
4170 return(ret);
4171}
4172
4173/**
Owen Taylor3473f882001-02-23 17:55:21 +00004174 * xmlValidateOneElement:
4175 * @ctxt: the validation context
4176 * @doc: a document instance
4177 * @elem: an element instance
4178 *
4179 * Try to validate a single element and it's attributes,
4180 * basically it does the following checks as described by the
4181 * XML-1.0 recommendation:
4182 * - [ VC: Element Valid ]
4183 * - [ VC: Required Attribute ]
4184 * Then call xmlValidateOneAttribute() for each attribute present.
4185 *
4186 * The ID/IDREF checkings are done separately
4187 *
4188 * returns 1 if valid or 0 otherwise
4189 */
4190
4191int
4192xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4193 xmlNodePtr elem) {
4194 xmlElementPtr elemDecl = NULL;
4195 xmlElementContentPtr cont;
4196 xmlAttributePtr attr;
4197 xmlNodePtr child;
4198 int ret = 1;
4199 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004200 const xmlChar *prefix = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004201
4202 CHECK_DTD;
4203
4204 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004205 switch (elem->type) {
4206 case XML_ATTRIBUTE_NODE:
4207 VERROR(ctxt->userData,
4208 "Attribute element not expected here\n");
4209 return(0);
4210 case XML_TEXT_NODE:
4211 if (elem->children != NULL) {
4212 VERROR(ctxt->userData, "Text element has childs !\n");
4213 return(0);
4214 }
4215 if (elem->properties != NULL) {
4216 VERROR(ctxt->userData, "Text element has attributes !\n");
4217 return(0);
4218 }
4219 if (elem->ns != NULL) {
4220 VERROR(ctxt->userData, "Text element has namespace !\n");
4221 return(0);
4222 }
4223 if (elem->nsDef != NULL) {
4224 VERROR(ctxt->userData,
4225 "Text element carries namespace definitions !\n");
4226 return(0);
4227 }
4228 if (elem->content == NULL) {
4229 VERROR(ctxt->userData,
4230 "Text element has no content !\n");
4231 return(0);
4232 }
4233 return(1);
4234 case XML_XINCLUDE_START:
4235 case XML_XINCLUDE_END:
4236 return(1);
4237 case XML_CDATA_SECTION_NODE:
4238 case XML_ENTITY_REF_NODE:
4239 case XML_PI_NODE:
4240 case XML_COMMENT_NODE:
4241 return(1);
4242 case XML_ENTITY_NODE:
4243 VERROR(ctxt->userData,
4244 "Entity element not expected here\n");
4245 return(0);
4246 case XML_NOTATION_NODE:
4247 VERROR(ctxt->userData,
4248 "Notation element not expected here\n");
4249 return(0);
4250 case XML_DOCUMENT_NODE:
4251 case XML_DOCUMENT_TYPE_NODE:
4252 case XML_DOCUMENT_FRAG_NODE:
4253 VERROR(ctxt->userData,
4254 "Document element not expected here\n");
4255 return(0);
4256 case XML_HTML_DOCUMENT_NODE:
4257 VERROR(ctxt->userData,
4258 "\n");
4259 return(0);
4260 case XML_ELEMENT_NODE:
4261 break;
4262 default:
4263 VERROR(ctxt->userData,
4264 "unknown element type %d\n", elem->type);
4265 return(0);
4266 }
4267 if (elem->name == NULL) return(0);
4268
4269 /*
4270 * Fetch the declaration for the qualified name
4271 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004272 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4273 prefix = elem->ns->prefix;
4274
4275 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004276 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004277 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004278 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4279 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004280 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004281 }
4282
4283 /*
4284 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004285 * This is "non-strict" validation should be done on the
4286 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004287 */
4288 if (elemDecl == NULL) {
4289 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4290 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4291 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4292 }
4293 if (elemDecl == NULL) {
4294 VERROR(ctxt->userData, "No declaration for element %s\n",
4295 elem->name);
4296 return(0);
4297 }
4298
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004299 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00004300 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004301 case XML_ELEMENT_TYPE_UNDEFINED:
4302 VERROR(ctxt->userData, "No declaration for element %s\n",
4303 elem->name);
4304 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004305 case XML_ELEMENT_TYPE_EMPTY:
4306 if (elem->children != NULL) {
4307 VERROR(ctxt->userData,
4308 "Element %s was declared EMPTY this one has content\n",
4309 elem->name);
4310 ret = 0;
4311 }
4312 break;
4313 case XML_ELEMENT_TYPE_ANY:
4314 /* I don't think anything is required then */
4315 break;
4316 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004317 /* simple case of declared as #PCDATA */
4318 if ((elemDecl->content != NULL) &&
4319 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4320 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4321 if (!ret) {
4322 VERROR(ctxt->userData,
4323 "Element %s was declared #PCDATA but contains non text nodes\n",
4324 elem->name);
4325 }
4326 break;
4327 }
Owen Taylor3473f882001-02-23 17:55:21 +00004328 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004329 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004330 while (child != NULL) {
4331 if (child->type == XML_ELEMENT_NODE) {
4332 name = child->name;
4333 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4334 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004335 snprintf((char *) qname, sizeof(qname), "%s:%s",
4336 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004337 qname[sizeof(qname) - 1] = 0;
4338 cont = elemDecl->content;
4339 while (cont != NULL) {
4340 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4341 if (xmlStrEqual(cont->name, qname)) break;
4342 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4343 (cont->c1 != NULL) &&
4344 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4345 if (xmlStrEqual(cont->c1->name, qname)) break;
4346 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4347 (cont->c1 == NULL) ||
4348 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4349 /* Internal error !!! */
4350 xmlGenericError(xmlGenericErrorContext,
4351 "Internal: MIXED struct bad\n");
4352 break;
4353 }
4354 cont = cont->c2;
4355 }
4356 if (cont != NULL)
4357 goto child_ok;
4358 }
4359 cont = elemDecl->content;
4360 while (cont != NULL) {
4361 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4362 if (xmlStrEqual(cont->name, name)) break;
4363 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4364 (cont->c1 != NULL) &&
4365 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4366 if (xmlStrEqual(cont->c1->name, name)) break;
4367 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4368 (cont->c1 == NULL) ||
4369 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4370 /* Internal error !!! */
4371 xmlGenericError(xmlGenericErrorContext,
4372 "Internal: MIXED struct bad\n");
4373 break;
4374 }
4375 cont = cont->c2;
4376 }
4377 if (cont == NULL) {
4378 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00004379 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004380 name, elem->name);
4381 ret = 0;
4382 }
4383 }
4384child_ok:
4385 child = child->next;
4386 }
4387 break;
4388 case XML_ELEMENT_TYPE_ELEMENT:
4389 child = elem->children;
4390 cont = elemDecl->content;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004391 ret = xmlValidateElementContent(ctxt, child, elemDecl, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00004392 break;
4393 }
4394
4395 /* [ VC: Required Attribute ] */
4396 attr = elemDecl->attributes;
4397 while (attr != NULL) {
4398 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004399 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00004400
Daniel Veillarde4301c82002-02-13 13:32:35 +00004401 if ((attr->prefix == NULL) &&
4402 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4403 xmlNsPtr ns;
4404
4405 ns = elem->nsDef;
4406 while (ns != NULL) {
4407 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004408 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004409 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00004410 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004411 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4412 xmlNsPtr ns;
4413
4414 ns = elem->nsDef;
4415 while (ns != NULL) {
4416 if (xmlStrEqual(attr->name, ns->prefix))
4417 goto found;
4418 ns = ns->next;
4419 }
4420 } else {
4421 xmlAttrPtr attrib;
4422
4423 attrib = elem->properties;
4424 while (attrib != NULL) {
4425 if (xmlStrEqual(attrib->name, attr->name)) {
4426 if (attr->prefix != NULL) {
4427 xmlNsPtr nameSpace = attrib->ns;
4428
4429 if (nameSpace == NULL)
4430 nameSpace = elem->ns;
4431 /*
4432 * qualified names handling is problematic, having a
4433 * different prefix should be possible but DTDs don't
4434 * allow to define the URI instead of the prefix :-(
4435 */
4436 if (nameSpace == NULL) {
4437 if (qualified < 0)
4438 qualified = 0;
4439 } else if (!xmlStrEqual(nameSpace->prefix,
4440 attr->prefix)) {
4441 if (qualified < 1)
4442 qualified = 1;
4443 } else
4444 goto found;
4445 } else {
4446 /*
4447 * We should allow applications to define namespaces
4448 * for their application even if the DTD doesn't
4449 * carry one, otherwise, basically we would always
4450 * break.
4451 */
4452 goto found;
4453 }
4454 }
4455 attrib = attrib->next;
4456 }
Owen Taylor3473f882001-02-23 17:55:21 +00004457 }
4458 if (qualified == -1) {
4459 if (attr->prefix == NULL) {
4460 VERROR(ctxt->userData,
4461 "Element %s doesn't carry attribute %s\n",
4462 elem->name, attr->name);
4463 ret = 0;
4464 } else {
4465 VERROR(ctxt->userData,
4466 "Element %s doesn't carry attribute %s:%s\n",
4467 elem->name, attr->prefix,attr->name);
4468 ret = 0;
4469 }
4470 } else if (qualified == 0) {
4471 VWARNING(ctxt->userData,
4472 "Element %s required attribute %s:%s has no prefix\n",
4473 elem->name, attr->prefix,attr->name);
4474 } else if (qualified == 1) {
4475 VWARNING(ctxt->userData,
4476 "Element %s required attribute %s:%s has different prefix\n",
4477 elem->name, attr->prefix,attr->name);
4478 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004479 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
4480 /*
4481 * Special tests checking #FIXED namespace declarations
4482 * have the right value since this is not done as an
4483 * attribute checking
4484 */
4485 if ((attr->prefix == NULL) &&
4486 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4487 xmlNsPtr ns;
4488
4489 ns = elem->nsDef;
4490 while (ns != NULL) {
4491 if (ns->prefix == NULL) {
4492 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4493 VERROR(ctxt->userData,
4494 "Element %s namespace name for default namespace does not match the DTD\n",
4495 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00004496 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004497 }
4498 goto found;
4499 }
4500 ns = ns->next;
4501 }
4502 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4503 xmlNsPtr ns;
4504
4505 ns = elem->nsDef;
4506 while (ns != NULL) {
4507 if (xmlStrEqual(attr->name, ns->prefix)) {
4508 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4509 VERROR(ctxt->userData,
4510 "Element %s namespace name for %s doesn't match the DTD\n",
4511 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00004512 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004513 }
4514 goto found;
4515 }
4516 ns = ns->next;
4517 }
4518 }
Owen Taylor3473f882001-02-23 17:55:21 +00004519 }
4520found:
4521 attr = attr->nexth;
4522 }
4523 return(ret);
4524}
4525
4526/**
4527 * xmlValidateRoot:
4528 * @ctxt: the validation context
4529 * @doc: a document instance
4530 *
4531 * Try to validate a the root element
4532 * basically it does the following check as described by the
4533 * XML-1.0 recommendation:
4534 * - [ VC: Root Element Type ]
4535 * it doesn't try to recurse or apply other check to the element
4536 *
4537 * returns 1 if valid or 0 otherwise
4538 */
4539
4540int
4541xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4542 xmlNodePtr root;
4543 if (doc == NULL) return(0);
4544
4545 root = xmlDocGetRootElement(doc);
4546 if ((root == NULL) || (root->name == NULL)) {
4547 VERROR(ctxt->userData, "Not valid: no root element\n");
4548 return(0);
4549 }
4550
4551 /*
4552 * When doing post validation against a separate DTD, those may
4553 * no internal subset has been generated
4554 */
4555 if ((doc->intSubset != NULL) &&
4556 (doc->intSubset->name != NULL)) {
4557 /*
4558 * Check first the document root against the NQName
4559 */
4560 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4561 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4562 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004563 snprintf((char *) qname, sizeof(qname), "%s:%s",
4564 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004565 qname[sizeof(qname) - 1] = 0;
4566 if (xmlStrEqual(doc->intSubset->name, qname))
4567 goto name_ok;
4568 }
4569 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4570 (xmlStrEqual(root->name, BAD_CAST "html")))
4571 goto name_ok;
4572 VERROR(ctxt->userData,
4573 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4574 root->name, doc->intSubset->name);
4575 return(0);
4576
4577 }
4578 }
4579name_ok:
4580 return(1);
4581}
4582
4583
4584/**
4585 * xmlValidateElement:
4586 * @ctxt: the validation context
4587 * @doc: a document instance
4588 * @elem: an element instance
4589 *
4590 * Try to validate the subtree under an element
4591 *
4592 * returns 1 if valid or 0 otherwise
4593 */
4594
4595int
4596xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4597 xmlNodePtr child;
4598 xmlAttrPtr attr;
4599 xmlChar *value;
4600 int ret = 1;
4601
4602 if (elem == NULL) return(0);
4603
4604 /*
4605 * XInclude elements were added after parsing in the infoset,
4606 * they don't really mean anything validation wise.
4607 */
4608 if ((elem->type == XML_XINCLUDE_START) ||
4609 (elem->type == XML_XINCLUDE_END))
4610 return(1);
4611
4612 CHECK_DTD;
4613
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004614 /*
4615 * Entities references have to be handled separately
4616 */
4617 if (elem->type == XML_ENTITY_REF_NODE) {
4618 return(1);
4619 }
4620
Owen Taylor3473f882001-02-23 17:55:21 +00004621 ret &= xmlValidateOneElement(ctxt, doc, elem);
4622 attr = elem->properties;
4623 while(attr != NULL) {
4624 value = xmlNodeListGetString(doc, attr->children, 0);
4625 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4626 if (value != NULL)
4627 xmlFree(value);
4628 attr= attr->next;
4629 }
4630 child = elem->children;
4631 while (child != NULL) {
4632 ret &= xmlValidateElement(ctxt, doc, child);
4633 child = child->next;
4634 }
4635
4636 return(ret);
4637}
4638
Daniel Veillard8730c562001-02-26 10:49:57 +00004639/**
4640 * xmlValidateRef:
4641 * @ref: A reference to be validated
4642 * @ctxt: Validation context
4643 * @name: Name of ID we are searching for
4644 *
4645 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004646static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004647xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004648 const xmlChar *name) {
4649 xmlAttrPtr id;
4650 xmlAttrPtr attr;
4651
4652 if (ref == NULL)
4653 return;
4654 attr = ref->attr;
4655 if (attr == NULL)
4656 return;
4657 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4658 id = xmlGetID(ctxt->doc, name);
4659 if (id == NULL) {
4660 VERROR(ctxt->userData,
4661 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4662 attr->name, name);
4663 ctxt->valid = 0;
4664 }
4665 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4666 xmlChar *dup, *str = NULL, *cur, save;
4667
4668 dup = xmlStrdup(name);
4669 if (dup == NULL) {
4670 ctxt->valid = 0;
4671 return;
4672 }
4673 cur = dup;
4674 while (*cur != 0) {
4675 str = cur;
4676 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4677 save = *cur;
4678 *cur = 0;
4679 id = xmlGetID(ctxt->doc, str);
4680 if (id == NULL) {
4681 VERROR(ctxt->userData,
4682 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4683 attr->name, str);
4684 ctxt->valid = 0;
4685 }
4686 if (save == 0)
4687 break;
4688 *cur = save;
4689 while (IS_BLANK(*cur)) cur++;
4690 }
4691 xmlFree(dup);
4692 }
4693}
4694
4695/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004696 * xmlWalkValidateList:
4697 * @data: Contents of current link
4698 * @user: Value supplied by the user
4699 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004700 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00004701 */
4702static int
4703xmlWalkValidateList(const void *data, const void *user)
4704{
4705 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4706 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4707 return 1;
4708}
4709
4710/**
4711 * xmlValidateCheckRefCallback:
4712 * @ref_list: List of references
4713 * @ctxt: Validation context
4714 * @name: Name of ID we are searching for
4715 *
4716 */
4717static void
4718xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4719 const xmlChar *name) {
4720 xmlValidateMemo memo;
4721
4722 if (ref_list == NULL)
4723 return;
4724 memo.ctxt = ctxt;
4725 memo.name = name;
4726
4727 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4728
4729}
4730
4731/**
Owen Taylor3473f882001-02-23 17:55:21 +00004732 * xmlValidateDocumentFinal:
4733 * @ctxt: the validation context
4734 * @doc: a document instance
4735 *
4736 * Does the final step for the document validation once all the
4737 * incremental validation steps have been completed
4738 *
4739 * basically it does the following checks described by the XML Rec
4740 *
4741 *
4742 * returns 1 if valid or 0 otherwise
4743 */
4744
4745int
4746xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4747 xmlRefTablePtr table;
4748
4749 if (doc == NULL) {
4750 xmlGenericError(xmlGenericErrorContext,
4751 "xmlValidateDocumentFinal: doc == NULL\n");
4752 return(0);
4753 }
4754
4755 /*
4756 * Check all the NOTATION/NOTATIONS attributes
4757 */
4758 /*
4759 * Check all the ENTITY/ENTITIES attributes definition for validity
4760 */
4761 /*
4762 * Check all the IDREF/IDREFS attributes definition for validity
4763 */
4764 table = (xmlRefTablePtr) doc->refs;
4765 ctxt->doc = doc;
4766 ctxt->valid = 1;
4767 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4768 return(ctxt->valid);
4769}
4770
4771/**
4772 * xmlValidateDtd:
4773 * @ctxt: the validation context
4774 * @doc: a document instance
4775 * @dtd: a dtd instance
4776 *
4777 * Try to validate the document against the dtd instance
4778 *
4779 * basically it does check all the definitions in the DtD.
4780 *
4781 * returns 1 if valid or 0 otherwise
4782 */
4783
4784int
4785xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4786 int ret;
4787 xmlDtdPtr oldExt;
4788 xmlNodePtr root;
4789
4790 if (dtd == NULL) return(0);
4791 if (doc == NULL) return(0);
4792 oldExt = doc->extSubset;
4793 doc->extSubset = dtd;
4794 ret = xmlValidateRoot(ctxt, doc);
4795 if (ret == 0) {
4796 doc->extSubset = oldExt;
4797 return(ret);
4798 }
4799 if (doc->ids != NULL) {
4800 xmlFreeIDTable(doc->ids);
4801 doc->ids = NULL;
4802 }
4803 if (doc->refs != NULL) {
4804 xmlFreeRefTable(doc->refs);
4805 doc->refs = NULL;
4806 }
4807 root = xmlDocGetRootElement(doc);
4808 ret = xmlValidateElement(ctxt, doc, root);
4809 ret &= xmlValidateDocumentFinal(ctxt, doc);
4810 doc->extSubset = oldExt;
4811 return(ret);
4812}
4813
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004814static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004815xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
4816 const xmlChar *name ATTRIBUTE_UNUSED) {
4817 if (cur == NULL)
4818 return;
4819 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
4820 xmlChar *notation = cur->content;
4821
Daniel Veillard878eab02002-02-19 13:46:09 +00004822 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004823 int ret;
4824
4825 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
4826 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00004827 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004828 }
4829 }
4830 }
4831}
4832
4833static void
Owen Taylor3473f882001-02-23 17:55:21 +00004834xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004835 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004836 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00004837 xmlDocPtr doc;
4838 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004839
Owen Taylor3473f882001-02-23 17:55:21 +00004840 if (cur == NULL)
4841 return;
4842 switch (cur->atype) {
4843 case XML_ATTRIBUTE_CDATA:
4844 case XML_ATTRIBUTE_ID:
4845 case XML_ATTRIBUTE_IDREF :
4846 case XML_ATTRIBUTE_IDREFS:
4847 case XML_ATTRIBUTE_NMTOKEN:
4848 case XML_ATTRIBUTE_NMTOKENS:
4849 case XML_ATTRIBUTE_ENUMERATION:
4850 break;
4851 case XML_ATTRIBUTE_ENTITY:
4852 case XML_ATTRIBUTE_ENTITIES:
4853 case XML_ATTRIBUTE_NOTATION:
4854 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004855
4856 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
4857 cur->atype, cur->defaultValue);
4858 if ((ret == 0) && (ctxt->valid == 1))
4859 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004860 }
4861 if (cur->tree != NULL) {
4862 xmlEnumerationPtr tree = cur->tree;
4863 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004864 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00004865 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004866 if ((ret == 0) && (ctxt->valid == 1))
4867 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004868 tree = tree->next;
4869 }
4870 }
4871 }
Daniel Veillard878eab02002-02-19 13:46:09 +00004872 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
4873 doc = cur->doc;
4874 if ((doc == NULL) || (cur->elem == NULL)) {
4875 VERROR(ctxt->userData,
4876 "xmlValidateAttributeCallback(%s): internal error\n",
4877 cur->name);
4878 return;
4879 }
4880 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
4881 if (elem == NULL)
4882 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
4883 if (elem == NULL) {
4884 VERROR(ctxt->userData,
4885 "attribute %s: could not find decl for element %s\n",
4886 cur->name, cur->elem);
4887 return;
4888 }
4889 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
4890 VERROR(ctxt->userData,
4891 "NOTATION attribute %s declared on EMPTY element %s\n",
4892 cur->name, cur->elem);
4893 ctxt->valid = 0;
4894 }
4895 }
Owen Taylor3473f882001-02-23 17:55:21 +00004896}
4897
4898/**
4899 * xmlValidateDtdFinal:
4900 * @ctxt: the validation context
4901 * @doc: a document instance
4902 *
4903 * Does the final step for the dtds validation once all the
4904 * subsets have been parsed
4905 *
4906 * basically it does the following checks described by the XML Rec
4907 * - check that ENTITY and ENTITIES type attributes default or
4908 * possible values matches one of the defined entities.
4909 * - check that NOTATION type attributes default or
4910 * possible values matches one of the defined notations.
4911 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004912 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00004913 */
4914
4915int
4916xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00004917 xmlDtdPtr dtd;
4918 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004919 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00004920
4921 if (doc == NULL) return(0);
4922 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4923 return(0);
4924 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004925 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00004926 dtd = doc->intSubset;
4927 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4928 table = (xmlAttributeTablePtr) dtd->attributes;
4929 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00004930 }
4931 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004932 entities = (xmlEntitiesTablePtr) dtd->entities;
4933 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
4934 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004935 }
4936 dtd = doc->extSubset;
4937 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4938 table = (xmlAttributeTablePtr) dtd->attributes;
4939 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00004940 }
4941 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004942 entities = (xmlEntitiesTablePtr) dtd->entities;
4943 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
4944 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004945 }
4946 return(ctxt->valid);
4947}
4948
4949/**
4950 * xmlValidateDocument:
4951 * @ctxt: the validation context
4952 * @doc: a document instance
4953 *
4954 * Try to validate the document instance
4955 *
4956 * basically it does the all the checks described by the XML Rec
4957 * i.e. validates the internal and external subset (if present)
4958 * and validate the document tree.
4959 *
4960 * returns 1 if valid or 0 otherwise
4961 */
4962
4963int
4964xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4965 int ret;
4966 xmlNodePtr root;
4967
4968 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4969 return(0);
4970 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
4971 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
4972 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
4973 doc->intSubset->SystemID);
4974 if (doc->extSubset == NULL) {
4975 if (doc->intSubset->SystemID != NULL) {
4976 VERROR(ctxt->userData,
4977 "Could not load the external subset \"%s\"\n",
4978 doc->intSubset->SystemID);
4979 } else {
4980 VERROR(ctxt->userData,
4981 "Could not load the external subset \"%s\"\n",
4982 doc->intSubset->ExternalID);
4983 }
4984 return(0);
4985 }
4986 }
4987
4988 if (doc->ids != NULL) {
4989 xmlFreeIDTable(doc->ids);
4990 doc->ids = NULL;
4991 }
4992 if (doc->refs != NULL) {
4993 xmlFreeRefTable(doc->refs);
4994 doc->refs = NULL;
4995 }
4996 ret = xmlValidateDtdFinal(ctxt, doc);
4997 if (!xmlValidateRoot(ctxt, doc)) return(0);
4998
4999 root = xmlDocGetRootElement(doc);
5000 ret &= xmlValidateElement(ctxt, doc, root);
5001 ret &= xmlValidateDocumentFinal(ctxt, doc);
5002 return(ret);
5003}
5004
5005
5006/************************************************************************
5007 * *
5008 * Routines for dynamic validation editing *
5009 * *
5010 ************************************************************************/
5011
5012/**
5013 * xmlValidGetPotentialChildren:
5014 * @ctree: an element content tree
5015 * @list: an array to store the list of child names
5016 * @len: a pointer to the number of element in the list
5017 * @max: the size of the array
5018 *
5019 * Build/extend a list of potential children allowed by the content tree
5020 *
5021 * returns the number of element in the list, or -1 in case of error.
5022 */
5023
5024int
5025xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
5026 int *len, int max) {
5027 int i;
5028
5029 if ((ctree == NULL) || (list == NULL) || (len == NULL))
5030 return(-1);
5031 if (*len >= max) return(*len);
5032
5033 switch (ctree->type) {
5034 case XML_ELEMENT_CONTENT_PCDATA:
5035 for (i = 0; i < *len;i++)
5036 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
5037 list[(*len)++] = BAD_CAST "#PCDATA";
5038 break;
5039 case XML_ELEMENT_CONTENT_ELEMENT:
5040 for (i = 0; i < *len;i++)
5041 if (xmlStrEqual(ctree->name, list[i])) return(*len);
5042 list[(*len)++] = ctree->name;
5043 break;
5044 case XML_ELEMENT_CONTENT_SEQ:
5045 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5046 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5047 break;
5048 case XML_ELEMENT_CONTENT_OR:
5049 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5050 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5051 break;
5052 }
5053
5054 return(*len);
5055}
5056
5057/**
5058 * xmlValidGetValidElements:
5059 * @prev: an element to insert after
5060 * @next: an element to insert next
5061 * @list: an array to store the list of child names
5062 * @max: the size of the array
5063 *
5064 * This function returns the list of authorized children to insert
5065 * within an existing tree while respecting the validity constraints
5066 * forced by the Dtd. The insertion point is defined using @prev and
5067 * @next in the following ways:
5068 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
5069 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
5070 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
5071 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
5072 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
5073 *
5074 * pointers to the element names are inserted at the beginning of the array
5075 * and do not need to be freed.
5076 *
5077 * returns the number of element in the list, or -1 in case of error. If
5078 * the function returns the value @max the caller is invited to grow the
5079 * receiving array and retry.
5080 */
5081
5082int
5083xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
5084 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005085 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00005086 int nb_valid_elements = 0;
5087 const xmlChar *elements[256];
5088 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005089 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005090
5091 xmlNode *ref_node;
5092 xmlNode *parent;
5093 xmlNode *test_node;
5094
5095 xmlNode *prev_next;
5096 xmlNode *next_prev;
5097 xmlNode *parent_childs;
5098 xmlNode *parent_last;
5099
5100 xmlElement *element_desc;
5101
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005102 vctxt.userData = NULL;
5103 vctxt.error = NULL;
5104 vctxt.warning = NULL;
5105
Owen Taylor3473f882001-02-23 17:55:21 +00005106 if (prev == NULL && next == NULL)
5107 return(-1);
5108
5109 if (list == NULL) return(-1);
5110 if (max <= 0) return(-1);
5111
5112 nb_valid_elements = 0;
5113 ref_node = prev ? prev : next;
5114 parent = ref_node->parent;
5115
5116 /*
5117 * Retrieves the parent element declaration
5118 */
5119 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
5120 parent->name);
5121 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
5122 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
5123 parent->name);
5124 if (element_desc == NULL) return(-1);
5125
5126 /*
5127 * Do a backup of the current tree structure
5128 */
5129 prev_next = prev ? prev->next : NULL;
5130 next_prev = next ? next->prev : NULL;
5131 parent_childs = parent->children;
5132 parent_last = parent->last;
5133
5134 /*
5135 * Creates a dummy node and insert it into the tree
5136 */
5137 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
5138 test_node->doc = ref_node->doc;
5139 test_node->parent = parent;
5140 test_node->prev = prev;
5141 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00005142 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00005143
5144 if (prev) prev->next = test_node;
5145 else parent->children = test_node;
5146
5147 if (next) next->prev = test_node;
5148 else parent->last = test_node;
5149
5150 /*
5151 * Insert each potential child node and check if the parent is
5152 * still valid
5153 */
5154 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
5155 elements, &nb_elements, 256);
5156
5157 for (i = 0;i < nb_elements;i++) {
5158 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005159 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005160 int j;
5161
5162 for (j = 0; j < nb_valid_elements;j++)
5163 if (xmlStrEqual(elements[i], list[j])) break;
5164 list[nb_valid_elements++] = elements[i];
5165 if (nb_valid_elements >= max) break;
5166 }
5167 }
5168
5169 /*
5170 * Restore the tree structure
5171 */
5172 if (prev) prev->next = prev_next;
5173 if (next) next->prev = next_prev;
5174 parent->children = parent_childs;
5175 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00005176
5177 /*
5178 * Free up the dummy node
5179 */
5180 test_node->name = name;
5181 xmlFreeNode(test_node);
5182
Owen Taylor3473f882001-02-23 17:55:21 +00005183 return(nb_valid_elements);
5184}