2 // System.Web.Configuration.GlobalizationConfigurationHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Configuration;
32 using System.Globalization;
36 namespace System.Web.Configuration
38 class GlobalizationConfigurationHandler : IConfigurationSectionHandler
40 static bool encoding_warning;
41 static bool culture_warning;
43 public object Create (object parent, object configContext, XmlNode section)
45 GlobalizationConfiguration config = new GlobalizationConfiguration (parent);
47 if (section.HasChildNodes)
48 ThrowException ("No child nodes allowed here.", section);
50 string attvalue = AttValue ("requestEncoding", section, true);
52 config.RequestEncoding = GetEncoding (section, "requestEncoding", attvalue);
54 attvalue = AttValue ("responseEncoding", section, true);
56 config.ResponseEncoding = GetEncoding (section, "responseEncoding", attvalue);
58 attvalue = AttValue ("fileEncoding", section, true);
60 config.FileEncoding = GetEncoding (section, "fileEncoding", attvalue);
62 attvalue = AttValue ("culture", section, true);
64 config.Culture = GetCulture (section, "culture", attvalue);
66 attvalue = AttValue ("uiCulture", section, true);
68 config.UICulture = GetCulture (section, "uiCulture", attvalue);
70 if (section.Attributes == null || section.Attributes.Count != 0)
71 ThrowException ("Unknown attribute(s).", section);
76 static Encoding GetEncoding (XmlNode section, string att, string enc)
78 Encoding encoding = null;
80 switch (enc.ToLower ()) {
85 case "iso-10646-ucs-2":
86 encoding = new UnicodeEncoding (false, true);
90 encoding = new UnicodeEncoding (true, true);
93 case "unicode-1-1-utf-8":
94 case "unicode-2-0-utf-8":
95 case "x-unicode-1-1-utf-8":
96 case "x-unicode-2-0-utf-8":
97 encoding = new UTF8Encoding (false, false);
100 encoding = Encoding.GetEncoding (enc);
104 EncodingFailed (section, att, enc);
105 encoding = new UTF8Encoding (false, false);
111 static CultureInfo GetCulture (XmlNode section, string att, string cul)
113 CultureInfo culture = null;
115 culture = new CultureInfo (cul);
117 CultureFailed (section, att, cul);
118 culture = new CultureInfo (0x007f); // Invariant
124 static void EncodingFailed (XmlNode section, string att, string enc)
126 if (encoding_warning)
129 encoding_warning = true;
130 Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
131 "{0}=\"{1}\"\n", att, enc);
134 static void CultureFailed (XmlNode section, string att, string cul)
139 culture_warning = true;
140 Console.WriteLine ("Culture {1} cannot be loaded. Perhaps your runtime \n" +
141 "don't have ICU support?\n{0}=\"{1}\"\n", att, cul);
144 // A few methods to save some typing
145 static string AttValue (string name, XmlNode node, bool optional)
147 return HandlersUtil.ExtractAttributeValue (name, node, optional);
150 static void ThrowException (string message, XmlNode node)
152 HandlersUtil.ThrowException (message, node);