[NUI] Add Tizen.NUI.XamlBuild module
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.XamlBuild / src / public / EXamlBuild / EXaml / EXamlSetProperty.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 Mono.Cecil;
18 using System;
19 using System.Collections.Generic;
20 using System.IO;
21 using System.Reflection;
22 using System.Text;
23 using Tizen.NUI.Binding;
24 using Tizen.NUI.EXaml.Build.Tasks;
25 using Tizen.NUI.Xaml;
26 using Tizen.NUI.Xaml.Build.Tasks;
27
28 namespace Tizen.NUI.EXaml
29 {
30     //use []
31     internal class EXamlSetProperty : EXamlOperation
32     {
33         internal override string Write()
34         {
35             if (false == instance.IsValid)
36             {
37                 return "";
38             }
39
40             string ret = String.Format("({0} ({1} {2} {3}))\n",
41                             eXamlContext.GetValueString((int)EXamlOperationType.SetProperty),
42                             eXamlContext.GetValueString(instance.Index),
43                             eXamlContext.GetValueString(eXamlContext.definedProperties.GetIndex(property.DeclaringType, property)),
44                             eXamlContext.GetValueString(value));
45
46             return ret;
47         }
48
49         public EXamlSetProperty(EXamlContext context, EXamlCreateObject instance, string propertyName, object value)
50             : base(context)
51         {
52             var property = instance.Type.GetProperty(fi=>fi.Name==propertyName, out declareTypeRef);
53             if (null != property)
54             {
55                 this.instance = instance;
56                 this.property = property;
57                 this.value = value;
58
59                 if (null != this.instance.Instance)
60                 {
61                     var propertyInfo = this.instance.Instance.GetType().GetProperty(property.Name);
62
63                     if (value is EXamlCreateObject eXamlCreateObject && null != eXamlCreateObject.Instance)
64                     {
65                         if (this.instance.Instance is BindingExtension bindingExtension
66                             &&
67                             eXamlCreateObject.Type.FullName == typeof(BindingMode).FullName)
68                         {
69                             bindingExtension.ModeInEXaml = eXamlCreateObject;
70                         }
71                         else if (eXamlCreateObject.Type.ResolveCached().IsEnum)
72                         {
73                             if (eXamlCreateObject.Type.FullName == typeof(BindingMode).FullName)
74                             {
75                                 var realValue = Enum.Parse(typeof(BindingMode), eXamlCreateObject.Instance as string);
76                                 propertyInfo.SetMethod.Invoke(this.instance.Instance, new object[] { realValue });
77                             }
78                         }
79                         else
80                         {
81                             if (instance.GetType().FullName == typeof(Xaml.Build.Tasks.ArrayExtension).FullName
82                                 &&
83                                 "Type" == propertyName)
84                             {
85                                 eXamlCreateObject.IsValid = false;
86                             }
87
88                             propertyInfo.SetMethod.Invoke(this.instance.Instance, new object[] { eXamlCreateObject.Instance });
89                         }
90                     }
91                     else
92                     {
93                         propertyInfo.SetMethod.Invoke(this.instance.Instance, new object[] { value });
94                     }
95                 }
96
97                 this.instance.AddProperty(declareTypeRef, property);
98
99                 eXamlContext.eXamlOperations.Add(this);
100             }
101             else
102             {
103                 throw new Exception("Property is not element");
104             }
105         }
106
107         private EXamlCreateObject instance;
108         private TypeReference declareTypeRef;
109         private PropertyDefinition property;
110         private object value;
111     }
112 }