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.Collections;
12 using System.Configuration;
15 namespace System.Web.Services.Configuration
23 Documentation = 1 << 3,
29 static WSConfig instance;
32 ArrayList extensionTypes = new ArrayList();
33 ArrayList extensionImporterTypes = new ArrayList();
34 ArrayList extensionReflectorTypes = new ArrayList();
35 ArrayList formatExtensionTypes = new ArrayList();
37 public WSConfig (WSConfig parent)
42 protocols = parent.protocols;
43 wsdlHelpPage = parent.wsdlHelpPage;
46 static WSProtocol ParseProtocol (string protoName, out string error)
51 proto = (WSProtocol) Enum.Parse (typeof (WSProtocol), protoName);
53 error = "Invalid protocol name";
60 // Methods to modify configuration values
61 public bool AddProtocol (string protoName, out string error)
63 if (protoName == "All") {
64 error = "Invalid protocol name";
68 WSProtocol proto = ParseProtocol (protoName, out error);
76 public bool RemoveProtocol (string protoName, out string error)
78 if (protoName == "All") {
79 error = "Invalid protocol name";
83 WSProtocol proto = ParseProtocol (protoName, out error);
91 public void ClearProtocol ()
96 // Methods to query/get configuration
97 public static bool IsSupported (WSProtocol proto)
99 return ((Instance.protocols & proto) == proto && (proto != 0) && (proto != WSProtocol.All));
103 public string WsdlHelpPage {
104 get { return wsdlHelpPage; }
105 set { wsdlHelpPage = value; }
108 static public WSConfig Instance {
110 //TODO: use HttpContext to get the configuration
111 if (instance != null)
114 lock (typeof (WSConfig)) {
115 if (instance != null)
118 instance = (WSConfig) ConfigurationSettings.GetConfig ("system.web/webServices");
125 public ArrayList ExtensionTypes {
126 get { return extensionTypes; }
129 public ArrayList ExtensionImporterTypes {
130 get { return extensionImporterTypes; }
133 public ArrayList ExtensionReflectorTypes {
134 get { return extensionReflectorTypes; }
137 public ArrayList FormatExtensionTypes {
138 get { return formatExtensionTypes; }
143 enum WSExtensionGroup
149 class WSExtensionConfig
153 WSExtensionGroup group;
155 public Exception SetType (string typeName)
157 Exception exc = null;
160 type = Type.GetType (typeName, true);
161 } catch (Exception e) {
168 public Exception SetPriority (string prio)
170 if (prio == null || prio == "")
173 Exception exc = null;
175 priority = Int32.Parse (prio);
176 } catch (Exception e) {
183 public Exception SetGroup (string grp)
185 if (grp == null || grp == "")
188 Exception exc = null;
190 group = (WSExtensionGroup) Int32.Parse (grp);
191 if (group < WSExtensionGroup.High || group > WSExtensionGroup.Low)
192 throw new ArgumentOutOfRangeException ("group", "Must be 0 or 1");
193 } catch (Exception e) {
205 public int Priority {
206 get { return priority; }
209 public WSExtensionGroup Group {
210 get { return group; }
214 class WebServicesConfigurationSectionHandler : IConfigurationSectionHandler
216 [MonoTODO("Some nodes not supported, see below")]
217 public object Create (object parent, object context, XmlNode section)
219 WSConfig config = new WSConfig (parent as WSConfig);
221 if (section.Attributes != null && section.Attributes.Count != 0)
222 ThrowException ("Unrecognized attribute", section);
224 XmlNodeList nodes = section.ChildNodes;
225 foreach (XmlNode child in nodes) {
226 XmlNodeType ntype = child.NodeType;
227 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
230 if (ntype != XmlNodeType.Element)
231 ThrowException ("Only elements allowed", child);
233 string name = child.Name;
234 if (name == "protocols") {
235 ConfigProtocols (child, config);
239 if (name == "soapExtensionTypes") {
240 ConfigSoapExtensionTypes (child, config.ExtensionTypes);
244 if (name == "soapExtensionReflectorTypes") {
245 ConfigSoapExtensionTypes (child, config.ExtensionReflectorTypes);
249 if (name == "soapExtensionImporterTypes") {
250 ConfigSoapExtensionTypes (child, config.ExtensionImporterTypes);
254 if (name == "serviceDescriptionFormatExtensionTypes") {
255 ConfigFormatExtensionTypes (child, config);
259 if (name == "wsdlHelpGenerator") {
260 string href = AttValue ("href", child, false);
261 if (child.Attributes != null && child.Attributes.Count != 0)
262 HandlersUtil.ThrowException ("Unrecognized attribute", child);
264 config.WsdlHelpPage = href;
268 ThrowException ("Unexpected element", child);
274 static void ConfigProtocols (XmlNode section, WSConfig config)
276 if (section.Attributes != null && section.Attributes.Count != 0)
277 ThrowException ("Unrecognized attribute", section);
279 XmlNodeList nodes = section.ChildNodes;
280 foreach (XmlNode child in nodes) {
281 XmlNodeType ntype = child.NodeType;
282 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
285 if (ntype != XmlNodeType.Element)
286 ThrowException ("Only elements allowed", child);
288 string name = child.Name;
291 string protoName = AttValue ("name", child, false);
292 if (child.Attributes != null && child.Attributes.Count != 0)
293 HandlersUtil.ThrowException ("Unrecognized attribute", child);
295 if (!config.AddProtocol (protoName, out error))
296 ThrowException (error, child);
301 if (name == "remove") {
302 string protoName = AttValue ("name", child, false);
303 if (child.Attributes != null && child.Attributes.Count != 0)
304 HandlersUtil.ThrowException ("Unrecognized attribute", child);
306 if (!config.RemoveProtocol (protoName, out error))
307 ThrowException (error, child);
312 if (name == "clear") {
313 if (child.Attributes != null && child.Attributes.Count != 0)
314 HandlersUtil.ThrowException ("Unrecognized attribute", child);
316 config.ClearProtocol ();
320 ThrowException ("Unexpected element", child);
324 static void ConfigSoapExtensionTypes (XmlNode section, ArrayList extensions)
326 if (section.Attributes != null && section.Attributes.Count != 0)
327 ThrowException ("Unrecognized attribute", section);
329 XmlNodeList nodes = section.ChildNodes;
330 foreach (XmlNode child in nodes) {
331 XmlNodeType ntype = child.NodeType;
332 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
335 if (ntype != XmlNodeType.Element)
336 ThrowException ("Only elements allowed", child);
338 string name = child.Name;
341 string seType = AttValue ("type", child, false);
342 string priority = AttValue ("priority", child);
343 string group = AttValue ("group", child);
344 if (child.Attributes != null && child.Attributes.Count != 0)
345 HandlersUtil.ThrowException ("Unrecognized attribute", child);
347 WSExtensionConfig wse = new WSExtensionConfig ();
348 Exception e = wse.SetType (seType);
350 ThrowException (e.Message, child);
352 e = wse.SetPriority (priority);
354 ThrowException (e.Message, child);
356 e = wse.SetGroup (group);
358 ThrowException (e.Message, child);
360 extensions.Add (wse);
364 ThrowException ("Unexpected element", child);
368 static void ConfigFormatExtensionTypes (XmlNode section, WSConfig config)
370 if (section.Attributes != null && section.Attributes.Count != 0)
371 ThrowException ("Unrecognized attribute", section);
373 XmlNodeList nodes = section.ChildNodes;
374 foreach (XmlNode child in nodes) {
375 XmlNodeType ntype = child.NodeType;
376 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
379 if (ntype != XmlNodeType.Element)
380 ThrowException ("Only elements allowed", child);
382 string name = child.Name;
385 string typeName = AttValue ("name", child, false);
386 if (child.Attributes != null && child.Attributes.Count != 0)
387 HandlersUtil.ThrowException ("Unrecognized attribute", child);
390 config.FormatExtensionTypes.Add (Type.GetType (typeName, true));
391 } catch (Exception e) {
392 ThrowException (e.Message, child);
397 ThrowException ("Unexpected element", child);
401 // To save some typing...
402 static string AttValue (string name, XmlNode node, bool optional)
404 return HandlersUtil.ExtractAttributeValue (name, node, optional);
407 static string AttValue (string name, XmlNode node)
409 return HandlersUtil.ExtractAttributeValue (name, node, true);
412 static void ThrowException (string message, XmlNode node)
414 HandlersUtil.ThrowException (message, node);
421 private HandlersUtil ()
425 static internal string ExtractAttributeValue (string attKey, XmlNode node)
427 return ExtractAttributeValue (attKey, node, false);
430 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
432 if (node.Attributes == null) {
436 ThrowException ("Required attribute not found: " + attKey, node);
439 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
443 ThrowException ("Required attribute not found: " + attKey, node);
446 string value = att.Value;
447 if (value == String.Empty) {
448 string opt = optional ? "Optional" : "Required";
449 ThrowException (opt + " attribute is empty: " + attKey, node);
455 static internal void ThrowException (string msg, XmlNode node)
457 if (node != null && node.Name != String.Empty)
458 msg = msg + " (node name: " + node.Name + ") ";
459 throw new ConfigurationException (msg, node);