1 /******************************************************************************
3 * Copyright (c) 2003 Novell Inc. www.novell.com
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
22 *******************************************************************************/
24 // Novell.Directory.Ldap.Utilclass.ResourcesHandler.cs
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
34 namespace Novell.Directory.Ldap.Utilclass
37 /// <summary> A utility class to get strings from the ExceptionMessages and
38 /// ResultCodeMessages resources.
40 public class ResourcesHandler
42 // Cannot create an instance of this class
43 private ResourcesHandler()
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.
69 /// <param name="messageOrKey"> Key string for the resource.
72 /// <param name="">arguments
75 /// <returns> the text for the message specified by the MessageKey or the Key
76 /// if it there is no message for that key.
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).)
90 /// <param name="messageOrKey"> Key string for the resource.
93 /// <param name="">arguments
96 /// <param name="locale"> The Locale that should be used to pull message
97 /// strings out of ExceptionMessages.
100 /// <returns> the text for the message specified by the MessageKey or the Key
101 /// if it there is no message for that key.
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)
115 if ((locale == null) || defaultLocale.Equals(locale))
117 locale = defaultLocale;
119 if (defaultMessages == null)
121 System.Threading.Thread.CurrentThread.CurrentUICulture = defaultLocale;
122 defaultMessages = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ExceptionMessages", "", null);
124 messages = defaultMessages;
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);
151 /// <summary> Returns a string representing the Ldap result code from the
152 /// default ResultCodeMessages resource.
155 /// <param name="code"> the result code
158 /// <returns> the String representing the result code.
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.
169 /// <param name="code"> the result code
172 /// <param name="locale"> The Locale that should be used to pull message
173 /// strings out of ResultMessages.
176 /// <returns> the String representing the result code.
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;
188 if (defaultResultCodes == null)
190 System.Threading.Thread.CurrentThread.CurrentUICulture = defaultLocale;
191 defaultResultCodes = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ResultCodeMessages", "", null);
193 messages = defaultResultCodes;
194 // Console.WriteLine("Test Message.." + pkg + "ResultCodeMessages" );
198 System.Threading.Thread.CurrentThread.CurrentUICulture = locale;
199 messages = System.Resources.ResourceManager.CreateFileBasedResourceManager(pkg + "ResultCodeMessages", "", null);
201 // result = messages.GetString(System.Convert.ToString(code));
202 result = Convert.ToString(code);
204 catch (System.Resources.MissingManifestResourceException mre)
206 result = getMessage(ExceptionMessages.UNKNOWN_RESULT, new System.Object[]{code}, locale);
210 static ResourcesHandler()
212 // defaultLocale = System.Globalization.CultureInfo.CurrentCulture;
213 defaultLocale=new System.Globalization.CultureInfo("en-US");
216 } //end class ResourcesHandler