#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <libxml/xmlmemory.h>
#include <libxml/hash.h>
#include <libxml/valid.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/xmlerror.h>
#include <libxml/list.h>
#include <libxml/globals.h>
#define TODO \
xmlGenericError(xmlGenericErrorContext, \
"Unimplemented block at %s:%d\n", \
__FILE__, __LINE__);
#define VERROR \
if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error
#define VWARNING \
if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning
#ifdef LIBXML_REGEXP_ENABLED
typedef struct _xmlValidState {
xmlElementPtr elemDecl;
xmlNodePtr node;
xmlRegExecCtxtPtr exec;
} _xmlValidState;
static int
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
if (ctxt->vstateMax == 0) {
ctxt->vstateMax = 10;
ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
sizeof(ctxt->vstateTab[0]));
if (ctxt->vstateTab == NULL) {
VERROR(ctxt->userData, "realloc failed !n");
return(-1);
}
}
if (ctxt->vstateNr >= ctxt->vstateMax) {
ctxt->vstateMax *= 2;
ctxt->vstateTab = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
if (ctxt->vstateTab == NULL) {
VERROR(ctxt->userData, "realloc failed !n");
return(-1);
}
}
ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
ctxt->vstateTab[ctxt->vstateNr].node = node;
if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
if (elemDecl->contModel == NULL)
xmlValidBuildContentModel(ctxt, elemDecl);
if (elemDecl->contModel != NULL) {
ctxt->vstateTab[ctxt->vstateNr].exec =
xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
} else {
ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
VERROR(ctxt->userData,
"Failed to build content model regexp for %s", node->name);
}
}
return(ctxt->vstateNr++);
}
static int
vstateVPop(xmlValidCtxtPtr ctxt) {
xmlElementPtr elemDecl;
if (ctxt->vstateNr < 1) return(-1);
ctxt->vstateNr--;
elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
ctxt->vstateTab[ctxt->vstateNr].node = NULL;
if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
}
ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
if (ctxt->vstateNr >= 1)
ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
else
ctxt->vstate = NULL;
return(ctxt->vstateNr);
}
#else
#define ROLLBACK_OR 0
#define ROLLBACK_PARENT 1
typedef struct _xmlValidState {
xmlElementContentPtr cont;
xmlNodePtr node;
long occurs;
unsigned char depth;
unsigned char state;
} _xmlValidState;
#define MAX_RECURSE 25000
#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
#define CONT ctxt->vstate->cont
#define NODE ctxt->vstate->node
#define DEPTH ctxt->vstate->depth
#define OCCURS ctxt->vstate->occurs
#define STATE ctxt->vstate->state
#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
static int
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
xmlNodePtr node, unsigned char depth, long occurs,
unsigned char state) {
int i = ctxt->vstateNr - 1;
if (ctxt->vstateNr > MAX_RECURSE) {
return(-1);
}
if (ctxt->vstateNr >= ctxt->vstateMax) {
ctxt->vstateMax *= 2;
ctxt->vstateTab = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
if (ctxt->vstateTab == NULL) {
xmlGenericError(xmlGenericErrorContext,
"realloc failed !n");
return(-1);
}
ctxt->vstate = &ctxt->vstateTab[0];
}
if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
(ctxt->vstateTab[i].node == node) &&
(ctxt->vstateTab[i].depth == depth) &&
(ctxt->vstateTab[i].occurs == occurs) &&
(ctxt->vstateTab[i].state == state))
return(ctxt->vstateNr);
ctxt->vstateTab[ctxt->vstateNr].cont = cont;
ctxt->vstateTab[ctxt->vstateNr].node = node;
ctxt->vstateTab[ctxt->vstateNr].depth = depth;
ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
ctxt->vstateTab[ctxt->vstateNr].state = state;
return(ctxt->vstateNr++);
}
static int
vstateVPop(xmlValidCtxtPtr ctxt) {
if (ctxt->vstateNr <= 1) return(-1);
ctxt->vstateNr--;
ctxt->vstate = &ctxt->vstateTab[0];
ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
return(ctxt->vstateNr);
}
#endif
static int
nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
{
if (ctxt->nodeMax <= 0) {
ctxt->nodeMax = 4;
ctxt->nodeTab =
(xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
sizeof(ctxt->nodeTab[0]));
if (ctxt->nodeTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
ctxt->nodeMax = 0;
return (0);
}
}
if (ctxt->nodeNr >= ctxt->nodeMax) {
ctxt->nodeMax *= 2;
ctxt->nodeTab =
(xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
ctxt->nodeMax *
sizeof(ctxt->nodeTab[0]));
if (ctxt->nodeTab == NULL) {
xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
return (0);
}
}
ctxt->nodeTab[ctxt->nodeNr] = value;
ctxt->node = value;
return (ctxt->nodeNr++);
}
static xmlNodePtr
nodeVPop(xmlValidCtxtPtr ctxt)
{
xmlNodePtr ret;
if (ctxt->nodeNr <= 0)
return (0);
ctxt->nodeNr--;
if (ctxt->nodeNr > 0)
ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
else
ctxt->node = NULL;
ret = ctxt->nodeTab[ctxt->nodeNr];
ctxt->nodeTab[ctxt->nodeNr] = 0;
return (ret);
}
#if 0
void
xmlFreeValidCtxt(xmlValidCtxtPtr ctxt) {
if (ctxt == NULL)
return;
#ifdef LIBXML_REGEXP_ENABLED
while (ctxt->vstateNr >= 0)
vstateVPop(ctxt);
if (ctxt->vstateNr <= 1) return(-1);
ctxt->vstateNr--;
elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
ctxt->vstateTab[ctxt->vstateNr].node = NULL;
if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
}
ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
if (ctxt->vstateNr >= 1)
ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
else
ctxt->vstate = NULL;
return(ctxt->vstateNr);
#else
#endif
}
#endif
#ifdef DEBUG_VALID_ALGO
static void
xmlValidPrintNode(xmlNodePtr cur) {
if (cur == NULL) {
xmlGenericError(xmlGenericErrorContext, "null");
return;
}
switch (cur->type) {
case XML_ELEMENT_NODE:
xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
break;
case XML_TEXT_NODE:
xmlGenericError(xmlGenericErrorContext, "text ");
break;
case XML_CDATA_SECTION_NODE:
xmlGenericError(xmlGenericErrorContext, "cdata ");
break;
case XML_ENTITY_REF_NODE:
xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
break;
case XML_PI_NODE:
xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
break;
case XML_COMMENT_NODE:
xmlGenericError(xmlGenericErrorContext, "comment ");
break;
case XML_ATTRIBUTE_NODE:
xmlGenericError(xmlGenericErrorContext, "?attr? ");
break;
case XML_ENTITY_NODE:
xmlGenericError(xmlGenericErrorContext, "?ent? ");
break;
case XML_DOCUMENT_NODE:
xmlGenericError(xmlGenericErrorContext, "?doc? ");
break;
case XML_DOCUMENT_TYPE_NODE:
xmlGenericError(xmlGenericErrorContext, "?doctype? ");
break;
case XML_DOCUMENT_FRAG_NODE:
xmlGenericError(xmlGenericErrorContext, "?frag? ");
break;
case XML_NOTATION_NODE:
xmlGenericError(xmlGenericErrorContext, "?nota? ");
break;
case XML_HTML_DOCUMENT_NODE:
xmlGenericError(xmlGenericErrorContext, "?html? ");
break;
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
xmlGenericError(xmlGenericErrorContext, "?docb? ");
break;
#endif
case XML_DTD_NODE:
xmlGenericError(xmlGenericErrorContext, "?dtd? ");
break;
case XML_ELEMENT_DECL:
xmlGenericError(xmlGenericErrorContext, "?edecl? ");
break;
case XML_ATTRIBUTE_DECL:
xmlGenericError(xmlGenericErrorContext, "?adecl? ");
break;
case XML_ENTITY_DECL:
xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
break;
case XML_NAMESPACE_DECL:
xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
break;
case XML_XINCLUDE_START:
xmlGenericError(xmlGenericErrorContext, "incstart ");
break;
case XML_XINCLUDE_END:
xmlGenericError(xmlGenericErrorContext, "incend ");
break;
}
}
static void
xmlValidPrintNodeList(xmlNodePtr cur) {
if (cur == NULL)
xmlGenericError(xmlGenericErrorContext, "null ");
while (cur != NULL) {
xmlValidPrintNode(cur);
cur = cur->next;
}
}
static void
xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
char expr[1000];
expr[0] = 0;
xmlGenericError(xmlGenericErrorContext, "valid: ");
xmlValidPrintNodeList(cur);
xmlGenericError(xmlGenericErrorContext, "against ");
xmlSnprintfElementContent(expr, 5000, cont, 1);
xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
}
static void
xmlValidDebugState(xmlValidStatePtr state) {
xmlGenericError(xmlGenericErrorContext, "(");
if (state->cont == NULL)
xmlGenericError(xmlGenericErrorContext, "null,");
else
switch (state->cont->type) {
case XML_ELEMENT_CONTENT_PCDATA:
xmlGenericError(xmlGenericErrorContext, "pcdata,");
break;
case XML_ELEMENT_CONTENT_ELEMENT:
xmlGenericError(xmlGenericErrorContext, "%s,",
state->cont->name);
break;
case XML_ELEMENT_CONTENT_SEQ:
xmlGenericError(xmlGenericErrorContext, "seq,");
break;
case XML_ELEMENT_CONTENT_OR:
xmlGenericError(xmlGenericErrorContext, "or,");
break;
}
xmlValidPrintNode(state->node);
xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
state->depth, state->occurs, state->state);
}
static void
xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
int i, j;
xmlGenericError(xmlGenericErrorContext, "state: ");
xmlValidDebugState(ctxt->vstate);
xmlGenericError(xmlGenericErrorContext, " stack: %d ",
ctxt->vstateNr - 1);
for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
xmlValidDebugState(&ctxt->vstateTab[j]);
xmlGenericError(xmlGenericErrorContext, "\n");
}
#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
#define DEBUG_VALID_MSG(m) \
xmlGenericError(xmlGenericErrorContext, "%s\n", m);
#else
#define DEBUG_VALID_STATE(n,c)
#define DEBUG_VALID_MSG(m)
#endif
#define VECTXT(ctxt, node) \
if ((ctxt != NULL) && (ctxt->error != NULL) && \
(node != NULL)) { \
xmlChar *base = xmlNodeGetBase(NULL,node); \
if (base != NULL) { \
ctxt->error(ctxt->userData, "%s:%d: ", base, \
(int) (long) node->content); \
xmlFree(base); \
} else \
ctxt->error(ctxt->userData, ":%d: ", \
(int) (long) node->content); \
}
#define VWCTXT(ctxt, node) \
if ((ctxt != NULL) && (ctxt->warning != NULL) && \
(node != NULL)) { \
xmlChar *base = xmlNodeGetBase(NULL,node); \
if (base != NULL) { \
ctxt->warning(ctxt->userData, "%s:%d: ", base, \
(int) (long) node->content); \
xmlFree(base); \
} else \
ctxt->warning(ctxt->userData, ":%d: ", \
(int) (long) node->content); \
}
#define CHECK_DTD \
if (doc == NULL) return(0); \
else if ((doc->intSubset == NULL) && \
(doc->extSubset == NULL)) return(0)
static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
int create);
xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem);
#ifdef LIBXML_REGEXP_ENABLED
static int
xmlValidBuildAContentModel(xmlElementContentPtr content,
xmlValidCtxtPtr ctxt,
const xmlChar *name) {
if (content == NULL) {
VERROR(ctxt->userData,
"Found unexpected type = NULL in %s content model\n", name);
return(0);
}
switch (content->type) {
case XML_ELEMENT_CONTENT_PCDATA:
VERROR(ctxt->userData, "ContentModel found PCDATA for element %s\n",
name);
return(0);
break;
case XML_ELEMENT_CONTENT_ELEMENT: {
xmlAutomataStatePtr oldstate = ctxt->state;
xmlChar *QName = NULL;
const xmlChar *fname = content->name;
if (content->prefix != NULL) {
int len;
len = xmlStrlen(content->name) +
xmlStrlen(content->prefix) + 2;
QName = xmlMalloc(len);
if (QName == NULL) {
VERROR(ctxt->userData,
"ContentModel %s : alloc failed\n", name);
return(0);
}
snprintf((char *) QName, len, "%s:%s",
(char *)content->prefix,
(char *)content->name);
fname = QName;
}
switch (content->ocur) {
case XML_ELEMENT_CONTENT_ONCE:
ctxt->state = xmlAutomataNewTransition(ctxt->am,
ctxt->state, NULL, fname, NULL);
break;
case XML_ELEMENT_CONTENT_OPT:
ctxt->state = xmlAutomataNewTransition(ctxt->am,
ctxt->state, NULL, fname, NULL);
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
break;
case XML_ELEMENT_CONTENT_PLUS:
ctxt->state = xmlAutomataNewTransition(ctxt->am,
ctxt->state, NULL, fname, NULL);
xmlAutomataNewTransition(ctxt->am, ctxt->state,
ctxt->state, fname, NULL);
break;
case XML_ELEMENT_CONTENT_MULT:
xmlAutomataNewTransition(ctxt->am, ctxt->state,
ctxt->state, fname, NULL);
ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
NULL);
break;
}
if (QName != NULL)
xmlFree(QName);
break;
}
case XML_ELEMENT_CONTENT_SEQ: {
xmlAutomataStatePtr oldstate, oldend;
xmlElementContentOccur ocur;
oldstate = ctxt->state;
ocur = content->ocur;
if (ocur != XML_ELEMENT_CONTENT_ONCE) {
ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
oldstate = ctxt->state;
}
do {
xmlValidBuildAContentModel(content->c1, ctxt, name);
content = content->c2;
} while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
(content->ocur == XML_ELEMENT_CONTENT_ONCE));
xmlValidBuildAContentModel(content, ctxt, name);
oldend = ctxt->state;
ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
switch (ocur) {
case XML_ELEMENT_CONTENT_ONCE:
break;
case XML_ELEMENT_CONTENT_OPT:
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
break;
case XML_ELEMENT_CONTENT_MULT:
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
break;
case XML_ELEMENT_CONTENT_PLUS:
xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
break;
}
break;
}
case XML_ELEMENT_CONTENT_OR: {
xmlAutomataStatePtr oldstate, oldend;
xmlElementContentOccur ocur;
ocur = content->ocur;
if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
(ocur == XML_ELEMENT_CONTENT_MULT)) {
ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
ctxt->state, NULL);
}
oldstate = ctxt->state;
oldend = xmlAutomataNewState(ctxt->am);
do {
ctxt->state = oldstate;
xmlValidBuildAContentModel(content->c1, ctxt, name);
xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
content = content->c2;
} while ((content->type == XML_ELEMENT_CONTENT_OR) &&
(content->ocur == XML_ELEMENT_CONTENT_ONCE));
ctxt->state = oldstate;
xmlValidBuildAContentModel(content, ctxt, name);
xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
switch (ocur) {
case XML_ELEMENT_CONTENT_ONCE:
break;
case XML_ELEMENT_CONTENT_OPT:
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
break;
case XML_ELEMENT_CONTENT_MULT:
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
break;
case XML_ELEMENT_CONTENT_PLUS:
xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
break;
}
break;
}
default:
VERROR(ctxt->userData, "ContentModel broken for element %s\n",
name);
return(0);
}
return(1);
}
int
xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
xmlAutomataStatePtr start;
if ((ctxt == NULL) || (elem == NULL))
return(0);
if (elem->type != XML_ELEMENT_DECL)
return(0);
if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
return(1);
if (elem->contModel != NULL) {
if (!xmlRegexpIsDeterminist(elem->contModel)) {
ctxt->valid = 0;
return(0);
}
return(1);
}
ctxt->am = xmlNewAutomata();
if (ctxt->am == NULL) {
VERROR(ctxt->userData, "Cannot create automata for element %s\n",
elem->name);
return(0);
}
start = ctxt->state = xmlAutomataGetInitState(ctxt->am);
xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
xmlAutomataSetFinalState(ctxt->am, ctxt->state);
elem->contModel = xmlAutomataCompile(ctxt->am);
if (!xmlRegexpIsDeterminist(elem->contModel)) {
char expr[5000];
expr[0] = 0;
xmlSnprintfElementContent(expr, 5000, elem->content, 1);
VERROR(ctxt->userData, "Content model of %s is not determinist: %s\n",
elem->name, expr);
#ifdef DEBUG_REGEXP_ALGO
xmlRegexpPrint(stderr, elem->contModel);
#endif
ctxt->valid = 0;
ctxt->state = NULL;
xmlFreeAutomata(ctxt->am);
ctxt->am = NULL;
return(0);
}
ctxt->state = NULL;
xmlFreeAutomata(ctxt->am);
ctxt->am = NULL;
return(1);
}
#endif
xmlChar *
xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
int len = 0;
xmlChar *ret = NULL;
*prefix = NULL;
#ifndef XML_XML_NAMESPACE
if ((name[0] == 'x') && (name[1] == 'm') &&
(name[2] == 'l') && (name[3] == ':'))
return(NULL);
#endif
if (name[0] == ':')
return(NULL);
while ((name[len] != 0) && (name[len] != ':'))
len++;
if (name[len] == 0)
return(NULL);
*prefix = xmlStrndup(name, len);
ret = xmlStrdup(&name[len + 1]);
return(ret);
}
xmlElementContentPtr
xmlNewElementContent(xmlChar *name, xmlElementContentType type) {
xmlElementContentPtr ret;
switch(type) {
case XML_ELEMENT_CONTENT_ELEMENT:
if (name == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlNewElementContent : name == NULL !\n");
}
break;
case XML_ELEMENT_CONTENT_PCDATA:
case XML_ELEMENT_CONTENT_SEQ:
case XML_ELEMENT_CONTENT_OR:
if (name != NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlNewElementContent : name != NULL !\n");
}
break;
default:
xmlGenericError(xmlGenericErrorContext,
"xmlNewElementContent: unknown type %d\n", type);
return(NULL);
}
ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlNewElementContent : out of memory!\n");
return(NULL);
}
memset(ret, 0, sizeof(xmlElementContent));
ret->type = type;
ret->ocur = XML_ELEMENT_CONTENT_ONCE;
if (name != NULL) {
xmlChar *prefix = NULL;
ret->name = xmlSplitQName2(name, &prefix);
if (ret->name == NULL)
ret->name = xmlStrdup(name);
ret->prefix = prefix;
} else {
ret->name = NULL;
ret->prefix = NULL;
}
ret->c1 = ret->c2 = ret->parent = NULL;
return(ret);
}
xmlElementContentPtr
xmlCopyElementContent(xmlElementContentPtr cur) {
xmlElementContentPtr ret;
if (cur == NULL) return(NULL);
ret = xmlNewElementContent((xmlChar *) cur->name, cur->type);
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlCopyElementContent : out of memory\n");
return(NULL);
}
if (cur->prefix != NULL)
ret->prefix = xmlStrdup(cur->prefix);
ret->ocur = cur->ocur;
if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1);
if (ret->c1 != NULL)
ret->c1->parent = ret;
if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2);
if (ret->c2 != NULL)
ret->c2->parent = ret;
return(ret);
}
void
xmlFreeElementContent(xmlElementContentPtr cur) {
if (cur == NULL) return;
switch (cur->type) {
case XML_ELEMENT_CONTENT_PCDATA:
case XML_ELEMENT_CONTENT_ELEMENT:
case XML_ELEMENT_CONTENT_SEQ:
case XML_ELEMENT_CONTENT_OR:
break;
default:
xmlGenericError(xmlGenericErrorContext,
"xmlFreeElementContent : type %d\n", cur->type);
return;
}
if (cur->c1 != NULL) xmlFreeElementContent(cur->c1);
if (cur->c2 != NULL) xmlFreeElementContent(cur->c2);
if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
xmlFree(cur);
}
static void
xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
if (content == NULL) return;
if (glob) xmlBufferWriteChar(buf, "(");
switch (content->type) {
case XML_ELEMENT_CONTENT_PCDATA:
xmlBufferWriteChar(buf, "#PCDATA");
break;
case XML_ELEMENT_CONTENT_ELEMENT:
if (content->prefix != NULL) {
xmlBufferWriteCHAR(buf, content->prefix);
xmlBufferWriteChar(buf, ":");
}
xmlBufferWriteCHAR(buf, content->name);
break;
case XML_ELEMENT_CONTENT_SEQ:
if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
(content->c1->type == XML_ELEMENT_CONTENT_SEQ))
xmlDumpElementContent(buf, content->c1, 1);
else
xmlDumpElementContent(buf, content->c1, 0);
xmlBufferWriteChar(buf, " , ");
if (content->c2->type == XML_ELEMENT_CONTENT_OR)
xmlDumpElementContent(buf, content->c2, 1);
else
xmlDumpElementContent(buf, content->c2, 0);
break;
case XML_ELEMENT_CONTENT_OR:
if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
(content->c1->type == XML_ELEMENT_CONTENT_SEQ))
xmlDumpElementContent(buf, content->c1, 1);
else
xmlDumpElementContent(buf, content->c1, 0);
xmlBufferWriteChar(buf, " | ");
if (content->c2->type == XML_ELEMENT_CONTENT_SEQ)
xmlDumpElementContent(buf, content->c2, 1);
else
xmlDumpElementContent(buf, content->c2, 0);
break;
default:
xmlGenericError(xmlGenericErrorContext,
"xmlDumpElementContent: unknown type %d\n",
content->type);
}
if (glob)
xmlBufferWriteChar(buf, ")");
switch (content->ocur) {
case XML_ELEMENT_CONTENT_ONCE:
break;
case XML_ELEMENT_CONTENT_OPT:
xmlBufferWriteChar(buf, "?");
break;
case XML_ELEMENT_CONTENT_MULT:
xmlBufferWriteChar(buf, "*");
break;
case XML_ELEMENT_CONTENT_PLUS:
xmlBufferWriteChar(buf, "+");
break;
}
}
void
xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
xmlElementContentPtr content ATTRIBUTE_UNUSED,
int glob ATTRIBUTE_UNUSED) {
}
void
xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) {
int len;
if (content == NULL) return;
len = strlen(buf);
if (size - len < 50) {
if ((size - len > 4) && (buf[len - 1] != '.'))
strcat(buf, " ...");
return;
}
if (glob) strcat(buf, "(");
switch (content->type) {
case XML_ELEMENT_CONTENT_PCDATA:
strcat(buf, "#PCDATA");
break;
case XML_ELEMENT_CONTENT_ELEMENT:
if (content->prefix != NULL) {
if (size - len < xmlStrlen(content->prefix) + 10) {
strcat(buf, " ...");
return;
}
strcat(buf, (char *) content->prefix);
strcat(buf, ":");
}
if (size - len < xmlStrlen(content->name) + 10) {
strcat(buf, " ...");
return;
}
strcat(buf, (char *) content->name);
break;
case XML_ELEMENT_CONTENT_SEQ:
if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
(content->c1->type == XML_ELEMENT_CONTENT_SEQ))
xmlSnprintfElementContent(buf, size, content->c1, 1);
else
xmlSnprintfElementContent(buf, size, content->c1, 0);
len = strlen(buf);
if (size - len < 50) {
if ((size - len > 4) && (buf[len - 1] != '.'))
strcat(buf, " ...");
return;
}
strcat(buf, " , ");
if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
(content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
(content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
xmlSnprintfElementContent(buf, size, content->c2, 1);
else
xmlSnprintfElementContent(buf, size, content->c2, 0);
break;
case XML_ELEMENT_CONTENT_OR:
if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
(content->c1->type == XML_ELEMENT_CONTENT_SEQ))
xmlSnprintfElementContent(buf, size, content->c1, 1);
else
xmlSnprintfElementContent(buf, size, content->c1, 0);
len = strlen(buf);
if (size - len < 50) {
if ((size - len > 4) && (buf[len - 1] != '.'))
strcat(buf, " ...");
return;
}
strcat(buf, " | ");
if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
(content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
(content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
xmlSnprintfElementContent(buf, size, content->c2, 1);
else
xmlSnprintfElementContent(buf, size, content->c2, 0);
break;
}
if (glob)
strcat(buf, ")");
switch (content->ocur) {
case XML_ELEMENT_CONTENT_ONCE:
break;
case XML_ELEMENT_CONTENT_OPT:
strcat(buf, "?");
break;
case XML_ELEMENT_CONTENT_MULT:
strcat(buf, "*");
break;
case XML_ELEMENT_CONTENT_PLUS:
strcat(buf, "+");
break;
}
}
static xmlElementTablePtr
xmlCreateElementTable(void) {
return(xmlHashCreate(0));
}
static void
xmlFreeElement(xmlElementPtr elem) {
if (elem == NULL) return;
xmlUnlinkNode((xmlNodePtr) elem);
xmlFreeElementContent(elem->content);
if (elem->name != NULL)
xmlFree((xmlChar *) elem->name);
if (elem->prefix != NULL)
xmlFree((xmlChar *) elem->prefix);
#ifdef LIBXML_REGEXP_ENABLED
if (elem->contModel != NULL)
xmlRegFreeRegexp(elem->contModel);
#endif
xmlFree(elem);
}
xmlElementPtr
xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
xmlElementTypeVal type,
xmlElementContentPtr content) {
xmlElementPtr ret;
xmlElementTablePtr table;
xmlAttributePtr oldAttributes = NULL;
xmlChar *ns, *uqname;
if (dtd == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddElementDecl: dtd == NULL\n");
return(NULL);
}
if (name == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddElementDecl: name == NULL\n");
return(NULL);
}
switch (type) {
case XML_ELEMENT_TYPE_EMPTY:
if (content != NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddElementDecl: content != NULL for EMPTY\n");
return(NULL);
}
break;
case XML_ELEMENT_TYPE_ANY:
if (content != NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddElementDecl: content != NULL for ANY\n");
return(NULL);
}
break;
case XML_ELEMENT_TYPE_MIXED:
if (content == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddElementDecl: content == NULL for MIXED\n");
return(NULL);
}
break;
case XML_ELEMENT_TYPE_ELEMENT:
if (content == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddElementDecl: content == NULL for ELEMENT\n");
return(NULL);
}
break;
default:
xmlGenericError(xmlGenericErrorContext,
"xmlAddElementDecl: unknown type %d\n", type);
return(NULL);
}
uqname = xmlSplitQName2(name, &ns);
if (uqname != NULL)
name = uqname;
table = (xmlElementTablePtr) dtd->elements;
if (table == NULL) {
table = xmlCreateElementTable();
dtd->elements = (void *) table;
}
if (table == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddElementDecl: Table creation failed!\n");
return(NULL);
}
if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
oldAttributes = ret->attributes;
ret->attributes = NULL;
xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
xmlFreeElement(ret);
}
}
ret = xmlHashLookup2(table, name, ns);
if (ret != NULL) {
if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
VERROR(ctxt->userData, "Redefinition of element %s\n", name);
if (uqname != NULL)
xmlFree(uqname);
return(NULL);
}
} else {
ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddElementDecl: out of memory\n");
return(NULL);
}
memset(ret, 0, sizeof(xmlElement));
ret->type = XML_ELEMENT_DECL;
ret->name = xmlStrdup(name);
ret->prefix = ns;
if (xmlHashAddEntry2(table, name, ns, ret)) {
VERROR(ctxt->userData, "Redefinition of element %s\n", name);
xmlFreeElement(ret);
if (uqname != NULL)
xmlFree(uqname);
return(NULL);
}
}
ret->etype = type;
ret->content = xmlCopyElementContent(content);
ret->attributes = oldAttributes;
ret->parent = dtd;
ret->doc = dtd->doc;
if (dtd->last == NULL) {
dtd->children = dtd->last = (xmlNodePtr) ret;
} else {
dtd->last->next = (xmlNodePtr) ret;
ret->prev = dtd->last;
dtd->last = (xmlNodePtr) ret;
}
if (uqname != NULL)
xmlFree(uqname);
return(ret);
}
void
xmlFreeElementTable(xmlElementTablePtr table) {
xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
}
static xmlElementPtr
xmlCopyElement(xmlElementPtr elem) {
xmlElementPtr cur;
cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
if (cur == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlCopyElement: out of memory !\n");
return(NULL);
}
memset(cur, 0, sizeof(xmlElement));
cur->type = XML_ELEMENT_DECL;
cur->etype = elem->etype;
if (elem->name != NULL)
cur->name = xmlStrdup(elem->name);
else
cur->name = NULL;
if (elem->prefix != NULL)
cur->prefix = xmlStrdup(elem->prefix);
else
cur->prefix = NULL;
cur->content = xmlCopyElementContent(elem->content);
cur->attributes = NULL;
return(cur);
}
xmlElementTablePtr
xmlCopyElementTable(xmlElementTablePtr table) {
return((xmlElementTablePtr) xmlHashCopy(table,
(xmlHashCopier) xmlCopyElement));
}
void
xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
switch (elem->etype) {
case XML_ELEMENT_TYPE_EMPTY:
xmlBufferWriteChar(buf, "<!ELEMENT ");
if (elem->prefix != NULL) {
xmlBufferWriteCHAR(buf, elem->prefix);
xmlBufferWriteChar(buf, ":");
}
xmlBufferWriteCHAR(buf, elem->name);
xmlBufferWriteChar(buf, " EMPTY>\n");
break;
case XML_ELEMENT_TYPE_ANY:
xmlBufferWriteChar(buf, "<!ELEMENT ");
if (elem->prefix != NULL) {
xmlBufferWriteCHAR(buf, elem->prefix);
xmlBufferWriteChar(buf, ":");
}
xmlBufferWriteCHAR(buf, elem->name);
xmlBufferWriteChar(buf, " ANY>\n");
break;
case XML_ELEMENT_TYPE_MIXED:
xmlBufferWriteChar(buf, "<!ELEMENT ");
if (elem->prefix != NULL) {
xmlBufferWriteCHAR(buf, elem->prefix);
xmlBufferWriteChar(buf, ":");
}
xmlBufferWriteCHAR(buf, elem->name);
xmlBufferWriteChar(buf, " ");
xmlDumpElementContent(buf, elem->content, 1);
xmlBufferWriteChar(buf, ">\n");
break;
case XML_ELEMENT_TYPE_ELEMENT:
xmlBufferWriteChar(buf, "<!ELEMENT ");
if (elem->prefix != NULL) {
xmlBufferWriteCHAR(buf, elem->prefix);
xmlBufferWriteChar(buf, ":");
}
xmlBufferWriteCHAR(buf, elem->name);
xmlBufferWriteChar(buf, " ");
xmlDumpElementContent(buf, elem->content, 1);
xmlBufferWriteChar(buf, ">\n");
break;
default:
xmlGenericError(xmlGenericErrorContext,
"xmlDumpElementDecl: internal: unknown type %d\n",
elem->etype);
}
}
void
xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf);
}
xmlEnumerationPtr
xmlCreateEnumeration(xmlChar *name) {
xmlEnumerationPtr ret;
ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlCreateEnumeration : xmlMalloc(%ld) failed\n",
(long)sizeof(xmlEnumeration));
return(NULL);
}
memset(ret, 0, sizeof(xmlEnumeration));
if (name != NULL)
ret->name = xmlStrdup(name);
return(ret);
}
void
xmlFreeEnumeration(xmlEnumerationPtr cur) {
if (cur == NULL) return;
if (cur->next != NULL) xmlFreeEnumeration(cur->next);
if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
xmlFree(cur);
}
xmlEnumerationPtr
xmlCopyEnumeration(xmlEnumerationPtr cur) {
xmlEnumerationPtr ret;
if (cur == NULL) return(NULL);
ret = xmlCreateEnumeration((xmlChar *) cur->name);
if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
else ret->next = NULL;
return(ret);
}
static void
xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
if (cur == NULL) return;
xmlBufferWriteCHAR(buf, cur->name);
if (cur->next == NULL)
xmlBufferWriteChar(buf, ")");
else {
xmlBufferWriteChar(buf, " | ");
xmlDumpEnumeration(buf, cur->next);
}
}
static xmlAttributeTablePtr
xmlCreateAttributeTable(void) {
return(xmlHashCreate(0));
}
static void
xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
const xmlChar* name ATTRIBUTE_UNUSED) {
attr->nexth = *list;
*list = attr;
}
xmlAttributePtr
xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
xmlAttributePtr ret = NULL;
xmlAttributeTablePtr table;
if (dtd == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlScanAttributeDecl: dtd == NULL\n");
return(NULL);
}
if (elem == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlScanAttributeDecl: elem == NULL\n");
return(NULL);
}
table = (xmlAttributeTablePtr) dtd->attributes;
if (table == NULL)
return(NULL);
xmlHashScan3(table, NULL, NULL, elem,
(xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
return(ret);
}
static int
xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
xmlAttributePtr cur;
int ret = 0;
if (elem == NULL) return(0);
cur = elem->attributes;
while (cur != NULL) {
if (cur->atype == XML_ATTRIBUTE_ID) {
ret ++;
if (ret > 1)
VERROR(ctxt->userData,
"Element %s has too many ID attributes defined : %s\n",
elem->name, cur->name);
}
cur = cur->nexth;
}
return(ret);
}
static void
xmlFreeAttribute(xmlAttributePtr attr) {
if (attr == NULL) return;
xmlUnlinkNode((xmlNodePtr) attr);
if (attr->tree != NULL)
xmlFreeEnumeration(attr->tree);
if (attr->elem != NULL)
xmlFree((xmlChar *) attr->elem);
if (attr->name != NULL)
xmlFree((xmlChar *) attr->name);
if (attr->defaultValue != NULL)
xmlFree((xmlChar *) attr->defaultValue);
if (attr->prefix != NULL)
xmlFree((xmlChar *) attr->prefix);
xmlFree(attr);
}
xmlAttributePtr
xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem,
const xmlChar *name, const xmlChar *ns,
xmlAttributeType type, xmlAttributeDefault def,
const xmlChar *defaultValue, xmlEnumerationPtr tree) {
xmlAttributePtr ret;
xmlAttributeTablePtr table;
xmlElementPtr elemDef;
if (dtd == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddAttributeDecl: dtd == NULL\n");
xmlFreeEnumeration(tree);
return(NULL);
}
if (name == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddAttributeDecl: name == NULL\n");
xmlFreeEnumeration(tree);
return(NULL);
}
if (elem == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddAttributeDecl: elem == NULL\n");
xmlFreeEnumeration(tree);
return(NULL);
}
switch (type) {
case XML_ATTRIBUTE_CDATA:
break;
case XML_ATTRIBUTE_ID:
break;
case XML_ATTRIBUTE_IDREF:
break;
case XML_ATTRIBUTE_IDREFS:
break;
case XML_ATTRIBUTE_ENTITY:
break;
case XML_ATTRIBUTE_ENTITIES:
break;
case XML_ATTRIBUTE_NMTOKEN:
break;
case XML_ATTRIBUTE_NMTOKENS:
break;
case XML_ATTRIBUTE_ENUMERATION:
break;
case XML_ATTRIBUTE_NOTATION:
break;
default:
xmlGenericError(xmlGenericErrorContext,
"xmlAddAttributeDecl: unknown type %d\n", type);
xmlFreeEnumeration(tree);
return(NULL);
}
if ((defaultValue != NULL) &&
(!xmlValidateAttributeValue(type, defaultValue))) {
VERROR(ctxt->userData, "Attribute %s of %s: invalid default value\n",
elem, name, defaultValue);
defaultValue = NULL;
ctxt->valid = 0;
}
if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
(dtd->doc->intSubset != NULL) &&
(dtd->doc->intSubset->attributes != NULL)) {
ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
if (ret != NULL)
return(NULL);
}
table = (xmlAttributeTablePtr) dtd->attributes;
if (table == NULL) {
table = xmlCreateAttributeTable();
dtd->attributes = (void *) table;
}
if (table == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddAttributeDecl: Table creation failed!\n");
return(NULL);
}
ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddAttributeDecl: out of memory\n");
return(NULL);
}
memset(ret, 0, sizeof(xmlAttribute));
ret->type = XML_ATTRIBUTE_DECL;
ret->atype = type;
ret->name = xmlStrdup(name);
ret->prefix = xmlStrdup(ns);
ret->elem = xmlStrdup(elem);
ret->def = def;
ret->tree = tree;
if (defaultValue != NULL)
ret->defaultValue = xmlStrdup(defaultValue);
if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) {
VWARNING(ctxt->userData,
"Attribute %s of element %s: already defined\n",
name, elem);
xmlFreeAttribute(ret);
return(NULL);
}
elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
if (elemDef != NULL) {
if ((type == XML_ATTRIBUTE_ID) &&
(xmlScanIDAttributeDecl(NULL, elemDef) != 0)) {
VERROR(ctxt->userData,
"Element %s has too may ID attributes defined : %s\n",
elem, name);
ctxt->valid = 0;
}
if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
((ret->prefix != NULL &&
(xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
ret->nexth = elemDef->attributes;
elemDef->attributes = ret;
} else {
xmlAttributePtr tmp = elemDef->attributes;
while ((tmp != NULL) &&
((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
((ret->prefix != NULL &&
(xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
if (tmp->nexth == NULL)
break;
tmp = tmp->nexth;
}
if (tmp != NULL) {
ret->nexth = tmp->nexth;
tmp->nexth = ret;
} else {
ret->nexth = elemDef->attributes;
elemDef->attributes = ret;
}
}
}
ret->parent = dtd;
ret->doc = dtd->doc;
if (dtd->last == NULL) {
dtd->children = dtd->last = (xmlNodePtr) ret;
} else {
dtd->last->next = (xmlNodePtr) ret;
ret->prev = dtd->last;
dtd->last = (xmlNodePtr) ret;
}
return(ret);
}
void
xmlFreeAttributeTable(xmlAttributeTablePtr table) {
xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
}
static xmlAttributePtr
xmlCopyAttribute(xmlAttributePtr attr) {
xmlAttributePtr cur;
cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
if (cur == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlCopyAttribute: out of memory !\n");
return(NULL);
}
memset(cur, 0, sizeof(xmlAttribute));
cur->type = XML_ATTRIBUTE_DECL;
cur->atype = attr->atype;
cur->def = attr->def;
cur->tree = xmlCopyEnumeration(attr->tree);
if (attr->elem != NULL)
cur->elem = xmlStrdup(attr->elem);
if (attr->name != NULL)
cur->name = xmlStrdup(attr->name);
if (attr->prefix != NULL)
cur->prefix = xmlStrdup(attr->prefix);
if (attr->defaultValue != NULL)
cur->defaultValue = xmlStrdup(attr->defaultValue);
return(cur);
}
xmlAttributeTablePtr
xmlCopyAttributeTable(xmlAttributeTablePtr table) {
return((xmlAttributeTablePtr) xmlHashCopy(table,
(xmlHashCopier) xmlCopyAttribute));
}
void
xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
xmlBufferWriteChar(buf, "<!ATTLIST ");
xmlBufferWriteCHAR(buf, attr->elem);
xmlBufferWriteChar(buf, " ");
if (attr->prefix != NULL) {
xmlBufferWriteCHAR(buf, attr->prefix);
xmlBufferWriteChar(buf, ":");
}
xmlBufferWriteCHAR(buf, attr->name);
switch (attr->atype) {
case XML_ATTRIBUTE_CDATA:
xmlBufferWriteChar(buf, " CDATA");
break;
case XML_ATTRIBUTE_ID:
xmlBufferWriteChar(buf, " ID");
break;
case XML_ATTRIBUTE_IDREF:
xmlBufferWriteChar(buf, " IDREF");
break;
case XML_ATTRIBUTE_IDREFS:
xmlBufferWriteChar(buf, " IDREFS");
break;
case XML_ATTRIBUTE_ENTITY:
xmlBufferWriteChar(buf, " ENTITY");
break;
case XML_ATTRIBUTE_ENTITIES:
xmlBufferWriteChar(buf, " ENTITIES");
break;
case XML_ATTRIBUTE_NMTOKEN:
xmlBufferWriteChar(buf, " NMTOKEN");
break;
case XML_ATTRIBUTE_NMTOKENS:
xmlBufferWriteChar(buf, " NMTOKENS");
break;
case XML_ATTRIBUTE_ENUMERATION:
xmlBufferWriteChar(buf, " (");
xmlDumpEnumeration(buf, attr->tree);
break;
case XML_ATTRIBUTE_NOTATION:
xmlBufferWriteChar(buf, " NOTATION (");
xmlDumpEnumeration(buf, attr->tree);
break;
default:
xmlGenericError(xmlGenericErrorContext,
"xmlDumpAttributeDecl: internal: unknown type %d\n",
attr->atype);
}
switch (attr->def) {
case XML_ATTRIBUTE_NONE:
break;
case XML_ATTRIBUTE_REQUIRED:
xmlBufferWriteChar(buf, " #REQUIRED");
break;
case XML_ATTRIBUTE_IMPLIED:
xmlBufferWriteChar(buf, " #IMPLIED");
break;
case XML_ATTRIBUTE_FIXED:
xmlBufferWriteChar(buf, " #FIXED");
break;
default:
xmlGenericError(xmlGenericErrorContext,
"xmlDumpAttributeDecl: internal: unknown default %d\n",
attr->def);
}
if (attr->defaultValue != NULL) {
xmlBufferWriteChar(buf, " ");
xmlBufferWriteQuotedString(buf, attr->defaultValue);
}
xmlBufferWriteChar(buf, ">\n");
}
void
xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf);
}
static xmlNotationTablePtr
xmlCreateNotationTable(void) {
return(xmlHashCreate(0));
}
static void
xmlFreeNotation(xmlNotationPtr nota) {
if (nota == NULL) return;
if (nota->name != NULL)
xmlFree((xmlChar *) nota->name);
if (nota->PublicID != NULL)
xmlFree((xmlChar *) nota->PublicID);
if (nota->SystemID != NULL)
xmlFree((xmlChar *) nota->SystemID);
xmlFree(nota);
}
xmlNotationPtr
xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd,
const xmlChar *name,
const xmlChar *PublicID, const xmlChar *SystemID) {
xmlNotationPtr ret;
xmlNotationTablePtr table;
if (dtd == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddNotationDecl: dtd == NULL\n");
return(NULL);
}
if (name == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddNotationDecl: name == NULL\n");
return(NULL);
}
if ((PublicID == NULL) && (SystemID == NULL)) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n");
return(NULL);
}
table = (xmlNotationTablePtr) dtd->notations;
if (table == NULL)
dtd->notations = table = xmlCreateNotationTable();
if (table == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddNotationDecl: Table creation failed!\n");
return(NULL);
}
ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddNotationDecl: out of memory\n");
return(NULL);
}
memset(ret, 0, sizeof(xmlNotation));
ret->name = xmlStrdup(name);
if (SystemID != NULL)
ret->SystemID = xmlStrdup(SystemID);
if (PublicID != NULL)
ret->PublicID = xmlStrdup(PublicID);
if (xmlHashAddEntry(table, name, ret)) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddNotationDecl: %s already defined\n", name);
xmlFreeNotation(ret);
return(NULL);
}
return(ret);
}
void
xmlFreeNotationTable(xmlNotationTablePtr table) {
xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
}
static xmlNotationPtr
xmlCopyNotation(xmlNotationPtr nota) {
xmlNotationPtr cur;
cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
if (cur == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlCopyNotation: out of memory !\n");
return(NULL);
}
if (nota->name != NULL)
cur->name = xmlStrdup(nota->name);
else
cur->name = NULL;
if (nota->PublicID != NULL)
cur->PublicID = xmlStrdup(nota->PublicID);
else
cur->PublicID = NULL;
if (nota->SystemID != NULL)
cur->SystemID = xmlStrdup(nota->SystemID);
else
cur->SystemID = NULL;
return(cur);
}
xmlNotationTablePtr
xmlCopyNotationTable(xmlNotationTablePtr table) {
return((xmlNotationTablePtr) xmlHashCopy(table,
(xmlHashCopier) xmlCopyNotation));
}
void
xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
xmlBufferWriteChar(buf, "<!NOTATION ");
xmlBufferWriteCHAR(buf, nota->name);
if (nota->PublicID != NULL) {
xmlBufferWriteChar(buf, " PUBLIC ");
xmlBufferWriteQuotedString(buf, nota->PublicID);
if (nota->SystemID != NULL) {
xmlBufferWriteChar(buf, " ");
xmlBufferWriteCHAR(buf, nota->SystemID);
}
} else {
xmlBufferWriteChar(buf, " SYSTEM ");
xmlBufferWriteCHAR(buf, nota->SystemID);
}
xmlBufferWriteChar(buf, " >\n");
}
void
xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf);
}
static xmlIDTablePtr
xmlCreateIDTable(void) {
return(xmlHashCreate(0));
}
static void
xmlFreeID(xmlIDPtr id) {
if (id == NULL) return;
if (id->value != NULL)
xmlFree((xmlChar *) id->value);
if (id->name != NULL)
xmlFree((xmlChar *) id->name);
xmlFree(id);
}
xmlIDPtr
xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
xmlAttrPtr attr) {
xmlIDPtr ret;
xmlIDTablePtr table;
if (doc == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddID: doc == NULL\n");
return(NULL);
}
if (value == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddID: value == NULL\n");
return(NULL);
}
if (attr == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddID: attr == NULL\n");
return(NULL);
}
table = (xmlIDTablePtr) doc->ids;
if (table == NULL)
doc->ids = table = xmlCreateIDTable();
if (table == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddID: Table creation failed!\n");
return(NULL);
}
ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddID: out of memory\n");
return(NULL);
}
ret->value = xmlStrdup(value);
if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
ret->name = xmlStrdup(attr->name);
ret->attr = NULL;
} else {
ret->attr = attr;
ret->name = NULL;
}
ret->lineno = xmlGetLineNo(attr->parent);
if (xmlHashAddEntry(table, value, ret) < 0) {
if (ctxt != NULL) {
VECTXT(ctxt, attr->parent);
VERROR(ctxt->userData, "ID %s already defined\n", value);
}
xmlFreeID(ret);
return(NULL);
}
return(ret);
}
void
xmlFreeIDTable(xmlIDTablePtr table) {
xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
}
int
xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
if (doc == NULL) return(0);
if (attr == NULL) return(0);
if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
return(0);
} else if (doc->type == XML_HTML_DOCUMENT_NODE) {
if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
(xmlStrEqual(BAD_CAST "name", attr->name)))
return(1);
return(0);
} else {
xmlAttributePtr attrDecl;
if (elem == NULL) return(0);
if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
int len = xmlStrlen(elem->name) + xmlStrlen(elem->ns->prefix) + 2;
xmlChar *fullname;
fullname = xmlMalloc(len);
if (fullname == NULL)
return(0);
snprintf((char *) fullname, len, "%s:%s", (char *) elem->ns->prefix,
(char *) elem->name);
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
attr->name);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
attr->name);
xmlFree(fullname);
} else {
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name,
attr->name);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,
attr->name);
}
if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
return(1);
}
return(0);
}
int
xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
xmlAttrPtr cur;
xmlIDTablePtr table;
xmlChar *ID;
if (doc == NULL) return(-1);
if (attr == NULL) return(-1);
table = (xmlIDTablePtr) doc->ids;
if (table == NULL)
return(-1);
if (attr == NULL)
return(-1);
ID = xmlNodeListGetString(doc, attr->children, 1);
if (ID == NULL)
return(-1);
cur = xmlHashLookup(table, ID);
if (cur != attr) {
xmlFree(ID);
return(-1);
}
xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID);
xmlFree(ID);
return(0);
}
xmlAttrPtr
xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
xmlIDTablePtr table;
xmlIDPtr id;
if (doc == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n");
return(NULL);
}
if (ID == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n");
return(NULL);
}
table = (xmlIDTablePtr) doc->ids;
if (table == NULL)
return(NULL);
id = xmlHashLookup(table, ID);
if (id == NULL)
return(NULL);
if (id->attr == NULL) {
return((xmlAttrPtr) doc);
}
return(id->attr);
}
typedef struct xmlRemoveMemo_t
{
xmlListPtr l;
xmlAttrPtr ap;
} xmlRemoveMemo;
typedef xmlRemoveMemo *xmlRemoveMemoPtr;
typedef struct xmlValidateMemo_t
{
xmlValidCtxtPtr ctxt;
const xmlChar *name;
} xmlValidateMemo;
typedef xmlValidateMemo *xmlValidateMemoPtr;
static xmlRefTablePtr
xmlCreateRefTable(void) {
return(xmlHashCreate(0));
}
static void
xmlFreeRef(xmlLinkPtr lk) {
xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
if (ref == NULL) return;
if (ref->value != NULL)
xmlFree((xmlChar *)ref->value);
if (ref->name != NULL)
xmlFree((xmlChar *)ref->name);
xmlFree(ref);
}
static void
xmlFreeRefList(xmlListPtr list_ref) {
if (list_ref == NULL) return;
xmlListDelete(list_ref);
}
static int
xmlWalkRemoveRef(const void *data, const void *user)
{
xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
if (attr0 == attr1) {
xmlListRemoveFirst(ref_list, (void *)data);
return 0;
}
return 1;
}
xmlRefPtr
xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value,
xmlAttrPtr attr) {
xmlRefPtr ret;
xmlRefTablePtr table;
xmlListPtr ref_list;
if (doc == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddRef: doc == NULL\n");
return(NULL);
}
if (value == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddRef: value == NULL\n");
return(NULL);
}
if (attr == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddRef: attr == NULL\n");
return(NULL);
}
table = (xmlRefTablePtr) doc->refs;
if (table == NULL)
doc->refs = table = xmlCreateRefTable();
if (table == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddRef: Table creation failed!\n");
return(NULL);
}
ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
if (ret == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddRef: out of memory\n");
return(NULL);
}
ret->value = xmlStrdup(value);
if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
ret->name = xmlStrdup(attr->name);
ret->attr = NULL;
} else {
ret->name = NULL;
ret->attr = attr;
}
ret->lineno = xmlGetLineNo(attr->parent);
if (NULL == (ref_list = xmlHashLookup(table, value))) {
if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) {
xmlGenericError(xmlGenericErrorContext,
"xmlAddRef: Reference list creation failed!\n");
return(NULL);
}
if (xmlHashAddEntry(table, value, ref_list) < 0) {
xmlListDelete(ref_list);
xmlGenericError(xmlGenericErrorContext,
"xmlAddRef: Reference list insertion failed!\n");
return(NULL);
}
}
xmlListInsert(ref_list, ret);
return(ret);
}
void
xmlFreeRefTable(xmlRefTablePtr table) {
xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
}
int
xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
return(0);
} else if (doc->type == XML_HTML_DOCUMENT_NODE) {
return(0);
} else {
xmlAttributePtr attrDecl;
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
elem->name, attr->name);
if ((attrDecl != NULL) &&
(attrDecl->atype == XML_ATTRIBUTE_IDREF ||
attrDecl->atype == XML_ATTRIBUTE_IDREFS))
return(1);
}
return(0);
}
int
xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
xmlListPtr ref_list;
xmlRefTablePtr table;
xmlChar *ID;
xmlRemoveMemo target;
if (doc == NULL) return(-1);
if (attr == NULL) return(-1);
table = (xmlRefTablePtr) doc->refs;
if (table == NULL)
return(-1);
if (attr == NULL)
return(-1);
ID = xmlNodeListGetString(doc, attr->children, 1);
if (ID == NULL)
return(-1);
ref_list = xmlHashLookup(table, ID);
if(ref_list == NULL) {
xmlFree(ID);
return (-1);
}
target.l = ref_list;
target.ap = attr;
xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
if (xmlListEmpty(ref_list))
xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
xmlFreeRefList);
xmlFree(ID);
return(0);
}
xmlListPtr
xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
xmlRefTablePtr table;
if (doc == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n");
return(NULL);
}
if (ID == NULL) {
xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n");
return(NULL);
}
table = (xmlRefTablePtr) doc->refs;
if (table == NULL)
return(NULL);
return (xmlHashLookup(table, ID));
}
xmlElementPtr
xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
xmlElementTablePtr table;
xmlElementPtr cur;
xmlChar *uqname = NULL, *prefix = NULL;
if (dtd == NULL) return(NULL);
if (dtd->elements == NULL)
return(NULL);
table = (xmlElementTablePtr) dtd->elements;
uqname = xmlSplitQName2(name, &prefix);
if (uqname != NULL)
name = uqname;
cur = xmlHashLookup2(table, name, prefix);
if (prefix != NULL) xmlFree(prefix);
if (uqname != NULL) xmlFree(uqname);
return(cur);
}
static xmlElementPtr
xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
xmlElementTablePtr table;
xmlElementPtr cur;
xmlChar *uqname = NULL, *prefix = NULL;
if (dtd == NULL) return(NULL);
if (dtd->elements == NULL) {
if (!create)
return(NULL);
table = (xmlElementTablePtr) dtd->elements;
if (table == NULL) {
table = xmlCreateElementTable();
dtd->elements = (void *) table;
}
if (table == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlGetDtdElementDesc2: Table creation failed!\n");
return(NULL);
}
}
table = (xmlElementTablePtr) dtd->elements;
uqname = xmlSplitQName2(name, &prefix);
if (uqname != NULL)
name = uqname;
cur = xmlHashLookup2(table, name, prefix);
if ((cur == NULL) && (create)) {
cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
if (cur == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlGetDtdElementDesc2: out of memory\n");
return(NULL);
}
memset(cur, 0, sizeof(xmlElement));
cur->type = XML_ELEMENT_DECL;
cur->name = xmlStrdup(name);
cur->prefix = xmlStrdup(prefix);
cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
xmlHashAddEntry2(table, name, prefix, cur);
}
if (prefix != NULL) xmlFree(prefix);
if (uqname != NULL) xmlFree(uqname);
return(cur);
}
xmlElementPtr
xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
const xmlChar *prefix) {
xmlElementTablePtr table;
if (dtd == NULL) return(NULL);
if (dtd->elements == NULL) return(NULL);
table = (xmlElementTablePtr) dtd->elements;
return(xmlHashLookup2(table, name, prefix));
}
xmlAttributePtr
xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
xmlAttributeTablePtr table;
xmlAttributePtr cur;
xmlChar *uqname = NULL, *prefix = NULL;
if (dtd == NULL) return(NULL);
if (dtd->attributes == NULL) return(NULL);
table = (xmlAttributeTablePtr) dtd->attributes;
if (table == NULL)
return(NULL);
uqname = xmlSplitQName2(name, &prefix);
if (uqname != NULL) {
cur = xmlHashLookup3(table, uqname, prefix, elem);
if (prefix != NULL) xmlFree(prefix);
if (uqname != NULL) xmlFree(uqname);
} else
cur = xmlHashLookup3(table, name, NULL, elem);
return(cur);
}
xmlAttributePtr
xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
const xmlChar *prefix) {
xmlAttributeTablePtr table;
if (dtd == NULL) return(NULL);
if (dtd->attributes == NULL) return(NULL);
table = (xmlAttributeTablePtr) dtd->attributes;
return(xmlHashLookup3(table, name, prefix, elem));
}
xmlNotationPtr
xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
xmlNotationTablePtr table;
if (dtd == NULL) return(NULL);
if (dtd->notations == NULL) return(NULL);
table = (xmlNotationTablePtr) dtd->notations;
return(xmlHashLookup(table, name));
}
int
xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
const xmlChar *notationName) {
xmlNotationPtr notaDecl;
if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
if ((notaDecl == NULL) && (doc->extSubset != NULL))
notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
if (notaDecl == NULL) {
VERROR(ctxt->userData, "NOTATION %s is not declared\n",
notationName);
return(0);
}
return(1);
}
int
xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
xmlElementPtr elemDecl;
if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
if ((elemDecl == NULL) && (doc->extSubset != NULL))
elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
if (elemDecl == NULL) return(-1);
switch (elemDecl->etype) {
case XML_ELEMENT_TYPE_UNDEFINED:
return(-1);
case XML_ELEMENT_TYPE_ELEMENT:
return(0);
case XML_ELEMENT_TYPE_EMPTY:
case XML_ELEMENT_TYPE_ANY:
case XML_ELEMENT_TYPE_MIXED:
return(1);
}
return(1);
}
int
xmlValidateNameValue(const xmlChar *value) {
const xmlChar *cur;
int val, len;
if (value == NULL) return(0);
cur = value;
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
if (!IS_LETTER(val) && (val != '_') &&
(val != ':')) {
return(0);
}
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
(val == '.') || (val == '-') ||
(val == '_') || (val == ':') ||
(IS_COMBINING(val)) ||
(IS_EXTENDER(val))) {
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
}
if (val != 0) return(0);
return(1);
}
int
xmlValidateNamesValue(const xmlChar *value) {
const xmlChar *cur;
int val, len;
if (value == NULL) return(0);
cur = value;
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
if (!IS_LETTER(val) && (val != '_') &&
(val != ':')) {
return(0);
}
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
(val == '.') || (val == '-') ||
(val == '_') || (val == ':') ||
(IS_COMBINING(val)) ||
(IS_EXTENDER(val))) {
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
}
while (IS_BLANK(val)) {
while (IS_BLANK(val)) {
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
}
if (!IS_LETTER(val) && (val != '_') &&
(val != ':')) {
return(0);
}
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
(val == '.') || (val == '-') ||
(val == '_') || (val == ':') ||
(IS_COMBINING(val)) ||
(IS_EXTENDER(val))) {
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
}
}
if (val != 0) return(0);
return(1);
}
int
xmlValidateNmtokenValue(const xmlChar *value) {
const xmlChar *cur;
int val, len;
if (value == NULL) return(0);
cur = value;
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
if (!IS_LETTER(val) && !IS_DIGIT(val) &&
(val != '.') && (val != '-') &&
(val != '_') && (val != ':') &&
(!IS_COMBINING(val)) &&
(!IS_EXTENDER(val)))
return(0);
while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
(val == '.') || (val == '-') ||
(val == '_') || (val == ':') ||
(IS_COMBINING(val)) ||
(IS_EXTENDER(val))) {
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
}
if (val != 0) return(0);
return(1);
}
int
xmlValidateNmtokensValue(const xmlChar *value) {
const xmlChar *cur;
int val, len;
if (value == NULL) return(0);
cur = value;
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
while (IS_BLANK(val)) {
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
}
if (!IS_LETTER(val) && !IS_DIGIT(val) &&
(val != '.') && (val != '-') &&
(val != '_') && (val != ':') &&
(!IS_COMBINING(val)) &&
(!IS_EXTENDER(val)))
return(0);
while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
(val == '.') || (val == '-') ||
(val == '_') || (val == ':') ||
(IS_COMBINING(val)) ||
(IS_EXTENDER(val))) {
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
}
while (IS_BLANK(val)) {
while (IS_BLANK(val)) {
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
}
if (val == 0) return(1);
if (!IS_LETTER(val) && !IS_DIGIT(val) &&
(val != '.') && (val != '-') &&
(val != '_') && (val != ':') &&
(!IS_COMBINING(val)) &&
(!IS_EXTENDER(val)))
return(0);
while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
(val == '.') || (val == '-') ||
(val == '_') || (val == ':') ||
(IS_COMBINING(val)) ||
(IS_EXTENDER(val))) {
val = xmlStringCurrentChar(NULL, cur, &len);
cur += len;
}
}
if (val != 0) return(0);
return(1);
}
int
xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
xmlNotationPtr nota ATTRIBUTE_UNUSED) {
int ret = 1;
return(ret);
}
int
xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
switch (type) {
case XML_ATTRIBUTE_ENTITIES:
case XML_ATTRIBUTE_IDREFS:
return(xmlValidateNamesValue(value));
case XML_ATTRIBUTE_ENTITY:
case XML_ATTRIBUTE_IDREF:
case XML_ATTRIBUTE_ID:
case XML_ATTRIBUTE_NOTATION:
return(xmlValidateNameValue(value));
case XML_ATTRIBUTE_NMTOKENS:
case XML_ATTRIBUTE_ENUMERATION:
return(xmlValidateNmtokensValue(value));
case XML_ATTRIBUTE_NMTOKEN:
return(xmlValidateNmtokenValue(value));
case XML_ATTRIBUTE_CDATA:
break;
}
return(1);
}
static int
xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
int ret = 1;
switch (type) {
case XML_ATTRIBUTE_IDREFS:
case XML_ATTRIBUTE_IDREF:
case XML_ATTRIBUTE_ID:
case XML_ATTRIBUTE_NMTOKENS:
case XML_ATTRIBUTE_ENUMERATION:
case XML_ATTRIBUTE_NMTOKEN:
case XML_ATTRIBUTE_CDATA:
break;
case XML_ATTRIBUTE_ENTITY: {
xmlEntityPtr ent;
ent = xmlGetDocEntity(doc, value);
if ((ent == NULL) && (doc->standalone == 1)) {
doc->standalone = 0;
ent = xmlGetDocEntity(doc, value);
if (ent != NULL) {
VERROR(ctxt->userData,
"standalone problem: attribute %s reference entity \"%s\" in external subset\n",
name, value);
}
}
if (ent == NULL) {
VERROR(ctxt->userData,
"ENTITY attribute %s reference an unknown entity \"%s\"\n",
name, value);
ret = 0;
} else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
VERROR(ctxt->userData,
"ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
name, value);
ret = 0;
}
break;
}
case XML_ATTRIBUTE_ENTITIES: {
xmlChar *dup, *nam = NULL, *cur, save;
xmlEntityPtr ent;
dup = xmlStrdup(value);
if (dup == NULL)
return(0);
cur = dup;
while (*cur != 0) {
nam = cur;
while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
save = *cur;
*cur = 0;
ent = xmlGetDocEntity(doc, nam);
if (ent == NULL) {
VERROR(ctxt->userData,
"ENTITIES attribute %s reference an unknown entity \"%s\"\n",
name, nam);
ret = 0;
} else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
VERROR(ctxt->userData,
"ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
name, nam);
ret = 0;
}
if (save == 0)
break;
*cur = save;
while (IS_BLANK(*cur)) cur++;
}
xmlFree(dup);
break;
}
case XML_ATTRIBUTE_NOTATION: {
xmlNotationPtr nota;
nota = xmlGetDtdNotationDesc(doc->intSubset, value);
if ((nota == NULL) && (doc->extSubset != NULL))
nota = xmlGetDtdNotationDesc(doc->extSubset, value);
if (nota == NULL) {
VERROR(ctxt->userData,
"NOTATION attribute %s reference an unknown notation \"%s\"\n",
name, value);
ret = 0;
}
break;
}
}
return(ret);
}
xmlChar *
xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
xmlChar *ret, *dst;
const xmlChar *src;
xmlAttributePtr attrDecl = NULL;
int extsubset = 0;
if (doc == NULL) return(NULL);
if (elem == NULL) return(NULL);
if (name == NULL) return(NULL);
if (value == NULL) return(NULL);
if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
xmlChar qname[500];
snprintf((char *) qname, sizeof(qname), "%s:%s",
elem->ns->prefix, elem->name);
qname[sizeof(qname) - 1] = 0;
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
if (attrDecl != NULL)
extsubset = 1;
}
}
if ((attrDecl == NULL) && (doc->intSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
if (attrDecl != NULL)
extsubset = 1;
}
if (attrDecl == NULL)
return(NULL);
if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
return(NULL);
ret = xmlStrdup(value);
if (ret == NULL)
return(NULL);
src = value;
dst = ret;
while (*src == 0x20) src++;
while (*src != 0) {
if (*src == 0x20) {
while (*src == 0x20) src++;
if (*src != 0)
*dst++ = 0x20;
} else {
*dst++ = *src++;
}
}
*dst = 0;
if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
VERROR(ctxt->userData,
"standalone: %s on %s value had to be normalized based on external subset declaration\n",
name, elem->name);
ctxt->valid = 0;
}
return(ret);
}
xmlChar *
xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
const xmlChar *name, const xmlChar *value) {
xmlChar *ret, *dst;
const xmlChar *src;
xmlAttributePtr attrDecl = NULL;
if (doc == NULL) return(NULL);
if (elem == NULL) return(NULL);
if (name == NULL) return(NULL);
if (value == NULL) return(NULL);
if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
xmlChar qname[500];
snprintf((char *) qname, sizeof(qname), "%s:%s",
elem->ns->prefix, elem->name);
qname[sizeof(qname) - 1] = 0;
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, name);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname, name);
}
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
if (attrDecl == NULL)
return(NULL);
if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
return(NULL);
ret = xmlStrdup(value);
if (ret == NULL)
return(NULL);
src = value;
dst = ret;
while (*src == 0x20) src++;
while (*src != 0) {
if (*src == 0x20) {
while (*src == 0x20) src++;
if (*src != 0)
*dst++ = 0x20;
} else {
*dst++ = *src++;
}
}
*dst = 0;
return(ret);
}
static void
xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
const xmlChar* name ATTRIBUTE_UNUSED) {
if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
}
int
xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
xmlAttributePtr attr) {
int ret = 1;
int val;
CHECK_DTD;
if(attr == NULL) return(1);
if (attr->defaultValue != NULL) {
val = xmlValidateAttributeValue(attr->atype, attr->defaultValue);
if (val == 0) {
VERROR(ctxt->userData,
"Syntax of default value for attribute %s of %s is not valid\n",
attr->name, attr->elem);
}
ret &= val;
}
if ((attr->atype == XML_ATTRIBUTE_ID)&&
(attr->def != XML_ATTRIBUTE_IMPLIED) &&
(attr->def != XML_ATTRIBUTE_REQUIRED)) {
VERROR(ctxt->userData,
"ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
attr->name, attr->elem);
ret = 0;
}
if (attr->atype == XML_ATTRIBUTE_ID) {
int nbId;
xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
attr->elem);
if (elem != NULL) {
nbId = xmlScanIDAttributeDecl(NULL, elem);
} else {
xmlAttributeTablePtr table;
nbId = 0;
table = (xmlAttributeTablePtr) doc->intSubset->attributes;
xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
xmlValidateAttributeIdCallback, &nbId);
}
if (nbId > 1) {
VERROR(ctxt->userData,
"Element %s has %d ID attribute defined in the internal subset : %s\n",
attr->elem, nbId, attr->name);
} else if (doc->extSubset != NULL) {
int extId = 0;
elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
if (elem != NULL) {
extId = xmlScanIDAttributeDecl(NULL, elem);
}
if (extId > 1) {
VERROR(ctxt->userData,
"Element %s has %d ID attribute defined in the external subset : %s\n",
attr->elem, extId, attr->name);
} else if (extId + nbId > 1) {
VERROR(ctxt->userData,
"Element %s has ID attributes defined in the internal and external subset : %s\n",
attr->elem, attr->name);
}
}
}
if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
xmlEnumerationPtr tree = attr->tree;
while (tree != NULL) {
if (xmlStrEqual(tree->name, attr->defaultValue)) break;
tree = tree->next;
}
if (tree == NULL) {
VERROR(ctxt->userData,
"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
attr->defaultValue, attr->name, attr->elem);
ret = 0;
}
}
return(ret);
}
int
xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
xmlElementPtr elem) {
int ret = 1;
xmlElementPtr tst;
CHECK_DTD;
if (elem == NULL) return(1);
#if 0
#ifdef LIBXML_REGEXP_ENABLED
ret = xmlValidBuildContentModel(ctxt, elem);
#endif
#endif
if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
xmlElementContentPtr cur, next;
const xmlChar *name;
cur = elem->content;
while (cur != NULL) {
if (cur->type != XML_ELEMENT_CONTENT_OR) break;
if (cur->c1 == NULL) break;
if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
name = cur->c1->name;
next = cur->c2;
while (next != NULL) {
if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
if (xmlStrEqual(next->name, name)) {
VERROR(ctxt->userData,
"Definition of %s has duplicate references of %s\n",
elem->name, name);
ret = 0;
}
break;
}
if (next->c1 == NULL) break;
if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
if (xmlStrEqual(next->c1->name, name)) {
VERROR(ctxt->userData,
"Definition of %s has duplicate references of %s\n",
elem->name, name);
ret = 0;
}
next = next->c2;
}
}
cur = cur->c2;
}
}
tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
if ((tst != NULL ) && (tst != elem) &&
((tst->prefix == elem->prefix) ||
(xmlStrEqual(tst->prefix, elem->prefix))) &&
(tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
VERROR(ctxt->userData, "Redefinition of element %s\n",
elem->name);
ret = 0;
}
tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
if ((tst != NULL ) && (tst != elem) &&
((tst->prefix == elem->prefix) ||
(xmlStrEqual(tst->prefix, elem->prefix))) &&
(tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
VERROR(ctxt->userData, "Redefinition of element %s\n",
elem->name);
ret = 0;
}
return(ret);
}
int
xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) {
xmlAttributePtr attrDecl = NULL;
int val;
int ret = 1;
CHECK_DTD;
if ((elem == NULL) || (elem->name == NULL)) return(0);
if ((attr == NULL) || (attr->name == NULL)) return(0);
if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
xmlChar qname[500];
snprintf((char *) qname, sizeof(qname), "%s:%s",
elem->ns->prefix, elem->name);
qname[sizeof(qname) - 1] = 0;
if (attr->ns != NULL) {
attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
attr->name, attr->ns->prefix);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
attr->name, attr->ns->prefix);
} else {
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname, attr->name);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
qname, attr->name);
}
}
if (attrDecl == NULL) {
if (attr->ns != NULL) {
attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
attr->name, attr->ns->prefix);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
attr->name, attr->ns->prefix);
} else {
attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
elem->name, attr->name);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
elem->name, attr->name);
}
}
if (attrDecl == NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"No declaration for attribute %s of element %s\n",
attr->name, elem->name);
return(0);
}
attr->atype = attrDecl->atype;
val = xmlValidateAttributeValue(attrDecl->atype, value);
if (val == 0) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Syntax of value for attribute %s of %s is not valid\n",
attr->name, elem->name);
ret = 0;
}
if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
if (!xmlStrEqual(value, attrDecl->defaultValue)) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Value for attribute %s of %s is different from default \"%s\"\n",
attr->name, elem->name, attrDecl->defaultValue);
ret = 0;
}
}
if (attrDecl->atype == XML_ATTRIBUTE_ID) {
if (xmlAddID(ctxt, doc, value, attr) == NULL)
ret = 0;
}
if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
(attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
if (xmlAddRef(ctxt, doc, value, attr) == NULL)
ret = 0;
}
if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
xmlEnumerationPtr tree = attrDecl->tree;
xmlNotationPtr nota;
nota = xmlGetDtdNotationDesc(doc->intSubset, value);
if (nota == NULL)
nota = xmlGetDtdNotationDesc(doc->extSubset, value);
if (nota == NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Value \"%s\" for attribute %s of %s is not a declared Notation\n",
value, attr->name, elem->name);
ret = 0;
}
while (tree != NULL) {
if (xmlStrEqual(tree->name, value)) break;
tree = tree->next;
}
if (tree == NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
value, attr->name, elem->name);
ret = 0;
}
}
if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
xmlEnumerationPtr tree = attrDecl->tree;
while (tree != NULL) {
if (xmlStrEqual(tree->name, value)) break;
tree = tree->next;
}
if (tree == NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
value, attr->name, elem->name);
ret = 0;
}
}
if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
(!xmlStrEqual(attrDecl->defaultValue, value))) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Value for attribute %s of %s must be \"%s\"\n",
attr->name, elem->name, attrDecl->defaultValue);
ret = 0;
}
ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
attrDecl->atype, value);
return(ret);
}
int
xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
xmlAttributePtr attrDecl = NULL;
int val;
int ret = 1;
CHECK_DTD;
if ((elem == NULL) || (elem->name == NULL)) return(0);
if ((ns == NULL) || (ns->href == NULL)) return(0);
if (prefix != NULL) {
xmlChar qname[500];
snprintf((char *) qname, sizeof(qname), "%s:%s",
prefix, elem->name);
qname[sizeof(qname) - 1] = 0;
if (ns->prefix != NULL) {
attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, qname,
ns->prefix, BAD_CAST "xmlns");
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, qname,
ns->prefix, BAD_CAST "xmlns");
} else {
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, qname,
BAD_CAST "xmlns");
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset, qname,
BAD_CAST "xmlns");
}
}
if (attrDecl == NULL) {
if (ns->prefix != NULL) {
attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
ns->prefix, BAD_CAST "xmlns");
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
ns->prefix, BAD_CAST "xmlns");
} else {
attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
elem->name, BAD_CAST "xmlns");
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
elem->name, BAD_CAST "xmlns");
}
}
if (attrDecl == NULL) {
VECTXT(ctxt, elem);
if (ns->prefix != NULL) {
VERROR(ctxt->userData,
"No declaration for attribute xmlns:%s of element %s\n",
ns->prefix, elem->name);
} else {
VERROR(ctxt->userData,
"No declaration for attribute xmlns of element %s\n",
elem->name);
}
return(0);
}
val = xmlValidateAttributeValue(attrDecl->atype, value);
if (val == 0) {
VECTXT(ctxt, elem);
if (ns->prefix != NULL) {
VERROR(ctxt->userData,
"Syntax of value for attribute xmlns:%s of %s is not valid\n",
ns->prefix, elem->name);
} else {
VERROR(ctxt->userData,
"Syntax of value for attribute xmlns of %s is not valid\n",
elem->name);
}
ret = 0;
}
if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
if (!xmlStrEqual(value, attrDecl->defaultValue)) {
VECTXT(ctxt, elem);
if (ns->prefix != NULL) {
VERROR(ctxt->userData,
"Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
ns->prefix, elem->name, attrDecl->defaultValue);
} else {
VERROR(ctxt->userData,
"Value for attribute xmlns of %s is different from default \"%s\"\n",
elem->name, attrDecl->defaultValue);
}
ret = 0;
}
}
if (attrDecl->atype == XML_ATTRIBUTE_ID) {
if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
ret = 0;
}
if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
(attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
ret = 0;
}
if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
xmlEnumerationPtr tree = attrDecl->tree;
xmlNotationPtr nota;
nota = xmlGetDtdNotationDesc(doc->intSubset, value);
if (nota == NULL)
nota = xmlGetDtdNotationDesc(doc->extSubset, value);
if (nota == NULL) {
VECTXT(ctxt, elem);
if (ns->prefix != NULL) {
VERROR(ctxt->userData,
"Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
value, ns->prefix, elem->name);
} else {
VERROR(ctxt->userData,
"Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
value, elem->name);
}
ret = 0;
}
while (tree != NULL) {
if (xmlStrEqual(tree->name, value)) break;
tree = tree->next;
}
if (tree == NULL) {
VECTXT(ctxt, elem);
if (ns->prefix != NULL) {
VERROR(ctxt->userData,
"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
value, ns->prefix, elem->name);
} else {
VERROR(ctxt->userData,
"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
value, elem->name);
}
ret = 0;
}
}
if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
xmlEnumerationPtr tree = attrDecl->tree;
while (tree != NULL) {
if (xmlStrEqual(tree->name, value)) break;
tree = tree->next;
}
if (tree == NULL) {
VECTXT(ctxt, elem);
if (ns->prefix != NULL) {
VERROR(ctxt->userData,
"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
value, ns->prefix, elem->name);
} else {
VERROR(ctxt->userData,
"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
value, elem->name);
}
ret = 0;
}
}
if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
(!xmlStrEqual(attrDecl->defaultValue, value))) {
VECTXT(ctxt, elem);
if (ns->prefix != NULL) {
VERROR(ctxt->userData,
"Value for attribute xmlns:%s of %s must be \"%s\"\n",
ns->prefix, elem->name, attrDecl->defaultValue);
} else {
VERROR(ctxt->userData,
"Value for attribute xmlns of %s must be \"%s\"\n",
elem->name, attrDecl->defaultValue);
}
ret = 0;
}
if (ns->prefix != NULL) {
ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
attrDecl->atype, value);
} else {
ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
attrDecl->atype, value);
}
return(ret);
}
#ifndef LIBXML_REGEXP_ENABLED
static xmlNodePtr
xmlValidateSkipIgnorable(xmlNodePtr child) {
while (child != NULL) {
switch (child->type) {
case XML_PI_NODE:
case XML_COMMENT_NODE:
case XML_XINCLUDE_START:
case XML_XINCLUDE_END:
child = child->next;
break;
case XML_TEXT_NODE:
if (xmlIsBlankNode(child))
child = child->next;
else
return(child);
break;
default:
return(child);
}
}
return(child);
}
static int
xmlValidateElementType(xmlValidCtxtPtr ctxt) {
int ret = -1;
int determinist = 1;
NODE = xmlValidateSkipIgnorable(NODE);
if ((NODE == NULL) && (CONT == NULL))
return(1);
if ((NODE == NULL) &&
((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
(CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
return(1);
}
if (CONT == NULL) return(-1);
if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
return(-2);
cont:
if (STATE == ROLLBACK_PARENT) {
DEBUG_VALID_MSG("restored parent branch");
DEBUG_VALID_STATE(NODE, CONT)
ret = 1;
goto analyze;
}
DEBUG_VALID_STATE(NODE, CONT)
if ((CONT != NULL) &&
((CONT->parent == NULL) ||
(CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
(CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
DEBUG_VALID_MSG("saving parent branch");
if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
return(0);
}
switch (CONT->type) {
case XML_ELEMENT_CONTENT_PCDATA:
if (NODE == NULL) {
DEBUG_VALID_MSG("pcdata failed no node");
ret = 0;
break;
}
if (NODE->type == XML_TEXT_NODE) {
DEBUG_VALID_MSG("pcdata found, skip to next");
do {
NODE = NODE->next;
NODE = xmlValidateSkipIgnorable(NODE);
if ((NODE != NULL) &&
(NODE->type == XML_ENTITY_REF_NODE))
return(-2);
} while ((NODE != NULL) &&
((NODE->type != XML_ELEMENT_NODE) &&
(NODE->type != XML_TEXT_NODE) &&
(NODE->type != XML_CDATA_SECTION_NODE)));
ret = 1;
break;
} else {
DEBUG_VALID_MSG("pcdata failed");
ret = 0;
break;
}
break;
case XML_ELEMENT_CONTENT_ELEMENT:
if (NODE == NULL) {
DEBUG_VALID_MSG("element failed no node");
ret = 0;
break;
}
ret = ((NODE->type == XML_ELEMENT_NODE) &&
(xmlStrEqual(NODE->name, CONT->name)));
if (ret == 1) {
if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
ret = (CONT->prefix == NULL);
} else if (CONT->prefix == NULL) {
ret = 0;
} else {
ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
}
}
if (ret == 1) {
DEBUG_VALID_MSG("element found, skip to next");
do {
NODE = NODE->next;
NODE = xmlValidateSkipIgnorable(NODE);
if ((NODE != NULL) &&
(NODE->type == XML_ENTITY_REF_NODE))
return(-2);
} while ((NODE != NULL) &&
((NODE->type != XML_ELEMENT_NODE) &&
(NODE->type != XML_TEXT_NODE) &&
(NODE->type != XML_CDATA_SECTION_NODE)));
} else {
DEBUG_VALID_MSG("element failed");
ret = 0;
break;
}
break;
case XML_ELEMENT_CONTENT_OR:
if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
if ((NODE == NULL) ||
(!xmlStrEqual(NODE->name, CONT->c1->name))) {
DEPTH++;
CONT = CONT->c2;
goto cont;
}
if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
ret = (CONT->c1->prefix == NULL);
} else if (CONT->c1->prefix == NULL) {
ret = 0;
} else {
ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
}
if (ret == 0) {
DEPTH++;
CONT = CONT->c2;
goto cont;
}
}
DEBUG_VALID_MSG("saving 'or' branch");
if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
OCCURS, ROLLBACK_OR) < 0)
return(-1);
DEPTH++;
CONT = CONT->c1;
goto cont;
case XML_ELEMENT_CONTENT_SEQ:
if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
(CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
if ((NODE == NULL) ||
(!xmlStrEqual(NODE->name, CONT->c1->name))) {
DEPTH++;
CONT = CONT->c2;
goto cont;
}
if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
ret = (CONT->c1->prefix == NULL);
} else if (CONT->c1->prefix == NULL) {
ret = 0;
} else {
ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
}
if (ret == 0) {
DEPTH++;
CONT = CONT->c2;
goto cont;
}
}
DEPTH++;
CONT = CONT->c1;
goto cont;
}
if (ret == -1) {
DEBUG_VALID_MSG("error found returning");
return(ret);
}
analyze:
while (CONT != NULL) {
if (ret == 0) {
switch (CONT->ocur) {
xmlNodePtr cur;
case XML_ELEMENT_CONTENT_ONCE:
cur = ctxt->vstate->node;
DEBUG_VALID_MSG("Once branch failed, rollback");
if (vstateVPop(ctxt) < 0 ) {
DEBUG_VALID_MSG("exhaustion, failed");
return(0);
}
if (cur != ctxt->vstate->node)
determinist = -3;
goto cont;
case XML_ELEMENT_CONTENT_PLUS:
if (OCCURRENCE == 0) {
cur = ctxt->vstate->node;
DEBUG_VALID_MSG("Plus branch failed, rollback");
if (vstateVPop(ctxt) < 0 ) {
DEBUG_VALID_MSG("exhaustion, failed");
return(0);
}
if (cur != ctxt->vstate->node)
determinist = -3;
goto cont;
}
DEBUG_VALID_MSG("Plus branch found");
ret = 1;
break;
case XML_ELEMENT_CONTENT_MULT:
#ifdef DEBUG_VALID_ALGO
if (OCCURRENCE == 0) {
DEBUG_VALID_MSG("Mult branch failed");
} else {
DEBUG_VALID_MSG("Mult branch found");
}
#endif
ret = 1;
break;
case XML_ELEMENT_CONTENT_OPT:
DEBUG_VALID_MSG("Option branch failed");
ret = 1;
break;
}
} else {
switch (CONT->ocur) {
case XML_ELEMENT_CONTENT_OPT:
DEBUG_VALID_MSG("Option branch succeeded");
ret = 1;
break;
case XML_ELEMENT_CONTENT_ONCE:
DEBUG_VALID_MSG("Once branch succeeded");
ret = 1;
break;
case XML_ELEMENT_CONTENT_PLUS:
if (STATE == ROLLBACK_PARENT) {
DEBUG_VALID_MSG("Plus branch rollback");
ret = 1;
break;
}
if (NODE == NULL) {
DEBUG_VALID_MSG("Plus branch exhausted");
ret = 1;
break;
}
DEBUG_VALID_MSG("Plus branch succeeded, continuing");
SET_OCCURRENCE;
goto cont;
case XML_ELEMENT_CONTENT_MULT:
if (STATE == ROLLBACK_PARENT) {
DEBUG_VALID_MSG("Mult branch rollback");
ret = 1;
break;
}
if (NODE == NULL) {
DEBUG_VALID_MSG("Mult branch exhausted");
ret = 1;
break;
}
DEBUG_VALID_MSG("Mult branch succeeded, continuing");
goto cont;
}
}
STATE = 0;
RESET_OCCURRENCE;
if (CONT->parent == NULL)
break;
switch (CONT->parent->type) {
case XML_ELEMENT_CONTENT_PCDATA:
DEBUG_VALID_MSG("Error: parent pcdata");
return(-1);
case XML_ELEMENT_CONTENT_ELEMENT:
DEBUG_VALID_MSG("Error: parent element");
return(-1);
case XML_ELEMENT_CONTENT_OR:
if (ret == 1) {
DEBUG_VALID_MSG("Or succeeded");
CONT = CONT->parent;
DEPTH--;
} else {
DEBUG_VALID_MSG("Or failed");
CONT = CONT->parent;
DEPTH--;
}
break;
case XML_ELEMENT_CONTENT_SEQ:
if (ret == 0) {
DEBUG_VALID_MSG("Sequence failed");
CONT = CONT->parent;
DEPTH--;
} else if (CONT == CONT->parent->c1) {
DEBUG_VALID_MSG("Sequence testing 2nd branch");
CONT = CONT->parent->c2;
goto cont;
} else {
DEBUG_VALID_MSG("Sequence succeeded");
CONT = CONT->parent;
DEPTH--;
}
}
}
if (NODE != NULL) {
xmlNodePtr cur;
cur = ctxt->vstate->node;
DEBUG_VALID_MSG("Failed, remaining input, rollback");
if (vstateVPop(ctxt) < 0 ) {
DEBUG_VALID_MSG("exhaustion, failed");
return(0);
}
if (cur != ctxt->vstate->node)
determinist = -3;
goto cont;
}
if (ret == 0) {
xmlNodePtr cur;
cur = ctxt->vstate->node;
DEBUG_VALID_MSG("Failure, rollback");
if (vstateVPop(ctxt) < 0 ) {
DEBUG_VALID_MSG("exhaustion, failed");
return(0);
}
if (cur != ctxt->vstate->node)
determinist = -3;
goto cont;
}
return(determinist);
}
#endif
static void
xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
xmlNodePtr cur;
int len;
if (node == NULL) return;
if (glob) strcat(buf, "(");
cur = node;
while (cur != NULL) {
len = strlen(buf);
if (size - len < 50) {
if ((size - len > 4) && (buf[len - 1] != '.'))
strcat(buf, " ...");
return;
}
switch (cur->type) {
case XML_ELEMENT_NODE:
if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
if ((size - len > 4) && (buf[len - 1] != '.'))
strcat(buf, " ...");
return;
}
strcat(buf, (char *) cur->ns->prefix);
strcat(buf, ":");
}
if (size - len < xmlStrlen(cur->name) + 10) {
if ((size - len > 4) && (buf[len - 1] != '.'))
strcat(buf, " ...");
return;
}
strcat(buf, (char *) cur->name);
if (cur->next != NULL)
strcat(buf, " ");
break;
case XML_TEXT_NODE:
if (xmlIsBlankNode(cur))
break;
case XML_CDATA_SECTION_NODE:
case XML_ENTITY_REF_NODE:
strcat(buf, "CDATA");
if (cur->next != NULL)
strcat(buf, " ");
break;
case XML_ATTRIBUTE_NODE:
case XML_DOCUMENT_NODE:
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
#endif
case XML_HTML_DOCUMENT_NODE:
case XML_DOCUMENT_TYPE_NODE:
case XML_DOCUMENT_FRAG_NODE:
case XML_NOTATION_NODE:
case XML_NAMESPACE_DECL:
strcat(buf, "???");
if (cur->next != NULL)
strcat(buf, " ");
break;
case XML_ENTITY_NODE:
case XML_PI_NODE:
case XML_DTD_NODE:
case XML_COMMENT_NODE:
case XML_ELEMENT_DECL:
case XML_ATTRIBUTE_DECL:
case XML_ENTITY_DECL:
case XML_XINCLUDE_START:
case XML_XINCLUDE_END:
break;
}
cur = cur->next;
}
if (glob) strcat(buf, ")");
}
static int
xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
int ret = 1;
#ifndef LIBXML_REGEXP_ENABLED
xmlNodePtr repl = NULL, last = NULL, tmp;
#endif
xmlNodePtr cur;
xmlElementContentPtr cont;
const xmlChar *name;
if (elemDecl == NULL)
return(-1);
cont = elemDecl->content;
name = elemDecl->name;
#ifdef LIBXML_REGEXP_ENABLED
if (elemDecl->contModel == NULL)
ret = xmlValidBuildContentModel(ctxt, elemDecl);
if (elemDecl->contModel == NULL) {
ret = -1;
} else {
xmlRegExecCtxtPtr exec;
if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
return(-1);
}
ctxt->nodeMax = 0;
ctxt->nodeNr = 0;
ctxt->nodeTab = NULL;
exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
if (exec != NULL) {
cur = child;
while (cur != NULL) {
switch (cur->type) {
case XML_ENTITY_REF_NODE:
if ((cur->children != NULL) &&
(cur->children->children != NULL)) {
nodeVPush(ctxt, cur);
cur = cur->children->children;
continue;
}
break;
case XML_TEXT_NODE:
if (xmlIsBlankNode(cur))
break;
ret = 0;
goto fail;
case XML_CDATA_SECTION_NODE:
ret = 0;
goto fail;
case XML_ELEMENT_NODE:
if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
xmlChar *QName;
int len;
len = xmlStrlen(cur->name) +
xmlStrlen(cur->ns->prefix) + 2;
QName = xmlMalloc(len);
if (QName == NULL) {
ret = -1;
goto fail;
}
snprintf((char *) QName, len, "%s:%s",
(char *)cur->ns->prefix,
(char *)cur->name);
ret = xmlRegExecPushString(exec, QName, NULL);
xmlFree(QName);
} else {
ret = xmlRegExecPushString(exec, cur->name, NULL);
}
break;
default:
break;
}
cur = cur->next;
while (cur == NULL) {
cur = nodeVPop(ctxt);
if (cur == NULL)
break;
cur = cur->next;
}
}
ret = xmlRegExecPushString(exec, NULL, NULL);
fail:
xmlRegFreeExecCtxt(exec);
}
}
#else
ctxt->vstateMax = 8;
ctxt->vstateTab = (xmlValidState *) xmlMalloc(
ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
if (ctxt->vstateTab == NULL) {
xmlGenericError(xmlGenericErrorContext,
"malloc failed !n");
return(-1);
}
ctxt->nodeMax = 0;
ctxt->nodeNr = 0;
ctxt->nodeTab = NULL;
ctxt->vstate = &ctxt->vstateTab[0];
ctxt->vstateNr = 1;
CONT = cont;
NODE = child;
DEPTH = 0;
OCCURS = 0;
STATE = 0;
ret = xmlValidateElementType(ctxt);
if ((ret == -3) && (warn)) {
VWARNING(ctxt->userData,
"Content model for Element %s is ambiguous\n", name);
} else if (ret == -2) {
DEBUG_VALID_MSG("Found an entity reference, linearizing");
cur = child;
while (cur != NULL) {
switch (cur->type) {
case XML_ENTITY_REF_NODE:
if ((cur->children != NULL) &&
(cur->children->children != NULL)) {
nodeVPush(ctxt, cur);
cur = cur->children->children;
continue;
}
break;
case XML_TEXT_NODE:
if (xmlIsBlankNode(cur))
break;
case XML_CDATA_SECTION_NODE:
case XML_ELEMENT_NODE:
tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (tmp == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlValidateElementContent : malloc failed\n");
xmlFreeNodeList(repl);
ret = -1;
goto done;
}
tmp->type = cur->type;
tmp->name = cur->name;
tmp->ns = cur->ns;
tmp->next = NULL;
tmp->content = NULL;
if (repl == NULL)
repl = last = tmp;
else {
last->next = tmp;
last = tmp;
}
if (cur->type == XML_CDATA_SECTION_NODE) {
tmp->content = xmlStrdup(BAD_CAST "CDATA");
}
break;
default:
break;
}
cur = cur->next;
while (cur == NULL) {
cur = nodeVPop(ctxt);
if (cur == NULL)
break;
cur = cur->next;
}
}
ctxt->vstate = &ctxt->vstateTab[0];
ctxt->vstateNr = 1;
CONT = cont;
NODE = repl;
DEPTH = 0;
OCCURS = 0;
STATE = 0;
ret = xmlValidateElementType(ctxt);
}
#endif
if ((warn) && ((ret != 1) && (ret != -3))) {
if ((ctxt != NULL) && (ctxt->warning != NULL)) {
char expr[5000];
char list[5000];
expr[0] = 0;
xmlSnprintfElementContent(expr, 5000, cont, 1);
list[0] = 0;
#ifndef LIBXML_REGEXP_ENABLED
if (repl != NULL)
xmlSnprintfElements(list, 5000, repl, 1);
else
#endif
xmlSnprintfElements(list, 5000, child, 1);
if (name != NULL) {
if (parent != NULL) VECTXT(ctxt, parent);
VERROR(ctxt->userData,
"Element %s content does not follow the DTD\nExpecting %s, got %s\n",
name, expr, list);
} else {
if (parent != NULL) VECTXT(ctxt, parent);
VERROR(ctxt->userData,
"Element content does not follow the DTD\nExpecting %s, got %s\n",
expr, list);
}
} else {
if (name != NULL) {
if (parent != NULL) VECTXT(ctxt, parent);
VERROR(ctxt->userData,
"Element %s content does not follow the DTD\n",
name);
} else {
if (parent != NULL) VECTXT(ctxt, parent);
VERROR(ctxt->userData,
"Element content does not follow the DTD\n");
}
}
ret = 0;
}
if (ret == -3)
ret = 1;
#ifndef LIBXML_REGEXP_ENABLED
done:
while (repl != NULL) {
tmp = repl->next;
xmlFree(repl);
repl = tmp;
}
ctxt->vstateMax = 0;
if (ctxt->vstateTab != NULL) {
xmlFree(ctxt->vstateTab);
ctxt->vstateTab = NULL;
}
#endif
ctxt->nodeMax = 0;
ctxt->nodeNr = 0;
if (ctxt->nodeTab != NULL) {
xmlFree(ctxt->nodeTab);
ctxt->nodeTab = NULL;
}
return(ret);
}
static int
xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
xmlNodePtr elem) {
int ret = 1;
xmlNodePtr cur, child;
if ((ctxt == NULL) || (doc == NULL) || (elem == NULL))
return(0);
child = elem->children;
cur = child;
while (cur != NULL) {
switch (cur->type) {
case XML_ENTITY_REF_NODE:
if ((cur->children != NULL) &&
(cur->children->children != NULL)) {
nodeVPush(ctxt, cur);
cur = cur->children->children;
continue;
}
break;
case XML_COMMENT_NODE:
case XML_PI_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
break;
default:
ret = 0;
goto done;
}
cur = cur->next;
while (cur == NULL) {
cur = nodeVPop(ctxt);
if (cur == NULL)
break;
cur = cur->next;
}
}
done:
ctxt->nodeMax = 0;
ctxt->nodeNr = 0;
if (ctxt->nodeTab != NULL) {
xmlFree(ctxt->nodeTab);
ctxt->nodeTab = NULL;
}
return(ret);
}
static int
xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
xmlElementContentPtr cont, const xmlChar *qname) {
while (cont != NULL) {
if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
if (xmlStrEqual(cont->name, qname))
return(1);
} else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
(cont->c1 != NULL) &&
(cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
if (xmlStrEqual(cont->c1->name, qname))
return(1);
} else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
(cont->c1 == NULL) ||
(cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
xmlGenericError(xmlGenericErrorContext,
"Internal: MIXED struct bad\n");
break;
}
cont = cont->c2;
}
return(0);
}
static xmlElementPtr
xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
xmlNodePtr elem, int *extsubset) {
xmlElementPtr elemDecl = NULL;
const xmlChar *prefix = NULL;
if ((elem == NULL) || (elem->name == NULL)) return(NULL);
if (extsubset != NULL)
*extsubset = 0;
if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
prefix = elem->ns->prefix;
if (prefix != NULL) {
elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
elem->name, prefix);
if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
elem->name, prefix);
if ((elemDecl != NULL) && (extsubset != NULL))
*extsubset = 1;
}
}
if (elemDecl == NULL) {
elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
if ((elemDecl != NULL) && (extsubset != NULL))
*extsubset = 1;
}
}
if (elemDecl == NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData, "No declaration for element %s\n",
elem->name);
}
return(elemDecl);
}
#ifdef LIBXML_REGEXP_ENABLED
int
xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
xmlNodePtr elem, const xmlChar *qname) {
int ret = 1;
xmlElementPtr eDecl;
int extsubset = 0;
if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
xmlValidStatePtr state = ctxt->vstate;
xmlElementPtr elemDecl;
if (state->elemDecl != NULL) {
elemDecl = state->elemDecl;
switch(elemDecl->etype) {
case XML_ELEMENT_TYPE_UNDEFINED:
ret = 0;
break;
case XML_ELEMENT_TYPE_EMPTY:
VECTXT(ctxt, state->node);
VERROR(ctxt->userData,
"Element %s was declared EMPTY this one has content\n",
state->node->name);
ret = 0;
break;
case XML_ELEMENT_TYPE_ANY:
break;
case XML_ELEMENT_TYPE_MIXED:
if ((elemDecl->content != NULL) &&
(elemDecl->content->type ==
XML_ELEMENT_CONTENT_PCDATA)) {
VECTXT(ctxt, state->node);
VERROR(ctxt->userData,
"Element %s was declared #PCDATA but contains non text nodes\n",
state->node->name);
ret = 0;
} else {
ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
qname);
if (ret != 1) {
VECTXT(ctxt, state->node);
VERROR(ctxt->userData,
"Element %s is not declared in %s list of possible children\n",
qname, state->node->name);
}
}
break;
case XML_ELEMENT_TYPE_ELEMENT:
if (state->exec != NULL) {
ret = xmlRegExecPushString(state->exec, qname, NULL);
if (ret < 0) {
VECTXT(ctxt, state->node);
VERROR(ctxt->userData,
"Element %s content does not follow the DTD\nMisplaced %s\n",
state->node->name, qname);
ret = 0;
} else {
ret = 1;
}
}
break;
}
}
}
eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
vstateVPush(ctxt, eDecl, elem);
return(ret);
}
int
xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
int ret = 1;
if (len <= 0)
return(ret);
if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
xmlValidStatePtr state = ctxt->vstate;
xmlElementPtr elemDecl;
if (state->elemDecl != NULL) {
elemDecl = state->elemDecl;
switch(elemDecl->etype) {
case XML_ELEMENT_TYPE_UNDEFINED:
ret = 0;
break;
case XML_ELEMENT_TYPE_EMPTY:
VECTXT(ctxt, state->node);
VERROR(ctxt->userData,
"Element %s was declared EMPTY this one has content\n",
state->node->name);
ret = 0;
break;
case XML_ELEMENT_TYPE_ANY:
break;
case XML_ELEMENT_TYPE_MIXED:
break;
case XML_ELEMENT_TYPE_ELEMENT:
if (len > 0) {
int i;
for (i = 0;i < len;i++) {
if (!IS_BLANK(data[i])) {
VECTXT(ctxt, state->node);
VERROR(ctxt->userData,
"Element %s content does not follow the DTD\nText not allowed\n",
state->node->name);
ret = 0;
goto done;
}
}
}
break;
}
}
}
done:
return(ret);
}
int
xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
xmlNodePtr elem, const xmlChar *qname ATTRIBUTE_UNUSED) {
int ret = 1;
if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
xmlValidStatePtr state = ctxt->vstate;
xmlElementPtr elemDecl;
if (state->elemDecl != NULL) {
elemDecl = state->elemDecl;
if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
if (state->exec != NULL) {
ret = xmlRegExecPushString(state->exec, NULL, NULL);
if (ret == 0) {
VECTXT(ctxt, state->node);
VERROR(ctxt->userData,
"Element %s content does not follow the DTD\nExpecting more child\n",
state->node->name);
} else {
ret = 1;
}
}
}
}
vstateVPop(ctxt);
}
return(ret);
}
#endif
int
xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
xmlNodePtr elem) {
xmlElementPtr elemDecl = NULL;
xmlElementContentPtr cont;
xmlAttributePtr attr;
xmlNodePtr child;
int ret = 1, tmp;
const xmlChar *name;
int extsubset = 0;
CHECK_DTD;
if (elem == NULL) return(0);
switch (elem->type) {
case XML_ATTRIBUTE_NODE:
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Attribute element not expected here\n");
return(0);
case XML_TEXT_NODE:
if (elem->children != NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData, "Text element has childs !\n");
return(0);
}
if (elem->properties != NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData, "Text element has attributes !\n");
return(0);
}
if (elem->ns != NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData, "Text element has namespace !\n");
return(0);
}
if (elem->nsDef != NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Text element carries namespace definitions !\n");
return(0);
}
if (elem->content == NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Text element has no content !\n");
return(0);
}
return(1);
case XML_XINCLUDE_START:
case XML_XINCLUDE_END:
return(1);
case XML_CDATA_SECTION_NODE:
case XML_ENTITY_REF_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
return(1);
case XML_ENTITY_NODE:
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Entity element not expected here\n");
return(0);
case XML_NOTATION_NODE:
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Notation element not expected here\n");
return(0);
case XML_DOCUMENT_NODE:
case XML_DOCUMENT_TYPE_NODE:
case XML_DOCUMENT_FRAG_NODE:
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Document element not expected here\n");
return(0);
case XML_HTML_DOCUMENT_NODE:
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"\n");
return(0);
case XML_ELEMENT_NODE:
break;
default:
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"unknown element type %d\n", elem->type);
return(0);
}
elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
if (elemDecl == NULL)
return(0);
if (ctxt->vstateNr == 0) {
switch (elemDecl->etype) {
case XML_ELEMENT_TYPE_UNDEFINED:
VECTXT(ctxt, elem);
VERROR(ctxt->userData, "No declaration for element %s\n",
elem->name);
return(0);
case XML_ELEMENT_TYPE_EMPTY:
if (elem->children != NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Element %s was declared EMPTY this one has content\n",
elem->name);
ret = 0;
}
break;
case XML_ELEMENT_TYPE_ANY:
break;
case XML_ELEMENT_TYPE_MIXED:
if ((elemDecl->content != NULL) &&
(elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
ret = xmlValidateOneCdataElement(ctxt, doc, elem);
if (!ret) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Element %s was declared #PCDATA but contains non text nodes\n",
elem->name);
}
break;
}
child = elem->children;
while (child != NULL) {
if (child->type == XML_ELEMENT_NODE) {
name = child->name;
if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
xmlChar qname[500];
snprintf((char *) qname, sizeof(qname), "%s:%s",
child->ns->prefix, child->name);
qname[sizeof(qname) - 1] = 0;
cont = elemDecl->content;
while (cont != NULL) {
if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
if (xmlStrEqual(cont->name, qname)) break;
} else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
(cont->c1 != NULL) &&
(cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
if (xmlStrEqual(cont->c1->name, qname)) break;
} else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
(cont->c1 == NULL) ||
(cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
xmlGenericError(xmlGenericErrorContext,
"Internal: MIXED struct bad\n");
break;
}
cont = cont->c2;
}
if (cont != NULL)
goto child_ok;
}
cont = elemDecl->content;
while (cont != NULL) {
if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
if (xmlStrEqual(cont->name, name)) break;
} else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
(cont->c1 != NULL) &&
(cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
if (xmlStrEqual(cont->c1->name, name)) break;
} else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
(cont->c1 == NULL) ||
(cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
xmlGenericError(xmlGenericErrorContext,
"Internal: MIXED struct bad\n");
break;
}
cont = cont->c2;
}
if (cont == NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Element %s is not declared in %s list of possible children\n",
name, elem->name);
ret = 0;
}
}
child_ok:
child = child->next;
}
break;
case XML_ELEMENT_TYPE_ELEMENT:
if ((doc->standalone == 1) && (extsubset == 1)) {
child = elem->children;
while (child != NULL) {
if (child->type == XML_TEXT_NODE) {
const xmlChar *content = child->content;
while (IS_BLANK(*content))
content++;
if (*content == 0) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"standalone: %s declared in the external subset contains white spaces nodes\n",
elem->name);
ret = 0;
break;
}
}
child =child->next;
}
}
child = elem->children;
cont = elemDecl->content;
tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
if (tmp <= 0)
ret = tmp;
break;
}
}
attr = elemDecl->attributes;
while (attr != NULL) {
if (attr->def == XML_ATTRIBUTE_REQUIRED) {
int qualified = -1;
if ((attr->prefix == NULL) &&
(xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
xmlNsPtr ns;
ns = elem->nsDef;
while (ns != NULL) {
if (ns->prefix == NULL)
goto found;
ns = ns->next;
}
} else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
xmlNsPtr ns;
ns = elem->nsDef;
while (ns != NULL) {
if (xmlStrEqual(attr->name, ns->prefix))
goto found;
ns = ns->next;
}
} else {
xmlAttrPtr attrib;
attrib = elem->properties;
while (attrib != NULL) {
if (xmlStrEqual(attrib->name, attr->name)) {
if (attr->prefix != NULL) {
xmlNsPtr nameSpace = attrib->ns;
if (nameSpace == NULL)
nameSpace = elem->ns;
if (nameSpace == NULL) {
if (qualified < 0)
qualified = 0;
} else if (!xmlStrEqual(nameSpace->prefix,
attr->prefix)) {
if (qualified < 1)
qualified = 1;
} else
goto found;
} else {
goto found;
}
}
attrib = attrib->next;
}
}
if (qualified == -1) {
if (attr->prefix == NULL) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Element %s does not carry attribute %s\n",
elem->name, attr->name);
ret = 0;
} else {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Element %s does not carry attribute %s:%s\n",
elem->name, attr->prefix,attr->name);
ret = 0;
}
} else if (qualified == 0) {
VWARNING(ctxt->userData,
"Element %s required attribute %s:%s has no prefix\n",
elem->name, attr->prefix,attr->name);
} else if (qualified == 1) {
VWARNING(ctxt->userData,
"Element %s required attribute %s:%s has different prefix\n",
elem->name, attr->prefix,attr->name);
}
} else if (attr->def == XML_ATTRIBUTE_FIXED) {
if ((attr->prefix == NULL) &&
(xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
xmlNsPtr ns;
ns = elem->nsDef;
while (ns != NULL) {
if (ns->prefix == NULL) {
if (!xmlStrEqual(attr->defaultValue, ns->href)) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Element %s namespace name for default namespace does not match the DTD\n",
elem->name);
ret = 0;
}
goto found;
}
ns = ns->next;
}
} else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
xmlNsPtr ns;
ns = elem->nsDef;
while (ns != NULL) {
if (xmlStrEqual(attr->name, ns->prefix)) {
if (!xmlStrEqual(attr->defaultValue, ns->href)) {
VECTXT(ctxt, elem);
VERROR(ctxt->userData,
"Element %s namespace name for %s does not match the DTD\n",
elem->name, ns->prefix);
ret = 0;
}
goto found;
}
ns = ns->next;
}
}
}
found:
attr = attr->nexth;
}
return(ret);
}
int
xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
xmlNodePtr root;
if (doc == NULL) return(0);
root = xmlDocGetRootElement(doc);
if ((root == NULL) || (root->name == NULL)) {
VERROR(ctxt->userData, "Not valid: no root element\n");
return(0);
}
if ((doc->intSubset != NULL) &&
(doc->intSubset->name != NULL)) {
if (!xmlStrEqual(doc->intSubset->name, root->name)) {
if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
xmlChar qname[500];
snprintf((char *) qname, sizeof(qname), "%s:%s",
root->ns->prefix, root->name);
qname[sizeof(qname) - 1] = 0;
if (xmlStrEqual(doc->intSubset->name, qname))
goto name_ok;
}
if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
(xmlStrEqual(root->name, BAD_CAST "html")))
goto name_ok;
VECTXT(ctxt, root);
VERROR(ctxt->userData,
"Not valid: root and DTD name do not match '%s' and '%s'\n",
root->name, doc->intSubset->name);
return(0);
}
}
name_ok:
return(1);
}
int
xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
xmlNodePtr child;
xmlAttrPtr attr;
xmlChar *value;
int ret = 1;
if (elem == NULL) return(0);
if ((elem->type == XML_XINCLUDE_START) ||
(elem->type == XML_XINCLUDE_END))
return(1);
CHECK_DTD;
if (elem->type == XML_ENTITY_REF_NODE) {
return(1);
}
ret &= xmlValidateOneElement(ctxt, doc, elem);
attr = elem->properties;
while(attr != NULL) {
value = xmlNodeListGetString(doc, attr->children, 0);
ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
if (value != NULL)
xmlFree(value);
attr= attr->next;
}
child = elem->children;
while (child != NULL) {
ret &= xmlValidateElement(ctxt, doc, child);
child = child->next;
}
return(ret);
}
static void
xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
const xmlChar *name) {
xmlAttrPtr id;
xmlAttrPtr attr;
if (ref == NULL)
return;
if ((ref->attr == NULL) && (ref->name == NULL))
return;
attr = ref->attr;
if (attr == NULL) {
xmlChar *dup, *str = NULL, *cur, save;
dup = xmlStrdup(name);
if (dup == NULL) {
ctxt->valid = 0;
return;
}
cur = dup;
while (*cur != 0) {
str = cur;
while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
save = *cur;
*cur = 0;
id = xmlGetID(ctxt->doc, str);
if (id == NULL) {
VERROR(ctxt->userData,
"attribute %s line %d references an unknown ID \"%s\"\n",
ref->name, ref->lineno, str);
ctxt->valid = 0;
}
if (save == 0)
break;
*cur = save;
while (IS_BLANK(*cur)) cur++;
}
xmlFree(dup);
} else if (attr->atype == XML_ATTRIBUTE_IDREF) {
id = xmlGetID(ctxt->doc, name);
if (id == NULL) {
VECTXT(ctxt, attr->parent);
VERROR(ctxt->userData,
"IDREF attribute %s references an unknown ID \"%s\"\n",
attr->name, name);
ctxt->valid = 0;
}
} else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
xmlChar *dup, *str = NULL, *cur, save;
dup = xmlStrdup(name);
if (dup == NULL) {
ctxt->valid = 0;
return;
}
cur = dup;
while (*cur != 0) {
str = cur;
while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
save = *cur;
*cur = 0;
id = xmlGetID(ctxt->doc, str);
if (id == NULL) {
VECTXT(ctxt, attr->parent);
VERROR(ctxt->userData,
"IDREFS attribute %s references an unknown ID \"%s\"\n",
attr->name, str);
ctxt->valid = 0;
}
if (save == 0)
break;
*cur = save;
while (IS_BLANK(*cur)) cur++;
}
xmlFree(dup);
}
}
static int
xmlWalkValidateList(const void *data, const void *user)
{
xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
return 1;
}
static void
xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
const xmlChar *name) {
xmlValidateMemo memo;
if (ref_list == NULL)
return;
memo.ctxt = ctxt;
memo.name = name;
xmlListWalk(ref_list, xmlWalkValidateList, &memo);
}
int
xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
xmlRefTablePtr table;
if (doc == NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlValidateDocumentFinal: doc == NULL\n");
return(0);
}
table = (xmlRefTablePtr) doc->refs;
ctxt->doc = doc;
ctxt->valid = 1;
xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
return(ctxt->valid);
}
int
xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
int ret;
xmlDtdPtr oldExt;
xmlNodePtr root;
if (dtd == NULL) return(0);
if (doc == NULL) return(0);
oldExt = doc->extSubset;
doc->extSubset = dtd;
ret = xmlValidateRoot(ctxt, doc);
if (ret == 0) {
doc->extSubset = oldExt;
return(ret);
}
if (doc->ids != NULL) {
xmlFreeIDTable(doc->ids);
doc->ids = NULL;
}
if (doc->refs != NULL) {
xmlFreeRefTable(doc->refs);
doc->refs = NULL;
}
root = xmlDocGetRootElement(doc);
ret = xmlValidateElement(ctxt, doc, root);
ret &= xmlValidateDocumentFinal(ctxt, doc);
doc->extSubset = oldExt;
return(ret);
}
static void
xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
const xmlChar *name ATTRIBUTE_UNUSED) {
if (cur == NULL)
return;
if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
xmlChar *notation = cur->content;
if (notation != NULL) {
int ret;
ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
if (ret != 1) {
ctxt->valid = 0;
}
}
}
}
static void
xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
const xmlChar *name ATTRIBUTE_UNUSED) {
int ret;
xmlDocPtr doc;
xmlElementPtr elem;
if (cur == NULL)
return;
switch (cur->atype) {
case XML_ATTRIBUTE_CDATA:
case XML_ATTRIBUTE_ID:
case XML_ATTRIBUTE_IDREF :
case XML_ATTRIBUTE_IDREFS:
case XML_ATTRIBUTE_NMTOKEN:
case XML_ATTRIBUTE_NMTOKENS:
case XML_ATTRIBUTE_ENUMERATION:
break;
case XML_ATTRIBUTE_ENTITY:
case XML_ATTRIBUTE_ENTITIES:
case XML_ATTRIBUTE_NOTATION:
if (cur->defaultValue != NULL) {
ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
cur->atype, cur->defaultValue);
if ((ret == 0) && (ctxt->valid == 1))
ctxt->valid = 0;
}
if (cur->tree != NULL) {
xmlEnumerationPtr tree = cur->tree;
while (tree != NULL) {
ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
cur->name, cur->atype, tree->name);
if ((ret == 0) && (ctxt->valid == 1))
ctxt->valid = 0;
tree = tree->next;
}
}
}
if (cur->atype == XML_ATTRIBUTE_NOTATION) {
doc = cur->doc;
if ((doc == NULL) || (cur->elem == NULL)) {
VERROR(ctxt->userData,
"xmlValidateAttributeCallback(%s): internal error\n",
cur->name);
return;
}
elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
if (elem == NULL)
elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
if (elem == NULL) {
VERROR(ctxt->userData,
"attribute %s: could not find decl for element %s\n",
cur->name, cur->elem);
return;
}
if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
VERROR(ctxt->userData,
"NOTATION attribute %s declared for EMPTY element %s\n",
cur->name, cur->elem);
ctxt->valid = 0;
}
}
}
int
xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
xmlDtdPtr dtd;
xmlAttributeTablePtr table;
xmlEntitiesTablePtr entities;
if (doc == NULL) return(0);
if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
return(0);
ctxt->doc = doc;
ctxt->valid = 1;
dtd = doc->intSubset;
if ((dtd != NULL) && (dtd->attributes != NULL)) {
table = (xmlAttributeTablePtr) dtd->attributes;
xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
}
if ((dtd != NULL) && (dtd->entities != NULL)) {
entities = (xmlEntitiesTablePtr) dtd->entities;
xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
ctxt);
}
dtd = doc->extSubset;
if ((dtd != NULL) && (dtd->attributes != NULL)) {
table = (xmlAttributeTablePtr) dtd->attributes;
xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
}
if ((dtd != NULL) && (dtd->entities != NULL)) {
entities = (xmlEntitiesTablePtr) dtd->entities;
xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
ctxt);
}
return(ctxt->valid);
}
int
xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
int ret;
xmlNodePtr root;
if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
VERROR(ctxt->userData, "no DTD found!\n" );
return(0);
}
if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
(doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
doc->intSubset->SystemID);
if (doc->extSubset == NULL) {
if (doc->intSubset->SystemID != NULL) {
VERROR(ctxt->userData,
"Could not load the external subset \"%s\"\n",
doc->intSubset->SystemID);
} else {
VERROR(ctxt->userData,
"Could not load the external subset \"%s\"\n",
doc->intSubset->ExternalID);
}
return(0);
}
}
if (doc->ids != NULL) {
xmlFreeIDTable(doc->ids);
doc->ids = NULL;
}
if (doc->refs != NULL) {
xmlFreeRefTable(doc->refs);
doc->refs = NULL;
}
ret = xmlValidateDtdFinal(ctxt, doc);
if (!xmlValidateRoot(ctxt, doc)) return(0);
root = xmlDocGetRootElement(doc);
ret &= xmlValidateElement(ctxt, doc, root);
ret &= xmlValidateDocumentFinal(ctxt, doc);
return(ret);
}
int
xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list,
int *len, int max) {
int i;
if ((ctree == NULL) || (list == NULL) || (len == NULL))
return(-1);
if (*len >= max) return(*len);
switch (ctree->type) {
case XML_ELEMENT_CONTENT_PCDATA:
for (i = 0; i < *len;i++)
if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len);
list[(*len)++] = BAD_CAST "#PCDATA";
break;
case XML_ELEMENT_CONTENT_ELEMENT:
for (i = 0; i < *len;i++)
if (xmlStrEqual(ctree->name, list[i])) return(*len);
list[(*len)++] = ctree->name;
break;
case XML_ELEMENT_CONTENT_SEQ:
xmlValidGetPotentialChildren(ctree->c1, list, len, max);
xmlValidGetPotentialChildren(ctree->c2, list, len, max);
break;
case XML_ELEMENT_CONTENT_OR:
xmlValidGetPotentialChildren(ctree->c1, list, len, max);
xmlValidGetPotentialChildren(ctree->c2, list, len, max);
break;
}
return(*len);
}
int
xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list,
int max) {
xmlValidCtxt vctxt;
int nb_valid_elements = 0;
const xmlChar *elements[256];
int nb_elements = 0, i;
const xmlChar *name;
xmlNode *ref_node;
xmlNode *parent;
xmlNode *test_node;
xmlNode *prev_next;
xmlNode *next_prev;
xmlNode *parent_childs;
xmlNode *parent_last;
xmlElement *element_desc;
memset(&vctxt, 0, sizeof (xmlValidCtxt));
if (prev == NULL && next == NULL)
return(-1);
if (list == NULL) return(-1);
if (max <= 0) return(-1);
nb_valid_elements = 0;
ref_node = prev ? prev : next;
parent = ref_node->parent;
element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
parent->name);
if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
parent->name);
if (element_desc == NULL) return(-1);
prev_next = prev ? prev->next : NULL;
next_prev = next ? next->prev : NULL;
parent_childs = parent->children;
parent_last = parent->last;
test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>");
test_node->doc = ref_node->doc;
test_node->parent = parent;
test_node->prev = prev;
test_node->next = next;
name = test_node->name;
if (prev) prev->next = test_node;
else parent->children = test_node;
if (next) next->prev = test_node;
else parent->last = test_node;
nb_elements = xmlValidGetPotentialChildren(element_desc->content,
elements, &nb_elements, 256);
for (i = 0;i < nb_elements;i++) {
test_node->name = elements[i];
if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
int j;
for (j = 0; j < nb_valid_elements;j++)
if (xmlStrEqual(elements[i], list[j])) break;
list[nb_valid_elements++] = elements[i];
if (nb_valid_elements >= max) break;
}
}
if (prev) prev->next = prev_next;
if (next) next->prev = next_prev;
parent->children = parent_childs;
parent->last = parent_last;
test_node->name = name;
xmlFreeNode(test_node);
return(nb_valid_elements);
}