GlobalizationSection.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // System.Web.Configuration.GlobalizationSection
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Configuration;
  32. using System.Globalization;
  33. using System.Text;
  34. using System.Xml;
  35. #if NET_2_0
  36. namespace System.Web.Configuration {
  37. public sealed class GlobalizationSection : ConfigurationSection
  38. {
  39. static ConfigurationProperty cultureProp;
  40. static ConfigurationProperty enableBestFitResponseEncodingProp;
  41. static ConfigurationProperty enableClientBasedCultureProp;
  42. static ConfigurationProperty fileEncodingProp;
  43. static ConfigurationProperty requestEncodingProp;
  44. static ConfigurationProperty resourceProviderFactoryTypeProp;
  45. static ConfigurationProperty responseEncodingProp;
  46. static ConfigurationProperty responseHeaderEncodingProp;
  47. static ConfigurationProperty uiCultureProp;
  48. static ConfigurationPropertyCollection properties;
  49. static GlobalizationSection ()
  50. {
  51. cultureProp = new ConfigurationProperty ("culture", typeof (string), "");
  52. enableBestFitResponseEncodingProp = new ConfigurationProperty ("enableBestFitResponseEncoding", typeof (bool), false);
  53. enableClientBasedCultureProp = new ConfigurationProperty ("enableClientBasedCulture", typeof (bool), false);
  54. fileEncodingProp = new ConfigurationProperty ("fileEncoding", typeof (string));
  55. requestEncodingProp = new ConfigurationProperty ("requestEncoding", typeof (string), "utf-8");
  56. resourceProviderFactoryTypeProp = new ConfigurationProperty ("resourceProviderFactoryType", typeof (string), "");
  57. responseEncodingProp = new ConfigurationProperty ("responseEncoding", typeof (string), "utf-8");
  58. responseHeaderEncodingProp = new ConfigurationProperty ("responseHeaderEncoding", typeof (string), "utf-8");
  59. uiCultureProp = new ConfigurationProperty ("uiCulture", typeof (string), "");
  60. properties = new ConfigurationPropertyCollection ();
  61. properties.Add (cultureProp);
  62. properties.Add (enableBestFitResponseEncodingProp);
  63. properties.Add (enableClientBasedCultureProp);
  64. properties.Add (fileEncodingProp);
  65. properties.Add (requestEncodingProp);
  66. properties.Add (resourceProviderFactoryTypeProp);
  67. properties.Add (responseEncodingProp);
  68. properties.Add (responseHeaderEncodingProp);
  69. properties.Add (uiCultureProp);
  70. }
  71. public GlobalizationSection ()
  72. {
  73. encodingHash = new Hashtable ();
  74. }
  75. void VerifyData ()
  76. {
  77. try {
  78. new CultureInfo (Culture);
  79. }
  80. catch {
  81. throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'culture' attribute");
  82. }
  83. try {
  84. new CultureInfo (UICulture);
  85. }
  86. catch {
  87. throw new ConfigurationErrorsException ("the <globalization> tag contains an invalid value for the 'uiCulture' attribute");
  88. }
  89. }
  90. protected override void PostDeserialize ()
  91. {
  92. base.PostDeserialize();
  93. VerifyData ();
  94. }
  95. protected override void PreSerialize (XmlWriter writer)
  96. {
  97. base.PreSerialize(writer);
  98. VerifyData ();
  99. }
  100. [ConfigurationProperty ("culture", DefaultValue = "")]
  101. public string Culture {
  102. get { return (string) base [cultureProp];}
  103. set { base[cultureProp] = value; }
  104. }
  105. [ConfigurationProperty ("enableBestFitResponseEncoding", DefaultValue = "False")]
  106. public bool EnableBestFitResponseEncoding {
  107. get { return (bool) base [enableBestFitResponseEncodingProp];}
  108. set { base[enableBestFitResponseEncodingProp] = value; }
  109. }
  110. [ConfigurationProperty ("enableClientBasedCulture", DefaultValue = "False")]
  111. public bool EnableClientBasedCulture {
  112. get { return (bool) base [enableClientBasedCultureProp];}
  113. set { base[enableClientBasedCultureProp] = value; }
  114. }
  115. [ConfigurationProperty ("fileEncoding")]
  116. public Encoding FileEncoding {
  117. get { return GetEncoding (fileEncodingProp, ref cached_fileencoding); }
  118. set { base[fileEncodingProp] = value.EncodingName; }
  119. }
  120. [ConfigurationProperty ("requestEncoding", DefaultValue = "utf-8")]
  121. public Encoding RequestEncoding {
  122. get { return GetEncoding (requestEncodingProp, ref cached_requestencoding); }
  123. set { base[requestEncodingProp] = value.EncodingName; }
  124. }
  125. [ConfigurationProperty ("resourceProviderFactoryType", DefaultValue = "")]
  126. public string ResourceProviderFactoryType {
  127. get { return (string) base [resourceProviderFactoryTypeProp];}
  128. set { base[resourceProviderFactoryTypeProp] = value; }
  129. }
  130. [ConfigurationProperty ("responseEncoding", DefaultValue = "utf-8")]
  131. public Encoding ResponseEncoding {
  132. get { return GetEncoding (responseEncodingProp, ref cached_responseencoding); }
  133. set { base[responseEncodingProp] = value.EncodingName; }
  134. }
  135. [ConfigurationProperty ("responseHeaderEncoding", DefaultValue = "utf-8")]
  136. public Encoding ResponseHeaderEncoding {
  137. get { return GetEncoding (responseHeaderEncodingProp, ref cached_responseheaderencoding); }
  138. set { base[responseHeaderEncodingProp] = value.EncodingName; }
  139. }
  140. [ConfigurationProperty ("uiCulture", DefaultValue = "")]
  141. public string UICulture {
  142. get { return (string) base [uiCultureProp];}
  143. set { base[uiCultureProp] = value; }
  144. }
  145. protected override ConfigurationPropertyCollection Properties {
  146. get { return properties; }
  147. }
  148. #region CompatabilityCode
  149. string cached_fileencoding;
  150. string cached_requestencoding;
  151. string cached_responseencoding;
  152. string cached_responseheaderencoding;
  153. Hashtable encodingHash;
  154. string cached_culture;
  155. CultureInfo cached_cultureinfo;
  156. string cached_uiculture;
  157. CultureInfo cached_uicultureinfo;
  158. static bool encoding_warning;
  159. static bool culture_warning;
  160. internal CultureInfo GetUICulture ()
  161. {
  162. if (cached_uiculture != UICulture) {
  163. try {
  164. cached_uicultureinfo = new CultureInfo (UICulture);
  165. } catch {
  166. CultureFailed ("UICulture", UICulture);
  167. cached_uicultureinfo = new CultureInfo (0x007f); // Invariant
  168. }
  169. }
  170. return cached_uicultureinfo;
  171. }
  172. internal CultureInfo GetCulture ()
  173. {
  174. if (cached_culture != Culture) {
  175. try {
  176. cached_cultureinfo = new CultureInfo (Culture);
  177. } catch {
  178. CultureFailed ("Culture", Culture);
  179. cached_cultureinfo = new CultureInfo (0x007f); // Invariant
  180. }
  181. }
  182. return cached_cultureinfo;
  183. }
  184. Encoding GetEncoding (ConfigurationProperty prop, ref string cached_encoding_name)
  185. {
  186. if (cached_encoding_name == null)
  187. cached_encoding_name = "utf-8";
  188. Encoding encoding = (Encoding)encodingHash [prop];
  189. if (encoding == null || encoding.EncodingName != cached_encoding_name) {
  190. try {
  191. switch (cached_encoding_name.ToLower ()) {
  192. case "utf-16le":
  193. case "utf-16":
  194. case "ucs-2":
  195. case "unicode":
  196. case "iso-10646-ucs-2":
  197. encoding = new UnicodeEncoding (false, true);
  198. break;
  199. case "utf-16be":
  200. case "unicodefffe":
  201. encoding = new UnicodeEncoding (true, true);
  202. break;
  203. case "utf-8":
  204. case "unicode-1-1-utf-8":
  205. case "unicode-2-0-utf-8":
  206. case "x-unicode-1-1-utf-8":
  207. case "x-unicode-2-0-utf-8":
  208. encoding = Encoding.UTF8;
  209. break;
  210. default:
  211. encoding = Encoding.GetEncoding (cached_encoding_name);
  212. break;
  213. }
  214. } catch {
  215. EncodingFailed (prop.Name, cached_encoding_name);
  216. encoding = new UTF8Encoding (false, false);
  217. }
  218. }
  219. encodingHash[prop] = encoding;
  220. cached_encoding_name = encoding.EncodingName;
  221. return encoding;
  222. }
  223. static void EncodingFailed (string att, string enc)
  224. {
  225. if (encoding_warning)
  226. return;
  227. encoding_warning = true;
  228. Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
  229. "{0}=\"{1}\"\n", att, enc);
  230. }
  231. static void CultureFailed (string att, string cul)
  232. {
  233. if (culture_warning)
  234. return;
  235. culture_warning = true;
  236. Console.WriteLine ("Culture {1} cannot be loaded. Perhaps your runtime \n" +
  237. "don't have ICU support?\n{0}=\"{1}\"\n", att, cul);
  238. }
  239. #endregion
  240. }
  241. }
  242. #endif