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;
41 namespace System.Web.Configuration {
43 public sealed class GlobalizationSection : ConfigurationSection
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;
56 static GlobalizationSection ()
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 ();
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);
80 public GlobalizationSection ()
82 encodingHash = new Hashtable ();
89 GetSanitizedCulture (Culture, ref fake);
92 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'culture' attribute");
96 GetSanitizedCulture (UICulture, ref fake);
99 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'uiCulture' attribute");
103 protected override void PostDeserialize ()
105 base.PostDeserialize();
110 protected override void PreSerialize (XmlWriter writer)
112 base.PreSerialize(writer);
117 [ConfigurationProperty ("culture", DefaultValue = "")]
118 public string Culture {
119 get { return (string) base [cultureProp];}
120 set { base[cultureProp] = value; }
123 [ConfigurationProperty ("enableBestFitResponseEncoding", DefaultValue = "False")]
124 public bool EnableBestFitResponseEncoding {
125 get { return (bool) base [enableBestFitResponseEncodingProp];}
126 set { base[enableBestFitResponseEncodingProp] = value; }
129 [ConfigurationProperty ("enableClientBasedCulture", DefaultValue = "False")]
130 public bool EnableClientBasedCulture {
131 get { return (bool) base [enableClientBasedCultureProp];}
132 set { base[enableClientBasedCultureProp] = value; }
135 [ConfigurationProperty ("fileEncoding")]
136 public Encoding FileEncoding {
137 get { return GetEncoding (fileEncodingProp, ref cached_fileencoding); }
138 set { base[fileEncodingProp] = value.WebName; }
141 [ConfigurationProperty ("requestEncoding", DefaultValue = "utf-8")]
142 public Encoding RequestEncoding {
143 get { return GetEncoding (requestEncodingProp, ref cached_requestencoding); }
144 set { base[requestEncodingProp] = value.WebName; }
147 [ConfigurationProperty ("resourceProviderFactoryType", DefaultValue = "")]
148 public string ResourceProviderFactoryType {
149 get { return (string) base [resourceProviderFactoryTypeProp];}
150 set { base[resourceProviderFactoryTypeProp] = value; }
153 [ConfigurationProperty ("responseEncoding", DefaultValue = "utf-8")]
154 public Encoding ResponseEncoding {
155 get { return GetEncoding (responseEncodingProp, ref cached_responseencoding); }
156 set { base[responseEncodingProp] = value.WebName; }
159 [ConfigurationProperty ("responseHeaderEncoding", DefaultValue = "utf-8")]
160 public Encoding ResponseHeaderEncoding {
161 get { return GetEncoding (responseHeaderEncodingProp, ref cached_responseheaderencoding); }
162 set { base[responseHeaderEncodingProp] = value.WebName; }
165 [ConfigurationProperty ("uiCulture", DefaultValue = "")]
166 public string UICulture {
167 get { return (string) base [uiCultureProp];}
168 set { base[uiCultureProp] = value; }
171 protected override ConfigurationPropertyCollection Properties {
172 get { return properties; }
175 #region CompatabilityCode
176 string cached_fileencoding;
177 string cached_requestencoding;
178 string cached_responseencoding;
179 string cached_responseheaderencoding;
180 Hashtable encodingHash;
182 string cached_culture;
183 CultureInfo cached_cultureinfo;
185 string cached_uiculture;
186 CultureInfo cached_uicultureinfo;
188 static bool encoding_warning;
189 static bool culture_warning;
194 internal bool IsAutoCulture {
195 get { return autoCulture; }
198 internal bool IsAutoUICulture {
199 get { return autoUICulture; }
202 CultureInfo GetSanitizedCulture (string culture, ref bool auto)
206 throw new ArgumentNullException ("culture");
207 if (culture.Length <= 3)
208 return new CultureInfo (culture);
209 if (culture.StartsWith ("auto")) {
211 if (culture.Length > 5 && culture[4] == ':')
212 return new CultureInfo (culture.Substring (5));
213 return Helpers.InvariantCulture;// (0x007f);
216 return new CultureInfo (culture);
219 internal CultureInfo GetUICulture ()
221 string uiculture = UICulture;
222 if (cached_uiculture != uiculture) {
224 cached_uicultureinfo = GetSanitizedCulture (uiculture, ref autoUICulture);
225 cached_uiculture = uiculture;
227 CultureFailed ("UICulture", uiculture);
228 cached_uicultureinfo = new CultureInfo (0x007f); // Invariant
229 cached_uiculture = null;
233 return cached_uicultureinfo;
236 internal CultureInfo GetCulture ()
238 string culture = Culture;
239 if (cached_culture != culture) {
241 cached_cultureinfo = GetSanitizedCulture (culture, ref autoCulture);
242 cached_culture = culture;
244 CultureFailed ("Culture", culture);
245 cached_cultureinfo = new CultureInfo (0x007f); // Invariant
246 cached_culture = null;
250 return cached_cultureinfo;
253 Encoding GetEncoding (ConfigurationProperty prop, ref string cached_encoding_name)
255 string enc = (string) base [prop];
256 if (cached_encoding_name == null)
257 cached_encoding_name = ((enc == null) ? "utf-8" : enc);
259 Encoding encoding = (Encoding)encodingHash [prop];
260 if (encoding == null || encoding.WebName != cached_encoding_name) {
262 switch (cached_encoding_name.ToLower (Helpers.InvariantCulture)) {
267 case "iso-10646-ucs-2":
268 encoding = new UnicodeEncoding (false, true);
272 encoding = new UnicodeEncoding (true, true);
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;
282 encoding = Encoding.GetEncoding (cached_encoding_name);
286 EncodingFailed (prop.Name, cached_encoding_name);
287 encoding = new UTF8Encoding (false, false);
291 encodingHash[prop] = encoding;
292 cached_encoding_name = encoding.WebName;
297 static void EncodingFailed (string att, string enc)
299 if (encoding_warning)
302 encoding_warning = true;
303 Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
304 "{0}=\"{1}\"\n", att, enc);
307 static void CultureFailed (string att, string cul)
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);