2 // System.Web.Configuration.GlobalizationSection
5 // Chris Toshok (toshok@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Configuration;
34 using System.Globalization;
37 using System.Web.Util;
40 namespace System.Web.Configuration {
42 public sealed class GlobalizationSection : ConfigurationSection
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;
55 static GlobalizationSection ()
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 ();
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);
79 public GlobalizationSection ()
81 encodingHash = new Hashtable ();
88 GetSanitizedCulture (Culture, ref fake);
91 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'culture' attribute");
95 GetSanitizedCulture (UICulture, ref fake);
98 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'uiCulture' attribute");
102 protected override void PostDeserialize ()
104 base.PostDeserialize();
109 protected override void PreSerialize (XmlWriter writer)
111 base.PreSerialize(writer);
116 [ConfigurationProperty ("culture", DefaultValue = "")]
117 public string Culture {
118 get { return (string) base [cultureProp];}
119 set { base[cultureProp] = value; }
122 [ConfigurationProperty ("enableBestFitResponseEncoding", DefaultValue = "False")]
123 public bool EnableBestFitResponseEncoding {
124 get { return (bool) base [enableBestFitResponseEncodingProp];}
125 set { base[enableBestFitResponseEncodingProp] = value; }
128 [ConfigurationProperty ("enableClientBasedCulture", DefaultValue = "False")]
129 public bool EnableClientBasedCulture {
130 get { return (bool) base [enableClientBasedCultureProp];}
131 set { base[enableClientBasedCultureProp] = value; }
134 [ConfigurationProperty ("fileEncoding")]
135 public Encoding FileEncoding {
136 get { return GetEncoding (fileEncodingProp, ref cached_fileencoding); }
137 set { base[fileEncodingProp] = value.WebName; }
140 [ConfigurationProperty ("requestEncoding", DefaultValue = "utf-8")]
141 public Encoding RequestEncoding {
142 get { return GetEncoding (requestEncodingProp, ref cached_requestencoding); }
143 set { base[requestEncodingProp] = value.WebName; }
146 [ConfigurationProperty ("resourceProviderFactoryType", DefaultValue = "")]
147 public string ResourceProviderFactoryType {
148 get { return (string) base [resourceProviderFactoryTypeProp];}
149 set { base[resourceProviderFactoryTypeProp] = value; }
152 [ConfigurationProperty ("responseEncoding", DefaultValue = "utf-8")]
153 public Encoding ResponseEncoding {
154 get { return GetEncoding (responseEncodingProp, ref cached_responseencoding); }
155 set { base[responseEncodingProp] = value.WebName; }
158 [ConfigurationProperty ("responseHeaderEncoding", DefaultValue = "utf-8")]
159 public Encoding ResponseHeaderEncoding {
160 get { return GetEncoding (responseHeaderEncodingProp, ref cached_responseheaderencoding); }
161 set { base[responseHeaderEncodingProp] = value.WebName; }
164 [ConfigurationProperty ("uiCulture", DefaultValue = "")]
165 public string UICulture {
166 get { return (string) base [uiCultureProp];}
167 set { base[uiCultureProp] = value; }
170 protected internal override ConfigurationPropertyCollection Properties {
171 get { return properties; }
174 #region CompatabilityCode
175 string cached_fileencoding;
176 string cached_requestencoding;
177 string cached_responseencoding;
178 string cached_responseheaderencoding;
179 Hashtable encodingHash;
181 string cached_culture;
182 CultureInfo cached_cultureinfo;
184 string cached_uiculture;
185 CultureInfo cached_uicultureinfo;
187 static bool encoding_warning;
188 static bool culture_warning;
193 internal bool IsAutoCulture {
194 get { return autoCulture; }
197 internal bool IsAutoUICulture {
198 get { return autoUICulture; }
201 CultureInfo GetSanitizedCulture (string culture, ref bool auto)
205 throw new ArgumentNullException ("culture");
206 if (culture.Length <= 3)
207 return new CultureInfo (culture);
208 if (culture.StartsWith ("auto")) {
210 if (culture.Length > 5 && culture[4] == ':')
211 return new CultureInfo (culture.Substring (5));
212 return Helpers.InvariantCulture;// (0x007f);
215 return new CultureInfo (culture);
218 internal CultureInfo GetUICulture ()
220 string uiculture = UICulture;
221 if (cached_uiculture != uiculture) {
223 cached_uicultureinfo = GetSanitizedCulture (uiculture, ref autoUICulture);
224 cached_uiculture = uiculture;
226 CultureFailed ("UICulture", uiculture);
227 cached_uicultureinfo = new CultureInfo (0x007f); // Invariant
228 cached_uiculture = null;
232 return cached_uicultureinfo;
235 internal CultureInfo GetCulture ()
237 string culture = Culture;
238 if (cached_culture != culture) {
240 cached_cultureinfo = GetSanitizedCulture (culture, ref autoCulture);
241 cached_culture = culture;
243 CultureFailed ("Culture", culture);
244 cached_cultureinfo = new CultureInfo (0x007f); // Invariant
245 cached_culture = null;
249 return cached_cultureinfo;
252 Encoding GetEncoding (ConfigurationProperty prop, ref string cached_encoding_name)
254 string enc = (string) base [prop];
255 if (cached_encoding_name == null)
256 cached_encoding_name = ((enc == null) ? "utf-8" : enc);
258 Encoding encoding = (Encoding)encodingHash [prop];
259 if (encoding == null || encoding.WebName != cached_encoding_name) {
261 switch (cached_encoding_name.ToLower (Helpers.InvariantCulture)) {
266 case "iso-10646-ucs-2":
267 encoding = new UnicodeEncoding (false, true);
271 encoding = new UnicodeEncoding (true, true);
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;
281 encoding = Encoding.GetEncoding (cached_encoding_name);
285 EncodingFailed (prop.Name, cached_encoding_name);
286 encoding = new UTF8Encoding (false, false);
290 encodingHash[prop] = encoding;
291 cached_encoding_name = encoding.WebName;
296 static void EncodingFailed (string att, string enc)
298 if (encoding_warning)
301 encoding_warning = true;
302 Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
303 "{0}=\"{1}\"\n", att, enc);
306 static void CultureFailed (string att, string cul)
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);