From 98824689e3f11cac83e9ee785ecd76616490d21c Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Mon, 20 Oct 2008 11:14:54 +0200 Subject: added ipaaction example --- worker/worker.c | 119 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 48 deletions(-) (limited to 'worker') diff --git a/worker/worker.c b/worker/worker.c index 492fa91..23c3de3 100644 --- a/worker/worker.c +++ b/worker/worker.c @@ -17,65 +17,64 @@ #define XMLCHARLEN 255 /* 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 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 namespace. + * 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 + * 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 + * namespace. */ xmlChar *default_namespace_prefix = (xmlChar *) "def"; -char * find_value_by_xpath(xmlDocPtr doc, xmlChar * xpathExpr, xmlChar *prefix, xmlChar * namespace) +char *find_value_by_xpath(xmlDocPtr doc, xmlChar * xpathExpr, + xmlChar * prefix, xmlChar * namespace) { xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj; - char *result=NULL; - xmlNodeSetPtr nodeset; - int i; - xmlChar *str; - + char *result = NULL; + /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if (xpathCtx == NULL) { fprintf(stderr, "Error: unable to create new XPath context\n"); - return(NULL); + return (NULL); } /* Register a namespace */ - if (xmlXPathRegisterNs (xpathCtx, prefix, namespace) != 0) { + if (xmlXPathRegisterNs(xpathCtx, prefix, namespace) != 0) { fprintf(stderr, "Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", "my", namespace); xmlXPathFreeContext(xpathCtx); - return(NULL); + return (NULL); } /* Evaluate xpath expression */ xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); if (xpathObj == NULL) { fprintf(stderr, - "Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr); + "Error: unable to evaluate xpath expression \"%s\"\n", + xpathExpr); xmlXPathFreeContext(xpathCtx); - return(NULL); + return (NULL); } if (xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) { printf("Nothing found ...\n"); xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); - return(NULL); - } else if (xmlXPathNodeSetGetLength(xpathObj->nodesetval) != 1 ) { - fprintf(stderr,"More than one node found!"); + return (NULL); + } else if (xmlXPathNodeSetGetLength(xpathObj->nodesetval) != 1) { + fprintf(stderr, "More than one node found!"); xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); - return(NULL); + return (NULL); } else { - nodeset = xpathObj->nodesetval; -/* FIXME: only allow one found value */ - for (i = 0; i < nodeset->nodeNr; i++) { - str = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1); - result = strdup((char *) str); - xmlFree(str); - } + result = + (char *) xmlNodeListGetString(doc, + xpathObj->nodesetval-> + nodeTab[0]->xmlChildrenNode, 1); } @@ -89,7 +88,7 @@ int main(int argc, char **argv) { xmlDocPtr doc; - xmlNodePtr rootNode; + xmlNodePtr rootNode; xmlChar *default_namespace; xmlChar xpathExpr[XMLCHARLEN]; char *rngFileName; @@ -117,33 +116,44 @@ int main(int argc, char **argv) } /* find the default namespace */ - rootNode=xmlDocGetRootElement(doc); - if(rootNode==NULL) { - fprintf(stderr, "Cannot find root node of document %s!\n", argv[1]); + rootNode = xmlDocGetRootElement(doc); + if (rootNode == NULL) { + fprintf(stderr, "Cannot find root node of document %s!\n", + argv[1]); exit(1); } - if(xmlStrncasecmp(rootNode->name,(xmlChar *) "IPA", XMLCHARLEN) != 0) { - fprintf(stderr, "Name of root node of document %s has to be 'ipa'!\n", argv[1]); + if (xmlStrncasecmp(rootNode->name, (xmlChar *) "IPA", XMLCHARLEN) != 0) { + fprintf(stderr, + "Name of root node of document %s has to be 'ipa'!\n", + argv[1]); exit(1); } - if(rootNode->ns->href==NULL) { - fprintf(stderr, "Root node of document %s must define a namespace!\n", argv[1]); + if (rootNode->ns->href == NULL) { + fprintf(stderr, + "Root node of document %s must define a namespace!\n", + argv[1]); exit(1); } - default_namespace=xmlStrndup(rootNode->ns->href,XMLCHARLEN); - if(default_namespace==NULL) { + default_namespace = xmlStrndup(rootNode->ns->href, XMLCHARLEN); + if (default_namespace == NULL) { fprintf(stderr, "Cannot copy namespace!\n"); exit(1); } /* extract XSTLfile and RNGfile from document using XPath */ - xmlStrPrintf(xpathExpr, XMLCHARLEN, (xmlChar *) "//%s:XSLTfile", default_namespace_prefix); - xsltFileName=find_value_by_xpath(doc,xpathExpr, default_namespace_prefix, default_namespace); + xmlStrPrintf(xpathExpr, XMLCHARLEN, (xmlChar *) "//%s:XSLTfile", + default_namespace_prefix); + xsltFileName = + find_value_by_xpath(doc, xpathExpr, default_namespace_prefix, + default_namespace); printf("--%s--\n", xsltFileName); - xmlStrPrintf(xpathExpr, XMLCHARLEN, (xmlChar *) "//%s:RNGfile", default_namespace_prefix); - rngFileName=find_value_by_xpath(doc,xpathExpr, default_namespace_prefix, default_namespace); + xmlStrPrintf(xpathExpr, XMLCHARLEN, (xmlChar *) "//%s:RNGfile", + default_namespace_prefix); + rngFileName = + find_value_by_xpath(doc, xpathExpr, default_namespace_prefix, + default_namespace); printf("--%s--\n", rngFileName); - + /* validate the document */ @@ -172,14 +182,27 @@ int main(int argc, char **argv) exit(1); } - output_file_name=find_value_by_xpath(xsltDoc,(xmlChar *) "//md:output_file/@name",(xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0"); - output_file_owner=find_value_by_xpath(xsltDoc,(xmlChar *) "//md:output_file/@owner",(xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0"); - output_file_group=find_value_by_xpath(xsltDoc,(xmlChar *) "//md:output_file/@group",(xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0"); - output_file_permission=find_value_by_xpath(xsltDoc,(xmlChar *) "//md:output_file/@permission",(xmlChar *) "md", (xmlChar *) "http://freeipa.org/xsl/metadata/1.0"); - printf("-%s-\n",output_file_name); - printf("-%s-\n",output_file_owner); - printf("-%s-\n",output_file_group); - printf("-%s-\n",output_file_permission); + output_file_name = + find_value_by_xpath(xsltDoc, (xmlChar *) "//md:output_handler/file/@name", + (xmlChar *) "md", (xmlChar *) + "http://freeipa.org/xsl/metadata/1.0"); + output_file_owner = + find_value_by_xpath(xsltDoc, (xmlChar *) "//md:output_handler/file/@owner", + (xmlChar *) "md", (xmlChar *) + "http://freeipa.org/xsl/metadata/1.0"); + output_file_group = + find_value_by_xpath(xsltDoc, (xmlChar *) "//md:output_handler/file/@group", + (xmlChar *) "md", (xmlChar *) + "http://freeipa.org/xsl/metadata/1.0"); + output_file_permission = + find_value_by_xpath(xsltDoc, + (xmlChar *) "//md:output_handler/file/@permission", + (xmlChar *) "md", (xmlChar *) + "http://freeipa.org/xsl/metadata/1.0"); + printf("-%s-\n", output_file_name); + printf("-%s-\n", output_file_owner); + printf("-%s-\n", output_file_group); + printf("-%s-\n", output_file_permission); cur = xsltParseStylesheetDoc(xsltDoc); if (cur == NULL) { -- cgit