diff options
author | louis <louis.braun@gmx.de> | 2015-07-07 17:59:00 +0200 |
---|---|---|
committer | louis <louis.braun@gmx.de> | 2015-07-07 17:59:00 +0200 |
commit | 5df1af0e176e8495b33f3837691b1f922d7316f5 (patch) | |
tree | f1c953d87873564057384377c0341b83843642d0 /libcore | |
parent | 5a6fb850b35bc63325cac482daaa70b00b0e8e8b (diff) | |
download | vdr-plugin-skindesigner-5df1af0e176e8495b33f3837691b1f922d7316f5.tar.gz vdr-plugin-skindesigner-5df1af0e176e8495b33f3837691b1f922d7316f5.tar.bz2 |
forgot to add new files
Diffstat (limited to 'libcore')
-rw-r--r-- | libcore/libxmlwrapper.c | 184 | ||||
-rw-r--r-- | libcore/libxmlwrapper.h | 45 |
2 files changed, 229 insertions, 0 deletions
diff --git a/libcore/libxmlwrapper.c b/libcore/libxmlwrapper.c new file mode 100644 index 0000000..565c12c --- /dev/null +++ b/libcore/libxmlwrapper.c @@ -0,0 +1,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;
+}
diff --git a/libcore/libxmlwrapper.h b/libcore/libxmlwrapper.h new file mode 100644 index 0000000..cb1872f --- /dev/null +++ b/libcore/libxmlwrapper.h @@ -0,0 +1,45 @@ +#ifndef __LIBXMLWRAPPER_H
+#define __LIBXMLWRAPPER_H
+
+#include <string>
+#include <vector>
+#include <map>
+#include <set>
+#include <stack>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxml/xmlerror.h>
+#include <vdr/plugin.h>
+
+using namespace std;
+
+#include "../views/viewhelpers.h"
+
+class cLibXMLWrapper {
+private:
+ xmlParserCtxtPtr ctxt;
+ xmlDocPtr doc;
+ xmlNodePtr root;
+ xmlNodePtr current;
+ stack<xmlNodePtr> nodeStack;
+protected:
+ void DeleteDocument(void);
+ bool ReadXMLFile(const char *path);
+ bool SetDocument(void);
+ bool Validate(void);
+ bool CheckNodeName(const char *name);
+ const char *NodeName(void);
+ vector<stringpair> ParseAttributes(void);
+ bool LevelDown(void);
+ bool LevelUp(void);
+ bool NextNode(void);
+ bool GetAttribute(string &name, string &value);
+ bool GetNodeValue(string &value);
+public:
+ cLibXMLWrapper(void);
+ virtual ~cLibXMLWrapper(void);
+ static void InitLibXML();
+ static void CleanupLibXML();
+};
+
+#endif //__LIBXMLWRAPPER_H
\ No newline at end of file |