2005-11-19 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration / HttpHandlersSectionHandler.cs
1 //
2 // System.Web.Configuration.HttpHandlersSectionHandler
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.Collections;
32 using System.Configuration;
33 using System.Xml;
34
35 namespace System.Web.Configuration
36 {
37         class HttpHandlersSectionHandler : IConfigurationSectionHandler
38         {
39                 public virtual object Create (object parent, object configContext, XmlNode section)
40                 {
41                         HandlerFactoryConfiguration mapper;
42                         
43                         if (parent is HandlerFactoryConfiguration)
44                                 mapper = new HandlerFactoryConfiguration ((HandlerFactoryConfiguration) parent);
45                         else
46                                 mapper = new HandlerFactoryConfiguration (null);
47                         
48                         if (section.Attributes != null && section.Attributes.Count != 0)
49                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
50
51                         XmlNodeList httpHandlers = section.ChildNodes;
52                         foreach (XmlNode child in httpHandlers) {
53                                 XmlNodeType ntype = child.NodeType;
54                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
55                                         continue;
56
57                                 if (ntype != XmlNodeType.Element)
58                                         HandlersUtil.ThrowException ("Only elements allowed", child);
59                                 
60                                 string name = child.Name;
61                                 if (name == "clear") {
62                                         if (child.Attributes != null && child.Attributes.Count != 0)
63                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
64
65                                         mapper.Clear ();
66                                         continue;
67                                 }
68                                         
69                                 string verb = HandlersUtil.ExtractAttributeValue ("verb", child);
70                                 string path = HandlersUtil.ExtractAttributeValue ("path", child);
71                                 string validateStr = HandlersUtil.ExtractAttributeValue ("validate", child, true);
72                                 bool validate;
73                                 if (validateStr == null) {
74                                         validate = true;
75                                 } else {
76                                         validate = validateStr == "true";
77                                         if (!validate && validateStr != "false")
78                                                 HandlersUtil.ThrowException (
79                                                                 "Invalid value for validate attribute.", child);
80                                 }
81
82                                 if (name == "add") {
83                                         string type = HandlersUtil.ExtractAttributeValue ("type", child);
84                                         if (child.Attributes != null && child.Attributes.Count != 0)
85                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
86
87                                         mapper.Add (verb, path, type, validate);
88                                         continue;
89                                 }
90
91                                 if (name == "remove") {
92                                         if (child.Attributes != null && child.Attributes.Count != 0)
93                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
94
95                                         if (validate && mapper.Remove (verb, path) == null)
96                                                 HandlersUtil.ThrowException ("There's no mapping to remove", child);
97                                         
98                                         continue;
99                                 }
100                                 HandlersUtil.ThrowException ("Unexpected element", child);
101                         }
102
103                         return mapper;
104                 }
105         }
106
107         internal class HandlersUtil
108         {
109                 private HandlersUtil ()
110                 {
111                 }
112
113                 static internal string ExtractAttributeValue (string attKey, XmlNode node)
114                 {
115                         return ExtractAttributeValue (attKey, node, false);
116                 }
117                         
118                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
119                 {
120                         return ExtractAttributeValue (attKey, node, optional, false);
121                 }
122                 
123                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
124                                                               bool allowEmpty)
125                 {
126                         if (node.Attributes == null) {
127                                 if (optional)
128                                         return null;
129
130                                 ThrowException ("Required attribute not found: " + attKey, node);
131                         }
132
133                         XmlNode att = node.Attributes.RemoveNamedItem (attKey);
134                         if (att == null) {
135                                 if (optional)
136                                         return null;
137                                 ThrowException ("Required attribute not found: " + attKey, node);
138                         }
139
140                         string value = att.Value;
141                         if (!allowEmpty && value == String.Empty) {
142                                 string opt = optional ? "Optional" : "Required";
143                                 ThrowException (opt + " attribute is empty: " + attKey, node);
144                         }
145
146                         return value;
147                 }
148
149                 static internal void ThrowException (string msg, XmlNode node)
150                 {
151                         if (node != null && node.Name != String.Empty)
152                                 msg = msg + " (node name: " + node.Name + ") ";
153                         throw new ConfigurationException (msg, node);
154                 }
155         }
156 }
157