merge with master
[platform/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 <stddef.h>
23 #include <dpl/wrt-dao-ro/config_parser_data.h>
24 #include <dpl/log/log.h>
25 #include <libxml/xmlreader.h>
26 #include <libxml/xmlstring.h>
27
28 namespace WrtDB {
29 bool IsSpace(const xmlChar* str);
30 bool CopyChar(xmlChar* out, xmlChar* in);
31
32 bool IsSpace(const xmlChar* str)
33 {
34     int charlen = xmlUTF8Size(str);
35
36     switch (charlen) {
37     case 1:
38         switch (*str) {
39         case 0x09:
40         case 0x0a:
41         case 0x0b:
42         case 0x0c:
43         case 0x0d:
44         case 0x20:
45             return true;
46
47         default:
48             return false;
49         }
50
51     case 2:
52         switch (*(str + 1)) {
53         case 0x85:
54         case 0xa0:
55             return true;
56
57         default:
58             return false;
59         }
60
61     case 3:
62         switch (*str) {
63         case 0xe1:
64         {
65             unsigned char c2 = *(str + 1);
66             unsigned char c3 = *(str + 2);
67             if ((c2 == 0x9a && c3 == 0x80) || (c2 == 0xa0 && c3 == 0x8e)) {
68                 return true;
69             } else {
70                 return false;
71             }
72         }
73
74         case 0xe2:
75             switch (*(str + 1)) {
76             case 0x80:
77                 switch (*(str + 2)) {
78                 case 0x80:
79                 case 0x81:
80                 case 0x82:
81                 case 0x83:
82                 case 0x84:
83                 case 0x85:
84                 case 0x86:
85                 case 0x87:
86                 case 0x88:
87                 case 0x89:
88                 case 0x8a:
89                 case 0xa8:
90                 case 0xa9:
91                 case 0xaf:
92                     return true;
93                 default:
94                     return false;
95                 }
96             case 0x81:
97                 if (*(str + 2) == 0x9f) {
98                     return true;
99                 } else {
100                     return false;
101                 }
102
103             default:
104                 return false;
105             }
106         case 0xe3:
107             if (*(str + 1) == 0x80 && *(str + 2) == 0x80) {
108                 return true;
109             } else {
110                 return false;
111             }
112
113         default:
114             return false;
115         }
116
117     default:
118         return false;
119     }
120 }
121
122 bool CopyChar(xmlChar* out,
123               xmlChar* in)
124 {
125     int size = xmlUTF8Size(in);
126     switch (size) {
127     case 6:
128         out[5] = in[5];
129     case 5:
130         out[4] = in[4];
131     case 4:
132         out[3] = in[3];
133     case 3:
134         out[2] = in[2];
135     case 2:
136         out[1] = in[1];
137     case 1:
138         out[0] = in[0];
139         return true;
140
141     default:
142         return false;
143     }
144 }
145
146 //TODO temporary fix until commits the rewrite of this functionality.
147 void NormalizeString(DPL::String& str)
148 {
149     DPL::Optional<DPL::String> opt = str;
150     NormalizeString(opt);
151     str = *opt;
152 }
153
154 void NormalizeString (DPL::Optional<DPL::String>& txt, bool isTrimSpace)
155 {
156     if (!!txt) {
157         std::string tmp = DPL::ToUTF8String(*txt);
158         const xmlChar* str = reinterpret_cast<const xmlChar*>(tmp.c_str());
159         if (!xmlCheckUTF8(str)) {
160             LogError("Not valid UTF8");
161             return;
162         }
163
164         int i = 0;
165         xmlChar* c;
166         while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
167             if (!IsSpace(c)) {
168                 break;
169             }
170             ++i;
171         }
172
173         xmlChar* tmpnew = xmlUTF8Strndup(c, xmlUTF8Strlen(c) + 1);
174         bool first = false;
175         xmlChar* s = tmpnew;
176         while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
177             if (IsSpace(c)) {
178                 first = true;
179                 ++i;
180             } else {
181                 if (c[0] == 0x0) {
182                     break;
183                 }
184                 if (first && !isTrimSpace) {
185                     xmlChar space[6] = { 0x20 };
186                     CopyChar(s, space);
187                     s += xmlUTF8Size(s);
188                     first = false;
189                 }
190                 CopyChar(s, c);
191                 s += xmlUTF8Size(s);
192                 ++i;
193             }
194         }
195         s[0] = 0x0;
196         txt = DPL::FromUTF8String(reinterpret_cast<char*>(tmpnew));
197         xmlFree(tmpnew);
198     }
199 }
200
201 void NormalizeAndTrimSpaceString(DPL::OptionalString& txt)
202 {
203     NormalizeString(txt, true);
204 }
205
206 bool ConfigParserData::Param::operator==(const Param& other) const
207 {
208     return name == other.name && value == other.value;
209 }
210
211 bool ConfigParserData::Param::operator!=(const Param& other) const
212 {
213     return name != other.name || value != other.value;
214 }
215
216 bool ConfigParserData::Param::operator >(const Param& other) const
217 {
218     if (name == other.name) {
219         return value > other.value;
220     } else {
221         return name > other.name;
222     }
223 }
224
225 bool ConfigParserData::Param::operator>=(const Param& other) const
226 {
227     if (name >= other.name) {
228         return true;
229     } else {
230         return value >= other.value;
231     }
232 }
233
234 bool ConfigParserData::Param::operator <(const Param& other) const
235 {
236     if (name == other.name) {
237         return value < other.value;
238     } else {
239         return name < other.name;
240     }
241 }
242
243 bool ConfigParserData::Param::operator<=(const Param& other) const
244 {
245     if (name <= other.name) {
246         return true;
247     } else {
248         return value <= other.value;
249     }
250 }
251
252 bool ConfigParserData::Feature::operator==(const Feature& other) const
253 {
254     return name == other.name && paramsList == other.paramsList;
255 }
256
257 bool ConfigParserData::Feature::operator!=(const Feature& other) const
258 {
259     return name != other.name || paramsList != other.paramsList;
260 }
261
262 bool ConfigParserData::Feature::operator >(const Feature& other) const
263 {
264     if (name > other.name) {
265         return true;
266     }
267     if (name < other.name) {
268         return false;
269     }
270     return paramsList > other.paramsList;
271 }
272
273 bool ConfigParserData::Feature::operator>=(const Feature& other) const
274 {
275     if (name > other.name) {
276         return true;
277     }
278     if (name < other.name) {
279         return false;
280     }
281     return paramsList >= other.paramsList;
282 }
283
284 bool ConfigParserData::Feature::operator <(const Feature& other) const
285 {
286     if (name < other.name) {
287         return true;
288     }
289     if (name > other.name) {
290         return false;
291     }
292     return paramsList < other.paramsList;
293 }
294
295 bool ConfigParserData::Feature::operator<=(const Feature& other) const
296 {
297     if (name < other.name) {
298         return true;
299     }
300     if (name > other.name) {
301         return false;
302     }
303     return paramsList <= other.paramsList;
304 }
305
306 bool ConfigParserData::Privilege::operator==(const Privilege& other) const
307 {
308     return name == other.name;
309 }
310
311 bool ConfigParserData::Privilege::operator!=(const Privilege& other) const
312 {
313     return name != other.name;
314 }
315
316 bool ConfigParserData::Privilege::operator >(const Privilege& other) const
317 {
318     return name > other.name;
319 }
320
321 bool ConfigParserData::Privilege::operator>=(const Privilege& other) const
322 {
323     return name >= other.name;
324 }
325
326 bool ConfigParserData::Privilege::operator <(const Privilege& other) const
327 {
328     return name < other.name;
329 }
330
331 bool ConfigParserData::Privilege::operator<=(const Privilege& other) const
332 {
333     return name < other.name;
334 }
335
336 bool ConfigParserData::Icon::operator==(const Icon& other) const
337 {
338     return src == other.src;
339 }
340
341 bool ConfigParserData::Icon::operator!=(const Icon& other) const
342 {
343     return src != other.src;
344 }
345
346 bool ConfigParserData::Icon::operator >(const Icon& other) const
347 {
348     return src > other.src;
349 }
350
351 bool ConfigParserData::Icon::operator>=(const Icon& other) const
352 {
353     return src >= other.src;
354 }
355
356 bool ConfigParserData::Icon::operator <(const Icon& other) const
357 {
358     return src < other.src;
359 }
360
361 bool ConfigParserData::Icon::operator<=(const Icon& other) const
362 {
363     return src <= other.src;
364 }
365
366 bool ConfigParserData::Preference::operator==(const Preference& other) const
367 {
368     return name == other.name;
369 }
370
371 bool ConfigParserData::Preference::operator!=(const Preference& other) const
372 {
373     return name != other.name;
374 }
375
376 bool ConfigParserData::Preference::operator >(const Preference& other) const
377 {
378     return name > other.name;
379 }
380
381 bool ConfigParserData::Preference::operator>=(const Preference& other) const
382 {
383     return name >= other.name;
384 }
385
386 bool ConfigParserData::Preference::operator <(const Preference& other) const
387 {
388     return name < other.name;
389 }
390
391 bool ConfigParserData::Preference::operator<=(const Preference& other) const
392 {
393     return name <= other.name;
394 }
395
396 bool ConfigParserData::AccessInfo::operator== (const AccessInfo& info) const
397 {
398     return m_strIRI == info.m_strIRI && m_bSubDomainAccess ==
399            info.m_bSubDomainAccess;
400 }
401
402 bool ConfigParserData::AccessInfo::operator!= (const AccessInfo& info) const
403 {
404     return m_strIRI != info.m_strIRI || m_bSubDomainAccess !=
405            info.m_bSubDomainAccess;
406 }
407
408 bool ConfigParserData::AccessInfo::operator <(const AccessInfo& info) const
409 {
410     if (m_strIRI == info.m_strIRI) {
411         return m_bSubDomainAccess < info.m_bSubDomainAccess;
412     } else {
413         return m_strIRI < info.m_strIRI;
414     }
415 }
416
417 bool ConfigParserData::Setting::operator==(const Setting& other) const
418 {
419     return m_name == other.m_name &&
420            m_value == other.m_value;
421 }
422
423 bool ConfigParserData::Setting::operator!=(const Setting& other) const
424 {
425     return m_name != other.m_name ||
426            m_value != other.m_value;
427 }
428
429 bool ConfigParserData::Setting::operator >(const Setting& other) const
430 {
431     return m_name > other.m_name;
432 }
433
434 bool ConfigParserData::Setting::operator>=(const Setting& other) const
435 {
436     return m_name >= other.m_name;
437 }
438
439 bool ConfigParserData::Setting::operator <(const Setting& other) const
440 {
441     return m_name < other.m_name;
442 }
443
444 bool ConfigParserData::Setting::operator<=(const Setting& other) const
445 {
446     return m_name <= other.m_name;
447 }
448
449 bool ConfigParserData::ServiceInfo::operator== (const ServiceInfo& info) const
450 {
451     return m_src == info.m_src &&
452            m_operation == info.m_operation &&
453            m_scheme == info.m_scheme &&
454            m_mime == info.m_mime &&
455            m_disposition == info.m_disposition;
456 }
457
458 bool ConfigParserData::ServiceInfo::operator!= (const ServiceInfo& info) const
459 {
460     return m_src != info.m_src &&
461            m_operation != info.m_operation &&
462            m_scheme != info.m_scheme &&
463            m_mime != info.m_mime &&
464            m_disposition != info.m_disposition;
465 }
466
467 bool ConfigParserData::LiveboxInfo::operator==(const LiveboxInfo& other) const
468 {
469     return m_liveboxId == other.m_liveboxId &&
470            m_autoLaunch == other.m_autoLaunch &&
471            m_updatePeriod == other.m_updatePeriod &&
472            m_primary == other.m_primary &&
473            m_label == other.m_label &&
474            m_icon == other.m_icon;
475 }
476
477 bool ConfigParserData::LiveboxInfo::operator!=(const LiveboxInfo& other) const
478 {
479     return m_liveboxId != other.m_liveboxId &&
480            m_autoLaunch != other.m_autoLaunch &&
481            m_updatePeriod != other.m_updatePeriod &&
482            m_primary != other.m_primary &&
483            m_label != other.m_label &&
484            m_icon != other.m_icon;
485 }
486 } // namespace WrtDB