modify license, permission and remove ^M char
[platform/framework/native/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 Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0/
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                 int formXmlResolution = 0;
97
98                 if (xmlStrncmp( ch, (xmlChar*)"720", len) == 0)
99                 {
100                         formXmlResolution = 720;
101                         XMLHandler->__errorResult = XMLHandler->__pGenerator->SetFormXmlResolution(formXmlResolution);
102                 }
103                 else
104                 {
105                         formXmlResolution = 480;
106                         XMLHandler->__errorResult = XMLHandler->__pGenerator->SetFormXmlResolution(formXmlResolution);
107                 }
108                 XMLHandler->__formXmlResolutionExists = false;
109         }
110 }
111
112 void
113 StartElementSaxFunc(void* pCtx, const xmlChar* pName, const xmlChar** pAttributes)
114 {
115         if (XMLHandler->__errorResult != E_SUCCESS)
116         {
117             xmlStopParser((xmlParserCtxt*)pCtx);
118         }
119
120         if (xmlStrcasecmp(pName, (xmlChar*) "Layout") == 0)
121         {
122             XMLHandler->SetAttribute(pAttributes);
123         }
124         else if (xmlStrcasecmp(pName, (xmlChar*) "Property") == 0)
125         {
126             XMLHandler->SetProperty(pAttributes);
127         }
128         else if (xmlStrcasecmp(pName, (xmlChar*) "Scene") == 0 || xmlStrcasecmp(pName, (xmlChar*) "ScenePanel") == 0 || xmlStrcasecmp(pName, (xmlChar*) "ScenePopup") == 0 || xmlStrcasecmp(pName, (xmlChar*) "SceneQuickPanelFrame") == 0)
129         {
130             XMLHandler->SetXmlVersion(pAttributes);
131         }
132         else if (xmlStrcasecmp(pName, (xmlChar*) "panelLayout") == 0)
133         {
134                 // empty statement
135         }
136         else if (xmlStrcasecmp(pName, (xmlChar*) "itemSet") == 0)
137         {
138                 // empty statement
139         }
140         else if (xmlStrcasecmp(pName, (xmlChar*) "LogicalCoordinate") == 0 || xmlStrcasecmp(pName, (xmlChar*) "BaseResolution") == 0)
141         {
142                 XMLHandler->__formXmlResolutionExists = true;
143         }
144         else if (xmlStrcasecmp(pName, (xmlChar*) "headerItem") == 0)
145         {
146               XMLHandler->SetItem(pName, pAttributes);
147
148         }
149         else if (xmlStrcasecmp(pName, (xmlChar*) "buttonItem") == 0)
150         {
151             XMLHandler->SetItem(pName, pAttributes);
152
153         }
154         else if (xmlStrcasecmp(pName, (xmlChar*) "footerItem") == 0)
155         {
156                 XMLHandler->SetItem(pName, pAttributes);
157
158         }
159         else if (xmlStrcasecmp(pName, (xmlChar*) "backButton") == 0)
160         {
161                 XMLHandler->SetItem(pName, pAttributes);
162
163         }
164         else if (xmlStrcasecmp(pName, (xmlChar*) "item") == 0)
165         {
166                 XMLHandler->SetItem(pName, pAttributes);
167
168         }
169         else
170         {
171                 XMLHandler->__pControl = XMLHandler->__pGenerator->CreateControl();
172                 if (XMLHandler->__pControl == null)
173                 {
174                 XMLHandler->__errorLog = "Failed to create UiBuilderControl";
175                 XMLHandler->__errorResult = E_INVALID_FORMAT;
176                 }
177
178                 XMLHandler->__pControl->Construct((char*) pName);
179
180                 if (pAttributes != null)
181                 {
182                         for (int i = 0; (pAttributes[i] != null); i++)
183                         {
184                                 if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "id") == 0)
185                                 {
186                                         if (pAttributes[++i] != null)
187                                         {
188                                                 XMLHandler->__pControl->SetId((char*) pAttributes[i]);
189                                         }
190                                 }
191                                 else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "parent") == 0)
192                                 {
193                                         if (pAttributes[++i] != null)
194                                         {
195                                                 XMLHandler->__pControl->SetParentWin((char*) pAttributes[i]);
196                                         }
197                                 }
198                         }
199                 }
200         }
201 }
202
203 void
204 _UiBuilderXmlHandler::ParseAttribute(const xmlChar** pAttributes, int index, const Tizen::Base::String& name)
205 {
206         bool findAttribute = false;
207
208         if (index == LAYOUT_SETTING)
209         {
210                 index = 0;
211                 const Tizen::Base::String parentWindow = __pControl->GetParentWin();
212                 _UiBuilderControl* pParentControl = XMLHandler->__pGenerator->GetControl(parentWindow);
213                 if (parentWindow.GetLength() != 0)
214                 {
215                         Tizen::Base::String layoutType(L"");
216                         _UiBuilderControlLayout* pParentProperty = pParentControl->GetAttribute(__layoutMode);
217                         if (pParentProperty->GetLayoutElement(L"type", layoutType))
218                         {
219                                 __pProperty->SetLayoutElement(index, L"parentType", layoutType);
220                                 index++;
221                         }
222                 }
223
224                 for (int i = 0; pAttributes[i] != null; i++)
225                 {
226                         if (pAttributes[++i] != null)
227                         {
228                                 if (__pProperty != null)
229                                 {
230                                         __pProperty->SetLayoutElement(index, (char*) pAttributes[i - 1], (char*) pAttributes[i]);
231                                         index++;
232                                 }
233                         }
234                 }
235         }
236         else if (name.Equals(L"mode", false) && pAttributes[index] != null)
237         {
238                 if (xmlStrcasecmp(pAttributes[index], (xmlChar*) "Portrait") == 0)
239                 {
240                         __pProperty = XMLHandler->__pControl->GetAttribute(UIBUILDER_ATTRIBUTE_PORTRAIT);
241                         __layoutMode = UIBUILDER_ATTRIBUTE_PORTRAIT;
242                         findAttribute = true;
243                 }
244                 else if (xmlStrcasecmp(pAttributes[index], (xmlChar*) "Landscape") == 0)
245                 {
246                         __pProperty = XMLHandler->__pControl->GetAttribute(UIBUILDER_ATTRIBUTE_LANDSCAPE);
247                         __layoutMode = UIBUILDER_ATTRIBUTE_LANDSCAPE;
248                         findAttribute = true;
249                 }
250                 if (findAttribute == false)
251                 {
252                         __pProperty = XMLHandler->__pControl->GetAttribute(UIBUILDER_ATTRIBUTE_PORTRAIT);
253                 }
254         }
255         else if (name.Equals(L"style", false) && pAttributes[index] != null)
256         {
257                 __pProperty->SetStyle((char*) pAttributes[index]);
258         }
259         else if (name.Equals(L"x", false) && pAttributes[index] != null)
260         {
261                 __xPosition = (char*) pAttributes[index];
262         }
263         else if (name.Equals(L"y", false) && pAttributes[index] != null)
264         {
265                 __yPosition = (char*) pAttributes[index];
266         }
267         else if (name.Equals(L"width", false) && pAttributes[index] != null)
268         {
269                 __width = (char*) pAttributes[index];
270         }
271         else if (name.Equals(L"height", false) && pAttributes[index] != null)
272         {
273                 __height = (char*) pAttributes[index];
274         }
275         else
276         {
277                 __errorLog = L"Failed to ExecAttribute";
278                 __errorResult = E_INVALID_FORMAT;
279
280         }
281
282 }
283
284 void
285 _UiBuilderXmlHandler::SetAttribute(const xmlChar** pAttributes)
286 {
287         if (pAttributes != null)
288         {
289                 for (int i = 0; (pAttributes[i] != null); i++)
290                 {
291                         if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "mode") == 0)
292                         {
293                                 ParseAttribute(pAttributes, ++i, L"mode");
294                         }
295                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "style") == 0)
296                         {
297                                 ParseAttribute(pAttributes, ++i, L"style");
298                         }
299                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "x") == 0)
300                         {
301                                 ParseAttribute(pAttributes, ++i, L"x");
302                         }
303                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "y") == 0)
304                         {
305                                 ParseAttribute(pAttributes, ++i, L"y");
306                         }
307                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "width") == 0)
308                         {
309                                 ParseAttribute(pAttributes, ++i, L"width");
310                         }
311                         else if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "height") == 0)
312                         {
313                                 ParseAttribute(pAttributes, ++i, L"height");
314                         }
315                 }
316         }
317         else
318         {
319                 __errorLog = L"Failed to SetAttribute";
320                 __errorResult = E_INVALID_FORMAT;
321         }
322
323         __pProperty = XMLHandler->__pControl->GetAttribute(__layoutMode);
324         __pProperty->SetRect(__xPosition, __yPosition, __width, __height);
325
326         //layout setting
327         if (pAttributes != null)
328         {
329                 ParseAttribute(pAttributes, LAYOUT_SETTING, (char*) "layout");
330         }
331         else
332         {
333                 __errorLog = L"Failed to layout setting";
334                 __errorResult = E_INVALID_FORMAT;
335         }
336
337 }
338
339 void
340 _UiBuilderXmlHandler::SetProperty(const xmlChar** pAttributes)
341 {
342         int index = 0;
343         if (pAttributes != null)
344         {
345                 for (int i = 0; (pAttributes[i] != null); i++)
346                 {
347                         if (pAttributes[++i] != null)
348                         {
349                                 XMLHandler->__pControl->SetElement(index, (char*) pAttributes[i - 1], (char*) pAttributes[i]);
350                         }
351                         index++;
352                 }
353         }
354         else
355         {
356                 __errorLog = L"Failed to SetProperty";
357                 __errorResult = E_INVALID_FORMAT;
358
359         }
360 }
361
362 void
363 _UiBuilderXmlHandler::SetItem(const xmlChar* name, const xmlChar** pAttributes)
364 {
365         int index = 0;
366         int controlIndex = 0;
367
368         while (pAttributes[index] != null)
369         {
370                 if (pAttributes[++index] != null)
371                 {
372                         XMLHandler->__pControl->SetItemElement(controlIndex, (char*) pAttributes[index - 1], (char*) pAttributes[index]);
373                         index++;
374                         controlIndex++;
375                 }
376                 else
377                 {
378                 __errorLog = L"Failed to SetItem";
379                 __errorResult = E_INVALID_FORMAT;
380
381                 return;
382                 }
383         }
384         XMLHandler->__pControl->SetItemName((char*) name);
385 }
386
387 void
388 _UiBuilderXmlHandler::SetXmlVersion(const xmlChar** pAttributes)
389 {
390           if (pAttributes != null)
391         {
392                 for (int i = 0; (pAttributes[i] != null); i++)
393                 {
394                         if (xmlStrcasecmp(pAttributes[i], (xmlChar*) "Dversion") == 0)
395                         {
396                                 if (pAttributes[++i] != null)
397                                 {
398                                         int ver = atoi((char*) pAttributes[i]);
399                                         XMLHandler->__pGenerator->SetVersion(ver);
400                                 }
401                         }
402                 }
403         }
404         else
405         {
406                 __errorLog = "Failed to SetXmlVersion";
407                 __errorResult = E_INVALID_FORMAT;
408         }
409 }
410 } }  // Tizen::Ui