Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / FUi_UiBuilderXmlHandler.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18 * @file         FUi_UiBuilderXmlHandler.cpp
19 * @brief                This is the implementation file for _UiBuilderXmlHandler class.
20 */
21
22 #include <string.h>
23 #include <FSysSystemInfo.h>
24 #include <FBaseSysLog.h>
25 #include "FUi_UiBuilderXmlHandler.h"
26 #include "FUi_UiBuilderControlElement.h"
27
28 using namespace Tizen::Ui;
29
30 namespace Tizen { namespace Ui
31 {
32 void StartElementSaxFunc(void* pCtx, const xmlChar* pName, const xmlChar** pAttributes);
33 void ParseCharactersSaxFunc(void *ctx, const xmlChar *ch, int len);
34 xmlSAXHandler mySaxHandler = {
35 null, null, null, null, null, null, null, null, null, null, null, null, null, null,
36 StartElementSaxFunc,
37 null, null, ParseCharactersSaxFunc, null, null, null, null, null, null, null, null, null, null, null, null, null, null
38 };
39
40 _UiBuilderXmlHandler* XMLHandler = null;
41
42 _UiBuilderXmlHandler::_UiBuilderXmlHandler(void)
43         : __pGenerator(null)
44         , __pControl(null)
45         , __formXmlResolutionExists(false)
46         , __errorLog("")
47         , __errorResult(E_SUCCESS)
48         , __layoutMode(UIBUILDER_ATTRIBUTE_PORTRAIT)
49         , __xPosition(L"")
50         , __yPosition(L"")
51         , __width(L"")
52         , __height(L"")
53         , __pProperty(null)
54
55 {
56 }
57
58 _UiBuilderXmlHandler::~_UiBuilderXmlHandler(void)
59 {
60 }
61
62 bool
63 _UiBuilderXmlHandler::Construct(_UiBuilder* pGenerator)
64 {
65         if (pGenerator)
66         {
67                 XMLHandler = this;
68                 __pGenerator = pGenerator;
69                 return true;
70         }
71         return false;
72 }
73
74 result
75 _UiBuilderXmlHandler::ParseXml(char* pXmlBuffer, int size)
76 {
77         xmlSAXHandlerPtr pMySaxhandler = &mySaxHandler;
78         xmlSubstituteEntitiesDefault(1);
79         xmlSAXUserParseMemory(pMySaxhandler, null, pXmlBuffer, size);
80         xmlCleanupMemory();
81
82         if (__errorResult != E_SUCCESS)
83         {
84                 SysLogException(NID_UI, E_INVALID_FORMAT, "[E_INVALID_FORMAT][UiBuilder] %ls", XMLHandler->__errorLog.GetPointer());
85                 return __errorResult;
86         }
87                 return E_SUCCESS;
88
89 }
90
91 void
92 ParseCharactersSaxFunc(void*ctx, const xmlChar*ch, int len)
93 {
94         if (XMLHandler->__formXmlResolutionExists == true)
95         {
96                 Tizen::Graphics::_LogicalResolution formXmlResolution;
97                 if (xmlStrncmp( ch, (xmlChar*)"720", len) == 0)
98                 {
99                         formXmlResolution = Tizen::Graphics::RESOLUTION_LOGICAL_720;
100                         XMLHandler->__errorResult = XMLHandler->__pGenerator->SetFormXmlResolution(formXmlResolution);
101                 }
102                 else
103                 {
104                         formXmlResolution = Tizen::Graphics::RESOLUTION_LOGICAL_480;
105                         XMLHandler->__errorResult = XMLHandler->__pGenerator->SetFormXmlResolution(formXmlResolution);
106                 }
107                 XMLHandler->__formXmlResolutionExists = false;
108         }
109 }
110
111 void
112 StartElementSaxFunc(void* pCtx, const xmlChar* pName, const xmlChar** pAttributes)
113 {
114         if (XMLHandler->__errorResult != E_SUCCESS)
115         {
116             xmlStopParser((xmlParserCtxt*)pCtx);
117         }
118
119         if (xmlStrcasecmp(pName, (xmlChar*) "Layout") == 0)
120         {
121             XMLHandler->SetAttribute(pAttributes);
122         }
123         else if (xmlStrcasecmp(pName, (xmlChar*) "Property") == 0)
124         {
125             XMLHandler->SetProperty(pAttributes);
126         }
127         else if (xmlStrcasecmp(pName, (xmlChar*) "Scene") == 0 || xmlStrcasecmp(pName, (xmlChar*) "ScenePanel") == 0 || xmlStrcasecmp(pName, (xmlChar*) "ScenePopup") == 0 || xmlStrcasecmp(pName, (xmlChar*) "SceneQuickPanelFrame") == 0)
128         {
129             XMLHandler->SetXmlVersion(pAttributes);
130         }
131         else if (xmlStrcasecmp(pName, (xmlChar*) "panelLayout") == 0)
132         {
133                 // empty statement
134         }
135         else if (xmlStrcasecmp(pName, (xmlChar*) "itemSet") == 0)
136         {
137                 // empty statement
138         }
139         else if (xmlStrcasecmp(pName, (xmlChar*) "LogicalCoordinate") == 0 || xmlStrcasecmp(pName, (xmlChar*) "BaseResolution") == 0)
140         {
141                 XMLHandler->__formXmlResolutionExists = true;
142         }
143         else if (xmlStrcasecmp(pName, (xmlChar*) "headerItem") == 0)
144         {
145               XMLHandler->SetItem(pName, pAttributes);
146
147         }
148         else if (xmlStrcasecmp(pName, (xmlChar*) "buttonItem") == 0)
149         {
150             XMLHandler->SetItem(pName, pAttributes);
151
152         }
153         else if (xmlStrcasecmp(pName, (xmlChar*) "footerItem") == 0)
154         {
155                 XMLHandler->SetItem(pName, pAttributes);
156
157         }
158         else if (xmlStrcasecmp(pName, (xmlChar*) "backButton") == 0)
159         {
160                 XMLHandler->SetItem(pName, pAttributes);
161
162         }
163         else if (xmlStrcasecmp(pName, (xmlChar*) "item") == 0)
164         {
165                 XMLHandler->SetItem(pName, pAttributes);
166
167         }
168         else
169         {
170                 XMLHandler->__pControl = XMLHandler->__pGenerator->CreateControl();
171                 if (XMLHandler->__pControl == null)
172                 {
173                 XMLHandler->__errorLog = "Failed to create UiBuilderControl";
174                 XMLHandler->__errorResult = E_INVALID_FORMAT;
175                 }
176
177                 XMLHandler->__pControl->Construct((char*) pName);
178
179                 if (pAttributes != null)
180                 {
181                         for (int i = 0; (pAttributes[i] != null); i++)
182                         {
183                                 if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "id") == 0)
184                                 {
185                                         if (pAttributes[++i] != null)
186                                         {
187                                                 XMLHandler->__pControl->SetId((char*) pAttributes[i]);
188                                         }
189                                 }
190                                 else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "parent") == 0)
191                                 {
192                                         if (pAttributes[++i] != null)
193                                         {
194                                                 XMLHandler->__pControl->SetParentWin((char*) pAttributes[i]);
195                                         }
196                                 }
197                         }
198                 }
199         }
200 }
201
202 void
203 _UiBuilderXmlHandler::ParseAttribute(const xmlChar** pAttributes, int index, const Tizen::Base::String& name)
204 {
205         bool findAttribute = false;
206
207         if (index == LAYOUT_SETTING)
208         {
209                 index = 0;
210                 const Tizen::Base::String parentWindow = __pControl->GetParentWin();
211                 _UiBuilderControl* pParentControl = XMLHandler->__pGenerator->GetControl(parentWindow);
212                 if (parentWindow.GetLength() != 0)
213                 {
214                         Tizen::Base::String layoutType(L"");
215                         _UiBuilderControlLayout* pParentProperty = pParentControl->GetAttribute(__layoutMode);
216                         if (pParentProperty->GetLayoutElement(L"type", layoutType))
217                         {
218                                 __pProperty->SetLayoutElement(index, L"parentType", layoutType);
219                                 index++;
220                         }
221                 }
222
223                 for (int i = 0; pAttributes[i] != null; i++)
224                 {
225                         if (pAttributes[++i] != null)
226                         {
227                                 if (__pProperty != null)
228                                 {
229                                         __pProperty->SetLayoutElement(index, (char*) pAttributes[i - 1], (char*) pAttributes[i]);
230                                         index++;
231                                 }
232                         }
233                 }
234         }
235         else if (name.Equals(L"mode", false) && pAttributes[index] != null)
236         {
237                 if (xmlStrcasecmp(pAttributes[index], (xmlChar*) "Portrait") == 0)
238                 {
239                         __pProperty = XMLHandler->__pControl->GetAttribute(UIBUILDER_ATTRIBUTE_PORTRAIT);
240                         __layoutMode = UIBUILDER_ATTRIBUTE_PORTRAIT;
241                         findAttribute = true;
242                 }
243                 else if (xmlStrcasecmp(pAttributes[index], (xmlChar*) "Landscape") == 0)
244                 {
245                         __pProperty = XMLHandler->__pControl->GetAttribute(UIBUILDER_ATTRIBUTE_LANDSCAPE);
246                         __layoutMode = UIBUILDER_ATTRIBUTE_LANDSCAPE;
247                         findAttribute = true;
248                 }
249                 if (findAttribute == false)
250                 {
251                         __pProperty = XMLHandler->__pControl->GetAttribute(UIBUILDER_ATTRIBUTE_PORTRAIT);
252                 }
253         }
254         else if (name.Equals(L"style", false) && pAttributes[index] != null)
255         {
256                 __pProperty->SetStyle((char*) pAttributes[index]);
257         }
258         else if (name.Equals(L"x", false) && pAttributes[index] != null)
259         {
260                 __xPosition = (char*) pAttributes[index];
261         }
262         else if (name.Equals(L"y", false) && pAttributes[index] != null)
263         {
264                 __yPosition = (char*) pAttributes[index];
265         }
266         else if (name.Equals(L"width", false) && pAttributes[index] != null)
267         {
268                 __width = (char*) pAttributes[index];
269         }
270         else if (name.Equals(L"height", false) && pAttributes[index] != null)
271         {
272                 __height = (char*) pAttributes[index];
273         }
274         else
275         {
276                 __errorLog = L"Failed to ExecAttribute";
277                 __errorResult = E_INVALID_FORMAT;
278
279         }
280
281 }
282
283 void
284 _UiBuilderXmlHandler::SetAttribute(const xmlChar** pAttributes)
285 {
286         if (pAttributes != null)
287         {
288                 for (int i = 0; (pAttributes[i] != null); i++)
289                 {
290                         if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "mode") == 0)
291                         {
292                                 ParseAttribute(pAttributes, ++i, L"mode");
293                         }
294                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "style") == 0)
295                         {
296                                 ParseAttribute(pAttributes, ++i, L"style");
297                         }
298                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "x") == 0)
299                         {
300                                 ParseAttribute(pAttributes, ++i, L"x");
301                         }
302                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "y") == 0)
303                         {
304                                 ParseAttribute(pAttributes, ++i, L"y");
305                         }
306                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "width") == 0)
307                         {
308                                 ParseAttribute(pAttributes, ++i, L"width");
309                         }
310                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "height") == 0)
311                         {
312                                 ParseAttribute(pAttributes, ++i, L"height");
313                         }
314                 }
315         }
316         else
317         {
318                 __errorLog = L"Failed to SetAttribute";
319                 __errorResult = E_INVALID_FORMAT;
320         }
321
322         __pProperty = XMLHandler->__pControl->GetAttribute(__layoutMode);
323         __pProperty->SetRect(__xPosition, __yPosition, __width, __height);
324
325         //layout setting
326         if (pAttributes != null)
327         {
328                 ParseAttribute(pAttributes, LAYOUT_SETTING, (char*) "layout");
329         }
330         else
331         {
332                 __errorLog = L"Failed to layout setting";
333                 __errorResult = E_INVALID_FORMAT;
334         }
335
336 }
337
338 void
339 _UiBuilderXmlHandler::SetProperty(const xmlChar** pAttributes)
340 {
341         int index = 0;
342         if (pAttributes != null)
343         {
344                 for (int i = 0; (pAttributes[i] != null); i++)
345                 {
346                         if (pAttributes[++i] != null)
347                         {
348                                 XMLHandler->__pControl->SetElement(index, (char*) pAttributes[i - 1], (char*) pAttributes[i]);
349                         }
350                         index++;
351                 }
352         }
353         else
354         {
355                 __errorLog = L"Failed to SetProperty";
356                 __errorResult = E_INVALID_FORMAT;
357
358         }
359 }
360
361 void
362 _UiBuilderXmlHandler::SetItem(const xmlChar* name, const xmlChar** pAttributes)
363 {
364         int index = 0;
365         int controlIndex = 0;
366
367         while (pAttributes[index] != null)
368         {
369                 if (pAttributes[++index] != null)
370                 {
371                         XMLHandler->__pControl->SetItemElement(controlIndex, (char*) pAttributes[index - 1], (char*) pAttributes[index]);
372                         index++;
373                         controlIndex++;
374                 }
375                 else
376                 {
377                 __errorLog = L"Failed to SetItem";
378                 __errorResult = E_INVALID_FORMAT;
379
380                 return;
381                 }
382         }
383         XMLHandler->__pControl->SetItemName((char*) name);
384 }
385
386 void
387 _UiBuilderXmlHandler::SetXmlVersion(const xmlChar** pAttributes)
388 {
389           if (pAttributes != null)
390         {
391                 for (int i = 0; (pAttributes[i] != null); i++)
392                 {
393                         if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "Dversion") == 0)
394                         {
395                                 if (pAttributes[++i] != null)
396                                 {
397                                         int ver = atoi((char*) pAttributes[i]);
398                                         XMLHandler->__pGenerator->SetVersion(ver);
399                                 }
400                         }
401                 }
402         }
403         else
404         {
405                 __errorLog = "Failed to SetXmlVersion";
406                 __errorResult = E_INVALID_FORMAT;
407         }
408 }
409 } }  // Tizen::Ui