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;
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 ();
85 protected override void PostDeserialize ()
87 base.PostDeserialize();
90 protected override void PreSerialize (XmlWriter writer)
92 base.PreSerialize(writer);
98 culture = new CultureInfo.GetCultureInfo (Culture);
101 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'culture' attribute");
105 culture = new CultureInfo.GetCultureInfo (UICulture);
108 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'uiCulture' attribute");
112 [ConfigurationProperty ("culture", DefaultValue = "")]
113 public string Culture {
114 get { return (string) base [cultureProp];}
115 set { base[cultureProp] = value; }
118 [ConfigurationProperty ("enableBestFitResponseEncoding", DefaultValue = "False")]
119 public bool EnableBestFitResponseEncoding {
120 get { return (bool) base [enableBestFitResponseEncodingProp];}
121 set { base[enableBestFitResponseEncodingProp] = value; }
124 [ConfigurationProperty ("enableClientBasedCulture", DefaultValue = "False")]
125 public bool EnableClientBasedCulture {
126 get { return (bool) base [enableClientBasedCultureProp];}
127 set { base[enableClientBasedCultureProp] = value; }
130 [ConfigurationProperty ("fileEncoding")]
131 public Encoding FileEncoding {
132 get { return GetEncoding (fileEncodingProp, ref cached_fileencoding); }
133 set { base[fileEncodingProp] = value.EncodingName; }
136 [ConfigurationProperty ("requestEncoding", DefaultValue = "utf-8")]
137 public Encoding RequestEncoding {
138 get { return GetEncoding (requestEncodingProp, ref cached_requestencoding); }
139 set { base[requestEncodingProp] = value.EncodingName; }
142 [ConfigurationProperty ("resourceProviderFactoryType", DefaultValue = "")]
143 public string ResourceProviderFactoryType {
144 get { return (string) base [resourceProviderFactoryTypeProp];}
145 set { base[resourceProviderFactoryTypeProp] = value; }
148 [ConfigurationProperty ("responseEncoding", DefaultValue = "utf-8")]
149 public Encoding ResponseEncoding {
150 get { return GetEncoding (responseEncodingProp, ref cached_responseencoding); }
151 set { base[responseEncodingProp] = value.EncodingName; }
154 [ConfigurationProperty ("responseHeaderEncoding", DefaultValue = "utf-8")]
155 public Encoding ResponseHeaderEncoding {
156 get { return GetEncoding (responseHeaderEncodingProp, ref cached_responseheaderencoding); }
157 set { base[responseHeaderEncodingProp] = value.EncodingName; }
160 [ConfigurationProperty ("uiCulture", DefaultValue = "")]
161 public string UICulture {
162 get { return (string) base [uiCultureProp];}
163 set { base[uiCultureProp] = value; }
166 protected override ConfigurationPropertyCollection Properties {
167 get { return properties; }
170 #region CompatabilityCode
171 string cached_fileencoding;
172 string cached_requestencoding;
173 string cached_responseencoding;
174 string cached_responseheaderencoding;
175 Hashtable encodingHash;
177 string cached_culture;
178 CultureInfo cached_cultureinfo;
180 string cached_uiculture;
181 CultureInfo cached_uicultureinfo;
183 static bool encoding_warning;
184 static bool culture_warning;
186 internal CultureInfo GetUICulture ()
188 if (cached_uiculture != UICulture) {
190 cached_uicultureinfo = new CultureInfo (UICulture);
192 CultureFailed ("UICulture", UICulture);
193 cached_uicultureinfo = new CultureInfo (0x007f); // Invariant
197 return cached_uicultureinfo;
200 internal CultureInfo GetCulture ()
202 if (cached_culture != Culture) {
204 cached_cultureinfo = new CultureInfo (Culture);
206 CultureFailed ("Culture", Culture);
207 cached_cultureinfo = new CultureInfo (0x007f); // Invariant
211 return cached_cultureinfo;
214 Encoding GetEncoding (ConfigurationProperty prop, ref string cached_encoding_name)
216 if (cached_encoding_name == null)
217 cached_encoding_name = "utf-8";
219 Encoding encoding = (Encoding)encodingHash [prop];
220 if (encoding == null || encoding.EncodingName != cached_encoding_name) {
222 switch (cached_encoding_name.ToLower ()) {
227 case "iso-10646-ucs-2":
228 encoding = new UnicodeEncoding (false, true);
232 encoding = new UnicodeEncoding (true, true);
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);
242 encoding = Encoding.GetEncoding (cached_encoding_name);
246 EncodingFailed (prop.Name, cached_encoding_name);
247 encoding = new UTF8Encoding (false, false);
251 encodingHash[prop] = encoding;
252 cached_encoding_name = encoding.EncodingName;
257 static void EncodingFailed (string att, string enc)
259 if (encoding_warning)
262 encoding_warning = true;
263 Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
264 "{0}=\"{1}\"\n", att, enc);
267 static void CultureFailed (string att, string cul)
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);