⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sax.c

📁 Vovida 社区开源的 SIP 协议源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * SAX.c : Default SAX handler to build a tree. * * See Copyright for the status of this software. * * Daniel Veillard <Daniel.Veillard@w3.org> */#include "global.h"#ifdef WIN32#include "win32config.h"#else#include "config.h"#endif#include <stdio.h>#include <stdlib.h>#include <libxml/xmlmemory.h>#include <libxml/tree.h>#include <libxml/parser.h>#include <libxml/parserInternals.h>#include <libxml/valid.h>#include <libxml/entities.h>#include "xml-error.h"#include <libxml/debugXML.h>#include <libxml/xmlIO.h>#include <libxml/SAX.h>/* #define DEBUG_SAX *//* #define DEBUG_SAX_TREE *//** * getPublicId: * @ctx: the user data (XML parser context) * * Return the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN" * * Returns a xmlChar * */const xmlChar *getPublicId(void *ctx){    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */    return(NULL);}/** * getSystemId: * @ctx: the user data (XML parser context) * * Return the system ID, basically URL or filename e.g. * http://www.sgmlsource.com/dtds/memo.dtd * * Returns a xmlChar * */const xmlChar *getSystemId(void *ctx){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    return(BAD_CAST ctxt->input->filename); }/** * getLineNumber: * @ctx: the user data (XML parser context) * * Return the line number of the current parsing point. * * Returns an int */intgetLineNumber(void *ctx){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    return(ctxt->input->line);}/** * getColumnNumber: * @ctx: the user data (XML parser context) * * Return the column number of the current parsing point. * * Returns an int */intgetColumnNumber(void *ctx){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    return(ctxt->input->col);}/* * The default SAX Locator. */xmlSAXLocator xmlDefaultSAXLocator = {    getPublicId, getSystemId, getLineNumber, getColumnNumber};/** * isStandalone: * @ctx: the user data (XML parser context) * * Is this document tagged standalone ? * * Returns 1 if true */intisStandalone(void *ctx){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    return(ctxt->myDoc->standalone == 1);}/** * hasInternalSubset: * @ctx: the user data (XML parser context) * * Does this document has an internal subset * * Returns 1 if true */inthasInternalSubset(void *ctx){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    return(ctxt->myDoc->intSubset != NULL);}/** * hasExternalSubset: * @ctx: the user data (XML parser context) * * Does this document has an external subset * * Returns 1 if true */inthasExternalSubset(void *ctx){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    return(ctxt->myDoc->extSubset != NULL);}/** * internalSubset: * @ctx: the user data (XML parser context) * * Callback on internal subset declaration. */voidinternalSubset(void *ctx, const xmlChar *name,	       const xmlChar *ExternalID, const xmlChar *SystemID){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;#ifdef DEBUG_SAX    fprintf(stderr, "SAX.internalSubset(%s, %s, %s)\n",            name, ExternalID, SystemID);#endif    xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);}/** * externalSubset: * @ctx: the user data (XML parser context) * * Callback on external subset declaration. */voidexternalSubset(void *ctx, const xmlChar *name,	       const xmlChar *ExternalID, const xmlChar *SystemID){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;#ifdef DEBUG_SAX    fprintf(stderr, "SAX.externalSubset(%s, %s, %s)\n",            name, ExternalID, SystemID);#endif    if (((ExternalID != NULL) || (SystemID != NULL)) &&        (ctxt->validate && ctxt->wellFormed && ctxt->myDoc)) {	/*	 * Try to fetch and parse the external subset.	 */	xmlParserInputPtr oldinput;	int oldinputNr;	int oldinputMax;	xmlParserInputPtr *oldinputTab;	int oldwellFormed;	xmlParserInputPtr input = NULL;	xmlCharEncoding enc;	/*	 * Ask the Entity resolver to load the damn thing	 */	if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))	    input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,	                                        SystemID);	if (input == NULL) {	    return;	}	xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);	/*	 * make sure we won't destroy the main document context	 */	oldinput = ctxt->input;	oldinputNr = ctxt->inputNr;	oldinputMax = ctxt->inputMax;	oldinputTab = ctxt->inputTab;	oldwellFormed = ctxt->wellFormed;	ctxt->inputTab = (xmlParserInputPtr *)	                 xmlMalloc(5 * sizeof(xmlParserInputPtr));	if (ctxt->inputTab == NULL) {	    ctxt->errNo = XML_ERR_NO_MEMORY;	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))		ctxt->sax->error(ctxt->userData, 		     "externalSubset: out of memory\n");	    ctxt->errNo = XML_ERR_NO_MEMORY;	    ctxt->input = oldinput;	    ctxt->inputNr = oldinputNr;	    ctxt->inputMax = oldinputMax;	    ctxt->inputTab = oldinputTab;	    return;	}	ctxt->inputNr = 0;	ctxt->inputMax = 5;	ctxt->input = NULL;	xmlPushInput(ctxt, input);	/*	 * On the fly encoding conversion if needed	 */	enc = xmlDetectCharEncoding(ctxt->input->cur, 4);	xmlSwitchEncoding(ctxt, enc);	if (input->filename == NULL)	    input->filename = (char *) xmlStrdup(SystemID);	input->line = 1;	input->col = 1;	input->base = ctxt->input->cur;	input->cur = ctxt->input->cur;	input->free = NULL;	/*	 * let's parse that entity knowing it's an external subset.	 */	xmlParseExternalSubset(ctxt, ExternalID, SystemID);        /*	 * Free up the external entities	 */	while (ctxt->inputNr > 1)	    xmlPopInput(ctxt);	xmlFreeInputStream(ctxt->input);        xmlFree(ctxt->inputTab);	/*	 * Restore the parsing context of the main entity	 */	ctxt->input = oldinput;	ctxt->inputNr = oldinputNr;	ctxt->inputMax = oldinputMax;	ctxt->inputTab = oldinputTab;	/* ctxt->wellFormed = oldwellFormed; */    }}/** * resolveEntity: * @ctx: the user data (XML parser context) * @publicId: The public ID of the entity * @systemId: The system ID of the entity * * The entity loader, to control the loading of external entities, * the application can either: *    - override this resolveEntity() callback in the SAX block *    - or better use the xmlSetExternalEntityLoader() function to *      set up it's own entity resolution routine * * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. */xmlParserInputPtrresolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;#ifdef DEBUG_SAX    fprintf(stderr, "SAX.resolveEntity(%s, %s)\n", publicId, systemId);#endif    return(xmlLoadExternalEntity((const char *) systemId,				 (const char *) publicId, ctxt));}/** * getEntity: * @ctx: the user data (XML parser context) * @name: The entity name * * Get an entity by name * * Returns the xmlEntityPtr if found. */xmlEntityPtrgetEntity(void *ctx, const xmlChar *name){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    xmlEntityPtr ret;#ifdef DEBUG_SAX    fprintf(stderr, "SAX.getEntity(%s)\n", name);#endif    ret = xmlGetDocEntity(ctxt->myDoc, name);    return(ret);}/** * getParameterEntity: * @ctx: the user data (XML parser context) * @name: The entity name * * Get a parameter entity by name * * Returns the xmlEntityPtr if found. */xmlEntityPtrgetParameterEntity(void *ctx, const xmlChar *name){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    xmlEntityPtr ret;#ifdef DEBUG_SAX    fprintf(stderr, "SAX.getParameterEntity(%s)\n", name);#endif    ret = xmlGetParameterEntity(ctxt->myDoc, name);    return(ret);}/** * entityDecl: * @ctx: the user data (XML parser context) * @name:  the entity name  * @type:  the entity type  * @publicId: The public ID of the entity * @systemId: The system ID of the entity * @content: the entity value (without processing). * * An entity definition has been parsed */voidentityDecl(void *ctx, const xmlChar *name, int type,          const xmlChar *publicId, const xmlChar *systemId, xmlChar *content){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;#ifdef DEBUG_SAX    fprintf(stderr, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",            name, type, publicId, systemId, content);#endif    if (ctxt->inSubset == 1)	xmlAddDocEntity(ctxt->myDoc, name, type, publicId,		              systemId, content);    else if (ctxt->inSubset == 2)	xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,		              systemId, content);    else {	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))	    ctxt->sax->error(ctxt, 	     "SAX.entityDecl(%s) called while not in subset\n", name);    }}/** * attributeDecl: * @ctx: the user data (XML parser context) * @fullname:  the attribute name  * @type:  the attribute type  * @publicId: The public ID of the attribute * @systemId: The system ID of the attribute * @content: the attribute value (without processing). * * An attribute definition has been parsed */voidattributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,              int type, int def, const xmlChar *defaultValue,	      xmlEnumerationPtr tree){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    xmlAttributePtr attr;    xmlChar *name = NULL, *prefix = NULL;#ifdef DEBUG_SAX    fprintf(stderr, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",            elem, fullname, type, def, defaultValue);#endif    name = xmlSplitQName(ctxt, fullname, &prefix);    if (ctxt->inSubset == 1)	attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,                               name, prefix, type, def, defaultValue, tree);    else if (ctxt->inSubset == 2)	attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,                               name, prefix, type, def, defaultValue, tree);    else {	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))	    ctxt->sax->error(ctxt, 	     "SAX.attributeDecl(%s) called while not in subset\n", name);	return;    }    if (attr == 0) ctxt->valid = 0;    if (ctxt->validate && ctxt->wellFormed &&        ctxt->myDoc && ctxt->myDoc->intSubset)	ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,	                                        attr);    if (prefix != NULL)	xmlFree(prefix);    if (name != NULL)	xmlFree(name);}/** * elementDecl: * @ctx: the user data (XML parser context) * @name:  the element name  * @type:  the element type  * @publicId: The public ID of the element * @systemId: The system ID of the element * @content: the element value (without processing). * * An element definition has been parsed */voidelementDecl(void *ctx, const xmlChar *name, int type,	    xmlElementContentPtr content){    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;    xmlElementPtr elem = NULL;#ifdef DEBUG_SAX    fprintf(stderr, "SAX.elementDecl(%s, %d, ...)\n",            fullname, type);#endif        if (ctxt->inSubset == 1)	elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,                             name, type, content);    else if (ctxt->inSubset == 2)	elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,                             name, type, content);    else {	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))	    ctxt->sax->error(ctxt, 	     "SAX.elementDecl(%s) called while not in subset\n", name);	return;    }    if (elem == NULL) ctxt->valid = 0;    if (ctxt->validate && ctxt->wellFormed &&        ctxt->myDoc && ctxt->myDoc->intSubset)	ctxt->valid &= xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);}/** * notationDecl: * @ctx: the user data (XML parser context) * @name: The name of the notation * @publicId: The public ID of the entity * @systemId: The system ID of the entity * * What to do when a notation declaration has been parsed. */voidnotationDecl(void *ctx, const xmlChar *name,	     const xmlChar *publicId, const xmlChar *systemId)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -