GlobalizationSection.cs 8.9 KB

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