diff options
Diffstat (limited to 'worker/xml_helper.c')
-rw-r--r-- | worker/xml_helper.c | 75 |
1 files changed, 49 insertions, 26 deletions
diff --git a/worker/xml_helper.c b/worker/xml_helper.c index bf6672e..1680d3b 100644 --- a/worker/xml_helper.c +++ b/worker/xml_helper.c @@ -38,7 +38,7 @@ /* If a default namespace is defined * * IMPORTANT: XPath 1.0 has no concept of a default namespace. Unprefixed - * names in XPath only match names which have no namespace. So, if the + * names in XPath only match names which have no namespace. So, if the * document uses a default namespace, it is required to associate a non-empty * prefix with the default namespace via register-namespace and add that * prefix to names in XPath expressions intended to match nodes in the default @@ -46,10 +46,54 @@ */ xmlChar *default_namespace_prefix = (xmlChar *) "def"; + +/** + * \brief Validate a XML document with the help of an RELAX NG file + * + * An already parsed XML ducument can be validated against a RELAX NG schema + * file. + * + * \param doc pointer to the already parsed XML document + * \param rng_file_name the name of the RELAX NG shcema file + * + * \return 0 if the XML document is valid with respect to the RELAX NG schema + * file, -1 if an error occured or the document is not valid. + * + */ +int validate_with_rng(xmlDocPtr doc, const char *rng_file_name) { + int ret=-1; + + xmlRelaxNGParserCtxtPtr rng_parser_context=NULL; + xmlRelaxNGPtr rng_schema=NULL; + xmlRelaxNGValidCtxtPtr rng_context=NULL; + + rng_parser_context = xmlRelaxNGNewParserCtxt(rng_file_name); + + CHECK(rng_parser_context, NULL, ("Failed to parse RNG file\n"), goto cleanup); + rng_schema = xmlRelaxNGParse(rng_parser_context); + CHECK(rng_schema, NULL, ("Failed to create RNG schema\n"), goto cleanup); + rng_context = xmlRelaxNGNewValidCtxt(rng_schema); + CHECK(rng_context, NULL, ("Failed to create RNG context\n"), goto cleanup); + if (xmlRelaxNGValidateDoc(rng_context, doc) == 0) { + DEBUG(3, ("The document is valid.\n")); + ret=0; + } else { + DEBUG(0, ("Error during validation.\n")); + goto cleanup; + } + +cleanup: + xmlRelaxNGFreeValidCtxt(rng_context); + xmlRelaxNGFree(rng_schema); + xmlRelaxNGFreeParserCtxt(rng_parser_context); + + return ret; +} + /** * \brief Validate a XML policy file * - * Call this function bedore any further processing of a XML policy file. It + * Call this function before any further processing of a XML policy file. It * will extract the name of the RELAX NG schema file from the metadata section * together with other information and validate the file accordingly. * @@ -65,11 +109,9 @@ xmlChar *default_namespace_prefix = (xmlChar *) "def"; */ int validate_policy(const char *policy_file_name, char **ipa_policy_type, char **xslt_file_name) { + int ret; xmlDocPtr doc=NULL; char *rng_file_name=NULL; - xmlRelaxNGParserCtxtPtr rng_parser_context=NULL; - xmlRelaxNGPtr rng_schema=NULL; - xmlRelaxNGValidCtxtPtr rng_context=NULL; xmlChar xpath_expr[XMLCHARLEN]; doc = xmlParseFile(policy_file_name); @@ -82,21 +124,8 @@ int validate_policy(const char *policy_file_name, char **ipa_policy_type, char * CHECK(rng_file_name, NULL, ("Name of RELANX NG schema file not found.\n"), goto failed); DEBUG(3, ("Found name of RELAX NG schema file: %s\n", rng_file_name)); - - /* validate the document */ - rng_parser_context = xmlRelaxNGNewParserCtxt(rng_file_name); - CHECK(rng_parser_context, NULL, ("Failed to parse RNG file\n"), goto failed); - rng_schema = xmlRelaxNGParse(rng_parser_context); - CHECK(rng_schema, NULL, ("Failed to create RNG schema\n"), goto failed); - rng_context = xmlRelaxNGNewValidCtxt(rng_schema); - CHECK(rng_context, NULL, ("Failed to create RNG context\n"), goto failed); - if (xmlRelaxNGValidateDoc(rng_context, doc) == 0) { - DEBUG(3, ("The document is valid.\n")); - } else { - DEBUG(0, ("Error during validation.\n")); - goto failed; - } - + ret=validate_with_rng(doc, rng_file_name); + CHECK(ret, -1, ("Validation failed for %s\n", policy_file_name), goto failed); xmlStrPrintf(xpath_expr, XMLCHARLEN, (xmlChar *) "//%s:ipa/*[2]", default_namespace_prefix); @@ -118,9 +147,6 @@ int validate_policy(const char *policy_file_name, char **ipa_policy_type, char * DEBUG(3, ("Found name of XSLT file: %s\n", *xslt_file_name)); } - xmlRelaxNGFreeValidCtxt(rng_context); - xmlRelaxNGFree(rng_schema); - xmlRelaxNGFreeParserCtxt(rng_parser_context); free(rng_file_name); xmlFreeDoc(doc); @@ -128,9 +154,6 @@ int validate_policy(const char *policy_file_name, char **ipa_policy_type, char * return 0; failed: - xmlRelaxNGFreeValidCtxt(rng_context); - xmlRelaxNGFree(rng_schema); - xmlRelaxNGFreeParserCtxt(rng_parser_context); free(rng_file_name); xmlFreeDoc(doc); |