Util: xml_extract: add node_exist option
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Fri, 3 Apr 2015 21:15:43 +0000 (17:15 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 11 Aug 2015 19:18:40 +0000 (15:18 -0400)
The node_exist argument permit a more flexible way to extract informations
within test suites. We can check the presence of a node and not only list
the content of a final node.

Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/regression/tools/mi/extract_xml.c

index 724e005f4e128da61c4ff41cc2ff7a5374581e06..7edf1716eb979ad07da7ba35f48d8edddaa3b660 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:
@@ -47,6 +48,8 @@
 
 
 int opt_verbose;
+int node_exist;
+
 /**
  * print_xpath_nodes:
  * nodes:  the nodes set.
@@ -56,7 +59,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 +84,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 +94,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 +115,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);
-
                        }
                }
        }
@@ -185,11 +199,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.025902 seconds and 4 git commands to generate.