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;
33 ArrayList extensionTypes = new ArrayList();
34 ArrayList extensionImporterTypes = new ArrayList();
35 ArrayList extensionReflectorTypes = new ArrayList();
36 ArrayList formatExtensionTypes = new ArrayList();
38 public WSConfig (WSConfig parent, object context)
43 protocols = parent.protocols;
44 wsdlHelpPage = parent.wsdlHelpPage;
45 if (wsdlHelpPage != null)
46 filePath = parent.filePath;
48 filePath = context as string;
51 static WSProtocol ParseProtocol (string protoName, out string error)
56 proto = (WSProtocol) Enum.Parse (typeof (WSProtocol), protoName);
58 error = "Invalid protocol name";
65 // Methods to modify configuration values
66 public bool AddProtocol (string protoName, out string error)
68 if (protoName == "All") {
69 error = "Invalid protocol name";
73 WSProtocol proto = ParseProtocol (protoName, out error);
81 public bool RemoveProtocol (string protoName, out string error)
83 if (protoName == "All") {
84 error = "Invalid protocol name";
88 WSProtocol proto = ParseProtocol (protoName, out error);
96 public void ClearProtocol ()
101 // Methods to query/get configuration
102 public static bool IsSupported (WSProtocol proto)
104 return ((Instance.protocols & proto) == proto && (proto != 0) && (proto != WSProtocol.All));
108 public string WsdlHelpPage {
109 get { return wsdlHelpPage; }
110 set { wsdlHelpPage = value; }
113 public string ConfigFilePath {
114 get { return filePath; }
115 set { filePath = value; }
118 static public WSConfig Instance {
120 //TODO: use HttpContext to get the configuration
121 if (instance != null)
124 lock (typeof (WSConfig)) {
125 if (instance != null)
128 instance = (WSConfig) ConfigurationSettings.GetConfig ("system.web/webServices");
135 public ArrayList ExtensionTypes {
136 get { return extensionTypes; }
139 public ArrayList ExtensionImporterTypes {
140 get { return extensionImporterTypes; }
143 public ArrayList ExtensionReflectorTypes {
144 get { return extensionReflectorTypes; }
147 public ArrayList FormatExtensionTypes {
148 get { return formatExtensionTypes; }
153 enum WSExtensionGroup
159 class WSExtensionConfig
163 WSExtensionGroup group;
165 public Exception SetType (string typeName)
167 Exception exc = null;
170 type = Type.GetType (typeName, true);
171 } catch (Exception e) {
178 public Exception SetPriority (string prio)
180 if (prio == null || prio == "")
183 Exception exc = null;
185 priority = Int32.Parse (prio);
186 } catch (Exception e) {
193 public Exception SetGroup (string grp)
195 if (grp == null || grp == "")
198 Exception exc = null;
200 group = (WSExtensionGroup) Int32.Parse (grp);
201 if (group < WSExtensionGroup.High || group > WSExtensionGroup.Low)
202 throw new ArgumentOutOfRangeException ("group", "Must be 0 or 1");
203 } catch (Exception e) {
215 public int Priority {
216 get { return priority; }
219 public WSExtensionGroup Group {
220 get { return group; }
224 class WebServicesConfigurationSectionHandler : IConfigurationSectionHandler
226 public object Create (object parent, object context, XmlNode section)
228 WSConfig config = new WSConfig (parent as WSConfig, context);
230 if (section.Attributes != null && section.Attributes.Count != 0)
231 ThrowException ("Unrecognized attribute", section);
233 XmlNodeList nodes = section.ChildNodes;
234 foreach (XmlNode child in nodes) {
235 XmlNodeType ntype = child.NodeType;
236 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
239 if (ntype != XmlNodeType.Element)
240 ThrowException ("Only elements allowed", child);
242 string name = child.Name;
243 if (name == "protocols") {
244 ConfigProtocols (child, config);
248 if (name == "soapExtensionTypes") {
249 ConfigSoapExtensionTypes (child, config.ExtensionTypes);
253 if (name == "soapExtensionReflectorTypes") {
254 ConfigSoapExtensionTypes (child, config.ExtensionReflectorTypes);
258 if (name == "soapExtensionImporterTypes") {
259 ConfigSoapExtensionTypes (child, config.ExtensionImporterTypes);
263 if (name == "serviceDescriptionFormatExtensionTypes") {
264 ConfigFormatExtensionTypes (child, config);
268 if (name == "wsdlHelpGenerator") {
269 string href = AttValue ("href", child, false);
270 if (child.Attributes != null && child.Attributes.Count != 0)
271 HandlersUtil.ThrowException ("Unrecognized attribute", child);
273 config.ConfigFilePath = context as string;
274 config.WsdlHelpPage = href;
278 ThrowException ("Unexpected element", child);
284 static void ConfigProtocols (XmlNode section, WSConfig config)
286 if (section.Attributes != null && section.Attributes.Count != 0)
287 ThrowException ("Unrecognized attribute", section);
289 XmlNodeList nodes = section.ChildNodes;
290 foreach (XmlNode child in nodes) {
291 XmlNodeType ntype = child.NodeType;
292 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
295 if (ntype != XmlNodeType.Element)
296 ThrowException ("Only elements allowed", child);
298 string name = child.Name;
301 string protoName = AttValue ("name", child, false);
302 if (child.Attributes != null && child.Attributes.Count != 0)
303 HandlersUtil.ThrowException ("Unrecognized attribute", child);
305 if (!config.AddProtocol (protoName, out error))
306 ThrowException (error, child);
311 if (name == "remove") {
312 string protoName = AttValue ("name", child, false);
313 if (child.Attributes != null && child.Attributes.Count != 0)
314 HandlersUtil.ThrowException ("Unrecognized attribute", child);
316 if (!config.RemoveProtocol (protoName, out error))
317 ThrowException (error, child);
322 if (name == "clear") {
323 if (child.Attributes != null && child.Attributes.Count != 0)
324 HandlersUtil.ThrowException ("Unrecognized attribute", child);
326 config.ClearProtocol ();
330 ThrowException ("Unexpected element", child);
334 static void ConfigSoapExtensionTypes (XmlNode section, ArrayList extensions)
336 if (section.Attributes != null && section.Attributes.Count != 0)
337 ThrowException ("Unrecognized attribute", section);
339 XmlNodeList nodes = section.ChildNodes;
340 foreach (XmlNode child in nodes) {
341 XmlNodeType ntype = child.NodeType;
342 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
345 if (ntype != XmlNodeType.Element)
346 ThrowException ("Only elements allowed", child);
348 string name = child.Name;
351 string seType = AttValue ("type", child, false);
352 string priority = AttValue ("priority", child);
353 string group = AttValue ("group", child);
354 if (child.Attributes != null && child.Attributes.Count != 0)
355 HandlersUtil.ThrowException ("Unrecognized attribute", child);
357 WSExtensionConfig wse = new WSExtensionConfig ();
358 Exception e = wse.SetType (seType);
360 ThrowException (e.Message, child);
362 e = wse.SetPriority (priority);
364 ThrowException (e.Message, child);
366 e = wse.SetGroup (group);
368 ThrowException (e.Message, child);
370 extensions.Add (wse);
374 ThrowException ("Unexpected element", child);
378 static void ConfigFormatExtensionTypes (XmlNode section, WSConfig config)
380 if (section.Attributes != null && section.Attributes.Count != 0)
381 ThrowException ("Unrecognized attribute", section);
383 XmlNodeList nodes = section.ChildNodes;
384 foreach (XmlNode child in nodes) {
385 XmlNodeType ntype = child.NodeType;
386 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
389 if (ntype != XmlNodeType.Element)
390 ThrowException ("Only elements allowed", child);
392 string name = child.Name;
395 string typeName = AttValue ("name", child, false);
396 if (child.Attributes != null && child.Attributes.Count != 0)
397 HandlersUtil.ThrowException ("Unrecognized attribute", child);
400 config.FormatExtensionTypes.Add (Type.GetType (typeName, true));
401 } catch (Exception e) {
402 ThrowException (e.Message, child);
407 ThrowException ("Unexpected element", child);
411 // To save some typing...
412 static string AttValue (string name, XmlNode node, bool optional)
414 return HandlersUtil.ExtractAttributeValue (name, node, optional);
417 static string AttValue (string name, XmlNode node)
419 return HandlersUtil.ExtractAttributeValue (name, node, true);
422 static void ThrowException (string message, XmlNode node)
424 HandlersUtil.ThrowException (message, node);
431 private HandlersUtil ()
435 static internal string ExtractAttributeValue (string attKey, XmlNode node)
437 return ExtractAttributeValue (attKey, node, false);
440 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
442 if (node.Attributes == null) {
446 ThrowException ("Required attribute not found: " + attKey, node);
449 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
453 ThrowException ("Required attribute not found: " + attKey, node);
456 string value = att.Value;
457 if (value == String.Empty) {
458 string opt = optional ? "Optional" : "Required";
459 ThrowException (opt + " attribute is empty: " + attKey, node);
465 static internal void ThrowException (string msg, XmlNode node)
467 if (node != null && node.Name != String.Empty)
468 msg = msg + " (node name: " + node.Name + ") ";
469 throw new ConfigurationException (msg, node);