blob: de596e41ce544806c71a1065c691d1bcd7bc9319 [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);
2895 if (ent == NULL) {
2896 VERROR(ctxt->userData,
2897 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2898 name, value);
2899 ret = 0;
2900 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2901 VERROR(ctxt->userData,
2902 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2903 name, value);
2904 ret = 0;
2905 }
2906 break;
2907 }
2908 case XML_ATTRIBUTE_ENTITIES: {
2909 xmlChar *dup, *nam = NULL, *cur, save;
2910 xmlEntityPtr ent;
2911
2912 dup = xmlStrdup(value);
2913 if (dup == NULL)
2914 return(0);
2915 cur = dup;
2916 while (*cur != 0) {
2917 nam = cur;
2918 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2919 save = *cur;
2920 *cur = 0;
2921 ent = xmlGetDocEntity(doc, nam);
2922 if (ent == NULL) {
2923 VERROR(ctxt->userData,
2924 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
2925 name, nam);
2926 ret = 0;
2927 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2928 VERROR(ctxt->userData,
2929 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
2930 name, nam);
2931 ret = 0;
2932 }
2933 if (save == 0)
2934 break;
2935 *cur = save;
2936 while (IS_BLANK(*cur)) cur++;
2937 }
2938 xmlFree(dup);
2939 break;
2940 }
2941 case XML_ATTRIBUTE_NOTATION: {
2942 xmlNotationPtr nota;
2943
2944 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
2945 if ((nota == NULL) && (doc->extSubset != NULL))
2946 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
2947
2948 if (nota == NULL) {
2949 VERROR(ctxt->userData,
2950 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
2951 name, value);
2952 ret = 0;
2953 }
2954 break;
2955 }
2956 }
2957 return(ret);
2958}
2959
2960/**
2961 * xmlValidNormalizeAttributeValue:
2962 * @doc: the document
2963 * @elem: the parent
2964 * @name: the attribute name
2965 * @value: the attribute value
2966 *
2967 * Does the validation related extra step of the normalization of attribute
2968 * values:
2969 *
2970 * If the declared value is not CDATA, then the XML processor must further
2971 * process the normalized attribute value by discarding any leading and
2972 * trailing space (#x20) characters, and by replacing sequences of space
2973 * (#x20) characters by single space (#x20) character.
2974 *
2975 * returns a new normalized string if normalization is needed, NULL otherwise
2976 * the caller must free the returned value.
2977 */
2978
2979xmlChar *
2980xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
2981 const xmlChar *name, const xmlChar *value) {
2982 xmlChar *ret, *dst;
2983 const xmlChar *src;
2984 xmlAttributePtr attrDecl = NULL;
2985
2986 if (doc == NULL) return(NULL);
2987 if (elem == NULL) return(NULL);
2988 if (name == NULL) return(NULL);
2989 if (value == NULL) return(NULL);
2990
2991 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
2992 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002993 snprintf((char *) qname, sizeof(qname), "%s:%s",
2994 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002995 qname[sizeof(qname) - 1] = 0;
2996 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
2997 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2998 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
2999 }
3000 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3001 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3002 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3003
3004 if (attrDecl == NULL)
3005 return(NULL);
3006 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3007 return(NULL);
3008
3009 ret = xmlStrdup(value);
3010 if (ret == NULL)
3011 return(NULL);
3012 src = value;
3013 dst = ret;
3014 while (*src == 0x20) src++;
3015 while (*src != 0) {
3016 if (*src == 0x20) {
3017 while (*src == 0x20) src++;
3018 if (*src != 0)
3019 *dst++ = 0x20;
3020 } else {
3021 *dst++ = *src++;
3022 }
3023 }
3024 *dst = 0;
3025 return(ret);
3026}
3027
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003028static void
Owen Taylor3473f882001-02-23 17:55:21 +00003029xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003030 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003031 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3032}
3033
3034/**
3035 * xmlValidateAttributeDecl:
3036 * @ctxt: the validation context
3037 * @doc: a document instance
3038 * @attr: an attribute definition
3039 *
3040 * Try to validate a single attribute definition
3041 * basically it does the following checks as described by the
3042 * XML-1.0 recommendation:
3043 * - [ VC: Attribute Default Legal ]
3044 * - [ VC: Enumeration ]
3045 * - [ VC: ID Attribute Default ]
3046 *
3047 * The ID/IDREF uniqueness and matching are done separately
3048 *
3049 * returns 1 if valid or 0 otherwise
3050 */
3051
3052int
3053xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3054 xmlAttributePtr attr) {
3055 int ret = 1;
3056 int val;
3057 CHECK_DTD;
3058 if(attr == NULL) return(1);
3059
3060 /* Attribute Default Legal */
3061 /* Enumeration */
3062 if (attr->defaultValue != NULL) {
3063 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3064 if (val == 0) {
3065 VERROR(ctxt->userData,
3066 "Syntax of default value for attribute %s on %s is not valid\n",
3067 attr->name, attr->elem);
3068 }
3069 ret &= val;
3070 }
3071
3072 /* ID Attribute Default */
3073 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3074 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3075 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3076 VERROR(ctxt->userData,
3077 "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n",
3078 attr->name, attr->elem);
3079 ret = 0;
3080 }
3081
3082 /* One ID per Element Type */
3083 if (attr->atype == XML_ATTRIBUTE_ID) {
3084 int nbId;
3085
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003086 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003087 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3088 attr->elem);
3089 if (elem != NULL) {
3090 nbId = xmlScanIDAttributeDecl(NULL, elem);
3091 } else {
3092 xmlAttributeTablePtr table;
3093
3094 /*
3095 * The attribute may be declared in the internal subset and the
3096 * element in the external subset.
3097 */
3098 nbId = 0;
3099 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3100 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3101 xmlValidateAttributeIdCallback, &nbId);
3102 }
3103 if (nbId > 1) {
3104 VERROR(ctxt->userData,
3105 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3106 attr->elem, nbId, attr->name);
3107 } else if (doc->extSubset != NULL) {
3108 int extId = 0;
3109 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3110 if (elem != NULL) {
3111 extId = xmlScanIDAttributeDecl(NULL, elem);
3112 }
3113 if (extId > 1) {
3114 VERROR(ctxt->userData,
3115 "Element %s has %d ID attribute defined in the external subset : %s\n",
3116 attr->elem, extId, attr->name);
3117 } else if (extId + nbId > 1) {
3118 VERROR(ctxt->userData,
3119"Element %s has ID attributes defined in the internal and external subset : %s\n",
3120 attr->elem, attr->name);
3121 }
3122 }
3123 }
3124
3125 /* Validity Constraint: Enumeration */
3126 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3127 xmlEnumerationPtr tree = attr->tree;
3128 while (tree != NULL) {
3129 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3130 tree = tree->next;
3131 }
3132 if (tree == NULL) {
3133 VERROR(ctxt->userData,
3134"Default value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3135 attr->defaultValue, attr->name, attr->elem);
3136 ret = 0;
3137 }
3138 }
3139
3140 return(ret);
3141}
3142
3143/**
3144 * xmlValidateElementDecl:
3145 * @ctxt: the validation context
3146 * @doc: a document instance
3147 * @elem: an element definition
3148 *
3149 * Try to validate a single element definition
3150 * basically it does the following checks as described by the
3151 * XML-1.0 recommendation:
3152 * - [ VC: One ID per Element Type ]
3153 * - [ VC: No Duplicate Types ]
3154 * - [ VC: Unique Element Type Declaration ]
3155 *
3156 * returns 1 if valid or 0 otherwise
3157 */
3158
3159int
3160xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3161 xmlElementPtr elem) {
3162 int ret = 1;
3163 xmlElementPtr tst;
3164
3165 CHECK_DTD;
3166
3167 if (elem == NULL) return(1);
3168
3169 /* No Duplicate Types */
3170 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3171 xmlElementContentPtr cur, next;
3172 const xmlChar *name;
3173
3174 cur = elem->content;
3175 while (cur != NULL) {
3176 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3177 if (cur->c1 == NULL) break;
3178 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3179 name = cur->c1->name;
3180 next = cur->c2;
3181 while (next != NULL) {
3182 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3183 if (xmlStrEqual(next->name, name)) {
3184 VERROR(ctxt->userData,
3185 "Definition of %s has duplicate references of %s\n",
3186 elem->name, name);
3187 ret = 0;
3188 }
3189 break;
3190 }
3191 if (next->c1 == NULL) break;
3192 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3193 if (xmlStrEqual(next->c1->name, name)) {
3194 VERROR(ctxt->userData,
3195 "Definition of %s has duplicate references of %s\n",
3196 elem->name, name);
3197 ret = 0;
3198 }
3199 next = next->c2;
3200 }
3201 }
3202 cur = cur->c2;
3203 }
3204 }
3205
3206 /* VC: Unique Element Type Declaration */
3207 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003208 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003209 ((tst->prefix == elem->prefix) ||
3210 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003211 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003212 VERROR(ctxt->userData, "Redefinition of element %s\n",
3213 elem->name);
3214 ret = 0;
3215 }
3216 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003217 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003218 ((tst->prefix == elem->prefix) ||
3219 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003220 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003221 VERROR(ctxt->userData, "Redefinition of element %s\n",
3222 elem->name);
3223 ret = 0;
3224 }
3225
Daniel Veillarda10efa82001-04-18 13:09:01 +00003226 /* One ID per Element Type
3227 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003228 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3229 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003230 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003231 return(ret);
3232}
3233
3234/**
3235 * xmlValidateOneAttribute:
3236 * @ctxt: the validation context
3237 * @doc: a document instance
3238 * @elem: an element instance
3239 * @attr: an attribute instance
3240 * @value: the attribute value (without entities processing)
3241 *
3242 * Try to validate a single attribute for an element
3243 * basically it does the following checks as described by the
3244 * XML-1.0 recommendation:
3245 * - [ VC: Attribute Value Type ]
3246 * - [ VC: Fixed Attribute Default ]
3247 * - [ VC: Entity Name ]
3248 * - [ VC: Name Token ]
3249 * - [ VC: ID ]
3250 * - [ VC: IDREF ]
3251 * - [ VC: Entity Name ]
3252 * - [ VC: Notation Attributes ]
3253 *
3254 * The ID/IDREF uniqueness and matching are done separately
3255 *
3256 * returns 1 if valid or 0 otherwise
3257 */
3258
3259int
3260xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3261 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3262 /* xmlElementPtr elemDecl; */
3263 xmlAttributePtr attrDecl = NULL;
3264 int val;
3265 int ret = 1;
3266
3267 CHECK_DTD;
3268 if ((elem == NULL) || (elem->name == NULL)) return(0);
3269 if ((attr == NULL) || (attr->name == NULL)) return(0);
3270
3271 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3272 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003273 snprintf((char *) qname, sizeof(qname), "%s:%s",
3274 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003275 qname[sizeof(qname) - 1] = 0;
3276 if (attr->ns != NULL) {
3277 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3278 attr->name, attr->ns->prefix);
3279 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3280 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3281 attr->name, attr->ns->prefix);
3282 } else {
3283 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3284 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3285 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3286 qname, attr->name);
3287 }
3288 }
3289 if (attrDecl == NULL) {
3290 if (attr->ns != NULL) {
3291 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3292 attr->name, attr->ns->prefix);
3293 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3294 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3295 attr->name, attr->ns->prefix);
3296 } else {
3297 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3298 elem->name, attr->name);
3299 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3300 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3301 elem->name, attr->name);
3302 }
3303 }
3304
3305
3306 /* Validity Constraint: Attribute Value Type */
3307 if (attrDecl == NULL) {
3308 VERROR(ctxt->userData,
3309 "No declaration for attribute %s on element %s\n",
3310 attr->name, elem->name);
3311 return(0);
3312 }
3313 attr->atype = attrDecl->atype;
3314
3315 val = xmlValidateAttributeValue(attrDecl->atype, value);
3316 if (val == 0) {
3317 VERROR(ctxt->userData,
3318 "Syntax of value for attribute %s on %s is not valid\n",
3319 attr->name, elem->name);
3320 ret = 0;
3321 }
3322
3323 /* Validity constraint: Fixed Attribute Default */
3324 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3325 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3326 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003327 "Value for attribute %s on %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003328 attr->name, elem->name, attrDecl->defaultValue);
3329 ret = 0;
3330 }
3331 }
3332
3333 /* Validity Constraint: ID uniqueness */
3334 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3335 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3336 ret = 0;
3337 }
3338
3339 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3340 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3341 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3342 ret = 0;
3343 }
3344
3345 /* Validity Constraint: Notation Attributes */
3346 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3347 xmlEnumerationPtr tree = attrDecl->tree;
3348 xmlNotationPtr nota;
3349
3350 /* First check that the given NOTATION was declared */
3351 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3352 if (nota == NULL)
3353 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3354
3355 if (nota == NULL) {
3356 VERROR(ctxt->userData,
3357 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3358 value, attr->name, elem->name);
3359 ret = 0;
3360 }
3361
3362 /* Second, verify that it's among the list */
3363 while (tree != NULL) {
3364 if (xmlStrEqual(tree->name, value)) break;
3365 tree = tree->next;
3366 }
3367 if (tree == NULL) {
3368 VERROR(ctxt->userData,
3369"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3370 value, attr->name, elem->name);
3371 ret = 0;
3372 }
3373 }
3374
3375 /* Validity Constraint: Enumeration */
3376 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3377 xmlEnumerationPtr tree = attrDecl->tree;
3378 while (tree != NULL) {
3379 if (xmlStrEqual(tree->name, value)) break;
3380 tree = tree->next;
3381 }
3382 if (tree == NULL) {
3383 VERROR(ctxt->userData,
3384 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3385 value, attr->name, elem->name);
3386 ret = 0;
3387 }
3388 }
3389
3390 /* Fixed Attribute Default */
3391 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3392 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3393 VERROR(ctxt->userData,
3394 "Value for attribute %s on %s must be \"%s\"\n",
3395 attr->name, elem->name, attrDecl->defaultValue);
3396 ret = 0;
3397 }
3398
3399 /* Extra check for the attribute value */
3400 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3401 attrDecl->atype, value);
3402
3403 return(ret);
3404}
3405
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003406/**
3407 * xmlValidateSkipIgnorable:
3408 * @ctxt: the validation context
3409 * @child: the child list
3410 *
3411 * Skip ignorable elements w.r.t. the validation process
3412 *
3413 * returns the first element to consider for validation of the content model
3414 */
3415
3416static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003417xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003418 while (child != NULL) {
3419 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003420 /* These things are ignored (skipped) during validation. */
3421 case XML_PI_NODE:
3422 case XML_COMMENT_NODE:
3423 case XML_XINCLUDE_START:
3424 case XML_XINCLUDE_END:
3425 child = child->next;
3426 break;
3427 case XML_TEXT_NODE:
3428 if (xmlIsBlankNode(child))
3429 child = child->next;
3430 else
3431 return(child);
3432 break;
3433 /* keep current node */
3434 default:
3435 return(child);
3436 }
3437 }
3438 return(child);
3439}
3440
3441/**
3442 * xmlValidateElementType:
3443 * @ctxt: the validation context
3444 *
3445 * Try to validate the content model of an element internal function
3446 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003447 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3448 * reference is found and -3 if the validation succeeded but
3449 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003450 */
3451
3452static int
3453xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003454 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003455 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003456
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003457 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003458 if ((NODE == NULL) && (CONT == NULL))
3459 return(1);
3460 if ((NODE == NULL) &&
3461 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3462 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3463 return(1);
3464 }
3465 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003466 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003467 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003468
3469 /*
3470 * We arrive here when more states need to be examined
3471 */
3472cont:
3473
3474 /*
3475 * We just recovered from a rollback generated by a possible
3476 * epsilon transition, go directly to the analysis phase
3477 */
3478 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003479 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003480 DEBUG_VALID_STATE(NODE, CONT)
3481 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003482 goto analyze;
3483 }
3484
3485 DEBUG_VALID_STATE(NODE, CONT)
3486 /*
3487 * we may have to save a backup state here. This is the equivalent
3488 * of handling epsilon transition in NFAs.
3489 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003490 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003491 ((CONT->parent == NULL) ||
3492 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003493 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003494 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00003495 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003496 DEBUG_VALID_MSG("saving parent branch");
3497 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3498 }
3499
3500
3501 /*
3502 * Check first if the content matches
3503 */
3504 switch (CONT->type) {
3505 case XML_ELEMENT_CONTENT_PCDATA:
3506 if (NODE == NULL) {
3507 DEBUG_VALID_MSG("pcdata failed no node");
3508 ret = 0;
3509 break;
3510 }
3511 if (NODE->type == XML_TEXT_NODE) {
3512 DEBUG_VALID_MSG("pcdata found, skip to next");
3513 /*
3514 * go to next element in the content model
3515 * skipping ignorable elems
3516 */
3517 do {
3518 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003519 NODE = xmlValidateSkipIgnorable(NODE);
3520 if ((NODE != NULL) &&
3521 (NODE->type == XML_ENTITY_REF_NODE))
3522 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003523 } while ((NODE != NULL) &&
3524 ((NODE->type != XML_ELEMENT_NODE) &&
3525 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003526 ret = 1;
3527 break;
3528 } else {
3529 DEBUG_VALID_MSG("pcdata failed");
3530 ret = 0;
3531 break;
3532 }
3533 break;
3534 case XML_ELEMENT_CONTENT_ELEMENT:
3535 if (NODE == NULL) {
3536 DEBUG_VALID_MSG("element failed no node");
3537 ret = 0;
3538 break;
3539 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003540 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3541 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003542 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003543 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3544 ret = (CONT->prefix == NULL);
3545 } else if (CONT->prefix == NULL) {
3546 ret = 0;
3547 } else {
3548 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3549 }
3550 }
3551 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003552 DEBUG_VALID_MSG("element found, skip to next");
3553 /*
3554 * go to next element in the content model
3555 * skipping ignorable elems
3556 */
3557 do {
3558 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003559 NODE = xmlValidateSkipIgnorable(NODE);
3560 if ((NODE != NULL) &&
3561 (NODE->type == XML_ENTITY_REF_NODE))
3562 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003563 } while ((NODE != NULL) &&
3564 ((NODE->type != XML_ELEMENT_NODE) &&
3565 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003566 } else {
3567 DEBUG_VALID_MSG("element failed");
3568 ret = 0;
3569 break;
3570 }
3571 break;
3572 case XML_ELEMENT_CONTENT_OR:
3573 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003574 * Small optimization.
3575 */
3576 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3577 if ((NODE == NULL) ||
3578 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3579 DEPTH++;
3580 CONT = CONT->c2;
3581 goto cont;
3582 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003583 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3584 ret = (CONT->c1->prefix == NULL);
3585 } else if (CONT->c1->prefix == NULL) {
3586 ret = 0;
3587 } else {
3588 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3589 }
3590 if (ret == 0) {
3591 DEPTH++;
3592 CONT = CONT->c2;
3593 goto cont;
3594 }
Daniel Veillard85349052001-04-20 13:48:21 +00003595 }
3596
3597 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003598 * save the second branch 'or' branch
3599 */
3600 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003601 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3602 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003603
3604 DEPTH++;
3605 CONT = CONT->c1;
3606 goto cont;
3607 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003608 /*
3609 * Small optimization.
3610 */
3611 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3612 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3613 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3614 if ((NODE == NULL) ||
3615 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3616 DEPTH++;
3617 CONT = CONT->c2;
3618 goto cont;
3619 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003620 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3621 ret = (CONT->c1->prefix == NULL);
3622 } else if (CONT->c1->prefix == NULL) {
3623 ret = 0;
3624 } else {
3625 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3626 }
3627 if (ret == 0) {
3628 DEPTH++;
3629 CONT = CONT->c2;
3630 goto cont;
3631 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003632 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003633 DEPTH++;
3634 CONT = CONT->c1;
3635 goto cont;
3636 }
3637
3638 /*
3639 * At this point handle going up in the tree
3640 */
3641 if (ret == -1) {
3642 DEBUG_VALID_MSG("error found returning");
3643 return(ret);
3644 }
3645analyze:
3646 while (CONT != NULL) {
3647 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003648 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003649 * this level.
3650 */
3651 if (ret == 0) {
3652 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003653 xmlNodePtr cur;
3654
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003655 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003656 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003657 DEBUG_VALID_MSG("Once branch failed, rollback");
3658 if (vstateVPop(ctxt) < 0 ) {
3659 DEBUG_VALID_MSG("exhaustion, failed");
3660 return(0);
3661 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003662 if (cur != ctxt->vstate->node)
3663 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003664 goto cont;
3665 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00003666 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003667 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003668 DEBUG_VALID_MSG("Plus branch failed, rollback");
3669 if (vstateVPop(ctxt) < 0 ) {
3670 DEBUG_VALID_MSG("exhaustion, failed");
3671 return(0);
3672 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003673 if (cur != ctxt->vstate->node)
3674 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003675 goto cont;
3676 }
3677 DEBUG_VALID_MSG("Plus branch found");
3678 ret = 1;
3679 break;
3680 case XML_ELEMENT_CONTENT_MULT:
3681#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00003682 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003683 DEBUG_VALID_MSG("Mult branch failed");
3684 } else {
3685 DEBUG_VALID_MSG("Mult branch found");
3686 }
3687#endif
3688 ret = 1;
3689 break;
3690 case XML_ELEMENT_CONTENT_OPT:
3691 DEBUG_VALID_MSG("Option branch failed");
3692 ret = 1;
3693 break;
3694 }
3695 } else {
3696 switch (CONT->ocur) {
3697 case XML_ELEMENT_CONTENT_OPT:
3698 DEBUG_VALID_MSG("Option branch succeeded");
3699 ret = 1;
3700 break;
3701 case XML_ELEMENT_CONTENT_ONCE:
3702 DEBUG_VALID_MSG("Once branch succeeded");
3703 ret = 1;
3704 break;
3705 case XML_ELEMENT_CONTENT_PLUS:
3706 if (STATE == ROLLBACK_PARENT) {
3707 DEBUG_VALID_MSG("Plus branch rollback");
3708 ret = 1;
3709 break;
3710 }
3711 if (NODE == NULL) {
3712 DEBUG_VALID_MSG("Plus branch exhausted");
3713 ret = 1;
3714 break;
3715 }
3716 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003717 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003718 goto cont;
3719 case XML_ELEMENT_CONTENT_MULT:
3720 if (STATE == ROLLBACK_PARENT) {
3721 DEBUG_VALID_MSG("Mult branch rollback");
3722 ret = 1;
3723 break;
3724 }
3725 if (NODE == NULL) {
3726 DEBUG_VALID_MSG("Mult branch exhausted");
3727 ret = 1;
3728 break;
3729 }
3730 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003731 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003732 goto cont;
3733 }
3734 }
3735 STATE = 0;
3736
3737 /*
3738 * Then act accordingly at the parent level
3739 */
Daniel Veillard5344c602001-12-31 16:37:34 +00003740 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003741 if (CONT->parent == NULL)
3742 break;
3743
3744 switch (CONT->parent->type) {
3745 case XML_ELEMENT_CONTENT_PCDATA:
3746 DEBUG_VALID_MSG("Error: parent pcdata");
3747 return(-1);
3748 case XML_ELEMENT_CONTENT_ELEMENT:
3749 DEBUG_VALID_MSG("Error: parent element");
3750 return(-1);
3751 case XML_ELEMENT_CONTENT_OR:
3752 if (ret == 1) {
3753 DEBUG_VALID_MSG("Or succeeded");
3754 CONT = CONT->parent;
3755 DEPTH--;
3756 } else {
3757 DEBUG_VALID_MSG("Or failed");
3758 CONT = CONT->parent;
3759 DEPTH--;
3760 }
3761 break;
3762 case XML_ELEMENT_CONTENT_SEQ:
3763 if (ret == 0) {
3764 DEBUG_VALID_MSG("Sequence failed");
3765 CONT = CONT->parent;
3766 DEPTH--;
3767 } else if (CONT == CONT->parent->c1) {
3768 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3769 CONT = CONT->parent->c2;
3770 goto cont;
3771 } else {
3772 DEBUG_VALID_MSG("Sequence succeeded");
3773 CONT = CONT->parent;
3774 DEPTH--;
3775 }
3776 }
3777 }
3778 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003779 xmlNodePtr cur;
3780
3781 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003782 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3783 if (vstateVPop(ctxt) < 0 ) {
3784 DEBUG_VALID_MSG("exhaustion, failed");
3785 return(0);
3786 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003787 if (cur != ctxt->vstate->node)
3788 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003789 goto cont;
3790 }
3791 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003792 xmlNodePtr cur;
3793
3794 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003795 DEBUG_VALID_MSG("Failure, rollback");
3796 if (vstateVPop(ctxt) < 0 ) {
3797 DEBUG_VALID_MSG("exhaustion, failed");
3798 return(0);
3799 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003800 if (cur != ctxt->vstate->node)
3801 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003802 goto cont;
3803 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003804 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003805}
3806
3807/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003808 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003809 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003810 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003811 * @content: An element
3812 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3813 *
3814 * This will dump the list of elements to the buffer
3815 * Intended just for the debug routine
3816 */
3817static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003818xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003819 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003820 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003821
3822 if (node == NULL) return;
3823 if (glob) strcat(buf, "(");
3824 cur = node;
3825 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003826 len = strlen(buf);
3827 if (size - len < 50) {
3828 if ((size - len > 4) && (buf[len - 1] != '.'))
3829 strcat(buf, " ...");
3830 return;
3831 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003832 switch (cur->type) {
3833 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003834 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
3835 if (size - len < xmlStrlen(cur->ns->prefix + 10)) {
3836 if ((size - len > 4) && (buf[len - 1] != '.'))
3837 strcat(buf, " ...");
3838 return;
3839 }
3840 strcat(buf, (char *) cur->ns->prefix);
3841 strcat(buf, ":");
3842 }
Daniel Veillardd3d06722001-08-15 12:06:36 +00003843 if (size - len < xmlStrlen(cur->name + 10)) {
3844 if ((size - len > 4) && (buf[len - 1] != '.'))
3845 strcat(buf, " ...");
3846 return;
3847 }
3848 strcat(buf, (char *) cur->name);
3849 if (cur->next != NULL)
3850 strcat(buf, " ");
3851 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003852 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003853 if (xmlIsBlankNode(cur))
3854 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003855 case XML_CDATA_SECTION_NODE:
3856 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003857 strcat(buf, "CDATA");
3858 if (cur->next != NULL)
3859 strcat(buf, " ");
3860 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003861 case XML_ATTRIBUTE_NODE:
3862 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003863#ifdef LIBXML_DOCB_ENABLED
3864 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003865#endif
3866 case XML_HTML_DOCUMENT_NODE:
3867 case XML_DOCUMENT_TYPE_NODE:
3868 case XML_DOCUMENT_FRAG_NODE:
3869 case XML_NOTATION_NODE:
3870 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003871 strcat(buf, "???");
3872 if (cur->next != NULL)
3873 strcat(buf, " ");
3874 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003875 case XML_ENTITY_NODE:
3876 case XML_PI_NODE:
3877 case XML_DTD_NODE:
3878 case XML_COMMENT_NODE:
3879 case XML_ELEMENT_DECL:
3880 case XML_ATTRIBUTE_DECL:
3881 case XML_ENTITY_DECL:
3882 case XML_XINCLUDE_START:
3883 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003884 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003885 }
3886 cur = cur->next;
3887 }
3888 if (glob) strcat(buf, ")");
3889}
3890
3891/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003892 * xmlValidateElementContent:
3893 * @ctxt: the validation context
3894 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003895 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003896 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003897 *
3898 * Try to validate the content model of an element
3899 *
3900 * returns 1 if valid or 0 if not and -1 in case of error
3901 */
3902
3903static int
3904xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003905 xmlElementPtr elemDecl, int warn) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003906 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003907 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003908 xmlElementContentPtr cont;
3909 const xmlChar *name;
3910
3911 if (elemDecl == NULL)
3912 return(-1);
3913 cont = elemDecl->content;
3914 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003915
3916 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003917 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003918 */
3919 ctxt->vstateMax = 8;
3920 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
3921 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
3922 if (ctxt->vstateTab == NULL) {
3923 xmlGenericError(xmlGenericErrorContext,
3924 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003925 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003926 }
3927 /*
3928 * The first entry in the stack is reserved to the current state
3929 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00003930 ctxt->nodeMax = 0;
3931 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00003932 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003933 ctxt->vstate = &ctxt->vstateTab[0];
3934 ctxt->vstateNr = 1;
3935 CONT = cont;
3936 NODE = child;
3937 DEPTH = 0;
3938 OCCURS = 0;
3939 STATE = 0;
3940 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003941 if ((ret == -3) && (warn)) {
3942 VWARNING(ctxt->userData,
3943 "Element %s content model is ambiguous\n", name);
3944 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003945 /*
3946 * An entities reference appeared at this level.
3947 * Buid a minimal representation of this node content
3948 * sufficient to run the validation process on it
3949 */
3950 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003951 cur = child;
3952 while (cur != NULL) {
3953 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003954 case XML_ENTITY_REF_NODE:
3955 /*
3956 * Push the current node to be able to roll back
3957 * and process within the entity
3958 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003959 if ((cur->children != NULL) &&
3960 (cur->children->children != NULL)) {
3961 nodeVPush(ctxt, cur);
3962 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003963 continue;
3964 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00003965 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003966 case XML_TEXT_NODE:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003967 case XML_CDATA_SECTION_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003968 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003969 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003970 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003971 case XML_ELEMENT_NODE:
3972 /*
3973 * Allocate a new node and minimally fills in
3974 * what's required
3975 */
3976 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3977 if (tmp == NULL) {
3978 xmlGenericError(xmlGenericErrorContext,
3979 "xmlValidateElementContent : malloc failed\n");
3980 xmlFreeNodeList(repl);
3981 ret = -1;
3982 goto done;
3983 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003984 tmp->type = cur->type;
3985 tmp->name = cur->name;
3986 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003987 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00003988 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003989 if (repl == NULL)
3990 repl = last = tmp;
3991 else {
3992 last->next = tmp;
3993 last = tmp;
3994 }
3995 break;
3996 default:
3997 break;
3998 }
3999 /*
4000 * Switch to next element
4001 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004002 cur = cur->next;
4003 while (cur == NULL) {
4004 cur = nodeVPop(ctxt);
4005 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004006 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004007 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004008 }
4009 }
4010
4011 /*
4012 * Relaunch the validation
4013 */
4014 ctxt->vstate = &ctxt->vstateTab[0];
4015 ctxt->vstateNr = 1;
4016 CONT = cont;
4017 NODE = repl;
4018 DEPTH = 0;
4019 OCCURS = 0;
4020 STATE = 0;
4021 ret = xmlValidateElementType(ctxt);
4022 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004023 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004024 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4025 char expr[5000];
4026 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004027
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004028 expr[0] = 0;
4029 xmlSnprintfElementContent(expr, 5000, cont, 1);
4030 list[0] = 0;
4031 if (repl != NULL)
4032 xmlSnprintfElements(list, 5000, repl, 1);
4033 else
4034 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004035
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004036 if (name != NULL) {
4037 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004038 "Element %s content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004039 name, expr, list);
4040 } else {
4041 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004042 "Element content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004043 expr, list);
4044 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004045 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004046 if (name != NULL) {
4047 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004048 "Element %s content doesn't follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004049 name);
4050 } else {
4051 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004052 "Element content doesn't follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004053 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004054 }
4055 ret = 0;
4056 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004057 if (ret == -3)
4058 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004059
4060
4061done:
4062 /*
4063 * Deallocate the copy if done, and free up the validation stack
4064 */
4065 while (repl != NULL) {
4066 tmp = repl->next;
4067 xmlFree(repl);
4068 repl = tmp;
4069 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004070 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004071 if (ctxt->vstateTab != NULL) {
4072 xmlFree(ctxt->vstateTab);
4073 ctxt->vstateTab = NULL;
4074 }
4075 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004076 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004077 if (ctxt->nodeTab != NULL) {
4078 xmlFree(ctxt->nodeTab);
4079 ctxt->nodeTab = NULL;
4080 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004081 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004082
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004083}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004084
Owen Taylor3473f882001-02-23 17:55:21 +00004085/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004086 * xmlValidateCdataElement:
4087 * @ctxt: the validation context
4088 * @doc: a document instance
4089 * @elem: an element instance
4090 *
4091 * Check that an element follows #CDATA
4092 *
4093 * returns 1 if valid or 0 otherwise
4094 */
4095static int
4096xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4097 xmlNodePtr elem) {
4098 int ret = 1;
4099 xmlNodePtr cur, child;
4100
4101 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4102 return(0);
4103
4104 child = elem->children;
4105
4106 cur = child;
4107 while (cur != NULL) {
4108 switch (cur->type) {
4109 case XML_ENTITY_REF_NODE:
4110 /*
4111 * Push the current node to be able to roll back
4112 * and process within the entity
4113 */
4114 if ((cur->children != NULL) &&
4115 (cur->children->children != NULL)) {
4116 nodeVPush(ctxt, cur);
4117 cur = cur->children->children;
4118 continue;
4119 }
4120 break;
4121 case XML_COMMENT_NODE:
4122 case XML_PI_NODE:
4123 case XML_TEXT_NODE:
4124 case XML_CDATA_SECTION_NODE:
4125 break;
4126 default:
4127 ret = 0;
4128 goto done;
4129 }
4130 /*
4131 * Switch to next element
4132 */
4133 cur = cur->next;
4134 while (cur == NULL) {
4135 cur = nodeVPop(ctxt);
4136 if (cur == NULL)
4137 break;
4138 cur = cur->next;
4139 }
4140 }
4141done:
4142 ctxt->nodeMax = 0;
4143 ctxt->nodeNr = 0;
4144 if (ctxt->nodeTab != NULL) {
4145 xmlFree(ctxt->nodeTab);
4146 ctxt->nodeTab = NULL;
4147 }
4148 return(ret);
4149}
4150
4151/**
Owen Taylor3473f882001-02-23 17:55:21 +00004152 * xmlValidateOneElement:
4153 * @ctxt: the validation context
4154 * @doc: a document instance
4155 * @elem: an element instance
4156 *
4157 * Try to validate a single element and it's attributes,
4158 * basically it does the following checks as described by the
4159 * XML-1.0 recommendation:
4160 * - [ VC: Element Valid ]
4161 * - [ VC: Required Attribute ]
4162 * Then call xmlValidateOneAttribute() for each attribute present.
4163 *
4164 * The ID/IDREF checkings are done separately
4165 *
4166 * returns 1 if valid or 0 otherwise
4167 */
4168
4169int
4170xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4171 xmlNodePtr elem) {
4172 xmlElementPtr elemDecl = NULL;
4173 xmlElementContentPtr cont;
4174 xmlAttributePtr attr;
4175 xmlNodePtr child;
4176 int ret = 1;
4177 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004178 const xmlChar *prefix = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004179
4180 CHECK_DTD;
4181
4182 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004183 switch (elem->type) {
4184 case XML_ATTRIBUTE_NODE:
4185 VERROR(ctxt->userData,
4186 "Attribute element not expected here\n");
4187 return(0);
4188 case XML_TEXT_NODE:
4189 if (elem->children != NULL) {
4190 VERROR(ctxt->userData, "Text element has childs !\n");
4191 return(0);
4192 }
4193 if (elem->properties != NULL) {
4194 VERROR(ctxt->userData, "Text element has attributes !\n");
4195 return(0);
4196 }
4197 if (elem->ns != NULL) {
4198 VERROR(ctxt->userData, "Text element has namespace !\n");
4199 return(0);
4200 }
4201 if (elem->nsDef != NULL) {
4202 VERROR(ctxt->userData,
4203 "Text element carries namespace definitions !\n");
4204 return(0);
4205 }
4206 if (elem->content == NULL) {
4207 VERROR(ctxt->userData,
4208 "Text element has no content !\n");
4209 return(0);
4210 }
4211 return(1);
4212 case XML_XINCLUDE_START:
4213 case XML_XINCLUDE_END:
4214 return(1);
4215 case XML_CDATA_SECTION_NODE:
4216 case XML_ENTITY_REF_NODE:
4217 case XML_PI_NODE:
4218 case XML_COMMENT_NODE:
4219 return(1);
4220 case XML_ENTITY_NODE:
4221 VERROR(ctxt->userData,
4222 "Entity element not expected here\n");
4223 return(0);
4224 case XML_NOTATION_NODE:
4225 VERROR(ctxt->userData,
4226 "Notation element not expected here\n");
4227 return(0);
4228 case XML_DOCUMENT_NODE:
4229 case XML_DOCUMENT_TYPE_NODE:
4230 case XML_DOCUMENT_FRAG_NODE:
4231 VERROR(ctxt->userData,
4232 "Document element not expected here\n");
4233 return(0);
4234 case XML_HTML_DOCUMENT_NODE:
4235 VERROR(ctxt->userData,
4236 "\n");
4237 return(0);
4238 case XML_ELEMENT_NODE:
4239 break;
4240 default:
4241 VERROR(ctxt->userData,
4242 "unknown element type %d\n", elem->type);
4243 return(0);
4244 }
4245 if (elem->name == NULL) return(0);
4246
4247 /*
4248 * Fetch the declaration for the qualified name
4249 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004250 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4251 prefix = elem->ns->prefix;
4252
4253 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004254 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004255 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004256 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4257 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004258 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004259 }
4260
4261 /*
4262 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004263 * This is "non-strict" validation should be done on the
4264 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004265 */
4266 if (elemDecl == NULL) {
4267 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4268 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4269 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4270 }
4271 if (elemDecl == NULL) {
4272 VERROR(ctxt->userData, "No declaration for element %s\n",
4273 elem->name);
4274 return(0);
4275 }
4276
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004277 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00004278 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004279 case XML_ELEMENT_TYPE_UNDEFINED:
4280 VERROR(ctxt->userData, "No declaration for element %s\n",
4281 elem->name);
4282 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004283 case XML_ELEMENT_TYPE_EMPTY:
4284 if (elem->children != NULL) {
4285 VERROR(ctxt->userData,
4286 "Element %s was declared EMPTY this one has content\n",
4287 elem->name);
4288 ret = 0;
4289 }
4290 break;
4291 case XML_ELEMENT_TYPE_ANY:
4292 /* I don't think anything is required then */
4293 break;
4294 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004295 /* simple case of declared as #PCDATA */
4296 if ((elemDecl->content != NULL) &&
4297 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4298 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4299 if (!ret) {
4300 VERROR(ctxt->userData,
4301 "Element %s was declared #PCDATA but contains non text nodes\n",
4302 elem->name);
4303 }
4304 break;
4305 }
Owen Taylor3473f882001-02-23 17:55:21 +00004306 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004307 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004308 while (child != NULL) {
4309 if (child->type == XML_ELEMENT_NODE) {
4310 name = child->name;
4311 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4312 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004313 snprintf((char *) qname, sizeof(qname), "%s:%s",
4314 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004315 qname[sizeof(qname) - 1] = 0;
4316 cont = elemDecl->content;
4317 while (cont != NULL) {
4318 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4319 if (xmlStrEqual(cont->name, qname)) break;
4320 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4321 (cont->c1 != NULL) &&
4322 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4323 if (xmlStrEqual(cont->c1->name, qname)) break;
4324 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4325 (cont->c1 == NULL) ||
4326 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4327 /* Internal error !!! */
4328 xmlGenericError(xmlGenericErrorContext,
4329 "Internal: MIXED struct bad\n");
4330 break;
4331 }
4332 cont = cont->c2;
4333 }
4334 if (cont != NULL)
4335 goto child_ok;
4336 }
4337 cont = elemDecl->content;
4338 while (cont != NULL) {
4339 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4340 if (xmlStrEqual(cont->name, name)) break;
4341 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4342 (cont->c1 != NULL) &&
4343 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4344 if (xmlStrEqual(cont->c1->name, name)) break;
4345 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4346 (cont->c1 == NULL) ||
4347 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4348 /* Internal error !!! */
4349 xmlGenericError(xmlGenericErrorContext,
4350 "Internal: MIXED struct bad\n");
4351 break;
4352 }
4353 cont = cont->c2;
4354 }
4355 if (cont == NULL) {
4356 VERROR(ctxt->userData,
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00004357 "Element %s is not declared in %s list of possible children\n",
Owen Taylor3473f882001-02-23 17:55:21 +00004358 name, elem->name);
4359 ret = 0;
4360 }
4361 }
4362child_ok:
4363 child = child->next;
4364 }
4365 break;
4366 case XML_ELEMENT_TYPE_ELEMENT:
4367 child = elem->children;
4368 cont = elemDecl->content;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004369 ret = xmlValidateElementContent(ctxt, child, elemDecl, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00004370 break;
4371 }
4372
4373 /* [ VC: Required Attribute ] */
4374 attr = elemDecl->attributes;
4375 while (attr != NULL) {
4376 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004377 int qualified = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00004378
Daniel Veillarde4301c82002-02-13 13:32:35 +00004379 if ((attr->prefix == NULL) &&
4380 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4381 xmlNsPtr ns;
4382
4383 ns = elem->nsDef;
4384 while (ns != NULL) {
4385 if (ns->prefix == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004386 goto found;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004387 ns = ns->next;
Owen Taylor3473f882001-02-23 17:55:21 +00004388 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004389 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4390 xmlNsPtr ns;
4391
4392 ns = elem->nsDef;
4393 while (ns != NULL) {
4394 if (xmlStrEqual(attr->name, ns->prefix))
4395 goto found;
4396 ns = ns->next;
4397 }
4398 } else {
4399 xmlAttrPtr attrib;
4400
4401 attrib = elem->properties;
4402 while (attrib != NULL) {
4403 if (xmlStrEqual(attrib->name, attr->name)) {
4404 if (attr->prefix != NULL) {
4405 xmlNsPtr nameSpace = attrib->ns;
4406
4407 if (nameSpace == NULL)
4408 nameSpace = elem->ns;
4409 /*
4410 * qualified names handling is problematic, having a
4411 * different prefix should be possible but DTDs don't
4412 * allow to define the URI instead of the prefix :-(
4413 */
4414 if (nameSpace == NULL) {
4415 if (qualified < 0)
4416 qualified = 0;
4417 } else if (!xmlStrEqual(nameSpace->prefix,
4418 attr->prefix)) {
4419 if (qualified < 1)
4420 qualified = 1;
4421 } else
4422 goto found;
4423 } else {
4424 /*
4425 * We should allow applications to define namespaces
4426 * for their application even if the DTD doesn't
4427 * carry one, otherwise, basically we would always
4428 * break.
4429 */
4430 goto found;
4431 }
4432 }
4433 attrib = attrib->next;
4434 }
Owen Taylor3473f882001-02-23 17:55:21 +00004435 }
4436 if (qualified == -1) {
4437 if (attr->prefix == NULL) {
4438 VERROR(ctxt->userData,
4439 "Element %s doesn't carry attribute %s\n",
4440 elem->name, attr->name);
4441 ret = 0;
4442 } else {
4443 VERROR(ctxt->userData,
4444 "Element %s doesn't carry attribute %s:%s\n",
4445 elem->name, attr->prefix,attr->name);
4446 ret = 0;
4447 }
4448 } else if (qualified == 0) {
4449 VWARNING(ctxt->userData,
4450 "Element %s required attribute %s:%s has no prefix\n",
4451 elem->name, attr->prefix,attr->name);
4452 } else if (qualified == 1) {
4453 VWARNING(ctxt->userData,
4454 "Element %s required attribute %s:%s has different prefix\n",
4455 elem->name, attr->prefix,attr->name);
4456 }
Daniel Veillarde4301c82002-02-13 13:32:35 +00004457 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
4458 /*
4459 * Special tests checking #FIXED namespace declarations
4460 * have the right value since this is not done as an
4461 * attribute checking
4462 */
4463 if ((attr->prefix == NULL) &&
4464 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
4465 xmlNsPtr ns;
4466
4467 ns = elem->nsDef;
4468 while (ns != NULL) {
4469 if (ns->prefix == NULL) {
4470 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4471 VERROR(ctxt->userData,
4472 "Element %s namespace name for default namespace does not match the DTD\n",
4473 elem->name);
Daniel Veillardc7612992002-02-17 22:47:37 +00004474 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004475 }
4476 goto found;
4477 }
4478 ns = ns->next;
4479 }
4480 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
4481 xmlNsPtr ns;
4482
4483 ns = elem->nsDef;
4484 while (ns != NULL) {
4485 if (xmlStrEqual(attr->name, ns->prefix)) {
4486 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
4487 VERROR(ctxt->userData,
4488 "Element %s namespace name for %s doesn't match the DTD\n",
4489 elem->name, ns->prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +00004490 ret = 0;
Daniel Veillarde4301c82002-02-13 13:32:35 +00004491 }
4492 goto found;
4493 }
4494 ns = ns->next;
4495 }
4496 }
Owen Taylor3473f882001-02-23 17:55:21 +00004497 }
4498found:
4499 attr = attr->nexth;
4500 }
4501 return(ret);
4502}
4503
4504/**
4505 * xmlValidateRoot:
4506 * @ctxt: the validation context
4507 * @doc: a document instance
4508 *
4509 * Try to validate a the root element
4510 * basically it does the following check as described by the
4511 * XML-1.0 recommendation:
4512 * - [ VC: Root Element Type ]
4513 * it doesn't try to recurse or apply other check to the element
4514 *
4515 * returns 1 if valid or 0 otherwise
4516 */
4517
4518int
4519xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4520 xmlNodePtr root;
4521 if (doc == NULL) return(0);
4522
4523 root = xmlDocGetRootElement(doc);
4524 if ((root == NULL) || (root->name == NULL)) {
4525 VERROR(ctxt->userData, "Not valid: no root element\n");
4526 return(0);
4527 }
4528
4529 /*
4530 * When doing post validation against a separate DTD, those may
4531 * no internal subset has been generated
4532 */
4533 if ((doc->intSubset != NULL) &&
4534 (doc->intSubset->name != NULL)) {
4535 /*
4536 * Check first the document root against the NQName
4537 */
4538 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4539 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4540 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004541 snprintf((char *) qname, sizeof(qname), "%s:%s",
4542 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004543 qname[sizeof(qname) - 1] = 0;
4544 if (xmlStrEqual(doc->intSubset->name, qname))
4545 goto name_ok;
4546 }
4547 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4548 (xmlStrEqual(root->name, BAD_CAST "html")))
4549 goto name_ok;
4550 VERROR(ctxt->userData,
4551 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4552 root->name, doc->intSubset->name);
4553 return(0);
4554
4555 }
4556 }
4557name_ok:
4558 return(1);
4559}
4560
4561
4562/**
4563 * xmlValidateElement:
4564 * @ctxt: the validation context
4565 * @doc: a document instance
4566 * @elem: an element instance
4567 *
4568 * Try to validate the subtree under an element
4569 *
4570 * returns 1 if valid or 0 otherwise
4571 */
4572
4573int
4574xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4575 xmlNodePtr child;
4576 xmlAttrPtr attr;
4577 xmlChar *value;
4578 int ret = 1;
4579
4580 if (elem == NULL) return(0);
4581
4582 /*
4583 * XInclude elements were added after parsing in the infoset,
4584 * they don't really mean anything validation wise.
4585 */
4586 if ((elem->type == XML_XINCLUDE_START) ||
4587 (elem->type == XML_XINCLUDE_END))
4588 return(1);
4589
4590 CHECK_DTD;
4591
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004592 /*
4593 * Entities references have to be handled separately
4594 */
4595 if (elem->type == XML_ENTITY_REF_NODE) {
4596 return(1);
4597 }
4598
Owen Taylor3473f882001-02-23 17:55:21 +00004599 ret &= xmlValidateOneElement(ctxt, doc, elem);
4600 attr = elem->properties;
4601 while(attr != NULL) {
4602 value = xmlNodeListGetString(doc, attr->children, 0);
4603 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4604 if (value != NULL)
4605 xmlFree(value);
4606 attr= attr->next;
4607 }
4608 child = elem->children;
4609 while (child != NULL) {
4610 ret &= xmlValidateElement(ctxt, doc, child);
4611 child = child->next;
4612 }
4613
4614 return(ret);
4615}
4616
Daniel Veillard8730c562001-02-26 10:49:57 +00004617/**
4618 * xmlValidateRef:
4619 * @ref: A reference to be validated
4620 * @ctxt: Validation context
4621 * @name: Name of ID we are searching for
4622 *
4623 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004624static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004625xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004626 const xmlChar *name) {
4627 xmlAttrPtr id;
4628 xmlAttrPtr attr;
4629
4630 if (ref == NULL)
4631 return;
4632 attr = ref->attr;
4633 if (attr == NULL)
4634 return;
4635 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4636 id = xmlGetID(ctxt->doc, name);
4637 if (id == NULL) {
4638 VERROR(ctxt->userData,
4639 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4640 attr->name, name);
4641 ctxt->valid = 0;
4642 }
4643 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4644 xmlChar *dup, *str = NULL, *cur, save;
4645
4646 dup = xmlStrdup(name);
4647 if (dup == NULL) {
4648 ctxt->valid = 0;
4649 return;
4650 }
4651 cur = dup;
4652 while (*cur != 0) {
4653 str = cur;
4654 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4655 save = *cur;
4656 *cur = 0;
4657 id = xmlGetID(ctxt->doc, str);
4658 if (id == NULL) {
4659 VERROR(ctxt->userData,
4660 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4661 attr->name, str);
4662 ctxt->valid = 0;
4663 }
4664 if (save == 0)
4665 break;
4666 *cur = save;
4667 while (IS_BLANK(*cur)) cur++;
4668 }
4669 xmlFree(dup);
4670 }
4671}
4672
4673/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004674 * xmlWalkValidateList:
4675 * @data: Contents of current link
4676 * @user: Value supplied by the user
4677 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004678 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00004679 */
4680static int
4681xmlWalkValidateList(const void *data, const void *user)
4682{
4683 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4684 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4685 return 1;
4686}
4687
4688/**
4689 * xmlValidateCheckRefCallback:
4690 * @ref_list: List of references
4691 * @ctxt: Validation context
4692 * @name: Name of ID we are searching for
4693 *
4694 */
4695static void
4696xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4697 const xmlChar *name) {
4698 xmlValidateMemo memo;
4699
4700 if (ref_list == NULL)
4701 return;
4702 memo.ctxt = ctxt;
4703 memo.name = name;
4704
4705 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4706
4707}
4708
4709/**
Owen Taylor3473f882001-02-23 17:55:21 +00004710 * xmlValidateDocumentFinal:
4711 * @ctxt: the validation context
4712 * @doc: a document instance
4713 *
4714 * Does the final step for the document validation once all the
4715 * incremental validation steps have been completed
4716 *
4717 * basically it does the following checks described by the XML Rec
4718 *
4719 *
4720 * returns 1 if valid or 0 otherwise
4721 */
4722
4723int
4724xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4725 xmlRefTablePtr table;
4726
4727 if (doc == NULL) {
4728 xmlGenericError(xmlGenericErrorContext,
4729 "xmlValidateDocumentFinal: doc == NULL\n");
4730 return(0);
4731 }
4732
4733 /*
4734 * Check all the NOTATION/NOTATIONS attributes
4735 */
4736 /*
4737 * Check all the ENTITY/ENTITIES attributes definition for validity
4738 */
4739 /*
4740 * Check all the IDREF/IDREFS attributes definition for validity
4741 */
4742 table = (xmlRefTablePtr) doc->refs;
4743 ctxt->doc = doc;
4744 ctxt->valid = 1;
4745 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4746 return(ctxt->valid);
4747}
4748
4749/**
4750 * xmlValidateDtd:
4751 * @ctxt: the validation context
4752 * @doc: a document instance
4753 * @dtd: a dtd instance
4754 *
4755 * Try to validate the document against the dtd instance
4756 *
4757 * basically it does check all the definitions in the DtD.
4758 *
4759 * returns 1 if valid or 0 otherwise
4760 */
4761
4762int
4763xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4764 int ret;
4765 xmlDtdPtr oldExt;
4766 xmlNodePtr root;
4767
4768 if (dtd == NULL) return(0);
4769 if (doc == NULL) return(0);
4770 oldExt = doc->extSubset;
4771 doc->extSubset = dtd;
4772 ret = xmlValidateRoot(ctxt, doc);
4773 if (ret == 0) {
4774 doc->extSubset = oldExt;
4775 return(ret);
4776 }
4777 if (doc->ids != NULL) {
4778 xmlFreeIDTable(doc->ids);
4779 doc->ids = NULL;
4780 }
4781 if (doc->refs != NULL) {
4782 xmlFreeRefTable(doc->refs);
4783 doc->refs = NULL;
4784 }
4785 root = xmlDocGetRootElement(doc);
4786 ret = xmlValidateElement(ctxt, doc, root);
4787 ret &= xmlValidateDocumentFinal(ctxt, doc);
4788 doc->extSubset = oldExt;
4789 return(ret);
4790}
4791
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004792static void
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004793xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
4794 const xmlChar *name ATTRIBUTE_UNUSED) {
4795 if (cur == NULL)
4796 return;
4797 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
4798 xmlChar *notation = cur->content;
4799
4800 if (cur != NULL) {
4801 int ret;
4802
4803 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
4804 if (ret != 1) {
4805 ctxt->valid = -1;
4806 }
4807 }
4808 }
4809}
4810
4811static void
Owen Taylor3473f882001-02-23 17:55:21 +00004812xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004813 const xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004814 int ret;
4815
Owen Taylor3473f882001-02-23 17:55:21 +00004816 if (cur == NULL)
4817 return;
4818 switch (cur->atype) {
4819 case XML_ATTRIBUTE_CDATA:
4820 case XML_ATTRIBUTE_ID:
4821 case XML_ATTRIBUTE_IDREF :
4822 case XML_ATTRIBUTE_IDREFS:
4823 case XML_ATTRIBUTE_NMTOKEN:
4824 case XML_ATTRIBUTE_NMTOKENS:
4825 case XML_ATTRIBUTE_ENUMERATION:
4826 break;
4827 case XML_ATTRIBUTE_ENTITY:
4828 case XML_ATTRIBUTE_ENTITIES:
4829 case XML_ATTRIBUTE_NOTATION:
4830 if (cur->defaultValue != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004831
4832 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
4833 cur->atype, cur->defaultValue);
4834 if ((ret == 0) && (ctxt->valid == 1))
4835 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004836 }
4837 if (cur->tree != NULL) {
4838 xmlEnumerationPtr tree = cur->tree;
4839 while (tree != NULL) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004840 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
Owen Taylor3473f882001-02-23 17:55:21 +00004841 cur->name, cur->atype, tree->name);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004842 if ((ret == 0) && (ctxt->valid == 1))
4843 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004844 tree = tree->next;
4845 }
4846 }
4847 }
4848}
4849
4850/**
4851 * xmlValidateDtdFinal:
4852 * @ctxt: the validation context
4853 * @doc: a document instance
4854 *
4855 * Does the final step for the dtds validation once all the
4856 * subsets have been parsed
4857 *
4858 * basically it does the following checks described by the XML Rec
4859 * - check that ENTITY and ENTITIES type attributes default or
4860 * possible values matches one of the defined entities.
4861 * - check that NOTATION type attributes default or
4862 * possible values matches one of the defined notations.
4863 *
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004864 * returns 1 if valid or 0 if invalid and -1 if not well-formed
Owen Taylor3473f882001-02-23 17:55:21 +00004865 */
4866
4867int
4868xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
Owen Taylor3473f882001-02-23 17:55:21 +00004869 xmlDtdPtr dtd;
4870 xmlAttributeTablePtr table;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004871 xmlEntitiesTablePtr entities;
Owen Taylor3473f882001-02-23 17:55:21 +00004872
4873 if (doc == NULL) return(0);
4874 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4875 return(0);
4876 ctxt->doc = doc;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004877 ctxt->valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00004878 dtd = doc->intSubset;
4879 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4880 table = (xmlAttributeTablePtr) dtd->attributes;
4881 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004882 entities = (xmlEntitiesTablePtr) dtd->entities;
4883 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
4884 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004885 }
4886 dtd = doc->extSubset;
4887 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4888 table = (xmlAttributeTablePtr) dtd->attributes;
4889 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
Daniel Veillard8ab0f582002-02-18 18:31:38 +00004890 entities = (xmlEntitiesTablePtr) dtd->entities;
4891 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
4892 ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004893 }
4894 return(ctxt->valid);
4895}
4896
4897/**
4898 * xmlValidateDocument:
4899 * @ctxt: the validation context
4900 * @doc: a document instance
4901 *
4902 * Try to validate the document instance
4903 *
4904 * basically it does the all the checks described by the XML Rec
4905 * i.e. validates the internal and external subset (if present)
4906 * and validate the document tree.
4907 *
4908 * returns 1 if valid or 0 otherwise
4909 */
4910
4911int
4912xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4913 int ret;
4914 xmlNodePtr root;
4915
4916 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4917 return(0);
4918 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
4919 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
4920 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
4921 doc->intSubset->SystemID);
4922 if (doc->extSubset == NULL) {
4923 if (doc->intSubset->SystemID != NULL) {
4924 VERROR(ctxt->userData,
4925 "Could not load the external subset \"%s\"\n",
4926 doc->intSubset->SystemID);
4927 } else {
4928 VERROR(ctxt->userData,
4929 "Could not load the external subset \"%s\"\n",
4930 doc->intSubset->ExternalID);
4931 }
4932 return(0);
4933 }
4934 }
4935
4936 if (doc->ids != NULL) {
4937 xmlFreeIDTable(doc->ids);
4938 doc->ids = NULL;
4939 }
4940 if (doc->refs != NULL) {
4941 xmlFreeRefTable(doc->refs);
4942 doc->refs = NULL;
4943 }
4944 ret = xmlValidateDtdFinal(ctxt, doc);
4945 if (!xmlValidateRoot(ctxt, doc)) return(0);
4946
4947 root = xmlDocGetRootElement(doc);
4948 ret &= xmlValidateElement(ctxt, doc, root);
4949 ret &= xmlValidateDocumentFinal(ctxt, doc);
4950 return(ret);
4951}
4952
4953
4954/************************************************************************
4955 * *
4956 * Routines for dynamic validation editing *
4957 * *
4958 ************************************************************************/
4959
4960/**
4961 * xmlValidGetPotentialChildren:
4962 * @ctree: an element content tree
4963 * @list: an array to store the list of child names
4964 * @len: a pointer to the number of element in the list
4965 * @max: the size of the array
4966 *
4967 * Build/extend a list of potential children allowed by the content tree
4968 *
4969 * returns the number of element in the list, or -1 in case of error.
4970 */
4971
4972int
4973xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
4974 int *len, int max) {
4975 int i;
4976
4977 if ((ctree == NULL) || (list == NULL) || (len == NULL))
4978 return(-1);
4979 if (*len >= max) return(*len);
4980
4981 switch (ctree->type) {
4982 case XML_ELEMENT_CONTENT_PCDATA:
4983 for (i = 0; i < *len;i++)
4984 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
4985 list[(*len)++] = BAD_CAST "#PCDATA";
4986 break;
4987 case XML_ELEMENT_CONTENT_ELEMENT:
4988 for (i = 0; i < *len;i++)
4989 if (xmlStrEqual(ctree->name, list[i])) return(*len);
4990 list[(*len)++] = ctree->name;
4991 break;
4992 case XML_ELEMENT_CONTENT_SEQ:
4993 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4994 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4995 break;
4996 case XML_ELEMENT_CONTENT_OR:
4997 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4998 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4999 break;
5000 }
5001
5002 return(*len);
5003}
5004
5005/**
5006 * xmlValidGetValidElements:
5007 * @prev: an element to insert after
5008 * @next: an element to insert next
5009 * @list: an array to store the list of child names
5010 * @max: the size of the array
5011 *
5012 * This function returns the list of authorized children to insert
5013 * within an existing tree while respecting the validity constraints
5014 * forced by the Dtd. The insertion point is defined using @prev and
5015 * @next in the following ways:
5016 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
5017 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
5018 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
5019 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
5020 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
5021 *
5022 * pointers to the element names are inserted at the beginning of the array
5023 * and do not need to be freed.
5024 *
5025 * returns the number of element in the list, or -1 in case of error. If
5026 * the function returns the value @max the caller is invited to grow the
5027 * receiving array and retry.
5028 */
5029
5030int
5031xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
5032 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005033 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00005034 int nb_valid_elements = 0;
5035 const xmlChar *elements[256];
5036 int nb_elements = 0, i;
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00005037 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005038
5039 xmlNode *ref_node;
5040 xmlNode *parent;
5041 xmlNode *test_node;
5042
5043 xmlNode *prev_next;
5044 xmlNode *next_prev;
5045 xmlNode *parent_childs;
5046 xmlNode *parent_last;
5047
5048 xmlElement *element_desc;
5049
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005050 vctxt.userData = NULL;
5051 vctxt.error = NULL;
5052 vctxt.warning = NULL;
5053
Owen Taylor3473f882001-02-23 17:55:21 +00005054 if (prev == NULL && next == NULL)
5055 return(-1);
5056
5057 if (list == NULL) return(-1);
5058 if (max <= 0) return(-1);
5059
5060 nb_valid_elements = 0;
5061 ref_node = prev ? prev : next;
5062 parent = ref_node->parent;
5063
5064 /*
5065 * Retrieves the parent element declaration
5066 */
5067 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
5068 parent->name);
5069 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
5070 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
5071 parent->name);
5072 if (element_desc == NULL) return(-1);
5073
5074 /*
5075 * Do a backup of the current tree structure
5076 */
5077 prev_next = prev ? prev->next : NULL;
5078 next_prev = next ? next->prev : NULL;
5079 parent_childs = parent->children;
5080 parent_last = parent->last;
5081
5082 /*
5083 * Creates a dummy node and insert it into the tree
5084 */
5085 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
5086 test_node->doc = ref_node->doc;
5087 test_node->parent = parent;
5088 test_node->prev = prev;
5089 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00005090 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00005091
5092 if (prev) prev->next = test_node;
5093 else parent->children = test_node;
5094
5095 if (next) next->prev = test_node;
5096 else parent->last = test_node;
5097
5098 /*
5099 * Insert each potential child node and check if the parent is
5100 * still valid
5101 */
5102 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
5103 elements, &nb_elements, 256);
5104
5105 for (i = 0;i < nb_elements;i++) {
5106 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005107 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005108 int j;
5109
5110 for (j = 0; j < nb_valid_elements;j++)
5111 if (xmlStrEqual(elements[i], list[j])) break;
5112 list[nb_valid_elements++] = elements[i];
5113 if (nb_valid_elements >= max) break;
5114 }
5115 }
5116
5117 /*
5118 * Restore the tree structure
5119 */
5120 if (prev) prev->next = prev_next;
5121 if (next) next->prev = next_prev;
5122 parent->children = parent_childs;
5123 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00005124
5125 /*
5126 * Free up the dummy node
5127 */
5128 test_node->name = name;
5129 xmlFreeNode(test_node);
5130
Owen Taylor3473f882001-02-23 17:55:21 +00005131 return(nb_valid_elements);
5132}