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;
34 using System.Globalization;
36 namespace System.Web.Configuration
38 class HttpHandlersSectionHandler : IConfigurationSectionHandler
40 public virtual object Create (object parent, object configContext, XmlNode section)
42 HandlerFactoryConfiguration mapper;
44 if (parent is HandlerFactoryConfiguration)
45 mapper = new HandlerFactoryConfiguration ((HandlerFactoryConfiguration) parent);
47 mapper = new HandlerFactoryConfiguration (null);
49 if (section.Attributes != null && section.Attributes.Count != 0)
50 HandlersUtil.ThrowException ("Unrecognized attribute", section);
52 XmlNodeList httpHandlers = section.ChildNodes;
53 foreach (XmlNode child in httpHandlers) {
54 XmlNodeType ntype = child.NodeType;
55 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
58 if (ntype != XmlNodeType.Element)
59 HandlersUtil.ThrowException ("Only elements allowed", child);
61 string name = child.Name;
62 if (name == "clear") {
63 if (child.Attributes != null && child.Attributes.Count != 0)
64 HandlersUtil.ThrowException ("Unrecognized attribute", child);
70 string verb = HandlersUtil.ExtractAttributeValue ("verb", child);
71 string path = HandlersUtil.ExtractAttributeValue ("path", child);
72 string validateStr = HandlersUtil.ExtractAttributeValue ("validate", child, true);
74 if (validateStr == null) {
78 validate = "true" == validateStr.ToLower (CultureInfo.InvariantCulture);
79 if (!validate && "false" != validateStr.ToLower (CultureInfo.InvariantCulture))
81 validate = validateStr == "true";
82 if (!validate && validateStr != "false")
84 HandlersUtil.ThrowException (
85 "Invalid value for validate attribute.", child);
89 string type = HandlersUtil.ExtractAttributeValue ("type", child);
90 if (child.Attributes != null && child.Attributes.Count != 0)
91 HandlersUtil.ThrowException ("Unrecognized attribute", child);
93 mapper.Add (verb, path, type, validate);
97 if (name == "remove") {
98 if (child.Attributes != null && child.Attributes.Count != 0)
99 HandlersUtil.ThrowException ("Unrecognized attribute", child);
101 if (validate && false == mapper.Remove (verb, path))
102 HandlersUtil.ThrowException ("There's no mapping to remove", child);
106 HandlersUtil.ThrowException ("Unexpected element", child);
113 internal class HandlersUtil
115 private HandlersUtil ()
119 static internal string ExtractAttributeValue (string attKey, XmlNode node)
121 return ExtractAttributeValue (attKey, node, false);
124 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
126 return ExtractAttributeValue (attKey, node, optional, false);
129 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
132 if (node.Attributes == null) {
136 ThrowException ("Required attribute not found: " + attKey, node);
139 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
143 ThrowException ("Required attribute not found: " + attKey, node);
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);
155 static internal void ThrowException (string msg, XmlNode node)
157 if (node != null && node.Name != String.Empty)
158 msg = msg + " (node name: " + node.Name + ") ";
159 throw new ConfigurationException (msg, node);