[NUI][EXaml] Support AppResourcePath in EXaml
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / EXaml / Operation / CreateInstance.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.Reflection;
20 using System.Text;
21 using Tizen.NUI.BaseComponents;
22 using Tizen.NUI.Binding;
23 using Tizen.NUI.Binding.Internals;
24 using Tizen.NUI.Xaml;
25
26 namespace Tizen.NUI.EXaml
27 {
28     internal class CreateInstance : Operation
29     {
30         public CreateInstance(GlobalDataList globalDataList, int typeIndex, int xFactoryMethodIndex, List<object> paramList = null)
31         {
32             this.typeIndex = typeIndex;
33             this.paramList = paramList;
34             this.globalDataList = globalDataList;
35             this.xFactoryMethodIndex = xFactoryMethodIndex;
36         }
37
38         private GlobalDataList globalDataList;
39         private int xFactoryMethodIndex;
40
41         public void Do()
42         {
43             if (0 == globalDataList.GatheredInstances.Count && null != Root)
44             {
45                 globalDataList.GatheredInstances.Add(Root);
46             }
47             else
48             {
49                 var type = globalDataList.GatheredTypes[typeIndex];
50
51                 var xFactoryMethod = (0 <= xFactoryMethodIndex) ? globalDataList.GatheredMethods[xFactoryMethodIndex] : null;
52
53                 if (null == paramList)
54                 {
55                     if (null == xFactoryMethod)
56                     {
57                         globalDataList.GatheredInstances.Add(Activator.CreateInstance(type));
58                     }
59                     else
60                     {
61                         globalDataList.GatheredInstances.Add(xFactoryMethod.Invoke(null, Array.Empty<object>()));
62                     }
63                 }
64                 else
65                 {
66                     for (int i = 0; i < paramList.Count; i++)
67                     {
68                         if (paramList[i] is Instance instance)
69                         {
70                             paramList[i] = globalDataList.GatheredInstances[instance.Index];
71                         }
72
73                         if (paramList[i] is ApplicationResourcePathExtension applicationResourcePath)
74                         {
75                             paramList[i] = applicationResourcePath.ProvideValue(null);
76                         }
77                     }
78
79                     if (null == xFactoryMethod)
80                     {
81                         globalDataList.GatheredInstances.Add(Activator.CreateInstance(type, paramList.ToArray()));
82                     }
83                     else
84                     {
85                         globalDataList.GatheredInstances.Add(xFactoryMethod.Invoke(null, paramList.ToArray()));
86                     }
87                 }
88             }
89
90             if (1 == globalDataList.GatheredInstances.Count)
91             {
92                 var rootObject = globalDataList.GatheredInstances[0] as BindableObject;
93                 if (null != rootObject)
94                 {
95                     rootObject.IsCreateByXaml = true;
96                     NameScope nameScope = new NameScope();
97                     NameScope.SetNameScope(rootObject, nameScope);
98                 }
99             }
100         }
101
102         private int typeIndex;
103
104         internal static object Root;
105
106         private List<object> paramList;
107     }
108 }