Tests: Consider lttng mi namespace in test_mi
[lttng-tools.git] / tests / regression / tools / mi / extract_xml.c
index 724e005f4e128da61c4ff41cc2ff7a5374581e06..ddf8c16773331b9786b2018c4cbf2d7fb5a651e5 100644 (file)
  */
 
 /*
- * Usage: extract_xml [-v] xml_path xpath_expression
+ * Usage: extract_xml [-v|-e] xml_path xpath_expression
  * Evaluate XPath expression and prints result node set.
  * args[1] path to the xml file
  * args[2] xpath expression to extract
+ * If -e look if node exist return "true" else nothing
  * If -v is set the name of the node will appear with his value delimited by
  * a semicolon(;)
  * Ex:
 #include <libxml/parser.h>
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
+#include <common/defaults.h>
 
 #if defined(LIBXML_XPATH_ENABLED)
 
 
 int opt_verbose;
+int node_exist;
+
 /**
  * print_xpath_nodes:
  * nodes:  the nodes set.
@@ -56,7 +60,7 @@ int opt_verbose;
  */
 static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
 {
-       int ret;
+       int ret = 0;
        int size;
        int i;
 
@@ -81,7 +85,9 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
                                if (xmlNodeIsText(cur->children)) {
                                        node_child_value_string = xmlNodeListGetString(doc,
                                                        cur->children, 1);
-                                       if (opt_verbose) {
+                                       if (node_exist) {
+                                               fprintf(output, "true\n");
+                                       } else if (opt_verbose) {
                                                fprintf(output, "%s;%s;\n", cur->name,
                                                                node_child_value_string);
                                        } else {
@@ -89,6 +95,20 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
                                                                node_child_value_string);
                                        }
                                        xmlFree(node_child_value_string);
+                               } else {
+                                       /* We don't want to print non-final element */
+                                       if (node_exist) {
+                                               fprintf(output, "true\n");
+                                       } else {
+                                               fprintf(stderr, "ERR:%s\n",
+                                                               "Xpath expression return non-final xml element");
+                                               ret = -1;
+                                               goto end;
+                                       }
+                               }
+                       } else {
+                               if (node_exist) {
+                                       fprintf(output, "true\n");
                                } else {
                                        /* We don't want to print non-final element */
                                        fprintf(stderr, "ERR:%s\n",
@@ -96,21 +116,16 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
                                        ret = -1;
                                        goto end;
                                }
-                       } else {
-                               /* We don't want to print non-final element */
-                               fprintf(stderr, "ERR:%s\n",
-                                               "Xpath expression return non-final xml element");
-                               ret = -1;
-                               goto end;
                        }
 
                } else {
                        cur = nodes->nodeTab[i];
-                       if (opt_verbose) {
+                       if (node_exist) {
+                               fprintf(output, "true\n");
+                       } else if (opt_verbose) {
                                fprintf(output, "%s;%s;\n", cur->parent->name, cur->content);
                        } else {
                                fprintf(output, "%s\n", cur->content);
-
                        }
                }
        }
@@ -121,6 +136,31 @@ end:
        return ret;
 }
 
+static int register_lttng_namespace(xmlXPathContextPtr xpathCtx)
+{
+       int ret;
+       xmlChar *prefix;
+       xmlChar *ns = NULL;
+
+       prefix = xmlCharStrdup("lttng");
+       if (!prefix) {
+               ret = -1;
+               goto end;
+       }
+
+       ns = xmlCharStrdup(DEFAULT_LTTNG_MI_NAMESPACE);
+       if (!ns) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = xmlXPathRegisterNs(xpathCtx, prefix, ns);
+       xmlFree(prefix);
+end:
+       xmlFree(ns);
+       return ret;
+}
+
 /*
  * Extract element corresponding to xpath
  * xml_path     The path to the xml file
@@ -133,6 +173,7 @@ end:
  */
 static int extract_xpath(const char *xml_path, const xmlChar *xpath)
 {
+       int ret;
        xmlDocPtr doc = NULL;
        xmlXPathContextPtr xpathCtx = NULL;
        xmlXPathObjectPtr xpathObj = NULL;
@@ -155,6 +196,15 @@ static int extract_xpath(const char *xml_path, const xmlChar *xpath)
                return -1;
        }
 
+       /* Register the LTTng MI namespace */
+       ret = register_lttng_namespace(xpathCtx);
+       if (ret) {
+               fprintf(stderr, "ERR: Could not register lttng namespace\n");
+               xmlXPathFreeContext(xpathCtx);
+               xmlFreeDoc(doc);
+               return -1;
+       }
+
        /* Evaluate xpath expression */
        xpathObj = xmlXPathEvalExpression(xpath, xpathCtx);
        if (!xpathObj) {
@@ -185,11 +235,14 @@ int main(int argc, char **argv)
        int opt;
 
        /* Parse command line and process file */
-       while ((opt = getopt(argc, argv, "v")) != -1) {
+       while ((opt = getopt(argc, argv, "ve")) != -1) {
                switch (opt) {
                case 'v':
                        opt_verbose = 1;
                        break;
+               case 'e':
+                       node_exist = 1;
+                       break;
                default:
                        abort();
                }
This page took 0.025254 seconds and 4 git commands to generate.