summaryrefslogtreecommitdiff
path: root/libcore/libxmlwrapper.c
blob: 565c12ccd8089e5ec8828b2994ec0f0cfedae5c1 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "../libcore/helpers.h"
#include "libxmlwrapper.h"

void SkinDesignerXMLErrorHandler (void * userData, xmlErrorPtr error) {
    esyslog("skindesigner: Error in XML: %s", error->message);
}

cLibXMLWrapper::cLibXMLWrapper(void) {
    ctxt = NULL;
    doc = NULL;
    root = NULL;

    initGenericErrorDefaultFunc(NULL);
    xmlSetStructuredErrorFunc(NULL, SkinDesignerXMLErrorHandler);
    ctxt = xmlNewParserCtxt();
}

cLibXMLWrapper::~cLibXMLWrapper() {
    DeleteDocument();
    xmlFreeParserCtxt(ctxt);
}

void cLibXMLWrapper::InitLibXML() {
    xmlInitParser();
}

void cLibXMLWrapper::CleanupLibXML() {
    xmlCleanupParser();
}

void cLibXMLWrapper::DeleteDocument(void) {
    if (doc) {
        xmlFreeDoc(doc);
        doc = NULL;
    }
    while (!nodeStack.empty())
        nodeStack.pop();
}

bool cLibXMLWrapper::ReadXMLFile(const char *path) {
    if (!ctxt) {
        esyslog("skindesigner: Failed to allocate parser context");
        return false;
    }
    if (!FileExists(path)) {
        dsyslog("skindesigner: reading XML Template %s failed", path);
        return false;
    }
    doc = xmlCtxtReadFile(ctxt, path, NULL, XML_PARSE_NOENT | XML_PARSE_DTDVALID);
    if (!doc) {
        dsyslog("skindesigner: reading XML Template %s failed", path);
        return false;
    }
    return true;
}

bool cLibXMLWrapper::SetDocument(void) {
    if (!doc)
        return false;
    root = xmlDocGetRootElement(doc);
    nodeStack.push(root);
    if (root == NULL) {
        esyslog("skindesigner: ERROR: XML File is empty");
        return false;
    }
    return true;
}

bool cLibXMLWrapper::Validate(void) {
    if (!ctxt)
        return false;
    if (ctxt->valid == 0) {
        esyslog("skindesigner: Failed to validate XML File");
        return false;
    }
    return true;
}

bool cLibXMLWrapper::CheckNodeName(const char *name) {
    if (nodeStack.empty())
        return false;
    xmlNodePtr current = nodeStack.top(); 
    if (xmlStrcmp(current->name, (const xmlChar *) name)) {
        return false;
    }
    return true;
}

const char *cLibXMLWrapper::NodeName(void) {
    xmlNodePtr current = nodeStack.top(); 
    return (const char*)current->name;
}

vector<stringpair> cLibXMLWrapper::ParseAttributes(void) {
    vector<stringpair> attributes;
    if (nodeStack.empty())
        return attributes;
    xmlNodePtr current = nodeStack.top();
    xmlAttrPtr attrPtr = current->properties;
    if (attrPtr == NULL) {
        return attributes;
    }
    while (NULL != attrPtr) {
        string name = (const char*)attrPtr->name;
        xmlChar *value = NULL;
        value = xmlGetProp(current, attrPtr->name);
        if (value)
            attributes.push_back(stringpair((const char*)attrPtr->name, (const char*)value));
        attrPtr = attrPtr->next;
        if (value)
            xmlFree(value);
    }
    return attributes;
}

bool cLibXMLWrapper::LevelDown(void) {
    if (nodeStack.empty())
        return false;
    xmlNodePtr current = nodeStack.top();
    xmlNodePtr child = current->xmlChildrenNode;
    while (child && child->type != XML_ELEMENT_NODE) {
        child = child->next;
    }
    if (!child)
        return false;
    nodeStack.push(child);
    return true;
}

bool cLibXMLWrapper::LevelUp(void) {
    if (nodeStack.size() == 1)
        return false;
    nodeStack.pop();
    return true;
}

bool cLibXMLWrapper::NextNode(void) {
    xmlNodePtr current = nodeStack.top();
    current = current->next;
    while (current && current->type != XML_ELEMENT_NODE) {
        current = current->next;
    }
    if (!current)
        return false;
    nodeStack.pop();
    nodeStack.push(current);
    return true;
}

bool cLibXMLWrapper::GetAttribute(string &name, string &value) {
    bool ok = false;
    xmlNodePtr current = nodeStack.top();
    xmlAttrPtr attr = current->properties;
    if (attr == NULL) {
        return ok;
    }
    xmlChar *xmlValue = NULL;
    while (NULL != attr) {
        if (xmlStrcmp(attr->name, (const xmlChar *) name.c_str())) {
            attr = attr->next;
            continue;
        }
        ok = true;
        xmlValue = xmlGetProp(current, attr->name);
        if (xmlValue) {
            value = (const char*)xmlValue;
            xmlFree(xmlValue);
        }
        break;
    }
    return ok;
}

bool cLibXMLWrapper::GetNodeValue(string &value) {
    xmlChar *val = NULL;
    xmlNodePtr current = nodeStack.top();
    val = xmlNodeListGetString(doc, current->xmlChildrenNode, 1);
    if (val) {
        value = (const char*)val;
        xmlFree(val);
        return true;
    }
    return false;
}