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)
33 using System.Resources;
34 using System.Threading;
35 using System.Reflection;
38 namespace Novell.Directory.Ldap.Utilclass
41 /// <summary> A utility class to get strings from the ExceptionMessages and
42 /// ResultCodeMessages resources.
44 public class ResourcesHandler
46 // Cannot create an instance of this class
47 private ResourcesHandler()
53 * Initialized when the first result string is requested
55 private static System.Resources.ResourceManager defaultResultCodes = null;
57 /// <summary> Initialized when the first Exception message string is requested</summary>
58 private static System.Resources.ResourceManager defaultMessages = null;
61 /// <summary> Package where resources are found</summary>
62 private static System.String pkg = "Novell.Directory.Ldap.Utilclass.";
64 /// <summary> The default Locale</summary>
65 private static System.Globalization.CultureInfo defaultLocale;
67 /// <summary> Returns a string using the MessageOrKey as a key into
68 /// ExceptionMessages or, if the Key does not exist, returns the
69 /// string messageOrKey. In addition it formats the arguments into the message
70 /// according to MessageFormat.
73 /// <param name="messageOrKey"> Key string for the resource.
76 /// <param name="">arguments
79 /// <returns> the text for the message specified by the MessageKey or the Key
80 /// if it there is no message for that key.
82 public static System.String getMessage(System.String messageOrKey, System.Object[] arguments)
84 return getMessage(messageOrKey, arguments, null);
87 /// <summary> Returns the message stored in the ExceptionMessages resource for the
88 /// specified locale using messageOrKey and argments passed into the
89 /// constructor. If no string exists in the resource then this returns
90 /// the string stored in message. (This method is identical to
91 /// getLdapErrorMessage(Locale locale).)
94 /// <param name="messageOrKey"> Key string for the resource.
97 /// <param name="">arguments
100 /// <param name="locale"> The Locale that should be used to pull message
101 /// strings out of ExceptionMessages.
104 /// <returns> the text for the message specified by the MessageKey or the Key
105 /// if it there is no message for that key.
107 public static System.String getMessage(System.String messageOrKey, System.Object[] arguments, System.Globalization.CultureInfo locale)
109 if (defaultMessages == null)
111 defaultMessages = new ResourceManager ("ExceptionMessages", Assembly.GetExecutingAssembly ());
114 if (defaultLocale == null)
115 defaultLocale = Thread.CurrentThread.CurrentUICulture;
118 locale = defaultLocale;
120 if (messageOrKey == null)
128 pattern = defaultMessages.GetString(messageOrKey, locale);
130 catch (System.Resources.MissingManifestResourceException mre)
132 pattern = messageOrKey;
135 // Format the message if arguments were passed
136 if (arguments != null)
138 StringBuilder strB = new StringBuilder();
139 strB.AppendFormat(pattern, arguments);
140 pattern = strB.ToString();
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 if (defaultResultCodes == null)
183 defaultResultCodes = ResourceManager.CreateFileBasedResourceManager("ResultCodeMessages", "Resources", null);*/
184 defaultResultCodes = new ResourceManager ("ResultCodeMessages", Assembly.GetExecutingAssembly ());
188 if (defaultLocale == null)
189 defaultLocale = Thread.CurrentThread.CurrentUICulture;
192 locale = defaultLocale;
197 result = defaultResultCodes.GetString(Convert.ToString(code), defaultLocale);
199 catch (ArgumentNullException mre)
201 result = getMessage(ExceptionMessages.UNKNOWN_RESULT, new Object[]{code}, locale);
206 static ResourcesHandler()
208 defaultLocale = Thread.CurrentThread.CurrentUICulture;
210 } //end class ResourcesHandler