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"
25 #include "InternalTypes.h"
27 #define CONTAINER_TAG "RESOURCE_CONTAINER"
33 static inline std::string trim_both(const std::string &str)
35 size_t npos = str.find_first_not_of(" \t\v\n\r");
37 if (npos == std::string::npos)
42 std::string tempString = str.substr(npos, str.length());
43 npos = tempString.find_last_not_of(" \t\v\n\r");
45 return npos == std::string::npos ? tempString : tempString.substr(0, npos + 1);
48 Configuration::Configuration()
53 Configuration::Configuration(string configFile)
57 m_pathConfigFile.append(configFile);
59 getConfigDocument(m_pathConfigFile);
62 Configuration::~Configuration()
66 bool Configuration::isLoaded() const
71 bool Configuration::isHasInput(std::string &bundleId) const
75 return m_mapisHasInput.at(bundleId);
77 catch (std::out_of_range &e)
83 void Configuration::getConfiguredBundles(configInfo *configOutput)
85 rapidxml::xml_node< char > *bundle;
86 rapidxml::xml_node< char > *subItem;
88 string strKey, strValue;
94 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
95 bundle->next_sibling())
97 std::map< std::string, std::string > bundleMap;
98 for (subItem = bundle->first_node(); subItem;
99 subItem = subItem->next_sibling())
101 strKey = subItem->name();
102 strValue = subItem->value();
104 if (strlen(subItem->value()) > 0)
107 std::make_pair(trim_both(strKey), trim_both(strValue)));
110 configOutput->push_back(bundleMap);
114 catch (rapidxml::parse_error &e)
116 OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
117 OC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
122 void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
124 rapidxml::xml_node< char > *bundle;
126 string strBundleId, strPath, strVersion;
132 std::map< std::string, std::string > bundleConfigMap;
135 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
136 bundle->next_sibling())
139 strBundleId = bundle->first_node(BUNDLE_ID)->value();
141 if (!strBundleId.compare(bundleId))
143 bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
146 strPath = bundle->first_node(BUNDLE_PATH)->value();
147 bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
150 strVersion = bundle->first_node(BUNDLE_VERSION)->value();
151 bundleConfigMap.insert(
152 std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
154 configOutput->push_back(bundleConfigMap);
160 catch (rapidxml::parse_error &e)
162 OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
163 OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
168 void Configuration::getResourceConfiguration(std::string bundleId,
169 std::vector< resourceInfo > *configOutput)
171 rapidxml::xml_node< char > *bundle;
172 rapidxml::xml_node< char > *resource;
173 rapidxml::xml_node< char > *item, *subItem, *subItem2;
176 string strKey, strValue;
183 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
184 bundle->next_sibling())
187 strBundleId = bundle->first_node(BUNDLE_ID)->value();
189 if (!strBundleId.compare(bundleId))
192 for (resource = bundle->first_node(OUTPUT_RESOURCES_TAG)->first_node(OUTPUT_RESOURCE_INFO);
193 resource; resource = resource->next_sibling())
195 resourceInfo tempResourceInfo;
197 for (item = resource->first_node(); item; item =
198 item->next_sibling())
200 strKey = item->name();
201 strValue = item->value();
203 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
204 tempResourceInfo.name = trim_both(strValue);
206 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
207 tempResourceInfo.uri = trim_both(strValue);
209 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
210 tempResourceInfo.address = trim_both(strValue);
212 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
213 tempResourceInfo.resourceType = trim_both(strValue);
217 for (subItem = item->first_node(); subItem; subItem =
218 subItem->next_sibling())
220 map< string, string > propertyMap;
222 strKey = subItem->name();
224 if (strKey.compare(INPUT_RESOURCE))
226 m_mapisHasInput[strBundleId] = true;
229 for (subItem2 = subItem->first_node(); subItem2;
230 subItem2 = subItem2->next_sibling())
232 string newStrKey = subItem2->name();
233 string newStrValue = subItem2->value();
235 propertyMap[trim_both(newStrKey)] = trim_both(
239 tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(
244 configOutput->push_back(tempResourceInfo);
249 catch (rapidxml::parse_error &e)
251 OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
252 OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
257 void Configuration::getConfigDocument(std::string pathConfigFile)
259 std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
263 xmlFile.seekg(0, std::ios::end);
264 unsigned int size = (unsigned int) xmlFile.tellg();
267 std::vector< char > xmlData(size + 1);
270 xmlFile.read(&xmlData.front(), (std::streamsize) size);
272 m_strConfigData = std::string(xmlData.data());
276 m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
279 catch (rapidxml::parse_error &e)
281 OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
282 OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
287 OC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");