2
0

GlobalizationConfigurationHandler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 = Encoding.Default;
  47. else
  48. config.RequestEncoding = GetEncoding (section, "requestEncoding", attvalue);
  49. attvalue = AttValue ("responseEncoding", section, true);
  50. if (attvalue == null)
  51. config.ResponseEncoding = Encoding.Default;
  52. else
  53. config.ResponseEncoding = GetEncoding (section, "responseEncoding", attvalue);
  54. attvalue = AttValue ("fileEncoding", section, true);
  55. if (attvalue == null)
  56. config.FileEncoding = Encoding.Default;
  57. else
  58. config.FileEncoding = GetEncoding (section, "fileEncoding", attvalue);
  59. attvalue = AttValue ("culture", section, true);
  60. if (attvalue != null)
  61. config.Culture = GetCulture (section, "culture", attvalue);
  62. attvalue = AttValue ("uiculture", section, true);
  63. if (attvalue != null)
  64. config.UICulture = GetCulture (section, "uiculture", attvalue);
  65. if (section.Attributes == null || section.Attributes.Count != 0)
  66. ThrowException ("Unknown attribute(s).", section);
  67. return config;
  68. }
  69. static Encoding GetEncoding (XmlNode section, string att, string enc)
  70. {
  71. Encoding encoding = null;
  72. try {
  73. switch (enc.ToLower ()) {
  74. case "utf-16le":
  75. case "utf-16":
  76. case "ucs-2":
  77. case "unicode":
  78. case "iso-10646-ucs-2":
  79. encoding = new UnicodeEncoding (false, true);
  80. break;
  81. case "utf-16be":
  82. case "unicodefffe":
  83. encoding = new UnicodeEncoding (true, true);
  84. break;
  85. case "utf-8":
  86. case "unicode-1-1-utf-8":
  87. case "unicode-2-0-utf-8":
  88. case "x-unicode-1-1-utf-8":
  89. case "x-unicode-2-0-utf-8":
  90. encoding = new UTF8Encoding (false, false);
  91. break;
  92. default:
  93. encoding = Encoding.GetEncoding (enc);
  94. break;
  95. }
  96. } catch {
  97. EncodingFailed (section, att, enc);
  98. encoding = new UTF8Encoding (false, false);
  99. }
  100. return encoding;
  101. }
  102. static CultureInfo GetCulture (XmlNode section, string att, string cul)
  103. {
  104. CultureInfo culture = null;
  105. try {
  106. culture = new CultureInfo (cul);
  107. } catch {
  108. CultureFailed (section, att, cul);
  109. culture = new CultureInfo (0x007f); // Invariant
  110. }
  111. return culture;
  112. }
  113. static void EncodingFailed (XmlNode section, string att, string enc)
  114. {
  115. if (encoding_warning)
  116. return;
  117. encoding_warning = true;
  118. Console.WriteLine ("Encoding {1} cannot be loaded. Perhaps your runtime \n" +
  119. "don't have ICU support?\n{0}=\"{1}\"\n", att, enc);
  120. }
  121. static void CultureFailed (XmlNode section, string att, string cul)
  122. {
  123. if (culture_warning)
  124. return;
  125. culture_warning = true;
  126. Console.WriteLine ("Culture {1} cannot be loaded. Perhaps your runtime \n" +
  127. "don't have ICU support?\n{0}=\"{1}\"\n", att, cul);
  128. }
  129. // A few methods to save some typing
  130. static string AttValue (string name, XmlNode node, bool optional)
  131. {
  132. return HandlersUtil.ExtractAttributeValue (name, node, optional);
  133. }
  134. static string AttValue (string name, XmlNode node)
  135. {
  136. return HandlersUtil.ExtractAttributeValue (name, node, true);
  137. }
  138. static void ThrowException (string message, XmlNode node)
  139. {
  140. HandlersUtil.ThrowException (message, node);
  141. }
  142. //
  143. }
  144. }