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

📄 schematron.c

📁 xml开源解析代码.版本为libxml2-2.6.29,可支持GB3212.网络消息发送XML时很有用.
💻 C
📖 第 1 页 / 共 4 页
字号:
        prev->patnext = ret;    }    return (ret);}/** * xmlSchematronFreeRules: * @rules:  a list of rules * * Free a list of rules. */static voidxmlSchematronFreeRules(xmlSchematronRulePtr rules) {    xmlSchematronRulePtr next;    while (rules != NULL) {        next = rules->next;	if (rules->tests)	    xmlSchematronFreeTests(rules->tests);	if (rules->context != NULL)	    xmlFree(rules->context);	if (rules->pattern)	    xmlFreePattern(rules->pattern);	if (rules->report != NULL)	    xmlFree(rules->report);	xmlFree(rules);	rules = next;    }}/** * xmlSchematronAddPattern: * @ctxt: the schema parsing context * @schema:  a schema structure * @node:  the node hosting the pattern * @id: the id or name of the pattern * * Add a pattern to a schematron * * Returns the new pointer or NULL in case of error */static xmlSchematronPatternPtrxmlSchematronAddPattern(xmlSchematronParserCtxtPtr ctxt,                     xmlSchematronPtr schema, xmlNodePtr node, xmlChar *name){    xmlSchematronPatternPtr ret;    if ((ctxt == NULL) || (schema == NULL) || (node == NULL) || (name == NULL))        return(NULL);    ret = (xmlSchematronPatternPtr) xmlMalloc(sizeof(xmlSchematronPattern));    if (ret == NULL) {        xmlSchematronPErrMemory(ctxt, "allocating schema pattern", node);        return (NULL);    }    memset(ret, 0, sizeof(xmlSchematronPattern));    ret->name = name;    ret->next = NULL;    if (schema->patterns == NULL) {	schema->patterns = ret;    } else {        xmlSchematronPatternPtr prev = schema->patterns;	while (prev->next != NULL)	     prev = prev->next;        prev->next = ret;    }    return (ret);}/** * xmlSchematronFreePatterns: * @patterns:  a list of patterns * * Free a list of patterns. */static voidxmlSchematronFreePatterns(xmlSchematronPatternPtr patterns) {    xmlSchematronPatternPtr next;    while (patterns != NULL) {        next = patterns->next;	if (patterns->name != NULL)	    xmlFree(patterns->name);	xmlFree(patterns);	patterns = next;    }}/** * xmlSchematronNewSchematron: * @ctxt:  a schema validation context * * Allocate a new Schematron structure. * * Returns the newly allocated structure or NULL in case or error */static xmlSchematronPtrxmlSchematronNewSchematron(xmlSchematronParserCtxtPtr ctxt){    xmlSchematronPtr ret;    ret = (xmlSchematronPtr) xmlMalloc(sizeof(xmlSchematron));    if (ret == NULL) {        xmlSchematronPErrMemory(ctxt, "allocating schema", NULL);        return (NULL);    }    memset(ret, 0, sizeof(xmlSchematron));    ret->dict = ctxt->dict;    xmlDictReference(ret->dict);    return (ret);}/** * xmlSchematronFree: * @schema:  a schema structure * * Deallocate a Schematron structure. */voidxmlSchematronFree(xmlSchematronPtr schema){    if (schema == NULL)        return;    if ((schema->doc != NULL) && (!(schema->preserve)))        xmlFreeDoc(schema->doc);    if (schema->namespaces != NULL)        xmlFree((char **) schema->namespaces);        xmlSchematronFreeRules(schema->rules);    xmlSchematronFreePatterns(schema->patterns);    xmlDictFree(schema->dict);    xmlFree(schema);}/** * xmlSchematronNewParserCtxt: * @URL:  the location of the schema * * Create an XML Schematrons parse context for that file/resource expected * to contain an XML Schematrons file. * * Returns the parser context or NULL in case of error */xmlSchematronParserCtxtPtrxmlSchematronNewParserCtxt(const char *URL){    xmlSchematronParserCtxtPtr ret;    if (URL == NULL)        return (NULL);    ret =        (xmlSchematronParserCtxtPtr)        xmlMalloc(sizeof(xmlSchematronParserCtxt));    if (ret == NULL) {        xmlSchematronPErrMemory(NULL, "allocating schema parser context",                                NULL);        return (NULL);    }    memset(ret, 0, sizeof(xmlSchematronParserCtxt));    ret->type = XML_STRON_CTXT_PARSER;    ret->dict = xmlDictCreate();    ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);    ret->includes = NULL;    ret->xctxt = xmlXPathNewContext(NULL);    if (ret->xctxt == NULL) {        xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",                                NULL);	xmlSchematronFreeParserCtxt(ret);        return (NULL);    }    ret->xctxt->flags = XML_XPATH_CHECKNS;    return (ret);}/** * xmlSchematronNewMemParserCtxt: * @buffer:  a pointer to a char array containing the schemas * @size:  the size of the array * * Create an XML Schematrons parse context for that memory buffer expected * to contain an XML Schematrons file. * * Returns the parser context or NULL in case of error */xmlSchematronParserCtxtPtrxmlSchematronNewMemParserCtxt(const char *buffer, int size){    xmlSchematronParserCtxtPtr ret;    if ((buffer == NULL) || (size <= 0))        return (NULL);    ret =        (xmlSchematronParserCtxtPtr)        xmlMalloc(sizeof(xmlSchematronParserCtxt));    if (ret == NULL) {        xmlSchematronPErrMemory(NULL, "allocating schema parser context",                                NULL);        return (NULL);    }    memset(ret, 0, sizeof(xmlSchematronParserCtxt));    ret->buffer = buffer;    ret->size = size;    ret->dict = xmlDictCreate();    ret->xctxt = xmlXPathNewContext(NULL);    if (ret->xctxt == NULL) {        xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",                                NULL);	xmlSchematronFreeParserCtxt(ret);        return (NULL);    }    return (ret);}/** * xmlSchematronNewDocParserCtxt: * @doc:  a preparsed document tree * * Create an XML Schematrons parse context for that document. * NB. The document may be modified during the parsing process. * * Returns the parser context or NULL in case of error */xmlSchematronParserCtxtPtrxmlSchematronNewDocParserCtxt(xmlDocPtr doc){    xmlSchematronParserCtxtPtr ret;    if (doc == NULL)        return (NULL);    ret =        (xmlSchematronParserCtxtPtr)        xmlMalloc(sizeof(xmlSchematronParserCtxt));    if (ret == NULL) {        xmlSchematronPErrMemory(NULL, "allocating schema parser context",                                NULL);        return (NULL);    }    memset(ret, 0, sizeof(xmlSchematronParserCtxt));    ret->doc = doc;    ret->dict = xmlDictCreate();    /* The application has responsibility for the document */    ret->preserve = 1;    ret->xctxt = xmlXPathNewContext(doc);    if (ret->xctxt == NULL) {        xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",                                NULL);	xmlSchematronFreeParserCtxt(ret);        return (NULL);    }    return (ret);}/** * xmlSchematronFreeParserCtxt: * @ctxt:  the schema parser context * * Free the resources associated to the schema parser context */voidxmlSchematronFreeParserCtxt(xmlSchematronParserCtxtPtr ctxt){    if (ctxt == NULL)        return;    if (ctxt->doc != NULL && !ctxt->preserve)        xmlFreeDoc(ctxt->doc);    if (ctxt->xctxt != NULL) {        xmlXPathFreeContext(ctxt->xctxt);    }    if (ctxt->namespaces != NULL)        xmlFree((char **) ctxt->namespaces);    xmlDictFree(ctxt->dict);    xmlFree(ctxt);}#if 0/** * xmlSchematronPushInclude: * @ctxt:  the schema parser context * @doc:  the included document * @cur:  the current include node * * Add an included document */static voidxmlSchematronPushInclude(xmlSchematronParserCtxtPtr ctxt,                        xmlDocPtr doc, xmlNodePtr cur){    if (ctxt->includes == NULL) {        ctxt->maxIncludes = 10;        ctxt->includes = (xmlNodePtr *)	    xmlMalloc(ctxt->maxIncludes * 2 * sizeof(xmlNodePtr));	if (ctxt->includes == NULL) {	    xmlSchematronPErrMemory(NULL, "allocating parser includes",				    NULL);	    return;	}        ctxt->nbIncludes = 0;    } else if (ctxt->nbIncludes + 2 >= ctxt->maxIncludes) {        xmlNodePtr *tmp;	tmp = (xmlNodePtr *)	    xmlRealloc(ctxt->includes, ctxt->maxIncludes * 4 *	               sizeof(xmlNodePtr));	if (tmp == NULL) {	    xmlSchematronPErrMemory(NULL, "allocating parser includes",				    NULL);	    return;	}        ctxt->includes = tmp;	ctxt->maxIncludes *= 2;    }    ctxt->includes[2 * ctxt->nbIncludes] = cur;    ctxt->includes[2 * ctxt->nbIncludes + 1] = (xmlNodePtr) doc;    ctxt->nbIncludes++;}/** * xmlSchematronPopInclude: * @ctxt:  the schema parser context * * Pop an include level. The included document is being freed * * Returns the node immediately following the include or NULL if the *         include list was empty. */static xmlNodePtrxmlSchematronPopInclude(xmlSchematronParserCtxtPtr ctxt){    xmlDocPtr doc;    xmlNodePtr ret;    if (ctxt->nbIncludes <= 0)        return(NULL);    ctxt->nbIncludes--;    doc = (xmlDocPtr) ctxt->includes[2 * ctxt->nbIncludes + 1];    ret = ctxt->includes[2 * ctxt->nbIncludes];    xmlFreeDoc(doc);    if (ret != NULL)	ret = ret->next;    if (ret == NULL)        return(xmlSchematronPopInclude(ctxt));    return(ret);}#endif/** * xmlSchematronAddNamespace: * @ctxt:  the schema parser context * @prefix:  the namespace prefix * @ns:  the namespace name * * Add a namespace definition in the context */static voidxmlSchematronAddNamespace(xmlSchematronParserCtxtPtr ctxt,                          const xmlChar *prefix, const xmlChar *ns){    if (ctxt->namespaces == NULL) {        ctxt->maxNamespaces = 10;        ctxt->namespaces = (const xmlChar **)	    xmlMalloc(ctxt->maxNamespaces * 2 * sizeof(const xmlChar *));	if (ctxt->namespaces == NULL) {	    xmlSchematronPErrMemory(NULL, "allocating parser namespaces",				    NULL);	    return;	}        ctxt->nbNamespaces = 0;    } else if (ctxt->nbNamespaces + 2 >= ctxt->maxNamespaces) {        const xmlChar **tmp;	tmp = (const xmlChar **)	    xmlRealloc((xmlChar **) ctxt->namespaces, ctxt->maxNamespaces * 4 *	               sizeof(const xmlChar *));	if (tmp == NULL) {	    xmlSchematronPErrMemory(NULL, "allocating parser namespaces",				    NULL);	    return;	}        ctxt->namespaces = tmp;	ctxt->maxNamespaces *= 2;    }    ctxt->namespaces[2 * ctxt->nbNamespaces] =         xmlDictLookup(ctxt->dict, ns, -1);    ctxt->namespaces[2 * ctxt->nbNamespaces + 1] =         xmlDictLookup(ctxt->dict, prefix, -1);    ctxt->nbNamespaces++;    ctxt->namespaces[2 * ctxt->nbNamespaces] = NULL;    ctxt->namespaces[2 * ctxt->nbNamespaces + 1] = NULL;}/** * xmlSchematronParseRule: * @ctxt:  a schema validation context * @rule:  the rule node * * parse a rule element */static voidxmlSchematronParseRule(xmlSchematronParserCtxtPtr ctxt,                       xmlSchematronPatternPtr pattern,		       xmlNodePtr rule){    xmlNodePtr cur;    int nbChecks = 0;    xmlChar *test;    xmlChar *context;    xmlChar *report;    xmlSchematronRulePtr ruleptr;    xmlSchematronTestPtr testptr;    if ((ctxt == NULL) || (rule == NULL)) return;    context = xmlGetNoNsProp(rule, BAD_CAST "context");    if (context == NULL) {	xmlSchematronPErr(ctxt, rule,	    XML_SCHEMAP_NOROOT,	    "rule has no context attribute",	    NULL, NULL);	return;    } else if (context[0] == 0) {	xmlSchematronPErr(ctxt, rule,	    XML_SCHEMAP_NOROOT,	    "rule has an empty context attribute",	    NULL, NULL);	xmlFree(context);

⌨️ 快捷键说明

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