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