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