Class files Implementing various utility classes used by LDAP C# Libraray
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap.Utilclass / ResourcesHandler.cs
1 /******************************************************************************
2 * The MIT License
3 * Copyright (c) 2003 Novell Inc.  www.novell.com
4
5 * Permission is hereby granted, free of charge, to any person obtaining  a copy
6 * of this software and associated documentation files (the Software), to deal
7 * in the Software without restriction, including  without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
9 * copies of the Software, and to  permit persons to whom the Software is 
10 * furnished to do so, subject to the following conditions:
11
12 * The above copyright notice and this permission notice shall be included in 
13 * all copies or substantial portions of the Software.
14
15 * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *******************************************************************************/
23 //
24 // Novell.Directory.Ldap.Utilclass.ResourcesHandler.cs
25 //
26 // Author:
27 //   Sunil Kumar (Sunilk@novell.com)
28 //
29 // (C) 2003 Novell, Inc (http://www.novell.com)
30 //
31
32 using System;
33
34 namespace Novell.Directory.Ldap.Utilclass
35 {
36         
37         /// <summary>  A utility class to get strings from the ExceptionMessages and
38         /// ResultCodeMessages resources.
39         /// </summary>
40         public class ResourcesHandler
41         {
42                 // Cannot create an instance of this class
43                 private ResourcesHandler()
44                 {
45                         return ;
46                 }
47                 
48                 /*
49                 *  Initialized when the first result string is requested
50                 */
51                 private static System.Resources.ResourceManager defaultResultCodes = null;
52                 
53                 /// <summary>  Initialized when the first Exception message string is requested</summary>
54                 private static System.Resources.ResourceManager defaultMessages = null;
55                 
56                 
57                 /// <summary> Package where resources are found</summary>
58                 private static System.String pkg = "Novell.Directory.Ldap.Utilclass.";
59                 
60                 /// <summary> The default Locale</summary>
61                 private static System.Globalization.CultureInfo defaultLocale;
62                 
63                 /// <summary> Returns a string using the MessageOrKey as a key into
64                 /// ExceptionMessages or, if the Key does not exist, returns the
65                 /// string messageOrKey.  In addition it formats the arguments into the message
66                 /// according to MessageFormat.
67                 /// 
68                 /// </summary>
69                 /// <param name="messageOrKey">   Key string for the resource.
70                 /// <br><br>
71                 /// </param>
72                 /// <param name="">arguments
73                 /// 
74                 /// </param>
75                 /// <returns> the text for the message specified by the MessageKey or the Key
76                 /// if it there is no message for that key.
77                 /// </returns>
78                 public static System.String getMessage(System.String messageOrKey, System.Object[] arguments)
79                 {
80                         return getMessage(messageOrKey, arguments, null);
81                 }
82                 
83                 /// <summary> Returns the message stored in the ExceptionMessages resource for the
84                 /// specified locale using messageOrKey and argments passed into the
85                 /// constructor.  If no string exists in the resource then this returns
86                 /// the string stored in message.  (This method is identical to
87                 /// getLdapErrorMessage(Locale locale).)
88                 /// 
89                 /// </summary>
90                 /// <param name="messageOrKey">   Key string for the resource.
91                 /// <br><br>
92                 /// </param>
93                 /// <param name="">arguments
94                 /// <br><br>
95                 /// </param>
96                 /// <param name="locale">         The Locale that should be used to pull message
97                 /// strings out of ExceptionMessages.
98                 /// 
99                 /// </param>
100                 /// <returns> the text for the message specified by the MessageKey or the Key
101                 /// if it there is no message for that key.
102                 /// </returns>
103                 public static System.String getMessage(System.String messageOrKey, System.Object[] arguments, System.Globalization.CultureInfo locale)
104                 {
105                         System.String pattern;
106                         System.Resources.ResourceManager messages = null;
107                         
108                         if ((System.Object) messageOrKey == null)
109                         {
110                                 messageOrKey = "";
111                         }
112                         
113                         try
114                         {
115                                 if ((locale == null) || defaultLocale.Equals(locale))
116                                 {
117                                         locale = defaultLocale;
118                                         // Default Locale
119                                         if (defaultMessages == null)
120                                         {
121                                                 System.Threading.Thread.CurrentThread.CurrentUICulture = defaultLocale;
122                                                 defaultMessages = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ExceptionMessages", "", null);
123                                         }
124                                         messages = defaultMessages;
125                                 }
126                                 else
127                                 {
128                                         System.Threading.Thread.CurrentThread.CurrentUICulture = locale;
129                                         messages = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ExceptionMessages", "", null);
130                                 }
131                                 pattern = messages.GetString(messageOrKey);
132                         }
133                         catch (System.Resources.MissingManifestResourceException mre)
134                         {
135                                 pattern = messageOrKey;
136                         }
137                         
138                         // Format the message if arguments were passed
139                         if (arguments != null)
140                         {
141 //                              MessageFormat mf = new MessageFormat(pattern);
142                                 pattern=System.String.Format(locale,pattern,arguments);
143 //                              mf.setLocale(locale);
144                                 //this needs to be reset with the new local - i18n defect in java
145 //                              mf.applyPattern(pattern);
146 //                              pattern = mf.format(arguments);
147                         }
148                         return pattern;
149                 }
150                 
151                 /// <summary> Returns a string representing the Ldap result code from the 
152                 /// default ResultCodeMessages resource.
153                 /// 
154                 /// </summary>
155                 /// <param name="code">   the result code 
156                 /// <br><br>
157                 /// </param>
158                 /// <returns>        the String representing the result code.
159                 /// </returns>
160                 public static System.String getResultString(int code)
161                 {
162                         return getResultString(code, null);
163                 }
164                 
165                 /// <summary> Returns a string representing the Ldap result code.  The message
166                 /// is obtained from the locale specific ResultCodeMessage resource.
167                 /// 
168                 /// </summary>
169                 /// <param name="code">   the result code 
170                 /// <br><br>
171                 /// </param>
172                 /// <param name="locale">         The Locale that should be used to pull message
173                 /// strings out of ResultMessages.
174                 /// 
175                 /// </param>
176                 /// <returns>        the String representing the result code.
177                 /// </returns>
178                 public static System.String getResultString(int code, System.Globalization.CultureInfo locale)
179                 {
180                         System.Resources.ResourceManager messages;
181                         System.String result;
182                         try
183                         {
184                                 if ((locale == null) || defaultLocale.Equals(locale))
185                                 {
186                                         locale = defaultLocale;
187                                         // Default Locale
188                                         if (defaultResultCodes == null)
189                                         {
190                                                 System.Threading.Thread.CurrentThread.CurrentUICulture = defaultLocale;
191                                                 defaultResultCodes = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ResultCodeMessages", "", null);
192                                         }
193                                         messages = defaultResultCodes;
194 //                                      Console.WriteLine("Test Message.." + pkg + "ResultCodeMessages" );
195                                 }
196                                 else
197                                 {
198                                         System.Threading.Thread.CurrentThread.CurrentUICulture = locale;
199                                         messages = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ResultCodeMessages", "", null);
200                                 }
201 //                              result = messages.GetString(System.Convert.ToString(code));
202                                 result = Convert.ToString(code);
203                         }
204                         catch (System.Resources.MissingManifestResourceException mre)
205                         {
206                                 result = getMessage(ExceptionMessages.UNKNOWN_RESULT, new System.Object[]{code}, locale);
207                         }
208                         return result;
209                 }
210                 static ResourcesHandler()
211                 {
212 //                      defaultLocale = System.Globalization.CultureInfo.CurrentCulture;
213                         defaultLocale=new System.Globalization.CultureInfo("en-US");
214
215                 }
216         } //end class ResourcesHandler
217 }