Update User Agent String
[framework/web/wrt-commons.git] / modules / widget_dao / dao / config_parser_data.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file        config_parser_data.cpp
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version     0.1
20  * @brief
21  */
22 #include <dpl/wrt-dao-ro/config_parser_data.h>
23 #include <dpl/log/log.h>
24 #include <libxml/xmlreader.h>
25 #include <libxml/xmlstring.h>
26
27 namespace WrtDB {
28
29 bool IsSpace(const xmlChar* str)
30 {
31     int charlen = xmlUTF8Size(str);
32
33     switch (charlen) {
34     case 1:
35         switch (*str) {
36         case 0x09:
37         case 0x0a:
38         case 0x0b:
39         case 0x0c:
40         case 0x0d:
41         case 0x20:
42             return true;
43
44         default:
45             return false;
46         }
47
48     case 2:
49         switch (*(str + 1)) {
50         case 0x85:
51         case 0xa0:
52             return true;
53
54         default:
55             return false;
56         }
57
58     case 3:
59         switch (*str) {
60         case 0xe1:
61         {
62             unsigned char c2 = *(str + 1);
63             unsigned char c3 = *(str + 2);
64             if ((c2 == 0x9a && c3 == 0x80) || (c2 == 0xa0 && c3 == 0x8e)) {
65                 return true;
66             } else {
67                 return false;
68             }
69         }
70
71         case 0xe2:
72             switch (*(str + 1)) {
73             case 0x80:
74                 switch (*(str + 2)) {
75                 case 0x80:
76                 case 0x81:
77                 case 0x82:
78                 case 0x83:
79                 case 0x84:
80                 case 0x85:
81                 case 0x86:
82                 case 0x87:
83                 case 0x88:
84                 case 0x89:
85                 case 0x8a:
86                 case 0xa8:
87                 case 0xa9:
88                 case 0xaf:
89                     return true;
90                 }
91             case 0x81:
92                 if (*(str + 2) == 0x9f) {
93                     return true;
94                 } else {
95                     return false;
96                 }
97
98             default:
99                 return false;
100             }
101         case 0xe3:
102             if (*(str + 1) == 0x80 && *(str + 2) == 0x80) {
103                 return true;
104             } else {
105                 return false;
106             }
107
108         default:
109             return false;
110         }
111
112     default:
113         return false;
114     }
115 }
116
117 bool CopyChar(xmlChar* out,
118         xmlChar* in)
119 {
120     int size = xmlUTF8Size(in);
121     switch (size) {
122     case 6:
123         out[5] = in[5];
124     case 5:
125         out[4] = in[4];
126     case 4:
127         out[3] = in[3];
128     case 3:
129         out[2] = in[2];
130     case 2:
131         out[1] = in[1];
132     case 1:
133         out[0] = in[0];
134         return true;
135
136     default:
137         return false;
138     }
139 }
140
141 //TODO temporary fix until commits the rewrite of this functionality.
142 void NormalizeString(DPL::String& str)
143 {
144     DPL::Optional<DPL::String> opt = str;
145     NormalizeString(opt);
146     str = *opt;
147 }
148
149 void NormalizeString (DPL::Optional<DPL::String>& txt)
150 {
151     if (!!txt) {
152         std::string tmp = DPL::ToUTF8String(*txt);
153         const xmlChar* str = reinterpret_cast<const xmlChar*>(tmp.c_str());
154         if (!xmlCheckUTF8(str)) {
155             LogError("Not valid UTF8");
156             return;
157         }
158
159         int i = 0;
160         xmlChar* c;
161         while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
162             if (!IsSpace(c)) {
163                 break;
164             }
165             ++i;
166         }
167
168         xmlChar* tmpnew = xmlUTF8Strndup(c, xmlUTF8Strlen(c) + 1);
169         bool first = false;
170         xmlChar* s = tmpnew;
171         while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
172             if (IsSpace(c)) {
173                 first = true;
174                 ++i;
175             } else {
176                 if (c[0] == 0x0) {
177                     break;
178                 }
179                 if (first) {
180                     xmlChar space[6] = { 0x20 };
181                     CopyChar(s, space);
182                     s += xmlUTF8Size(s);
183                     first = false;
184                 }
185                 CopyChar(s, c);
186                 s += xmlUTF8Size(s);
187                 ++i;
188             }
189         }
190         s[0] = 0x0;
191         txt = DPL::FromUTF8String(reinterpret_cast<char*>(tmpnew));
192         xmlFree(tmpnew);
193     }
194 }
195
196 bool ConfigParserData::Param::operator==(const Param& other) const
197 {
198     return name == other.name && value == other.value;
199 }
200
201 bool ConfigParserData::Param::operator!=(const Param& other) const
202 {
203     return name != other.name || value != other.value;
204 }
205
206 bool ConfigParserData::Param::operator >(const Param& other) const
207 {
208     if (name == other.name) {
209         return value > other.value;
210     } else {
211         return name > other.name;
212     }
213 }
214
215 bool ConfigParserData::Param::operator>=(const Param& other) const
216 {
217     if (name >= other.name) {
218         return true;
219     } else {
220         return value >= other.value;
221     }
222 }
223
224 bool ConfigParserData::Param::operator <(const Param& other) const
225 {
226     if (name == other.name) {
227         return value < other.value;
228     } else {
229         return name < other.name;
230     }
231 }
232
233 bool ConfigParserData::Param::operator<=(const Param& other) const
234 {
235     if (name <= other.name) {
236         return true;
237     } else {
238         return value <= other.value;
239     }
240 }
241
242 bool ConfigParserData::Feature::operator==(const Feature& other) const
243 {
244     return name == other.name && paramsList == other.paramsList;
245 }
246
247 bool ConfigParserData::Feature::operator!=(const Feature& other) const
248 {
249     return name != other.name || paramsList != other.paramsList;
250 }
251
252 bool ConfigParserData::Feature::operator >(const Feature& other) const
253 {
254     if (name > other.name) {
255         return true;
256     }
257     if (name < other.name) {
258         return false;
259     }
260     return paramsList > other.paramsList;
261 }
262
263 bool ConfigParserData::Feature::operator>=(const Feature& other) const
264 {
265     if (name > other.name) {
266         return true;
267     }
268     if (name < other.name) {
269         return false;
270     }
271     return paramsList >= other.paramsList;
272 }
273
274 bool ConfigParserData::Feature::operator <(const Feature& other) const
275 {
276     if (name < other.name) {
277         return true;
278     }
279     if (name > other.name) {
280         return false;
281     }
282     return paramsList < other.paramsList;
283 }
284
285 bool ConfigParserData::Feature::operator<=(const Feature& other) const
286 {
287     if (name < other.name) {
288         return true;
289     }
290     if (name > other.name) {
291         return false;
292     }
293     return paramsList <= other.paramsList;
294 }
295
296 bool ConfigParserData::Icon::operator==(const Icon& other) const
297 {
298     return src == other.src;
299 }
300
301 bool ConfigParserData::Icon::operator!=(const Icon& other) const
302 {
303     return src != other.src;
304 }
305
306 bool ConfigParserData::Icon::operator >(const Icon& other) const
307 {
308     return src > other.src;
309 }
310
311 bool ConfigParserData::Icon::operator>=(const Icon& other) const
312 {
313     return src >= other.src;
314 }
315
316 bool ConfigParserData::Icon::operator <(const Icon& other) const
317 {
318     return src < other.src;
319 }
320
321 bool ConfigParserData::Icon::operator<=(const Icon& other) const
322 {
323     return src <= other.src;
324 }
325
326 bool ConfigParserData::Preference::operator==(const Preference& other) const
327 {
328     return name == other.name;
329 }
330
331 bool ConfigParserData::Preference::operator!=(const Preference& other) const
332 {
333     return name != other.name;
334 }
335
336 bool ConfigParserData::Preference::operator >(const Preference& other) const
337 {
338     return name > other.name;
339 }
340
341 bool ConfigParserData::Preference::operator>=(const Preference& other) const
342 {
343     return name >= other.name;
344 }
345
346 bool ConfigParserData::Preference::operator <(const Preference& other) const
347 {
348     return name < other.name;
349 }
350
351 bool ConfigParserData::Preference::operator<=(const Preference& other) const
352 {
353     return name <= other.name;
354 }
355
356 bool ConfigParserData::AccessInfo::operator== (const AccessInfo& info) const
357 {
358     return m_strIRI == info.m_strIRI && m_bSubDomainAccess ==
359            info.m_bSubDomainAccess;
360 }
361
362 bool ConfigParserData::AccessInfo::operator!= (const AccessInfo& info) const
363 {
364     return m_strIRI != info.m_strIRI || m_bSubDomainAccess !=
365            info.m_bSubDomainAccess;
366 }
367
368 bool ConfigParserData::AccessInfo::operator <(const AccessInfo& info) const
369 {
370     if (m_strIRI == info.m_strIRI) {
371         return m_bSubDomainAccess < info.m_bSubDomainAccess;
372     } else {
373         return m_strIRI < info.m_strIRI;
374     }
375 }
376
377 bool ConfigParserData::Setting::operator==(const Setting& other) const
378 {
379     return m_name == other.m_name &&
380         m_value == other.m_value;
381 }
382
383 bool ConfigParserData::Setting::operator!=(const Setting& other) const
384 {
385     return m_name != other.m_name ||
386         m_value != other.m_value;
387 }
388
389 bool ConfigParserData::Setting::operator >(const Setting& other) const
390 {
391     return m_name > other.m_name;
392 }
393
394 bool ConfigParserData::Setting::operator>=(const Setting& other) const
395 {
396     return m_name >= other.m_name;
397 }
398
399 bool ConfigParserData::Setting::operator <(const Setting& other) const
400 {
401     return m_name < other.m_name;
402 }
403
404 bool ConfigParserData::Setting::operator<=(const Setting& other) const
405 {
406     return m_name <= other.m_name;
407 }
408
409 bool ConfigParserData::ServiceInfo::operator== (const ServiceInfo& info) const
410 {
411     return m_src == info.m_src &&
412     m_operation == info.m_operation &&
413     m_scheme == info.m_scheme &&
414     m_mime == info.m_mime;
415 }
416
417 bool ConfigParserData::ServiceInfo::operator!= (const ServiceInfo& info) const
418 {
419     return m_src != info.m_src &&
420     m_operation != info.m_operation &&
421     m_scheme != info.m_scheme &&
422     m_mime != info.m_mime;
423 }
424 } // namespace WrtDB