2005-10-29 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration / HttpModulesConfigurationHandler.cs
1 //
2 // System.Web.Configuration.HttpModulesConfigurationHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 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.Xml;
33 using System.Web.Security;
34
35 namespace System.Web.Configuration
36 {
37         class HttpModulesConfigurationHandler : IConfigurationSectionHandler
38         {
39                 public virtual object Create (object parent, object configContext, XmlNode section)
40                 {
41                         ModulesConfiguration mapper;
42                         
43                         if (parent is ModulesConfiguration)
44                                 mapper = new ModulesConfiguration ((ModulesConfiguration) parent);
45                         else
46                                 mapper = new ModulesConfiguration (null);
47                         
48                         if (section.Attributes != null && section.Attributes.Count != 0)
49                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
50
51                         XmlNodeList httpModules = section.ChildNodes;
52
53                         foreach (XmlNode child in httpModules) {
54                                 XmlNodeType ntype = child.NodeType;
55                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
56                                         continue;
57
58                                 if (ntype != XmlNodeType.Element)
59                                         HandlersUtil.ThrowException ("Only elements allowed", child);
60
61                                 string name = child.Name;
62                                 if (name == "clear") {
63                                         if (child.Attributes.Count != 0)
64                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
65
66                                         mapper.Clear ();
67                                         continue;
68                                 }
69
70                                 string name_attr = HandlersUtil.ExtractAttributeValue ("name", child);
71                                 if (name == "add") {
72                                         string type = HandlersUtil.ExtractAttributeValue ("type", child);
73                                         if (child.Attributes.Count != 0)
74                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
75
76                                         // FIXME: gotta remove this. Just here to make it work with my local config
77                                         if (type.StartsWith ("System.Web.Mobile"))
78                                                 continue;
79
80                                         mapper.Add (name_attr, type);
81                                         continue;
82                                 }
83
84                                 if (name == "remove") {
85                                         if (child.Attributes.Count != 0)
86                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
87
88                                         if (mapper.Remove (name_attr) == null)
89                                                 HandlersUtil.ThrowException ("Module not loaded", child);
90                                         continue;
91                                 }
92
93                                 HandlersUtil.ThrowException ("Unrecognized element", child);
94                         }
95
96                         mapper.Add ("DefaultAuthentication", typeof (DefaultAuthenticationModule));
97                         
98                         return mapper;
99                 }
100         }
101 }
102