1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "Configuration.h"
27 static inline std::string trim_both(const std::string &str)
29 int npos = str.find_first_not_of(" \t\v\n\r");
36 unsigned int n = (unsigned int) npos;
37 std::string tempString = n == std::string::npos ? str : str.substr(n, str.length());
39 n = tempString.find_last_not_of(" \t\v\n\r");
41 return n == std::string::npos ? tempString : tempString.substr(0, n + 1);
44 Configuration::Configuration()
49 Configuration::Configuration(string configFile)
53 getCurrentPath(&m_pathConfigFile);
54 m_pathConfigFile.append("/");
55 m_pathConfigFile.append(configFile);
57 getConfigDocument(m_pathConfigFile);
60 Configuration::~Configuration()
64 bool Configuration::isLoaded()
69 void Configuration::getConfiguredBundles(configInfo *configOutput)
71 rapidxml::xml_node< char > *bundle;
72 rapidxml::xml_node< char > *subItem;
74 string strKey, strValue;
80 //cout << "Name of first node is: " << m_xmlDoc.first_node()->name() << endl;
82 for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling())
84 std::map< std::string, std::string > bundleMap;
85 //cout << "Bundle: " << bundle->name() << endl;
86 for (subItem = bundle->first_node(); subItem; subItem = subItem->next_sibling())
88 strKey = subItem->name();
89 strValue = subItem->value();
91 if (strlen(subItem->value()) > 0)
93 bundleMap.insert(std::make_pair(trim_both(strKey), trim_both(strValue)));
94 //cout << strKey << " " << strValue << endl;
97 configOutput->push_back(bundleMap);
101 catch (rapidxml::parse_error &e)
103 cout << "xml parsing failed !!" << endl;
104 cout << e.what() << endl;
109 void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
111 rapidxml::xml_node< char > *bundle;
113 string strBundleId, strPath, strVersion;
119 std::map< std::string, std::string > bundleConfigMap;
122 for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling())
125 strBundleId = bundle->first_node("id")->value();
127 if (!strBundleId.compare(bundleId))
129 bundleConfigMap.insert(std::make_pair("id", trim_both(strBundleId)));
132 strPath = bundle->first_node("path")->value();
133 bundleConfigMap.insert(std::make_pair("path", trim_both(strPath)));
136 strVersion = bundle->first_node("version")->value();
137 bundleConfigMap.insert(std::make_pair("version", trim_both(strVersion)));
139 configOutput->push_back(bundleConfigMap);
145 catch (rapidxml::parse_error &e)
147 cout << "xml parsing failed !!" << endl;
148 cout << e.what() << endl;
153 void Configuration::getResourceConfiguration(std::string bundleId,
154 std::vector< resourceInfo > *configOutput)
156 rapidxml::xml_node< char > *bundle;
157 rapidxml::xml_node< char > *resource;
158 rapidxml::xml_node< char > *item, *subItem, *subItem2;
161 string strKey, strValue;
168 for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling())
171 strBundleId = bundle->first_node("id")->value();
173 if (!strBundleId.compare(bundleId))
176 for (resource = bundle->first_node("resources")->first_node("resourceInfo"); resource;
177 resource = resource->next_sibling())
179 resourceInfo tempResourceInfo;
181 for (item = resource->first_node(); item; item = item->next_sibling())
183 strKey = item->name();
184 strValue = item->value();
186 if (!strKey.compare("name"))
187 tempResourceInfo.name = trim_both(strValue);
189 else if (!strKey.compare("uri"))
190 tempResourceInfo.uri = trim_both(strValue);
192 else if (!strKey.compare("address"))
193 tempResourceInfo.address = trim_both(strValue);
195 else if (!strKey.compare("resourceType"))
196 tempResourceInfo.resourceType = trim_both(strValue);
200 for (subItem = item->first_node(); subItem; subItem = subItem->next_sibling())
202 map< string, string > propertyMap;
204 strKey = subItem->name();
206 for (subItem2 = subItem->first_node(); subItem2; subItem2 = subItem2->next_sibling())
208 string newStrKey = subItem2->name();
209 string newStrValue = subItem2->value();
211 propertyMap[trim_both(newStrKey)] = trim_both(newStrValue);
214 tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(propertyMap);
218 configOutput->push_back(tempResourceInfo);
223 catch (rapidxml::parse_error &e)
225 std::cout << "xml parsing failed !!" << std::endl;
226 std::cout << e.what() << std::endl;
231 void Configuration::getConfigDocument(std::string pathConfigFile)
233 std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
237 xmlFile.seekg(0, std::ios::end);
238 unsigned int size = (unsigned int) xmlFile.tellg();
241 std::vector< char > xmlData(size + 1);
244 xmlFile.read(&xmlData.front(), (std::streamsize) size);
246 m_strConfigData = std::string(xmlData.data());
250 m_xmlDoc.parse< 0 >((char *)m_strConfigData.c_str());
253 catch (rapidxml::parse_error &e)
255 std::cout << "xml parsing failed !!" << std::endl;
256 std::cout << e.what() << std::endl;
261 std::cout << "Configuration File load failed !!" << std::endl;
265 void Configuration::getCurrentPath(std::string *pPath)
268 char *strPath = NULL;
270 int length = readlink("/proc/self/exe", buffer, 2047);
272 buffer[length] = '\0';
274 strPath = strrchr(buffer, '/');
278 pPath->append(buffer);