**** Merged from MCS ****
[mono-project.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap.Utilclass / ResourcesHandler.cs
blob342913e27764a35a69cfe05f00ec72649ed1dade
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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 *******************************************************************************/
24 // Novell.Directory.Ldap.Utilclass.ResourcesHandler.cs
26 // Author:
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
32 using System;
34 namespace Novell.Directory.Ldap.Utilclass
37 /// <summary> A utility class to get strings from the ExceptionMessages and
38 /// ResultCodeMessages resources.
39 /// </summary>
40 public class ResourcesHandler
42 // Cannot create an instance of this class
43 private ResourcesHandler()
45 return ;
49 * Initialized when the first result string is requested
51 private static System.Resources.ResourceManager defaultResultCodes = null;
53 /// <summary> Initialized when the first Exception message string is requested</summary>
54 private static System.Resources.ResourceManager defaultMessages = null;
57 /// <summary> Package where resources are found</summary>
58 private static System.String pkg = "Novell.Directory.Ldap.Utilclass.";
60 /// <summary> The default Locale</summary>
61 private static System.Globalization.CultureInfo defaultLocale;
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 ///
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)
80 return getMessage(messageOrKey, arguments, null);
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 ///
92 /// </param>
93 /// <param name="">arguments
94 ///
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)
105 System.String pattern;
106 System.Resources.ResourceManager messages = null;
108 if ((System.Object) messageOrKey == null)
110 messageOrKey = "";
115 if ((locale == null) || defaultLocale.Equals(locale))
117 locale = defaultLocale;
118 // Default Locale
119 if (defaultMessages == null)
121 System.Threading.Thread.CurrentThread.CurrentUICulture = defaultLocale;
122 defaultMessages = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ExceptionMessages", "", null);
124 messages = defaultMessages;
126 else
128 System.Threading.Thread.CurrentThread.CurrentUICulture = locale;
129 messages = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ExceptionMessages", "", null);
131 pattern = messages.GetString(messageOrKey);
133 catch (System.Resources.MissingManifestResourceException mre)
135 pattern = messageOrKey;
138 // Format the message if arguments were passed
139 if (arguments != null)
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);
148 return pattern;
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 ///
157 /// </param>
158 /// <returns> the String representing the result code.
159 /// </returns>
160 public static System.String getResultString(int code)
162 return getResultString(code, null);
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 ///
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)
180 System.Resources.ResourceManager messages;
181 System.String result;
184 if ((locale == null) || defaultLocale.Equals(locale))
186 locale = defaultLocale;
187 // Default Locale
188 if (defaultResultCodes == null)
190 // System.Threading.Thread.CurrentThread.CurrentUICulture = defaultLocale;
191 defaultResultCodes = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ResultCodeMessages", "", null);
193 messages = defaultResultCodes;
195 else
197 System.Threading.Thread.CurrentThread.CurrentUICulture = locale;
198 messages = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ResultCodeMessages", "", null);
200 // result = messages.GetString(System.Convert.ToString(code));
201 result = Convert.ToString(code);
203 catch (System.Resources.MissingManifestResourceException mre)
205 result = getMessage(ExceptionMessages.UNKNOWN_RESULT, new System.Object[]{code}, locale);
207 return result;
209 static ResourcesHandler()
211 // defaultLocale = System.Globalization.CultureInfo.CurrentCulture;
213 } //end class ResourcesHandler