e545c2538375cc1a124e38f97179f7bcb64cb0cf
[platform/upstream/iotivity.git] / service / resource-container / src / Configuration.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "Configuration.h"
22
23 #include <stdexcept>
24 #include <utility>
25 #include "InternalTypes.h"
26
27 #define CONTAINER_TAG "RESOURCE_CONTAINER"
28
29 namespace OIC
30 {
31     namespace Service
32     {
33         static inline std::string trim_both(const std::string &str)
34         {
35             size_t npos = str.find_first_not_of(" \t\v\n\r");
36
37             if (npos == std::string::npos)
38             {
39                 return "";
40             }
41
42             std::string tempString = str.substr(npos, str.length());
43             npos = tempString.find_last_not_of(" \t\v\n\r");
44
45             return npos == std::string::npos ? tempString : tempString.substr(0, npos + 1);
46         }
47
48         Configuration::Configuration()
49         {
50             m_loaded = false;
51         }
52
53         Configuration::Configuration(string configFile)
54         {
55             m_loaded = false;
56
57             m_pathConfigFile.append(configFile);
58
59             getConfigDocument(m_pathConfigFile);
60         }
61
62         Configuration::~Configuration()
63         {
64         }
65
66         bool Configuration::isLoaded() const
67         {
68             return m_loaded;
69         }
70
71         bool Configuration::isHasInput(std::string &bundleId) const
72         {
73             try
74             {
75                 return m_mapisHasInput.at(bundleId);
76             }
77             catch (std::out_of_range &e)
78             {
79                 return false;
80             }
81         }
82
83         void Configuration::getConfiguredBundles(configInfo *configOutput)
84         {
85             rapidxml::xml_node< char > *bundle;
86             rapidxml::xml_node< char > *subItem;
87
88             string strKey, strValue;
89
90             if (m_loaded)
91             {
92                 try
93                 {
94                     for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
95                              bundle->next_sibling())
96                     {
97                         std::map< std::string, std::string > bundleMap;
98                         for (subItem = bundle->first_node(); subItem;
99                              subItem = subItem->next_sibling())
100                         {
101                             strKey = subItem->name();
102                             strValue = subItem->value();
103
104                             if (strlen(subItem->value()) > 0)
105                             {
106                                 bundleMap.insert(
107                                     std::make_pair(trim_both(strKey), trim_both(strValue)));
108                             }
109                         }
110                         configOutput->push_back(bundleMap);
111                     }
112
113                 }
114                 catch (rapidxml::parse_error &e)
115                 {
116                     OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
117                     OC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
118                 }
119             }
120         }
121
122         void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
123         {
124             rapidxml::xml_node< char > *bundle;
125
126             string strBundleId, strPath, strVersion;
127
128             if (m_loaded)
129             {
130                 try
131                 {
132                     std::map< std::string, std::string > bundleConfigMap;
133
134                     // <bundle>
135                     for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
136                              bundle->next_sibling())
137                     {
138                         // <id>
139                         strBundleId = bundle->first_node(BUNDLE_ID)->value();
140
141                         if (!strBundleId.compare(bundleId))
142                         {
143                             bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
144
145                             // <path>
146                             strPath = bundle->first_node(BUNDLE_PATH)->value();
147                             bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
148
149                             // <version>
150                             strVersion = bundle->first_node(BUNDLE_VERSION)->value();
151                             bundleConfigMap.insert(
152                                 std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
153
154                             configOutput->push_back(bundleConfigMap);
155
156                             break;
157                         }
158                     }
159                 }
160                 catch (rapidxml::parse_error &e)
161                 {
162                     OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
163                     OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
164                 }
165             }
166         }
167
168         void Configuration::getResourceConfiguration(std::string bundleId,
169                 std::vector< resourceInfo > *configOutput)
170         {
171             rapidxml::xml_node< char > *bundle;
172             rapidxml::xml_node< char > *resource;
173             rapidxml::xml_node< char > *item, *subItem, *subItem2;
174
175             string strBundleId;
176             string strKey, strValue;
177
178             if (m_loaded)
179             {
180                 try
181                 {
182                     // <bundle>
183                     for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
184                              bundle->next_sibling())
185                     {
186                         // <id>
187                         strBundleId = bundle->first_node(BUNDLE_ID)->value();
188
189                         if (!strBundleId.compare(bundleId))
190                         {
191                             // <resourceInfo>
192                             for (resource = bundle->first_node(OUTPUT_RESOURCES_TAG)->first_node(OUTPUT_RESOURCE_INFO);
193                                  resource; resource = resource->next_sibling())
194                             {
195                                 resourceInfo tempResourceInfo;
196
197                                 for (item = resource->first_node(); item; item =
198                                          item->next_sibling())
199                                 {
200                                     strKey = item->name();
201                                     strValue = item->value();
202
203                                     if (!strKey.compare(OUTPUT_RESOURCE_NAME))
204                                         tempResourceInfo.name = trim_both(strValue);
205
206                                     else if (!strKey.compare(OUTPUT_RESOURCE_URI))
207                                         tempResourceInfo.uri = trim_both(strValue);
208
209                                     else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
210                                         tempResourceInfo.address = trim_both(strValue);
211
212                                     else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
213                                         tempResourceInfo.resourceType = trim_both(strValue);
214
215                                     else
216                                     {
217                                         for (subItem = item->first_node(); subItem; subItem =
218                                                  subItem->next_sibling())
219                                         {
220                                             map< string, string > propertyMap;
221
222                                             strKey = subItem->name();
223
224                                             if (strKey.compare(INPUT_RESOURCE))
225                                             {
226                                                 m_mapisHasInput[strBundleId] = true;
227                                             }
228
229                                             for (subItem2 = subItem->first_node(); subItem2;
230                                                  subItem2 = subItem2->next_sibling())
231                                             {
232                                                 string newStrKey = subItem2->name();
233                                                 string newStrValue = subItem2->value();
234
235                                                 propertyMap[trim_both(newStrKey)] = trim_both(
236                                                                                         newStrValue);
237                                             }
238
239                                             tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(
240                                                 propertyMap);
241                                         }
242                                     }
243                                 }
244                                 configOutput->push_back(tempResourceInfo);
245                             }
246                         }
247                     }
248                 }
249                 catch (rapidxml::parse_error &e)
250                 {
251                     OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
252                     OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
253                 }
254             }
255         }
256
257         void Configuration::getConfigDocument(std::string pathConfigFile)
258         {
259             std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
260
261             if (!xmlFile.fail())
262             {
263                 xmlFile.seekg(0, std::ios::end);
264                 unsigned int size = (unsigned int) xmlFile.tellg();
265                 xmlFile.seekg(0);
266
267                 std::vector< char > xmlData(size + 1);
268                 xmlData[size] = 0;
269
270                 xmlFile.read(&xmlData.front(), (std::streamsize) size);
271                 xmlFile.close();
272                 m_strConfigData = std::string(xmlData.data());
273
274                 try
275                 {
276                     m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
277                     m_loaded = true;
278                 }
279                 catch (rapidxml::parse_error &e)
280                 {
281                     OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
282                     OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
283                 }
284             }
285             else
286             {
287                 OC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");
288             }
289         }
290     }
291 }