vdr-plugin-skindesigner/libtemplate/xmlparser.c

624 lines
18 KiB
C
Raw Normal View History

2014-09-27 09:25:14 +02:00
#include "xmlparser.h"
#include "../config.h"
2014-11-15 09:47:22 +01:00
#include "../libcore/helpers.h"
2014-09-27 09:25:14 +02:00
using namespace std;
cXmlParser::cXmlParser(void) {
view = NULL;
2015-01-19 09:23:15 +01:00
globals = NULL;
skinSetup = NULL;
2014-09-27 09:25:14 +02:00
}
cXmlParser::~cXmlParser() {
}
/*********************************************************************
* PUBLIC Functions
*********************************************************************/
bool cXmlParser::ReadView(cTemplateView *view, string xmlFile) {
if (!view)
return false;
2014-09-27 09:25:14 +02:00
this->view = view;
string xmlPath = GetPath(xmlFile);
if (! ReadXMLFile(xmlPath.c_str()) )
2014-09-27 09:25:14 +02:00
return false;
if (! Validate() )
2014-09-27 09:25:14 +02:00
return false;
if (! SetDocument() )
2014-09-27 09:25:14 +02:00
return false;
if (! CheckNodeName(view->GetViewName()) )
return false;
return true;
}
2014-09-27 09:25:14 +02:00
bool cXmlParser::ParseView(void) {
if (!view)
2014-09-27 09:25:14 +02:00
return false;
vector<stringpair> rootAttribs = ParseAttributes();
ValidateAttributes(NodeName(), rootAttribs);
view->SetParameters(rootAttribs);
if (!LevelDown())
2014-09-27 09:25:14 +02:00
return false;
do {
if (view->ValidSubView(NodeName())) {
ParseSubView();
} else if (view->ValidViewElement(NodeName())) {
ParseViewElement();
} else if (view->ValidViewList(NodeName())) {
ParseViewList();
} else if (view->ValidViewGrid(NodeName())) {
ParseGrid();
} else if (CheckNodeName("tab")) {
ParseViewTab(view);
} else {
return false;
}
} while (NextNode());
2014-09-27 09:25:14 +02:00
return true;
2014-09-27 09:25:14 +02:00
}
2014-11-15 09:47:22 +01:00
bool cXmlParser::ReadPluginView(string plugName, int templateNumber, string templateName) {
string xmlPath = GetPath(templateName);
DeleteDocument();
if (! ReadXMLFile(xmlPath.c_str()) )
2014-11-15 09:47:22 +01:00
return false;
if (! Validate() )
2014-11-15 09:47:22 +01:00
return false;
if (! SetDocument() )
return false;
return true;
}
2014-11-15 09:47:22 +01:00
bool cXmlParser::ParsePluginView(string plugName, int templateNumber) {
cTemplateView *plugView = new cTemplateViewMenu();
view->AddPluginView(plugName, templateNumber, plugView);
vector<stringpair> attribs = ParseAttributes();
ValidateAttributes(NodeName(), attribs);
plugView->SetParameters(attribs);
2014-11-15 09:47:22 +01:00
if (!LevelDown())
2014-11-15 09:47:22 +01:00
return false;
do {
if (plugView->ValidViewElement(NodeName())) {
ParseViewElement(plugView);
} else if (plugView->ValidViewList(NodeName())) {
ParseViewList(plugView);
} else if (CheckNodeName("tab")) {
ParseViewTab(plugView);
} else {
return false;
}
} while (NextNode());
2014-11-15 09:47:22 +01:00
return true;
}
bool cXmlParser::ReadGlobals(cGlobals *globals, string xmlFile) {
2014-09-27 09:25:14 +02:00
this->globals = globals;
string xmlPath = GetPath(xmlFile);
DeleteDocument();
if (! ReadXMLFile(xmlPath.c_str()) )
2014-09-27 09:25:14 +02:00
return false;
if (! Validate() )
2014-09-27 09:25:14 +02:00
return false;
if (! SetDocument() )
2014-09-27 09:25:14 +02:00
return false;
if (! CheckNodeName("globals") )
2014-09-27 09:25:14 +02:00
return false;
return true;
}
2014-09-27 09:25:14 +02:00
bool cXmlParser::ParseGlobals(void) {
if (!LevelDown())
2014-09-27 09:25:14 +02:00
return false;
do {
if (CheckNodeName("colors")) {
ParseGlobalColors();
} else if (CheckNodeName("variables")) {
ParseGlobalVariables();
} else if (CheckNodeName("fonts")) {
ParseGlobalFonts();
} else if (CheckNodeName("translations")) {
ParseTranslations();
}
} while (NextNode());
2014-09-27 09:25:14 +02:00
return true;
}
bool cXmlParser::ReadSkinSetup(cSkinSetup *skinSetup, string xmlFile) {
2015-01-19 09:23:15 +01:00
this->skinSetup = skinSetup;
if (! ReadXMLFile(xmlFile.c_str()) )
2015-01-19 09:23:15 +01:00
return false;
if (! Validate() )
2015-01-19 09:23:15 +01:00
return false;
if (! SetDocument() )
2015-01-19 09:23:15 +01:00
return false;
if (! CheckNodeName("setup") )
2015-01-19 09:23:15 +01:00
return false;
return true;
}
bool cXmlParser::ParseSkinSetup(string skin) {
if (!LevelDown())
return false;
do {
if (CheckNodeName("menu")) {
ParseSetupMenu();
} else if (CheckNodeName("translations")) {
ParseTranslations();
}
} while (NextNode());
return true;
}
/*********************************************************************
* PRIVATE Functions
*********************************************************************/
2015-01-19 09:23:15 +01:00
bool cXmlParser::ParseSubView(void) {
2014-09-27 09:25:14 +02:00
if (!view)
return false;
cTemplateView *subView = new cTemplateViewMenu();
view->AddSubView(NodeName(), subView);
2014-09-27 09:25:14 +02:00
vector<pair<string, string> > subViewAttribs = ParseAttributes();
ValidateAttributes(NodeName(), subViewAttribs);
subView->SetParameters(subViewAttribs);
2014-09-27 09:25:14 +02:00
if (!LevelDown())
return false;
do {
if (subView->ValidViewElement(NodeName())) {
ParseViewElement(subView);
} else if (subView->ValidViewList(NodeName())) {
ParseViewList(subView);
} else if (CheckNodeName("tab")) {
ParseViewTab(subView);
2014-09-27 09:25:14 +02:00
} else {
return false;
}
} while (NextNode());
LevelUp();
2014-09-27 09:25:14 +02:00
return true;
}
void cXmlParser::ParseViewElement(cTemplateView *subView) {
if (!view)
return;
const char *viewElement = NodeName();
vector<stringpair> attributes = ParseAttributes();
ValidateAttributes("viewelement", attributes);
2014-11-15 09:47:22 +01:00
if (!LevelDown())
return;
do {
if (!CheckNodeName("areacontainer") && !CheckNodeName("area") && !CheckNodeName("areascroll")) {
esyslog("skindesigner: invalid tag \"%s\" in viewelement", NodeName());
continue;
}
cTemplatePixmapNode *pix = NULL;
if (CheckNodeName("area") || CheckNodeName("areascroll")) {
pix = ParseArea();
} else {
pix = ParseAreaContainer();
}
if (subView)
subView->AddPixmap(viewElement, pix, attributes);
else
view->AddPixmap(viewElement, pix, attributes);
} while (NextNode());
LevelUp();
}
2014-11-15 09:47:22 +01:00
void cXmlParser::ParseViewList(cTemplateView *subView) {
if (!view)
return;
vector<stringpair> attribs = ParseAttributes();
ValidateAttributes(NodeName(), attribs);
2014-11-15 09:47:22 +01:00
cTemplateViewList *viewList = new cTemplateViewList();
viewList->SetGlobals(globals);
viewList->SetParameters(attribs);
2014-11-15 09:47:22 +01:00
if (!LevelDown())
return;
2014-11-15 09:47:22 +01:00
do {
if (CheckNodeName("currentelement")) {
2014-11-15 09:47:22 +01:00
cTemplateViewElement *currentElement = new cTemplateViewElement();
vector<stringpair> attribsCur = ParseAttributes();
ValidateAttributes(NodeName(), attribsCur);
currentElement->SetGlobals(globals);
currentElement->SetParameters(attribsCur);
if (!LevelDown())
return;
do {
if (!CheckNodeName("areacontainer") && !CheckNodeName("area") && !CheckNodeName("areascroll")) {
esyslog("skindesigner: invalid tag \"%s\" in viewelement", NodeName());
continue;
}
cTemplatePixmapNode *pix = NULL;
if (CheckNodeName("area") || CheckNodeName("areascroll")) {
pix = ParseArea();
} else {
pix = ParseAreaContainer();
}
currentElement->AddPixmap(pix);
} while (NextNode());
LevelUp();
viewList->AddCurrentElement(currentElement);
2014-11-15 09:47:22 +01:00
} else if (CheckNodeName("listelement")) {
cTemplateViewElement *listElement = new cTemplateViewElement();
vector<stringpair> attribsList = ParseAttributes();
ValidateAttributes(NodeName(), attribsList);
listElement->SetGlobals(globals);
listElement->SetParameters(attribsList);
if (!LevelDown())
return;
do {
if (!CheckNodeName("areacontainer") && !CheckNodeName("area") && !CheckNodeName("areascroll")) {
esyslog("skindesigner: invalid tag \"%s\" in viewelement", NodeName());
continue;
}
cTemplatePixmapNode *pix = NULL;
if (CheckNodeName("area") || CheckNodeName("areascroll")) {
pix = ParseArea();
} else {
pix = ParseAreaContainer();
}
listElement->AddPixmap(pix);
} while (NextNode());
LevelUp();
viewList->AddListElement(listElement);
2014-11-15 09:47:22 +01:00
}
} while (NextNode());
LevelUp();
2014-11-15 09:47:22 +01:00
if (subView)
subView->AddViewList(NodeName(), viewList);
else
view->AddViewList(NodeName(), viewList);
2014-11-15 09:47:22 +01:00
}
void cXmlParser::ParseViewTab(cTemplateView *subView) {
if (!view || !subView)
return;
2014-09-27 09:25:14 +02:00
vector<stringpair> attribs = ParseAttributes();
ValidateAttributes(NodeName(), attribs);
2014-09-27 09:25:14 +02:00
cTemplateViewTab *viewTab = new cTemplateViewTab();
viewTab->SetGlobals(globals);
viewTab->SetParameters(attribs);
viewTab->SetScrolling();
ParseFunctionCalls(viewTab);
subView->AddViewTab(viewTab);
2014-09-27 09:25:14 +02:00
}
void cXmlParser::ParseGrid(void) {
if (!view)
return;
vector<stringpair> attributes = ParseAttributes();
ValidateAttributes(NodeName(), attributes);
if (!LevelDown())
return;
do {
2015-01-19 09:23:15 +01:00
if (!CheckNodeName("areacontainer") && !CheckNodeName("area") && !CheckNodeName("areascroll")) {
esyslog("skindesigner: invalid tag \"%s\" in grid", NodeName());
2015-01-19 09:23:15 +01:00
continue;
}
cTemplatePixmapNode *pix = NULL;
if (CheckNodeName("area") || CheckNodeName("areascroll")) {
pix = ParseArea();
} else {
pix = ParseAreaContainer();
2015-01-19 09:23:15 +01:00
}
view->AddPixmapGrid(pix, attributes);
} while (NextNode());
LevelUp();
2015-01-19 09:23:15 +01:00
}
cTemplatePixmap *cXmlParser::ParseArea(void) {
vector<stringpair> attribs = ParseAttributes();
ValidateAttributes(NodeName(), attribs);
cTemplatePixmap *pix = new cTemplatePixmap();
if (CheckNodeName("areascroll")) {
pix->SetScrolling();
2014-09-27 09:25:14 +02:00
}
pix->SetParameters(attribs);
ParseFunctionCalls(pix);
return pix;
2014-09-27 09:25:14 +02:00
}
cTemplatePixmapContainer *cXmlParser::ParseAreaContainer(void) {
vector<stringpair> attribs = ParseAttributes();
ValidateAttributes(NodeName(), attribs);
2014-09-27 09:25:14 +02:00
cTemplatePixmapContainer *pixContainer = new cTemplatePixmapContainer();
pixContainer->SetParameters(attribs);
if (!LevelDown())
return pixContainer;
do {
if (!CheckNodeName("area") && !CheckNodeName("areascroll")) {
esyslog("skindesigner: invalid tag \"%s\" in areacontainer", NodeName());
continue;
}
cTemplatePixmap *pix = ParseArea();
pixContainer->AddPixmap(pix);
} while (NextNode());
LevelUp();
return pixContainer;
2014-09-27 09:25:14 +02:00
}
void cXmlParser::ParseFunctionCalls(cTemplatePixmap *pix) {
if (!view)
2015-01-19 09:23:15 +01:00
return;
if (!LevelDown())
2015-01-19 09:23:15 +01:00
return;
do {
if (CheckNodeName("loop")) {
vector<stringpair> attribs = ParseAttributes();
ValidateAttributes(NodeName(), attribs);
cTemplateLoopFunction *loopFunc = new cTemplateLoopFunction();
loopFunc->SetParameters(attribs);
ParseLoopFunctionCalls(loopFunc);
pix->AddLoopFunction(loopFunc);
} else if (view->ValidFunction(NodeName())) {
vector<stringpair> attribs = ParseAttributes();
ValidateAttributes(NodeName(), attribs);
pix->AddFunction(NodeName(), attribs);
2015-01-19 09:23:15 +01:00
}
} while (NextNode());
LevelUp();
}
void cXmlParser::ParseLoopFunctionCalls(cTemplateLoopFunction *loopFunc) {
if (!view)
return;
if (!LevelDown())
return;
do {
if (view->ValidFunction(NodeName())) {
vector<stringpair> attribs = ParseAttributes();
ValidateAttributes(NodeName(), attribs);
loopFunc->AddFunction(NodeName(), attribs);
}
} while (NextNode());
LevelUp();
2015-01-19 09:23:15 +01:00
}
void cXmlParser::ParseGlobalColors(void) {
if (!LevelDown())
2014-09-27 09:25:14 +02:00
return;
do {
if (!CheckNodeName("color")) {
2014-09-27 09:25:14 +02:00
continue;
}
string attributeName = "name";
string colorName = "";
string colorValue = "";
bool ok = GetAttribute(attributeName, colorName);
2014-09-27 09:25:14 +02:00
if (ok) {
ok = GetNodeValue(colorValue);
if (ok)
InsertColor(colorName, colorValue);
2014-09-27 09:25:14 +02:00
}
} while (NextNode());
LevelUp();
2014-09-27 09:25:14 +02:00
}
void cXmlParser::InsertColor(string name, string value) {
if (value.size() != 8)
return;
std::stringstream str;
str << value;
tColor colVal;
str >> std::hex >> colVal;
globals->AddColor(name, colVal);
2014-09-27 09:25:14 +02:00
}
void cXmlParser::ParseGlobalVariables(void) {
if (!LevelDown())
2014-09-27 09:25:14 +02:00
return;
do {
if (!CheckNodeName("var")) {
2014-09-27 09:25:14 +02:00
continue;
}
string attributeName = "name";
string attributeType = "type";
string varName = "";
string varType = "";
string varValue = "";
bool ok1 = GetAttribute(attributeName, varName);
bool ok2 = GetAttribute(attributeType, varType);
if (ok1 && ok2) {
bool ok = GetNodeValue(varValue);
if (ok)
InsertVariable(varName, varType, varValue);
2014-09-27 09:25:14 +02:00
}
} while (NextNode());
LevelUp();
2014-09-27 09:25:14 +02:00
}
void cXmlParser::InsertVariable(string name, string type, string value) {
if (!type.compare("int")) {
int val = atoi(value.c_str());
globals->AddInt(name, val);
} else if (!type.compare("double")) {
globals->AddDouble(name, value);
2014-09-27 09:25:14 +02:00
} else if (!type.compare("string")) {
globals->AddString(name, value);
2014-09-27 09:25:14 +02:00
}
}
void cXmlParser::ParseGlobalFonts(void) {
if (!LevelDown())
2014-09-27 09:25:14 +02:00
return;
do {
if (!CheckNodeName("font")) {
2014-09-27 09:25:14 +02:00
continue;
}
string attributeName = "name";
string fontName = "";
string fontValue = "";
bool ok = GetAttribute(attributeName, fontName);
2014-09-27 09:25:14 +02:00
if (ok) {
ok = GetNodeValue(fontValue);
if (ok) {
globals->AddFont(fontName, fontValue);
}
2014-09-27 09:25:14 +02:00
}
} while (NextNode());
LevelUp();
2014-09-27 09:25:14 +02:00
}
void cXmlParser::ParseTranslations(void) {
if (!LevelDown())
2014-09-27 09:25:14 +02:00
return;
do {
if (!CheckNodeName("token")) {
2014-09-27 09:25:14 +02:00
continue;
}
string attributeName = "name";
string tokenName = "";
2014-09-27 09:25:14 +02:00
if (!GetAttribute(attributeName, tokenName))
2014-09-27 09:25:14 +02:00
continue;
if (!LevelDown())
2014-09-27 09:25:14 +02:00
continue;
stringmap tokenTranslations;
do {
if (!CheckNodeName("trans")) {
2014-09-27 09:25:14 +02:00
continue;
}
string attributeName = "lang";
string language = "";
if (!GetAttribute(attributeName, language))
2014-09-27 09:25:14 +02:00
continue;
string translation = "";
if (!GetNodeValue(translation))
continue;
tokenTranslations.insert(stringpair(language, translation));
} while (NextNode());
LevelUp();
2015-02-12 18:50:58 +01:00
if (globals) {
globals->AddTranslation(tokenName, tokenTranslations);
} else if (skinSetup) {
skinSetup->SetTranslation(tokenName, tokenTranslations);
2015-02-12 18:50:58 +01:00
}
} while (NextNode());
LevelUp();
2015-02-12 18:50:58 +01:00
}
void cXmlParser::ParseSetupMenu(void) {
if (!skinSetup)
2014-09-27 09:25:14 +02:00
return;
if (!LevelDown())
2014-09-27 09:25:14 +02:00
return;
do {
if (CheckNodeName("parameter")) {
ParseSetupParameter();
} else if (CheckNodeName("submenu")) {
string attributeName = "name";
string subMenuName = "";
string attributeDisplayText = "displaytext";
string subDisplayText = "";
GetAttribute(attributeName, subMenuName);
GetAttribute(attributeDisplayText, subDisplayText);
skinSetup->SetSubMenu(subMenuName, subDisplayText);
ParseSetupMenu();
2014-09-27 09:25:14 +02:00
}
} while (NextNode());
skinSetup->SubMenuDone();
LevelUp();
2014-09-27 09:25:14 +02:00
}
void cXmlParser::ParseSetupParameter(void) {
if (!skinSetup)
2014-09-27 09:25:14 +02:00
return;
string attributeType = "type";
string paramType = "";
string attributeName = "name";
string paramName = "";
string attributeDisplayText = "displaytext";
string paramDisplayText = "";
string attributeMin = "min";
string paramMin = "";
string attributeMax = "max";
string paramMax = "";
string paramValue = "";
GetAttribute(attributeType, paramType);
GetAttribute(attributeName, paramName);
GetAttribute(attributeDisplayText, paramDisplayText);
GetAttribute(attributeMin, paramMin);
GetAttribute(attributeMax, paramMax);
GetNodeValue(paramValue);
2014-09-27 09:25:14 +02:00
skinSetup->SetParameter(paramType, paramName, paramDisplayText, paramMin, paramMax, paramValue);
2014-09-27 09:25:14 +02:00
}
void cXmlParser::ValidateAttributes(const char *nodeName, vector<stringpair> &attributes) {
bool repeat = true;
while (repeat) {
repeat = false;
for (vector<stringpair>::iterator it = attributes.begin(); it != attributes.end(); it++) {
string attributeName = (*it).first;
if (!view->ValidAttribute(nodeName, attributeName.c_str())) {
attributes.erase(it);
repeat = true;
break;
}
2014-09-27 09:25:14 +02:00
}
}
}
string cXmlParser::GetPath(string xmlFile) {
string activeSkin = Setup.OSDSkin;
string activeTheme = Setup.OSDTheme;
string path = "";
if (!xmlFile.compare("globals.xml")) {
path = *cString::sprintf("%s%s/%s", *config.GetSkinPath(activeSkin), activeSkin.c_str(), xmlFile.c_str());
} else if (!xmlFile.compare("theme.xml")) {
path = *cString::sprintf("%s%s/themes/%s/%s", *config.GetSkinPath(activeSkin), activeSkin.c_str(), activeTheme.c_str(), xmlFile.c_str());
} else if (!xmlFile.compare("setup.xml")) {
path = *cString::sprintf("%s%s/%s", *config.GetSkinPath(activeSkin), activeSkin.c_str(), xmlFile.c_str());
} else {
path = *cString::sprintf("%s%s/xmlfiles/%s", *config.GetSkinPath(activeSkin), activeSkin.c_str(), xmlFile.c_str());
}
return path;
}