X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=tests%2Fregression%2Ftools%2Fmi%2Fextract_xml.c;h=ddf8c16773331b9786b2018c4cbf2d7fb5a651e5;hp=724e005f4e128da61c4ff41cc2ff7a5374581e06;hb=41af1adf5100627c2e11565e7d9e4b9c5a8b9fac;hpb=68270f0f604eefdc89583950a7cfa02fe7a0cab5 diff --git a/tests/regression/tools/mi/extract_xml.c b/tests/regression/tools/mi/extract_xml.c index 724e005f4..ddf8c1677 100644 --- a/tests/regression/tools/mi/extract_xml.c +++ b/tests/regression/tools/mi/extract_xml.c @@ -15,10 +15,11 @@ */ /* - * 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: @@ -42,11 +43,14 @@ #include #include #include +#include #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(); }