System.Web.Configuration.(AdapterDictionary,AuthenticationMode,MachineKeyValidation...
[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
40 namespace System.Web.Configuration {
41
42         public sealed class GlobalizationSection : ConfigurationSection
43         {
44                 static ConfigurationProperty cultureProp;
45                 static ConfigurationProperty enableBestFitResponseEncodingProp;
46                 static ConfigurationProperty enableClientBasedCultureProp;
47                 static ConfigurationProperty fileEncodingProp;
48                 static ConfigurationProperty requestEncodingProp;
49                 static ConfigurationProperty resourceProviderFactoryTypeProp;
50                 static ConfigurationProperty responseEncodingProp;
51                 static ConfigurationProperty responseHeaderEncodingProp;
52                 static ConfigurationProperty uiCultureProp;
53                 static ConfigurationPropertyCollection properties;
54
55                 static GlobalizationSection ()
56                 {
57                         cultureProp = new ConfigurationProperty ("culture", typeof (string), "");
58                         enableBestFitResponseEncodingProp = new ConfigurationProperty ("enableBestFitResponseEncoding", typeof (bool), false);
59                         enableClientBasedCultureProp = new ConfigurationProperty ("enableClientBasedCulture", typeof (bool), false);
60                         fileEncodingProp = new ConfigurationProperty ("fileEncoding", typeof (string));
61                         requestEncodingProp = new ConfigurationProperty ("requestEncoding", typeof (string), "utf-8");
62                         resourceProviderFactoryTypeProp = new ConfigurationProperty ("resourceProviderFactoryType", typeof (string), "");
63                         responseEncodingProp = new ConfigurationProperty ("responseEncoding", typeof (string), "utf-8");
64                         responseHeaderEncodingProp = new ConfigurationProperty ("responseHeaderEncoding", typeof (string), "utf-8");
65                         uiCultureProp = new ConfigurationProperty ("uiCulture", typeof (string), "");
66                         properties = new ConfigurationPropertyCollection ();
67
68                         properties.Add (cultureProp);
69                         properties.Add (enableBestFitResponseEncodingProp);
70                         properties.Add (enableClientBasedCultureProp);
71                         properties.Add (fileEncodingProp);
72                         properties.Add (requestEncodingProp);
73                         properties.Add (resourceProviderFactoryTypeProp);
74                         properties.Add (responseEncodingProp);
75                         properties.Add (responseHeaderEncodingProp);
76                         properties.Add (uiCultureProp);
77                 }
78
79                 public GlobalizationSection ()
80                 {
81                         encodingHash = new Hashtable ();
82                 }
83
84                 void VerifyData ()
85                 {
86                         bool fake = false;
87                         try {
88                                 GetSanitizedCulture (Culture, ref fake);
89                         }
90                         catch {
91                                 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'culture' attribute");
92                         }
93
94                         try {
95                                 GetSanitizedCulture (UICulture, ref fake);
96                         }
97                         catch {
98                                 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'uiCulture' attribute");
99                         }
100                 }
101
102                 protected override void PostDeserialize ()
103                 {
104                         base.PostDeserialize();
105
106                         VerifyData ();
107                 }
108
109                 protected override void PreSerialize (XmlWriter writer)
110                 {
111                         base.PreSerialize(writer);
112
113                         VerifyData ();
114                 }
115
116                 [ConfigurationProperty ("culture", DefaultValue = "")]
117                 public string Culture {
118                         get { return (string) base [cultureProp];}
119                         set { base[cultureProp] = value; }
120                 }
121
122                 [ConfigurationProperty ("enableBestFitResponseEncoding", DefaultValue = "False")]
123                 public bool EnableBestFitResponseEncoding {
124                         get { return (bool) base [enableBestFitResponseEncodingProp];}
125                         set { base[enableBestFitResponseEncodingProp] = value; }
126                 }
127
128                 [ConfigurationProperty ("enableClientBasedCulture", DefaultValue = "False")]
129                 public bool EnableClientBasedCulture {
130                         get { return (bool) base [enableClientBasedCultureProp];}
131                         set { base[enableClientBasedCultureProp] = value; }
132                 }
133
134                 [ConfigurationProperty ("fileEncoding")]
135                 public Encoding FileEncoding {
136                         get { return GetEncoding (fileEncodingProp, ref cached_fileencoding); }
137                         set { base[fileEncodingProp] = value.WebName; }
138                 }
139
140                 [ConfigurationProperty ("requestEncoding", DefaultValue = "utf-8")]
141                 public Encoding RequestEncoding {
142                         get { return GetEncoding (requestEncodingProp, ref cached_requestencoding); }
143                         set { base[requestEncodingProp] = value.WebName; }
144                 }
145
146                 [ConfigurationProperty ("resourceProviderFactoryType", DefaultValue = "")]
147                 public string ResourceProviderFactoryType {
148                         get { return (string) base [resourceProviderFactoryTypeProp];}
149                         set { base[resourceProviderFactoryTypeProp] = value; }
150                 }
151
152                 [ConfigurationProperty ("responseEncoding", DefaultValue = "utf-8")]
153                 public Encoding ResponseEncoding {
154                         get { return GetEncoding (responseEncodingProp, ref cached_responseencoding); }
155                         set { base[responseEncodingProp] = value.WebName; }
156                 }
157
158                 [ConfigurationProperty ("responseHeaderEncoding", DefaultValue = "utf-8")]
159                 public Encoding ResponseHeaderEncoding {
160                         get { return GetEncoding (responseHeaderEncodingProp, ref cached_responseheaderencoding); }
161                         set { base[responseHeaderEncodingProp] = value.WebName; }
162                 }
163
164                 [ConfigurationProperty ("uiCulture", DefaultValue = "")]
165                 public string UICulture {
166                         get { return (string) base [uiCultureProp];}
167                         set { base[uiCultureProp] = value; }
168                 }
169
170                 protected internal override ConfigurationPropertyCollection Properties {
171                         get { return properties; }
172                 }
173
174 #region CompatabilityCode
175                 string cached_fileencoding;
176                 string cached_requestencoding;
177                 string cached_responseencoding;
178                 string cached_responseheaderencoding;
179                 Hashtable encodingHash;
180
181                 string cached_culture;
182                 CultureInfo cached_cultureinfo;
183
184                 string cached_uiculture;
185                 CultureInfo cached_uicultureinfo;
186
187                 static bool encoding_warning;
188                 static bool culture_warning;
189
190                 bool autoCulture;
191                 bool autoUICulture;
192
193                 internal bool IsAutoCulture {
194                         get { return autoCulture; }
195                 }
196
197                 internal bool IsAutoUICulture {
198                         get { return autoUICulture; }
199                 }
200                 
201                 CultureInfo GetSanitizedCulture (string culture, ref bool auto)
202                 {
203                         auto = false;
204                         if (culture == null)
205                                 throw new ArgumentNullException ("culture");
206                         if (culture.Length <= 3)
207                                 return new CultureInfo (culture);
208                         if (culture.StartsWith ("auto")) {
209                                 auto = true;
210                                 if (culture.Length > 5 && culture[4] == ':')
211                                         return new CultureInfo (culture.Substring (5));
212                                 return Helpers.InvariantCulture;// (0x007f);
213                         }
214
215                         return new CultureInfo (culture);
216                 }
217                                 
218                 internal CultureInfo GetUICulture ()
219                 {
220                         string uiculture = UICulture;
221                         if (cached_uiculture != uiculture) {
222                                 try {
223                                         cached_uicultureinfo = GetSanitizedCulture (uiculture, ref autoUICulture);
224                                         cached_uiculture = uiculture;
225                                 } catch {
226                                         CultureFailed ("UICulture", uiculture);
227                                         cached_uicultureinfo = new CultureInfo (0x007f); // Invariant
228                                         cached_uiculture = null;
229                                 }
230                         }
231
232                         return cached_uicultureinfo;
233                 }
234
235                 internal CultureInfo GetCulture ()
236                 {
237                         string culture = Culture;
238                         if (cached_culture != culture) {
239                                 try {
240                                         cached_cultureinfo = GetSanitizedCulture (culture, ref autoCulture);
241                                         cached_culture = culture;
242                                 } catch {
243                                         CultureFailed ("Culture", culture);
244                                         cached_cultureinfo = new CultureInfo (0x007f); // Invariant
245                                         cached_culture = null;
246                                 }
247                         }
248
249                         return cached_cultureinfo;
250                 }
251
252                 Encoding GetEncoding (ConfigurationProperty prop, ref string cached_encoding_name)
253                 {
254                         string enc = (string) base [prop];
255                         if (cached_encoding_name == null)
256                                 cached_encoding_name = ((enc == null) ? "utf-8" : enc);
257
258                         Encoding encoding = (Encoding)encodingHash [prop];
259                         if (encoding == null || encoding.WebName != cached_encoding_name) {
260                                 try {
261                                         switch (cached_encoding_name.ToLower (Helpers.InvariantCulture)) {
262                                         case "utf-16le":
263                                         case "utf-16":
264                                         case "ucs-2":
265                                         case "unicode":
266                                         case "iso-10646-ucs-2":
267                                                 encoding = new UnicodeEncoding (false, true);
268                                                 break;
269                                         case "utf-16be":
270                                         case "unicodefffe":
271                                                 encoding = new UnicodeEncoding (true, true);
272                                                 break;
273                                         case "utf-8":
274                                         case "unicode-1-1-utf-8":
275                                         case "unicode-2-0-utf-8":
276                                         case "x-unicode-1-1-utf-8":
277                                         case "x-unicode-2-0-utf-8":
278                                                 encoding = Encoding.UTF8;
279                                                 break;
280                                         default:
281                                                 encoding = Encoding.GetEncoding (cached_encoding_name);
282                                                 break;
283                                         }
284                                 } catch {
285                                         EncodingFailed (prop.Name, cached_encoding_name);
286                                         encoding = new UTF8Encoding (false, false);
287                                 }
288                         }
289
290                         encodingHash[prop] = encoding;
291                         cached_encoding_name = encoding.WebName;
292
293                         return encoding;
294                 }
295
296                 static void EncodingFailed (string att, string enc)
297                 {
298                         if (encoding_warning)
299                                 return;
300
301                         encoding_warning = true;
302                         Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
303                                            "{0}=\"{1}\"\n", att, enc);
304                 }
305
306                 static void CultureFailed (string att, string cul)
307                 {
308                         if (culture_warning)
309                                 return;
310
311                         culture_warning = true;
312                         Console.WriteLine ("Culture {1} cannot be loaded. Perhaps your runtime \n" +
313                                            "don't have ICU support?\n{0}=\"{1}\"\n", att, cul);
314                 }
315
316 #endregion
317         }
318
319 }
320
321