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 ();
87 new CultureInfo (Culture);
90 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'culture' attribute");
94 new CultureInfo (UICulture);
97 throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'uiCulture' attribute");
101 protected override void PostDeserialize ()
103 base.PostDeserialize();
108 protected override void PreSerialize (XmlWriter writer)
110 base.PreSerialize(writer);
115 [ConfigurationProperty ("culture", DefaultValue = "")]
116 public string Culture {
117 get { return (string) base [cultureProp];}
118 set { base[cultureProp] = value; }
121 [ConfigurationProperty ("enableBestFitResponseEncoding", DefaultValue = "False")]
122 public bool EnableBestFitResponseEncoding {
123 get { return (bool) base [enableBestFitResponseEncodingProp];}
124 set { base[enableBestFitResponseEncodingProp] = value; }
127 [ConfigurationProperty ("enableClientBasedCulture", DefaultValue = "False")]
128 public bool EnableClientBasedCulture {
129 get { return (bool) base [enableClientBasedCultureProp];}
130 set { base[enableClientBasedCultureProp] = value; }
133 [ConfigurationProperty ("fileEncoding")]
134 public Encoding FileEncoding {
135 get { return GetEncoding (fileEncodingProp, ref cached_fileencoding); }
136 set { base[fileEncodingProp] = value.EncodingName; }
139 [ConfigurationProperty ("requestEncoding", DefaultValue = "utf-8")]
140 public Encoding RequestEncoding {
141 get { return GetEncoding (requestEncodingProp, ref cached_requestencoding); }
142 set { base[requestEncodingProp] = value.EncodingName; }
145 [ConfigurationProperty ("resourceProviderFactoryType", DefaultValue = "")]
146 public string ResourceProviderFactoryType {
147 get { return (string) base [resourceProviderFactoryTypeProp];}
148 set { base[resourceProviderFactoryTypeProp] = value; }
151 [ConfigurationProperty ("responseEncoding", DefaultValue = "utf-8")]
152 public Encoding ResponseEncoding {
153 get { return GetEncoding (responseEncodingProp, ref cached_responseencoding); }
154 set { base[responseEncodingProp] = value.EncodingName; }
157 [ConfigurationProperty ("responseHeaderEncoding", DefaultValue = "utf-8")]
158 public Encoding ResponseHeaderEncoding {
159 get { return GetEncoding (responseHeaderEncodingProp, ref cached_responseheaderencoding); }
160 set { base[responseHeaderEncodingProp] = value.EncodingName; }
163 [ConfigurationProperty ("uiCulture", DefaultValue = "")]
164 public string UICulture {
165 get { return (string) base [uiCultureProp];}
166 set { base[uiCultureProp] = value; }
169 protected override ConfigurationPropertyCollection Properties {
170 get { return properties; }
173 #region CompatabilityCode
174 string cached_fileencoding;
175 string cached_requestencoding;
176 string cached_responseencoding;
177 string cached_responseheaderencoding;
178 Hashtable encodingHash;
180 string cached_culture;
181 CultureInfo cached_cultureinfo;
183 string cached_uiculture;
184 CultureInfo cached_uicultureinfo;
186 static bool encoding_warning;
187 static bool culture_warning;
189 internal CultureInfo GetUICulture ()
191 if (cached_uiculture != UICulture) {
193 cached_uicultureinfo = new CultureInfo (UICulture);
195 CultureFailed ("UICulture", UICulture);
196 cached_uicultureinfo = new CultureInfo (0x007f); // Invariant
200 return cached_uicultureinfo;
203 internal CultureInfo GetCulture ()
205 if (cached_culture != Culture) {
207 cached_cultureinfo = new CultureInfo (Culture);
209 CultureFailed ("Culture", Culture);
210 cached_cultureinfo = new CultureInfo (0x007f); // Invariant
214 return cached_cultureinfo;
217 Encoding GetEncoding (ConfigurationProperty prop, ref string cached_encoding_name)
219 if (cached_encoding_name == null)
220 cached_encoding_name = "utf-8";
222 Encoding encoding = (Encoding)encodingHash [prop];
223 if (encoding == null || encoding.EncodingName != cached_encoding_name) {
225 switch (cached_encoding_name.ToLower ()) {
230 case "iso-10646-ucs-2":
231 encoding = new UnicodeEncoding (false, true);
235 encoding = new UnicodeEncoding (true, true);
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;
245 encoding = Encoding.GetEncoding (cached_encoding_name);
249 EncodingFailed (prop.Name, cached_encoding_name);
250 encoding = new UTF8Encoding (false, false);
254 encodingHash[prop] = encoding;
255 cached_encoding_name = encoding.EncodingName;
260 static void EncodingFailed (string att, string enc)
262 if (encoding_warning)
265 encoding_warning = true;
266 Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
267 "{0}=\"{1}\"\n", att, enc);
270 static void CultureFailed (string att, string cul)
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);