[NUI][AT-SPI] Promote ReadingInfoTypes to Container
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Gadget / Tizen.NUI / NUIGadgetResourceManager.cs
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
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.Globalization;
19 using System.Collections.Generic;
20 using System.Reflection;
21 using System.IO;
22 using System.ComponentModel;
23 using System.Resources;
24
25 namespace Tizen.NUI
26 {
27     /// <summary>
28     /// This class has the methods of the NUIGadgetResourceManager.
29     /// </summary>
30     /// <since_tizen> 10 </since_tizen>
31     [EditorBrowsable(EditorBrowsableState.Never)]
32     public class NUIGadgetResourceManager
33     {
34         private readonly string _resourcePath;
35         private readonly string _resourceDll;
36         private readonly string _resourceClassName;
37         private readonly IDictionary<string, global::System.Resources.ResourceManager> _resourceMap = new Dictionary<string, global::System.Resources.ResourceManager>();
38
39         internal NUIGadgetResourceManager(NUIGadgetInfo info)
40         {
41             _resourcePath = info.ResourcePath; ;
42             _resourceDll = info.ResourceFile;
43             _resourceClassName = info.ResourceClassName;
44         }
45
46         /// <summary>
47         /// Initializes the resource manager of the gadget.
48         /// </summary>
49         /// <param name="resourcePath">The path of the resource</param>
50         /// <param name="resourceDll">The file name of the resource.</param>
51         /// <param name="resourceClassName">The class name of the resource.</param>
52         /// <since_tizen> 10 </since_tizen>
53         public NUIGadgetResourceManager(string resourcePath, string resourceDll, string resourceClassName)
54         {
55             _resourcePath = resourcePath;
56             _resourceDll = resourceDll;
57             _resourceClassName = resourceClassName;
58         }
59
60         /// <summary>
61         /// Get the value of the specified string resource.
62         /// </summary>
63         /// <param name="name">The name of the resource to retrieve.</param>
64         /// <returns>The value of the resource, or null if name cannot be found in a resource set.</returns>
65         /// <since_tizen> 10 </since_tizen>
66         public string GetString(string name)
67         {
68             return GetString(name, CultureInfo.CurrentUICulture);
69         }
70
71         /// <summary>
72         /// Gets the return value of the string resource localied for the specified culture.
73         /// </summary>
74         /// <param name="name">The name of the resource to retrieve.</param>
75         /// <param name="cultureInfo">An object that represents the culture for which the resource is localied.</param>
76         /// <returns>The value of the resource localied for the specified culture, or null if name cannot be found in a resource set.</returns>
77         /// <exception cref="ArgumentNullException">Thrown when failed because of a invalid argument.</exception>
78         /// <since_tizen> 10 </since_tizen>
79         public string GetString(string name, CultureInfo cultureInfo)
80         {
81             if (string.IsNullOrEmpty(name))
82             {
83                 throw new ArgumentNullException(nameof(name));
84             }
85
86             if (cultureInfo == null)
87             {
88                 Log.Warn("Use CurrentUICulture");
89                 cultureInfo = CultureInfo.CurrentUICulture;
90             }
91
92             string result = string.Empty;
93             try
94             {
95                 var resourceManager = GetResourceManager(cultureInfo.Name);
96                 if (resourceManager != null)
97                 {
98                     result = resourceManager.GetString(name, cultureInfo);
99                 }
100
101                 if (string.IsNullOrEmpty(result))
102                 {
103                     resourceManager = GetResourceManager("default");
104                     if (resourceManager != null)
105                     {
106 #pragma warning disable CA1304
107                         result = resourceManager.GetString(name);
108 #pragma warning restore CA1304
109                     }
110                 }
111             }
112             catch (InvalidOperationException e)
113             {
114                 Log.Error("InvalidOperationException occurs. " + e.Message);
115             }
116             catch (MissingManifestResourceException e)
117             {
118                 Log.Error("MissingManifestResourceException occurs. " + e.Message);
119             }
120             catch (MissingSatelliteAssemblyException e)
121             {
122                 Log.Error("MissingSateliteAssemblyException occurs. " + e.Message);
123             }
124
125             return result;
126         }
127
128         private global::System.Resources.ResourceManager GetResourceManager(string path, string baseName)
129         {
130             global::System.Resources.ResourceManager resourceManager = null;
131
132             if (string.IsNullOrEmpty(path))
133             {
134                 return null;
135             }
136
137             if (!File.Exists(path))
138             {
139                 Log.Warn(path + " does not exist");
140                 return null;
141             }
142
143 #pragma warning disable CA1031
144             try
145             {
146                 Assembly assembly = Assembly.Load(File.ReadAllBytes(path));
147                 if (assembly != null)
148                 {
149                     resourceManager = new global::System.Resources.ResourceManager(baseName, assembly);
150                     if (resourceManager == null)
151                     {
152                         Log.Error("Failed to create ResourceManager");
153                         return null;
154                     }
155                 }
156             }
157             catch (ArgumentNullException e)
158             {
159                 Log.Error("ArgumentNullException occurs. " + e.Message);
160             }
161             catch (BadImageFormatException e)
162             {
163                 Log.Error("BadImageFormatException occurs. " + e.Message);
164             }
165             catch (Exception e)
166             {
167                 Log.Error("Exception occurs. " + e.Message);
168             }
169 #pragma warning restore CA1031
170
171             return resourceManager;
172         }
173
174
175         private global::System.Resources.ResourceManager GetResourceManager(string locale)
176         {
177             global::System.Resources.ResourceManager resourceManager;
178
179             if (_resourceMap.TryGetValue(locale, out resourceManager))
180             {
181                 return resourceManager;
182             }
183
184             string baseName = _resourceClassName;
185             string path;
186             if (locale == "default")
187             {
188                 path = _resourcePath + _resourceDll;
189             }
190             else
191             {
192                 path = _resourcePath + locale + "/" + _resourceDll;
193                 baseName += "." + locale;
194             }
195
196             resourceManager = GetResourceManager(path, baseName);
197             if (resourceManager != null)
198             {
199                 _resourceMap.Add(locale, resourceManager);
200             }
201
202             return resourceManager;
203         }
204     }
205 }