New test.
[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 using System.Globalization;
35
36 namespace System.Web.Configuration
37 {
38         class HttpHandlersSectionHandler : IConfigurationSectionHandler
39         {
40                 public virtual object Create (object parent, object configContext, XmlNode section)
41                 {
42                         HandlerFactoryConfiguration mapper;
43                         
44                         if (parent is HandlerFactoryConfiguration)
45                                 mapper = new HandlerFactoryConfiguration ((HandlerFactoryConfiguration) parent);
46                         else
47                                 mapper = new HandlerFactoryConfiguration (null);
48                         
49                         if (section.Attributes != null && section.Attributes.Count != 0)
50                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
51
52                         XmlNodeList httpHandlers = section.ChildNodes;
53                         foreach (XmlNode child in httpHandlers) {
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 != null && child.Attributes.Count != 0)
64                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
65
66                                         mapper.Clear ();
67                                         continue;
68                                 }
69                                         
70                                 string verb = HandlersUtil.ExtractAttributeValue ("verb", child);
71                                 string path = HandlersUtil.ExtractAttributeValue ("path", child);
72                                 string validateStr = HandlersUtil.ExtractAttributeValue ("validate", child, true);
73                                 bool validate;
74                                 if (validateStr == null) {
75                                         validate = true;
76                                 } else {
77 #if NET_2_0
78                                         validate = "true" == validateStr.ToLower (CultureInfo.InvariantCulture);
79                                         if (!validate && "false" != validateStr.ToLower (CultureInfo.InvariantCulture))
80 #else
81                                         validate = validateStr == "true";
82                                         if (!validate && validateStr != "false")
83 #endif
84                                                 HandlersUtil.ThrowException (
85                                                                 "Invalid value for validate attribute.", child);
86                                 }
87
88                                 if (name == "add") {
89                                         string type = HandlersUtil.ExtractAttributeValue ("type", child);
90                                         if (child.Attributes != null && child.Attributes.Count != 0)
91                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
92
93                                         mapper.Add (verb, path, type, validate);
94                                         continue;
95                                 }
96
97                                 if (name == "remove") {
98                                         if (child.Attributes != null && child.Attributes.Count != 0)
99                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
100
101                                         if (validate && false == mapper.Remove (verb, path))
102                                                 HandlersUtil.ThrowException ("There's no mapping to remove", child);
103                                         
104                                         continue;
105                                 }
106                                 HandlersUtil.ThrowException ("Unexpected element", child);
107                         }
108
109                         return mapper;
110                 }
111         }
112
113         internal class HandlersUtil
114         {
115                 private HandlersUtil ()
116                 {
117                 }
118
119                 static internal string ExtractAttributeValue (string attKey, XmlNode node)
120                 {
121                         return ExtractAttributeValue (attKey, node, false);
122                 }
123                         
124                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
125                 {
126                         return ExtractAttributeValue (attKey, node, optional, false);
127                 }
128                 
129                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
130                                                               bool allowEmpty)
131                 {
132                         if (node.Attributes == null) {
133                                 if (optional)
134                                         return null;
135
136                                 ThrowException ("Required attribute not found: " + attKey, node);
137                         }
138
139                         XmlNode att = node.Attributes.RemoveNamedItem (attKey);
140                         if (att == null) {
141                                 if (optional)
142                                         return null;
143                                 ThrowException ("Required attribute not found: " + attKey, node);
144                         }
145
146                         string value = att.Value;
147                         if (!allowEmpty && value == String.Empty) {
148                                 string opt = optional ? "Optional" : "Required";
149                                 ThrowException (opt + " attribute is empty: " + attKey, node);
150                         }
151
152                         return value;
153                 }
154
155                 static internal void ThrowException (string msg, XmlNode node)
156                 {
157                         if (node != null && node.Name != String.Empty)
158                                 msg = msg + " (node name: " + node.Name + ") ";
159                         throw new ConfigurationException (msg, node);
160                 }
161         }
162 }
163