Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / SensorProcessor / ContextRepository.cpp
index d4b133d..7e09d07 100644 (file)
@@ -18,6 +18,7 @@
 *
 ******************************************************************/
 #include "ContextRepository.h"
+#include "rapidxml/rapidxml.hpp"
 
 static inline std::string trim_both(const std::string &str)
 {
@@ -55,7 +56,17 @@ CLEANUP:
     return res;
 }
 
-SSMRESULT CContextRepository::registerResourceFinderEvent(IN IResourceEvent *pResourceEvent)
+SSMRESULT CContextRepository::stopResourceFinder()
+{
+    SSMRESULT res = SSM_E_FAIL;
+
+    SSM_CLEANUP_ASSERT(m_resourceFinder->stopResourceFinder());
+
+CLEANUP:
+    return res;
+}
+
+SSMRESULT CContextRepository::registerResourceFinderEvent(IResourceEvent *pResourceEvent)
 {
     m_resourceEvents.push_back(pResourceEvent);
     return SSM_S_OK;
@@ -65,8 +76,8 @@ void CContextRepository::finalRelease()
 {
 }
 
-SSMRESULT CContextRepository::initRepository(IN std::string name, IN std::string type,
-        IN std::string pathSoftSensors, IN std::string pathDescription)
+SSMRESULT CContextRepository::initRepository(std::string name, std::string type,
+        std::string pathSoftSensors, std::string pathDescription)
 {
     SSMRESULT res = SSM_E_FAIL;
 
@@ -77,7 +88,7 @@ SSMRESULT CContextRepository::initRepository(IN std::string name, IN std::string
 
     if (pathSoftSensors.length() == 0)
     {
-        SSM_CLEANUP_ASSERT(GetCurrentPath(&m_pathSoftSensors));
+        SSM_CLEANUP_ASSERT(getCurrentPath(&m_pathSoftSensors));
         m_pathSoftSensors.append("/");
     }
     else
@@ -87,7 +98,7 @@ SSMRESULT CContextRepository::initRepository(IN std::string name, IN std::string
 
     if (pathDescription.length() == 0)
     {
-        SSM_CLEANUP_ASSERT(GetCurrentPath(&m_pathSoftSensorsDescription));
+        SSM_CLEANUP_ASSERT(getCurrentPath(&m_pathSoftSensorsDescription));
         m_pathSoftSensorsDescription.append("/");
         m_pathSoftSensorsDescription.append(DEFAULT_PATH_SOFT_SENSORS);
     }
@@ -108,10 +119,9 @@ SSMRESULT CContextRepository::loadXMLFromString(char *xmlData,
         std::vector<DictionaryData> *dataList)
 {
     // use  rapidxml-----------------------
-    SSMRESULT res = SSM_E_FAIL;
+    SSMRESULT res = SSM_E_INVALIDXML;
     rapidxml::xml_document< char > xmlDoc;
     //xmlDoc.parse< 0 >( &xmlData.front() );
-    xmlDoc.parse< 0 >(xmlData);
 
     std::string keyStr;
     std::string valueStr;
@@ -121,73 +131,84 @@ SSMRESULT CContextRepository::loadXMLFromString(char *xmlData,
     rapidxml::xml_node< char > *subItem2;
     rapidxml::xml_node< char > *subItem3;
 
-    // get value
-    rapidxml::xml_node< char > *root = xmlDoc.first_node();
+    rapidxml::xml_node< char > *root;
 
-    if (!root)
+    try
     {
-        SSM_CLEANUP_ASSERT(SSM_E_FAIL);
-    }
+        xmlDoc.parse< 0 >(xmlData);
 
-    for ( item = root->first_node(); item; item = item->next_sibling() )
-    {
-        DictionaryData dictionaryData;
-        for ( subItem = item->first_node(); subItem ; subItem = subItem->next_sibling() )
+        // get value
+        root = xmlDoc.first_node();
+
+        if (!root)
         {
-            //root name
-            keyStr = subItem->name();  // key
-            valueStr = subItem->value();   // value
+            throw rapidxml::parse_error("No Root Element", 0);
+        }
 
-            if (!keyStr.compare("name"))
-            {
-                dictionaryData.rootName = trim_both(valueStr);
-            }
-            ////std::cout<<keyStr << " : " << subItem->value() <<std::endl<<std::endl; //root_name
-            for (subItem2 = subItem->first_node(); subItem2 ; subItem2 = subItem2->next_sibling())
+        for (item = root->first_node(); item; item = item->next_sibling())
+        {
+            DictionaryData dictionaryData;
+            for (subItem = item->first_node(); subItem; subItem = subItem->next_sibling())
             {
-                std::map<std::string, std::string> propertyMap;
-                std::vector<std::string> enterconditionVector;
+                //root name
+                keyStr = subItem->name();  // key
+                valueStr = subItem->value();   // value
 
-                keyStr = subItem2->name();  // key
-                valueStr = subItem2->value();   // value
-
-                if (!keyStr.compare("input"))
+                if (!keyStr.compare("name"))
                 {
-                    dictionaryData.inputs.push_back(trim_both(valueStr));
+                    dictionaryData.rootName = trim_both(valueStr);
                 }
-                ////std::cout<<name << " :: " << subItem2->value() <<std::endl<<std::endl;
-                for (subItem3 = subItem2->first_node(); subItem3 ; subItem3 = subItem3->next_sibling())
+                ////std::cout<<keyStr << " : " << subItem->value() <<std::endl<<std::endl; //root_name
+                for (subItem2 = subItem->first_node(); subItem2; subItem2 = subItem2->next_sibling())
                 {
-                    std::string newKeyStr = subItem3->name();  // key
-                    valueStr = subItem3->value();   // value
+                    std::map<std::string, std::string> propertyMap;
+                    std::vector<std::string> enterconditionVector;
+
+                    keyStr = subItem2->name();  // key
+                    valueStr = subItem2->value();   // value
 
-                    if (!keyStr.compare("attribute") || !keyStr.compare("output") )
+                    if (!keyStr.compare("input"))
                     {
-                        propertyMap.insert(std::make_pair(trim_both(newKeyStr), trim_both(valueStr)));
+                        dictionaryData.inputs.push_back(trim_both(valueStr));
+                    }
+                    ////std::cout<<name << " :: " << subItem2->value() <<std::endl<<std::endl;
+                    for (subItem3 = subItem2->first_node(); subItem3; subItem3 = subItem3->next_sibling())
+                    {
+                        std::string newKeyStr = subItem3->name();  // key
+                        valueStr = subItem3->value();   // value
+
+                        if (!keyStr.compare("attribute") || !keyStr.compare("output"))
+                        {
+                            propertyMap.insert(std::make_pair(trim_both(newKeyStr), trim_both(valueStr)));
+                        }
+                    }
+                    if (!keyStr.compare("attribute"))
+                    {
+                        dictionaryData.attributeProperty.push_back(propertyMap);
+                    }
+                    else if (!keyStr.compare("output"))
+                    {
+                        dictionaryData.outputProperty.push_back(propertyMap);
                     }
-                }
-                if (!keyStr.compare("attribute"))
-                {
-                    dictionaryData.attributeProperty.push_back(propertyMap);
-                }
-                else if (!keyStr.compare("output"))
-                {
-                    dictionaryData.outputProperty.push_back(propertyMap);
                 }
             }
+            //for accurate data.
+            /*
+            dictionaryData.app_input_count = std::to_string((long long)dictionaryData.app_inputs.size());
+            dictionaryData.input_count = std::to_string((long long)dictionaryData.inputs.size());
+            dictionaryData.attribute_property_count = std::to_string((long long)dictionaryData.attribute_property.size());
+            dictionaryData.output_property_count = std::to_string((long long)dictionaryData.output_property.size());
+            */
+
+            dataList->push_back(dictionaryData);
         }
-        //for accurate data.
-        /*
-        dictionaryData.app_input_count = std::to_string((long long)dictionaryData.app_inputs.size());
-        dictionaryData.input_count = std::to_string((long long)dictionaryData.inputs.size());
-        dictionaryData.attribute_property_count = std::to_string((long long)dictionaryData.attribute_property.size());
-        dictionaryData.output_property_count = std::to_string((long long)dictionaryData.output_property.size());
-        */
-
-        dataList->push_back(dictionaryData);
-    }
 
-    res = SSM_S_OK;
+        res = SSM_S_OK;
+    }
+    catch (rapidxml::parse_error &e)
+    {
+        SSM_CLEANUP_ASSERT(SSM_E_INVALIDXML);
+    }
 
 CLEANUP:
     return res;
@@ -223,7 +244,7 @@ CLEANUP:
     return res;
 }
 
-SSMRESULT CContextRepository::getSoftSensorList(OUT std::vector<ISSMResource *> *pSoftSensorList)
+SSMRESULT CContextRepository::getSoftSensorList(std::vector<ISSMResource *> *pSoftSensorList)
 {
     for (size_t i = 0; i < m_lstSoftSensor.size(); i++)
     {
@@ -233,7 +254,7 @@ SSMRESULT CContextRepository::getSoftSensorList(OUT std::vector<ISSMResource *>
     return SSM_S_OK;
 }
 
-SSMRESULT CContextRepository::getPrimitiveSensorList(OUT std::vector<ISSMResource *>
+SSMRESULT CContextRepository::getPrimitiveSensorList(std::vector<ISSMResource *>
         *pPrimitiveSensorList)
 {
     for (size_t i = 0; i < m_lstPrimitiveSensor.size(); i++)
@@ -244,7 +265,7 @@ SSMRESULT CContextRepository::getPrimitiveSensorList(OUT std::vector<ISSMResourc
     return SSM_S_OK;
 }
 
-SSMRESULT CContextRepository::onResourceFound(IN ISSMResource *pSensor)
+SSMRESULT CContextRepository::onResourceFound(ISSMResource *pSensor)
 {
     m_lstPrimitiveSensor.push_back(pSensor);
 
@@ -256,7 +277,7 @@ SSMRESULT CContextRepository::onResourceFound(IN ISSMResource *pSensor)
     return SSM_S_OK;
 }
 
-SSMRESULT CContextRepository::onResourceLost(IN ISSMResource *pSensor)
+SSMRESULT CContextRepository::onResourceLost(ISSMResource *pSensor)
 {
     std::vector<ISSMResource *>::iterator    itor;
     itor = std::find(m_lstPrimitiveSensor.begin(), m_lstPrimitiveSensor.end(), pSensor);
@@ -270,12 +291,12 @@ SSMRESULT CContextRepository::onResourceLost(IN ISSMResource *pSensor)
     return SSM_E_FAIL;
 }
 
-SSMRESULT CContextRepository::startObserveResource(IN ISSMResource *pSensor, IN IEvent *pEvent)
+SSMRESULT CContextRepository::startObserveResource(ISSMResource *pSensor, IEvent *pEvent)
 {
     return m_resourceFinder->startObserveResource(pSensor, pEvent);
 }
 
-SSMRESULT CContextRepository::stopObserveResource(IN ISSMResource *pSensor)
+SSMRESULT CContextRepository::stopObserveResource(ISSMResource *pSensor)
 {
     return m_resourceFinder->stopObserveResource(pSensor);
 }
@@ -318,7 +339,7 @@ SSMRESULT CContextRepository::loadSoftSensor(std::string softSensorName, ICtxDel
     std::stringstream   sstream;
     SSMRESULT           res = SSM_E_FAIL;
 
-    typedef void(*InitContext)(IN ICtxDelegate *);
+    typedef void(*InitContext)(ICtxDelegate *);
     InitContext InitializeContextFunction = NULL;
 
     // load dll(so)
@@ -384,7 +405,7 @@ CLEANUP:
     return res;
 }
 
-SSMRESULT CContextRepository::GetCurrentPath(std::string *path)
+SSMRESULT CContextRepository::getCurrentPath(std::string *path)
 {
     char        buffer[2048];
     SSMRESULT   res = SSM_E_FAIL;
@@ -428,4 +449,4 @@ SSMRESULT CContextRepository::GetCurrentPath(std::string *path)
 
 CLEANUP:
     return res;
-}
\ No newline at end of file
+}