blob: 7be1a4ae70901ff9c061ca17f23d0932ffc03f8b [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) &&
3537 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003538 ret = 1;
3539 break;
3540 } else {
3541 DEBUG_VALID_MSG("pcdata failed");
3542 ret = 0;
3543 break;
3544 }
3545 break;
3546 case XML_ELEMENT_CONTENT_ELEMENT:
3547 if (NODE == NULL) {
3548 DEBUG_VALID_MSG("element failed no node");
3549 ret = 0;
3550 break;
3551 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003552 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3553 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003554 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003555 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3556 ret = (CONT->prefix == NULL);
3557 } else if (CONT->prefix == NULL) {
3558 ret = 0;
3559 } else {
3560 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3561 }
3562 }
3563 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003564 DEBUG_VALID_MSG("element found, skip to next");
3565 /*
3566 * go to next element in the content model
3567 * skipping ignorable elems
3568 */
3569 do {
3570 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003571 NODE = xmlValidateSkipIgnorable(NODE);
3572 if ((NODE != NULL) &&
3573 (NODE->type == XML_ENTITY_REF_NODE))
3574 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003575 } while ((NODE != NULL) &&
3576 ((NODE->type != XML_ELEMENT_NODE) &&
3577 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003578 } else {
3579 DEBUG_VALID_MSG("element failed");
3580 ret = 0;
3581 break;
3582 }
3583 break;
3584 case XML_ELEMENT_CONTENT_OR:
3585 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003586 * Small optimization.
3587 */
3588 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3589 if ((NODE == NULL) ||
3590 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3591 DEPTH++;
3592 CONT = CONT->c2;
3593 goto cont;
3594 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003595 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3596 ret = (CONT->c1->prefix == NULL);
3597 } else if (CONT->c1->prefix == NULL) {
3598 ret = 0;
3599 } else {
3600 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3601 }
3602 if (ret == 0) {
3603 DEPTH++;
3604 CONT = CONT->c2;
3605 goto cont;
3606 }
Daniel Veillard85349052001-04-20 13:48:21 +00003607 }
3608
3609 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003610 * save the second branch 'or' branch
3611 */
3612 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003613 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3614 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003615
3616 DEPTH++;
3617 CONT = CONT->c1;
3618 goto cont;
3619 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003620 /*
3621 * Small optimization.
3622 */
3623 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3624 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3625 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3626 if ((NODE == NULL) ||
3627 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3628 DEPTH++;
3629 CONT = CONT->c2;
3630 goto cont;
3631 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003632 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3633 ret = (CONT->c1->prefix == NULL);
3634 } else if (CONT->c1->prefix == NULL) {
3635 ret = 0;
3636 } else {
3637 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3638 }
3639 if (ret == 0) {
3640 DEPTH++;
3641 CONT = CONT->c2;
3642 goto cont;
3643 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003644 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003645 DEPTH++;
3646 CONT = CONT->c1;
3647 goto cont;
3648 }
3649
3650 /*
3651 * At this point handle going up in the tree
3652 */
3653 if (ret == -1) {
3654 DEBUG_VALID_MSG("error found returning");
3655 return(ret);
3656 }
3657analyze:
3658 while (CONT != NULL) {
3659 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003660 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003661 * this level.
3662 */
3663 if (ret == 0) {
3664 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003665 xmlNodePtr cur;
3666
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003667 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003668 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003669 DEBUG_VALID_MSG("Once branch failed, rollback");
3670 if (vstateVPop(ctxt) < 0 ) {
3671 DEBUG_VALID_MSG("exhaustion, failed");
3672 return(0);
3673 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003674 if (cur != ctxt->vstate->node)
3675 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003676 goto cont;
3677 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00003678 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003679 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003680 DEBUG_VALID_MSG("Plus branch failed, rollback");
3681 if (vstateVPop(ctxt) < 0 ) {
3682 DEBUG_VALID_MSG("exhaustion, failed");
3683 return(0);
3684 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003685 if (cur != ctxt->vstate->node)
3686 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003687 goto cont;
3688 }
3689 DEBUG_VALID_MSG("Plus branch found");
3690 ret = 1;
3691 break;
3692 case XML_ELEMENT_CONTENT_MULT:
3693#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00003694 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003695 DEBUG_VALID_MSG("Mult branch failed");
3696 } else {
3697 DEBUG_VALID_MSG("Mult branch found");
3698 }
3699#endif
3700 ret = 1;
3701 break;
3702 case XML_ELEMENT_CONTENT_OPT:
3703 DEBUG_VALID_MSG("Option branch failed");
3704 ret = 1;
3705 break;
3706 }
3707 } else {
3708 switch (CONT->ocur) {
3709 case XML_ELEMENT_CONTENT_OPT:
3710 DEBUG_VALID_MSG("Option branch succeeded");
3711 ret = 1;
3712 break;
3713 case XML_ELEMENT_CONTENT_ONCE:
3714 DEBUG_VALID_MSG("Once branch succeeded");
3715 ret = 1;
3716 break;
3717 case XML_ELEMENT_CONTENT_PLUS:
3718 if (STATE == ROLLBACK_PARENT) {
3719 DEBUG_VALID_MSG("Plus branch rollback");
3720 ret = 1;
3721 break;
3722 }
3723 if (NODE == NULL) {
3724 DEBUG_VALID_MSG("Plus branch exhausted");
3725 ret = 1;
3726 break;
3727 }
3728 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003729 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003730 goto cont;
3731 case XML_ELEMENT_CONTENT_MULT:
3732 if (STATE == ROLLBACK_PARENT) {
3733 DEBUG_VALID_MSG("Mult branch rollback");
3734 ret = 1;
3735 break;
3736 }
3737 if (NODE == NULL) {
3738 DEBUG_VALID_MSG("Mult branch exhausted");
3739 ret = 1;
3740 break;
3741 }
3742 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003743 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003744 goto cont;
3745 }
3746 }
3747 STATE = 0;
3748
3749 /*
3750 * Then act accordingly at the parent level
3751 */
Daniel Veillard5344c602001-12-31 16:37:34 +00003752 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003753 if (CONT->parent == NULL)
3754 break;
3755
3756 switch (CONT->parent->type) {
3757 case XML_ELEMENT_CONTENT_PCDATA:
3758 DEBUG_VALID_MSG("Error: parent pcdata");
3759 return(-1);
3760 case XML_ELEMENT_CONTENT_ELEMENT:
3761 DEBUG_VALID_MSG("Error: parent element");
3762 return(-1);
3763 case XML_ELEMENT_CONTENT_OR:
3764 if (ret == 1) {
3765 DEBUG_VALID_MSG("Or succeeded");
3766 CONT = CONT->parent;
3767 DEPTH--;
3768 } else {
3769 DEBUG_VALID_MSG("Or failed");
3770 CONT = CONT->parent;
3771 DEPTH--;
3772 }
3773 break;
3774 case XML_ELEMENT_CONTENT_SEQ:
3775 if (ret == 0) {
3776 DEBUG_VALID_MSG("Sequence failed");
3777 CONT = CONT->parent;
3778 DEPTH--;
3779 } else if (CONT == CONT->parent->c1) {
3780 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3781 CONT = CONT->parent->c2;
3782 goto cont;
3783 } else {
3784 DEBUG_VALID_MSG("Sequence succeeded");
3785 CONT = CONT->parent;
3786 DEPTH--;
3787 }
3788 }
3789 }
3790 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003791 xmlNodePtr cur;
3792
3793 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003794 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3795 if (vstateVPop(ctxt) < 0 ) {
3796 DEBUG_VALID_MSG("exhaustion, failed");
3797 return(0);
3798 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003799 if (cur != ctxt->vstate->node)
3800 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003801 goto cont;
3802 }
3803 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003804 xmlNodePtr cur;
3805
3806 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003807 DEBUG_VALID_MSG("Failure, rollback");
3808 if (vstateVPop(ctxt) < 0 ) {
3809 DEBUG_VALID_MSG("exhaustion, failed");
3810 return(0);
3811 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003812 if (cur != ctxt->vstate->node)
3813 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003814 goto cont;
3815 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003816 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003817}
3818
3819/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003820 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003821 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003822 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003823 * @content: An element
3824 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3825 *
3826 * This will dump the list of elements to the buffer
3827 * Intended just for the debug routine
3828 */
3829static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003830xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003831 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003832 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003833
3834 if (node == NULL) return;
3835 if (glob) strcat(buf, "(");
3836 cur = node;
3837 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003838 len = strlen(buf);
3839 if (size - len < 50) {
3840 if ((size - len > 4) && (buf[len - 1] != '.'))
3841 strcat(buf, " ...");
3842 return;
3843 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003844 switch (cur->type) {
3845 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003846 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
3847 if (size - len < xmlStrlen(cur->ns->prefix + 10)) {
3848 if ((size - len > 4) && (buf[len - 1] != '.'))
3849 strcat(buf, " ...");
3850 return;
3851 }
3852 strcat(buf, (char *) cur->ns->prefix);
3853 strcat(buf, ":");
3854 }
Daniel Veillardd3d06722001-08-15 12:06:36 +00003855 if (size - len < xmlStrlen(cur->name + 10)) {
3856 if ((size - len > 4) && (buf[len - 1] != '.'))
3857 strcat(buf, " ...");
3858 return;
3859 }
3860 strcat(buf, (char *) cur->name);
3861 if (cur->next != NULL)
3862 strcat(buf, " ");
3863 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003864 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003865 if (xmlIsBlankNode(cur))
3866 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003867 case XML_CDATA_SECTION_NODE:
3868 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003869 strcat(buf, "CDATA");
3870 if (cur->next != NULL)
3871 strcat(buf, " ");
3872 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003873 case XML_ATTRIBUTE_NODE:
3874 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003875#ifdef LIBXML_DOCB_ENABLED
3876 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003877#endif
3878 case XML_HTML_DOCUMENT_NODE:
3879 case XML_DOCUMENT_TYPE_NODE:
3880 case XML_DOCUMENT_FRAG_NODE:
3881 case XML_NOTATION_NODE:
3882 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003883 strcat(buf, "???");
3884 if (cur->next != NULL)
3885 strcat(buf, " ");
3886 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003887 case XML_ENTITY_NODE:
3888 case XML_PI_NODE:
3889 case XML_DTD_NODE:
3890 case XML_COMMENT_NODE:
3891 case XML_ELEMENT_DECL:
3892 case XML_ATTRIBUTE_DECL:
3893 case XML_ENTITY_DECL:
3894 case XML_XINCLUDE_START:
3895 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003896 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003897 }
3898 cur = cur->next;
3899 }
3900 if (glob) strcat(buf, ")");
3901}
3902
3903/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003904 * xmlValidateElementContent:
3905 * @ctxt: the validation context
3906 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003907 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003908 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003909 *
3910 * Try to validate the content model of an element
3911 *
3912 * returns 1 if valid or 0 if not and -1 in case of error
3913 */
3914
3915static int
3916xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003917 xmlElementPtr elemDecl, int warn) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003918 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003919 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003920 xmlElementContentPtr cont;
3921 const xmlChar *name;
3922
3923 if (elemDecl == NULL)
3924 return(-1);
3925 cont = elemDecl->content;
3926 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003927
3928 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003929 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003930 */
3931 ctxt->vstateMax = 8;
3932 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
3933 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
3934 if (ctxt->vstateTab == NULL) {
3935 xmlGenericError(xmlGenericErrorContext,
3936 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003937 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003938 }
3939 /*
3940 * The first entry in the stack is reserved to the current state
3941 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00003942 ctxt->nodeMax = 0;
3943 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00003944 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003945 ctxt->vstate = &ctxt->vstateTab[0];
3946 ctxt->vstateNr = 1;
3947 CONT = cont;
3948 NODE = child;
3949 DEPTH = 0;
3950 OCCURS = 0;
3951 STATE = 0;
3952 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003953 if ((ret == -3) && (warn)) {
3954 VWARNING(ctxt->userData,
3955 "Element %s content model is ambiguous\n", name);
3956 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003957 /*
3958 * An entities reference appeared at this level.
3959 * Buid a minimal representation of this node content
3960 * sufficient to run the validation process on it
3961 */
3962 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003963 cur = child;
3964 while (cur != NULL) {
3965 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003966 case XML_ENTITY_REF_NODE:
3967 /*
3968 * Push the current node to be able to roll back
3969 * and process within the entity
3970 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003971 if ((cur->children != NULL) &&
3972 (cur->children->children != NULL)) {
3973 nodeVPush(ctxt, cur);
3974 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003975 continue;
3976 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00003977 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003978 case XML_TEXT_NODE:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003979 case XML_CDATA_SECTION_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003980 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003981 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003982 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003983 case XML_ELEMENT_NODE:
3984 /*
3985 * Allocate a new node and minimally fills in
3986 * what's required
3987 */
3988 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3989 if (tmp == NULL) {
3990 xmlGenericError(xmlGenericErrorContext,
3991 "xmlValidateElementContent : malloc failed\n");
3992 xmlFreeNodeList(repl);
3993 ret = -1;
3994 goto done;
3995 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003996 tmp->type = cur->type;
3997 tmp->name = cur->name;
3998 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003999 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00004000 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004001 if (repl == NULL)
4002 repl = last = tmp;
4003 else {
4004 last->next = tmp;
4005 last = tmp;
4006 }
4007 break;
4008 default:
4009 break;
4010 }
4011 /*
4012 * Switch to next element
4013 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004014 cur = cur->next;
4015 while (cur == NULL) {
4016 cur = nodeVPop(ctxt);
4017 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004018 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004019 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004020 }
4021 }
4022
4023 /*
4024 * Relaunch the validation
4025 */
4026 ctxt->vstate = &ctxt->vstateTab[0];
4027 ctxt->vstateNr = 1;
4028 CONT = cont;
4029 NODE = repl;
4030 DEPTH = 0;
4031 OCCURS = 0;
4032 STATE = 0;
4033 ret = xmlValidateElementType(ctxt);
4034 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004035 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004036 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4037 char expr[5000];
4038 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004039
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004040 expr[0] = 0;
4041 xmlSnprintfElementContent(expr, 5000, cont, 1);
4042 list[0] = 0;
4043 if (repl != NULL)
4044 xmlSnprintfElements(list, 5000, repl, 1);
4045 else
4046 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004047
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004048 if (name != NULL) {
4049 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004050 "Element %s content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004051 name, expr, list);
4052 } else {
4053 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004054 "Element content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004055 expr, list);
4056 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004057 } else {
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\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004061 name);
4062 } else {
4063 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004064 "Element content doesn't follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004065 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004066 }
4067 ret = 0;
4068 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004069 if (ret == -3)
4070 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004071
4072
4073done:
4074 /*
4075 * Deallocate the copy if done, and free up the validation stack
4076 */
4077 while (repl != NULL) {
4078 tmp = repl->next;
4079 xmlFree(repl);
4080 repl = tmp;
4081 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004082 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004083 if (ctxt->vstateTab != NULL) {
4084 xmlFree(ctxt->vstateTab);
4085 ctxt->vstateTab = NULL;
4086 }
4087 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004088 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004089 if (ctxt->nodeTab != NULL) {
4090 xmlFree(ctxt->nodeTab);
4091 ctxt->nodeTab = NULL;
4092 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004093 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004094
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004095}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004096
Owen Taylor3473f882001-02-23 17:55:21 +00004097/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004098 * xmlValidateCdataElement:
4099 * @ctxt: the validation context
4100 * @doc: a document instance
4101 * @elem: an element instance
4102 *
4103 * Check that an element follows #CDATA
4104 *
4105 * returns 1 if valid or 0 otherwise
4106 */
4107static int
4108xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4109 xmlNodePtr elem) {
4110 int ret = 1;
4111 xmlNodePtr cur, child;
4112
4113 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4114 return(0);
4115
4116 child = elem->children;
4117
4118 cur = child;
4119 while (cur != NULL) {
4120 switch (cur->type) {
4121 case XML_ENTITY_REF_NODE:
4122 /*
4123 * Push the current node to be able to roll back
4124 * and process within the entity
4125 */
4126 if ((cur->children != NULL) &&
4127 (cur->children->children != NULL)) {
4128 nodeVPush(ctxt, cur);
4129 cur = cur->children->children;
4130 continue;
4131 }
4132 break;
4133 case XML_COMMENT_NODE:
4134 case XML_PI_NODE:
4135 case XML_TEXT_NODE:
4136 case XML_CDATA_SECTION_NODE:
4137 break;
4138 default:
4139 ret = 0;
4140 goto done;
4141 }
4142 /*
4143 * Switch to next element
4144 */
4145 cur = cur->next;
4146 while (cur == NULL) {
4147 cur = nodeVPop(ctxt);
4148 if (cur == NULL)
4149 break;
4150 cur = cur->next;
4151 }
4152 }
4153done:
4154 ctxt->nodeMax = 0;
4155 ctxt->nodeNr = 0;
4156 if (ctxt->nodeTab != NULL) {
4157 xmlFree(ctxt->nodeTab);
4158 ctxt->nodeTab = NULL;
4159 }
4160 return(ret);
4161}
4162
4163/**
Owen Taylor3473f882001-02-23 17:55:21 +00004164 * xmlValidateOneElement:
4165 * @ctxt: the validation context
4166 * @doc: a document instance
4167 * @elem: an element instance
4168 *
4169 * Try to validate a single element and it's attributes,
4170 * basically it does the following checks as described by the
4171 * XML-1.0 recommendation:
4172 * - [ VC: Element Valid ]
4173 * - [ VC: Required Attribute ]
4174 * Then call xmlValidateOneAttribute() for each attribute present.
4175 *
4176 * The ID/IDREF checkings are done separately
4177 *
4178 * returns 1 if valid or 0 otherwise
4179 */
4180
4181int
4182xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4183 xmlNodePtr elem) {
4184 xmlElementPtr elemDecl = NULL;
4185 xmlElementContentPtr cont;
4186 xmlAttributePtr attr;
4187 xmlNodePtr child;
4188 int ret = 1;
4189 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004190 const xmlChar *prefix = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004191
4192 CHECK_DTD;
4193
4194 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004195 switch (elem->type) {
4196 case XML_ATTRIBUTE_NODE:
4197 VERROR(ctxt->userData,
4198 "Attribute element not expected here\n");
4199 return(0);
4200 case XML_TEXT_NODE:
4201 if (elem->children != NULL) {
4202 VERROR(ctxt->userData, "Text element has childs !\n");
4203 return(0);
4204 }
4205 if (elem->properties != NULL) {
4206 VERROR(ctxt->userData, "Text element has attributes !\n");
4207 return(0);
4208 }
4209 if (elem->ns != NULL) {
4210 VERROR(ctxt->userData, "Text element has namespace !\n");
4211 return(0);
4212 }
4213 if (elem->nsDef != NULL) {
4214 VERROR(ctxt->userData,
4215 "Text element carries namespace definitions !\n");
4216 return(0);
4217 }
4218 if (elem->content == NULL) {
4219 VERROR(ctxt->userData,
4220 "Text element has no content !\n");
4221 return(0);
4222 }
4223 return(1);
4224 case XML_XINCLUDE_START:
4225 case XML_XINCLUDE_END:
4226 return(1);
4227 case XML_CDATA_SECTION_NODE:
4228 case XML_ENTITY_REF_NODE:
4229 case XML_PI_NODE:
4230 case XML_COMMENT_NODE:
4231 return(1);
4232 case XML_ENTITY_NODE:
4233 VERROR(ctxt->userData,
4234 "Entity element not expected here\n");
4235 return(0);
4236 case XML_NOTATION_NODE:
4237 VERROR(ctxt->userData,
4238 "Notation element not expected here\n");
4239 return(0);
4240 case XML_DOCUMENT_NODE:
4241 case XML_DOCUMENT_TYPE_NODE:
4242 case XML_DOCUMENT_FRAG_NODE:
4243 VERROR(ctxt->userData,
4244 "Document element not expected here\n");
4245 return(0);
4246 case XML_HTML_DOCUMENT_NODE:
4247 VERROR(ctxt->userData,
4248 "\n");
4249 return(0);
4250 case XML_ELEMENT_NODE:
4251 break;
4252 default:
4253 VERROR(ctxt->userData,
4254 "unknown element type %d\n", elem->type);
4255 return(0);
4256 }
4257 if (elem->name == NULL) return(0);
4258
4259 /*
4260 * Fetch the declaration for the qualified name
4261 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004262 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4263 prefix = elem->ns->prefix;
4264
4265 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004266 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004267 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004268 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4269 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004270 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004271 }
4272
4273 /*
4274 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004275 * This is "non-strict" validation should be done on the
4276 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004277 */
4278 if (elemDecl == NULL) {
4279 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4280 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4281 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4282 }
4283 if (elemDecl == NULL) {
4284 VERROR(ctxt->userData, "No declaration for element %s\n",
4285 elem->name);
4286 return(0);
4287 }
4288
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004289 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00004290 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004291 case XML_ELEMENT_TYPE_UNDEFINED:
4292 VERROR(ctxt->userData, "No declaration for element %s\n",
4293 elem->name);
4294 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004295 case XML_ELEMENT_TYPE_EMPTY:
4296 if (elem->children != NULL) {
4297 VERROR(ctxt->userData,
4298 "Element %s was declared EMPTY this one has content\n",
4299 elem->name);
4300 ret = 0;
4301 }
4302 break;
4303 case XML_ELEMENT_TYPE_ANY:
4304 /* I don't think anything is required then */
4305 break;
4306 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004307 /* simple case of declared as #PCDATA */
4308 if ((elemDecl->content != NULL) &&
4309 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4310 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4311 if (!ret) {
4312 VERROR(ctxt->userData,
4313 "Element %s was declared #PCDATA but contains non text nodes\n",
4314 elem->name);
4315 }
4316 break;
4317 }
Owen Taylor3473f882001-02-23 17:55:21 +00004318 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004319 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004320 while (child != NULL) {
4321 if (child->type == XML_ELEMENT_NODE) {
4322 name = child->name;
4323 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4324 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004325 snprintf((char *) qname, sizeof(qname), "%s:%s",
4326 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004327 qname[sizeof(qname) - 1] = 0;
4328 cont = elemDecl->content;
4329 while (cont != NULL) {
4330 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4331 if (xmlStrEqual(cont->name, qname)) break;
4332 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4333 (cont->c1 != NULL) &&
4334 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4335 if (xmlStrEqual(cont->c1->name, qname)) break;
4336 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4337 (cont->c1 == NULL) ||
4338 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4339 /* Internal error !!! */
4340 xmlGenericError(xmlGenericErrorContext,
4341 "Internal: MIXED struct bad\n");
4342 break;
4343 }
4344 cont = cont->c2;
4345 }
4346 if (cont != NULL)
4347 goto child_ok;
4348 }
4349 cont = elemDecl->content;
4350 while (cont != NULL) {
4351 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4352 if (xmlStrEqual(cont->name, name)) break;
4353 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4354 (cont->c1 != NULL) &&
4355 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4356 if (xmlStrEqual(cont->c1->name, name)) break;
4357 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4358 (cont->c1 == NULL) ||
4359 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4360 /* Internal error !!! */
4361 xmlGenericError(xmlGenericErrorContext,
4362 "Internal: MIXED struct bad\n");
4363 break;
4364 }
4365 cont = cont->c2;
4366 }
4367 if (cont == NULL) {
4368 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00004369 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004370 name, elem->name);
4371 ret = 0;
4372 }
4373 }
4374child_ok:
4375 child = child->next;
4376 }
4377 break;
4378 case XML_ELEMENT_TYPE_ELEMENT:
4379 child = elem->children;
4380 cont = elemDecl->content;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004381 ret = xmlValidateElementContent(ctxt, child, elemDecl, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00004382 break;
4383 }
4384
4385 /* [ VC: Required Attribute ] */
4386 attr = elemDecl->attributes;
4387 while (attr != NULL) {
4388 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004389 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00004390
Daniel Veillarde4301c82002-02-13 13:32:35 +00004391 if ((attr->prefix == NULL) &&
4392 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4393 xmlNsPtr ns;
4394
4395 ns = elem->nsDef;
4396 while (ns != NULL) {
4397 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004398 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004399 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00004400 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004401 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4402 xmlNsPtr ns;
4403
4404 ns = elem->nsDef;
4405 while (ns != NULL) {
4406 if (xmlStrEqual(attr->name, ns->prefix))
4407 goto found;
4408 ns = ns->next;
4409 }
4410 } else {
4411 xmlAttrPtr attrib;
4412
4413 attrib = elem->properties;
4414 while (attrib != NULL) {
4415 if (xmlStrEqual(attrib->name, attr->name)) {
4416 if (attr->prefix != NULL) {
4417 xmlNsPtr nameSpace = attrib->ns;
4418
4419 if (nameSpace == NULL)
4420 nameSpace = elem->ns;
4421 /*
4422 * qualified names handling is problematic, having a
4423 * different prefix should be possible but DTDs don't
4424 * allow to define the URI instead of the prefix :-(
4425 */
4426 if (nameSpace == NULL) {
4427 if (qualified < 0)
4428 qualified = 0;
4429 } else if (!xmlStrEqual(nameSpace->prefix,
4430 attr->prefix)) {
4431 if (qualified < 1)
4432 qualified = 1;
4433 } else
4434 goto found;
4435 } else {
4436 /*
4437 * We should allow applications to define namespaces
4438 * for their application even if the DTD doesn't
4439 * carry one, otherwise, basically we would always
4440 * break.
4441 */
4442 goto found;
4443 }
4444 }
4445 attrib = attrib->next;
4446 }
Owen Taylor3473f882001-02-23 17:55:21 +00004447 }
4448 if (qualified == -1) {
4449 if (attr->prefix == NULL) {
4450 VERROR(ctxt->userData,
4451 "Element %s doesn't carry attribute %s\n",
4452 elem->name, attr->name);
4453 ret = 0;
4454 } else {
4455 VERROR(ctxt->userData,
4456 "Element %s doesn't carry attribute %s:%s\n",
4457 elem->name, attr->prefix,attr->name);
4458 ret = 0;
4459 }
4460 } else if (qualified == 0) {
4461 VWARNING(ctxt->userData,
4462 "Element %s required attribute %s:%s has no prefix\n",
4463 elem->name, attr->prefix,attr->name);
4464 } else if (qualified == 1) {
4465 VWARNING(ctxt->userData,
4466 "Element %s required attribute %s:%s has different prefix\n",
4467 elem->name, attr->prefix,attr->name);
4468 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004469 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
4470 /*
4471 * Special tests checking #FIXED namespace declarations
4472 * have the right value since this is not done as an
4473 * attribute checking
4474 */
4475 if ((attr->prefix == NULL) &&
4476 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4477 xmlNsPtr ns;
4478
4479 ns = elem->nsDef;
4480 while (ns != NULL) {
4481 if (ns->prefix == NULL) {
4482 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4483 VERROR(ctxt->userData,
4484 "Element %s namespace name for default namespace does not match the DTD\n",
4485 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00004486 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004487 }
4488 goto found;
4489 }
4490 ns = ns->next;
4491 }
4492 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4493 xmlNsPtr ns;
4494
4495 ns = elem->nsDef;
4496 while (ns != NULL) {
4497 if (xmlStrEqual(attr->name, ns->prefix)) {
4498 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4499 VERROR(ctxt->userData,
4500 "Element %s namespace name for %s doesn't match the DTD\n",
4501 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00004502 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004503 }
4504 goto found;
4505 }
4506 ns = ns->next;
4507 }
4508 }
Owen Taylor3473f882001-02-23 17:55:21 +00004509 }
4510found:
4511 attr = attr->nexth;
4512 }
4513 return(ret);
4514}
4515
4516/**
4517 * xmlValidateRoot:
4518 * @ctxt: the validation context
4519 * @doc: a document instance
4520 *
4521 * Try to validate a the root element
4522 * basically it does the following check as described by the
4523 * XML-1.0 recommendation:
4524 * - [ VC: Root Element Type ]
4525 * it doesn't try to recurse or apply other check to the element
4526 *
4527 * returns 1 if valid or 0 otherwise
4528 */
4529
4530int
4531xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4532 xmlNodePtr root;
4533 if (doc == NULL) return(0);
4534
4535 root = xmlDocGetRootElement(doc);
4536 if ((root == NULL) || (root->name == NULL)) {
4537 VERROR(ctxt->userData, "Not valid: no root element\n");
4538 return(0);
4539 }
4540
4541 /*
4542 * When doing post validation against a separate DTD, those may
4543 * no internal subset has been generated
4544 */
4545 if ((doc->intSubset != NULL) &&
4546 (doc->intSubset->name != NULL)) {
4547 /*
4548 * Check first the document root against the NQName
4549 */
4550 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4551 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4552 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004553 snprintf((char *) qname, sizeof(qname), "%s:%s",
4554 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004555 qname[sizeof(qname) - 1] = 0;
4556 if (xmlStrEqual(doc->intSubset->name, qname))
4557 goto name_ok;
4558 }
4559 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4560 (xmlStrEqual(root->name, BAD_CAST "html")))
4561 goto name_ok;
4562 VERROR(ctxt->userData,
4563 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4564 root->name, doc->intSubset->name);
4565 return(0);
4566
4567 }
4568 }
4569name_ok:
4570 return(1);
4571}
4572
4573
4574/**
4575 * xmlValidateElement:
4576 * @ctxt: the validation context
4577 * @doc: a document instance
4578 * @elem: an element instance
4579 *
4580 * Try to validate the subtree under an element
4581 *
4582 * returns 1 if valid or 0 otherwise
4583 */
4584
4585int
4586xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4587 xmlNodePtr child;
4588 xmlAttrPtr attr;
4589 xmlChar *value;
4590 int ret = 1;
4591
4592 if (elem == NULL) return(0);
4593
4594 /*
4595 * XInclude elements were added after parsing in the infoset,
4596 * they don't really mean anything validation wise.
4597 */
4598 if ((elem->type == XML_XINCLUDE_START) ||
4599 (elem->type == XML_XINCLUDE_END))
4600 return(1);
4601
4602 CHECK_DTD;
4603
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004604 /*
4605 * Entities references have to be handled separately
4606 */
4607 if (elem->type == XML_ENTITY_REF_NODE) {
4608 return(1);
4609 }
4610
Owen Taylor3473f882001-02-23 17:55:21 +00004611 ret &= xmlValidateOneElement(ctxt, doc, elem);
4612 attr = elem->properties;
4613 while(attr != NULL) {
4614 value = xmlNodeListGetString(doc, attr->children, 0);
4615 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4616 if (value != NULL)
4617 xmlFree(value);
4618 attr= attr->next;
4619 }
4620 child = elem->children;
4621 while (child != NULL) {
4622 ret &= xmlValidateElement(ctxt, doc, child);
4623 child = child->next;
4624 }
4625
4626 return(ret);
4627}
4628
Daniel Veillard8730c562001-02-26 10:49:57 +00004629/**
4630 * xmlValidateRef:
4631 * @ref: A reference to be validated
4632 * @ctxt: Validation context
4633 * @name: Name of ID we are searching for
4634 *
4635 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004636static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004637xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004638 const xmlChar *name) {
4639 xmlAttrPtr id;
4640 xmlAttrPtr attr;
4641
4642 if (ref == NULL)
4643 return;
4644 attr = ref->attr;
4645 if (attr == NULL)
4646 return;
4647 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4648 id = xmlGetID(ctxt->doc, name);
4649 if (id == NULL) {
4650 VERROR(ctxt->userData,
4651 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4652 attr->name, name);
4653 ctxt->valid = 0;
4654 }
4655 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4656 xmlChar *dup, *str = NULL, *cur, save;
4657
4658 dup = xmlStrdup(name);
4659 if (dup == NULL) {
4660 ctxt->valid = 0;
4661 return;
4662 }
4663 cur = dup;
4664 while (*cur != 0) {
4665 str = cur;
4666 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4667 save = *cur;
4668 *cur = 0;
4669 id = xmlGetID(ctxt->doc, str);
4670 if (id == NULL) {
4671 VERROR(ctxt->userData,
4672 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4673 attr->name, str);
4674 ctxt->valid = 0;
4675 }
4676 if (save == 0)
4677 break;
4678 *cur = save;
4679 while (IS_BLANK(*cur)) cur++;
4680 }
4681 xmlFree(dup);
4682 }
4683}
4684
4685/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004686 * xmlWalkValidateList:
4687 * @data: Contents of current link
4688 * @user: Value supplied by the user
4689 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004690 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00004691 */
4692static int
4693xmlWalkValidateList(const void *data, const void *user)
4694{
4695 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4696 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4697 return 1;
4698}
4699
4700/**
4701 * xmlValidateCheckRefCallback:
4702 * @ref_list: List of references
4703 * @ctxt: Validation context
4704 * @name: Name of ID we are searching for
4705 *
4706 */
4707static void
4708xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4709 const xmlChar *name) {
4710 xmlValidateMemo memo;
4711
4712 if (ref_list == NULL)
4713 return;
4714 memo.ctxt = ctxt;
4715 memo.name = name;
4716
4717 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4718
4719}
4720
4721/**
Owen Taylor3473f882001-02-23 17:55:21 +00004722 * xmlValidateDocumentFinal:
4723 * @ctxt: the validation context
4724 * @doc: a document instance
4725 *
4726 * Does the final step for the document validation once all the
4727 * incremental validation steps have been completed
4728 *
4729 * basically it does the following checks described by the XML Rec
4730 *
4731 *
4732 * returns 1 if valid or 0 otherwise
4733 */
4734
4735int
4736xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4737 xmlRefTablePtr table;
4738
4739 if (doc == NULL) {
4740 xmlGenericError(xmlGenericErrorContext,
4741 "xmlValidateDocumentFinal: doc == NULL\n");
4742 return(0);
4743 }
4744
4745 /*
4746 * Check all the NOTATION/NOTATIONS attributes
4747 */
4748 /*
4749 * Check all the ENTITY/ENTITIES attributes definition for validity
4750 */
4751 /*
4752 * Check all the IDREF/IDREFS attributes definition for validity
4753 */
4754 table = (xmlRefTablePtr) doc->refs;
4755 ctxt->doc = doc;
4756 ctxt->valid = 1;
4757 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4758 return(ctxt->valid);
4759}
4760
4761/**
4762 * xmlValidateDtd:
4763 * @ctxt: the validation context
4764 * @doc: a document instance
4765 * @dtd: a dtd instance
4766 *
4767 * Try to validate the document against the dtd instance
4768 *
4769 * basically it does check all the definitions in the DtD.
4770 *
4771 * returns 1 if valid or 0 otherwise
4772 */
4773
4774int
4775xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4776 int ret;
4777 xmlDtdPtr oldExt;
4778 xmlNodePtr root;
4779
4780 if (dtd == NULL) return(0);
4781 if (doc == NULL) return(0);
4782 oldExt = doc->extSubset;
4783 doc->extSubset = dtd;
4784 ret = xmlValidateRoot(ctxt, doc);
4785 if (ret == 0) {
4786 doc->extSubset = oldExt;
4787 return(ret);
4788 }
4789 if (doc->ids != NULL) {
4790 xmlFreeIDTable(doc->ids);
4791 doc->ids = NULL;
4792 }
4793 if (doc->refs != NULL) {
4794 xmlFreeRefTable(doc->refs);
4795 doc->refs = NULL;
4796 }
4797 root = xmlDocGetRootElement(doc);
4798 ret = xmlValidateElement(ctxt, doc, root);
4799 ret &= xmlValidateDocumentFinal(ctxt, doc);
4800 doc->extSubset = oldExt;
4801 return(ret);
4802}
4803
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004804static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004805xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
4806 const xmlChar *name ATTRIBUTE_UNUSED) {
4807 if (cur == NULL)
4808 return;
4809 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
4810 xmlChar *notation = cur->content;
4811
Daniel Veillard878eab02002-02-19 13:46:09 +00004812 if (notation != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004813 int ret;
4814
4815 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
4816 if (ret != 1) {
Daniel Veillard878eab02002-02-19 13:46:09 +00004817 ctxt->valid = 0;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004818 }
4819 }
4820 }
4821}
4822
4823static void
Owen Taylor3473f882001-02-23 17:55:21 +00004824xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004825 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004826 int ret;
Daniel Veillard878eab02002-02-19 13:46:09 +00004827 xmlDocPtr doc;
4828 xmlElementPtr elem;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004829
Owen Taylor3473f882001-02-23 17:55:21 +00004830 if (cur == NULL)
4831 return;
4832 switch (cur->atype) {
4833 case XML_ATTRIBUTE_CDATA:
4834 case XML_ATTRIBUTE_ID:
4835 case XML_ATTRIBUTE_IDREF :
4836 case XML_ATTRIBUTE_IDREFS:
4837 case XML_ATTRIBUTE_NMTOKEN:
4838 case XML_ATTRIBUTE_NMTOKENS:
4839 case XML_ATTRIBUTE_ENUMERATION:
4840 break;
4841 case XML_ATTRIBUTE_ENTITY:
4842 case XML_ATTRIBUTE_ENTITIES:
4843 case XML_ATTRIBUTE_NOTATION:
4844 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004845
4846 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
4847 cur->atype, cur->defaultValue);
4848 if ((ret == 0) && (ctxt->valid == 1))
4849 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004850 }
4851 if (cur->tree != NULL) {
4852 xmlEnumerationPtr tree = cur->tree;
4853 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004854 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00004855 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004856 if ((ret == 0) && (ctxt->valid == 1))
4857 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004858 tree = tree->next;
4859 }
4860 }
4861 }
Daniel Veillard878eab02002-02-19 13:46:09 +00004862 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
4863 doc = cur->doc;
4864 if ((doc == NULL) || (cur->elem == NULL)) {
4865 VERROR(ctxt->userData,
4866 "xmlValidateAttributeCallback(%s): internal error\n",
4867 cur->name);
4868 return;
4869 }
4870 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
4871 if (elem == NULL)
4872 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
4873 if (elem == NULL) {
4874 VERROR(ctxt->userData,
4875 "attribute %s: could not find decl for element %s\n",
4876 cur->name, cur->elem);
4877 return;
4878 }
4879 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
4880 VERROR(ctxt->userData,
4881 "NOTATION attribute %s declared on EMPTY element %s\n",
4882 cur->name, cur->elem);
4883 ctxt->valid = 0;
4884 }
4885 }
Owen Taylor3473f882001-02-23 17:55:21 +00004886}
4887
4888/**
4889 * xmlValidateDtdFinal:
4890 * @ctxt: the validation context
4891 * @doc: a document instance
4892 *
4893 * Does the final step for the dtds validation once all the
4894 * subsets have been parsed
4895 *
4896 * basically it does the following checks described by the XML Rec
4897 * - check that ENTITY and ENTITIES type attributes default or
4898 * possible values matches one of the defined entities.
4899 * - check that NOTATION type attributes default or
4900 * possible values matches one of the defined notations.
4901 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004902 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00004903 */
4904
4905int
4906xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00004907 xmlDtdPtr dtd;
4908 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004909 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00004910
4911 if (doc == NULL) return(0);
4912 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4913 return(0);
4914 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004915 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00004916 dtd = doc->intSubset;
4917 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4918 table = (xmlAttributeTablePtr) dtd->attributes;
4919 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard878eab02002-02-19 13:46:09 +00004920 }
4921 if ((dtd != NULL) && (dtd->entities != NULL)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004922 entities = (xmlEntitiesTablePtr) dtd->entities;
4923 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
4924 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004925 }
4926 dtd = doc->extSubset;
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 return(ctxt->valid);
4937}
4938
4939/**
4940 * xmlValidateDocument:
4941 * @ctxt: the validation context
4942 * @doc: a document instance
4943 *
4944 * Try to validate the document instance
4945 *
4946 * basically it does the all the checks described by the XML Rec
4947 * i.e. validates the internal and external subset (if present)
4948 * and validate the document tree.
4949 *
4950 * returns 1 if valid or 0 otherwise
4951 */
4952
4953int
4954xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4955 int ret;
4956 xmlNodePtr root;
4957
4958 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4959 return(0);
4960 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
4961 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
4962 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
4963 doc->intSubset->SystemID);
4964 if (doc->extSubset == NULL) {
4965 if (doc->intSubset->SystemID != NULL) {
4966 VERROR(ctxt->userData,
4967 "Could not load the external subset \"%s\"\n",
4968 doc->intSubset->SystemID);
4969 } else {
4970 VERROR(ctxt->userData,
4971 "Could not load the external subset \"%s\"\n",
4972 doc->intSubset->ExternalID);
4973 }
4974 return(0);
4975 }
4976 }
4977
4978 if (doc->ids != NULL) {
4979 xmlFreeIDTable(doc->ids);
4980 doc->ids = NULL;
4981 }
4982 if (doc->refs != NULL) {
4983 xmlFreeRefTable(doc->refs);
4984 doc->refs = NULL;
4985 }
4986 ret = xmlValidateDtdFinal(ctxt, doc);
4987 if (!xmlValidateRoot(ctxt, doc)) return(0);
4988
4989 root = xmlDocGetRootElement(doc);
4990 ret &= xmlValidateElement(ctxt, doc, root);
4991 ret &= xmlValidateDocumentFinal(ctxt, doc);
4992 return(ret);
4993}
4994
4995
4996/************************************************************************
4997 * *
4998 * Routines for dynamic validation editing *
4999 * *
5000 ************************************************************************/
5001
5002/**
5003 * xmlValidGetPotentialChildren:
5004 * @ctree: an element content tree
5005 * @list: an array to store the list of child names
5006 * @len: a pointer to the number of element in the list
5007 * @max: the size of the array
5008 *
5009 * Build/extend a list of potential children allowed by the content tree
5010 *
5011 * returns the number of element in the list, or -1 in case of error.
5012 */
5013
5014int
5015xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
5016 int *len, int max) {
5017 int i;
5018
5019 if ((ctree == NULL) || (list == NULL) || (len == NULL))
5020 return(-1);
5021 if (*len >= max) return(*len);
5022
5023 switch (ctree->type) {
5024 case XML_ELEMENT_CONTENT_PCDATA:
5025 for (i = 0; i < *len;i++)
5026 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
5027 list[(*len)++] = BAD_CAST "#PCDATA";
5028 break;
5029 case XML_ELEMENT_CONTENT_ELEMENT:
5030 for (i = 0; i < *len;i++)
5031 if (xmlStrEqual(ctree->name, list[i])) return(*len);
5032 list[(*len)++] = ctree->name;
5033 break;
5034 case XML_ELEMENT_CONTENT_SEQ:
5035 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5036 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5037 break;
5038 case XML_ELEMENT_CONTENT_OR:
5039 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
5040 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
5041 break;
5042 }
5043
5044 return(*len);
5045}
5046
5047/**
5048 * xmlValidGetValidElements:
5049 * @prev: an element to insert after
5050 * @next: an element to insert next
5051 * @list: an array to store the list of child names
5052 * @max: the size of the array
5053 *
5054 * This function returns the list of authorized children to insert
5055 * within an existing tree while respecting the validity constraints
5056 * forced by the Dtd. The insertion point is defined using @prev and
5057 * @next in the following ways:
5058 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
5059 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
5060 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
5061 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
5062 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
5063 *
5064 * pointers to the element names are inserted at the beginning of the array
5065 * and do not need to be freed.
5066 *
5067 * returns the number of element in the list, or -1 in case of error. If
5068 * the function returns the value @max the caller is invited to grow the
5069 * receiving array and retry.
5070 */
5071
5072int
5073xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
5074 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005075 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00005076 int nb_valid_elements = 0;
5077 const xmlChar *elements[256];
5078 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005079 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005080
5081 xmlNode *ref_node;
5082 xmlNode *parent;
5083 xmlNode *test_node;
5084
5085 xmlNode *prev_next;
5086 xmlNode *next_prev;
5087 xmlNode *parent_childs;
5088 xmlNode *parent_last;
5089
5090 xmlElement *element_desc;
5091
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005092 vctxt.userData = NULL;
5093 vctxt.error = NULL;
5094 vctxt.warning = NULL;
5095
Owen Taylor3473f882001-02-23 17:55:21 +00005096 if (prev == NULL && next == NULL)
5097 return(-1);
5098
5099 if (list == NULL) return(-1);
5100 if (max <= 0) return(-1);
5101
5102 nb_valid_elements = 0;
5103 ref_node = prev ? prev : next;
5104 parent = ref_node->parent;
5105
5106 /*
5107 * Retrieves the parent element declaration
5108 */
5109 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
5110 parent->name);
5111 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
5112 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
5113 parent->name);
5114 if (element_desc == NULL) return(-1);
5115
5116 /*
5117 * Do a backup of the current tree structure
5118 */
5119 prev_next = prev ? prev->next : NULL;
5120 next_prev = next ? next->prev : NULL;
5121 parent_childs = parent->children;
5122 parent_last = parent->last;
5123
5124 /*
5125 * Creates a dummy node and insert it into the tree
5126 */
5127 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
5128 test_node->doc = ref_node->doc;
5129 test_node->parent = parent;
5130 test_node->prev = prev;
5131 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00005132 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00005133
5134 if (prev) prev->next = test_node;
5135 else parent->children = test_node;
5136
5137 if (next) next->prev = test_node;
5138 else parent->last = test_node;
5139
5140 /*
5141 * Insert each potential child node and check if the parent is
5142 * still valid
5143 */
5144 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
5145 elements, &nb_elements, 256);
5146
5147 for (i = 0;i < nb_elements;i++) {
5148 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005149 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005150 int j;
5151
5152 for (j = 0; j < nb_valid_elements;j++)
5153 if (xmlStrEqual(elements[i], list[j])) break;
5154 list[nb_valid_elements++] = elements[i];
5155 if (nb_valid_elements >= max) break;
5156 }
5157 }
5158
5159 /*
5160 * Restore the tree structure
5161 */
5162 if (prev) prev->next = prev_next;
5163 if (next) next->prev = next_prev;
5164 parent->children = parent_childs;
5165 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00005166
5167 /*
5168 * Free up the dummy node
5169 */
5170 test_node->name = name;
5171 xmlFreeNode(test_node);
5172
Owen Taylor3473f882001-02-23 17:55:21 +00005173 return(nb_valid_elements);
5174}