From 1be6a957fa90294f982f9e8531a05c86c49028fb Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Thu, 20 Nov 2008 22:37:14 +0100 Subject: removed default_namespace from most parameter lists --- worker/xml_helper.c | 65 ++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) (limited to 'worker/xml_helper.c') diff --git a/worker/xml_helper.c b/worker/xml_helper.c index 6d91e13..35932e8 100644 --- a/worker/xml_helper.c +++ b/worker/xml_helper.c @@ -33,8 +33,6 @@ xmlChar *default_namespace_prefix = (xmlChar *) "def"; * together with other information and validate the file accordingly. * * \param policy_file_name name of the XML policy file - * \param default_namespace will contain the default namespace of the XML - * policy file if the function returns successfully * \param ipa_policy_type will contain the IPA policy type, i.e. action, * config or role, if the function returns successfully * \param xslt_file_name will contain the name of the XSLT file if the IPA @@ -45,36 +43,19 @@ xmlChar *default_namespace_prefix = (xmlChar *) "def"; * */ -int validate_policy(const char *policy_file_name, xmlChar **default_namespace, char **ipa_policy_type, char **xslt_file_name) { +int validate_policy(const char *policy_file_name, char **ipa_policy_type, char **xslt_file_name) { xmlDocPtr doc; - xmlNodePtr root_node; char *rng_file_name; xmlRelaxNGValidCtxtPtr rng_context; xmlChar xpath_expr[XMLCHARLEN]; - doc = xmlParseFile(policy_file_name); CHECK(doc, NULL, ("Cannot parse document %s!\n", policy_file_name), exit(1)); - /* find the default namespace */ - root_node = xmlDocGetRootElement(doc); - CHECK(root_node, NULL, - ("Cannot find root node of document %s!\n", policy_file_name), exit(1)); - if (xmlStrncasecmp(root_node->name, (xmlChar *) "IPA", XMLCHARLEN) != 0) { - DEBUG(0, - ("Name of root node of document %s has to be 'ipa'!\n", policy_file_name)); - exit(1); - } - CHECK(root_node->ns->href, NULL, - ("Root node of document %s must define a namespace!\n", policy_file_name), exit(1)); - *default_namespace = xmlStrndup(root_node->ns->href, XMLCHARLEN); - CHECK(*default_namespace, NULL, ("Cannot copy namespace!\n"), exit(1)); - DEBUG(3, ("Default namespace of %s is %s\n", policy_file_name, *default_namespace)); - xmlStrPrintf(xpath_expr, XMLCHARLEN, (xmlChar *) "//%s:ipa/*[2]", default_namespace_prefix); - *ipa_policy_type = find_by_xpath(doc, xpath_expr, FIND_NAME, default_namespace_prefix, *default_namespace); + *ipa_policy_type = find_by_xpath(doc, xpath_expr, FIND_NAME); CHECK(*ipa_policy_type, NULL, ("Type of IPA policy not found.\n"), exit(1)); DEBUG(3, ("Found IPA policy type: %s\n", *ipa_policy_type)); if ( strncmp(*ipa_policy_type, "ipaconfig",9) != 0 && @@ -87,8 +68,7 @@ int validate_policy(const char *policy_file_name, xmlChar **default_namespace, c xmlStrPrintf(xpath_expr, XMLCHARLEN, (xmlChar *) "//%s:RNGfile", default_namespace_prefix); rng_file_name = - find_by_xpath(doc, xpath_expr, FIND_VALUE, default_namespace_prefix, - *default_namespace); + find_by_xpath(doc, xpath_expr, FIND_VALUE); CHECK(rng_file_name, NULL, ("Name of RELANX NG schema file not found.\n"), exit(1)); DEBUG(3, ("Found name of RELAX NG schema file: %s\n", rng_file_name)); @@ -113,7 +93,7 @@ int validate_policy(const char *policy_file_name, xmlChar **default_namespace, c if (strncmp(*ipa_policy_type, "ipaaction", 9)!=0) { xmlStrPrintf(xpath_expr, XMLCHARLEN, (xmlChar *) "//%s:XSLTfile", default_namespace_prefix); *xslt_file_name = - find_by_xpath(doc, xpath_expr, FIND_VALUE, default_namespace_prefix, *default_namespace); + find_by_xpath(doc, xpath_expr, FIND_VALUE); CHECK(*xslt_file_name, NULL, ("Name of XSLT file not found.\n"), exit(1)); DEBUG(3, ("Found name of XSLT file: %s\n", *xslt_file_name)); } @@ -145,6 +125,28 @@ int print_all_attributes(const xmlNode *node) { return 0; } + +xmlChar *get_default_namespace(xmlDocPtr doc) { + xmlNodePtr root_node; + xmlChar *default_namespace; + + root_node = xmlDocGetRootElement(doc); + CHECK(root_node, NULL, + ("Cannot find root node of the current document!\n"), return NULL); + if (xmlStrncasecmp(root_node->name, (xmlChar *) "IPA", XMLCHARLEN) != 0) { + DEBUG(0, + ("Name of root node of the current document has to be 'ipa'!\n")); + exit(1); + } + CHECK(root_node->ns->href, NULL, + ("Root node of the current document must define a namespace!\n"), return NULL); + default_namespace = xmlStrndup(root_node->ns->href, XMLCHARLEN); + CHECK(default_namespace, NULL, ("Cannot copy namespace!\n"), return NULL); + DEBUG(3, ("Default namespace is %s\n", default_namespace)); + + return default_namespace; +} + /** * \brief find a single name or value defined by a XPath expression * @@ -156,29 +158,30 @@ int print_all_attributes(const xmlNode *node) { * \param xpath_expr a XPath expression describing the node to search for * \param type use FIND_NAME to return the name and FIND_VALUE to return the * value of the node - * \param prefix prefix of the namespace of the node to search for - * \param namespare namespace URI of the node to search for * * \return pointer to the found string or NULL in case of an error * */ -char *find_by_xpath(const xmlDocPtr doc, const xmlChar * xpath_expr, const int type, const xmlChar * prefix, - const xmlChar * namespace) +char *find_by_xpath(const xmlDocPtr doc, const xmlChar * xpath_expr, const int type) { xmlXPathContextPtr xpath_context; xmlXPathObjectPtr xpath_obj; char *result = NULL; + xmlChar *namespace; + + namespace = get_default_namespace(doc); + CHECK(namespace, NULL, ("No default namespace found.\n"), return NULL); /* Create xpath evaluation context */ xpath_context = xmlXPathNewContext(doc); CHECK_NULL_FATAL(xpath_context, ("Error: unable to create new XPath context\n")); /* Register a namespace */ - if (xmlXPathRegisterNs(xpath_context, prefix, namespace) != 0) { + if (xmlXPathRegisterNs(xpath_context, default_namespace_prefix, namespace) != 0) { DEBUG(0, ("Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", - prefix , namespace)); + default_namespace_prefix , namespace)); xmlXPathFreeContext(xpath_context); return NULL; } @@ -218,7 +221,7 @@ char *find_by_xpath(const xmlDocPtr doc, const xmlChar * xpath_expr, const int t } } - + xmlFree(namespace); xmlXPathFreeObject(xpath_obj); xmlXPathFreeContext(xpath_context); return result; -- cgit