Fix for running against RabbitMQ 2.2
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / GlobalizationSection.cs
1 //
2 // System.Web.Configuration.GlobalizationSection
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Configuration;
34 using System.Globalization;
35 using System.Text;
36 using System.Xml;
37 using System.Web.Util;
38
39 #if NET_2_0
40
41 namespace System.Web.Configuration {
42
43         public sealed class GlobalizationSection : ConfigurationSection
44         {
45                 static ConfigurationProperty cultureProp;
46                 static ConfigurationProperty enableBestFitResponseEncodingProp;
47                 static ConfigurationProperty enableClientBasedCultureProp;
48                 static ConfigurationProperty fileEncodingProp;
49                 static ConfigurationProperty requestEncodingProp;
50                 static ConfigurationProperty resourceProviderFactoryTypeProp;
51                 static ConfigurationProperty responseEncodingProp;
52                 static ConfigurationProperty responseHeaderEncodingProp;
53                 static ConfigurationProperty uiCultureProp;
54                 static ConfigurationPropertyCollection properties;
55
56                 static GlobalizationSection ()
57                 {
58                         cultureProp = new ConfigurationProperty ("culture", typeof (string), "");
59                         enableBestFitResponseEncodingProp = new ConfigurationProperty ("enableBestFitResponseEncoding", typeof (bool), false);
60                         enableClientBasedCultureProp = new ConfigurationProperty ("enableClientBasedCulture", typeof (bool), false);
61                         fileEncodingProp = new ConfigurationProperty ("fileEncoding", typeof (string));
62                         requestEncodingProp = new ConfigurationProperty ("requestEncoding", typeof (string), "utf-8");
63                         resourceProviderFactoryTypeProp = new ConfigurationProperty ("resourceProviderFactoryType", typeof (string), "");
64                         responseEncodingProp = new ConfigurationProperty ("responseEncoding", typeof (string), "utf-8");
65                         responseHeaderEncodingProp = new ConfigurationProperty ("responseHeaderEncoding", typeof (string), "utf-8");
66                         uiCultureProp = new ConfigurationProperty ("uiCulture", typeof (string), "");
67                         properties = new ConfigurationPropertyCollection ();
68
69                         properties.Add (cultureProp);
70                         properties.Add (enableBestFitResponseEncodingProp);
71                         properties.Add (enableClientBasedCultureProp);
72                         properties.Add (fileEncodingProp);
73                         properties.Add (requestEncodingProp);
74                         properties.Add (resourceProviderFactoryTypeProp);
75                         properties.Add (responseEncodingProp);
76                         properties.Add (responseHeaderEncodingProp);
77                         properties.Add (uiCultureProp);
78                 }
79
80                 public GlobalizationSection ()
81                 {
82                         encodingHash = new Hashtable ();
83                 }
84
85                 void VerifyData ()
86                 {
87                         bool fake = false;
88                         try {
89                                 GetSanitizedCulture (Culture, ref fake);
90                         }
91                         catch {
92                                 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'culture' attribute");
93                         }
94
95                         try {
96                                 GetSanitizedCulture (UICulture, ref fake);
97                         }
98                         catch {
99                                 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'uiCulture' attribute");
100                         }
101                 }
102
103                 protected override void PostDeserialize ()
104                 {
105                         base.PostDeserialize();
106
107                         VerifyData ();
108                 }
109
110                 protected override void PreSerialize (XmlWriter writer)
111                 {
112                         base.PreSerialize(writer);
113
114                         VerifyData ();
115                 }
116
117                 [ConfigurationProperty ("culture", DefaultValue = "")]
118                 public string Culture {
119                         get { return (string) base [cultureProp];}
120                         set { base[cultureProp] = value; }
121                 }
122
123                 [ConfigurationProperty ("enableBestFitResponseEncoding", DefaultValue = "False")]
124                 public bool EnableBestFitResponseEncoding {
125                         get { return (bool) base [enableBestFitResponseEncodingProp];}
126                         set { base[enableBestFitResponseEncodingProp] = value; }
127                 }
128
129                 [ConfigurationProperty ("enableClientBasedCulture", DefaultValue = "False")]
130                 public bool EnableClientBasedCulture {
131                         get { return (bool) base [enableClientBasedCultureProp];}
132                         set { base[enableClientBasedCultureProp] = value; }
133                 }
134
135                 [ConfigurationProperty ("fileEncoding")]
136                 public Encoding FileEncoding {
137                         get { return GetEncoding (fileEncodingProp, ref cached_fileencoding); }
138                         set { base[fileEncodingProp] = value.WebName; }
139                 }
140
141                 [ConfigurationProperty ("requestEncoding", DefaultValue = "utf-8")]
142                 public Encoding RequestEncoding {
143                         get { return GetEncoding (requestEncodingProp, ref cached_requestencoding); }
144                         set { base[requestEncodingProp] = value.WebName; }
145                 }
146
147                 [ConfigurationProperty ("resourceProviderFactoryType", DefaultValue = "")]
148                 public string ResourceProviderFactoryType {
149                         get { return (string) base [resourceProviderFactoryTypeProp];}
150                         set { base[resourceProviderFactoryTypeProp] = value; }
151                 }
152
153                 [ConfigurationProperty ("responseEncoding", DefaultValue = "utf-8")]
154                 public Encoding ResponseEncoding {
155                         get { return GetEncoding (responseEncodingProp, ref cached_responseencoding); }
156                         set { base[responseEncodingProp] = value.WebName; }
157                 }
158
159                 [ConfigurationProperty ("responseHeaderEncoding", DefaultValue = "utf-8")]
160                 public Encoding ResponseHeaderEncoding {
161                         get { return GetEncoding (responseHeaderEncodingProp, ref cached_responseheaderencoding); }
162                         set { base[responseHeaderEncodingProp] = value.WebName; }
163                 }
164
165                 [ConfigurationProperty ("uiCulture", DefaultValue = "")]
166                 public string UICulture {
167                         get { return (string) base [uiCultureProp];}
168                         set { base[uiCultureProp] = value; }
169                 }
170
171                 protected override ConfigurationPropertyCollection Properties {
172                         get { return properties; }
173                 }
174
175 #region CompatabilityCode
176                 string cached_fileencoding;
177                 string cached_requestencoding;
178                 string cached_responseencoding;
179                 string cached_responseheaderencoding;
180                 Hashtable encodingHash;
181
182                 string cached_culture;
183                 CultureInfo cached_cultureinfo;
184
185                 string cached_uiculture;
186                 CultureInfo cached_uicultureinfo;
187
188                 static bool encoding_warning;
189                 static bool culture_warning;
190
191                 bool autoCulture;
192                 bool autoUICulture;
193
194                 internal bool IsAutoCulture {
195                         get { return autoCulture; }
196                 }
197
198                 internal bool IsAutoUICulture {
199                         get { return autoUICulture; }
200                 }
201                 
202                 CultureInfo GetSanitizedCulture (string culture, ref bool auto)
203                 {
204                         auto = false;
205                         if (culture == null)
206                                 throw new ArgumentNullException ("culture");
207                         if (culture.Length <= 3)
208                                 return new CultureInfo (culture);
209                         if (culture.StartsWith ("auto")) {
210                                 auto = true;
211                                 if (culture.Length > 5 && culture[4] == ':')
212                                         return new CultureInfo (culture.Substring (5));
213                                 return Helpers.InvariantCulture;// (0x007f);
214                         }
215
216                         return new CultureInfo (culture);
217                 }
218                                 
219                 internal CultureInfo GetUICulture ()
220                 {
221                         string uiculture = UICulture;
222                         if (cached_uiculture != uiculture) {
223                                 try {
224                                         cached_uicultureinfo = GetSanitizedCulture (uiculture, ref autoUICulture);
225                                         cached_uiculture = uiculture;
226                                 } catch {
227                                         CultureFailed ("UICulture", uiculture);
228                                         cached_uicultureinfo = new CultureInfo (0x007f); // Invariant
229                                         cached_uiculture = null;
230                                 }
231                         }
232
233                         return cached_uicultureinfo;
234                 }
235
236                 internal CultureInfo GetCulture ()
237                 {
238                         string culture = Culture;
239                         if (cached_culture != culture) {
240                                 try {
241                                         cached_cultureinfo = GetSanitizedCulture (culture, ref autoCulture);
242                                         cached_culture = culture;
243                                 } catch {
244                                         CultureFailed ("Culture", culture);
245                                         cached_cultureinfo = new CultureInfo (0x007f); // Invariant
246                                         cached_culture = null;
247                                 }
248                         }
249
250                         return cached_cultureinfo;
251                 }
252
253                 Encoding GetEncoding (ConfigurationProperty prop, ref string cached_encoding_name)
254                 {
255                         string enc = (string) base [prop];
256                         if (cached_encoding_name == null)
257                                 cached_encoding_name = ((enc == null) ? "utf-8" : enc);
258
259                         Encoding encoding = (Encoding)encodingHash [prop];
260                         if (encoding == null || encoding.WebName != cached_encoding_name) {
261                                 try {
262                                         switch (cached_encoding_name.ToLower (Helpers.InvariantCulture)) {
263                                         case "utf-16le":
264                                         case "utf-16":
265                                         case "ucs-2":
266                                         case "unicode":
267                                         case "iso-10646-ucs-2":
268                                                 encoding = new UnicodeEncoding (false, true);
269                                                 break;
270                                         case "utf-16be":
271                                         case "unicodefffe":
272                                                 encoding = new UnicodeEncoding (true, true);
273                                                 break;
274                                         case "utf-8":
275                                         case "unicode-1-1-utf-8":
276                                         case "unicode-2-0-utf-8":
277                                         case "x-unicode-1-1-utf-8":
278                                         case "x-unicode-2-0-utf-8":
279                                                 encoding = Encoding.UTF8;
280                                                 break;
281                                         default:
282                                                 encoding = Encoding.GetEncoding (cached_encoding_name);
283                                                 break;
284                                         }
285                                 } catch {
286                                         EncodingFailed (prop.Name, cached_encoding_name);
287                                         encoding = new UTF8Encoding (false, false);
288                                 }
289                         }
290
291                         encodingHash[prop] = encoding;
292                         cached_encoding_name = encoding.WebName;
293
294                         return encoding;
295                 }
296
297                 static void EncodingFailed (string att, string enc)
298                 {
299                         if (encoding_warning)
300                                 return;
301
302                         encoding_warning = true;
303                         Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
304                                            "{0}=\"{1}\"\n", att, enc);
305                 }
306
307                 static void CultureFailed (string att, string cul)
308                 {
309                         if (culture_warning)
310                                 return;
311
312                         culture_warning = true;
313                         Console.WriteLine ("Culture {1} cannot be loaded. Perhaps your runtime \n" +
314                                            "don't have ICU support?\n{0}=\"{1}\"\n", att, cul);
315                 }
316
317 #endregion
318         }
319
320 }
321
322 #endif
323