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 m_pathConfigFile.append(configFile);
55 getConfigDocument(m_pathConfigFile);
58 Configuration::~Configuration()
62 bool Configuration::isLoaded()
67 void Configuration::getConfiguredBundles(configInfo *configOutput)
69 rapidxml::xml_node< char > *bundle;
70 rapidxml::xml_node< char > *subItem;
72 string strKey, strValue;
78 //cout << "Name of first node is: " << m_xmlDoc.first_node()->name() << endl;
80 for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle =
81 bundle->next_sibling())
83 std::map< std::string, std::string > bundleMap;
84 //cout << "Bundle: " << bundle->name() << endl;
85 for (subItem = bundle->first_node(); subItem;
86 subItem = subItem->next_sibling())
88 strKey = subItem->name();
89 strValue = subItem->value();
91 if (strlen(subItem->value()) > 0)
94 std::make_pair(trim_both(strKey), trim_both(strValue)));
95 //cout << strKey << " " << strValue << endl;
98 configOutput->push_back(bundleMap);
102 catch (rapidxml::parse_error &e)
104 cout << "xml parsing failed !!" << endl;
105 cout << e.what() << endl;
110 void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
112 rapidxml::xml_node< char > *bundle;
114 string strBundleId, strPath, strVersion;
120 std::map< std::string, std::string > bundleConfigMap;
123 for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle =
124 bundle->next_sibling())
127 strBundleId = bundle->first_node("id")->value();
129 if (!strBundleId.compare(bundleId))
131 bundleConfigMap.insert(std::make_pair("id", trim_both(strBundleId)));
134 strPath = bundle->first_node("path")->value();
135 bundleConfigMap.insert(std::make_pair("path", trim_both(strPath)));
138 strVersion = bundle->first_node("version")->value();
139 bundleConfigMap.insert(
140 std::make_pair("version", trim_both(strVersion)));
142 configOutput->push_back(bundleConfigMap);
148 catch (rapidxml::parse_error &e)
150 cout << "xml parsing failed !!" << endl;
151 cout << e.what() << endl;
156 void Configuration::getResourceConfiguration(std::string bundleId,
157 std::vector< resourceInfo > *configOutput)
159 rapidxml::xml_node< char > *bundle;
160 rapidxml::xml_node< char > *resource;
161 rapidxml::xml_node< char > *item, *subItem, *subItem2;
164 string strKey, strValue;
171 for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle =
172 bundle->next_sibling())
175 strBundleId = bundle->first_node("id")->value();
177 if (!strBundleId.compare(bundleId))
180 for (resource = bundle->first_node("resources")->first_node(
181 "resourceInfo"); resource; resource = resource->next_sibling())
183 resourceInfo tempResourceInfo;
185 for (item = resource->first_node(); item; item =
186 item->next_sibling())
188 strKey = item->name();
189 strValue = item->value();
191 if (!strKey.compare("name"))
192 tempResourceInfo.name = trim_both(strValue);
194 else if (!strKey.compare("uri"))
195 tempResourceInfo.uri = trim_both(strValue);
197 else if (!strKey.compare("address"))
198 tempResourceInfo.address = trim_both(strValue);
200 else if (!strKey.compare("resourceType"))
201 tempResourceInfo.resourceType = trim_both(strValue);
205 for (subItem = item->first_node(); subItem; subItem =
206 subItem->next_sibling())
208 map< string, string > propertyMap;
210 strKey = subItem->name();
212 for (subItem2 = subItem->first_node(); subItem2;
213 subItem2 = subItem2->next_sibling())
215 string newStrKey = subItem2->name();
216 string newStrValue = subItem2->value();
218 propertyMap[trim_both(newStrKey)] = trim_both(
222 tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(
227 configOutput->push_back(tempResourceInfo);
232 catch (rapidxml::parse_error &e)
234 std::cout << "xml parsing failed !!" << std::endl;
235 std::cout << e.what() << std::endl;
240 void Configuration::getConfigDocument(std::string pathConfigFile)
242 std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
246 xmlFile.seekg(0, std::ios::end);
247 unsigned int size = (unsigned int) xmlFile.tellg();
250 std::vector< char > xmlData(size + 1);
253 xmlFile.read(&xmlData.front(), (std::streamsize) size);
255 m_strConfigData = std::string(xmlData.data());
259 m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
262 catch (rapidxml::parse_error &e)
264 std::cout << "xml parsing failed !!" << std::endl;
265 std::cout << e.what() << std::endl;
270 std::cout << "Configuration File load failed !!" << std::endl;