Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / EXaml / LoadEXaml.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI.EXaml
22 {
23     internal static class LoadEXaml
24     {
25         internal static GlobalDataList GatherDataList(string xaml)
26         {
27             var globalDataList = new GlobalDataList();
28
29             int index = 0;
30
31             var createInstance = new CreateInstanceAction(globalDataList, null);
32             var getObjectByProperty = new GetObjectByPropertyAction(globalDataList, null);
33             var addExistInstance = new AddExistInstanceAction(globalDataList, null);
34             var registerXName = new RegisterXNameAction(globalDataList, null);
35             var setProperty = new SetPropertyAction(globalDataList, null);
36             var addToCollectionProperty = new AddToCollectionPropertyAction(globalDataList, null);
37             var addEvent = new AddEventAction(globalDataList, null);
38             var setBindalbeProperty = new SetBindalbePropertyAction(globalDataList, null);
39             var addObject = new CallAddMethodAction(globalDataList, null);
40             var setDynamicResourceAction = new SetDynamicResourceAction(globalDataList, null);
41             var addToResourceDictionaryAction = new AddToResourceDictionaryAction(globalDataList, null);
42             var setBindingAction = new SetBindingAction(globalDataList, null);
43             var otherActions = new OtherActions(globalDataList, null);
44
45             foreach (char c in xaml)
46             {
47                 if (null == currentOp)
48                 {
49                     switch (c)
50                     {
51                         case '<':
52                             if (0 == index)
53                             {
54                                 currentOp = new GatherAssembliesBlock(globalDataList, null);
55                                 currentOp.Init();
56                                 index++;
57                             }
58                             else if (1 == index)
59                             {
60                                 currentOp = new GatherTypesBlock(globalDataList, null);
61                                 currentOp.Init();
62                                 index++;
63                             }
64                             else if (2 == index)
65                             {
66                                 currentOp = new GatherPropertiesBlock(globalDataList, null);
67                                 currentOp.Init();
68                                 index++;
69                             }
70                             else if (3 == index)
71                             {
72                                 currentOp = new GatherEventsBlock(globalDataList, null);
73                                 currentOp.Init();
74                                 index++;
75                             }
76                             else if (4 == index)
77                             {
78                                 currentOp = new GatherMethodsBlock(globalDataList, null);
79                                 currentOp.Init();
80                                 index++;
81                             }
82                             else if (5 == index)
83                             {
84                                 currentOp = new GatherBindablePropertiesBlock(globalDataList, null);
85                                 currentOp.Init();
86                                 index++;
87                             }
88                             break;
89
90                         case '{':
91                             currentOp = createInstance;
92                             currentOp.Init();
93                             break;
94
95                         case '`':
96                             currentOp = getObjectByProperty;
97                             currentOp.Init();
98                             break;
99
100                         case '@':
101                             currentOp = addExistInstance;
102                             currentOp.Init();
103                             break;
104
105                         case '&':
106                             currentOp = registerXName;
107                             currentOp.Init();
108                             break;
109
110                         case '[':
111                             currentOp = setProperty;
112                             currentOp.Init();
113                             break;
114
115                         case '~':
116                             currentOp = addToCollectionProperty;
117                             currentOp.Init();
118                             break;
119
120                         case '#':
121                             currentOp = addEvent;
122                             currentOp.Init();
123                             break;
124
125                         case '!':
126                             currentOp = setBindalbeProperty;
127                             currentOp.Init();
128                             break;
129
130                         case '$':
131                             currentOp = setDynamicResourceAction;
132                             currentOp.Init();
133                             break;
134
135                         case '^':
136                             currentOp = addObject;
137                             currentOp.Init();
138                             break;
139
140                         case '*':
141                             currentOp = addToResourceDictionaryAction;
142                             currentOp.Init();
143                             break;
144
145                         case '%':
146                             currentOp = setBindingAction;
147                             currentOp.Init();
148                             break;
149
150                         case 'a':
151                             currentOp = otherActions;
152                             currentOp.Init();
153                             break;
154                     }
155                 }
156                 else
157                 {
158                     currentOp = currentOp.DealChar(c);
159                 }
160             }
161
162             foreach (var op in globalDataList.PreLoadOperations)
163             {
164                 op.Do();
165             }
166
167             return globalDataList;
168         }
169
170         internal static void Load(object view, string xaml)
171         {
172             var globalDataList = GatherDataList(xaml);
173
174             CreateInstanceAction.Root = view;
175
176             foreach (var op in globalDataList.Operations)
177             {
178                 op.Do();
179             }
180         }
181
182         internal static void Load(object view, object preloadData)
183         {
184             var globalDataList = preloadData as GlobalDataList;
185
186             if (null == globalDataList)
187             {
188                 return;
189             }
190
191             CreateInstanceAction.Root = view;
192
193             foreach (var op in globalDataList.Operations)
194             {
195                 op.Do();
196             }
197         }
198
199         private static Action currentOp = null;
200     }
201 }