ad319d3c46c10c01cda3cebd62ba92ef8fbf9993
[lttng-tools.git] / tests / utils / xml-utils / extract_xml.cpp
1 /*
2 * Copyright (C) 2014 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 /*
9 * Usage: extract_xml [-v|-e] xml_path xpath_expression
10 * Evaluate XPath expression and prints result node set.
11 * args[1] path to the xml file
12 * args[2] xpath expression to extract
13 * If -e look if node exist return "true" else nothing
14 * If -v is set the name of the node will appear with his value delimited by
15 * a semicolon(;)
16 * Ex:
17 * Command:extract_xml ../file.xml /test/node/text()
18 * Output:
19 * a
20 * b
21 * c
22 * With -v
23 * node;a;
24 * node;b;
25 * node;c;
26 */
27 #include "common.hpp"
28
29 #include <common/defaults.hpp>
30
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xpath.h>
34 #include <libxml/xpathInternals.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #if defined(LIBXML_XPATH_ENABLED)
42
43 static int opt_verbose;
44 static int node_exist;
45 static bool result = false;
46
47 /**
48 * print_xpath_nodes:
49 * nodes: the nodes set.
50 * output: the output file handle.
51 *
52 * Print the node content to the file
53 */
54 static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
55 {
56 int ret = 0;
57 int size;
58 int i;
59
60 xmlNodePtr cur;
61 xmlChar *node_child_value_string = nullptr;
62
63 LTTNG_ASSERT(output);
64 size = (nodes) ? nodes->nodeNr : 0;
65
66 for (i = 0; i < size; ++i) {
67 LTTNG_ASSERT(nodes->nodeTab[i]);
68
69 if (nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
70 fprintf(stderr,
71 "ERR:%s\n",
72 "This executable does not support xml namespacing\n");
73 ret = -1;
74 goto end;
75 } else if (nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
76 cur = nodes->nodeTab[i];
77
78 if (xmlChildElementCount(cur) == 0) {
79 if (xmlNodeIsText(cur->children)) {
80 node_child_value_string =
81 xmlNodeListGetString(doc, cur->children, 1);
82 if (node_exist) {
83 result = true;
84 } else if (opt_verbose) {
85 fprintf(output,
86 "%s;%s;\n",
87 cur->name,
88 node_child_value_string);
89 } else {
90 fprintf(output, "%s\n", node_child_value_string);
91 }
92 xmlFree(node_child_value_string);
93 } else {
94 /* We don't want to print non-final element */
95 if (node_exist) {
96 result = true;
97 } else {
98 fprintf(stderr,
99 "ERR:%s\n",
100 "Xpath expression return non-final xml element");
101 ret = -1;
102 goto end;
103 }
104 }
105 } else {
106 if (node_exist) {
107 result = true;
108 } else {
109 /* We don't want to print non-final element */
110 fprintf(stderr,
111 "ERR:%s\n",
112 "Xpath expression return non-final xml element");
113 ret = -1;
114 goto end;
115 }
116 }
117
118 } else {
119 cur = nodes->nodeTab[i];
120 if (node_exist) {
121 result = true;
122 } else if (opt_verbose) {
123 fprintf(output, "%s;%s;\n", cur->parent->name, cur->content);
124 } else {
125 fprintf(output, "%s\n", cur->content);
126 }
127 }
128 }
129 /* Command Success */
130 ret = 0;
131
132 end:
133 return ret;
134 }
135
136 static int register_lttng_namespace(xmlXPathContextPtr xpathCtx)
137 {
138 int ret;
139 xmlChar *prefix;
140 xmlChar *ns = nullptr;
141
142 prefix = xmlCharStrdup("lttng");
143 if (!prefix) {
144 ret = -1;
145 goto end;
146 }
147
148 ns = xmlCharStrdup(DEFAULT_LTTNG_MI_NAMESPACE);
149 if (!ns) {
150 ret = -1;
151 goto end;
152 }
153
154 ret = xmlXPathRegisterNs(xpathCtx, prefix, ns);
155 end:
156 xmlFree(prefix);
157 xmlFree(ns);
158 return ret;
159 }
160
161 /*
162 * Extract element corresponding to xpath
163 * xml_path The path to the xml file
164 * xpath: The xpath to evaluate.
165 *
166 * Evaluate an xpath expression onto an xml file.
167 * and print the result one by line.
168 *
169 * Returns 0 on success and a negative value otherwise.
170 */
171 static int extract_xpath(const char *xml_path, const xmlChar *xpath)
172 {
173 int ret;
174 xmlDocPtr doc = nullptr;
175 xmlXPathContextPtr xpathCtx = nullptr;
176 xmlXPathObjectPtr xpathObj = nullptr;
177
178 LTTNG_ASSERT(xml_path);
179 LTTNG_ASSERT(xpath);
180
181 xml_parser_ctx_uptr parserCtx{ xmlNewParserCtxt() };
182
183 if (!parserCtx) {
184 fprintf(stderr, "ERR: could not allocate an XML parser context\n");
185 return -1;
186 }
187
188 /* Parse the xml file */
189 doc = xmlCtxtReadFile(parserCtx.get(), xml_path, nullptr, XML_PARSE_NOBLANKS);
190 if (!doc) {
191 fprintf(stderr, "ERR parsing: xml file invalid \"%s\"\n", xml_path);
192 return -1;
193 }
194
195 /* Initialize a xpath context */
196 xpathCtx = xmlXPathNewContext(doc);
197 if (!xpathCtx) {
198 fprintf(stderr, "ERR: XPath context invalid\n");
199 xmlFreeDoc(doc);
200 return -1;
201 }
202
203 /* Register the LTTng MI namespace */
204 ret = register_lttng_namespace(xpathCtx);
205 if (ret) {
206 fprintf(stderr, "ERR: Could not register lttng namespace\n");
207 xmlXPathFreeContext(xpathCtx);
208 xmlFreeDoc(doc);
209 return -1;
210 }
211
212 /* Evaluate xpath expression */
213 xpathObj = xmlXPathEvalExpression(xpath, xpathCtx);
214 if (!xpathObj) {
215 fprintf(stderr, "ERR: invalid xpath expression \"%s\"\n", xpath);
216 xmlXPathFreeContext(xpathCtx);
217 xmlFreeDoc(doc);
218 return -1;
219 }
220
221 /* Print results */
222 if (print_xpath_nodes(doc, xpathObj->nodesetval, stdout)) {
223 xmlXPathFreeObject(xpathObj);
224 xmlXPathFreeContext(xpathCtx);
225 xmlFreeDoc(doc);
226 return -1;
227 }
228 if (node_exist && result) {
229 fprintf(stdout, "true\n");
230 }
231
232 /* Cleanup */
233 xmlXPathFreeObject(xpathObj);
234 xmlXPathFreeContext(xpathCtx);
235 xmlFreeDoc(doc);
236
237 return 0;
238 }
239
240 int main(int argc, char **argv)
241 {
242 int opt;
243
244 /* Parse command line and process file */
245 while ((opt = getopt(argc, argv, "ve")) != -1) {
246 switch (opt) {
247 case 'v':
248 opt_verbose = 1;
249 break;
250 case 'e':
251 node_exist = 1;
252 break;
253 default:
254 abort();
255 }
256 }
257
258 if (!(optind + 1 < argc)) {
259 fprintf(stderr, "ERR:%s\n", "Arguments missing");
260 return -1;
261 }
262
263 /* Init libxml */
264 xmlInitParser();
265 if (access(argv[optind], F_OK)) {
266 fprintf(stderr, "ERR:%s\n", "Xml path not valid");
267 return -1;
268 }
269 /* Do the main job */
270 if (extract_xpath(argv[optind], (xmlChar *) argv[optind + 1])) {
271 return -1;
272 }
273
274 /* Shutdown libxml */
275 xmlCleanupParser();
276
277 return 0;
278 }
279
280 #else
281 int main(void)
282 {
283 fprintf(stderr, "XPath support not compiled in\n");
284 return -1;
285 }
286 #endif
This page took 0.03371 seconds and 3 git commands to generate.