📄 valid.c
字号:
xmlBufferWriteChar(buf, " | ");
xmlDumpEnumeration(buf, cur->next);
}
}
#endif /* LIBXML_OUTPUT_ENABLED */
#ifdef LIBXML_VALID_ENABLED
/**
* xmlScanAttributeDeclCallback:
* @attr: the attribute decl
* @list: the list to update
*
* Callback called by xmlScanAttributeDecl when a new attribute
* has to be entered in the list.
*/
static void
xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list,
const xmlChar* name ATTRIBUTE_UNUSED) {
attr->nexth = *list;
*list = attr;
}
/**
* xmlScanAttributeDecl:
* @dtd: pointer to the DTD
* @elem: the element name
*
* When inserting a new element scan the DtD for existing attributes
* for that element and initialize the Attribute chain
*
* Returns the pointer to the first attribute decl in the chain,
* possibly NULL.
*/
xmlAttributePtr
xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) {
xmlAttributePtr ret = NULL;
xmlAttributeTablePtr table;
if (dtd == NULL) {
return(NULL);
}
if (elem == NULL) {
return(NULL);
}
table = (xmlAttributeTablePtr) dtd->attributes;
if (table == NULL)
return(NULL);
/* WRONG !!! */
xmlHashScan3(table, NULL, NULL, elem,
(xmlHashScanner) xmlScanAttributeDeclCallback, &ret);
return(ret);
}
/**
* xmlScanIDAttributeDecl:
* @ctxt: the validation context
* @elem: the element name
* @err: whether to raise errors here
*
* Verify that the element don't have too many ID attributes
* declared.
*
* Returns the number of ID attributes found.
*/
static int
xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) {
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) && (err))
xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
"Element %s has too many ID attributes defined : %s\n",
elem->name, cur->name, NULL);
}
cur = cur->nexth;
}
return(ret);
}
#endif /* LIBXML_VALID_ENABLED */
/**
* xmlFreeAttribute:
* @elem: An attribute
*
* Deallocate the memory used by an attribute definition
*/
static void
xmlFreeAttribute(xmlAttributePtr attr) {
xmlDictPtr dict;
if (attr == NULL) return;
if (attr->doc != NULL)
dict = attr->doc->dict;
else
dict = NULL;
xmlUnlinkNode((xmlNodePtr) attr);
if (attr->tree != NULL)
xmlFreeEnumeration(attr->tree);
if (dict) {
if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
xmlFree((xmlChar *) attr->elem);
if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
xmlFree((xmlChar *) attr->name);
if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
xmlFree((xmlChar *) attr->prefix);
if ((attr->defaultValue != NULL) &&
(!xmlDictOwns(dict, attr->defaultValue)))
xmlFree((xmlChar *) attr->defaultValue);
} else {
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);
}
/**
* xmlAddAttributeDecl:
* @ctxt: the validation context
* @dtd: pointer to the DTD
* @elem: the element name
* @name: the attribute name
* @ns: the attribute namespace prefix
* @type: the attribute type
* @def: the attribute default type
* @defaultValue: the attribute default value
* @tree: if it's an enumeration, the associated list
*
* Register a new attribute declaration
* Note that @tree becomes the ownership of the DTD
*
* Returns NULL if not new, otherwise the attribute decl
*/
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;
xmlDictPtr dict = NULL;
if (dtd == NULL) {
xmlFreeEnumeration(tree);
return(NULL);
}
if (name == NULL) {
xmlFreeEnumeration(tree);
return(NULL);
}
if (elem == NULL) {
xmlFreeEnumeration(tree);
return(NULL);
}
if (dtd->doc != NULL)
dict = dtd->doc->dict;
#ifdef LIBXML_VALID_ENABLED
/*
* Check the type and possibly the default value.
*/
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:
xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
"Internal: ATTRIBUTE struct corrupted invalid type\n",
NULL);
xmlFreeEnumeration(tree);
return(NULL);
}
if ((defaultValue != NULL) &&
(!xmlValidateAttributeValue(type, defaultValue))) {
xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
"Attribute %s of %s: invalid default value\n",
elem, name, defaultValue);
defaultValue = NULL;
if (ctxt != NULL)
ctxt->valid = 0;
}
#endif /* LIBXML_VALID_ENABLED */
/*
* Check first that an attribute defined in the external subset wasn't
* already defined in the internal subset
*/
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);
}
/*
* Create the Attribute table if needed.
*/
table = (xmlAttributeTablePtr) dtd->attributes;
if (table == NULL) {
table = xmlHashCreateDict(0, dict);
dtd->attributes = (void *) table;
}
if (table == NULL) {
xmlVErrMemory(ctxt,
"xmlAddAttributeDecl: Table creation failed!\n");
return(NULL);
}
ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
if (ret == NULL) {
xmlVErrMemory(ctxt, "malloc failed");
return(NULL);
}
memset(ret, 0, sizeof(xmlAttribute));
ret->type = XML_ATTRIBUTE_DECL;
/*
* fill the structure.
*/
ret->atype = type;
/*
* doc must be set before possible error causes call
* to xmlFreeAttribute (because it's used to check on
* dict use)
*/
ret->doc = dtd->doc;
if (dict) {
ret->name = xmlDictLookup(dict, name, -1);
ret->prefix = xmlDictLookup(dict, ns, -1);
ret->elem = xmlDictLookup(dict, elem, -1);
} else {
ret->name = xmlStrdup(name);
ret->prefix = xmlStrdup(ns);
ret->elem = xmlStrdup(elem);
}
ret->def = def;
ret->tree = tree;
if (defaultValue != NULL) {
if (dict)
ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
else
ret->defaultValue = xmlStrdup(defaultValue);
}
/*
* Validity Check:
* Search the DTD for previous declarations of the ATTLIST
*/
if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) {
#ifdef LIBXML_VALID_ENABLED
/*
* The attribute is already defined in this DTD.
*/
xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
"Attribute %s of element %s: already defined\n",
name, elem, NULL);
#endif /* LIBXML_VALID_ENABLED */
xmlFreeAttribute(ret);
return(NULL);
}
/*
* Validity Check:
* Multiple ID per element
*/
elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
if (elemDef != NULL) {
#ifdef LIBXML_VALID_ENABLED
if ((type == XML_ATTRIBUTE_ID) &&
(xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) {
xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
"Element %s has too may ID attributes defined : %s\n",
elem, name, NULL);
if (ctxt != NULL)
ctxt->valid = 0;
}
#endif /* LIBXML_VALID_ENABLED */
/*
* Insert namespace default def first they need to be
* processed first.
*/
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;
}
}
}
/*
* Link it to the DTD
*/
ret->parent = dtd;
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);
}
/**
* xmlFreeAttributeTable:
* @table: An attribute table
*
* Deallocate the memory used by an entities hash table.
*/
void
xmlFreeAttributeTable(xmlAttributeTablePtr table) {
xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
}
#ifdef LIBXML_TREE_ENABLED
/**
* xmlCopyAttribute:
* @attr: An attribute
*
* Build a copy of an attribute.
*
* Returns the new xmlAttributePtr or NULL in case of error.
*/
static xmlAttributePtr
xmlCopyAttribute(xmlAttributePtr attr) {
xmlAttributePtr cur;
cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
if (cur == NULL) {
xmlVErrMemory(NULL, "malloc failed");
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);
}
/**
* xmlCopyAttributeTable:
* @table: An attribute table
*
* Build a copy of an attribute table.
*
* Returns the new xmlAttributeTablePtr or NULL in case of error.
*/
xmlAttributeTablePtr
xmlCopyAttributeTable(xmlAttributeTablePtr table) {
return((xmlAttributeTablePtr) xmlHashCopy(table,
(xmlHashCopier) xmlCopyAttribute));
}
#endif /* LIBXML_TREE_ENABLED */
#ifdef LIBXML_OUTPUT_ENABLED
/**
* xmlDumpAttributeDecl:
* @buf: the XML buffer output
* @attr: An attribute declaration
*
* This will dump the content of the attribute declaration as an XML
* DTD definition
*/
void
xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
if ((buf == NULL) || (attr == NULL))
return;
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -