merge -r 53370:58178
[mono.git] / mcs / class / System.Web / System.Web.Configuration / GlobalizationConfigurationHandler.cs
1 //
2 // System.Web.Configuration.GlobalizationConfigurationHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Configuration;
32 using System.Globalization;
33 using System.Text;
34 using System.Xml;
35
36 namespace System.Web.Configuration
37 {
38         class GlobalizationConfigurationHandler : IConfigurationSectionHandler
39         {
40                 static bool encoding_warning;
41                 static bool culture_warning;
42
43                 public object Create (object parent, object configContext, XmlNode section)
44                 {
45                         GlobalizationConfiguration config = new GlobalizationConfiguration (parent);
46
47                         if (section.HasChildNodes)
48                                 ThrowException ("No child nodes allowed here.", section);
49
50                         string attvalue = AttValue ("requestEncoding", section, true);
51                         if (attvalue != null)
52                                 config.RequestEncoding = GetEncoding (section, "requestEncoding", attvalue);
53
54                         attvalue = AttValue ("responseEncoding", section, true);
55                         if (attvalue != null)
56                                 config.ResponseEncoding = GetEncoding (section, "responseEncoding", attvalue);
57
58                         attvalue = AttValue ("fileEncoding", section, true);
59                         if (attvalue != null)
60                                 config.FileEncoding = GetEncoding (section, "fileEncoding", attvalue);
61
62                         attvalue = AttValue ("culture", section, true);
63                         if (attvalue != null)
64                                 config.Culture = GetCulture (section, "culture", attvalue);
65
66                         attvalue = AttValue ("uiCulture", section, true);
67                         if (attvalue != null)
68                                 config.UICulture = GetCulture (section, "uiCulture", attvalue);
69
70                         if (section.Attributes == null || section.Attributes.Count != 0)
71                                 ThrowException ("Unknown attribute(s).", section);
72
73                         return config;
74                 }
75
76                 static Encoding GetEncoding (XmlNode section, string att, string enc)
77                 {
78                         Encoding encoding = null;
79                         try {
80                                 switch (enc.ToLower ()) {
81                                 case "utf-16le":
82                                 case "utf-16":
83                                 case "ucs-2":
84                                 case "unicode":
85                                 case "iso-10646-ucs-2":
86                                         encoding = new UnicodeEncoding (false, true);
87                                         break;
88                                 case "utf-16be":
89                                 case "unicodefffe":
90                                         encoding = new UnicodeEncoding (true, true);
91                                         break;
92                                 case "utf-8":
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);
98                                         break;
99                                 default:
100                                         encoding = Encoding.GetEncoding (enc);
101                                         break;
102                                 }
103                         } catch {
104                                 EncodingFailed (section, att, enc);
105                                 encoding = new UTF8Encoding (false, false);
106                         }
107
108                         return encoding;
109                 }
110                 
111                 static CultureInfo GetCulture (XmlNode section, string att, string cul)
112                 {
113                         CultureInfo culture = null;
114                         try {
115                                 culture = new CultureInfo (cul);
116                         } catch {
117                                 CultureFailed (section, att, cul);
118                                 culture = new CultureInfo (0x007f); // Invariant
119                         }
120
121                         return culture;
122                 }
123                 
124                 static void EncodingFailed (XmlNode section, string att, string enc)
125                 {
126                         if (encoding_warning)
127                                 return;
128
129                         encoding_warning = true;
130                         Console.WriteLine ("Encoding {1} cannot be loaded.\n" +
131                                            "{0}=\"{1}\"\n", att, enc);
132                 }
133
134                 static void CultureFailed (XmlNode section, string att, string cul)
135                 {
136                         if (culture_warning)
137                                 return;
138
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);
142                 }
143
144                 // A few methods to save some typing
145                 static string AttValue (string name, XmlNode node, bool optional)
146                 {
147                         return HandlersUtil.ExtractAttributeValue (name, node, optional);
148                 }
149
150                 static void ThrowException (string message, XmlNode node)
151                 {
152                         HandlersUtil.ThrowException (message, node);
153                 }
154                 //
155         }
156 }
157