GlobalizationConfigurationHandler.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // System.Web.Configuration.GlobalizationConfigurationHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.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.Configuration;
  30. using System.Globalization;
  31. using System.Text;
  32. using System.Xml;
  33. namespace System.Web.Configuration
  34. {
  35. class GlobalizationConfigurationHandler : IConfigurationSectionHandler
  36. {
  37. static bool encoding_warning;
  38. static bool culture_warning;
  39. public object Create (object parent, object configContext, XmlNode section)
  40. {
  41. GlobalizationConfiguration config = new GlobalizationConfiguration (parent);
  42. if (section.HasChildNodes)
  43. ThrowException ("No child nodes allowed here.", section);
  44. string attvalue = AttValue ("requestEncoding", section, true);
  45. if (attvalue != null)
  46. config.RequestEncoding = GetEncoding (section, "requestEncoding", attvalue);
  47. attvalue = AttValue ("responseEncoding", section, true);
  48. if (attvalue != null)
  49. config.ResponseEncoding = GetEncoding (section, "responseEncoding", attvalue);
  50. attvalue = AttValue ("fileEncoding", section, true);
  51. if (attvalue != null)
  52. config.FileEncoding = GetEncoding (section, "fileEncoding", attvalue);
  53. attvalue = AttValue ("culture", section, true);
  54. if (attvalue != null)
  55. config.Culture = GetCulture (section, "culture", attvalue);
  56. attvalue = AttValue ("uiCulture", section, true);
  57. if (attvalue != null)
  58. config.UICulture = GetCulture (section, "uiCulture", attvalue);
  59. if (section.Attributes == null || section.Attributes.Count != 0)
  60. ThrowException ("Unknown attribute(s).", section);
  61. return config;
  62. }
  63. static Encoding GetEncoding (XmlNode section, string att, string enc)
  64. {
  65. Encoding encoding = null;
  66. try {
  67. switch (enc.ToLower ()) {
  68. case "utf-16le":
  69. case "utf-16":
  70. case "ucs-2":
  71. case "unicode":
  72. case "iso-10646-ucs-2":
  73. encoding = new UnicodeEncoding (false, true);
  74. break;
  75. case "utf-16be":
  76. case "unicodefffe":
  77. encoding = new UnicodeEncoding (true, true);
  78. break;
  79. case "utf-8":
  80. case "unicode-1-1-utf-8":
  81. case "unicode-2-0-utf-8":
  82. case "x-unicode-1-1-utf-8":
  83. case "x-unicode-2-0-utf-8":
  84. encoding = new UTF8Encoding (false, false);
  85. break;
  86. default:
  87. encoding = Encoding.GetEncoding (enc);
  88. break;
  89. }
  90. } catch {
  91. EncodingFailed (section, att, enc);
  92. encoding = new UTF8Encoding (false, false);
  93. }
  94. return encoding;
  95. }
  96. static CultureInfo GetCulture (XmlNode section, string att, string cul)
  97. {
  98. CultureInfo culture = null;
  99. try {
  100. culture = new CultureInfo (cul);
  101. } catch {
  102. CultureFailed (section, att, cul);
  103. culture = new CultureInfo (0x007f); // Invariant
  104. }
  105. return culture;
  106. }
  107. static void EncodingFailed (XmlNode section, string att, string enc)
  108. {
  109. if (encoding_warning)
  110. return;
  111. encoding_warning = true;
  112. Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
  113. "{0}=\"{1}\"\n", att, enc);
  114. }
  115. static void CultureFailed (XmlNode section, string att, string cul)
  116. {
  117. if (culture_warning)
  118. return;
  119. culture_warning = true;
  120. Console.WriteLine ("Culture {1} cannot be loaded. Perhaps your runtime \n" +
  121. "don't have ICU support?\n{0}=\"{1}\"\n", att, cul);
  122. }
  123. // A few methods to save some typing
  124. static string AttValue (string name, XmlNode node, bool optional)
  125. {
  126. return HandlersUtil.ExtractAttributeValue (name, node, optional);
  127. }
  128. static void ThrowException (string message, XmlNode node)
  129. {
  130. HandlersUtil.ThrowException (message, node);
  131. }
  132. //
  133. }
  134. }