GlobalizationSection.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. [MonoTODO]
  76. protected override void PostDeserialize ()
  77. {
  78. base.PostDeserialize();
  79. }
  80. [MonoTODO]
  81. protected override void PreSerialize (XmlWriter writer)
  82. {
  83. base.PostDeserialize();
  84. }
  85. [ConfigurationProperty ("culture", DefaultValue = "")]
  86. public string Culture {
  87. get { return (string) base [cultureProp];}
  88. set { base[cultureProp] = value; }
  89. }
  90. [ConfigurationProperty ("enableBestFitResponseEncoding", DefaultValue = "False")]
  91. public bool EnableBestFitResponseEncoding {
  92. get { return (bool) base [enableBestFitResponseEncodingProp];}
  93. set { base[enableBestFitResponseEncodingProp] = value; }
  94. }
  95. [ConfigurationProperty ("enableClientBasedCulture", DefaultValue = "False")]
  96. public bool EnableClientBasedCulture {
  97. get { return (bool) base [enableClientBasedCultureProp];}
  98. set { base[enableClientBasedCultureProp] = value; }
  99. }
  100. [ConfigurationProperty ("fileEncoding")]
  101. public Encoding FileEncoding {
  102. get { return GetEncoding (fileEncodingProp, ref cached_fileencoding); }
  103. set { base[fileEncodingProp] = value.EncodingName; }
  104. }
  105. [ConfigurationProperty ("requestEncoding", DefaultValue = "utf-8")]
  106. public Encoding RequestEncoding {
  107. get { return GetEncoding (requestEncodingProp, ref cached_requestencoding); }
  108. set { base[requestEncodingProp] = value.EncodingName; }
  109. }
  110. [ConfigurationProperty ("resourceProviderFactoryType", DefaultValue = "")]
  111. public string ResourceProviderFactoryType {
  112. get { return (string) base [resourceProviderFactoryTypeProp];}
  113. set { base[resourceProviderFactoryTypeProp] = value; }
  114. }
  115. [ConfigurationProperty ("responseEncoding", DefaultValue = "utf-8")]
  116. public Encoding ResponseEncoding {
  117. get { return GetEncoding (responseEncodingProp, ref cached_responseencoding); }
  118. set { base[responseEncodingProp] = value.EncodingName; }
  119. }
  120. [ConfigurationProperty ("responseHeaderEncoding", DefaultValue = "utf-8")]
  121. public Encoding ResponseHeaderEncoding {
  122. get { return GetEncoding (responseHeaderEncodingProp, ref cached_responseheaderencoding); }
  123. set { base[responseHeaderEncodingProp] = value.EncodingName; }
  124. }
  125. [ConfigurationProperty ("uiCulture", DefaultValue = "")]
  126. public string UICulture {
  127. get { return (string) base [uiCultureProp];}
  128. set { base[uiCultureProp] = value; }
  129. }
  130. protected override ConfigurationPropertyCollection Properties {
  131. get { return properties; }
  132. }
  133. #region CompatabilityCode
  134. string cached_fileencoding;
  135. string cached_requestencoding;
  136. string cached_responseencoding;
  137. string cached_responseheaderencoding;
  138. Hashtable encodingHash;
  139. string cached_culture;
  140. CultureInfo cached_cultureinfo;
  141. string cached_uiculture;
  142. CultureInfo cached_uicultureinfo;
  143. static bool encoding_warning;
  144. static bool culture_warning;
  145. internal CultureInfo GetUICulture ()
  146. {
  147. if (cached_uiculture != UICulture) {
  148. try {
  149. cached_uicultureinfo = new CultureInfo (UICulture);
  150. } catch {
  151. CultureFailed ("UICulture", UICulture);
  152. cached_uicultureinfo = new CultureInfo (0x007f); // Invariant
  153. }
  154. }
  155. return cached_uicultureinfo;
  156. }
  157. internal CultureInfo GetCulture ()
  158. {
  159. if (cached_culture != Culture) {
  160. try {
  161. cached_cultureinfo = new CultureInfo (Culture);
  162. } catch {
  163. CultureFailed ("Culture", Culture);
  164. cached_cultureinfo = new CultureInfo (0x007f); // Invariant
  165. }
  166. }
  167. return cached_cultureinfo;
  168. }
  169. Encoding GetEncoding (ConfigurationProperty prop, ref string cached_encoding_name)
  170. {
  171. Encoding encoding = (Encoding)encodingHash [prop];
  172. if (encoding == null || encoding.EncodingName != cached_encoding_name) {
  173. try {
  174. switch (cached_encoding_name.ToLower ()) {
  175. case "utf-16le":
  176. case "utf-16":
  177. case "ucs-2":
  178. case "unicode":
  179. case "iso-10646-ucs-2":
  180. encoding = new UnicodeEncoding (false, true);
  181. break;
  182. case "utf-16be":
  183. case "unicodefffe":
  184. encoding = new UnicodeEncoding (true, true);
  185. break;
  186. case "utf-8":
  187. case "unicode-1-1-utf-8":
  188. case "unicode-2-0-utf-8":
  189. case "x-unicode-1-1-utf-8":
  190. case "x-unicode-2-0-utf-8":
  191. encoding = new UTF8Encoding (false, false);
  192. break;
  193. default:
  194. encoding = Encoding.GetEncoding (cached_encoding_name);
  195. break;
  196. }
  197. } catch {
  198. EncodingFailed (prop.Name, cached_encoding_name);
  199. encoding = new UTF8Encoding (false, false);
  200. }
  201. }
  202. encodingHash[prop] = encoding;
  203. cached_encoding_name = encoding.EncodingName;
  204. return encoding;
  205. }
  206. static void EncodingFailed (string att, string enc)
  207. {
  208. if (encoding_warning)
  209. return;
  210. encoding_warning = true;
  211. Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
  212. "{0}=\"{1}\"\n", att, enc);
  213. }
  214. static void CultureFailed (string att, string cul)
  215. {
  216. if (culture_warning)
  217. return;
  218. culture_warning = true;
  219. Console.WriteLine ("Culture {1} cannot be loaded. Perhaps your runtime \n" +
  220. "don't have ICU support?\n{0}=\"{1}\"\n", att, cul);
  221. }
  222. #endregion
  223. }
  224. }
  225. #endif