remove ModeParser
authorYoungjae Shin <yj99.shin@samsung.com>
Tue, 12 May 2020 06:35:59 +0000 (15:35 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Wed, 13 May 2020 07:30:22 +0000 (16:30 +0900)
The ModeParser is not useful because the mode description is fixed on XML.

supervisor/ModeManager.cpp
supervisor/ModeManager.h
supervisor/ModeParser.h [deleted file]
supervisor/ModeXMLParser.cpp
supervisor/ModeXMLParser.h

index 48b91f157b375e779d7b05ba5182330f12c97e60..3a1d6d67ef5e81d2e33c29a8464893c9e32e4133 100644 (file)
@@ -46,28 +46,17 @@ void ModeManager::init()
        }
 }
 
-// TODO: apply better polymorphism
-ModeParser* ModeManager::getModeParser(const string &path)
+void ModeManager::addModeAttributes(ModeXMLParser &parser, const string &path)
 {
-       const size_t suffixLen = sizeof("xml") - 1;
-       if (suffixLen < path.length()
-                       || path.compare(path.length() - suffixLen, suffixLen, "xml")) {
-               return new ModeXMLParser(path, ruleMgr);
-       } else {
-               ERR("Wrong path(%s)", path.c_str());
-               throw std::invalid_argument("Wrong path");
-       }
-}
-
-void ModeManager::addModeAttributes(ModeParser *parser, const string &path)
-{
-       Mode mode = parser->getMode();
-       modeMap.insert(std::make_pair(mode.getName(), std::make_tuple(path, mode.getModeType(), mode.isHidden())));
+       Mode mode = parser.getMode();
+       auto infos = std::make_tuple(path, mode.getModeType(), mode.isHidden());
+       modeMap.insert(std::make_pair(mode.getName(), infos));
 
        if (careTaker.hasUndoInfo(mode.getName()))
                careTaker.restoreMode(mode);
 
-       DBG("[%zu] modeName : %s, modePath : %s, type : %d, hidden : %s", modeMap.size(), mode.getName().c_str(), path.c_str(), mode.getModeType(), mode.isHidden()? "true": "false");
+       DBG("[%zu]Name(%s), Path(%s), Type(%d), hidden(%s)", modeMap.size(), mode.getName().c_str(),
+               path.c_str(), mode.getModeType(), mode.isHidden() ? "true": "false");
 }
 
 int ModeManager::applyMode(const string &modeName, ClientPrivilege &priv, bool isTest)
