Debian Codegraph @

mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap.Utilclass/ResourcesHandler.cs

raw
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 ([email protected])
28 //
29 // (C) 2003 Novell, Inc (http://www.novell.com)
30 //
31
32 using System;
33 using System.Resources;
34 using System.Threading;
35 using System.Reflection;
36 using System.Text;
37
38 namespace Novell.Directory.Ldap.Utilclass
39 {
40
41 /// <summary> A utility class to get strings from the ExceptionMessages and
42 /// ResultCodeMessages resources.
43 /// </summary>
44 public class ResourcesHandler
45 {
46 // Cannot create an instance of this class
47 private ResourcesHandler()
48 {
49 return ;
50 }
51
52 /*
53 * Initialized when the first result string is requested
54 */
55 private static System.Resources.ResourceManager defaultResultCodes = null;
56
57 /// <summary> Initialized when the first Exception message string is requested</summary>
58 private static System.Resources.ResourceManager defaultMessages = null;
59
60
61 /// <summary> Package where resources are found</summary>
62 private static System.String pkg = "Novell.Directory.Ldap.Utilclass.";
63
64 /// <summary> The default Locale</summary>
65 private static System.Globalization.CultureInfo defaultLocale;
66
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.
71 ///
72 /// </summary>
73 /// <param name="messageOrKey"> Key string for the resource.
74 ///
75 /// </param>
76 /// <param name="">arguments
77 ///
78 /// </param>
79 /// <returns> the text for the message specified by the MessageKey or the Key
80 /// if it there is no message for that key.
81 /// </returns>
82 public static System.String getMessage(System.String messageOrKey, System.Object[] arguments)
83 {
84 return getMessage(messageOrKey, arguments, null);
85 }
86
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).)
92 ///
93 /// </summary>
94 /// <param name="messageOrKey"> Key string for the resource.
95 ///
96 /// </param>
97 /// <param name="">arguments
98 ///
99 /// </param>
100 /// <param name="locale"> The Locale that should be used to pull message
101 /// strings out of ExceptionMessages.
102 ///
103 /// </param>
104 /// <returns> the text for the message specified by the MessageKey or the Key
105 /// if it there is no message for that key.
106 /// </returns>
107 public static System.String getMessage(System.String messageOrKey, System.Object[] arguments, System.Globalization.CultureInfo locale)
108 {
109 if (defaultMessages == null)
110 {
111 defaultMessages = new ResourceManager ("ExceptionMessages", Assembly.GetExecutingAssembly ());
112 }
113
114 if (defaultLocale == null)
115 defaultLocale = Thread.CurrentThread.CurrentUICulture;
116
117 if (locale == null)
118 locale = defaultLocale;
119
120 if (messageOrKey == null)
121 {
122 messageOrKey = "";
123 }
124
125 string pattern;
126 try
127 {
128 pattern = defaultMessages.GetString(messageOrKey, locale);
129 }
130 catch (System.Resources.MissingManifestResourceException mre)
131 {
132 pattern = messageOrKey;
133 }
134
135 // Format the message if arguments were passed
136 if (arguments != null)
137 {
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);
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 ///
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 ///
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 if (defaultResultCodes == null)
181 {
182 /*
183 defaultResultCodes = ResourceManager.CreateFileBasedResourceManager("ResultCodeMessages", "Resources", null);*/
184 defaultResultCodes = new ResourceManager ("ResultCodeMessages", Assembly.GetExecutingAssembly ());
185
186 }
187
188 if (defaultLocale == null)
189 defaultLocale = Thread.CurrentThread.CurrentUICulture;
190
191 if (locale == null)
192 locale = defaultLocale;
193
194 string result;
195 try
196 {
197 result = defaultResultCodes.GetString(Convert.ToString(code), defaultLocale);
198 }
199 catch (ArgumentNullException mre)
200 {
201 result = getMessage(ExceptionMessages.UNKNOWN_RESULT, new Object[]{code}, locale);
202 }
203 return result;
204 }
205
206 static ResourcesHandler()
207 {
208 defaultLocale = Thread.CurrentThread.CurrentUICulture;
209 }
210 } //end class ResourcesHandler
211 }