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