New test.
[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
38 #if NET_2_0
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                         try {
87                                 new CultureInfo (Culture);
88                         }
89                         catch {
90                                 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'culture' attribute");
91                         }
92
93                         try {
94                                 new CultureInfo (UICulture);
95                         }
96                         catch {
97                                 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'uiCulture' attribute");
98                         }
99                 }
100
101                 protected override void PostDeserialize ()
102                 {
103                         base.PostDeserialize();
104
105                         VerifyData ();
106                 }
107
108                 protected override void PreSerialize (XmlWriter writer)
109                 {
110                         base.PreSerialize(writer);
111
112                         VerifyData ();
113                 }
114
115                 [ConfigurationProperty ("culture", DefaultValue = "")]
116                 public string Culture {
117                         get { return (string) base [cultureProp];}
118                         set { base[cultureProp] = value; }
119                 }
120
121                 [ConfigurationProperty ("enableBestFitResponseEncoding", DefaultValue = "False")]
122                 public bool EnableBestFitResponseEncoding {
123                         get { return (bool) base [enableBestFitResponseEncodingProp];}
124                         set { base[enableBestFitResponseEncodingProp] = value; }
125                 }
126
127                 [ConfigurationProperty ("enableClientBasedCulture", DefaultValue = "False")]
128                 public bool EnableClientBasedCulture {
129                         get { return (bool) base [enableClientBasedCultureProp];}
130                         set { base[enableClientBasedCultureProp] = value; }
131                 }
132
133                 [ConfigurationProperty ("fileEncoding")]
134                 public Encoding FileEncoding {
135                         get { return GetEncoding (fileEncodingProp, ref cached_fileencoding); }
136                         set { base[fileEncodingProp] = value.EncodingName; }
137                 }
138
139                 [ConfigurationProperty ("requestEncoding", DefaultValue = "utf-8")]
140                 public Encoding RequestEncoding {
141                         get { return GetEncoding (requestEncodingProp, ref cached_requestencoding); }
142                         set { base[requestEncodingProp] = value.EncodingName; }
143                 }
144
145                 [ConfigurationProperty ("resourceProviderFactoryType", DefaultValue = "")]
146                 public string ResourceProviderFactoryType {
147                         get { return (string) base [resourceProviderFactoryTypeProp];}
148                         set { base[resourceProviderFactoryTypeProp] = value; }
149                 }
150
151                 [ConfigurationProperty ("responseEncoding", DefaultValue = "utf-8")]
152                 public Encoding ResponseEncoding {
153                         get { return GetEncoding (responseEncodingProp, ref cached_responseencoding); }
154                         set { base[responseEncodingProp] = value.EncodingName; }
155                 }
156
157                 [ConfigurationProperty ("responseHeaderEncoding", DefaultValue = "utf-8")]
158                 public Encoding ResponseHeaderEncoding {
159                         get { return GetEncoding (responseHeaderEncodingProp, ref cached_responseheaderencoding); }
160                         set { base[responseHeaderEncodingProp] = value.EncodingName; }
161                 }
162
163                 [ConfigurationProperty ("uiCulture", DefaultValue = "")]
164                 public string UICulture {
165                         get { return (string) base [uiCultureProp];}
166                         set { base[uiCultureProp] = value; }
167                 }
168
169                 protected override ConfigurationPropertyCollection Properties {
170                         get { return properties; }
171                 }
172
173 #region CompatabilityCode
174                 string cached_fileencoding;
175                 string cached_requestencoding;
176                 string cached_responseencoding;
177                 string cached_responseheaderencoding;
178                 Hashtable encodingHash;
179
180                 string cached_culture;
181                 CultureInfo cached_cultureinfo;
182
183                 string cached_uiculture;
184                 CultureInfo cached_uicultureinfo;
185
186                 static bool encoding_warning;
187                 static bool culture_warning;
188
189                 internal CultureInfo GetUICulture ()
190                 {
191                         if (cached_uiculture != UICulture) {
192                                 try {
193                                         cached_uicultureinfo = new CultureInfo (UICulture);
194                                 } catch {
195                                         CultureFailed ("UICulture", UICulture);
196                                         cached_uicultureinfo = new CultureInfo (0x007f); // Invariant
197                                 }
198                         }
199
200                         return cached_uicultureinfo;
201                 }
202
203                 internal CultureInfo GetCulture ()
204                 {
205                         if (cached_culture != Culture) {
206                                 try {
207                                         cached_cultureinfo = new CultureInfo (Culture);
208                                 } catch {
209                                         CultureFailed ("Culture", Culture);
210                                         cached_cultureinfo = new CultureInfo (0x007f); // Invariant
211                                 }
212                         }
213
214                         return cached_cultureinfo;
215                 }
216
217                 Encoding GetEncoding (ConfigurationProperty prop, ref string cached_encoding_name)
218                 {
219                         if (cached_encoding_name == null)
220                                 cached_encoding_name = "utf-8";
221
222                         Encoding encoding = (Encoding)encodingHash [prop];
223                         if (encoding == null || encoding.EncodingName != cached_encoding_name) {
224                                 try {
225                                         switch (cached_encoding_name.ToLower ()) {
226                                         case "utf-16le":
227                                         case "utf-16":
228                                         case "ucs-2":
229                                         case "unicode":
230                                         case "iso-10646-ucs-2":
231                                                 encoding = new UnicodeEncoding (false, true);
232                                                 break;
233                                         case "utf-16be":
234                                         case "unicodefffe":
235                                                 encoding = new UnicodeEncoding (true, true);
236                                                 break;
237                                         case "utf-8":
238                                         case "unicode-1-1-utf-8":
239                                         case "unicode-2-0-utf-8":
240                                         case "x-unicode-1-1-utf-8":
241                                         case "x-unicode-2-0-utf-8":
242                                                 encoding = Encoding.UTF8;
243                                                 break;
244                                         default:
245                                                 encoding = Encoding.GetEncoding (cached_encoding_name);
246                                                 break;
247                                         }
248                                 } catch {
249                                         EncodingFailed (prop.Name, cached_encoding_name);
250                                         encoding = new UTF8Encoding (false, false);
251                                 }
252                         }
253
254                         encodingHash[prop] = encoding;
255                         cached_encoding_name = encoding.EncodingName;
256
257                         return encoding;
258                 }
259
260                 static void EncodingFailed (string att, string enc)
261                 {
262                         if (encoding_warning)
263                                 return;
264
265                         encoding_warning = true;
266                         Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
267                                            "{0}=\"{1}\"\n", att, enc);
268                 }
269
270                 static void CultureFailed (string att, string cul)
271                 {
272                         if (culture_warning)
273                                 return;
274
275                         culture_warning = true;
276                         Console.WriteLine ("Culture {1} cannot be loaded. Perhaps your runtime \n" +
277                                            "don't have ICU support?\n{0}=\"{1}\"\n", att, cul);
278                 }
279
280 #endregion
281         }
282
283 }
284
285 #endif
286