2 // System.Web.Configuration.HttpHandlersSectionHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 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.Collections;
32 using System.Configuration;
35 namespace System.Web.Configuration
37 class HttpHandlersSectionHandler : IConfigurationSectionHandler
39 public virtual object Create (object parent, object configContext, XmlNode section)
41 HandlerFactoryConfiguration mapper;
43 if (parent is HandlerFactoryConfiguration)
44 mapper = new HandlerFactoryConfiguration ((HandlerFactoryConfiguration) parent);
46 mapper = new HandlerFactoryConfiguration (null);
48 if (section.Attributes != null && section.Attributes.Count != 0)
49 HandlersUtil.ThrowException ("Unrecognized attribute", section);
51 XmlNodeList httpHandlers = section.ChildNodes;
52 foreach (XmlNode child in httpHandlers) {
53 XmlNodeType ntype = child.NodeType;
54 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
57 if (ntype != XmlNodeType.Element)
58 HandlersUtil.ThrowException ("Only elements allowed", child);
60 string name = child.Name;
61 if (name == "clear") {
62 if (child.Attributes != null && child.Attributes.Count != 0)
63 HandlersUtil.ThrowException ("Unrecognized attribute", child);
69 string verb = HandlersUtil.ExtractAttributeValue ("verb", child);
70 string path = HandlersUtil.ExtractAttributeValue ("path", child);
71 string validateStr = HandlersUtil.ExtractAttributeValue ("validate", child, true);
73 if (validateStr == null) {
76 validate = validateStr == "true";
77 if (!validate && validateStr != "false")
78 HandlersUtil.ThrowException (
79 "Invalid value for validate attribute.", child);
83 string type = HandlersUtil.ExtractAttributeValue ("type", child);
84 if (child.Attributes != null && child.Attributes.Count != 0)
85 HandlersUtil.ThrowException ("Unrecognized attribute", child);
87 mapper.Add (verb, path, type, validate);
91 if (name == "remove") {
92 if (child.Attributes != null && child.Attributes.Count != 0)
93 HandlersUtil.ThrowException ("Unrecognized attribute", child);
95 if (validate && mapper.Remove (verb, path) == null)
96 HandlersUtil.ThrowException ("There's no mapping to remove", child);
100 HandlersUtil.ThrowException ("Unexpected element", child);
107 internal class HandlersUtil
109 private HandlersUtil ()
113 static internal string ExtractAttributeValue (string attKey, XmlNode node)
115 return ExtractAttributeValue (attKey, node, false);
118 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
120 return ExtractAttributeValue (attKey, node, optional, false);
123 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
126 if (node.Attributes == null) {
130 ThrowException ("Required attribute not found: " + attKey, node);
133 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
137 ThrowException ("Required attribute not found: " + attKey, node);
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);
149 static internal void ThrowException (string msg, XmlNode node)
151 if (node != null && node.Name != String.Empty)
152 msg = msg + " (node name: " + node.Name + ") ";
153 throw new ConfigurationException (msg, node);