2 * Generic XML helper functions
3 * Copyright (c) 2012-2013, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "xml-utils.h"
15 static xml_node_t * get_node_uri_iter(struct xml_node_ctx *ctx,
16 xml_node_t *root, char *uri)
22 end = strchr(uri, '/');
27 xml_node_for_each_sibling(ctx, node) {
28 xml_node_for_each_check(ctx, node);
29 name = xml_node_get_localname(ctx, node);
30 if (strcasecmp(name, uri) == 0)
38 return get_node_uri_iter(ctx, xml_node_first_child(ctx, node),
46 xml_node_t * get_node_uri(struct xml_node_ctx *ctx, xml_node_t *root,
52 search = os_strdup(uri);
56 node = get_node_uri_iter(ctx, root, search);
63 static xml_node_t * get_node_iter(struct xml_node_ctx *ctx,
64 xml_node_t *root, const char *path)
70 end = os_strchr(path, '/');
74 xml_node_for_each_child(ctx, node, root) {
75 xml_node_for_each_check(ctx, node);
76 name = xml_node_get_localname(ctx, node);
77 if (os_strcasecmp(name, path) == 0)
84 return get_node_iter(ctx, node, end);
89 xml_node_t * get_node(struct xml_node_ctx *ctx, xml_node_t *root,
95 search = os_strdup(path);
99 node = get_node_iter(ctx, root, search);
106 xml_node_t * get_child_node(struct xml_node_ctx *ctx, xml_node_t *root,
112 xml_node_for_each_child(ctx, node, root) {
113 xml_node_for_each_check(ctx, node);
114 match = get_node(ctx, node, path);
123 xml_node_t * node_from_file(struct xml_node_ctx *ctx, const char *name)
126 char *buf, *buf2, *start;
129 buf = os_readfile(name, &len);
132 buf2 = os_realloc(buf, len + 1);
140 start = os_strstr(buf, "<!DOCTYPE ");
142 char *pos = start + 1;
147 else if (*pos == '>') {
157 /* Remove DOCTYPE to allow the file to be parsed */
158 os_memset(start, ' ', pos - start);
162 node = xml_node_from_buf(ctx, buf);
169 int node_to_file(struct xml_node_ctx *ctx, const char *fname, xml_node_t *node)
174 str = xml_node_to_str(ctx, node);
178 f = fopen(fname, "w");
184 fprintf(f, "%s\n", str);
192 static char * get_val(struct xml_node_ctx *ctx, xml_node_t *node)
196 val = xml_node_get_text(ctx, node);
201 if (*pos != ' ' && *pos != '\t' && *pos != '\r' && *pos != '\n')
210 static char * add_path(const char *prev, const char *leaf)
218 len = os_strlen(prev) + 1 + os_strlen(leaf) + 1;
219 new_uri = os_malloc(len);
221 os_snprintf(new_uri, len, "%s/%s", prev, leaf);
227 static void node_to_tnds(struct xml_node_ctx *ctx, xml_node_t *out,
228 xml_node_t *in, const char *uri)
236 xml_node_for_each_child(ctx, node, in) {
237 xml_node_for_each_check(ctx, node);
238 name = xml_node_get_localname(ctx, node);
240 tnds = xml_node_create(ctx, out, NULL, "Node");
243 xml_node_create_text(ctx, tnds, NULL, "NodeName", name);
246 xml_node_create_text(ctx, tnds, NULL, "Path", uri);
248 val = get_val(ctx, node);
250 xml_node_create_text(ctx, tnds, NULL, "Value", val);
251 xml_node_get_text_free(ctx, val);
254 new_uri = add_path(uri, name);
255 node_to_tnds(ctx, new_uri ? out : tnds, node, new_uri);
261 static int add_ddfname(struct xml_node_ctx *ctx, xml_node_t *parent,
266 node = xml_node_create(ctx, parent, NULL, "RTProperties");
269 node = xml_node_create(ctx, node, NULL, "Type");
272 xml_node_create_text(ctx, node, NULL, "DDFName", urn);
277 xml_node_t * mo_to_tnds(struct xml_node_ctx *ctx, xml_node_t *mo,
278 int use_path, const char *urn, const char *ns_uri)
284 root = xml_node_create_root(ctx, ns_uri, NULL, NULL, "MgmtTree");
288 xml_node_create_text(ctx, root, NULL, "VerDTD", "1.2");
290 name = xml_node_get_localname(ctx, mo);
292 node = xml_node_create(ctx, root, NULL, "Node");
295 xml_node_create_text(ctx, node, NULL, "NodeName", name);
297 add_ddfname(ctx, node, urn);
299 node_to_tnds(ctx, use_path ? root : node, mo, use_path ? name : NULL);
304 xml_node_free(ctx, root);
309 static xml_node_t * get_first_child_node(struct xml_node_ctx *ctx,
316 xml_node_for_each_child(ctx, child, node) {
317 xml_node_for_each_check(ctx, child);
318 lname = xml_node_get_localname(ctx, child);
319 if (os_strcasecmp(lname, name) == 0)
327 static char * get_node_text(struct xml_node_ctx *ctx, xml_node_t *node,
328 const char *node_name)
330 node = get_first_child_node(ctx, node, node_name);
333 return xml_node_get_text(ctx, node);
337 static xml_node_t * add_mo_node(struct xml_node_ctx *ctx, xml_node_t *root,
338 xml_node_t *node, const char *uri)
340 char *nodename, *value, *path;
343 nodename = get_node_text(ctx, node, "NodeName");
344 if (nodename == NULL)
346 value = get_node_text(ctx, node, "Value");
349 root = xml_node_create_root(ctx, NULL, NULL, NULL,
352 xml_node_set_text(ctx, root, value);
355 xml_node_get_text_free(ctx, nodename);
356 xml_node_get_text_free(ctx, value);
359 path = get_node_text(ctx, node, "Path");
362 parent = get_node_uri(ctx, root, uri);
363 xml_node_get_text_free(ctx, path);
364 if (parent == NULL) {
365 printf("Could not find URI '%s'\n", uri);
366 xml_node_get_text_free(ctx, nodename);
367 xml_node_get_text_free(ctx, value);
371 xml_node_create_text(ctx, parent, NULL, nodename,
374 xml_node_create(ctx, parent, NULL, nodename);
377 xml_node_get_text_free(ctx, nodename);
378 xml_node_get_text_free(ctx, value);
384 static xml_node_t * tnds_to_mo_iter(struct xml_node_ctx *ctx, xml_node_t *root,
385 xml_node_t *node, const char *uri)
391 xml_node_for_each_sibling(ctx, node) {
392 xml_node_for_each_check(ctx, node);
394 nodename = get_node_text(ctx, node, "NodeName");
395 if (nodename == NULL)
398 name = xml_node_get_localname(ctx, node);
399 if (strcmp(name, "Node") == 0) {
401 printf("Invalid TNDS tree structure - "
402 "multiple top level nodes\n");
403 xml_node_get_text_free(ctx, nodename);
406 root = add_mo_node(ctx, root, node, uri);
409 child = get_first_child_node(ctx, node, "Node");
412 tnds_to_mo_iter(ctx, root, child, nodename);
415 new_uri = add_path(uri, nodename);
416 tnds_to_mo_iter(ctx, root, child, new_uri);
420 xml_node_get_text_free(ctx, nodename);
427 xml_node_t * tnds_to_mo(struct xml_node_ctx *ctx, xml_node_t *tnds)
432 name = xml_node_get_localname(ctx, tnds);
433 if (name == NULL || os_strcmp(name, "MgmtTree") != 0)
436 node = get_first_child_node(ctx, tnds, "Node");
439 return tnds_to_mo_iter(ctx, NULL, node, NULL);
443 xml_node_t * soap_build_envelope(struct xml_node_ctx *ctx, xml_node_t *node)
445 xml_node_t *envelope, *body;
448 envelope = xml_node_create_root(
449 ctx, "http://www.w3.org/2003/05/soap-envelope", "soap12", &ns,
451 if (envelope == NULL)
453 body = xml_node_create(ctx, envelope, ns, "Body");
454 xml_node_add_child(ctx, body, node);
459 xml_node_t * soap_get_body(struct xml_node_ctx *ctx, xml_node_t *soap)
461 xml_node_t *body, *child;
463 body = get_node_uri(ctx, soap, "Envelope/Body");
466 xml_node_for_each_child(ctx, child, body) {
467 xml_node_for_each_check(ctx, child);