From a158189e0cbded6ecdd178927cd949985e672e19 Mon Sep 17 00:00:00 2001 From: Donghoon Kwak Date: Fri, 21 Jun 2019 15:26:47 +0900 Subject: [PATCH] Refactoring ModeXMLParser --- supervisor/Mode.cpp | 9 +- supervisor/Mode.h | 6 +- supervisor/ModeManager.cpp | 14 ++- supervisor/ModeXMLParser.cpp | 211 ++++++++++++++++++++------------------ supervisor/ModeXMLParser.h | 17 +-- supervisor/XMLGenerator.cpp | 6 +- unittest/modes_test_generator.cpp | 10 +- unittest/modes_test_parser.cpp | 30 +++--- 8 files changed, 159 insertions(+), 144 deletions(-) diff --git a/supervisor/Mode.cpp b/supervisor/Mode.cpp index eb23373..9cace64 100644 --- a/supervisor/Mode.cpp +++ b/supervisor/Mode.cpp @@ -20,13 +20,11 @@ MODES_NAMESPACE_USE; Mode::~Mode() { - for (auto it = reqList.begin(); it != reqList.end(); ++it) - delete *it; } void Mode::addRequirement(Req *req) { - reqList.push_back(req); + reqList.push_back(std::shared_ptr(req)); } void Mode::setName(const std::string &data) @@ -51,11 +49,12 @@ std::string Mode::getModeType() void Mode::apply() { - std::list::iterator it; + std::list>::iterator it; for (it = reqList.begin(); it != reqList.end(); it++) (*it)->apply(); } -std::list Mode::getReqList(){ +std::list> Mode::getReqList() +{ return reqList; } diff --git a/supervisor/Mode.h b/supervisor/Mode.h index 7c73a06..b0e4ea2 100644 --- a/supervisor/Mode.h +++ b/supervisor/Mode.h @@ -17,6 +17,7 @@ #include #include +#include #include "mdss.h" #include "Req.h" @@ -32,14 +33,13 @@ public: std::string getModeType(); void addRequirement(Req *req); - std::list getReqList(); - + std::list> getReqList(); void apply(); private: std::string name; std::string type; - std::list reqList; + std::list> reqList; #ifdef MDS_TEST friend class TestParser; #endif diff --git a/supervisor/ModeManager.cpp b/supervisor/ModeManager.cpp index bd5a5f4..8c429a9 100644 --- a/supervisor/ModeManager.cpp +++ b/supervisor/ModeManager.cpp @@ -20,13 +20,14 @@ #include #include #include "mdss.h" +#include "ModesEx.h" #include "ModeXMLParser.h" using std::string; MODES_NAMESPACE_USE; ModeManager::ModeManager(ActionSchemaManager &asMgr, PluginManager &piMgr) - :actSchemaMgr(asMgr), pluginMgr(piMgr) + : actSchemaMgr(asMgr), pluginMgr(piMgr) { } @@ -81,8 +82,7 @@ int ModeManager::applyMode(const string &modeName) mode.apply(); delete parser; - } - catch (ModesEx &e) { + } catch (ModesEx &e) { ERR("parser Fail(%s)", e.what()); } return MODES_ERROR_NONE; @@ -110,7 +110,7 @@ bool ModeManager::makeModeMap(const string &dirPath) string file(entry->d_name); size_t pos = file.find_last_of("_"); if (string::npos == pos - || file.compare(pos, sizeof(MODES_MODE_FILE_SUFFIX) - 1, MODES_MODE_FILE_SUFFIX)) { + || file.compare(pos, sizeof(MODES_MODE_FILE_SUFFIX) - 1, MODES_MODE_FILE_SUFFIX)) { continue; } @@ -120,11 +120,9 @@ bool ModeManager::makeModeMap(const string &dirPath) modeParser->validateMode(modeSyntaxFile); addModeName(modeParser, fileFullPath); delete modeParser; - } - catch (ModesEx &e) { + } catch (ModesEx &e) { ERR("parser(%s) Fail(%s)", fileFullPath.c_str(), e.what()); - } - catch (std::invalid_argument &e) { + } catch (std::invalid_argument &e) { ERR("parser Fail(%s)", e.what()); } } diff --git a/supervisor/ModeXMLParser.cpp b/supervisor/ModeXMLParser.cpp index 193c2b7..a833718 100644 --- a/supervisor/ModeXMLParser.cpp +++ b/supervisor/ModeXMLParser.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ #include "ModeXMLParser.h" - #include #include #include @@ -23,157 +22,165 @@ #include "Mode.h" #include "ModesEx.h" #include "TReq.h" +#include "XMLParser.h" #include "ModeTag.h" MODES_NAMESPACE_USE; ModeXMLParser::ModeXMLParser(const std::string &modeFile, ActionSchemaManager &asMgr, PluginManager &piMgr) - :ModeParser(asMgr), pluginManager(piMgr), parser(modeFile) + : ModeParser(asMgr), pluginManager(piMgr) { + this->modeFile = modeFile; } -void ModeXMLParser::parseMode(xmlNodePtr node, Mode &modedesc) +ModeXMLParser::~ModeXMLParser() { - char *tmp; - - tmp = (char*)xmlGetProp(node, ModeTag::NAME); - if (tmp) { - modedesc.setName(tmp); - xmlFree(tmp); - } else { - ERR("Mode : No %s", ModeTag::NAME); - } - - tmp = (char*)xmlGetProp(node, ModeTag::TYPE); - if (tmp) { - modedesc.setModeType(tmp); - xmlFree(tmp); - } else { - ERR("Mode : No %s", ModeTag::TYPE); - } } -void ModeXMLParser::bindPlugin(const std::string &name, Req *req) +Mode ModeXMLParser::getMode() { - std::string pluginName; - int pos = name.find_first_of("."); - pluginName = name.substr(0, pos); - req->setPlugin(pluginManager.getPlugin(pluginName)); + XMLParser parser(modeFile); + iterateElement(parser.getRoot()); + return mode; } -void ModeXMLParser::parseReq(xmlNodePtr node, Mode &modedesc) +std::string ModeXMLParser::getModeName() { - char *tmp; - Req *req = NULL; - - tmp = (char*)xmlGetProp(node, ModeTag::ACTION); - if (tmp) { - req = schemaMgr.createAction(tmp); - if (nullptr == req) { - ERR("createAction(%s) Fail", tmp); - req = new TReq(tmp); - } - bindPlugin(tmp, req); - - xmlFree(tmp); - } else { - ERR("Req : No %s", ModeTag::ACTION); - } - if (NULL == req) { - ERR("req is NULL"); - throw ModesEx(ModesEx::PARSER_ERROR, "No req"); + if (!mode.getName().empty()) { + return mode.getName(); } - tmp = (char*)xmlGetProp(node, ModeTag::RESTICT); - if (tmp && MDS_EQUAL == strcmp(tmp, ModeTag::RESTICT_LOCK)) { - req->setRestrict(Req::REQ_LOCK); - } else { - req->setRestrict(Req::REQ_NONE); - } - xmlFree(tmp); + XMLParser parser(modeFile); + xmlNodePtr cur, root; - tmp = (char*)xmlGetProp(node, ModeTag::ID); - if (tmp) { - req->setID(tmp); - xmlFree(tmp); - } + root = parser.getRoot(); + for (cur = root->children; cur; cur = cur->next) { + if (xmlIsBlankNode(cur)) + continue; - tmp = (char*)xmlNodeGetContent(node); - if (tmp) { - req->setValue(tmp); - xmlFree(tmp); - } else { - ERR("Req : No Value"); + if (MDS_EQUAL == xmlStrcmp(cur->name, ModeTag::MODE)) { + mode.setName(getModeName(cur)); + break; + } } - req->printInfo(); - - modedesc.addRequirement(req); + return mode.getName(); } -void ModeXMLParser::getDatafromNode(xmlNodePtr node, Mode &modedesc) +void ModeXMLParser::validateMode(const std::string &xsd) { - if (MDS_EQUAL == xmlStrcmp(node->name, ModeTag::MODE)) { - parseMode(node, modedesc); - } else if (MDS_EQUAL == xmlStrcmp(node->name, ModeTag::REQ)) { - parseReq(node, modedesc); - } else { - DBG("Unhandled node : %s", node->name); + XMLParser parser = XMLParser(modeFile); + try { + parser.validate(xsd); + } catch (ModesEx &e) { + ERR("validate() Fail(%s)", e.what()); + throw ModesEx(ModesEx::INVALID_ARG); } } -void ModeXMLParser::iterateElement(xmlNodePtr node, Mode &mode) +void ModeXMLParser::iterateElement(xmlNodePtr node) { xmlNode *cur = NULL; - for (cur = node; cur; cur = cur->next) { if (xmlIsBlankNode(cur)) continue; - getDatafromNode(cur, mode); + getDatafromNode(cur); + iterateElement(cur->children); + } +} - iterateElement(cur->children, mode); +void ModeXMLParser::getDatafromNode(xmlNodePtr node) +{ + if (MDS_EQUAL == xmlStrcmp(node->name, ModeTag::MODE)) { + parseMode(node); + } else if (MDS_EQUAL == xmlStrcmp(node->name, ModeTag::REQ)) { + parseReq(node); + } else { + DBG("Unhandled node : %s", node->name); } } -Mode ModeXMLParser::getMode() +void ModeXMLParser::parseMode(xmlNodePtr node) { - Mode mode; + mode.setName(getModeName(node)); + mode.setModeType(getModeType(node)); +} - iterateElement(parser.getRoot(), mode); +std::string ModeXMLParser::getModeName(xmlNodePtr node) +{ + return getXmlTagStringValue(node, ModeTag::NAME); +} - return mode; +std::string ModeXMLParser::getModeType(xmlNodePtr node) +{ + return getXmlTagStringValue(node, ModeTag::TYPE); } -std::string ModeXMLParser::getModeName() +std::string ModeXMLParser::getXmlTagStringValue(xmlNodePtr node, const xmlChar* tag) { - xmlNodePtr cur, root; - std::string modeName; + char *xmlTagValue; + std::string xmlTagString; + + xmlTagValue = (char*)xmlGetProp(node, tag); + if (xmlTagValue == NULL) { + ERR("XML Value is NULL. Tag[%s]", tag); + xmlTagString = ""; + return xmlTagString; + } - root = parser.getRoot(); - for (cur = root->children; cur; cur = cur->next) { - if (xmlIsBlankNode(cur)) - continue; + xmlTagString = xmlTagValue; + xmlFree(xmlTagValue); + return xmlTagString; +} - if (MDS_EQUAL == xmlStrcmp(cur->name, ModeTag::MODE)) { - char *name = (char*)xmlGetProp(cur, ModeTag::NAME); - modeName = name; - xmlFree(name); +void ModeXMLParser::parseReq(xmlNodePtr node) +{ + char *actionProp = (char*)xmlGetProp(node, ModeTag::ACTION); + if (actionProp == NULL) { + ERR("ACTION TAG Value is null! [%s]", ModeTag::ACTION); + throw ModesEx(ModesEx::PARSER_ERROR, "ACTION TAG Value is null!"); + } + + Req *req = schemaMgr.createAction(actionProp); + if (req == nullptr) { + WARN("schemaMgr createAction(%s) Fail", actionProp); + req = new TReq(actionProp); + if (req == NULL) { + ERR("Req is null"); + throw ModesEx(ModesEx::PARSER_ERROR, "Req is null!"); } } - return modeName; -} + bindPlugin(actionProp, req); + xmlFree(actionProp); -void ModeXMLParser::validateMode(const std::string &xsd) -{ - try { - parser.validate(xsd); + char *restictProp = (char*)xmlGetProp(node, ModeTag::RESTICT); + if (restictProp && MDS_EQUAL == strcmp(restictProp, ModeTag::RESTICT_LOCK)) { + req->setRestrict(Req::REQ_LOCK); + } else { + req->setRestrict(Req::REQ_NONE); } - catch (ModesEx &e) { - ERR("validate() Fail(%s)", e.what()); - throw ModesEx(ModesEx::INVALID_ARG); + xmlFree(restictProp); + + char *nodeContent = (char*)xmlNodeGetContent(node); + if (nodeContent == NULL) { + ERR("Node Content is null!"); } - return; + req->setValue(nodeContent ? nodeContent : ""); + xmlFree(nodeContent); + + req->printInfo(); + + mode.addRequirement(req); } + +void ModeXMLParser::bindPlugin(const std::string &name, Req *req) +{ + std::string pluginName; + int pos = name.find_first_of("."); + pluginName = name.substr(0, pos); + req->setPlugin(pluginManager.getPlugin(pluginName)); +} + diff --git a/supervisor/ModeXMLParser.h b/supervisor/ModeXMLParser.h index ce86552..f5c41c8 100644 --- a/supervisor/ModeXMLParser.h +++ b/supervisor/ModeXMLParser.h @@ -17,9 +17,7 @@ #include #include "mdss.h" -#include "ModesEx.h" #include "ModeParser.h" -#include "XMLParser.h" #include "PluginManager.h" MODES_NAMESPACE_BEGIN @@ -27,20 +25,25 @@ MODES_NAMESPACE_BEGIN class ModeXMLParser : public ModeParser { public: ModeXMLParser(const std::string &modeFile, ActionSchemaManager &mgr, PluginManager &pluginMgr); + ~ModeXMLParser(); Mode getMode() override; std::string getModeName() override; void validateMode(const std::string &xsd) override; private: - void parseMode(xmlNodePtr node, Mode &modedesc); - void parseReq(xmlNodePtr node, Mode &modedesc); - void getDatafromNode(xmlNodePtr node, Mode &modedesc); - void iterateElement(xmlNodePtr node, Mode &modedesc); + void iterateElement(xmlNodePtr node); + void getDatafromNode(xmlNodePtr node); + void parseMode(xmlNodePtr node); + std::string getModeName(xmlNodePtr node); + std::string getModeType(xmlNodePtr node); + std::string getXmlTagStringValue(xmlNodePtr node, const xmlChar *tag); + void parseReq(xmlNodePtr node); void bindPlugin(const std::string &name, Req *req); PluginManager &pluginManager; - XMLParser parser; + Mode mode; + std::string modeFile; }; MODES_NAMESPACE_END diff --git a/supervisor/XMLGenerator.cpp b/supervisor/XMLGenerator.cpp index f10c8d7..72e70c8 100644 --- a/supervisor/XMLGenerator.cpp +++ b/supervisor/XMLGenerator.cpp @@ -23,7 +23,7 @@ MODES_NAMESPACE_USE; XMLGenerator::XMLGenerator() - :doc(NULL), rootNode(NULL), modeNode(NULL) + : doc(NULL), rootNode(NULL), modeNode(NULL) { } @@ -73,8 +73,8 @@ void XMLGenerator::makeMode(const std::string &filename, Mode &mode) xmlSetProp(modeNode, ModeTag::TYPE, (xmlChar*)modeType.c_str()); xmlAddChild(rootNode, modeNode); - std::list reqList = mode.getReqList(); - std::list::iterator it; + std::list> reqList = mode.getReqList(); + std::list>::iterator it; for (it = reqList.begin(); it != reqList.end(); it++) { xmlNodePtr reqNode; Req::ReqRestrict restriction = (*it)->getRestrict(); diff --git a/unittest/modes_test_generator.cpp b/unittest/modes_test_generator.cpp index b2f2d63..76a26b1 100644 --- a/unittest/modes_test_generator.cpp +++ b/unittest/modes_test_generator.cpp @@ -25,7 +25,8 @@ MODES_NAMESPACE_USE; using namespace std; -TEST(XMLGenerator, makeMode) { +TEST(XMLGenerator, makeMode) +{ PluginManager pluginManager; ActionSchemaManager schemaMgr; @@ -44,17 +45,18 @@ TEST(XMLGenerator, makeMode) { Mode genMode = genmodeparser.getMode(); EXPECT_EQ("ex1", genmodeparser.getModeName()); - list reqList = mode.getReqList(); + std::list> reqList = mode.getReqList(); EXPECT_FALSE(reqList.empty()); - for (list::iterator it = reqList.begin(); it != reqList.end(); ++it) { + for (std::list>::iterator it = reqList.begin(); it != reqList.end(); ++it) { cout << "* Req : " << (*it)->getName() << endl; cout << "\t\t- Restrict : " << (*it)->getRestrict() << endl; cout << "\t\t- Content : " << (*it)->getStringOfValue() << endl; } } -TEST(XMLGenerator, exFileName) { +TEST(XMLGenerator, exFileName) +{ EXPECT_THROW({ PluginManager pluginManager; ActionSchemaManager schemaMgr; diff --git a/unittest/modes_test_parser.cpp b/unittest/modes_test_parser.cpp index 665e90e..081e84b 100644 --- a/unittest/modes_test_parser.cpp +++ b/unittest/modes_test_parser.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ #include +#include #include #include "supervisor/Mode.h" #include "supervisor/ModeXMLParser.h" @@ -24,19 +25,20 @@ using namespace std; MODES_NAMESPACE_BEGIN class TestParser { - public: - void parserTest(const string &fileName); - list getActionList(Mode& m); - private: +public: + void parserTest(const string &fileName); + list> getActionList(Mode& m); +private: }; MODES_NAMESPACE_END -list TestParser::getActionList(Mode& m) +list> TestParser::getActionList(Mode& m) { return m.reqList; } -TEST(XMLParser, getModeName) { +TEST(XMLParser, getModeName) +{ PluginManager pluginManager; ActionSchemaManager schemaMgr; ModeXMLParser modeparser("tizen_ex1_mode.xml", schemaMgr, pluginManager); @@ -45,7 +47,8 @@ TEST(XMLParser, getModeName) { EXPECT_EQ("ex1", modeparser.getModeName()); } -TEST(XMLParser, getMode) { +TEST(XMLParser, getMode) +{ PluginManager pluginManager; ActionSchemaManager schemaMgr; ModeXMLParser modeparser("tizen_ex2_mode.xml", schemaMgr, pluginManager); @@ -54,7 +57,8 @@ TEST(XMLParser, getMode) { //EXPECT_TRUE(mode.empty()); } -TEST(Mode, getModeName) { +TEST(Mode, getModeName) +{ PluginManager pluginManager; ActionSchemaManager schemaMgr; ModeXMLParser modeparser("tizen_ex2_mode.xml", schemaMgr, pluginManager); @@ -62,7 +66,8 @@ TEST(Mode, getModeName) { EXPECT_EQ("ex2", mode.getName()); } -TEST(Mode, getModeType) { +TEST(Mode, getModeType) +{ PluginManager pluginManager; ActionSchemaManager schemaMgr; ModeXMLParser modeparser("tizen_ex1_mode.xml", schemaMgr, pluginManager); @@ -70,17 +75,18 @@ TEST(Mode, getModeType) { EXPECT_EQ("normal", mode.getModeType()); } -TEST(Mode, printAction) { +TEST(Mode, printAction) +{ PluginManager pluginManager; ActionSchemaManager schemaMgr; ModeXMLParser modeparser("tizen_ex1_mode.xml", schemaMgr, pluginManager); Mode mode = modeparser.getMode(); TestParser parserT; - list reqList = parserT.getActionList(mode); + list> reqList = parserT.getActionList(mode); EXPECT_FALSE(reqList.empty()); - for (list::iterator it = reqList.begin(); it != reqList.end(); ++it) { + for (list>::iterator it = reqList.begin(); it != reqList.end(); ++it) { cout << "* Req : " << (*it)->getName() << endl; cout << "\t\t- Restrict : " << (*it)->getRestrict() << endl; //cout << "\t\t- Content : " << (*it)->getValue() << endl; -- 2.7.4