@@ -82,8 +71,8 @@ int ModeManager::applyMode(const string &modeName, ClientPrivilege &priv, bool i
        DBG("applyMode(%s)", path.c_str());
 
        try {
-               std::unique_ptr<ModeParser> parser(getModeParser(path));
-               Mode mode = parser->getMode();
+               ModeXMLParser parser(path, ruleMgr);
+               Mode mode = parser.getMode();
 
                DBG("applyMode(%s) Run", mode.getName().c_str());
 
@@ -128,10 +117,10 @@ int ModeManager::applyMode(const string &modeName, ClientPrivilege &priv, bool i
 
 int ModeManager::addMode(const Mode &mode)
 {
-       string filename = MODES_CUSTOM_MODE_DEFAULT_DIR "/tizen_" + mode.getName() + "_mode.xml";
+       string path = MODES_CUSTOM_MODE_DEFAULT_DIR "/tizen_" + mode.getName() + "_mode.xml";
        XMLGenerator xmlWriter;
 
-       auto element = std::make_pair(mode.getName(), std::make_tuple(filename, mode.getModeType(), false));
+       auto element = std::make_pair(mode.getName(), std::make_tuple(path, mode.getModeType(), false));
        auto result = modeMap.insert(element);
        if (false == result.second) {
                ERR("Invalid mode(%s), already exists", mode.getName().c_str());
@@ -139,14 +128,15 @@ int ModeManager::addMode(const Mode &mode)
        }
 
        try {
-               xmlWriter.makeModeXML(filename, mode);
+               xmlWriter.makeModeXML(path, mode);
        } catch (ModesEx &e) {
                ERR("XML generator Fail(%s)", e.what());
                modeMap.erase(result.first);
                return MODES_ERROR_INVALID_PARAMETER;
        }
 
-       DBG("[%zu] addMode modeName : %s, modePath : %s, type : %d, hidden : false", modeMap.size(), mode.getName().c_str(), filename.c_str(), mode.getModeType());
+       DBG("[%zu]addMode : Name(%s), Path(%s), Type(%d)", modeMap.size(), mode.getName().c_str(),
+               path.c_str(), mode.getModeType());
 
        return MODES_ERROR_NONE;
 }
@@ -164,19 +154,18 @@ int ModeManager::removeMode(const string &modeName)
                return MODES_ERROR_INVALID_PARAMETER;
        }
 
-       string filename = std::get<0>(result->second);
-       if (string::npos == filename.find(MODES_CUSTOM_MODE_DEFAULT_DIR))
-       {
+       string path = std::get<0>(result->second);
+       if (string::npos == path.find(MODES_CUSTOM_MODE_DEFAULT_DIR)) {
                ERR("Invalid mode(%s), custom mode only can be removed.", modeName.c_str());
                return MODES_ERROR_INVALID_PARAMETER;
        }
 
        modeMap.erase(result);
 
-       if (0 != remove(filename.c_str()))
-               ERR("remove(%s) Fail(%d)", filename.c_str(), errno);
+       if (0 != remove(path.c_str()))
+               ERR("remove(%s) Fail(%d)", path.c_str(), errno);
 
-       DBG("[%zu] Remove modeName : %s, modePath : %s", modeMap.size(), modeName.c_str(), filename.c_str());
+       DBG("[%zu]removeMode : Name(%s), Path(%s)", modeMap.size(), modeName.c_str(), path.c_str());
 
        return MODES_ERROR_NONE;
 }
@@ -270,10 +259,9 @@ bool ModeManager::makeModeMap(const string &dirPath)
 
                string fileFullPath = dirPath + "/" + string(entry->d_name);
                try {
-                       ModeParser *modeParser = getModeParser(fileFullPath);
-                       modeParser->validateMode(modeSyntaxFile);
+                       ModeXMLParser modeParser(fileFullPath, ruleMgr);
+                       modeParser.validateMode(modeSyntaxFile);
                        addModeAttributes(modeParser, fileFullPath);
-                       delete modeParser;
                } catch (ModesEx &e) {
                        ERR("parser(%s) Fail(%s)", fileFullPath.c_str(), e.what());
                } catch (std::invalid_argument &e) {
index 7953d63cdcdcc229468d1cb8ed81821082d84fcb..36f7a8f9b209711c742142586f62043284c8c564 100644 (file)
 #include <utility>
 #include <set>
 #include "mdss.h"
-#include "ModeParser.h"
 #include "RuleManager.h"
-#include "ModeCareTaker.h"
 #include "ModeObserver.h"
+#include "ModeCareTaker.h"
+#include "ModeXMLParser.h"
 #include "ClientPrivilege.h"
 
 MODES_NAMESPACE_BEGIN
@@ -44,8 +44,7 @@ public:
 private:
        void notifyObservers(const std::string &modeName, ModeObserver::ModeState state);
        bool makeModeMap(const std::string &dirPath);
-       void addModeAttributes(ModeParser *parser, const std::string &path);
-       ModeParser* getModeParser(const std::string &path);
+       void addModeAttributes(ModeXMLParser &parser, const std::string &path);
 
        std::map<std::string, std::tuple<std::string, int, bool>> modeMap;
        std::set<std::string> modeDirList;
diff --git a/supervisor/ModeParser.h b/supervisor/ModeParser.h
deleted file mode 100644 (file)
index d32e19e..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <utility>
-#include "mdss.h"
-#include "Mode.h"
-#include "RuleManager.h"
-
-MODES_NAMESPACE_BEGIN
-
-class ModeParser {
-public:
-       virtual ~ModeParser() = default;
-
-       virtual Mode getMode() = 0;
-       virtual void validateMode(const std::string &syntaxFile) = 0;
-protected:
-       ModeParser(RuleManager &rMgr) :ruleMgr(rMgr)
-       {
-       }
-       RuleManager &ruleMgr;
-};
-
-MODES_NAMESPACE_END
index cdd8f12bb281cd34639ae4666e72eccf26fa8a06..ebe8a38f5b9bbe5fbe0c9d75f44a2bfd5d4632dd 100644 (file)
@@ -29,7 +29,7 @@ MODES_NAMESPACE_USE;
 
 
 ModeXMLParser::ModeXMLParser(const std::string &modeFile, RuleManager &rMgr)
-       : ModeParser(rMgr), XMLParser(modeFile), isParsedModeAttr(false)
+       : XMLParser(modeFile), ruleMgr(rMgr)
 {
 }
 
index 1edb0431093e0da9f55847a77ebed84e48833db4..f7bbd5fbc607b580cdf00b3bceb40416530f38ea 100644 (file)
 
 #include <libxml/tree.h>
 #include "mdss.h"
-#include "ModeParser.h"
+#include "Mode.h"
 #include "XMLParser.h"
+#include "RuleManager.h"
 
 MODES_NAMESPACE_BEGIN
 
-class ModeXMLParser : public ModeParser, public XMLParser {
+class ModeXMLParser : public XMLParser {
 public:
        ModeXMLParser(const std::string &modeFile, RuleManager &mgr);
        ~ModeXMLParser() = default;
 
-       Mode getMode() override;
-       void validateMode(const std::string &xsd) override;
+       Mode getMode();
+       void validateMode(const std::string &xsd);
 
 private:
        void parseModeAttr(xmlNodePtr node);
@@ -39,12 +40,11 @@ private:
        void parseAction(xmlNodePtr node);
        void parseUndo(xmlNodePtr node);
        Action* parseActionInfo(xmlNodePtr node);
-       void parseModeAttrOnly();
        void iterateElement(xmlNodePtr node);
 
        Mode mode;
        std::string filePath;
-       bool isParsedModeAttr;
+       RuleManager &ruleMgr;
 };
 
 MODES_NAMESPACE_END