2 // System.Web.Services.Configuration.WebServicesConfigurationSectionHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
11 using System.Configuration;
14 namespace System.Web.Services.Configuration
22 Documentation = 1 << 3,
31 public WSConfig (WSConfig parent)
36 protocols = parent.protocols;
37 wsdlHelpPage = parent.wsdlHelpPage;
40 static WSProtocol ParseProtocol (string protoName, out string error)
45 proto = (WSProtocol) Enum.Parse (typeof (WSProtocol), protoName);
47 error = "Invalid protocol name";
54 // Methods to modify configuration values
55 public bool AddProtocol (string protoName, out string error)
57 if (protoName == "All") {
58 error = "Invalid protocol name";
62 WSProtocol proto = ParseProtocol (protoName, out error);
66 if ((protocols & proto) != 0) {
67 error = "Protocol already added";
75 // Methods to query/get configuration
76 public bool IsSupported (WSProtocol proto)
78 return (proto & WSProtocol.All) == proto;
82 public string WsdlHelpPage {
83 get { return wsdlHelpPage; }
84 set { wsdlHelpPage = value; }
89 class WebServicesConfigurationSectionHandler : IConfigurationSectionHandler
91 [MonoTODO("Some nodes not supported, see below")]
92 public object Create (object parent, object context, XmlNode section)
94 WSConfig config = new WSConfig (parent as WSConfig);
96 if (section.Attributes != null && section.Attributes.Count != 0)
97 ThrowException ("Unrecognized attribute", section);
99 XmlNodeList nodes = section.ChildNodes;
100 foreach (XmlNode child in nodes) {
101 XmlNodeType ntype = child.NodeType;
102 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
105 if (ntype != XmlNodeType.Element)
106 ThrowException ("Only elements allowed", child);
108 string name = child.Name;
109 if (name == "protocols") {
110 ConfigProtocols (child, config);
114 if (name == "soapExtensionTypes") {
115 //TODO: Not supported by now
119 if (name == "soapExtensionReflectorTypes") {
120 //TODO: Not supported by now
124 if (name == "soapExtensionImporterTypes") {
125 //TODO: Not supported by now
129 if (name == "serviceDescriptionFormatExtensionTypes") {
130 //TODO: Not supported by now
134 if (name == "wsdlHelpGenerator") {
135 string href = AttValue ("href", child, false);
136 if (child.Attributes != null && child.Attributes.Count != 0)
137 HandlersUtil.ThrowException ("Unrecognized attribute", child);
139 config.WsdlHelpPage = href;
143 ThrowException ("Unexpected element", child);
149 static void ConfigProtocols (XmlNode section, WSConfig config)
151 if (section.Attributes != null && section.Attributes.Count != 0)
152 ThrowException ("Unrecognized attribute", section);
154 XmlNodeList nodes = section.ChildNodes;
155 foreach (XmlNode child in nodes) {
156 XmlNodeType ntype = child.NodeType;
157 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
160 if (ntype != XmlNodeType.Element)
161 ThrowException ("Only elements allowed", child);
163 string name = child.Name;
166 string protoName = AttValue ("name", child, false);
167 if (child.Attributes != null && child.Attributes.Count != 0)
168 HandlersUtil.ThrowException ("Unrecognized attribute", child);
170 if (!config.AddProtocol (protoName, out error))
171 ThrowException (error, child);
176 ThrowException ("Unexpected element", child);
180 // To save some typing...
181 static string AttValue (string name, XmlNode node, bool optional)
183 return HandlersUtil.ExtractAttributeValue (name, node, optional);
186 static string AttValue (string name, XmlNode node)
188 return HandlersUtil.ExtractAttributeValue (name, node, true);
191 static void ThrowException (string message, XmlNode node)
193 HandlersUtil.ThrowException (message, node);
200 private HandlersUtil ()
204 static internal string ExtractAttributeValue (string attKey, XmlNode node)
206 return ExtractAttributeValue (attKey, node, false);
209 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
211 if (node.Attributes == null) {
215 ThrowException ("Required attribute not found: " + attKey, node);
218 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
222 ThrowException ("Required attribute not found: " + attKey, node);
225 string value = att.Value;
226 if (value == String.Empty) {
227 string opt = optional ? "Optional" : "Required";
228 ThrowException (opt + " attribute is empty: " + attKey, node);
234 static internal void ThrowException (string msg, XmlNode node)
236 if (node != null && node.Name != String.Empty)
237 msg = msg + " (node name: " + node.Name + ") ";
238 throw new ConfigurationException (msg, node);