2 * xml.c: the implementation of aug_to_xml and supporting functions
4 * Copyright (C) 2017 David Lutterkort
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Author: David Lutterkort <lutter@watzmann.net>
30 #include <libxml/tree.h>
32 static int to_xml_span(xmlNodePtr elem, const char *pfor, int start, int end) {
38 span_elem = xmlNewChild(elem, NULL, BAD_CAST "span", NULL);
39 if (span_elem == NULL)
42 prop = xmlSetProp(span_elem, BAD_CAST "for", BAD_CAST pfor);
46 /* Format and set the start property */
47 r = xasprintf(&buf, "%d", start);
51 prop = xmlSetProp(span_elem, BAD_CAST "start", BAD_CAST buf);
56 /* Format and set the end property */
57 r = xasprintf(&buf, "%d", end);
61 prop = xmlSetProp(span_elem, BAD_CAST "end", BAD_CAST buf);
69 static int to_xml_one(xmlNodePtr elem, const struct tree *tree,
75 prop = xmlSetProp(elem, BAD_CAST "label", BAD_CAST tree->label);
80 struct span *span = tree->span;
82 prop = xmlSetProp(elem, BAD_CAST "file",
83 BAD_CAST span->filename->str);
87 r = to_xml_span(elem, "label", span->label_start, span->label_end);
91 r = to_xml_span(elem, "value", span->value_start, span->value_end);
95 r = to_xml_span(elem, "node", span->span_start, span->span_end);
100 if (pathin != NULL) {
101 prop = xmlSetProp(elem, BAD_CAST "path", BAD_CAST pathin);
105 if (tree->value != NULL) {
106 value = xmlNewTextChild(elem, NULL, BAD_CAST "value",
107 BAD_CAST tree->value);
116 static int to_xml_rec(xmlNodePtr pnode, struct tree *start,
117 const char *pathin) {
121 elem = xmlNewChild(pnode, NULL, BAD_CAST "node", NULL);
124 r = to_xml_one(elem, start, pathin);
128 list_for_each(tree, start->children) {
129 if (TREE_HIDDEN(tree))
131 r = to_xml_rec(elem, tree, NULL);
141 static int tree_to_xml(struct pathx *p, xmlNode **xml, const char *pathin) {
147 *xml = xmlNewNode(NULL, BAD_CAST "augeas");
150 expr = xmlSetProp(*xml, BAD_CAST "match", BAD_CAST pathin);
154 for (tree = pathx_first(p); tree != NULL; tree = pathx_next(p)) {
155 if (TREE_HIDDEN(tree))
157 path = path_of_tree(tree);
160 r = to_xml_rec(*xml, tree, path);
173 int aug_to_xml(const struct augeas *aug, const char *pathin,
174 xmlNode **xmldoc, unsigned int flags) {
175 struct pathx *p = NULL;
180 ARG_CHECK(flags != 0, aug, "aug_to_xml: FLAGS must be 0");
181 ARG_CHECK(xmldoc == NULL, aug, "aug_to_xml: XMLDOC must be non-NULL");
185 if (pathin == NULL || strlen(pathin) == 0 || strcmp(pathin, "/") == 0) {
189 p = pathx_aug_parse(aug, aug->origin, tree_root_ctx(aug), pathin, true);
191 result = tree_to_xml(p, xmldoc, pathin);
192 ERR_THROW(result < 0, aug, AUG_ENOMEM, NULL);