summaryrefslogtreecommitdiffstats
path: root/common/eurephia_xml.c
blob: 816791642e1fa8174652eed507553da740711632 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* eurephia_xml.c  --  Generic helper functions for XML parsing
 *
 *  GPLv2 - Copyright (C) 2008  David Sommerseth <dazo@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; version 2
 *  of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifdef HAVE_LIBXML2
#include <assert.h>

#include <libxml/tree.h>
#include <libxml/xmlstring.h>

#include <eurephia_nullsafe.h>
#include <eurephia_log.h>

char *xmlGetAttrValue(xmlAttr *attr, const char *key) {
        xmlAttr *aptr;

        for( aptr = attr; aptr != NULL; aptr = aptr->next ) {
                if( xmlStrcmp(aptr->name, (xmlChar *)key) == 0 ) {
                        return (char *)(aptr->children != NULL ? aptr->children->content : NULL);
                }
        }
        return NULL;
}

xmlNode *xmlFindNode(xmlNode *node, const char *key) {
        xmlNode *nptr = NULL;

        if( node->children == NULL ) {
                return NULL;
        }
        for( nptr = node->children; nptr != NULL; nptr = nptr->next ) {
                if( xmlStrcmp(nptr->name, (xmlChar *)key) == 0 ) {
                        return nptr;
                }
        }
        return NULL;
}


int eurephiaXML_CreateDoc(eurephiaCTX *ctx, int format, const char *eurephiaRoot,
                          xmlDoc **doc, xmlNode **root_n)
{
        char tmp[34];

        // Create a new XML document
        *doc = xmlNewDoc((xmlChar *)"1.0");
        assert(*doc != NULL);

        // Set the XML root to be <eurephia/>
        *root_n = xmlNewNode(NULL, (xmlChar *)"eurephia");
        assert(*root_n != NULL);

        // Add eurephia XML document format version id
        snprintf(tmp, 33, "%i%c", format, '\0');        xmlNewProp(*root_n, (xmlChar *)"format", (xmlChar *)tmp);
        xmlDocSetRootElement(*doc, *root_n);

        // Add the eurephia XML root (always inside the <eurephia/> tags)
        *root_n = xmlNewChild(*root_n, NULL, (xmlChar*)eurephiaRoot, NULL);

        return 1;
}


xmlNode *eurephiaXML_getRoot(eurephiaCTX *ctx, xmlDoc *doc, const char *nodeset, int req_format) {
        xmlNode *root = NULL;
        char *xmlformat_str = NULL;
        int xmlformat = 0;

        root = xmlDocGetRootElement(doc);
        if( (root == NULL) || (xmlStrcmp(root->name, (xmlChar *)"eurephia") != 0) ) {
                eurephia_log(ctx, LOG_FATAL, 0, "Could not find eurephia XML root element.  "
                             "Not a valid eurephia XML document.");
                return NULL;
        }

        xmlformat_str = xmlGetAttrValue(root->properties, "format");
        xmlformat = atoi_nullsafe(xmlformat_str);
        if( xmlformat < req_format ) {
                eurephia_log(ctx, LOG_ERROR, 0, "eurephia XML document format is not supported. "
                             "The XML document uses '%s', while we need minimum '%i'", xmlformat_str, req_format);
                return NULL;
        }

        return xmlFindNode(root, nodeset);
}


inline char *xmlExtractContent(xmlNode *n) {
        return (char *) (((n != NULL) && (n->children != NULL)) ? n->children->content : NULL);
}


inline char *xmlGetNodeContent(xmlNode *node, const char *key) {
        return xmlExtractContent(xmlFindNode(node, key));
}
#endif