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
76 OC_LOG_V(INFO, CONTAINER_TAG, "isHasInput: (%d) %s",m_mapisHasInput.at(bundleId), bundleId.c_str() );
77 return m_mapisHasInput.at(bundleId);
79 catch (std::out_of_range &e)
81 OC_LOG_V(INFO, CONTAINER_TAG, "isHasInput out of range %s.", bundleId.c_str());
86 void Configuration::getConfiguredBundles(configInfo *configOutput)
88 rapidxml::xml_node< char > *bundle;
89 rapidxml::xml_node< char > *subItem;
91 string strKey, strValue;
97 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
98 bundle->next_sibling())
100 std::map< std::string, std::string > bundleMap;
101 for (subItem = bundle->first_node(); subItem;
102 subItem = subItem->next_sibling())
104 strKey = subItem->name();
105 strValue = subItem->value();
107 if (strlen(subItem->value()) > 0)
110 std::make_pair(trim_both(strKey), trim_both(strValue)));
113 configOutput->push_back(bundleMap);
117 catch (rapidxml::parse_error &e)
119 OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
120 OC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
125 void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
127 rapidxml::xml_node< char > *bundle;
129 string strBundleId, strPath, strVersion;
135 std::map< std::string, std::string > bundleConfigMap;
138 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
139 bundle->next_sibling())
142 strBundleId = bundle->first_node(BUNDLE_ID)->value();
144 if (!strBundleId.compare(bundleId))
146 bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
149 strPath = bundle->first_node(BUNDLE_PATH)->value();
150 bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
153 strVersion = bundle->first_node(BUNDLE_VERSION)->value();
154 bundleConfigMap.insert(
155 std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
157 configOutput->push_back(bundleConfigMap);
163 catch (rapidxml::parse_error &e)
165 OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
166 OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
171 void Configuration::getResourceConfiguration(std::string bundleId,
172 std::vector< resourceInfo > *configOutput)
174 rapidxml::xml_node< char > *bundle;
175 rapidxml::xml_node< char > *resource;
176 rapidxml::xml_node< char > *item, *subItem, *subItem2;
179 string strKey, strValue;
180 OC_LOG(INFO, CONTAINER_TAG, "Loading resource configuration!");
187 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
188 bundle->next_sibling())
191 strBundleId = bundle->first_node(BUNDLE_ID)->value();
193 OC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !", strBundleId.c_str(), bundleId.c_str());
195 if (!strBundleId.compare(bundleId))
197 OC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
199 for (resource = bundle->first_node(OUTPUT_RESOURCES_TAG)->first_node(OUTPUT_RESOURCE_INFO);
200 resource; resource = resource->next_sibling())
202 resourceInfo tempResourceInfo;
204 for (item = resource->first_node(); item; item =
205 item->next_sibling())
207 strKey = item->name();
208 strValue = item->value();
210 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
211 tempResourceInfo.name = trim_both(strValue);
213 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
214 tempResourceInfo.uri = trim_both(strValue);
216 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
217 tempResourceInfo.address = trim_both(strValue);
219 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
220 tempResourceInfo.resourceType = trim_both(strValue);
224 for (subItem = item->first_node(); subItem; subItem =
225 subItem->next_sibling())
227 map< string, string > propertyMap;
229 strKey = subItem->name();
231 if (strKey.compare(INPUT_RESOURCE))
233 m_mapisHasInput[strBundleId] = true;
234 OC_LOG_V(INFO, CONTAINER_TAG, "Bundle has input (%s)", strBundleId.c_str());
237 for (subItem2 = subItem->first_node(); subItem2;
238 subItem2 = subItem2->next_sibling())
240 string newStrKey = subItem2->name();
241 string newStrValue = subItem2->value();
243 propertyMap[trim_both(newStrKey)] = trim_both(
247 tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(
252 configOutput->push_back(tempResourceInfo);
257 catch (rapidxml::parse_error &e)
259 OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
260 OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
264 OC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
268 void Configuration::getConfigDocument(std::string pathConfigFile)
270 std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
274 xmlFile.seekg(0, std::ios::end);
275 unsigned int size = (unsigned int) xmlFile.tellg();
278 std::vector< char > xmlData(size + 1);
281 xmlFile.read(&xmlData.front(), (std::streamsize) size);
283 m_strConfigData = std::string(xmlData.data());
287 m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
290 catch (rapidxml::parse_error &e)
292 OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
293 OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
298 OC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");