blob: 99b30ddfd18d2963a3986357323be3a6fa6a3314 [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
318xmlElementPtr xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name);
Daniel Veillarda10efa82001-04-18 13:09:01 +0000319static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
320 int create);
Owen Taylor3473f882001-02-23 17:55:21 +0000321xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
322
323/************************************************************************
324 * *
325 * QName handling helper *
326 * *
327 ************************************************************************/
328
329/**
330 * xmlSplitQName2:
331 * @name: an XML parser context
332 * @prefix: a xmlChar **
333 *
334 * parse an XML qualified name string
335 *
336 * [NS 5] QName ::= (Prefix ':')? LocalPart
337 *
338 * [NS 6] Prefix ::= NCName
339 *
340 * [NS 7] LocalPart ::= NCName
341 *
342 * Returns NULL if not a QName, otherwise the local part, and prefix
343 * is updated to get the Prefix if any.
344 */
345
346xmlChar *
347xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
348 int len = 0;
349 xmlChar *ret = NULL;
350
351 *prefix = NULL;
352
Daniel Veillardf4309d72001-10-02 09:28:58 +0000353#ifndef XML_XML_NAMESPACE
Owen Taylor3473f882001-02-23 17:55:21 +0000354 /* xml: prefix is not really a namespace */
355 if ((name[0] == 'x') && (name[1] == 'm') &&
356 (name[2] == 'l') && (name[3] == ':'))
357 return(NULL);
Daniel Veillardf4309d72001-10-02 09:28:58 +0000358#endif
Owen Taylor3473f882001-02-23 17:55:21 +0000359
360 /* nasty but valid */
361 if (name[0] == ':')
362 return(NULL);
363
364 /*
365 * we are not trying to validate but just to cut, and yes it will
366 * work even if this is as set of UTF-8 encoded chars
367 */
368 while ((name[len] != 0) && (name[len] != ':'))
369 len++;
370
371 if (name[len] == 0)
372 return(NULL);
373
374 *prefix = xmlStrndup(name, len);
375 ret = xmlStrdup(&name[len + 1]);
376
377 return(ret);
378}
379
380/****************************************************************
381 * *
382 * Util functions for data allocation/deallocation *
383 * *
384 ****************************************************************/
385
386/**
387 * xmlNewElementContent:
388 * @name: the subelement name or NULL
389 * @type: the type of element content decl
390 *
391 * Allocate an element content structure.
392 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000393 * Returns NULL if not, otherwise the new element content structure
Owen Taylor3473f882001-02-23 17:55:21 +0000394 */
395xmlElementContentPtr
396xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
397 xmlElementContentPtr ret;
398
399 switch(type) {
400 case XML_ELEMENT_CONTENT_ELEMENT:
401 if (name == NULL) {
402 xmlGenericError(xmlGenericErrorContext,
403 "xmlNewElementContent : name == NULL !\n");
404 }
405 break;
406 case XML_ELEMENT_CONTENT_PCDATA:
407 case XML_ELEMENT_CONTENT_SEQ:
408 case XML_ELEMENT_CONTENT_OR:
409 if (name != NULL) {
410 xmlGenericError(xmlGenericErrorContext,
411 "xmlNewElementContent : name != NULL !\n");
412 }
413 break;
414 default:
415 xmlGenericError(xmlGenericErrorContext,
416 "xmlNewElementContent: unknown type %d\n", type);
417 return(NULL);
418 }
419 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
420 if (ret == NULL) {
421 xmlGenericError(xmlGenericErrorContext,
422 "xmlNewElementContent : out of memory!\n");
423 return(NULL);
424 }
425 ret->type = type;
426 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000427 if (name != NULL) {
428 xmlChar *prefix = NULL;
429 ret->name = xmlSplitQName2(name, &prefix);
430 if (ret->name == NULL)
431 ret->name = xmlStrdup(name);
432 ret->prefix = prefix;
433 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000434 ret->name = NULL;
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000435 ret->prefix = NULL;
436 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000437 ret->c1 = ret->c2 = ret->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000438 return(ret);
439}
440
441/**
442 * xmlCopyElementContent:
443 * @content: An element content pointer.
444 *
445 * Build a copy of an element content description.
446 *
447 * Returns the new xmlElementContentPtr or NULL in case of error.
448 */
449xmlElementContentPtr
450xmlCopyElementContent(xmlElementContentPtr cur) {
451 xmlElementContentPtr ret;
452
453 if (cur == NULL) return(NULL);
454 ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
455 if (ret == NULL) {
456 xmlGenericError(xmlGenericErrorContext,
457 "xmlCopyElementContent : out of memory\n");
458 return(NULL);
459 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000460 if (cur->prefix != NULL)
461 ret->prefix = xmlStrdup(cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000462 ret->ocur = cur->ocur;
463 if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000464 if (ret->c1 != NULL)
465 ret->c1->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000466 if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +0000467 if (ret->c2 != NULL)
468 ret->c2->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000469 return(ret);
470}
471
472/**
473 * xmlFreeElementContent:
474 * @cur: the element content tree to free
475 *
476 * Free an element content structure. This is a recursive call !
477 */
478void
479xmlFreeElementContent(xmlElementContentPtr cur) {
480 if (cur == NULL) return;
481 switch (cur->type) {
482 case XML_ELEMENT_CONTENT_PCDATA:
483 case XML_ELEMENT_CONTENT_ELEMENT:
484 case XML_ELEMENT_CONTENT_SEQ:
485 case XML_ELEMENT_CONTENT_OR:
486 break;
487 default:
488 xmlGenericError(xmlGenericErrorContext,
489 "xmlFreeElementContent : type %d\n", cur->type);
490 return;
491 }
492 if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
493 if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
494 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000495 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000496 xmlFree(cur);
497}
498
499/**
500 * xmlDumpElementContent:
501 * @buf: An XML buffer
502 * @content: An element table
503 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
504 *
505 * This will dump the content of the element table as an XML DTD definition
506 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000507static void
Owen Taylor3473f882001-02-23 17:55:21 +0000508xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
509 if (content == NULL) return;
510
511 if (glob) xmlBufferWriteChar(buf, "(");
512 switch (content->type) {
513 case XML_ELEMENT_CONTENT_PCDATA:
514 xmlBufferWriteChar(buf, "#PCDATA");
515 break;
516 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000517 if (content->prefix != NULL) {
518 xmlBufferWriteCHAR(buf, content->prefix);
519 xmlBufferWriteChar(buf, ":");
520 }
Owen Taylor3473f882001-02-23 17:55:21 +0000521 xmlBufferWriteCHAR(buf, content->name);
522 break;
523 case XML_ELEMENT_CONTENT_SEQ:
524 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
525 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
526 xmlDumpElementContent(buf, content->c1, 1);
527 else
528 xmlDumpElementContent(buf, content->c1, 0);
529 xmlBufferWriteChar(buf, " , ");
530 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
531 xmlDumpElementContent(buf, content->c2, 1);
532 else
533 xmlDumpElementContent(buf, content->c2, 0);
534 break;
535 case XML_ELEMENT_CONTENT_OR:
536 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
537 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
538 xmlDumpElementContent(buf, content->c1, 1);
539 else
540 xmlDumpElementContent(buf, content->c1, 0);
541 xmlBufferWriteChar(buf, " | ");
542 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
543 xmlDumpElementContent(buf, content->c2, 1);
544 else
545 xmlDumpElementContent(buf, content->c2, 0);
546 break;
547 default:
548 xmlGenericError(xmlGenericErrorContext,
549 "xmlDumpElementContent: unknown type %d\n",
550 content->type);
551 }
552 if (glob)
553 xmlBufferWriteChar(buf, ")");
554 switch (content->ocur) {
555 case XML_ELEMENT_CONTENT_ONCE:
556 break;
557 case XML_ELEMENT_CONTENT_OPT:
558 xmlBufferWriteChar(buf, "?");
559 break;
560 case XML_ELEMENT_CONTENT_MULT:
561 xmlBufferWriteChar(buf, "*");
562 break;
563 case XML_ELEMENT_CONTENT_PLUS:
564 xmlBufferWriteChar(buf, "+");
565 break;
566 }
567}
568
569/**
570 * xmlSprintfElementContent:
571 * @buf: an output buffer
572 * @content: An element table
573 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
574 *
Daniel Veillardd3d06722001-08-15 12:06:36 +0000575 * Deprecated, unsafe, use xmlSnprintfElementContent
576 */
577void
578xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
579 xmlElementContentPtr content ATTRIBUTE_UNUSED,
580 int glob ATTRIBUTE_UNUSED) {
581}
582
583/**
584 * xmlSnprintfElementContent:
585 * @buf: an output buffer
586 * @size: the buffer size
587 * @content: An element table
588 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
589 *
Owen Taylor3473f882001-02-23 17:55:21 +0000590 * This will dump the content of the element content definition
591 * Intended just for the debug routine
592 */
593void
Daniel Veillardd3d06722001-08-15 12:06:36 +0000594xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
595 int len;
596
Owen Taylor3473f882001-02-23 17:55:21 +0000597 if (content == NULL) return;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000598 len = strlen(buf);
599 if (size - len < 50) {
600 if ((size - len > 4) && (buf[len - 1] != '.'))
601 strcat(buf, " ...");
602 return;
603 }
Owen Taylor3473f882001-02-23 17:55:21 +0000604 if (glob) strcat(buf, "(");
605 switch (content->type) {
606 case XML_ELEMENT_CONTENT_PCDATA:
607 strcat(buf, "#PCDATA");
608 break;
609 case XML_ELEMENT_CONTENT_ELEMENT:
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000610 if (content->prefix != NULL) {
611 if (size - len < xmlStrlen(content->prefix + 10)) {
612 strcat(buf, " ...");
613 return;
614 }
615 strcat(buf, (char *) content->prefix);
616 strcat(buf, ":");
617 }
Daniel Veillardd3d06722001-08-15 12:06:36 +0000618 if (size - len < xmlStrlen(content->name + 10)) {
619 strcat(buf, " ...");
620 return;
621 }
Owen Taylor3473f882001-02-23 17:55:21 +0000622 strcat(buf, (char *) content->name);
623 break;
624 case XML_ELEMENT_CONTENT_SEQ:
625 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
626 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000627 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000628 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000629 xmlSnprintfElementContent(buf, size, content->c1, 0);
630 len = strlen(buf);
631 if (size - len < 50) {
632 if ((size - len > 4) && (buf[len - 1] != '.'))
633 strcat(buf, " ...");
634 return;
635 }
Owen Taylor3473f882001-02-23 17:55:21 +0000636 strcat(buf, " , ");
637 if (content->c2->type == XML_ELEMENT_CONTENT_OR)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000638 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000639 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000640 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000641 break;
642 case XML_ELEMENT_CONTENT_OR:
643 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
644 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
Daniel Veillardd3d06722001-08-15 12:06:36 +0000645 xmlSnprintfElementContent(buf, size, content->c1, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000646 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000647 xmlSnprintfElementContent(buf, size, content->c1, 0);
648 len = strlen(buf);
649 if (size - len < 50) {
650 if ((size - len > 4) && (buf[len - 1] != '.'))
651 strcat(buf, " ...");
652 return;
653 }
Owen Taylor3473f882001-02-23 17:55:21 +0000654 strcat(buf, " | ");
655 if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
Daniel Veillardd3d06722001-08-15 12:06:36 +0000656 xmlSnprintfElementContent(buf, size, content->c2, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000657 else
Daniel Veillardd3d06722001-08-15 12:06:36 +0000658 xmlSnprintfElementContent(buf, size, content->c2, 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000659 break;
660 }
661 if (glob)
662 strcat(buf, ")");
663 switch (content->ocur) {
664 case XML_ELEMENT_CONTENT_ONCE:
665 break;
666 case XML_ELEMENT_CONTENT_OPT:
667 strcat(buf, "?");
668 break;
669 case XML_ELEMENT_CONTENT_MULT:
670 strcat(buf, "*");
671 break;
672 case XML_ELEMENT_CONTENT_PLUS:
673 strcat(buf, "+");
674 break;
675 }
676}
677
678/****************************************************************
679 * *
680 * Registration of DTD declarations *
681 * *
682 ****************************************************************/
683
684/**
685 * xmlCreateElementTable:
686 *
687 * create and initialize an empty element hash table.
688 *
689 * Returns the xmlElementTablePtr just created or NULL in case of error.
690 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000691static xmlElementTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +0000692xmlCreateElementTable(void) {
693 return(xmlHashCreate(0));
694}
695
696/**
697 * xmlFreeElement:
698 * @elem: An element
699 *
700 * Deallocate the memory used by an element definition
701 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000702static void
Owen Taylor3473f882001-02-23 17:55:21 +0000703xmlFreeElement(xmlElementPtr elem) {
704 if (elem == NULL) return;
705 xmlUnlinkNode((xmlNodePtr) elem);
706 xmlFreeElementContent(elem->content);
707 if (elem->name != NULL)
708 xmlFree((xmlChar *) elem->name);
709 if (elem->prefix != NULL)
710 xmlFree((xmlChar *) elem->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000711 xmlFree(elem);
712}
713
714
715/**
716 * xmlAddElementDecl:
717 * @ctxt: the validation context
718 * @dtd: pointer to the DTD
719 * @name: the entity name
720 * @type: the element type
721 * @content: the element content tree or NULL
722 *
723 * Register a new element declaration
724 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000725 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +0000726 */
727xmlElementPtr
728xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
729 xmlElementTypeVal type,
730 xmlElementContentPtr content) {
731 xmlElementPtr ret;
732 xmlElementTablePtr table;
Daniel Veillarda10efa82001-04-18 13:09:01 +0000733 xmlAttributePtr oldAttributes = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000734 xmlChar *ns, *uqname;
735
736 if (dtd == NULL) {
737 xmlGenericError(xmlGenericErrorContext,
738 "xmlAddElementDecl: dtd == NULL\n");
739 return(NULL);
740 }
741 if (name == NULL) {
742 xmlGenericError(xmlGenericErrorContext,
743 "xmlAddElementDecl: name == NULL\n");
744 return(NULL);
745 }
746 switch (type) {
747 case XML_ELEMENT_TYPE_EMPTY:
748 if (content != NULL) {
749 xmlGenericError(xmlGenericErrorContext,
750 "xmlAddElementDecl: content != NULL for EMPTY\n");
751 return(NULL);
752 }
753 break;
754 case XML_ELEMENT_TYPE_ANY:
755 if (content != NULL) {
756 xmlGenericError(xmlGenericErrorContext,
757 "xmlAddElementDecl: content != NULL for ANY\n");
758 return(NULL);
759 }
760 break;
761 case XML_ELEMENT_TYPE_MIXED:
762 if (content == NULL) {
763 xmlGenericError(xmlGenericErrorContext,
764 "xmlAddElementDecl: content == NULL for MIXED\n");
765 return(NULL);
766 }
767 break;
768 case XML_ELEMENT_TYPE_ELEMENT:
769 if (content == NULL) {
770 xmlGenericError(xmlGenericErrorContext,
771 "xmlAddElementDecl: content == NULL for ELEMENT\n");
772 return(NULL);
773 }
774 break;
775 default:
776 xmlGenericError(xmlGenericErrorContext,
777 "xmlAddElementDecl: unknown type %d\n", type);
778 return(NULL);
779 }
780
781 /*
782 * check if name is a QName
783 */
784 uqname = xmlSplitQName2(name, &ns);
785 if (uqname != NULL)
786 name = uqname;
787
788 /*
789 * Create the Element table if needed.
790 */
791 table = (xmlElementTablePtr) dtd->elements;
792 if (table == NULL) {
793 table = xmlCreateElementTable();
794 dtd->elements = (void *) table;
795 }
796 if (table == NULL) {
797 xmlGenericError(xmlGenericErrorContext,
798 "xmlAddElementDecl: Table creation failed!\n");
799 return(NULL);
800 }
801
Daniel Veillarda10efa82001-04-18 13:09:01 +0000802 /*
803 * lookup old attributes inserted on an undefined element in the
804 * internal subset.
805 */
806 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
807 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
808 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
809 oldAttributes = ret->attributes;
810 ret->attributes = NULL;
811 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
812 xmlFreeElement(ret);
813 }
Owen Taylor3473f882001-02-23 17:55:21 +0000814 }
Owen Taylor3473f882001-02-23 17:55:21 +0000815
816 /*
Daniel Veillarda10efa82001-04-18 13:09:01 +0000817 * The element may already be present if one of its attribute
818 * was registered first
819 */
820 ret = xmlHashLookup2(table, name, ns);
821 if (ret != NULL) {
822 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
823 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000824 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000825 */
826 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
827 if (uqname != NULL)
828 xmlFree(uqname);
829 return(NULL);
830 }
831 } else {
832 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
833 if (ret == NULL) {
834 xmlGenericError(xmlGenericErrorContext,
835 "xmlAddElementDecl: out of memory\n");
836 return(NULL);
837 }
838 memset(ret, 0, sizeof(xmlElement));
839 ret->type = XML_ELEMENT_DECL;
840
841 /*
842 * fill the structure.
843 */
844 ret->name = xmlStrdup(name);
845 ret->prefix = ns;
846
847 /*
848 * Validity Check:
849 * Insertion must not fail
850 */
851 if (xmlHashAddEntry2(table, name, ns, ret)) {
852 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000853 * The element is already defined in this DTD.
Daniel Veillarda10efa82001-04-18 13:09:01 +0000854 */
855 VERROR(ctxt->userData, "Redefinition of element %s\n", name);
856 xmlFreeElement(ret);
857 if (uqname != NULL)
858 xmlFree(uqname);
859 return(NULL);
860 }
861 }
862
863 /*
864 * Finish to fill the structure.
Owen Taylor3473f882001-02-23 17:55:21 +0000865 */
866 ret->etype = type;
Owen Taylor3473f882001-02-23 17:55:21 +0000867 ret->content = xmlCopyElementContent(content);
Daniel Veillarda10efa82001-04-18 13:09:01 +0000868 ret->attributes = oldAttributes;
Owen Taylor3473f882001-02-23 17:55:21 +0000869
870 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000871 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000872 */
873 ret->parent = dtd;
874 ret->doc = dtd->doc;
875 if (dtd->last == NULL) {
876 dtd->children = dtd->last = (xmlNodePtr) ret;
877 } else {
878 dtd->last->next = (xmlNodePtr) ret;
879 ret->prev = dtd->last;
880 dtd->last = (xmlNodePtr) ret;
881 }
882 if (uqname != NULL)
883 xmlFree(uqname);
884 return(ret);
885}
886
887/**
888 * xmlFreeElementTable:
889 * @table: An element table
890 *
891 * Deallocate the memory used by an element hash table.
892 */
893void
894xmlFreeElementTable(xmlElementTablePtr table) {
895 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
896}
897
898/**
899 * xmlCopyElement:
900 * @elem: An element
901 *
902 * Build a copy of an element.
903 *
904 * Returns the new xmlElementPtr or NULL in case of error.
905 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000906static xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +0000907xmlCopyElement(xmlElementPtr elem) {
908 xmlElementPtr cur;
909
910 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
911 if (cur == NULL) {
912 xmlGenericError(xmlGenericErrorContext,
913 "xmlCopyElement: out of memory !\n");
914 return(NULL);
915 }
916 memset(cur, 0, sizeof(xmlElement));
917 cur->type = XML_ELEMENT_DECL;
918 cur->etype = elem->etype;
919 if (elem->name != NULL)
920 cur->name = xmlStrdup(elem->name);
921 else
922 cur->name = NULL;
923 if (elem->prefix != NULL)
924 cur->prefix = xmlStrdup(elem->prefix);
925 else
926 cur->prefix = NULL;
927 cur->content = xmlCopyElementContent(elem->content);
928 /* TODO : rebuild the attribute list on the copy */
929 cur->attributes = NULL;
930 return(cur);
931}
932
933/**
934 * xmlCopyElementTable:
935 * @table: An element table
936 *
937 * Build a copy of an element table.
938 *
939 * Returns the new xmlElementTablePtr or NULL in case of error.
940 */
941xmlElementTablePtr
942xmlCopyElementTable(xmlElementTablePtr table) {
943 return((xmlElementTablePtr) xmlHashCopy(table,
944 (xmlHashCopier) xmlCopyElement));
945}
946
947/**
948 * xmlDumpElementDecl:
949 * @buf: the XML buffer output
950 * @elem: An element table
951 *
952 * This will dump the content of the element declaration as an XML
953 * DTD definition
954 */
955void
956xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
957 switch (elem->etype) {
958 case XML_ELEMENT_TYPE_EMPTY:
959 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000960 if (elem->prefix != NULL) {
961 xmlBufferWriteCHAR(buf, elem->prefix);
962 xmlBufferWriteChar(buf, ":");
963 }
Owen Taylor3473f882001-02-23 17:55:21 +0000964 xmlBufferWriteCHAR(buf, elem->name);
965 xmlBufferWriteChar(buf, " EMPTY>\n");
966 break;
967 case XML_ELEMENT_TYPE_ANY:
968 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000969 if (elem->prefix != NULL) {
970 xmlBufferWriteCHAR(buf, elem->prefix);
971 xmlBufferWriteChar(buf, ":");
972 }
Owen Taylor3473f882001-02-23 17:55:21 +0000973 xmlBufferWriteCHAR(buf, elem->name);
974 xmlBufferWriteChar(buf, " ANY>\n");
975 break;
976 case XML_ELEMENT_TYPE_MIXED:
977 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000978 if (elem->prefix != NULL) {
979 xmlBufferWriteCHAR(buf, elem->prefix);
980 xmlBufferWriteChar(buf, ":");
981 }
Owen Taylor3473f882001-02-23 17:55:21 +0000982 xmlBufferWriteCHAR(buf, elem->name);
983 xmlBufferWriteChar(buf, " ");
984 xmlDumpElementContent(buf, elem->content, 1);
985 xmlBufferWriteChar(buf, ">\n");
986 break;
987 case XML_ELEMENT_TYPE_ELEMENT:
988 xmlBufferWriteChar(buf, "<!ELEMENT ");
Daniel Veillardbe480fb2001-11-08 23:36:42 +0000989 if (elem->prefix != NULL) {
990 xmlBufferWriteCHAR(buf, elem->prefix);
991 xmlBufferWriteChar(buf, ":");
992 }
Owen Taylor3473f882001-02-23 17:55:21 +0000993 xmlBufferWriteCHAR(buf, elem->name);
994 xmlBufferWriteChar(buf, " ");
995 xmlDumpElementContent(buf, elem->content, 1);
996 xmlBufferWriteChar(buf, ">\n");
997 break;
998 default:
999 xmlGenericError(xmlGenericErrorContext,
1000 "xmlDumpElementDecl: internal: unknown type %d\n",
1001 elem->etype);
1002 }
1003}
1004
1005/**
1006 * xmlDumpElementTable:
1007 * @buf: the XML buffer output
1008 * @table: An element table
1009 *
1010 * This will dump the content of the element table as an XML DTD definition
1011 */
1012void
1013xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1014 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
1015}
1016
1017/**
1018 * xmlCreateEnumeration:
1019 * @name: the enumeration name or NULL
1020 *
1021 * create and initialize an enumeration attribute node.
1022 *
1023 * Returns the xmlEnumerationPtr just created or NULL in case
1024 * of error.
1025 */
1026xmlEnumerationPtr
1027xmlCreateEnumeration(xmlChar *name) {
1028 xmlEnumerationPtr ret;
1029
1030 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1031 if (ret == NULL) {
1032 xmlGenericError(xmlGenericErrorContext,
1033 "xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
1034 (long)sizeof(xmlEnumeration));
1035 return(NULL);
1036 }
1037 memset(ret, 0, sizeof(xmlEnumeration));
1038
1039 if (name != NULL)
1040 ret->name = xmlStrdup(name);
1041 return(ret);
1042}
1043
1044/**
1045 * xmlFreeEnumeration:
1046 * @cur: the tree to free.
1047 *
1048 * free an enumeration attribute node (recursive).
1049 */
1050void
1051xmlFreeEnumeration(xmlEnumerationPtr cur) {
1052 if (cur == NULL) return;
1053
1054 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1055
1056 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001057 xmlFree(cur);
1058}
1059
1060/**
1061 * xmlCopyEnumeration:
1062 * @cur: the tree to copy.
1063 *
1064 * Copy an enumeration attribute node (recursive).
1065 *
1066 * Returns the xmlEnumerationPtr just created or NULL in case
1067 * of error.
1068 */
1069xmlEnumerationPtr
1070xmlCopyEnumeration(xmlEnumerationPtr cur) {
1071 xmlEnumerationPtr ret;
1072
1073 if (cur == NULL) return(NULL);
1074 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1075
1076 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1077 else ret->next = NULL;
1078
1079 return(ret);
1080}
1081
1082/**
1083 * xmlDumpEnumeration:
1084 * @buf: the XML buffer output
1085 * @enum: An enumeration
1086 *
1087 * This will dump the content of the enumeration
1088 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001089static void
Owen Taylor3473f882001-02-23 17:55:21 +00001090xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1091 if (cur == NULL) return;
1092
1093 xmlBufferWriteCHAR(buf, cur->name);
1094 if (cur->next == NULL)
1095 xmlBufferWriteChar(buf, ")");
1096 else {
1097 xmlBufferWriteChar(buf, " | ");
1098 xmlDumpEnumeration(buf, cur->next);
1099 }
1100}
1101
1102/**
1103 * xmlCreateAttributeTable:
1104 *
1105 * create and initialize an empty attribute hash table.
1106 *
1107 * Returns the xmlAttributeTablePtr just created or NULL in case
1108 * of error.
1109 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001110static xmlAttributeTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001111xmlCreateAttributeTable(void) {
1112 return(xmlHashCreate(0));
1113}
1114
1115/**
1116 * xmlScanAttributeDeclCallback:
1117 * @attr: the attribute decl
1118 * @list: the list to update
1119 *
1120 * Callback called by xmlScanAttributeDecl when a new attribute
1121 * has to be entered in the list.
1122 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001123static void
Owen Taylor3473f882001-02-23 17:55:21 +00001124xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001125 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001126 attr->nexth = *list;
1127 *list = attr;
1128}
1129
1130/**
1131 * xmlScanAttributeDecl:
1132 * @dtd: pointer to the DTD
1133 * @elem: the element name
1134 *
1135 * When inserting a new element scan the DtD for existing attributes
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001136 * for that element and initialize the Attribute chain
Owen Taylor3473f882001-02-23 17:55:21 +00001137 *
1138 * Returns the pointer to the first attribute decl in the chain,
1139 * possibly NULL.
1140 */
1141xmlAttributePtr
1142xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
1143 xmlAttributePtr ret = NULL;
1144 xmlAttributeTablePtr table;
1145
1146 if (dtd == NULL) {
1147 xmlGenericError(xmlGenericErrorContext,
1148 "xmlScanAttributeDecl: dtd == NULL\n");
1149 return(NULL);
1150 }
1151 if (elem == NULL) {
1152 xmlGenericError(xmlGenericErrorContext,
1153 "xmlScanAttributeDecl: elem == NULL\n");
1154 return(NULL);
1155 }
1156 table = (xmlAttributeTablePtr) dtd->attributes;
1157 if (table == NULL)
1158 return(NULL);
1159
1160 /* WRONG !!! */
1161 xmlHashScan3(table, NULL, NULL, elem,
1162 (xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
1163 return(ret);
1164}
1165
1166/**
1167 * xmlScanIDAttributeDecl:
1168 * @ctxt: the validation context
1169 * @elem: the element name
1170 *
1171 * Verify that the element don't have too many ID attributes
1172 * declared.
1173 *
1174 * Returns the number of ID attributes found.
1175 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001176static int
Owen Taylor3473f882001-02-23 17:55:21 +00001177xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
1178 xmlAttributePtr cur;
1179 int ret = 0;
1180
1181 if (elem == NULL) return(0);
1182 cur = elem->attributes;
1183 while (cur != NULL) {
1184 if (cur->atype == XML_ATTRIBUTE_ID) {
1185 ret ++;
1186 if (ret > 1)
1187 VERROR(ctxt->userData,
Daniel Veillarda10efa82001-04-18 13:09:01 +00001188 "Element %s has too many ID attributes defined : %s\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001189 elem->name, cur->name);
1190 }
1191 cur = cur->nexth;
1192 }
1193 return(ret);
1194}
1195
1196/**
1197 * xmlFreeAttribute:
1198 * @elem: An attribute
1199 *
1200 * Deallocate the memory used by an attribute definition
1201 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001202static void
Owen Taylor3473f882001-02-23 17:55:21 +00001203xmlFreeAttribute(xmlAttributePtr attr) {
1204 if (attr == NULL) return;
1205 xmlUnlinkNode((xmlNodePtr) attr);
1206 if (attr->tree != NULL)
1207 xmlFreeEnumeration(attr->tree);
1208 if (attr->elem != NULL)
1209 xmlFree((xmlChar *) attr->elem);
1210 if (attr->name != NULL)
1211 xmlFree((xmlChar *) attr->name);
1212 if (attr->defaultValue != NULL)
1213 xmlFree((xmlChar *) attr->defaultValue);
1214 if (attr->prefix != NULL)
1215 xmlFree((xmlChar *) attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001216 xmlFree(attr);
1217}
1218
1219
1220/**
1221 * xmlAddAttributeDecl:
1222 * @ctxt: the validation context
1223 * @dtd: pointer to the DTD
1224 * @elem: the element name
1225 * @name: the attribute name
1226 * @ns: the attribute namespace prefix
1227 * @type: the attribute type
1228 * @def: the attribute default type
1229 * @defaultValue: the attribute default value
1230 * @tree: if it's an enumeration, the associated list
1231 *
1232 * Register a new attribute declaration
1233 * Note that @tree becomes the ownership of the DTD
1234 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001235 * Returns NULL if not new, otherwise the attribute decl
Owen Taylor3473f882001-02-23 17:55:21 +00001236 */
1237xmlAttributePtr
1238xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
1239 const xmlChar *name, const xmlChar *ns,
1240 xmlAttributeType type, xmlAttributeDefault def,
1241 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1242 xmlAttributePtr ret;
1243 xmlAttributeTablePtr table;
1244 xmlElementPtr elemDef;
1245
1246 if (dtd == NULL) {
1247 xmlGenericError(xmlGenericErrorContext,
1248 "xmlAddAttributeDecl: dtd == NULL\n");
1249 xmlFreeEnumeration(tree);
1250 return(NULL);
1251 }
1252 if (name == NULL) {
1253 xmlGenericError(xmlGenericErrorContext,
1254 "xmlAddAttributeDecl: name == NULL\n");
1255 xmlFreeEnumeration(tree);
1256 return(NULL);
1257 }
1258 if (elem == NULL) {
1259 xmlGenericError(xmlGenericErrorContext,
1260 "xmlAddAttributeDecl: elem == NULL\n");
1261 xmlFreeEnumeration(tree);
1262 return(NULL);
1263 }
1264 /*
1265 * Check the type and possibly the default value.
1266 */
1267 switch (type) {
1268 case XML_ATTRIBUTE_CDATA:
1269 break;
1270 case XML_ATTRIBUTE_ID:
1271 break;
1272 case XML_ATTRIBUTE_IDREF:
1273 break;
1274 case XML_ATTRIBUTE_IDREFS:
1275 break;
1276 case XML_ATTRIBUTE_ENTITY:
1277 break;
1278 case XML_ATTRIBUTE_ENTITIES:
1279 break;
1280 case XML_ATTRIBUTE_NMTOKEN:
1281 break;
1282 case XML_ATTRIBUTE_NMTOKENS:
1283 break;
1284 case XML_ATTRIBUTE_ENUMERATION:
1285 break;
1286 case XML_ATTRIBUTE_NOTATION:
1287 break;
1288 default:
1289 xmlGenericError(xmlGenericErrorContext,
1290 "xmlAddAttributeDecl: unknown type %d\n", type);
1291 xmlFreeEnumeration(tree);
1292 return(NULL);
1293 }
1294 if ((defaultValue != NULL) &&
1295 (!xmlValidateAttributeValue(type, defaultValue))) {
1296 VERROR(ctxt->userData, "Attribute %s on %s: invalid default value\n",
1297 elem, name, defaultValue);
1298 defaultValue = NULL;
1299 }
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) &&
1360 (xmlScanIDAttributeDecl(NULL, elemDef) != 0))
1361 VERROR(ctxt->userData,
1362 "Element %s has too may ID attributes defined : %s\n",
1363 elem, name);
Daniel Veillard48da9102001-08-07 01:10:10 +00001364 /*
1365 * Insert namespace default def first they need to be
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001366 * processed first.
Daniel Veillard48da9102001-08-07 01:10:10 +00001367 */
1368 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1369 ((ret->prefix != NULL &&
1370 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1371 ret->nexth = elemDef->attributes;
1372 elemDef->attributes = ret;
1373 } else {
1374 xmlAttributePtr tmp = elemDef->attributes;
1375
1376 while ((tmp != NULL) &&
1377 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1378 ((ret->prefix != NULL &&
1379 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1380 if (tmp->nexth == NULL)
1381 break;
1382 tmp = tmp->nexth;
1383 }
1384 if (tmp != NULL) {
1385 ret->nexth = tmp->nexth;
1386 tmp->nexth = ret;
1387 } else {
1388 ret->nexth = elemDef->attributes;
1389 elemDef->attributes = ret;
1390 }
1391 }
Owen Taylor3473f882001-02-23 17:55:21 +00001392 }
1393
1394 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001395 * Link it to the DTD
Owen Taylor3473f882001-02-23 17:55:21 +00001396 */
1397 ret->parent = dtd;
1398 ret->doc = dtd->doc;
1399 if (dtd->last == NULL) {
1400 dtd->children = dtd->last = (xmlNodePtr) ret;
1401 } else {
1402 dtd->last->next = (xmlNodePtr) ret;
1403 ret->prev = dtd->last;
1404 dtd->last = (xmlNodePtr) ret;
1405 }
1406 return(ret);
1407}
1408
1409/**
1410 * xmlFreeAttributeTable:
1411 * @table: An attribute table
1412 *
1413 * Deallocate the memory used by an entities hash table.
1414 */
1415void
1416xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1417 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
1418}
1419
1420/**
1421 * xmlCopyAttribute:
1422 * @attr: An attribute
1423 *
1424 * Build a copy of an attribute.
1425 *
1426 * Returns the new xmlAttributePtr or NULL in case of error.
1427 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001428static xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001429xmlCopyAttribute(xmlAttributePtr attr) {
1430 xmlAttributePtr cur;
1431
1432 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1433 if (cur == NULL) {
1434 xmlGenericError(xmlGenericErrorContext,
1435 "xmlCopyAttribute: out of memory !\n");
1436 return(NULL);
1437 }
1438 memset(cur, 0, sizeof(xmlAttribute));
Daniel Veillard36065812002-01-24 15:02:46 +00001439 cur->type = XML_ATTRIBUTE_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00001440 cur->atype = attr->atype;
1441 cur->def = attr->def;
1442 cur->tree = xmlCopyEnumeration(attr->tree);
1443 if (attr->elem != NULL)
1444 cur->elem = xmlStrdup(attr->elem);
1445 if (attr->name != NULL)
1446 cur->name = xmlStrdup(attr->name);
Daniel Veillard36065812002-01-24 15:02:46 +00001447 if (attr->prefix != NULL)
1448 cur->prefix = xmlStrdup(attr->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001449 if (attr->defaultValue != NULL)
1450 cur->defaultValue = xmlStrdup(attr->defaultValue);
1451 return(cur);
1452}
1453
1454/**
1455 * xmlCopyAttributeTable:
1456 * @table: An attribute table
1457 *
1458 * Build a copy of an attribute table.
1459 *
1460 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1461 */
1462xmlAttributeTablePtr
1463xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1464 return((xmlAttributeTablePtr) xmlHashCopy(table,
1465 (xmlHashCopier) xmlCopyAttribute));
1466}
1467
1468/**
1469 * xmlDumpAttributeDecl:
1470 * @buf: the XML buffer output
1471 * @attr: An attribute declaration
1472 *
1473 * This will dump the content of the attribute declaration as an XML
1474 * DTD definition
1475 */
1476void
1477xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1478 xmlBufferWriteChar(buf, "<!ATTLIST ");
1479 xmlBufferWriteCHAR(buf, attr->elem);
1480 xmlBufferWriteChar(buf, " ");
1481 if (attr->prefix != NULL) {
1482 xmlBufferWriteCHAR(buf, attr->prefix);
1483 xmlBufferWriteChar(buf, ":");
1484 }
1485 xmlBufferWriteCHAR(buf, attr->name);
1486 switch (attr->atype) {
1487 case XML_ATTRIBUTE_CDATA:
1488 xmlBufferWriteChar(buf, " CDATA");
1489 break;
1490 case XML_ATTRIBUTE_ID:
1491 xmlBufferWriteChar(buf, " ID");
1492 break;
1493 case XML_ATTRIBUTE_IDREF:
1494 xmlBufferWriteChar(buf, " IDREF");
1495 break;
1496 case XML_ATTRIBUTE_IDREFS:
1497 xmlBufferWriteChar(buf, " IDREFS");
1498 break;
1499 case XML_ATTRIBUTE_ENTITY:
1500 xmlBufferWriteChar(buf, " ENTITY");
1501 break;
1502 case XML_ATTRIBUTE_ENTITIES:
1503 xmlBufferWriteChar(buf, " ENTITIES");
1504 break;
1505 case XML_ATTRIBUTE_NMTOKEN:
1506 xmlBufferWriteChar(buf, " NMTOKEN");
1507 break;
1508 case XML_ATTRIBUTE_NMTOKENS:
1509 xmlBufferWriteChar(buf, " NMTOKENS");
1510 break;
1511 case XML_ATTRIBUTE_ENUMERATION:
1512 xmlBufferWriteChar(buf, " (");
1513 xmlDumpEnumeration(buf, attr->tree);
1514 break;
1515 case XML_ATTRIBUTE_NOTATION:
1516 xmlBufferWriteChar(buf, " NOTATION (");
1517 xmlDumpEnumeration(buf, attr->tree);
1518 break;
1519 default:
1520 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001521 "xmlDumpAttributeDecl: internal: unknown type %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001522 attr->atype);
1523 }
1524 switch (attr->def) {
1525 case XML_ATTRIBUTE_NONE:
1526 break;
1527 case XML_ATTRIBUTE_REQUIRED:
1528 xmlBufferWriteChar(buf, " #REQUIRED");
1529 break;
1530 case XML_ATTRIBUTE_IMPLIED:
1531 xmlBufferWriteChar(buf, " #IMPLIED");
1532 break;
1533 case XML_ATTRIBUTE_FIXED:
1534 xmlBufferWriteChar(buf, " #FIXED");
1535 break;
1536 default:
1537 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001538 "xmlDumpAttributeDecl: internal: unknown default %d\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001539 attr->def);
1540 }
1541 if (attr->defaultValue != NULL) {
1542 xmlBufferWriteChar(buf, " ");
1543 xmlBufferWriteQuotedString(buf, attr->defaultValue);
1544 }
1545 xmlBufferWriteChar(buf, ">\n");
1546}
1547
1548/**
1549 * xmlDumpAttributeTable:
1550 * @buf: the XML buffer output
1551 * @table: An attribute table
1552 *
1553 * This will dump the content of the attribute table as an XML DTD definition
1554 */
1555void
1556xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1557 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
1558}
1559
1560/************************************************************************
1561 * *
1562 * NOTATIONs *
1563 * *
1564 ************************************************************************/
1565/**
1566 * xmlCreateNotationTable:
1567 *
1568 * create and initialize an empty notation hash table.
1569 *
1570 * Returns the xmlNotationTablePtr just created or NULL in case
1571 * of error.
1572 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001573static xmlNotationTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001574xmlCreateNotationTable(void) {
1575 return(xmlHashCreate(0));
1576}
1577
1578/**
1579 * xmlFreeNotation:
1580 * @not: A notation
1581 *
1582 * Deallocate the memory used by an notation definition
1583 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001584static void
Owen Taylor3473f882001-02-23 17:55:21 +00001585xmlFreeNotation(xmlNotationPtr nota) {
1586 if (nota == NULL) return;
1587 if (nota->name != NULL)
1588 xmlFree((xmlChar *) nota->name);
1589 if (nota->PublicID != NULL)
1590 xmlFree((xmlChar *) nota->PublicID);
1591 if (nota->SystemID != NULL)
1592 xmlFree((xmlChar *) nota->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +00001593 xmlFree(nota);
1594}
1595
1596
1597/**
1598 * xmlAddNotationDecl:
1599 * @dtd: pointer to the DTD
1600 * @ctxt: the validation context
1601 * @name: the entity name
1602 * @PublicID: the public identifier or NULL
1603 * @SystemID: the system identifier or NULL
1604 *
1605 * Register a new notation declaration
1606 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001607 * Returns NULL if not, otherwise the entity
Owen Taylor3473f882001-02-23 17:55:21 +00001608 */
1609xmlNotationPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001610xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001611 const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00001612 const xmlChar *PublicID, const xmlChar *SystemID) {
1613 xmlNotationPtr ret;
1614 xmlNotationTablePtr table;
1615
1616 if (dtd == NULL) {
1617 xmlGenericError(xmlGenericErrorContext,
1618 "xmlAddNotationDecl: dtd == NULL\n");
1619 return(NULL);
1620 }
1621 if (name == NULL) {
1622 xmlGenericError(xmlGenericErrorContext,
1623 "xmlAddNotationDecl: name == NULL\n");
1624 return(NULL);
1625 }
1626 if ((PublicID == NULL) && (SystemID == NULL)) {
1627 xmlGenericError(xmlGenericErrorContext,
1628 "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
1629 }
1630
1631 /*
1632 * Create the Notation table if needed.
1633 */
1634 table = (xmlNotationTablePtr) dtd->notations;
1635 if (table == NULL)
1636 dtd->notations = table = xmlCreateNotationTable();
1637 if (table == NULL) {
1638 xmlGenericError(xmlGenericErrorContext,
1639 "xmlAddNotationDecl: Table creation failed!\n");
1640 return(NULL);
1641 }
1642
1643 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1644 if (ret == NULL) {
1645 xmlGenericError(xmlGenericErrorContext,
1646 "xmlAddNotationDecl: out of memory\n");
1647 return(NULL);
1648 }
1649 memset(ret, 0, sizeof(xmlNotation));
1650
1651 /*
1652 * fill the structure.
1653 */
1654 ret->name = xmlStrdup(name);
1655 if (SystemID != NULL)
1656 ret->SystemID = xmlStrdup(SystemID);
1657 if (PublicID != NULL)
1658 ret->PublicID = xmlStrdup(PublicID);
1659
1660 /*
1661 * Validity Check:
1662 * Check the DTD for previous declarations of the ATTLIST
1663 */
1664 if (xmlHashAddEntry(table, name, ret)) {
1665 xmlGenericError(xmlGenericErrorContext,
1666 "xmlAddNotationDecl: %s already defined\n", name);
1667 xmlFreeNotation(ret);
1668 return(NULL);
1669 }
1670 return(ret);
1671}
1672
1673/**
1674 * xmlFreeNotationTable:
1675 * @table: An notation table
1676 *
1677 * Deallocate the memory used by an entities hash table.
1678 */
1679void
1680xmlFreeNotationTable(xmlNotationTablePtr table) {
1681 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
1682}
1683
1684/**
1685 * xmlCopyNotation:
1686 * @nota: A notation
1687 *
1688 * Build a copy of a notation.
1689 *
1690 * Returns the new xmlNotationPtr or NULL in case of error.
1691 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001692static xmlNotationPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001693xmlCopyNotation(xmlNotationPtr nota) {
1694 xmlNotationPtr cur;
1695
1696 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1697 if (cur == NULL) {
1698 xmlGenericError(xmlGenericErrorContext,
1699 "xmlCopyNotation: out of memory !\n");
1700 return(NULL);
1701 }
1702 if (nota->name != NULL)
1703 cur->name = xmlStrdup(nota->name);
1704 else
1705 cur->name = NULL;
1706 if (nota->PublicID != NULL)
1707 cur->PublicID = xmlStrdup(nota->PublicID);
1708 else
1709 cur->PublicID = NULL;
1710 if (nota->SystemID != NULL)
1711 cur->SystemID = xmlStrdup(nota->SystemID);
1712 else
1713 cur->SystemID = NULL;
1714 return(cur);
1715}
1716
1717/**
1718 * xmlCopyNotationTable:
1719 * @table: A notation table
1720 *
1721 * Build a copy of a notation table.
1722 *
1723 * Returns the new xmlNotationTablePtr or NULL in case of error.
1724 */
1725xmlNotationTablePtr
1726xmlCopyNotationTable(xmlNotationTablePtr table) {
1727 return((xmlNotationTablePtr) xmlHashCopy(table,
1728 (xmlHashCopier) xmlCopyNotation));
1729}
1730
1731/**
1732 * xmlDumpNotationDecl:
1733 * @buf: the XML buffer output
1734 * @nota: A notation declaration
1735 *
1736 * This will dump the content the notation declaration as an XML DTD definition
1737 */
1738void
1739xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
1740 xmlBufferWriteChar(buf, "<!NOTATION ");
1741 xmlBufferWriteCHAR(buf, nota->name);
1742 if (nota->PublicID != NULL) {
1743 xmlBufferWriteChar(buf, " PUBLIC ");
1744 xmlBufferWriteQuotedString(buf, nota->PublicID);
1745 if (nota->SystemID != NULL) {
1746 xmlBufferWriteChar(buf, " ");
1747 xmlBufferWriteCHAR(buf, nota->SystemID);
1748 }
1749 } else {
1750 xmlBufferWriteChar(buf, " SYSTEM ");
1751 xmlBufferWriteCHAR(buf, nota->SystemID);
1752 }
1753 xmlBufferWriteChar(buf, " >\n");
1754}
1755
1756/**
1757 * xmlDumpNotationTable:
1758 * @buf: the XML buffer output
1759 * @table: A notation table
1760 *
1761 * This will dump the content of the notation table as an XML DTD definition
1762 */
1763void
1764xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
1765 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
1766}
1767
1768/************************************************************************
1769 * *
1770 * IDs *
1771 * *
1772 ************************************************************************/
1773/**
1774 * xmlCreateIDTable:
1775 *
1776 * create and initialize an empty id hash table.
1777 *
1778 * Returns the xmlIDTablePtr just created or NULL in case
1779 * of error.
1780 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001781static xmlIDTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00001782xmlCreateIDTable(void) {
1783 return(xmlHashCreate(0));
1784}
1785
1786/**
1787 * xmlFreeID:
1788 * @not: A id
1789 *
1790 * Deallocate the memory used by an id definition
1791 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001792static void
Owen Taylor3473f882001-02-23 17:55:21 +00001793xmlFreeID(xmlIDPtr id) {
1794 if (id == NULL) return;
1795 if (id->value != NULL)
1796 xmlFree((xmlChar *) id->value);
Owen Taylor3473f882001-02-23 17:55:21 +00001797 xmlFree(id);
1798}
1799
1800/**
1801 * xmlAddID:
1802 * @ctxt: the validation context
1803 * @doc: pointer to the document
1804 * @value: the value name
1805 * @attr: the attribute holding the ID
1806 *
1807 * Register a new id declaration
1808 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001809 * Returns NULL if not, otherwise the new xmlIDPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001810 */
1811xmlIDPtr
1812xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
1813 xmlAttrPtr attr) {
1814 xmlIDPtr ret;
1815 xmlIDTablePtr table;
1816
1817 if (doc == NULL) {
1818 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001819 "xmlAddID: doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001820 return(NULL);
1821 }
1822 if (value == NULL) {
1823 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001824 "xmlAddID: value == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001825 return(NULL);
1826 }
1827 if (attr == NULL) {
1828 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001829 "xmlAddID: attr == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001830 return(NULL);
1831 }
1832
1833 /*
1834 * Create the ID table if needed.
1835 */
1836 table = (xmlIDTablePtr) doc->ids;
1837 if (table == NULL)
1838 doc->ids = table = xmlCreateIDTable();
1839 if (table == NULL) {
1840 xmlGenericError(xmlGenericErrorContext,
1841 "xmlAddID: Table creation failed!\n");
1842 return(NULL);
1843 }
1844
1845 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
1846 if (ret == NULL) {
1847 xmlGenericError(xmlGenericErrorContext,
1848 "xmlAddID: out of memory\n");
1849 return(NULL);
1850 }
1851
1852 /*
1853 * fill the structure.
1854 */
1855 ret->value = xmlStrdup(value);
1856 ret->attr = attr;
1857
1858 if (xmlHashAddEntry(table, value, ret) < 0) {
1859 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001860 * The id is already defined in this DTD.
Owen Taylor3473f882001-02-23 17:55:21 +00001861 */
1862 VERROR(ctxt->userData, "ID %s already defined\n", value);
1863 xmlFreeID(ret);
1864 return(NULL);
1865 }
1866 return(ret);
1867}
1868
1869/**
1870 * xmlFreeIDTable:
1871 * @table: An id table
1872 *
1873 * Deallocate the memory used by an ID hash table.
1874 */
1875void
1876xmlFreeIDTable(xmlIDTablePtr table) {
1877 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
1878}
1879
1880/**
1881 * xmlIsID:
1882 * @doc: the document
1883 * @elem: the element carrying the attribute
1884 * @attr: the attribute
1885 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001886 * Determine whether an attribute is of type ID. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00001887 * then this is simple, otherwise we use an heuristic: name ID (upper
1888 * or lowercase).
1889 *
1890 * Returns 0 or 1 depending on the lookup result
1891 */
1892int
1893xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
1894 if (doc == NULL) return(0);
1895 if (attr == NULL) return(0);
1896 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
1897 return(0);
1898 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
1899 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
1900 (xmlStrEqual(BAD_CAST "name", attr->name)))
1901 return(1);
1902 return(0);
1903 } else {
1904 xmlAttributePtr attrDecl;
1905
1906 if (elem == NULL) return(0);
1907 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
1908 if ((attrDecl == NULL) && (doc->extSubset != NULL))
1909 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
1910 attr->name);
1911
1912 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
1913 return(1);
1914 }
1915 return(0);
1916}
1917
1918/**
1919 * xmlRemoveID
1920 * @doc: the document
1921 * @attr: the attribute
1922 *
1923 * Remove the given attribute from the ID table maintained internally.
1924 *
1925 * Returns -1 if the lookup failed and 0 otherwise
1926 */
1927int
1928xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
1929 xmlAttrPtr cur;
1930 xmlIDTablePtr table;
1931 xmlChar *ID;
1932
1933 if (doc == NULL) return(-1);
1934 if (attr == NULL) return(-1);
1935 table = (xmlIDTablePtr) doc->ids;
1936 if (table == NULL)
1937 return(-1);
1938
1939 if (attr == NULL)
1940 return(-1);
1941 ID = xmlNodeListGetString(doc, attr->children, 1);
1942 if (ID == NULL)
1943 return(-1);
1944 cur = xmlHashLookup(table, ID);
1945 if (cur != attr) {
1946 xmlFree(ID);
1947 return(-1);
1948 }
1949 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
1950 xmlFree(ID);
1951 return(0);
1952}
1953
1954/**
1955 * xmlGetID:
1956 * @doc: pointer to the document
1957 * @ID: the ID value
1958 *
1959 * Search the attribute declaring the given ID
1960 *
1961 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
1962 */
1963xmlAttrPtr
1964xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
1965 xmlIDTablePtr table;
1966 xmlIDPtr id;
1967
1968 if (doc == NULL) {
1969 xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
1970 return(NULL);
1971 }
1972
1973 if (ID == NULL) {
1974 xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
1975 return(NULL);
1976 }
1977
1978 table = (xmlIDTablePtr) doc->ids;
1979 if (table == NULL)
1980 return(NULL);
1981
1982 id = xmlHashLookup(table, ID);
1983 if (id == NULL)
1984 return(NULL);
1985 return(id->attr);
1986}
1987
1988/************************************************************************
1989 * *
1990 * Refs *
1991 * *
1992 ************************************************************************/
Daniel Veillard8730c562001-02-26 10:49:57 +00001993typedef struct xmlRemoveMemo_t
Owen Taylor3473f882001-02-23 17:55:21 +00001994{
1995 xmlListPtr l;
1996 xmlAttrPtr ap;
Daniel Veillard8730c562001-02-26 10:49:57 +00001997} xmlRemoveMemo;
1998
1999typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2000
2001typedef struct xmlValidateMemo_t
2002{
2003 xmlValidCtxtPtr ctxt;
2004 const xmlChar *name;
2005} xmlValidateMemo;
2006
2007typedef xmlValidateMemo *xmlValidateMemoPtr;
Owen Taylor3473f882001-02-23 17:55:21 +00002008
2009/**
2010 * xmlCreateRefTable:
2011 *
2012 * create and initialize an empty ref hash table.
2013 *
2014 * Returns the xmlRefTablePtr just created or NULL in case
2015 * of error.
2016 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002017static xmlRefTablePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002018xmlCreateRefTable(void) {
2019 return(xmlHashCreate(0));
2020}
2021
2022/**
2023 * xmlFreeRef:
2024 * @lk: A list link
2025 *
2026 * Deallocate the memory used by a ref definition
2027 */
2028static void
2029xmlFreeRef(xmlLinkPtr lk) {
Daniel Veillard37721922001-05-04 15:21:12 +00002030 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2031 if (ref == NULL) return;
2032 if (ref->value != NULL)
2033 xmlFree((xmlChar *)ref->value);
2034 xmlFree(ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002035}
2036
2037/**
2038 * xmlFreeRefList:
2039 * @list_ref: A list of references.
2040 *
2041 * Deallocate the memory used by a list of references
2042 */
2043static void
2044xmlFreeRefList(xmlListPtr list_ref) {
Daniel Veillard37721922001-05-04 15:21:12 +00002045 if (list_ref == NULL) return;
2046 xmlListDelete(list_ref);
Owen Taylor3473f882001-02-23 17:55:21 +00002047}
2048
2049/**
2050 * xmlWalkRemoveRef:
2051 * @data: Contents of current link
2052 * @user: Value supplied by the user
2053 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002054 * Returns 0 to abort the walk or 1 to continue
Owen Taylor3473f882001-02-23 17:55:21 +00002055 */
2056static int
2057xmlWalkRemoveRef(const void *data, const void *user)
2058{
Daniel Veillard37721922001-05-04 15:21:12 +00002059 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2060 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2061 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
Owen Taylor3473f882001-02-23 17:55:21 +00002062
Daniel Veillard37721922001-05-04 15:21:12 +00002063 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2064 xmlListRemoveFirst(ref_list, (void *)data);
2065 return 0;
2066 }
2067 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002068}
2069
2070/**
2071 * xmlAddRef:
2072 * @ctxt: the validation context
2073 * @doc: pointer to the document
2074 * @value: the value name
2075 * @attr: the attribute holding the Ref
2076 *
2077 * Register a new ref declaration
2078 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002079 * Returns NULL if not, otherwise the new xmlRefPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002080 */
2081xmlRefPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002082xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
Owen Taylor3473f882001-02-23 17:55:21 +00002083 xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002084 xmlRefPtr ret;
2085 xmlRefTablePtr table;
2086 xmlListPtr ref_list;
Owen Taylor3473f882001-02-23 17:55:21 +00002087
Daniel Veillard37721922001-05-04 15:21:12 +00002088 if (doc == NULL) {
2089 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002090 "xmlAddRef: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002091 return(NULL);
2092 }
2093 if (value == NULL) {
2094 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002095 "xmlAddRef: value == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002096 return(NULL);
2097 }
2098 if (attr == NULL) {
2099 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002100 "xmlAddRef: attr == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002101 return(NULL);
2102 }
Owen Taylor3473f882001-02-23 17:55:21 +00002103
Daniel Veillard37721922001-05-04 15:21:12 +00002104 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002105 * Create the Ref table if needed.
2106 */
Daniel Veillard37721922001-05-04 15:21:12 +00002107 table = (xmlRefTablePtr) doc->refs;
2108 if (table == NULL)
2109 doc->refs = table = xmlCreateRefTable();
2110 if (table == NULL) {
2111 xmlGenericError(xmlGenericErrorContext,
2112 "xmlAddRef: Table creation failed!\n");
2113 return(NULL);
2114 }
Owen Taylor3473f882001-02-23 17:55:21 +00002115
Daniel Veillard37721922001-05-04 15:21:12 +00002116 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2117 if (ret == NULL) {
2118 xmlGenericError(xmlGenericErrorContext,
2119 "xmlAddRef: out of memory\n");
2120 return(NULL);
2121 }
Owen Taylor3473f882001-02-23 17:55:21 +00002122
Daniel Veillard37721922001-05-04 15:21:12 +00002123 /*
Owen Taylor3473f882001-02-23 17:55:21 +00002124 * fill the structure.
2125 */
Daniel Veillard37721922001-05-04 15:21:12 +00002126 ret->value = xmlStrdup(value);
2127 ret->attr = attr;
Owen Taylor3473f882001-02-23 17:55:21 +00002128
Daniel Veillard37721922001-05-04 15:21:12 +00002129 /* To add a reference :-
2130 * References are maintained as a list of references,
2131 * Lookup the entry, if no entry create new nodelist
2132 * Add the owning node to the NodeList
2133 * Return the ref
2134 */
Owen Taylor3473f882001-02-23 17:55:21 +00002135
Daniel Veillard37721922001-05-04 15:21:12 +00002136 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2137 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
2138 xmlGenericError(xmlGenericErrorContext,
2139 "xmlAddRef: Reference list creation failed!\n");
2140 return(NULL);
2141 }
2142 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2143 xmlListDelete(ref_list);
2144 xmlGenericError(xmlGenericErrorContext,
2145 "xmlAddRef: Reference list insertion failed!\n");
2146 return(NULL);
2147 }
2148 }
2149 xmlListInsert(ref_list, ret);
2150 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002151}
2152
2153/**
2154 * xmlFreeRefTable:
2155 * @table: An ref table
2156 *
2157 * Deallocate the memory used by an Ref hash table.
2158 */
2159void
2160xmlFreeRefTable(xmlRefTablePtr table) {
Daniel Veillard37721922001-05-04 15:21:12 +00002161 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
Owen Taylor3473f882001-02-23 17:55:21 +00002162}
2163
2164/**
2165 * xmlIsRef:
2166 * @doc: the document
2167 * @elem: the element carrying the attribute
2168 * @attr: the attribute
2169 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002170 * Determine whether an attribute is of type Ref. In case we have DTD(s)
Owen Taylor3473f882001-02-23 17:55:21 +00002171 * then this is simple, otherwise we use an heuristic: name Ref (upper
2172 * or lowercase).
2173 *
2174 * Returns 0 or 1 depending on the lookup result
2175 */
2176int
2177xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002178 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2179 return(0);
2180 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2181 /* TODO @@@ */
2182 return(0);
2183 } else {
2184 xmlAttributePtr attrDecl;
Owen Taylor3473f882001-02-23 17:55:21 +00002185
Daniel Veillard37721922001-05-04 15:21:12 +00002186 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2187 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2188 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2189 elem->name, attr->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002190
Daniel Veillard37721922001-05-04 15:21:12 +00002191 if ((attrDecl != NULL) &&
2192 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2193 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2194 return(1);
2195 }
2196 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002197}
2198
2199/**
2200 * xmlRemoveRef
2201 * @doc: the document
2202 * @attr: the attribute
2203 *
2204 * Remove the given attribute from the Ref table maintained internally.
2205 *
2206 * Returns -1 if the lookup failed and 0 otherwise
2207 */
2208int
2209xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
Daniel Veillard37721922001-05-04 15:21:12 +00002210 xmlListPtr ref_list;
2211 xmlRefTablePtr table;
2212 xmlChar *ID;
2213 xmlRemoveMemo target;
Owen Taylor3473f882001-02-23 17:55:21 +00002214
Daniel Veillard37721922001-05-04 15:21:12 +00002215 if (doc == NULL) return(-1);
2216 if (attr == NULL) return(-1);
2217 table = (xmlRefTablePtr) doc->refs;
2218 if (table == NULL)
2219 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002220
Daniel Veillard37721922001-05-04 15:21:12 +00002221 if (attr == NULL)
2222 return(-1);
2223 ID = xmlNodeListGetString(doc, attr->children, 1);
2224 if (ID == NULL)
2225 return(-1);
2226 ref_list = xmlHashLookup(table, ID);
Owen Taylor3473f882001-02-23 17:55:21 +00002227
Daniel Veillard37721922001-05-04 15:21:12 +00002228 if(ref_list == NULL) {
2229 xmlFree(ID);
2230 return (-1);
2231 }
2232 /* At this point, ref_list refers to a list of references which
2233 * have the same key as the supplied attr. Our list of references
2234 * is ordered by reference address and we don't have that information
2235 * here to use when removing. We'll have to walk the list and
2236 * check for a matching attribute, when we find one stop the walk
2237 * and remove the entry.
2238 * The list is ordered by reference, so that means we don't have the
2239 * key. Passing the list and the reference to the walker means we
2240 * will have enough data to be able to remove the entry.
2241 */
2242 target.l = ref_list;
2243 target.ap = attr;
2244
2245 /* Remove the supplied attr from our list */
2246 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
Owen Taylor3473f882001-02-23 17:55:21 +00002247
Daniel Veillard37721922001-05-04 15:21:12 +00002248 /*If the list is empty then remove the list entry in the hash */
2249 if (xmlListEmpty(ref_list))
2250 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
2251 xmlFreeRefList);
2252 xmlFree(ID);
2253 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002254}
2255
2256/**
2257 * xmlGetRefs:
2258 * @doc: pointer to the document
2259 * @ID: the ID value
2260 *
2261 * Find the set of references for the supplied ID.
2262 *
2263 * Returns NULL if not found, otherwise node set for the ID.
2264 */
2265xmlListPtr
2266xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
Daniel Veillard37721922001-05-04 15:21:12 +00002267 xmlRefTablePtr table;
Owen Taylor3473f882001-02-23 17:55:21 +00002268
Daniel Veillard37721922001-05-04 15:21:12 +00002269 if (doc == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002270 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
Daniel Veillard37721922001-05-04 15:21:12 +00002271 return(NULL);
2272 }
Owen Taylor3473f882001-02-23 17:55:21 +00002273
Daniel Veillard37721922001-05-04 15:21:12 +00002274 if (ID == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002275 xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == 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 table = (xmlRefTablePtr) doc->refs;
2280 if (table == NULL)
2281 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002282
Daniel Veillard37721922001-05-04 15:21:12 +00002283 return (xmlHashLookup(table, ID));
Owen Taylor3473f882001-02-23 17:55:21 +00002284}
2285
2286/************************************************************************
2287 * *
2288 * Routines for validity checking *
2289 * *
2290 ************************************************************************/
2291
2292/**
2293 * xmlGetDtdElementDesc:
2294 * @dtd: a pointer to the DtD to search
2295 * @name: the element name
2296 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002297 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002298 *
2299 * returns the xmlElementPtr if found or NULL
2300 */
2301
2302xmlElementPtr
2303xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2304 xmlElementTablePtr table;
2305 xmlElementPtr cur;
2306 xmlChar *uqname = NULL, *prefix = NULL;
2307
2308 if (dtd == NULL) return(NULL);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002309 if (dtd->elements == NULL)
2310 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002311 table = (xmlElementTablePtr) dtd->elements;
2312
2313 uqname = xmlSplitQName2(name, &prefix);
Daniel Veillarda10efa82001-04-18 13:09:01 +00002314 if (uqname != NULL)
2315 name = uqname;
2316 cur = xmlHashLookup2(table, name, prefix);
2317 if (prefix != NULL) xmlFree(prefix);
2318 if (uqname != NULL) xmlFree(uqname);
2319 return(cur);
2320}
2321/**
2322 * xmlGetDtdElementDesc2:
2323 * @dtd: a pointer to the DtD to search
2324 * @name: the element name
2325 * @create: create an empty description if not found
2326 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002327 * Search the DTD for the description of this element
Daniel Veillarda10efa82001-04-18 13:09:01 +00002328 *
2329 * returns the xmlElementPtr if found or NULL
2330 */
2331
Daniel Veillard86fd5a72001-12-13 14:55:21 +00002332static xmlElementPtr
Daniel Veillarda10efa82001-04-18 13:09:01 +00002333xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
2334 xmlElementTablePtr table;
2335 xmlElementPtr cur;
2336 xmlChar *uqname = NULL, *prefix = NULL;
2337
2338 if (dtd == NULL) return(NULL);
2339 if (dtd->elements == NULL) {
2340 if (!create)
2341 return(NULL);
2342 /*
2343 * Create the Element table if needed.
2344 */
2345 table = (xmlElementTablePtr) dtd->elements;
2346 if (table == NULL) {
2347 table = xmlCreateElementTable();
2348 dtd->elements = (void *) table;
2349 }
2350 if (table == NULL) {
2351 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002352 "xmlGetDtdElementDesc2: Table creation failed!\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002353 return(NULL);
2354 }
2355 }
2356 table = (xmlElementTablePtr) dtd->elements;
2357
2358 uqname = xmlSplitQName2(name, &prefix);
2359 if (uqname != NULL)
2360 name = uqname;
2361 cur = xmlHashLookup2(table, name, prefix);
2362 if ((cur == NULL) && (create)) {
2363 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2364 if (cur == NULL) {
2365 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002366 "xmlGetDtdElementDesc2: out of memory\n");
Daniel Veillarda10efa82001-04-18 13:09:01 +00002367 return(NULL);
2368 }
2369 memset(cur, 0, sizeof(xmlElement));
2370 cur->type = XML_ELEMENT_DECL;
2371
2372 /*
2373 * fill the structure.
2374 */
2375 cur->name = xmlStrdup(name);
2376 cur->prefix = xmlStrdup(prefix);
2377 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2378
2379 xmlHashAddEntry2(table, name, prefix, cur);
2380 }
2381 if (prefix != NULL) xmlFree(prefix);
2382 if (uqname != NULL) xmlFree(uqname);
Owen Taylor3473f882001-02-23 17:55:21 +00002383 return(cur);
2384}
2385
2386/**
2387 * xmlGetDtdQElementDesc:
2388 * @dtd: a pointer to the DtD to search
2389 * @name: the element name
2390 * @prefix: the element namespace prefix
2391 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002392 * Search the DTD for the description of this element
Owen Taylor3473f882001-02-23 17:55:21 +00002393 *
2394 * returns the xmlElementPtr if found or NULL
2395 */
2396
Daniel Veillard48da9102001-08-07 01:10:10 +00002397xmlElementPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002398xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
2399 const xmlChar *prefix) {
2400 xmlElementTablePtr table;
2401
2402 if (dtd == NULL) return(NULL);
2403 if (dtd->elements == NULL) return(NULL);
2404 table = (xmlElementTablePtr) dtd->elements;
2405
2406 return(xmlHashLookup2(table, name, prefix));
2407}
2408
2409/**
2410 * xmlGetDtdAttrDesc:
2411 * @dtd: a pointer to the DtD to search
2412 * @elem: the element name
2413 * @name: the attribute name
2414 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002415 * Search the DTD for the description of this attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002416 * this element.
2417 *
2418 * returns the xmlAttributePtr if found or NULL
2419 */
2420
2421xmlAttributePtr
2422xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
2423 xmlAttributeTablePtr table;
2424 xmlAttributePtr cur;
2425 xmlChar *uqname = NULL, *prefix = NULL;
2426
2427 if (dtd == NULL) return(NULL);
2428 if (dtd->attributes == NULL) return(NULL);
2429
2430 table = (xmlAttributeTablePtr) dtd->attributes;
2431 if (table == NULL)
2432 return(NULL);
2433
2434 uqname = xmlSplitQName2(name, &prefix);
2435
2436 if (uqname != NULL) {
2437 cur = xmlHashLookup3(table, uqname, prefix, elem);
2438 if (prefix != NULL) xmlFree(prefix);
2439 if (uqname != NULL) xmlFree(uqname);
2440 } else
2441 cur = xmlHashLookup3(table, name, NULL, elem);
2442 return(cur);
2443}
2444
2445/**
2446 * xmlGetDtdQAttrDesc:
2447 * @dtd: a pointer to the DtD to search
2448 * @elem: the element name
2449 * @name: the attribute name
2450 * @prefix: the attribute namespace prefix
2451 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002452 * Search the DTD for the description of this qualified attribute on
Owen Taylor3473f882001-02-23 17:55:21 +00002453 * this element.
2454 *
2455 * returns the xmlAttributePtr if found or NULL
2456 */
2457
Daniel Veillard48da9102001-08-07 01:10:10 +00002458xmlAttributePtr
Owen Taylor3473f882001-02-23 17:55:21 +00002459xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
2460 const xmlChar *prefix) {
2461 xmlAttributeTablePtr table;
2462
2463 if (dtd == NULL) return(NULL);
2464 if (dtd->attributes == NULL) return(NULL);
2465 table = (xmlAttributeTablePtr) dtd->attributes;
2466
2467 return(xmlHashLookup3(table, name, prefix, elem));
2468}
2469
2470/**
2471 * xmlGetDtdNotationDesc:
2472 * @dtd: a pointer to the DtD to search
2473 * @name: the notation name
2474 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002475 * Search the DTD for the description of this notation
Owen Taylor3473f882001-02-23 17:55:21 +00002476 *
2477 * returns the xmlNotationPtr if found or NULL
2478 */
2479
2480xmlNotationPtr
2481xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
2482 xmlNotationTablePtr table;
2483
2484 if (dtd == NULL) return(NULL);
2485 if (dtd->notations == NULL) return(NULL);
2486 table = (xmlNotationTablePtr) dtd->notations;
2487
2488 return(xmlHashLookup(table, name));
2489}
2490
2491/**
2492 * xmlValidateNotationUse:
2493 * @ctxt: the validation context
2494 * @doc: the document
2495 * @notationName: the notation name to check
2496 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002497 * Validate that the given name match a notation declaration.
Owen Taylor3473f882001-02-23 17:55:21 +00002498 * - [ VC: Notation Declared ]
2499 *
2500 * returns 1 if valid or 0 otherwise
2501 */
2502
2503int
2504xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2505 const xmlChar *notationName) {
2506 xmlNotationPtr notaDecl;
2507 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2508
2509 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
2510 if ((notaDecl == NULL) && (doc->extSubset != NULL))
2511 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
2512
2513 if (notaDecl == NULL) {
2514 VERROR(ctxt->userData, "NOTATION %s is not declared\n",
2515 notationName);
2516 return(0);
2517 }
2518 return(1);
2519}
2520
2521/**
2522 * xmlIsMixedElement
2523 * @doc: the document
2524 * @name: the element name
2525 *
2526 * Search in the DtDs whether an element accept Mixed content (or ANY)
2527 * basically if it is supposed to accept text childs
2528 *
2529 * returns 0 if no, 1 if yes, and -1 if no element description is available
2530 */
2531
2532int
2533xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
2534 xmlElementPtr elemDecl;
2535
2536 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
2537
2538 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
2539 if ((elemDecl == NULL) && (doc->extSubset != NULL))
2540 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
2541 if (elemDecl == NULL) return(-1);
2542 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00002543 case XML_ELEMENT_TYPE_UNDEFINED:
2544 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002545 case XML_ELEMENT_TYPE_ELEMENT:
2546 return(0);
2547 case XML_ELEMENT_TYPE_EMPTY:
2548 /*
2549 * return 1 for EMPTY since we want VC error to pop up
2550 * on <empty> </empty> for example
2551 */
2552 case XML_ELEMENT_TYPE_ANY:
2553 case XML_ELEMENT_TYPE_MIXED:
2554 return(1);
2555 }
2556 return(1);
2557}
2558
2559/**
2560 * xmlValidateNameValue:
2561 * @value: an Name value
2562 *
2563 * Validate that the given value match Name production
2564 *
2565 * returns 1 if valid or 0 otherwise
2566 */
2567
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002568static int
Owen Taylor3473f882001-02-23 17:55:21 +00002569xmlValidateNameValue(const xmlChar *value) {
2570 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002571 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002572
2573 if (value == NULL) return(0);
2574 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002575 val = xmlStringCurrentChar(NULL, cur, &len);
2576 cur += len;
2577 if (!IS_LETTER(val) && (val != '_') &&
2578 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002579 return(0);
2580 }
2581
Daniel Veillardd8224e02002-01-13 15:43:22 +00002582 val = xmlStringCurrentChar(NULL, cur, &len);
2583 cur += len;
2584 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2585 (val == '.') || (val == '-') ||
2586 (val == '_') || (val == ':') ||
2587 (IS_COMBINING(val)) ||
2588 (IS_EXTENDER(val))) {
2589 val = xmlStringCurrentChar(NULL, cur, &len);
2590 cur += len;
2591 }
Owen Taylor3473f882001-02-23 17:55:21 +00002592
Daniel Veillardd8224e02002-01-13 15:43:22 +00002593 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002594
2595 return(1);
2596}
2597
2598/**
2599 * xmlValidateNamesValue:
2600 * @value: an Names value
2601 *
2602 * Validate that the given value match Names production
2603 *
2604 * returns 1 if valid or 0 otherwise
2605 */
2606
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002607static int
Owen Taylor3473f882001-02-23 17:55:21 +00002608xmlValidateNamesValue(const xmlChar *value) {
2609 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002610 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002611
2612 if (value == NULL) return(0);
2613 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002614 val = xmlStringCurrentChar(NULL, cur, &len);
2615 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002616
Daniel Veillardd8224e02002-01-13 15:43:22 +00002617 if (!IS_LETTER(val) && (val != '_') &&
2618 (val != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002619 return(0);
2620 }
2621
Daniel Veillardd8224e02002-01-13 15:43:22 +00002622 val = xmlStringCurrentChar(NULL, cur, &len);
2623 cur += len;
2624 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2625 (val == '.') || (val == '-') ||
2626 (val == '_') || (val == ':') ||
2627 (IS_COMBINING(val)) ||
2628 (IS_EXTENDER(val))) {
2629 val = xmlStringCurrentChar(NULL, cur, &len);
2630 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002631 }
2632
Daniel Veillardd8224e02002-01-13 15:43:22 +00002633 while (IS_BLANK(val)) {
2634 while (IS_BLANK(val)) {
2635 val = xmlStringCurrentChar(NULL, cur, &len);
2636 cur += len;
2637 }
2638
2639 if (!IS_LETTER(val) && (val != '_') &&
2640 (val != ':')) {
2641 return(0);
2642 }
2643 val = xmlStringCurrentChar(NULL, cur, &len);
2644 cur += len;
2645
2646 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2647 (val == '.') || (val == '-') ||
2648 (val == '_') || (val == ':') ||
2649 (IS_COMBINING(val)) ||
2650 (IS_EXTENDER(val))) {
2651 val = xmlStringCurrentChar(NULL, cur, &len);
2652 cur += len;
2653 }
2654 }
2655
2656 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002657
2658 return(1);
2659}
2660
2661/**
2662 * xmlValidateNmtokenValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002663 * @value: an Nmtoken value
Owen Taylor3473f882001-02-23 17:55:21 +00002664 *
2665 * Validate that the given value match Nmtoken production
2666 *
2667 * [ VC: Name Token ]
2668 *
2669 * returns 1 if valid or 0 otherwise
2670 */
2671
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002672static int
Owen Taylor3473f882001-02-23 17:55:21 +00002673xmlValidateNmtokenValue(const xmlChar *value) {
2674 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002675 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002676
2677 if (value == NULL) return(0);
2678 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002679 val = xmlStringCurrentChar(NULL, cur, &len);
2680 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002681
Daniel Veillardd8224e02002-01-13 15:43:22 +00002682 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2683 (val != '.') && (val != '-') &&
2684 (val != '_') && (val != ':') &&
2685 (!IS_COMBINING(val)) &&
2686 (!IS_EXTENDER(val)))
Owen Taylor3473f882001-02-23 17:55:21 +00002687 return(0);
2688
Daniel Veillardd8224e02002-01-13 15:43:22 +00002689 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2690 (val == '.') || (val == '-') ||
2691 (val == '_') || (val == ':') ||
2692 (IS_COMBINING(val)) ||
2693 (IS_EXTENDER(val))) {
2694 val = xmlStringCurrentChar(NULL, cur, &len);
2695 cur += len;
2696 }
Owen Taylor3473f882001-02-23 17:55:21 +00002697
Daniel Veillardd8224e02002-01-13 15:43:22 +00002698 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002699
2700 return(1);
2701}
2702
2703/**
2704 * xmlValidateNmtokensValue:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002705 * @value: an Nmtokens value
Owen Taylor3473f882001-02-23 17:55:21 +00002706 *
2707 * Validate that the given value match Nmtokens production
2708 *
2709 * [ VC: Name Token ]
2710 *
2711 * returns 1 if valid or 0 otherwise
2712 */
2713
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002714static int
Owen Taylor3473f882001-02-23 17:55:21 +00002715xmlValidateNmtokensValue(const xmlChar *value) {
2716 const xmlChar *cur;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002717 int val, len;
Owen Taylor3473f882001-02-23 17:55:21 +00002718
2719 if (value == NULL) return(0);
2720 cur = value;
Daniel Veillardd8224e02002-01-13 15:43:22 +00002721 val = xmlStringCurrentChar(NULL, cur, &len);
2722 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002723
Daniel Veillardd8224e02002-01-13 15:43:22 +00002724 while (IS_BLANK(val)) {
2725 val = xmlStringCurrentChar(NULL, cur, &len);
2726 cur += len;
Owen Taylor3473f882001-02-23 17:55:21 +00002727 }
2728
Daniel Veillardd8224e02002-01-13 15:43:22 +00002729 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2730 (val != '.') && (val != '-') &&
2731 (val != '_') && (val != ':') &&
2732 (!IS_COMBINING(val)) &&
2733 (!IS_EXTENDER(val)))
2734 return(0);
2735
2736 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2737 (val == '.') || (val == '-') ||
2738 (val == '_') || (val == ':') ||
2739 (IS_COMBINING(val)) ||
2740 (IS_EXTENDER(val))) {
2741 val = xmlStringCurrentChar(NULL, cur, &len);
2742 cur += len;
2743 }
2744
2745 while (IS_BLANK(val)) {
2746 while (IS_BLANK(val)) {
2747 val = xmlStringCurrentChar(NULL, cur, &len);
2748 cur += len;
2749 }
2750 if (val == 0) return(1);
2751
2752 if (!IS_LETTER(val) && !IS_DIGIT(val) &&
2753 (val != '.') && (val != '-') &&
2754 (val != '_') && (val != ':') &&
2755 (!IS_COMBINING(val)) &&
2756 (!IS_EXTENDER(val)))
2757 return(0);
2758
2759 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
2760 (val == '.') || (val == '-') ||
2761 (val == '_') || (val == ':') ||
2762 (IS_COMBINING(val)) ||
2763 (IS_EXTENDER(val))) {
2764 val = xmlStringCurrentChar(NULL, cur, &len);
2765 cur += len;
2766 }
2767 }
2768
2769 if (val != 0) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002770
2771 return(1);
2772}
2773
2774/**
2775 * xmlValidateNotationDecl:
2776 * @ctxt: the validation context
2777 * @doc: a document instance
2778 * @nota: a notation definition
2779 *
2780 * Try to validate a single notation definition
2781 * basically it does the following checks as described by the
2782 * XML-1.0 recommendation:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002783 * - it seems that no validity constraint exists on notation declarations
Owen Taylor3473f882001-02-23 17:55:21 +00002784 * But this function get called anyway ...
2785 *
2786 * returns 1 if valid or 0 otherwise
2787 */
2788
2789int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002790xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
2791 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002792 int ret = 1;
2793
2794 return(ret);
2795}
2796
2797/**
2798 * xmlValidateAttributeValue:
2799 * @type: an attribute type
2800 * @value: an attribute value
2801 *
2802 * Validate that the given attribute value match the proper production
2803 *
2804 * [ VC: ID ]
2805 * Values of type ID must match the Name production....
2806 *
2807 * [ VC: IDREF ]
2808 * Values of type IDREF must match the Name production, and values
2809 * of type IDREFS must match Names ...
2810 *
2811 * [ VC: Entity Name ]
2812 * Values of type ENTITY must match the Name production, values
2813 * of type ENTITIES must match Names ...
2814 *
2815 * [ VC: Name Token ]
2816 * Values of type NMTOKEN must match the Nmtoken production; values
2817 * of type NMTOKENS must match Nmtokens.
2818 *
2819 * returns 1 if valid or 0 otherwise
2820 */
2821
2822int
2823xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
2824 switch (type) {
2825 case XML_ATTRIBUTE_ENTITIES:
2826 case XML_ATTRIBUTE_IDREFS:
2827 return(xmlValidateNamesValue(value));
2828 case XML_ATTRIBUTE_ENTITY:
2829 case XML_ATTRIBUTE_IDREF:
2830 case XML_ATTRIBUTE_ID:
2831 case XML_ATTRIBUTE_NOTATION:
2832 return(xmlValidateNameValue(value));
2833 case XML_ATTRIBUTE_NMTOKENS:
2834 case XML_ATTRIBUTE_ENUMERATION:
2835 return(xmlValidateNmtokensValue(value));
2836 case XML_ATTRIBUTE_NMTOKEN:
2837 return(xmlValidateNmtokenValue(value));
2838 case XML_ATTRIBUTE_CDATA:
2839 break;
2840 }
2841 return(1);
2842}
2843
2844/**
2845 * xmlValidateAttributeValue2:
2846 * @ctxt: the validation context
2847 * @doc: the document
2848 * @name: the attribute name (used for error reporting only)
2849 * @type: the attribute type
2850 * @value: the attribute value
2851 *
2852 * Validate that the given attribute value match a given type.
2853 * This typically cannot be done before having finished parsing
2854 * the subsets.
2855 *
2856 * [ VC: IDREF ]
2857 * Values of type IDREF must match one of the declared IDs
2858 * Values of type IDREFS must match a sequence of the declared IDs
2859 * each Name must match the value of an ID attribute on some element
2860 * in the XML document; i.e. IDREF values must match the value of
2861 * some ID attribute
2862 *
2863 * [ VC: Entity Name ]
2864 * Values of type ENTITY must match one declared entity
2865 * Values of type ENTITIES must match a sequence of declared entities
2866 *
2867 * [ VC: Notation Attributes ]
2868 * all notation names in the declaration must be declared.
2869 *
2870 * returns 1 if valid or 0 otherwise
2871 */
2872
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002873static int
Owen Taylor3473f882001-02-23 17:55:21 +00002874xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
2875 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
2876 int ret = 1;
2877 switch (type) {
2878 case XML_ATTRIBUTE_IDREFS:
2879 case XML_ATTRIBUTE_IDREF:
2880 case XML_ATTRIBUTE_ID:
2881 case XML_ATTRIBUTE_NMTOKENS:
2882 case XML_ATTRIBUTE_ENUMERATION:
2883 case XML_ATTRIBUTE_NMTOKEN:
2884 case XML_ATTRIBUTE_CDATA:
2885 break;
2886 case XML_ATTRIBUTE_ENTITY: {
2887 xmlEntityPtr ent;
2888
2889 ent = xmlGetDocEntity(doc, value);
2890 if (ent == NULL) {
2891 VERROR(ctxt->userData,
2892 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
2893 name, value);
2894 ret = 0;
2895 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2896 VERROR(ctxt->userData,
2897 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
2898 name, value);
2899 ret = 0;
2900 }
2901 break;
2902 }
2903 case XML_ATTRIBUTE_ENTITIES: {
2904 xmlChar *dup, *nam = NULL, *cur, save;
2905 xmlEntityPtr ent;
2906
2907 dup = xmlStrdup(value);
2908 if (dup == NULL)
2909 return(0);
2910 cur = dup;
2911 while (*cur != 0) {
2912 nam = cur;
2913 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
2914 save = *cur;
2915 *cur = 0;
2916 ent = xmlGetDocEntity(doc, nam);
2917 if (ent == NULL) {
2918 VERROR(ctxt->userData,
2919 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
2920 name, nam);
2921 ret = 0;
2922 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
2923 VERROR(ctxt->userData,
2924 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
2925 name, nam);
2926 ret = 0;
2927 }
2928 if (save == 0)
2929 break;
2930 *cur = save;
2931 while (IS_BLANK(*cur)) cur++;
2932 }
2933 xmlFree(dup);
2934 break;
2935 }
2936 case XML_ATTRIBUTE_NOTATION: {
2937 xmlNotationPtr nota;
2938
2939 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
2940 if ((nota == NULL) && (doc->extSubset != NULL))
2941 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
2942
2943 if (nota == NULL) {
2944 VERROR(ctxt->userData,
2945 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
2946 name, value);
2947 ret = 0;
2948 }
2949 break;
2950 }
2951 }
2952 return(ret);
2953}
2954
2955/**
2956 * xmlValidNormalizeAttributeValue:
2957 * @doc: the document
2958 * @elem: the parent
2959 * @name: the attribute name
2960 * @value: the attribute value
2961 *
2962 * Does the validation related extra step of the normalization of attribute
2963 * values:
2964 *
2965 * If the declared value is not CDATA, then the XML processor must further
2966 * process the normalized attribute value by discarding any leading and
2967 * trailing space (#x20) characters, and by replacing sequences of space
2968 * (#x20) characters by single space (#x20) character.
2969 *
2970 * returns a new normalized string if normalization is needed, NULL otherwise
2971 * the caller must free the returned value.
2972 */
2973
2974xmlChar *
2975xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
2976 const xmlChar *name, const xmlChar *value) {
2977 xmlChar *ret, *dst;
2978 const xmlChar *src;
2979 xmlAttributePtr attrDecl = NULL;
2980
2981 if (doc == NULL) return(NULL);
2982 if (elem == NULL) return(NULL);
2983 if (name == NULL) return(NULL);
2984 if (value == NULL) return(NULL);
2985
2986 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
2987 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002988 snprintf((char *) qname, sizeof(qname), "%s:%s",
2989 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002990 qname[sizeof(qname) - 1] = 0;
2991 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
2992 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2993 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
2994 }
2995 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
2996 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2997 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
2998
2999 if (attrDecl == NULL)
3000 return(NULL);
3001 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3002 return(NULL);
3003
3004 ret = xmlStrdup(value);
3005 if (ret == NULL)
3006 return(NULL);
3007 src = value;
3008 dst = ret;
3009 while (*src == 0x20) src++;
3010 while (*src != 0) {
3011 if (*src == 0x20) {
3012 while (*src == 0x20) src++;
3013 if (*src != 0)
3014 *dst++ = 0x20;
3015 } else {
3016 *dst++ = *src++;
3017 }
3018 }
3019 *dst = 0;
3020 return(ret);
3021}
3022
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003023static void
Owen Taylor3473f882001-02-23 17:55:21 +00003024xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003025 const xmlChar* name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003026 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3027}
3028
3029/**
3030 * xmlValidateAttributeDecl:
3031 * @ctxt: the validation context
3032 * @doc: a document instance
3033 * @attr: an attribute definition
3034 *
3035 * Try to validate a single attribute definition
3036 * basically it does the following checks as described by the
3037 * XML-1.0 recommendation:
3038 * - [ VC: Attribute Default Legal ]
3039 * - [ VC: Enumeration ]
3040 * - [ VC: ID Attribute Default ]
3041 *
3042 * The ID/IDREF uniqueness and matching are done separately
3043 *
3044 * returns 1 if valid or 0 otherwise
3045 */
3046
3047int
3048xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3049 xmlAttributePtr attr) {
3050 int ret = 1;
3051 int val;
3052 CHECK_DTD;
3053 if(attr == NULL) return(1);
3054
3055 /* Attribute Default Legal */
3056 /* Enumeration */
3057 if (attr->defaultValue != NULL) {
3058 val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
3059 if (val == 0) {
3060 VERROR(ctxt->userData,
3061 "Syntax of default value for attribute %s on %s is not valid\n",
3062 attr->name, attr->elem);
3063 }
3064 ret &= val;
3065 }
3066
3067 /* ID Attribute Default */
3068 if ((attr->atype == XML_ATTRIBUTE_ID)&&
3069 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3070 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3071 VERROR(ctxt->userData,
3072 "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n",
3073 attr->name, attr->elem);
3074 ret = 0;
3075 }
3076
3077 /* One ID per Element Type */
3078 if (attr->atype == XML_ATTRIBUTE_ID) {
3079 int nbId;
3080
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003081 /* the trick is that we parse DtD as their own internal subset */
Owen Taylor3473f882001-02-23 17:55:21 +00003082 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
3083 attr->elem);
3084 if (elem != NULL) {
3085 nbId = xmlScanIDAttributeDecl(NULL, elem);
3086 } else {
3087 xmlAttributeTablePtr table;
3088
3089 /*
3090 * The attribute may be declared in the internal subset and the
3091 * element in the external subset.
3092 */
3093 nbId = 0;
3094 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3095 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
3096 xmlValidateAttributeIdCallback, &nbId);
3097 }
3098 if (nbId > 1) {
3099 VERROR(ctxt->userData,
3100 "Element %s has %d ID attribute defined in the internal subset : %s\n",
3101 attr->elem, nbId, attr->name);
3102 } else if (doc->extSubset != NULL) {
3103 int extId = 0;
3104 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
3105 if (elem != NULL) {
3106 extId = xmlScanIDAttributeDecl(NULL, elem);
3107 }
3108 if (extId > 1) {
3109 VERROR(ctxt->userData,
3110 "Element %s has %d ID attribute defined in the external subset : %s\n",
3111 attr->elem, extId, attr->name);
3112 } else if (extId + nbId > 1) {
3113 VERROR(ctxt->userData,
3114"Element %s has ID attributes defined in the internal and external subset : %s\n",
3115 attr->elem, attr->name);
3116 }
3117 }
3118 }
3119
3120 /* Validity Constraint: Enumeration */
3121 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3122 xmlEnumerationPtr tree = attr->tree;
3123 while (tree != NULL) {
3124 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3125 tree = tree->next;
3126 }
3127 if (tree == NULL) {
3128 VERROR(ctxt->userData,
3129"Default value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3130 attr->defaultValue, attr->name, attr->elem);
3131 ret = 0;
3132 }
3133 }
3134
3135 return(ret);
3136}
3137
3138/**
3139 * xmlValidateElementDecl:
3140 * @ctxt: the validation context
3141 * @doc: a document instance
3142 * @elem: an element definition
3143 *
3144 * Try to validate a single element definition
3145 * basically it does the following checks as described by the
3146 * XML-1.0 recommendation:
3147 * - [ VC: One ID per Element Type ]
3148 * - [ VC: No Duplicate Types ]
3149 * - [ VC: Unique Element Type Declaration ]
3150 *
3151 * returns 1 if valid or 0 otherwise
3152 */
3153
3154int
3155xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3156 xmlElementPtr elem) {
3157 int ret = 1;
3158 xmlElementPtr tst;
3159
3160 CHECK_DTD;
3161
3162 if (elem == NULL) return(1);
3163
3164 /* No Duplicate Types */
3165 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3166 xmlElementContentPtr cur, next;
3167 const xmlChar *name;
3168
3169 cur = elem->content;
3170 while (cur != NULL) {
3171 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3172 if (cur->c1 == NULL) break;
3173 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3174 name = cur->c1->name;
3175 next = cur->c2;
3176 while (next != NULL) {
3177 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3178 if (xmlStrEqual(next->name, name)) {
3179 VERROR(ctxt->userData,
3180 "Definition of %s has duplicate references of %s\n",
3181 elem->name, name);
3182 ret = 0;
3183 }
3184 break;
3185 }
3186 if (next->c1 == NULL) break;
3187 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3188 if (xmlStrEqual(next->c1->name, name)) {
3189 VERROR(ctxt->userData,
3190 "Definition of %s has duplicate references of %s\n",
3191 elem->name, name);
3192 ret = 0;
3193 }
3194 next = next->c2;
3195 }
3196 }
3197 cur = cur->c2;
3198 }
3199 }
3200
3201 /* VC: Unique Element Type Declaration */
3202 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003203 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003204 ((tst->prefix == elem->prefix) ||
3205 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003206 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003207 VERROR(ctxt->userData, "Redefinition of element %s\n",
3208 elem->name);
3209 ret = 0;
3210 }
3211 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
Daniel Veillarda10efa82001-04-18 13:09:01 +00003212 if ((tst != NULL ) && (tst != elem) &&
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003213 ((tst->prefix == elem->prefix) ||
3214 (xmlStrEqual(tst->prefix, elem->prefix))) &&
Daniel Veillarda10efa82001-04-18 13:09:01 +00003215 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003216 VERROR(ctxt->userData, "Redefinition of element %s\n",
3217 elem->name);
3218 ret = 0;
3219 }
3220
Daniel Veillarda10efa82001-04-18 13:09:01 +00003221 /* One ID per Element Type
3222 * already done when registering the attribute
Owen Taylor3473f882001-02-23 17:55:21 +00003223 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
3224 ret = 0;
Daniel Veillarda10efa82001-04-18 13:09:01 +00003225 } */
Owen Taylor3473f882001-02-23 17:55:21 +00003226 return(ret);
3227}
3228
3229/**
3230 * xmlValidateOneAttribute:
3231 * @ctxt: the validation context
3232 * @doc: a document instance
3233 * @elem: an element instance
3234 * @attr: an attribute instance
3235 * @value: the attribute value (without entities processing)
3236 *
3237 * Try to validate a single attribute for an element
3238 * basically it does the following checks as described by the
3239 * XML-1.0 recommendation:
3240 * - [ VC: Attribute Value Type ]
3241 * - [ VC: Fixed Attribute Default ]
3242 * - [ VC: Entity Name ]
3243 * - [ VC: Name Token ]
3244 * - [ VC: ID ]
3245 * - [ VC: IDREF ]
3246 * - [ VC: Entity Name ]
3247 * - [ VC: Notation Attributes ]
3248 *
3249 * The ID/IDREF uniqueness and matching are done separately
3250 *
3251 * returns 1 if valid or 0 otherwise
3252 */
3253
3254int
3255xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3256 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
3257 /* xmlElementPtr elemDecl; */
3258 xmlAttributePtr attrDecl = NULL;
3259 int val;
3260 int ret = 1;
3261
3262 CHECK_DTD;
3263 if ((elem == NULL) || (elem->name == NULL)) return(0);
3264 if ((attr == NULL) || (attr->name == NULL)) return(0);
3265
3266 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3267 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00003268 snprintf((char *) qname, sizeof(qname), "%s:%s",
3269 elem->ns->prefix, elem->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003270 qname[sizeof(qname) - 1] = 0;
3271 if (attr->ns != NULL) {
3272 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
3273 attr->name, attr->ns->prefix);
3274 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3275 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
3276 attr->name, attr->ns->prefix);
3277 } else {
3278 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
3279 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3280 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3281 qname, attr->name);
3282 }
3283 }
3284 if (attrDecl == NULL) {
3285 if (attr->ns != NULL) {
3286 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3287 attr->name, attr->ns->prefix);
3288 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3289 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3290 attr->name, attr->ns->prefix);
3291 } else {
3292 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
3293 elem->name, attr->name);
3294 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3295 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3296 elem->name, attr->name);
3297 }
3298 }
3299
3300
3301 /* Validity Constraint: Attribute Value Type */
3302 if (attrDecl == NULL) {
3303 VERROR(ctxt->userData,
3304 "No declaration for attribute %s on element %s\n",
3305 attr->name, elem->name);
3306 return(0);
3307 }
3308 attr->atype = attrDecl->atype;
3309
3310 val = xmlValidateAttributeValue(attrDecl->atype, value);
3311 if (val == 0) {
3312 VERROR(ctxt->userData,
3313 "Syntax of value for attribute %s on %s is not valid\n",
3314 attr->name, elem->name);
3315 ret = 0;
3316 }
3317
3318 /* Validity constraint: Fixed Attribute Default */
3319 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3320 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3321 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003322 "Value for attribute %s on %s is different from default \"%s\"\n",
Owen Taylor3473f882001-02-23 17:55:21 +00003323 attr->name, elem->name, attrDecl->defaultValue);
3324 ret = 0;
3325 }
3326 }
3327
3328 /* Validity Constraint: ID uniqueness */
3329 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
3330 if (xmlAddID(ctxt, doc, value, attr) == NULL)
3331 ret = 0;
3332 }
3333
3334 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3335 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3336 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3337 ret = 0;
3338 }
3339
3340 /* Validity Constraint: Notation Attributes */
3341 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3342 xmlEnumerationPtr tree = attrDecl->tree;
3343 xmlNotationPtr nota;
3344
3345 /* First check that the given NOTATION was declared */
3346 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3347 if (nota == NULL)
3348 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3349
3350 if (nota == NULL) {
3351 VERROR(ctxt->userData,
3352 "Value \"%s\" for attribute %s on %s is not a declared Notation\n",
3353 value, attr->name, elem->name);
3354 ret = 0;
3355 }
3356
3357 /* Second, verify that it's among the list */
3358 while (tree != NULL) {
3359 if (xmlStrEqual(tree->name, value)) break;
3360 tree = tree->next;
3361 }
3362 if (tree == NULL) {
3363 VERROR(ctxt->userData,
3364"Value \"%s\" for attribute %s on %s is not among the enumerated notations\n",
3365 value, attr->name, elem->name);
3366 ret = 0;
3367 }
3368 }
3369
3370 /* Validity Constraint: Enumeration */
3371 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
3372 xmlEnumerationPtr tree = attrDecl->tree;
3373 while (tree != NULL) {
3374 if (xmlStrEqual(tree->name, value)) break;
3375 tree = tree->next;
3376 }
3377 if (tree == NULL) {
3378 VERROR(ctxt->userData,
3379 "Value \"%s\" for attribute %s on %s is not among the enumerated set\n",
3380 value, attr->name, elem->name);
3381 ret = 0;
3382 }
3383 }
3384
3385 /* Fixed Attribute Default */
3386 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
3387 (!xmlStrEqual(attrDecl->defaultValue, value))) {
3388 VERROR(ctxt->userData,
3389 "Value for attribute %s on %s must be \"%s\"\n",
3390 attr->name, elem->name, attrDecl->defaultValue);
3391 ret = 0;
3392 }
3393
3394 /* Extra check for the attribute value */
3395 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
3396 attrDecl->atype, value);
3397
3398 return(ret);
3399}
3400
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003401/**
3402 * xmlValidateSkipIgnorable:
3403 * @ctxt: the validation context
3404 * @child: the child list
3405 *
3406 * Skip ignorable elements w.r.t. the validation process
3407 *
3408 * returns the first element to consider for validation of the content model
3409 */
3410
3411static xmlNodePtr
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003412xmlValidateSkipIgnorable(xmlNodePtr child) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003413 while (child != NULL) {
3414 switch (child->type) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003415 /* These things are ignored (skipped) during validation. */
3416 case XML_PI_NODE:
3417 case XML_COMMENT_NODE:
3418 case XML_XINCLUDE_START:
3419 case XML_XINCLUDE_END:
3420 child = child->next;
3421 break;
3422 case XML_TEXT_NODE:
3423 if (xmlIsBlankNode(child))
3424 child = child->next;
3425 else
3426 return(child);
3427 break;
3428 /* keep current node */
3429 default:
3430 return(child);
3431 }
3432 }
3433 return(child);
3434}
3435
3436/**
3437 * xmlValidateElementType:
3438 * @ctxt: the validation context
3439 *
3440 * Try to validate the content model of an element internal function
3441 *
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003442 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
3443 * reference is found and -3 if the validation succeeded but
3444 * the content model is not determinist.
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003445 */
3446
3447static int
3448xmlValidateElementType(xmlValidCtxtPtr ctxt) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00003449 int ret = -1;
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003450 int determinist = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003451
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003452 NODE = xmlValidateSkipIgnorable(NODE);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003453 if ((NODE == NULL) && (CONT == NULL))
3454 return(1);
3455 if ((NODE == NULL) &&
3456 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
3457 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
3458 return(1);
3459 }
3460 if (CONT == NULL) return(-1);
Daniel Veillard7533cc82001-04-24 15:52:00 +00003461 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003462 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003463
3464 /*
3465 * We arrive here when more states need to be examined
3466 */
3467cont:
3468
3469 /*
3470 * We just recovered from a rollback generated by a possible
3471 * epsilon transition, go directly to the analysis phase
3472 */
3473 if (STATE == ROLLBACK_PARENT) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003474 DEBUG_VALID_MSG("restored parent branch");
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003475 DEBUG_VALID_STATE(NODE, CONT)
3476 ret = 1;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003477 goto analyze;
3478 }
3479
3480 DEBUG_VALID_STATE(NODE, CONT)
3481 /*
3482 * we may have to save a backup state here. This is the equivalent
3483 * of handling epsilon transition in NFAs.
3484 */
Daniel Veillarde62d36c2001-05-15 08:53:16 +00003485 if ((CONT != NULL) &&
Daniel Veillardce2c2f02001-10-18 14:57:24 +00003486 ((CONT->parent == NULL) ||
3487 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003488 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
Daniel Veillardca1f1722001-04-20 15:47:35 +00003489 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
Daniel Veillard5344c602001-12-31 16:37:34 +00003490 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003491 DEBUG_VALID_MSG("saving parent branch");
3492 vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT);
3493 }
3494
3495
3496 /*
3497 * Check first if the content matches
3498 */
3499 switch (CONT->type) {
3500 case XML_ELEMENT_CONTENT_PCDATA:
3501 if (NODE == NULL) {
3502 DEBUG_VALID_MSG("pcdata failed no node");
3503 ret = 0;
3504 break;
3505 }
3506 if (NODE->type == XML_TEXT_NODE) {
3507 DEBUG_VALID_MSG("pcdata found, skip to next");
3508 /*
3509 * go to next element in the content model
3510 * skipping ignorable elems
3511 */
3512 do {
3513 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003514 NODE = xmlValidateSkipIgnorable(NODE);
3515 if ((NODE != NULL) &&
3516 (NODE->type == XML_ENTITY_REF_NODE))
3517 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003518 } while ((NODE != NULL) &&
3519 ((NODE->type != XML_ELEMENT_NODE) &&
3520 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003521 ret = 1;
3522 break;
3523 } else {
3524 DEBUG_VALID_MSG("pcdata failed");
3525 ret = 0;
3526 break;
3527 }
3528 break;
3529 case XML_ELEMENT_CONTENT_ELEMENT:
3530 if (NODE == NULL) {
3531 DEBUG_VALID_MSG("element failed no node");
3532 ret = 0;
3533 break;
3534 }
Daniel Veillard8bdd2202001-06-11 12:47:59 +00003535 ret = ((NODE->type == XML_ELEMENT_NODE) &&
3536 (xmlStrEqual(NODE->name, CONT->name)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003537 if (ret == 1) {
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003538 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3539 ret = (CONT->prefix == NULL);
3540 } else if (CONT->prefix == NULL) {
3541 ret = 0;
3542 } else {
3543 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
3544 }
3545 }
3546 if (ret == 1) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003547 DEBUG_VALID_MSG("element found, skip to next");
3548 /*
3549 * go to next element in the content model
3550 * skipping ignorable elems
3551 */
3552 do {
3553 NODE = NODE->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003554 NODE = xmlValidateSkipIgnorable(NODE);
3555 if ((NODE != NULL) &&
3556 (NODE->type == XML_ENTITY_REF_NODE))
3557 return(-2);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003558 } while ((NODE != NULL) &&
3559 ((NODE->type != XML_ELEMENT_NODE) &&
3560 (NODE->type != XML_TEXT_NODE)));
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003561 } else {
3562 DEBUG_VALID_MSG("element failed");
3563 ret = 0;
3564 break;
3565 }
3566 break;
3567 case XML_ELEMENT_CONTENT_OR:
3568 /*
Daniel Veillard85349052001-04-20 13:48:21 +00003569 * Small optimization.
3570 */
3571 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3572 if ((NODE == NULL) ||
3573 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3574 DEPTH++;
3575 CONT = CONT->c2;
3576 goto cont;
3577 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003578 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3579 ret = (CONT->c1->prefix == NULL);
3580 } else if (CONT->c1->prefix == NULL) {
3581 ret = 0;
3582 } else {
3583 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3584 }
3585 if (ret == 0) {
3586 DEPTH++;
3587 CONT = CONT->c2;
3588 goto cont;
3589 }
Daniel Veillard85349052001-04-20 13:48:21 +00003590 }
3591
3592 /*
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003593 * save the second branch 'or' branch
3594 */
3595 DEBUG_VALID_MSG("saving 'or' branch");
Daniel Veillard268fd1b2001-08-26 18:46:36 +00003596 vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
3597 OCCURS, ROLLBACK_OR);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003598
3599 DEPTH++;
3600 CONT = CONT->c1;
3601 goto cont;
3602 case XML_ELEMENT_CONTENT_SEQ:
Daniel Veillard1d047672001-06-09 16:41:01 +00003603 /*
3604 * Small optimization.
3605 */
3606 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
3607 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
3608 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
3609 if ((NODE == NULL) ||
3610 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
3611 DEPTH++;
3612 CONT = CONT->c2;
3613 goto cont;
3614 }
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003615 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
3616 ret = (CONT->c1->prefix == NULL);
3617 } else if (CONT->c1->prefix == NULL) {
3618 ret = 0;
3619 } else {
3620 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
3621 }
3622 if (ret == 0) {
3623 DEPTH++;
3624 CONT = CONT->c2;
3625 goto cont;
3626 }
Daniel Veillard1d047672001-06-09 16:41:01 +00003627 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003628 DEPTH++;
3629 CONT = CONT->c1;
3630 goto cont;
3631 }
3632
3633 /*
3634 * At this point handle going up in the tree
3635 */
3636 if (ret == -1) {
3637 DEBUG_VALID_MSG("error found returning");
3638 return(ret);
3639 }
3640analyze:
3641 while (CONT != NULL) {
3642 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003643 * First do the analysis depending on the occurrence model at
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003644 * this level.
3645 */
3646 if (ret == 0) {
3647 switch (CONT->ocur) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003648 xmlNodePtr cur;
3649
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003650 case XML_ELEMENT_CONTENT_ONCE:
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003651 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003652 DEBUG_VALID_MSG("Once branch failed, rollback");
3653 if (vstateVPop(ctxt) < 0 ) {
3654 DEBUG_VALID_MSG("exhaustion, failed");
3655 return(0);
3656 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003657 if (cur != ctxt->vstate->node)
3658 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003659 goto cont;
3660 case XML_ELEMENT_CONTENT_PLUS:
Daniel Veillard5344c602001-12-31 16:37:34 +00003661 if (OCCURRENCE == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003662 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003663 DEBUG_VALID_MSG("Plus branch failed, rollback");
3664 if (vstateVPop(ctxt) < 0 ) {
3665 DEBUG_VALID_MSG("exhaustion, failed");
3666 return(0);
3667 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003668 if (cur != ctxt->vstate->node)
3669 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003670 goto cont;
3671 }
3672 DEBUG_VALID_MSG("Plus branch found");
3673 ret = 1;
3674 break;
3675 case XML_ELEMENT_CONTENT_MULT:
3676#ifdef DEBUG_VALID_ALGO
Daniel Veillard5344c602001-12-31 16:37:34 +00003677 if (OCCURRENCE == 0) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003678 DEBUG_VALID_MSG("Mult branch failed");
3679 } else {
3680 DEBUG_VALID_MSG("Mult branch found");
3681 }
3682#endif
3683 ret = 1;
3684 break;
3685 case XML_ELEMENT_CONTENT_OPT:
3686 DEBUG_VALID_MSG("Option branch failed");
3687 ret = 1;
3688 break;
3689 }
3690 } else {
3691 switch (CONT->ocur) {
3692 case XML_ELEMENT_CONTENT_OPT:
3693 DEBUG_VALID_MSG("Option branch succeeded");
3694 ret = 1;
3695 break;
3696 case XML_ELEMENT_CONTENT_ONCE:
3697 DEBUG_VALID_MSG("Once branch succeeded");
3698 ret = 1;
3699 break;
3700 case XML_ELEMENT_CONTENT_PLUS:
3701 if (STATE == ROLLBACK_PARENT) {
3702 DEBUG_VALID_MSG("Plus branch rollback");
3703 ret = 1;
3704 break;
3705 }
3706 if (NODE == NULL) {
3707 DEBUG_VALID_MSG("Plus branch exhausted");
3708 ret = 1;
3709 break;
3710 }
3711 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003712 SET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003713 goto cont;
3714 case XML_ELEMENT_CONTENT_MULT:
3715 if (STATE == ROLLBACK_PARENT) {
3716 DEBUG_VALID_MSG("Mult branch rollback");
3717 ret = 1;
3718 break;
3719 }
3720 if (NODE == NULL) {
3721 DEBUG_VALID_MSG("Mult branch exhausted");
3722 ret = 1;
3723 break;
3724 }
3725 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
Daniel Veillard5344c602001-12-31 16:37:34 +00003726 /* SET_OCCURRENCE; */
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003727 goto cont;
3728 }
3729 }
3730 STATE = 0;
3731
3732 /*
3733 * Then act accordingly at the parent level
3734 */
Daniel Veillard5344c602001-12-31 16:37:34 +00003735 RESET_OCCURRENCE;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003736 if (CONT->parent == NULL)
3737 break;
3738
3739 switch (CONT->parent->type) {
3740 case XML_ELEMENT_CONTENT_PCDATA:
3741 DEBUG_VALID_MSG("Error: parent pcdata");
3742 return(-1);
3743 case XML_ELEMENT_CONTENT_ELEMENT:
3744 DEBUG_VALID_MSG("Error: parent element");
3745 return(-1);
3746 case XML_ELEMENT_CONTENT_OR:
3747 if (ret == 1) {
3748 DEBUG_VALID_MSG("Or succeeded");
3749 CONT = CONT->parent;
3750 DEPTH--;
3751 } else {
3752 DEBUG_VALID_MSG("Or failed");
3753 CONT = CONT->parent;
3754 DEPTH--;
3755 }
3756 break;
3757 case XML_ELEMENT_CONTENT_SEQ:
3758 if (ret == 0) {
3759 DEBUG_VALID_MSG("Sequence failed");
3760 CONT = CONT->parent;
3761 DEPTH--;
3762 } else if (CONT == CONT->parent->c1) {
3763 DEBUG_VALID_MSG("Sequence testing 2nd branch");
3764 CONT = CONT->parent->c2;
3765 goto cont;
3766 } else {
3767 DEBUG_VALID_MSG("Sequence succeeded");
3768 CONT = CONT->parent;
3769 DEPTH--;
3770 }
3771 }
3772 }
3773 if (NODE != NULL) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003774 xmlNodePtr cur;
3775
3776 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003777 DEBUG_VALID_MSG("Failed, remaining input, rollback");
3778 if (vstateVPop(ctxt) < 0 ) {
3779 DEBUG_VALID_MSG("exhaustion, failed");
3780 return(0);
3781 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003782 if (cur != ctxt->vstate->node)
3783 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003784 goto cont;
3785 }
3786 if (ret == 0) {
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003787 xmlNodePtr cur;
3788
3789 cur = ctxt->vstate->node;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003790 DEBUG_VALID_MSG("Failure, rollback");
3791 if (vstateVPop(ctxt) < 0 ) {
3792 DEBUG_VALID_MSG("exhaustion, failed");
3793 return(0);
3794 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003795 if (cur != ctxt->vstate->node)
3796 determinist = -3;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003797 goto cont;
3798 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003799 return(determinist);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003800}
3801
3802/**
Daniel Veillardd3d06722001-08-15 12:06:36 +00003803 * xmlSnprintfElements:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003804 * @buf: an output buffer
Daniel Veillardd3d06722001-08-15 12:06:36 +00003805 * @size: the size of the buffer
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003806 * @content: An element
3807 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
3808 *
3809 * This will dump the list of elements to the buffer
3810 * Intended just for the debug routine
3811 */
3812static void
Daniel Veillardd3d06722001-08-15 12:06:36 +00003813xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003814 xmlNodePtr cur;
Daniel Veillardd3d06722001-08-15 12:06:36 +00003815 int len;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003816
3817 if (node == NULL) return;
3818 if (glob) strcat(buf, "(");
3819 cur = node;
3820 while (cur != NULL) {
Daniel Veillardd3d06722001-08-15 12:06:36 +00003821 len = strlen(buf);
3822 if (size - len < 50) {
3823 if ((size - len > 4) && (buf[len - 1] != '.'))
3824 strcat(buf, " ...");
3825 return;
3826 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003827 switch (cur->type) {
3828 case XML_ELEMENT_NODE:
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003829 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
3830 if (size - len < xmlStrlen(cur->ns->prefix + 10)) {
3831 if ((size - len > 4) && (buf[len - 1] != '.'))
3832 strcat(buf, " ...");
3833 return;
3834 }
3835 strcat(buf, (char *) cur->ns->prefix);
3836 strcat(buf, ":");
3837 }
Daniel Veillardd3d06722001-08-15 12:06:36 +00003838 if (size - len < xmlStrlen(cur->name + 10)) {
3839 if ((size - len > 4) && (buf[len - 1] != '.'))
3840 strcat(buf, " ...");
3841 return;
3842 }
3843 strcat(buf, (char *) cur->name);
3844 if (cur->next != NULL)
3845 strcat(buf, " ");
3846 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003847 case XML_TEXT_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003848 if (xmlIsBlankNode(cur))
3849 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003850 case XML_CDATA_SECTION_NODE:
3851 case XML_ENTITY_REF_NODE:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003852 strcat(buf, "CDATA");
3853 if (cur->next != NULL)
3854 strcat(buf, " ");
3855 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003856 case XML_ATTRIBUTE_NODE:
3857 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00003858#ifdef LIBXML_DOCB_ENABLED
3859 case XML_DOCB_DOCUMENT_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003860#endif
3861 case XML_HTML_DOCUMENT_NODE:
3862 case XML_DOCUMENT_TYPE_NODE:
3863 case XML_DOCUMENT_FRAG_NODE:
3864 case XML_NOTATION_NODE:
3865 case XML_NAMESPACE_DECL:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003866 strcat(buf, "???");
3867 if (cur->next != NULL)
3868 strcat(buf, " ");
3869 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003870 case XML_ENTITY_NODE:
3871 case XML_PI_NODE:
3872 case XML_DTD_NODE:
3873 case XML_COMMENT_NODE:
3874 case XML_ELEMENT_DECL:
3875 case XML_ATTRIBUTE_DECL:
3876 case XML_ENTITY_DECL:
3877 case XML_XINCLUDE_START:
3878 case XML_XINCLUDE_END:
Daniel Veillardd3d06722001-08-15 12:06:36 +00003879 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003880 }
3881 cur = cur->next;
3882 }
3883 if (glob) strcat(buf, ")");
3884}
3885
3886/**
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003887 * xmlValidateElementContent:
3888 * @ctxt: the validation context
3889 * @child: the child list
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003890 * @elemDecl: pointer to the element declaration
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003891 * @warn: emit the error message
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003892 *
3893 * Try to validate the content model of an element
3894 *
3895 * returns 1 if valid or 0 if not and -1 in case of error
3896 */
3897
3898static int
3899xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003900 xmlElementPtr elemDecl, int warn) {
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003901 int ret;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003902 xmlNodePtr repl = NULL, last = NULL, cur, tmp;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00003903 xmlElementContentPtr cont;
3904 const xmlChar *name;
3905
3906 if (elemDecl == NULL)
3907 return(-1);
3908 cont = elemDecl->content;
3909 name = elemDecl->name;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003910
3911 /*
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003912 * Allocate the stack
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003913 */
3914 ctxt->vstateMax = 8;
3915 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
3916 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
3917 if (ctxt->vstateTab == NULL) {
3918 xmlGenericError(xmlGenericErrorContext,
3919 "malloc failed !n");
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003920 return(-1);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003921 }
3922 /*
3923 * The first entry in the stack is reserved to the current state
3924 */
Daniel Veillarda9142e72001-06-19 11:07:54 +00003925 ctxt->nodeMax = 0;
3926 ctxt->nodeNr = 0;
Daniel Veillard61b33d52001-04-24 13:55:12 +00003927 ctxt->nodeTab = NULL;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00003928 ctxt->vstate = &ctxt->vstateTab[0];
3929 ctxt->vstateNr = 1;
3930 CONT = cont;
3931 NODE = child;
3932 DEPTH = 0;
3933 OCCURS = 0;
3934 STATE = 0;
3935 ret = xmlValidateElementType(ctxt);
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00003936 if ((ret == -3) && (warn)) {
3937 VWARNING(ctxt->userData,
3938 "Element %s content model is ambiguous\n", name);
3939 } else if (ret == -2) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003940 /*
3941 * An entities reference appeared at this level.
3942 * Buid a minimal representation of this node content
3943 * sufficient to run the validation process on it
3944 */
3945 DEBUG_VALID_MSG("Found an entity reference, linearizing");
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003946 cur = child;
3947 while (cur != NULL) {
3948 switch (cur->type) {
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003949 case XML_ENTITY_REF_NODE:
3950 /*
3951 * Push the current node to be able to roll back
3952 * and process within the entity
3953 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003954 if ((cur->children != NULL) &&
3955 (cur->children->children != NULL)) {
3956 nodeVPush(ctxt, cur);
3957 cur = cur->children->children;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003958 continue;
3959 }
Daniel Veillard64b98c02001-06-17 17:20:21 +00003960 break;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003961 case XML_TEXT_NODE:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003962 case XML_CDATA_SECTION_NODE:
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003963 if (xmlIsBlankNode(cur))
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003964 break;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00003965 /* no break on purpose */
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003966 case XML_ELEMENT_NODE:
3967 /*
3968 * Allocate a new node and minimally fills in
3969 * what's required
3970 */
3971 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3972 if (tmp == NULL) {
3973 xmlGenericError(xmlGenericErrorContext,
3974 "xmlValidateElementContent : malloc failed\n");
3975 xmlFreeNodeList(repl);
3976 ret = -1;
3977 goto done;
3978 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003979 tmp->type = cur->type;
3980 tmp->name = cur->name;
3981 tmp->ns = cur->ns;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003982 tmp->next = NULL;
Daniel Veillarded472f32001-12-13 08:48:14 +00003983 tmp->content = NULL;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00003984 if (repl == NULL)
3985 repl = last = tmp;
3986 else {
3987 last->next = tmp;
3988 last = tmp;
3989 }
3990 break;
3991 default:
3992 break;
3993 }
3994 /*
3995 * Switch to next element
3996 */
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00003997 cur = cur->next;
3998 while (cur == NULL) {
3999 cur = nodeVPop(ctxt);
4000 if (cur == NULL)
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004001 break;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004002 cur = cur->next;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004003 }
4004 }
4005
4006 /*
4007 * Relaunch the validation
4008 */
4009 ctxt->vstate = &ctxt->vstateTab[0];
4010 ctxt->vstateNr = 1;
4011 CONT = cont;
4012 NODE = repl;
4013 DEPTH = 0;
4014 OCCURS = 0;
4015 STATE = 0;
4016 ret = xmlValidateElementType(ctxt);
4017 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004018 if ((warn) && ((ret != 1) && (ret != -3))) {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004019 if ((ctxt != NULL) && (ctxt->warning != NULL)) {
4020 char expr[5000];
4021 char list[5000];
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004022
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004023 expr[0] = 0;
4024 xmlSnprintfElementContent(expr, 5000, cont, 1);
4025 list[0] = 0;
4026 if (repl != NULL)
4027 xmlSnprintfElements(list, 5000, repl, 1);
4028 else
4029 xmlSnprintfElements(list, 5000, child, 1);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004030
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004031 if (name != NULL) {
4032 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004033 "Element %s content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004034 name, expr, list);
4035 } else {
4036 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004037 "Element content doesn't follow the DTD\nExpecting %s, got %s\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004038 expr, list);
4039 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004040 } else {
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004041 if (name != NULL) {
4042 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004043 "Element %s content doesn't follow the DTD\n",
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004044 name);
4045 } else {
4046 VERROR(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004047 "Element content doesn't follow the DTD\n");
Daniel Veillardb4545fd2001-11-20 09:37:09 +00004048 }
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004049 }
4050 ret = 0;
4051 }
Daniel Veillard4de4d3b2001-05-07 20:50:47 +00004052 if (ret == -3)
4053 ret = 1;
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004054
4055
4056done:
4057 /*
4058 * Deallocate the copy if done, and free up the validation stack
4059 */
4060 while (repl != NULL) {
4061 tmp = repl->next;
4062 xmlFree(repl);
4063 repl = tmp;
4064 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004065 ctxt->vstateMax = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004066 if (ctxt->vstateTab != NULL) {
4067 xmlFree(ctxt->vstateTab);
4068 ctxt->vstateTab = NULL;
4069 }
4070 ctxt->nodeMax = 0;
Daniel Veillarda9142e72001-06-19 11:07:54 +00004071 ctxt->nodeNr = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004072 if (ctxt->nodeTab != NULL) {
4073 xmlFree(ctxt->nodeTab);
4074 ctxt->nodeTab = NULL;
4075 }
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004076 return(ret);
Daniel Veillard1c14b8d2001-04-21 10:28:59 +00004077
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004078}
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004079
Owen Taylor3473f882001-02-23 17:55:21 +00004080/**
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004081 * xmlValidateCdataElement:
4082 * @ctxt: the validation context
4083 * @doc: a document instance
4084 * @elem: an element instance
4085 *
4086 * Check that an element follows #CDATA
4087 *
4088 * returns 1 if valid or 0 otherwise
4089 */
4090static int
4091xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4092 xmlNodePtr elem) {
4093 int ret = 1;
4094 xmlNodePtr cur, child;
4095
4096 if ((ctxt == NULL) || (doc == NULL) | (elem == NULL))
4097 return(0);
4098
4099 child = elem->children;
4100
4101 cur = child;
4102 while (cur != NULL) {
4103 switch (cur->type) {
4104 case XML_ENTITY_REF_NODE:
4105 /*
4106 * Push the current node to be able to roll back
4107 * and process within the entity
4108 */
4109 if ((cur->children != NULL) &&
4110 (cur->children->children != NULL)) {
4111 nodeVPush(ctxt, cur);
4112 cur = cur->children->children;
4113 continue;
4114 }
4115 break;
4116 case XML_COMMENT_NODE:
4117 case XML_PI_NODE:
4118 case XML_TEXT_NODE:
4119 case XML_CDATA_SECTION_NODE:
4120 break;
4121 default:
4122 ret = 0;
4123 goto done;
4124 }
4125 /*
4126 * Switch to next element
4127 */
4128 cur = cur->next;
4129 while (cur == NULL) {
4130 cur = nodeVPop(ctxt);
4131 if (cur == NULL)
4132 break;
4133 cur = cur->next;
4134 }
4135 }
4136done:
4137 ctxt->nodeMax = 0;
4138 ctxt->nodeNr = 0;
4139 if (ctxt->nodeTab != NULL) {
4140 xmlFree(ctxt->nodeTab);
4141 ctxt->nodeTab = NULL;
4142 }
4143 return(ret);
4144}
4145
4146/**
Owen Taylor3473f882001-02-23 17:55:21 +00004147 * xmlValidateOneElement:
4148 * @ctxt: the validation context
4149 * @doc: a document instance
4150 * @elem: an element instance
4151 *
4152 * Try to validate a single element and it's attributes,
4153 * basically it does the following checks as described by the
4154 * XML-1.0 recommendation:
4155 * - [ VC: Element Valid ]
4156 * - [ VC: Required Attribute ]
4157 * Then call xmlValidateOneAttribute() for each attribute present.
4158 *
4159 * The ID/IDREF checkings are done separately
4160 *
4161 * returns 1 if valid or 0 otherwise
4162 */
4163
4164int
4165xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4166 xmlNodePtr elem) {
4167 xmlElementPtr elemDecl = NULL;
4168 xmlElementContentPtr cont;
4169 xmlAttributePtr attr;
4170 xmlNodePtr child;
4171 int ret = 1;
4172 const xmlChar *name;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004173 const xmlChar *prefix = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004174
4175 CHECK_DTD;
4176
4177 if (elem == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004178 switch (elem->type) {
4179 case XML_ATTRIBUTE_NODE:
4180 VERROR(ctxt->userData,
4181 "Attribute element not expected here\n");
4182 return(0);
4183 case XML_TEXT_NODE:
4184 if (elem->children != NULL) {
4185 VERROR(ctxt->userData, "Text element has childs !\n");
4186 return(0);
4187 }
4188 if (elem->properties != NULL) {
4189 VERROR(ctxt->userData, "Text element has attributes !\n");
4190 return(0);
4191 }
4192 if (elem->ns != NULL) {
4193 VERROR(ctxt->userData, "Text element has namespace !\n");
4194 return(0);
4195 }
4196 if (elem->nsDef != NULL) {
4197 VERROR(ctxt->userData,
4198 "Text element carries namespace definitions !\n");
4199 return(0);
4200 }
4201 if (elem->content == NULL) {
4202 VERROR(ctxt->userData,
4203 "Text element has no content !\n");
4204 return(0);
4205 }
4206 return(1);
4207 case XML_XINCLUDE_START:
4208 case XML_XINCLUDE_END:
4209 return(1);
4210 case XML_CDATA_SECTION_NODE:
4211 case XML_ENTITY_REF_NODE:
4212 case XML_PI_NODE:
4213 case XML_COMMENT_NODE:
4214 return(1);
4215 case XML_ENTITY_NODE:
4216 VERROR(ctxt->userData,
4217 "Entity element not expected here\n");
4218 return(0);
4219 case XML_NOTATION_NODE:
4220 VERROR(ctxt->userData,
4221 "Notation element not expected here\n");
4222 return(0);
4223 case XML_DOCUMENT_NODE:
4224 case XML_DOCUMENT_TYPE_NODE:
4225 case XML_DOCUMENT_FRAG_NODE:
4226 VERROR(ctxt->userData,
4227 "Document element not expected here\n");
4228 return(0);
4229 case XML_HTML_DOCUMENT_NODE:
4230 VERROR(ctxt->userData,
4231 "\n");
4232 return(0);
4233 case XML_ELEMENT_NODE:
4234 break;
4235 default:
4236 VERROR(ctxt->userData,
4237 "unknown element type %d\n", elem->type);
4238 return(0);
4239 }
4240 if (elem->name == NULL) return(0);
4241
4242 /*
4243 * Fetch the declaration for the qualified name
4244 */
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004245 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
4246 prefix = elem->ns->prefix;
4247
4248 if (prefix != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004249 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004250 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004251 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4252 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004253 elem->name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00004254 }
4255
4256 /*
4257 * Fetch the declaration for the non qualified name
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004258 * This is "non-strict" validation should be done on the
4259 * full QName but in that case being flexible makes sense.
Owen Taylor3473f882001-02-23 17:55:21 +00004260 */
4261 if (elemDecl == NULL) {
4262 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4263 if ((elemDecl == NULL) && (doc->extSubset != NULL))
4264 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4265 }
4266 if (elemDecl == NULL) {
4267 VERROR(ctxt->userData, "No declaration for element %s\n",
4268 elem->name);
4269 return(0);
4270 }
4271
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004272 /* Check that the element content matches the definition */
Owen Taylor3473f882001-02-23 17:55:21 +00004273 switch (elemDecl->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +00004274 case XML_ELEMENT_TYPE_UNDEFINED:
4275 VERROR(ctxt->userData, "No declaration for element %s\n",
4276 elem->name);
4277 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004278 case XML_ELEMENT_TYPE_EMPTY:
4279 if (elem->children != NULL) {
4280 VERROR(ctxt->userData,
4281 "Element %s was declared EMPTY this one has content\n",
4282 elem->name);
4283 ret = 0;
4284 }
4285 break;
4286 case XML_ELEMENT_TYPE_ANY:
4287 /* I don't think anything is required then */
4288 break;
4289 case XML_ELEMENT_TYPE_MIXED:
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004290 /* simple case of declared as #PCDATA */
4291 if ((elemDecl->content != NULL) &&
4292 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
4293 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
4294 if (!ret) {
4295 VERROR(ctxt->userData,
4296 "Element %s was declared #PCDATA but contains non text nodes\n",
4297 elem->name);
4298 }
4299 break;
4300 }
Owen Taylor3473f882001-02-23 17:55:21 +00004301 child = elem->children;
Daniel Veillard04e2dae2001-07-09 20:07:25 +00004302 /* Hum, this start to get messy */
Owen Taylor3473f882001-02-23 17:55:21 +00004303 while (child != NULL) {
4304 if (child->type == XML_ELEMENT_NODE) {
4305 name = child->name;
4306 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
4307 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004308 snprintf((char *) qname, sizeof(qname), "%s:%s",
4309 child->ns->prefix, child->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004310 qname[sizeof(qname) - 1] = 0;
4311 cont = elemDecl->content;
4312 while (cont != NULL) {
4313 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4314 if (xmlStrEqual(cont->name, qname)) break;
4315 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4316 (cont->c1 != NULL) &&
4317 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
4318 if (xmlStrEqual(cont->c1->name, qname)) break;
4319 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4320 (cont->c1 == NULL) ||
4321 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
4322 /* Internal error !!! */
4323 xmlGenericError(xmlGenericErrorContext,
4324 "Internal: MIXED struct bad\n");
4325 break;
4326 }
4327 cont = cont->c2;
4328 }
4329 if (cont != NULL)
4330 goto child_ok;
4331 }
4332 cont = elemDecl->content;
4333 while (cont != NULL) {
4334 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
4335 if (xmlStrEqual(cont->name, name)) break;
4336 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
4337 (cont->c1 != NULL) &&
4338 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
4339 if (xmlStrEqual(cont->c1->name, name)) break;
4340 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
4341 (cont->c1 == NULL) ||
4342 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
4343 /* Internal error !!! */
4344 xmlGenericError(xmlGenericErrorContext,
4345 "Internal: MIXED struct bad\n");
4346 break;
4347 }
4348 cont = cont->c2;
4349 }
4350 if (cont == NULL) {
4351 VERROR(ctxt->userData,
4352 "Element %s is not declared in %s list of possible childs\n",
4353 name, elem->name);
4354 ret = 0;
4355 }
4356 }
4357child_ok:
4358 child = child->next;
4359 }
4360 break;
4361 case XML_ELEMENT_TYPE_ELEMENT:
4362 child = elem->children;
4363 cont = elemDecl->content;
Daniel Veillardbe480fb2001-11-08 23:36:42 +00004364 ret = xmlValidateElementContent(ctxt, child, elemDecl, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00004365 break;
4366 }
4367
4368 /* [ VC: Required Attribute ] */
4369 attr = elemDecl->attributes;
4370 while (attr != NULL) {
4371 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
4372 xmlAttrPtr attrib;
4373 int qualified = -1;
4374
4375 attrib = elem->properties;
4376 while (attrib != NULL) {
4377 if (xmlStrEqual(attrib->name, attr->name)) {
4378 if (attr->prefix != NULL) {
4379 xmlNsPtr nameSpace = attrib->ns;
4380
4381 if (nameSpace == NULL)
4382 nameSpace = elem->ns;
4383 /*
4384 * qualified names handling is problematic, having a
4385 * different prefix should be possible but DTDs don't
4386 * allow to define the URI instead of the prefix :-(
4387 */
4388 if (nameSpace == NULL) {
4389 if (qualified < 0)
4390 qualified = 0;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00004391 } else if (!xmlStrEqual(nameSpace->prefix,
4392 attr->prefix)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004393 if (qualified < 1)
4394 qualified = 1;
4395 } else
4396 goto found;
4397 } else {
4398 /*
4399 * We should allow applications to define namespaces
4400 * for their application even if the DTD doesn't
4401 * carry one, otherwise, basically we would always
4402 * break.
4403 */
4404 goto found;
4405 }
4406 }
4407 attrib = attrib->next;
4408 }
4409 if (qualified == -1) {
4410 if (attr->prefix == NULL) {
4411 VERROR(ctxt->userData,
4412 "Element %s doesn't carry attribute %s\n",
4413 elem->name, attr->name);
4414 ret = 0;
4415 } else {
4416 VERROR(ctxt->userData,
4417 "Element %s doesn't carry attribute %s:%s\n",
4418 elem->name, attr->prefix,attr->name);
4419 ret = 0;
4420 }
4421 } else if (qualified == 0) {
4422 VWARNING(ctxt->userData,
4423 "Element %s required attribute %s:%s has no prefix\n",
4424 elem->name, attr->prefix,attr->name);
4425 } else if (qualified == 1) {
4426 VWARNING(ctxt->userData,
4427 "Element %s required attribute %s:%s has different prefix\n",
4428 elem->name, attr->prefix,attr->name);
4429 }
4430 }
4431found:
4432 attr = attr->nexth;
4433 }
4434 return(ret);
4435}
4436
4437/**
4438 * xmlValidateRoot:
4439 * @ctxt: the validation context
4440 * @doc: a document instance
4441 *
4442 * Try to validate a the root element
4443 * basically it does the following check as described by the
4444 * XML-1.0 recommendation:
4445 * - [ VC: Root Element Type ]
4446 * it doesn't try to recurse or apply other check to the element
4447 *
4448 * returns 1 if valid or 0 otherwise
4449 */
4450
4451int
4452xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4453 xmlNodePtr root;
4454 if (doc == NULL) return(0);
4455
4456 root = xmlDocGetRootElement(doc);
4457 if ((root == NULL) || (root->name == NULL)) {
4458 VERROR(ctxt->userData, "Not valid: no root element\n");
4459 return(0);
4460 }
4461
4462 /*
4463 * When doing post validation against a separate DTD, those may
4464 * no internal subset has been generated
4465 */
4466 if ((doc->intSubset != NULL) &&
4467 (doc->intSubset->name != NULL)) {
4468 /*
4469 * Check first the document root against the NQName
4470 */
4471 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
4472 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
4473 xmlChar qname[500];
Owen Taylor3473f882001-02-23 17:55:21 +00004474 snprintf((char *) qname, sizeof(qname), "%s:%s",
4475 root->ns->prefix, root->name);
Owen Taylor3473f882001-02-23 17:55:21 +00004476 qname[sizeof(qname) - 1] = 0;
4477 if (xmlStrEqual(doc->intSubset->name, qname))
4478 goto name_ok;
4479 }
4480 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
4481 (xmlStrEqual(root->name, BAD_CAST "html")))
4482 goto name_ok;
4483 VERROR(ctxt->userData,
4484 "Not valid: root and DtD name do not match '%s' and '%s'\n",
4485 root->name, doc->intSubset->name);
4486 return(0);
4487
4488 }
4489 }
4490name_ok:
4491 return(1);
4492}
4493
4494
4495/**
4496 * xmlValidateElement:
4497 * @ctxt: the validation context
4498 * @doc: a document instance
4499 * @elem: an element instance
4500 *
4501 * Try to validate the subtree under an element
4502 *
4503 * returns 1 if valid or 0 otherwise
4504 */
4505
4506int
4507xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
4508 xmlNodePtr child;
4509 xmlAttrPtr attr;
4510 xmlChar *value;
4511 int ret = 1;
4512
4513 if (elem == NULL) return(0);
4514
4515 /*
4516 * XInclude elements were added after parsing in the infoset,
4517 * they don't really mean anything validation wise.
4518 */
4519 if ((elem->type == XML_XINCLUDE_START) ||
4520 (elem->type == XML_XINCLUDE_END))
4521 return(1);
4522
4523 CHECK_DTD;
4524
Daniel Veillard10ea86c2001-06-20 13:55:33 +00004525 /*
4526 * Entities references have to be handled separately
4527 */
4528 if (elem->type == XML_ENTITY_REF_NODE) {
4529 return(1);
4530 }
4531
Owen Taylor3473f882001-02-23 17:55:21 +00004532 ret &= xmlValidateOneElement(ctxt, doc, elem);
4533 attr = elem->properties;
4534 while(attr != NULL) {
4535 value = xmlNodeListGetString(doc, attr->children, 0);
4536 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
4537 if (value != NULL)
4538 xmlFree(value);
4539 attr= attr->next;
4540 }
4541 child = elem->children;
4542 while (child != NULL) {
4543 ret &= xmlValidateElement(ctxt, doc, child);
4544 child = child->next;
4545 }
4546
4547 return(ret);
4548}
4549
Daniel Veillard8730c562001-02-26 10:49:57 +00004550/**
4551 * xmlValidateRef:
4552 * @ref: A reference to be validated
4553 * @ctxt: Validation context
4554 * @name: Name of ID we are searching for
4555 *
4556 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004557static void
Daniel Veillard8730c562001-02-26 10:49:57 +00004558xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00004559 const xmlChar *name) {
4560 xmlAttrPtr id;
4561 xmlAttrPtr attr;
4562
4563 if (ref == NULL)
4564 return;
4565 attr = ref->attr;
4566 if (attr == NULL)
4567 return;
4568 if (attr->atype == XML_ATTRIBUTE_IDREF) {
4569 id = xmlGetID(ctxt->doc, name);
4570 if (id == NULL) {
4571 VERROR(ctxt->userData,
4572 "IDREF attribute %s reference an unknown ID \"%s\"\n",
4573 attr->name, name);
4574 ctxt->valid = 0;
4575 }
4576 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
4577 xmlChar *dup, *str = NULL, *cur, save;
4578
4579 dup = xmlStrdup(name);
4580 if (dup == NULL) {
4581 ctxt->valid = 0;
4582 return;
4583 }
4584 cur = dup;
4585 while (*cur != 0) {
4586 str = cur;
4587 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
4588 save = *cur;
4589 *cur = 0;
4590 id = xmlGetID(ctxt->doc, str);
4591 if (id == NULL) {
4592 VERROR(ctxt->userData,
4593 "IDREFS attribute %s reference an unknown ID \"%s\"\n",
4594 attr->name, str);
4595 ctxt->valid = 0;
4596 }
4597 if (save == 0)
4598 break;
4599 *cur = save;
4600 while (IS_BLANK(*cur)) cur++;
4601 }
4602 xmlFree(dup);
4603 }
4604}
4605
4606/**
Daniel Veillard8730c562001-02-26 10:49:57 +00004607 * xmlWalkValidateList:
4608 * @data: Contents of current link
4609 * @user: Value supplied by the user
4610 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004611 * Returns 0 to abort the walk or 1 to continue
Daniel Veillard8730c562001-02-26 10:49:57 +00004612 */
4613static int
4614xmlWalkValidateList(const void *data, const void *user)
4615{
4616 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
4617 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
4618 return 1;
4619}
4620
4621/**
4622 * xmlValidateCheckRefCallback:
4623 * @ref_list: List of references
4624 * @ctxt: Validation context
4625 * @name: Name of ID we are searching for
4626 *
4627 */
4628static void
4629xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
4630 const xmlChar *name) {
4631 xmlValidateMemo memo;
4632
4633 if (ref_list == NULL)
4634 return;
4635 memo.ctxt = ctxt;
4636 memo.name = name;
4637
4638 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
4639
4640}
4641
4642/**
Owen Taylor3473f882001-02-23 17:55:21 +00004643 * xmlValidateDocumentFinal:
4644 * @ctxt: the validation context
4645 * @doc: a document instance
4646 *
4647 * Does the final step for the document validation once all the
4648 * incremental validation steps have been completed
4649 *
4650 * basically it does the following checks described by the XML Rec
4651 *
4652 *
4653 * returns 1 if valid or 0 otherwise
4654 */
4655
4656int
4657xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4658 xmlRefTablePtr table;
4659
4660 if (doc == NULL) {
4661 xmlGenericError(xmlGenericErrorContext,
4662 "xmlValidateDocumentFinal: doc == NULL\n");
4663 return(0);
4664 }
4665
4666 /*
4667 * Check all the NOTATION/NOTATIONS attributes
4668 */
4669 /*
4670 * Check all the ENTITY/ENTITIES attributes definition for validity
4671 */
4672 /*
4673 * Check all the IDREF/IDREFS attributes definition for validity
4674 */
4675 table = (xmlRefTablePtr) doc->refs;
4676 ctxt->doc = doc;
4677 ctxt->valid = 1;
4678 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
4679 return(ctxt->valid);
4680}
4681
4682/**
4683 * xmlValidateDtd:
4684 * @ctxt: the validation context
4685 * @doc: a document instance
4686 * @dtd: a dtd instance
4687 *
4688 * Try to validate the document against the dtd instance
4689 *
4690 * basically it does check all the definitions in the DtD.
4691 *
4692 * returns 1 if valid or 0 otherwise
4693 */
4694
4695int
4696xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
4697 int ret;
4698 xmlDtdPtr oldExt;
4699 xmlNodePtr root;
4700
4701 if (dtd == NULL) return(0);
4702 if (doc == NULL) return(0);
4703 oldExt = doc->extSubset;
4704 doc->extSubset = dtd;
4705 ret = xmlValidateRoot(ctxt, doc);
4706 if (ret == 0) {
4707 doc->extSubset = oldExt;
4708 return(ret);
4709 }
4710 if (doc->ids != NULL) {
4711 xmlFreeIDTable(doc->ids);
4712 doc->ids = NULL;
4713 }
4714 if (doc->refs != NULL) {
4715 xmlFreeRefTable(doc->refs);
4716 doc->refs = NULL;
4717 }
4718 root = xmlDocGetRootElement(doc);
4719 ret = xmlValidateElement(ctxt, doc, root);
4720 ret &= xmlValidateDocumentFinal(ctxt, doc);
4721 doc->extSubset = oldExt;
4722 return(ret);
4723}
4724
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004725static void
Owen Taylor3473f882001-02-23 17:55:21 +00004726xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004727 const xmlChar *name ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00004728 if (cur == NULL)
4729 return;
4730 switch (cur->atype) {
4731 case XML_ATTRIBUTE_CDATA:
4732 case XML_ATTRIBUTE_ID:
4733 case XML_ATTRIBUTE_IDREF :
4734 case XML_ATTRIBUTE_IDREFS:
4735 case XML_ATTRIBUTE_NMTOKEN:
4736 case XML_ATTRIBUTE_NMTOKENS:
4737 case XML_ATTRIBUTE_ENUMERATION:
4738 break;
4739 case XML_ATTRIBUTE_ENTITY:
4740 case XML_ATTRIBUTE_ENTITIES:
4741 case XML_ATTRIBUTE_NOTATION:
4742 if (cur->defaultValue != NULL) {
4743 ctxt->valid &= xmlValidateAttributeValue2(ctxt, ctxt->doc,
4744 cur->name, cur->atype, cur->defaultValue);
4745 }
4746 if (cur->tree != NULL) {
4747 xmlEnumerationPtr tree = cur->tree;
4748 while (tree != NULL) {
4749 ctxt->valid &= xmlValidateAttributeValue2(ctxt, ctxt->doc,
4750 cur->name, cur->atype, tree->name);
4751 tree = tree->next;
4752 }
4753 }
4754 }
4755}
4756
4757/**
4758 * xmlValidateDtdFinal:
4759 * @ctxt: the validation context
4760 * @doc: a document instance
4761 *
4762 * Does the final step for the dtds validation once all the
4763 * subsets have been parsed
4764 *
4765 * basically it does the following checks described by the XML Rec
4766 * - check that ENTITY and ENTITIES type attributes default or
4767 * possible values matches one of the defined entities.
4768 * - check that NOTATION type attributes default or
4769 * possible values matches one of the defined notations.
4770 *
4771 * returns 1 if valid or 0 otherwise
4772 */
4773
4774int
4775xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4776 int ret = 1;
4777 xmlDtdPtr dtd;
4778 xmlAttributeTablePtr table;
4779
4780 if (doc == NULL) return(0);
4781 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4782 return(0);
4783 ctxt->doc = doc;
4784 ctxt->valid = ret;
4785 dtd = doc->intSubset;
4786 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4787 table = (xmlAttributeTablePtr) dtd->attributes;
4788 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
4789 }
4790 dtd = doc->extSubset;
4791 if ((dtd != NULL) && (dtd->attributes != NULL)) {
4792 table = (xmlAttributeTablePtr) dtd->attributes;
4793 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
4794 }
4795 return(ctxt->valid);
4796}
4797
4798/**
4799 * xmlValidateDocument:
4800 * @ctxt: the validation context
4801 * @doc: a document instance
4802 *
4803 * Try to validate the document instance
4804 *
4805 * basically it does the all the checks described by the XML Rec
4806 * i.e. validates the internal and external subset (if present)
4807 * and validate the document tree.
4808 *
4809 * returns 1 if valid or 0 otherwise
4810 */
4811
4812int
4813xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
4814 int ret;
4815 xmlNodePtr root;
4816
4817 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
4818 return(0);
4819 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
4820 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
4821 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
4822 doc->intSubset->SystemID);
4823 if (doc->extSubset == NULL) {
4824 if (doc->intSubset->SystemID != NULL) {
4825 VERROR(ctxt->userData,
4826 "Could not load the external subset \"%s\"\n",
4827 doc->intSubset->SystemID);
4828 } else {
4829 VERROR(ctxt->userData,
4830 "Could not load the external subset \"%s\"\n",
4831 doc->intSubset->ExternalID);
4832 }
4833 return(0);
4834 }
4835 }
4836
4837 if (doc->ids != NULL) {
4838 xmlFreeIDTable(doc->ids);
4839 doc->ids = NULL;
4840 }
4841 if (doc->refs != NULL) {
4842 xmlFreeRefTable(doc->refs);
4843 doc->refs = NULL;
4844 }
4845 ret = xmlValidateDtdFinal(ctxt, doc);
4846 if (!xmlValidateRoot(ctxt, doc)) return(0);
4847
4848 root = xmlDocGetRootElement(doc);
4849 ret &= xmlValidateElement(ctxt, doc, root);
4850 ret &= xmlValidateDocumentFinal(ctxt, doc);
4851 return(ret);
4852}
4853
4854
4855/************************************************************************
4856 * *
4857 * Routines for dynamic validation editing *
4858 * *
4859 ************************************************************************/
4860
4861/**
4862 * xmlValidGetPotentialChildren:
4863 * @ctree: an element content tree
4864 * @list: an array to store the list of child names
4865 * @len: a pointer to the number of element in the list
4866 * @max: the size of the array
4867 *
4868 * Build/extend a list of potential children allowed by the content tree
4869 *
4870 * returns the number of element in the list, or -1 in case of error.
4871 */
4872
4873int
4874xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
4875 int *len, int max) {
4876 int i;
4877
4878 if ((ctree == NULL) || (list == NULL) || (len == NULL))
4879 return(-1);
4880 if (*len >= max) return(*len);
4881
4882 switch (ctree->type) {
4883 case XML_ELEMENT_CONTENT_PCDATA:
4884 for (i = 0; i < *len;i++)
4885 if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
4886 list[(*len)++] = BAD_CAST "#PCDATA";
4887 break;
4888 case XML_ELEMENT_CONTENT_ELEMENT:
4889 for (i = 0; i < *len;i++)
4890 if (xmlStrEqual(ctree->name, list[i])) return(*len);
4891 list[(*len)++] = ctree->name;
4892 break;
4893 case XML_ELEMENT_CONTENT_SEQ:
4894 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4895 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4896 break;
4897 case XML_ELEMENT_CONTENT_OR:
4898 xmlValidGetPotentialChildren(ctree->c1, list, len, max);
4899 xmlValidGetPotentialChildren(ctree->c2, list, len, max);
4900 break;
4901 }
4902
4903 return(*len);
4904}
4905
4906/**
4907 * xmlValidGetValidElements:
4908 * @prev: an element to insert after
4909 * @next: an element to insert next
4910 * @list: an array to store the list of child names
4911 * @max: the size of the array
4912 *
4913 * This function returns the list of authorized children to insert
4914 * within an existing tree while respecting the validity constraints
4915 * forced by the Dtd. The insertion point is defined using @prev and
4916 * @next in the following ways:
4917 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
4918 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
4919 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
4920 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
4921 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
4922 *
4923 * pointers to the element names are inserted at the beginning of the array
4924 * and do not need to be freed.
4925 *
4926 * returns the number of element in the list, or -1 in case of error. If
4927 * the function returns the value @max the caller is invited to grow the
4928 * receiving array and retry.
4929 */
4930
4931int
4932xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
4933 int max) {
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004934 xmlValidCtxt vctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004935 int nb_valid_elements = 0;
4936 const xmlChar *elements[256];
4937 int nb_elements = 0, i;
Daniel Veillardd455d792002-02-08 13:37:46 +00004938 xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004939
4940 xmlNode *ref_node;
4941 xmlNode *parent;
4942 xmlNode *test_node;
4943
4944 xmlNode *prev_next;
4945 xmlNode *next_prev;
4946 xmlNode *parent_childs;
4947 xmlNode *parent_last;
4948
4949 xmlElement *element_desc;
4950
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004951 vctxt.userData = NULL;
4952 vctxt.error = NULL;
4953 vctxt.warning = NULL;
4954
Owen Taylor3473f882001-02-23 17:55:21 +00004955 if (prev == NULL && next == NULL)
4956 return(-1);
4957
4958 if (list == NULL) return(-1);
4959 if (max <= 0) return(-1);
4960
4961 nb_valid_elements = 0;
4962 ref_node = prev ? prev : next;
4963 parent = ref_node->parent;
4964
4965 /*
4966 * Retrieves the parent element declaration
4967 */
4968 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
4969 parent->name);
4970 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
4971 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
4972 parent->name);
4973 if (element_desc == NULL) return(-1);
4974
4975 /*
4976 * Do a backup of the current tree structure
4977 */
4978 prev_next = prev ? prev->next : NULL;
4979 next_prev = next ? next->prev : NULL;
4980 parent_childs = parent->children;
4981 parent_last = parent->last;
4982
4983 /*
4984 * Creates a dummy node and insert it into the tree
4985 */
4986 test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
4987 test_node->doc = ref_node->doc;
4988 test_node->parent = parent;
4989 test_node->prev = prev;
4990 test_node->next = next;
Daniel Veillardd455d792002-02-08 13:37:46 +00004991 name = test_node->name;
Owen Taylor3473f882001-02-23 17:55:21 +00004992
4993 if (prev) prev->next = test_node;
4994 else parent->children = test_node;
4995
4996 if (next) next->prev = test_node;
4997 else parent->last = test_node;
4998
4999 /*
5000 * Insert each potential child node and check if the parent is
5001 * still valid
5002 */
5003 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
5004 elements, &nb_elements, 256);
5005
5006 for (i = 0;i < nb_elements;i++) {
5007 test_node->name = elements[i];
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005008 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005009 int j;
5010
5011 for (j = 0; j < nb_valid_elements;j++)
5012 if (xmlStrEqual(elements[i], list[j])) break;
5013 list[nb_valid_elements++] = elements[i];
5014 if (nb_valid_elements >= max) break;
5015 }
5016 }
5017
5018 /*
5019 * Restore the tree structure
5020 */
5021 if (prev) prev->next = prev_next;
5022 if (next) next->prev = next_prev;
5023 parent->children = parent_childs;
5024 parent->last = parent_last;
Daniel Veillardd455d792002-02-08 13:37:46 +00005025
5026 /*
5027 * Free up the dummy node
5028 */
5029 test_node->name = name;
5030 xmlFreeNode(test_node);
5031
Owen Taylor3473f882001-02-23 17:55:21 +00005032 return(nb_valid_elements);
5033}